PageRenderTime 46ms CodeModel.GetById 17ms 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
  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, sdlRpcEncodingStyle, in);
  1325. } else {
  1326. tmp2->encodingStyle = SOAP_ENCODING_DEFAULT;
  1327. }
  1328. tmp2->name = sdl_deserialize_string(in);
  1329. tmp2->ns = sdl_deserialize_string(in);
  1330. WSDL_CACHE_GET_INT(n, in);
  1331. tmp2->encode = encoders[n];
  1332. WSDL_CACHE_GET_INT(n, in);
  1333. tmp2->element = types[n];
  1334. --j;
  1335. }
  1336. }
  1337. }
  1338. }
  1339. }
  1340. static HashTable* sdl_deserialize_parameters(encodePtr *encoders, sdlTypePtr *types, char **in)
  1341. {
  1342. int i, n;
  1343. HashTable *ht;
  1344. WSDL_CACHE_GET_INT(i, in);
  1345. if (i == 0) {return NULL;}
  1346. ht = emalloc(sizeof(HashTable));
  1347. zend_hash_init(ht, i, NULL, delete_parameter, 0);
  1348. while (i > 0) {
  1349. sdlParamPtr param = emalloc(sizeof(sdlParam));
  1350. sdl_deserialize_key(ht, param, in);
  1351. param->paramName = sdl_deserialize_string(in);
  1352. WSDL_CACHE_GET_INT(param->order, in);
  1353. WSDL_CACHE_GET_INT(n, in);
  1354. param->encode = encoders[n];
  1355. WSDL_CACHE_GET_INT(n, in);
  1356. param->element = types[n];
  1357. --i;
  1358. }
  1359. return ht;
  1360. }
  1361. static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, time_t t, time_t *cached)
  1362. {
  1363. sdlPtr sdl;
  1364. time_t old_t;
  1365. int i, num_groups, num_types, num_elements, num_encoders, num_bindings, num_func;
  1366. sdlFunctionPtr *functions = NULL;
  1367. sdlBindingPtr *bindings;
  1368. sdlTypePtr *types;
  1369. encodePtr *encoders;
  1370. const encode *enc;
  1371. int f;
  1372. struct stat st;
  1373. char *in, *buf;
  1374. f = open(fn, O_RDONLY|O_BINARY);
  1375. if (f < 0) {
  1376. return NULL;
  1377. }
  1378. if (fstat(f, &st) != 0) {
  1379. close(f);
  1380. return NULL;
  1381. }
  1382. buf = in = emalloc(st.st_size);
  1383. if (read(f, in, st.st_size) != st.st_size) {
  1384. close(f);
  1385. efree(in);
  1386. return NULL;
  1387. }
  1388. close(f);
  1389. if (strncmp(in,"wsdl",4) != 0 || in[4] != WSDL_CACHE_VERSION || in[5] != '\0') {
  1390. unlink(fn);
  1391. efree(buf);
  1392. return NULL;
  1393. }
  1394. in += 6;
  1395. WSDL_CACHE_GET(old_t, time_t, &in);
  1396. if (old_t < t) {
  1397. unlink(fn);
  1398. efree(buf);
  1399. return NULL;
  1400. }
  1401. *cached = old_t;
  1402. WSDL_CACHE_GET_INT(i, &in);
  1403. if (i == 0 && strncmp(in, uri, i) != 0) {
  1404. unlink(fn);
  1405. efree(buf);
  1406. return NULL;
  1407. }
  1408. WSDL_CACHE_SKIP(i, &in);
  1409. sdl = emalloc(sizeof(*sdl));
  1410. memset(sdl, 0, sizeof(*sdl));
  1411. sdl->source = sdl_deserialize_string(&in);
  1412. sdl->target_ns = sdl_deserialize_string(&in);
  1413. WSDL_CACHE_GET_INT(num_groups, &in);
  1414. WSDL_CACHE_GET_INT(num_types, &in);
  1415. WSDL_CACHE_GET_INT(num_elements, &in);
  1416. WSDL_CACHE_GET_INT(num_encoders, &in);
  1417. i = num_groups+num_types+num_elements;
  1418. types = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1419. types[0] = NULL;
  1420. while (i > 0) {
  1421. types[i] = emalloc(sizeof(sdlType));
  1422. memset(types[i], 0, sizeof(sdlType));
  1423. i--;
  1424. }
  1425. i = num_encoders;
  1426. enc = defaultEncoding;
  1427. while (enc->details.type != END_KNOWN_TYPES) {
  1428. i++; enc++;
  1429. }
  1430. encoders = safe_emalloc((i+1), sizeof(encodePtr), 0);
  1431. i = num_encoders;
  1432. encoders[0] = NULL;
  1433. while (i > 0) {
  1434. encoders[i] = emalloc(sizeof(encode));
  1435. memset(encoders[i], 0, sizeof(encode));
  1436. i--;
  1437. }
  1438. i = num_encoders;
  1439. enc = defaultEncoding;
  1440. while (enc->details.type != END_KNOWN_TYPES) {
  1441. encoders[++i] = (encodePtr)enc++;
  1442. }
  1443. i = 1;
  1444. if (num_groups > 0) {
  1445. sdl->groups = emalloc(sizeof(HashTable));
  1446. zend_hash_init(sdl->groups, num_groups, NULL, delete_type, 0);
  1447. while (i < num_groups+1) {
  1448. sdl_deserialize_key(sdl->groups, types[i], &in);
  1449. sdl_deserialize_type(types[i], types, encoders, &in);
  1450. i++;
  1451. }
  1452. }
  1453. if (num_types > 0) {
  1454. sdl->types = emalloc(sizeof(HashTable));
  1455. zend_hash_init(sdl->types, num_types, NULL, delete_type, 0);
  1456. while (i < num_groups+num_types+1) {
  1457. sdl_deserialize_key(sdl->types, types[i], &in);
  1458. sdl_deserialize_type(types[i], types, encoders, &in);
  1459. i++;
  1460. }
  1461. }
  1462. if (num_elements > 0) {
  1463. sdl->elements = emalloc(sizeof(HashTable));
  1464. zend_hash_init(sdl->elements, num_elements, NULL, delete_type, 0);
  1465. while (i < num_groups+num_types+num_elements+1) {
  1466. sdl_deserialize_key(sdl->elements, types[i], &in);
  1467. sdl_deserialize_type(types[i], types, encoders, &in);
  1468. i++;
  1469. }
  1470. }
  1471. i = 1;
  1472. if (num_encoders > 0) {
  1473. sdl->encoders = emalloc(sizeof(HashTable));
  1474. zend_hash_init(sdl->encoders, num_encoders, NULL, delete_encoder, 0);
  1475. while (i < num_encoders+1) {
  1476. sdl_deserialize_key(sdl->encoders, encoders[i], &in);
  1477. sdl_deserialize_encoder(encoders[i], types, &in);
  1478. i++;
  1479. }
  1480. }
  1481. /* deserialize bindings */
  1482. WSDL_CACHE_GET_INT(num_bindings, &in);
  1483. bindings = safe_emalloc(num_bindings, sizeof(sdlBindingPtr), 0);
  1484. if (num_bindings > 0) {
  1485. sdl->bindings = emalloc(sizeof(HashTable));
  1486. zend_hash_init(sdl->bindings, num_bindings, NULL, delete_binding, 0);
  1487. for (i = 0; i < num_bindings; i++) {
  1488. sdlBindingPtr binding = emalloc(sizeof(sdlBinding));
  1489. memset(binding, 0, sizeof(sdlBinding));
  1490. sdl_deserialize_key(sdl->bindings, binding, &in);
  1491. binding->name = sdl_deserialize_string(&in);
  1492. binding->location = sdl_deserialize_string(&in);
  1493. WSDL_CACHE_GET_1(binding->bindingType,sdlBindingType,&in);
  1494. if (binding->bindingType == BINDING_SOAP && *in != 0) {
  1495. sdlSoapBindingPtr soap_binding = binding->bindingAttributes = emalloc(sizeof(sdlSoapBinding));
  1496. WSDL_CACHE_GET_1(soap_binding->style,sdlEncodingStyle,&in);
  1497. WSDL_CACHE_GET_1(soap_binding->transport,sdlTransport,&in);
  1498. } else {
  1499. WSDL_CACHE_SKIP(1,&in);
  1500. }
  1501. bindings[i] = binding;
  1502. }
  1503. }
  1504. /* deserialize functions */
  1505. WSDL_CACHE_GET_INT(num_func, &in);
  1506. zend_hash_init(&sdl->functions, num_func, NULL, delete_function, 0);
  1507. if (num_func > 0) {
  1508. functions = safe_emalloc(num_func, sizeof(sdlFunctionPtr), 0);
  1509. for (i = 0; i < num_func; i++) {
  1510. int binding_num, num_faults;
  1511. sdlFunctionPtr func = emalloc(sizeof(sdlFunction));
  1512. sdl_deserialize_key(&sdl->functions, func, &in);
  1513. func->functionName = sdl_deserialize_string(&in);
  1514. func->requestName = sdl_deserialize_string(&in);
  1515. func->responseName = sdl_deserialize_string(&in);
  1516. WSDL_CACHE_GET_INT(binding_num, &in);
  1517. if (binding_num == 0) {
  1518. func->binding = NULL;
  1519. } else {
  1520. func->binding = bindings[binding_num-1];
  1521. }
  1522. if (func->binding && func->binding->bindingType == BINDING_SOAP && *in != 0) {
  1523. sdlSoapBindingFunctionPtr binding = func->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunction));
  1524. memset(binding, 0, sizeof(sdlSoapBindingFunction));
  1525. WSDL_CACHE_GET_1(binding->style,sdlEncodingStyle,&in);
  1526. binding->soapAction = sdl_deserialize_string(&in);
  1527. sdl_deserialize_soap_body(&binding->input, encoders, types, &in);
  1528. sdl_deserialize_soap_body(&binding->output, encoders, types, &in);
  1529. } else {
  1530. WSDL_CACHE_SKIP(1, &in);
  1531. func->bindingAttributes = NULL;
  1532. }
  1533. func->requestParameters = sdl_deserialize_parameters(encoders, types, &in);
  1534. func->responseParameters = sdl_deserialize_parameters(encoders, types, &in);
  1535. WSDL_CACHE_GET_INT(num_faults, &in);
  1536. if (num_faults > 0) {
  1537. int j;
  1538. func->faults = emalloc(sizeof(HashTable));
  1539. zend_hash_init(func->faults, num_faults, NULL, delete_fault, 0);
  1540. for (j = 0; j < num_faults; j++) {
  1541. sdlFaultPtr fault = emalloc(sizeof(sdlFault));
  1542. sdl_deserialize_key(func->faults, fault, &in);
  1543. fault->name =sdl_deserialize_string(&in);
  1544. fault->details =sdl_deserialize_parameters(encoders, types, &in);
  1545. if (*in != 0) {
  1546. sdlSoapBindingFunctionFaultPtr binding = fault->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  1547. memset(binding, 0, sizeof(sdlSoapBindingFunctionFault));
  1548. WSDL_CACHE_GET_1(binding->use,sdlEncodingUse,&in);
  1549. if (binding->use == SOAP_ENCODED) {
  1550. WSDL_CACHE_GET_1(binding->encodingStyle, sdlRpcEncodingStyle, &in);
  1551. } else {
  1552. binding->encodingStyle = SOAP_ENCODING_DEFAULT;
  1553. }
  1554. binding->ns = sdl_deserialize_string(&in);
  1555. } else {
  1556. WSDL_CACHE_SKIP(1, &in);
  1557. fault->bindingAttributes = NULL;
  1558. }
  1559. }
  1560. } else {
  1561. func->faults = NULL;
  1562. }
  1563. functions[i] = func;
  1564. }
  1565. }
  1566. /* deserialize requests */
  1567. WSDL_CACHE_GET_INT(i, &in);
  1568. if (i > 0) {
  1569. sdl->requests = emalloc(sizeof(HashTable));
  1570. zend_hash_init(sdl->requests, i, NULL, NULL, 0);
  1571. while (i > 0) {
  1572. int function_num;
  1573. WSDL_CACHE_GET_INT(function_num, &in);
  1574. sdl_deserialize_key(sdl->requests, functions[function_num-1], &in);
  1575. i--;
  1576. }
  1577. }
  1578. if (functions) {
  1579. efree(functions);
  1580. }
  1581. efree(bindings);
  1582. efree(encoders);
  1583. efree(types);
  1584. efree(buf);
  1585. return sdl;
  1586. }
  1587. static void sdl_serialize_string(const char *str, smart_str *out)
  1588. {
  1589. if (str) {
  1590. int i = strlen(str);
  1591. WSDL_CACHE_PUT_INT(i, out);
  1592. if (i > 0) {
  1593. WSDL_CACHE_PUT_N(str, i, out);
  1594. }
  1595. } else {
  1596. WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
  1597. }
  1598. }
  1599. // TODO: refactor it
  1600. static void sdl_serialize_key(zend_string *key, smart_str *out)
  1601. {
  1602. if (key) {
  1603. WSDL_CACHE_PUT_INT(ZSTR_LEN(key), out);
  1604. WSDL_CACHE_PUT_N(ZSTR_VAL(key), ZSTR_LEN(key), out);
  1605. } else {
  1606. WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
  1607. }
  1608. }
  1609. static void sdl_serialize_encoder_ref(encodePtr enc, HashTable *tmp_encoders, smart_str *out) {
  1610. if (enc) {
  1611. zval *encoder_num;
  1612. if ((encoder_num = zend_hash_str_find(tmp_encoders, (char*)&enc, sizeof(enc))) != 0) {
  1613. WSDL_CACHE_PUT_INT(Z_LVAL_P(encoder_num), out);
  1614. } else {
  1615. WSDL_CACHE_PUT_INT(0, out);
  1616. }
  1617. } else {
  1618. WSDL_CACHE_PUT_INT(0, out);
  1619. }
  1620. }
  1621. static void sdl_serialize_type_ref(sdlTypePtr type, HashTable *tmp_types, smart_str *out) {
  1622. if (type) {
  1623. zval *type_num;
  1624. if ((type_num = zend_hash_str_find(tmp_types, (char*)&type, sizeof(type))) != NULL) {
  1625. WSDL_CACHE_PUT_INT(Z_LVAL_P(type_num), out);
  1626. } else {
  1627. WSDL_CACHE_PUT_INT(0, out);
  1628. }
  1629. } else {
  1630. WSDL_CACHE_PUT_INT(0,out);
  1631. }
  1632. }
  1633. static void sdl_serialize_attribute(sdlAttributePtr attr, HashTable *tmp_encoders, smart_str *out)
  1634. {
  1635. int i;
  1636. sdl_serialize_string(attr->name, out);
  1637. sdl_serialize_string(attr->namens, out);
  1638. sdl_serialize_string(attr->ref, out);
  1639. sdl_serialize_string(attr->def, out);
  1640. sdl_serialize_string(attr->fixed, out);
  1641. WSDL_CACHE_PUT_1(attr->form, out);
  1642. WSDL_CACHE_PUT_1(attr->use, out);
  1643. sdl_serialize_encoder_ref(attr->encode, tmp_encoders, out);
  1644. if (attr->extraAttributes) {
  1645. i = zend_hash_num_elements(attr->extraAttributes);
  1646. } else {
  1647. i = 0;
  1648. }
  1649. WSDL_CACHE_PUT_INT(i, out);
  1650. if (i > 0) {
  1651. sdlExtraAttributePtr tmp;
  1652. zend_string *key;
  1653. ZEND_HASH_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
  1654. sdl_serialize_key(key, out);
  1655. sdl_serialize_string(tmp->ns, out);
  1656. sdl_serialize_string(tmp->val, out);
  1657. } ZEND_HASH_FOREACH_END();
  1658. }
  1659. }
  1660. static void sdl_serialize_model(sdlContentModelPtr model, HashTable *tmp_types, HashTable *tmp_elements, smart_str *out)
  1661. {
  1662. WSDL_CACHE_PUT_1(model->kind, out);
  1663. WSDL_CACHE_PUT_INT(model->min_occurs, out);
  1664. WSDL_CACHE_PUT_INT(model->max_occurs, out);
  1665. switch (model->kind) {
  1666. case XSD_CONTENT_ELEMENT:
  1667. sdl_serialize_type_ref(model->u.element, tmp_elements, out);
  1668. break;
  1669. case XSD_CONTENT_SEQUENCE:
  1670. case XSD_CONTENT_ALL:
  1671. case XSD_CONTENT_CHOICE: {
  1672. sdlContentModelPtr tmp;
  1673. int i = zend_hash_num_elements(model->u.content);
  1674. WSDL_CACHE_PUT_INT(i, out);
  1675. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  1676. sdl_serialize_model(tmp, tmp_types, tmp_elements, out);
  1677. } ZEND_HASH_FOREACH_END();
  1678. }
  1679. break;
  1680. case XSD_CONTENT_GROUP_REF:
  1681. sdl_serialize_string(model->u.group_ref,out);
  1682. break;
  1683. case XSD_CONTENT_GROUP:
  1684. sdl_serialize_type_ref(model->u.group, tmp_types, out);
  1685. break;
  1686. default:
  1687. break;
  1688. }
  1689. }
  1690. static void sdl_serialize_resriction_int(sdlRestrictionIntPtr x, smart_str *out)
  1691. {
  1692. if (x) {
  1693. WSDL_CACHE_PUT_1(1, out);
  1694. WSDL_CACHE_PUT_INT(x->value, out);
  1695. WSDL_CACHE_PUT_1(x->fixed, out);
  1696. } else {
  1697. WSDL_CACHE_PUT_1(0, out);
  1698. }
  1699. }
  1700. static void sdl_serialize_resriction_char(sdlRestrictionCharPtr x, smart_str *out)
  1701. {
  1702. if (x) {
  1703. WSDL_CACHE_PUT_1(1, out);
  1704. sdl_serialize_string(x->value, out);
  1705. WSDL_CACHE_PUT_1(x->fixed, out);
  1706. } else {
  1707. WSDL_CACHE_PUT_1(0, out);
  1708. }
  1709. }
  1710. static void sdl_serialize_type(sdlTypePtr type, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1711. {
  1712. int i;
  1713. HashTable *tmp_elements = NULL;
  1714. WSDL_CACHE_PUT_1(type->kind, out);
  1715. sdl_serialize_string(type->name, out);
  1716. sdl_serialize_string(type->namens, out);
  1717. sdl_serialize_string(type->def, out);
  1718. sdl_serialize_string(type->fixed, out);
  1719. sdl_serialize_string(type->ref, out);
  1720. WSDL_CACHE_PUT_1(type->nillable, out);
  1721. WSDL_CACHE_PUT_1(type->form, out);
  1722. sdl_serialize_encoder_ref(type->encode, tmp_encoders, out);
  1723. if (type->restrictions) {
  1724. WSDL_CACHE_PUT_1(1, out);
  1725. sdl_serialize_resriction_int(type->restrictions->minExclusive,out);
  1726. sdl_serialize_resriction_int(type->restrictions->minInclusive,out);
  1727. sdl_serialize_resriction_int(type->restrictions->maxExclusive,out);
  1728. sdl_serialize_resriction_int(type->restrictions->maxInclusive,out);
  1729. sdl_serialize_resriction_int(type->restrictions->totalDigits,out);
  1730. sdl_serialize_resriction_int(type->restrictions->fractionDigits,out);
  1731. sdl_serialize_resriction_int(type->restrictions->length,out);
  1732. sdl_serialize_resriction_int(type->restrictions->minLength,out);
  1733. sdl_serialize_resriction_int(type->restrictions->maxLength,out);
  1734. sdl_serialize_resriction_char(type->restrictions->whiteSpace,out);
  1735. sdl_serialize_resriction_char(type->restrictions->pattern,out);
  1736. if (type->restrictions->enumeration) {
  1737. i = zend_hash_num_elements(type->restrictions->enumeration);
  1738. } else {
  1739. i = 0;
  1740. }
  1741. WSDL_CACHE_PUT_INT(i, out);
  1742. if (i > 0) {
  1743. sdlRestrictionCharPtr tmp;
  1744. zend_string *key;
  1745. ZEND_HASH_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
  1746. sdl_serialize_resriction_char(tmp, out);
  1747. sdl_serialize_key(key, out);
  1748. } ZEND_HASH_FOREACH_END();
  1749. }
  1750. } else {
  1751. WSDL_CACHE_PUT_1(0, out);
  1752. }
  1753. if (type->elements) {
  1754. i = zend_hash_num_elements(type->elements);
  1755. } else {
  1756. i = 0;
  1757. }
  1758. WSDL_CACHE_PUT_INT(i, out);
  1759. if (i > 0) {
  1760. sdlTypePtr tmp;
  1761. zend_string *key;
  1762. zval zv;
  1763. tmp_elements = emalloc(sizeof(HashTable));
  1764. zend_hash_init(tmp_elements, i, NULL, NULL, 0);
  1765. ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
  1766. sdl_serialize_key(key, out);
  1767. sdl_serialize_type(tmp, tmp_encoders, tmp_types, out);
  1768. ZVAL_LONG(&zv, i);
  1769. zend_hash_str_add(tmp_elements, (char*)&tmp, sizeof(tmp), &zv);
  1770. i--;
  1771. } ZEND_HASH_FOREACH_END();
  1772. }
  1773. if (type->attributes) {
  1774. i = zend_hash_num_elements(type->attributes);
  1775. } else {
  1776. i = 0;
  1777. }
  1778. WSDL_CACHE_PUT_INT(i, out);
  1779. if (i > 0) {
  1780. sdlAttributePtr tmp;
  1781. zend_string *key;
  1782. ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
  1783. sdl_serialize_key(key, out);
  1784. sdl_serialize_attribute(tmp, tmp_encoders, out);
  1785. } ZEND_HASH_FOREACH_END();
  1786. }
  1787. if (type->model) {
  1788. WSDL_CACHE_PUT_1(1, out);
  1789. sdl_serialize_model(type->model, tmp_types, tmp_elements, out);
  1790. } else {
  1791. WSDL_CACHE_PUT_1(0, out);
  1792. }
  1793. if (tmp_elements != NULL) {
  1794. zend_hash_destroy(tmp_elements);
  1795. efree(tmp_elements);
  1796. }
  1797. }
  1798. static void sdl_serialize_encoder(encodePtr enc, HashTable *tmp_types, smart_str *out)
  1799. {
  1800. WSDL_CACHE_PUT_INT(enc->details.type, out);
  1801. sdl_serialize_string(enc->details.type_str, out);
  1802. sdl_serialize_string(enc->details.ns, out);
  1803. sdl_serialize_type_ref(enc->details.sdl_type, tmp_types, out);
  1804. }
  1805. static void sdl_serialize_parameters(HashTable *ht, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1806. {
  1807. int i;
  1808. if (ht) {
  1809. i = zend_hash_num_elements(ht);
  1810. } else {
  1811. i = 0;
  1812. }
  1813. WSDL_CACHE_PUT_INT(i, out);
  1814. if (i > 0) {
  1815. sdlParamPtr tmp;
  1816. zend_string *key;
  1817. ZEND_HASH_FOREACH_STR_KEY_PTR(ht, key, tmp) {
  1818. sdl_serialize_key(key, out);
  1819. sdl_serialize_string(tmp->paramName, out);
  1820. WSDL_CACHE_PUT_INT(tmp->order, out);
  1821. sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
  1822. sdl_serialize_type_ref(tmp->element, tmp_types, out);
  1823. } ZEND_HASH_FOREACH_END();
  1824. }
  1825. }
  1826. static void sdl_serialize_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1827. {
  1828. int i, j;
  1829. WSDL_CACHE_PUT_1(body->use, out);
  1830. if (body->use == SOAP_ENCODED) {
  1831. WSDL_CACHE_PUT_1(body->encodingStyle, out);
  1832. }
  1833. sdl_serialize_string(body->ns, out);
  1834. if (body->headers) {
  1835. i = zend_hash_num_elements(body->headers);
  1836. } else {
  1837. i = 0;
  1838. }
  1839. WSDL_CACHE_PUT_INT(i, out);
  1840. if (i > 0) {
  1841. sdlSoapBindingFunctionHeaderPtr tmp;
  1842. zend_string *key;
  1843. ZEND_HASH_FOREACH_STR_KEY_PTR(body->headers, key, tmp) {
  1844. sdl_serialize_key(key, out);
  1845. WSDL_CACHE_PUT_1(tmp->use, out);
  1846. if (tmp->use == SOAP_ENCODED) {
  1847. WSDL_CACHE_PUT_1(tmp->encodingStyle, out);
  1848. }
  1849. sdl_serialize_string(tmp->name, out);
  1850. sdl_serialize_string(tmp->ns, out);
  1851. sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
  1852. sdl_serialize_type_ref(tmp->element, tmp_types, out);
  1853. if (tmp->headerfaults) {
  1854. j = zend_hash_num_elements(tmp->headerfaults);
  1855. } else {
  1856. j = 0;
  1857. }
  1858. WSDL_CACHE_PUT_INT(j, out);
  1859. if (j > 0) {
  1860. sdlSoapBindingFunctionHeaderPtr tmp2;
  1861. zend_string *key;
  1862. ZEND_HASH_FOREACH_STR_KEY_PTR(body->headers, key, tmp2) {
  1863. sdl_serialize_key(key, out);
  1864. WSDL_CACHE_PUT_1(tmp2->use, out);
  1865. if (tmp2->use == SOAP_ENCODED) {
  1866. WSDL_CACHE_PUT_1(tmp2->encodingStyle, out);
  1867. }
  1868. sdl_serialize_string(tmp2->name, out);
  1869. sdl_serialize_string(tmp2->ns, out);
  1870. sdl_serialize_encoder_ref(tmp2->encode, tmp_encoders, out);
  1871. sdl_serialize_type_ref(tmp2->element, tmp_types, out);
  1872. } ZEND_HASH_FOREACH_END();
  1873. }
  1874. } ZEND_HASH_FOREACH_END();
  1875. }
  1876. }
  1877. static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr sdl)
  1878. {
  1879. smart_str buf = {0};
  1880. smart_str *out = &buf;
  1881. int i;
  1882. int type_num = 1;
  1883. int encoder_num = 1;
  1884. int f;
  1885. const encode *enc;
  1886. HashTable tmp_types;
  1887. HashTable tmp_encoders;
  1888. HashTable tmp_bindings;
  1889. HashTable tmp_functions;
  1890. #ifdef ZEND_WIN32
  1891. f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  1892. #else
  1893. f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  1894. #endif
  1895. if (f < 0) {return;}
  1896. zend_hash_init(&tmp_types, 0, NULL, NULL, 0);
  1897. zend_hash_init(&tmp_encoders, 0, NULL, NULL, 0);
  1898. zend_hash_init(&tmp_bindings, 0, NULL, NULL, 0);
  1899. zend_hash_init(&tmp_functions, 0, NULL, NULL, 0);
  1900. WSDL_CACHE_PUT_N("wsdl", 4, out);
  1901. WSDL_CACHE_PUT_1(WSDL_CACHE_VERSION,out);
  1902. WSDL_CACHE_PUT_1(0,out);
  1903. WSDL_CACHE_PUT_N(&t, sizeof(t), out);
  1904. sdl_serialize_string(uri, out);
  1905. sdl_serialize_string(sdl->source, out);
  1906. sdl_serialize_string(sdl->target_ns, out);
  1907. if (sdl->groups) {
  1908. i = zend_hash_num_elements(sdl->groups);
  1909. } else {
  1910. i = 0;
  1911. }
  1912. WSDL_CACHE_PUT_INT(i, out);
  1913. if (i > 0) {
  1914. sdlTypePtr tmp;
  1915. zval zv;
  1916. ZEND_HASH_FOREACH_PTR(sdl->groups, tmp) {
  1917. ZVAL_LONG(&zv, type_num);
  1918. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1919. ++type_num;
  1920. } ZEND_HASH_FOREACH_END();
  1921. }
  1922. if (sdl->types) {
  1923. i = zend_hash_num_elements(sdl->types);
  1924. } else {
  1925. i = 0;
  1926. }
  1927. WSDL_CACHE_PUT_INT(i, out);
  1928. if (i > 0) {
  1929. sdlTypePtr tmp;
  1930. zval zv;
  1931. ZEND_HASH_FOREACH_PTR(sdl->types, tmp) {
  1932. ZVAL_LONG(&zv, type_num);
  1933. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1934. ++type_num;
  1935. } ZEND_HASH_FOREACH_END();
  1936. }
  1937. if (sdl->elements) {
  1938. i = zend_hash_num_elements(sdl->elements);
  1939. } else {
  1940. i = 0;
  1941. }
  1942. WSDL_CACHE_PUT_INT(i, out);
  1943. if (i > 0) {
  1944. sdlTypePtr tmp;
  1945. zval zv;
  1946. ZEND_HASH_FOREACH_PTR(sdl->elements, tmp) {
  1947. ZVAL_LONG(&zv, type_num);
  1948. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1949. ++type_num;
  1950. } ZEND_HASH_FOREACH_END();
  1951. }
  1952. if (sdl->encoders) {
  1953. i = zend_hash_num_elements(sdl->encoders);
  1954. } else {
  1955. i = 0;
  1956. }
  1957. WSDL_CACHE_PUT_INT(i, out);
  1958. if (i > 0) {
  1959. encodePtr tmp;
  1960. zval zv;
  1961. ZEND_HASH_FOREACH_PTR(sdl->encoders, tmp) {
  1962. ZVAL_LONG(&zv, encoder_num);
  1963. zend_hash_str_add(&tmp_encoders, (char*)&tmp, sizeof(tmp), &zv);
  1964. ++encoder_num;
  1965. } ZEND_HASH_FOREACH_END();
  1966. }
  1967. enc = defaultEncoding;
  1968. while (enc->details.type != END_KNOWN_TYPES) {
  1969. zval zv;
  1970. ZVAL_LONG(&zv, encoder_num);
  1971. zend_hash_str_add(&tmp_encoders, (char*)&enc, sizeof(encodePtr), &zv);
  1972. enc++;
  1973. ++encoder_num;
  1974. }
  1975. if (sdl->groups) {
  1976. sdlTypePtr tmp;
  1977. zend_string *key;
  1978. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
  1979. sdl_serialize_key(key, out);
  1980. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1981. } ZEND_HASH_FOREACH_END();
  1982. }
  1983. if (sdl->types) {
  1984. sdlTypePtr tmp;
  1985. zend_string *key;
  1986. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
  1987. sdl_serialize_key(key, out);
  1988. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1989. } ZEND_HASH_FOREACH_END();
  1990. }
  1991. if (sdl->elements) {
  1992. sdlTypePtr tmp;
  1993. zend_string *key;
  1994. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
  1995. sdl_serialize_key(key, out);
  1996. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1997. } ZEND_HASH_FOREACH_END();
  1998. }
  1999. if (sdl->encoders) {
  2000. encodePtr tmp;
  2001. zend_string *key;
  2002. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
  2003. sdl_serialize_key(key, out);
  2004. sdl_serialize_encoder(tmp, &tmp_types, out);
  2005. } ZEND_HASH_FOREACH_END();
  2006. }
  2007. /* serialize bindings */
  2008. if (sdl->bindings) {
  2009. i = zend_hash_num_elements(sdl->bindings);
  2010. } else {
  2011. i = 0;
  2012. }
  2013. WSDL_CACHE_PUT_INT(i, out);
  2014. if (i > 0) {
  2015. sdlBindingPtr tmp;
  2016. int binding_num = 1;
  2017. zval zv;
  2018. zend_string *key;
  2019. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
  2020. sdl_serialize_key(key, out);
  2021. sdl_serialize_string(tmp->name, out);
  2022. sdl_serialize_string(tmp->location, out);
  2023. WSDL_CACHE_PUT_1(tmp->bindingType,out);
  2024. if (tmp->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
  2025. sdlSoapBindingPtr binding = (sdlSoapBindingPtr)tmp->bindingAttributes;
  2026. WSDL_CACHE_PUT_1(binding->style, out);
  2027. WSDL_CACHE_PUT_1(binding->transport, out);
  2028. } else {
  2029. WSDL_CACHE_PUT_1(0,out);
  2030. }
  2031. ZVAL_LONG(&zv, binding_num);
  2032. zend_hash_str_add(&tmp_bindings, (char*)&tmp, sizeof(tmp), &zv);
  2033. binding_num++;
  2034. } ZEND_HASH_FOREACH_END();
  2035. }
  2036. /* serialize functions */
  2037. i = zend_hash_num_elements(&sdl->functions);
  2038. WSDL_CACHE_PUT_INT(i, out);
  2039. if (i > 0) {
  2040. sdlFunctionPtr tmp;
  2041. zval *binding_num, zv;
  2042. int function_num = 1;
  2043. zend_string *key;
  2044. ZEND_HASH_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
  2045. sdl_serialize_key(key, out);
  2046. sdl_serialize_string(tmp->functionName, out);
  2047. sdl_serialize_string(tmp->requestName, out);
  2048. sdl_serialize_string(tmp->responseName, out);
  2049. if (tmp->binding) {
  2050. binding_num = zend_hash_str_find(&tmp_bindings,(char*)&tmp->binding, sizeof(tmp->binding));
  2051. if (binding_num) {
  2052. WSDL_CACHE_PUT_INT(Z_LVAL_P(binding_num), out);
  2053. if (Z_LVAL_P(binding_num) >= 0) {
  2054. if (tmp->binding->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
  2055. sdlSoapBindingFunctionPtr binding = (sdlSoapBindingFunctionPtr)tmp->bindingAttributes;
  2056. WSDL_CACHE_PUT_1(binding->style, out);
  2057. sdl_serialize_string(binding->soapAction, out);
  2058. sdl_serialize_soap_body(&binding->input, &tmp_encoders, &tmp_types, out);
  2059. sdl_serialize_soap_body(&binding->output, &tmp_encoders, &tmp_types, out);
  2060. } else {
  2061. WSDL_CACHE_PUT_1(0,out);
  2062. }
  2063. }
  2064. }
  2065. }
  2066. sdl_serialize_parameters(tmp->requestParameters, &tmp_encoders, &tmp_types, out);
  2067. sdl_serialize_parameters(tmp->responseParameters, &tmp_encoders, &tmp_types, out);
  2068. if (tmp->faults) {
  2069. sdlFaultPtr fault;
  2070. zend_string *key;
  2071. WSDL_CACHE_PUT_INT(zend_hash_num_elements(tmp->faults), out);
  2072. ZEND_HASH_FOREACH_STR_KEY_PTR(tmp->faults, key, fault) {
  2073. sdl_serialize_key(key, out);
  2074. sdl_serialize_string(fault->name, out);
  2075. sdl_serialize_parameters(fault->details, &tmp_encoders, &tmp_types, out);
  2076. if (tmp->binding->bindingType == BINDING_SOAP && fault->bindingAttributes != NULL) {
  2077. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  2078. WSDL_CACHE_PUT_1(binding->use, out);
  2079. if (binding->use == SOAP_ENCODED) {
  2080. WSDL_CACHE_PUT_1(binding->encodingStyle, out);
  2081. }
  2082. sdl_serialize_string(binding->ns, out);
  2083. } else {
  2084. WSDL_CACHE_PUT_1(0, out);
  2085. }
  2086. } ZEND_HASH_FOREACH_END();
  2087. } else {
  2088. WSDL_CACHE_PUT_INT(0, out);
  2089. }
  2090. ZVAL_LONG(&zv, function_num);
  2091. zend_hash_str_add(&tmp_functions, (char*)&tmp, sizeof(tmp), &zv);
  2092. function_num++;
  2093. } ZEND_HASH_FOREACH_END();
  2094. }
  2095. /* serialize requests */
  2096. if (sdl->requests) {
  2097. i = zend_hash_num_elements(sdl->requests);
  2098. } else {
  2099. i = 0;
  2100. }
  2101. WSDL_CACHE_PUT_INT(i, out);
  2102. if (i > 0) {
  2103. sdlFunctionPtr tmp;
  2104. zval *function_num;
  2105. zend_string *key;
  2106. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->requests, key, tmp) {
  2107. function_num = zend_hash_str_find(&tmp_functions, (char*)&tmp, sizeof(tmp));
  2108. WSDL_CACHE_PUT_INT(Z_LVAL_P(function_num), out);
  2109. sdl_serialize_key(key, out);
  2110. } ZEND_HASH_FOREACH_END();
  2111. }
  2112. php_ignore_value(write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)));
  2113. close(f);
  2114. smart_str_free(&buf);
  2115. zend_hash_destroy(&tmp_functions);
  2116. zend_hash_destroy(&tmp_bindings);
  2117. zend_hash_destroy(&tmp_encoders);
  2118. zend_hash_destroy(&tmp_types);
  2119. }
  2120. static void make_persistent_restriction_int(void *data)
  2121. {
  2122. sdlRestrictionIntPtr *rest = (sdlRestrictionIntPtr *)data;
  2123. sdlRestrictionIntPtr prest = NULL;
  2124. prest = malloc(sizeof(sdlRestrictionInt));
  2125. *prest = **rest;
  2126. *rest = prest;
  2127. }
  2128. static void make_persistent_restriction_char_int(sdlRestrictionCharPtr *rest)
  2129. {
  2130. sdlRestrictionCharPtr prest = NULL;
  2131. prest = malloc(sizeof(sdlRestrictionChar));
  2132. memset(prest, 0, sizeof(sdlRestrictionChar));
  2133. prest->value = strdup((*rest)->value);
  2134. prest->fixed = (*rest)->fixed;
  2135. *rest = prest;
  2136. }
  2137. static void make_persistent_sdl_type_ref(sdlTypePtr *type, HashTable *ptr_map, HashTable *bp_types)
  2138. {
  2139. sdlTypePtr tmp;
  2140. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)type, sizeof(sdlTypePtr))) != NULL) {
  2141. *type = tmp;
  2142. } else {
  2143. zend_hash_next_index_insert_ptr(bp_types, *type);
  2144. }
  2145. }
  2146. static void make_persistent_sdl_encoder_ref(encodePtr *enc, HashTable *ptr_map, HashTable *bp_encoders)
  2147. {
  2148. encodePtr tmp;
  2149. /* do not process defaultEncoding's here */
  2150. if ((*enc) >= defaultEncoding && (*enc) < defaultEncoding + numDefaultEncodings) {
  2151. return;
  2152. }
  2153. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)enc, sizeof(encodePtr))) != NULL) {
  2154. *enc = tmp;
  2155. } else {
  2156. zend_hash_next_index_insert_ptr(bp_encoders, enc);
  2157. }
  2158. }
  2159. static HashTable* make_persistent_sdl_function_headers(HashTable *headers, HashTable *ptr_map)
  2160. {
  2161. HashTable *pheaders;
  2162. sdlSoapBindingFunctionHeaderPtr tmp, pheader;
  2163. encodePtr penc;
  2164. sdlTypePtr ptype;
  2165. zend_string *key;
  2166. pheaders = malloc(sizeof(HashTable));
  2167. zend_hash_init(pheaders, zend_hash_num_elements(headers), NULL, delete_header_persistent, 1);
  2168. ZEND_HASH_FOREACH_STR_KEY_PTR(headers, key, tmp) {
  2169. pheader = malloc(sizeof(sdlSoapBindingFunctionHeader));
  2170. memset(pheader, 0, sizeof(sdlSoapBindingFunctionHeader));
  2171. *pheader = *tmp;
  2172. if (pheader->name) {
  2173. pheader->name = strdup(pheader->name);
  2174. }
  2175. if (pheader->ns) {
  2176. pheader->ns = strdup(pheader->ns);
  2177. }
  2178. if (pheader->encode && pheader->encode->details.sdl_type) {
  2179. if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->encode, sizeof(encodePtr))) == NULL) {
  2180. assert(0);
  2181. }
  2182. pheader->encode = penc;
  2183. }
  2184. if (pheader->element) {
  2185. if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->element, sizeof(sdlTypePtr))) == NULL) {
  2186. assert(0);
  2187. }
  2188. pheader->element = ptype;
  2189. }
  2190. if (pheader->headerfaults) {
  2191. pheader->headerfaults = make_persistent_sdl_function_headers(pheader->headerfaults, ptr_map);
  2192. }
  2193. if (key) {
  2194. /* We have to duplicate key emalloc->malloc */
  2195. zend_hash_str_add_ptr(pheaders, ZSTR_VAL(key), ZSTR_LEN(key), pheader);
  2196. } else {
  2197. zend_hash_next_index_insert_ptr(pheaders, pheader);
  2198. }
  2199. } ZEND_HASH_FOREACH_END();
  2200. return pheaders;
  2201. }
  2202. static void make_persistent_sdl_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *ptr_map)
  2203. {
  2204. if (body->ns) {
  2205. body->ns = strdup(body->ns);
  2206. }
  2207. if (body->headers) {
  2208. body->headers = make_persistent_sdl_function_headers(body->headers, ptr_map);
  2209. }
  2210. }
  2211. static HashTable* make_persistent_sdl_parameters(HashTable *params, HashTable *ptr_map)
  2212. {
  2213. HashTable *pparams;
  2214. sdlParamPtr tmp, pparam;
  2215. sdlTypePtr ptype;
  2216. encodePtr penc;
  2217. zend_string *key;
  2218. pparams = malloc(sizeof(HashTable));
  2219. zend_hash_init(pparams, zend_hash_num_elements(params), NULL, delete_parameter_persistent, 1);
  2220. ZEND_HASH_FOREACH_STR_KEY_PTR(params, key, tmp) {
  2221. pparam = malloc(sizeof(sdlParam));
  2222. memset(pparam, 0, sizeof(sdlParam));
  2223. *pparam = *tmp;
  2224. if (pparam->paramName) {
  2225. pparam->paramName = strdup(pparam->paramName);
  2226. }
  2227. if (pparam->encode && pparam->encode->details.sdl_type) {
  2228. if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->encode, sizeof(encodePtr))) == NULL) {
  2229. assert(0);
  2230. }
  2231. pparam->encode = penc;
  2232. }
  2233. if (pparam->element) {
  2234. if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->element, sizeof(sdlTypePtr))) == NULL) {
  2235. assert(0);
  2236. }
  2237. pparam->element = ptype;
  2238. }
  2239. if (key) {
  2240. /* We have to duplicate key emalloc->malloc */
  2241. zend_hash_str_add_ptr(pparams, ZSTR_VAL(key), ZSTR_LEN(key), pparam);
  2242. } else {
  2243. zend_hash_next_index_insert_ptr(pparams, pparam);
  2244. }
  2245. } ZEND_HASH_FOREACH_END();
  2246. return pparams;
  2247. }
  2248. static HashTable* make_persistent_sdl_function_faults(sdlFunctionPtr func, HashTable *faults, HashTable *ptr_map)
  2249. {
  2250. HashTable *pfaults;
  2251. sdlFaultPtr tmp, pfault;
  2252. zend_string *key;
  2253. pfaults = malloc(sizeof(HashTable));
  2254. zend_hash_init(pfaults, zend_hash_num_elements(faults), NULL, delete_fault_persistent, 1);
  2255. ZEND_HASH_FOREACH_STR_KEY_PTR(faults, key, tmp) {
  2256. pfault = malloc(sizeof(sdlFault));
  2257. memset(pfault, 0, sizeof(sdlFault));
  2258. *pfault = *tmp;
  2259. if (pfault->name) {
  2260. pfault->name = strdup(pfault->name);
  2261. }
  2262. if (pfault->details) {
  2263. pfault->details = make_persistent_sdl_parameters(pfault->details, ptr_map);
  2264. }
  2265. if (func->binding->bindingType == BINDING_SOAP && pfault->bindingAttributes) {
  2266. sdlSoapBindingFunctionFaultPtr soap_binding;
  2267. soap_binding = malloc(sizeof(sdlSoapBindingFunctionFault));
  2268. memset(soap_binding, 0, sizeof(sdlSoapBindingFunctionFault));
  2269. *soap_binding = *(sdlSoapBindingFunctionFaultPtr)pfault->bindingAttributes;
  2270. if (soap_binding->ns) {
  2271. soap_binding->ns = strdup(soap_binding->ns);
  2272. }
  2273. pfault->bindingAttributes = soap_binding;
  2274. }
  2275. if (key) {
  2276. /* We have to duplicate key emalloc->malloc */
  2277. zend_hash_str_add_ptr(pfaults, ZSTR_VAL(key), ZSTR_LEN(key), pfault);
  2278. } else {
  2279. zend_hash_next_index_insert_ptr(pfaults, pfault);
  2280. }
  2281. } ZEND_HASH_FOREACH_END();
  2282. return pfaults;
  2283. }
  2284. static sdlAttributePtr make_persistent_sdl_attribute(sdlAttributePtr attr, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2285. {
  2286. sdlAttributePtr pattr;
  2287. zend_string *key;
  2288. pattr = malloc(sizeof(sdlAttribute));
  2289. memset(pattr, 0, sizeof(sdlAttribute));
  2290. *pattr = *attr;
  2291. if (pattr->name) {
  2292. pattr->name = strdup(pattr->name);
  2293. }
  2294. if (pattr->namens) {
  2295. pattr->namens = strdup(pattr->namens);
  2296. }
  2297. if (pattr->ref) {
  2298. pattr->ref = strdup(pattr->ref);
  2299. }
  2300. if (pattr->def) {
  2301. pattr->def = strdup(pattr->def);
  2302. }
  2303. if (pattr->fixed) {
  2304. pattr->fixed = strdup(pattr->fixed);
  2305. }
  2306. /* we do not want to process defaultEncoding's here */
  2307. if (pattr->encode) {
  2308. make_persistent_sdl_encoder_ref(&pattr->encode, ptr_map, bp_encoders);
  2309. }
  2310. if (pattr->extraAttributes) {
  2311. sdlExtraAttributePtr tmp, pextra;
  2312. pattr->extraAttributes = malloc(sizeof(HashTable));
  2313. zend_hash_init(pattr->extraAttributes, zend_hash_num_elements(attr->extraAttributes), NULL, delete_extra_attribute_persistent, 1);
  2314. ZEND_HASH_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
  2315. if (key) {
  2316. pextra = malloc(sizeof(sdlExtraAttribute));
  2317. memset(pextra, 0, sizeof(sdlExtraAttribute));
  2318. if (tmp->ns) {
  2319. pextra->ns = strdup(tmp->ns);
  2320. }
  2321. if (tmp->val) {
  2322. pextra->val = strdup(tmp->val);
  2323. }
  2324. /* We have to duplicate key emalloc->malloc */
  2325. zend_hash_str_add_ptr(pattr->extraAttributes, ZSTR_VAL(key), ZSTR_LEN(key), pextra);
  2326. }
  2327. } ZEND_HASH_FOREACH_END();
  2328. }
  2329. return pattr;
  2330. }
  2331. static sdlContentModelPtr make_persistent_sdl_model(sdlContentModelPtr model, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2332. {
  2333. sdlContentModelPtr pmodel;
  2334. sdlContentModelPtr tmp, pcontent;
  2335. pmodel = malloc(sizeof(sdlContentModel));
  2336. memset(pmodel, 0, sizeof(sdlContentModel));
  2337. *pmodel = *model;
  2338. switch (pmodel->kind) {
  2339. case XSD_CONTENT_ELEMENT:
  2340. if (pmodel->u.element) {
  2341. make_persistent_sdl_type_ref(&pmodel->u.element, ptr_map, bp_types);
  2342. }
  2343. break;
  2344. case XSD_CONTENT_SEQUENCE:
  2345. case XSD_CONTENT_ALL:
  2346. case XSD_CONTENT_CHOICE:
  2347. pmodel->u.content = malloc(sizeof(HashTable));
  2348. zend_hash_init(pmodel->u.content, zend_hash_num_elements(model->u.content), NULL, delete_model_persistent, 1);
  2349. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  2350. pcontent = make_persistent_sdl_model(tmp, ptr_map, bp_types, bp_encoders);
  2351. zend_hash_next_index_insert_ptr(pmodel->u.content, pcontent);
  2352. } ZEND_HASH_FOREACH_END();
  2353. break;
  2354. case XSD_CONTENT_GROUP_REF:
  2355. if (pmodel->u.group_ref) {
  2356. pmodel->u.group_ref = strdup(pmodel->u.group_ref);
  2357. }
  2358. break;
  2359. case XSD_CONTENT_GROUP:
  2360. if (pmodel->u.group) {
  2361. make_persistent_sdl_type_ref(&pmodel->u.group, ptr_map, bp_types);
  2362. }
  2363. break;
  2364. default:
  2365. break;
  2366. }
  2367. return pmodel;
  2368. }
  2369. static sdlTypePtr make_persistent_sdl_type(sdlTypePtr type, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2370. {
  2371. zend_string *key;
  2372. sdlTypePtr ptype = NULL;
  2373. ptype = malloc(sizeof(sdlType));
  2374. memset(ptype, 0, sizeof(sdlType));
  2375. *ptype = *type;
  2376. if (ptype->name) {
  2377. ptype->name = strdup(ptype->name);
  2378. }
  2379. if (ptype->namens) {
  2380. ptype->namens = strdup(ptype->namens);
  2381. }
  2382. if (ptype->def) {
  2383. ptype->def = strdup(ptype->def);
  2384. }
  2385. if (ptype->fixed) {
  2386. ptype->fixed = strdup(ptype->fixed);
  2387. }
  2388. if (ptype->ref) {
  2389. ptype->ref = strdup(ptype->ref);
  2390. }
  2391. /* we do not want to process defaultEncoding's here */
  2392. if (ptype->encode) {
  2393. make_persistent_sdl_encoder_ref(&ptype->encode, ptr_map, bp_encoders);
  2394. }
  2395. if (ptype->restrictions) {
  2396. ptype->restrictions = malloc(sizeof(sdlRestrictions));
  2397. memset(ptype->restrictions, 0, sizeof(sdlRestrictions));
  2398. *ptype->restrictions = *type->restrictions;
  2399. if (ptype->restrictions->minExclusive) {
  2400. make_persistent_restriction_int(&ptype->restrictions->minExclusive);
  2401. }
  2402. if (ptype->restrictions->maxExclusive) {
  2403. make_persistent_restriction_int(&ptype->restrictions->maxExclusive);
  2404. }
  2405. if (ptype->restrictions->minInclusive) {
  2406. make_persistent_restriction_int(&ptype->restrictions->minInclusive);
  2407. }
  2408. if (ptype->restrictions->maxInclusive) {
  2409. make_persistent_restriction_int(&ptype->restrictions->maxInclusive);
  2410. }
  2411. if (ptype->restrictions->totalDigits) {
  2412. make_persistent_restriction_int(&ptype->restrictions->totalDigits);
  2413. }
  2414. if (ptype->restrictions->fractionDigits) {
  2415. make_persistent_restriction_int(&ptype->restrictions->fractionDigits);
  2416. }
  2417. if (ptype->restrictions->length) {
  2418. make_persistent_restriction_int(&ptype->restrictions->length);
  2419. }
  2420. if (ptype->restrictions->minLength) {
  2421. make_persistent_restriction_int(&ptype->restrictions->minLength);
  2422. }
  2423. if (ptype->restrictions->maxLength) {
  2424. make_persistent_restriction_int(&ptype->restrictions->maxLength);
  2425. }
  2426. if (ptype->restrictions->whiteSpace) {
  2427. make_persistent_restriction_char_int(&ptype->restrictions->whiteSpace);
  2428. }
  2429. if (ptype->restrictions->pattern) {
  2430. make_persistent_restriction_char_int(&ptype->restrictions->pattern);
  2431. }
  2432. if (type->restrictions->enumeration) {
  2433. sdlRestrictionCharPtr tmp, penum;
  2434. ptype->restrictions->enumeration = malloc(sizeof(HashTable));
  2435. zend_hash_init(ptype->restrictions->enumeration, zend_hash_num_elements(type->restrictions->enumeration), NULL, delete_restriction_var_char_persistent, 1);
  2436. ZEND_HASH_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
  2437. penum = tmp;
  2438. make_persistent_restriction_char_int(&penum);
  2439. /* We have to duplicate key emalloc->malloc */
  2440. zend_hash_str_add_ptr(ptype->restrictions->enumeration, ZSTR_VAL(key), ZSTR_LEN(key), penum);
  2441. } ZEND_HASH_FOREACH_END();
  2442. }
  2443. }
  2444. if (ptype->elements) {
  2445. sdlTypePtr tmp, pelem;
  2446. ptype->elements = malloc(sizeof(HashTable));
  2447. zend_hash_init(ptype->elements, zend_hash_num_elements(type->elements), NULL, delete_type_persistent, 1);
  2448. ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
  2449. pelem = make_persistent_sdl_type(tmp, ptr_map, bp_types, bp_encoders);
  2450. if (key) {
  2451. /* We have to duplicate key emalloc->malloc */
  2452. zend_hash_str_add_ptr(ptype->elements, ZSTR_VAL(key), ZSTR_LEN(key), pelem);
  2453. } else {
  2454. zend_hash_next_index_insert_ptr(ptype->elements, pelem);
  2455. }
  2456. zend_hash_str_add_ptr(ptr_map, (char*)&tmp, sizeof(tmp), pelem);
  2457. } ZEND_HASH_FOREACH_END();
  2458. }
  2459. if (ptype->attributes) {
  2460. sdlAttributePtr tmp, pattr;
  2461. ptype->attributes = malloc(sizeof(HashTable));
  2462. zend_hash_init(ptype->attributes, zend_hash_num_elements(type->attributes), NULL, delete_attribute_persistent, 1);
  2463. ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
  2464. pattr = make_persistent_sdl_attribute(tmp, ptr_map, bp_types, bp_encoders);
  2465. if (key) {
  2466. /* We have to duplicate key emalloc->malloc */
  2467. zend_hash_str_add_ptr(ptype->attributes, ZSTR_VAL(key), ZSTR_LEN(key), pattr);
  2468. } else {
  2469. zend_hash_next_index_insert_ptr(ptype->attributes, pattr);
  2470. }
  2471. } ZEND_HASH_FOREACH_END();
  2472. }
  2473. if (type->model) {
  2474. ptype->model = make_persistent_sdl_model(ptype->model, ptr_map, bp_types, bp_encoders);
  2475. }
  2476. return ptype;
  2477. }
  2478. static encodePtr make_persistent_sdl_encoder(encodePtr enc, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2479. {
  2480. encodePtr penc = NULL;
  2481. penc = malloc(sizeof(encode));
  2482. memset(penc, 0, sizeof(encode));
  2483. *penc = *enc;
  2484. if (penc->details.type_str) {
  2485. penc->details.type_str = strdup(penc->details.type_str);
  2486. }
  2487. if (penc->details.ns) {
  2488. penc->details.ns = strdup(penc->details.ns);
  2489. }
  2490. if (penc->details.sdl_type) {
  2491. make_persistent_sdl_type_ref(&penc->details.sdl_type, ptr_map, bp_types);
  2492. }
  2493. return penc;
  2494. }
  2495. static sdlBindingPtr make_persistent_sdl_binding(sdlBindingPtr bind, HashTable *ptr_map)
  2496. {
  2497. sdlBindingPtr pbind = NULL;
  2498. pbind = malloc(sizeof(sdlBinding));
  2499. memset(pbind, 0, sizeof(sdlBinding));
  2500. *pbind = *bind;
  2501. if (pbind->name) {
  2502. pbind->name = strdup(pbind->name);
  2503. }
  2504. if (pbind->location) {
  2505. pbind->location = strdup(pbind->location);
  2506. }
  2507. if (pbind->bindingType == BINDING_SOAP && pbind->bindingAttributes) {
  2508. sdlSoapBindingPtr soap_binding;
  2509. soap_binding = malloc(sizeof(sdlSoapBinding));
  2510. memset(soap_binding, 0, sizeof(sdlSoapBinding));
  2511. *soap_binding = *(sdlSoapBindingPtr)pbind->bindingAttributes;
  2512. pbind->bindingAttributes = soap_binding;
  2513. }
  2514. return pbind;
  2515. }
  2516. static sdlFunctionPtr make_persistent_sdl_function(sdlFunctionPtr func, HashTable *ptr_map)
  2517. {
  2518. sdlFunctionPtr pfunc = NULL;
  2519. pfunc = malloc(sizeof(sdlFunction));
  2520. memset(pfunc, 0, sizeof(sdlFunction));
  2521. *pfunc = *func;
  2522. if (pfunc->functionName) {
  2523. pfunc->functionName = strdup(pfunc->functionName);
  2524. }
  2525. if (pfunc->requestName) {
  2526. pfunc->requestName = strdup(pfunc->requestName);
  2527. }
  2528. if (pfunc->responseName) {
  2529. pfunc->responseName = strdup(pfunc->responseName);
  2530. }
  2531. if (pfunc->binding) {
  2532. sdlBindingPtr tmp;
  2533. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char*)&pfunc->binding, sizeof(pfunc->binding))) == NULL) {
  2534. assert(0);
  2535. }
  2536. pfunc->binding = tmp;
  2537. if (pfunc->binding->bindingType == BINDING_SOAP && pfunc->bindingAttributes) {
  2538. sdlSoapBindingFunctionPtr soap_binding;
  2539. soap_binding = malloc(sizeof(sdlSoapBindingFunction));
  2540. memset(soap_binding, 0, sizeof(sdlSoapBindingFunction));
  2541. *soap_binding = *(sdlSoapBindingFunctionPtr)pfunc->bindingAttributes;
  2542. if (soap_binding->soapAction) {
  2543. soap_binding->soapAction = strdup(soap_binding->soapAction);
  2544. }
  2545. make_persistent_sdl_soap_body(&soap_binding->input, ptr_map);
  2546. make_persistent_sdl_soap_body(&soap_binding->output, ptr_map);
  2547. pfunc->bindingAttributes = soap_binding;
  2548. }
  2549. if (pfunc->requestParameters) {
  2550. pfunc->requestParameters = make_persistent_sdl_parameters(pfunc->requestParameters, ptr_map);
  2551. }
  2552. if (pfunc->responseParameters) {
  2553. pfunc->responseParameters = make_persistent_sdl_parameters(pfunc->responseParameters, ptr_map);
  2554. }
  2555. if (pfunc->faults) {
  2556. pfunc->faults = make_persistent_sdl_function_faults(pfunc, pfunc->faults, ptr_map);
  2557. }
  2558. }
  2559. return pfunc;
  2560. }
  2561. static sdlPtr make_persistent_sdl(sdlPtr sdl)
  2562. {
  2563. sdlPtr psdl = NULL;
  2564. HashTable ptr_map;
  2565. HashTable bp_types, bp_encoders;
  2566. zend_string *key;
  2567. zend_hash_init(&bp_types, 0, NULL, NULL, 0);
  2568. zend_hash_init(&bp_encoders, 0, NULL, NULL, 0);
  2569. zend_hash_init(&ptr_map, 0, NULL, NULL, 0);
  2570. psdl = malloc(sizeof(*sdl));
  2571. memset(psdl, 0, sizeof(*sdl));
  2572. if (sdl->source) {
  2573. psdl->source = strdup(sdl->source);
  2574. }
  2575. if (sdl->target_ns) {
  2576. psdl->target_ns = strdup(sdl->target_ns);
  2577. }
  2578. if (sdl->groups) {
  2579. sdlTypePtr tmp;
  2580. sdlTypePtr ptype;
  2581. psdl->groups = malloc(sizeof(HashTable));
  2582. zend_hash_init(psdl->groups, zend_hash_num_elements(sdl->groups), NULL, delete_type_persistent, 1);
  2583. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
  2584. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2585. if (key) {
  2586. /* We have to duplicate key emalloc->malloc */
  2587. zend_hash_str_add_ptr(psdl->groups, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2588. } else {
  2589. zend_hash_next_index_insert_ptr(psdl->groups, ptype);
  2590. }
  2591. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2592. } ZEND_HASH_FOREACH_END();
  2593. }
  2594. if (sdl->types) {
  2595. sdlTypePtr tmp;
  2596. sdlTypePtr ptype;
  2597. psdl->types = malloc(sizeof(HashTable));
  2598. zend_hash_init(psdl->types, zend_hash_num_elements(sdl->types), NULL, delete_type_persistent, 1);
  2599. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
  2600. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2601. if (key) {
  2602. /* We have to duplicate key emalloc->malloc */
  2603. zend_hash_str_add_ptr(psdl->types, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2604. } else {
  2605. zend_hash_next_index_insert_ptr(psdl->types, ptype);
  2606. }
  2607. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2608. } ZEND_HASH_FOREACH_END();
  2609. }
  2610. if (sdl->elements) {
  2611. sdlTypePtr tmp;
  2612. sdlTypePtr ptype;
  2613. psdl->elements = malloc(sizeof(HashTable));
  2614. zend_hash_init(psdl->elements, zend_hash_num_elements(sdl->elements), NULL, delete_type_persistent, 1);
  2615. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
  2616. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2617. if (key) {
  2618. /* We have to duplicate key emalloc->malloc */
  2619. zend_hash_str_add_ptr(psdl->elements, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2620. } else {
  2621. zend_hash_next_index_insert_ptr(psdl->elements, ptype);
  2622. }
  2623. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2624. } ZEND_HASH_FOREACH_END();
  2625. }
  2626. if (sdl->encoders) {
  2627. encodePtr tmp;
  2628. encodePtr penc;
  2629. psdl->encoders = malloc(sizeof(HashTable));
  2630. zend_hash_init(psdl->encoders, zend_hash_num_elements(sdl->encoders), NULL, delete_encoder_persistent, 1);
  2631. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
  2632. penc = make_persistent_sdl_encoder(tmp, &ptr_map, &bp_types, &bp_encoders);
  2633. if (key) {
  2634. /* We have to duplicate key emalloc->malloc */
  2635. zend_hash_str_add_ptr(psdl->encoders, ZSTR_VAL(key), ZSTR_LEN(key), penc);
  2636. } else {
  2637. zend_hash_next_index_insert_ptr(psdl->encoders, penc);
  2638. }
  2639. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), penc);
  2640. } ZEND_HASH_FOREACH_END();
  2641. }
  2642. /* do backpatching here */
  2643. if (zend_hash_num_elements(&bp_types)) {
  2644. sdlTypePtr *tmp, ptype = NULL;
  2645. ZEND_HASH_FOREACH_PTR(&bp_types, tmp) {
  2646. if ((ptype = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
  2647. assert(0);
  2648. }
  2649. *tmp = ptype;
  2650. } ZEND_HASH_FOREACH_END();
  2651. }
  2652. if (zend_hash_num_elements(&bp_encoders)) {
  2653. encodePtr *tmp, penc = NULL;
  2654. ZEND_HASH_FOREACH_PTR(&bp_encoders, tmp) {
  2655. if ((penc = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
  2656. assert(0);
  2657. }
  2658. *tmp = penc;
  2659. } ZEND_HASH_FOREACH_END();
  2660. }
  2661. if (sdl->bindings) {
  2662. sdlBindingPtr tmp;
  2663. sdlBindingPtr pbind;
  2664. psdl->bindings = malloc(sizeof(HashTable));
  2665. zend_hash_init(psdl->bindings, zend_hash_num_elements(sdl->bindings), NULL, delete_binding_persistent, 1);
  2666. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
  2667. pbind = make_persistent_sdl_binding(tmp, &ptr_map);
  2668. if (key) {
  2669. /* We have to duplicate key emalloc->malloc */
  2670. zend_hash_str_add_ptr(psdl->bindings, ZSTR_VAL(key), ZSTR_LEN(key), pbind);
  2671. } else {
  2672. zend_hash_next_index_insert_ptr(psdl->bindings, pbind);
  2673. }
  2674. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pbind);
  2675. } ZEND_HASH_FOREACH_END();
  2676. }
  2677. zend_hash_init(&psdl->functions, zend_hash_num_elements(&sdl->functions), NULL, delete_function_persistent, 1);
  2678. if (zend_hash_num_elements(&sdl->functions)) {
  2679. sdlFunctionPtr tmp;
  2680. sdlFunctionPtr pfunc;
  2681. ZEND_HASH_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
  2682. pfunc = make_persistent_sdl_function(tmp, &ptr_map);
  2683. if (key) {
  2684. /* We have to duplicate key emalloc->malloc */
  2685. zend_hash_str_add_ptr(&psdl->functions, ZSTR_VAL(key), ZSTR_LEN(key), pfunc);
  2686. } else {
  2687. zend_hash_next_index_insert_ptr(&psdl->functions, pfunc);
  2688. }
  2689. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pfunc);
  2690. } ZEND_HASH_FOREACH_END();
  2691. }
  2692. if (sdl->requests) {
  2693. zval *zv;
  2694. sdlFunctionPtr tmp;
  2695. sdlFunctionPtr preq;
  2696. psdl->requests = malloc(sizeof(HashTable));
  2697. zend_hash_init(psdl->requests, zend_hash_num_elements(sdl->requests), NULL, NULL, 1);
  2698. ZEND_HASH_FOREACH_STR_KEY_VAL(sdl->requests, key, zv) {
  2699. tmp = Z_PTR_P(zv);
  2700. if ((preq = zend_hash_str_find_ptr(&ptr_map, (char*)&tmp, sizeof(tmp))) == NULL) {
  2701. assert(0);
  2702. }
  2703. Z_PTR_P(zv) = preq;
  2704. if (key) {
  2705. /* We have to duplicate key emalloc->malloc */
  2706. zend_hash_str_add_ptr(psdl->requests, ZSTR_VAL(key), ZSTR_LEN(key), preq);
  2707. }
  2708. } ZEND_HASH_FOREACH_END();
  2709. }
  2710. zend_hash_destroy(&ptr_map);
  2711. zend_hash_destroy(&bp_encoders);
  2712. zend_hash_destroy(&bp_types);
  2713. return psdl;
  2714. }
  2715. typedef struct _sdl_cache_bucket {
  2716. sdlPtr sdl;
  2717. time_t time;
  2718. } sdl_cache_bucket;
  2719. static void delete_psdl_int(sdl_cache_bucket *p)
  2720. {
  2721. sdlPtr tmp = p->sdl;
  2722. zend_hash_destroy(&tmp->functions);
  2723. if (tmp->source) {
  2724. free(tmp->source);
  2725. }
  2726. if (tmp->target_ns) {
  2727. free(tmp->target_ns);
  2728. }
  2729. if (tmp->elements) {
  2730. zend_hash_destroy(tmp->elements);
  2731. free(tmp->elements);
  2732. }
  2733. if (tmp->encoders) {
  2734. zend_hash_destroy(tmp->encoders);
  2735. free(tmp->encoders);
  2736. }
  2737. if (tmp->types) {
  2738. zend_hash_destroy(tmp->types);
  2739. free(tmp->types);
  2740. }
  2741. if (tmp->groups) {
  2742. zend_hash_destroy(tmp->groups);
  2743. free(tmp->groups);
  2744. }
  2745. if (tmp->bindings) {
  2746. zend_hash_destroy(tmp->bindings);
  2747. free(tmp->bindings);
  2748. }
  2749. if (tmp->requests) {
  2750. zend_hash_destroy(tmp->requests);
  2751. free(tmp->requests);
  2752. }
  2753. free(tmp);
  2754. }
  2755. static void delete_psdl(zval *zv)
  2756. {
  2757. delete_psdl_int(Z_PTR_P(zv));
  2758. free(Z_PTR_P(zv));
  2759. }
  2760. sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
  2761. {
  2762. char fn[MAXPATHLEN];
  2763. sdlPtr sdl = NULL;
  2764. char* old_error_code = SOAP_GLOBAL(error_code);
  2765. size_t uri_len = 0;
  2766. php_stream_context *context=NULL;
  2767. zval *tmp, *proxy_host, *proxy_port, orig_context, new_context;
  2768. smart_str headers = {0};
  2769. char* key = NULL;
  2770. time_t t = time(0);
  2771. zend_bool has_proxy_authorization = 0;
  2772. zend_bool has_authorization = 0;
  2773. ZVAL_UNDEF(&orig_context);
  2774. ZVAL_UNDEF(&new_context);
  2775. if (strchr(uri,':') != NULL || IS_ABSOLUTE_PATH(uri, uri_len)) {
  2776. uri_len = strlen(uri);
  2777. } else if (VCWD_REALPATH(uri, fn) == NULL) {
  2778. cache_wsdl = WSDL_CACHE_NONE;
  2779. } else {
  2780. uri = fn;
  2781. uri_len = strlen(uri);
  2782. }
  2783. if ((cache_wsdl & WSDL_CACHE_MEMORY) && SOAP_GLOBAL(mem_cache)) {
  2784. sdl_cache_bucket *p;
  2785. if (NULL != (p = zend_hash_str_find_ptr(SOAP_GLOBAL(mem_cache), uri, uri_len))) {
  2786. if (p->time < t - SOAP_GLOBAL(cache_ttl)) {
  2787. /* in-memory cache entry is expired */
  2788. zend_hash_str_del(&EG(persistent_list), uri, uri_len);
  2789. } else {
  2790. return p->sdl;
  2791. }
  2792. }
  2793. }
  2794. if ((cache_wsdl & WSDL_CACHE_DISK) && (uri_len < MAXPATHLEN)) {
  2795. time_t t = time(0);
  2796. char md5str[33];
  2797. PHP_MD5_CTX context;
  2798. unsigned char digest[16];
  2799. int len = strlen(SOAP_GLOBAL(cache_dir));
  2800. time_t cached;
  2801. char *user = php_get_current_user();
  2802. int user_len = user ? strlen(user) + 1 : 0;
  2803. md5str[0] = '\0';
  2804. PHP_MD5Init(&context);
  2805. PHP_MD5Update(&context, (unsigned char*)uri, uri_len);
  2806. PHP_MD5Final(digest, &context);
  2807. make_digest(md5str, digest);
  2808. key = emalloc(len+sizeof("/wsdl-")-1+user_len+2+sizeof(md5str));
  2809. memcpy(key,SOAP_GLOBAL(cache_dir),len);
  2810. memcpy(key+len,"/wsdl-",sizeof("/wsdl-")-1);
  2811. len += sizeof("/wsdl-")-1;
  2812. if (user_len) {
  2813. memcpy(key+len, user, user_len-1);
  2814. len += user_len-1;
  2815. key[len++] = '-';
  2816. }
  2817. if (WSDL_CACHE_VERSION <= 0x9f) {
  2818. key[len++] = (WSDL_CACHE_VERSION >> 8) + '0';
  2819. } else {
  2820. key[len++] = (WSDL_CACHE_VERSION >> 8) - 10 + 'a';
  2821. }
  2822. if ((WSDL_CACHE_VERSION & 0xf) <= 0x9) {
  2823. key[len++] = (WSDL_CACHE_VERSION & 0xf) + '0';
  2824. } else {
  2825. key[len++] = (WSDL_CACHE_VERSION & 0xf) - 10 + 'a';
  2826. }
  2827. memcpy(key+len,md5str,sizeof(md5str));
  2828. if ((sdl = get_sdl_from_cache(key, uri, t-SOAP_GLOBAL(cache_ttl), &cached)) != NULL) {
  2829. t = cached;
  2830. efree(key);
  2831. goto cache_in_memory;
  2832. }
  2833. }
  2834. if (NULL != (tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr),
  2835. "_stream_context", sizeof("_stream_context")-1))) {
  2836. context = php_stream_context_from_zval(tmp, 0);
  2837. } else {
  2838. context = php_stream_context_alloc();
  2839. }
  2840. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_user_agent", sizeof("_user_agent")-1)) != NULL &&
  2841. Z_TYPE_P(tmp) == IS_STRING && Z_STRLEN_P(tmp) > 0) {
  2842. smart_str_appends(&headers, "User-Agent: ");
  2843. smart_str_appends(&headers, Z_STRVAL_P(tmp));
  2844. smart_str_appends(&headers, "\r\n");
  2845. }
  2846. if ((proxy_host = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_host", sizeof("_proxy_host")-1)) != NULL &&
  2847. Z_TYPE_P(proxy_host) == IS_STRING &&
  2848. (proxy_port = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_port", sizeof("_proxy_port")-1)) != NULL &&
  2849. Z_TYPE_P(proxy_port) == IS_LONG) {
  2850. zval str_proxy;
  2851. smart_str proxy = {0};
  2852. smart_str_appends(&proxy,"tcp://");
  2853. smart_str_appends(&proxy,Z_STRVAL_P(proxy_host));
  2854. smart_str_appends(&proxy,":");
  2855. smart_str_append_long(&proxy,Z_LVAL_P(proxy_port));
  2856. smart_str_0(&proxy);
  2857. ZVAL_NEW_STR(&str_proxy, proxy.s);
  2858. if (!context) {
  2859. context = php_stream_context_alloc();
  2860. }
  2861. php_stream_context_set_option(context, "http", "proxy", &str_proxy);
  2862. zval_ptr_dtor(&str_proxy);
  2863. if (uri_len < sizeof("https://")-1 ||
  2864. strncasecmp(uri, "https://", sizeof("https://")-1) != 0) {
  2865. ZVAL_TRUE(&str_proxy);
  2866. php_stream_context_set_option(context, "http", "request_fulluri", &str_proxy);
  2867. }
  2868. has_proxy_authorization = proxy_authentication(this_ptr, &headers);
  2869. }
  2870. has_authorization = basic_authentication(this_ptr, &headers);
  2871. /* Use HTTP/1.1 with "Connection: close" by default */
  2872. if ((tmp = php_stream_context_get_option(context, "http", "protocol_version")) == NULL) {
  2873. zval http_version;
  2874. ZVAL_DOUBLE(&http_version, 1.1);
  2875. php_stream_context_set_option(context, "http", "protocol_version", &http_version);
  2876. smart_str_appendl(&headers, "Connection: close\r\n", sizeof("Connection: close\r\n")-1);
  2877. }
  2878. if (headers.s && ZSTR_LEN(headers.s) > 0) {
  2879. zval str_headers;
  2880. if (!context) {
  2881. context = php_stream_context_alloc();
  2882. } else {
  2883. http_context_headers(context, has_authorization, has_proxy_authorization, 0, &headers);
  2884. }
  2885. smart_str_0(&headers);
  2886. ZVAL_NEW_STR(&str_headers, headers.s);
  2887. php_stream_context_set_option(context, "http", "header", &str_headers);
  2888. zval_ptr_dtor(&str_headers);
  2889. }
  2890. if (context) {
  2891. php_stream_context_to_zval(context, &new_context);
  2892. php_libxml_switch_context(&new_context, &orig_context);
  2893. }
  2894. SOAP_GLOBAL(error_code) = "WSDL";
  2895. sdl = load_wsdl(this_ptr, uri);
  2896. if (sdl) {
  2897. sdl->is_persistent = 0;
  2898. }
  2899. SOAP_GLOBAL(error_code) = old_error_code;
  2900. if (context) {
  2901. php_libxml_switch_context(&orig_context, NULL);
  2902. zval_ptr_dtor(&new_context);
  2903. }
  2904. if ((cache_wsdl & WSDL_CACHE_DISK) && key) {
  2905. if (sdl) {
  2906. add_sdl_to_cache(key, uri, t, sdl);
  2907. }
  2908. efree(key);
  2909. }
  2910. cache_in_memory:
  2911. if (cache_wsdl & WSDL_CACHE_MEMORY) {
  2912. if (sdl) {
  2913. sdlPtr psdl;
  2914. sdl_cache_bucket p;
  2915. if (SOAP_GLOBAL(mem_cache) == NULL) {
  2916. SOAP_GLOBAL(mem_cache) = malloc(sizeof(HashTable));
  2917. zend_hash_init(SOAP_GLOBAL(mem_cache), 0, NULL, delete_psdl, 1);
  2918. } else if (SOAP_GLOBAL(cache_limit) > 0 &&
  2919. SOAP_GLOBAL(cache_limit) <= (zend_long)zend_hash_num_elements(SOAP_GLOBAL(mem_cache))) {
  2920. /* in-memory cache overflow */
  2921. sdl_cache_bucket *q;
  2922. time_t latest = t;
  2923. zend_string *latest_key = NULL, *key;
  2924. ZEND_HASH_FOREACH_STR_KEY_PTR(SOAP_GLOBAL(mem_cache), key, q) {
  2925. if (q->time < latest) {
  2926. latest = q->time;
  2927. latest_key = key;
  2928. }
  2929. } ZEND_HASH_FOREACH_END();
  2930. if (latest_key) {
  2931. zend_hash_del(SOAP_GLOBAL(mem_cache), latest_key);
  2932. } else {
  2933. return sdl;
  2934. }
  2935. }
  2936. psdl = make_persistent_sdl(sdl);
  2937. psdl->is_persistent = 1;
  2938. p.time = t;
  2939. p.sdl = psdl;
  2940. zend_hash_str_update_mem(SOAP_GLOBAL(mem_cache), uri,
  2941. uri_len, &p, sizeof(sdl_cache_bucket));
  2942. /* remove non-persitent sdl structure */
  2943. delete_sdl_impl(sdl);
  2944. /* and replace it with persistent one */
  2945. sdl = psdl;
  2946. }
  2947. }
  2948. return sdl;
  2949. }
  2950. /* Deletes */
  2951. void delete_sdl_impl(void *handle)
  2952. {
  2953. sdlPtr tmp = (sdlPtr)handle;
  2954. zend_hash_destroy(&tmp->functions);
  2955. if (tmp->source) {
  2956. efree(tmp->source);
  2957. }
  2958. if (tmp->target_ns) {
  2959. efree(tmp->target_ns);
  2960. }
  2961. if (tmp->elements) {
  2962. zend_hash_destroy(tmp->elements);
  2963. efree(tmp->elements);
  2964. }
  2965. if (tmp->encoders) {
  2966. zend_hash_destroy(tmp->encoders);
  2967. efree(tmp->encoders);
  2968. }
  2969. if (tmp->types) {
  2970. zend_hash_destroy(tmp->types);
  2971. efree(tmp->types);
  2972. }
  2973. if (tmp->groups) {
  2974. zend_hash_destroy(tmp->groups);
  2975. efree(tmp->groups);
  2976. }
  2977. if (tmp->bindings) {
  2978. zend_hash_destroy(tmp->bindings);
  2979. efree(tmp->bindings);
  2980. }
  2981. if (tmp->requests) {
  2982. zend_hash_destroy(tmp->requests);
  2983. efree(tmp->requests);
  2984. }
  2985. efree(tmp);
  2986. }
  2987. void delete_sdl(void *handle)
  2988. {
  2989. sdlPtr tmp = (sdlPtr)handle;
  2990. if (!tmp->is_persistent) {
  2991. delete_sdl_impl(tmp);
  2992. }
  2993. }
  2994. static void delete_binding(zval *zv)
  2995. {
  2996. sdlBindingPtr binding = Z_PTR_P(zv);
  2997. if (binding->location) {
  2998. efree(binding->location);
  2999. }
  3000. if (binding->name) {
  3001. efree(binding->name);
  3002. }
  3003. if (binding->bindingType == BINDING_SOAP) {
  3004. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3005. if (soapBind) {
  3006. efree(soapBind);
  3007. }
  3008. }
  3009. efree(binding);
  3010. }
  3011. static void delete_binding_persistent(zval *zv)
  3012. {
  3013. sdlBindingPtr binding = Z_PTR_P(zv);
  3014. if (binding->location) {
  3015. free(binding->location);
  3016. }
  3017. if (binding->name) {
  3018. free(binding->name);
  3019. }
  3020. if (binding->bindingType == BINDING_SOAP) {
  3021. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3022. if (soapBind) {
  3023. free(soapBind);
  3024. }
  3025. }
  3026. free(binding);
  3027. }
  3028. static void delete_sdl_soap_binding_function_body(sdlSoapBindingFunctionBody body)
  3029. {
  3030. if (body.ns) {
  3031. efree(body.ns);
  3032. }
  3033. if (body.headers) {
  3034. zend_hash_destroy(body.headers);
  3035. efree(body.headers);
  3036. }
  3037. }
  3038. static void delete_sdl_soap_binding_function_body_persistent(sdlSoapBindingFunctionBody body)
  3039. {
  3040. if (body.ns) {
  3041. free(body.ns);
  3042. }
  3043. if (body.headers) {
  3044. zend_hash_destroy(body.headers);
  3045. free(body.headers);
  3046. }
  3047. }
  3048. static void delete_function(zval *zv)
  3049. {
  3050. sdlFunctionPtr function = Z_PTR_P(zv);
  3051. if (function->functionName) {
  3052. efree(function->functionName);
  3053. }
  3054. if (function->requestName) {
  3055. efree(function->requestName);
  3056. }
  3057. if (function->responseName) {
  3058. efree(function->responseName);
  3059. }
  3060. if (function->requestParameters) {
  3061. zend_hash_destroy(function->requestParameters);
  3062. efree(function->requestParameters);
  3063. }
  3064. if (function->responseParameters) {
  3065. zend_hash_destroy(function->responseParameters);
  3066. efree(function->responseParameters);
  3067. }
  3068. if (function->faults) {
  3069. zend_hash_destroy(function->faults);
  3070. efree(function->faults);
  3071. }
  3072. if (function->bindingAttributes &&
  3073. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3074. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3075. if (soapFunction->soapAction) {
  3076. efree(soapFunction->soapAction);
  3077. }
  3078. delete_sdl_soap_binding_function_body(soapFunction->input);
  3079. delete_sdl_soap_binding_function_body(soapFunction->output);
  3080. efree(soapFunction);
  3081. }
  3082. efree(function);
  3083. }
  3084. static void delete_function_persistent(zval *zv)
  3085. {
  3086. sdlFunctionPtr function = Z_PTR_P(zv);
  3087. if (function->functionName) {
  3088. free(function->functionName);
  3089. }
  3090. if (function->requestName) {
  3091. free(function->requestName);
  3092. }
  3093. if (function->responseName) {
  3094. free(function->responseName);
  3095. }
  3096. if (function->requestParameters) {
  3097. zend_hash_destroy(function->requestParameters);
  3098. free(function->requestParameters);
  3099. }
  3100. if (function->responseParameters) {
  3101. zend_hash_destroy(function->responseParameters);
  3102. free(function->responseParameters);
  3103. }
  3104. if (function->faults) {
  3105. zend_hash_destroy(function->faults);
  3106. free(function->faults);
  3107. }
  3108. if (function->bindingAttributes &&
  3109. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3110. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3111. if (soapFunction->soapAction) {
  3112. free(soapFunction->soapAction);
  3113. }
  3114. delete_sdl_soap_binding_function_body_persistent(soapFunction->input);
  3115. delete_sdl_soap_binding_function_body_persistent(soapFunction->output);
  3116. free(soapFunction);
  3117. }
  3118. free(function);
  3119. }
  3120. static void delete_parameter(zval *zv)
  3121. {
  3122. sdlParamPtr param = Z_PTR_P(zv);
  3123. if (param->paramName) {
  3124. efree(param->paramName);
  3125. }
  3126. efree(param);
  3127. }
  3128. static void delete_parameter_persistent(zval *zv)
  3129. {
  3130. sdlParamPtr param = Z_PTR_P(zv);
  3131. if (param->paramName) {
  3132. free(param->paramName);
  3133. }
  3134. free(param);
  3135. }
  3136. static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr)
  3137. {
  3138. if (hdr->name) {
  3139. efree(hdr->name);
  3140. }
  3141. if (hdr->ns) {
  3142. efree(hdr->ns);
  3143. }
  3144. if (hdr->headerfaults) {
  3145. zend_hash_destroy(hdr->headerfaults);
  3146. efree(hdr->headerfaults);
  3147. }
  3148. efree(hdr);
  3149. }
  3150. static void delete_header(zval *zv)
  3151. {
  3152. delete_header_int(Z_PTR_P(zv));
  3153. }
  3154. static void delete_header_persistent(zval *zv)
  3155. {
  3156. sdlSoapBindingFunctionHeaderPtr hdr = Z_PTR_P(zv);
  3157. if (hdr->name) {
  3158. free(hdr->name);
  3159. }
  3160. if (hdr->ns) {
  3161. free(hdr->ns);
  3162. }
  3163. if (hdr->headerfaults) {
  3164. zend_hash_destroy(hdr->headerfaults);
  3165. free(hdr->headerfaults);
  3166. }
  3167. free(hdr);
  3168. }
  3169. static void delete_fault(zval *zv)
  3170. {
  3171. sdlFaultPtr fault = Z_PTR_P(zv);
  3172. if (fault->name) {
  3173. efree(fault->name);
  3174. }
  3175. if (fault->details) {
  3176. zend_hash_destroy(fault->details);
  3177. efree(fault->details);
  3178. }
  3179. if (fault->bindingAttributes) {
  3180. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3181. if (binding->ns) {
  3182. efree(binding->ns);
  3183. }
  3184. efree(fault->bindingAttributes);
  3185. }
  3186. efree(fault);
  3187. }
  3188. static void delete_fault_persistent(zval *zv)
  3189. {
  3190. sdlFaultPtr fault = Z_PTR_P(zv);
  3191. if (fault->name) {
  3192. free(fault->name);
  3193. }
  3194. if (fault->details) {
  3195. zend_hash_destroy(fault->details);
  3196. free(fault->details);
  3197. }
  3198. if (fault->bindingAttributes) {
  3199. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3200. if (binding->ns) {
  3201. free(binding->ns);
  3202. }
  3203. free(fault->bindingAttributes);
  3204. }
  3205. free(fault);
  3206. }
  3207. static void delete_document(zval *zv)
  3208. {
  3209. xmlDocPtr doc = Z_PTR_P(zv);
  3210. xmlFreeDoc(doc);
  3211. }