php_packet_soap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Brad Lafountain <rodif_bl@yahoo.com> |
  16. | Shane Caraveo <shane@caraveo.com> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "php_soap.h"
  21. /* SOAP client calls this function to parse response from SOAP server */
  22. int parse_packet_soap(zval *this_ptr, char *buffer, int buffer_size, sdlFunctionPtr fn, char *fn_name, zval *return_value, zval *soap_headers)
  23. {
  24. char* envelope_ns = NULL;
  25. xmlDocPtr response;
  26. xmlNodePtr trav, env, head, body, resp, cur, fault;
  27. xmlAttrPtr attr;
  28. int param_count = 0;
  29. int soap_version = SOAP_1_1;
  30. HashTable *hdrs = NULL;
  31. ZVAL_NULL(return_value);
  32. /* Response for one-way opearation */
  33. if (buffer_size == 0) {
  34. return TRUE;
  35. }
  36. /* Parse XML packet */
  37. response = soap_xmlParseMemory(buffer, buffer_size);
  38. if (!response) {
  39. add_soap_fault(this_ptr, "Client", "looks like we got no XML document", NULL, NULL);
  40. return FALSE;
  41. }
  42. if (xmlGetIntSubset(response) != NULL) {
  43. add_soap_fault(this_ptr, "Client", "DTD are not supported by SOAP", NULL, NULL);
  44. xmlFreeDoc(response);
  45. return FALSE;
  46. }
  47. /* Get <Envelope> element */
  48. env = NULL;
  49. trav = response->children;
  50. while (trav != NULL) {
  51. if (trav->type == XML_ELEMENT_NODE) {
  52. if (env == NULL && node_is_equal_ex(trav,"Envelope",SOAP_1_1_ENV_NAMESPACE)) {
  53. env = trav;
  54. envelope_ns = SOAP_1_1_ENV_NAMESPACE;
  55. soap_version = SOAP_1_1;
  56. } else if (env == NULL && node_is_equal_ex(trav,"Envelope",SOAP_1_2_ENV_NAMESPACE)) {
  57. env = trav;
  58. envelope_ns = SOAP_1_2_ENV_NAMESPACE;
  59. soap_version = SOAP_1_2;
  60. } else {
  61. add_soap_fault(this_ptr, "VersionMismatch", "Wrong Version", NULL, NULL);
  62. xmlFreeDoc(response);
  63. return FALSE;
  64. }
  65. }
  66. trav = trav->next;
  67. }
  68. if (env == NULL) {
  69. add_soap_fault(this_ptr, "Client", "looks like we got XML without \"Envelope\" element", NULL, NULL);
  70. xmlFreeDoc(response);
  71. return FALSE;
  72. }
  73. attr = env->properties;
  74. while (attr != NULL) {
  75. if (attr->ns == NULL) {
  76. add_soap_fault(this_ptr, "Client", "A SOAP Envelope element cannot have non Namespace qualified attributes", NULL, NULL);
  77. xmlFreeDoc(response);
  78. return FALSE;
  79. } else if (attr_is_equal_ex(attr,"encodingStyle",SOAP_1_2_ENV_NAMESPACE)) {
  80. if (soap_version == SOAP_1_2) {
  81. add_soap_fault(this_ptr, "Client", "encodingStyle cannot be specified on the Envelope", NULL, NULL);
  82. xmlFreeDoc(response);
  83. return FALSE;
  84. } else if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE) != 0) {
  85. add_soap_fault(this_ptr, "Client", "Unknown data encoding style", NULL, NULL);
  86. xmlFreeDoc(response);
  87. return FALSE;
  88. }
  89. }
  90. attr = attr->next;
  91. }
  92. /* Get <Header> element */
  93. head = NULL;
  94. trav = env->children;
  95. while (trav != NULL && trav->type != XML_ELEMENT_NODE) {
  96. trav = trav->next;
  97. }
  98. if (trav != NULL && node_is_equal_ex(trav,"Header",envelope_ns)) {
  99. head = trav;
  100. trav = trav->next;
  101. }
  102. /* Get <Body> element */
  103. body = NULL;
  104. while (trav != NULL && trav->type != XML_ELEMENT_NODE) {
  105. trav = trav->next;
  106. }
  107. if (trav != NULL && node_is_equal_ex(trav,"Body",envelope_ns)) {
  108. body = trav;
  109. trav = trav->next;
  110. }
  111. while (trav != NULL && trav->type != XML_ELEMENT_NODE) {
  112. trav = trav->next;
  113. }
  114. if (body == NULL) {
  115. add_soap_fault(this_ptr, "Client", "Body must be present in a SOAP envelope", NULL, NULL);
  116. xmlFreeDoc(response);
  117. return FALSE;
  118. }
  119. attr = body->properties;
  120. while (attr != NULL) {
  121. if (attr->ns == NULL) {
  122. if (soap_version == SOAP_1_2) {
  123. add_soap_fault(this_ptr, "Client", "A SOAP Body element cannot have non Namespace qualified attributes", NULL, NULL);
  124. xmlFreeDoc(response);
  125. return FALSE;
  126. }
  127. } else if (attr_is_equal_ex(attr,"encodingStyle",SOAP_1_2_ENV_NAMESPACE)) {
  128. if (soap_version == SOAP_1_2) {
  129. add_soap_fault(this_ptr, "Client", "encodingStyle cannot be specified on the Body", NULL, NULL);
  130. xmlFreeDoc(response);
  131. return FALSE;
  132. } else if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE) != 0) {
  133. add_soap_fault(this_ptr, "Client", "Unknown data encoding style", NULL, NULL);
  134. xmlFreeDoc(response);
  135. return FALSE;
  136. }
  137. }
  138. attr = attr->next;
  139. }
  140. if (trav != NULL && soap_version == SOAP_1_2) {
  141. add_soap_fault(this_ptr, "Client", "A SOAP 1.2 envelope can contain only Header and Body", NULL, NULL);
  142. xmlFreeDoc(response);
  143. return FALSE;
  144. }
  145. if (head != NULL) {
  146. attr = head->properties;
  147. while (attr != NULL) {
  148. if (attr->ns == NULL) {
  149. add_soap_fault(this_ptr, "Client", "A SOAP Header element cannot have non Namespace qualified attributes", NULL, NULL);
  150. xmlFreeDoc(response);
  151. return FALSE;
  152. } else if (attr_is_equal_ex(attr,"encodingStyle",SOAP_1_2_ENV_NAMESPACE)) {
  153. if (soap_version == SOAP_1_2) {
  154. add_soap_fault(this_ptr, "Client", "encodingStyle cannot be specified on the Header", NULL, NULL);
  155. xmlFreeDoc(response);
  156. return FALSE;
  157. } else if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE) != 0) {
  158. add_soap_fault(this_ptr, "Client", "Unknown data encoding style", NULL, NULL);
  159. xmlFreeDoc(response);
  160. return FALSE;
  161. }
  162. }
  163. attr = attr->next;
  164. }
  165. }
  166. /* Check if <Body> contains <Fault> element */
  167. fault = get_node_ex(body->children,"Fault",envelope_ns);
  168. if (fault != NULL) {
  169. char *faultcode = NULL;
  170. zend_string *faultstring = NULL, *faultactor = NULL;
  171. zval details;
  172. xmlNodePtr tmp;
  173. ZVAL_UNDEF(&details);
  174. if (soap_version == SOAP_1_1) {
  175. tmp = get_node(fault->children, "faultcode");
  176. if (tmp != NULL && tmp->children != NULL) {
  177. faultcode = (char*)tmp->children->content;
  178. }
  179. tmp = get_node(fault->children, "faultstring");
  180. if (tmp != NULL && tmp->children != NULL) {
  181. zval zv;
  182. master_to_zval(&zv, get_conversion(IS_STRING), tmp);
  183. faultstring = Z_STR(zv);
  184. }
  185. tmp = get_node(fault->children, "faultactor");
  186. if (tmp != NULL && tmp->children != NULL) {
  187. zval zv;
  188. master_to_zval(&zv, get_conversion(IS_STRING), tmp);
  189. faultactor = Z_STR(zv);
  190. }
  191. tmp = get_node(fault->children, "detail");
  192. if (tmp != NULL) {
  193. master_to_zval(&details, NULL, tmp);
  194. }
  195. } else {
  196. tmp = get_node(fault->children, "Code");
  197. if (tmp != NULL && tmp->children != NULL) {
  198. tmp = get_node(tmp->children, "Value");
  199. if (tmp != NULL && tmp->children != NULL) {
  200. faultcode = (char*)tmp->children->content;
  201. }
  202. }
  203. tmp = get_node(fault->children,"Reason");
  204. if (tmp != NULL && tmp->children != NULL) {
  205. /* TODO: lang attribute */
  206. tmp = get_node(tmp->children,"Text");
  207. if (tmp != NULL && tmp->children != NULL) {
  208. zval zv;
  209. master_to_zval(&zv, get_conversion(IS_STRING), tmp);
  210. faultstring = Z_STR(zv);
  211. }
  212. }
  213. tmp = get_node(fault->children,"Detail");
  214. if (tmp != NULL) {
  215. master_to_zval(&details, NULL, tmp);
  216. }
  217. }
  218. add_soap_fault(this_ptr, faultcode, faultstring ? ZSTR_VAL(faultstring) : NULL, faultactor ? ZSTR_VAL(faultactor) : NULL, &details);
  219. if (faultstring) {
  220. zend_string_release_ex(faultstring, 0);
  221. }
  222. if (faultactor) {
  223. zend_string_release_ex(faultactor, 0);
  224. }
  225. if (Z_REFCOUNTED(details)) {
  226. Z_DELREF(details);
  227. }
  228. xmlFreeDoc(response);
  229. return FALSE;
  230. }
  231. /* Parse content of <Body> element */
  232. array_init(return_value);
  233. resp = body->children;
  234. while (resp != NULL && resp->type != XML_ELEMENT_NODE) {
  235. resp = resp->next;
  236. }
  237. if (resp != NULL) {
  238. if (fn != NULL && fn->binding && fn->binding->bindingType == BINDING_SOAP) {
  239. /* Function has WSDL description */
  240. sdlParamPtr param = NULL;
  241. xmlNodePtr val = NULL;
  242. char *name, *ns = NULL;
  243. zval tmp;
  244. sdlSoapBindingFunctionPtr fnb = (sdlSoapBindingFunctionPtr)fn->bindingAttributes;
  245. int res_count;
  246. hdrs = fnb->output.headers;
  247. if (fn->responseParameters) {
  248. res_count = zend_hash_num_elements(fn->responseParameters);
  249. ZEND_HASH_FOREACH_PTR(fn->responseParameters, param) {
  250. if (fnb->style == SOAP_DOCUMENT) {
  251. if (param->element) {
  252. name = param->element->name;
  253. ns = param->element->namens;
  254. /*
  255. name = param->encode->details.type_str;
  256. ns = param->encode->details.ns;
  257. */
  258. } else {
  259. name = param->paramName;
  260. }
  261. } else {
  262. name = fn->responseName;
  263. /* ns = ? */
  264. }
  265. /* Get value of parameter */
  266. cur = get_node_ex(resp, name, ns);
  267. if (!cur) {
  268. cur = get_node(resp, name);
  269. /* TODO: produce warning invalid ns */
  270. }
  271. if (!cur && fnb->style == SOAP_RPC) {
  272. cur = resp;
  273. }
  274. if (cur) {
  275. if (fnb->style == SOAP_DOCUMENT) {
  276. val = cur;
  277. } else {
  278. val = get_node(cur->children, param->paramName);
  279. if (res_count == 1) {
  280. if (val == NULL) {
  281. val = get_node(cur->children, "return");
  282. }
  283. if (val == NULL) {
  284. val = get_node(cur->children, "result");
  285. }
  286. if (val == NULL && cur->children && cur->children->next == NULL) {
  287. val = cur->children;
  288. }
  289. }
  290. }
  291. }
  292. if (!val) {
  293. /* TODO: may be "nil" is not OK? */
  294. ZVAL_NULL(&tmp);
  295. /*
  296. add_soap_fault(this_ptr, "Client", "Can't find response data", NULL, NULL);
  297. xmlFreeDoc(response);
  298. return FALSE;
  299. */
  300. } else {
  301. /* Decoding value of parameter */
  302. if (param != NULL) {
  303. master_to_zval(&tmp, param->encode, val);
  304. } else {
  305. master_to_zval(&tmp, NULL, val);
  306. }
  307. }
  308. add_assoc_zval(return_value, param->paramName, &tmp);
  309. param_count++;
  310. } ZEND_HASH_FOREACH_END();
  311. }
  312. } else {
  313. /* Function has no WSDL description */
  314. xmlNodePtr val;
  315. val = resp->children;
  316. while (val != NULL) {
  317. while (val && val->type != XML_ELEMENT_NODE) {
  318. val = val->next;
  319. }
  320. if (val != NULL) {
  321. if (!node_is_equal_ex(val,"result",RPC_SOAP12_NAMESPACE)) {
  322. zval tmp;
  323. zval *arr;
  324. master_to_zval(&tmp, NULL, val);
  325. if (val->name) {
  326. if ((arr = zend_hash_str_find(Z_ARRVAL_P(return_value), (char*)val->name, strlen((char*)val->name))) != NULL) {
  327. add_next_index_zval(arr, &tmp);
  328. } else if (val->next && get_node(val->next, (char*)val->name)) {
  329. zval arr;
  330. array_init(&arr);
  331. add_next_index_zval(&arr, &tmp);
  332. add_assoc_zval(return_value, (char*)val->name, &arr);
  333. } else {
  334. add_assoc_zval(return_value, (char*)val->name, &tmp);
  335. }
  336. } else {
  337. add_next_index_zval(return_value, &tmp);
  338. }
  339. ++param_count;
  340. }
  341. val = val->next;
  342. }
  343. }
  344. }
  345. }
  346. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  347. if (param_count == 0) {
  348. zend_array_destroy(Z_ARR_P(return_value));
  349. ZVAL_NULL(return_value);
  350. } else if (param_count == 1) {
  351. zval *tmp;
  352. zend_hash_internal_pointer_reset(Z_ARRVAL_P(return_value));
  353. tmp = zend_hash_get_current_data(Z_ARRVAL_P(return_value));
  354. if (!Z_REFCOUNTED_P(return_value)) {
  355. ZVAL_COPY(return_value, tmp);
  356. } else {
  357. zend_refcounted *garbage = Z_COUNTED_P(return_value);
  358. ZVAL_COPY(return_value, tmp);
  359. rc_dtor_func(garbage);
  360. }
  361. }
  362. }
  363. if (soap_headers && head) {
  364. trav = head->children;
  365. while (trav != NULL) {
  366. if (trav->type == XML_ELEMENT_NODE) {
  367. encodePtr enc = NULL;
  368. zval val;
  369. if (hdrs) {
  370. smart_str key = {0};
  371. sdlSoapBindingFunctionHeaderPtr hdr;
  372. if (trav->ns) {
  373. smart_str_appends(&key, (char*)trav->ns->href);
  374. smart_str_appendc(&key,':');
  375. }
  376. smart_str_appends(&key, (char*)trav->name);
  377. smart_str_0(&key);
  378. if ((hdr = zend_hash_find_ptr(hdrs, key.s)) != NULL) {
  379. enc = hdr->encode;
  380. }
  381. smart_str_free(&key);
  382. }
  383. master_to_zval(&val, enc, trav);
  384. add_assoc_zval(soap_headers, (char*)trav->name, &val);
  385. }
  386. trav = trav->next;
  387. }
  388. }
  389. xmlFreeDoc(response);
  390. return TRUE;
  391. }