xref: /PHP-5.4/ext/soap/php_encoding.c (revision 75f40ae1)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Brad Lafountain <rodif_bl@yahoo.com>                        |
16   |          Shane Caraveo <shane@caraveo.com>                           |
17   |          Dmitry Stogov <dmitry@zend.com>                             |
18   +----------------------------------------------------------------------+
19 */
20 /* $Id$ */
21 
22 #include <time.h>
23 
24 #include "php_soap.h"
25 #include "ext/libxml/php_libxml.h"
26 #include "ext/standard/base64.h"
27 #include <libxml/parserInternals.h>
28 #include "zend_strtod.h"
29 #include "zend_interfaces.h"
30 
31 /* zval type decode */
32 static zval *to_zval_double(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
33 static zval *to_zval_long(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
34 static zval *to_zval_bool(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
35 static zval *to_zval_string(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
36 static zval *to_zval_stringr(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
37 static zval *to_zval_stringc(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
38 static zval *to_zval_map(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
39 static zval *to_zval_null(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
40 static zval *to_zval_base64(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
41 static zval *to_zval_hexbin(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
42 
43 static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
44 static xmlNodePtr to_xml_double(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
45 static xmlNodePtr to_xml_bool(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
46 
47 /* String encode */
48 static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
49 static xmlNodePtr to_xml_base64(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
50 static xmlNodePtr to_xml_hexbin(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
51 
52 /* Null encode */
53 static xmlNodePtr to_xml_null(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
54 
55 /* Array encode */
56 static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
57 static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
58 
59 static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
60 static xmlNodePtr to_xml_list1(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
61 
62 /* Datetime encode/decode */
63 static xmlNodePtr to_xml_datetime_ex(encodeTypePtr type, zval *data, char *format, int style, xmlNodePtr parent TSRMLS_DC);
64 static xmlNodePtr to_xml_datetime(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
65 static xmlNodePtr to_xml_time(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
66 static xmlNodePtr to_xml_date(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
67 static xmlNodePtr to_xml_gyearmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
68 static xmlNodePtr to_xml_gyear(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
69 static xmlNodePtr to_xml_gmonthday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
70 static xmlNodePtr to_xml_gday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
71 static xmlNodePtr to_xml_gmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
72 static xmlNodePtr to_xml_duration(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
73 
74 static zval *to_zval_object(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
75 static zval *to_zval_array(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
76 
77 static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
78 static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
79 
80 static zval *to_zval_any(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
81 static xmlNodePtr to_xml_any(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
82 
83 /* Try and guess for non-wsdl clients and servers */
84 static zval *guess_zval_convert(encodeTypePtr type, xmlNodePtr data TSRMLS_DC);
85 static xmlNodePtr guess_xml_convert(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC);
86 
87 static int is_map(zval *array);
88 static encodePtr get_array_type(xmlNodePtr node, zval *array, smart_str *out_type TSRMLS_DC);
89 
90 static xmlNodePtr check_and_resolve_href(xmlNodePtr data);
91 
92 static void set_ns_prop(xmlNodePtr node, char *ns, char *name, char *val);
93 static void set_xsi_nil(xmlNodePtr node);
94 static void set_xsi_type(xmlNodePtr node, char *type);
95 
96 static void get_type_str(xmlNodePtr node, const char* ns, const char* type, smart_str* ret);
97 static void set_ns_and_type_ex(xmlNodePtr node, char *ns, char *type);
98 
99 static void set_ns_and_type(xmlNodePtr node, encodeTypePtr type);
100 
101 #define FIND_XML_NULL(xml,zval) \
102 	{ \
103 		xmlAttrPtr null; \
104 		if (!xml) { \
105 			ZVAL_NULL(zval); \
106 			return zval; \
107 		} \
108 		if (xml->properties) { \
109 			null = get_attribute(xml->properties, "nil"); \
110 			if (null) { \
111 				ZVAL_NULL(zval); \
112 				return zval; \
113 			} \
114 		} \
115 	}
116 
117 #define CHECK_XML_NULL(xml) \
118 	{ \
119 		xmlAttrPtr null; \
120 		if (!xml) { \
121 			zval *ret; \
122 			ALLOC_INIT_ZVAL(ret); \
123 			ZVAL_NULL(ret); \
124 			return ret; \
125 		} \
126 		if (xml->properties) { \
127 			null = get_attribute(xml->properties, "nil"); \
128 			if (null) { \
129 				zval *ret; \
130 				ALLOC_INIT_ZVAL(ret); \
131 				ZVAL_NULL(ret); \
132 				return ret; \
133 			} \
134 		} \
135 	}
136 
137 #define FIND_ZVAL_NULL(zval, xml, style) \
138 { \
139 	if (!zval || Z_TYPE_P(zval) == IS_NULL) { \
140 	  if (style == SOAP_ENCODED) {\
141 			set_xsi_nil(xml); \
142 		} \
143 		return xml; \
144 	} \
145 }
146 
147 encode defaultEncoding[] = {
148 	{{UNKNOWN_TYPE, NULL, NULL, NULL}, guess_zval_convert, guess_xml_convert},
149 
150 	{{IS_NULL, "nil", XSI_NAMESPACE, NULL}, to_zval_null, to_xml_null},
151 	{{IS_STRING, XSD_STRING_STRING, XSD_NAMESPACE, NULL}, to_zval_string, to_xml_string},
152 	{{IS_LONG, XSD_INT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
153 	{{IS_DOUBLE, XSD_FLOAT_STRING, XSD_NAMESPACE, NULL}, to_zval_double, to_xml_double},
154 	{{IS_BOOL, XSD_BOOLEAN_STRING, XSD_NAMESPACE, NULL}, to_zval_bool, to_xml_bool},
155 	{{IS_CONSTANT, XSD_STRING_STRING, XSD_NAMESPACE, NULL}, to_zval_string, to_xml_string},
156 	{{IS_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_array, guess_array_map},
157 	{{IS_CONSTANT_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_array, to_xml_array},
158 	{{IS_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
159 	{{IS_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_array, guess_array_map},
160 	{{IS_CONSTANT_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_array, to_xml_array},
161 	{{IS_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
162 
163 	{{XSD_STRING, XSD_STRING_STRING, XSD_NAMESPACE, NULL}, to_zval_string, to_xml_string},
164 	{{XSD_BOOLEAN, XSD_BOOLEAN_STRING, XSD_NAMESPACE, NULL}, to_zval_bool, to_xml_bool},
165 	{{XSD_DECIMAL, XSD_DECIMAL_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
166 	{{XSD_FLOAT, XSD_FLOAT_STRING, XSD_NAMESPACE, NULL}, to_zval_double, to_xml_double},
167 	{{XSD_DOUBLE, XSD_DOUBLE_STRING, XSD_NAMESPACE, NULL}, to_zval_double, to_xml_double},
168 
169 	{{XSD_DATETIME, XSD_DATETIME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_datetime},
170 	{{XSD_TIME, XSD_TIME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_time},
171 	{{XSD_DATE, XSD_DATE_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_date},
172 	{{XSD_GYEARMONTH, XSD_GYEARMONTH_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gyearmonth},
173 	{{XSD_GYEAR, XSD_GYEAR_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gyear},
174 	{{XSD_GMONTHDAY, XSD_GMONTHDAY_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gmonthday},
175 	{{XSD_GDAY, XSD_GDAY_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gday},
176 	{{XSD_GMONTH, XSD_GMONTH_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_gmonth},
177 	{{XSD_DURATION, XSD_DURATION_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_duration},
178 
179 	{{XSD_HEXBINARY, XSD_HEXBINARY_STRING, XSD_NAMESPACE, NULL}, to_zval_hexbin, to_xml_hexbin},
180 	{{XSD_BASE64BINARY, XSD_BASE64BINARY_STRING, XSD_NAMESPACE, NULL}, to_zval_base64, to_xml_base64},
181 
182 	{{XSD_LONG, XSD_LONG_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
183 	{{XSD_INT, XSD_INT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
184 	{{XSD_SHORT, XSD_SHORT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
185 	{{XSD_BYTE, XSD_BYTE_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
186 	{{XSD_NONPOSITIVEINTEGER, XSD_NONPOSITIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
187 	{{XSD_POSITIVEINTEGER, XSD_POSITIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
188 	{{XSD_NONNEGATIVEINTEGER, XSD_NONNEGATIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
189 	{{XSD_NEGATIVEINTEGER, XSD_NEGATIVEINTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
190 	{{XSD_UNSIGNEDBYTE, XSD_UNSIGNEDBYTE_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
191 	{{XSD_UNSIGNEDSHORT, XSD_UNSIGNEDSHORT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
192 	{{XSD_UNSIGNEDINT, XSD_UNSIGNEDINT_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
193 	{{XSD_UNSIGNEDLONG, XSD_UNSIGNEDLONG_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
194 	{{XSD_INTEGER, XSD_INTEGER_STRING, XSD_NAMESPACE, NULL}, to_zval_long, to_xml_long},
195 
196 	{{XSD_ANYTYPE, XSD_ANYTYPE_STRING, XSD_NAMESPACE, NULL}, guess_zval_convert, guess_xml_convert},
197 	{{XSD_UR_TYPE, XSD_UR_TYPE_STRING, XSD_NAMESPACE, NULL}, guess_zval_convert, guess_xml_convert},
198 	{{XSD_ANYURI, XSD_ANYURI_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
199 	{{XSD_QNAME, XSD_QNAME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
200 	{{XSD_NOTATION, XSD_NOTATION_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
201 	{{XSD_NORMALIZEDSTRING, XSD_NORMALIZEDSTRING_STRING, XSD_NAMESPACE, NULL}, to_zval_stringr, to_xml_string},
202 	{{XSD_TOKEN, XSD_TOKEN_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
203 	{{XSD_LANGUAGE, XSD_LANGUAGE_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
204 	{{XSD_NMTOKEN, XSD_NMTOKEN_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
205 	{{XSD_NMTOKENS, XSD_NMTOKENS_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_list1},
206 	{{XSD_NAME, XSD_NAME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
207 	{{XSD_NCNAME, XSD_NCNAME_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
208 	{{XSD_ID, XSD_ID_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
209 	{{XSD_IDREF, XSD_IDREF_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
210 	{{XSD_IDREFS, XSD_IDREFS_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_list1},
211 	{{XSD_ENTITY, XSD_ENTITY_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
212 	{{XSD_ENTITIES, XSD_ENTITIES_STRING, XSD_NAMESPACE, NULL}, to_zval_stringc, to_xml_list1},
213 
214 	{{APACHE_MAP, APACHE_MAP_STRING, APACHE_NAMESPACE, NULL}, to_zval_map, to_xml_map},
215 
216 	{{SOAP_ENC_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
217 	{{SOAP_ENC_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_1_ENC_NAMESPACE, NULL}, to_zval_array, to_xml_array},
218 	{{SOAP_ENC_OBJECT, SOAP_ENC_OBJECT_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_object, to_xml_object},
219 	{{SOAP_ENC_ARRAY, SOAP_ENC_ARRAY_STRING, SOAP_1_2_ENC_NAMESPACE, NULL}, to_zval_array, to_xml_array},
220 
221 	/* support some of the 1999 data types */
222 	{{XSD_STRING, XSD_STRING_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_string, to_xml_string},
223 	{{XSD_BOOLEAN, XSD_BOOLEAN_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_bool, to_xml_bool},
224 	{{XSD_DECIMAL, XSD_DECIMAL_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
225 	{{XSD_FLOAT, XSD_FLOAT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_double, to_xml_double},
226 	{{XSD_DOUBLE, XSD_DOUBLE_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_double, to_xml_double},
227 
228 	{{XSD_LONG, XSD_LONG_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
229 	{{XSD_INT, XSD_INT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
230 	{{XSD_SHORT, XSD_SHORT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
231 	{{XSD_BYTE, XSD_BYTE_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_long, to_xml_long},
232 	{{XSD_1999_TIMEINSTANT, XSD_1999_TIMEINSTANT_STRING, XSD_1999_NAMESPACE, NULL}, to_zval_stringc, to_xml_string},
233 
234 	{{XSD_ANYXML, "<anyXML>", "<anyXML>", NULL}, to_zval_any, to_xml_any},
235 
236 	{{END_KNOWN_TYPES, NULL, NULL, NULL}, guess_zval_convert, guess_xml_convert}
237 };
238 
239 int numDefaultEncodings = sizeof(defaultEncoding)/sizeof(encode);
240 
241 
whiteSpace_replace(xmlChar * str)242 void whiteSpace_replace(xmlChar* str)
243 {
244 	while (*str != '\0') {
245 		if (*str == '\x9' || *str == '\xA' || *str == '\xD') {
246 			*str = ' ';
247 		}
248 		str++;
249 	}
250 }
251 
whiteSpace_collapse(xmlChar * str)252 void whiteSpace_collapse(xmlChar* str)
253 {
254 	xmlChar *pos;
255 	xmlChar old;
256 
257 	pos = str;
258 	whiteSpace_replace(str);
259 	while (*str == ' ') {
260 		str++;
261 	}
262 	old = '\0';
263 	while (*str != '\0') {
264 		if (*str != ' ' || old != ' ') {
265 			*pos = *str;
266 			pos++;
267 		}
268 		old = *str;
269 		str++;
270 	}
271 	if (old == ' ') {
272 	 	--pos;
273 	}
274 	*pos = '\0';
275 }
276 
find_encoder_by_type_name(sdlPtr sdl,const char * type)277 static encodePtr find_encoder_by_type_name(sdlPtr sdl, const char *type)
278 {
279 	if (sdl && sdl->encoders) {
280 		HashPosition pos;
281 		encodePtr *enc;
282 
283 		for (zend_hash_internal_pointer_reset_ex(sdl->encoders, &pos);
284 		     zend_hash_get_current_data_ex(sdl->encoders, (void **) &enc, &pos) == SUCCESS;
285 		     zend_hash_move_forward_ex(sdl->encoders, &pos)) {
286 		    if (strcmp((*enc)->details.type_str, type) == 0) {
287 				return *enc;
288 			}
289 		}
290 	}
291 	return NULL;
292 }
293 
soap_check_zval_ref(zval * data,xmlNodePtr node TSRMLS_DC)294 static zend_bool soap_check_zval_ref(zval *data, xmlNodePtr node TSRMLS_DC) {
295 	xmlNodePtr *node_ptr;
296 
297 	if (SOAP_GLOBAL(ref_map)) {
298 		if (Z_TYPE_P(data) == IS_OBJECT) {
299 			data = (zval*)zend_objects_get_address(data TSRMLS_CC);
300 		}
301 		if (zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)data, (void**)&node_ptr) == SUCCESS) {
302 			xmlAttrPtr attr = (*node_ptr)->properties;
303 			char *id;
304 			smart_str prefix = {0};
305 
306 			if (*node_ptr == node) {
307 				return 0;
308 			}
309 			xmlNodeSetName(node, (*node_ptr)->name);
310 			xmlSetNs(node, (*node_ptr)->ns);
311 			if (SOAP_GLOBAL(soap_version) == SOAP_1_1) {
312 				while (1) {
313 					attr = get_attribute(attr, "id");
314 					if (attr == NULL || attr->ns == NULL) {
315 						break;
316 					}
317 					attr = attr->next;
318 				}
319 				if (attr) {
320 					id = (char*)attr->children->content;
321 					smart_str_appendc(&prefix, '#');
322 					smart_str_appends(&prefix, id);
323 					smart_str_0(&prefix);
324 					id = prefix.c;
325 				} else {
326 					SOAP_GLOBAL(cur_uniq_ref)++;
327 					smart_str_appendl(&prefix, "#ref", 4);
328 					smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
329 					smart_str_0(&prefix);
330 					id = prefix.c;
331 					xmlSetProp((*node_ptr), BAD_CAST("id"), BAD_CAST(id+1));
332 				}
333 				xmlSetProp(node, BAD_CAST("href"), BAD_CAST(id));
334 			} else {
335 				attr = get_attribute_ex(attr, "id", SOAP_1_2_ENC_NAMESPACE);
336 				if (attr) {
337 					id = (char*)attr->children->content;
338 					smart_str_appendc(&prefix, '#');
339 					smart_str_appends(&prefix, id);
340 					smart_str_0(&prefix);
341 					id = prefix.c;
342 				} else {
343 					SOAP_GLOBAL(cur_uniq_ref)++;
344 					smart_str_appendl(&prefix, "#ref", 4);
345 					smart_str_append_long(&prefix, SOAP_GLOBAL(cur_uniq_ref));
346 					smart_str_0(&prefix);
347 					id = prefix.c;
348 					set_ns_prop((*node_ptr), SOAP_1_2_ENC_NAMESPACE, "id", id+1);
349 				}
350 				set_ns_prop(node, SOAP_1_2_ENC_NAMESPACE, "ref", id);
351 			}
352 			smart_str_free(&prefix);
353 			return 1;
354 		} else {
355 			zend_hash_index_update(SOAP_GLOBAL(ref_map), (ulong)data, (void**)&node, sizeof(xmlNodePtr), NULL);
356 		}
357 	}
358 	return 0;
359 }
360 
soap_find_xml_ref(xmlNodePtr node TSRMLS_DC)361 static zval* soap_find_xml_ref(xmlNodePtr node TSRMLS_DC)
362 {
363 	zval **data_ptr;
364 
365 	if (SOAP_GLOBAL(ref_map) &&
366 	    zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)node, (void**)&data_ptr) == SUCCESS) {
367 		Z_SET_ISREF_PP(data_ptr);
368 		Z_ADDREF_PP(data_ptr);
369 		return *data_ptr;
370 	}
371 	return NULL;
372 }
373 
soap_check_xml_ref(zval ** data,xmlNodePtr node TSRMLS_DC)374 static zend_bool soap_check_xml_ref(zval **data, xmlNodePtr node TSRMLS_DC)
375 {
376 	zval **data_ptr;
377 
378 	if (SOAP_GLOBAL(ref_map)) {
379 		if (zend_hash_index_find(SOAP_GLOBAL(ref_map), (ulong)node, (void**)&data_ptr) == SUCCESS) {
380 			if (*data != *data_ptr) {
381 				zval_ptr_dtor(data);
382 				*data = *data_ptr;
383 				Z_SET_ISREF_PP(data);
384 				Z_ADDREF_PP(data);
385 				return 1;
386 			}
387 		} else {
388 			zend_hash_index_update(SOAP_GLOBAL(ref_map), (ulong)node, (void**)data, sizeof(zval*), NULL);
389 		}
390 	}
391 	return 0;
392 }
393 
master_to_xml_int(encodePtr encode,zval * data,int style,xmlNodePtr parent,int check_class_map TSRMLS_DC)394 static xmlNodePtr master_to_xml_int(encodePtr encode, zval *data, int style, xmlNodePtr parent, int check_class_map TSRMLS_DC)
395 {
396 	xmlNodePtr node = NULL;
397 	int add_type = 0;
398 
399 	/* Special handling of class SoapVar */
400 	if (data &&
401 	    Z_TYPE_P(data) == IS_OBJECT &&
402 	    Z_OBJCE_P(data) == soap_var_class_entry) {
403 		zval **ztype, **zdata, **zns, **zstype, **zname, **znamens;
404 		encodePtr enc = NULL;
405 		HashTable *ht = Z_OBJPROP_P(data);
406 
407 		if (zend_hash_find(ht, "enc_type", sizeof("enc_type"), (void **)&ztype) == FAILURE ||
408 		    Z_TYPE_PP(ztype) != IS_LONG) {
409 			soap_error0(E_ERROR, "Encoding: SoapVar has no 'enc_type' property");
410 		}
411 
412 		if (zend_hash_find(ht, "enc_stype", sizeof("enc_stype"), (void **)&zstype) == SUCCESS &&
413 		    Z_TYPE_PP(zstype) == IS_STRING) {
414 			if (zend_hash_find(ht, "enc_ns", sizeof("enc_ns"), (void **)&zns) == SUCCESS &&
415 			    Z_TYPE_PP(zns) == IS_STRING) {
416 				enc = get_encoder(SOAP_GLOBAL(sdl), Z_STRVAL_PP(zns), Z_STRVAL_PP(zstype));
417 			} else {
418 				zns = NULL;
419 				enc = get_encoder_ex(SOAP_GLOBAL(sdl), Z_STRVAL_PP(zstype), Z_STRLEN_PP(zstype));
420 			}
421 			if (enc == NULL && SOAP_GLOBAL(typemap)) {
422 				encodePtr *new_enc;
423 				smart_str nscat = {0};
424 
425 				if (zns != NULL) {
426 					smart_str_appendl(&nscat, Z_STRVAL_PP(zns), Z_STRLEN_PP(zns));
427 					smart_str_appendc(&nscat, ':');
428 				}
429 				smart_str_appendl(&nscat, Z_STRVAL_PP(zstype), Z_STRLEN_PP(zstype));
430 				smart_str_0(&nscat);
431 				if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
432 					enc = *new_enc;
433 				}
434 				smart_str_free(&nscat);
435 			}
436 		}
437 		if (enc == NULL) {
438 			enc = get_conversion(Z_LVAL_P(*ztype));
439 		}
440 		if (enc == NULL) {
441 			enc = encode;
442 		}
443 
444 		if (zend_hash_find(ht, "enc_value", sizeof("enc_value"), (void **)&zdata) == FAILURE) {
445 			node = master_to_xml(enc, NULL, style, parent TSRMLS_CC);
446 		} else {
447 			node = master_to_xml(enc, *zdata, style, parent TSRMLS_CC);
448 		}
449 
450 		if (style == SOAP_ENCODED || (SOAP_GLOBAL(sdl) && encode != enc)) {
451 			if (zend_hash_find(ht, "enc_stype", sizeof("enc_stype"), (void **)&zstype) == SUCCESS &&
452 			    Z_TYPE_PP(zstype) == IS_STRING) {
453 				if (zend_hash_find(ht, "enc_ns", sizeof("enc_ns"), (void **)&zns) == SUCCESS &&
454 				    Z_TYPE_PP(zns) == IS_STRING) {
455 					set_ns_and_type_ex(node, Z_STRVAL_PP(zns), Z_STRVAL_PP(zstype));
456 				} else {
457 					set_ns_and_type_ex(node, NULL, Z_STRVAL_PP(zstype));
458 				}
459 			}
460 		}
461 
462 		if (zend_hash_find(ht, "enc_name", sizeof("enc_name"), (void **)&zname) == SUCCESS &&
463 		    Z_TYPE_PP(zname) == IS_STRING) {
464 			xmlNodeSetName(node, BAD_CAST(Z_STRVAL_PP(zname)));
465 		}
466 		if (zend_hash_find(ht, "enc_namens", sizeof("enc_namens"), (void **)&znamens) == SUCCESS &&
467 		    Z_TYPE_PP(znamens) == IS_STRING) {
468 			xmlNsPtr nsp = encode_add_ns(node, Z_STRVAL_PP(znamens));
469 			xmlSetNs(node, nsp);
470 		}
471 	} else {
472 		if (check_class_map && SOAP_GLOBAL(class_map) && data &&
473 		    Z_TYPE_P(data) == IS_OBJECT &&
474 		    !Z_OBJPROP_P(data)->nApplyCount) {
475 			zend_class_entry *ce = Z_OBJCE_P(data);
476 			HashPosition pos;
477 			zval **tmp;
478 			char *type_name = NULL;
479 			uint type_len;
480 			ulong idx;
481 
482 			for (zend_hash_internal_pointer_reset_ex(SOAP_GLOBAL(class_map), &pos);
483 			     zend_hash_get_current_data_ex(SOAP_GLOBAL(class_map), (void **) &tmp, &pos) == SUCCESS;
484 			     zend_hash_move_forward_ex(SOAP_GLOBAL(class_map), &pos)) {
485 				if (Z_TYPE_PP(tmp) == IS_STRING &&
486 				    ce->name_length == Z_STRLEN_PP(tmp) &&
487 				    zend_binary_strncasecmp(ce->name, ce->name_length, Z_STRVAL_PP(tmp), ce->name_length, ce->name_length) == 0 &&
488 				    zend_hash_get_current_key_ex(SOAP_GLOBAL(class_map), &type_name, &type_len, &idx, 0, &pos) == HASH_KEY_IS_STRING) {
489 
490 					/* TODO: namespace isn't stored */
491 					encodePtr enc = NULL;
492 					if (SOAP_GLOBAL(sdl)) {
493 						enc = get_encoder(SOAP_GLOBAL(sdl), SOAP_GLOBAL(sdl)->target_ns, type_name);
494 						if (!enc) {
495 							enc = find_encoder_by_type_name(SOAP_GLOBAL(sdl), type_name);
496 						}
497 					}
498 					if (enc) {
499 						if (encode != enc && style == SOAP_LITERAL) {
500 							add_type = 1;
501 						}
502 						encode = enc;
503 					}
504 					break;
505 				}
506 			}
507 		}
508 
509 		if (encode == NULL) {
510 			encode = get_conversion(UNKNOWN_TYPE);
511 		}
512 		if (SOAP_GLOBAL(typemap) && encode->details.type_str) {
513 			smart_str nscat = {0};
514 			encodePtr *new_enc;
515 
516 			if (encode->details.ns) {
517 				smart_str_appends(&nscat, encode->details.ns);
518 				smart_str_appendc(&nscat, ':');
519 			}
520 			smart_str_appends(&nscat, encode->details.type_str);
521 			smart_str_0(&nscat);
522 			if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
523 				encode = *new_enc;
524 			}
525 			smart_str_free(&nscat);
526 		}
527 		if (encode->to_xml) {
528 			node = encode->to_xml(&encode->details, data, style, parent TSRMLS_CC);
529 			if (add_type) {
530 				set_ns_and_type(node, &encode->details);
531 			}
532 		}
533 	}
534 	return node;
535 }
536 
master_to_xml(encodePtr encode,zval * data,int style,xmlNodePtr parent TSRMLS_DC)537 xmlNodePtr master_to_xml(encodePtr encode, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
538 {
539 	return master_to_xml_int(encode, data, style, parent, 1 TSRMLS_CC);
540 }
541 
master_to_zval_int(encodePtr encode,xmlNodePtr data TSRMLS_DC)542 static zval *master_to_zval_int(encodePtr encode, xmlNodePtr data TSRMLS_DC)
543 {
544 	zval *ret = NULL;
545 
546 	if (SOAP_GLOBAL(typemap)) {
547 		if (encode->details.type_str) {
548 			smart_str nscat = {0};
549 			encodePtr *new_enc;
550 
551 			if (encode->details.ns) {
552 				smart_str_appends(&nscat, encode->details.ns);
553 				smart_str_appendc(&nscat, ':');
554 			}
555 			smart_str_appends(&nscat, encode->details.type_str);
556 			smart_str_0(&nscat);
557 			if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
558 				encode = *new_enc;
559 			}
560 			smart_str_free(&nscat);
561 		} else {
562 			xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
563 
564 			if (type_attr != NULL) {
565 				encodePtr *new_enc;
566 				xmlNsPtr nsptr;
567 				char *ns, *cptype;
568 				smart_str nscat = {0};
569 
570 				parse_namespace(type_attr->children->content, &cptype, &ns);
571 				nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
572 				if (nsptr != NULL) {
573 					smart_str_appends(&nscat, (char*)nsptr->href);
574 					smart_str_appendc(&nscat, ':');
575 				}
576 				smart_str_appends(&nscat, cptype);
577 				smart_str_0(&nscat);
578 				efree(cptype);
579 				if (ns) {efree(ns);}
580 				if (zend_hash_find(SOAP_GLOBAL(typemap), nscat.c, nscat.len + 1, (void**)&new_enc) == SUCCESS) {
581 					encode = *new_enc;
582 				}
583 				smart_str_free(&nscat);
584 			}
585 		}
586 	}
587 	if (encode->to_zval) {
588 		ret = encode->to_zval(&encode->details, data TSRMLS_CC);
589 	}
590 	return ret;
591 }
592 
master_to_zval(encodePtr encode,xmlNodePtr data TSRMLS_DC)593 zval *master_to_zval(encodePtr encode, xmlNodePtr data TSRMLS_DC)
594 {
595 	data = check_and_resolve_href(data);
596 
597 	if (encode == NULL) {
598 		encode = get_conversion(UNKNOWN_TYPE);
599 	} else {
600 		/* Use xsi:type if it is defined */
601 		xmlAttrPtr type_attr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
602 
603 		if (type_attr != NULL) {
604 			encodePtr  enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, type_attr->children->content);
605 
606 			if (enc != NULL && enc != encode) {
607 			  encodePtr tmp = enc;
608 			  while (tmp &&
609 			         tmp->details.sdl_type != NULL &&
610 			         tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
611 			    if (enc == tmp->details.sdl_type->encode ||
612 			        tmp == tmp->details.sdl_type->encode) {
613 			    	enc = NULL;
614 			    	break;
615 			    }
616 			    tmp = tmp->details.sdl_type->encode;
617 			  }
618 			  if (enc != NULL) {
619 			    encode = enc;
620 			  }
621 			}
622 		}
623 	}
624 	return master_to_zval_int(encode, data TSRMLS_CC);
625 }
626 
to_xml_user(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)627 xmlNodePtr to_xml_user(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
628 {
629 	xmlNodePtr ret = NULL;
630 	zval *return_value;
631 
632 	if (type && type->map && type->map->to_xml) {
633 		MAKE_STD_ZVAL(return_value);
634 
635 		if (call_user_function(EG(function_table), NULL, type->map->to_xml, return_value, 1, &data TSRMLS_CC) == FAILURE) {
636 			soap_error0(E_ERROR, "Encoding: Error calling to_xml callback");
637 		}
638 		if (Z_TYPE_P(return_value) == IS_STRING) {
639 			xmlDocPtr doc = soap_xmlParseMemory(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
640 			if (doc && doc->children) {
641 				ret = xmlDocCopyNode(doc->children, parent->doc, 1);
642 			}
643 			xmlFreeDoc(doc);
644 		}
645 
646 		zval_ptr_dtor(&return_value);
647 	}
648 	if (!ret) {
649 		ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
650 	}
651 	xmlAddChild(parent, ret);
652 	if (style == SOAP_ENCODED) {
653 		set_ns_and_type(ret, type);
654 	}
655 	return ret;
656 }
657 
to_zval_user(encodeTypePtr type,xmlNodePtr node TSRMLS_DC)658 zval *to_zval_user(encodeTypePtr type, xmlNodePtr node TSRMLS_DC)
659 {
660 	zval *return_value;
661 
662 	if (type && type->map && type->map->to_zval) {
663 		xmlBufferPtr buf;
664 		zval *data;
665 		xmlNodePtr copy;
666 
667 		copy = xmlCopyNode(node, 1);
668 		buf = xmlBufferCreate();
669 		xmlNodeDump(buf, NULL, copy, 0, 0);
670 		MAKE_STD_ZVAL(data);
671 		ZVAL_STRING(data, (char*)xmlBufferContent(buf), 1);
672 		xmlBufferFree(buf);
673 		xmlFreeNode(copy);
674 
675 		ALLOC_INIT_ZVAL(return_value);
676 
677 		if (call_user_function(EG(function_table), NULL, type->map->to_zval, return_value, 1, &data TSRMLS_CC) == FAILURE) {
678 			soap_error0(E_ERROR, "Encoding: Error calling from_xml callback");
679 		}
680 		zval_ptr_dtor(&data);
681 	} else {
682 		ALLOC_INIT_ZVAL(return_value);
683 	}
684 	return return_value;
685 }
686 
687 /* TODO: get rid of "bogus".. ither by passing in the already created xmlnode or passing in the node name */
688 /* String encode/decode */
to_zval_string(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)689 static zval *to_zval_string(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
690 {
691 	zval *ret;
692 	MAKE_STD_ZVAL(ret);
693 	FIND_XML_NULL(data, ret);
694 	if (data && data->children) {
695 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
696 			if (SOAP_GLOBAL(encoding) != NULL) {
697 				xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
698 				xmlBufferPtr out = xmlBufferCreate();
699 				int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
700 
701 				if (n >= 0) {
702 					ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
703 				} else {
704 					ZVAL_STRING(ret, (char*)data->children->content, 1);
705 				}
706 				xmlBufferFree(out);
707 				xmlBufferFree(in);
708 			} else {
709 				ZVAL_STRING(ret, (char*)data->children->content, 1);
710 			}
711 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
712 			ZVAL_STRING(ret, (char*)data->children->content, 1);
713 		} else {
714 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
715 		}
716 	} else {
717 		ZVAL_EMPTY_STRING(ret);
718 	}
719 	return ret;
720 }
721 
to_zval_stringr(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)722 static zval *to_zval_stringr(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
723 {
724 	zval *ret;
725 	MAKE_STD_ZVAL(ret);
726 	FIND_XML_NULL(data, ret);
727 	if (data && data->children) {
728 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
729 			whiteSpace_replace(data->children->content);
730 			if (SOAP_GLOBAL(encoding) != NULL) {
731 				xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
732 				xmlBufferPtr out = xmlBufferCreate();
733 				int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
734 
735 				if (n >= 0) {
736 					ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
737 				} else {
738 					ZVAL_STRING(ret, (char*)data->children->content, 1);
739 				}
740 				xmlBufferFree(out);
741 				xmlBufferFree(in);
742 			} else {
743 				ZVAL_STRING(ret, (char*)data->children->content, 1);
744 			}
745 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
746 			ZVAL_STRING(ret, (char*)data->children->content, 1);
747 		} else {
748 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
749 		}
750 	} else {
751 		ZVAL_EMPTY_STRING(ret);
752 	}
753 	return ret;
754 }
755 
to_zval_stringc(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)756 static zval *to_zval_stringc(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
757 {
758 	zval *ret;
759 	MAKE_STD_ZVAL(ret);
760 	FIND_XML_NULL(data, ret);
761 	if (data && data->children) {
762 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
763 			whiteSpace_collapse(data->children->content);
764 			if (SOAP_GLOBAL(encoding) != NULL) {
765 				xmlBufferPtr in  = xmlBufferCreateStatic(data->children->content, xmlStrlen(data->children->content));
766 				xmlBufferPtr out = xmlBufferCreate();
767 				int n = xmlCharEncOutFunc(SOAP_GLOBAL(encoding), out, in);
768 
769 				if (n >= 0) {
770 					ZVAL_STRING(ret, (char*)xmlBufferContent(out), 1);
771 				} else {
772 					ZVAL_STRING(ret, (char*)data->children->content, 1);
773 				}
774 				xmlBufferFree(out);
775 				xmlBufferFree(in);
776 			} else {
777 				ZVAL_STRING(ret, (char*)data->children->content, 1);
778 			}
779 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
780 			ZVAL_STRING(ret, (char*)data->children->content, 1);
781 		} else {
782 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
783 		}
784 	} else {
785 		ZVAL_EMPTY_STRING(ret);
786 	}
787 	return ret;
788 }
789 
to_zval_base64(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)790 static zval *to_zval_base64(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
791 {
792 	zval *ret;
793 	char *str;
794 	int str_len;
795 
796 	MAKE_STD_ZVAL(ret);
797 	FIND_XML_NULL(data, ret);
798 	if (data && data->children) {
799 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
800 			whiteSpace_collapse(data->children->content);
801 			str = (char*)php_base64_decode(data->children->content, strlen((char*)data->children->content), &str_len);
802 			if (!str) {
803 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
804 			}
805 			ZVAL_STRINGL(ret, str, str_len, 0);
806 		} else if (data->children->type == XML_CDATA_SECTION_NODE && data->children->next == NULL) {
807 			str = (char*)php_base64_decode(data->children->content, strlen((char*)data->children->content), &str_len);
808 			if (!str) {
809 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
810 			}
811 			ZVAL_STRINGL(ret, str, str_len, 0);
812 		} else {
813 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
814 		}
815 	} else {
816 		ZVAL_EMPTY_STRING(ret);
817 	}
818 	return ret;
819 }
820 
to_zval_hexbin(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)821 static zval *to_zval_hexbin(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
822 {
823 	zval *ret;
824 	unsigned char *str;
825 	int str_len, i, j;
826 	unsigned char c;
827 
828 	MAKE_STD_ZVAL(ret);
829 	FIND_XML_NULL(data, ret);
830 	if (data && data->children) {
831 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
832 			whiteSpace_collapse(data->children->content);
833 		} else if (data->children->type != XML_CDATA_SECTION_NODE || data->children->next != NULL) {
834 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
835 			return ret;
836 		}
837 		str_len = strlen((char*)data->children->content) / 2;
838 		str = emalloc(str_len+1);
839 		for (i = j = 0; i < str_len; i++) {
840 			c = data->children->content[j++];
841 			if (c >= '0' && c <= '9') {
842 				str[i] = (c - '0') << 4;
843 			} else if (c >= 'a' && c <= 'f') {
844 				str[i] = (c - 'a' + 10) << 4;
845 			} else if (c >= 'A' && c <= 'F') {
846 				str[i] = (c - 'A' + 10) << 4;
847 			} else {
848 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
849 			}
850 			c = data->children->content[j++];
851 			if (c >= '0' && c <= '9') {
852 				str[i] |= c - '0';
853 			} else if (c >= 'a' && c <= 'f') {
854 				str[i] |= c - 'a' + 10;
855 			} else if (c >= 'A' && c <= 'F') {
856 				str[i] |= c - 'A' + 10;
857 			} else {
858 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
859 			}
860 		}
861 		str[str_len] = '\0';
862 		ZVAL_STRINGL(ret, (char*)str, str_len, 0);
863 	} else {
864 		ZVAL_EMPTY_STRING(ret);
865 	}
866 	return ret;
867 }
868 
to_xml_string(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)869 static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
870 {
871 	xmlNodePtr ret, text;
872 	char *str;
873 	int new_len;
874 
875 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
876 	xmlAddChild(parent, ret);
877 	FIND_ZVAL_NULL(data, ret, style);
878 
879 	if (Z_TYPE_P(data) == IS_STRING) {
880 		str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
881 		new_len = Z_STRLEN_P(data);
882 	} else {
883 		zval tmp = *data;
884 
885 		zval_copy_ctor(&tmp);
886 		convert_to_string(&tmp);
887 		str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
888 		new_len = Z_STRLEN(tmp);
889 		zval_dtor(&tmp);
890 	}
891 
892 	if (SOAP_GLOBAL(encoding) != NULL) {
893 		xmlBufferPtr in  = xmlBufferCreateStatic(str, new_len);
894 		xmlBufferPtr out = xmlBufferCreate();
895 		int n = xmlCharEncInFunc(SOAP_GLOBAL(encoding), out, in);
896 
897 		if (n >= 0) {
898 			efree(str);
899 			str = estrdup((char*)xmlBufferContent(out));
900 			new_len = n;
901 		}
902 		xmlBufferFree(out);
903 		xmlBufferFree(in);
904 	}
905 
906 	if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) {
907 		char *err = emalloc(new_len + 8);
908 		char c;
909 		int i;
910 
911 		memcpy(err, str, new_len+1);
912 		for (i = 0; (c = err[i++]);) {
913 			if ((c & 0x80) == 0) {
914 			} else if ((c & 0xe0) == 0xc0) {
915 				if ((err[i] & 0xc0) != 0x80) {
916 					break;
917 				}
918 				i++;
919 			} else if ((c & 0xf0) == 0xe0) {
920 				if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80) {
921 					break;
922 				}
923 				i += 2;
924 			} else if ((c & 0xf8) == 0xf0) {
925 				if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80 || (err[i+2] & 0xc0) != 0x80) {
926 					break;
927 				}
928 				i += 3;
929 			} else {
930 				break;
931 			}
932 		}
933 		if (c) {
934 			err[i-1] = '\\';
935 			err[i++] = 'x';
936 			err[i++] = ((unsigned char)c >> 4) + ((((unsigned char)c >> 4) > 9) ? ('a' - 10) : '0');
937 			err[i++] = (c & 15) + (((c & 15) > 9) ? ('a' - 10) : '0');
938 			err[i++] = '.';
939 			err[i++] = '.';
940 			err[i++] = '.';
941 			err[i++] = 0;
942 		}
943 
944 		soap_error1(E_ERROR,  "Encoding: string '%s' is not a valid utf-8 string", err);
945 	}
946 
947 	text = xmlNewTextLen(BAD_CAST(str), new_len);
948 	xmlAddChild(ret, text);
949 	efree(str);
950 
951 	if (style == SOAP_ENCODED) {
952 		set_ns_and_type(ret, type);
953 	}
954 	return ret;
955 }
956 
to_xml_base64(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)957 static xmlNodePtr to_xml_base64(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
958 {
959 	xmlNodePtr ret, text;
960 	unsigned char *str;
961 	int str_len;
962 
963 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
964 	xmlAddChild(parent, ret);
965 	FIND_ZVAL_NULL(data, ret, style);
966 
967 	if (Z_TYPE_P(data) == IS_STRING) {
968 		str = php_base64_encode((unsigned char*)Z_STRVAL_P(data), Z_STRLEN_P(data), &str_len);
969 		text = xmlNewTextLen(str, str_len);
970 		xmlAddChild(ret, text);
971 		efree(str);
972 	} else {
973 		zval tmp = *data;
974 
975 		zval_copy_ctor(&tmp);
976 		convert_to_string(&tmp);
977 		str = php_base64_encode((unsigned char*)Z_STRVAL(tmp), Z_STRLEN(tmp), &str_len);
978 		text = xmlNewTextLen(str, str_len);
979 		xmlAddChild(ret, text);
980 		efree(str);
981 		zval_dtor(&tmp);
982 	}
983 
984 	if (style == SOAP_ENCODED) {
985 		set_ns_and_type(ret, type);
986 	}
987 	return ret;
988 }
989 
to_xml_hexbin(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)990 static xmlNodePtr to_xml_hexbin(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
991 {
992 	static char hexconvtab[] = "0123456789ABCDEF";
993 	xmlNodePtr ret, text;
994 	unsigned char *str;
995 	zval tmp;
996 	int i, j;
997 
998 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
999 	xmlAddChild(parent, ret);
1000 	FIND_ZVAL_NULL(data, ret, style);
1001 
1002 	if (Z_TYPE_P(data) != IS_STRING) {
1003 		tmp = *data;
1004 		zval_copy_ctor(&tmp);
1005 		convert_to_string(&tmp);
1006 		data = &tmp;
1007 	}
1008 	str = (unsigned char *) safe_emalloc(Z_STRLEN_P(data) * 2, sizeof(char), 1);
1009 
1010 	for (i = j = 0; i < Z_STRLEN_P(data); i++) {
1011 		str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) >> 4];
1012 		str[j++] = hexconvtab[((unsigned char)Z_STRVAL_P(data)[i]) & 15];
1013 	}
1014 	str[j] = '\0';
1015 
1016 	text = xmlNewTextLen(str, Z_STRLEN_P(data) * 2 * sizeof(char));
1017 	xmlAddChild(ret, text);
1018 	efree(str);
1019 	if (data == &tmp) {
1020 		zval_dtor(&tmp);
1021 	}
1022 
1023 	if (style == SOAP_ENCODED) {
1024 		set_ns_and_type(ret, type);
1025 	}
1026 	return ret;
1027 }
1028 
to_zval_double(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)1029 static zval *to_zval_double(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
1030 {
1031 	zval *ret;
1032 	MAKE_STD_ZVAL(ret);
1033 	FIND_XML_NULL(data, ret);
1034 
1035 	if (data && data->children) {
1036 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
1037 			long lval;
1038 			double dval;
1039 
1040 			whiteSpace_collapse(data->children->content);
1041 			switch (is_numeric_string((char*)data->children->content, strlen((char*)data->children->content), &lval, &dval, 0)) {
1042 				case IS_LONG:
1043 					Z_TYPE_P(ret) = IS_DOUBLE;
1044 					Z_DVAL_P(ret) = lval;
1045 					break;
1046 				case IS_DOUBLE:
1047 					Z_TYPE_P(ret) = IS_DOUBLE;
1048 					Z_DVAL_P(ret) = dval;
1049 					break;
1050 				default:
1051 					if (strncasecmp((char*)data->children->content, "NaN", sizeof("NaN")-1) == 0) {
1052 						ZVAL_DOUBLE(ret, php_get_nan());
1053 					} else if (strncasecmp((char*)data->children->content, "INF", sizeof("INF")-1) == 0) {
1054 						ZVAL_DOUBLE(ret, php_get_inf());
1055 					} else if (strncasecmp((char*)data->children->content, "-INF", sizeof("-INF")-1) == 0) {
1056 						ZVAL_DOUBLE(ret, -php_get_inf());
1057 					} else {
1058 						soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1059 					}
1060 			}
1061 		} else {
1062 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1063 		}
1064 	} else {
1065 		ZVAL_NULL(ret);
1066 	}
1067 	return ret;
1068 }
1069 
to_zval_long(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)1070 static zval *to_zval_long(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
1071 {
1072 	zval *ret;
1073 	MAKE_STD_ZVAL(ret);
1074 	FIND_XML_NULL(data, ret);
1075 
1076 	if (data && data->children) {
1077 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
1078 			long lval;
1079 			double dval;
1080 
1081 			whiteSpace_collapse(data->children->content);
1082 			errno = 0;
1083 
1084 			switch ((Z_TYPE_P(ret) = is_numeric_string((char*)data->children->content, strlen((char*)data->children->content), &lval, &dval, 0))) {
1085 				case IS_LONG:
1086 					Z_LVAL_P(ret) = lval;
1087 					break;
1088 				case IS_DOUBLE:
1089 					Z_DVAL_P(ret) = dval;
1090 					break;
1091 				default:
1092 					soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1093 			}
1094 		} else {
1095 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1096 		}
1097 	} else {
1098 		ZVAL_NULL(ret);
1099 	}
1100 	return ret;
1101 }
1102 
to_xml_long(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)1103 static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
1104 {
1105 	xmlNodePtr ret;
1106 
1107 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1108 	xmlAddChild(parent, ret);
1109 	FIND_ZVAL_NULL(data, ret, style);
1110 
1111 	if (Z_TYPE_P(data) == IS_DOUBLE) {
1112 		char s[256];
1113 
1114 		snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
1115 		xmlNodeSetContent(ret, BAD_CAST(s));
1116 	} else {
1117 		zval tmp = *data;
1118 
1119 		zval_copy_ctor(&tmp);
1120 		if (Z_TYPE(tmp) != IS_LONG) {
1121 			convert_to_long(&tmp);
1122 		}
1123 		convert_to_string(&tmp);
1124 		xmlNodeSetContentLen(ret, BAD_CAST(Z_STRVAL(tmp)), Z_STRLEN(tmp));
1125 		zval_dtor(&tmp);
1126 	}
1127 
1128 	if (style == SOAP_ENCODED) {
1129 		set_ns_and_type(ret, type);
1130 	}
1131 	return ret;
1132 }
1133 
to_xml_double(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)1134 static xmlNodePtr to_xml_double(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
1135 {
1136 	xmlNodePtr ret;
1137 	zval tmp;
1138 	char *str;
1139 
1140 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1141 	xmlAddChild(parent, ret);
1142 	FIND_ZVAL_NULL(data, ret, style);
1143 
1144 	tmp = *data;
1145 	if (Z_TYPE(tmp) != IS_DOUBLE) {
1146 		zval_copy_ctor(&tmp);
1147 		convert_to_double(&tmp);
1148 	}
1149 
1150 	str = (char *) safe_emalloc(EG(precision), 1, MAX_LENGTH_OF_DOUBLE + 1);
1151 	php_gcvt(Z_DVAL(tmp), EG(precision), '.', 'E', str);
1152 	xmlNodeSetContentLen(ret, BAD_CAST(str), strlen(str));
1153 	efree(str);
1154 
1155 	if (style == SOAP_ENCODED) {
1156 		set_ns_and_type(ret, type);
1157 	}
1158 	return ret;
1159 }
1160 
to_zval_bool(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)1161 static zval *to_zval_bool(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
1162 {
1163 	zval *ret;
1164 	MAKE_STD_ZVAL(ret);
1165 	FIND_XML_NULL(data, ret);
1166 
1167 	if (data && data->children) {
1168 		if (data->children->type == XML_TEXT_NODE && data->children->next == NULL) {
1169 			whiteSpace_collapse(data->children->content);
1170 			if (stricmp((char*)data->children->content, "true") == 0 ||
1171 				stricmp((char*)data->children->content, "t") == 0 ||
1172 				strcmp((char*)data->children->content, "1") == 0) {
1173 				ZVAL_BOOL(ret, 1);
1174 			} else if (stricmp((char*)data->children->content, "false") == 0 ||
1175 				stricmp((char*)data->children->content, "f") == 0 ||
1176 				strcmp((char*)data->children->content, "0") == 0) {
1177 				ZVAL_BOOL(ret, 0);
1178 			} else {
1179 				ZVAL_STRING(ret, (char*)data->children->content, 1);
1180 				convert_to_boolean(ret);
1181 			}
1182 		} else {
1183 			soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
1184 		}
1185 	} else {
1186 		ZVAL_NULL(ret);
1187 	}
1188 	return ret;
1189 }
1190 
to_xml_bool(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)1191 static xmlNodePtr to_xml_bool(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
1192 {
1193 	xmlNodePtr ret;
1194 
1195 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1196 	xmlAddChild(parent, ret);
1197 	FIND_ZVAL_NULL(data, ret, style);
1198 
1199 	if (zend_is_true(data)) {
1200 		xmlNodeSetContent(ret, BAD_CAST("true"));
1201 	} else {
1202 		xmlNodeSetContent(ret, BAD_CAST("false"));
1203 	}
1204 
1205 	if (style == SOAP_ENCODED) {
1206 		set_ns_and_type(ret, type);
1207 	}
1208 	return ret;
1209 }
1210 
1211 /* Null encode/decode */
to_zval_null(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)1212 static zval *to_zval_null(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
1213 {
1214 	zval *ret;
1215 	MAKE_STD_ZVAL(ret);
1216 	ZVAL_NULL(ret);
1217 	return ret;
1218 }
1219 
to_xml_null(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)1220 static xmlNodePtr to_xml_null(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
1221 {
1222 	xmlNodePtr ret;
1223 
1224 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1225 	xmlAddChild(parent, ret);
1226 	if (style == SOAP_ENCODED) {
1227 		set_xsi_nil(ret);
1228 	}
1229 	return ret;
1230 }
1231 
set_zval_property(zval * object,char * name,zval * val TSRMLS_DC)1232 static void set_zval_property(zval* object, char* name, zval* val TSRMLS_DC)
1233 {
1234 	zend_class_entry *old_scope;
1235 
1236 	old_scope = EG(scope);
1237 	EG(scope) = Z_OBJCE_P(object);
1238 	Z_DELREF_P(val);
1239 	add_property_zval(object, name, val);
1240 	EG(scope) = old_scope;
1241 }
1242 
get_zval_property(zval * object,char * name TSRMLS_DC)1243 static zval* get_zval_property(zval* object, char* name TSRMLS_DC)
1244 {
1245 	if (Z_TYPE_P(object) == IS_OBJECT) {
1246 		zval member;
1247 		zval *data;
1248 		zend_class_entry *old_scope;
1249 
1250 		INIT_PZVAL(&member);
1251 		ZVAL_STRING(&member, name, 0);
1252 		old_scope = EG(scope);
1253 		EG(scope) = Z_OBJCE_P(object);
1254 		data = Z_OBJ_HT_P(object)->read_property(object, &member, BP_VAR_IS, 0 TSRMLS_CC);
1255 		if (data == EG(uninitialized_zval_ptr)) {
1256 			/* Hack for bug #32455 */
1257 			zend_property_info *property_info;
1258 
1259 			property_info = zend_get_property_info(Z_OBJCE_P(object), &member, 1 TSRMLS_CC);
1260 			EG(scope) = old_scope;
1261 			if (property_info && zend_hash_quick_exists(Z_OBJPROP_P(object), property_info->name, property_info->name_length+1, property_info->h)) {
1262 				return data;
1263 			}
1264 			return NULL;
1265 		}
1266 		EG(scope) = old_scope;
1267 		return data;
1268 	} else if (Z_TYPE_P(object) == IS_ARRAY) {
1269 		zval **data_ptr;
1270 
1271 		if (zend_hash_find(Z_ARRVAL_P(object), name, strlen(name)+1, (void**)&data_ptr) == SUCCESS) {
1272 		  return *data_ptr;
1273 		}
1274 	}
1275   return NULL;
1276 }
1277 
unset_zval_property(zval * object,char * name TSRMLS_DC)1278 static void unset_zval_property(zval* object, char* name TSRMLS_DC)
1279 {
1280 	if (Z_TYPE_P(object) == IS_OBJECT) {
1281 		zval member;
1282 		zend_class_entry *old_scope;
1283 
1284 		INIT_PZVAL(&member);
1285 		ZVAL_STRING(&member, name, 0);
1286 		old_scope = EG(scope);
1287 		EG(scope) = Z_OBJCE_P(object);
1288 		Z_OBJ_HT_P(object)->unset_property(object, &member, 0 TSRMLS_CC);
1289 		EG(scope) = old_scope;
1290 	} else if (Z_TYPE_P(object) == IS_ARRAY) {
1291 		zend_hash_del(Z_ARRVAL_P(object), name, strlen(name)+1);
1292 	}
1293 }
1294 
model_to_zval_any(zval * ret,xmlNodePtr node TSRMLS_DC)1295 static void model_to_zval_any(zval *ret, xmlNodePtr node TSRMLS_DC)
1296 {
1297 	zval* any = NULL;
1298 	char* name = NULL;
1299 
1300 	while (node != NULL) {
1301 		if (get_zval_property(ret, (char*)node->name TSRMLS_CC) == NULL) {
1302 			zval* val = master_to_zval(get_conversion(XSD_ANYXML), node TSRMLS_CC);
1303 
1304 			if (any && Z_TYPE_P(any) != IS_ARRAY) {
1305 				/* Convert into array */
1306 				zval *arr;
1307 
1308 				MAKE_STD_ZVAL(arr);
1309 				array_init(arr);
1310 				if (name) {
1311 					add_assoc_zval(arr, name, any);
1312 				} else {
1313 					add_next_index_zval(arr, any);
1314 				}
1315 				any = arr;
1316 			}
1317 
1318 			if (Z_TYPE_P(val) == IS_STRING && *Z_STRVAL_P(val) == '<') {
1319 				name = NULL;
1320 				while (node->next != NULL) {
1321 					zval* val2 = master_to_zval(get_conversion(XSD_ANYXML), node->next TSRMLS_CC);
1322 					if (Z_TYPE_P(val2) != IS_STRING ||  *Z_STRVAL_P(val) != '<') {
1323 						break;
1324 					}
1325 					add_string_to_string(val, val, val2);
1326 					zval_ptr_dtor(&val2);
1327 					node = node->next;
1328 				}
1329 			} else {
1330 				name = (char*)node->name;
1331 			}
1332 
1333 			if (any == NULL) {
1334 				if (name) {
1335 					/* Convert into array */
1336 					zval *arr;
1337 
1338 					MAKE_STD_ZVAL(arr);
1339 					array_init(arr);
1340 					add_assoc_zval(arr, name, val);
1341 					any = arr;
1342 					name = NULL;
1343 				} else {
1344 					any = val;
1345 				}
1346 			} else {
1347 				/* Add array element */
1348 				if (name) {
1349 					zval **el;
1350 					if (zend_hash_find(Z_ARRVAL_P(any), name, strlen(name)+1, (void**)&el) == SUCCESS) {
1351 						if (Z_TYPE_PP(el) != IS_ARRAY) {
1352 							/* Convert into array */
1353 							zval *arr;
1354 
1355 							MAKE_STD_ZVAL(arr);
1356 							array_init(arr);
1357 							add_next_index_zval(arr, *el);
1358 							*el = arr;
1359 						}
1360 						add_next_index_zval(*el, val);
1361 					} else {
1362 						add_assoc_zval(any, name, val);
1363 					}
1364 				} else {
1365 					add_next_index_zval(any, val);
1366 				}
1367 				name = NULL;
1368 			}
1369 		}
1370 		node = node->next;
1371 	}
1372 	if (any) {
1373 		set_zval_property(ret, name ? name : "any", any TSRMLS_CC);
1374 	}
1375 }
1376 
model_to_zval_object(zval * ret,sdlContentModelPtr model,xmlNodePtr data,sdlPtr sdl TSRMLS_DC)1377 static void model_to_zval_object(zval *ret, sdlContentModelPtr model, xmlNodePtr data, sdlPtr sdl TSRMLS_DC)
1378 {
1379 	switch (model->kind) {
1380 		case XSD_CONTENT_ELEMENT:
1381 			if (model->u.element->name) {
1382 				xmlNodePtr node = get_node(data->children, model->u.element->name);
1383 
1384 				if (node) {
1385 					zval *val;
1386 					xmlNodePtr r_node;
1387 
1388 					r_node = check_and_resolve_href(node);
1389 					if (r_node && r_node->children && r_node->children->content) {
1390 						if (model->u.element->fixed && strcmp(model->u.element->fixed, (char*)r_node->children->content) != 0) {
1391 							soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, r_node->children->content);
1392 						}
1393 						val = master_to_zval(model->u.element->encode, r_node TSRMLS_CC);
1394 					} else if (model->u.element->fixed) {
1395 						xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1396 						xmlNodeSetContent(dummy, BAD_CAST(model->u.element->fixed));
1397 						val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
1398 						xmlFreeNode(dummy);
1399 					} else if (model->u.element->def && !model->u.element->nillable) {
1400 						xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1401 						xmlNodeSetContent(dummy, BAD_CAST(model->u.element->def));
1402 						val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
1403 						xmlFreeNode(dummy);
1404 					} else {
1405 						val = master_to_zval(model->u.element->encode, r_node TSRMLS_CC);
1406 					}
1407 					if ((node = get_node(node->next, model->u.element->name)) != NULL) {
1408 						zval *array;
1409 
1410 						MAKE_STD_ZVAL(array);
1411 						array_init(array);
1412 						add_next_index_zval(array, val);
1413 						do {
1414 							if (node && node->children && node->children->content) {
1415 								if (model->u.element->fixed && strcmp(model->u.element->fixed, (char*)node->children->content) != 0) {
1416 									soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, node->children->content);
1417 								}
1418 								val = master_to_zval(model->u.element->encode, node TSRMLS_CC);
1419 							} else if (model->u.element->fixed) {
1420 								xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1421 								xmlNodeSetContent(dummy, BAD_CAST(model->u.element->fixed));
1422 								val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
1423 								xmlFreeNode(dummy);
1424 							} else if (model->u.element->def && !model->u.element->nillable) {
1425 								xmlNodePtr dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1426 								xmlNodeSetContent(dummy, BAD_CAST(model->u.element->def));
1427 								val = master_to_zval(model->u.element->encode, dummy TSRMLS_CC);
1428 								xmlFreeNode(dummy);
1429 							} else {
1430 								val = master_to_zval(model->u.element->encode, node TSRMLS_CC);
1431 							}
1432 							add_next_index_zval(array, val);
1433 						} while ((node = get_node(node->next, model->u.element->name)) != NULL);
1434 						val = array;
1435 					} else if ((Z_TYPE_P(val) != IS_NULL || !model->u.element->nillable) &&
1436 					           (SOAP_GLOBAL(features) & SOAP_SINGLE_ELEMENT_ARRAYS) &&
1437 					           (model->max_occurs == -1 || model->max_occurs > 1)) {
1438 						zval *array;
1439 
1440 						MAKE_STD_ZVAL(array);
1441 						array_init(array);
1442 						add_next_index_zval(array, val);
1443 						val = array;
1444 					}
1445 					set_zval_property(ret, model->u.element->name, val TSRMLS_CC);
1446 				}
1447 			}
1448 			break;
1449 		case XSD_CONTENT_ALL:
1450 		case XSD_CONTENT_SEQUENCE:
1451 		case XSD_CONTENT_CHOICE: {
1452 			sdlContentModelPtr *tmp;
1453 			HashPosition pos;
1454 			sdlContentModelPtr any = NULL;
1455 
1456 			zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
1457 			while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
1458 				if ((*tmp)->kind == XSD_CONTENT_ANY) {
1459 					any = *tmp;
1460 				} else {
1461 					model_to_zval_object(ret, *tmp, data, sdl TSRMLS_CC);
1462 				}
1463 				zend_hash_move_forward_ex(model->u.content, &pos);
1464 			}
1465 			if (any) {
1466 				model_to_zval_any(ret, data->children TSRMLS_CC);
1467 			}
1468 			break;
1469 		}
1470 		case XSD_CONTENT_GROUP:
1471 			model_to_zval_object(ret, model->u.group->model, data, sdl TSRMLS_CC);
1472 			break;
1473 		default:
1474 		  break;
1475 	}
1476 }
1477 
1478 /* Struct encode/decode */
to_zval_object_ex(encodeTypePtr type,xmlNodePtr data,zend_class_entry * pce TSRMLS_DC)1479 static zval *to_zval_object_ex(encodeTypePtr type, xmlNodePtr data, zend_class_entry *pce TSRMLS_DC)
1480 {
1481 	zval *ret;
1482 	xmlNodePtr trav;
1483 	sdlPtr sdl;
1484 	sdlTypePtr sdlType = type->sdl_type;
1485 	zend_class_entry *ce = ZEND_STANDARD_CLASS_DEF_PTR;
1486 	zval *redo_any = NULL;
1487 
1488 	if (pce) {
1489 		ce = pce;
1490 	} else if (SOAP_GLOBAL(class_map) && type->type_str) {
1491 		zval             **classname;
1492 		zend_class_entry  *tmp;
1493 
1494 		if (zend_hash_find(SOAP_GLOBAL(class_map), type->type_str, strlen(type->type_str)+1, (void**)&classname) == SUCCESS &&
1495 		    Z_TYPE_PP(classname) == IS_STRING &&
1496 		    (tmp = zend_fetch_class(Z_STRVAL_PP(classname), Z_STRLEN_PP(classname), ZEND_FETCH_CLASS_AUTO TSRMLS_CC)) != NULL) {
1497 			ce = tmp;
1498 		}
1499 	}
1500 	sdl = SOAP_GLOBAL(sdl);
1501 	if (sdlType) {
1502 		if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
1503 		    sdlType->encode && type != &sdlType->encode->details) {
1504 			encodePtr enc;
1505 
1506 			enc = sdlType->encode;
1507 			while (enc && enc->details.sdl_type &&
1508 			       enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1509 			       enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1510 			       enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1511 				enc = enc->details.sdl_type->encode;
1512 			}
1513 			if (enc) {
1514 				zval *base;
1515 
1516 				ALLOC_INIT_ZVAL(ret);
1517 				if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
1518 					return ret;
1519 				}
1520 
1521 				object_init_ex(ret, ce);
1522 				base = master_to_zval_int(enc, data TSRMLS_CC);
1523 				set_zval_property(ret, "_", base TSRMLS_CC);
1524 			} else {
1525 				ALLOC_INIT_ZVAL(ret);
1526 				FIND_XML_NULL(data, ret);
1527 				if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
1528 					return ret;
1529 				}
1530 				object_init_ex(ret, ce);
1531 			}
1532 		} else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
1533 		           sdlType->encode &&
1534 		           type != &sdlType->encode->details) {
1535 			if (sdlType->encode->details.sdl_type &&
1536 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1537 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1538 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1539 
1540 				CHECK_XML_NULL(data);
1541 				if ((ret = soap_find_xml_ref(data TSRMLS_CC)) != NULL) {
1542 					return ret;
1543 				}
1544 
1545 			    if (ce != ZEND_STANDARD_CLASS_DEF_PTR &&
1546 			        sdlType->encode->to_zval == sdl_guess_convert_zval &&
1547 			        sdlType->encode->details.sdl_type != NULL &&
1548 			        (sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_COMPLEX ||
1549 			         sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_RESTRICTION ||
1550 			         sdlType->encode->details.sdl_type->kind == XSD_TYPEKIND_EXTENSION) &&
1551 			        (sdlType->encode->details.sdl_type->encode == NULL ||
1552 			         (sdlType->encode->details.sdl_type->encode->details.type != IS_ARRAY &&
1553 			          sdlType->encode->details.sdl_type->encode->details.type != SOAP_ENC_ARRAY))) {
1554 					ret = to_zval_object_ex(&sdlType->encode->details, data, ce TSRMLS_CC);
1555 			    } else {
1556 					ret = master_to_zval_int(sdlType->encode, data TSRMLS_CC);
1557 				}
1558 				if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
1559 					return ret;
1560 				}
1561 				redo_any = get_zval_property(ret, "any" TSRMLS_CC);
1562 				if (Z_TYPE_P(ret) == IS_OBJECT && ce != ZEND_STANDARD_CLASS_DEF_PTR) {
1563 					zend_object *zobj = zend_objects_get_address(ret TSRMLS_CC);
1564 					zobj->ce = ce;
1565 				}
1566 			} else {
1567 				zval *base;
1568 
1569 				ALLOC_INIT_ZVAL(ret);
1570 				if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
1571 					return ret;
1572 				}
1573 
1574 				object_init_ex(ret, ce);
1575 				base = master_to_zval_int(sdlType->encode, data TSRMLS_CC);
1576 				set_zval_property(ret, "_", base TSRMLS_CC);
1577 			}
1578 		} else {
1579 			ALLOC_INIT_ZVAL(ret);
1580 			FIND_XML_NULL(data, ret);
1581 			if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
1582 				return ret;
1583 			}
1584 			object_init_ex(ret, ce);
1585 		}
1586 		if (sdlType->model) {
1587 			if (redo_any) {
1588 				Z_ADDREF_P(redo_any);
1589 				unset_zval_property(ret, "any" TSRMLS_CC);
1590 			}
1591 			model_to_zval_object(ret, sdlType->model, data, sdl TSRMLS_CC);
1592 			if (redo_any) {
1593 				zval *tmp = get_zval_property(ret, "any" TSRMLS_CC);
1594 
1595 				if (tmp == NULL) {
1596 					model_to_zval_any(ret, data->children TSRMLS_CC);
1597 				} else if (Z_REFCOUNT_P(tmp) == 0) {
1598 					zval_dtor(tmp);
1599 					efree(tmp);
1600 				}
1601 				zval_ptr_dtor(&redo_any);
1602 			}
1603 		}
1604 		if (sdlType->attributes) {
1605 			sdlAttributePtr *attr;
1606 			HashPosition pos;
1607 
1608 			zend_hash_internal_pointer_reset_ex(sdlType->attributes, &pos);
1609 			while (zend_hash_get_current_data_ex(sdlType->attributes, (void**)&attr, &pos) == SUCCESS) {
1610 				if ((*attr)->name) {
1611 					xmlAttrPtr val = get_attribute(data->properties, (*attr)->name);
1612 					char *str_val = NULL;
1613 
1614 					if (val && val->children && val->children->content) {
1615 						str_val = (char*)val->children->content;
1616 						if ((*attr)->fixed && strcmp((*attr)->fixed, str_val) != 0) {
1617 							soap_error3(E_ERROR, "Encoding: Attribute '%s' has fixed value '%s' (value '%s' is not allowed)", (*attr)->name, (*attr)->fixed, str_val);
1618 						}
1619 					} else if ((*attr)->fixed) {
1620 						str_val = (*attr)->fixed;
1621 					} else if ((*attr)->def) {
1622 						str_val = (*attr)->def;
1623 					}
1624 					if (str_val) {
1625 						xmlNodePtr dummy, text;
1626 						zval *data;
1627 
1628 						dummy = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1629 						text = xmlNewText(BAD_CAST(str_val));
1630 						xmlAddChild(dummy, text);
1631 						data = master_to_zval((*attr)->encode, dummy TSRMLS_CC);
1632 						xmlFreeNode(dummy);
1633 						set_zval_property(ret, (*attr)->name, data TSRMLS_CC);
1634 					}
1635 				}
1636 				zend_hash_move_forward_ex(sdlType->attributes, &pos);
1637 			}
1638 		}
1639 	} else {
1640 
1641 		ALLOC_INIT_ZVAL(ret);
1642 		FIND_XML_NULL(data, ret);
1643 		if (soap_check_xml_ref(&ret, data TSRMLS_CC)) {
1644 			return ret;
1645 		}
1646 
1647 		object_init_ex(ret, ce);
1648 		trav = data->children;
1649 
1650 		while (trav != NULL) {
1651 			if (trav->type == XML_ELEMENT_NODE) {
1652 				zval  *tmpVal;
1653 				zval *prop;
1654 
1655 				tmpVal = master_to_zval(NULL, trav TSRMLS_CC);
1656 
1657 				prop = get_zval_property(ret, (char*)trav->name TSRMLS_CC);
1658 				if (!prop) {
1659 					if (!trav->next || !get_node(trav->next, (char*)trav->name)) {
1660 						set_zval_property(ret, (char*)trav->name, tmpVal TSRMLS_CC);
1661 					} else {
1662 						zval *arr;
1663 
1664 						MAKE_STD_ZVAL(arr);
1665 						array_init(arr);
1666 						add_next_index_zval(arr, tmpVal);
1667 						set_zval_property(ret, (char*)trav->name, arr TSRMLS_CC);
1668 					}
1669 				} else {
1670 					/* Property already exist - make array */
1671 					if (Z_TYPE_P(prop) != IS_ARRAY) {
1672 						/* Convert into array */
1673 						zval *arr;
1674 
1675 						MAKE_STD_ZVAL(arr);
1676 						array_init(arr);
1677 						Z_ADDREF_P(prop);
1678 						add_next_index_zval(arr, prop);
1679 						set_zval_property(ret, (char*)trav->name, arr TSRMLS_CC);
1680 						prop = arr;
1681 					}
1682 					/* Add array element */
1683 					add_next_index_zval(prop, tmpVal);
1684 				}
1685 			}
1686 			trav = trav->next;
1687 		}
1688 	}
1689 	return ret;
1690 }
1691 
to_zval_object(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)1692 static zval *to_zval_object(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
1693 {
1694 	return to_zval_object_ex(type, data, NULL TSRMLS_CC);
1695 }
1696 
1697 
model_to_xml_object(xmlNodePtr node,sdlContentModelPtr model,zval * object,int style,int strict TSRMLS_DC)1698 static int model_to_xml_object(xmlNodePtr node, sdlContentModelPtr model, zval *object, int style, int strict TSRMLS_DC)
1699 {
1700 	switch (model->kind) {
1701 		case XSD_CONTENT_ELEMENT: {
1702 			zval *data;
1703 			xmlNodePtr property;
1704 			encodePtr enc;
1705 
1706 			data = get_zval_property(object, model->u.element->name TSRMLS_CC);
1707 			if (data &&
1708 			    Z_TYPE_P(data) == IS_NULL &&
1709 			    !model->u.element->nillable &&
1710 			    model->min_occurs > 0 &&
1711 			    !strict) {
1712 				return 0;
1713 			}
1714 			if (data) {
1715 				enc = model->u.element->encode;
1716 				if ((model->max_occurs == -1 || model->max_occurs > 1) &&
1717 				    Z_TYPE_P(data) == IS_ARRAY &&
1718 				    !is_map(data)) {
1719 					HashTable *ht = Z_ARRVAL_P(data);
1720 					zval **val;
1721 
1722 					zend_hash_internal_pointer_reset(ht);
1723 					while (zend_hash_get_current_data(ht,(void**)&val) == SUCCESS) {
1724 						if (Z_TYPE_PP(val) == IS_NULL && model->u.element->nillable) {
1725 							property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1726 							xmlAddChild(node, property);
1727 							set_xsi_nil(property);
1728 						} else {
1729 							property = master_to_xml(enc, *val, style, node TSRMLS_CC);
1730 							if (property->children && property->children->content &&
1731 							    model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
1732 								soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, property->children->content);
1733 							}
1734 						}
1735 						xmlNodeSetName(property, BAD_CAST(model->u.element->name));
1736 						if (style == SOAP_LITERAL &&
1737 						    model->u.element->namens &&
1738 						    model->u.element->form == XSD_FORM_QUALIFIED) {
1739 							xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1740 							xmlSetNs(property, nsp);
1741 						}
1742 						zend_hash_move_forward(ht);
1743 					}
1744 				} else {
1745 					if (Z_TYPE_P(data) == IS_NULL && model->u.element->nillable) {
1746 						property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1747 						xmlAddChild(node, property);
1748 						set_xsi_nil(property);
1749 					} else if (Z_TYPE_P(data) == IS_NULL && model->min_occurs == 0) {
1750 						return 1;
1751 					} else {
1752 						property = master_to_xml(enc, data, style, node TSRMLS_CC);
1753 						if (property->children && property->children->content &&
1754 						    model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
1755 							soap_error3(E_ERROR, "Encoding: Element '%s' has fixed value '%s' (value '%s' is not allowed)", model->u.element->name, model->u.element->fixed, property->children->content);
1756 						}
1757 					}
1758 					xmlNodeSetName(property, BAD_CAST(model->u.element->name));
1759 					if (style == SOAP_LITERAL &&
1760 					    model->u.element->namens &&
1761 					    model->u.element->form == XSD_FORM_QUALIFIED) {
1762 						xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1763 						xmlSetNs(property, nsp);
1764 					}
1765 				}
1766 				return 1;
1767 			} else if (strict && model->u.element->nillable && model->min_occurs > 0) {
1768 				property = xmlNewNode(NULL, BAD_CAST(model->u.element->name));
1769 				xmlAddChild(node, property);
1770 				set_xsi_nil(property);
1771 				if (style == SOAP_LITERAL &&
1772 				    model->u.element->namens &&
1773 				    model->u.element->form == XSD_FORM_QUALIFIED) {
1774 					xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1775 					xmlSetNs(property, nsp);
1776 				}
1777 				return 1;
1778 			} else if (model->min_occurs == 0) {
1779 				return 2;
1780 			} else {
1781 				if (strict) {
1782 					soap_error1(E_ERROR,  "Encoding: object has no '%s' property", model->u.element->name);
1783 				}
1784 				return 0;
1785 			}
1786 			break;
1787 		}
1788 		case XSD_CONTENT_ANY: {
1789 			zval *data;
1790 			xmlNodePtr property;
1791 			encodePtr enc;
1792 
1793 			data = get_zval_property(object, "any" TSRMLS_CC);
1794 			if (data) {
1795 				enc = get_conversion(XSD_ANYXML);
1796 				if ((model->max_occurs == -1 || model->max_occurs > 1) &&
1797 				    Z_TYPE_P(data) == IS_ARRAY &&
1798 				    !is_map(data)) {
1799 					HashTable *ht = Z_ARRVAL_P(data);
1800 					zval **val;
1801 
1802 					zend_hash_internal_pointer_reset(ht);
1803 					while (zend_hash_get_current_data(ht,(void**)&val) == SUCCESS) {
1804 						property = master_to_xml(enc, *val, style, node TSRMLS_CC);
1805 						zend_hash_move_forward(ht);
1806 					}
1807 				} else {
1808 					property = master_to_xml(enc, data, style, node TSRMLS_CC);
1809 				}
1810 				return 1;
1811 			} else if (model->min_occurs == 0) {
1812 				return 2;
1813 			} else {
1814 				if (strict) {
1815 					soap_error0(E_ERROR,  "Encoding: object has no 'any' property");
1816 				}
1817 				return 0;
1818 			}
1819 			break;
1820 		}
1821 		case XSD_CONTENT_SEQUENCE:
1822 		case XSD_CONTENT_ALL: {
1823 			sdlContentModelPtr *tmp;
1824 			HashPosition pos;
1825 
1826 			zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
1827 			while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
1828 				if (!model_to_xml_object(node, *tmp, object, style, strict && ((*tmp)->min_occurs > 0) TSRMLS_CC)) {
1829 					if (!strict || (*tmp)->min_occurs > 0) {
1830 						return 0;
1831 					}
1832 				}
1833 				strict = 1;
1834 				zend_hash_move_forward_ex(model->u.content, &pos);
1835 			}
1836 			return 1;
1837 		}
1838 		case XSD_CONTENT_CHOICE: {
1839 			sdlContentModelPtr *tmp;
1840 			HashPosition pos;
1841 			int ret = 0;
1842 
1843 			zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
1844 			while (zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos) == SUCCESS) {
1845 				int tmp_ret = model_to_xml_object(node, *tmp, object, style, 0 TSRMLS_CC);
1846 				if (tmp_ret == 1) {
1847 					return 1;
1848 				} else if (tmp_ret != 0) {
1849 					ret = 1;
1850 				}
1851 				zend_hash_move_forward_ex(model->u.content, &pos);
1852 			}
1853 			return ret;
1854 		}
1855 		case XSD_CONTENT_GROUP: {
1856 			return model_to_xml_object(node, model->u.group->model, object, style, strict && model->min_occurs > 0 TSRMLS_CC);
1857 		}
1858 		default:
1859 		  break;
1860 	}
1861 	return 1;
1862 }
1863 
model_array_element(sdlContentModelPtr model)1864 static sdlTypePtr model_array_element(sdlContentModelPtr model)
1865 {
1866 	switch (model->kind) {
1867 		case XSD_CONTENT_ELEMENT: {
1868 			if (model->max_occurs == -1 || model->max_occurs > 1) {
1869 			  return model->u.element;
1870 			} else {
1871 			  return NULL;
1872 			}
1873 		}
1874 		case XSD_CONTENT_SEQUENCE:
1875 		case XSD_CONTENT_ALL:
1876 		case XSD_CONTENT_CHOICE: {
1877 			sdlContentModelPtr *tmp;
1878 			HashPosition pos;
1879 
1880 			if (zend_hash_num_elements(model->u.content) != 1) {
1881 			  return NULL;
1882 			}
1883 			zend_hash_internal_pointer_reset_ex(model->u.content, &pos);
1884 			zend_hash_get_current_data_ex(model->u.content, (void**)&tmp, &pos);
1885 			return model_array_element(*tmp);
1886 		}
1887 		case XSD_CONTENT_GROUP: {
1888 			return model_array_element(model->u.group->model);
1889 		}
1890 		default:
1891 		  break;
1892 	}
1893 	return NULL;
1894 }
1895 
to_xml_object(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)1896 static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
1897 {
1898 	xmlNodePtr xmlParam;
1899 	HashTable *prop = NULL;
1900 	int i;
1901 	sdlTypePtr sdlType = type->sdl_type;
1902 
1903 	if (!data || Z_TYPE_P(data) == IS_NULL) {
1904 		xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1905 		xmlAddChild(parent, xmlParam);
1906 		if (style == SOAP_ENCODED) {
1907 			set_xsi_nil(xmlParam);
1908 			set_ns_and_type(xmlParam, type);
1909 		}
1910 		return xmlParam;
1911 	}
1912 
1913 	if (Z_TYPE_P(data) == IS_OBJECT) {
1914 		prop = Z_OBJPROP_P(data);
1915 	} else if (Z_TYPE_P(data) == IS_ARRAY) {
1916 		prop = Z_ARRVAL_P(data);
1917 	}
1918 
1919 	if (sdlType) {
1920 		if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
1921 		    sdlType->encode && type != &sdlType->encode->details) {
1922 			encodePtr enc;
1923 
1924 			enc = sdlType->encode;
1925 			while (enc && enc->details.sdl_type &&
1926 			       enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1927 			       enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1928 			       enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1929 				enc = enc->details.sdl_type->encode;
1930 			}
1931 			if (enc) {
1932 				zval *tmp = get_zval_property(data, "_" TSRMLS_CC);
1933 				if (tmp) {
1934 					xmlParam = master_to_xml(enc, tmp, style, parent TSRMLS_CC);
1935 				} else if (prop == NULL) {
1936 					xmlParam = master_to_xml(enc, data, style, parent TSRMLS_CC);
1937 				} else {
1938 					xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1939 					xmlAddChild(parent, xmlParam);
1940 				}
1941 			} else {
1942 				xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1943 				xmlAddChild(parent, xmlParam);
1944 			}
1945 		} else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
1946 		           sdlType->encode && type != &sdlType->encode->details) {
1947 			if (sdlType->encode->details.sdl_type &&
1948 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1949 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1950 			    sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1951 
1952 				if (prop) prop->nApplyCount++;
1953 				xmlParam = master_to_xml(sdlType->encode, data, style, parent TSRMLS_CC);
1954 				if (prop) prop->nApplyCount--;
1955 			} else {
1956 				zval *tmp = get_zval_property(data, "_" TSRMLS_CC);
1957 
1958 				if (tmp) {
1959 					xmlParam = master_to_xml(sdlType->encode, tmp, style, parent TSRMLS_CC);
1960 				} else if (prop == NULL) {
1961 					xmlParam = master_to_xml(sdlType->encode, data, style, parent TSRMLS_CC);
1962 				} else {
1963 					xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1964 					xmlAddChild(parent, xmlParam);
1965 				}
1966 			}
1967 		} else {
1968 			xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1969 			xmlAddChild(parent, xmlParam);
1970 		}
1971 
1972 		if (soap_check_zval_ref(data, xmlParam TSRMLS_CC)) {
1973 			return xmlParam;
1974 		}
1975 		if (prop != NULL) {
1976 			sdlTypePtr array_el;
1977 
1978 			if (Z_TYPE_P(data) == IS_ARRAY &&
1979 		      !is_map(data) &&
1980 		      sdlType->attributes == NULL &&
1981 		      sdlType->model != NULL &&
1982 		      (array_el = model_array_element(sdlType->model)) != NULL) {
1983 				zval **val;
1984 
1985 				zend_hash_internal_pointer_reset(prop);
1986 				while (zend_hash_get_current_data(prop,(void**)&val) == SUCCESS) {
1987 					xmlNodePtr property;
1988 					if (Z_TYPE_PP(val) == IS_NULL && array_el->nillable) {
1989 						property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1990 						xmlAddChild(xmlParam, property);
1991 						set_xsi_nil(property);
1992 					} else {
1993 						property = master_to_xml(array_el->encode, *val, style, xmlParam TSRMLS_CC);
1994 					}
1995 					xmlNodeSetName(property, BAD_CAST(array_el->name));
1996 					if (style == SOAP_LITERAL &&
1997 					   array_el->namens &&
1998 					   array_el->form == XSD_FORM_QUALIFIED) {
1999 						xmlNsPtr nsp = encode_add_ns(property, array_el->namens);
2000 						xmlSetNs(property, nsp);
2001 					}
2002 					zend_hash_move_forward(prop);
2003 				}
2004 			} else if (sdlType->model) {
2005 				model_to_xml_object(xmlParam, sdlType->model, data, style, 1 TSRMLS_CC);
2006 			}
2007 			if (sdlType->attributes) {
2008 				sdlAttributePtr *attr;
2009 				zval *zattr;
2010 				HashPosition pos;
2011 
2012 				zend_hash_internal_pointer_reset_ex(sdlType->attributes, &pos);
2013 				while (zend_hash_get_current_data_ex(sdlType->attributes, (void**)&attr, &pos) == SUCCESS) {
2014 					if ((*attr)->name) {
2015 						zattr = get_zval_property(data, (*attr)->name TSRMLS_CC);
2016 						if (zattr) {
2017 							xmlNodePtr dummy;
2018 
2019 							dummy = master_to_xml((*attr)->encode, zattr, SOAP_LITERAL, xmlParam TSRMLS_CC);
2020 							if (dummy->children && dummy->children->content) {
2021 								if ((*attr)->fixed && strcmp((*attr)->fixed, (char*)dummy->children->content) != 0) {
2022 									soap_error3(E_ERROR, "Encoding: Attribute '%s' has fixed value '%s' (value '%s' is not allowed)", (*attr)->name, (*attr)->fixed, dummy->children->content);
2023 								}
2024 								/* we need to handle xml: namespace specially, since it is
2025 								   an implicit schema. Otherwise, use form.
2026 								*/
2027 								if ((*attr)->namens &&
2028 								    (!strncmp((*attr)->namens, XML_NAMESPACE, sizeof(XML_NAMESPACE)) ||
2029 								     (*attr)->form == XSD_FORM_QUALIFIED)) {
2030 									xmlNsPtr nsp = encode_add_ns(xmlParam, (*attr)->namens);
2031 
2032 									xmlSetNsProp(xmlParam, nsp, BAD_CAST((*attr)->name), dummy->children->content);
2033 								} else {
2034 									xmlSetProp(xmlParam, BAD_CAST((*attr)->name), dummy->children->content);
2035 								}
2036 							}
2037 							xmlUnlinkNode(dummy);
2038 							xmlFreeNode(dummy);
2039 						}
2040 					}
2041 					zend_hash_move_forward_ex(sdlType->attributes, &pos);
2042 				}
2043 			}
2044 		}
2045 		if (style == SOAP_ENCODED) {
2046 			set_ns_and_type(xmlParam, type);
2047 		}
2048 	} else {
2049 		xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2050 		xmlAddChild(parent, xmlParam);
2051 
2052 		if (soap_check_zval_ref(data, xmlParam TSRMLS_CC)) {
2053 			return xmlParam;
2054 		}
2055 		if (prop != NULL) {
2056 			i = zend_hash_num_elements(prop);
2057 			zend_hash_internal_pointer_reset(prop);
2058 
2059 			for (;i > 0;i--) {
2060 				xmlNodePtr property;
2061 				zval **zprop;
2062 				char *str_key;
2063 				ulong index;
2064 				int key_type;
2065 				unsigned int str_key_len;
2066 
2067 				key_type = zend_hash_get_current_key_ex(prop, &str_key, &str_key_len, &index, FALSE, NULL);
2068 				zend_hash_get_current_data(prop, (void **)&zprop);
2069 
2070 				property = master_to_xml(get_conversion((*zprop)->type), (*zprop), style, xmlParam TSRMLS_CC);
2071 
2072 				if (key_type == HASH_KEY_IS_STRING) {
2073 					const char *prop_name;
2074 
2075 					if (Z_TYPE_P(data) == IS_OBJECT) {
2076 						const char *class_name;
2077 
2078 						zend_unmangle_property_name(str_key, str_key_len-1, &class_name, &prop_name);
2079 					} else {
2080 						prop_name = str_key;
2081 					}
2082 					if (prop_name) {
2083 						xmlNodeSetName(property, BAD_CAST(prop_name));
2084 					}
2085 				}
2086 				zend_hash_move_forward(prop);
2087 			}
2088 		}
2089 		if (style == SOAP_ENCODED) {
2090 			set_ns_and_type(xmlParam, type);
2091 		}
2092 	}
2093 	return xmlParam;
2094 }
2095 
2096 /* Array encode/decode */
guess_array_map(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)2097 static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
2098 {
2099 	encodePtr enc = NULL;
2100 
2101 	if (data && Z_TYPE_P(data) == IS_ARRAY) {
2102 		if (is_map(data)) {
2103 			enc = get_conversion(APACHE_MAP);
2104 		} else {
2105 			enc = get_conversion(SOAP_ENC_ARRAY);
2106 		}
2107 	}
2108 	if (!enc) {
2109 		enc = get_conversion(IS_NULL);
2110 	}
2111 
2112 	return master_to_xml(enc, data, style, parent TSRMLS_CC);
2113 }
2114 
calc_dimension_12(const char * str)2115 static int calc_dimension_12(const char* str)
2116 {
2117 	int i = 0, flag = 0;
2118 	while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
2119 		str++;
2120 	}
2121 	if (*str == '*') {
2122 		i++;
2123 		str++;
2124 	}
2125 	while (*str != '\0') {
2126 		if (*str >= '0' && *str <= '9') {
2127 			if (flag == 0) {
2128 	   		i++;
2129 	   		flag = 1;
2130 	   	}
2131 	  } else if (*str == '*') {
2132 			soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
2133 		} else {
2134 			flag = 0;
2135 		}
2136 		str++;
2137 	}
2138 	return i;
2139 }
2140 
get_position_12(int dimension,const char * str)2141 static int* get_position_12(int dimension, const char* str)
2142 {
2143 	int *pos;
2144 	int i = -1, flag = 0;
2145 
2146 	pos = safe_emalloc(sizeof(int), dimension, 0);
2147 	memset(pos,0,sizeof(int)*dimension);
2148 	while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
2149 		str++;
2150 	}
2151 	if (*str == '*') {
2152 		str++;
2153 		i++;
2154 	}
2155 	while (*str != '\0') {
2156 		if (*str >= '0' && *str <= '9') {
2157 			if (flag == 0) {
2158 				i++;
2159 				flag = 1;
2160 			}
2161 			pos[i] = (pos[i]*10)+(*str-'0');
2162 		} else if (*str == '*') {
2163 			soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
2164 		} else {
2165 		  flag = 0;
2166 		}
2167 		str++;
2168 	}
2169 	return pos;
2170 }
2171 
calc_dimension(const char * str)2172 static int calc_dimension(const char* str)
2173 {
2174 	int i = 1;
2175 	while (*str != ']' && *str != '\0') {
2176 		if (*str == ',') {
2177     		i++;
2178 		}
2179 		str++;
2180 	}
2181 	return i;
2182 }
2183 
get_position_ex(int dimension,const char * str,int ** pos)2184 static void get_position_ex(int dimension, const char* str, int** pos)
2185 {
2186 	int i = 0;
2187 
2188 	memset(*pos,0,sizeof(int)*dimension);
2189 	while (*str != ']' && *str != '\0' && i < dimension) {
2190 		if (*str >= '0' && *str <= '9') {
2191 			(*pos)[i] = ((*pos)[i]*10)+(*str-'0');
2192 		} else if (*str == ',') {
2193 			i++;
2194 		}
2195 		str++;
2196 	}
2197 }
2198 
get_position(int dimension,const char * str)2199 static int* get_position(int dimension, const char* str)
2200 {
2201 	int *pos;
2202 
2203 	pos = safe_emalloc(sizeof(int), dimension, 0);
2204 	get_position_ex(dimension, str, &pos);
2205 	return pos;
2206 }
2207 
add_xml_array_elements(xmlNodePtr xmlParam,sdlTypePtr type,encodePtr enc,xmlNsPtr ns,int dimension,int * dims,zval * data,int style TSRMLS_DC)2208 static void add_xml_array_elements(xmlNodePtr xmlParam,
2209                                    sdlTypePtr type,
2210                                    encodePtr enc,
2211                                    xmlNsPtr ns,
2212                                    int dimension ,
2213                                    int* dims,
2214                                    zval* data,
2215                                    int style
2216                                    TSRMLS_DC)
2217 {
2218 	int j;
2219 
2220 	if (data && Z_TYPE_P(data) == IS_ARRAY) {
2221 	 	zend_hash_internal_pointer_reset(data->value.ht);
2222 		for (j=0; j<dims[0]; j++) {
2223  			zval **zdata;
2224 
2225  			if (zend_hash_get_current_data(data->value.ht, (void **)&zdata) != SUCCESS) {
2226  				zdata = NULL;
2227  			}
2228  			if (dimension == 1) {
2229 	 			xmlNodePtr xparam;
2230 
2231 	 			if (zdata) {
2232 	 				if (enc == NULL) {
2233  						xparam = master_to_xml(get_conversion((*zdata)->type), (*zdata), style, xmlParam TSRMLS_CC);
2234  					} else {
2235  						xparam = master_to_xml(enc, (*zdata), style, xmlParam TSRMLS_CC);
2236 		 			}
2237 		 		} else {
2238 					xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2239 					xmlAddChild(xmlParam, xparam);
2240 		 		}
2241 
2242 	 			if (type) {
2243  					xmlNodeSetName(xparam, BAD_CAST(type->name));
2244  				} else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2245 					xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2246 					xmlSetNs(xparam, ns);
2247 				} else {
2248  					xmlNodeSetName(xparam, BAD_CAST("item"));
2249  				}
2250  			} else {
2251  				if (zdata) {
2252 	 			  add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, *zdata, style TSRMLS_CC);
2253 	 			} else {
2254 	 			  add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style TSRMLS_CC);
2255 	 			}
2256  			}
2257  			zend_hash_move_forward(data->value.ht);
2258  		}
2259  	} else {
2260 		for (j=0; j<dims[0]; j++) {
2261  			if (dimension == 1) {
2262 	 			xmlNodePtr xparam;
2263 
2264 				xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2265 				xmlAddChild(xmlParam, xparam);
2266 	 			if (type) {
2267  					xmlNodeSetName(xparam, BAD_CAST(type->name));
2268  				} else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2269 					xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2270 					xmlSetNs(xparam, ns);
2271 				} else {
2272  					xmlNodeSetName(xparam, BAD_CAST("item"));
2273  				}
2274  			} else {
2275  			  add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style TSRMLS_CC);
2276  			}
2277 		}
2278  	}
2279 }
2280 
array_num_elements(HashTable * ht)2281 static inline int array_num_elements(HashTable* ht)
2282 {
2283 	if (ht->pListTail && ht->pListTail->nKeyLength == 0) {
2284 		return ht->pListTail->h-1;
2285 	}
2286 	return 0;
2287 }
2288 
to_xml_array(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)2289 static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
2290 {
2291 	sdlTypePtr sdl_type = type->sdl_type;
2292 	sdlTypePtr element_type = NULL;
2293 	smart_str array_type = {0}, array_size = {0};
2294 	int i;
2295 	xmlNodePtr xmlParam;
2296 	encodePtr enc = NULL;
2297 	int dimension = 1;
2298 	int* dims;
2299 	int soap_version;
2300 	zval *array_copy = NULL;
2301 
2302 	soap_version = SOAP_GLOBAL(soap_version);
2303 
2304 	xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2305 	xmlAddChild(parent, xmlParam);
2306 
2307 	if (!data || Z_TYPE_P(data) == IS_NULL) {
2308 		if (style == SOAP_ENCODED) {
2309 			set_xsi_nil(xmlParam);
2310 			if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
2311 				set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
2312 			} else {
2313 				set_ns_and_type(xmlParam, type);
2314 			}
2315 		}
2316 		return xmlParam;
2317 	}
2318 
2319 	if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), zend_ce_traversable TSRMLS_CC)) {
2320 		zend_object_iterator   *iter;
2321 		zend_class_entry       *ce = Z_OBJCE_P(data);
2322 		zval                  **val;
2323 		char                   *str_key;
2324 		uint                    str_key_len;
2325 		ulong                   int_key;
2326 		int                     key_type;
2327 
2328 		ALLOC_ZVAL(array_copy);
2329 		INIT_PZVAL(array_copy);
2330 		array_init(array_copy);
2331 
2332 		iter = ce->get_iterator(ce, data, 0 TSRMLS_CC);
2333 
2334 		if (EG(exception)) {
2335 			goto iterator_done;
2336 		}
2337 
2338 		if (iter->funcs->rewind) {
2339 			iter->funcs->rewind(iter TSRMLS_CC);
2340 			if (EG(exception)) {
2341 				goto iterator_done;
2342 			}
2343 		}
2344 
2345 		while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
2346 			if (EG(exception)) {
2347 				goto iterator_done;
2348 			}
2349 
2350 			iter->funcs->get_current_data(iter, &val TSRMLS_CC);
2351 			if (EG(exception)) {
2352 				goto iterator_done;
2353 			}
2354 			if (iter->funcs->get_current_key) {
2355 				key_type = iter->funcs->get_current_key(iter, &str_key, &str_key_len, &int_key TSRMLS_CC);
2356 				if (EG(exception)) {
2357 					goto iterator_done;
2358 				}
2359 				switch(key_type) {
2360 					case HASH_KEY_IS_STRING:
2361 						add_assoc_zval_ex(array_copy, str_key, str_key_len, *val);
2362 						efree(str_key);
2363 						break;
2364 					case HASH_KEY_IS_LONG:
2365 						add_index_zval(array_copy, int_key, *val);
2366 						break;
2367 				}
2368 			} else {
2369 				add_next_index_zval(array_copy, *val);
2370 			}
2371 			Z_ADDREF_PP(val);
2372 
2373 			iter->funcs->move_forward(iter TSRMLS_CC);
2374 			if (EG(exception)) {
2375 				goto iterator_done;
2376 			}
2377 		}
2378 iterator_done:
2379 		iter->funcs->dtor(iter TSRMLS_CC);
2380 		if (EG(exception)) {
2381 			zval_ptr_dtor(&array_copy);
2382 			array_copy = NULL;
2383 		} else {
2384 			data = array_copy;
2385 		}
2386 	}
2387 
2388 	if (Z_TYPE_P(data) == IS_ARRAY) {
2389 		sdlAttributePtr *arrayType;
2390 		sdlExtraAttributePtr *ext;
2391 		sdlTypePtr elementType;
2392 
2393 		i = zend_hash_num_elements(Z_ARRVAL_P(data));
2394 
2395 		if (sdl_type &&
2396 		    sdl_type->attributes &&
2397 		    zend_hash_find(sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
2398 		      sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType"),
2399 		      (void **)&arrayType) == SUCCESS &&
2400 		    (*arrayType)->extraAttributes &&
2401 		    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType"), (void **)&ext) == SUCCESS) {
2402 
2403 			char *value, *end;
2404 			zval** el;
2405 
2406 			value = estrdup((*ext)->val);
2407 			end = strrchr(value,'[');
2408 			if (end) {
2409 				*end = '\0';
2410 				end++;
2411 				dimension = calc_dimension(end);
2412 			}
2413 			if ((*ext)->ns != NULL) {
2414 				enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, value);
2415 				get_type_str(xmlParam, (*ext)->ns, value, &array_type);
2416 			} else {
2417 				smart_str_appends(&array_type, value);
2418 			}
2419 
2420 			dims = safe_emalloc(sizeof(int), dimension, 0);
2421 			dims[0] = i;
2422 			el = &data;
2423 			for (i = 1; i < dimension; i++) {
2424 				if (el != NULL && Z_TYPE_PP(el) == IS_ARRAY &&
2425 				    zend_hash_num_elements(Z_ARRVAL_PP(el)) > 0) {
2426 				  zend_hash_internal_pointer_reset(Z_ARRVAL_PP(el));
2427 					zend_hash_get_current_data(Z_ARRVAL_PP(el), (void**)&el);
2428 					if (Z_TYPE_PP(el) == IS_ARRAY) {
2429 						dims[i] = zend_hash_num_elements(Z_ARRVAL_PP(el));
2430 					} else {
2431 						dims[i] = 0;
2432 					}
2433 				}
2434 			}
2435 
2436 			smart_str_append_long(&array_size, dims[0]);
2437 			for (i=1; i<dimension; i++) {
2438 				smart_str_appendc(&array_size, ',');
2439 				smart_str_append_long(&array_size, dims[i]);
2440 			}
2441 
2442 			efree(value);
2443 
2444 		} else if (sdl_type &&
2445 		           sdl_type->attributes &&
2446 		           zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
2447 		             sizeof(SOAP_1_2_ENC_NAMESPACE":itemType"),
2448 		             (void **)&arrayType) == SUCCESS &&
2449 		           (*arrayType)->extraAttributes &&
2450 		           zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType"), (void **)&ext) == SUCCESS) {
2451 			if ((*ext)->ns != NULL) {
2452 				enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, (*ext)->val);
2453 				get_type_str(xmlParam, (*ext)->ns, (*ext)->val, &array_type);
2454 			} else {
2455 				smart_str_appends(&array_type, (*ext)->val);
2456 			}
2457 			if (zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2458 			                   sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
2459 			                   (void **)&arrayType) == SUCCESS &&
2460 			    (*arrayType)->extraAttributes &&
2461 			    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
2462 				dimension = calc_dimension_12((*ext)->val);
2463 				dims = get_position_12(dimension, (*ext)->val);
2464 				if (dims[0] == 0) {dims[0] = i;}
2465 
2466 				smart_str_append_long(&array_size, dims[0]);
2467 				for (i=1; i<dimension; i++) {
2468 					smart_str_appendc(&array_size, ',');
2469 					smart_str_append_long(&array_size, dims[i]);
2470 				}
2471 			} else {
2472 				dims = emalloc(sizeof(int));
2473 				*dims = 0;
2474 				smart_str_append_long(&array_size, i);
2475 			}
2476 		} else if (sdl_type &&
2477 		           sdl_type->attributes &&
2478 		           zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2479 		             sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
2480 		             (void **)&arrayType) == SUCCESS &&
2481 		           (*arrayType)->extraAttributes &&
2482 		           zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraySize"), (void **)&ext) == SUCCESS) {
2483 			dimension = calc_dimension_12((*ext)->val);
2484 			dims = get_position_12(dimension, (*ext)->val);
2485 			if (dims[0] == 0) {dims[0] = i;}
2486 
2487 			smart_str_append_long(&array_size, dims[0]);
2488 			for (i=1; i<dimension; i++) {
2489 				smart_str_appendc(&array_size, ',');
2490 				smart_str_append_long(&array_size, dims[i]);
2491 			}
2492 
2493 			if (sdl_type && sdl_type->elements &&
2494 			    zend_hash_num_elements(sdl_type->elements) == 1 &&
2495 			    (zend_hash_internal_pointer_reset(sdl_type->elements),
2496 			     zend_hash_get_current_data(sdl_type->elements, (void**)&elementType) == SUCCESS) &&
2497 					(elementType = *(sdlTypePtr*)elementType) != NULL &&
2498 			     elementType->encode && elementType->encode->details.type_str) {
2499 				element_type = elementType;
2500 				enc = elementType->encode;
2501 				get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
2502 			} else {
2503 				enc = get_array_type(xmlParam, data, &array_type TSRMLS_CC);
2504 			}
2505 		} else if (sdl_type && sdl_type->elements &&
2506 		           zend_hash_num_elements(sdl_type->elements) == 1 &&
2507 		           (zend_hash_internal_pointer_reset(sdl_type->elements),
2508 		            zend_hash_get_current_data(sdl_type->elements, (void**)&elementType) == SUCCESS) &&
2509 		           (elementType = *(sdlTypePtr*)elementType) != NULL &&
2510 		           elementType->encode && elementType->encode->details.type_str) {
2511 
2512 			element_type = elementType;
2513 			enc = elementType->encode;
2514 			get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
2515 
2516 			smart_str_append_long(&array_size, i);
2517 
2518 			dims = safe_emalloc(sizeof(int), dimension, 0);
2519 			dims[0] = i;
2520 		} else {
2521 
2522 			enc = get_array_type(xmlParam, data, &array_type TSRMLS_CC);
2523 			smart_str_append_long(&array_size, i);
2524 			dims = safe_emalloc(sizeof(int), dimension, 0);
2525 			dims[0] = i;
2526 		}
2527 
2528 		if (style == SOAP_ENCODED) {
2529 			if (soap_version == SOAP_1_1) {
2530 				smart_str_0(&array_type);
2531 				if (strcmp(array_type.c,"xsd:anyType") == 0) {
2532 					smart_str_free(&array_type);
2533 					smart_str_appendl(&array_type,"xsd:ur-type",sizeof("xsd:ur-type")-1);
2534 				}
2535 				smart_str_appendc(&array_type, '[');
2536 				smart_str_append(&array_type, &array_size);
2537 				smart_str_appendc(&array_type, ']');
2538 				smart_str_0(&array_type);
2539 				set_ns_prop(xmlParam, SOAP_1_1_ENC_NAMESPACE, "arrayType", array_type.c);
2540 			} else {
2541 				int i = 0;
2542 				while (i < array_size.len) {
2543 					if (array_size.c[i] == ',') {array_size.c[i] = ' ';}
2544 					++i;
2545 				}
2546 				smart_str_0(&array_type);
2547 				smart_str_0(&array_size);
2548 				set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "itemType", array_type.c);
2549 				set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "arraySize", array_size.c);
2550 			}
2551 		}
2552 		smart_str_free(&array_type);
2553 		smart_str_free(&array_size);
2554 
2555 		add_xml_array_elements(xmlParam, element_type, enc, enc?encode_add_ns(xmlParam,enc->details.ns):NULL, dimension, dims, data, style TSRMLS_CC);
2556 		efree(dims);
2557 	}
2558 	if (style == SOAP_ENCODED) {
2559 		if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
2560 			set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
2561 		} else {
2562 			set_ns_and_type(xmlParam, type);
2563 		}
2564 	}
2565 
2566 	if (array_copy) {
2567 		zval_ptr_dtor(&array_copy);
2568 	}
2569 
2570 	return xmlParam;
2571 }
2572 
to_zval_array(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)2573 static zval *to_zval_array(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
2574 {
2575 	zval *ret;
2576 	xmlNodePtr trav;
2577 	encodePtr enc = NULL;
2578 	int dimension = 1;
2579 	int* dims = NULL;
2580 	int* pos = NULL;
2581 	xmlAttrPtr attr;
2582 	sdlPtr sdl;
2583 	sdlAttributePtr *arrayType;
2584 	sdlExtraAttributePtr *ext;
2585 	sdlTypePtr elementType;
2586 
2587 	MAKE_STD_ZVAL(ret);
2588 	FIND_XML_NULL(data, ret);
2589 	sdl = SOAP_GLOBAL(sdl);
2590 
2591 	if (data &&
2592 	    (attr = get_attribute(data->properties,"arrayType")) &&
2593 	    attr->children && attr->children->content) {
2594 		char *type, *end, *ns;
2595 		xmlNsPtr nsptr;
2596 
2597 		parse_namespace(attr->children->content, &type, &ns);
2598 		nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
2599 
2600 		end = strrchr(type,'[');
2601 		if (end) {
2602 			*end = '\0';
2603 			dimension = calc_dimension(end+1);
2604 			dims = get_position(dimension, end+1);
2605 		}
2606 		if (nsptr != NULL) {
2607 			enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
2608 		}
2609 		efree(type);
2610 		if (ns) {efree(ns);}
2611 
2612 	} else if ((attr = get_attribute(data->properties,"itemType")) &&
2613 	    attr->children &&
2614 	    attr->children->content) {
2615 		char *type, *ns;
2616 		xmlNsPtr nsptr;
2617 
2618 		parse_namespace(attr->children->content, &type, &ns);
2619 		nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
2620 		if (nsptr != NULL) {
2621 			enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
2622 		}
2623 		efree(type);
2624 		if (ns) {efree(ns);}
2625 
2626 		if ((attr = get_attribute(data->properties,"arraySize")) &&
2627 		    attr->children && attr->children->content) {
2628 			dimension = calc_dimension_12((char*)attr->children->content);
2629 			dims = get_position_12(dimension, (char*)attr->children->content);
2630 		} else {
2631 			dims = emalloc(sizeof(int));
2632 			*dims = 0;
2633 		}
2634 
2635 	} else if ((attr = get_attribute(data->properties,"arraySize")) &&
2636 	    attr->children && attr->children->content) {
2637 
2638 		dimension = calc_dimension_12((char*)attr->children->content);
2639 		dims = get_position_12(dimension, (char*)attr->children->content);
2640 
2641 	} else if (type->sdl_type != NULL &&
2642 	           type->sdl_type->attributes != NULL &&
2643 	           zend_hash_find(type->sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
2644 	                          sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType"),
2645 	                          (void **)&arrayType) == SUCCESS &&
2646 	           (*arrayType)->extraAttributes &&
2647 	           zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType"), (void **)&ext) == SUCCESS) {
2648 		char *type, *end;
2649 
2650 		type = estrdup((*ext)->val);
2651 		end = strrchr(type,'[');
2652 		if (end) {
2653 			*end = '\0';
2654 		}
2655 		if ((*ext)->ns != NULL) {
2656 			enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, type);
2657 		}
2658 		efree(type);
2659 
2660 		dims = emalloc(sizeof(int));
2661 		*dims = 0;
2662 
2663 	} else if (type->sdl_type != NULL &&
2664 	           type->sdl_type->attributes != NULL &&
2665 	           zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
2666 	                          sizeof(SOAP_1_2_ENC_NAMESPACE":itemType"),
2667 	                          (void **)&arrayType) == SUCCESS &&
2668 	           (*arrayType)->extraAttributes &&
2669 	           zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType"), (void **)&ext) == SUCCESS) {
2670 
2671 		if ((*ext)->ns != NULL) {
2672 			enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, (*ext)->val);
2673 		}
2674 
2675 		if (zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2676 		                   sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
2677 		                   (void **)&arrayType) == SUCCESS &&
2678 		    (*arrayType)->extraAttributes &&
2679 		    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
2680 			dimension = calc_dimension_12((*ext)->val);
2681 			dims = get_position_12(dimension, (*ext)->val);
2682 		} else {
2683 			dims = emalloc(sizeof(int));
2684 			*dims = 0;
2685 		}
2686 	} else if (type->sdl_type != NULL &&
2687 	           type->sdl_type->attributes != NULL &&
2688 	           zend_hash_find(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2689 	                          sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
2690 	                          (void **)&arrayType) == SUCCESS &&
2691 	           (*arrayType)->extraAttributes &&
2692 	           zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
2693 
2694 		dimension = calc_dimension_12((*ext)->val);
2695 		dims = get_position_12(dimension, (*ext)->val);
2696 		if (type->sdl_type && type->sdl_type->elements &&
2697 		    zend_hash_num_elements(type->sdl_type->elements) == 1 &&
2698 		    (zend_hash_internal_pointer_reset(type->sdl_type->elements),
2699 		     zend_hash_get_current_data(type->sdl_type->elements, (void**)&elementType) == SUCCESS) &&
2700 		    (elementType = *(sdlTypePtr*)elementType) != NULL &&
2701 		    elementType->encode) {
2702 			enc = elementType->encode;
2703 		}
2704 	} else if (type->sdl_type && type->sdl_type->elements &&
2705 	           zend_hash_num_elements(type->sdl_type->elements) == 1 &&
2706 	           (zend_hash_internal_pointer_reset(type->sdl_type->elements),
2707 	            zend_hash_get_current_data(type->sdl_type->elements, (void**)&elementType) == SUCCESS) &&
2708 	           (elementType = *(sdlTypePtr*)elementType) != NULL &&
2709 	           elementType->encode) {
2710 		enc = elementType->encode;
2711 	}
2712 	if (dims == NULL) {
2713 		dimension = 1;
2714 		dims = emalloc(sizeof(int));
2715 		*dims = 0;
2716 	}
2717 	pos = safe_emalloc(sizeof(int), dimension, 0);
2718 	memset(pos,0,sizeof(int)*dimension);
2719 	if (data &&
2720 	    (attr = get_attribute(data->properties,"offset")) &&
2721 	     attr->children && attr->children->content) {
2722 		char* tmp = strrchr((char*)attr->children->content,'[');
2723 
2724 		if (tmp == NULL) {
2725 			tmp = (char*)attr->children->content;
2726 		}
2727 		get_position_ex(dimension, tmp, &pos);
2728 	}
2729 
2730 	array_init(ret);
2731 	trav = data->children;
2732 	while (trav) {
2733 		if (trav->type == XML_ELEMENT_NODE) {
2734 			int i;
2735 			zval *tmpVal, *ar;
2736 			xmlAttrPtr position = get_attribute(trav->properties,"position");
2737 
2738 			tmpVal = master_to_zval(enc, trav TSRMLS_CC);
2739 			if (position != NULL && position->children && position->children->content) {
2740 				char* tmp = strrchr((char*)position->children->content, '[');
2741 				if (tmp == NULL) {
2742 					tmp = (char*)position->children->content;
2743 				}
2744 				get_position_ex(dimension, tmp, &pos);
2745 			}
2746 
2747 			/* Get/Create intermediate arrays for multidimensional arrays */
2748 			i = 0;
2749 			ar = ret;
2750 			while (i < dimension-1) {
2751 				zval** ar2;
2752 				if (zend_hash_index_find(Z_ARRVAL_P(ar), pos[i], (void**)&ar2) == SUCCESS) {
2753 					ar = *ar2;
2754 				} else {
2755 					zval *tmpAr;
2756 					MAKE_STD_ZVAL(tmpAr);
2757 					array_init(tmpAr);
2758 					zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpAr, sizeof(zval*), (void**)&ar2);
2759 					ar = *ar2;
2760 				}
2761 				i++;
2762 			}
2763 			zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpVal, sizeof(zval *), NULL);
2764 
2765 			/* Increment position */
2766 			i = dimension;
2767 			while (i > 0) {
2768 			  i--;
2769 			  pos[i]++;
2770 				if (pos[i] >= dims[i]) {
2771 					if (i > 0) {
2772 						pos[i] = 0;
2773 					} else {
2774 						/* TODO: Array index overflow */
2775 					}
2776 				} else {
2777 				  break;
2778 				}
2779 			}
2780 		}
2781 		trav = trav->next;
2782 	}
2783 	efree(dims);
2784 	efree(pos);
2785 	return ret;
2786 }
2787 
2788 /* Map encode/decode */
to_xml_map(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)2789 static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
2790 {
2791 	xmlNodePtr xmlParam;
2792 	int i;
2793 
2794 	xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2795 	xmlAddChild(parent, xmlParam);
2796 	FIND_ZVAL_NULL(data, xmlParam, style);
2797 
2798 	if (Z_TYPE_P(data) == IS_ARRAY) {
2799 		i = zend_hash_num_elements(Z_ARRVAL_P(data));
2800 		zend_hash_internal_pointer_reset(data->value.ht);
2801 		for (;i > 0;i--) {
2802 			xmlNodePtr xparam, item;
2803 			xmlNodePtr key;
2804 			zval **temp_data;
2805 			char *key_val;
2806 			ulong int_val;
2807 
2808 			zend_hash_get_current_data(data->value.ht, (void **)&temp_data);
2809 			item = xmlNewNode(NULL, BAD_CAST("item"));
2810 			xmlAddChild(xmlParam, item);
2811 			key = xmlNewNode(NULL, BAD_CAST("key"));
2812 			xmlAddChild(item,key);
2813 			if (zend_hash_get_current_key(data->value.ht, &key_val, &int_val, FALSE) == HASH_KEY_IS_STRING) {
2814 				if (style == SOAP_ENCODED) {
2815 					set_xsi_type(key, "xsd:string");
2816 				}
2817 				xmlNodeSetContent(key, BAD_CAST(key_val));
2818 			} else {
2819 				smart_str tmp = {0};
2820 				smart_str_append_long(&tmp, int_val);
2821 				smart_str_0(&tmp);
2822 
2823 				if (style == SOAP_ENCODED) {
2824 					set_xsi_type(key, "xsd:int");
2825 				}
2826 				xmlNodeSetContentLen(key, BAD_CAST(tmp.c), tmp.len);
2827 
2828 				smart_str_free(&tmp);
2829 			}
2830 
2831 			xparam = master_to_xml(get_conversion((*temp_data)->type), (*temp_data), style, item TSRMLS_CC);
2832 			xmlNodeSetName(xparam, BAD_CAST("value"));
2833 
2834 			zend_hash_move_forward(data->value.ht);
2835 		}
2836 	}
2837 	if (style == SOAP_ENCODED) {
2838 		set_ns_and_type(xmlParam, type);
2839 	}
2840 
2841 	return xmlParam;
2842 }
2843 
to_zval_map(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)2844 static zval *to_zval_map(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
2845 {
2846 	zval *ret, *key, *value;
2847 	xmlNodePtr trav, item, xmlKey, xmlValue;
2848 
2849 	MAKE_STD_ZVAL(ret);
2850 	FIND_XML_NULL(data, ret);
2851 
2852 	if (data && data->children) {
2853 		array_init(ret);
2854 		trav = data->children;
2855 
2856 		trav = data->children;
2857 		FOREACHNODE(trav, "item", item) {
2858 			xmlKey = get_node(item->children, "key");
2859 			if (!xmlKey) {
2860 				soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing key");
2861 			}
2862 
2863 			xmlValue = get_node(item->children, "value");
2864 			if (!xmlKey) {
2865 				soap_error0(E_ERROR,  "Encoding: Can't decode apache map, missing value");
2866 			}
2867 
2868 			key = master_to_zval(NULL, xmlKey TSRMLS_CC);
2869 			value = master_to_zval(NULL, xmlValue TSRMLS_CC);
2870 
2871 			if (Z_TYPE_P(key) == IS_STRING) {
2872 				zend_symtable_update(Z_ARRVAL_P(ret), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &value, sizeof(zval *), NULL);
2873 			} else if (Z_TYPE_P(key) == IS_LONG) {
2874 				zend_hash_index_update(Z_ARRVAL_P(ret), Z_LVAL_P(key), &value, sizeof(zval *), NULL);
2875 			} else {
2876 				soap_error0(E_ERROR,  "Encoding: Can't decode apache map, only Strings or Longs are allowd as keys");
2877 			}
2878 			zval_ptr_dtor(&key);
2879 		}
2880 		ENDFOREACH(trav);
2881 	} else {
2882 		ZVAL_NULL(ret);
2883 	}
2884 	return ret;
2885 }
2886 
2887 /* Unknown encode/decode */
guess_xml_convert(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)2888 static xmlNodePtr guess_xml_convert(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
2889 {
2890 	encodePtr  enc;
2891 	xmlNodePtr ret;
2892 
2893 	if (data) {
2894 		enc = get_conversion(data->type);
2895 	} else {
2896 		enc = get_conversion(IS_NULL);
2897 	}
2898 	ret = master_to_xml_int(enc, data, style, parent, 0 TSRMLS_CC);
2899 /*
2900 	if (style == SOAP_LITERAL && SOAP_GLOBAL(sdl)) {
2901 		set_ns_and_type(ret, &enc->details);
2902 	}
2903 */
2904 	return ret;
2905 }
2906 
guess_zval_convert(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)2907 static zval *guess_zval_convert(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
2908 {
2909 	encodePtr enc = NULL;
2910 	xmlAttrPtr tmpattr;
2911 	xmlChar *type_name = NULL;
2912 	zval *ret;
2913 
2914 	data = check_and_resolve_href(data);
2915 
2916 	if (data == NULL) {
2917 		enc = get_conversion(IS_NULL);
2918 	} else if (data->properties && get_attribute_ex(data->properties, "nil", XSI_NAMESPACE)) {
2919 		enc = get_conversion(IS_NULL);
2920 	} else {
2921 		tmpattr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
2922 		if (tmpattr != NULL) {
2923 		  type_name = tmpattr->children->content;
2924 			enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, tmpattr->children->content);
2925 			if (enc && type == &enc->details) {
2926 				enc = NULL;
2927 			}
2928 			if (enc != NULL) {
2929 			  encodePtr tmp = enc;
2930 			  while (tmp &&
2931 			         tmp->details.sdl_type != NULL &&
2932 			         tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
2933 			    if (enc == tmp->details.sdl_type->encode ||
2934 			        tmp == tmp->details.sdl_type->encode) {
2935 			    	enc = NULL;
2936 			    	break;
2937 			    }
2938 			    tmp = tmp->details.sdl_type->encode;
2939 			  }
2940 			}
2941 		}
2942 
2943 		if (enc == NULL) {
2944 			/* Didn't have a type, totally guess here */
2945 			/* Logic: has children = IS_OBJECT else IS_STRING */
2946 			xmlNodePtr trav;
2947 
2948 			if (get_attribute(data->properties, "arrayType") ||
2949 			    get_attribute(data->properties, "itemType") ||
2950 			    get_attribute(data->properties, "arraySize")) {
2951 				enc = get_conversion(SOAP_ENC_ARRAY);
2952 			} else {
2953 				enc = get_conversion(XSD_STRING);
2954 				trav = data->children;
2955 				while (trav != NULL) {
2956 					if (trav->type == XML_ELEMENT_NODE) {
2957 						enc = get_conversion(SOAP_ENC_OBJECT);
2958 						break;
2959 					}
2960 					trav = trav->next;
2961 				}
2962 			}
2963 		}
2964 	}
2965 	ret = master_to_zval_int(enc, data TSRMLS_CC);
2966 	if (SOAP_GLOBAL(sdl) && type_name && enc->details.sdl_type) {
2967 		zval* soapvar;
2968 		char *ns, *cptype;
2969 		xmlNsPtr nsptr;
2970 
2971 		MAKE_STD_ZVAL(soapvar);
2972 		object_init_ex(soapvar, soap_var_class_entry);
2973 		add_property_long(soapvar, "enc_type", enc->details.type);
2974 		Z_DELREF_P(ret);
2975 		add_property_zval(soapvar, "enc_value", ret);
2976 		parse_namespace(type_name, &cptype, &ns);
2977 		nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
2978 		add_property_string(soapvar, "enc_stype", cptype, 1);
2979 		if (nsptr) {
2980 			add_property_string(soapvar, "enc_ns", (char*)nsptr->href, 1);
2981 		}
2982 		efree(cptype);
2983 		if (ns) {efree(ns);}
2984 		ret = soapvar;
2985 	}
2986 	return ret;
2987 }
2988 
2989 /* Time encode/decode */
to_xml_datetime_ex(encodeTypePtr type,zval * data,char * format,int style,xmlNodePtr parent TSRMLS_DC)2990 static xmlNodePtr to_xml_datetime_ex(encodeTypePtr type, zval *data, char *format, int style, xmlNodePtr parent TSRMLS_DC)
2991 {
2992 	/* logic hacked from ext/standard/datetime.c */
2993 	struct tm *ta, tmbuf;
2994 	time_t timestamp;
2995 	int max_reallocs = 5;
2996 	size_t buf_len=64, real_len;
2997 	char *buf;
2998 	char tzbuf[8];
2999 
3000 	xmlNodePtr xmlParam;
3001 
3002 	xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
3003 	xmlAddChild(parent, xmlParam);
3004 	FIND_ZVAL_NULL(data, xmlParam, style);
3005 
3006 	if (Z_TYPE_P(data) == IS_LONG) {
3007 		timestamp = Z_LVAL_P(data);
3008 		ta = php_localtime_r(&timestamp, &tmbuf);
3009 		/*ta = php_gmtime_r(&timestamp, &tmbuf);*/
3010 		if (!ta) {
3011 			soap_error1(E_ERROR, "Encoding: Invalid timestamp %ld", Z_LVAL_P(data));
3012 		}
3013 
3014 		buf = (char *) emalloc(buf_len);
3015 		while ((real_len = strftime(buf, buf_len, format, ta)) == buf_len || real_len == 0) {
3016 			buf_len *= 2;
3017 			buf = (char *) erealloc(buf, buf_len);
3018 			if (!--max_reallocs) break;
3019 		}
3020 
3021 		/* Time zone support */
3022 #ifdef HAVE_TM_GMTOFF
3023 		snprintf(tzbuf, sizeof(tzbuf), "%c%02d:%02d", (ta->tm_gmtoff < 0) ? '-' : '+', abs(ta->tm_gmtoff / 3600), abs( (ta->tm_gmtoff % 3600) / 60 ));
3024 #else
3025 # if defined(__CYGWIN__) || defined(NETWARE)
3026 		snprintf(tzbuf, sizeof(tzbuf), "%c%02d:%02d", ((ta->tm_isdst ? _timezone - 3600:_timezone)>0)?'-':'+', abs((ta->tm_isdst ? _timezone - 3600 : _timezone) / 3600), abs(((ta->tm_isdst ? _timezone - 3600 : _timezone) % 3600) / 60));
3027 # else
3028 		snprintf(tzbuf, sizeof(tzbuf), "%c%02d:%02d", ((ta->tm_isdst ? timezone - 3600:timezone)>0)?'-':'+', abs((ta->tm_isdst ? timezone - 3600 : timezone) / 3600), abs(((ta->tm_isdst ? timezone - 3600 : timezone) % 3600) / 60));
3029 # endif
3030 #endif
3031 		if (strcmp(tzbuf,"+00:00") == 0) {
3032 		  strcpy(tzbuf,"Z");
3033 		  real_len++;
3034 		} else {
3035 			real_len += 6;
3036 		}
3037 		if (real_len >= buf_len) {
3038 			buf = (char *) erealloc(buf, real_len+1);
3039 		}
3040 		strcat(buf, tzbuf);
3041 
3042 		xmlNodeSetContent(xmlParam, BAD_CAST(buf));
3043 		efree(buf);
3044 	} else if (Z_TYPE_P(data) == IS_STRING) {
3045 		xmlNodeSetContentLen(xmlParam, BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
3046 	}
3047 
3048 	if (style == SOAP_ENCODED) {
3049 		set_ns_and_type(xmlParam, type);
3050 	}
3051 	return xmlParam;
3052 }
3053 
to_xml_duration(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3054 static xmlNodePtr to_xml_duration(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3055 {
3056 	/* TODO: '-'?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T([0-9]+H)?([0-9]+M)?([0-9]+S)? */
3057 	return to_xml_string(type, data, style, parent TSRMLS_CC);
3058 }
3059 
to_xml_datetime(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3060 static xmlNodePtr to_xml_datetime(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3061 {
3062 	return to_xml_datetime_ex(type, data, "%Y-%m-%dT%H:%M:%S", style, parent TSRMLS_CC);
3063 }
3064 
to_xml_time(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3065 static xmlNodePtr to_xml_time(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3066 {
3067 	/* TODO: microsecconds */
3068 	return to_xml_datetime_ex(type, data, "%H:%M:%S", style, parent TSRMLS_CC);
3069 }
3070 
to_xml_date(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3071 static xmlNodePtr to_xml_date(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3072 {
3073 	return to_xml_datetime_ex(type, data, "%Y-%m-%d", style, parent TSRMLS_CC);
3074 }
3075 
to_xml_gyearmonth(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3076 static xmlNodePtr to_xml_gyearmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3077 {
3078 	return to_xml_datetime_ex(type, data, "%Y-%m", style, parent TSRMLS_CC);
3079 }
3080 
to_xml_gyear(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3081 static xmlNodePtr to_xml_gyear(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3082 {
3083 	return to_xml_datetime_ex(type, data, "%Y", style, parent TSRMLS_CC);
3084 }
3085 
to_xml_gmonthday(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3086 static xmlNodePtr to_xml_gmonthday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3087 {
3088 	return to_xml_datetime_ex(type, data, "--%m-%d", style, parent TSRMLS_CC);
3089 }
3090 
to_xml_gday(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3091 static xmlNodePtr to_xml_gday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3092 {
3093 	return to_xml_datetime_ex(type, data, "---%d", style, parent TSRMLS_CC);
3094 }
3095 
to_xml_gmonth(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3096 static xmlNodePtr to_xml_gmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3097 {
3098 	return to_xml_datetime_ex(type, data, "--%m--", style, parent TSRMLS_CC);
3099 }
3100 
to_zval_list(encodeTypePtr enc,xmlNodePtr data TSRMLS_DC)3101 static zval* to_zval_list(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC) {
3102 	/*FIXME*/
3103 	return to_zval_stringc(enc, data TSRMLS_CC);
3104 }
3105 
to_xml_list(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3106 static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
3107 	xmlNodePtr ret;
3108 	encodePtr list_enc = NULL;
3109 
3110 	if (enc->sdl_type && enc->sdl_type->kind == XSD_TYPEKIND_LIST && enc->sdl_type->elements) {
3111 		sdlTypePtr *type;
3112 
3113 		zend_hash_internal_pointer_reset(enc->sdl_type->elements);
3114 		if (zend_hash_get_current_data(enc->sdl_type->elements, (void**)&type) == SUCCESS) {
3115 			list_enc = (*type)->encode;
3116 		}
3117 	}
3118 
3119 	ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
3120 	xmlAddChild(parent, ret);
3121 	FIND_ZVAL_NULL(data, ret, style);
3122 	if (Z_TYPE_P(data) == IS_ARRAY) {
3123 		zval **tmp;
3124 		smart_str list = {0};
3125 		HashTable *ht = Z_ARRVAL_P(data);
3126 
3127 		zend_hash_internal_pointer_reset(ht);
3128 		while (zend_hash_get_current_data(ht, (void**)&tmp) == SUCCESS) {
3129 			xmlNodePtr dummy = master_to_xml(list_enc, *tmp, SOAP_LITERAL, ret TSRMLS_CC);
3130 			if (dummy && dummy->children && dummy->children->content) {
3131 				if (list.len != 0) {
3132 					smart_str_appendc(&list, ' ');
3133 				}
3134 				smart_str_appends(&list, (char*)dummy->children->content);
3135 			} else {
3136 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
3137 			}
3138 			xmlUnlinkNode(dummy);
3139 			xmlFreeNode(dummy);
3140 			zend_hash_move_forward(ht);
3141 		}
3142 		smart_str_0(&list);
3143 		xmlNodeSetContentLen(ret, BAD_CAST(list.c), list.len);
3144 		smart_str_free(&list);
3145 	} else {
3146 		zval tmp = *data;
3147 		char *str, *start, *next;
3148 		smart_str list = {0};
3149 
3150 		if (Z_TYPE_P(data) != IS_STRING) {
3151 			zval_copy_ctor(&tmp);
3152 			convert_to_string(&tmp);
3153 			data = &tmp;
3154 		}
3155 		str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
3156 		whiteSpace_collapse(BAD_CAST(str));
3157 		start = str;
3158 		while (start != NULL && *start != '\0') {
3159 			xmlNodePtr dummy;
3160 			zval dummy_zval;
3161 
3162 			next = strchr(start,' ');
3163 			if (next != NULL) {
3164 			  *next = '\0';
3165 			  next++;
3166 			}
3167 			ZVAL_STRING(&dummy_zval, start, 0);
3168 			dummy = master_to_xml(list_enc, &dummy_zval, SOAP_LITERAL, ret TSRMLS_CC);
3169 			if (dummy && dummy->children && dummy->children->content) {
3170 				if (list.len != 0) {
3171 					smart_str_appendc(&list, ' ');
3172 				}
3173 				smart_str_appends(&list, (char*)dummy->children->content);
3174 			} else {
3175 				soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
3176 			}
3177 			xmlUnlinkNode(dummy);
3178 			xmlFreeNode(dummy);
3179 
3180 			start = next;
3181 		}
3182 		smart_str_0(&list);
3183 		xmlNodeSetContentLen(ret, BAD_CAST(list.c), list.len);
3184 		smart_str_free(&list);
3185 		efree(str);
3186 		if (data == &tmp) {zval_dtor(&tmp);}
3187 	}
3188 	return ret;
3189 }
3190 
to_xml_list1(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3191 static xmlNodePtr to_xml_list1(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
3192 	/*FIXME: minLength=1 */
3193 	return to_xml_list(enc,data,style, parent TSRMLS_CC);
3194 }
3195 
to_zval_union(encodeTypePtr enc,xmlNodePtr data TSRMLS_DC)3196 static zval* to_zval_union(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC) {
3197 	/*FIXME*/
3198 	return to_zval_list(enc, data TSRMLS_CC);
3199 }
3200 
to_xml_union(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3201 static xmlNodePtr to_xml_union(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC) {
3202 	/*FIXME*/
3203 	return to_xml_list(enc,data,style, parent TSRMLS_CC);
3204 }
3205 
to_zval_any(encodeTypePtr type,xmlNodePtr data TSRMLS_DC)3206 static zval *to_zval_any(encodeTypePtr type, xmlNodePtr data TSRMLS_DC)
3207 {
3208 	xmlBufferPtr buf;
3209 	zval *ret;
3210 
3211 	if (SOAP_GLOBAL(sdl) && SOAP_GLOBAL(sdl)->elements && data->name) {
3212 		smart_str nscat = {0};
3213 		sdlTypePtr *sdl_type;
3214 
3215 		if (data->ns && data->ns->href) {
3216 			smart_str_appends(&nscat, (char*)data->ns->href);
3217 			smart_str_appendc(&nscat, ':');
3218 		}
3219 		smart_str_appends(&nscat, (char*)data->name);
3220 		smart_str_0(&nscat);
3221 
3222 		if (zend_hash_find(SOAP_GLOBAL(sdl)->elements, nscat.c, nscat.len+1, (void **)&sdl_type) == SUCCESS &&
3223 		    (*sdl_type)->encode) {
3224 			smart_str_free(&nscat);
3225 			return master_to_zval_int((*sdl_type)->encode, data TSRMLS_CC);
3226 		}
3227 		smart_str_free(&nscat);
3228 	}
3229 
3230 	buf = xmlBufferCreate();
3231 	xmlNodeDump(buf, NULL, data, 0, 0);
3232 	MAKE_STD_ZVAL(ret);
3233 	ZVAL_STRING(ret, (char*)xmlBufferContent(buf), 1);
3234 	xmlBufferFree(buf);
3235 	return ret;
3236 }
3237 
to_xml_any(encodeTypePtr type,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3238 static xmlNodePtr to_xml_any(encodeTypePtr type, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3239 {
3240 	xmlNodePtr ret = NULL;
3241 
3242 	if (Z_TYPE_P(data) == IS_ARRAY) {
3243 		HashPosition pos;
3244 		zval **el;
3245 		encodePtr enc = get_conversion(XSD_ANYXML);
3246 		char *name;
3247 		uint name_len;
3248 		ulong idx;
3249 
3250 		for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(data), &pos);
3251 		     zend_hash_get_current_data_ex(Z_ARRVAL_P(data), (void **) &el, &pos) == SUCCESS;
3252 		     zend_hash_move_forward_ex(Z_ARRVAL_P(data), &pos)) {
3253 			ret = master_to_xml(enc, *el, style, parent TSRMLS_CC);
3254 		    if (ret &&
3255 		        ret->name != xmlStringTextNoenc &&
3256 		        zend_hash_get_current_key_ex(Z_ARRVAL_P(data), &name, &name_len, &idx, 0, &pos) == HASH_KEY_IS_STRING) {
3257 				xmlNodeSetName(ret, BAD_CAST(name));
3258 		    }
3259 		}
3260 		return ret;
3261 	}
3262 	if (Z_TYPE_P(data) == IS_STRING) {
3263 		ret = xmlNewTextLen(BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
3264 	} else {
3265 		zval tmp = *data;
3266 
3267 		zval_copy_ctor(&tmp);
3268 		convert_to_string(&tmp);
3269 		ret = xmlNewTextLen(BAD_CAST(Z_STRVAL(tmp)), Z_STRLEN(tmp));
3270 		zval_dtor(&tmp);
3271 	}
3272 
3273 	ret->name = xmlStringTextNoenc;
3274 	ret->parent = parent;
3275 	ret->doc = parent->doc;
3276 	ret->prev = parent->last;
3277 	ret->next = NULL;
3278 	if (parent->last) {
3279 		parent->last->next = ret;
3280 	} else {
3281 		parent->children = ret;
3282 	}
3283 	parent->last = ret;
3284 
3285 	return ret;
3286 }
3287 
sdl_guess_convert_zval(encodeTypePtr enc,xmlNodePtr data TSRMLS_DC)3288 zval *sdl_guess_convert_zval(encodeTypePtr enc, xmlNodePtr data TSRMLS_DC)
3289 {
3290 	sdlTypePtr type;
3291 
3292 	type = enc->sdl_type;
3293 	if (type == NULL) {
3294 		return guess_zval_convert(enc, data TSRMLS_CC);
3295 	}
3296 /*FIXME: restriction support
3297 	if (type && type->restrictions &&
3298 	    data &&  data->children && data->children->content) {
3299 		if (type->restrictions->whiteSpace && type->restrictions->whiteSpace->value) {
3300 			if (strcmp(type->restrictions->whiteSpace->value,"replace") == 0) {
3301 				whiteSpace_replace(data->children->content);
3302 			} else if (strcmp(type->restrictions->whiteSpace->value,"collapse") == 0) {
3303 				whiteSpace_collapse(data->children->content);
3304 			}
3305 		}
3306 		if (type->restrictions->enumeration) {
3307 			if (!zend_hash_exists(type->restrictions->enumeration,data->children->content,strlen(data->children->content)+1)) {
3308 				soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\"", data->children->content);
3309 			}
3310 		}
3311 		if (type->restrictions->minLength &&
3312 		    strlen(data->children->content) < type->restrictions->minLength->value) {
3313 		  soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
3314 		}
3315 		if (type->restrictions->maxLength &&
3316 		    strlen(data->children->content) > type->restrictions->maxLength->value) {
3317 		  soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
3318 		}
3319 		if (type->restrictions->length &&
3320 		    strlen(data->children->content) != type->restrictions->length->value) {
3321 		  soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
3322 		}
3323 	}
3324 */
3325 	switch (type->kind) {
3326 		case XSD_TYPEKIND_SIMPLE:
3327 			if (type->encode && enc != &type->encode->details) {
3328 				return master_to_zval_int(type->encode, data TSRMLS_CC);
3329 			} else {
3330 				return guess_zval_convert(enc, data TSRMLS_CC);
3331 			}
3332 			break;
3333 		case XSD_TYPEKIND_LIST:
3334 			return to_zval_list(enc, data TSRMLS_CC);
3335 		case XSD_TYPEKIND_UNION:
3336 			return to_zval_union(enc, data TSRMLS_CC);
3337 		case XSD_TYPEKIND_COMPLEX:
3338 		case XSD_TYPEKIND_RESTRICTION:
3339 		case XSD_TYPEKIND_EXTENSION:
3340 			if (type->encode &&
3341 			    (type->encode->details.type == IS_ARRAY ||
3342 			     type->encode->details.type == SOAP_ENC_ARRAY)) {
3343 				return to_zval_array(enc, data TSRMLS_CC);
3344 			}
3345 			return to_zval_object(enc, data TSRMLS_CC);
3346 		default:
3347 	  	soap_error0(E_ERROR, "Encoding: Internal Error");
3348 			return guess_zval_convert(enc, data TSRMLS_CC);
3349 	}
3350 }
3351 
sdl_guess_convert_xml(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent TSRMLS_DC)3352 xmlNodePtr sdl_guess_convert_xml(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent TSRMLS_DC)
3353 {
3354 	sdlTypePtr type;
3355 	xmlNodePtr ret = NULL;
3356 
3357 	type = enc->sdl_type;
3358 
3359 	if (type == NULL) {
3360 		ret = guess_xml_convert(enc, data, style, parent TSRMLS_CC);
3361 		if (style == SOAP_ENCODED) {
3362 			set_ns_and_type(ret, enc);
3363 		}
3364 		return ret;
3365 	}
3366 /*FIXME: restriction support
3367 	if (type) {
3368 		if (type->restrictions && Z_TYPE_P(data) == IS_STRING) {
3369 			if (type->restrictions->enumeration) {
3370 				if (!zend_hash_exists(type->restrictions->enumeration,Z_STRVAL_P(data),Z_STRLEN_P(data)+1)) {
3371 					soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\".", Z_STRVAL_P(data));
3372 				}
3373 			}
3374 			if (type->restrictions->minLength &&
3375 			    Z_STRLEN_P(data) < type->restrictions->minLength->value) {
3376 		  	soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
3377 			}
3378 			if (type->restrictions->maxLength &&
3379 			    Z_STRLEN_P(data) > type->restrictions->maxLength->value) {
3380 		  	soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
3381 			}
3382 			if (type->restrictions->length &&
3383 			    Z_STRLEN_P(data) != type->restrictions->length->value) {
3384 		  	soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
3385 			}
3386 		}
3387 	}
3388 */
3389 	switch(type->kind) {
3390 		case XSD_TYPEKIND_SIMPLE:
3391 			if (type->encode && enc != &type->encode->details) {
3392 				ret = master_to_xml(type->encode, data, style, parent TSRMLS_CC);
3393 			} else {
3394 				ret = guess_xml_convert(enc, data, style, parent TSRMLS_CC);
3395 			}
3396 			break;
3397 		case XSD_TYPEKIND_LIST:
3398 			ret = to_xml_list(enc, data, style, parent TSRMLS_CC);
3399 			break;
3400 		case XSD_TYPEKIND_UNION:
3401 			ret = to_xml_union(enc, data, style, parent TSRMLS_CC);
3402 			break;
3403 		case XSD_TYPEKIND_COMPLEX:
3404 		case XSD_TYPEKIND_RESTRICTION:
3405 		case XSD_TYPEKIND_EXTENSION:
3406 			if (type->encode &&
3407 			    (type->encode->details.type == IS_ARRAY ||
3408 			     type->encode->details.type == SOAP_ENC_ARRAY)) {
3409 				return to_xml_array(enc, data, style, parent TSRMLS_CC);
3410 			} else {
3411 				return to_xml_object(enc, data, style, parent TSRMLS_CC);
3412 			}
3413 			break;
3414 		default:
3415 	  	soap_error0(E_ERROR, "Encoding: Internal Error");
3416 			break;
3417 	}
3418 	if (style == SOAP_ENCODED) {
3419 		set_ns_and_type(ret, enc);
3420 	}
3421 	return ret;
3422 }
3423 
check_and_resolve_href(xmlNodePtr data)3424 static xmlNodePtr check_and_resolve_href(xmlNodePtr data)
3425 {
3426 	if (data && data->properties) {
3427 		xmlAttrPtr href;
3428 
3429 		href = data->properties;
3430 		while (1) {
3431 			href = get_attribute(href, "href");
3432 			if (href == NULL || href->ns == NULL) {break;}
3433 			href = href->next;
3434 		}
3435 		if (href) {
3436 			/*  Internal href try and find node */
3437 			if (href->children->content[0] == '#') {
3438 				xmlNodePtr ret = get_node_with_attribute_recursive(data->doc->children, NULL, "id", (char*)&href->children->content[1]);
3439 				if (!ret) {
3440 					soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
3441 				}
3442 				return ret;
3443 			} else {
3444 				/*  TODO: External href....? */
3445 				soap_error1(E_ERROR, "Encoding: External reference '%s'", href->children->content);
3446 			}
3447 		}
3448 		/* SOAP 1.2 enc:id enc:ref */
3449 		href = get_attribute_ex(data->properties, "ref", SOAP_1_2_ENC_NAMESPACE);
3450 		if (href) {
3451 			xmlChar* id;
3452 			xmlNodePtr ret;
3453 
3454 			if (href->children->content[0] == '#') {
3455 				id = href->children->content+1;
3456 			} else {
3457 				id = href->children->content;
3458 			}
3459 			ret = get_node_with_attribute_recursive_ex(data->doc->children, NULL, NULL, "id", (char*)id, SOAP_1_2_ENC_NAMESPACE);
3460 			if (!ret) {
3461 				soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
3462 			} else if (ret == data) {
3463 				soap_error1(E_ERROR, "Encoding: Violation of id and ref information items '%s'", href->children->content);
3464 			}
3465 			return ret;
3466 		}
3467 	}
3468 	return data;
3469 }
3470 
set_ns_and_type(xmlNodePtr node,encodeTypePtr type)3471 static void set_ns_and_type(xmlNodePtr node, encodeTypePtr type)
3472 {
3473 	set_ns_and_type_ex(node, type->ns, type->type_str);
3474 }
3475 
set_ns_and_type_ex(xmlNodePtr node,char * ns,char * type)3476 static void set_ns_and_type_ex(xmlNodePtr node, char *ns, char *type)
3477 {
3478 	smart_str nstype = {0};
3479 	get_type_str(node, ns, type, &nstype);
3480 	set_xsi_type(node, nstype.c);
3481 	smart_str_free(&nstype);
3482 }
3483 
xmlSearchNsPrefixByHref(xmlDocPtr doc,xmlNodePtr node,const xmlChar * href)3484 static xmlNsPtr xmlSearchNsPrefixByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
3485 {
3486 	xmlNsPtr cur;
3487 	xmlNodePtr orig = node;
3488 
3489 	while (node) {
3490 		if (node->type == XML_ENTITY_REF_NODE ||
3491 		    node->type == XML_ENTITY_NODE ||
3492 		    node->type == XML_ENTITY_DECL) {
3493 			return NULL;
3494 		}
3495 		if (node->type == XML_ELEMENT_NODE) {
3496 			cur = node->nsDef;
3497 			while (cur != NULL) {
3498 				if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
3499 					if (xmlSearchNs(doc, node, cur->prefix) == cur) {
3500 						return cur;
3501 					}
3502 				}
3503 				cur = cur->next;
3504 			}
3505 			if (orig != node) {
3506 				cur = node->ns;
3507 				if (cur != NULL) {
3508 					if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
3509 						if (xmlSearchNs(doc, node, cur->prefix) == cur) {
3510 							return cur;
3511 						}
3512 					}
3513 				}
3514 			}
3515 		}
3516 		node = node->parent;
3517 	}
3518 	return NULL;
3519 }
3520 
encode_add_ns(xmlNodePtr node,const char * ns)3521 xmlNsPtr encode_add_ns(xmlNodePtr node, const char* ns)
3522 {
3523 	xmlNsPtr xmlns;
3524 
3525 	if (ns == NULL) {
3526 	  return NULL;
3527 	}
3528 
3529 	xmlns = xmlSearchNsByHref(node->doc, node, BAD_CAST(ns));
3530 	if (xmlns != NULL && xmlns->prefix == NULL) {
3531 		xmlns = xmlSearchNsPrefixByHref(node->doc, node, BAD_CAST(ns));
3532 	}
3533 	if (xmlns == NULL) {
3534 		xmlChar* prefix;
3535         TSRMLS_FETCH();
3536 
3537 		if (zend_hash_find(&SOAP_GLOBAL(defEncNs), (char*)ns, strlen(ns) + 1, (void **)&prefix) == SUCCESS) {
3538 			xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), prefix);
3539 		} else {
3540 			smart_str prefix = {0};
3541 			int num = ++SOAP_GLOBAL(cur_uniq_ns);
3542 
3543 			while (1) {
3544 				smart_str_appendl(&prefix, "ns", 2);
3545 				smart_str_append_long(&prefix, num);
3546 				smart_str_0(&prefix);
3547 				if (xmlSearchNs(node->doc, node, BAD_CAST(prefix.c)) == NULL) {
3548 					break;
3549 				}
3550 				smart_str_free(&prefix);
3551 				prefix.c = NULL;
3552 				prefix.len = 0;
3553 				num = ++SOAP_GLOBAL(cur_uniq_ns);
3554 			}
3555 
3556 			xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), BAD_CAST(prefix.c));
3557 			smart_str_free(&prefix);
3558 		}
3559 	}
3560 	return xmlns;
3561 }
3562 
set_ns_prop(xmlNodePtr node,char * ns,char * name,char * val)3563 static void set_ns_prop(xmlNodePtr node, char *ns, char *name, char *val)
3564 {
3565 	xmlSetNsProp(node, encode_add_ns(node, ns), BAD_CAST(name), BAD_CAST(val));
3566 }
3567 
set_xsi_nil(xmlNodePtr node)3568 static void set_xsi_nil(xmlNodePtr node)
3569 {
3570 	set_ns_prop(node, XSI_NAMESPACE, "nil", "true");
3571 }
3572 
set_xsi_type(xmlNodePtr node,char * type)3573 static void set_xsi_type(xmlNodePtr node, char *type)
3574 {
3575 	set_ns_prop(node, XSI_NAMESPACE, "type", type);
3576 }
3577 
encode_reset_ns()3578 void encode_reset_ns()
3579 {
3580 	TSRMLS_FETCH();
3581 	SOAP_GLOBAL(cur_uniq_ns) = 0;
3582 	SOAP_GLOBAL(cur_uniq_ref) = 0;
3583 	if (SOAP_GLOBAL(ref_map)) {
3584 		zend_hash_destroy(SOAP_GLOBAL(ref_map));
3585 	} else {
3586 		SOAP_GLOBAL(ref_map) = emalloc(sizeof(HashTable));
3587 	}
3588 	zend_hash_init(SOAP_GLOBAL(ref_map), 0, NULL, NULL, 0);
3589 }
3590 
encode_finish()3591 void encode_finish()
3592 {
3593 	TSRMLS_FETCH();
3594 	SOAP_GLOBAL(cur_uniq_ns) = 0;
3595 	SOAP_GLOBAL(cur_uniq_ref) = 0;
3596 	if (SOAP_GLOBAL(ref_map)) {
3597 		zend_hash_destroy(SOAP_GLOBAL(ref_map));
3598 		efree(SOAP_GLOBAL(ref_map));
3599 		SOAP_GLOBAL(ref_map) = NULL;
3600 	}
3601 }
3602 
get_conversion(int encode)3603 encodePtr get_conversion(int encode)
3604 {
3605 	encodePtr *enc = NULL;
3606 	TSRMLS_FETCH();
3607 
3608 	if (zend_hash_index_find(&SOAP_GLOBAL(defEncIndex), encode, (void **)&enc) == FAILURE) {
3609 		soap_error0(E_ERROR,  "Encoding: Cannot find encoding");
3610 		return NULL;
3611 	} else {
3612 		return *enc;
3613 	}
3614 }
3615 
is_map(zval * array)3616 static int is_map(zval *array)
3617 {
3618 	int i, count = zend_hash_num_elements(Z_ARRVAL_P(array));
3619 
3620 	zend_hash_internal_pointer_reset(Z_ARRVAL_P(array));
3621 	for (i = 0; i < count; i++) {
3622 		char *str_index;
3623 		ulong num_index;
3624 
3625 		if (zend_hash_get_current_key(Z_ARRVAL_P(array), &str_index, &num_index, 0) == HASH_KEY_IS_STRING ||
3626 		    num_index != i) {
3627 			return TRUE;
3628 		}
3629 		zend_hash_move_forward(Z_ARRVAL_P(array));
3630 	}
3631 	return FALSE;
3632 }
3633 
get_array_type(xmlNodePtr node,zval * array,smart_str * type TSRMLS_DC)3634 static encodePtr get_array_type(xmlNodePtr node, zval *array, smart_str *type TSRMLS_DC)
3635 {
3636 	HashTable *ht;
3637 	int i, count, cur_type, prev_type, different;
3638 	zval **tmp;
3639 	char *prev_stype = NULL, *cur_stype = NULL, *prev_ns = NULL, *cur_ns = NULL;
3640 
3641 	if (!array || Z_TYPE_P(array) != IS_ARRAY) {
3642 		smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
3643 		return get_conversion(XSD_ANYTYPE);
3644 	}
3645 
3646 	different = FALSE;
3647 	cur_type = prev_type = 0;
3648 	ht = HASH_OF(array);
3649 	count = zend_hash_num_elements(ht);
3650 
3651 	zend_hash_internal_pointer_reset(ht);
3652 	for (i = 0;i < count;i++) {
3653 		zend_hash_get_current_data(ht, (void **)&tmp);
3654 
3655 		if (Z_TYPE_PP(tmp) == IS_OBJECT &&
3656 		    Z_OBJCE_PP(tmp) == soap_var_class_entry) {
3657 			zval **ztype;
3658 
3659 			if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_type", sizeof("enc_type"), (void **)&ztype) == FAILURE ||
3660 			    Z_TYPE_PP(ztype) != IS_LONG) {
3661 				soap_error0(E_ERROR,  "Encoding: SoapVar has no 'enc_type' property");
3662 			}
3663 			cur_type = Z_LVAL_PP(ztype);
3664 
3665 			if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_stype", sizeof("enc_stype"), (void **)&ztype) == SUCCESS &&
3666 			    Z_TYPE_PP(ztype) == IS_STRING) {
3667 				cur_stype = Z_STRVAL_PP(ztype);
3668 			} else {
3669 				cur_stype = NULL;
3670 			}
3671 
3672 			if (zend_hash_find(Z_OBJPROP_PP(tmp), "enc_ns", sizeof("enc_ns"), (void **)&ztype) == SUCCESS &&
3673 			    Z_TYPE_PP(ztype) == IS_STRING) {
3674 				cur_ns = Z_STRVAL_PP(ztype);
3675 			} else {
3676 				cur_ns = NULL;
3677 			}
3678 
3679 		} else if (Z_TYPE_PP(tmp) == IS_ARRAY && is_map(*tmp)) {
3680 			cur_type = APACHE_MAP;
3681 			cur_stype = NULL;
3682 			cur_ns = NULL;
3683 		} else {
3684 			cur_type = Z_TYPE_PP(tmp);
3685 			cur_stype = NULL;
3686 			cur_ns = NULL;
3687 		}
3688 
3689 		if (i > 0) {
3690 			if ((cur_type != prev_type) ||
3691 			    (cur_stype != NULL && prev_stype != NULL && strcmp(cur_stype,prev_stype) != 0) ||
3692 			    (cur_stype == NULL && cur_stype != prev_stype) ||
3693 			    (cur_ns != NULL && prev_ns != NULL && strcmp(cur_ns,prev_ns) != 0) ||
3694 			    (cur_ns == NULL && cur_ns != prev_ns)) {
3695 				different = TRUE;
3696 				break;
3697 			}
3698 		}
3699 
3700 		prev_type = cur_type;
3701 		prev_stype = cur_stype;
3702 		prev_ns = cur_ns;
3703 		zend_hash_move_forward(ht);
3704 	}
3705 
3706 	if (different || count == 0) {
3707 		smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
3708 		return get_conversion(XSD_ANYTYPE);
3709 	} else {
3710 		encodePtr enc;
3711 
3712 		if (cur_stype != NULL) {
3713 			smart_str array_type = {0};
3714 
3715 			if (cur_ns) {
3716 				xmlNsPtr ns = encode_add_ns(node, cur_ns);
3717 
3718 				smart_str_appends(type, (char*)ns->prefix);
3719 				smart_str_appendc(type, ':');
3720 				smart_str_appends(&array_type, cur_ns);
3721 				smart_str_appendc(&array_type, ':');
3722 			}
3723 			smart_str_appends(type, cur_stype);
3724 			smart_str_0(type);
3725 			smart_str_appends(&array_type, cur_stype);
3726 			smart_str_0(&array_type);
3727 
3728 			enc = get_encoder_ex(SOAP_GLOBAL(sdl), array_type.c, array_type.len);
3729 			smart_str_free(&array_type);
3730 			return enc;
3731 		} else {
3732 			enc = get_conversion(cur_type);
3733 			get_type_str(node, enc->details.ns, enc->details.type_str, type);
3734 			return enc;
3735 		}
3736 	}
3737 }
3738 
get_type_str(xmlNodePtr node,const char * ns,const char * type,smart_str * ret)3739 static void get_type_str(xmlNodePtr node, const char* ns, const char* type, smart_str* ret)
3740 {
3741 	TSRMLS_FETCH();
3742 
3743 	if (ns) {
3744 		xmlNsPtr xmlns;
3745 		if (SOAP_GLOBAL(soap_version) == SOAP_1_2 &&
3746 		    strcmp(ns,SOAP_1_1_ENC_NAMESPACE) == 0) {
3747 			ns = SOAP_1_2_ENC_NAMESPACE;
3748 		} else if (SOAP_GLOBAL(soap_version) == SOAP_1_1 &&
3749 		           strcmp(ns,SOAP_1_2_ENC_NAMESPACE) == 0) {
3750 			ns = SOAP_1_1_ENC_NAMESPACE;
3751 		}
3752 		xmlns = encode_add_ns(node, ns);
3753 		smart_str_appends(ret, (char*)xmlns->prefix);
3754 		smart_str_appendc(ret, ':');
3755 	}
3756 	smart_str_appendl(ret, type, strlen(type));
3757 	smart_str_0(ret);
3758 }
3759 
delete_mapping(void * data)3760 static void delete_mapping(void *data)
3761 {
3762 	soapMappingPtr map = (soapMappingPtr)data;
3763 
3764 	if (map->to_xml) {
3765 		zval_ptr_dtor(&map->to_xml);
3766 	}
3767 	if (map->to_zval) {
3768 		zval_ptr_dtor(&map->to_zval);
3769 	}
3770 	efree(map);
3771 }
3772 
delete_encoder(void * encode)3773 void delete_encoder(void *encode)
3774 {
3775 	encodePtr t = *((encodePtr*)encode);
3776 	if (t->details.ns) {
3777 		efree(t->details.ns);
3778 	}
3779 	if (t->details.type_str) {
3780 		efree(t->details.type_str);
3781 	}
3782 	if (t->details.map) {
3783 		delete_mapping(t->details.map);
3784 	}
3785 	efree(t);
3786 }
3787 
delete_encoder_persistent(void * encode)3788 void delete_encoder_persistent(void *encode)
3789 {
3790 	encodePtr t = *((encodePtr*)encode);
3791 	if (t->details.ns) {
3792 		free(t->details.ns);
3793 	}
3794 	if (t->details.type_str) {
3795 		free(t->details.type_str);
3796 	}
3797 	/* we should never have mapping in persistent encoder */
3798 	assert(t->details.map == NULL);
3799 	free(t);
3800 }
3801