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

/ext/soap/php_sdl.c

http://github.com/infusion/PHP
C | 3660 lines | 3150 code | 451 blank | 59 comment | 937 complexity | 20c169c3e9195f7297b0b7594439edd9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Brad Lafountain <rodif_bl@yahoo.com> |
  16. | Shane Caraveo <shane@caraveo.com> |
  17. | Dmitry Stogov <dmitry@zend.com> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id: php_sdl.c 306939 2011-01-01 02:19:59Z felipe $ */
  21. #include "php_soap.h"
  22. #include "ext/libxml/php_libxml.h"
  23. #include "libxml/uri.h"
  24. #include "ext/standard/md5.h"
  25. #include "tsrm_virtual_cwd.h"
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #ifndef O_BINARY
  30. # define O_BINARY 0
  31. #endif
  32. static void delete_fault(void *fault);
  33. static void delete_fault_persistent(void *fault);
  34. static void delete_binding(void *binding);
  35. static void delete_binding_persistent(void *binding);
  36. static void delete_function(void *function);
  37. static void delete_function_persistent(void *function);
  38. static void delete_parameter(void *paramater);
  39. static void delete_parameter_persistent(void *paramater);
  40. static void delete_header(void *header);
  41. static void delete_header_persistent(void *header);
  42. static void delete_document(void *doc_ptr);
  43. encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  44. {
  45. encodePtr enc = NULL;
  46. xmlNsPtr nsptr;
  47. char *ns, *cptype;
  48. parse_namespace(type, &cptype, &ns);
  49. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  50. if (nsptr != NULL) {
  51. enc = get_encoder(sdl, (char*)nsptr->href, cptype);
  52. if (enc == NULL) {
  53. enc = get_encoder_ex(sdl, cptype, strlen(cptype));
  54. }
  55. } else {
  56. enc = get_encoder_ex(sdl, (char*)type, xmlStrlen(type));
  57. }
  58. efree(cptype);
  59. if (ns) {efree(ns);}
  60. return enc;
  61. }
  62. static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  63. {
  64. sdlTypePtr ret = NULL;
  65. if (sdl->elements) {
  66. xmlNsPtr nsptr;
  67. char *ns, *cptype;
  68. sdlTypePtr *sdl_type;
  69. parse_namespace(type, &cptype, &ns);
  70. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  71. if (nsptr != NULL) {
  72. int ns_len = xmlStrlen(nsptr->href);
  73. int type_len = strlen(cptype);
  74. int len = ns_len + type_len + 1;
  75. char *nscat = emalloc(len + 1);
  76. memcpy(nscat, nsptr->href, ns_len);
  77. nscat[ns_len] = ':';
  78. memcpy(nscat+ns_len+1, cptype, type_len);
  79. nscat[len] = '\0';
  80. if (zend_hash_find(sdl->elements, nscat, len + 1, (void **)&sdl_type) == SUCCESS) {
  81. ret = *sdl_type;
  82. } else if (zend_hash_find(sdl->elements, (char*)type, type_len + 1, (void **)&sdl_type) == SUCCESS) {
  83. ret = *sdl_type;
  84. }
  85. efree(nscat);
  86. } else {
  87. if (zend_hash_find(sdl->elements, (char*)type, xmlStrlen(type) + 1, (void **)&sdl_type) == SUCCESS) {
  88. ret = *sdl_type;
  89. }
  90. }
  91. efree(cptype);
  92. if (ns) {efree(ns);}
  93. }
  94. return ret;
  95. }
  96. encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type)
  97. {
  98. encodePtr enc = NULL;
  99. char *nscat;
  100. int ns_len = strlen(ns);
  101. int type_len = strlen(type);
  102. int len = ns_len + type_len + 1;
  103. nscat = emalloc(len + 1);
  104. memcpy(nscat, ns, ns_len);
  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_update(sdl->encoders, nscat, len + 1, &new_enc, sizeof(encodePtr), NULL);
  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. TSRMLS_FETCH();
  151. if (zend_hash_find(&SOAP_GLOBAL(defEnc), (char*)nscat, len + 1, (void **)&enc) == SUCCESS) {
  152. return (*enc);
  153. } else if (sdl && sdl->encoders && zend_hash_find(sdl->encoders, (char*)nscat, len + 1, (void **)&enc) == SUCCESS) {
  154. return (*enc);
  155. }
  156. return NULL;
  157. }
  158. sdlBindingPtr get_binding_from_type(sdlPtr sdl, int type)
  159. {
  160. sdlBindingPtr *binding;
  161. if (sdl == NULL) {
  162. return NULL;
  163. }
  164. for (zend_hash_internal_pointer_reset(sdl->bindings);
  165. zend_hash_get_current_data(sdl->bindings, (void **) &binding) == SUCCESS;
  166. zend_hash_move_forward(sdl->bindings)) {
  167. if ((*binding)->bindingType == type) {
  168. return *binding;
  169. }
  170. }
  171. return NULL;
  172. }
  173. sdlBindingPtr get_binding_from_name(sdlPtr sdl, char *name, char *ns)
  174. {
  175. sdlBindingPtr binding = NULL;
  176. smart_str key = {0};
  177. smart_str_appends(&key, ns);
  178. smart_str_appendc(&key, ':');
  179. smart_str_appends(&key, name);
  180. smart_str_0(&key);
  181. zend_hash_find(sdl->bindings, key.c, key.len, (void **)&binding);
  182. smart_str_free(&key);
  183. return binding;
  184. }
  185. static int is_wsdl_element(xmlNodePtr node)
  186. {
  187. if (node->ns && strcmp((char*)node->ns->href, WSDL_NAMESPACE) != 0) {
  188. xmlAttrPtr attr;
  189. if ((attr = get_attribute_ex(node->properties, "required", WSDL_NAMESPACE)) != NULL &&
  190. attr->children && attr->children->content &&
  191. (strcmp((char*)attr->children->content, "1") == 0 ||
  192. strcmp((char*)attr->children->content, "true") == 0)) {
  193. soap_error1(E_ERROR, "Parsing WSDL: Unknown required WSDL extension '%s'", node->ns->href);
  194. }
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. void sdl_set_uri_credentials(sdlCtx *ctx, char *uri TSRMLS_DC)
  200. {
  201. char *s;
  202. int l1, l2;
  203. zval *context = NULL;
  204. zval **header = NULL;
  205. /* check if we load xsd from the same server */
  206. s = strstr(ctx->sdl->source, "://");
  207. if (!s) return;
  208. s = strchr(s+3, '/');
  209. l1 = s - ctx->sdl->source;
  210. s = strstr((char*)uri, "://");
  211. if (!s) return;
  212. s = strchr(s+3, '/');
  213. l2 = s - (char*)uri;
  214. if (l1 != l2 || memcmp(ctx->sdl->source, uri, l1) != 0) {
  215. /* another server. clear authentication credentals */
  216. context = php_libxml_switch_context(NULL TSRMLS_CC);
  217. php_libxml_switch_context(context TSRMLS_CC);
  218. if (context) {
  219. ctx->context = php_stream_context_from_zval(context, 1);
  220. if (ctx->context &&
  221. php_stream_context_get_option(ctx->context, "http", "header", &header) == SUCCESS) {
  222. s = strstr(Z_STRVAL_PP(header), "Authorization: Basic");
  223. if (s && (s == Z_STRVAL_PP(header) || *(s-1) == '\n' || *(s-1) == '\r')) {
  224. char *rest = strstr(s, "\r\n");
  225. if (rest) {
  226. zval new_header;
  227. rest += 2;
  228. Z_TYPE(new_header) = IS_STRING;
  229. Z_STRLEN(new_header) = Z_STRLEN_PP(header) - (rest - s);
  230. Z_STRVAL(new_header) = emalloc(Z_STRLEN_PP(header) + 1);
  231. memcpy(Z_STRVAL(new_header), Z_STRVAL_PP(header), s - Z_STRVAL_PP(header));
  232. memcpy(Z_STRVAL(new_header) + (s - Z_STRVAL_PP(header)), rest, Z_STRLEN_PP(header) - (rest - Z_STRVAL_PP(header)) + 1);
  233. ctx->old_header = *header;
  234. Z_ADDREF_P(ctx->old_header);
  235. php_stream_context_set_option(ctx->context, "http", "header", &new_header);
  236. zval_dtor(&new_header);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. void sdl_restore_uri_credentials(sdlCtx *ctx TSRMLS_DC)
  244. {
  245. if (ctx->old_header) {
  246. php_stream_context_set_option(ctx->context, "http", "header", ctx->old_header);
  247. zval_ptr_dtor(&ctx->old_header);
  248. ctx->old_header = NULL;
  249. }
  250. ctx->context = NULL;
  251. }
  252. static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include TSRMLS_DC)
  253. {
  254. sdlPtr tmpsdl = ctx->sdl;
  255. xmlDocPtr wsdl;
  256. xmlNodePtr root, definitions, trav;
  257. xmlAttrPtr targetNamespace;
  258. if (zend_hash_exists(&ctx->docs, struri, strlen(struri)+1)) {
  259. return;
  260. }
  261. sdl_set_uri_credentials(ctx, struri TSRMLS_CC);
  262. wsdl = soap_xmlParseFile(struri TSRMLS_CC);
  263. sdl_restore_uri_credentials(ctx TSRMLS_CC);
  264. if (!wsdl) {
  265. xmlErrorPtr xmlErrorPtr = xmlGetLastError();
  266. if (xmlErrorPtr) {
  267. soap_error2(E_ERROR, "Parsing WSDL: Couldn't load from '%s' : %s", struri, xmlErrorPtr->message);
  268. } else {
  269. soap_error1(E_ERROR, "Parsing WSDL: Couldn't load from '%s'", struri);
  270. }
  271. }
  272. zend_hash_add(&ctx->docs, struri, strlen(struri)+1, (void**)&wsdl, sizeof(xmlDocPtr), NULL);
  273. root = wsdl->children;
  274. definitions = get_node_ex(root, "definitions", WSDL_NAMESPACE);
  275. if (!definitions) {
  276. if (include) {
  277. xmlNodePtr schema = get_node_ex(root, "schema", XSD_NAMESPACE);
  278. if (schema) {
  279. load_schema(ctx, schema TSRMLS_CC);
  280. return;
  281. }
  282. }
  283. soap_error1(E_ERROR, "Parsing WSDL: Couldn't find <definitions> in '%s'", struri);
  284. }
  285. if (!include) {
  286. targetNamespace = get_attribute(definitions->properties, "targetNamespace");
  287. if (targetNamespace) {
  288. tmpsdl->target_ns = estrdup((char*)targetNamespace->children->content);
  289. }
  290. }
  291. trav = definitions->children;
  292. while (trav != NULL) {
  293. if (!is_wsdl_element(trav)) {
  294. trav = trav->next;
  295. continue;
  296. }
  297. if (node_is_equal(trav,"types")) {
  298. /* TODO: Only one "types" is allowed */
  299. xmlNodePtr trav2 = trav->children;
  300. while (trav2 != NULL) {
  301. if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
  302. load_schema(ctx, trav2 TSRMLS_CC);
  303. } else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  304. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
  305. }
  306. trav2 = trav2->next;
  307. }
  308. } else if (node_is_equal(trav,"import")) {
  309. /* TODO: namespace ??? */
  310. xmlAttrPtr tmp = get_attribute(trav->properties, "location");
  311. if (tmp) {
  312. xmlChar *uri;
  313. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  314. if (base == NULL) {
  315. uri = xmlBuildURI(tmp->children->content, trav->doc->URL);
  316. } else {
  317. uri = xmlBuildURI(tmp->children->content, base);
  318. xmlFree(base);
  319. }
  320. load_wsdl_ex(this_ptr, (char*)uri, ctx, 1 TSRMLS_CC);
  321. xmlFree(uri);
  322. }
  323. } else if (node_is_equal(trav,"message")) {
  324. xmlAttrPtr name = get_attribute(trav->properties, "name");
  325. if (name && name->children && name->children->content) {
  326. if (zend_hash_add(&ctx->messages, (char*)name->children->content, xmlStrlen(name->children->content)+1,&trav, sizeof(xmlNodePtr), NULL) != SUCCESS) {
  327. soap_error1(E_ERROR, "Parsing WSDL: <message> '%s' already defined", name->children->content);
  328. }
  329. } else {
  330. soap_error0(E_ERROR, "Parsing WSDL: <message> has no name attribute");
  331. }
  332. } else if (node_is_equal(trav,"portType")) {
  333. xmlAttrPtr name = get_attribute(trav->properties, "name");
  334. if (name && name->children && name->children->content) {
  335. if (zend_hash_add(&ctx->portTypes, (char*)name->children->content, xmlStrlen(name->children->content)+1,&trav, sizeof(xmlNodePtr), NULL) != SUCCESS) {
  336. soap_error1(E_ERROR, "Parsing WSDL: <portType> '%s' already defined", name->children->content);
  337. }
  338. } else {
  339. soap_error0(E_ERROR, "Parsing WSDL: <portType> has no name attribute");
  340. }
  341. } else if (node_is_equal(trav,"binding")) {
  342. xmlAttrPtr name = get_attribute(trav->properties, "name");
  343. if (name && name->children && name->children->content) {
  344. if (zend_hash_add(&ctx->bindings, (char*)name->children->content, xmlStrlen(name->children->content)+1,&trav, sizeof(xmlNodePtr), NULL) != SUCCESS) {
  345. soap_error1(E_ERROR, "Parsing WSDL: <binding> '%s' already defined", name->children->content);
  346. }
  347. } else {
  348. soap_error0(E_ERROR, "Parsing WSDL: <binding> has no name attribute");
  349. }
  350. } else if (node_is_equal(trav,"service")) {
  351. xmlAttrPtr name = get_attribute(trav->properties, "name");
  352. if (name && name->children && name->children->content) {
  353. if (zend_hash_add(&ctx->services, (char*)name->children->content, xmlStrlen(name->children->content)+1,&trav, sizeof(xmlNodePtr), NULL) != SUCCESS) {
  354. soap_error1(E_ERROR, "Parsing WSDL: <service> '%s' already defined", name->children->content);
  355. }
  356. } else {
  357. soap_error0(E_ERROR, "Parsing WSDL: <service> has no name attribute");
  358. }
  359. } else if (!node_is_equal(trav,"documentation")) {
  360. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  361. }
  362. trav = trav->next;
  363. }
  364. }
  365. static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xmlNodePtr header, char* wsdl_soap_namespace, int fault)
  366. {
  367. xmlAttrPtr tmp;
  368. xmlNodePtr *message, part;
  369. char *ctype;
  370. sdlSoapBindingFunctionHeaderPtr h;
  371. tmp = get_attribute(header->properties, "message");
  372. if (!tmp) {
  373. soap_error0(E_ERROR, "Parsing WSDL: Missing message attribute for <header>");
  374. }
  375. ctype = strrchr((char*)tmp->children->content,':');
  376. if (ctype == NULL) {
  377. ctype = (char*)tmp->children->content;
  378. } else {
  379. ++ctype;
  380. }
  381. if (zend_hash_find(&ctx->messages, ctype, strlen(ctype)+1, (void**)&message) != SUCCESS) {
  382. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", tmp->children->content);
  383. }
  384. tmp = get_attribute(header->properties, "part");
  385. if (!tmp) {
  386. soap_error0(E_ERROR, "Parsing WSDL: Missing part attribute for <header>");
  387. }
  388. part = get_node_with_attribute_ex((*message)->children, "part", WSDL_NAMESPACE, "name", (char*)tmp->children->content, NULL);
  389. if (!part) {
  390. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", tmp->children->content);
  391. }
  392. h = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  393. memset(h, 0, sizeof(sdlSoapBindingFunctionHeader));
  394. h->name = estrdup((char*)tmp->children->content);
  395. tmp = get_attribute(header->properties, "use");
  396. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  397. h->use = SOAP_ENCODED;
  398. } else {
  399. h->use = SOAP_LITERAL;
  400. }
  401. tmp = get_attribute(header->properties, "namespace");
  402. if (tmp) {
  403. h->ns = estrdup((char*)tmp->children->content);
  404. }
  405. if (h->use == SOAP_ENCODED) {
  406. tmp = get_attribute(header->properties, "encodingStyle");
  407. if (tmp) {
  408. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  409. h->encodingStyle = SOAP_ENCODING_1_1;
  410. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  411. h->encodingStyle = SOAP_ENCODING_1_2;
  412. } else {
  413. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  414. }
  415. } else {
  416. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  417. }
  418. }
  419. tmp = get_attribute(part->properties, "type");
  420. if (tmp != NULL) {
  421. h->encode = get_encoder_from_prefix(ctx->sdl, part, tmp->children->content);
  422. } else {
  423. tmp = get_attribute(part->properties, "element");
  424. if (tmp != NULL) {
  425. h->element = get_element(ctx->sdl, part, tmp->children->content);
  426. if (h->element) {
  427. h->encode = h->element->encode;
  428. if (!h->ns && h->element->namens) {
  429. h->ns = estrdup(h->element->namens);
  430. }
  431. if (h->element->name) {
  432. efree(h->name);
  433. h->name = estrdup(h->element->name);
  434. }
  435. }
  436. }
  437. }
  438. if (!fault) {
  439. xmlNodePtr trav = header->children;
  440. while (trav != NULL) {
  441. if (node_is_equal_ex(trav, "headerfault", wsdl_soap_namespace)) {
  442. sdlSoapBindingFunctionHeaderPtr hf = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 1);
  443. smart_str key = {0};
  444. if (h->headerfaults == NULL) {
  445. h->headerfaults = emalloc(sizeof(HashTable));
  446. zend_hash_init(h->headerfaults, 0, NULL, delete_header, 0);
  447. }
  448. if (hf->ns) {
  449. smart_str_appends(&key,hf->ns);
  450. smart_str_appendc(&key,':');
  451. }
  452. smart_str_appends(&key,hf->name);
  453. smart_str_0(&key);
  454. if (zend_hash_add(h->headerfaults, key.c, key.len+1, (void**)&hf, sizeof(sdlSoapBindingFunctionHeaderPtr), NULL) != SUCCESS) {
  455. delete_header((void**)&hf);
  456. }
  457. smart_str_free(&key);
  458. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  459. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  460. }
  461. trav = trav->next;
  462. }
  463. }
  464. return h;
  465. }
  466. static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap_namespace, sdlSoapBindingFunctionBody *binding, HashTable* params)
  467. {
  468. xmlNodePtr body, trav;
  469. xmlAttrPtr tmp;
  470. trav = node->children;
  471. while (trav != NULL) {
  472. if (node_is_equal_ex(trav, "body", wsdl_soap_namespace)) {
  473. body = trav;
  474. tmp = get_attribute(body->properties, "use");
  475. if (tmp && !strncmp((char*)tmp->children->content, "literal", sizeof("literal"))) {
  476. binding->use = SOAP_LITERAL;
  477. } else {
  478. binding->use = SOAP_ENCODED;
  479. }
  480. tmp = get_attribute(body->properties, "namespace");
  481. if (tmp) {
  482. binding->ns = estrdup((char*)tmp->children->content);
  483. }
  484. tmp = get_attribute(body->properties, "parts");
  485. if (tmp) {
  486. HashTable ht;
  487. char *parts = (char*)tmp->children->content;
  488. /* Delete all parts those are not in the "parts" attribute */
  489. zend_hash_init(&ht, 0, NULL, delete_parameter, 0);
  490. while (*parts) {
  491. HashPosition pos;
  492. sdlParamPtr *param;
  493. int found = 0;
  494. char *end;
  495. while (*parts == ' ') ++parts;
  496. if (*parts == '\0') break;
  497. end = strchr(parts, ' ');
  498. if (end) *end = '\0';
  499. zend_hash_internal_pointer_reset_ex(params, &pos);
  500. while (zend_hash_get_current_data_ex(params, (void **)&param, &pos) != FAILURE) {
  501. if ((*param)->paramName &&
  502. strcmp(parts, (*param)->paramName) == 0) {
  503. sdlParamPtr x_param;
  504. x_param = emalloc(sizeof(sdlParam));
  505. *x_param = **param;
  506. (*param)->paramName = NULL;
  507. zend_hash_next_index_insert(&ht, &x_param, sizeof(sdlParamPtr), NULL);
  508. found = 1;
  509. break;
  510. }
  511. zend_hash_move_forward_ex(params, &pos);
  512. }
  513. if (!found) {
  514. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", parts);
  515. }
  516. parts += strlen(parts);
  517. if (end) *end = ' ';
  518. }
  519. zend_hash_destroy(params);
  520. *params = ht;
  521. }
  522. if (binding->use == SOAP_ENCODED) {
  523. tmp = get_attribute(body->properties, "encodingStyle");
  524. if (tmp) {
  525. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  526. binding->encodingStyle = SOAP_ENCODING_1_1;
  527. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  528. binding->encodingStyle = SOAP_ENCODING_1_2;
  529. } else {
  530. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  531. }
  532. } else {
  533. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  534. }
  535. }
  536. } else if (node_is_equal_ex(trav, "header", wsdl_soap_namespace)) {
  537. sdlSoapBindingFunctionHeaderPtr h = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 0);
  538. smart_str key = {0};
  539. if (binding->headers == NULL) {
  540. binding->headers = emalloc(sizeof(HashTable));
  541. zend_hash_init(binding->headers, 0, NULL, delete_header, 0);
  542. }
  543. if (h->ns) {
  544. smart_str_appends(&key,h->ns);
  545. smart_str_appendc(&key,':');
  546. }
  547. smart_str_appends(&key,h->name);
  548. smart_str_0(&key);
  549. if (zend_hash_add(binding->headers, key.c, key.len+1, (void**)&h, sizeof(sdlSoapBindingFunctionHeaderPtr), NULL) != SUCCESS) {
  550. delete_header((void**)&h);
  551. }
  552. smart_str_free(&key);
  553. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  554. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  555. }
  556. trav = trav->next;
  557. }
  558. }
  559. static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
  560. {
  561. xmlNodePtr trav, part, message = NULL, *tmp;
  562. HashTable* parameters = NULL;
  563. char *ctype;
  564. ctype = strrchr((char*)message_name,':');
  565. if (ctype == NULL) {
  566. ctype = (char*)message_name;
  567. } else {
  568. ++ctype;
  569. }
  570. if (zend_hash_find(&ctx->messages, ctype, strlen(ctype)+1, (void**)&tmp) != SUCCESS) {
  571. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", message_name);
  572. }
  573. message = *tmp;
  574. parameters = emalloc(sizeof(HashTable));
  575. zend_hash_init(parameters, 0, NULL, delete_parameter, 0);
  576. trav = message->children;
  577. while (trav != NULL) {
  578. xmlAttrPtr element, type, name;
  579. sdlParamPtr param;
  580. if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
  581. soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>", trav->name);
  582. }
  583. if (node_is_equal(trav,"documentation")) {
  584. trav = trav->next;
  585. continue;
  586. }
  587. if (!node_is_equal(trav,"part")) {
  588. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  589. }
  590. part = trav;
  591. param = emalloc(sizeof(sdlParam));
  592. memset(param,0,sizeof(sdlParam));
  593. param->order = 0;
  594. name = get_attribute(part->properties, "name");
  595. if (name == NULL) {
  596. soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'", message->name);
  597. }
  598. param->paramName = estrdup((char*)name->children->content);
  599. type = get_attribute(part->properties, "type");
  600. if (type != NULL) {
  601. param->encode = get_encoder_from_prefix(ctx->sdl, part, type->children->content);
  602. } else {
  603. element = get_attribute(part->properties, "element");
  604. if (element != NULL) {
  605. param->element = get_element(ctx->sdl, part, element->children->content);
  606. if (param->element) {
  607. param->encode = param->element->encode;
  608. }
  609. }
  610. }
  611. zend_hash_next_index_insert(parameters, &param, sizeof(sdlParamPtr), NULL);
  612. trav = trav->next;
  613. }
  614. return parameters;
  615. }
  616. static sdlPtr load_wsdl(zval *this_ptr, char *struri TSRMLS_DC)
  617. {
  618. sdlCtx ctx;
  619. int i,n;
  620. memset(&ctx,0,sizeof(ctx));
  621. ctx.sdl = emalloc(sizeof(sdl));
  622. memset(ctx.sdl, 0, sizeof(sdl));
  623. ctx.sdl->source = estrdup(struri);
  624. zend_hash_init(&ctx.sdl->functions, 0, NULL, delete_function, 0);
  625. zend_hash_init(&ctx.docs, 0, NULL, delete_document, 0);
  626. zend_hash_init(&ctx.messages, 0, NULL, NULL, 0);
  627. zend_hash_init(&ctx.bindings, 0, NULL, NULL, 0);
  628. zend_hash_init(&ctx.portTypes, 0, NULL, NULL, 0);
  629. zend_hash_init(&ctx.services, 0, NULL, NULL, 0);
  630. load_wsdl_ex(this_ptr, struri,&ctx, 0 TSRMLS_CC);
  631. schema_pass2(&ctx);
  632. n = zend_hash_num_elements(&ctx.services);
  633. if (n > 0) {
  634. zend_hash_internal_pointer_reset(&ctx.services);
  635. for (i = 0; i < n; i++) {
  636. xmlNodePtr *tmp, service;
  637. xmlNodePtr trav, port;
  638. int has_soap_port = 0;
  639. zend_hash_get_current_data(&ctx.services, (void **)&tmp);
  640. service = *tmp;
  641. trav = service->children;
  642. while (trav != NULL) {
  643. xmlAttrPtr type, name, bindingAttr, location;
  644. xmlNodePtr portType, operation;
  645. xmlNodePtr address, binding, trav2;
  646. char *ctype;
  647. sdlBindingPtr tmpbinding;
  648. char *wsdl_soap_namespace = NULL;
  649. if (!is_wsdl_element(trav) || node_is_equal(trav,"documentation")) {
  650. trav = trav->next;
  651. continue;
  652. }
  653. if (!node_is_equal(trav,"port")) {
  654. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  655. }
  656. port = trav;
  657. tmpbinding = emalloc(sizeof(sdlBinding));
  658. memset(tmpbinding, 0, sizeof(sdlBinding));
  659. bindingAttr = get_attribute(port->properties, "binding");
  660. if (bindingAttr == NULL) {
  661. soap_error0(E_ERROR, "Parsing WSDL: No binding associated with <port>");
  662. }
  663. /* find address and figure out binding type */
  664. address = NULL;
  665. trav2 = port->children;
  666. while (trav2 != NULL) {
  667. if (node_is_equal(trav2,"address") && trav2->ns) {
  668. if (!strncmp((char*)trav2->ns->href, WSDL_SOAP11_NAMESPACE, sizeof(WSDL_SOAP11_NAMESPACE))) {
  669. address = trav2;
  670. wsdl_soap_namespace = WSDL_SOAP11_NAMESPACE;
  671. tmpbinding->bindingType = BINDING_SOAP;
  672. } else if (!strncmp((char*)trav2->ns->href, WSDL_SOAP12_NAMESPACE, sizeof(WSDL_SOAP12_NAMESPACE))) {
  673. address = trav2;
  674. wsdl_soap_namespace = WSDL_SOAP12_NAMESPACE;
  675. tmpbinding->bindingType = BINDING_SOAP;
  676. } else if (!strncmp((char*)trav2->ns->href, RPC_SOAP12_NAMESPACE, sizeof(RPC_SOAP12_NAMESPACE))) {
  677. address = trav2;
  678. wsdl_soap_namespace = RPC_SOAP12_NAMESPACE;
  679. tmpbinding->bindingType = BINDING_SOAP;
  680. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP11_NAMESPACE, sizeof(WSDL_HTTP11_NAMESPACE))) {
  681. address = trav2;
  682. tmpbinding->bindingType = BINDING_HTTP;
  683. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP12_NAMESPACE, sizeof(WSDL_HTTP12_NAMESPACE))) {
  684. address = trav2;
  685. tmpbinding->bindingType = BINDING_HTTP;
  686. }
  687. }
  688. if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  689. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
  690. }
  691. trav2 = trav2->next;
  692. }
  693. if (!address || tmpbinding->bindingType == BINDING_HTTP) {
  694. if (has_soap_port || trav->next || i < n-1) {
  695. efree(tmpbinding);
  696. trav = trav->next;
  697. continue;
  698. } else if (!address) {
  699. soap_error0(E_ERROR, "Parsing WSDL: No address associated with <port>");
  700. }
  701. }
  702. has_soap_port = 1;
  703. location = get_attribute(address->properties, "location");
  704. if (!location) {
  705. soap_error0(E_ERROR, "Parsing WSDL: No location associated with <port>");
  706. }
  707. tmpbinding->location = estrdup((char*)location->children->content);
  708. ctype = strrchr((char*)bindingAttr->children->content,':');
  709. if (ctype == NULL) {
  710. ctype = (char*)bindingAttr->children->content;
  711. } else {
  712. ++ctype;
  713. }
  714. if (zend_hash_find(&ctx.bindings, ctype, strlen(ctype)+1, (void*)&tmp) != SUCCESS) {
  715. soap_error1(E_ERROR, "Parsing WSDL: No <binding> element with name '%s'", ctype);
  716. }
  717. binding = *tmp;
  718. if (tmpbinding->bindingType == BINDING_SOAP) {
  719. sdlSoapBindingPtr soapBinding;
  720. xmlNodePtr soapBindingNode;
  721. xmlAttrPtr tmp;
  722. soapBinding = emalloc(sizeof(sdlSoapBinding));
  723. memset(soapBinding, 0, sizeof(sdlSoapBinding));
  724. soapBinding->style = SOAP_DOCUMENT;
  725. soapBindingNode = get_node_ex(binding->children, "binding", wsdl_soap_namespace);
  726. if (soapBindingNode) {
  727. tmp = get_attribute(soapBindingNode->properties, "style");
  728. if (tmp && !strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  729. soapBinding->style = SOAP_RPC;
  730. }
  731. tmp = get_attribute(soapBindingNode->properties, "transport");
  732. if (tmp) {
  733. if (strncmp((char*)tmp->children->content, WSDL_HTTP_TRANSPORT, sizeof(WSDL_HTTP_TRANSPORT)) == 0) {
  734. soapBinding->transport = SOAP_TRANSPORT_HTTP;
  735. } else {
  736. /* try the next binding */
  737. efree(soapBinding);
  738. efree(tmpbinding->location);
  739. efree(tmpbinding);
  740. trav = trav->next;
  741. continue;
  742. }
  743. }
  744. }
  745. tmpbinding->bindingAttributes = (void *)soapBinding;
  746. }
  747. name = get_attribute(binding->properties, "name");
  748. if (name == NULL) {
  749. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <binding>");
  750. }
  751. tmpbinding->name = estrdup((char*)name->children->content);
  752. type = get_attribute(binding->properties, "type");
  753. if (type == NULL) {
  754. soap_error0(E_ERROR, "Parsing WSDL: Missing 'type' attribute for <binding>");
  755. }
  756. ctype = strrchr((char*)type->children->content,':');
  757. if (ctype == NULL) {
  758. ctype = (char*)type->children->content;
  759. } else {
  760. ++ctype;
  761. }
  762. if (zend_hash_find(&ctx.portTypes, ctype, strlen(ctype)+1, (void**)&tmp) != SUCCESS) {
  763. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType> with name '%s'", name->children->content);
  764. }
  765. portType = *tmp;
  766. trav2 = binding->children;
  767. while (trav2 != NULL) {
  768. sdlFunctionPtr function;
  769. xmlNodePtr input, output, fault, portTypeOperation, trav3;
  770. xmlAttrPtr op_name, paramOrder;
  771. if ((tmpbinding->bindingType == BINDING_SOAP &&
  772. node_is_equal_ex(trav2, "binding", wsdl_soap_namespace)) ||
  773. !is_wsdl_element(trav2) ||
  774. node_is_equal(trav2,"documentation")) {
  775. trav2 = trav2->next;
  776. continue;
  777. }
  778. if (!node_is_equal(trav2,"operation")) {
  779. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
  780. }
  781. operation = trav2;
  782. op_name = get_attribute(operation->properties, "name");
  783. if (op_name == NULL) {
  784. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <operation>");
  785. }
  786. trav3 = operation->children;
  787. while (trav3 != NULL) {
  788. if (tmpbinding->bindingType == BINDING_SOAP &&
  789. node_is_equal_ex(trav3, "operation", wsdl_soap_namespace)) {
  790. } else if (is_wsdl_element(trav3) &&
  791. !node_is_equal(trav3,"input") &&
  792. !node_is_equal(trav3,"output") &&
  793. !node_is_equal(trav3,"fault") &&
  794. !node_is_equal(trav3,"documentation")) {
  795. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav3->name);
  796. }
  797. trav3 = trav3->next;
  798. }
  799. portTypeOperation = get_node_with_attribute_ex(portType->children, "operation", WSDL_NAMESPACE, "name", (char*)op_name->children->content, NULL);
  800. if (portTypeOperation == NULL) {
  801. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType>/<operation> with name '%s'", op_name->children->content);
  802. }
  803. function = emalloc(sizeof(sdlFunction));
  804. memset(function, 0, sizeof(sdlFunction));
  805. function->functionName = estrdup((char*)op_name->children->content);
  806. if (tmpbinding->bindingType == BINDING_SOAP) {
  807. sdlSoapBindingFunctionPtr soapFunctionBinding;
  808. sdlSoapBindingPtr soapBinding;
  809. xmlNodePtr soapOperation;
  810. xmlAttrPtr tmp;
  811. soapFunctionBinding = emalloc(sizeof(sdlSoapBindingFunction));
  812. memset(soapFunctionBinding, 0, sizeof(sdlSoapBindingFunction));
  813. soapBinding = (sdlSoapBindingPtr)tmpbinding->bindingAttributes;
  814. soapFunctionBinding->style = soapBinding->style;
  815. soapOperation = get_node_ex(operation->children, "operation", wsdl_soap_namespace);
  816. if (soapOperation) {
  817. tmp = get_attribute(soapOperation->properties, "soapAction");
  818. if (tmp) {
  819. soapFunctionBinding->soapAction = estrdup((char*)tmp->children->content);
  820. }
  821. tmp = get_attribute(soapOperation->properties, "style");
  822. if (tmp) {
  823. if (!strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  824. soapFunctionBinding->style = SOAP_RPC;
  825. } else {
  826. soapFunctionBinding->style = SOAP_DOCUMENT;
  827. }
  828. } else {
  829. soapFunctionBinding->style = soapBinding->style;
  830. }
  831. }
  832. function->bindingAttributes = (void *)soapFunctionBinding;
  833. }
  834. input = get_node_ex(portTypeOperation->children, "input", WSDL_NAMESPACE);
  835. if (input != NULL) {
  836. xmlAttrPtr message, name;
  837. message = get_attribute(input->properties, "message");
  838. if (message == NULL) {
  839. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <input> of '%s'", op_name->children->content);
  840. }
  841. function->requestParameters = wsdl_message(&ctx, message->children->content);
  842. name = get_attribute(input->properties, "name");
  843. /* FIXME
  844. if (name != NULL) {
  845. function->requestName = estrdup(name->children->content);
  846. } else {
  847. */
  848. {
  849. function->requestName = estrdup(function->functionName);
  850. }
  851. if (tmpbinding->bindingType == BINDING_SOAP) {
  852. input = get_node_ex(operation->children, "input", WSDL_NAMESPACE);
  853. if (input != NULL) {
  854. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  855. wsdl_soap_binding_body(&ctx, input, wsdl_soap_namespace, &soapFunctionBinding->input, function->requestParameters);
  856. }
  857. }
  858. }
  859. output = get_node_ex(portTypeOperation->children, "output", WSDL_NAMESPACE);
  860. if (output != NULL) {
  861. xmlAttrPtr message, name;
  862. message = get_attribute(output->properties, "message");
  863. if (message == NULL) {
  864. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  865. }
  866. function->responseParameters = wsdl_message(&ctx, message->children->content);
  867. name = get_attribute(output->properties, "name");
  868. /* FIXME
  869. if (name != NULL) {
  870. function->responseName = estrdup(name->children->content);
  871. } else if (input == NULL) {
  872. function->responseName = estrdup(function->functionName);
  873. } else {
  874. */
  875. {
  876. int len = strlen(function->functionName);
  877. function->responseName = emalloc(len + sizeof("Response"));
  878. memcpy(function->responseName, function->functionName, len);
  879. memcpy(function->responseName+len, "Response", sizeof("Response"));
  880. }
  881. if (tmpbinding->bindingType == BINDING_SOAP) {
  882. output = get_node_ex(operation->children, "output", WSDL_NAMESPACE);
  883. if (output != NULL) {
  884. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  885. wsdl_soap_binding_body(&ctx, output, wsdl_soap_namespace, &soapFunctionBinding->output, function->responseParameters);
  886. }
  887. }
  888. }
  889. paramOrder = get_attribute(portTypeOperation->properties, "parameterOrder");
  890. if (paramOrder) {
  891. /* FIXME: */
  892. }
  893. fault = portTypeOperation->children;
  894. while (fault != NULL) {
  895. if (node_is_equal_ex(fault, "fault", WSDL_NAMESPACE)) {
  896. xmlAttrPtr message, name;
  897. sdlFaultPtr f;
  898. name = get_attribute(fault->properties, "name");
  899. if (name == NULL) {
  900. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <fault> of '%s'", op_name->children->content);
  901. }
  902. message = get_attribute(fault->properties, "message");
  903. if (message == NULL) {
  904. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  905. }
  906. f = emalloc(sizeof(sdlFault));
  907. memset(f, 0, sizeof(sdlFault));
  908. f->name = estrdup((char*)name->children->content);
  909. f->details = wsdl_message(&ctx, message->children->content);
  910. if (f->details == NULL || zend_hash_num_elements(f->details) > 1) {
  911. soap_error1(E_ERROR, "Parsing WSDL: The fault message '%s' must have a single part", message->children->content);
  912. }
  913. if (tmpbinding->bindingType == BINDING_SOAP) {
  914. xmlNodePtr soap_fault = get_node_with_attribute_ex(operation->children, "fault", WSDL_NAMESPACE, "name", f->name, NULL);
  915. if (soap_fault != NULL) {
  916. xmlNodePtr trav = soap_fault->children;
  917. while (trav != NULL) {
  918. if (node_is_equal_ex(trav, "fault", wsdl_soap_namespace)) {
  919. xmlAttrPtr tmp;
  920. sdlSoapBindingFunctionFaultPtr binding;
  921. binding = f->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  922. memset(f->bindingAttributes, 0, sizeof(sdlSoapBindingFunctionFault));
  923. tmp = get_attribute(trav->properties, "use");
  924. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  925. binding->use = SOAP_ENCODED;
  926. } else {
  927. binding->use = SOAP_LITERAL;
  928. }
  929. tmp = get_attribute(trav->properties, "namespace");
  930. if (tmp) {
  931. binding->ns = estrdup((char*)tmp->children->content);
  932. }
  933. if (binding->use == SOAP_ENCODED) {
  934. tmp = get_attribute(trav->properties, "encodingStyle");
  935. if (tmp) {
  936. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  937. binding->encodingStyle = SOAP_ENCODING_1_1;
  938. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  939. binding->encodingStyle = SOAP_ENCODING_1_2;
  940. } else {
  941. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  942. }
  943. } else {
  944. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  945. }
  946. }
  947. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  948. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  949. }
  950. trav = trav->next;
  951. }
  952. }
  953. }
  954. if (function->faults == NULL) {
  955. function->faults = emalloc(sizeof(HashTable));
  956. zend_hash_init(function->faults, 0, NULL, delete_fault, 0);
  957. }
  958. if (zend_hash_add(function->faults, f->name, strlen(f->name)+1, (void**)&f, sizeof(sdlFaultPtr), NULL) != SUCCESS) {
  959. soap_error2(E_ERROR, "Parsing WSDL: <fault> with name '%s' already defined in '%s'", f->name, op_name->children->content);
  960. }
  961. }
  962. fault = fault->next;
  963. }
  964. function->binding = tmpbinding;
  965. {
  966. char *tmp = estrdup(function->functionName);
  967. int len = strlen(tmp);
  968. if (zend_hash_add(&ctx.sdl->functions, php_strtolower(tmp, len), len+1, &function, sizeof(sdlFunctionPtr), NULL) != SUCCESS) {
  969. zend_hash_next_index_insert(&ctx.sdl->functions, &function, sizeof(sdlFunctionPtr), NULL);
  970. }
  971. efree(tmp);
  972. if (function->requestName != NULL && strcmp(function->requestName,function->functionName) != 0) {
  973. if (ctx.sdl->requests == NULL) {
  974. ctx.sdl->requests = emalloc(sizeof(HashTable));
  975. zend_hash_init(ctx.sdl->requests, 0, NULL, NULL, 0);
  976. }
  977. tmp = estrdup(function->requestName);
  978. len = strlen(tmp);
  979. zend_hash_add(ctx.sdl->requests, php_strtolower(tmp, len), len+1, &function, sizeof(sdlFunctionPtr), NULL);
  980. efree(tmp);
  981. }
  982. }
  983. trav2 = trav2->next;
  984. }
  985. if (!ctx.sdl->bindings) {
  986. ctx.sdl->bindings = emalloc(sizeof(HashTable));
  987. zend_hash_init(ctx.sdl->bindings, 0, NULL, delete_binding, 0);
  988. }
  989. zend_hash_add(ctx.sdl->bindings, tmpbinding->name, strlen(tmpbinding->name), &tmpbinding, sizeof(sdlBindingPtr), NULL);
  990. trav= trav->next;
  991. }
  992. zend_hash_move_forward(&ctx.services);
  993. }
  994. } else {
  995. soap_error0(E_ERROR, "Parsing WSDL: Couldn't bind to service");
  996. }
  997. if (ctx.sdl->bindings == NULL || ctx.sdl->bindings->nNumOfElements == 0) {
  998. soap_error0(E_ERROR, "Parsing WSDL: Could not find any usable binding services in WSDL.");
  999. }
  1000. zend_hash_destroy(&ctx.messages);
  1001. zend_hash_destroy(&ctx.bindings);
  1002. zend_hash_destroy(&ctx.portTypes);
  1003. zend_hash_destroy(&ctx.services);
  1004. zend_hash_destroy(&ctx.docs);
  1005. return ctx.sdl;
  1006. }
  1007. #define WSDL_CACHE_VERSION 0x0e
  1008. #define WSDL_CACHE_GET(ret,type,buf) memcpy(&ret,*buf,sizeof(type)); *buf += sizeof(type);
  1009. #define WSDL_CACHE_GET_INT(ret,buf) ret = ((unsigned char)(*buf)[0])|((unsigned char)(*buf)[1]<<8)|((unsigned char)(*buf)[2]<<16)|((int)(*buf)[3]<<24); *buf += 4;
  1010. #define WSDL_CACHE_GET_1(ret,type,buf) ret = (type)(**buf); (*buf)++;
  1011. #define WSDL_CACHE_GET_N(ret,n,buf) memcpy(ret,*buf,n); *buf += n;
  1012. #define WSDL_CACHE_SKIP(n,buf) *buf += n;
  1013. #define WSDL_CACHE_PUT_INT(val,buf) smart_str_appendc(buf,val & 0xff); \
  1014. smart_str_appendc(buf,(val >> 8) & 0xff); \
  1015. smart_str_appendc(buf,(val >> 16) & 0xff); \
  1016. smart_str_appendc(buf,(val >> 24) & 0xff);
  1017. #define WSDL_CACHE_PUT_1(val,buf) smart_str_appendc(buf,val);
  1018. #define WSDL_CACHE_PUT_N(val,n,buf) smart_str_appendl(buf,(char*)val,n);
  1019. static char* sdl_deserialize_string(char **in)
  1020. {
  1021. char *s;
  1022. int len;
  1023. WSDL_CACHE_GET_INT(len, in);
  1024. if (len == 0x7fffffff) {
  1025. return NULL;
  1026. } else {
  1027. s = emalloc(len+1);
  1028. WSDL_CACHE_GET_N(s, len, in);
  1029. s[len] = '\0';
  1030. return s;
  1031. }
  1032. }
  1033. static void sdl_deserialize_key(HashTable* ht, void* data, char **in)
  1034. {
  1035. int len;
  1036. WSDL_CACHE_GET_INT(len, in);
  1037. if (len == 0) {
  1038. zend_hash_next_index_insert(ht, &data, sizeof(void*), NULL);
  1039. } else {
  1040. zend_hash_add(ht, *in, len, &data, sizeof(void*), NULL);
  1041. WSDL_CACHE_SKIP(len, in);
  1042. }
  1043. }
  1044. static void sdl_deserialize_attribute(sdlAttributePtr attr, encodePtr *encoders, char **in)
  1045. {
  1046. int i;
  1047. attr->name = sdl_deserialize_string(in);
  1048. attr->namens = sdl_deserialize_string(in);
  1049. attr->ref = sdl_deserialize_string(in);
  1050. attr->def = sdl_deserialize_string(in);
  1051. attr->fixed = sdl_deserialize_string(in);
  1052. WSDL_CACHE_GET_1(attr->form, sdlForm, in);
  1053. WSDL_CACHE_GET_1(attr->use, sdlUse, in);
  1054. WSDL_CACHE_GET_INT(i, in);
  1055. attr->encode = encoders[i];
  1056. WSDL_CACHE_GET_INT(i, in);
  1057. if (i > 0) {
  1058. attr->extraAttributes = emalloc(sizeof(HashTable));
  1059. zend_hash_init(attr->extraAttributes, i, NULL, delete_extra_attribute, 0);
  1060. while (i > 0) {
  1061. sdlExtraAttributePtr x = emalloc(sizeof(sdlExtraAttribute));
  1062. sdl_deserialize_key(attr->extraAttributes, x, in);
  1063. x->ns = sdl_deserialize_string(in);
  1064. x->val = sdl_deserialize_string(in);
  1065. --i;
  1066. }
  1067. }
  1068. }
  1069. static sdlRestrictionIntPtr sdl_deserialize_resriction_int(char **in)
  1070. {
  1071. if (**in == 1) {
  1072. sdlRestrictionIntPtr x = emalloc(sizeof(sdlRestrictionInt));
  1073. WSDL_CACHE_SKIP(1, in);
  1074. WSDL_CACHE_GET_INT(x->value, in);
  1075. WSDL_CACHE_GET_1(x->fixed, char, in);
  1076. return x;
  1077. } else {
  1078. WSDL_CACHE_SKIP(1, in);
  1079. return NULL;
  1080. }
  1081. }
  1082. static sdlRestrictionCharPtr sdl_deserialize_resriction_char(char **in)
  1083. {
  1084. if (**in == 1) {
  1085. sdlRestrictionCharPtr x = emalloc(sizeof(sdlRestrictionChar));
  1086. WSDL_CACHE_SKIP(1, in);
  1087. x->value = sdl_deserialize_string(in);
  1088. WSDL_CACHE_GET_1(x->fixed, char, in);
  1089. return x;
  1090. } else {
  1091. WSDL_CACHE_SKIP(1, in);
  1092. return NULL;
  1093. }
  1094. }
  1095. static sdlContentModelPtr sdl_deserialize_model(sdlTypePtr *types, sdlTypePtr *elements, char **in)
  1096. {
  1097. int i;
  1098. sdlContentModelPtr model = emalloc(sizeof(sdlContentModel));
  1099. WSDL_CACHE_GET_1(model->kind, sdlContentKind, in);
  1100. WSDL_CACHE_GET_INT(model->min_occurs, in);
  1101. WSDL_CACHE_GET_INT(model->max_occurs, in);
  1102. switch (model->kind) {
  1103. case XSD_CONTENT_ELEMENT:
  1104. WSDL_CACHE_GET_INT(i, in);
  1105. model->u.element = elements[i];
  1106. break;
  1107. case XSD_CONTENT_SEQUENCE:
  1108. case XSD_CONTENT_ALL:
  1109. case XSD_CONTENT_CHOICE:
  1110. WSDL_CACHE_GET_INT(i, in);
  1111. model->u.content = emalloc(sizeof(HashTable));
  1112. zend_hash_init(model->u.content, i, NULL, delete_model, 0);
  1113. while (i > 0) {
  1114. sdlContentModelPtr x = sdl_deserialize_model(types, elements, in);
  1115. zend_hash_next_index_insert(model->u.content,&x,sizeof(sdlContentModelPtr),NULL);
  1116. i--;
  1117. }
  1118. break;
  1119. case XSD_CONTENT_GROUP_REF:
  1120. model->u.group_ref = sdl_deserialize_string(in);
  1121. break;
  1122. case XSD_CONTENT_GROUP:
  1123. WSDL_CACHE_GET_INT(i, in);
  1124. model->u.group = types[i];
  1125. break;
  1126. default:
  1127. break;
  1128. }
  1129. return model;
  1130. }
  1131. static void sdl_deserialize_type(sdlTypePtr type, sdlTypePtr *types, encodePtr *encoders, char **in)
  1132. {
  1133. int i;
  1134. sdlTypePtr *elements = NULL;
  1135. WSDL_CACHE_GET_1(type->kind, sdlTypeKind, in);
  1136. type->name = sdl_deserialize_string(in);
  1137. type->namens = sdl_deserialize_string(in);
  1138. type->def = sdl_deserialize_string(in);
  1139. type->fixed = sdl_deserialize_string(in);
  1140. type->ref = sdl_deserialize_string(in);
  1141. WSDL_CACHE_GET_1(type->nillable, char, in);
  1142. WSDL_CACHE_GET_1(type->form, sdlForm, in);
  1143. WSDL_CACHE_GET_INT(i, in);
  1144. type->encode = encoders[i];
  1145. if (**in == 1) {
  1146. WSDL_CACHE_SKIP(1, in);
  1147. type->restrictions = emalloc(sizeof(sdlRestrictions));
  1148. /*memset(type->restrictions, 0, sizeof(sdlRestrictions));*/
  1149. type->restrictions->minExclusive = sdl_deserialize_resriction_int(in);
  1150. type->restrictions->minInclusive = sdl_deserialize_resriction_int(in);
  1151. type->restrictions->maxExclusive = sdl_deserialize_resriction_int(in);
  1152. type->restrictions->maxInclusive = sdl_deserialize_resriction_int(in);
  1153. type->restrictions->totalDigits = sdl_deserialize_resriction_int(in);
  1154. type->restrictions->fractionDigits = sdl_deserialize_resriction_int(in);
  1155. type->restrictions->length = sdl_deserialize_resriction_int(in);
  1156. type->restrictions->minLength = sdl_deserialize_resriction_int(in);
  1157. type->restrictions->maxLength = sdl_deserialize_resriction_int(in);
  1158. type->restrictions->whiteSpace = sdl_deserialize_resriction_char(in);
  1159. type->restrictions->pattern = sdl_deserialize_resriction_char(in);
  1160. WSDL_CACHE_GET_INT(i, in);
  1161. if (i > 0) {
  1162. type->restrictions->enumeration = emalloc(sizeof(HashTable));
  1163. zend_hash_init(type->restrictions->enumeration, i, NULL, delete_restriction_var_char, 0);
  1164. while (i > 0) {
  1165. sdlRestrictionCharPtr x = sdl_deserialize_resriction_char(in);
  1166. sdl_deserialize_key(type->restrictions->enumeration, x, in);
  1167. --i;
  1168. }
  1169. } else {
  1170. type->restrictions->enumeration = NULL;
  1171. }
  1172. } else {
  1173. WSDL_CACHE_SKIP(1, in);
  1174. }
  1175. WSDL_CACHE_GET_INT(i, in);
  1176. if (i > 0) {
  1177. elements = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1178. elements[0] = NULL;
  1179. type->elements = emalloc(sizeof(HashTable));
  1180. zend_hash_init(type->elements, i, NULL, delete_type, 0);
  1181. while (i > 0) {
  1182. sdlTypePtr t = emalloc(sizeof(sdlType));
  1183. memset(t, 0, sizeof(sdlType));
  1184. sdl_deserialize_key(type->elements, t, in);
  1185. sdl_deserialize_type(t, types, encoders, in);
  1186. elements[i] = t;
  1187. --i;
  1188. }
  1189. }
  1190. WSDL_CACHE_GET_INT(i, in);
  1191. if (i > 0) {
  1192. type->attributes = emalloc(sizeof(HashTable));
  1193. zend_hash_init(type->attributes, i, NULL, delete_attribute, 0);
  1194. while (i > 0) {
  1195. sdlAttributePtr attr = emalloc(sizeof(sdlAttribute));
  1196. memset(attr, 0, sizeof(sdlAttribute));
  1197. sdl_deserialize_key(type->attributes, attr, in);
  1198. sdl_deserialize_attribute(attr, encoders, in);
  1199. --i;
  1200. }
  1201. }
  1202. if (**in != 0) {
  1203. WSDL_CACHE_SKIP(1, in);
  1204. type->model = sdl_deserialize_model(types, elements, in);
  1205. } else {
  1206. WSDL_CACHE_SKIP(1, in);
  1207. }
  1208. if (elements != NULL) {
  1209. efree(elements);
  1210. }
  1211. }
  1212. static void sdl_deserialize_encoder(encodePtr enc, sdlTypePtr *types, char **in)
  1213. {
  1214. int i;
  1215. WSDL_CACHE_GET_INT(enc->details.type, in);
  1216. enc->details.type_str = sdl_deserialize_string(in);
  1217. enc->details.ns = sdl_deserialize_string(in);
  1218. WSDL_CACHE_GET_INT(i, in);
  1219. enc->details.sdl_type = types[i];
  1220. enc->to_xml = sdl_guess_convert_xml;
  1221. enc->to_zval = sdl_guess_convert_zval;
  1222. if (enc->details.sdl_type == NULL) {
  1223. int ns_len = strlen(enc->details.ns);
  1224. int type_len = strlen(enc->details.type_str);
  1225. if (((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  1226. memcmp(enc->details.ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  1227. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  1228. memcmp(enc->details.ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  1229. char *enc_nscat;
  1230. int enc_ns_len;
  1231. int enc_len;
  1232. encodePtr real_enc;
  1233. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  1234. enc_len = enc_ns_len + type_len + 1;
  1235. enc_nscat = emalloc(enc_len + 1);
  1236. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  1237. enc_nscat[enc_ns_len] = ':';
  1238. memcpy(enc_nscat+enc_ns_len+1, enc->details.type_str, type_len);
  1239. enc_nscat[enc_len] = '\0';
  1240. real_enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  1241. efree(enc_nscat);
  1242. if (real_enc) {
  1243. enc->to_zval = real_enc->to_zval;
  1244. enc->to_xml = real_enc->to_xml;
  1245. }
  1246. }
  1247. }
  1248. }
  1249. static void sdl_deserialize_soap_body(sdlSoapBindingFunctionBodyPtr body, encodePtr *encoders, sdlTypePtr *types, char **in)
  1250. {
  1251. int i, j, n;
  1252. WSDL_CACHE_GET_1(body->use, sdlEncodingUse, in);
  1253. if (body->use == SOAP_ENCODED) {
  1254. WSDL_CACHE_GET_1(body->encodingStyle, sdlRpcEncodingStyle, in);
  1255. } else {
  1256. body->encodingStyle = SOAP_ENCODING_DEFAULT;
  1257. }
  1258. body->ns = sdl_deserialize_string(in);
  1259. WSDL_CACHE_GET_INT(i, in);
  1260. if (i > 0) {
  1261. body->headers = emalloc(sizeof(HashTable));
  1262. zend_hash_init(body->headers, i, NULL, delete_header, 0);
  1263. while (i > 0) {
  1264. sdlSoapBindingFunctionHeaderPtr tmp = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1265. memset(tmp, 0, sizeof(sdlSoapBindingFunctionHeader));
  1266. sdl_deserialize_key(body->headers, tmp, in);
  1267. WSDL_CACHE_GET_1(tmp->use, sdlEncodingUse, in);
  1268. if (tmp->use == SOAP_ENCODED) {
  1269. WSDL_CACHE_GET_1(tmp->encodingStyle, sdlRpcEncodingStyle, in);
  1270. } else {
  1271. tmp->encodingStyle = SOAP_ENCODING_DEFAULT;
  1272. }
  1273. tmp->name = sdl_deserialize_string(in);
  1274. tmp->ns = sdl_deserialize_string(in);
  1275. WSDL_CACHE_GET_INT(n, in);
  1276. tmp->encode = encoders[n];
  1277. WSDL_CACHE_GET_INT(n, in);
  1278. tmp->element = types[n];
  1279. --i;
  1280. WSDL_CACHE_GET_INT(j, in);
  1281. if (j > 0) {
  1282. tmp->headerfaults = emalloc(sizeof(HashTable));
  1283. zend_hash_init(tmp->headerfaults, i, NULL, delete_header, 0);
  1284. while (j > 0) {
  1285. sdlSoapBindingFunctionHeaderPtr tmp2 = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1286. memset(tmp2, 0, sizeof(sdlSoapBindingFunctionHeader));
  1287. sdl_deserialize_key(tmp->headerfaults, tmp2, in);
  1288. WSDL_CACHE_GET_1(tmp2->use, sdlEncodingUse, in);
  1289. if (tmp2->use == SOAP_ENCODED) {
  1290. WSDL_CACHE_GET_1(tmp2->encodingStyle, sdlRpcEncodingStyle, in);
  1291. } else {
  1292. tmp2->encodingStyle = SOAP_ENCODING_DEFAULT;
  1293. }
  1294. tmp2->name = sdl_deserialize_string(in);
  1295. tmp2->ns = sdl_deserialize_string(in);
  1296. WSDL_CACHE_GET_INT(n, in);
  1297. tmp2->encode = encoders[n];
  1298. WSDL_CACHE_GET_INT(n, in);
  1299. tmp2->element = types[n];
  1300. --j;
  1301. }
  1302. }
  1303. }
  1304. }
  1305. }
  1306. static HashTable* sdl_deserialize_parameters(encodePtr *encoders, sdlTypePtr *types, char **in)
  1307. {
  1308. int i, n;
  1309. HashTable *ht;
  1310. WSDL_CACHE_GET_INT(i, in);
  1311. if (i == 0) {return NULL;}
  1312. ht = emalloc(sizeof(HashTable));
  1313. zend_hash_init(ht, i, NULL, delete_parameter, 0);
  1314. while (i > 0) {
  1315. sdlParamPtr param = emalloc(sizeof(sdlParam));
  1316. sdl_deserialize_key(ht, param, in);
  1317. param->paramName = sdl_deserialize_string(in);
  1318. WSDL_CACHE_GET_INT(param->order, in);
  1319. WSDL_CACHE_GET_INT(n, in);
  1320. param->encode = encoders[n];
  1321. WSDL_CACHE_GET_INT(n, in);
  1322. param->element = types[n];
  1323. --i;
  1324. }
  1325. return ht;
  1326. }
  1327. static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, time_t t, time_t *cached TSRMLS_DC)
  1328. {
  1329. sdlPtr sdl;
  1330. time_t old_t;
  1331. int i, num_groups, num_types, num_elements, num_encoders, num_bindings, num_func;
  1332. sdlFunctionPtr *functions = NULL;
  1333. sdlBindingPtr *bindings;
  1334. sdlTypePtr *types;
  1335. encodePtr *encoders;
  1336. encodePtr enc;
  1337. int f;
  1338. struct stat st;
  1339. char *in, *buf;
  1340. f = open(fn, O_RDONLY|O_BINARY);
  1341. if (f < 0) {
  1342. return NULL;
  1343. }
  1344. if (fstat(f, &st) != 0) {
  1345. close(f);
  1346. return NULL;
  1347. }
  1348. buf = in = emalloc(st.st_size);
  1349. if (read(f, in, st.st_size) != st.st_size) {
  1350. close(f);
  1351. efree(in);
  1352. return NULL;
  1353. }
  1354. close(f);
  1355. if (strncmp(in,"wsdl",4) != 0 || in[4] != WSDL_CACHE_VERSION || in[5] != '\0') {
  1356. unlink(fn);
  1357. efree(buf);
  1358. return NULL;
  1359. }
  1360. in += 6;
  1361. WSDL_CACHE_GET(old_t, time_t, &in);
  1362. if (old_t < t) {
  1363. unlink(fn);
  1364. efree(buf);
  1365. return NULL;
  1366. }
  1367. *cached = old_t;
  1368. WSDL_CACHE_GET_INT(i, &in);
  1369. if (i == 0 && strncmp(in, uri, i) != 0) {
  1370. unlink(fn);
  1371. efree(buf);
  1372. return NULL;
  1373. }
  1374. WSDL_CACHE_SKIP(i, &in);
  1375. sdl = emalloc(sizeof(*sdl));
  1376. memset(sdl, 0, sizeof(*sdl));
  1377. sdl->source = sdl_deserialize_string(&in);
  1378. sdl->target_ns = sdl_deserialize_string(&in);
  1379. WSDL_CACHE_GET_INT(num_groups, &in);
  1380. WSDL_CACHE_GET_INT(num_types, &in);
  1381. WSDL_CACHE_GET_INT(num_elements, &in);
  1382. WSDL_CACHE_GET_INT(num_encoders, &in);
  1383. i = num_groups+num_types+num_elements;
  1384. types = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1385. types[0] = NULL;
  1386. while (i > 0) {
  1387. types[i] = emalloc(sizeof(sdlType));
  1388. memset(types[i], 0, sizeof(sdlType));
  1389. i--;
  1390. }
  1391. i = num_encoders;
  1392. enc = defaultEncoding;
  1393. while (enc->details.type != END_KNOWN_TYPES) {
  1394. i++; enc++;
  1395. }
  1396. encoders = safe_emalloc((i+1), sizeof(encodePtr), 0);
  1397. i = num_encoders;
  1398. encoders[0] = NULL;
  1399. while (i > 0) {
  1400. encoders[i] = emalloc(sizeof(encode));
  1401. memset(encoders[i], 0, sizeof(encode));
  1402. i--;
  1403. }
  1404. i = num_encoders;
  1405. enc = defaultEncoding;
  1406. while (enc->details.type != END_KNOWN_TYPES) {
  1407. encoders[++i] = enc++;
  1408. }
  1409. i = 1;
  1410. if (num_groups > 0) {
  1411. sdl->groups = emalloc(sizeof(HashTable));
  1412. zend_hash_init(sdl->groups, num_groups, NULL, delete_type, 0);
  1413. while (i < num_groups+1) {
  1414. sdl_deserialize_key(sdl->groups, types[i], &in);
  1415. sdl_deserialize_type(types[i], types, encoders, &in);
  1416. i++;
  1417. }
  1418. }
  1419. if (num_types > 0) {
  1420. sdl->types = emalloc(sizeof(HashTable));
  1421. zend_hash_init(sdl->types, num_types, NULL, delete_type, 0);
  1422. while (i < num_groups+num_types+1) {
  1423. sdl_deserialize_key(sdl->types, types[i], &in);
  1424. sdl_deserialize_type(types[i], types, encoders, &in);
  1425. i++;
  1426. }
  1427. }
  1428. if (num_elements > 0) {
  1429. sdl->elements = emalloc(sizeof(HashTable));
  1430. zend_hash_init(sdl->elements, num_elements, NULL, delete_type, 0);
  1431. while (i < num_groups+num_types+num_elements+1) {
  1432. sdl_deserialize_key(sdl->elements, types[i], &in);
  1433. sdl_deserialize_type(types[i], types, encoders, &in);
  1434. i++;
  1435. }
  1436. }
  1437. i = 1;
  1438. if (num_encoders > 0) {
  1439. sdl->encoders = emalloc(sizeof(HashTable));
  1440. zend_hash_init(sdl->encoders, num_encoders, NULL, delete_encoder, 0);
  1441. while (i < num_encoders+1) {
  1442. sdl_deserialize_key(sdl->encoders, encoders[i], &in);
  1443. sdl_deserialize_encoder(encoders[i], types, &in);
  1444. i++;
  1445. }
  1446. }
  1447. /* deserialize bindings */
  1448. WSDL_CACHE_GET_INT(num_bindings, &in);
  1449. bindings = safe_emalloc(num_bindings, sizeof(sdlBindingPtr), 0);
  1450. if (num_bindings > 0) {
  1451. sdl->bindings = emalloc(sizeof(HashTable));
  1452. zend_hash_init(sdl->bindings, num_bindings, NULL, delete_binding, 0);
  1453. for (i = 0; i < num_bindings; i++) {
  1454. sdlBindingPtr binding = emalloc(sizeof(sdlBinding));
  1455. memset(binding, 0, sizeof(sdlBinding));
  1456. sdl_deserialize_key(sdl->bindings, binding, &in);
  1457. binding->name = sdl_deserialize_string(&in);
  1458. binding->location = sdl_deserialize_string(&in);
  1459. WSDL_CACHE_GET_1(binding->bindingType,sdlBindingType,&in);
  1460. if (binding->bindingType == BINDING_SOAP && *in != 0) {
  1461. sdlSoapBindingPtr soap_binding = binding->bindingAttributes = emalloc(sizeof(sdlSoapBinding));
  1462. WSDL_CACHE_GET_1(soap_binding->style,sdlEncodingStyle,&in);
  1463. WSDL_CACHE_GET_1(soap_binding->transport,sdlTransport,&in);
  1464. } else {
  1465. WSDL_CACHE_SKIP(1,&in);
  1466. }
  1467. bindings[i] = binding;
  1468. }
  1469. }
  1470. /* deserialize functions */
  1471. WSDL_CACHE_GET_INT(num_func, &in);
  1472. zend_hash_init(&sdl->functions, num_func, NULL, delete_function, 0);
  1473. if (num_func > 0) {
  1474. functions = safe_emalloc(num_func, sizeof(sdlFunctionPtr), 0);
  1475. for (i = 0; i < num_func; i++) {
  1476. int binding_num, num_faults;
  1477. sdlFunctionPtr func = emalloc(sizeof(sdlFunction));
  1478. sdl_deserialize_key(&sdl->functions, func, &in);
  1479. func->functionName = sdl_deserialize_string(&in);
  1480. func->requestName = sdl_deserialize_string(&in);
  1481. func->responseName = sdl_deserialize_string(&in);
  1482. WSDL_CACHE_GET_INT(binding_num, &in);
  1483. if (binding_num == 0) {
  1484. func->binding = NULL;
  1485. } else {
  1486. func->binding = bindings[binding_num-1];
  1487. }
  1488. if (func->binding && func->binding->bindingType == BINDING_SOAP && *in != 0) {
  1489. sdlSoapBindingFunctionPtr binding = func->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunction));
  1490. memset(binding, 0, sizeof(sdlSoapBindingFunction));
  1491. WSDL_CACHE_GET_1(binding->style,sdlEncodingStyle,&in);
  1492. binding->soapAction = sdl_deserialize_string(&in);
  1493. sdl_deserialize_soap_body(&binding->input, encoders, types, &in);
  1494. sdl_deserialize_soap_body(&binding->output, encoders, types, &in);
  1495. } else {
  1496. WSDL_CACHE_SKIP(1, &in);
  1497. func->bindingAttributes = NULL;
  1498. }
  1499. func->requestParameters = sdl_deserialize_parameters(encoders, types, &in);
  1500. func->responseParameters = sdl_deserialize_parameters(encoders, types, &in);
  1501. WSDL_CACHE_GET_INT(num_faults, &in);
  1502. if (num_faults > 0) {
  1503. int j;
  1504. func->faults = emalloc(sizeof(HashTable));
  1505. zend_hash_init(func->faults, num_faults, NULL, delete_fault, 0);
  1506. for (j = 0; j < num_faults; j++) {
  1507. sdlFaultPtr fault = emalloc(sizeof(sdlFault));
  1508. sdl_deserialize_key(func->faults, fault, &in);
  1509. fault->name =sdl_deserialize_string(&in);
  1510. fault->details =sdl_deserialize_parameters(encoders, types, &in);
  1511. if (*in != 0) {
  1512. sdlSoapBindingFunctionFaultPtr binding = fault->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  1513. memset(binding, 0, sizeof(sdlSoapBindingFunctionFault));
  1514. WSDL_CACHE_GET_1(binding->use,sdlEncodingUse,&in);
  1515. if (binding->use == SOAP_ENCODED) {
  1516. WSDL_CACHE_GET_1(binding->encodingStyle, sdlRpcEncodingStyle, &in);
  1517. } else {
  1518. binding->encodingStyle = SOAP_ENCODING_DEFAULT;
  1519. }
  1520. binding->ns = sdl_deserialize_string(&in);
  1521. } else {
  1522. WSDL_CACHE_SKIP(1, &in);
  1523. fault->bindingAttributes = NULL;
  1524. }
  1525. }
  1526. } else {
  1527. func->faults = NULL;
  1528. }
  1529. functions[i] = func;
  1530. }
  1531. }
  1532. /* deserialize requests */
  1533. WSDL_CACHE_GET_INT(i, &in);
  1534. if (i > 0) {
  1535. sdl->requests = emalloc(sizeof(HashTable));
  1536. zend_hash_init(sdl->requests, i, NULL, NULL, 0);
  1537. while (i > 0) {
  1538. int function_num;
  1539. WSDL_CACHE_GET_INT(function_num, &in);
  1540. sdl_deserialize_key(sdl->requests, functions[function_num-1], &in);
  1541. i--;
  1542. }
  1543. }
  1544. if (functions) {
  1545. efree(functions);
  1546. }
  1547. efree(bindings);
  1548. efree(encoders);
  1549. efree(types);
  1550. efree(buf);
  1551. return sdl;
  1552. }
  1553. static void sdl_serialize_string(const char *str, smart_str *out)
  1554. {
  1555. int i;
  1556. if (str) {
  1557. i = strlen(str);
  1558. WSDL_CACHE_PUT_INT(i, out);
  1559. if (i > 0) {
  1560. WSDL_CACHE_PUT_N(str, i, out);
  1561. }
  1562. } else {
  1563. WSDL_CACHE_PUT_INT(0x7fffffff, out);
  1564. }
  1565. }
  1566. static void sdl_serialize_key(HashTable *ht, smart_str *out)
  1567. {
  1568. char *key;
  1569. uint key_len;
  1570. ulong index;
  1571. if (zend_hash_get_current_key_ex(ht, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  1572. WSDL_CACHE_PUT_INT(key_len, out);
  1573. WSDL_CACHE_PUT_N(key, key_len, out);
  1574. } else {
  1575. WSDL_CACHE_PUT_INT(0, out);
  1576. }
  1577. }
  1578. static void sdl_serialize_encoder_ref(encodePtr enc, HashTable *tmp_encoders, smart_str *out) {
  1579. if (enc) {
  1580. int *encoder_num;
  1581. if (zend_hash_find(tmp_encoders, (char*)&enc, sizeof(enc), (void**)&encoder_num) == SUCCESS) {
  1582. WSDL_CACHE_PUT_INT(*encoder_num, out);
  1583. } else {
  1584. WSDL_CACHE_PUT_INT(0, out);
  1585. }
  1586. } else {
  1587. WSDL_CACHE_PUT_INT(0, out);
  1588. }
  1589. }
  1590. static void sdl_serialize_type_ref(sdlTypePtr type, HashTable *tmp_types, smart_str *out) {
  1591. if (type) {
  1592. int *type_num;
  1593. if (zend_hash_find(tmp_types, (char*)&type, sizeof(type), (void**)&type_num) == SUCCESS) {
  1594. WSDL_CACHE_PUT_INT(*type_num, out);
  1595. } else {
  1596. WSDL_CACHE_PUT_INT(0, out);
  1597. }
  1598. } else {
  1599. WSDL_CACHE_PUT_INT(0,out);
  1600. }
  1601. }
  1602. static void sdl_serialize_attribute(sdlAttributePtr attr, HashTable *tmp_encoders, smart_str *out)
  1603. {
  1604. int i;
  1605. sdl_serialize_string(attr->name, out);
  1606. sdl_serialize_string(attr->namens, out);
  1607. sdl_serialize_string(attr->ref, out);
  1608. sdl_serialize_string(attr->def, out);
  1609. sdl_serialize_string(attr->fixed, out);
  1610. WSDL_CACHE_PUT_1(attr->form, out);
  1611. WSDL_CACHE_PUT_1(attr->use, out);
  1612. sdl_serialize_encoder_ref(attr->encode, tmp_encoders, out);
  1613. if (attr->extraAttributes) {
  1614. i = zend_hash_num_elements(attr->extraAttributes);
  1615. } else {
  1616. i = 0;
  1617. }
  1618. WSDL_CACHE_PUT_INT(i, out);
  1619. if (i > 0) {
  1620. sdlExtraAttributePtr *tmp;
  1621. zend_hash_internal_pointer_reset(attr->extraAttributes);
  1622. while (zend_hash_get_current_data(attr->extraAttributes, (void**)&tmp) == SUCCESS) {
  1623. sdl_serialize_key(attr->extraAttributes, out);
  1624. sdl_serialize_string((*tmp)->ns, out);
  1625. sdl_serialize_string((*tmp)->val, out);
  1626. zend_hash_move_forward(attr->extraAttributes);
  1627. }
  1628. }
  1629. }
  1630. static void sdl_serialize_model(sdlContentModelPtr model, HashTable *tmp_types, HashTable *tmp_elements, smart_str *out)
  1631. {
  1632. WSDL_CACHE_PUT_1(model->kind, out);
  1633. WSDL_CACHE_PUT_INT(model->min_occurs, out);
  1634. WSDL_CACHE_PUT_INT(model->max_occurs, out);
  1635. switch (model->kind) {
  1636. case XSD_CONTENT_ELEMENT:
  1637. sdl_serialize_type_ref(model->u.element, tmp_elements, out);
  1638. break;
  1639. case XSD_CONTENT_SEQUENCE:
  1640. case XSD_CONTENT_ALL:
  1641. case XSD_CONTENT_CHOICE: {
  1642. sdlContentModelPtr *tmp;
  1643. int i = zend_hash_num_elements(model->u.content);
  1644. WSDL_CACHE_PUT_INT(i, out);
  1645. zend_hash_internal_pointer_reset(model->u.content);
  1646. while (zend_hash_get_current_data(model->u.content, (void**)&tmp) == SUCCESS) {
  1647. sdl_serialize_model(*tmp, tmp_types, tmp_elements, out);
  1648. zend_hash_move_forward(model->u.content);
  1649. }
  1650. }
  1651. break;
  1652. case XSD_CONTENT_GROUP_REF:
  1653. sdl_serialize_string(model->u.group_ref,out);
  1654. break;
  1655. case XSD_CONTENT_GROUP:
  1656. sdl_serialize_type_ref(model->u.group, tmp_types, out);
  1657. break;
  1658. default:
  1659. break;
  1660. }
  1661. }
  1662. static void sdl_serialize_resriction_int(sdlRestrictionIntPtr x, smart_str *out)
  1663. {
  1664. if (x) {
  1665. WSDL_CACHE_PUT_1(1, out);
  1666. WSDL_CACHE_PUT_INT(x->value, out);
  1667. WSDL_CACHE_PUT_1(x->fixed, out);
  1668. } else {
  1669. WSDL_CACHE_PUT_1(0, out);
  1670. }
  1671. }
  1672. static void sdl_serialize_resriction_char(sdlRestrictionCharPtr x, smart_str *out)
  1673. {
  1674. if (x) {
  1675. WSDL_CACHE_PUT_1(1, out);
  1676. sdl_serialize_string(x->value, out);
  1677. WSDL_CACHE_PUT_1(x->fixed, out);
  1678. } else {
  1679. WSDL_CACHE_PUT_1(0, out);
  1680. }
  1681. }
  1682. static void sdl_serialize_type(sdlTypePtr type, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1683. {
  1684. int i;
  1685. HashTable *tmp_elements = NULL;
  1686. WSDL_CACHE_PUT_1(type->kind, out);
  1687. sdl_serialize_string(type->name, out);
  1688. sdl_serialize_string(type->namens, out);
  1689. sdl_serialize_string(type->def, out);
  1690. sdl_serialize_string(type->fixed, out);
  1691. sdl_serialize_string(type->ref, out);
  1692. WSDL_CACHE_PUT_1(type->nillable, out);
  1693. WSDL_CACHE_PUT_1(type->form, out);
  1694. sdl_serialize_encoder_ref(type->encode, tmp_encoders, out);
  1695. if (type->restrictions) {
  1696. WSDL_CACHE_PUT_1(1, out);
  1697. sdl_serialize_resriction_int(type->restrictions->minExclusive,out);
  1698. sdl_serialize_resriction_int(type->restrictions->minInclusive,out);
  1699. sdl_serialize_resriction_int(type->restrictions->maxExclusive,out);
  1700. sdl_serialize_resriction_int(type->restrictions->maxInclusive,out);
  1701. sdl_serialize_resriction_int(type->restrictions->totalDigits,out);
  1702. sdl_serialize_resriction_int(type->restrictions->fractionDigits,out);
  1703. sdl_serialize_resriction_int(type->restrictions->length,out);
  1704. sdl_serialize_resriction_int(type->restrictions->minLength,out);
  1705. sdl_serialize_resriction_int(type->restrictions->maxLength,out);
  1706. sdl_serialize_resriction_char(type->restrictions->whiteSpace,out);
  1707. sdl_serialize_resriction_char(type->restrictions->pattern,out);
  1708. if (type->restrictions->enumeration) {
  1709. i = zend_hash_num_elements(type->restrictions->enumeration);
  1710. } else {
  1711. i = 0;
  1712. }
  1713. WSDL_CACHE_PUT_INT(i, out);
  1714. if (i > 0) {
  1715. sdlRestrictionCharPtr *tmp;
  1716. zend_hash_internal_pointer_reset(type->restrictions->enumeration);
  1717. while (zend_hash_get_current_data(type->restrictions->enumeration, (void**)&tmp) == SUCCESS) {
  1718. sdl_serialize_resriction_char(*tmp, out);
  1719. sdl_serialize_key(type->restrictions->enumeration, out);
  1720. zend_hash_move_forward(type->restrictions->enumeration);
  1721. }
  1722. }
  1723. } else {
  1724. WSDL_CACHE_PUT_1(0, out);
  1725. }
  1726. if (type->elements) {
  1727. i = zend_hash_num_elements(type->elements);
  1728. } else {
  1729. i = 0;
  1730. }
  1731. WSDL_CACHE_PUT_INT(i, out);
  1732. if (i > 0) {
  1733. sdlTypePtr *tmp;
  1734. tmp_elements = emalloc(sizeof(HashTable));
  1735. zend_hash_init(tmp_elements, i, NULL, NULL, 0);
  1736. zend_hash_internal_pointer_reset(type->elements);
  1737. while (zend_hash_get_current_data(type->elements, (void**)&tmp) == SUCCESS) {
  1738. sdl_serialize_key(type->elements, out);
  1739. sdl_serialize_type(*tmp, tmp_encoders, tmp_types, out);
  1740. zend_hash_add(tmp_elements, (char*)tmp, sizeof(*tmp), &i, sizeof(int), NULL);
  1741. i--;
  1742. zend_hash_move_forward(type->elements);
  1743. }
  1744. }
  1745. if (type->attributes) {
  1746. i = zend_hash_num_elements(type->attributes);
  1747. } else {
  1748. i = 0;
  1749. }
  1750. WSDL_CACHE_PUT_INT(i, out);
  1751. if (i > 0) {
  1752. sdlAttributePtr *tmp;
  1753. zend_hash_internal_pointer_reset(type->attributes);
  1754. while (zend_hash_get_current_data(type->attributes, (void**)&tmp) == SUCCESS) {
  1755. sdl_serialize_key(type->attributes, out);
  1756. sdl_serialize_attribute(*tmp, tmp_encoders, out);
  1757. zend_hash_move_forward(type->attributes);
  1758. }
  1759. }
  1760. if (type->model) {
  1761. WSDL_CACHE_PUT_1(1, out);
  1762. sdl_serialize_model(type->model, tmp_types, tmp_elements, out);
  1763. } else {
  1764. WSDL_CACHE_PUT_1(0, out);
  1765. }
  1766. if (tmp_elements != NULL) {
  1767. zend_hash_destroy(tmp_elements);
  1768. efree(tmp_elements);
  1769. }
  1770. }
  1771. static void sdl_serialize_encoder(encodePtr enc, HashTable *tmp_types, smart_str *out)
  1772. {
  1773. WSDL_CACHE_PUT_INT(enc->details.type, out);
  1774. sdl_serialize_string(enc->details.type_str, out);
  1775. sdl_serialize_string(enc->details.ns, out);
  1776. sdl_serialize_type_ref(enc->details.sdl_type, tmp_types, out);
  1777. }
  1778. static void sdl_serialize_parameters(HashTable *ht, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1779. {
  1780. int i;
  1781. if (ht) {
  1782. i = zend_hash_num_elements(ht);
  1783. } else {
  1784. i = 0;
  1785. }
  1786. WSDL_CACHE_PUT_INT(i, out);
  1787. if (i > 0) {
  1788. sdlParamPtr *tmp;
  1789. zend_hash_internal_pointer_reset(ht);
  1790. while (zend_hash_get_current_data(ht, (void**)&tmp) == SUCCESS) {
  1791. sdl_serialize_key(ht, out);
  1792. sdl_serialize_string((*tmp)->paramName, out);
  1793. WSDL_CACHE_PUT_INT((*tmp)->order, out);
  1794. sdl_serialize_encoder_ref((*tmp)->encode, tmp_encoders, out);
  1795. sdl_serialize_type_ref((*tmp)->element, tmp_types, out);
  1796. zend_hash_move_forward(ht);
  1797. }
  1798. }
  1799. }
  1800. static void sdl_serialize_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1801. {
  1802. int i, j;
  1803. WSDL_CACHE_PUT_1(body->use, out);
  1804. if (body->use == SOAP_ENCODED) {
  1805. WSDL_CACHE_PUT_1(body->encodingStyle, out);
  1806. }
  1807. sdl_serialize_string(body->ns, out);
  1808. if (body->headers) {
  1809. i = zend_hash_num_elements(body->headers);
  1810. } else {
  1811. i = 0;
  1812. }
  1813. WSDL_CACHE_PUT_INT(i, out);
  1814. if (i > 0) {
  1815. sdlSoapBindingFunctionHeaderPtr *tmp;
  1816. zend_hash_internal_pointer_reset(body->headers);
  1817. while (zend_hash_get_current_data(body->headers, (void**)&tmp) == SUCCESS) {
  1818. sdl_serialize_key(body->headers, out);
  1819. WSDL_CACHE_PUT_1((*tmp)->use, out);
  1820. if ((*tmp)->use == SOAP_ENCODED) {
  1821. WSDL_CACHE_PUT_1((*tmp)->encodingStyle, out);
  1822. }
  1823. sdl_serialize_string((*tmp)->name, out);
  1824. sdl_serialize_string((*tmp)->ns, out);
  1825. sdl_serialize_encoder_ref((*tmp)->encode, tmp_encoders, out);
  1826. sdl_serialize_type_ref((*tmp)->element, tmp_types, out);
  1827. if ((*tmp)->headerfaults) {
  1828. j = zend_hash_num_elements((*tmp)->headerfaults);
  1829. } else {
  1830. j = 0;
  1831. }
  1832. WSDL_CACHE_PUT_INT(j, out);
  1833. if (j > 0) {
  1834. sdlSoapBindingFunctionHeaderPtr *tmp2;
  1835. zend_hash_internal_pointer_reset((*tmp)->headerfaults);
  1836. while (zend_hash_get_current_data((*tmp)->headerfaults, (void**)&tmp2) == SUCCESS) {
  1837. sdl_serialize_key((*tmp)->headerfaults, out);
  1838. WSDL_CACHE_PUT_1((*tmp2)->use, out);
  1839. if ((*tmp2)->use == SOAP_ENCODED) {
  1840. WSDL_CACHE_PUT_1((*tmp2)->encodingStyle, out);
  1841. }
  1842. sdl_serialize_string((*tmp2)->name, out);
  1843. sdl_serialize_string((*tmp2)->ns, out);
  1844. sdl_serialize_encoder_ref((*tmp2)->encode, tmp_encoders, out);
  1845. sdl_serialize_type_ref((*tmp2)->element, tmp_types, out);
  1846. zend_hash_move_forward((*tmp)->headerfaults);
  1847. }
  1848. }
  1849. zend_hash_move_forward(body->headers);
  1850. }
  1851. }
  1852. }
  1853. static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr sdl TSRMLS_DC)
  1854. {
  1855. smart_str buf = {0};
  1856. smart_str *out = &buf;
  1857. int i;
  1858. int type_num = 1;
  1859. int encoder_num = 1;
  1860. int f;
  1861. encodePtr enc;
  1862. HashTable tmp_types;
  1863. HashTable tmp_encoders;
  1864. HashTable tmp_bindings;
  1865. HashTable tmp_functions;
  1866. #ifdef ZEND_WIN32
  1867. f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  1868. #else
  1869. f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  1870. #endif
  1871. if (f < 0) {return;}
  1872. zend_hash_init(&tmp_types, 0, NULL, NULL, 0);
  1873. zend_hash_init(&tmp_encoders, 0, NULL, NULL, 0);
  1874. zend_hash_init(&tmp_bindings, 0, NULL, NULL, 0);
  1875. zend_hash_init(&tmp_functions, 0, NULL, NULL, 0);
  1876. WSDL_CACHE_PUT_N("wsdl", 4, out);
  1877. WSDL_CACHE_PUT_1(WSDL_CACHE_VERSION,out);
  1878. WSDL_CACHE_PUT_1(0,out);
  1879. WSDL_CACHE_PUT_N(&t, sizeof(t), out);
  1880. sdl_serialize_string(uri, out);
  1881. sdl_serialize_string(sdl->source, out);
  1882. sdl_serialize_string(sdl->target_ns, out);
  1883. if (sdl->groups) {
  1884. i = zend_hash_num_elements(sdl->groups);
  1885. } else {
  1886. i = 0;
  1887. }
  1888. WSDL_CACHE_PUT_INT(i, out);
  1889. if (i > 0) {
  1890. sdlTypePtr *tmp;
  1891. zend_hash_internal_pointer_reset(sdl->groups);
  1892. while (zend_hash_get_current_data(sdl->groups, (void**)&tmp) == SUCCESS) {
  1893. zend_hash_add(&tmp_types, (char*)tmp, sizeof(*tmp), (void**)&type_num, sizeof(type_num), NULL);
  1894. ++type_num;
  1895. zend_hash_move_forward(sdl->groups);
  1896. }
  1897. }
  1898. if (sdl->types) {
  1899. i = zend_hash_num_elements(sdl->types);
  1900. } else {
  1901. i = 0;
  1902. }
  1903. WSDL_CACHE_PUT_INT(i, out);
  1904. if (i > 0) {
  1905. sdlTypePtr *tmp;
  1906. zend_hash_internal_pointer_reset(sdl->types);
  1907. while (zend_hash_get_current_data(sdl->types, (void**)&tmp) == SUCCESS) {
  1908. zend_hash_add(&tmp_types, (char*)tmp, sizeof(*tmp), (void**)&type_num, sizeof(type_num), NULL);
  1909. ++type_num;
  1910. zend_hash_move_forward(sdl->types);
  1911. }
  1912. }
  1913. if (sdl->elements) {
  1914. i = zend_hash_num_elements(sdl->elements);
  1915. } else {
  1916. i = 0;
  1917. }
  1918. WSDL_CACHE_PUT_INT(i, out);
  1919. if (i > 0) {
  1920. sdlTypePtr *tmp;
  1921. zend_hash_internal_pointer_reset(sdl->elements);
  1922. while (zend_hash_get_current_data(sdl->elements, (void**)&tmp) == SUCCESS) {
  1923. zend_hash_add(&tmp_types, (char*)tmp, sizeof(*tmp), (void**)&type_num, sizeof(type_num), NULL);
  1924. ++type_num;
  1925. zend_hash_move_forward(sdl->elements);
  1926. }
  1927. }
  1928. if (sdl->encoders) {
  1929. i = zend_hash_num_elements(sdl->encoders);
  1930. } else {
  1931. i = 0;
  1932. }
  1933. WSDL_CACHE_PUT_INT(i, out);
  1934. if (i > 0) {
  1935. encodePtr *tmp;
  1936. zend_hash_internal_pointer_reset(sdl->encoders);
  1937. while (zend_hash_get_current_data(sdl->encoders, (void**)&tmp) == SUCCESS) {
  1938. zend_hash_add(&tmp_encoders, (char*)tmp, sizeof(*tmp), (void**)&encoder_num, sizeof(encoder_num), NULL);
  1939. ++encoder_num;
  1940. zend_hash_move_forward(sdl->encoders);
  1941. }
  1942. }
  1943. enc = defaultEncoding;
  1944. while (enc->details.type != END_KNOWN_TYPES) {
  1945. zend_hash_add(&tmp_encoders, (char*)&enc, sizeof(encodePtr), (void**)&encoder_num, sizeof(encoder_num), NULL);
  1946. enc++;
  1947. ++encoder_num;
  1948. }
  1949. if (sdl->groups) {
  1950. sdlTypePtr *tmp;
  1951. zend_hash_internal_pointer_reset(sdl->groups);
  1952. while (zend_hash_get_current_data(sdl->groups, (void**)&tmp) == SUCCESS) {
  1953. sdl_serialize_key(sdl->groups, out);
  1954. sdl_serialize_type(*tmp, &tmp_encoders, &tmp_types, out);
  1955. zend_hash_move_forward(sdl->groups);
  1956. }
  1957. }
  1958. if (sdl->types) {
  1959. sdlTypePtr *tmp;
  1960. zend_hash_internal_pointer_reset(sdl->types);
  1961. while (zend_hash_get_current_data(sdl->types, (void**)&tmp) == SUCCESS) {
  1962. sdl_serialize_key(sdl->types, out);
  1963. sdl_serialize_type(*tmp, &tmp_encoders, &tmp_types, out);
  1964. zend_hash_move_forward(sdl->types);
  1965. }
  1966. }
  1967. if (sdl->elements) {
  1968. sdlTypePtr *tmp;
  1969. zend_hash_internal_pointer_reset(sdl->elements);
  1970. while (zend_hash_get_current_data(sdl->elements, (void**)&tmp) == SUCCESS) {
  1971. sdl_serialize_key(sdl->elements, out);
  1972. sdl_serialize_type(*tmp, &tmp_encoders, &tmp_types, out);
  1973. zend_hash_move_forward(sdl->elements);
  1974. }
  1975. }
  1976. if (sdl->encoders) {
  1977. encodePtr *tmp;
  1978. zend_hash_internal_pointer_reset(sdl->encoders);
  1979. while (zend_hash_get_current_data(sdl->encoders, (void**)&tmp) == SUCCESS) {
  1980. sdl_serialize_key(sdl->encoders, out);
  1981. sdl_serialize_encoder(*tmp, &tmp_types, out);
  1982. zend_hash_move_forward(sdl->encoders);
  1983. }
  1984. }
  1985. /* serialize bindings */
  1986. if (sdl->bindings) {
  1987. i = zend_hash_num_elements(sdl->bindings);
  1988. } else {
  1989. i = 0;
  1990. }
  1991. WSDL_CACHE_PUT_INT(i, out);
  1992. if (i > 0) {
  1993. sdlBindingPtr *tmp;
  1994. int binding_num = 1;
  1995. zend_hash_internal_pointer_reset(sdl->bindings);
  1996. while (zend_hash_get_current_data(sdl->bindings, (void**)&tmp) == SUCCESS) {
  1997. sdl_serialize_key(sdl->bindings, out);
  1998. sdl_serialize_string((*tmp)->name, out);
  1999. sdl_serialize_string((*tmp)->location, out);
  2000. WSDL_CACHE_PUT_1((*tmp)->bindingType,out);
  2001. if ((*tmp)->bindingType == BINDING_SOAP && (*tmp)->bindingAttributes != NULL) {
  2002. sdlSoapBindingPtr binding = (sdlSoapBindingPtr)(*tmp)->bindingAttributes;
  2003. WSDL_CACHE_PUT_1(binding->style, out);
  2004. WSDL_CACHE_PUT_1(binding->transport, out);
  2005. } else {
  2006. WSDL_CACHE_PUT_1(0,out);
  2007. }
  2008. zend_hash_add(&tmp_bindings, (char*)tmp, sizeof(*tmp), (void**)&binding_num, sizeof(binding_num), NULL);
  2009. binding_num++;
  2010. zend_hash_move_forward(sdl->bindings);
  2011. }
  2012. }
  2013. /* serialize functions */
  2014. i = zend_hash_num_elements(&sdl->functions);
  2015. WSDL_CACHE_PUT_INT(i, out);
  2016. if (i > 0) {
  2017. sdlFunctionPtr *tmp;
  2018. int *binding_num;
  2019. int function_num = 1;
  2020. zend_hash_internal_pointer_reset(&sdl->functions);
  2021. while (zend_hash_get_current_data(&sdl->functions, (void**)&tmp) == SUCCESS) {
  2022. sdl_serialize_key(&sdl->functions, out);
  2023. sdl_serialize_string((*tmp)->functionName, out);
  2024. sdl_serialize_string((*tmp)->requestName, out);
  2025. sdl_serialize_string((*tmp)->responseName, out);
  2026. if ((*tmp)->binding == NULL ||
  2027. zend_hash_find(&tmp_bindings,(char*)&(*tmp)->binding,sizeof((*tmp)->binding), (void**)&binding_num) != SUCCESS) {
  2028. }
  2029. WSDL_CACHE_PUT_INT(*binding_num, out);
  2030. if (*binding_num >= 0) {
  2031. if ((*tmp)->binding->bindingType == BINDING_SOAP && (*tmp)->bindingAttributes != NULL) {
  2032. sdlSoapBindingFunctionPtr binding = (sdlSoapBindingFunctionPtr)(*tmp)->bindingAttributes;
  2033. WSDL_CACHE_PUT_1(binding->style, out);
  2034. sdl_serialize_string(binding->soapAction, out);
  2035. sdl_serialize_soap_body(&binding->input, &tmp_encoders, &tmp_types, out);
  2036. sdl_serialize_soap_body(&binding->output, &tmp_encoders, &tmp_types, out);
  2037. } else {
  2038. WSDL_CACHE_PUT_1(0,out);
  2039. }
  2040. }
  2041. sdl_serialize_parameters((*tmp)->requestParameters, &tmp_encoders, &tmp_types, out);
  2042. sdl_serialize_parameters((*tmp)->responseParameters, &tmp_encoders, &tmp_types, out);
  2043. if ((*tmp)->faults) {
  2044. sdlFaultPtr *fault;
  2045. WSDL_CACHE_PUT_INT(zend_hash_num_elements((*tmp)->faults), out);
  2046. zend_hash_internal_pointer_reset((*tmp)->faults);
  2047. while (zend_hash_get_current_data((*tmp)->faults, (void**)&fault) == SUCCESS) {
  2048. sdl_serialize_key((*tmp)->faults, out);
  2049. sdl_serialize_string((*fault)->name, out);
  2050. sdl_serialize_parameters((*fault)->details, &tmp_encoders, &tmp_types, out);
  2051. if ((*tmp)->binding->bindingType == BINDING_SOAP && (*fault)->bindingAttributes != NULL) {
  2052. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)(*fault)->bindingAttributes;
  2053. WSDL_CACHE_PUT_1(binding->use, out);
  2054. if (binding->use == SOAP_ENCODED) {
  2055. WSDL_CACHE_PUT_1(binding->encodingStyle, out);
  2056. }
  2057. sdl_serialize_string(binding->ns, out);
  2058. } else {
  2059. WSDL_CACHE_PUT_1(0, out);
  2060. }
  2061. zend_hash_move_forward((*tmp)->faults);
  2062. }
  2063. } else {
  2064. WSDL_CACHE_PUT_INT(0, out);
  2065. }
  2066. zend_hash_add(&tmp_functions, (char*)tmp, sizeof(*tmp), (void**)&function_num, sizeof(function_num), NULL);
  2067. function_num++;
  2068. zend_hash_move_forward(&sdl->functions);
  2069. }
  2070. }
  2071. /* serialize requests */
  2072. if (sdl->requests) {
  2073. i = zend_hash_num_elements(sdl->requests);
  2074. } else {
  2075. i = 0;
  2076. }
  2077. WSDL_CACHE_PUT_INT(i, out);
  2078. if (i > 0) {
  2079. sdlFunctionPtr *tmp;
  2080. int *function_num;
  2081. zend_hash_internal_pointer_reset(sdl->requests);
  2082. while (zend_hash_get_current_data(sdl->requests, (void**)&tmp) == SUCCESS) {
  2083. if (zend_hash_find(&tmp_functions, (char*)tmp, sizeof(*tmp), (void**)&function_num) != SUCCESS) {
  2084. }
  2085. WSDL_CACHE_PUT_INT(*function_num, out);
  2086. sdl_serialize_key(sdl->requests, out);
  2087. zend_hash_move_forward(sdl->requests);
  2088. }
  2089. }
  2090. write(f, buf.c, buf.len);
  2091. close(f);
  2092. smart_str_free(&buf);
  2093. zend_hash_destroy(&tmp_functions);
  2094. zend_hash_destroy(&tmp_bindings);
  2095. zend_hash_destroy(&tmp_encoders);
  2096. zend_hash_destroy(&tmp_types);
  2097. }
  2098. static void make_persistent_restriction_int(void *data)
  2099. {
  2100. sdlRestrictionIntPtr *rest = (sdlRestrictionIntPtr *)data;
  2101. sdlRestrictionIntPtr prest = NULL;
  2102. prest = malloc(sizeof(sdlRestrictionInt));
  2103. *prest = **rest;
  2104. *rest = prest;
  2105. }
  2106. static void make_persistent_restriction_char(void *data)
  2107. {
  2108. sdlRestrictionCharPtr *rest = (sdlRestrictionCharPtr *)data;
  2109. sdlRestrictionCharPtr prest = NULL;
  2110. prest = malloc(sizeof(sdlRestrictionChar));
  2111. memset(prest, 0, sizeof(sdlRestrictionChar));
  2112. prest->value = strdup((*rest)->value);
  2113. prest->fixed = (*rest)->fixed;
  2114. *rest = prest;
  2115. }
  2116. static void make_persistent_sdl_type_ref(sdlTypePtr *type, HashTable *ptr_map, HashTable *bp_types)
  2117. {
  2118. sdlTypePtr *tmp;
  2119. if (zend_hash_find(ptr_map, (char *)type, sizeof(sdlTypePtr), (void**)&tmp) == SUCCESS) {
  2120. *type = *tmp;
  2121. } else {
  2122. zend_hash_next_index_insert(bp_types, (void*)&type, sizeof(sdlTypePtr*), NULL);
  2123. }
  2124. }
  2125. static void make_persistent_sdl_encoder_ref(encodePtr *enc, HashTable *ptr_map, HashTable *bp_encoders)
  2126. {
  2127. encodePtr *tmp;
  2128. /* do not process defaultEncoding's here */
  2129. if ((*enc) >= defaultEncoding && (*enc) < defaultEncoding + numDefaultEncodings) {
  2130. return;
  2131. }
  2132. if (zend_hash_find(ptr_map, (char *)enc, sizeof(encodePtr), (void**)&tmp) == SUCCESS) {
  2133. *enc = *tmp;
  2134. } else {
  2135. zend_hash_next_index_insert(bp_encoders, (void*)&enc, sizeof(encodePtr*), NULL);
  2136. }
  2137. }
  2138. static HashTable* make_persistent_sdl_function_headers(HashTable *headers, HashTable *ptr_map)
  2139. {
  2140. HashTable *pheaders;
  2141. sdlSoapBindingFunctionHeaderPtr *tmp, pheader;
  2142. encodePtr *penc;
  2143. sdlTypePtr *ptype;
  2144. ulong index;
  2145. char *key;
  2146. uint key_len;
  2147. pheaders = malloc(sizeof(HashTable));
  2148. zend_hash_init(pheaders, zend_hash_num_elements(headers), NULL, delete_header_persistent, 1);
  2149. zend_hash_internal_pointer_reset(headers);
  2150. while (zend_hash_get_current_data(headers, (void**)&tmp) == SUCCESS) {
  2151. pheader = malloc(sizeof(sdlSoapBindingFunctionHeader));
  2152. memset(pheader, 0, sizeof(sdlSoapBindingFunctionHeader));
  2153. *pheader = **tmp;
  2154. if (pheader->name) {
  2155. pheader->name = strdup(pheader->name);
  2156. }
  2157. if (pheader->ns) {
  2158. pheader->ns = strdup(pheader->ns);
  2159. }
  2160. if (pheader->encode->details.sdl_type) {
  2161. if (zend_hash_find(ptr_map, (char*)&pheader->encode, sizeof(encodePtr), (void**)&penc) == FAILURE) {
  2162. assert(0);
  2163. }
  2164. pheader->encode = *penc;
  2165. }
  2166. if (pheader->element) {
  2167. if (zend_hash_find(ptr_map, (char*)&pheader->element, sizeof(sdlTypePtr), (void**)&ptype) == FAILURE) {
  2168. assert(0);
  2169. }
  2170. pheader->element = *ptype;
  2171. }
  2172. if (pheader->headerfaults) {
  2173. pheader->headerfaults = make_persistent_sdl_function_headers(pheader->headerfaults, ptr_map);
  2174. }
  2175. if (zend_hash_get_current_key_ex(headers, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2176. zend_hash_add(pheaders, key, key_len, (void*)&pheader, sizeof(sdlSoapBindingFunctionHeaderPtr), NULL);
  2177. } else {
  2178. zend_hash_next_index_insert(pheaders, (void*)&pheader, sizeof(sdlSoapBindingFunctionHeaderPtr), NULL);
  2179. }
  2180. zend_hash_move_forward(headers);
  2181. }
  2182. return pheaders;
  2183. }
  2184. static void make_persistent_sdl_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *ptr_map)
  2185. {
  2186. if (body->ns) {
  2187. body->ns = strdup(body->ns);
  2188. }
  2189. if (body->headers) {
  2190. body->headers = make_persistent_sdl_function_headers(body->headers, ptr_map);
  2191. }
  2192. }
  2193. static HashTable* make_persistent_sdl_parameters(HashTable *params, HashTable *ptr_map)
  2194. {
  2195. HashTable *pparams;
  2196. sdlParamPtr *tmp, pparam;
  2197. sdlTypePtr *ptype;
  2198. encodePtr *penc;
  2199. ulong index;
  2200. char *key;
  2201. uint key_len;
  2202. pparams = malloc(sizeof(HashTable));
  2203. zend_hash_init(pparams, zend_hash_num_elements(params), NULL, delete_parameter_persistent, 1);
  2204. zend_hash_internal_pointer_reset(params);
  2205. while (zend_hash_get_current_data(params, (void**)&tmp) == SUCCESS) {
  2206. pparam = malloc(sizeof(sdlParam));
  2207. memset(pparam, 0, sizeof(sdlParam));
  2208. *pparam = **tmp;
  2209. if (pparam->paramName) {
  2210. pparam->paramName = strdup(pparam->paramName);
  2211. }
  2212. if (pparam->encode && pparam->encode->details.sdl_type) {
  2213. if (zend_hash_find(ptr_map, (char*)&pparam->encode, sizeof(encodePtr), (void**)&penc) == FAILURE) {
  2214. assert(0);
  2215. }
  2216. pparam->encode = *penc;
  2217. }
  2218. if (pparam->element) {
  2219. if (zend_hash_find(ptr_map, (char*)&pparam->element, sizeof(sdlTypePtr), (void**)&ptype) == FAILURE) {
  2220. assert(0);
  2221. }
  2222. pparam->element = *ptype;
  2223. }
  2224. if (zend_hash_get_current_key_ex(params, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2225. zend_hash_add(pparams, key, key_len, (void*)&pparam, sizeof(sdlParamPtr), NULL);
  2226. } else {
  2227. zend_hash_next_index_insert(pparams, (void*)&pparam, sizeof(sdlParamPtr), NULL);
  2228. }
  2229. zend_hash_move_forward(params);
  2230. }
  2231. return pparams;
  2232. }
  2233. static HashTable* make_persistent_sdl_function_faults(sdlFunctionPtr func, HashTable *faults, HashTable *ptr_map)
  2234. {
  2235. HashTable *pfaults;
  2236. sdlFaultPtr *tmp, pfault;
  2237. ulong index;
  2238. char *key;
  2239. uint key_len;
  2240. pfaults = malloc(sizeof(HashTable));
  2241. zend_hash_init(pfaults, zend_hash_num_elements(faults), NULL, delete_fault_persistent, 1);
  2242. zend_hash_internal_pointer_reset(faults);
  2243. while (zend_hash_get_current_data(faults, (void**)&tmp) == SUCCESS) {
  2244. pfault = malloc(sizeof(sdlFault));
  2245. memset(pfault, 0, sizeof(sdlFault));
  2246. *pfault = **tmp;
  2247. if (pfault->name) {
  2248. pfault->name = strdup(pfault->name);
  2249. }
  2250. if (pfault->details) {
  2251. pfault->details = make_persistent_sdl_parameters(pfault->details, ptr_map);
  2252. }
  2253. if (func->binding->bindingType == BINDING_SOAP && pfault->bindingAttributes) {
  2254. sdlSoapBindingFunctionFaultPtr soap_binding;
  2255. soap_binding = malloc(sizeof(sdlSoapBindingFunctionFault));
  2256. memset(soap_binding, 0, sizeof(sdlSoapBindingFunctionFault));
  2257. *soap_binding = *(sdlSoapBindingFunctionFaultPtr)pfault->bindingAttributes;
  2258. if (soap_binding->ns) {
  2259. soap_binding->ns = strdup(soap_binding->ns);
  2260. }
  2261. pfault->bindingAttributes = soap_binding;
  2262. }
  2263. if (zend_hash_get_current_key_ex(faults, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2264. zend_hash_add(pfaults, key, key_len, (void*)&pfault, sizeof(sdlParamPtr), NULL);
  2265. } else {
  2266. zend_hash_next_index_insert(pfaults, (void*)&pfault, sizeof(sdlParamPtr), NULL);
  2267. }
  2268. zend_hash_move_forward(faults);
  2269. }
  2270. return pfaults;
  2271. }
  2272. static sdlAttributePtr make_persistent_sdl_attribute(sdlAttributePtr attr, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2273. {
  2274. sdlAttributePtr pattr;
  2275. ulong index;
  2276. char *key;
  2277. uint key_len;
  2278. pattr = malloc(sizeof(sdlAttribute));
  2279. memset(pattr, 0, sizeof(sdlAttribute));
  2280. *pattr = *attr;
  2281. if (pattr->name) {
  2282. pattr->name = strdup(pattr->name);
  2283. }
  2284. if (pattr->namens) {
  2285. pattr->namens = strdup(pattr->namens);
  2286. }
  2287. if (pattr->ref) {
  2288. pattr->ref = strdup(pattr->ref);
  2289. }
  2290. if (pattr->def) {
  2291. pattr->def = strdup(pattr->def);
  2292. }
  2293. if (pattr->fixed) {
  2294. pattr->fixed = strdup(pattr->fixed);
  2295. }
  2296. /* we do not want to process defaultEncoding's here */
  2297. if (pattr->encode) {
  2298. make_persistent_sdl_encoder_ref(&pattr->encode, ptr_map, bp_encoders);
  2299. }
  2300. if (pattr->extraAttributes) {
  2301. sdlExtraAttributePtr *tmp, pextra;
  2302. pattr->extraAttributes = malloc(sizeof(HashTable));
  2303. zend_hash_init(pattr->extraAttributes, zend_hash_num_elements(attr->extraAttributes), NULL, delete_extra_attribute_persistent, 1);
  2304. zend_hash_internal_pointer_reset(pattr->extraAttributes);
  2305. while (zend_hash_get_current_data(attr->extraAttributes, (void**)&tmp) == SUCCESS) {
  2306. pextra = malloc(sizeof(sdlExtraAttribute));
  2307. memset(pextra, 0, sizeof(sdlExtraAttribute));
  2308. if ((*tmp)->ns) {
  2309. pextra->ns = strdup((*tmp)->ns);
  2310. }
  2311. if ((*tmp)->val) {
  2312. pextra->val = strdup((*tmp)->val);
  2313. }
  2314. if (zend_hash_get_current_key_ex(attr->extraAttributes, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2315. zend_hash_add(pattr->extraAttributes, key, key_len, (void*)&pextra, sizeof(sdlExtraAttributePtr), NULL);
  2316. }
  2317. zend_hash_move_forward(attr->extraAttributes);
  2318. }
  2319. }
  2320. return pattr;
  2321. }
  2322. static sdlContentModelPtr make_persistent_sdl_model(sdlContentModelPtr model, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2323. {
  2324. sdlContentModelPtr pmodel;
  2325. sdlContentModelPtr *tmp, pcontent;
  2326. pmodel = malloc(sizeof(sdlContentModel));
  2327. memset(pmodel, 0, sizeof(sdlContentModel));
  2328. *pmodel = *model;
  2329. switch (pmodel->kind) {
  2330. case XSD_CONTENT_ELEMENT:
  2331. if (pmodel->u.element) {
  2332. make_persistent_sdl_type_ref(&pmodel->u.element, ptr_map, bp_types);
  2333. }
  2334. break;
  2335. case XSD_CONTENT_SEQUENCE:
  2336. case XSD_CONTENT_ALL:
  2337. case XSD_CONTENT_CHOICE:
  2338. pmodel->u.content = malloc(sizeof(HashTable));
  2339. zend_hash_init(pmodel->u.content, zend_hash_num_elements(model->u.content), NULL, delete_model_persistent, 1);
  2340. zend_hash_internal_pointer_reset(model->u.content);
  2341. while (zend_hash_get_current_data(model->u.content, (void**)&tmp) == SUCCESS) {
  2342. pcontent = make_persistent_sdl_model(*tmp, ptr_map, bp_types, bp_encoders);
  2343. zend_hash_next_index_insert(pmodel->u.content, (void*)&pcontent, sizeof(sdlContentModelPtr), NULL);
  2344. zend_hash_move_forward(model->u.content);
  2345. }
  2346. break;
  2347. case XSD_CONTENT_GROUP_REF:
  2348. if (pmodel->u.group_ref) {
  2349. pmodel->u.group_ref = strdup(pmodel->u.group_ref);
  2350. }
  2351. break;
  2352. case XSD_CONTENT_GROUP:
  2353. if (pmodel->u.group) {
  2354. make_persistent_sdl_type_ref(&pmodel->u.group, ptr_map, bp_types);
  2355. }
  2356. break;
  2357. default:
  2358. break;
  2359. }
  2360. return pmodel;
  2361. }
  2362. static sdlTypePtr make_persistent_sdl_type(sdlTypePtr type, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2363. {
  2364. ulong index;
  2365. char *key;
  2366. uint key_len;
  2367. sdlTypePtr ptype = NULL;
  2368. ptype = malloc(sizeof(sdlType));
  2369. memset(ptype, 0, sizeof(sdlType));
  2370. *ptype = *type;
  2371. if (ptype->name) {
  2372. ptype->name = strdup(ptype->name);
  2373. }
  2374. if (ptype->namens) {
  2375. ptype->namens = strdup(ptype->namens);
  2376. }
  2377. if (ptype->def) {
  2378. ptype->def = strdup(ptype->def);
  2379. }
  2380. if (ptype->fixed) {
  2381. ptype->fixed = strdup(ptype->fixed);
  2382. }
  2383. if (ptype->ref) {
  2384. ptype->ref = strdup(ptype->ref);
  2385. }
  2386. /* we do not want to process defaultEncoding's here */
  2387. if (ptype->encode) {
  2388. make_persistent_sdl_encoder_ref(&ptype->encode, ptr_map, bp_encoders);
  2389. }
  2390. if (ptype->restrictions) {
  2391. ptype->restrictions = malloc(sizeof(sdlRestrictions));
  2392. memset(ptype->restrictions, 0, sizeof(sdlRestrictions));
  2393. *ptype->restrictions = *type->restrictions;
  2394. if (ptype->restrictions->minExclusive) {
  2395. make_persistent_restriction_int(&ptype->restrictions->minExclusive);
  2396. }
  2397. if (ptype->restrictions->maxExclusive) {
  2398. make_persistent_restriction_int(&ptype->restrictions->maxExclusive);
  2399. }
  2400. if (ptype->restrictions->minInclusive) {
  2401. make_persistent_restriction_int(&ptype->restrictions->minInclusive);
  2402. }
  2403. if (ptype->restrictions->maxInclusive) {
  2404. make_persistent_restriction_int(&ptype->restrictions->maxInclusive);
  2405. }
  2406. if (ptype->restrictions->totalDigits) {
  2407. make_persistent_restriction_int(&ptype->restrictions->totalDigits);
  2408. }
  2409. if (ptype->restrictions->fractionDigits) {
  2410. make_persistent_restriction_int(&ptype->restrictions->fractionDigits);
  2411. }
  2412. if (ptype->restrictions->length) {
  2413. make_persistent_restriction_int(&ptype->restrictions->length);
  2414. }
  2415. if (ptype->restrictions->minLength) {
  2416. make_persistent_restriction_int(&ptype->restrictions->minLength);
  2417. }
  2418. if (ptype->restrictions->maxLength) {
  2419. make_persistent_restriction_int(&ptype->restrictions->maxLength);
  2420. }
  2421. if (ptype->restrictions->whiteSpace) {
  2422. make_persistent_restriction_char(&ptype->restrictions->whiteSpace);
  2423. }
  2424. if (ptype->restrictions->pattern) {
  2425. make_persistent_restriction_char(&ptype->restrictions->pattern);
  2426. }
  2427. if (type->restrictions->enumeration) {
  2428. sdlRestrictionCharPtr tmp;
  2429. ptype->restrictions->enumeration = malloc(sizeof(HashTable));
  2430. zend_hash_init(ptype->restrictions->enumeration, zend_hash_num_elements(type->restrictions->enumeration), NULL, delete_restriction_var_char_persistent, 1);
  2431. zend_hash_copy(ptype->restrictions->enumeration, type->restrictions->enumeration, make_persistent_restriction_char, (void*)&tmp, sizeof(sdlRestrictionCharPtr));
  2432. }
  2433. }
  2434. if (ptype->elements) {
  2435. sdlTypePtr *tmp, pelem;
  2436. ptype->elements = malloc(sizeof(HashTable));
  2437. zend_hash_init(ptype->elements, zend_hash_num_elements(type->elements), NULL, delete_type_persistent, 1);
  2438. zend_hash_internal_pointer_reset(type->elements);
  2439. while (zend_hash_get_current_data(type->elements, (void **)&tmp) == SUCCESS) {
  2440. pelem = make_persistent_sdl_type(*tmp, ptr_map, bp_types, bp_encoders);
  2441. if (zend_hash_get_current_key_ex(type->elements, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2442. zend_hash_add(ptype->elements, key, key_len, (void*)&pelem, sizeof(sdlTypePtr), NULL);
  2443. } else {
  2444. zend_hash_next_index_insert(ptype->elements, (void*)&pelem, sizeof(sdlTypePtr), NULL);
  2445. }
  2446. zend_hash_add(ptr_map, (char*)tmp, sizeof(*tmp), (void*)&pelem, sizeof(sdlTypePtr), NULL);
  2447. zend_hash_move_forward(type->elements);
  2448. }
  2449. }
  2450. if (ptype->attributes) {
  2451. sdlAttributePtr *tmp, pattr;
  2452. ptype->attributes = malloc(sizeof(HashTable));
  2453. zend_hash_init(ptype->attributes, zend_hash_num_elements(type->attributes), NULL, delete_attribute_persistent, 1);
  2454. zend_hash_internal_pointer_reset(type->attributes);
  2455. while (zend_hash_get_current_data(type->attributes, (void **)&tmp) == SUCCESS) {
  2456. pattr = make_persistent_sdl_attribute(*tmp, ptr_map, bp_types, bp_encoders);
  2457. if (zend_hash_get_current_key_ex(type->attributes, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2458. zend_hash_add(ptype->attributes, key, key_len, (void*)&pattr, sizeof(sdlAttributePtr), NULL);
  2459. } else {
  2460. zend_hash_next_index_insert(ptype->attributes, (void*)&pattr, sizeof(sdlAttributePtr), NULL);
  2461. }
  2462. zend_hash_move_forward(type->attributes);
  2463. }
  2464. }
  2465. if (type->model) {
  2466. ptype->model = make_persistent_sdl_model(ptype->model, ptr_map, bp_types, bp_encoders);
  2467. }
  2468. return ptype;
  2469. }
  2470. static encodePtr make_persistent_sdl_encoder(encodePtr enc, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2471. {
  2472. encodePtr penc = NULL;
  2473. penc = malloc(sizeof(encode));
  2474. memset(penc, 0, sizeof(encode));
  2475. *penc = *enc;
  2476. if (penc->details.type_str) {
  2477. penc->details.type_str = strdup(penc->details.type_str);
  2478. }
  2479. if (penc->details.ns) {
  2480. penc->details.ns = strdup(penc->details.ns);
  2481. }
  2482. if (penc->details.sdl_type) {
  2483. make_persistent_sdl_type_ref(&penc->details.sdl_type, ptr_map, bp_types);
  2484. }
  2485. return penc;
  2486. }
  2487. static sdlBindingPtr make_persistent_sdl_binding(sdlBindingPtr bind, HashTable *ptr_map)
  2488. {
  2489. sdlBindingPtr pbind = NULL;
  2490. pbind = malloc(sizeof(sdlBinding));
  2491. memset(pbind, 0, sizeof(sdlBinding));
  2492. *pbind = *bind;
  2493. if (pbind->name) {
  2494. pbind->name = strdup(pbind->name);
  2495. }
  2496. if (pbind->location) {
  2497. pbind->location = strdup(pbind->location);
  2498. }
  2499. if (pbind->bindingType == BINDING_SOAP && pbind->bindingAttributes) {
  2500. sdlSoapBindingPtr soap_binding;
  2501. soap_binding = malloc(sizeof(sdlSoapBinding));
  2502. memset(soap_binding, 0, sizeof(sdlSoapBinding));
  2503. *soap_binding = *(sdlSoapBindingPtr)pbind->bindingAttributes;
  2504. pbind->bindingAttributes = soap_binding;
  2505. }
  2506. return pbind;
  2507. }
  2508. static sdlFunctionPtr make_persistent_sdl_function(sdlFunctionPtr func, HashTable *ptr_map)
  2509. {
  2510. sdlFunctionPtr pfunc = NULL;
  2511. pfunc = malloc(sizeof(sdlFunction));
  2512. memset(pfunc, 0, sizeof(sdlFunction));
  2513. *pfunc = *func;
  2514. if (pfunc->functionName) {
  2515. pfunc->functionName = strdup(pfunc->functionName);
  2516. }
  2517. if (pfunc->requestName) {
  2518. pfunc->requestName = strdup(pfunc->requestName);
  2519. }
  2520. if (pfunc->responseName) {
  2521. pfunc->responseName = strdup(pfunc->responseName);
  2522. }
  2523. if (pfunc->binding) {
  2524. sdlBindingPtr *tmp;
  2525. if (zend_hash_find(ptr_map, (char*)&pfunc->binding, sizeof(pfunc->binding), (void**)&tmp) == FAILURE) {
  2526. assert(0);
  2527. }
  2528. pfunc->binding = *tmp;
  2529. if (pfunc->binding->bindingType == BINDING_SOAP && pfunc->bindingAttributes) {
  2530. sdlSoapBindingFunctionPtr soap_binding;
  2531. soap_binding = malloc(sizeof(sdlSoapBindingFunction));
  2532. memset(soap_binding, 0, sizeof(sdlSoapBindingFunction));
  2533. *soap_binding = *(sdlSoapBindingFunctionPtr)pfunc->bindingAttributes;
  2534. if (soap_binding->soapAction) {
  2535. soap_binding->soapAction = strdup(soap_binding->soapAction);
  2536. }
  2537. make_persistent_sdl_soap_body(&soap_binding->input, ptr_map);
  2538. make_persistent_sdl_soap_body(&soap_binding->output, ptr_map);
  2539. pfunc->bindingAttributes = soap_binding;
  2540. }
  2541. if (pfunc->requestParameters) {
  2542. pfunc->requestParameters = make_persistent_sdl_parameters(pfunc->requestParameters, ptr_map);
  2543. }
  2544. if (pfunc->responseParameters) {
  2545. pfunc->responseParameters = make_persistent_sdl_parameters(pfunc->responseParameters, ptr_map);
  2546. }
  2547. if (pfunc->faults) {
  2548. pfunc->faults = make_persistent_sdl_function_faults(pfunc, pfunc->faults, ptr_map);
  2549. }
  2550. }
  2551. return pfunc;
  2552. }
  2553. static sdlPtr make_persistent_sdl(sdlPtr sdl TSRMLS_DC)
  2554. {
  2555. sdlPtr psdl = NULL;
  2556. HashTable ptr_map;
  2557. HashTable bp_types, bp_encoders;
  2558. ulong index;
  2559. char *key;
  2560. uint key_len;
  2561. zend_hash_init(&bp_types, 0, NULL, NULL, 0);
  2562. zend_hash_init(&bp_encoders, 0, NULL, NULL, 0);
  2563. zend_hash_init(&ptr_map, 0, NULL, NULL, 0);
  2564. psdl = malloc(sizeof(*sdl));
  2565. memset(psdl, 0, sizeof(*sdl));
  2566. if (sdl->source) {
  2567. psdl->source = strdup(sdl->source);
  2568. }
  2569. if (sdl->target_ns) {
  2570. psdl->target_ns = strdup(sdl->target_ns);
  2571. }
  2572. if (sdl->groups) {
  2573. sdlTypePtr *tmp;
  2574. sdlTypePtr ptype;
  2575. psdl->groups = malloc(sizeof(HashTable));
  2576. zend_hash_init(psdl->groups, zend_hash_num_elements(sdl->groups), NULL, delete_type_persistent, 1);
  2577. zend_hash_internal_pointer_reset(sdl->groups);
  2578. while (zend_hash_get_current_data(sdl->groups, (void **)&tmp) == SUCCESS) {
  2579. ptype = make_persistent_sdl_type(*tmp, &ptr_map, &bp_types, &bp_encoders);
  2580. if (zend_hash_get_current_key_ex(sdl->groups, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2581. zend_hash_add(psdl->groups, key, key_len, (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2582. } else {
  2583. zend_hash_next_index_insert(psdl->groups, (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2584. }
  2585. zend_hash_add(&ptr_map, (char*)tmp, sizeof(*tmp), (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2586. zend_hash_move_forward(sdl->groups);
  2587. }
  2588. }
  2589. if (sdl->types) {
  2590. sdlTypePtr *tmp;
  2591. sdlTypePtr ptype;
  2592. psdl->types = malloc(sizeof(HashTable));
  2593. zend_hash_init(psdl->types, zend_hash_num_elements(sdl->types), NULL, delete_type_persistent, 1);
  2594. zend_hash_internal_pointer_reset(sdl->types);
  2595. while (zend_hash_get_current_data(sdl->types, (void **)&tmp) == SUCCESS) {
  2596. ptype = make_persistent_sdl_type(*tmp, &ptr_map, &bp_types, &bp_encoders);
  2597. if (zend_hash_get_current_key_ex(sdl->types, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2598. zend_hash_add(psdl->types, key, key_len, (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2599. } else {
  2600. zend_hash_next_index_insert(psdl->types, (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2601. }
  2602. zend_hash_add(&ptr_map, (char*)tmp, sizeof(*tmp), (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2603. zend_hash_move_forward(sdl->types);
  2604. }
  2605. }
  2606. if (sdl->elements) {
  2607. sdlTypePtr *tmp;
  2608. sdlTypePtr ptype;
  2609. psdl->elements = malloc(sizeof(HashTable));
  2610. zend_hash_init(psdl->elements, zend_hash_num_elements(sdl->elements), NULL, delete_type_persistent, 1);
  2611. zend_hash_internal_pointer_reset(sdl->elements);
  2612. while (zend_hash_get_current_data(sdl->elements, (void **)&tmp) == SUCCESS) {
  2613. ptype = make_persistent_sdl_type(*tmp, &ptr_map, &bp_types, &bp_encoders);
  2614. if (zend_hash_get_current_key_ex(sdl->elements, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2615. zend_hash_add(psdl->elements, key, key_len, (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2616. } else {
  2617. zend_hash_next_index_insert(psdl->elements, (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2618. }
  2619. zend_hash_add(&ptr_map, (char*)tmp, sizeof(*tmp), (void*)&ptype, sizeof(sdlTypePtr), NULL);
  2620. zend_hash_move_forward(sdl->elements);
  2621. }
  2622. }
  2623. if (sdl->encoders) {
  2624. encodePtr *tmp;
  2625. encodePtr penc;
  2626. psdl->encoders = malloc(sizeof(HashTable));
  2627. zend_hash_init(psdl->encoders, zend_hash_num_elements(sdl->encoders), NULL, delete_encoder_persistent, 1);
  2628. zend_hash_internal_pointer_reset(sdl->encoders);
  2629. while (zend_hash_get_current_data(sdl->encoders, (void **)&tmp) == SUCCESS) {
  2630. penc = make_persistent_sdl_encoder(*tmp, &ptr_map, &bp_types, &bp_encoders);
  2631. if (zend_hash_get_current_key_ex(sdl->encoders, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2632. zend_hash_add(psdl->encoders, key, key_len, (void*)&penc, sizeof(encodePtr), NULL);
  2633. } else {
  2634. zend_hash_next_index_insert(psdl->encoders, (void*)&penc, sizeof(encodePtr), NULL);
  2635. }
  2636. zend_hash_add(&ptr_map, (char*)tmp, sizeof(*tmp), (void*)&penc, sizeof(encodePtr), NULL);
  2637. zend_hash_move_forward(sdl->encoders);
  2638. }
  2639. }
  2640. /* do backpatching here */
  2641. if (zend_hash_num_elements(&bp_types)) {
  2642. sdlTypePtr **tmp, *ptype = NULL;
  2643. zend_hash_internal_pointer_reset(&bp_types);
  2644. while (zend_hash_get_current_data(&bp_types, (void**)&tmp) == SUCCESS) {
  2645. if (zend_hash_find(&ptr_map, (char*)(*tmp), sizeof(**tmp), (void**)&ptype) == FAILURE) {
  2646. assert(0);
  2647. }
  2648. **tmp = *ptype;
  2649. zend_hash_move_forward(&bp_types);
  2650. }
  2651. }
  2652. if (zend_hash_num_elements(&bp_encoders)) {
  2653. encodePtr **tmp, *penc = NULL;
  2654. zend_hash_internal_pointer_reset(&bp_encoders);
  2655. while (zend_hash_get_current_data(&bp_encoders, (void**)&tmp) == SUCCESS) {
  2656. if (zend_hash_find(&ptr_map, (char*)(*tmp), sizeof(**tmp), (void**)&penc) == FAILURE) {
  2657. assert(0);
  2658. }
  2659. **tmp = *penc;
  2660. zend_hash_move_forward(&bp_encoders);
  2661. }
  2662. }
  2663. if (sdl->bindings) {
  2664. sdlBindingPtr *tmp;
  2665. sdlBindingPtr pbind;
  2666. psdl->bindings = malloc(sizeof(HashTable));
  2667. zend_hash_init(psdl->bindings, zend_hash_num_elements(sdl->bindings), NULL, delete_binding_persistent, 1);
  2668. zend_hash_internal_pointer_reset(sdl->bindings);
  2669. while (zend_hash_get_current_data(sdl->bindings, (void **)&tmp) == SUCCESS) {
  2670. pbind = make_persistent_sdl_binding(*tmp, &ptr_map);
  2671. if (zend_hash_get_current_key_ex(sdl->bindings, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2672. zend_hash_add(psdl->bindings, key, key_len, (void*)&pbind, sizeof(sdlBindingPtr), NULL);
  2673. } else {
  2674. zend_hash_next_index_insert(psdl->bindings, (void*)&pbind, sizeof(sdlBindingPtr), NULL);
  2675. }
  2676. zend_hash_add(&ptr_map, (char*)tmp, sizeof(*tmp), (void*)&pbind, sizeof(sdlBindingPtr), NULL);
  2677. zend_hash_move_forward(sdl->bindings);
  2678. }
  2679. }
  2680. zend_hash_init(&psdl->functions, zend_hash_num_elements(&sdl->functions), NULL, delete_function_persistent, 1);
  2681. if (zend_hash_num_elements(&sdl->functions)) {
  2682. sdlFunctionPtr *tmp;
  2683. sdlFunctionPtr pfunc;
  2684. zend_hash_internal_pointer_reset(&sdl->functions);
  2685. while (zend_hash_get_current_data(&sdl->functions, (void **)&tmp) == SUCCESS) {
  2686. pfunc = make_persistent_sdl_function(*tmp, &ptr_map);
  2687. if (zend_hash_get_current_key_ex(&sdl->functions, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2688. zend_hash_add(&psdl->functions, key, key_len, (void*)&pfunc, sizeof(sdlFunctionPtr), NULL);
  2689. } else {
  2690. zend_hash_next_index_insert(&psdl->functions, (void*)&pfunc, sizeof(sdlFunctionPtr), NULL);
  2691. }
  2692. zend_hash_add(&ptr_map, (char*)tmp, sizeof(*tmp), (void*)&pfunc, sizeof(sdlFunctionPtr), NULL);
  2693. zend_hash_move_forward(&sdl->functions);
  2694. }
  2695. }
  2696. if (sdl->requests) {
  2697. sdlFunctionPtr *tmp;
  2698. sdlFunctionPtr *preq;
  2699. psdl->requests = malloc(sizeof(HashTable));
  2700. zend_hash_init(psdl->requests, zend_hash_num_elements(sdl->requests), NULL, NULL, 1);
  2701. zend_hash_internal_pointer_reset(sdl->requests);
  2702. while (zend_hash_get_current_data(sdl->requests, (void **)&tmp) == SUCCESS) {
  2703. if (zend_hash_find(&ptr_map, (char*)tmp, sizeof(*tmp), (void**)&preq) == FAILURE) {
  2704. assert(0);
  2705. }
  2706. *tmp = *preq;
  2707. if (zend_hash_get_current_key_ex(sdl->requests, &key, &key_len, &index, 0, NULL) == HASH_KEY_IS_STRING) {
  2708. zend_hash_add(psdl->requests, key, key_len, (void*)&preq, sizeof(sdlFunctionPtr), NULL);
  2709. }
  2710. zend_hash_move_forward(sdl->requests);
  2711. }
  2712. }
  2713. zend_hash_destroy(&ptr_map);
  2714. zend_hash_destroy(&bp_encoders);
  2715. zend_hash_destroy(&bp_types);
  2716. return psdl;
  2717. }
  2718. typedef struct _sdl_cache_bucket {
  2719. sdlPtr sdl;
  2720. time_t time;
  2721. } sdl_cache_bucket;
  2722. static void delete_psdl(void *data)
  2723. {
  2724. sdl_cache_bucket *p = (sdl_cache_bucket*)data;
  2725. sdlPtr tmp = p->sdl;
  2726. zend_hash_destroy(&tmp->functions);
  2727. if (tmp->source) {
  2728. free(tmp->source);
  2729. }
  2730. if (tmp->target_ns) {
  2731. free(tmp->target_ns);
  2732. }
  2733. if (tmp->elements) {
  2734. zend_hash_destroy(tmp->elements);
  2735. free(tmp->elements);
  2736. }
  2737. if (tmp->encoders) {
  2738. zend_hash_destroy(tmp->encoders);
  2739. free(tmp->encoders);
  2740. }
  2741. if (tmp->types) {
  2742. zend_hash_destroy(tmp->types);
  2743. free(tmp->types);
  2744. }
  2745. if (tmp->groups) {
  2746. zend_hash_destroy(tmp->groups);
  2747. free(tmp->groups);
  2748. }
  2749. if (tmp->bindings) {
  2750. zend_hash_destroy(tmp->bindings);
  2751. free(tmp->bindings);
  2752. }
  2753. if (tmp->requests) {
  2754. zend_hash_destroy(tmp->requests);
  2755. free(tmp->requests);
  2756. }
  2757. free(tmp);
  2758. }
  2759. sdlPtr get_sdl(zval *this_ptr, char *uri, long cache_wsdl TSRMLS_DC)
  2760. {
  2761. char fn[MAXPATHLEN];
  2762. sdlPtr sdl = NULL;
  2763. char* old_error_code = SOAP_GLOBAL(error_code);
  2764. int uri_len = 0;
  2765. php_stream_context *context=NULL;
  2766. zval **tmp, **proxy_host, **proxy_port, *orig_context = NULL, *new_context = NULL;
  2767. smart_str headers = {0};
  2768. char* key = NULL;
  2769. time_t t = sapi_get_request_time(TSRMLS_C);
  2770. if (strchr(uri,':') != NULL || IS_ABSOLUTE_PATH(uri, uri_len)) {
  2771. uri_len = strlen(uri);
  2772. } else if (VCWD_REALPATH(uri, fn) == NULL) {
  2773. cache_wsdl = WSDL_CACHE_NONE;
  2774. } else {
  2775. uri = fn;
  2776. uri_len = strlen(uri);
  2777. }
  2778. if ((cache_wsdl & WSDL_CACHE_MEMORY) && SOAP_GLOBAL(mem_cache)) {
  2779. sdl_cache_bucket *p;
  2780. if (SUCCESS == zend_hash_find(SOAP_GLOBAL(mem_cache), uri, uri_len+1, (void*)&p)) {
  2781. if (p->time < t - SOAP_GLOBAL(cache_ttl)) {
  2782. /* in-memory cache entry is expired */
  2783. zend_hash_del(&EG(persistent_list), uri, uri_len+1);
  2784. } else {
  2785. return p->sdl;
  2786. }
  2787. }
  2788. }
  2789. if ((cache_wsdl & WSDL_CACHE_DISK) && (uri_len < MAXPATHLEN)) {
  2790. time_t t = sapi_get_request_time(TSRMLS_C);
  2791. char md5str[33];
  2792. PHP_MD5_CTX context;
  2793. unsigned char digest[16];
  2794. int len = strlen(SOAP_GLOBAL(cache_dir));
  2795. time_t cached;
  2796. char *user = php_get_current_user();
  2797. int user_len = user ? strlen(user) + 1 : 0;
  2798. md5str[0] = '\0';
  2799. PHP_MD5Init(&context);
  2800. PHP_MD5Update(&context, (unsigned char*)uri, uri_len);
  2801. PHP_MD5Final(digest, &context);
  2802. make_digest(md5str, digest);
  2803. key = emalloc(len+sizeof("/wsdl-")-1+user_len+sizeof(md5str));
  2804. memcpy(key,SOAP_GLOBAL(cache_dir),len);
  2805. memcpy(key+len,"/wsdl-",sizeof("/wsdl-")-1);
  2806. len += sizeof("/wsdl-")-1;
  2807. if (user_len) {
  2808. memcpy(key+len, user, user_len-1);
  2809. len += user_len-1;
  2810. key[len++] = '-';
  2811. }
  2812. memcpy(key+len,md5str,sizeof(md5str));
  2813. if ((sdl = get_sdl_from_cache(key, uri, t-SOAP_GLOBAL(cache_ttl), &cached TSRMLS_CC)) != NULL) {
  2814. t = cached;
  2815. efree(key);
  2816. goto cache_in_memory;
  2817. }
  2818. }
  2819. if (SUCCESS == zend_hash_find(Z_OBJPROP_P(this_ptr),
  2820. "_stream_context", sizeof("_stream_context"), (void**)&tmp)) {
  2821. context = php_stream_context_from_zval(*tmp, 0);
  2822. } else {
  2823. context = php_stream_context_alloc();
  2824. }
  2825. if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_proxy_host", sizeof("_proxy_host"), (void **) &proxy_host) == SUCCESS &&
  2826. Z_TYPE_PP(proxy_host) == IS_STRING &&
  2827. zend_hash_find(Z_OBJPROP_P(this_ptr), "_proxy_port", sizeof("_proxy_port"), (void **) &proxy_port) == SUCCESS &&
  2828. Z_TYPE_PP(proxy_port) == IS_LONG) {
  2829. zval str_port, *str_proxy;
  2830. smart_str proxy = {0};
  2831. str_port = **proxy_port;
  2832. zval_copy_ctor(&str_port);
  2833. convert_to_string(&str_port);
  2834. smart_str_appends(&proxy,"tcp://");
  2835. smart_str_appends(&proxy,Z_STRVAL_PP(proxy_host));
  2836. smart_str_appends(&proxy,":");
  2837. smart_str_appends(&proxy,Z_STRVAL(str_port));
  2838. smart_str_0(&proxy);
  2839. zval_dtor(&str_port);
  2840. MAKE_STD_ZVAL(str_proxy);
  2841. ZVAL_STRING(str_proxy, proxy.c, 1);
  2842. smart_str_free(&proxy);
  2843. if (!context) {
  2844. context = php_stream_context_alloc();
  2845. }
  2846. php_stream_context_set_option(context, "http", "proxy", str_proxy);
  2847. zval_ptr_dtor(&str_proxy);
  2848. if (uri_len < sizeof("https://")-1 ||
  2849. strncasecmp(uri, "https://", sizeof("https://")-1) != 0) {
  2850. MAKE_STD_ZVAL(str_proxy);
  2851. ZVAL_BOOL(str_proxy, 1);
  2852. php_stream_context_set_option(context, "http", "request_fulluri", str_proxy);
  2853. zval_ptr_dtor(&str_proxy);
  2854. }
  2855. proxy_authentication(this_ptr, &headers TSRMLS_CC);
  2856. }
  2857. basic_authentication(this_ptr, &headers TSRMLS_CC);
  2858. /* Use HTTP/1.1 with "Connection: close" by default */
  2859. if (php_stream_context_get_option(context, "http", "protocol_version", &tmp) == FAILURE) {
  2860. zval *http_version;
  2861. MAKE_STD_ZVAL(http_version);
  2862. ZVAL_DOUBLE(http_version, 1.1);
  2863. php_stream_context_set_option(context, "http", "protocol_version", http_version);
  2864. zval_ptr_dtor(&http_version);
  2865. smart_str_appendl(&headers, "Connection: close", sizeof("Connection: close")-1);
  2866. }
  2867. if (headers.len > 0) {
  2868. zval *str_headers;
  2869. if (!context) {
  2870. context = php_stream_context_alloc();
  2871. }
  2872. smart_str_0(&headers);
  2873. MAKE_STD_ZVAL(str_headers);
  2874. ZVAL_STRING(str_headers, headers.c, 1);
  2875. php_stream_context_set_option(context, "http", "header", str_headers);
  2876. smart_str_free(&headers);
  2877. zval_ptr_dtor(&str_headers);
  2878. }
  2879. if (context) {
  2880. MAKE_STD_ZVAL(new_context);
  2881. php_stream_context_to_zval(context, new_context);
  2882. orig_context = php_libxml_switch_context(new_context TSRMLS_CC);
  2883. }
  2884. SOAP_GLOBAL(error_code) = "WSDL";
  2885. sdl = load_wsdl(this_ptr, uri TSRMLS_CC);
  2886. if (sdl) {
  2887. sdl->is_persistent = 0;
  2888. }
  2889. SOAP_GLOBAL(error_code) = old_error_code;
  2890. if (context) {
  2891. php_libxml_switch_context(orig_context TSRMLS_CC);
  2892. zval_ptr_dtor(&new_context);
  2893. }
  2894. if ((cache_wsdl & WSDL_CACHE_DISK) && key) {
  2895. if (sdl) {
  2896. add_sdl_to_cache(key, uri, t, sdl TSRMLS_CC);
  2897. }
  2898. efree(key);
  2899. }
  2900. cache_in_memory:
  2901. if (cache_wsdl & WSDL_CACHE_MEMORY) {
  2902. if (sdl) {
  2903. sdlPtr psdl;
  2904. sdl_cache_bucket p;
  2905. if (SOAP_GLOBAL(mem_cache) == NULL) {
  2906. SOAP_GLOBAL(mem_cache) = malloc(sizeof(HashTable));
  2907. zend_hash_init(SOAP_GLOBAL(mem_cache), 0, NULL, delete_psdl, 1);
  2908. } else if (SOAP_GLOBAL(cache_limit) > 0 &&
  2909. SOAP_GLOBAL(cache_limit) <= zend_hash_num_elements(SOAP_GLOBAL(mem_cache))) {
  2910. /* in-memory cache overflow */
  2911. sdl_cache_bucket *q;
  2912. HashPosition pos;
  2913. time_t latest = t;
  2914. char *key = NULL;
  2915. uint key_len;
  2916. ulong idx;
  2917. for (zend_hash_internal_pointer_reset_ex(SOAP_GLOBAL(mem_cache), &pos);
  2918. zend_hash_get_current_data_ex(SOAP_GLOBAL(mem_cache), (void **) &q, &pos) == SUCCESS;
  2919. zend_hash_move_forward_ex(SOAP_GLOBAL(mem_cache), &pos)) {
  2920. if (q->time < latest) {
  2921. latest = q->time;
  2922. zend_hash_get_current_key_ex(SOAP_GLOBAL(mem_cache), &key, &key_len, &idx, 0, &pos);
  2923. }
  2924. }
  2925. if (key) {
  2926. zend_hash_del(SOAP_GLOBAL(mem_cache), key, key_len);
  2927. } else {
  2928. return sdl;
  2929. }
  2930. }
  2931. psdl = make_persistent_sdl(sdl TSRMLS_CC);
  2932. psdl->is_persistent = 1;
  2933. p.time = t;
  2934. p.sdl = psdl;
  2935. if (SUCCESS == zend_hash_update(SOAP_GLOBAL(mem_cache), uri,
  2936. uri_len+1, (void*)&p, sizeof(sdl_cache_bucket), NULL)) {
  2937. /* remove non-persitent sdl structure */
  2938. delete_sdl_impl(sdl);
  2939. /* and replace it with persistent one */
  2940. sdl = psdl;
  2941. } else {
  2942. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to register persistent entry");
  2943. /* clean up persistent sdl */
  2944. delete_psdl(&p);
  2945. /* keep non-persistent sdl and return it */
  2946. }
  2947. }
  2948. }
  2949. return sdl;
  2950. }
  2951. /* Deletes */
  2952. void delete_sdl_impl(void *handle)
  2953. {
  2954. sdlPtr tmp = (sdlPtr)handle;
  2955. zend_hash_destroy(&tmp->functions);
  2956. if (tmp->source) {
  2957. efree(tmp->source);
  2958. }
  2959. if (tmp->target_ns) {
  2960. efree(tmp->target_ns);
  2961. }
  2962. if (tmp->elements) {
  2963. zend_hash_destroy(tmp->elements);
  2964. efree(tmp->elements);
  2965. }
  2966. if (tmp->encoders) {
  2967. zend_hash_destroy(tmp->encoders);
  2968. efree(tmp->encoders);
  2969. }
  2970. if (tmp->types) {
  2971. zend_hash_destroy(tmp->types);
  2972. efree(tmp->types);
  2973. }
  2974. if (tmp->groups) {
  2975. zend_hash_destroy(tmp->groups);
  2976. efree(tmp->groups);
  2977. }
  2978. if (tmp->bindings) {
  2979. zend_hash_destroy(tmp->bindings);
  2980. efree(tmp->bindings);
  2981. }
  2982. if (tmp->requests) {
  2983. zend_hash_destroy(tmp->requests);
  2984. efree(tmp->requests);
  2985. }
  2986. efree(tmp);
  2987. }
  2988. void delete_sdl(void *handle)
  2989. {
  2990. sdlPtr tmp = (sdlPtr)handle;
  2991. if (!tmp->is_persistent) {
  2992. delete_sdl_impl(tmp);
  2993. }
  2994. }
  2995. static void delete_binding(void *data)
  2996. {
  2997. sdlBindingPtr binding = *((sdlBindingPtr*)data);
  2998. if (binding->location) {
  2999. efree(binding->location);
  3000. }
  3001. if (binding->name) {
  3002. efree(binding->name);
  3003. }
  3004. if (binding->bindingType == BINDING_SOAP) {
  3005. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3006. if (soapBind) {
  3007. efree(soapBind);
  3008. }
  3009. }
  3010. efree(binding);
  3011. }
  3012. static void delete_binding_persistent(void *data)
  3013. {
  3014. sdlBindingPtr binding = *((sdlBindingPtr*)data);
  3015. if (binding->location) {
  3016. free(binding->location);
  3017. }
  3018. if (binding->name) {
  3019. free(binding->name);
  3020. }
  3021. if (binding->bindingType == BINDING_SOAP) {
  3022. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3023. if (soapBind) {
  3024. free(soapBind);
  3025. }
  3026. }
  3027. free(binding);
  3028. }
  3029. static void delete_sdl_soap_binding_function_body(sdlSoapBindingFunctionBody body)
  3030. {
  3031. if (body.ns) {
  3032. efree(body.ns);
  3033. }
  3034. if (body.headers) {
  3035. zend_hash_destroy(body.headers);
  3036. efree(body.headers);
  3037. }
  3038. }
  3039. static void delete_sdl_soap_binding_function_body_persistent(sdlSoapBindingFunctionBody body)
  3040. {
  3041. if (body.ns) {
  3042. free(body.ns);
  3043. }
  3044. if (body.headers) {
  3045. zend_hash_destroy(body.headers);
  3046. free(body.headers);
  3047. }
  3048. }
  3049. static void delete_function(void *data)
  3050. {
  3051. sdlFunctionPtr function = *((sdlFunctionPtr*)data);
  3052. if (function->functionName) {
  3053. efree(function->functionName);
  3054. }
  3055. if (function->requestName) {
  3056. efree(function->requestName);
  3057. }
  3058. if (function->responseName) {
  3059. efree(function->responseName);
  3060. }
  3061. if (function->requestParameters) {
  3062. zend_hash_destroy(function->requestParameters);
  3063. efree(function->requestParameters);
  3064. }
  3065. if (function->responseParameters) {
  3066. zend_hash_destroy(function->responseParameters);
  3067. efree(function->responseParameters);
  3068. }
  3069. if (function->faults) {
  3070. zend_hash_destroy(function->faults);
  3071. efree(function->faults);
  3072. }
  3073. if (function->bindingAttributes &&
  3074. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3075. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3076. if (soapFunction->soapAction) {
  3077. efree(soapFunction->soapAction);
  3078. }
  3079. delete_sdl_soap_binding_function_body(soapFunction->input);
  3080. delete_sdl_soap_binding_function_body(soapFunction->output);
  3081. efree(soapFunction);
  3082. }
  3083. efree(function);
  3084. }
  3085. static void delete_function_persistent(void *data)
  3086. {
  3087. sdlFunctionPtr function = *((sdlFunctionPtr*)data);
  3088. if (function->functionName) {
  3089. free(function->functionName);
  3090. }
  3091. if (function->requestName) {
  3092. free(function->requestName);
  3093. }
  3094. if (function->responseName) {
  3095. free(function->responseName);
  3096. }
  3097. if (function->requestParameters) {
  3098. zend_hash_destroy(function->requestParameters);
  3099. free(function->requestParameters);
  3100. }
  3101. if (function->responseParameters) {
  3102. zend_hash_destroy(function->responseParameters);
  3103. free(function->responseParameters);
  3104. }
  3105. if (function->faults) {
  3106. zend_hash_destroy(function->faults);
  3107. free(function->faults);
  3108. }
  3109. if (function->bindingAttributes &&
  3110. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3111. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3112. if (soapFunction->soapAction) {
  3113. free(soapFunction->soapAction);
  3114. }
  3115. delete_sdl_soap_binding_function_body_persistent(soapFunction->input);
  3116. delete_sdl_soap_binding_function_body_persistent(soapFunction->output);
  3117. free(soapFunction);
  3118. }
  3119. free(function);
  3120. }
  3121. static void delete_parameter(void *data)
  3122. {
  3123. sdlParamPtr param = *((sdlParamPtr*)data);
  3124. if (param->paramName) {
  3125. efree(param->paramName);
  3126. }
  3127. efree(param);
  3128. }
  3129. static void delete_parameter_persistent(void *data)
  3130. {
  3131. sdlParamPtr param = *((sdlParamPtr*)data);
  3132. if (param->paramName) {
  3133. free(param->paramName);
  3134. }
  3135. free(param);
  3136. }
  3137. static void delete_header(void *data)
  3138. {
  3139. sdlSoapBindingFunctionHeaderPtr hdr = *((sdlSoapBindingFunctionHeaderPtr*)data);
  3140. if (hdr->name) {
  3141. efree(hdr->name);
  3142. }
  3143. if (hdr->ns) {
  3144. efree(hdr->ns);
  3145. }
  3146. if (hdr->headerfaults) {
  3147. zend_hash_destroy(hdr->headerfaults);
  3148. efree(hdr->headerfaults);
  3149. }
  3150. efree(hdr);
  3151. }
  3152. static void delete_header_persistent(void *data)
  3153. {
  3154. sdlSoapBindingFunctionHeaderPtr hdr = *((sdlSoapBindingFunctionHeaderPtr*)data);
  3155. if (hdr->name) {
  3156. free(hdr->name);
  3157. }
  3158. if (hdr->ns) {
  3159. free(hdr->ns);
  3160. }
  3161. if (hdr->headerfaults) {
  3162. zend_hash_destroy(hdr->headerfaults);
  3163. free(hdr->headerfaults);
  3164. }
  3165. free(hdr);
  3166. }
  3167. static void delete_fault(void *data)
  3168. {
  3169. sdlFaultPtr fault = *((sdlFaultPtr*)data);
  3170. if (fault->name) {
  3171. efree(fault->name);
  3172. }
  3173. if (fault->details) {
  3174. zend_hash_destroy(fault->details);
  3175. efree(fault->details);
  3176. }
  3177. if (fault->bindingAttributes) {
  3178. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3179. if (binding->ns) {
  3180. efree(binding->ns);
  3181. }
  3182. efree(fault->bindingAttributes);
  3183. }
  3184. efree(fault);
  3185. }
  3186. static void delete_fault_persistent(void *data)
  3187. {
  3188. sdlFaultPtr fault = *((sdlFaultPtr*)data);
  3189. if (fault->name) {
  3190. free(fault->name);
  3191. }
  3192. if (fault->details) {
  3193. zend_hash_destroy(fault->details);
  3194. free(fault->details);
  3195. }
  3196. if (fault->bindingAttributes) {
  3197. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3198. if (binding->ns) {
  3199. free(binding->ns);
  3200. }
  3201. free(fault->bindingAttributes);
  3202. }
  3203. free(fault);
  3204. }
  3205. static void delete_document(void *doc_ptr)
  3206. {
  3207. xmlDocPtr doc = *((xmlDocPtr*)doc_ptr);
  3208. xmlFreeDoc(doc);
  3209. }