PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/soap - php_encoding.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 2035 0.0 %
Date: 2014-04-06 Functions: 0 87 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 06 Apr 2014 17:31:09 +0000 (9 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.