PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/soap/php_sdl.c

http://github.com/php/php-src
C | 3670 lines | 3137 code | 459 blank | 74 comment | 878 complexity | 95c3ae281b354782b9b1b33bf0ae0eee 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. | http://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_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. static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include)
  282. {
  283. sdlPtr tmpsdl = ctx->sdl;
  284. xmlDocPtr wsdl;
  285. xmlNodePtr root, definitions, trav;
  286. xmlAttrPtr targetNamespace;
  287. if (zend_hash_str_exists(&ctx->docs, struri, strlen(struri))) {
  288. return;
  289. }
  290. sdl_set_uri_credentials(ctx, struri);
  291. wsdl = soap_xmlParseFile(struri);
  292. sdl_restore_uri_credentials(ctx);
  293. if (!wsdl) {
  294. xmlErrorPtr xmlErrorPtr = xmlGetLastError();
  295. if (xmlErrorPtr) {
  296. soap_error2(E_ERROR, "Parsing WSDL: Couldn't load from '%s' : %s", struri, xmlErrorPtr->message);
  297. } else {
  298. soap_error1(E_ERROR, "Parsing WSDL: Couldn't load from '%s'", struri);
  299. }
  300. }
  301. zend_hash_str_add_ptr(&ctx->docs, struri, strlen(struri), wsdl);
  302. root = wsdl->children;
  303. definitions = get_node_ex(root, "definitions", WSDL_NAMESPACE);
  304. if (!definitions) {
  305. if (include) {
  306. xmlNodePtr schema = get_node_ex(root, "schema", XSD_NAMESPACE);
  307. if (schema) {
  308. load_schema(ctx, schema);
  309. return;
  310. }
  311. }
  312. soap_error1(E_ERROR, "Parsing WSDL: Couldn't find <definitions> in '%s'", struri);
  313. }
  314. if (!include) {
  315. targetNamespace = get_attribute(definitions->properties, "targetNamespace");
  316. if (targetNamespace) {
  317. tmpsdl->target_ns = estrdup((char*)targetNamespace->children->content);
  318. }
  319. }
  320. trav = definitions->children;
  321. while (trav != NULL) {
  322. if (!is_wsdl_element(trav)) {
  323. trav = trav->next;
  324. continue;
  325. }
  326. if (node_is_equal(trav,"types")) {
  327. /* TODO: Only one "types" is allowed */
  328. xmlNodePtr trav2 = trav->children;
  329. while (trav2 != NULL) {
  330. if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
  331. load_schema(ctx, trav2);
  332. } else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  333. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
  334. }
  335. trav2 = trav2->next;
  336. }
  337. } else if (node_is_equal(trav,"import")) {
  338. /* TODO: namespace ??? */
  339. xmlAttrPtr tmp = get_attribute(trav->properties, "location");
  340. if (tmp) {
  341. xmlChar *uri;
  342. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  343. if (base == NULL) {
  344. uri = xmlBuildURI(tmp->children->content, trav->doc->URL);
  345. } else {
  346. uri = xmlBuildURI(tmp->children->content, base);
  347. xmlFree(base);
  348. }
  349. load_wsdl_ex(this_ptr, (char*)uri, ctx, 1);
  350. xmlFree(uri);
  351. }
  352. } else if (node_is_equal(trav,"message")) {
  353. xmlAttrPtr name = get_attribute(trav->properties, "name");
  354. if (name && name->children && name->children->content) {
  355. if (zend_hash_str_add_ptr(&ctx->messages, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  356. soap_error1(E_ERROR, "Parsing WSDL: <message> '%s' already defined", name->children->content);
  357. }
  358. } else {
  359. soap_error0(E_ERROR, "Parsing WSDL: <message> has no name attribute");
  360. }
  361. } else if (node_is_equal(trav,"portType")) {
  362. xmlAttrPtr name = get_attribute(trav->properties, "name");
  363. if (name && name->children && name->children->content) {
  364. if (zend_hash_str_add_ptr(&ctx->portTypes, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  365. soap_error1(E_ERROR, "Parsing WSDL: <portType> '%s' already defined", name->children->content);
  366. }
  367. } else {
  368. soap_error0(E_ERROR, "Parsing WSDL: <portType> has no name attribute");
  369. }
  370. } else if (node_is_equal(trav,"binding")) {
  371. xmlAttrPtr name = get_attribute(trav->properties, "name");
  372. if (name && name->children && name->children->content) {
  373. if (zend_hash_str_add_ptr(&ctx->bindings, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  374. soap_error1(E_ERROR, "Parsing WSDL: <binding> '%s' already defined", name->children->content);
  375. }
  376. } else {
  377. soap_error0(E_ERROR, "Parsing WSDL: <binding> has no name attribute");
  378. }
  379. } else if (node_is_equal(trav,"service")) {
  380. xmlAttrPtr name = get_attribute(trav->properties, "name");
  381. if (name && name->children && name->children->content) {
  382. if (zend_hash_str_add_ptr(&ctx->services, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  383. soap_error1(E_ERROR, "Parsing WSDL: <service> '%s' already defined", name->children->content);
  384. }
  385. } else {
  386. soap_error0(E_ERROR, "Parsing WSDL: <service> has no name attribute");
  387. }
  388. } else if (!node_is_equal(trav,"documentation")) {
  389. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  390. }
  391. trav = trav->next;
  392. }
  393. }
  394. static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xmlNodePtr header, char* wsdl_soap_namespace, int fault)
  395. {
  396. xmlAttrPtr tmp;
  397. xmlNodePtr message, part;
  398. char *ctype;
  399. sdlSoapBindingFunctionHeaderPtr h;
  400. tmp = get_attribute(header->properties, "message");
  401. if (!tmp) {
  402. soap_error0(E_ERROR, "Parsing WSDL: Missing message attribute for <header>");
  403. }
  404. ctype = strrchr((char*)tmp->children->content,':');
  405. if (ctype == NULL) {
  406. ctype = (char*)tmp->children->content;
  407. } else {
  408. ++ctype;
  409. }
  410. if ((message = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  411. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", tmp->children->content);
  412. }
  413. tmp = get_attribute(header->properties, "part");
  414. if (!tmp) {
  415. soap_error0(E_ERROR, "Parsing WSDL: Missing part attribute for <header>");
  416. }
  417. part = get_node_with_attribute_ex(message->children, "part", WSDL_NAMESPACE, "name", (char*)tmp->children->content, NULL);
  418. if (!part) {
  419. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", tmp->children->content);
  420. }
  421. h = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  422. memset(h, 0, sizeof(sdlSoapBindingFunctionHeader));
  423. h->name = estrdup((char*)tmp->children->content);
  424. tmp = get_attribute(header->properties, "use");
  425. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  426. h->use = SOAP_ENCODED;
  427. } else {
  428. h->use = SOAP_LITERAL;
  429. }
  430. tmp = get_attribute(header->properties, "namespace");
  431. if (tmp) {
  432. h->ns = estrdup((char*)tmp->children->content);
  433. }
  434. if (h->use == SOAP_ENCODED) {
  435. tmp = get_attribute(header->properties, "encodingStyle");
  436. if (tmp) {
  437. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  438. h->encodingStyle = SOAP_ENCODING_1_1;
  439. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  440. h->encodingStyle = SOAP_ENCODING_1_2;
  441. } else {
  442. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  443. }
  444. } else {
  445. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  446. }
  447. }
  448. tmp = get_attribute(part->properties, "type");
  449. if (tmp != NULL) {
  450. h->encode = get_encoder_from_prefix(ctx->sdl, part, tmp->children->content);
  451. } else {
  452. tmp = get_attribute(part->properties, "element");
  453. if (tmp != NULL) {
  454. h->element = get_element(ctx->sdl, part, tmp->children->content);
  455. if (h->element) {
  456. h->encode = h->element->encode;
  457. if (!h->ns && h->element->namens) {
  458. h->ns = estrdup(h->element->namens);
  459. }
  460. if (h->element->name) {
  461. efree(h->name);
  462. h->name = estrdup(h->element->name);
  463. }
  464. }
  465. }
  466. }
  467. if (!fault) {
  468. xmlNodePtr trav = header->children;
  469. while (trav != NULL) {
  470. if (node_is_equal_ex(trav, "headerfault", wsdl_soap_namespace)) {
  471. sdlSoapBindingFunctionHeaderPtr hf = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 1);
  472. smart_str key = {0};
  473. if (h->headerfaults == NULL) {
  474. h->headerfaults = emalloc(sizeof(HashTable));
  475. zend_hash_init(h->headerfaults, 0, NULL, delete_header, 0);
  476. }
  477. if (hf->ns) {
  478. smart_str_appends(&key,hf->ns);
  479. smart_str_appendc(&key,':');
  480. }
  481. smart_str_appends(&key,hf->name);
  482. smart_str_0(&key);
  483. if (zend_hash_add_ptr(h->headerfaults, key.s, hf) == NULL) {
  484. delete_header_int(hf);
  485. }
  486. smart_str_free(&key);
  487. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  488. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  489. }
  490. trav = trav->next;
  491. }
  492. }
  493. return h;
  494. }
  495. static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap_namespace, sdlSoapBindingFunctionBody *binding, HashTable* params)
  496. {
  497. xmlNodePtr body, trav;
  498. xmlAttrPtr tmp;
  499. trav = node->children;
  500. while (trav != NULL) {
  501. if (node_is_equal_ex(trav, "body", wsdl_soap_namespace)) {
  502. body = trav;
  503. tmp = get_attribute(body->properties, "use");
  504. if (tmp && !strncmp((char*)tmp->children->content, "literal", sizeof("literal"))) {
  505. binding->use = SOAP_LITERAL;
  506. } else {
  507. binding->use = SOAP_ENCODED;
  508. }
  509. tmp = get_attribute(body->properties, "namespace");
  510. if (tmp) {
  511. binding->ns = estrdup((char*)tmp->children->content);
  512. }
  513. tmp = get_attribute(body->properties, "parts");
  514. if (tmp) {
  515. HashTable ht;
  516. char *parts = (char*)tmp->children->content;
  517. /* Delete all parts those are not in the "parts" attribute */
  518. zend_hash_init(&ht, 0, NULL, delete_parameter, 0);
  519. while (*parts) {
  520. sdlParamPtr param;
  521. int found = 0;
  522. char *end;
  523. while (*parts == ' ') ++parts;
  524. if (*parts == '\0') break;
  525. end = strchr(parts, ' ');
  526. if (end) *end = '\0';
  527. ZEND_HASH_FOREACH_PTR(params, param) {
  528. if (param->paramName &&
  529. strcmp(parts, param->paramName) == 0) {
  530. sdlParamPtr x_param;
  531. x_param = emalloc(sizeof(sdlParam));
  532. *x_param = *param;
  533. param->paramName = NULL;
  534. zend_hash_next_index_insert_ptr(&ht, x_param);
  535. found = 1;
  536. break;
  537. }
  538. } ZEND_HASH_FOREACH_END();
  539. if (!found) {
  540. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", parts);
  541. }
  542. parts += strlen(parts);
  543. if (end) *end = ' ';
  544. }
  545. zend_hash_destroy(params);
  546. *params = ht;
  547. }
  548. if (binding->use == SOAP_ENCODED) {
  549. tmp = get_attribute(body->properties, "encodingStyle");
  550. if (tmp) {
  551. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  552. binding->encodingStyle = SOAP_ENCODING_1_1;
  553. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  554. binding->encodingStyle = SOAP_ENCODING_1_2;
  555. } else {
  556. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  557. }
  558. } else {
  559. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  560. }
  561. }
  562. } else if (node_is_equal_ex(trav, "header", wsdl_soap_namespace)) {
  563. sdlSoapBindingFunctionHeaderPtr h = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 0);
  564. smart_str key = {0};
  565. if (binding->headers == NULL) {
  566. binding->headers = emalloc(sizeof(HashTable));
  567. zend_hash_init(binding->headers, 0, NULL, delete_header, 0);
  568. }
  569. if (h->ns) {
  570. smart_str_appends(&key,h->ns);
  571. smart_str_appendc(&key,':');
  572. }
  573. smart_str_appends(&key,h->name);
  574. smart_str_0(&key);
  575. if (zend_hash_add_ptr(binding->headers, key.s, h) == NULL) {
  576. delete_header_int(h);
  577. }
  578. smart_str_free(&key);
  579. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  580. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  581. }
  582. trav = trav->next;
  583. }
  584. }
  585. static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
  586. {
  587. xmlNodePtr trav, part, message = NULL, tmp;
  588. HashTable* parameters = NULL;
  589. char *ctype;
  590. ctype = strrchr((char*)message_name,':');
  591. if (ctype == NULL) {
  592. ctype = (char*)message_name;
  593. } else {
  594. ++ctype;
  595. }
  596. if ((tmp = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  597. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", message_name);
  598. }
  599. message = tmp;
  600. parameters = emalloc(sizeof(HashTable));
  601. zend_hash_init(parameters, 0, NULL, delete_parameter, 0);
  602. trav = message->children;
  603. while (trav != NULL) {
  604. xmlAttrPtr element, type, name;
  605. sdlParamPtr param;
  606. if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
  607. soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>", trav->name);
  608. }
  609. if (node_is_equal(trav,"documentation")) {
  610. trav = trav->next;
  611. continue;
  612. }
  613. if (!node_is_equal(trav,"part")) {
  614. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  615. }
  616. part = trav;
  617. param = emalloc(sizeof(sdlParam));
  618. memset(param,0,sizeof(sdlParam));
  619. param->order = 0;
  620. name = get_attribute(part->properties, "name");
  621. if (name == NULL) {
  622. soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'", message->name);
  623. }
  624. param->paramName = estrdup((char*)name->children->content);
  625. type = get_attribute(part->properties, "type");
  626. if (type != NULL) {
  627. param->encode = get_encoder_from_prefix(ctx->sdl, part, type->children->content);
  628. } else {
  629. element = get_attribute(part->properties, "element");
  630. if (element != NULL) {
  631. param->element = get_element(ctx->sdl, part, element->children->content);
  632. if (param->element) {
  633. param->encode = param->element->encode;
  634. }
  635. }
  636. }
  637. zend_hash_next_index_insert_ptr(parameters, param);
  638. trav = trav->next;
  639. }
  640. return parameters;
  641. }
  642. static sdlPtr load_wsdl(zval *this_ptr, char *struri)
  643. {
  644. sdlCtx ctx;
  645. int i,n;
  646. memset(&ctx,0,sizeof(ctx));
  647. ctx.sdl = emalloc(sizeof(sdl));
  648. memset(ctx.sdl, 0, sizeof(sdl));
  649. ctx.sdl->source = estrdup(struri);
  650. zend_hash_init(&ctx.sdl->functions, 0, NULL, delete_function, 0);
  651. zend_hash_init(&ctx.docs, 0, NULL, delete_document, 0);
  652. zend_hash_init(&ctx.messages, 0, NULL, NULL, 0);
  653. zend_hash_init(&ctx.bindings, 0, NULL, NULL, 0);
  654. zend_hash_init(&ctx.portTypes, 0, NULL, NULL, 0);
  655. zend_hash_init(&ctx.services, 0, NULL, NULL, 0);
  656. load_wsdl_ex(this_ptr, struri, &ctx, 0);
  657. zend_try {
  658. schema_pass2(&ctx);
  659. n = zend_hash_num_elements(&ctx.services);
  660. if (n > 0) {
  661. zend_hash_internal_pointer_reset(&ctx.services);
  662. for (i = 0; i < n; i++) {
  663. xmlNodePtr service, tmp;
  664. xmlNodePtr trav, port;
  665. int has_soap_port = 0;
  666. service = tmp = zend_hash_get_current_data_ptr(&ctx.services);
  667. trav = service->children;
  668. while (trav != NULL) {
  669. xmlAttrPtr type, name, bindingAttr, location;
  670. xmlNodePtr portType, operation;
  671. xmlNodePtr address, binding, trav2;
  672. char *ctype;
  673. sdlBindingPtr tmpbinding;
  674. char *wsdl_soap_namespace = NULL;
  675. if (!is_wsdl_element(trav) || node_is_equal(trav,"documentation")) {
  676. trav = trav->next;
  677. continue;
  678. }
  679. if (!node_is_equal(trav,"port")) {
  680. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  681. }
  682. port = trav;
  683. tmpbinding = emalloc(sizeof(sdlBinding));
  684. memset(tmpbinding, 0, sizeof(sdlBinding));
  685. bindingAttr = get_attribute(port->properties, "binding");
  686. if (bindingAttr == NULL) {
  687. soap_error0(E_ERROR, "Parsing WSDL: No binding associated with <port>");
  688. }
  689. /* find address and figure out binding type */
  690. address = NULL;
  691. trav2 = port->children;
  692. while (trav2 != NULL) {
  693. if (node_is_equal(trav2,"address") && trav2->ns) {
  694. if (!strncmp((char*)trav2->ns->href, WSDL_SOAP11_NAMESPACE, sizeof(WSDL_SOAP11_NAMESPACE))) {
  695. address = trav2;
  696. wsdl_soap_namespace = WSDL_SOAP11_NAMESPACE;
  697. tmpbinding->bindingType = BINDING_SOAP;
  698. } else if (!strncmp((char*)trav2->ns->href, WSDL_SOAP12_NAMESPACE, sizeof(WSDL_SOAP12_NAMESPACE))) {
  699. address = trav2;
  700. wsdl_soap_namespace = WSDL_SOAP12_NAMESPACE;
  701. tmpbinding->bindingType = BINDING_SOAP;
  702. } else if (!strncmp((char*)trav2->ns->href, RPC_SOAP12_NAMESPACE, sizeof(RPC_SOAP12_NAMESPACE))) {
  703. address = trav2;
  704. wsdl_soap_namespace = RPC_SOAP12_NAMESPACE;
  705. tmpbinding->bindingType = BINDING_SOAP;
  706. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP11_NAMESPACE, sizeof(WSDL_HTTP11_NAMESPACE))) {
  707. address = trav2;
  708. tmpbinding->bindingType = BINDING_HTTP;
  709. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP12_NAMESPACE, sizeof(WSDL_HTTP12_NAMESPACE))) {
  710. address = trav2;
  711. tmpbinding->bindingType = BINDING_HTTP;
  712. }
  713. }
  714. if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  715. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
  716. }
  717. trav2 = trav2->next;
  718. }
  719. if (!address || tmpbinding->bindingType == BINDING_HTTP) {
  720. if (has_soap_port || trav->next || i < n-1) {
  721. efree(tmpbinding);
  722. trav = trav->next;
  723. continue;
  724. } else if (!address) {
  725. soap_error0(E_ERROR, "Parsing WSDL: No address associated with <port>");
  726. }
  727. }
  728. has_soap_port = 1;
  729. location = get_attribute(address->properties, "location");
  730. if (!location) {
  731. soap_error0(E_ERROR, "Parsing WSDL: No location associated with <port>");
  732. }
  733. tmpbinding->location = estrdup((char*)location->children->content);
  734. ctype = strrchr((char*)bindingAttr->children->content,':');
  735. if (ctype == NULL) {
  736. ctype = (char*)bindingAttr->children->content;
  737. } else {
  738. ++ctype;
  739. }
  740. if ((tmp = zend_hash_str_find_ptr(&ctx.bindings, ctype, strlen(ctype))) == NULL) {
  741. soap_error1(E_ERROR, "Parsing WSDL: No <binding> element with name '%s'", ctype);
  742. }
  743. binding = tmp;
  744. if (tmpbinding->bindingType == BINDING_SOAP) {
  745. sdlSoapBindingPtr soapBinding;
  746. xmlNodePtr soapBindingNode;
  747. xmlAttrPtr tmp;
  748. soapBinding = emalloc(sizeof(sdlSoapBinding));
  749. memset(soapBinding, 0, sizeof(sdlSoapBinding));
  750. soapBinding->style = SOAP_DOCUMENT;
  751. soapBindingNode = get_node_ex(binding->children, "binding", wsdl_soap_namespace);
  752. if (soapBindingNode) {
  753. tmp = get_attribute(soapBindingNode->properties, "style");
  754. if (tmp && !strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  755. soapBinding->style = SOAP_RPC;
  756. }
  757. tmp = get_attribute(soapBindingNode->properties, "transport");
  758. if (tmp) {
  759. if (strncmp((char*)tmp->children->content, WSDL_HTTP_TRANSPORT, sizeof(WSDL_HTTP_TRANSPORT)) == 0) {
  760. soapBinding->transport = SOAP_TRANSPORT_HTTP;
  761. } else {
  762. /* try the next binding */
  763. efree(soapBinding);
  764. efree(tmpbinding->location);
  765. efree(tmpbinding);
  766. trav = trav->next;
  767. continue;
  768. }
  769. }
  770. }
  771. tmpbinding->bindingAttributes = (void *)soapBinding;
  772. }
  773. name = get_attribute(binding->properties, "name");
  774. if (name == NULL) {
  775. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <binding>");
  776. }
  777. tmpbinding->name = estrdup((char*)name->children->content);
  778. type = get_attribute(binding->properties, "type");
  779. if (type == NULL) {
  780. soap_error0(E_ERROR, "Parsing WSDL: Missing 'type' attribute for <binding>");
  781. }
  782. ctype = strrchr((char*)type->children->content,':');
  783. if (ctype == NULL) {
  784. ctype = (char*)type->children->content;
  785. } else {
  786. ++ctype;
  787. }
  788. if ((tmp = zend_hash_str_find_ptr(&ctx.portTypes, ctype, strlen(ctype))) == NULL) {
  789. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType> with name '%s'", name->children->content);
  790. }
  791. portType = tmp;
  792. trav2 = binding->children;
  793. while (trav2 != NULL) {
  794. sdlFunctionPtr function;
  795. xmlNodePtr input, output, fault, portTypeOperation, trav3;
  796. xmlAttrPtr op_name, paramOrder;
  797. if ((tmpbinding->bindingType == BINDING_SOAP &&
  798. node_is_equal_ex(trav2, "binding", wsdl_soap_namespace)) ||
  799. !is_wsdl_element(trav2) ||
  800. node_is_equal(trav2,"documentation")) {
  801. trav2 = trav2->next;
  802. continue;
  803. }
  804. if (!node_is_equal(trav2,"operation")) {
  805. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
  806. }
  807. operation = trav2;
  808. op_name = get_attribute(operation->properties, "name");
  809. if (op_name == NULL) {
  810. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <operation>");
  811. }
  812. trav3 = operation->children;
  813. while (trav3 != NULL) {
  814. if (tmpbinding->bindingType == BINDING_SOAP &&
  815. node_is_equal_ex(trav3, "operation", wsdl_soap_namespace)) {
  816. } else if (is_wsdl_element(trav3) &&
  817. !node_is_equal(trav3,"input") &&
  818. !node_is_equal(trav3,"output") &&
  819. !node_is_equal(trav3,"fault") &&
  820. !node_is_equal(trav3,"documentation")) {
  821. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav3->name);
  822. }
  823. trav3 = trav3->next;
  824. }
  825. portTypeOperation = get_node_with_attribute_ex(portType->children, "operation", WSDL_NAMESPACE, "name", (char*)op_name->children->content, NULL);
  826. if (portTypeOperation == NULL) {
  827. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType>/<operation> with name '%s'", op_name->children->content);
  828. }
  829. function = emalloc(sizeof(sdlFunction));
  830. memset(function, 0, sizeof(sdlFunction));
  831. function->functionName = estrdup((char*)op_name->children->content);
  832. if (tmpbinding->bindingType == BINDING_SOAP) {
  833. sdlSoapBindingFunctionPtr soapFunctionBinding;
  834. sdlSoapBindingPtr soapBinding;
  835. xmlNodePtr soapOperation;
  836. xmlAttrPtr tmp;
  837. soapFunctionBinding = emalloc(sizeof(sdlSoapBindingFunction));
  838. memset(soapFunctionBinding, 0, sizeof(sdlSoapBindingFunction));
  839. soapBinding = (sdlSoapBindingPtr)tmpbinding->bindingAttributes;
  840. soapFunctionBinding->style = soapBinding->style;
  841. soapOperation = get_node_ex(operation->children, "operation", wsdl_soap_namespace);
  842. if (soapOperation) {
  843. tmp = get_attribute(soapOperation->properties, "soapAction");
  844. if (tmp) {
  845. soapFunctionBinding->soapAction = estrdup((char*)tmp->children->content);
  846. }
  847. tmp = get_attribute(soapOperation->properties, "style");
  848. if (tmp) {
  849. if (!strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  850. soapFunctionBinding->style = SOAP_RPC;
  851. } else {
  852. soapFunctionBinding->style = SOAP_DOCUMENT;
  853. }
  854. } else {
  855. soapFunctionBinding->style = soapBinding->style;
  856. }
  857. }
  858. function->bindingAttributes = (void *)soapFunctionBinding;
  859. }
  860. input = get_node_ex(portTypeOperation->children, "input", WSDL_NAMESPACE);
  861. if (input != NULL) {
  862. xmlAttrPtr message;
  863. message = get_attribute(input->properties, "message");
  864. if (message == NULL) {
  865. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <input> of '%s'", op_name->children->content);
  866. }
  867. function->requestParameters = wsdl_message(&ctx, message->children->content);
  868. /* FIXME
  869. xmlAttrPtr name = get_attribute(input->properties, "name");
  870. if (name != NULL) {
  871. function->requestName = estrdup(name->children->content);
  872. } else {
  873. */
  874. {
  875. function->requestName = estrdup(function->functionName);
  876. }
  877. if (tmpbinding->bindingType == BINDING_SOAP) {
  878. input = get_node_ex(operation->children, "input", WSDL_NAMESPACE);
  879. if (input != NULL) {
  880. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  881. wsdl_soap_binding_body(&ctx, input, wsdl_soap_namespace, &soapFunctionBinding->input, function->requestParameters);
  882. }
  883. }
  884. }
  885. output = get_node_ex(portTypeOperation->children, "output", WSDL_NAMESPACE);
  886. if (output != NULL) {
  887. xmlAttrPtr message;
  888. message = get_attribute(output->properties, "message");
  889. if (message == NULL) {
  890. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  891. }
  892. function->responseParameters = wsdl_message(&ctx, message->children->content);
  893. /* FIXME
  894. xmlAttrPtr name = get_attribute(output->properties, "name");
  895. if (name != NULL) {
  896. function->responseName = estrdup(name->children->content);
  897. } else if (input == NULL) {
  898. function->responseName = estrdup(function->functionName);
  899. } else {
  900. */
  901. {
  902. int len = strlen(function->functionName);
  903. function->responseName = emalloc(len + sizeof("Response"));
  904. memcpy(function->responseName, function->functionName, len);
  905. memcpy(function->responseName+len, "Response", sizeof("Response"));
  906. }
  907. if (tmpbinding->bindingType == BINDING_SOAP) {
  908. output = get_node_ex(operation->children, "output", WSDL_NAMESPACE);
  909. if (output != NULL) {
  910. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  911. wsdl_soap_binding_body(&ctx, output, wsdl_soap_namespace, &soapFunctionBinding->output, function->responseParameters);
  912. }
  913. }
  914. }
  915. paramOrder = get_attribute(portTypeOperation->properties, "parameterOrder");
  916. if (paramOrder) {
  917. /* FIXME: */
  918. }
  919. fault = portTypeOperation->children;
  920. while (fault != NULL) {
  921. if (node_is_equal_ex(fault, "fault", WSDL_NAMESPACE)) {
  922. xmlAttrPtr message, name;
  923. sdlFaultPtr f;
  924. name = get_attribute(fault->properties, "name");
  925. if (name == NULL) {
  926. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <fault> of '%s'", op_name->children->content);
  927. }
  928. message = get_attribute(fault->properties, "message");
  929. if (message == NULL) {
  930. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  931. }
  932. f = emalloc(sizeof(sdlFault));
  933. memset(f, 0, sizeof(sdlFault));
  934. f->name = estrdup((char*)name->children->content);
  935. f->details = wsdl_message(&ctx, message->children->content);
  936. if (f->details == NULL || zend_hash_num_elements(f->details) > 1) {
  937. soap_error1(E_ERROR, "Parsing WSDL: The fault message '%s' must have a single part", message->children->content);
  938. }
  939. if (tmpbinding->bindingType == BINDING_SOAP) {
  940. xmlNodePtr soap_fault = get_node_with_attribute_ex(operation->children, "fault", WSDL_NAMESPACE, "name", f->name, NULL);
  941. if (soap_fault != NULL) {
  942. xmlNodePtr trav = soap_fault->children;
  943. while (trav != NULL) {
  944. if (node_is_equal_ex(trav, "fault", wsdl_soap_namespace)) {
  945. xmlAttrPtr tmp;
  946. sdlSoapBindingFunctionFaultPtr binding;
  947. binding = f->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  948. memset(f->bindingAttributes, 0, sizeof(sdlSoapBindingFunctionFault));
  949. tmp = get_attribute(trav->properties, "use");
  950. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  951. binding->use = SOAP_ENCODED;
  952. } else {
  953. binding->use = SOAP_LITERAL;
  954. }
  955. tmp = get_attribute(trav->properties, "namespace");
  956. if (tmp) {
  957. binding->ns = estrdup((char*)tmp->children->content);
  958. }
  959. if (binding->use == SOAP_ENCODED) {
  960. tmp = get_attribute(trav->properties, "encodingStyle");
  961. if (tmp) {
  962. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  963. binding->encodingStyle = SOAP_ENCODING_1_1;
  964. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  965. binding->encodingStyle = SOAP_ENCODING_1_2;
  966. } else {
  967. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  968. }
  969. } else {
  970. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  971. }
  972. }
  973. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  974. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  975. }
  976. trav = trav->next;
  977. }
  978. }
  979. }
  980. if (function->faults == NULL) {
  981. function->faults = emalloc(sizeof(HashTable));
  982. zend_hash_init(function->faults, 0, NULL, delete_fault, 0);
  983. }
  984. if (zend_hash_str_add_ptr(function->faults, f->name, strlen(f->name), f) == NULL) {
  985. soap_error2(E_ERROR, "Parsing WSDL: <fault> with name '%s' already defined in '%s'", f->name, op_name->children->content);
  986. }
  987. }
  988. fault = fault->next;
  989. }
  990. function->binding = tmpbinding;
  991. {
  992. char *tmp = estrdup(function->functionName);
  993. int len = strlen(tmp);
  994. if (zend_hash_str_add_ptr(&ctx.sdl->functions, php_strtolower(tmp, len), len, function) == NULL) {
  995. zend_hash_next_index_insert_ptr(&ctx.sdl->functions, function);
  996. }
  997. efree(tmp);
  998. if (function->requestName != NULL && strcmp(function->requestName,function->functionName) != 0) {
  999. if (ctx.sdl->requests == NULL) {
  1000. ctx.sdl->requests = emalloc(sizeof(HashTable));
  1001. zend_hash_init(ctx.sdl->requests, 0, NULL, NULL, 0);
  1002. }
  1003. tmp = estrdup(function->requestName);
  1004. len = strlen(tmp);
  1005. zend_hash_str_add_ptr(ctx.sdl->requests, php_strtolower(tmp, len), len, function);
  1006. efree(tmp);
  1007. }
  1008. }
  1009. trav2 = trav2->next;
  1010. }
  1011. if (!ctx.sdl->bindings) {
  1012. ctx.sdl->bindings = emalloc(sizeof(HashTable));
  1013. zend_hash_init(ctx.sdl->bindings, 0, NULL, delete_binding, 0);
  1014. }
  1015. if (!zend_hash_str_add_ptr(ctx.sdl->bindings, tmpbinding->name, strlen(tmpbinding->name), tmpbinding)) {
  1016. zend_hash_next_index_insert_ptr(ctx.sdl->bindings, tmpbinding);
  1017. }
  1018. trav= trav->next;
  1019. }
  1020. zend_hash_move_forward(&ctx.services);
  1021. }
  1022. } else {
  1023. soap_error0(E_ERROR, "Parsing WSDL: Couldn't bind to service");
  1024. }
  1025. if (ctx.sdl->bindings == NULL || ctx.sdl->bindings->nNumOfElements == 0) {
  1026. soap_error0(E_ERROR, "Parsing WSDL: Could not find any usable binding services in WSDL.");
  1027. }
  1028. } zend_catch {
  1029. /* Avoid persistent memory leak. */
  1030. zend_hash_destroy(&ctx.docs);
  1031. zend_bailout();
  1032. } zend_end_try();
  1033. zend_hash_destroy(&ctx.messages);
  1034. zend_hash_destroy(&ctx.bindings);
  1035. zend_hash_destroy(&ctx.portTypes);
  1036. zend_hash_destroy(&ctx.services);
  1037. zend_hash_destroy(&ctx.docs);
  1038. return ctx.sdl;
  1039. }
  1040. #define WSDL_CACHE_VERSION 0x10
  1041. #define WSDL_CACHE_GET(ret,type,buf) memcpy(&ret,*buf,sizeof(type)); *buf += sizeof(type);
  1042. #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;
  1043. #define WSDL_CACHE_GET_1(ret,type,buf) ret = (type)(**buf); (*buf)++;
  1044. #define WSDL_CACHE_GET_N(ret,n,buf) memcpy(ret,*buf,n); *buf += n;
  1045. #define WSDL_CACHE_SKIP(n,buf) *buf += n;
  1046. #define WSDL_CACHE_PUT_INT(val,buf) smart_str_appendc(buf,(char)(val & 0xff)); \
  1047. smart_str_appendc(buf,(char)((val >> 8) & 0xff)); \
  1048. smart_str_appendc(buf,(char)((val >> 16) & 0xff)); \
  1049. smart_str_appendc(buf,(char)((val >> 24) & 0xff));
  1050. #define WSDL_CACHE_PUT_1(val,buf) smart_str_appendc(buf,val);
  1051. #define WSDL_CACHE_PUT_N(val,n,buf) smart_str_appendl(buf,(char*)val,n);
  1052. #define WSDL_NO_STRING_MARKER 0x7fffffff
  1053. static char* sdl_deserialize_string(char **in)
  1054. {
  1055. char *s;
  1056. int len;
  1057. WSDL_CACHE_GET_INT(len, in);
  1058. if (len == WSDL_NO_STRING_MARKER) {
  1059. return NULL;
  1060. } else {
  1061. s = emalloc(len+1);
  1062. WSDL_CACHE_GET_N(s, len, in);
  1063. s[len] = '\0';
  1064. return s;
  1065. }
  1066. }
  1067. static void sdl_deserialize_key(HashTable* ht, void* data, char **in)
  1068. {
  1069. int len;
  1070. WSDL_CACHE_GET_INT(len, in);
  1071. if (len == WSDL_NO_STRING_MARKER) {
  1072. zend_hash_next_index_insert_ptr(ht, data);
  1073. } else {
  1074. zend_hash_str_add_ptr(ht, *in, len, data);
  1075. WSDL_CACHE_SKIP(len, in);
  1076. }
  1077. }
  1078. static void sdl_deserialize_attribute(sdlAttributePtr attr, encodePtr *encoders, char **in)
  1079. {
  1080. int i;
  1081. attr->name = sdl_deserialize_string(in);
  1082. attr->namens = sdl_deserialize_string(in);
  1083. attr->ref = sdl_deserialize_string(in);
  1084. attr->def = sdl_deserialize_string(in);
  1085. attr->fixed = sdl_deserialize_string(in);
  1086. WSDL_CACHE_GET_1(attr->form, sdlForm, in);
  1087. WSDL_CACHE_GET_1(attr->use, sdlUse, in);
  1088. WSDL_CACHE_GET_INT(i, in);
  1089. attr->encode = encoders[i];
  1090. WSDL_CACHE_GET_INT(i, in);
  1091. if (i > 0) {
  1092. attr->extraAttributes = emalloc(sizeof(HashTable));
  1093. zend_hash_init(attr->extraAttributes, i, NULL, delete_extra_attribute, 0);
  1094. while (i > 0) {
  1095. sdlExtraAttributePtr x = emalloc(sizeof(sdlExtraAttribute));
  1096. sdl_deserialize_key(attr->extraAttributes, x, in);
  1097. x->ns = sdl_deserialize_string(in);
  1098. x->val = sdl_deserialize_string(in);
  1099. --i;
  1100. }
  1101. }
  1102. }
  1103. static sdlRestrictionIntPtr sdl_deserialize_resriction_int(char **in)
  1104. {
  1105. if (**in == 1) {
  1106. sdlRestrictionIntPtr x = emalloc(sizeof(sdlRestrictionInt));
  1107. WSDL_CACHE_SKIP(1, in);
  1108. WSDL_CACHE_GET_INT(x->value, in);
  1109. WSDL_CACHE_GET_1(x->fixed, char, in);
  1110. return x;
  1111. } else {
  1112. WSDL_CACHE_SKIP(1, in);
  1113. return NULL;
  1114. }
  1115. }
  1116. static sdlRestrictionCharPtr sdl_deserialize_resriction_char(char **in)
  1117. {
  1118. if (**in == 1) {
  1119. sdlRestrictionCharPtr x = emalloc(sizeof(sdlRestrictionChar));
  1120. WSDL_CACHE_SKIP(1, in);
  1121. x->value = sdl_deserialize_string(in);
  1122. WSDL_CACHE_GET_1(x->fixed, char, in);
  1123. return x;
  1124. } else {
  1125. WSDL_CACHE_SKIP(1, in);
  1126. return NULL;
  1127. }
  1128. }
  1129. static sdlContentModelPtr sdl_deserialize_model(sdlTypePtr *types, sdlTypePtr *elements, char **in)
  1130. {
  1131. int i;
  1132. sdlContentModelPtr model = emalloc(sizeof(sdlContentModel));
  1133. WSDL_CACHE_GET_1(model->kind, sdlContentKind, in);
  1134. WSDL_CACHE_GET_INT(model->min_occurs, in);
  1135. WSDL_CACHE_GET_INT(model->max_occurs, in);
  1136. switch (model->kind) {
  1137. case XSD_CONTENT_ELEMENT:
  1138. WSDL_CACHE_GET_INT(i, in);
  1139. model->u.element = elements[i];
  1140. break;
  1141. case XSD_CONTENT_SEQUENCE:
  1142. case XSD_CONTENT_ALL:
  1143. case XSD_CONTENT_CHOICE:
  1144. WSDL_CACHE_GET_INT(i, in);
  1145. model->u.content = emalloc(sizeof(HashTable));
  1146. zend_hash_init(model->u.content, i, NULL, delete_model, 0);
  1147. while (i > 0) {
  1148. sdlContentModelPtr x = sdl_deserialize_model(types, elements, in);
  1149. zend_hash_next_index_insert_ptr(model->u.content, x);
  1150. i--;
  1151. }
  1152. break;
  1153. case XSD_CONTENT_GROUP_REF:
  1154. model->u.group_ref = sdl_deserialize_string(in);
  1155. break;
  1156. case XSD_CONTENT_GROUP:
  1157. WSDL_CACHE_GET_INT(i, in);
  1158. model->u.group = types[i];
  1159. break;
  1160. default:
  1161. break;
  1162. }
  1163. return model;
  1164. }
  1165. static void sdl_deserialize_type(sdlTypePtr type, sdlTypePtr *types, encodePtr *encoders, char **in)
  1166. {
  1167. int i;
  1168. sdlTypePtr *elements = NULL;
  1169. WSDL_CACHE_GET_1(type->kind, sdlTypeKind, in);
  1170. type->name = sdl_deserialize_string(in);
  1171. type->namens = sdl_deserialize_string(in);
  1172. type->def = sdl_deserialize_string(in);
  1173. type->fixed = sdl_deserialize_string(in);
  1174. type->ref = sdl_deserialize_string(in);
  1175. WSDL_CACHE_GET_1(type->nillable, char, in);
  1176. WSDL_CACHE_GET_1(type->form, sdlForm, in);
  1177. WSDL_CACHE_GET_INT(i, in);
  1178. type->encode = encoders[i];
  1179. if (**in == 1) {
  1180. WSDL_CACHE_SKIP(1, in);
  1181. type->restrictions = emalloc(sizeof(sdlRestrictions));
  1182. /*memset(type->restrictions, 0, sizeof(sdlRestrictions));*/
  1183. type->restrictions->minExclusive = sdl_deserialize_resriction_int(in);
  1184. type->restrictions->minInclusive = sdl_deserialize_resriction_int(in);
  1185. type->restrictions->maxExclusive = sdl_deserialize_resriction_int(in);
  1186. type->restrictions->maxInclusive = sdl_deserialize_resriction_int(in);
  1187. type->restrictions->totalDigits = sdl_deserialize_resriction_int(in);
  1188. type->restrictions->fractionDigits = sdl_deserialize_resriction_int(in);
  1189. type->restrictions->length = sdl_deserialize_resriction_int(in);
  1190. type->restrictions->minLength = sdl_deserialize_resriction_int(in);
  1191. type->restrictions->maxLength = sdl_deserialize_resriction_int(in);
  1192. type->restrictions->whiteSpace = sdl_deserialize_resriction_char(in);
  1193. type->restrictions->pattern = sdl_deserialize_resriction_char(in);
  1194. WSDL_CACHE_GET_INT(i, in);
  1195. if (i > 0) {
  1196. type->restrictions->enumeration = emalloc(sizeof(HashTable));
  1197. zend_hash_init(type->restrictions->enumeration, i, NULL, delete_restriction_var_char, 0);
  1198. while (i > 0) {
  1199. sdlRestrictionCharPtr x = sdl_deserialize_resriction_char(in);
  1200. sdl_deserialize_key(type->restrictions->enumeration, x, in);
  1201. --i;
  1202. }
  1203. } else {
  1204. type->restrictions->enumeration = NULL;
  1205. }
  1206. } else {
  1207. WSDL_CACHE_SKIP(1, in);
  1208. }
  1209. WSDL_CACHE_GET_INT(i, in);
  1210. if (i > 0) {
  1211. elements = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1212. elements[0] = NULL;
  1213. type->elements = emalloc(sizeof(HashTable));
  1214. zend_hash_init(type->elements, i, NULL, delete_type, 0);
  1215. while (i > 0) {
  1216. sdlTypePtr t = emalloc(sizeof(sdlType));
  1217. memset(t, 0, sizeof(sdlType));
  1218. sdl_deserialize_key(type->elements, t, in);
  1219. sdl_deserialize_type(t, types, encoders, in);
  1220. elements[i] = t;
  1221. --i;
  1222. }
  1223. }
  1224. WSDL_CACHE_GET_INT(i, in);
  1225. if (i > 0) {
  1226. type->attributes = emalloc(sizeof(HashTable));
  1227. zend_hash_init(type->attributes, i, NULL, delete_attribute, 0);
  1228. while (i > 0) {
  1229. sdlAttributePtr attr = emalloc(sizeof(sdlAttribute));
  1230. memset(attr, 0, sizeof(sdlAttribute));
  1231. sdl_deserialize_key(type->attributes, attr, in);
  1232. sdl_deserialize_attribute(attr, encoders, in);
  1233. --i;
  1234. }
  1235. }
  1236. if (**in != 0) {
  1237. WSDL_CACHE_SKIP(1, in);
  1238. type->model = sdl_deserialize_model(types, elements, in);
  1239. } else {
  1240. WSDL_CACHE_SKIP(1, in);
  1241. }
  1242. if (elements != NULL) {
  1243. efree(elements);
  1244. }
  1245. }
  1246. static void sdl_deserialize_encoder(encodePtr enc, sdlTypePtr *types, char **in)
  1247. {
  1248. int i;
  1249. WSDL_CACHE_GET_INT(enc->details.type, in);
  1250. enc->details.type_str = sdl_deserialize_string(in);
  1251. enc->details.ns = sdl_deserialize_string(in);
  1252. WSDL_CACHE_GET_INT(i, in);
  1253. enc->details.sdl_type = types[i];
  1254. enc->to_xml = sdl_guess_convert_xml;
  1255. enc->to_zval = sdl_guess_convert_zval;
  1256. if (enc->details.sdl_type == NULL) {
  1257. int ns_len = strlen(enc->details.ns);
  1258. int type_len = strlen(enc->details.type_str);
  1259. if (((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  1260. memcmp(enc->details.ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  1261. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  1262. memcmp(enc->details.ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  1263. char *enc_nscat;
  1264. int enc_ns_len;
  1265. int enc_len;
  1266. encodePtr real_enc;
  1267. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  1268. enc_len = enc_ns_len + type_len + 1;
  1269. enc_nscat = emalloc(enc_len + 1);
  1270. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  1271. enc_nscat[enc_ns_len] = ':';
  1272. memcpy(enc_nscat+enc_ns_len+1, enc->details.type_str, type_len);
  1273. enc_nscat[enc_len] = '\0';
  1274. real_enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  1275. efree(enc_nscat);
  1276. if (real_enc) {
  1277. enc->to_zval = real_enc->to_zval;
  1278. enc->to_xml = real_enc->to_xml;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. static void sdl_deserialize_soap_body(sdlSoapBindingFunctionBodyPtr body, encodePtr *encoders, sdlTypePtr *types, char **in)
  1284. {
  1285. int i, j, n;
  1286. WSDL_CACHE_GET_1(body->use, sdlEncodingUse, in);
  1287. if (body->use == SOAP_ENCODED) {
  1288. WSDL_CACHE_GET_1(body->encodingStyle, sdlRpcEncodingStyle, in);
  1289. } else {
  1290. body->encodingStyle = SOAP_ENCODING_DEFAULT;
  1291. }
  1292. body->ns = sdl_deserialize_string(in);
  1293. WSDL_CACHE_GET_INT(i, in);
  1294. if (i > 0) {
  1295. body->headers = emalloc(sizeof(HashTable));
  1296. zend_hash_init(body->headers, i, NULL, delete_header, 0);
  1297. while (i > 0) {
  1298. sdlSoapBindingFunctionHeaderPtr tmp = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1299. memset(tmp, 0, sizeof(sdlSoapBindingFunctionHeader));
  1300. sdl_deserialize_key(body->headers, tmp, in);
  1301. WSDL_CACHE_GET_1(tmp->use, sdlEncodingUse, in);
  1302. if (tmp->use == SOAP_ENCODED) {
  1303. WSDL_CACHE_GET_1(tmp->encodingStyle, sdlRpcEncodingStyle, in);
  1304. } else {
  1305. tmp->encodingStyle = SOAP_ENCODING_DEFAULT;
  1306. }
  1307. tmp->name = sdl_deserialize_string(in);
  1308. tmp->ns = sdl_deserialize_string(in);
  1309. WSDL_CACHE_GET_INT(n, in);
  1310. tmp->encode = encoders[n];
  1311. WSDL_CACHE_GET_INT(n, in);
  1312. tmp->element = types[n];
  1313. --i;
  1314. WSDL_CACHE_GET_INT(j, in);
  1315. if (j > 0) {
  1316. tmp->headerfaults = emalloc(sizeof(HashTable));
  1317. zend_hash_init(tmp->headerfaults, i, NULL, delete_header, 0);
  1318. while (j > 0) {
  1319. sdlSoapBindingFunctionHeaderPtr tmp2 = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1320. memset(tmp2, 0, sizeof(sdlSoapBindingFunctionHeader));
  1321. sdl_deserialize_key(tmp->headerfaults, tmp2, in);
  1322. WSDL_CACHE_GET_1(tmp2->use, sdlEncodingUse, in);
  1323. if (tmp2->use == SOAP_ENCODED) {
  1324. WSDL_CACHE_GET_1(tmp2->encodingStyle,

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