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_ADDREF_P(prop);
1565 add_next_index_zval(&arr, prop);
1566 set_zval_property(ret, (char*)trav->name, &arr);
1567 prop = &arr;
1568 }
1569 /* Add array element */
1570 add_next_index_zval(prop, &tmpVal);
1571 }
1572 }
1573 trav = trav->next;
1574 }
1575 }
1576 return ret;
1577 }
1578
to_zval_object(zval * ret,encodeTypePtr type,xmlNodePtr data)1579 static zval *to_zval_object(zval *ret, encodeTypePtr type, xmlNodePtr data)
1580 {
1581 return to_zval_object_ex(ret, type, data, NULL);
1582 }
1583
1584
model_to_xml_object(xmlNodePtr node,sdlContentModelPtr model,zval * object,int style,int strict)1585 static int model_to_xml_object(xmlNodePtr node, sdlContentModelPtr model, zval *object, int style, int strict)
1586 {
1587 switch (model->kind) {
1588 case XSD_CONTENT_ELEMENT: {
1589 zval *data;
1590 xmlNodePtr property;
1591 encodePtr enc;
1592 zval rv;
1593
1594 data = get_zval_property(object, model->u.element->name, &rv);
1595 if (data &&
1596 Z_TYPE_P(data) == IS_NULL &&
1597 !model->u.element->nillable &&
1598 model->min_occurs > 0 &&
1599 !strict) {
1600 return 0;
1601 }
1602 if (data) {
1603 enc = model->u.element->encode;
1604 if ((model->max_occurs == -1 || model->max_occurs > 1) &&
1605 Z_TYPE_P(data) == IS_ARRAY &&
1606 !is_map(data)) {
1607 HashTable *ht = Z_ARRVAL_P(data);
1608 zval *val;
1609
1610 ZEND_HASH_FOREACH_VAL(ht, val) {
1611 ZVAL_DEREF(val);
1612 if (Z_TYPE_P(val) == IS_NULL && model->u.element->nillable) {
1613 property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1614 xmlAddChild(node, property);
1615 set_xsi_nil(property);
1616 } else {
1617 property = master_to_xml(enc, val, style, node);
1618 if (property->children && property->children->content &&
1619 model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
1620 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);
1621 }
1622 }
1623 xmlNodeSetName(property, BAD_CAST(model->u.element->name));
1624 if (style == SOAP_LITERAL &&
1625 model->u.element->namens &&
1626 model->u.element->form == XSD_FORM_QUALIFIED) {
1627 xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1628 xmlSetNs(property, nsp);
1629 }
1630 } ZEND_HASH_FOREACH_END();
1631 } else {
1632 if (Z_TYPE_P(data) == IS_NULL && model->u.element->nillable) {
1633 property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1634 xmlAddChild(node, property);
1635 set_xsi_nil(property);
1636 } else if (Z_TYPE_P(data) == IS_NULL && model->min_occurs == 0) {
1637 return 1;
1638 } else {
1639 property = master_to_xml(enc, data, style, node);
1640 if (property->children && property->children->content &&
1641 model->u.element->fixed && strcmp(model->u.element->fixed, (char*)property->children->content) != 0) {
1642 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);
1643 }
1644 }
1645 xmlNodeSetName(property, BAD_CAST(model->u.element->name));
1646 if (style == SOAP_LITERAL &&
1647 model->u.element->namens &&
1648 model->u.element->form == XSD_FORM_QUALIFIED) {
1649 xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1650 xmlSetNs(property, nsp);
1651 }
1652 }
1653 return 1;
1654 } else if (strict && model->u.element->nillable && model->min_occurs > 0) {
1655 property = xmlNewNode(NULL, BAD_CAST(model->u.element->name));
1656 xmlAddChild(node, property);
1657 set_xsi_nil(property);
1658 if (style == SOAP_LITERAL &&
1659 model->u.element->namens &&
1660 model->u.element->form == XSD_FORM_QUALIFIED) {
1661 xmlNsPtr nsp = encode_add_ns(property, model->u.element->namens);
1662 xmlSetNs(property, nsp);
1663 }
1664 return 1;
1665 } else if (model->min_occurs == 0) {
1666 return 2;
1667 } else {
1668 if (strict) {
1669 soap_error1(E_ERROR, "Encoding: object has no '%s' property", model->u.element->name);
1670 }
1671 return 0;
1672 }
1673 break;
1674 }
1675 case XSD_CONTENT_ANY: {
1676 zval *data;
1677 encodePtr enc;
1678 zval rv;
1679
1680 data = get_zval_property(object, "any", &rv);
1681 if (data) {
1682 enc = get_conversion(XSD_ANYXML);
1683 if ((model->max_occurs == -1 || model->max_occurs > 1) &&
1684 Z_TYPE_P(data) == IS_ARRAY &&
1685 !is_map(data)) {
1686 HashTable *ht = Z_ARRVAL_P(data);
1687 zval *val;
1688
1689 ZEND_HASH_FOREACH_VAL(ht, val) {
1690 master_to_xml(enc, val, style, node);
1691 } ZEND_HASH_FOREACH_END();
1692 } else {
1693 master_to_xml(enc, data, style, node);
1694 }
1695 return 1;
1696 } else if (model->min_occurs == 0) {
1697 return 2;
1698 } else {
1699 if (strict) {
1700 soap_error0(E_ERROR, "Encoding: object has no 'any' property");
1701 }
1702 return 0;
1703 }
1704 break;
1705 }
1706 case XSD_CONTENT_SEQUENCE:
1707 case XSD_CONTENT_ALL: {
1708 sdlContentModelPtr tmp;
1709
1710 ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1711 if (!model_to_xml_object(node, tmp, object, style, strict && (tmp->min_occurs > 0))) {
1712 if (!strict || tmp->min_occurs > 0) {
1713 return 0;
1714 }
1715 }
1716 strict = 1;
1717 } ZEND_HASH_FOREACH_END();
1718 return 1;
1719 }
1720 case XSD_CONTENT_CHOICE: {
1721 sdlContentModelPtr tmp;
1722 int ret = 0;
1723
1724 ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1725 int tmp_ret = model_to_xml_object(node, tmp, object, style, 0);
1726 if (tmp_ret == 1) {
1727 return 1;
1728 } else if (tmp_ret != 0) {
1729 ret = 1;
1730 }
1731 } ZEND_HASH_FOREACH_END();
1732 return ret;
1733 }
1734 case XSD_CONTENT_GROUP: {
1735 return model_to_xml_object(node, model->u.group->model, object, style, strict && model->min_occurs > 0);
1736 }
1737 default:
1738 break;
1739 }
1740 return 1;
1741 }
1742
model_array_element(sdlContentModelPtr model)1743 static sdlTypePtr model_array_element(sdlContentModelPtr model)
1744 {
1745 switch (model->kind) {
1746 case XSD_CONTENT_ELEMENT: {
1747 if (model->max_occurs == -1 || model->max_occurs > 1) {
1748 return model->u.element;
1749 } else {
1750 return NULL;
1751 }
1752 }
1753 case XSD_CONTENT_SEQUENCE:
1754 case XSD_CONTENT_ALL:
1755 case XSD_CONTENT_CHOICE: {
1756 sdlContentModelPtr tmp;
1757
1758 if (zend_hash_num_elements(model->u.content) != 1) {
1759 return NULL;
1760 }
1761 ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
1762 return model_array_element(tmp);
1763 } ZEND_HASH_FOREACH_END();
1764 }
1765 /* TODO Check this is correct */
1766 ZEND_FALLTHROUGH;
1767 case XSD_CONTENT_GROUP: {
1768 return model_array_element(model->u.group->model);
1769 }
1770 default:
1771 break;
1772 }
1773 return NULL;
1774 }
1775
to_xml_object(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1776 static xmlNodePtr to_xml_object(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1777 {
1778 xmlNodePtr xmlParam;
1779 HashTable *prop = NULL;
1780 sdlTypePtr sdlType = type->sdl_type;
1781
1782 if (!data || Z_TYPE_P(data) == IS_NULL) {
1783 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1784 xmlAddChild(parent, xmlParam);
1785 if (style == SOAP_ENCODED) {
1786 set_xsi_nil(xmlParam);
1787 set_ns_and_type(xmlParam, type);
1788 }
1789 return xmlParam;
1790 }
1791
1792 if (Z_TYPE_P(data) == IS_OBJECT) {
1793 prop = Z_OBJPROP_P(data);
1794 } else if (Z_TYPE_P(data) == IS_ARRAY) {
1795 prop = Z_ARRVAL_P(data);
1796 }
1797
1798 if (sdlType) {
1799 if (sdlType->kind == XSD_TYPEKIND_RESTRICTION &&
1800 sdlType->encode && type != &sdlType->encode->details) {
1801 encodePtr enc;
1802
1803 enc = sdlType->encode;
1804 while (enc && enc->details.sdl_type &&
1805 enc->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1806 enc->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1807 enc->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1808 enc = enc->details.sdl_type->encode;
1809 }
1810 if (enc) {
1811 zval rv;
1812 zval *tmp = get_zval_property(data, "_", &rv);
1813 if (tmp) {
1814 xmlParam = master_to_xml(enc, tmp, style, parent);
1815 } else if (prop == NULL) {
1816 xmlParam = master_to_xml(enc, data, style, parent);
1817 } else {
1818 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1819 xmlAddChild(parent, xmlParam);
1820 }
1821 } else {
1822 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1823 xmlAddChild(parent, xmlParam);
1824 }
1825 } else if (sdlType->kind == XSD_TYPEKIND_EXTENSION &&
1826 sdlType->encode && type != &sdlType->encode->details) {
1827 if (sdlType->encode->details.sdl_type &&
1828 sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_SIMPLE &&
1829 sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_LIST &&
1830 sdlType->encode->details.sdl_type->kind != XSD_TYPEKIND_UNION) {
1831
1832 if (prop) { GC_TRY_PROTECT_RECURSION(prop); }
1833 xmlParam = master_to_xml(sdlType->encode, data, style, parent);
1834 if (prop) { GC_TRY_UNPROTECT_RECURSION(prop); }
1835 } else {
1836 zval rv;
1837 zval *tmp = get_zval_property(data, "_", &rv);
1838
1839 if (tmp) {
1840 xmlParam = master_to_xml(sdlType->encode, tmp, style, parent);
1841 } else if (prop == NULL) {
1842 xmlParam = master_to_xml(sdlType->encode, data, style, parent);
1843 } else {
1844 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1845 xmlAddChild(parent, xmlParam);
1846 }
1847 }
1848 } else {
1849 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1850 xmlAddChild(parent, xmlParam);
1851 }
1852
1853 if (soap_check_zval_ref(data, xmlParam)) {
1854 return xmlParam;
1855 }
1856 if (prop != NULL) {
1857 sdlTypePtr array_el;
1858
1859 if (Z_TYPE_P(data) == IS_ARRAY &&
1860 !is_map(data) &&
1861 sdlType->attributes == NULL &&
1862 sdlType->model != NULL &&
1863 (array_el = model_array_element(sdlType->model)) != NULL) {
1864 zval *val;
1865
1866 ZEND_HASH_FOREACH_VAL(prop, val) {
1867 xmlNodePtr property;
1868 ZVAL_DEREF(val);
1869 if (Z_TYPE_P(val) == IS_NULL && array_el->nillable) {
1870 property = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1871 xmlAddChild(xmlParam, property);
1872 set_xsi_nil(property);
1873 } else {
1874 property = master_to_xml(array_el->encode, val, style, xmlParam);
1875 }
1876 xmlNodeSetName(property, BAD_CAST(array_el->name));
1877 if (style == SOAP_LITERAL &&
1878 array_el->namens &&
1879 array_el->form == XSD_FORM_QUALIFIED) {
1880 xmlNsPtr nsp = encode_add_ns(property, array_el->namens);
1881 xmlSetNs(property, nsp);
1882 }
1883 } ZEND_HASH_FOREACH_END();
1884 } else if (sdlType->model) {
1885 model_to_xml_object(xmlParam, sdlType->model, data, style, 1);
1886 }
1887 if (sdlType->attributes) {
1888 sdlAttributePtr attr;
1889 zval *zattr, rv;
1890
1891 ZEND_HASH_FOREACH_PTR(sdlType->attributes, attr) {
1892 if (attr->name) {
1893 zattr = get_zval_property(data, attr->name, &rv);
1894 if (zattr) {
1895 xmlNodePtr dummy;
1896
1897 dummy = master_to_xml(attr->encode, zattr, SOAP_LITERAL, xmlParam);
1898 if (dummy->children && dummy->children->content) {
1899 if (attr->fixed && strcmp(attr->fixed, (char*)dummy->children->content) != 0) {
1900 soap_error3(E_ERROR, "Encoding: Attribute '%s' has fixed value '%s' (value '%s' is not allowed)", attr->name, attr->fixed, dummy->children->content);
1901 }
1902 /* we need to handle xml: namespace specially, since it is
1903 an implicit schema. Otherwise, use form.
1904 */
1905 if (attr->namens &&
1906 (!strncmp(attr->namens, XML_NAMESPACE, sizeof(XML_NAMESPACE)) ||
1907 attr->form == XSD_FORM_QUALIFIED)) {
1908 xmlNsPtr nsp = encode_add_ns(xmlParam, attr->namens);
1909
1910 xmlSetNsProp(xmlParam, nsp, BAD_CAST(attr->name), dummy->children->content);
1911 } else {
1912 xmlSetProp(xmlParam, BAD_CAST(attr->name), dummy->children->content);
1913 }
1914 }
1915 xmlUnlinkNode(dummy);
1916 xmlFreeNode(dummy);
1917 }
1918 }
1919 } ZEND_HASH_FOREACH_END();
1920 }
1921 }
1922 if (style == SOAP_ENCODED) {
1923 set_ns_and_type(xmlParam, type);
1924 }
1925 } else {
1926 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
1927 xmlAddChild(parent, xmlParam);
1928
1929 if (soap_check_zval_ref(data, xmlParam)) {
1930 return xmlParam;
1931 }
1932 if (prop != NULL) {
1933 zval *zprop;
1934 zend_string *str_key;
1935 xmlNodePtr property;
1936
1937 ZEND_HASH_FOREACH_STR_KEY_VAL_IND(prop, str_key, zprop) {
1938 ZVAL_DEREF(zprop);
1939 property = master_to_xml(get_conversion(Z_TYPE_P(zprop)), zprop, style, xmlParam);
1940
1941 if (str_key) {
1942 const char *prop_name;
1943
1944 if (Z_TYPE_P(data) == IS_OBJECT) {
1945 const char *class_name;
1946
1947 zend_unmangle_property_name(str_key, &class_name, &prop_name);
1948 } else {
1949 prop_name = ZSTR_VAL(str_key);
1950 }
1951 if (prop_name) {
1952 xmlNodeSetName(property, BAD_CAST(prop_name));
1953 }
1954 }
1955 } ZEND_HASH_FOREACH_END();
1956 }
1957 if (style == SOAP_ENCODED) {
1958 set_ns_and_type(xmlParam, type);
1959 }
1960 }
1961 return xmlParam;
1962 }
1963
1964 /* Array encode/decode */
guess_array_map(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)1965 static xmlNodePtr guess_array_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
1966 {
1967 encodePtr enc = NULL;
1968
1969 if (data && Z_TYPE_P(data) == IS_ARRAY) {
1970 if (is_map(data)) {
1971 enc = get_conversion(APACHE_MAP);
1972 } else {
1973 enc = get_conversion(SOAP_ENC_ARRAY);
1974 }
1975 }
1976 if (!enc) {
1977 enc = get_conversion(IS_NULL);
1978 }
1979
1980 return master_to_xml(enc, data, style, parent);
1981 }
1982
calc_dimension_12(const char * str)1983 static int calc_dimension_12(const char* str)
1984 {
1985 int i = 0, flag = 0;
1986 while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
1987 str++;
1988 }
1989 if (*str == '*') {
1990 i++;
1991 str++;
1992 }
1993 while (*str != '\0') {
1994 if (*str >= '0' && *str <= '9') {
1995 if (flag == 0) {
1996 i++;
1997 flag = 1;
1998 }
1999 } else if (*str == '*') {
2000 soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
2001 } else {
2002 flag = 0;
2003 }
2004 str++;
2005 }
2006 return i;
2007 }
2008
get_position_12(int dimension,const char * str)2009 static int* get_position_12(int dimension, const char* str)
2010 {
2011 int *pos;
2012 int i = -1, flag = 0;
2013
2014 pos = safe_emalloc(sizeof(int), dimension, 0);
2015 memset(pos,0,sizeof(int)*dimension);
2016 while (*str != '\0' && (*str < '0' || *str > '9') && (*str != '*')) {
2017 str++;
2018 }
2019 if (*str == '*') {
2020 str++;
2021 i++;
2022 }
2023 while (*str != '\0') {
2024 if (*str >= '0' && *str <= '9') {
2025 if (flag == 0) {
2026 i++;
2027 flag = 1;
2028 }
2029 pos[i] = (pos[i]*10)+(*str-'0');
2030 } else if (*str == '*') {
2031 soap_error0(E_ERROR, "Encoding: '*' may only be first arraySize value in list");
2032 } else {
2033 flag = 0;
2034 }
2035 str++;
2036 }
2037 return pos;
2038 }
2039
calc_dimension(const char * str)2040 static int calc_dimension(const char* str)
2041 {
2042 int i = 1;
2043 while (*str != ']' && *str != '\0') {
2044 if (*str == ',') {
2045 i++;
2046 }
2047 str++;
2048 }
2049 return i;
2050 }
2051
get_position_ex(int dimension,const char * str,int ** pos)2052 static void get_position_ex(int dimension, const char* str, int** pos)
2053 {
2054 int i = 0;
2055
2056 memset(*pos,0,sizeof(int)*dimension);
2057 while (*str != ']' && *str != '\0' && i < dimension) {
2058 if (*str >= '0' && *str <= '9') {
2059 (*pos)[i] = ((*pos)[i]*10)+(*str-'0');
2060 } else if (*str == ',') {
2061 i++;
2062 }
2063 str++;
2064 }
2065 }
2066
get_position(int dimension,const char * str)2067 static int* get_position(int dimension, const char* str)
2068 {
2069 int *pos;
2070
2071 pos = safe_emalloc(sizeof(int), dimension, 0);
2072 get_position_ex(dimension, str, &pos);
2073 return pos;
2074 }
2075
add_xml_array_elements(xmlNodePtr xmlParam,sdlTypePtr type,encodePtr enc,xmlNsPtr ns,int dimension,int * dims,zval * data,int style)2076 static void add_xml_array_elements(xmlNodePtr xmlParam,
2077 sdlTypePtr type,
2078 encodePtr enc,
2079 xmlNsPtr ns,
2080 int dimension ,
2081 int* dims,
2082 zval* data,
2083 int style
2084 )
2085 {
2086 int j = 0;
2087 zval *zdata;
2088 xmlNodePtr xparam;
2089
2090 if (data && Z_TYPE_P(data) == IS_ARRAY) {
2091 ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(data), zdata) {
2092 if (j >= dims[0]) {
2093 break;
2094 }
2095 ZVAL_DEREF(zdata);
2096 if (dimension == 1) {
2097 if (enc == NULL) {
2098 xparam = master_to_xml(get_conversion(Z_TYPE_P(zdata)), zdata, style, xmlParam);
2099 } else {
2100 xparam = master_to_xml(enc, zdata, style, xmlParam);
2101 }
2102
2103 if (type) {
2104 xmlNodeSetName(xparam, BAD_CAST(type->name));
2105 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2106 xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2107 xmlSetNs(xparam, ns);
2108 } else {
2109 xmlNodeSetName(xparam, BAD_CAST("item"));
2110 }
2111 } else {
2112 add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, zdata, style);
2113 }
2114 j++;
2115 } ZEND_HASH_FOREACH_END();
2116
2117 if (dimension == 1) {
2118 while (j < dims[0]) {
2119 xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2120 xmlAddChild(xmlParam, xparam);
2121
2122 if (type) {
2123 xmlNodeSetName(xparam, BAD_CAST(type->name));
2124 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2125 xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2126 xmlSetNs(xparam, ns);
2127 } else {
2128 xmlNodeSetName(xparam, BAD_CAST("item"));
2129 }
2130
2131 j++;
2132 }
2133 } else {
2134 while (j < dims[0]) {
2135 add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style);
2136 j++;
2137 }
2138 }
2139 } else {
2140 for (j=0; j<dims[0]; j++) {
2141 if (dimension == 1) {
2142 xmlNodePtr xparam;
2143
2144 xparam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2145 xmlAddChild(xmlParam, xparam);
2146 if (type) {
2147 xmlNodeSetName(xparam, BAD_CAST(type->name));
2148 } else if (style == SOAP_LITERAL && enc && enc->details.type_str) {
2149 xmlNodeSetName(xparam, BAD_CAST(enc->details.type_str));
2150 xmlSetNs(xparam, ns);
2151 } else {
2152 xmlNodeSetName(xparam, BAD_CAST("item"));
2153 }
2154 } else {
2155 add_xml_array_elements(xmlParam, type, enc, ns, dimension-1, dims+1, NULL, style);
2156 }
2157 }
2158 }
2159 }
2160
to_xml_array(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2161 static xmlNodePtr to_xml_array(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2162 {
2163 sdlTypePtr sdl_type = type->sdl_type;
2164 sdlTypePtr element_type = NULL;
2165 smart_str array_type = {0}, array_size = {0};
2166 int i;
2167 xmlNodePtr xmlParam;
2168 encodePtr enc = NULL;
2169 int dimension = 1;
2170 int* dims;
2171 int soap_version;
2172 zval array_copy;
2173
2174 ZVAL_UNDEF(&array_copy);
2175 soap_version = SOAP_GLOBAL(soap_version);
2176
2177 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2178 xmlAddChild(parent, xmlParam);
2179
2180 if (!data || Z_TYPE_P(data) == IS_NULL) {
2181 if (style == SOAP_ENCODED) {
2182 set_xsi_nil(xmlParam);
2183 if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
2184 set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
2185 } else {
2186 set_ns_and_type(xmlParam, type);
2187 }
2188 }
2189 return xmlParam;
2190 }
2191
2192 if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), zend_ce_traversable)) {
2193 zend_object_iterator *iter;
2194 zend_class_entry *ce = Z_OBJCE_P(data);
2195 zval *val;
2196
2197 array_init(&array_copy);
2198
2199 iter = ce->get_iterator(ce, data, 0);
2200
2201 if (EG(exception)) {
2202 goto iterator_done;
2203 }
2204
2205 if (iter->funcs->rewind) {
2206 iter->funcs->rewind(iter);
2207 if (EG(exception)) {
2208 goto iterator_done;
2209 }
2210 }
2211
2212 while (iter->funcs->valid(iter) == SUCCESS) {
2213 if (EG(exception)) {
2214 goto iterator_done;
2215 }
2216
2217 val = iter->funcs->get_current_data(iter);
2218 if (EG(exception)) {
2219 goto iterator_done;
2220 }
2221 if (iter->funcs->get_current_key) {
2222 zval key;
2223 iter->funcs->get_current_key(iter, &key);
2224 if (EG(exception)) {
2225 goto iterator_done;
2226 }
2227 array_set_zval_key(Z_ARRVAL(array_copy), &key, val);
2228 zval_ptr_dtor(val);
2229 zval_ptr_dtor(&key);
2230 } else {
2231 add_next_index_zval(&array_copy, val);
2232 }
2233 Z_TRY_ADDREF_P(val);
2234
2235 iter->funcs->move_forward(iter);
2236 if (EG(exception)) {
2237 goto iterator_done;
2238 }
2239 }
2240 iterator_done:
2241 OBJ_RELEASE(&iter->std);
2242 if (EG(exception)) {
2243 zval_ptr_dtor(&array_copy);
2244 ZVAL_UNDEF(&array_copy);
2245 } else {
2246 data = &array_copy;
2247 }
2248 }
2249
2250 if (Z_TYPE_P(data) == IS_ARRAY) {
2251 sdlAttributePtr arrayType;
2252 sdlExtraAttributePtr ext;
2253 sdlTypePtr elementType;
2254
2255 i = zend_hash_num_elements(Z_ARRVAL_P(data));
2256
2257 if (sdl_type &&
2258 sdl_type->attributes &&
2259 (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
2260 sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType")-1)) != NULL &&
2261 arrayType->extraAttributes &&
2262 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType")-1)) != NULL) {
2263
2264 char *value, *end;
2265 zval *el;
2266
2267 value = estrdup(ext->val);
2268 end = strrchr(value,'[');
2269 if (end) {
2270 *end = '\0';
2271 end++;
2272 dimension = calc_dimension(end);
2273 }
2274 if (ext->ns != NULL) {
2275 enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, value);
2276 get_type_str(xmlParam, ext->ns, value, &array_type);
2277 } else {
2278 smart_str_appends(&array_type, value);
2279 }
2280
2281 dims = safe_emalloc(sizeof(int), dimension, 0);
2282 dims[0] = i;
2283 el = data;
2284 for (i = 1; i < dimension; i++) {
2285 if (el != NULL && Z_TYPE_P(el) == IS_ARRAY &&
2286 zend_hash_num_elements(Z_ARRVAL_P(el)) > 0) {
2287 ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(el), el) {
2288 break;
2289 } ZEND_HASH_FOREACH_END();
2290 ZVAL_DEREF(el);
2291 if (Z_TYPE_P(el) == IS_ARRAY) {
2292 dims[i] = zend_hash_num_elements(Z_ARRVAL_P(el));
2293 } else {
2294 dims[i] = 0;
2295 }
2296 }
2297 }
2298
2299 smart_str_append_long(&array_size, dims[0]);
2300 for (i=1; i<dimension; i++) {
2301 smart_str_appendc(&array_size, ',');
2302 smart_str_append_long(&array_size, dims[i]);
2303 }
2304
2305 efree(value);
2306
2307 } else if (sdl_type &&
2308 sdl_type->attributes &&
2309 (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
2310 sizeof(SOAP_1_2_ENC_NAMESPACE":itemType")-1)) != NULL &&
2311 arrayType->extraAttributes &&
2312 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType")-1)) != NULL) {
2313 if (ext->ns != NULL) {
2314 enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, ext->val);
2315 get_type_str(xmlParam, ext->ns, ext->val, &array_type);
2316 } else {
2317 smart_str_appends(&array_type, ext->val);
2318 }
2319 if ((arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2320 sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
2321 arrayType->extraAttributes &&
2322 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize")-1)) != NULL) {
2323 dimension = calc_dimension_12(ext->val);
2324 dims = get_position_12(dimension, ext->val);
2325 if (dims[0] == 0) {dims[0] = i;}
2326
2327 smart_str_append_long(&array_size, dims[0]);
2328 for (i=1; i<dimension; i++) {
2329 smart_str_appendc(&array_size, ',');
2330 smart_str_append_long(&array_size, dims[i]);
2331 }
2332 } else {
2333 dims = emalloc(sizeof(int));
2334 *dims = 0;
2335 smart_str_append_long(&array_size, i);
2336 }
2337 } else if (sdl_type &&
2338 sdl_type->attributes &&
2339 (arrayType = zend_hash_str_find_ptr(sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2340 sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
2341 arrayType->extraAttributes &&
2342 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraySize")-1)) != NULL) {
2343 dimension = calc_dimension_12(ext->val);
2344 dims = get_position_12(dimension, ext->val);
2345 if (dims[0] == 0) {dims[0] = i;}
2346
2347 smart_str_append_long(&array_size, dims[0]);
2348 for (i=1; i<dimension; i++) {
2349 smart_str_appendc(&array_size, ',');
2350 smart_str_append_long(&array_size, dims[i]);
2351 }
2352
2353 if (sdl_type && sdl_type->elements &&
2354 zend_hash_num_elements(sdl_type->elements) == 1 &&
2355 (zend_hash_internal_pointer_reset(sdl_type->elements),
2356 (elementType = zend_hash_get_current_data_ptr(sdl_type->elements)) != NULL) &&
2357 elementType->encode && elementType->encode->details.type_str) {
2358 element_type = elementType;
2359 enc = elementType->encode;
2360 get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
2361 } else {
2362 enc = get_array_type(xmlParam, data, &array_type);
2363 }
2364 } else if (sdl_type && sdl_type->elements &&
2365 zend_hash_num_elements(sdl_type->elements) == 1 &&
2366 (zend_hash_internal_pointer_reset(sdl_type->elements),
2367 (elementType = zend_hash_get_current_data_ptr(sdl_type->elements)) != NULL) &&
2368 elementType->encode && elementType->encode->details.type_str) {
2369
2370 element_type = elementType;
2371 enc = elementType->encode;
2372 get_type_str(xmlParam, elementType->encode->details.ns, elementType->encode->details.type_str, &array_type);
2373
2374 smart_str_append_long(&array_size, i);
2375
2376 dims = safe_emalloc(sizeof(int), dimension, 0);
2377 dims[0] = i;
2378 } else {
2379
2380 enc = get_array_type(xmlParam, data, &array_type);
2381 smart_str_append_long(&array_size, i);
2382 dims = safe_emalloc(sizeof(int), dimension, 0);
2383 dims[0] = i;
2384 }
2385
2386 if (style == SOAP_ENCODED) {
2387 if (soap_version == SOAP_1_1) {
2388 smart_str_0(&array_type);
2389 if (strcmp(ZSTR_VAL(array_type.s),"xsd:anyType") == 0) {
2390 smart_str_free(&array_type);
2391 smart_str_appendl(&array_type,"xsd:ur-type",sizeof("xsd:ur-type")-1);
2392 }
2393 smart_str_appendc(&array_type, '[');
2394 smart_str_append_smart_str(&array_type, &array_size);
2395 smart_str_appendc(&array_type, ']');
2396 smart_str_0(&array_type);
2397 set_ns_prop(xmlParam, SOAP_1_1_ENC_NAMESPACE, "arrayType", ZSTR_VAL(array_type.s));
2398 } else {
2399 size_t i = 0;
2400 while (i < ZSTR_LEN(array_size.s)) {
2401 if (ZSTR_VAL(array_size.s)[i] == ',') {ZSTR_VAL(array_size.s)[i] = ' ';}
2402 ++i;
2403 }
2404 smart_str_0(&array_type);
2405 smart_str_0(&array_size);
2406 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "itemType", ZSTR_VAL(array_type.s));
2407 set_ns_prop(xmlParam, SOAP_1_2_ENC_NAMESPACE, "arraySize", ZSTR_VAL(array_size.s));
2408 }
2409 }
2410 smart_str_free(&array_type);
2411 smart_str_free(&array_size);
2412
2413 add_xml_array_elements(xmlParam, element_type, enc, enc?encode_add_ns(xmlParam,enc->details.ns):NULL, dimension, dims, data, style);
2414 efree(dims);
2415 }
2416 if (style == SOAP_ENCODED) {
2417 if (SOAP_GLOBAL(features) & SOAP_USE_XSI_ARRAY_TYPE) {
2418 set_ns_and_type_ex(xmlParam, (soap_version == SOAP_1_1) ? SOAP_1_1_ENC_NAMESPACE : SOAP_1_2_ENC_NAMESPACE, "Array");
2419 } else {
2420 set_ns_and_type(xmlParam, type);
2421 }
2422 }
2423
2424 zval_ptr_dtor(&array_copy);
2425
2426 return xmlParam;
2427 }
2428
to_zval_array(zval * ret,encodeTypePtr type,xmlNodePtr data)2429 static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
2430 {
2431 xmlNodePtr trav;
2432 encodePtr enc = NULL;
2433 int dimension = 1;
2434 int* dims = NULL;
2435 int* pos = NULL;
2436 xmlAttrPtr attr;
2437 sdlAttributePtr arrayType;
2438 sdlExtraAttributePtr ext;
2439 sdlTypePtr elementType;
2440
2441 ZVAL_NULL(ret);
2442 FIND_XML_NULL(data, ret);
2443
2444 if (data &&
2445 (attr = get_attribute(data->properties,"arrayType")) &&
2446 attr->children && attr->children->content) {
2447 char *type, *end, *ns;
2448 xmlNsPtr nsptr;
2449
2450 parse_namespace(attr->children->content, &type, &ns);
2451 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
2452
2453 end = strrchr(type,'[');
2454 if (end) {
2455 *end = '\0';
2456 dimension = calc_dimension(end+1);
2457 dims = get_position(dimension, end+1);
2458 }
2459 if (nsptr != NULL) {
2460 enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
2461 }
2462 efree(type);
2463 if (ns) {efree(ns);}
2464
2465 } else if ((attr = get_attribute(data->properties,"itemType")) &&
2466 attr->children &&
2467 attr->children->content) {
2468 char *type, *ns;
2469 xmlNsPtr nsptr;
2470
2471 parse_namespace(attr->children->content, &type, &ns);
2472 nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
2473 if (nsptr != NULL) {
2474 enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
2475 }
2476 efree(type);
2477 if (ns) {efree(ns);}
2478
2479 if ((attr = get_attribute(data->properties,"arraySize")) &&
2480 attr->children && attr->children->content) {
2481 dimension = calc_dimension_12((char*)attr->children->content);
2482 dims = get_position_12(dimension, (char*)attr->children->content);
2483 } else {
2484 dims = emalloc(sizeof(int));
2485 *dims = 0;
2486 }
2487
2488 } else if ((attr = get_attribute(data->properties,"arraySize")) &&
2489 attr->children && attr->children->content) {
2490
2491 dimension = calc_dimension_12((char*)attr->children->content);
2492 dims = get_position_12(dimension, (char*)attr->children->content);
2493
2494 } else if (type->sdl_type != NULL &&
2495 type->sdl_type->attributes != NULL &&
2496 (arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_1_ENC_NAMESPACE":arrayType",
2497 sizeof(SOAP_1_1_ENC_NAMESPACE":arrayType")-1)) != NULL &&
2498 arrayType->extraAttributes &&
2499 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arrayType", sizeof(WSDL_NAMESPACE":arrayType")-1)) != NULL) {
2500 char *type, *end;
2501
2502 type = estrdup(ext->val);
2503 end = strrchr(type,'[');
2504 if (end) {
2505 *end = '\0';
2506 }
2507 if (ext->ns != NULL) {
2508 enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, type);
2509 }
2510 efree(type);
2511
2512 dims = emalloc(sizeof(int));
2513 *dims = 0;
2514
2515 } else if (type->sdl_type != NULL &&
2516 type->sdl_type->attributes != NULL &&
2517 (arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":itemType",
2518 sizeof(SOAP_1_2_ENC_NAMESPACE":itemType")-1)) != NULL &&
2519 arrayType->extraAttributes &&
2520 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":itemType", sizeof(WSDL_NAMESPACE":itemType")-1)) != NULL) {
2521
2522 if (ext->ns != NULL) {
2523 enc = get_encoder(SOAP_GLOBAL(sdl), ext->ns, ext->val);
2524 }
2525
2526 if ((arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2527 sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
2528 arrayType->extraAttributes &&
2529 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize")-1)) != NULL) {
2530 dimension = calc_dimension_12(ext->val);
2531 dims = get_position_12(dimension, ext->val);
2532 } else {
2533 dims = emalloc(sizeof(int));
2534 *dims = 0;
2535 }
2536 } else if (type->sdl_type != NULL &&
2537 type->sdl_type->attributes != NULL &&
2538 (arrayType = zend_hash_str_find_ptr(type->sdl_type->attributes, SOAP_1_2_ENC_NAMESPACE":arraySize",
2539 sizeof(SOAP_1_2_ENC_NAMESPACE":arraySize")-1)) != NULL &&
2540 arrayType->extraAttributes &&
2541 (ext = zend_hash_str_find_ptr(arrayType->extraAttributes, WSDL_NAMESPACE":arraySize", sizeof(WSDL_NAMESPACE":arraysize")-1)) != NULL) {
2542
2543 dimension = calc_dimension_12(ext->val);
2544 dims = get_position_12(dimension, ext->val);
2545 if (type->sdl_type && type->sdl_type->elements &&
2546 zend_hash_num_elements(type->sdl_type->elements) == 1 &&
2547 (zend_hash_internal_pointer_reset(type->sdl_type->elements),
2548 (elementType = zend_hash_get_current_data_ptr(type->sdl_type->elements)) != NULL) &&
2549 elementType->encode) {
2550 enc = elementType->encode;
2551 }
2552 } else if (type->sdl_type && type->sdl_type->elements &&
2553 zend_hash_num_elements(type->sdl_type->elements) == 1 &&
2554 (zend_hash_internal_pointer_reset(type->sdl_type->elements),
2555 (elementType = zend_hash_get_current_data_ptr(type->sdl_type->elements)) != NULL) &&
2556 elementType->encode) {
2557 enc = elementType->encode;
2558 }
2559 if (dims == NULL) {
2560 dimension = 1;
2561 dims = emalloc(sizeof(int));
2562 *dims = 0;
2563 }
2564 pos = safe_emalloc(sizeof(int), dimension, 0);
2565 memset(pos,0,sizeof(int)*dimension);
2566 if (data &&
2567 (attr = get_attribute(data->properties,"offset")) &&
2568 attr->children && attr->children->content) {
2569 char* tmp = strrchr((char*)attr->children->content,'[');
2570
2571 if (tmp == NULL) {
2572 tmp = (char*)attr->children->content;
2573 }
2574 get_position_ex(dimension, tmp, &pos);
2575 }
2576
2577 array_init(ret);
2578 trav = data->children;
2579 while (trav) {
2580 if (trav->type == XML_ELEMENT_NODE) {
2581 int i;
2582 zval tmpVal, *ar;
2583 xmlAttrPtr position = get_attribute(trav->properties,"position");
2584
2585 ZVAL_NULL(&tmpVal);
2586 master_to_zval(&tmpVal, enc, trav);
2587 if (position != NULL && position->children && position->children->content) {
2588 char* tmp = strrchr((char*)position->children->content, '[');
2589 if (tmp == NULL) {
2590 tmp = (char*)position->children->content;
2591 }
2592 get_position_ex(dimension, tmp, &pos);
2593 }
2594
2595 /* Get/Create intermediate arrays for multidimensional arrays */
2596 i = 0;
2597 ar = ret;
2598 while (i < dimension-1) {
2599 zval* ar2;
2600 if ((ar2 = zend_hash_index_find(Z_ARRVAL_P(ar), pos[i])) != NULL) {
2601 ar = ar2;
2602 } else {
2603 zval tmpAr;
2604 array_init(&tmpAr);
2605 ar = zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpAr);
2606 }
2607 i++;
2608 }
2609 zend_hash_index_update(Z_ARRVAL_P(ar), pos[i], &tmpVal);
2610
2611 /* Increment position */
2612 i = dimension;
2613 while (i > 0) {
2614 i--;
2615 pos[i]++;
2616 if (pos[i] >= dims[i]) {
2617 if (i > 0) {
2618 pos[i] = 0;
2619 } else {
2620 /* TODO: Array index overflow */
2621 }
2622 } else {
2623 break;
2624 }
2625 }
2626 }
2627 trav = trav->next;
2628 }
2629 efree(dims);
2630 efree(pos);
2631 return ret;
2632 }
2633
2634 /* Map encode/decode */
to_xml_map(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2635 static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2636 {
2637 zval *temp_data;
2638 zend_string *key_val;
2639 zend_ulong int_val;
2640 xmlNodePtr xmlParam;
2641 xmlNodePtr xparam, item;
2642 xmlNodePtr key;
2643
2644 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2645 xmlAddChild(parent, xmlParam);
2646 FIND_ZVAL_NULL(data, xmlParam, style);
2647
2648 if (Z_TYPE_P(data) == IS_ARRAY) {
2649 ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(data), int_val, key_val, temp_data) {
2650 item = xmlNewNode(NULL, BAD_CAST("item"));
2651 xmlAddChild(xmlParam, item);
2652 key = xmlNewNode(NULL, BAD_CAST("key"));
2653 xmlAddChild(item,key);
2654 if (key_val) {
2655 if (style == SOAP_ENCODED) {
2656 set_xsi_type(key, "xsd:string");
2657 }
2658 xmlNodeSetContent(key, BAD_CAST(ZSTR_VAL(key_val)));
2659 } else {
2660 smart_str tmp = {0};
2661 smart_str_append_long(&tmp, int_val);
2662 smart_str_0(&tmp);
2663
2664 if (style == SOAP_ENCODED) {
2665 set_xsi_type(key, "xsd:int");
2666 }
2667 xmlNodeSetContentLen(key, BAD_CAST(ZSTR_VAL(tmp.s)), ZSTR_LEN(tmp.s));
2668
2669 smart_str_free(&tmp);
2670 }
2671
2672 ZVAL_DEREF(temp_data);
2673 xparam = master_to_xml(get_conversion(Z_TYPE_P(temp_data)), temp_data, style, item);
2674 xmlNodeSetName(xparam, BAD_CAST("value"));
2675 } ZEND_HASH_FOREACH_END();
2676 }
2677 if (style == SOAP_ENCODED) {
2678 set_ns_and_type(xmlParam, type);
2679 }
2680
2681 return xmlParam;
2682 }
2683
to_zval_map(zval * ret,encodeTypePtr type,xmlNodePtr data)2684 static zval *to_zval_map(zval *ret, encodeTypePtr type, xmlNodePtr data)
2685 {
2686 zval key, value;
2687 xmlNodePtr trav, item, xmlKey, xmlValue;
2688
2689 ZVAL_NULL(ret);
2690 FIND_XML_NULL(data, ret);
2691
2692 if (data && data->children) {
2693 array_init(ret);
2694 trav = data->children;
2695
2696 trav = data->children;
2697 FOREACHNODE(trav, "item", item) {
2698 xmlKey = get_node(item->children, "key");
2699 if (!xmlKey) {
2700 soap_error0(E_ERROR, "Encoding: Can't decode apache map, missing key");
2701 }
2702
2703 xmlValue = get_node(item->children, "value");
2704 if (!xmlKey) {
2705 soap_error0(E_ERROR, "Encoding: Can't decode apache map, missing value");
2706 }
2707
2708 ZVAL_NULL(&key);
2709 master_to_zval(&key, NULL, xmlKey);
2710 ZVAL_NULL(&value);
2711 master_to_zval(&value, NULL, xmlValue);
2712
2713 if (Z_TYPE(key) == IS_STRING) {
2714 zend_symtable_update(Z_ARRVAL_P(ret), Z_STR(key), &value);
2715 } else if (Z_TYPE(key) == IS_LONG) {
2716 zend_hash_index_update(Z_ARRVAL_P(ret), Z_LVAL(key), &value);
2717 } else {
2718 soap_error0(E_ERROR, "Encoding: Can't decode apache map, only Strings or Longs are allowed as keys");
2719 }
2720 zval_ptr_dtor(&key);
2721 }
2722 ENDFOREACH(trav);
2723 } else {
2724 ZVAL_NULL(ret);
2725 }
2726 return ret;
2727 }
2728
2729 /* Unknown encode/decode */
guess_xml_convert(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2730 static xmlNodePtr guess_xml_convert(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2731 {
2732 encodePtr enc;
2733 xmlNodePtr ret;
2734
2735 if (data) {
2736 enc = get_conversion(Z_TYPE_P(data));
2737 } else {
2738 enc = get_conversion(IS_NULL);
2739 }
2740 ret = master_to_xml_int(enc, data, style, parent, 0);
2741 /*
2742 if (style == SOAP_LITERAL && SOAP_GLOBAL(sdl)) {
2743 set_ns_and_type(ret, &enc->details);
2744 }
2745 */
2746 return ret;
2747 }
2748
guess_zval_convert(zval * ret,encodeTypePtr type,xmlNodePtr data)2749 static zval *guess_zval_convert(zval *ret, encodeTypePtr type, xmlNodePtr data)
2750 {
2751 encodePtr enc = NULL;
2752 xmlAttrPtr tmpattr;
2753 xmlChar *type_name = NULL;
2754
2755 data = check_and_resolve_href(data);
2756
2757 if (data == NULL) {
2758 enc = get_conversion(IS_NULL);
2759 } else if (data->properties && get_attribute_ex(data->properties, "nil", XSI_NAMESPACE)) {
2760 enc = get_conversion(IS_NULL);
2761 } else {
2762 tmpattr = get_attribute_ex(data->properties,"type", XSI_NAMESPACE);
2763 if (tmpattr != NULL) {
2764 type_name = tmpattr->children->content;
2765 enc = get_encoder_from_prefix(SOAP_GLOBAL(sdl), data, tmpattr->children->content);
2766 if (enc && type == &enc->details) {
2767 enc = NULL;
2768 }
2769 if (enc != NULL) {
2770 encodePtr tmp = enc;
2771 while (tmp &&
2772 tmp->details.sdl_type != NULL &&
2773 tmp->details.sdl_type->kind != XSD_TYPEKIND_COMPLEX) {
2774 if (enc == tmp->details.sdl_type->encode ||
2775 tmp == tmp->details.sdl_type->encode) {
2776 enc = NULL;
2777 break;
2778 }
2779 tmp = tmp->details.sdl_type->encode;
2780 }
2781 }
2782 }
2783
2784 if (enc == NULL) {
2785 /* Didn't have a type, totally guess here */
2786 /* Logic: has children = IS_OBJECT else IS_STRING */
2787 xmlNodePtr trav;
2788
2789 if (get_attribute(data->properties, "arrayType") ||
2790 get_attribute(data->properties, "itemType") ||
2791 get_attribute(data->properties, "arraySize")) {
2792 enc = get_conversion(SOAP_ENC_ARRAY);
2793 } else {
2794 enc = get_conversion(XSD_STRING);
2795 trav = data->children;
2796 while (trav != NULL) {
2797 if (trav->type == XML_ELEMENT_NODE) {
2798 enc = get_conversion(SOAP_ENC_OBJECT);
2799 break;
2800 }
2801 trav = trav->next;
2802 }
2803 }
2804 }
2805 }
2806 master_to_zval_int(ret, enc, data);
2807 if (SOAP_GLOBAL(sdl) && type_name && enc->details.sdl_type) {
2808 zval soapvar;
2809 char *ns, *cptype;
2810 xmlNsPtr nsptr;
2811
2812 object_init_ex(&soapvar, soap_var_class_entry);
2813 ZVAL_LONG(Z_VAR_ENC_TYPE_P(&soapvar), enc->details.type);
2814 ZVAL_COPY_VALUE(Z_VAR_ENC_VALUE_P(&soapvar), ret);
2815 parse_namespace(type_name, &cptype, &ns);
2816 nsptr = xmlSearchNs(data->doc, data, BAD_CAST(ns));
2817 ZVAL_STRING(Z_VAR_ENC_STYPE_P(&soapvar), cptype);
2818 if (nsptr) {
2819 ZVAL_STRING(Z_VAR_ENC_NS_P(&soapvar), (char*)nsptr->href);
2820 }
2821 efree(cptype);
2822 if (ns) {efree(ns);}
2823 ZVAL_COPY_VALUE(ret, &soapvar);
2824 }
2825 return ret;
2826 }
2827
2828 /* Time encode/decode */
to_xml_datetime_ex(encodeTypePtr type,zval * data,char * format,int style,xmlNodePtr parent)2829 static xmlNodePtr to_xml_datetime_ex(encodeTypePtr type, zval *data, char *format, int style, xmlNodePtr parent)
2830 {
2831 /* logic hacked from ext/standard/datetime.c */
2832 struct tm *ta, tmbuf;
2833 time_t timestamp;
2834 int max_reallocs = 5;
2835 size_t buf_len=64, real_len;
2836 char *buf;
2837 char tzbuf[8];
2838
2839 xmlNodePtr xmlParam;
2840
2841 xmlParam = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2842 xmlAddChild(parent, xmlParam);
2843 FIND_ZVAL_NULL(data, xmlParam, style);
2844
2845 if (Z_TYPE_P(data) == IS_LONG) {
2846 timestamp = Z_LVAL_P(data);
2847 ta = php_localtime_r(×tamp, &tmbuf);
2848 /*ta = php_gmtime_r(×tamp, &tmbuf);*/
2849 if (!ta) {
2850 soap_error1(E_ERROR, "Encoding: Invalid timestamp " ZEND_LONG_FMT, Z_LVAL_P(data));
2851 }
2852
2853 buf = (char *) emalloc(buf_len);
2854 while ((real_len = strftime(buf, buf_len, format, ta)) == buf_len || real_len == 0) {
2855 buf_len *= 2;
2856 buf = (char *) erealloc(buf, buf_len);
2857 if (!--max_reallocs) break;
2858 }
2859
2860 /* Time zone support */
2861 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
2862 snprintf(tzbuf, sizeof(tzbuf), "%c%02ld:%02ld",
2863 (ta->tm_gmtoff < 0) ? '-' : '+',
2864 labs(ta->tm_gmtoff / 3600), labs( (ta->tm_gmtoff % 3600) / 60 ));
2865 #else
2866 # if defined(__CYGWIN__) || (defined(PHP_WIN32) && defined(_MSC_VER) && _MSC_VER >= 1900)
2867 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));
2868 # else
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 # endif
2871 #endif
2872 if (strcmp(tzbuf,"+00:00") == 0) {
2873 strcpy(tzbuf,"Z");
2874 real_len++;
2875 } else {
2876 real_len += 6;
2877 }
2878 if (real_len >= buf_len) {
2879 buf = (char *) erealloc(buf, real_len+1);
2880 }
2881 strcat(buf, tzbuf);
2882
2883 xmlNodeSetContent(xmlParam, BAD_CAST(buf));
2884 efree(buf);
2885 } else if (Z_TYPE_P(data) == IS_STRING) {
2886 xmlNodeSetContentLen(xmlParam, BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
2887 }
2888
2889 if (style == SOAP_ENCODED) {
2890 set_ns_and_type(xmlParam, type);
2891 }
2892 return xmlParam;
2893 }
2894
to_xml_duration(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2895 static xmlNodePtr to_xml_duration(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2896 {
2897 /* TODO: '-'?P([0-9]+Y)?([0-9]+M)?([0-9]+D)?T([0-9]+H)?([0-9]+M)?([0-9]+S)? */
2898 return to_xml_string(type, data, style, parent);
2899 }
2900
to_xml_datetime(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2901 static xmlNodePtr to_xml_datetime(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2902 {
2903 return to_xml_datetime_ex(type, data, "%Y-%m-%dT%H:%M:%S", style, parent);
2904 }
2905
to_xml_time(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2906 static xmlNodePtr to_xml_time(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2907 {
2908 /* TODO: microsecconds */
2909 return to_xml_datetime_ex(type, data, "%H:%M:%S", style, parent);
2910 }
2911
to_xml_date(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2912 static xmlNodePtr to_xml_date(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2913 {
2914 return to_xml_datetime_ex(type, data, "%Y-%m-%d", style, parent);
2915 }
2916
to_xml_gyearmonth(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2917 static xmlNodePtr to_xml_gyearmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2918 {
2919 return to_xml_datetime_ex(type, data, "%Y-%m", style, parent);
2920 }
2921
to_xml_gyear(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2922 static xmlNodePtr to_xml_gyear(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2923 {
2924 return to_xml_datetime_ex(type, data, "%Y", style, parent);
2925 }
2926
to_xml_gmonthday(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2927 static xmlNodePtr to_xml_gmonthday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2928 {
2929 return to_xml_datetime_ex(type, data, "--%m-%d", style, parent);
2930 }
2931
to_xml_gday(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2932 static xmlNodePtr to_xml_gday(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2933 {
2934 return to_xml_datetime_ex(type, data, "---%d", style, parent);
2935 }
2936
to_xml_gmonth(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)2937 static xmlNodePtr to_xml_gmonth(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
2938 {
2939 return to_xml_datetime_ex(type, data, "--%m--", style, parent);
2940 }
2941
to_zval_list(zval * ret,encodeTypePtr enc,xmlNodePtr data)2942 static zval* to_zval_list(zval *ret, encodeTypePtr enc, xmlNodePtr data) {
2943 /*FIXME*/
2944 return to_zval_stringc(ret, enc, data);
2945 }
2946
to_xml_list(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent)2947 static xmlNodePtr to_xml_list(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent) {
2948 xmlNodePtr ret;
2949 encodePtr list_enc = NULL;
2950
2951 if (enc->sdl_type && enc->sdl_type->kind == XSD_TYPEKIND_LIST && enc->sdl_type->elements) {
2952 sdlTypePtr type;
2953
2954 ZEND_HASH_FOREACH_PTR(enc->sdl_type->elements, type) {
2955 list_enc = type->encode;
2956 break;
2957 } ZEND_HASH_FOREACH_END();
2958 }
2959
2960 ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
2961 xmlAddChild(parent, ret);
2962 FIND_ZVAL_NULL(data, ret, style);
2963 if (Z_TYPE_P(data) == IS_ARRAY) {
2964 zval *tmp;
2965 smart_str list = {0};
2966 HashTable *ht = Z_ARRVAL_P(data);
2967
2968 ZEND_HASH_FOREACH_VAL(ht, tmp) {
2969 xmlNodePtr dummy = master_to_xml(list_enc, tmp, SOAP_LITERAL, ret);
2970 if (dummy && dummy->children && dummy->children->content) {
2971 if (list.s && ZSTR_LEN(list.s) != 0) {
2972 smart_str_appendc(&list, ' ');
2973 }
2974 smart_str_appends(&list, (char*)dummy->children->content);
2975 } else {
2976 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
2977 }
2978 xmlUnlinkNode(dummy);
2979 xmlFreeNode(dummy);
2980 } ZEND_HASH_FOREACH_END();
2981 smart_str_0(&list);
2982 if (list.s) {
2983 xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(list.s)), ZSTR_LEN(list.s));
2984 } else {
2985 xmlNodeSetContentLen(ret, BAD_CAST(""), 0);
2986 }
2987 smart_str_free(&list);
2988 } else {
2989 zval tmp;
2990 char *str, *start, *next;
2991 smart_str list = {0};
2992
2993 if (Z_TYPE_P(data) != IS_STRING) {
2994 ZVAL_STR(&tmp, zval_get_string_func(data));
2995 data = &tmp;
2996 }
2997 str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
2998 whiteSpace_collapse(BAD_CAST(str));
2999 start = str;
3000 while (start != NULL && *start != '\0') {
3001 xmlNodePtr dummy;
3002 zval dummy_zval;
3003
3004 next = strchr(start,' ');
3005 if (next != NULL) {
3006 *next = '\0';
3007 next++;
3008 }
3009 ZVAL_STRING(&dummy_zval, start);
3010 dummy = master_to_xml(list_enc, &dummy_zval, SOAP_LITERAL, ret);
3011 zval_ptr_dtor(&dummy_zval);
3012 if (dummy && dummy->children && dummy->children->content) {
3013 if (list.s && ZSTR_LEN(list.s) != 0) {
3014 smart_str_appendc(&list, ' ');
3015 }
3016 smart_str_appends(&list, (char*)dummy->children->content);
3017 } else {
3018 soap_error0(E_ERROR, "Encoding: Violation of encoding rules");
3019 }
3020 xmlUnlinkNode(dummy);
3021 xmlFreeNode(dummy);
3022
3023 start = next;
3024 }
3025 smart_str_0(&list);
3026 if (list.s) {
3027 xmlNodeSetContentLen(ret, BAD_CAST(ZSTR_VAL(list.s)), ZSTR_LEN(list.s));
3028 } else {
3029 xmlNodeSetContentLen(ret, BAD_CAST(""), 0);
3030 }
3031 smart_str_free(&list);
3032 efree(str);
3033 if (data == &tmp) {
3034 zval_ptr_dtor_str(&tmp);
3035 }
3036 }
3037 return ret;
3038 }
3039
to_xml_list1(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent)3040 static xmlNodePtr to_xml_list1(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent) {
3041 /*FIXME: minLength=1 */
3042 return to_xml_list(enc,data,style, parent);
3043 }
3044
to_zval_union(zval * ret,encodeTypePtr enc,xmlNodePtr data)3045 static zval* to_zval_union(zval *ret, encodeTypePtr enc, xmlNodePtr data) {
3046 /*FIXME*/
3047 return to_zval_list(ret, enc, data);
3048 }
3049
to_xml_union(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent)3050 static xmlNodePtr to_xml_union(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent) {
3051 /*FIXME*/
3052 return to_xml_list(enc,data,style, parent);
3053 }
3054
to_zval_any(zval * ret,encodeTypePtr type,xmlNodePtr data)3055 static zval *to_zval_any(zval *ret, encodeTypePtr type, xmlNodePtr data)
3056 {
3057 xmlBufferPtr buf;
3058
3059 if (SOAP_GLOBAL(sdl) && SOAP_GLOBAL(sdl)->elements && data->name) {
3060 smart_str nscat = {0};
3061 sdlTypePtr sdl_type;
3062
3063 if (data->ns && data->ns->href) {
3064 smart_str_appends(&nscat, (char*)data->ns->href);
3065 smart_str_appendc(&nscat, ':');
3066 }
3067 smart_str_appends(&nscat, (char*)data->name);
3068 smart_str_0(&nscat);
3069
3070 if ((sdl_type = zend_hash_find_ptr(SOAP_GLOBAL(sdl)->elements, nscat.s)) != NULL &&
3071 sdl_type->encode) {
3072 smart_str_free(&nscat);
3073 return master_to_zval_int(ret, sdl_type->encode, data);
3074 }
3075 smart_str_free(&nscat);
3076 }
3077
3078 buf = xmlBufferCreate();
3079 xmlNodeDump(buf, NULL, data, 0, 0);
3080 ZVAL_STRING(ret, (char*)xmlBufferContent(buf));
3081 xmlBufferFree(buf);
3082 return ret;
3083 }
3084
to_xml_any(encodeTypePtr type,zval * data,int style,xmlNodePtr parent)3085 static xmlNodePtr to_xml_any(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
3086 {
3087 xmlNodePtr ret = NULL;
3088
3089 if (Z_TYPE_P(data) == IS_ARRAY) {
3090 zval *el;
3091 encodePtr enc = get_conversion(XSD_ANYXML);
3092 zend_string *name;
3093
3094 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(data), name, el) {
3095 ret = master_to_xml(enc, el, style, parent);
3096 if (ret &&
3097 ret->name != xmlStringTextNoenc) {
3098 xmlNodeSetName(ret, BAD_CAST(ZSTR_VAL(name)));
3099 }
3100 } ZEND_HASH_FOREACH_END();
3101 return ret;
3102 }
3103 if (Z_TYPE_P(data) == IS_STRING) {
3104 ret = xmlNewTextLen(BAD_CAST(Z_STRVAL_P(data)), Z_STRLEN_P(data));
3105 } else {
3106 zend_string *tmp = zval_get_string_func(data);
3107 ret = xmlNewTextLen(BAD_CAST(ZSTR_VAL(tmp)), ZSTR_LEN(tmp));
3108 zend_string_release_ex(tmp, 0);
3109 }
3110
3111 ret->name = xmlStringTextNoenc;
3112 ret->parent = parent;
3113 ret->doc = parent->doc;
3114 ret->prev = parent->last;
3115 ret->next = NULL;
3116 if (parent->last) {
3117 parent->last->next = ret;
3118 } else {
3119 parent->children = ret;
3120 }
3121 parent->last = ret;
3122
3123 return ret;
3124 }
3125
sdl_guess_convert_zval(zval * ret,encodeTypePtr enc,xmlNodePtr data)3126 zval *sdl_guess_convert_zval(zval *ret, encodeTypePtr enc, xmlNodePtr data)
3127 {
3128 sdlTypePtr type;
3129
3130 type = enc->sdl_type;
3131 if (type == NULL) {
3132 return guess_zval_convert(ret, enc, data);
3133 }
3134 /*FIXME: restriction support
3135 if (type && type->restrictions &&
3136 data && data->children && data->children->content) {
3137 if (type->restrictions->whiteSpace && type->restrictions->whiteSpace->value) {
3138 if (strcmp(type->restrictions->whiteSpace->value,"replace") == 0) {
3139 whiteSpace_replace(data->children->content);
3140 } else if (strcmp(type->restrictions->whiteSpace->value,"collapse") == 0) {
3141 whiteSpace_collapse(data->children->content);
3142 }
3143 }
3144 if (type->restrictions->enumeration) {
3145 if (!zend_hash_exists(type->restrictions->enumeration,data->children->content,strlen(data->children->content)+1)) {
3146 soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\"", data->children->content);
3147 }
3148 }
3149 if (type->restrictions->minLength &&
3150 strlen(data->children->content) < type->restrictions->minLength->value) {
3151 soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
3152 }
3153 if (type->restrictions->maxLength &&
3154 strlen(data->children->content) > type->restrictions->maxLength->value) {
3155 soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
3156 }
3157 if (type->restrictions->length &&
3158 strlen(data->children->content) != type->restrictions->length->value) {
3159 soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
3160 }
3161 }
3162 */
3163 switch (type->kind) {
3164 case XSD_TYPEKIND_SIMPLE:
3165 if (type->encode && enc != &type->encode->details) {
3166 return master_to_zval_int(ret, type->encode, data);
3167 } else {
3168 return guess_zval_convert(ret, enc, data);
3169 }
3170 break;
3171 case XSD_TYPEKIND_LIST:
3172 return to_zval_list(ret, enc, data);
3173 case XSD_TYPEKIND_UNION:
3174 return to_zval_union(ret, enc, data);
3175 case XSD_TYPEKIND_COMPLEX:
3176 case XSD_TYPEKIND_RESTRICTION:
3177 case XSD_TYPEKIND_EXTENSION:
3178 if (type->encode &&
3179 (type->encode->details.type == IS_ARRAY ||
3180 type->encode->details.type == SOAP_ENC_ARRAY)) {
3181 return to_zval_array(ret, enc, data);
3182 }
3183 return to_zval_object(ret, enc, data);
3184 default:
3185 soap_error0(E_ERROR, "Encoding: Internal Error");
3186 return guess_zval_convert(ret, enc, data);
3187 }
3188 }
3189
sdl_guess_convert_xml(encodeTypePtr enc,zval * data,int style,xmlNodePtr parent)3190 xmlNodePtr sdl_guess_convert_xml(encodeTypePtr enc, zval *data, int style, xmlNodePtr parent)
3191 {
3192 sdlTypePtr type;
3193 xmlNodePtr ret = NULL;
3194
3195 type = enc->sdl_type;
3196
3197 if (type == NULL) {
3198 ret = guess_xml_convert(enc, data, style, parent);
3199 if (style == SOAP_ENCODED) {
3200 set_ns_and_type(ret, enc);
3201 }
3202 return ret;
3203 }
3204 /*FIXME: restriction support
3205 if (type) {
3206 if (type->restrictions && Z_TYPE_P(data) == IS_STRING) {
3207 if (type->restrictions->enumeration) {
3208 if (!zend_hash_exists(type->restrictions->enumeration,Z_STRVAL_P(data),Z_STRLEN_P(data)+1)) {
3209 soap_error1(E_WARNING, "Encoding: Restriction: invalid enumeration value \"%s\".", Z_STRVAL_P(data));
3210 }
3211 }
3212 if (type->restrictions->minLength &&
3213 Z_STRLEN_P(data) < type->restrictions->minLength->value) {
3214 soap_error0(E_WARNING, "Encoding: Restriction: length less than 'minLength'");
3215 }
3216 if (type->restrictions->maxLength &&
3217 Z_STRLEN_P(data) > type->restrictions->maxLength->value) {
3218 soap_error0(E_WARNING, "Encoding: Restriction: length greater than 'maxLength'");
3219 }
3220 if (type->restrictions->length &&
3221 Z_STRLEN_P(data) != type->restrictions->length->value) {
3222 soap_error0(E_WARNING, "Encoding: Restriction: length is not equal to 'length'");
3223 }
3224 }
3225 }
3226 */
3227 switch(type->kind) {
3228 case XSD_TYPEKIND_SIMPLE:
3229 if (type->encode && enc != &type->encode->details) {
3230 ret = master_to_xml(type->encode, data, style, parent);
3231 } else {
3232 ret = guess_xml_convert(enc, data, style, parent);
3233 }
3234 break;
3235 case XSD_TYPEKIND_LIST:
3236 ret = to_xml_list(enc, data, style, parent);
3237 break;
3238 case XSD_TYPEKIND_UNION:
3239 ret = to_xml_union(enc, data, style, parent);
3240 break;
3241 case XSD_TYPEKIND_COMPLEX:
3242 case XSD_TYPEKIND_RESTRICTION:
3243 case XSD_TYPEKIND_EXTENSION:
3244 if (type->encode &&
3245 (type->encode->details.type == IS_ARRAY ||
3246 type->encode->details.type == SOAP_ENC_ARRAY)) {
3247 return to_xml_array(enc, data, style, parent);
3248 } else {
3249 return to_xml_object(enc, data, style, parent);
3250 }
3251 break;
3252 default:
3253 soap_error0(E_ERROR, "Encoding: Internal Error");
3254 break;
3255 }
3256 if (style == SOAP_ENCODED) {
3257 set_ns_and_type(ret, enc);
3258 }
3259 return ret;
3260 }
3261
check_and_resolve_href(xmlNodePtr data)3262 static xmlNodePtr check_and_resolve_href(xmlNodePtr data)
3263 {
3264 if (data && data->properties) {
3265 xmlAttrPtr href;
3266
3267 href = data->properties;
3268 while (1) {
3269 href = get_attribute(href, "href");
3270 if (href == NULL || href->ns == NULL) {break;}
3271 href = href->next;
3272 }
3273 if (href) {
3274 /* Internal href try and find node */
3275 if (href->children->content[0] == '#') {
3276 xmlNodePtr ret = get_node_with_attribute_recursive(data->doc->children, NULL, "id", (char*)&href->children->content[1]);
3277 if (!ret) {
3278 soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
3279 }
3280 return ret;
3281 } else {
3282 /* TODO: External href....? */
3283 soap_error1(E_ERROR, "Encoding: External reference '%s'", href->children->content);
3284 }
3285 }
3286 /* SOAP 1.2 enc:id enc:ref */
3287 href = get_attribute_ex(data->properties, "ref", SOAP_1_2_ENC_NAMESPACE);
3288 if (href) {
3289 xmlChar* id;
3290 xmlNodePtr ret;
3291
3292 if (href->children->content[0] == '#') {
3293 id = href->children->content+1;
3294 } else {
3295 id = href->children->content;
3296 }
3297 ret = get_node_with_attribute_recursive_ex(data->doc->children, NULL, NULL, "id", (char*)id, SOAP_1_2_ENC_NAMESPACE);
3298 if (!ret) {
3299 soap_error1(E_ERROR, "Encoding: Unresolved reference '%s'", href->children->content);
3300 } else if (ret == data) {
3301 soap_error1(E_ERROR, "Encoding: Violation of id and ref information items '%s'", href->children->content);
3302 }
3303 return ret;
3304 }
3305 }
3306 return data;
3307 }
3308
set_ns_and_type(xmlNodePtr node,encodeTypePtr type)3309 static void set_ns_and_type(xmlNodePtr node, encodeTypePtr type)
3310 {
3311 set_ns_and_type_ex(node, type->ns, type->type_str);
3312 }
3313
set_ns_and_type_ex(xmlNodePtr node,char * ns,char * type)3314 static void set_ns_and_type_ex(xmlNodePtr node, char *ns, char *type)
3315 {
3316 smart_str nstype = {0};
3317 get_type_str(node, ns, type, &nstype);
3318 set_xsi_type(node, ZSTR_VAL(nstype.s));
3319 smart_str_free(&nstype);
3320 }
3321
xmlSearchNsPrefixByHref(xmlDocPtr doc,xmlNodePtr node,const xmlChar * href)3322 static xmlNsPtr xmlSearchNsPrefixByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
3323 {
3324 xmlNsPtr cur;
3325 xmlNodePtr orig = node;
3326
3327 while (node) {
3328 if (node->type == XML_ENTITY_REF_NODE ||
3329 node->type == XML_ENTITY_NODE ||
3330 node->type == XML_ENTITY_DECL) {
3331 return NULL;
3332 }
3333 if (node->type == XML_ELEMENT_NODE) {
3334 cur = node->nsDef;
3335 while (cur != NULL) {
3336 if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
3337 if (xmlSearchNs(doc, node, cur->prefix) == cur) {
3338 return cur;
3339 }
3340 }
3341 cur = cur->next;
3342 }
3343 if (orig != node) {
3344 cur = node->ns;
3345 if (cur != NULL) {
3346 if (cur->prefix && cur->href && xmlStrEqual(cur->href, href)) {
3347 if (xmlSearchNs(doc, node, cur->prefix) == cur) {
3348 return cur;
3349 }
3350 }
3351 }
3352 }
3353 }
3354 node = node->parent;
3355 }
3356 return NULL;
3357 }
3358
encode_add_ns(xmlNodePtr node,const char * ns)3359 xmlNsPtr encode_add_ns(xmlNodePtr node, const char* ns)
3360 {
3361 xmlNsPtr xmlns;
3362
3363 if (ns == NULL) {
3364 return NULL;
3365 }
3366
3367 xmlns = xmlSearchNsByHref(node->doc, node, BAD_CAST(ns));
3368 if (xmlns != NULL && xmlns->prefix == NULL) {
3369 xmlns = xmlSearchNsPrefixByHref(node->doc, node, BAD_CAST(ns));
3370 }
3371 if (xmlns == NULL) {
3372 xmlChar* prefix;
3373
3374 if ((prefix = zend_hash_str_find_ptr(&SOAP_GLOBAL(defEncNs), (char*)ns, strlen(ns))) != NULL) {
3375 xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), prefix);
3376 } else {
3377 smart_str prefix = {0};
3378 int num = ++SOAP_GLOBAL(cur_uniq_ns);
3379 xmlChar *enc_ns;
3380
3381 while (1) {
3382 smart_str_appendl(&prefix, "ns", 2);
3383 smart_str_append_long(&prefix, num);
3384 smart_str_0(&prefix);
3385 if (xmlSearchNs(node->doc, node, BAD_CAST(ZSTR_VAL(prefix.s))) == NULL) {
3386 break;
3387 }
3388 smart_str_free(&prefix);
3389 prefix.s = NULL;
3390 num = ++SOAP_GLOBAL(cur_uniq_ns);
3391 }
3392
3393 enc_ns = xmlEncodeSpecialChars(node->doc, BAD_CAST(ns));
3394 xmlns = xmlNewNs(node->doc->children, enc_ns, BAD_CAST(prefix.s ? ZSTR_VAL(prefix.s) : ""));
3395 xmlFree(enc_ns);
3396 smart_str_free(&prefix);
3397 }
3398 }
3399 return xmlns;
3400 }
3401
set_ns_prop(xmlNodePtr node,char * ns,char * name,char * val)3402 static void set_ns_prop(xmlNodePtr node, char *ns, char *name, char *val)
3403 {
3404 xmlSetNsProp(node, encode_add_ns(node, ns), BAD_CAST(name), BAD_CAST(val));
3405 }
3406
set_xsi_nil(xmlNodePtr node)3407 static void set_xsi_nil(xmlNodePtr node)
3408 {
3409 set_ns_prop(node, XSI_NAMESPACE, "nil", "true");
3410 }
3411
set_xsi_type(xmlNodePtr node,char * type)3412 static void set_xsi_type(xmlNodePtr node, char *type)
3413 {
3414 set_ns_prop(node, XSI_NAMESPACE, "type", type);
3415 }
3416
encode_reset_ns()3417 void encode_reset_ns()
3418 {
3419 SOAP_GLOBAL(cur_uniq_ns) = 0;
3420 SOAP_GLOBAL(cur_uniq_ref) = 0;
3421 if (SOAP_GLOBAL(ref_map)) {
3422 zend_hash_destroy(SOAP_GLOBAL(ref_map));
3423 } else {
3424 SOAP_GLOBAL(ref_map) = emalloc(sizeof(HashTable));
3425 }
3426 zend_hash_init(SOAP_GLOBAL(ref_map), 0, NULL, NULL, 0);
3427 }
3428
encode_finish()3429 void encode_finish()
3430 {
3431 SOAP_GLOBAL(cur_uniq_ns) = 0;
3432 SOAP_GLOBAL(cur_uniq_ref) = 0;
3433 if (SOAP_GLOBAL(ref_map)) {
3434 zend_hash_destroy(SOAP_GLOBAL(ref_map));
3435 efree(SOAP_GLOBAL(ref_map));
3436 SOAP_GLOBAL(ref_map) = NULL;
3437 }
3438 }
3439
get_conversion(int encode)3440 encodePtr get_conversion(int encode)
3441 {
3442 encodePtr enc;
3443
3444 if ((enc = zend_hash_index_find_ptr(&SOAP_GLOBAL(defEncIndex), encode)) == NULL) {
3445 soap_error0(E_ERROR, "Encoding: Cannot find encoding");
3446 return NULL;
3447 } else {
3448 return enc;
3449 }
3450 }
3451
is_map(zval * array)3452 static int is_map(zval *array)
3453 {
3454 zend_ulong index;
3455 zend_string *key;
3456 zend_ulong i = 0;
3457
3458 if (HT_IS_PACKED(Z_ARRVAL_P(array)) && HT_IS_WITHOUT_HOLES(Z_ARRVAL_P(array))) {
3459 return FALSE;
3460 }
3461
3462 ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(array), index, key) {
3463 if (key || index != i) {
3464 return TRUE;
3465 }
3466 i++;
3467 } ZEND_HASH_FOREACH_END();
3468 return FALSE;
3469 }
3470
get_array_type(xmlNodePtr node,zval * array,smart_str * type)3471 static encodePtr get_array_type(xmlNodePtr node, zval *array, smart_str *type)
3472 {
3473 HashTable *ht;
3474 int i, cur_type, prev_type, different;
3475 zval *tmp;
3476 char *prev_stype = NULL, *cur_stype = NULL, *prev_ns = NULL, *cur_ns = NULL;
3477
3478 if (!array || Z_TYPE_P(array) != IS_ARRAY) {
3479 smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
3480 return get_conversion(XSD_ANYTYPE);
3481 }
3482
3483 i = 0;
3484 different = FALSE;
3485 cur_type = prev_type = 0;
3486 ht = Z_ARRVAL_P(array);
3487
3488 ZEND_HASH_FOREACH_VAL_IND(ht, tmp) {
3489 ZVAL_DEREF(tmp);
3490 if (Z_TYPE_P(tmp) == IS_OBJECT &&
3491 Z_OBJCE_P(tmp) == soap_var_class_entry) {
3492 zval *ztype = Z_VAR_ENC_TYPE_P(tmp);
3493 if (Z_TYPE_P(ztype) != IS_LONG) {
3494 soap_error0(E_ERROR, "Encoding: SoapVar has no 'enc_type' property");
3495 }
3496 cur_type = Z_LVAL_P(ztype);
3497
3498 zval *zstype = Z_VAR_ENC_STYPE_P(tmp);
3499 if (Z_TYPE_P(zstype) == IS_STRING) {
3500 cur_stype = Z_STRVAL_P(zstype);
3501 } else {
3502 cur_stype = NULL;
3503 }
3504
3505 zval *zns = Z_VAR_ENC_NS_P(tmp);
3506 if (Z_TYPE_P(zns) == IS_STRING) {
3507 cur_ns = Z_STRVAL_P(zns);
3508 } else {
3509 cur_ns = NULL;
3510 }
3511
3512 } else if (Z_TYPE_P(tmp) == IS_ARRAY && is_map(tmp)) {
3513 cur_type = APACHE_MAP;
3514 cur_stype = NULL;
3515 cur_ns = NULL;
3516 } else {
3517 cur_type = Z_TYPE_P(tmp);
3518 cur_stype = NULL;
3519 cur_ns = NULL;
3520 }
3521
3522 if (i > 0) {
3523 if ((cur_type != prev_type) ||
3524 (cur_stype != NULL && prev_stype != NULL && strcmp(cur_stype,prev_stype) != 0) ||
3525 (cur_stype == NULL && cur_stype != prev_stype) ||
3526 (cur_ns != NULL && prev_ns != NULL && strcmp(cur_ns,prev_ns) != 0) ||
3527 (cur_ns == NULL && cur_ns != prev_ns)) {
3528 different = TRUE;
3529 break;
3530 }
3531 }
3532
3533 prev_type = cur_type;
3534 prev_stype = cur_stype;
3535 prev_ns = cur_ns;
3536 i++;
3537 } ZEND_HASH_FOREACH_END();
3538
3539 if (different || i == 0) {
3540 smart_str_appendl(type, "xsd:anyType", sizeof("xsd:anyType")-1);
3541 return get_conversion(XSD_ANYTYPE);
3542 } else {
3543 encodePtr enc;
3544
3545 if (cur_stype != NULL) {
3546 smart_str array_type = {0};
3547
3548 if (cur_ns) {
3549 xmlNsPtr ns = encode_add_ns(node, cur_ns);
3550
3551 smart_str_appends(type, (char*)ns->prefix);
3552 smart_str_appendc(type, ':');
3553 smart_str_appends(&array_type, cur_ns);
3554 smart_str_appendc(&array_type, ':');
3555 }
3556 smart_str_appends(type, cur_stype);
3557 smart_str_0(type);
3558 smart_str_appends(&array_type, cur_stype);
3559 smart_str_0(&array_type);
3560
3561 enc = get_encoder_ex(SOAP_GLOBAL(sdl), ZSTR_VAL(array_type.s), ZSTR_LEN(array_type.s));
3562 smart_str_free(&array_type);
3563 return enc;
3564 } else {
3565 enc = get_conversion(cur_type);
3566 get_type_str(node, enc->details.ns, enc->details.type_str, type);
3567 return enc;
3568 }
3569 }
3570 return NULL;
3571 }
3572
get_type_str(xmlNodePtr node,const char * ns,const char * type,smart_str * ret)3573 static void get_type_str(xmlNodePtr node, const char* ns, const char* type, smart_str* ret)
3574 {
3575
3576 if (ns) {
3577 xmlNsPtr xmlns;
3578 if (SOAP_GLOBAL(soap_version) == SOAP_1_2 &&
3579 strcmp(ns,SOAP_1_1_ENC_NAMESPACE) == 0) {
3580 ns = SOAP_1_2_ENC_NAMESPACE;
3581 } else if (SOAP_GLOBAL(soap_version) == SOAP_1_1 &&
3582 strcmp(ns,SOAP_1_2_ENC_NAMESPACE) == 0) {
3583 ns = SOAP_1_1_ENC_NAMESPACE;
3584 }
3585 xmlns = encode_add_ns(node, ns);
3586 smart_str_appends(ret, (char*)xmlns->prefix);
3587 smart_str_appendc(ret, ':');
3588 }
3589 smart_str_appendl(ret, type, strlen(type));
3590 smart_str_0(ret);
3591 }
3592
delete_mapping(void * data)3593 static void delete_mapping(void *data)
3594 {
3595 soapMappingPtr map = (soapMappingPtr)data;
3596
3597 zval_ptr_dtor(&map->to_xml);
3598 zval_ptr_dtor(&map->to_zval);
3599 efree(map);
3600 }
3601
delete_encoder(zval * zv)3602 void delete_encoder(zval *zv)
3603 {
3604 encodePtr t = Z_PTR_P(zv);
3605 if (t->details.ns) {
3606 efree(t->details.ns);
3607 }
3608 if (t->details.type_str) {
3609 efree(t->details.type_str);
3610 }
3611 if (t->details.map) {
3612 delete_mapping(t->details.map);
3613 }
3614 efree(t);
3615 }
3616
delete_encoder_persistent(zval * zv)3617 void delete_encoder_persistent(zval *zv)
3618 {
3619 encodePtr t = Z_PTR_P(zv);
3620 if (t->details.ns) {
3621 free(t->details.ns);
3622 }
3623 if (t->details.type_str) {
3624 free(t->details.type_str);
3625 }
3626 /* we should never have mapping in persistent encoder */
3627 assert(t->details.map == NULL);
3628 free(t);
3629 }
3630