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