php_encoding.c 110 KB

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