PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/soap/php_sdl.c

https://github.com/php/php-src
C | 3673 lines | 3138 code | 461 blank | 74 comment | 877 complexity | 99f8f392426facf20ff26b707cd33ac4 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Brad Lafountain <rodif_bl@yahoo.com> |
  14. | Shane Caraveo <shane@caraveo.com> |
  15. | Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php_soap.h"
  19. #include "ext/libxml/php_libxml.h"
  20. #include "libxml/uri.h"
  21. #include "ext/standard/md5.h"
  22. #include "zend_virtual_cwd.h"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #ifndef O_BINARY
  27. # define O_BINARY 0
  28. #endif
  29. static void delete_fault(zval *zv);
  30. static void delete_fault_persistent(zval *zv);
  31. static void delete_binding(zval *zv);
  32. static void delete_binding_persistent(zval *zv);
  33. static void delete_function(zval *zv);
  34. static void delete_function_persistent(zval *zv);
  35. static void delete_parameter(zval *zv);
  36. static void delete_parameter_persistent(zval *zv);
  37. static void delete_header(zval *header);
  38. static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr);
  39. static void delete_header_persistent(zval *zv);
  40. static void delete_document(zval *zv);
  41. encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  42. {
  43. encodePtr enc = NULL;
  44. xmlNsPtr nsptr;
  45. char *ns, *cptype;
  46. parse_namespace(type, &cptype, &ns);
  47. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  48. if (nsptr != NULL) {
  49. enc = get_encoder(sdl, (char*)nsptr->href, cptype);
  50. if (enc == NULL) {
  51. enc = get_encoder_ex(sdl, cptype, strlen(cptype));
  52. }
  53. } else {
  54. enc = get_encoder_ex(sdl, (char*)type, xmlStrlen(type));
  55. }
  56. efree(cptype);
  57. if (ns) {efree(ns);}
  58. return enc;
  59. }
  60. static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  61. {
  62. sdlTypePtr ret = NULL;
  63. if (sdl->elements) {
  64. xmlNsPtr nsptr;
  65. char *ns, *cptype;
  66. sdlTypePtr sdl_type;
  67. parse_namespace(type, &cptype, &ns);
  68. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  69. if (nsptr != NULL) {
  70. int ns_len = xmlStrlen(nsptr->href);
  71. int type_len = strlen(cptype);
  72. int len = ns_len + type_len + 1;
  73. char *nscat = emalloc(len + 1);
  74. memcpy(nscat, nsptr->href, ns_len);
  75. nscat[ns_len] = ':';
  76. memcpy(nscat+ns_len+1, cptype, type_len);
  77. nscat[len] = '\0';
  78. if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, nscat, len)) != NULL) {
  79. ret = sdl_type;
  80. } else if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, type_len)) != NULL) {
  81. ret = sdl_type;
  82. }
  83. efree(nscat);
  84. } else {
  85. if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, xmlStrlen(type))) != NULL) {
  86. ret = sdl_type;
  87. }
  88. }
  89. efree(cptype);
  90. if (ns) {efree(ns);}
  91. }
  92. return ret;
  93. }
  94. encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type)
  95. {
  96. encodePtr enc = NULL;
  97. char *nscat;
  98. int ns_len = ns ? strlen(ns) : 0;
  99. int type_len = strlen(type);
  100. int len = ns_len + type_len + 1;
  101. nscat = emalloc(len + 1);
  102. if (ns) {
  103. memcpy(nscat, ns, ns_len);
  104. }
  105. nscat[ns_len] = ':';
  106. memcpy(nscat+ns_len+1, type, type_len);
  107. nscat[len] = '\0';
  108. enc = get_encoder_ex(sdl, nscat, len);
  109. if (enc == NULL &&
  110. ((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  111. memcmp(ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  112. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  113. memcmp(ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  114. char *enc_nscat;
  115. int enc_ns_len;
  116. int enc_len;
  117. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  118. enc_len = enc_ns_len + type_len + 1;
  119. enc_nscat = emalloc(enc_len + 1);
  120. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  121. enc_nscat[enc_ns_len] = ':';
  122. memcpy(enc_nscat+enc_ns_len+1, type, type_len);
  123. enc_nscat[enc_len] = '\0';
  124. enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  125. efree(enc_nscat);
  126. if (enc && sdl) {
  127. encodePtr new_enc = pemalloc(sizeof(encode), sdl->is_persistent);
  128. memcpy(new_enc, enc, sizeof(encode));
  129. if (sdl->is_persistent) {
  130. new_enc->details.ns = zend_strndup(ns, ns_len);
  131. new_enc->details.type_str = strdup(new_enc->details.type_str);
  132. } else {
  133. new_enc->details.ns = estrndup(ns, ns_len);
  134. new_enc->details.type_str = estrdup(new_enc->details.type_str);
  135. }
  136. if (sdl->encoders == NULL) {
  137. sdl->encoders = pemalloc(sizeof(HashTable), sdl->is_persistent);
  138. zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, sdl->is_persistent);
  139. }
  140. zend_hash_str_update_ptr(sdl->encoders, nscat, len, new_enc);
  141. enc = new_enc;
  142. }
  143. }
  144. efree(nscat);
  145. return enc;
  146. }
  147. encodePtr get_encoder_ex(sdlPtr sdl, const char *nscat, int len)
  148. {
  149. encodePtr enc;
  150. if ((enc = zend_hash_str_find_ptr(&SOAP_GLOBAL(defEnc), (char*)nscat, len)) != NULL) {
  151. return enc;
  152. } else if (sdl && sdl->encoders && (enc = zend_hash_str_find_ptr(sdl->encoders, (char*)nscat, len)) != NULL) {
  153. return enc;
  154. }
  155. return NULL;
  156. }
  157. sdlBindingPtr get_binding_from_type(sdlPtr sdl, sdlBindingType type)
  158. {
  159. sdlBindingPtr binding;
  160. if (sdl == NULL) {
  161. return NULL;
  162. }
  163. ZEND_HASH_MAP_FOREACH_PTR(sdl->bindings, binding) {
  164. if (binding->bindingType == type) {
  165. return binding;
  166. }
  167. } ZEND_HASH_FOREACH_END();
  168. return NULL;
  169. }
  170. sdlBindingPtr get_binding_from_name(sdlPtr sdl, char *name, char *ns)
  171. {
  172. sdlBindingPtr binding;
  173. smart_str key = {0};
  174. smart_str_appends(&key, ns);
  175. smart_str_appendc(&key, ':');
  176. smart_str_appends(&key, name);
  177. smart_str_0(&key);
  178. binding = zend_hash_find_ptr(sdl->bindings, key.s);
  179. smart_str_free(&key);
  180. return binding;
  181. }
  182. static int is_wsdl_element(xmlNodePtr node)
  183. {
  184. if (node->ns && strcmp((char*)node->ns->href, WSDL_NAMESPACE) != 0) {
  185. xmlAttrPtr attr;
  186. if ((attr = get_attribute_ex(node->properties, "required", WSDL_NAMESPACE)) != NULL &&
  187. attr->children && attr->children->content &&
  188. (strcmp((char*)attr->children->content, "1") == 0 ||
  189. strcmp((char*)attr->children->content, "true") == 0)) {
  190. soap_error1(E_ERROR, "Parsing WSDL: Unknown required WSDL extension '%s'", node->ns->href);
  191. }
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. void sdl_set_uri_credentials(sdlCtx *ctx, char *uri)
  197. {
  198. char *s;
  199. size_t l1, l2;
  200. zval context;
  201. zval *header = NULL;
  202. /* check if we load xsd from the same server */
  203. s = strstr(ctx->sdl->source, "://");
  204. if (!s) return;
  205. s = strchr(s+3, '/');
  206. l1 = s ? (size_t)(s - ctx->sdl->source) : strlen(ctx->sdl->source);
  207. s = strstr((char*)uri, "://");
  208. if (!s) return;
  209. s = strchr(s+3, '/');
  210. l2 = s ? (size_t)(s - (char*)uri) : strlen((char*)uri);
  211. if (l1 != l2) {
  212. /* check for http://...:80/ */
  213. if (l1 > 11 &&
  214. ctx->sdl->source[4] == ':' &&
  215. ctx->sdl->source[l1-3] == ':' &&
  216. ctx->sdl->source[l1-2] == '8' &&
  217. ctx->sdl->source[l1-1] == '0') {
  218. l1 -= 3;
  219. }
  220. if (l2 > 11 &&
  221. uri[4] == ':' &&
  222. uri[l2-3] == ':' &&
  223. uri[l2-2] == '8' &&
  224. uri[l2-1] == '0') {
  225. l2 -= 3;
  226. }
  227. /* check for https://...:443/ */
  228. if (l1 > 13 &&
  229. ctx->sdl->source[4] == 's' &&
  230. ctx->sdl->source[l1-4] == ':' &&
  231. ctx->sdl->source[l1-3] == '4' &&
  232. ctx->sdl->source[l1-2] == '4' &&
  233. ctx->sdl->source[l1-1] == '3') {
  234. l1 -= 4;
  235. }
  236. if (l2 > 13 &&
  237. uri[4] == 's' &&
  238. uri[l2-4] == ':' &&
  239. uri[l2-3] == '4' &&
  240. uri[l2-2] == '4' &&
  241. uri[l2-1] == '3') {
  242. l2 -= 4;
  243. }
  244. }
  245. if (l1 != l2 || memcmp(ctx->sdl->source, uri, l1) != 0) {
  246. /* another server. clear authentication credentals */
  247. php_libxml_switch_context(NULL, &context);
  248. php_libxml_switch_context(&context, NULL);
  249. if (Z_TYPE(context) != IS_UNDEF) {
  250. zval *context_ptr = &context;
  251. ctx->context = php_stream_context_from_zval(context_ptr, 1);
  252. if (ctx->context &&
  253. (header = php_stream_context_get_option(ctx->context, "http", "header")) != NULL) {
  254. s = strstr(Z_STRVAL_P(header), "Authorization: Basic");
  255. if (s && (s == Z_STRVAL_P(header) || *(s-1) == '\n' || *(s-1) == '\r')) {
  256. char *rest = strstr(s, "\r\n");
  257. if (rest) {
  258. zval new_header;
  259. rest += 2;
  260. ZVAL_NEW_STR(&new_header, zend_string_alloc(Z_STRLEN_P(header) - (rest - s), 0));
  261. memcpy(Z_STRVAL(new_header), Z_STRVAL_P(header), s - Z_STRVAL_P(header));
  262. memcpy(Z_STRVAL(new_header) + (s - Z_STRVAL_P(header)), rest, Z_STRLEN_P(header) - (rest - Z_STRVAL_P(header)) + 1);
  263. ZVAL_COPY(&ctx->old_header, header);
  264. php_stream_context_set_option(ctx->context, "http", "header", &new_header);
  265. zval_ptr_dtor(&new_header);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. void sdl_restore_uri_credentials(sdlCtx *ctx)
  273. {
  274. if (Z_TYPE(ctx->old_header) != IS_UNDEF) {
  275. php_stream_context_set_option(ctx->context, "http", "header", &ctx->old_header);
  276. zval_ptr_dtor(&ctx->old_header);
  277. ZVAL_UNDEF(&ctx->old_header);
  278. }
  279. ctx->context = NULL;
  280. }
  281. #define SAFE_STR(a) ((a)?((const char *)a):"")
  282. static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include)
  283. {
  284. sdlPtr tmpsdl = ctx->sdl;
  285. xmlDocPtr wsdl;
  286. xmlNodePtr root, definitions, trav;
  287. xmlAttrPtr targetNamespace;
  288. if (zend_hash_str_exists(&ctx->docs, struri, strlen(struri))) {
  289. return;
  290. }
  291. sdl_set_uri_credentials(ctx, struri);
  292. wsdl = soap_xmlParseFile(struri);
  293. sdl_restore_uri_credentials(ctx);
  294. if (!wsdl) {
  295. xmlErrorPtr xmlErrorPtr = xmlGetLastError();
  296. if (xmlErrorPtr) {
  297. soap_error2(E_ERROR, "Parsing WSDL: Couldn't load from '%s' : %s", struri, xmlErrorPtr->message);
  298. } else {
  299. soap_error1(E_ERROR, "Parsing WSDL: Couldn't load from '%s'", struri);
  300. }
  301. }
  302. zend_hash_str_add_ptr(&ctx->docs, struri, strlen(struri), wsdl);
  303. root = wsdl->children;
  304. definitions = get_node_ex(root, "definitions", WSDL_NAMESPACE);
  305. if (!definitions) {
  306. if (include) {
  307. xmlNodePtr schema = get_node_ex(root, "schema", XSD_NAMESPACE);
  308. if (schema) {
  309. load_schema(ctx, schema);
  310. return;
  311. }
  312. }
  313. soap_error1(E_ERROR, "Parsing WSDL: Couldn't find <definitions> in '%s'", struri);
  314. }
  315. if (!include) {
  316. targetNamespace = get_attribute(definitions->properties, "targetNamespace");
  317. if (targetNamespace) {
  318. tmpsdl->target_ns = estrdup((char*)targetNamespace->children->content);
  319. }
  320. }
  321. trav = definitions->children;
  322. while (trav != NULL) {
  323. if (!is_wsdl_element(trav)) {
  324. trav = trav->next;
  325. continue;
  326. }
  327. if (node_is_equal(trav,"types")) {
  328. /* TODO: Only one "types" is allowed */
  329. xmlNodePtr trav2 = trav->children;
  330. while (trav2 != NULL) {
  331. if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
  332. load_schema(ctx, trav2);
  333. } else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  334. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  335. }
  336. trav2 = trav2->next;
  337. }
  338. } else if (node_is_equal(trav,"import")) {
  339. /* TODO: namespace ??? */
  340. xmlAttrPtr tmp = get_attribute(trav->properties, "location");
  341. if (tmp) {
  342. xmlChar *uri;
  343. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  344. if (base == NULL) {
  345. uri = xmlBuildURI(tmp->children->content, trav->doc->URL);
  346. } else {
  347. uri = xmlBuildURI(tmp->children->content, base);
  348. xmlFree(base);
  349. }
  350. load_wsdl_ex(this_ptr, (char*)uri, ctx, 1);
  351. xmlFree(uri);
  352. }
  353. } else if (node_is_equal(trav,"message")) {
  354. xmlAttrPtr name = get_attribute(trav->properties, "name");
  355. if (name && name->children && name->children->content) {
  356. if (zend_hash_str_add_ptr(&ctx->messages, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  357. soap_error1(E_ERROR, "Parsing WSDL: <message> '%s' already defined", name->children->content);
  358. }
  359. } else {
  360. soap_error0(E_ERROR, "Parsing WSDL: <message> has no name attribute");
  361. }
  362. } else if (node_is_equal(trav,"portType")) {
  363. xmlAttrPtr name = get_attribute(trav->properties, "name");
  364. if (name && name->children && name->children->content) {
  365. if (zend_hash_str_add_ptr(&ctx->portTypes, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  366. soap_error1(E_ERROR, "Parsing WSDL: <portType> '%s' already defined", name->children->content);
  367. }
  368. } else {
  369. soap_error0(E_ERROR, "Parsing WSDL: <portType> has no name attribute");
  370. }
  371. } else if (node_is_equal(trav,"binding")) {
  372. xmlAttrPtr name = get_attribute(trav->properties, "name");
  373. if (name && name->children && name->children->content) {
  374. if (zend_hash_str_add_ptr(&ctx->bindings, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  375. soap_error1(E_ERROR, "Parsing WSDL: <binding> '%s' already defined", name->children->content);
  376. }
  377. } else {
  378. soap_error0(E_ERROR, "Parsing WSDL: <binding> has no name attribute");
  379. }
  380. } else if (node_is_equal(trav,"service")) {
  381. xmlAttrPtr name = get_attribute(trav->properties, "name");
  382. if (name && name->children && name->children->content) {
  383. if (zend_hash_str_add_ptr(&ctx->services, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  384. soap_error1(E_ERROR, "Parsing WSDL: <service> '%s' already defined", name->children->content);
  385. }
  386. } else {
  387. soap_error0(E_ERROR, "Parsing WSDL: <service> has no name attribute");
  388. }
  389. } else if (!node_is_equal(trav,"documentation")) {
  390. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  391. }
  392. trav = trav->next;
  393. }
  394. }
  395. static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xmlNodePtr header, char* wsdl_soap_namespace, int fault)
  396. {
  397. xmlAttrPtr tmp;
  398. xmlNodePtr message, part;
  399. char *ctype;
  400. sdlSoapBindingFunctionHeaderPtr h;
  401. tmp = get_attribute(header->properties, "message");
  402. if (!tmp) {
  403. soap_error0(E_ERROR, "Parsing WSDL: Missing message attribute for <header>");
  404. }
  405. ctype = strrchr((char*)tmp->children->content,':');
  406. if (ctype == NULL) {
  407. ctype = (char*)tmp->children->content;
  408. } else {
  409. ++ctype;
  410. }
  411. if ((message = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  412. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", tmp->children->content);
  413. }
  414. tmp = get_attribute(header->properties, "part");
  415. if (!tmp) {
  416. soap_error0(E_ERROR, "Parsing WSDL: Missing part attribute for <header>");
  417. }
  418. part = get_node_with_attribute_ex(message->children, "part", WSDL_NAMESPACE, "name", (char*)tmp->children->content, NULL);
  419. if (!part) {
  420. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", tmp->children->content);
  421. }
  422. h = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  423. memset(h, 0, sizeof(sdlSoapBindingFunctionHeader));
  424. h->name = estrdup((char*)tmp->children->content);
  425. tmp = get_attribute(header->properties, "use");
  426. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  427. h->use = SOAP_ENCODED;
  428. } else {
  429. h->use = SOAP_LITERAL;
  430. }
  431. tmp = get_attribute(header->properties, "namespace");
  432. if (tmp) {
  433. h->ns = estrdup((char*)tmp->children->content);
  434. }
  435. if (h->use == SOAP_ENCODED) {
  436. tmp = get_attribute(header->properties, "encodingStyle");
  437. if (tmp) {
  438. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  439. h->encodingStyle = SOAP_ENCODING_1_1;
  440. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  441. h->encodingStyle = SOAP_ENCODING_1_2;
  442. } else {
  443. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  444. }
  445. } else {
  446. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  447. }
  448. }
  449. tmp = get_attribute(part->properties, "type");
  450. if (tmp != NULL) {
  451. h->encode = get_encoder_from_prefix(ctx->sdl, part, tmp->children->content);
  452. } else {
  453. tmp = get_attribute(part->properties, "element");
  454. if (tmp != NULL) {
  455. h->element = get_element(ctx->sdl, part, tmp->children->content);
  456. if (h->element) {
  457. h->encode = h->element->encode;
  458. if (!h->ns && h->element->namens) {
  459. h->ns = estrdup(h->element->namens);
  460. }
  461. if (h->element->name) {
  462. efree(h->name);
  463. h->name = estrdup(h->element->name);
  464. }
  465. }
  466. }
  467. }
  468. if (!fault) {
  469. xmlNodePtr trav = header->children;
  470. while (trav != NULL) {
  471. if (node_is_equal_ex(trav, "headerfault", wsdl_soap_namespace)) {
  472. sdlSoapBindingFunctionHeaderPtr hf = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 1);
  473. smart_str key = {0};
  474. if (h->headerfaults == NULL) {
  475. h->headerfaults = emalloc(sizeof(HashTable));
  476. zend_hash_init(h->headerfaults, 0, NULL, delete_header, 0);
  477. }
  478. if (hf->ns) {
  479. smart_str_appends(&key,hf->ns);
  480. smart_str_appendc(&key,':');
  481. }
  482. smart_str_appends(&key,hf->name);
  483. smart_str_0(&key);
  484. if (zend_hash_add_ptr(h->headerfaults, key.s, hf) == NULL) {
  485. delete_header_int(hf);
  486. }
  487. smart_str_free(&key);
  488. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  489. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  490. }
  491. trav = trav->next;
  492. }
  493. }
  494. return h;
  495. }
  496. static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap_namespace, sdlSoapBindingFunctionBody *binding, HashTable* params)
  497. {
  498. xmlNodePtr body, trav;
  499. xmlAttrPtr tmp;
  500. trav = node->children;
  501. while (trav != NULL) {
  502. if (node_is_equal_ex(trav, "body", wsdl_soap_namespace)) {
  503. body = trav;
  504. tmp = get_attribute(body->properties, "use");
  505. if (tmp && !strncmp((char*)tmp->children->content, "literal", sizeof("literal"))) {
  506. binding->use = SOAP_LITERAL;
  507. } else {
  508. binding->use = SOAP_ENCODED;
  509. }
  510. tmp = get_attribute(body->properties, "namespace");
  511. if (tmp) {
  512. binding->ns = estrdup((char*)tmp->children->content);
  513. }
  514. tmp = get_attribute(body->properties, "parts");
  515. if (tmp) {
  516. HashTable ht;
  517. char *parts = (char*)tmp->children->content;
  518. /* Delete all parts those are not in the "parts" attribute */
  519. zend_hash_init(&ht, 0, NULL, delete_parameter, 0);
  520. while (*parts) {
  521. sdlParamPtr param;
  522. int found = 0;
  523. char *end;
  524. while (*parts == ' ') ++parts;
  525. if (*parts == '\0') break;
  526. end = strchr(parts, ' ');
  527. if (end) *end = '\0';
  528. ZEND_HASH_FOREACH_PTR(params, param) {
  529. if (param->paramName &&
  530. strcmp(parts, param->paramName) == 0) {
  531. sdlParamPtr x_param;
  532. x_param = emalloc(sizeof(sdlParam));
  533. *x_param = *param;
  534. param->paramName = NULL;
  535. zend_hash_next_index_insert_ptr(&ht, x_param);
  536. found = 1;
  537. break;
  538. }
  539. } ZEND_HASH_FOREACH_END();
  540. if (!found) {
  541. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", parts);
  542. }
  543. parts += strlen(parts);
  544. if (end) *end = ' ';
  545. }
  546. zend_hash_destroy(params);
  547. *params = ht;
  548. }
  549. if (binding->use == SOAP_ENCODED) {
  550. tmp = get_attribute(body->properties, "encodingStyle");
  551. if (tmp) {
  552. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  553. binding->encodingStyle = SOAP_ENCODING_1_1;
  554. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  555. binding->encodingStyle = SOAP_ENCODING_1_2;
  556. } else {
  557. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  558. }
  559. } else {
  560. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  561. }
  562. }
  563. } else if (node_is_equal_ex(trav, "header", wsdl_soap_namespace)) {
  564. sdlSoapBindingFunctionHeaderPtr h = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 0);
  565. smart_str key = {0};
  566. if (binding->headers == NULL) {
  567. binding->headers = emalloc(sizeof(HashTable));
  568. zend_hash_init(binding->headers, 0, NULL, delete_header, 0);
  569. }
  570. if (h->ns) {
  571. smart_str_appends(&key,h->ns);
  572. smart_str_appendc(&key,':');
  573. }
  574. smart_str_appends(&key,h->name);
  575. smart_str_0(&key);
  576. if (zend_hash_add_ptr(binding->headers, key.s, h) == NULL) {
  577. delete_header_int(h);
  578. }
  579. smart_str_free(&key);
  580. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  581. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  582. }
  583. trav = trav->next;
  584. }
  585. }
  586. static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
  587. {
  588. xmlNodePtr trav, part, message = NULL, tmp;
  589. HashTable* parameters = NULL;
  590. char *ctype;
  591. ctype = strrchr((char*)message_name,':');
  592. if (ctype == NULL) {
  593. ctype = (char*)message_name;
  594. } else {
  595. ++ctype;
  596. }
  597. if ((tmp = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  598. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", message_name);
  599. }
  600. message = tmp;
  601. parameters = emalloc(sizeof(HashTable));
  602. zend_hash_init(parameters, 0, NULL, delete_parameter, 0);
  603. trav = message->children;
  604. while (trav != NULL) {
  605. xmlAttrPtr element, type, name;
  606. sdlParamPtr param;
  607. if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
  608. soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>", SAFE_STR(trav->name));
  609. }
  610. if (node_is_equal(trav,"documentation")) {
  611. trav = trav->next;
  612. continue;
  613. }
  614. if (!node_is_equal(trav,"part")) {
  615. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  616. }
  617. part = trav;
  618. param = emalloc(sizeof(sdlParam));
  619. memset(param,0,sizeof(sdlParam));
  620. param->order = 0;
  621. name = get_attribute(part->properties, "name");
  622. if (name == NULL) {
  623. soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'", SAFE_STR(message->name));
  624. }
  625. param->paramName = estrdup((char*)name->children->content);
  626. type = get_attribute(part->properties, "type");
  627. if (type != NULL) {
  628. param->encode = get_encoder_from_prefix(ctx->sdl, part, type->children->content);
  629. } else {
  630. element = get_attribute(part->properties, "element");
  631. if (element != NULL) {
  632. param->element = get_element(ctx->sdl, part, element->children->content);
  633. if (param->element) {
  634. param->encode = param->element->encode;
  635. }
  636. }
  637. }
  638. zend_hash_next_index_insert_ptr(parameters, param);
  639. trav = trav->next;
  640. }
  641. return parameters;
  642. }
  643. static sdlPtr load_wsdl(zval *this_ptr, char *struri)
  644. {
  645. sdlCtx ctx;
  646. int i,n;
  647. memset(&ctx,0,sizeof(ctx));
  648. ctx.sdl = emalloc(sizeof(sdl));
  649. memset(ctx.sdl, 0, sizeof(sdl));
  650. ctx.sdl->source = estrdup(struri);
  651. zend_hash_init(&ctx.sdl->functions, 0, NULL, delete_function, 0);
  652. zend_hash_init(&ctx.docs, 0, NULL, delete_document, 0);
  653. zend_hash_init(&ctx.messages, 0, NULL, NULL, 0);
  654. zend_hash_init(&ctx.bindings, 0, NULL, NULL, 0);
  655. zend_hash_init(&ctx.portTypes, 0, NULL, NULL, 0);
  656. zend_hash_init(&ctx.services, 0, NULL, NULL, 0);
  657. zend_try {
  658. load_wsdl_ex(this_ptr, struri, &ctx, 0);
  659. schema_pass2(&ctx);
  660. n = zend_hash_num_elements(&ctx.services);
  661. if (n > 0) {
  662. zend_hash_internal_pointer_reset(&ctx.services);
  663. for (i = 0; i < n; i++) {
  664. xmlNodePtr service, tmp;
  665. xmlNodePtr trav, port;
  666. int has_soap_port = 0;
  667. service = tmp = zend_hash_get_current_data_ptr(&ctx.services);
  668. trav = service->children;
  669. while (trav != NULL) {
  670. xmlAttrPtr type, name, bindingAttr, location;
  671. xmlNodePtr portType, operation;
  672. xmlNodePtr address, binding, trav2;
  673. char *ctype;
  674. sdlBindingPtr tmpbinding;
  675. char *wsdl_soap_namespace = NULL;
  676. if (!is_wsdl_element(trav) || node_is_equal(trav,"documentation")) {
  677. trav = trav->next;
  678. continue;
  679. }
  680. if (!node_is_equal(trav,"port")) {
  681. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  682. }
  683. port = trav;
  684. tmpbinding = emalloc(sizeof(sdlBinding));
  685. memset(tmpbinding, 0, sizeof(sdlBinding));
  686. bindingAttr = get_attribute(port->properties, "binding");
  687. if (bindingAttr == NULL) {
  688. soap_error0(E_ERROR, "Parsing WSDL: No binding associated with <port>");
  689. }
  690. /* find address and figure out binding type */
  691. address = NULL;
  692. trav2 = port->children;
  693. while (trav2 != NULL) {
  694. if (node_is_equal(trav2,"address") && trav2->ns) {
  695. if (!strncmp((char*)trav2->ns->href, WSDL_SOAP11_NAMESPACE, sizeof(WSDL_SOAP11_NAMESPACE))) {
  696. address = trav2;
  697. wsdl_soap_namespace = WSDL_SOAP11_NAMESPACE;
  698. tmpbinding->bindingType = BINDING_SOAP;
  699. } else if (!strncmp((char*)trav2->ns->href, WSDL_SOAP12_NAMESPACE, sizeof(WSDL_SOAP12_NAMESPACE))) {
  700. address = trav2;
  701. wsdl_soap_namespace = WSDL_SOAP12_NAMESPACE;
  702. tmpbinding->bindingType = BINDING_SOAP;
  703. } else if (!strncmp((char*)trav2->ns->href, RPC_SOAP12_NAMESPACE, sizeof(RPC_SOAP12_NAMESPACE))) {
  704. address = trav2;
  705. wsdl_soap_namespace = RPC_SOAP12_NAMESPACE;
  706. tmpbinding->bindingType = BINDING_SOAP;
  707. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP11_NAMESPACE, sizeof(WSDL_HTTP11_NAMESPACE))) {
  708. address = trav2;
  709. tmpbinding->bindingType = BINDING_HTTP;
  710. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP12_NAMESPACE, sizeof(WSDL_HTTP12_NAMESPACE))) {
  711. address = trav2;
  712. tmpbinding->bindingType = BINDING_HTTP;
  713. }
  714. }
  715. if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  716. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  717. }
  718. trav2 = trav2->next;
  719. }
  720. if (!address || tmpbinding->bindingType == BINDING_HTTP) {
  721. if (has_soap_port || trav->next || i < n-1) {
  722. efree(tmpbinding);
  723. trav = trav->next;
  724. continue;
  725. } else if (!address) {
  726. soap_error0(E_ERROR, "Parsing WSDL: No address associated with <port>");
  727. }
  728. }
  729. has_soap_port = 1;
  730. location = get_attribute(address->properties, "location");
  731. if (!location) {
  732. soap_error0(E_ERROR, "Parsing WSDL: No location associated with <port>");
  733. }
  734. tmpbinding->location = estrdup((char*)location->children->content);
  735. ctype = strrchr((char*)bindingAttr->children->content,':');
  736. if (ctype == NULL) {
  737. ctype = (char*)bindingAttr->children->content;
  738. } else {
  739. ++ctype;
  740. }
  741. if ((tmp = zend_hash_str_find_ptr(&ctx.bindings, ctype, strlen(ctype))) == NULL) {
  742. soap_error1(E_ERROR, "Parsing WSDL: No <binding> element with name '%s'", ctype);
  743. }
  744. binding = tmp;
  745. if (tmpbinding->bindingType == BINDING_SOAP) {
  746. sdlSoapBindingPtr soapBinding;
  747. xmlNodePtr soapBindingNode;
  748. xmlAttrPtr tmp;
  749. soapBinding = emalloc(sizeof(sdlSoapBinding));
  750. memset(soapBinding, 0, sizeof(sdlSoapBinding));
  751. soapBinding->style = SOAP_DOCUMENT;
  752. soapBindingNode = get_node_ex(binding->children, "binding", wsdl_soap_namespace);
  753. if (soapBindingNode) {
  754. tmp = get_attribute(soapBindingNode->properties, "style");
  755. if (tmp && !strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  756. soapBinding->style = SOAP_RPC;
  757. }
  758. tmp = get_attribute(soapBindingNode->properties, "transport");
  759. if (tmp) {
  760. if (strncmp((char*)tmp->children->content, WSDL_HTTP_TRANSPORT, sizeof(WSDL_HTTP_TRANSPORT)) == 0) {
  761. soapBinding->transport = SOAP_TRANSPORT_HTTP;
  762. } else {
  763. /* try the next binding */
  764. efree(soapBinding);
  765. efree(tmpbinding->location);
  766. efree(tmpbinding);
  767. trav = trav->next;
  768. continue;
  769. }
  770. }
  771. }
  772. tmpbinding->bindingAttributes = (void *)soapBinding;
  773. }
  774. name = get_attribute(binding->properties, "name");
  775. if (name == NULL) {
  776. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <binding>");
  777. }
  778. tmpbinding->name = estrdup((char*)name->children->content);
  779. type = get_attribute(binding->properties, "type");
  780. if (type == NULL) {
  781. soap_error0(E_ERROR, "Parsing WSDL: Missing 'type' attribute for <binding>");
  782. }
  783. ctype = strrchr((char*)type->children->content,':');
  784. if (ctype == NULL) {
  785. ctype = (char*)type->children->content;
  786. } else {
  787. ++ctype;
  788. }
  789. if ((tmp = zend_hash_str_find_ptr(&ctx.portTypes, ctype, strlen(ctype))) == NULL) {
  790. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType> with name '%s'", name->children->content);
  791. }
  792. portType = tmp;
  793. trav2 = binding->children;
  794. while (trav2 != NULL) {
  795. sdlFunctionPtr function;
  796. xmlNodePtr input, output, fault, portTypeOperation, trav3;
  797. xmlAttrPtr op_name, paramOrder;
  798. if ((tmpbinding->bindingType == BINDING_SOAP &&
  799. node_is_equal_ex(trav2, "binding", wsdl_soap_namespace)) ||
  800. !is_wsdl_element(trav2) ||
  801. node_is_equal(trav2,"documentation")) {
  802. trav2 = trav2->next;
  803. continue;
  804. }
  805. if (!node_is_equal(trav2,"operation")) {
  806. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  807. }
  808. operation = trav2;
  809. op_name = get_attribute(operation->properties, "name");
  810. if (op_name == NULL) {
  811. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <operation>");
  812. }
  813. trav3 = operation->children;
  814. while (trav3 != NULL) {
  815. if (tmpbinding->bindingType == BINDING_SOAP &&
  816. node_is_equal_ex(trav3, "operation", wsdl_soap_namespace)) {
  817. } else if (is_wsdl_element(trav3) &&
  818. !node_is_equal(trav3,"input") &&
  819. !node_is_equal(trav3,"output") &&
  820. !node_is_equal(trav3,"fault") &&
  821. !node_is_equal(trav3,"documentation")) {
  822. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav3->name));
  823. }
  824. trav3 = trav3->next;
  825. }
  826. portTypeOperation = get_node_with_attribute_ex(portType->children, "operation", WSDL_NAMESPACE, "name", (char*)op_name->children->content, NULL);
  827. if (portTypeOperation == NULL) {
  828. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType>/<operation> with name '%s'", op_name->children->content);
  829. }
  830. function = emalloc(sizeof(sdlFunction));
  831. memset(function, 0, sizeof(sdlFunction));
  832. function->functionName = estrdup((char*)op_name->children->content);
  833. if (tmpbinding->bindingType == BINDING_SOAP) {
  834. sdlSoapBindingFunctionPtr soapFunctionBinding;
  835. sdlSoapBindingPtr soapBinding;
  836. xmlNodePtr soapOperation;
  837. xmlAttrPtr tmp;
  838. soapFunctionBinding = emalloc(sizeof(sdlSoapBindingFunction));
  839. memset(soapFunctionBinding, 0, sizeof(sdlSoapBindingFunction));
  840. soapBinding = (sdlSoapBindingPtr)tmpbinding->bindingAttributes;
  841. soapFunctionBinding->style = soapBinding->style;
  842. soapOperation = get_node_ex(operation->children, "operation", wsdl_soap_namespace);
  843. if (soapOperation) {
  844. tmp = get_attribute(soapOperation->properties, "soapAction");
  845. if (tmp) {
  846. soapFunctionBinding->soapAction = estrdup((char*)tmp->children->content);
  847. }
  848. tmp = get_attribute(soapOperation->properties, "style");
  849. if (tmp) {
  850. if (!strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  851. soapFunctionBinding->style = SOAP_RPC;
  852. } else {
  853. soapFunctionBinding->style = SOAP_DOCUMENT;
  854. }
  855. } else {
  856. soapFunctionBinding->style = soapBinding->style;
  857. }
  858. }
  859. function->bindingAttributes = (void *)soapFunctionBinding;
  860. }
  861. input = get_node_ex(portTypeOperation->children, "input", WSDL_NAMESPACE);
  862. if (input != NULL) {
  863. xmlAttrPtr message;
  864. message = get_attribute(input->properties, "message");
  865. if (message == NULL) {
  866. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <input> of '%s'", op_name->children->content);
  867. }
  868. function->requestParameters = wsdl_message(&ctx, message->children->content);
  869. /* FIXME
  870. xmlAttrPtr name = get_attribute(input->properties, "name");
  871. if (name != NULL) {
  872. function->requestName = estrdup(name->children->content);
  873. } else {
  874. */
  875. {
  876. function->requestName = estrdup(function->functionName);
  877. }
  878. if (tmpbinding->bindingType == BINDING_SOAP) {
  879. input = get_node_ex(operation->children, "input", WSDL_NAMESPACE);
  880. if (input != NULL) {
  881. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  882. wsdl_soap_binding_body(&ctx, input, wsdl_soap_namespace, &soapFunctionBinding->input, function->requestParameters);
  883. }
  884. }
  885. }
  886. output = get_node_ex(portTypeOperation->children, "output", WSDL_NAMESPACE);
  887. if (output != NULL) {
  888. xmlAttrPtr message;
  889. message = get_attribute(output->properties, "message");
  890. if (message == NULL) {
  891. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  892. }
  893. function->responseParameters = wsdl_message(&ctx, message->children->content);
  894. /* FIXME
  895. xmlAttrPtr name = get_attribute(output->properties, "name");
  896. if (name != NULL) {
  897. function->responseName = estrdup(name->children->content);
  898. } else if (input == NULL) {
  899. function->responseName = estrdup(function->functionName);
  900. } else {
  901. */
  902. {
  903. int len = strlen(function->functionName);
  904. function->responseName = emalloc(len + sizeof("Response"));
  905. memcpy(function->responseName, function->functionName, len);
  906. memcpy(function->responseName+len, "Response", sizeof("Response"));
  907. }
  908. if (tmpbinding->bindingType == BINDING_SOAP) {
  909. output = get_node_ex(operation->children, "output", WSDL_NAMESPACE);
  910. if (output != NULL) {
  911. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  912. wsdl_soap_binding_body(&ctx, output, wsdl_soap_namespace, &soapFunctionBinding->output, function->responseParameters);
  913. }
  914. }
  915. }
  916. paramOrder = get_attribute(portTypeOperation->properties, "parameterOrder");
  917. if (paramOrder) {
  918. /* FIXME: */
  919. }
  920. fault = portTypeOperation->children;
  921. while (fault != NULL) {
  922. if (node_is_equal_ex(fault, "fault", WSDL_NAMESPACE)) {
  923. xmlAttrPtr message, name;
  924. sdlFaultPtr f;
  925. name = get_attribute(fault->properties, "name");
  926. if (name == NULL) {
  927. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <fault> of '%s'", op_name->children->content);
  928. }
  929. message = get_attribute(fault->properties, "message");
  930. if (message == NULL) {
  931. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  932. }
  933. f = emalloc(sizeof(sdlFault));
  934. memset(f, 0, sizeof(sdlFault));
  935. f->name = estrdup((char*)name->children->content);
  936. f->details = wsdl_message(&ctx, message->children->content);
  937. if (f->details == NULL || zend_hash_num_elements(f->details) > 1) {
  938. soap_error1(E_ERROR, "Parsing WSDL: The fault message '%s' must have a single part", message->children->content);
  939. }
  940. if (tmpbinding->bindingType == BINDING_SOAP) {
  941. xmlNodePtr soap_fault = get_node_with_attribute_ex(operation->children, "fault", WSDL_NAMESPACE, "name", f->name, NULL);
  942. if (soap_fault != NULL) {
  943. xmlNodePtr trav = soap_fault->children;
  944. while (trav != NULL) {
  945. if (node_is_equal_ex(trav, "fault", wsdl_soap_namespace)) {
  946. xmlAttrPtr tmp;
  947. sdlSoapBindingFunctionFaultPtr binding;
  948. binding = f->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  949. memset(f->bindingAttributes, 0, sizeof(sdlSoapBindingFunctionFault));
  950. tmp = get_attribute(trav->properties, "use");
  951. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  952. binding->use = SOAP_ENCODED;
  953. } else {
  954. binding->use = SOAP_LITERAL;
  955. }
  956. tmp = get_attribute(trav->properties, "namespace");
  957. if (tmp) {
  958. binding->ns = estrdup((char*)tmp->children->content);
  959. }
  960. if (binding->use == SOAP_ENCODED) {
  961. tmp = get_attribute(trav->properties, "encodingStyle");
  962. if (tmp) {
  963. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  964. binding->encodingStyle = SOAP_ENCODING_1_1;
  965. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  966. binding->encodingStyle = SOAP_ENCODING_1_2;
  967. } else {
  968. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  969. }
  970. } else {
  971. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  972. }
  973. }
  974. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  975. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  976. }
  977. trav = trav->next;
  978. }
  979. }
  980. }
  981. if (function->faults == NULL) {
  982. function->faults = emalloc(sizeof(HashTable));
  983. zend_hash_init(function->faults, 0, NULL, delete_fault, 0);
  984. }
  985. if (zend_hash_str_add_ptr(function->faults, f->name, strlen(f->name), f) == NULL) {
  986. soap_error2(E_ERROR, "Parsing WSDL: <fault> with name '%s' already defined in '%s'", f->name, op_name->children->content);
  987. }
  988. }
  989. fault = fault->next;
  990. }
  991. function->binding = tmpbinding;
  992. {
  993. char *tmp = estrdup(function->functionName);
  994. int len = strlen(tmp);
  995. zend_str_tolower(tmp, len);
  996. if (zend_hash_str_add_ptr(&ctx.sdl->functions, tmp, len, function) == NULL) {
  997. zend_hash_next_index_insert_ptr(&ctx.sdl->functions, function);
  998. }
  999. efree(tmp);
  1000. if (function->requestName != NULL && strcmp(function->requestName,function->functionName) != 0) {
  1001. if (ctx.sdl->requests == NULL) {
  1002. ctx.sdl->requests = emalloc(sizeof(HashTable));
  1003. zend_hash_init(ctx.sdl->requests, 0, NULL, NULL, 0);
  1004. }
  1005. tmp = estrdup(function->requestName);
  1006. len = strlen(tmp);
  1007. zend_str_tolower(tmp, len);
  1008. zend_hash_str_add_ptr(ctx.sdl->requests, tmp, len, function);
  1009. efree(tmp);
  1010. }
  1011. }
  1012. trav2 = trav2->next;
  1013. }
  1014. if (!ctx.sdl->bindings) {
  1015. ctx.sdl->bindings = emalloc(sizeof(HashTable));
  1016. zend_hash_init(ctx.sdl->bindings, 0, NULL, delete_binding, 0);
  1017. }
  1018. if (!zend_hash_str_add_ptr(ctx.sdl->bindings, tmpbinding->name, strlen(tmpbinding->name), tmpbinding)) {
  1019. zend_hash_next_index_insert_ptr(ctx.sdl->bindings, tmpbinding);
  1020. }
  1021. trav= trav->next;
  1022. }
  1023. zend_hash_move_forward(&ctx.services);
  1024. }
  1025. } else {
  1026. soap_error0(E_ERROR, "Parsing WSDL: Couldn't bind to service");
  1027. }
  1028. if (ctx.sdl->bindings == NULL || ctx.sdl->bindings->nNumOfElements == 0) {
  1029. soap_error0(E_ERROR, "Parsing WSDL: Could not find any usable binding services in WSDL.");
  1030. }
  1031. } zend_catch {
  1032. /* Avoid persistent memory leak. */
  1033. zend_hash_destroy(&ctx.docs);
  1034. zend_bailout();
  1035. } zend_end_try();
  1036. zend_hash_destroy(&ctx.messages);
  1037. zend_hash_destroy(&ctx.bindings);
  1038. zend_hash_destroy(&ctx.portTypes);
  1039. zend_hash_destroy(&ctx.services);
  1040. zend_hash_destroy(&ctx.docs);
  1041. return ctx.sdl;
  1042. }
  1043. #define WSDL_CACHE_VERSION 0x10
  1044. #define WSDL_CACHE_GET(ret,type,buf) memcpy(&ret,*buf,sizeof(type)); *buf += sizeof(type);
  1045. #define WSDL_CACHE_GET_INT(ret,buf) ret = ((unsigned char)(*buf)[0])|((unsigned char)(*buf)[1]<<8)|((unsigned char)(*buf)[2]<<16)|((unsigned)(*buf)[3]<<24); *buf += 4;
  1046. #define WSDL_CACHE_GET_1(ret,type,buf) ret = (type)(**buf); (*buf)++;
  1047. #define WSDL_CACHE_GET_N(ret,n,buf) memcpy(ret,*buf,n); *buf += n;
  1048. #define WSDL_CACHE_SKIP(n,buf) *buf += n;
  1049. #define WSDL_CACHE_PUT_INT(val,buf) smart_str_appendc(buf,(char)(val & 0xff)); \
  1050. smart_str_appendc(buf,(char)((val >> 8) & 0xff)); \
  1051. smart_str_appendc(buf,(char)((val >> 16) & 0xff)); \
  1052. smart_str_appendc(buf,(char)((val >> 24) & 0xff));
  1053. #define WSDL_CACHE_PUT_1(val,buf) smart_str_appendc(buf,val);
  1054. #define WSDL_CACHE_PUT_N(val,n,buf) smart_str_appendl(buf,(char*)val,n);
  1055. #define WSDL_NO_STRING_MARKER 0x7fffffff
  1056. static char* sdl_deserialize_string(char **in)
  1057. {
  1058. char *s;
  1059. int len;
  1060. WSDL_CACHE_GET_INT(len, in);
  1061. if (len == WSDL_NO_STRING_MARKER) {
  1062. return NULL;
  1063. } else {
  1064. s = emalloc(len+1);
  1065. WSDL_CACHE_GET_N(s, len, in);
  1066. s[len] = '\0';
  1067. return s;
  1068. }
  1069. }
  1070. static void sdl_deserialize_key(HashTable* ht, void* data, char **in)
  1071. {
  1072. int len;
  1073. WSDL_CACHE_GET_INT(len, in);
  1074. if (len == WSDL_NO_STRING_MARKER) {
  1075. zend_hash_next_index_insert_ptr(ht, data);
  1076. } else {
  1077. zend_hash_str_add_ptr(ht, *in, len, data);
  1078. WSDL_CACHE_SKIP(len, in);
  1079. }
  1080. }
  1081. static void sdl_deserialize_attribute(sdlAttributePtr attr, encodePtr *encoders, char **in)
  1082. {
  1083. int i;
  1084. attr->name = sdl_deserialize_string(in);
  1085. attr->namens = sdl_deserialize_string(in);
  1086. attr->ref = sdl_deserialize_string(in);
  1087. attr->def = sdl_deserialize_string(in);
  1088. attr->fixed = sdl_deserialize_string(in);
  1089. WSDL_CACHE_GET_1(attr->form, sdlForm, in);
  1090. WSDL_CACHE_GET_1(attr->use, sdlUse, in);
  1091. WSDL_CACHE_GET_INT(i, in);
  1092. attr->encode = encoders[i];
  1093. WSDL_CACHE_GET_INT(i, in);
  1094. if (i > 0) {
  1095. attr->extraAttributes = emalloc(sizeof(HashTable));
  1096. zend_hash_init(attr->extraAttributes, i, NULL, delete_extra_attribute, 0);
  1097. while (i > 0) {
  1098. sdlExtraAttributePtr x = emalloc(sizeof(sdlExtraAttribute));
  1099. sdl_deserialize_key(attr->extraAttributes, x, in);
  1100. x->ns = sdl_deserialize_string(in);
  1101. x->val = sdl_deserialize_string(in);
  1102. --i;
  1103. }
  1104. }
  1105. }
  1106. static sdlRestrictionIntPtr sdl_deserialize_resriction_int(char **in)
  1107. {
  1108. if (**in == 1) {
  1109. sdlRestrictionIntPtr x = emalloc(sizeof(sdlRestrictionInt));
  1110. WSDL_CACHE_SKIP(1, in);
  1111. WSDL_CACHE_GET_INT(x->value, in);
  1112. WSDL_CACHE_GET_1(x->fixed, char, in);
  1113. return x;
  1114. } else {
  1115. WSDL_CACHE_SKIP(1, in);
  1116. return NULL;
  1117. }
  1118. }
  1119. static sdlRestrictionCharPtr sdl_deserialize_resriction_char(char **in)
  1120. {
  1121. if (**in == 1) {
  1122. sdlRestrictionCharPtr x = emalloc(sizeof(sdlRestrictionChar));
  1123. WSDL_CACHE_SKIP(1, in);
  1124. x->value = sdl_deserialize_string(in);
  1125. WSDL_CACHE_GET_1(x->fixed, char, in);
  1126. return x;
  1127. } else {
  1128. WSDL_CACHE_SKIP(1, in);
  1129. return NULL;
  1130. }
  1131. }
  1132. static sdlContentModelPtr sdl_deserialize_model(sdlTypePtr *types, sdlTypePtr *elements, char **in)
  1133. {
  1134. int i;
  1135. sdlContentModelPtr model = emalloc(sizeof(sdlContentModel));
  1136. WSDL_CACHE_GET_1(model->kind, sdlContentKind, in);
  1137. WSDL_CACHE_GET_INT(model->min_occurs, in);
  1138. WSDL_CACHE_GET_INT(model->max_occurs, in);
  1139. switch (model->kind) {
  1140. case XSD_CONTENT_ELEMENT:
  1141. WSDL_CACHE_GET_INT(i, in);
  1142. model->u.element = elements[i];
  1143. break;
  1144. case XSD_CONTENT_SEQUENCE:
  1145. case XSD_CONTENT_ALL:
  1146. case XSD_CONTENT_CHOICE:
  1147. WSDL_CACHE_GET_INT(i, in);
  1148. model->u.content = emalloc(sizeof(HashTable));
  1149. zend_hash_init(model->u.content, i, NULL, delete_model, 0);
  1150. while (i > 0) {
  1151. sdlContentModelPtr x = sdl_deserialize_model(types, elements, in);
  1152. zend_hash_next_index_insert_ptr(model->u.content, x);
  1153. i--;
  1154. }
  1155. break;
  1156. case XSD_CONTENT_GROUP_REF:
  1157. model->u.group_ref = sdl_deserialize_string(in);
  1158. break;
  1159. case XSD_CONTENT_GROUP:
  1160. WSDL_CACHE_GET_INT(i, in);
  1161. model->u.group = types[i];
  1162. break;
  1163. default:
  1164. break;
  1165. }
  1166. return model;
  1167. }
  1168. static void sdl_deserialize_type(sdlTypePtr type, sdlTypePtr *types, encodePtr *encoders, char **in)
  1169. {
  1170. int i;
  1171. sdlTypePtr *elements = NULL;
  1172. WSDL_CACHE_GET_1(type->kind, sdlTypeKind, in);
  1173. type->name = sdl_deserialize_string(in);
  1174. type->namens = sdl_deserialize_string(in);
  1175. type->def = sdl_deserialize_string(in);
  1176. type->fixed = sdl_deserialize_string(in);
  1177. type->ref = sdl_deserialize_string(in);
  1178. WSDL_CACHE_GET_1(type->nillable, char, in);
  1179. WSDL_CACHE_GET_1(type->form, sdlForm, in);
  1180. WSDL_CACHE_GET_INT(i, in);
  1181. type->encode = encoders[i];
  1182. if (**in == 1) {
  1183. WSDL_CACHE_SKIP(1, in);
  1184. type->restrictions = emalloc(sizeof(sdlRestrictions));
  1185. /*memset(type->restrictions, 0, sizeof(sdlRestrictions));*/
  1186. type->restrictions->minExclusive = sdl_deserialize_resriction_int(in);
  1187. type->restrictions->minInclusive = sdl_deserialize_resriction_int(in);
  1188. type->restrictions->maxExclusive = sdl_deserialize_resriction_int(in);
  1189. type->restrictions->maxInclusive = sdl_deserialize_resriction_int(in);
  1190. type->restrictions->totalDigits = sdl_deserialize_resriction_int(in);
  1191. type->restrictions->fractionDigits = sdl_deserialize_resriction_int(in);
  1192. type->restrictions->length = sdl_deserialize_resriction_int(in);
  1193. type->restrictions->minLength = sdl_deserialize_resriction_int(in);
  1194. type->restrictions->maxLength = sdl_deserialize_resriction_int(in);
  1195. type->restrictions->whiteSpace = sdl_deserialize_resriction_char(in);
  1196. type->restrictions->pattern = sdl_deserialize_resriction_char(in);
  1197. WSDL_CACHE_GET_INT(i, in);
  1198. if (i > 0) {
  1199. type->restrictions->enumeration = emalloc(sizeof(HashTable));
  1200. zend_hash_init(type->restrictions->enumeration, i, NULL, delete_restriction_var_char, 0);
  1201. while (i > 0) {
  1202. sdlRestrictionCharPtr x = sdl_deserialize_resriction_char(in);
  1203. sdl_deserialize_key(type->restrictions->enumeration, x, in);
  1204. --i;
  1205. }
  1206. } else {
  1207. type->restrictions->enumeration = NULL;
  1208. }
  1209. } else {
  1210. WSDL_CACHE_SKIP(1, in);
  1211. }
  1212. WSDL_CACHE_GET_INT(i, in);
  1213. if (i > 0) {
  1214. elements = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1215. elements[0] = NULL;
  1216. type->elements = emalloc(sizeof(HashTable));
  1217. zend_hash_init(type->elements, i, NULL, delete_type, 0);
  1218. while (i > 0) {
  1219. sdlTypePtr t = emalloc(sizeof(sdlType));
  1220. memset(t, 0, sizeof(sdlType));
  1221. sdl_deserialize_key(type->elements, t, in);
  1222. sdl_deserialize_type(t, types, encoders, in);
  1223. elements[i] = t;
  1224. --i;
  1225. }
  1226. }
  1227. WSDL_CACHE_GET_INT(i, in);
  1228. if (i > 0) {
  1229. type->attributes = emalloc(sizeof(HashTable));
  1230. zend_hash_init(type->attributes, i, NULL, delete_attribute, 0);
  1231. while (i > 0) {
  1232. sdlAttributePtr attr = emalloc(sizeof(sdlAttribute));
  1233. memset(attr, 0, sizeof(sdlAttribute));
  1234. sdl_deserialize_key(type->attributes, attr, in);
  1235. sdl_deserialize_attribute(attr, encoders, in);
  1236. --i;
  1237. }
  1238. }
  1239. if (**in != 0) {
  1240. WSDL_CACHE_SKIP(1, in);
  1241. type->model = sdl_deserialize_model(types, elements, in);
  1242. } else {
  1243. WSDL_CACHE_SKIP(1, in);
  1244. }
  1245. if (elements != NULL) {
  1246. efree(elements);
  1247. }
  1248. }
  1249. static void sdl_deserialize_encoder(encodePtr enc, sdlTypePtr *types, char **in)
  1250. {
  1251. int i;
  1252. WSDL_CACHE_GET_INT(enc->details.type, in);
  1253. enc->details.type_str = sdl_deserialize_string(in);
  1254. enc->details.ns = sdl_deserialize_string(in);
  1255. WSDL_CACHE_GET_INT(i, in);
  1256. enc->details.sdl_type = types[i];
  1257. enc->to_xml = sdl_guess_convert_xml;
  1258. enc->to_zval = sdl_guess_convert_zval;
  1259. if (enc->details.sdl_type == NULL) {
  1260. int ns_len = strlen(enc->details.ns);
  1261. int type_len = strlen(enc->details.type_str);
  1262. if (((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  1263. memcmp(enc->details.ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  1264. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  1265. memcmp(enc->details.ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  1266. char *enc_nscat;
  1267. int enc_ns_len;
  1268. int enc_len;
  1269. encodePtr real_enc;
  1270. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  1271. enc_len = enc_ns_len + type_len + 1;
  1272. enc_nscat = emalloc(enc_len + 1);
  1273. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  1274. enc_nscat[enc_ns_len] = ':';
  1275. memcpy(enc_nscat+enc_ns_len+1, enc->details.type_str, type_len);
  1276. enc_nscat[enc_len] = '\0';
  1277. real_enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  1278. efree(enc_nscat);
  1279. if (real_enc) {
  1280. enc->to_zval = real_enc->to_zval;
  1281. enc->to_xml = real_enc->to_xml;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. static void sdl_deserialize_soap_body(sdlSoapBindingFunctionBodyPtr body, encodePtr *encoders, sdlTypePtr *types, char **in)
  1287. {
  1288. int i, j, n;
  1289. WSDL_CACHE_GET_1(body->use, sdlEncodingUse, in);
  1290. if (body->use == SOAP_ENCODED) {
  1291. WSDL_CACHE_GET_1(body->encodingStyle, sdlRpcEncodingStyle, in);
  1292. } else {
  1293. body->encodingStyle = SOAP_ENCODING_DEFAULT;
  1294. }
  1295. body->ns = sdl_deserialize_string(in);
  1296. WSDL_CACHE_GET_INT(i, in);
  1297. if (i > 0) {
  1298. body->headers = emalloc(sizeof(HashTable));
  1299. zend_hash_init(body->headers, i, NULL, delete_header, 0);
  1300. while (i > 0) {
  1301. sdlSoapBindingFunctionHeaderPtr tmp = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1302. memset(tmp, 0, sizeof(sdlSoapBindingFunctionHeader));
  1303. sdl_deserialize_key(body->headers, tmp, in);
  1304. WSDL_CACHE_GET_1(tmp->use, sdlEncodingUse, in);
  1305. if (tmp->use == SOAP_ENCODED) {
  1306. WSDL_CACHE_GET_1(tmp->encodingStyle, sdlRpcEncodingStyle, in);
  1307. } else {
  1308. tmp->encodingStyle = SOAP_ENCODING_DEFAULT;
  1309. }
  1310. tmp->name = sdl_deserialize_string(in);
  1311. tmp->ns = sdl_deserialize_string(in);
  1312. WSDL_CACHE_GET_INT(n, in);
  1313. tmp->encode = encoders[n];
  1314. WSDL_CACHE_GET_INT(n, in);
  1315. tmp->element = types[n];
  1316. --i;
  1317. WSDL_CACHE_GET_INT(j, in);
  1318. if (j > 0) {
  1319. tmp->headerfaults = emalloc(sizeof(HashTable));
  1320. zend_hash_init(tmp->headerfaults, i, NULL, delete_header, 0);
  1321. while (j > 0) {
  1322. sdlSoapBindingFunctionHeaderPtr tmp2 = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1323. memset(tmp2, 0, sizeof(sdlSoapBindin

Large files files are truncated, but you can click here to view the full file