xref: /PHP-5.5/ext/soap/php_encoding.c (revision 75f40ae1)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2015 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 
2324 		ALLOC_ZVAL(array_copy);
2325 		INIT_PZVAL(array_copy);
2326 		array_init(array_copy);
2327 
2328 		iter = ce->get_iterator(ce, data, 0 TSRMLS_CC);
2329 
2330 		if (EG(exception)) {
2331 			goto iterator_done;
2332 		}
2333 
2334 		if (iter->funcs->rewind) {
2335 			iter->funcs->rewind(iter TSRMLS_CC);
2336 			if (EG(exception)) {
2337 				goto iterator_done;
2338 			}
2339 		}
2340 
2341 		while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
2342 			if (EG(exception)) {
2343 				goto iterator_done;
2344 			}
2345 
2346 			iter->funcs->get_current_data(iter, &val TSRMLS_CC);
2347 			if (EG(exception)) {
2348 				goto iterator_done;
2349 			}
2350 			if (iter->funcs->get_current_key) {
2351 				zval key;
2352 				iter->funcs->get_current_key(iter, &key TSRMLS_CC);
2353 				if (EG(exception)) {
2354 					goto iterator_done;
2355 				}
2356 				array_set_zval_key(Z_ARRVAL_P(array_copy), &key, *val);
2357 				zval_ptr_dtor(val);
2358 				zval_dtor(&key);
2359 			} else {
2360 				add_next_index_zval(array_copy, *val);
2361 			}
2362 			Z_ADDREF_PP(val);
2363 
2364 			iter->funcs->move_forward(iter TSRMLS_CC);
2365 			if (EG(exception)) {
2366 				goto iterator_done;
2367 			}
2368 		}
2369 iterator_done:
2370 		iter->funcs->dtor(iter TSRMLS_CC);
2371 		if (EG(exception)) {
2372 			zval_ptr_dtor(&array_copy);
2373 			array_copy = NULL;
2374 		} else {
2375 			data = array_copy;
2376 		}
2377 	}
2378 
2379 	if (Z_TYPE_P(data) == IS_ARRAY) {
2380 		sdlAttributePtr *arrayType;
2381 		sdlExtraAttributePtr *ext;
2382 		sdlTypePtr elementType;
2383 
2384 		i = zend_hash_num_elements(Z_ARRVAL_P(data));
2385 
2386 		if (sdl_type &&
2387 		    sdl_type->attributes &&
2388 		    zend_hash_find(sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
2389 		      sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType"),
2390 		      (void **)&arrayType) == SUCCESS &&
2391 		    (*arrayType)->extraAttributes &&
2392 		    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType"), (void **)&ext) == SUCCESS) {
2393 
2394 			char *value, *end;
2395 			zval** el;
2396 
2397 			value = estrdup((*ext)->val);
2398 			end = strrchr(value,'[');
2399 			if (end) {
2400 				*end = '\0';
2401 				end++;
2402 				dimension = calc_dimension(end);
2403 			}
2404 			if ((*ext)->ns != NULL) {
2405 				enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, value);
2406 				get_type_str(xmlParam, (*ext)->ns, value, &array_type);
2407 			} else {
2408 				smart_str_appends(&array_type, value);
2409 			}
2410 
2411 			dims = safe_emalloc(sizeof(int), dimension, 0);
2412 			dims[0] = i;
2413 			el = &data;
2414 			for (i = 1; i < dimension; i++) {
2415 				if (el != NULL && Z_TYPE_PP(el) == IS_ARRAY &&
2416 				    zend_hash_num_elements(Z_ARRVAL_PP(el)) > 0) {
2417 				  zend_hash_internal_pointer_reset(Z_ARRVAL_PP(el));
2418 					zend_hash_get_current_data(Z_ARRVAL_PP(el), (void**)&el);
2419 					if (Z_TYPE_PP(el) == IS_ARRAY) {
2420 						dims[i] = zend_hash_num_elements(Z_ARRVAL_PP(el));
2421 					} else {
2422 						dims[i] = 0;
2423 					}
2424 				}
2425 			}
2426 
2427 			smart_str_append_long(&array_size, dims[0]);
2428 			for (i=1; i<dimension; i++) {
2429 				smart_str_appendc(&array_size, ',');
2430 				smart_str_append_long(&array_size, dims[i]);
2431 			}
2432 
2433 			efree(value);
2434 
2435 		} else if (sdl_type &&
2436 		           sdl_type->attributes &&
2437 		           zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
2438 		             sizeof(SOAP_1_2_ENC_NAMESPACE":itemType"),
2439 		             (void **)&arrayType) == SUCCESS &&
2440 		           (*arrayType)->extraAttributes &&
2441 		           zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType"), (void **)&ext) == SUCCESS) {
2442 			if ((*ext)->ns != NULL) {
2443 				enc = get_encoder(SOAP_GLOBAL(sdl), (*ext)->ns, (*ext)->val);
2444 				get_type_str(xmlParam, (*ext)->ns, (*ext)->val, &array_type);
2445 			} else {
2446 				smart_str_appends(&array_type, (*ext)->val);
2447 			}
2448 			if (zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2449 			                   sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
2450 			                   (void **)&arrayType) == SUCCESS &&
2451 			    (*arrayType)->extraAttributes &&
2452 			    zend_hash_find((*arrayType)->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize"), (void **)&ext) == SUCCESS) {
2453 				dimension = calc_dimension_12((*ext)->val);
2454 				dims = get_position_12(dimension, (*ext)->val);
2455 				if (dims[0] == 0) {dims[0] = i;}
2456 
2457 				smart_str_append_long(&array_size, dims[0]);
2458 				for (i=1; i<dimension; i++) {
2459 					smart_str_appendc(&array_size, ',');
2460 					smart_str_append_long(&array_size, dims[i]);
2461 				}
2462 			} else {
2463 				dims = emalloc(sizeof(int));
2464 				*dims = 0;
2465 				smart_str_append_long(&array_size, i);
2466 			}
2467 		} else if (sdl_type &&
2468 		           sdl_type->attributes &&
2469 		           zend_hash_find(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2470 		             sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize"),
2471 		             (void **)&arrayType) == SUCCESS &&