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