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