php_sdl.c 107 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 "php_soap.h"
  21. #include "ext/libxml/php_libxml.h"
  22. #include "libxml/uri.h"
  23. #include "ext/standard/md5.h"
  24. #include "zend_virtual_cwd.h"
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #ifndef O_BINARY
  29. # define O_BINARY 0
  30. #endif
  31. static void delete_fault(zval *zv);
  32. static void delete_fault_persistent(zval *zv);
  33. static void delete_binding(zval *zv);
  34. static void delete_binding_persistent(zval *zv);
  35. static void delete_function(zval *zv);
  36. static void delete_function_persistent(zval *zv);
  37. static void delete_parameter(zval *zv);
  38. static void delete_parameter_persistent(zval *zv);
  39. static void delete_header(zval *header);
  40. static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr);
  41. static void delete_header_persistent(zval *zv);
  42. static void delete_document(zval *zv);
  43. encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  44. {
  45. encodePtr enc = NULL;
  46. xmlNsPtr nsptr;
  47. char *ns, *cptype;
  48. parse_namespace(type, &cptype, &ns);
  49. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  50. if (nsptr != NULL) {
  51. enc = get_encoder(sdl, (char*)nsptr->href, cptype);
  52. if (enc == NULL) {
  53. enc = get_encoder_ex(sdl, cptype, strlen(cptype));
  54. }
  55. } else {
  56. enc = get_encoder_ex(sdl, (char*)type, xmlStrlen(type));
  57. }
  58. efree(cptype);
  59. if (ns) {efree(ns);}
  60. return enc;
  61. }
  62. static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
  63. {
  64. sdlTypePtr ret = NULL;
  65. if (sdl->elements) {
  66. xmlNsPtr nsptr;
  67. char *ns, *cptype;
  68. sdlTypePtr sdl_type;
  69. parse_namespace(type, &cptype, &ns);
  70. nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
  71. if (nsptr != NULL) {
  72. int ns_len = xmlStrlen(nsptr->href);
  73. int type_len = strlen(cptype);
  74. int len = ns_len + type_len + 1;
  75. char *nscat = emalloc(len + 1);
  76. memcpy(nscat, nsptr->href, ns_len);
  77. nscat[ns_len] = ':';
  78. memcpy(nscat+ns_len+1, cptype, type_len);
  79. nscat[len] = '\0';
  80. if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, nscat, len)) != NULL) {
  81. ret = sdl_type;
  82. } else if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, type_len)) != NULL) {
  83. ret = sdl_type;
  84. }
  85. efree(nscat);
  86. } else {
  87. if ((sdl_type = zend_hash_str_find_ptr(sdl->elements, (char*)type, xmlStrlen(type))) != NULL) {
  88. ret = sdl_type;
  89. }
  90. }
  91. efree(cptype);
  92. if (ns) {efree(ns);}
  93. }
  94. return ret;
  95. }
  96. encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type)
  97. {
  98. encodePtr enc = NULL;
  99. char *nscat;
  100. int ns_len = ns ? strlen(ns) : 0;
  101. int type_len = strlen(type);
  102. int len = ns_len + type_len + 1;
  103. nscat = emalloc(len + 1);
  104. memcpy(nscat, ns, ns_len);
  105. nscat[ns_len] = ':';
  106. memcpy(nscat+ns_len+1, type, type_len);
  107. nscat[len] = '\0';
  108. enc = get_encoder_ex(sdl, nscat, len);
  109. if (enc == NULL &&
  110. ((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  111. memcmp(ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  112. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  113. memcmp(ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  114. char *enc_nscat;
  115. int enc_ns_len;
  116. int enc_len;
  117. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  118. enc_len = enc_ns_len + type_len + 1;
  119. enc_nscat = emalloc(enc_len + 1);
  120. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  121. enc_nscat[enc_ns_len] = ':';
  122. memcpy(enc_nscat+enc_ns_len+1, type, type_len);
  123. enc_nscat[enc_len] = '\0';
  124. enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  125. efree(enc_nscat);
  126. if (enc && sdl) {
  127. encodePtr new_enc = pemalloc(sizeof(encode), sdl->is_persistent);
  128. memcpy(new_enc, enc, sizeof(encode));
  129. if (sdl->is_persistent) {
  130. new_enc->details.ns = zend_strndup(ns, ns_len);
  131. new_enc->details.type_str = strdup(new_enc->details.type_str);
  132. } else {
  133. new_enc->details.ns = estrndup(ns, ns_len);
  134. new_enc->details.type_str = estrdup(new_enc->details.type_str);
  135. }
  136. if (sdl->encoders == NULL) {
  137. sdl->encoders = pemalloc(sizeof(HashTable), sdl->is_persistent);
  138. zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, sdl->is_persistent);
  139. }
  140. zend_hash_str_update_ptr(sdl->encoders, nscat, len, new_enc);
  141. enc = new_enc;
  142. }
  143. }
  144. efree(nscat);
  145. return enc;
  146. }
  147. encodePtr get_encoder_ex(sdlPtr sdl, const char *nscat, int len)
  148. {
  149. encodePtr enc;
  150. if ((enc = zend_hash_str_find_ptr(&SOAP_GLOBAL(defEnc), (char*)nscat, len)) != NULL) {
  151. return enc;
  152. } else if (sdl && sdl->encoders && (enc = zend_hash_str_find_ptr(sdl->encoders, (char*)nscat, len)) != NULL) {
  153. return enc;
  154. }
  155. return NULL;
  156. }
  157. sdlBindingPtr get_binding_from_type(sdlPtr sdl, sdlBindingType type)
  158. {
  159. sdlBindingPtr binding;
  160. if (sdl == NULL) {
  161. return NULL;
  162. }
  163. ZEND_HASH_FOREACH_PTR(sdl->bindings, binding) {
  164. if (binding->bindingType == type) {
  165. return binding;
  166. }
  167. } ZEND_HASH_FOREACH_END();
  168. return NULL;
  169. }
  170. sdlBindingPtr get_binding_from_name(sdlPtr sdl, char *name, char *ns)
  171. {
  172. sdlBindingPtr binding;
  173. smart_str key = {0};
  174. smart_str_appends(&key, ns);
  175. smart_str_appendc(&key, ':');
  176. smart_str_appends(&key, name);
  177. smart_str_0(&key);
  178. binding = zend_hash_find_ptr(sdl->bindings, key.s);
  179. smart_str_free(&key);
  180. return binding;
  181. }
  182. static int is_wsdl_element(xmlNodePtr node)
  183. {
  184. if (node->ns && strcmp((char*)node->ns->href, WSDL_NAMESPACE) != 0) {
  185. xmlAttrPtr attr;
  186. if ((attr = get_attribute_ex(node->properties, "required", WSDL_NAMESPACE)) != NULL &&
  187. attr->children && attr->children->content &&
  188. (strcmp((char*)attr->children->content, "1") == 0 ||
  189. strcmp((char*)attr->children->content, "true") == 0)) {
  190. soap_error1(E_ERROR, "Parsing WSDL: Unknown required WSDL extension '%s'", node->ns->href);
  191. }
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. void sdl_set_uri_credentials(sdlCtx *ctx, char *uri)
  197. {
  198. char *s;
  199. size_t l1, l2;
  200. zval context;
  201. zval *header = NULL;
  202. /* check if we load xsd from the same server */
  203. s = strstr(ctx->sdl->source, "://");
  204. if (!s) return;
  205. s = strchr(s+3, '/');
  206. l1 = s ? (size_t)(s - ctx->sdl->source) : strlen(ctx->sdl->source);
  207. s = strstr((char*)uri, "://");
  208. if (!s) return;
  209. s = strchr(s+3, '/');
  210. l2 = s ? (size_t)(s - (char*)uri) : strlen((char*)uri);
  211. if (l1 != l2) {
  212. /* check for http://...:80/ */
  213. if (l1 > 11 &&
  214. ctx->sdl->source[4] == ':' &&
  215. ctx->sdl->source[l1-3] == ':' &&
  216. ctx->sdl->source[l1-2] == '8' &&
  217. ctx->sdl->source[l1-1] == '0') {
  218. l1 -= 3;
  219. }
  220. if (l2 > 11 &&
  221. uri[4] == ':' &&
  222. uri[l2-3] == ':' &&
  223. uri[l2-2] == '8' &&
  224. uri[l2-1] == '0') {
  225. l2 -= 3;
  226. }
  227. /* check for https://...:443/ */
  228. if (l1 > 13 &&
  229. ctx->sdl->source[4] == 's' &&
  230. ctx->sdl->source[l1-4] == ':' &&
  231. ctx->sdl->source[l1-3] == '4' &&
  232. ctx->sdl->source[l1-2] == '4' &&
  233. ctx->sdl->source[l1-1] == '3') {
  234. l1 -= 4;
  235. }
  236. if (l2 > 13 &&
  237. uri[4] == 's' &&
  238. uri[l2-4] == ':' &&
  239. uri[l2-3] == '4' &&
  240. uri[l2-2] == '4' &&
  241. uri[l2-1] == '3') {
  242. l2 -= 4;
  243. }
  244. }
  245. if (l1 != l2 || memcmp(ctx->sdl->source, uri, l1) != 0) {
  246. /* another server. clear authentication credentals */
  247. php_libxml_switch_context(NULL, &context);
  248. php_libxml_switch_context(&context, NULL);
  249. if (Z_TYPE(context) != IS_UNDEF) {
  250. zval *context_ptr = &context;
  251. ctx->context = php_stream_context_from_zval(context_ptr, 1);
  252. if (ctx->context &&
  253. (header = php_stream_context_get_option(ctx->context, "http", "header")) != NULL) {
  254. s = strstr(Z_STRVAL_P(header), "Authorization: Basic");
  255. if (s && (s == Z_STRVAL_P(header) || *(s-1) == '\n' || *(s-1) == '\r')) {
  256. char *rest = strstr(s, "\r\n");
  257. if (rest) {
  258. zval new_header;
  259. rest += 2;
  260. ZVAL_NEW_STR(&new_header, zend_string_alloc(Z_STRLEN_P(header) - (rest - s), 0));
  261. memcpy(Z_STRVAL(new_header), Z_STRVAL_P(header), s - Z_STRVAL_P(header));
  262. memcpy(Z_STRVAL(new_header) + (s - Z_STRVAL_P(header)), rest, Z_STRLEN_P(header) - (rest - Z_STRVAL_P(header)) + 1);
  263. ZVAL_COPY(&ctx->old_header, header);
  264. php_stream_context_set_option(ctx->context, "http", "header", &new_header);
  265. zval_ptr_dtor(&new_header);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. void sdl_restore_uri_credentials(sdlCtx *ctx)
  273. {
  274. if (Z_TYPE(ctx->old_header) != IS_UNDEF) {
  275. php_stream_context_set_option(ctx->context, "http", "header", &ctx->old_header);
  276. zval_ptr_dtor(&ctx->old_header);
  277. ZVAL_UNDEF(&ctx->old_header);
  278. }
  279. ctx->context = NULL;
  280. }
  281. #define SAFE_STR(a) ((a)?((const char *)a):"")
  282. static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include)
  283. {
  284. sdlPtr tmpsdl = ctx->sdl;
  285. xmlDocPtr wsdl;
  286. xmlNodePtr root, definitions, trav;
  287. xmlAttrPtr targetNamespace;
  288. if (zend_hash_str_exists(&ctx->docs, struri, strlen(struri))) {
  289. return;
  290. }
  291. sdl_set_uri_credentials(ctx, struri);
  292. wsdl = soap_xmlParseFile(struri);
  293. sdl_restore_uri_credentials(ctx);
  294. if (!wsdl) {
  295. xmlErrorPtr xmlErrorPtr = xmlGetLastError();
  296. if (xmlErrorPtr) {
  297. soap_error2(E_ERROR, "Parsing WSDL: Couldn't load from '%s' : %s", struri, xmlErrorPtr->message);
  298. } else {
  299. soap_error1(E_ERROR, "Parsing WSDL: Couldn't load from '%s'", struri);
  300. }
  301. }
  302. zend_hash_str_add_ptr(&ctx->docs, struri, strlen(struri), wsdl);
  303. root = wsdl->children;
  304. definitions = get_node_ex(root, "definitions", WSDL_NAMESPACE);
  305. if (!definitions) {
  306. if (include) {
  307. xmlNodePtr schema = get_node_ex(root, "schema", XSD_NAMESPACE);
  308. if (schema) {
  309. load_schema(ctx, schema);
  310. return;
  311. }
  312. }
  313. soap_error1(E_ERROR, "Parsing WSDL: Couldn't find <definitions> in '%s'", struri);
  314. }
  315. if (!include) {
  316. targetNamespace = get_attribute(definitions->properties, "targetNamespace");
  317. if (targetNamespace) {
  318. tmpsdl->target_ns = estrdup((char*)targetNamespace->children->content);
  319. }
  320. }
  321. trav = definitions->children;
  322. while (trav != NULL) {
  323. if (!is_wsdl_element(trav)) {
  324. trav = trav->next;
  325. continue;
  326. }
  327. if (node_is_equal(trav,"types")) {
  328. /* TODO: Only one "types" is allowed */
  329. xmlNodePtr trav2 = trav->children;
  330. while (trav2 != NULL) {
  331. if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
  332. load_schema(ctx, trav2);
  333. } else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  334. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  335. }
  336. trav2 = trav2->next;
  337. }
  338. } else if (node_is_equal(trav,"import")) {
  339. /* TODO: namespace ??? */
  340. xmlAttrPtr tmp = get_attribute(trav->properties, "location");
  341. if (tmp) {
  342. xmlChar *uri;
  343. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  344. if (base == NULL) {
  345. uri = xmlBuildURI(tmp->children->content, trav->doc->URL);
  346. } else {
  347. uri = xmlBuildURI(tmp->children->content, base);
  348. xmlFree(base);
  349. }
  350. load_wsdl_ex(this_ptr, (char*)uri, ctx, 1);
  351. xmlFree(uri);
  352. }
  353. } else if (node_is_equal(trav,"message")) {
  354. xmlAttrPtr name = get_attribute(trav->properties, "name");
  355. if (name && name->children && name->children->content) {
  356. if (zend_hash_str_add_ptr(&ctx->messages, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  357. soap_error1(E_ERROR, "Parsing WSDL: <message> '%s' already defined", name->children->content);
  358. }
  359. } else {
  360. soap_error0(E_ERROR, "Parsing WSDL: <message> has no name attribute");
  361. }
  362. } else if (node_is_equal(trav,"portType")) {
  363. xmlAttrPtr name = get_attribute(trav->properties, "name");
  364. if (name && name->children && name->children->content) {
  365. if (zend_hash_str_add_ptr(&ctx->portTypes, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  366. soap_error1(E_ERROR, "Parsing WSDL: <portType> '%s' already defined", name->children->content);
  367. }
  368. } else {
  369. soap_error0(E_ERROR, "Parsing WSDL: <portType> has no name attribute");
  370. }
  371. } else if (node_is_equal(trav,"binding")) {
  372. xmlAttrPtr name = get_attribute(trav->properties, "name");
  373. if (name && name->children && name->children->content) {
  374. if (zend_hash_str_add_ptr(&ctx->bindings, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  375. soap_error1(E_ERROR, "Parsing WSDL: <binding> '%s' already defined", name->children->content);
  376. }
  377. } else {
  378. soap_error0(E_ERROR, "Parsing WSDL: <binding> has no name attribute");
  379. }
  380. } else if (node_is_equal(trav,"service")) {
  381. xmlAttrPtr name = get_attribute(trav->properties, "name");
  382. if (name && name->children && name->children->content) {
  383. if (zend_hash_str_add_ptr(&ctx->services, (char*)name->children->content, xmlStrlen(name->children->content), trav) == NULL) {
  384. soap_error1(E_ERROR, "Parsing WSDL: <service> '%s' already defined", name->children->content);
  385. }
  386. } else {
  387. soap_error0(E_ERROR, "Parsing WSDL: <service> has no name attribute");
  388. }
  389. } else if (!node_is_equal(trav,"documentation")) {
  390. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  391. }
  392. trav = trav->next;
  393. }
  394. }
  395. static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xmlNodePtr header, char* wsdl_soap_namespace, int fault)
  396. {
  397. xmlAttrPtr tmp;
  398. xmlNodePtr message, part;
  399. char *ctype;
  400. sdlSoapBindingFunctionHeaderPtr h;
  401. tmp = get_attribute(header->properties, "message");
  402. if (!tmp) {
  403. soap_error0(E_ERROR, "Parsing WSDL: Missing message attribute for <header>");
  404. }
  405. ctype = strrchr((char*)tmp->children->content,':');
  406. if (ctype == NULL) {
  407. ctype = (char*)tmp->children->content;
  408. } else {
  409. ++ctype;
  410. }
  411. if ((message = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  412. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", tmp->children->content);
  413. }
  414. tmp = get_attribute(header->properties, "part");
  415. if (!tmp) {
  416. soap_error0(E_ERROR, "Parsing WSDL: Missing part attribute for <header>");
  417. }
  418. part = get_node_with_attribute_ex(message->children, "part", WSDL_NAMESPACE, "name", (char*)tmp->children->content, NULL);
  419. if (!part) {
  420. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", tmp->children->content);
  421. }
  422. h = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  423. memset(h, 0, sizeof(sdlSoapBindingFunctionHeader));
  424. h->name = estrdup((char*)tmp->children->content);
  425. tmp = get_attribute(header->properties, "use");
  426. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  427. h->use = SOAP_ENCODED;
  428. } else {
  429. h->use = SOAP_LITERAL;
  430. }
  431. tmp = get_attribute(header->properties, "namespace");
  432. if (tmp) {
  433. h->ns = estrdup((char*)tmp->children->content);
  434. }
  435. if (h->use == SOAP_ENCODED) {
  436. tmp = get_attribute(header->properties, "encodingStyle");
  437. if (tmp) {
  438. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  439. h->encodingStyle = SOAP_ENCODING_1_1;
  440. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  441. h->encodingStyle = SOAP_ENCODING_1_2;
  442. } else {
  443. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  444. }
  445. } else {
  446. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  447. }
  448. }
  449. tmp = get_attribute(part->properties, "type");
  450. if (tmp != NULL) {
  451. h->encode = get_encoder_from_prefix(ctx->sdl, part, tmp->children->content);
  452. } else {
  453. tmp = get_attribute(part->properties, "element");
  454. if (tmp != NULL) {
  455. h->element = get_element(ctx->sdl, part, tmp->children->content);
  456. if (h->element) {
  457. h->encode = h->element->encode;
  458. if (!h->ns && h->element->namens) {
  459. h->ns = estrdup(h->element->namens);
  460. }
  461. if (h->element->name) {
  462. efree(h->name);
  463. h->name = estrdup(h->element->name);
  464. }
  465. }
  466. }
  467. }
  468. if (!fault) {
  469. xmlNodePtr trav = header->children;
  470. while (trav != NULL) {
  471. if (node_is_equal_ex(trav, "headerfault", wsdl_soap_namespace)) {
  472. sdlSoapBindingFunctionHeaderPtr hf = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 1);
  473. smart_str key = {0};
  474. if (h->headerfaults == NULL) {
  475. h->headerfaults = emalloc(sizeof(HashTable));
  476. zend_hash_init(h->headerfaults, 0, NULL, delete_header, 0);
  477. }
  478. if (hf->ns) {
  479. smart_str_appends(&key,hf->ns);
  480. smart_str_appendc(&key,':');
  481. }
  482. smart_str_appends(&key,hf->name);
  483. smart_str_0(&key);
  484. if (zend_hash_add_ptr(h->headerfaults, key.s, hf) == NULL) {
  485. delete_header_int(hf);
  486. }
  487. smart_str_free(&key);
  488. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  489. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  490. }
  491. trav = trav->next;
  492. }
  493. }
  494. return h;
  495. }
  496. static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap_namespace, sdlSoapBindingFunctionBody *binding, HashTable* params)
  497. {
  498. xmlNodePtr body, trav;
  499. xmlAttrPtr tmp;
  500. trav = node->children;
  501. while (trav != NULL) {
  502. if (node_is_equal_ex(trav, "body", wsdl_soap_namespace)) {
  503. body = trav;
  504. tmp = get_attribute(body->properties, "use");
  505. if (tmp && !strncmp((char*)tmp->children->content, "literal", sizeof("literal"))) {
  506. binding->use = SOAP_LITERAL;
  507. } else {
  508. binding->use = SOAP_ENCODED;
  509. }
  510. tmp = get_attribute(body->properties, "namespace");
  511. if (tmp) {
  512. binding->ns = estrdup((char*)tmp->children->content);
  513. }
  514. tmp = get_attribute(body->properties, "parts");
  515. if (tmp) {
  516. HashTable ht;
  517. char *parts = (char*)tmp->children->content;
  518. /* Delete all parts those are not in the "parts" attribute */
  519. zend_hash_init(&ht, 0, NULL, delete_parameter, 0);
  520. while (*parts) {
  521. sdlParamPtr param;
  522. int found = 0;
  523. char *end;
  524. while (*parts == ' ') ++parts;
  525. if (*parts == '\0') break;
  526. end = strchr(parts, ' ');
  527. if (end) *end = '\0';
  528. ZEND_HASH_FOREACH_PTR(params, param) {
  529. if (param->paramName &&
  530. strcmp(parts, param->paramName) == 0) {
  531. sdlParamPtr x_param;
  532. x_param = emalloc(sizeof(sdlParam));
  533. *x_param = *param;
  534. param->paramName = NULL;
  535. zend_hash_next_index_insert_ptr(&ht, x_param);
  536. found = 1;
  537. break;
  538. }
  539. } ZEND_HASH_FOREACH_END();
  540. if (!found) {
  541. soap_error1(E_ERROR, "Parsing WSDL: Missing part '%s' in <message>", parts);
  542. }
  543. parts += strlen(parts);
  544. if (end) *end = ' ';
  545. }
  546. zend_hash_destroy(params);
  547. *params = ht;
  548. }
  549. if (binding->use == SOAP_ENCODED) {
  550. tmp = get_attribute(body->properties, "encodingStyle");
  551. if (tmp) {
  552. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  553. binding->encodingStyle = SOAP_ENCODING_1_1;
  554. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  555. binding->encodingStyle = SOAP_ENCODING_1_2;
  556. } else {
  557. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  558. }
  559. } else {
  560. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  561. }
  562. }
  563. } else if (node_is_equal_ex(trav, "header", wsdl_soap_namespace)) {
  564. sdlSoapBindingFunctionHeaderPtr h = wsdl_soap_binding_header(ctx, trav, wsdl_soap_namespace, 0);
  565. smart_str key = {0};
  566. if (binding->headers == NULL) {
  567. binding->headers = emalloc(sizeof(HashTable));
  568. zend_hash_init(binding->headers, 0, NULL, delete_header, 0);
  569. }
  570. if (h->ns) {
  571. smart_str_appends(&key,h->ns);
  572. smart_str_appendc(&key,':');
  573. }
  574. smart_str_appends(&key,h->name);
  575. smart_str_0(&key);
  576. if (zend_hash_add_ptr(binding->headers, key.s, h) == NULL) {
  577. delete_header_int(h);
  578. }
  579. smart_str_free(&key);
  580. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  581. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  582. }
  583. trav = trav->next;
  584. }
  585. }
  586. static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
  587. {
  588. xmlNodePtr trav, part, message = NULL, tmp;
  589. HashTable* parameters = NULL;
  590. char *ctype;
  591. ctype = strrchr((char*)message_name,':');
  592. if (ctype == NULL) {
  593. ctype = (char*)message_name;
  594. } else {
  595. ++ctype;
  596. }
  597. if ((tmp = zend_hash_str_find_ptr(&ctx->messages, ctype, strlen(ctype))) == NULL) {
  598. soap_error1(E_ERROR, "Parsing WSDL: Missing <message> with name '%s'", message_name);
  599. }
  600. message = tmp;
  601. parameters = emalloc(sizeof(HashTable));
  602. zend_hash_init(parameters, 0, NULL, delete_parameter, 0);
  603. trav = message->children;
  604. while (trav != NULL) {
  605. xmlAttrPtr element, type, name;
  606. sdlParamPtr param;
  607. if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
  608. soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>", SAFE_STR(trav->name));
  609. }
  610. if (node_is_equal(trav,"documentation")) {
  611. trav = trav->next;
  612. continue;
  613. }
  614. if (!node_is_equal(trav,"part")) {
  615. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  616. }
  617. part = trav;
  618. param = emalloc(sizeof(sdlParam));
  619. memset(param,0,sizeof(sdlParam));
  620. param->order = 0;
  621. name = get_attribute(part->properties, "name");
  622. if (name == NULL) {
  623. soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'", SAFE_STR(message->name));
  624. }
  625. param->paramName = estrdup((char*)name->children->content);
  626. type = get_attribute(part->properties, "type");
  627. if (type != NULL) {
  628. param->encode = get_encoder_from_prefix(ctx->sdl, part, type->children->content);
  629. } else {
  630. element = get_attribute(part->properties, "element");
  631. if (element != NULL) {
  632. param->element = get_element(ctx->sdl, part, element->children->content);
  633. if (param->element) {
  634. param->encode = param->element->encode;
  635. }
  636. }
  637. }
  638. zend_hash_next_index_insert_ptr(parameters, param);
  639. trav = trav->next;
  640. }
  641. return parameters;
  642. }
  643. static sdlPtr load_wsdl(zval *this_ptr, char *struri)
  644. {
  645. sdlCtx ctx;
  646. int i,n;
  647. memset(&ctx,0,sizeof(ctx));
  648. ctx.sdl = emalloc(sizeof(sdl));
  649. memset(ctx.sdl, 0, sizeof(sdl));
  650. ctx.sdl->source = estrdup(struri);
  651. zend_hash_init(&ctx.sdl->functions, 0, NULL, delete_function, 0);
  652. zend_hash_init(&ctx.docs, 0, NULL, delete_document, 0);
  653. zend_hash_init(&ctx.messages, 0, NULL, NULL, 0);
  654. zend_hash_init(&ctx.bindings, 0, NULL, NULL, 0);
  655. zend_hash_init(&ctx.portTypes, 0, NULL, NULL, 0);
  656. zend_hash_init(&ctx.services, 0, NULL, NULL, 0);
  657. load_wsdl_ex(this_ptr, struri,&ctx, 0);
  658. schema_pass2(&ctx);
  659. n = zend_hash_num_elements(&ctx.services);
  660. if (n > 0) {
  661. zend_hash_internal_pointer_reset(&ctx.services);
  662. for (i = 0; i < n; i++) {
  663. xmlNodePtr service, tmp;
  664. xmlNodePtr trav, port;
  665. int has_soap_port = 0;
  666. service = tmp = zend_hash_get_current_data_ptr(&ctx.services);
  667. trav = service->children;
  668. while (trav != NULL) {
  669. xmlAttrPtr type, name, bindingAttr, location;
  670. xmlNodePtr portType, operation;
  671. xmlNodePtr address, binding, trav2;
  672. char *ctype;
  673. sdlBindingPtr tmpbinding;
  674. char *wsdl_soap_namespace = NULL;
  675. if (!is_wsdl_element(trav) || node_is_equal(trav,"documentation")) {
  676. trav = trav->next;
  677. continue;
  678. }
  679. if (!node_is_equal(trav,"port")) {
  680. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  681. }
  682. port = trav;
  683. tmpbinding = emalloc(sizeof(sdlBinding));
  684. memset(tmpbinding, 0, sizeof(sdlBinding));
  685. bindingAttr = get_attribute(port->properties, "binding");
  686. if (bindingAttr == NULL) {
  687. soap_error0(E_ERROR, "Parsing WSDL: No binding associated with <port>");
  688. }
  689. /* find address and figure out binding type */
  690. address = NULL;
  691. trav2 = port->children;
  692. while (trav2 != NULL) {
  693. if (node_is_equal(trav2,"address") && trav2->ns) {
  694. if (!strncmp((char*)trav2->ns->href, WSDL_SOAP11_NAMESPACE, sizeof(WSDL_SOAP11_NAMESPACE))) {
  695. address = trav2;
  696. wsdl_soap_namespace = WSDL_SOAP11_NAMESPACE;
  697. tmpbinding->bindingType = BINDING_SOAP;
  698. } else if (!strncmp((char*)trav2->ns->href, WSDL_SOAP12_NAMESPACE, sizeof(WSDL_SOAP12_NAMESPACE))) {
  699. address = trav2;
  700. wsdl_soap_namespace = WSDL_SOAP12_NAMESPACE;
  701. tmpbinding->bindingType = BINDING_SOAP;
  702. } else if (!strncmp((char*)trav2->ns->href, RPC_SOAP12_NAMESPACE, sizeof(RPC_SOAP12_NAMESPACE))) {
  703. address = trav2;
  704. wsdl_soap_namespace = RPC_SOAP12_NAMESPACE;
  705. tmpbinding->bindingType = BINDING_SOAP;
  706. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP11_NAMESPACE, sizeof(WSDL_HTTP11_NAMESPACE))) {
  707. address = trav2;
  708. tmpbinding->bindingType = BINDING_HTTP;
  709. } else if (!strncmp((char*)trav2->ns->href, WSDL_HTTP12_NAMESPACE, sizeof(WSDL_HTTP12_NAMESPACE))) {
  710. address = trav2;
  711. tmpbinding->bindingType = BINDING_HTTP;
  712. }
  713. }
  714. if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  715. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  716. }
  717. trav2 = trav2->next;
  718. }
  719. if (!address || tmpbinding->bindingType == BINDING_HTTP) {
  720. if (has_soap_port || trav->next || i < n-1) {
  721. efree(tmpbinding);
  722. trav = trav->next;
  723. continue;
  724. } else if (!address) {
  725. soap_error0(E_ERROR, "Parsing WSDL: No address associated with <port>");
  726. }
  727. }
  728. has_soap_port = 1;
  729. location = get_attribute(address->properties, "location");
  730. if (!location) {
  731. soap_error0(E_ERROR, "Parsing WSDL: No location associated with <port>");
  732. }
  733. tmpbinding->location = estrdup((char*)location->children->content);
  734. ctype = strrchr((char*)bindingAttr->children->content,':');
  735. if (ctype == NULL) {
  736. ctype = (char*)bindingAttr->children->content;
  737. } else {
  738. ++ctype;
  739. }
  740. if ((tmp = zend_hash_str_find_ptr(&ctx.bindings, ctype, strlen(ctype))) == NULL) {
  741. soap_error1(E_ERROR, "Parsing WSDL: No <binding> element with name '%s'", ctype);
  742. }
  743. binding = tmp;
  744. if (tmpbinding->bindingType == BINDING_SOAP) {
  745. sdlSoapBindingPtr soapBinding;
  746. xmlNodePtr soapBindingNode;
  747. xmlAttrPtr tmp;
  748. soapBinding = emalloc(sizeof(sdlSoapBinding));
  749. memset(soapBinding, 0, sizeof(sdlSoapBinding));
  750. soapBinding->style = SOAP_DOCUMENT;
  751. soapBindingNode = get_node_ex(binding->children, "binding", wsdl_soap_namespace);
  752. if (soapBindingNode) {
  753. tmp = get_attribute(soapBindingNode->properties, "style");
  754. if (tmp && !strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  755. soapBinding->style = SOAP_RPC;
  756. }
  757. tmp = get_attribute(soapBindingNode->properties, "transport");
  758. if (tmp) {
  759. if (strncmp((char*)tmp->children->content, WSDL_HTTP_TRANSPORT, sizeof(WSDL_HTTP_TRANSPORT)) == 0) {
  760. soapBinding->transport = SOAP_TRANSPORT_HTTP;
  761. } else {
  762. /* try the next binding */
  763. efree(soapBinding);
  764. efree(tmpbinding->location);
  765. efree(tmpbinding);
  766. trav = trav->next;
  767. continue;
  768. }
  769. }
  770. }
  771. tmpbinding->bindingAttributes = (void *)soapBinding;
  772. }
  773. name = get_attribute(binding->properties, "name");
  774. if (name == NULL) {
  775. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <binding>");
  776. }
  777. tmpbinding->name = estrdup((char*)name->children->content);
  778. type = get_attribute(binding->properties, "type");
  779. if (type == NULL) {
  780. soap_error0(E_ERROR, "Parsing WSDL: Missing 'type' attribute for <binding>");
  781. }
  782. ctype = strrchr((char*)type->children->content,':');
  783. if (ctype == NULL) {
  784. ctype = (char*)type->children->content;
  785. } else {
  786. ++ctype;
  787. }
  788. if ((tmp = zend_hash_str_find_ptr(&ctx.portTypes, ctype, strlen(ctype))) == NULL) {
  789. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType> with name '%s'", name->children->content);
  790. }
  791. portType = tmp;
  792. trav2 = binding->children;
  793. while (trav2 != NULL) {
  794. sdlFunctionPtr function;
  795. xmlNodePtr input, output, fault, portTypeOperation, trav3;
  796. xmlAttrPtr op_name, paramOrder;
  797. if ((tmpbinding->bindingType == BINDING_SOAP &&
  798. node_is_equal_ex(trav2, "binding", wsdl_soap_namespace)) ||
  799. !is_wsdl_element(trav2) ||
  800. node_is_equal(trav2,"documentation")) {
  801. trav2 = trav2->next;
  802. continue;
  803. }
  804. if (!node_is_equal(trav2,"operation")) {
  805. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  806. }
  807. operation = trav2;
  808. op_name = get_attribute(operation->properties, "name");
  809. if (op_name == NULL) {
  810. soap_error0(E_ERROR, "Parsing WSDL: Missing 'name' attribute for <operation>");
  811. }
  812. trav3 = operation->children;
  813. while (trav3 != NULL) {
  814. if (tmpbinding->bindingType == BINDING_SOAP &&
  815. node_is_equal_ex(trav3, "operation", wsdl_soap_namespace)) {
  816. } else if (is_wsdl_element(trav3) &&
  817. !node_is_equal(trav3,"input") &&
  818. !node_is_equal(trav3,"output") &&
  819. !node_is_equal(trav3,"fault") &&
  820. !node_is_equal(trav3,"documentation")) {
  821. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav3->name));
  822. }
  823. trav3 = trav3->next;
  824. }
  825. portTypeOperation = get_node_with_attribute_ex(portType->children, "operation", WSDL_NAMESPACE, "name", (char*)op_name->children->content, NULL);
  826. if (portTypeOperation == NULL) {
  827. soap_error1(E_ERROR, "Parsing WSDL: Missing <portType>/<operation> with name '%s'", op_name->children->content);
  828. }
  829. function = emalloc(sizeof(sdlFunction));
  830. memset(function, 0, sizeof(sdlFunction));
  831. function->functionName = estrdup((char*)op_name->children->content);
  832. if (tmpbinding->bindingType == BINDING_SOAP) {
  833. sdlSoapBindingFunctionPtr soapFunctionBinding;
  834. sdlSoapBindingPtr soapBinding;
  835. xmlNodePtr soapOperation;
  836. xmlAttrPtr tmp;
  837. soapFunctionBinding = emalloc(sizeof(sdlSoapBindingFunction));
  838. memset(soapFunctionBinding, 0, sizeof(sdlSoapBindingFunction));
  839. soapBinding = (sdlSoapBindingPtr)tmpbinding->bindingAttributes;
  840. soapFunctionBinding->style = soapBinding->style;
  841. soapOperation = get_node_ex(operation->children, "operation", wsdl_soap_namespace);
  842. if (soapOperation) {
  843. tmp = get_attribute(soapOperation->properties, "soapAction");
  844. if (tmp) {
  845. soapFunctionBinding->soapAction = estrdup((char*)tmp->children->content);
  846. }
  847. tmp = get_attribute(soapOperation->properties, "style");
  848. if (tmp) {
  849. if (!strncmp((char*)tmp->children->content, "rpc", sizeof("rpc"))) {
  850. soapFunctionBinding->style = SOAP_RPC;
  851. } else {
  852. soapFunctionBinding->style = SOAP_DOCUMENT;
  853. }
  854. } else {
  855. soapFunctionBinding->style = soapBinding->style;
  856. }
  857. }
  858. function->bindingAttributes = (void *)soapFunctionBinding;
  859. }
  860. input = get_node_ex(portTypeOperation->children, "input", WSDL_NAMESPACE);
  861. if (input != NULL) {
  862. xmlAttrPtr message;
  863. message = get_attribute(input->properties, "message");
  864. if (message == NULL) {
  865. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <input> of '%s'", op_name->children->content);
  866. }
  867. function->requestParameters = wsdl_message(&ctx, message->children->content);
  868. /* FIXME
  869. xmlAttrPtr name = get_attribute(input->properties, "name");
  870. if (name != NULL) {
  871. function->requestName = estrdup(name->children->content);
  872. } else {
  873. */
  874. {
  875. function->requestName = estrdup(function->functionName);
  876. }
  877. if (tmpbinding->bindingType == BINDING_SOAP) {
  878. input = get_node_ex(operation->children, "input", WSDL_NAMESPACE);
  879. if (input != NULL) {
  880. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  881. wsdl_soap_binding_body(&ctx, input, wsdl_soap_namespace, &soapFunctionBinding->input, function->requestParameters);
  882. }
  883. }
  884. }
  885. output = get_node_ex(portTypeOperation->children, "output", WSDL_NAMESPACE);
  886. if (output != NULL) {
  887. xmlAttrPtr message;
  888. message = get_attribute(output->properties, "message");
  889. if (message == NULL) {
  890. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  891. }
  892. function->responseParameters = wsdl_message(&ctx, message->children->content);
  893. /* FIXME
  894. xmlAttrPtr name = get_attribute(output->properties, "name");
  895. if (name != NULL) {
  896. function->responseName = estrdup(name->children->content);
  897. } else if (input == NULL) {
  898. function->responseName = estrdup(function->functionName);
  899. } else {
  900. */
  901. {
  902. int len = strlen(function->functionName);
  903. function->responseName = emalloc(len + sizeof("Response"));
  904. memcpy(function->responseName, function->functionName, len);
  905. memcpy(function->responseName+len, "Response", sizeof("Response"));
  906. }
  907. if (tmpbinding->bindingType == BINDING_SOAP) {
  908. output = get_node_ex(operation->children, "output", WSDL_NAMESPACE);
  909. if (output != NULL) {
  910. sdlSoapBindingFunctionPtr soapFunctionBinding = function->bindingAttributes;
  911. wsdl_soap_binding_body(&ctx, output, wsdl_soap_namespace, &soapFunctionBinding->output, function->responseParameters);
  912. }
  913. }
  914. }
  915. paramOrder = get_attribute(portTypeOperation->properties, "parameterOrder");
  916. if (paramOrder) {
  917. /* FIXME: */
  918. }
  919. fault = portTypeOperation->children;
  920. while (fault != NULL) {
  921. if (node_is_equal_ex(fault, "fault", WSDL_NAMESPACE)) {
  922. xmlAttrPtr message, name;
  923. sdlFaultPtr f;
  924. name = get_attribute(fault->properties, "name");
  925. if (name == NULL) {
  926. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <fault> of '%s'", op_name->children->content);
  927. }
  928. message = get_attribute(fault->properties, "message");
  929. if (message == NULL) {
  930. soap_error1(E_ERROR, "Parsing WSDL: Missing name for <output> of '%s'", op_name->children->content);
  931. }
  932. f = emalloc(sizeof(sdlFault));
  933. memset(f, 0, sizeof(sdlFault));
  934. f->name = estrdup((char*)name->children->content);
  935. f->details = wsdl_message(&ctx, message->children->content);
  936. if (f->details == NULL || zend_hash_num_elements(f->details) > 1) {
  937. soap_error1(E_ERROR, "Parsing WSDL: The fault message '%s' must have a single part", message->children->content);
  938. }
  939. if (tmpbinding->bindingType == BINDING_SOAP) {
  940. xmlNodePtr soap_fault = get_node_with_attribute_ex(operation->children, "fault", WSDL_NAMESPACE, "name", f->name, NULL);
  941. if (soap_fault != NULL) {
  942. xmlNodePtr trav = soap_fault->children;
  943. while (trav != NULL) {
  944. if (node_is_equal_ex(trav, "fault", wsdl_soap_namespace)) {
  945. xmlAttrPtr tmp;
  946. sdlSoapBindingFunctionFaultPtr binding;
  947. binding = f->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  948. memset(f->bindingAttributes, 0, sizeof(sdlSoapBindingFunctionFault));
  949. tmp = get_attribute(trav->properties, "use");
  950. if (tmp && !strncmp((char*)tmp->children->content, "encoded", sizeof("encoded"))) {
  951. binding->use = SOAP_ENCODED;
  952. } else {
  953. binding->use = SOAP_LITERAL;
  954. }
  955. tmp = get_attribute(trav->properties, "namespace");
  956. if (tmp) {
  957. binding->ns = estrdup((char*)tmp->children->content);
  958. }
  959. if (binding->use == SOAP_ENCODED) {
  960. tmp = get_attribute(trav->properties, "encodingStyle");
  961. if (tmp) {
  962. if (strncmp((char*)tmp->children->content, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)) == 0) {
  963. binding->encodingStyle = SOAP_ENCODING_1_1;
  964. } else if (strncmp((char*)tmp->children->content, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)) == 0) {
  965. binding->encodingStyle = SOAP_ENCODING_1_2;
  966. } else {
  967. soap_error1(E_ERROR, "Parsing WSDL: Unknown encodingStyle '%s'", tmp->children->content);
  968. }
  969. } else {
  970. soap_error0(E_ERROR, "Parsing WSDL: Unspecified encodingStyle");
  971. }
  972. }
  973. } else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  974. soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav->name));
  975. }
  976. trav = trav->next;
  977. }
  978. }
  979. }
  980. if (function->faults == NULL) {
  981. function->faults = emalloc(sizeof(HashTable));
  982. zend_hash_init(function->faults, 0, NULL, delete_fault, 0);
  983. }
  984. if (zend_hash_str_add_ptr(function->faults, f->name, strlen(f->name), f) == NULL) {
  985. soap_error2(E_ERROR, "Parsing WSDL: <fault> with name '%s' already defined in '%s'", f->name, op_name->children->content);
  986. }
  987. }
  988. fault = fault->next;
  989. }
  990. function->binding = tmpbinding;
  991. {
  992. char *tmp = estrdup(function->functionName);
  993. int len = strlen(tmp);
  994. if (zend_hash_str_add_ptr(&ctx.sdl->functions, php_strtolower(tmp, len), len, function) == NULL) {
  995. zend_hash_next_index_insert_ptr(&ctx.sdl->functions, function);
  996. }
  997. efree(tmp);
  998. if (function->requestName != NULL && strcmp(function->requestName,function->functionName) != 0) {
  999. if (ctx.sdl->requests == NULL) {
  1000. ctx.sdl->requests = emalloc(sizeof(HashTable));
  1001. zend_hash_init(ctx.sdl->requests, 0, NULL, NULL, 0);
  1002. }
  1003. tmp = estrdup(function->requestName);
  1004. len = strlen(tmp);
  1005. zend_hash_str_add_ptr(ctx.sdl->requests, php_strtolower(tmp, len), len, function);
  1006. efree(tmp);
  1007. }
  1008. }
  1009. trav2 = trav2->next;
  1010. }
  1011. if (!ctx.sdl->bindings) {
  1012. ctx.sdl->bindings = emalloc(sizeof(HashTable));
  1013. zend_hash_init(ctx.sdl->bindings, 0, NULL, delete_binding, 0);
  1014. }
  1015. if (!zend_hash_str_add_ptr(ctx.sdl->bindings, tmpbinding->name, strlen(tmpbinding->name), tmpbinding)) {
  1016. zend_hash_next_index_insert_ptr(ctx.sdl->bindings, tmpbinding);
  1017. }
  1018. trav= trav->next;
  1019. }
  1020. zend_hash_move_forward(&ctx.services);
  1021. }
  1022. } else {
  1023. soap_error0(E_ERROR, "Parsing WSDL: Couldn't bind to service");
  1024. }
  1025. if (ctx.sdl->bindings == NULL || ctx.sdl->bindings->nNumOfElements == 0) {
  1026. soap_error0(E_ERROR, "Parsing WSDL: Could not find any usable binding services in WSDL.");
  1027. }
  1028. zend_hash_destroy(&ctx.messages);
  1029. zend_hash_destroy(&ctx.bindings);
  1030. zend_hash_destroy(&ctx.portTypes);
  1031. zend_hash_destroy(&ctx.services);
  1032. zend_hash_destroy(&ctx.docs);
  1033. return ctx.sdl;
  1034. }
  1035. #define WSDL_CACHE_VERSION 0x10
  1036. #define WSDL_CACHE_GET(ret,type,buf) memcpy(&ret,*buf,sizeof(type)); *buf += sizeof(type);
  1037. #define WSDL_CACHE_GET_INT(ret,buf) ret = ((unsigned char)(*buf)[0])|((unsigned char)(*buf)[1]<<8)|((unsigned char)(*buf)[2]<<16)|((int)(*buf)[3]<<24); *buf += 4;
  1038. #define WSDL_CACHE_GET_1(ret,type,buf) ret = (type)(**buf); (*buf)++;
  1039. #define WSDL_CACHE_GET_N(ret,n,buf) memcpy(ret,*buf,n); *buf += n;
  1040. #define WSDL_CACHE_SKIP(n,buf) *buf += n;
  1041. #define WSDL_CACHE_PUT_INT(val,buf) smart_str_appendc(buf,val & 0xff); \
  1042. smart_str_appendc(buf,(val >> 8) & 0xff); \
  1043. smart_str_appendc(buf,(val >> 16) & 0xff); \
  1044. smart_str_appendc(buf,(val >> 24) & 0xff);
  1045. #define WSDL_CACHE_PUT_1(val,buf) smart_str_appendc(buf,val);
  1046. #define WSDL_CACHE_PUT_N(val,n,buf) smart_str_appendl(buf,(char*)val,n);
  1047. #define WSDL_NO_STRING_MARKER 0x7fffffff
  1048. static char* sdl_deserialize_string(char **in)
  1049. {
  1050. char *s;
  1051. int len;
  1052. WSDL_CACHE_GET_INT(len, in);
  1053. if (len == WSDL_NO_STRING_MARKER) {
  1054. return NULL;
  1055. } else {
  1056. s = emalloc(len+1);
  1057. WSDL_CACHE_GET_N(s, len, in);
  1058. s[len] = '\0';
  1059. return s;
  1060. }
  1061. }
  1062. static void sdl_deserialize_key(HashTable* ht, void* data, char **in)
  1063. {
  1064. int len;
  1065. WSDL_CACHE_GET_INT(len, in);
  1066. if (len == WSDL_NO_STRING_MARKER) {
  1067. zend_hash_next_index_insert_ptr(ht, data);
  1068. } else {
  1069. zend_hash_str_add_ptr(ht, *in, len, data);
  1070. WSDL_CACHE_SKIP(len, in);
  1071. }
  1072. }
  1073. static void sdl_deserialize_attribute(sdlAttributePtr attr, encodePtr *encoders, char **in)
  1074. {
  1075. int i;
  1076. attr->name = sdl_deserialize_string(in);
  1077. attr->namens = sdl_deserialize_string(in);
  1078. attr->ref = sdl_deserialize_string(in);
  1079. attr->def = sdl_deserialize_string(in);
  1080. attr->fixed = sdl_deserialize_string(in);
  1081. WSDL_CACHE_GET_1(attr->form, sdlForm, in);
  1082. WSDL_CACHE_GET_1(attr->use, sdlUse, in);
  1083. WSDL_CACHE_GET_INT(i, in);
  1084. attr->encode = encoders[i];
  1085. WSDL_CACHE_GET_INT(i, in);
  1086. if (i > 0) {
  1087. attr->extraAttributes = emalloc(sizeof(HashTable));
  1088. zend_hash_init(attr->extraAttributes, i, NULL, delete_extra_attribute, 0);
  1089. while (i > 0) {
  1090. sdlExtraAttributePtr x = emalloc(sizeof(sdlExtraAttribute));
  1091. sdl_deserialize_key(attr->extraAttributes, x, in);
  1092. x->ns = sdl_deserialize_string(in);
  1093. x->val = sdl_deserialize_string(in);
  1094. --i;
  1095. }
  1096. }
  1097. }
  1098. static sdlRestrictionIntPtr sdl_deserialize_resriction_int(char **in)
  1099. {
  1100. if (**in == 1) {
  1101. sdlRestrictionIntPtr x = emalloc(sizeof(sdlRestrictionInt));
  1102. WSDL_CACHE_SKIP(1, in);
  1103. WSDL_CACHE_GET_INT(x->value, in);
  1104. WSDL_CACHE_GET_1(x->fixed, char, in);
  1105. return x;
  1106. } else {
  1107. WSDL_CACHE_SKIP(1, in);
  1108. return NULL;
  1109. }
  1110. }
  1111. static sdlRestrictionCharPtr sdl_deserialize_resriction_char(char **in)
  1112. {
  1113. if (**in == 1) {
  1114. sdlRestrictionCharPtr x = emalloc(sizeof(sdlRestrictionChar));
  1115. WSDL_CACHE_SKIP(1, in);
  1116. x->value = sdl_deserialize_string(in);
  1117. WSDL_CACHE_GET_1(x->fixed, char, in);
  1118. return x;
  1119. } else {
  1120. WSDL_CACHE_SKIP(1, in);
  1121. return NULL;
  1122. }
  1123. }
  1124. static sdlContentModelPtr sdl_deserialize_model(sdlTypePtr *types, sdlTypePtr *elements, char **in)
  1125. {
  1126. int i;
  1127. sdlContentModelPtr model = emalloc(sizeof(sdlContentModel));
  1128. WSDL_CACHE_GET_1(model->kind, sdlContentKind, in);
  1129. WSDL_CACHE_GET_INT(model->min_occurs, in);
  1130. WSDL_CACHE_GET_INT(model->max_occurs, in);
  1131. switch (model->kind) {
  1132. case XSD_CONTENT_ELEMENT:
  1133. WSDL_CACHE_GET_INT(i, in);
  1134. model->u.element = elements[i];
  1135. break;
  1136. case XSD_CONTENT_SEQUENCE:
  1137. case XSD_CONTENT_ALL:
  1138. case XSD_CONTENT_CHOICE:
  1139. WSDL_CACHE_GET_INT(i, in);
  1140. model->u.content = emalloc(sizeof(HashTable));
  1141. zend_hash_init(model->u.content, i, NULL, delete_model, 0);
  1142. while (i > 0) {
  1143. sdlContentModelPtr x = sdl_deserialize_model(types, elements, in);
  1144. zend_hash_next_index_insert_ptr(model->u.content, x);
  1145. i--;
  1146. }
  1147. break;
  1148. case XSD_CONTENT_GROUP_REF:
  1149. model->u.group_ref = sdl_deserialize_string(in);
  1150. break;
  1151. case XSD_CONTENT_GROUP:
  1152. WSDL_CACHE_GET_INT(i, in);
  1153. model->u.group = types[i];
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. return model;
  1159. }
  1160. static void sdl_deserialize_type(sdlTypePtr type, sdlTypePtr *types, encodePtr *encoders, char **in)
  1161. {
  1162. int i;
  1163. sdlTypePtr *elements = NULL;
  1164. WSDL_CACHE_GET_1(type->kind, sdlTypeKind, in);
  1165. type->name = sdl_deserialize_string(in);
  1166. type->namens = sdl_deserialize_string(in);
  1167. type->def = sdl_deserialize_string(in);
  1168. type->fixed = sdl_deserialize_string(in);
  1169. type->ref = sdl_deserialize_string(in);
  1170. WSDL_CACHE_GET_1(type->nillable, char, in);
  1171. WSDL_CACHE_GET_1(type->form, sdlForm, in);
  1172. WSDL_CACHE_GET_INT(i, in);
  1173. type->encode = encoders[i];
  1174. if (**in == 1) {
  1175. WSDL_CACHE_SKIP(1, in);
  1176. type->restrictions = emalloc(sizeof(sdlRestrictions));
  1177. /*memset(type->restrictions, 0, sizeof(sdlRestrictions));*/
  1178. type->restrictions->minExclusive = sdl_deserialize_resriction_int(in);
  1179. type->restrictions->minInclusive = sdl_deserialize_resriction_int(in);
  1180. type->restrictions->maxExclusive = sdl_deserialize_resriction_int(in);
  1181. type->restrictions->maxInclusive = sdl_deserialize_resriction_int(in);
  1182. type->restrictions->totalDigits = sdl_deserialize_resriction_int(in);
  1183. type->restrictions->fractionDigits = sdl_deserialize_resriction_int(in);
  1184. type->restrictions->length = sdl_deserialize_resriction_int(in);
  1185. type->restrictions->minLength = sdl_deserialize_resriction_int(in);
  1186. type->restrictions->maxLength = sdl_deserialize_resriction_int(in);
  1187. type->restrictions->whiteSpace = sdl_deserialize_resriction_char(in);
  1188. type->restrictions->pattern = sdl_deserialize_resriction_char(in);
  1189. WSDL_CACHE_GET_INT(i, in);
  1190. if (i > 0) {
  1191. type->restrictions->enumeration = emalloc(sizeof(HashTable));
  1192. zend_hash_init(type->restrictions->enumeration, i, NULL, delete_restriction_var_char, 0);
  1193. while (i > 0) {
  1194. sdlRestrictionCharPtr x = sdl_deserialize_resriction_char(in);
  1195. sdl_deserialize_key(type->restrictions->enumeration, x, in);
  1196. --i;
  1197. }
  1198. } else {
  1199. type->restrictions->enumeration = NULL;
  1200. }
  1201. } else {
  1202. WSDL_CACHE_SKIP(1, in);
  1203. }
  1204. WSDL_CACHE_GET_INT(i, in);
  1205. if (i > 0) {
  1206. elements = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1207. elements[0] = NULL;
  1208. type->elements = emalloc(sizeof(HashTable));
  1209. zend_hash_init(type->elements, i, NULL, delete_type, 0);
  1210. while (i > 0) {
  1211. sdlTypePtr t = emalloc(sizeof(sdlType));
  1212. memset(t, 0, sizeof(sdlType));
  1213. sdl_deserialize_key(type->elements, t, in);
  1214. sdl_deserialize_type(t, types, encoders, in);
  1215. elements[i] = t;
  1216. --i;
  1217. }
  1218. }
  1219. WSDL_CACHE_GET_INT(i, in);
  1220. if (i > 0) {
  1221. type->attributes = emalloc(sizeof(HashTable));
  1222. zend_hash_init(type->attributes, i, NULL, delete_attribute, 0);
  1223. while (i > 0) {
  1224. sdlAttributePtr attr = emalloc(sizeof(sdlAttribute));
  1225. memset(attr, 0, sizeof(sdlAttribute));
  1226. sdl_deserialize_key(type->attributes, attr, in);
  1227. sdl_deserialize_attribute(attr, encoders, in);
  1228. --i;
  1229. }
  1230. }
  1231. if (**in != 0) {
  1232. WSDL_CACHE_SKIP(1, in);
  1233. type->model = sdl_deserialize_model(types, elements, in);
  1234. } else {
  1235. WSDL_CACHE_SKIP(1, in);
  1236. }
  1237. if (elements != NULL) {
  1238. efree(elements);
  1239. }
  1240. }
  1241. static void sdl_deserialize_encoder(encodePtr enc, sdlTypePtr *types, char **in)
  1242. {
  1243. int i;
  1244. WSDL_CACHE_GET_INT(enc->details.type, in);
  1245. enc->details.type_str = sdl_deserialize_string(in);
  1246. enc->details.ns = sdl_deserialize_string(in);
  1247. WSDL_CACHE_GET_INT(i, in);
  1248. enc->details.sdl_type = types[i];
  1249. enc->to_xml = sdl_guess_convert_xml;
  1250. enc->to_zval = sdl_guess_convert_zval;
  1251. if (enc->details.sdl_type == NULL) {
  1252. int ns_len = strlen(enc->details.ns);
  1253. int type_len = strlen(enc->details.type_str);
  1254. if (((ns_len == sizeof(SOAP_1_1_ENC_NAMESPACE)-1 &&
  1255. memcmp(enc->details.ns, SOAP_1_1_ENC_NAMESPACE, sizeof(SOAP_1_1_ENC_NAMESPACE)-1) == 0) ||
  1256. (ns_len == sizeof(SOAP_1_2_ENC_NAMESPACE)-1 &&
  1257. memcmp(enc->details.ns, SOAP_1_2_ENC_NAMESPACE, sizeof(SOAP_1_2_ENC_NAMESPACE)-1) == 0))) {
  1258. char *enc_nscat;
  1259. int enc_ns_len;
  1260. int enc_len;
  1261. encodePtr real_enc;
  1262. enc_ns_len = sizeof(XSD_NAMESPACE)-1;
  1263. enc_len = enc_ns_len + type_len + 1;
  1264. enc_nscat = emalloc(enc_len + 1);
  1265. memcpy(enc_nscat, XSD_NAMESPACE, sizeof(XSD_NAMESPACE)-1);
  1266. enc_nscat[enc_ns_len] = ':';
  1267. memcpy(enc_nscat+enc_ns_len+1, enc->details.type_str, type_len);
  1268. enc_nscat[enc_len] = '\0';
  1269. real_enc = get_encoder_ex(NULL, enc_nscat, enc_len);
  1270. efree(enc_nscat);
  1271. if (real_enc) {
  1272. enc->to_zval = real_enc->to_zval;
  1273. enc->to_xml = real_enc->to_xml;
  1274. }
  1275. }
  1276. }
  1277. }
  1278. static void sdl_deserialize_soap_body(sdlSoapBindingFunctionBodyPtr body, encodePtr *encoders, sdlTypePtr *types, char **in)
  1279. {
  1280. int i, j, n;
  1281. WSDL_CACHE_GET_1(body->use, sdlEncodingUse, in);
  1282. if (body->use == SOAP_ENCODED) {
  1283. WSDL_CACHE_GET_1(body->encodingStyle, sdlRpcEncodingStyle, in);
  1284. } else {
  1285. body->encodingStyle = SOAP_ENCODING_DEFAULT;
  1286. }
  1287. body->ns = sdl_deserialize_string(in);
  1288. WSDL_CACHE_GET_INT(i, in);
  1289. if (i > 0) {
  1290. body->headers = emalloc(sizeof(HashTable));
  1291. zend_hash_init(body->headers, i, NULL, delete_header, 0);
  1292. while (i > 0) {
  1293. sdlSoapBindingFunctionHeaderPtr tmp = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1294. memset(tmp, 0, sizeof(sdlSoapBindingFunctionHeader));
  1295. sdl_deserialize_key(body->headers, tmp, in);
  1296. WSDL_CACHE_GET_1(tmp->use, sdlEncodingUse, in);
  1297. if (tmp->use == SOAP_ENCODED) {
  1298. WSDL_CACHE_GET_1(tmp->encodingStyle, sdlRpcEncodingStyle, in);
  1299. } else {
  1300. tmp->encodingStyle = SOAP_ENCODING_DEFAULT;
  1301. }
  1302. tmp->name = sdl_deserialize_string(in);
  1303. tmp->ns = sdl_deserialize_string(in);
  1304. WSDL_CACHE_GET_INT(n, in);
  1305. tmp->encode = encoders[n];
  1306. WSDL_CACHE_GET_INT(n, in);
  1307. tmp->element = types[n];
  1308. --i;
  1309. WSDL_CACHE_GET_INT(j, in);
  1310. if (j > 0) {
  1311. tmp->headerfaults = emalloc(sizeof(HashTable));
  1312. zend_hash_init(tmp->headerfaults, i, NULL, delete_header, 0);
  1313. while (j > 0) {
  1314. sdlSoapBindingFunctionHeaderPtr tmp2 = emalloc(sizeof(sdlSoapBindingFunctionHeader));
  1315. memset(tmp2, 0, sizeof(sdlSoapBindingFunctionHeader));
  1316. sdl_deserialize_key(tmp->headerfaults, tmp2, in);
  1317. WSDL_CACHE_GET_1(tmp2->use, sdlEncodingUse, in);
  1318. if (tmp2->use == SOAP_ENCODED) {
  1319. WSDL_CACHE_GET_1(tmp2->encodingStyle, sdlRpcEncodingStyle, in);
  1320. } else {
  1321. tmp2->encodingStyle = SOAP_ENCODING_DEFAULT;
  1322. }
  1323. tmp2->name = sdl_deserialize_string(in);
  1324. tmp2->ns = sdl_deserialize_string(in);
  1325. WSDL_CACHE_GET_INT(n, in);
  1326. tmp2->encode = encoders[n];
  1327. WSDL_CACHE_GET_INT(n, in);
  1328. tmp2->element = types[n];
  1329. --j;
  1330. }
  1331. }
  1332. }
  1333. }
  1334. }
  1335. static HashTable* sdl_deserialize_parameters(encodePtr *encoders, sdlTypePtr *types, char **in)
  1336. {
  1337. int i, n;
  1338. HashTable *ht;
  1339. WSDL_CACHE_GET_INT(i, in);
  1340. if (i == 0) {return NULL;}
  1341. ht = emalloc(sizeof(HashTable));
  1342. zend_hash_init(ht, i, NULL, delete_parameter, 0);
  1343. while (i > 0) {
  1344. sdlParamPtr param = emalloc(sizeof(sdlParam));
  1345. sdl_deserialize_key(ht, param, in);
  1346. param->paramName = sdl_deserialize_string(in);
  1347. WSDL_CACHE_GET_INT(param->order, in);
  1348. WSDL_CACHE_GET_INT(n, in);
  1349. param->encode = encoders[n];
  1350. WSDL_CACHE_GET_INT(n, in);
  1351. param->element = types[n];
  1352. --i;
  1353. }
  1354. return ht;
  1355. }
  1356. static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, time_t t, time_t *cached)
  1357. {
  1358. sdlPtr sdl;
  1359. time_t old_t;
  1360. int i, num_groups, num_types, num_elements, num_encoders, num_bindings, num_func;
  1361. sdlFunctionPtr *functions = NULL;
  1362. sdlBindingPtr *bindings;
  1363. sdlTypePtr *types;
  1364. encodePtr *encoders;
  1365. const encode *enc;
  1366. int f;
  1367. struct stat st;
  1368. char *in, *buf;
  1369. f = open(fn, O_RDONLY|O_BINARY);
  1370. if (f < 0) {
  1371. return NULL;
  1372. }
  1373. if (fstat(f, &st) != 0) {
  1374. close(f);
  1375. return NULL;
  1376. }
  1377. buf = in = emalloc(st.st_size);
  1378. if (read(f, in, st.st_size) != st.st_size) {
  1379. close(f);
  1380. efree(in);
  1381. return NULL;
  1382. }
  1383. close(f);
  1384. if (strncmp(in,"wsdl",4) != 0 || in[4] != WSDL_CACHE_VERSION || in[5] != '\0') {
  1385. unlink(fn);
  1386. efree(buf);
  1387. return NULL;
  1388. }
  1389. in += 6;
  1390. WSDL_CACHE_GET(old_t, time_t, &in);
  1391. if (old_t < t) {
  1392. unlink(fn);
  1393. efree(buf);
  1394. return NULL;
  1395. }
  1396. *cached = old_t;
  1397. WSDL_CACHE_GET_INT(i, &in);
  1398. if (i == 0 && strncmp(in, uri, i) != 0) {
  1399. unlink(fn);
  1400. efree(buf);
  1401. return NULL;
  1402. }
  1403. WSDL_CACHE_SKIP(i, &in);
  1404. sdl = emalloc(sizeof(*sdl));
  1405. memset(sdl, 0, sizeof(*sdl));
  1406. sdl->source = sdl_deserialize_string(&in);
  1407. sdl->target_ns = sdl_deserialize_string(&in);
  1408. WSDL_CACHE_GET_INT(num_groups, &in);
  1409. WSDL_CACHE_GET_INT(num_types, &in);
  1410. WSDL_CACHE_GET_INT(num_elements, &in);
  1411. WSDL_CACHE_GET_INT(num_encoders, &in);
  1412. i = num_groups+num_types+num_elements;
  1413. types = safe_emalloc((i+1), sizeof(sdlTypePtr), 0);
  1414. types[0] = NULL;
  1415. while (i > 0) {
  1416. types[i] = emalloc(sizeof(sdlType));
  1417. memset(types[i], 0, sizeof(sdlType));
  1418. i--;
  1419. }
  1420. i = num_encoders;
  1421. enc = defaultEncoding;
  1422. while (enc->details.type != END_KNOWN_TYPES) {
  1423. i++; enc++;
  1424. }
  1425. encoders = safe_emalloc((i+1), sizeof(encodePtr), 0);
  1426. i = num_encoders;
  1427. encoders[0] = NULL;
  1428. while (i > 0) {
  1429. encoders[i] = emalloc(sizeof(encode));
  1430. memset(encoders[i], 0, sizeof(encode));
  1431. i--;
  1432. }
  1433. i = num_encoders;
  1434. enc = defaultEncoding;
  1435. while (enc->details.type != END_KNOWN_TYPES) {
  1436. encoders[++i] = (encodePtr)enc++;
  1437. }
  1438. i = 1;
  1439. if (num_groups > 0) {
  1440. sdl->groups = emalloc(sizeof(HashTable));
  1441. zend_hash_init(sdl->groups, num_groups, NULL, delete_type, 0);
  1442. while (i < num_groups+1) {
  1443. sdl_deserialize_key(sdl->groups, types[i], &in);
  1444. sdl_deserialize_type(types[i], types, encoders, &in);
  1445. i++;
  1446. }
  1447. }
  1448. if (num_types > 0) {
  1449. sdl->types = emalloc(sizeof(HashTable));
  1450. zend_hash_init(sdl->types, num_types, NULL, delete_type, 0);
  1451. while (i < num_groups+num_types+1) {
  1452. sdl_deserialize_key(sdl->types, types[i], &in);
  1453. sdl_deserialize_type(types[i], types, encoders, &in);
  1454. i++;
  1455. }
  1456. }
  1457. if (num_elements > 0) {
  1458. sdl->elements = emalloc(sizeof(HashTable));
  1459. zend_hash_init(sdl->elements, num_elements, NULL, delete_type, 0);
  1460. while (i < num_groups+num_types+num_elements+1) {
  1461. sdl_deserialize_key(sdl->elements, types[i], &in);
  1462. sdl_deserialize_type(types[i], types, encoders, &in);
  1463. i++;
  1464. }
  1465. }
  1466. i = 1;
  1467. if (num_encoders > 0) {
  1468. sdl->encoders = emalloc(sizeof(HashTable));
  1469. zend_hash_init(sdl->encoders, num_encoders, NULL, delete_encoder, 0);
  1470. while (i < num_encoders+1) {
  1471. sdl_deserialize_key(sdl->encoders, encoders[i], &in);
  1472. sdl_deserialize_encoder(encoders[i], types, &in);
  1473. i++;
  1474. }
  1475. }
  1476. /* deserialize bindings */
  1477. WSDL_CACHE_GET_INT(num_bindings, &in);
  1478. bindings = safe_emalloc(num_bindings, sizeof(sdlBindingPtr), 0);
  1479. if (num_bindings > 0) {
  1480. sdl->bindings = emalloc(sizeof(HashTable));
  1481. zend_hash_init(sdl->bindings, num_bindings, NULL, delete_binding, 0);
  1482. for (i = 0; i < num_bindings; i++) {
  1483. sdlBindingPtr binding = emalloc(sizeof(sdlBinding));
  1484. memset(binding, 0, sizeof(sdlBinding));
  1485. sdl_deserialize_key(sdl->bindings, binding, &in);
  1486. binding->name = sdl_deserialize_string(&in);
  1487. binding->location = sdl_deserialize_string(&in);
  1488. WSDL_CACHE_GET_1(binding->bindingType,sdlBindingType,&in);
  1489. if (binding->bindingType == BINDING_SOAP && *in != 0) {
  1490. sdlSoapBindingPtr soap_binding = binding->bindingAttributes = emalloc(sizeof(sdlSoapBinding));
  1491. WSDL_CACHE_GET_1(soap_binding->style,sdlEncodingStyle,&in);
  1492. WSDL_CACHE_GET_1(soap_binding->transport,sdlTransport,&in);
  1493. } else {
  1494. WSDL_CACHE_SKIP(1,&in);
  1495. }
  1496. bindings[i] = binding;
  1497. }
  1498. }
  1499. /* deserialize functions */
  1500. WSDL_CACHE_GET_INT(num_func, &in);
  1501. zend_hash_init(&sdl->functions, num_func, NULL, delete_function, 0);
  1502. if (num_func > 0) {
  1503. functions = safe_emalloc(num_func, sizeof(sdlFunctionPtr), 0);
  1504. for (i = 0; i < num_func; i++) {
  1505. int binding_num, num_faults;
  1506. sdlFunctionPtr func = emalloc(sizeof(sdlFunction));
  1507. sdl_deserialize_key(&sdl->functions, func, &in);
  1508. func->functionName = sdl_deserialize_string(&in);
  1509. func->requestName = sdl_deserialize_string(&in);
  1510. func->responseName = sdl_deserialize_string(&in);
  1511. WSDL_CACHE_GET_INT(binding_num, &in);
  1512. if (binding_num == 0) {
  1513. func->binding = NULL;
  1514. } else {
  1515. func->binding = bindings[binding_num-1];
  1516. }
  1517. if (func->binding && func->binding->bindingType == BINDING_SOAP && *in != 0) {
  1518. sdlSoapBindingFunctionPtr binding = func->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunction));
  1519. memset(binding, 0, sizeof(sdlSoapBindingFunction));
  1520. WSDL_CACHE_GET_1(binding->style,sdlEncodingStyle,&in);
  1521. binding->soapAction = sdl_deserialize_string(&in);
  1522. sdl_deserialize_soap_body(&binding->input, encoders, types, &in);
  1523. sdl_deserialize_soap_body(&binding->output, encoders, types, &in);
  1524. } else {
  1525. WSDL_CACHE_SKIP(1, &in);
  1526. func->bindingAttributes = NULL;
  1527. }
  1528. func->requestParameters = sdl_deserialize_parameters(encoders, types, &in);
  1529. func->responseParameters = sdl_deserialize_parameters(encoders, types, &in);
  1530. WSDL_CACHE_GET_INT(num_faults, &in);
  1531. if (num_faults > 0) {
  1532. int j;
  1533. func->faults = emalloc(sizeof(HashTable));
  1534. zend_hash_init(func->faults, num_faults, NULL, delete_fault, 0);
  1535. for (j = 0; j < num_faults; j++) {
  1536. sdlFaultPtr fault = emalloc(sizeof(sdlFault));
  1537. sdl_deserialize_key(func->faults, fault, &in);
  1538. fault->name =sdl_deserialize_string(&in);
  1539. fault->details =sdl_deserialize_parameters(encoders, types, &in);
  1540. if (*in != 0) {
  1541. sdlSoapBindingFunctionFaultPtr binding = fault->bindingAttributes = emalloc(sizeof(sdlSoapBindingFunctionFault));
  1542. memset(binding, 0, sizeof(sdlSoapBindingFunctionFault));
  1543. WSDL_CACHE_GET_1(binding->use,sdlEncodingUse,&in);
  1544. if (binding->use == SOAP_ENCODED) {
  1545. WSDL_CACHE_GET_1(binding->encodingStyle, sdlRpcEncodingStyle, &in);
  1546. } else {
  1547. binding->encodingStyle = SOAP_ENCODING_DEFAULT;
  1548. }
  1549. binding->ns = sdl_deserialize_string(&in);
  1550. } else {
  1551. WSDL_CACHE_SKIP(1, &in);
  1552. fault->bindingAttributes = NULL;
  1553. }
  1554. }
  1555. } else {
  1556. func->faults = NULL;
  1557. }
  1558. functions[i] = func;
  1559. }
  1560. }
  1561. /* deserialize requests */
  1562. WSDL_CACHE_GET_INT(i, &in);
  1563. if (i > 0) {
  1564. sdl->requests = emalloc(sizeof(HashTable));
  1565. zend_hash_init(sdl->requests, i, NULL, NULL, 0);
  1566. while (i > 0) {
  1567. int function_num;
  1568. WSDL_CACHE_GET_INT(function_num, &in);
  1569. sdl_deserialize_key(sdl->requests, functions[function_num-1], &in);
  1570. i--;
  1571. }
  1572. }
  1573. if (functions) {
  1574. efree(functions);
  1575. }
  1576. efree(bindings);
  1577. efree(encoders);
  1578. efree(types);
  1579. efree(buf);
  1580. return sdl;
  1581. }
  1582. static void sdl_serialize_string(const char *str, smart_str *out)
  1583. {
  1584. if (str) {
  1585. int i = strlen(str);
  1586. WSDL_CACHE_PUT_INT(i, out);
  1587. if (i > 0) {
  1588. WSDL_CACHE_PUT_N(str, i, out);
  1589. }
  1590. } else {
  1591. WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
  1592. }
  1593. }
  1594. // TODO: refactor it
  1595. static void sdl_serialize_key(zend_string *key, smart_str *out)
  1596. {
  1597. if (key) {
  1598. WSDL_CACHE_PUT_INT(ZSTR_LEN(key), out);
  1599. WSDL_CACHE_PUT_N(ZSTR_VAL(key), ZSTR_LEN(key), out);
  1600. } else {
  1601. WSDL_CACHE_PUT_INT(WSDL_NO_STRING_MARKER, out);
  1602. }
  1603. }
  1604. static void sdl_serialize_encoder_ref(encodePtr enc, HashTable *tmp_encoders, smart_str *out) {
  1605. if (enc) {
  1606. zval *encoder_num;
  1607. if ((encoder_num = zend_hash_str_find(tmp_encoders, (char*)&enc, sizeof(enc))) != 0) {
  1608. WSDL_CACHE_PUT_INT(Z_LVAL_P(encoder_num), out);
  1609. } else {
  1610. WSDL_CACHE_PUT_INT(0, out);
  1611. }
  1612. } else {
  1613. WSDL_CACHE_PUT_INT(0, out);
  1614. }
  1615. }
  1616. static void sdl_serialize_type_ref(sdlTypePtr type, HashTable *tmp_types, smart_str *out) {
  1617. if (type) {
  1618. zval *type_num;
  1619. if ((type_num = zend_hash_str_find(tmp_types, (char*)&type, sizeof(type))) != NULL) {
  1620. WSDL_CACHE_PUT_INT(Z_LVAL_P(type_num), out);
  1621. } else {
  1622. WSDL_CACHE_PUT_INT(0, out);
  1623. }
  1624. } else {
  1625. WSDL_CACHE_PUT_INT(0,out);
  1626. }
  1627. }
  1628. static void sdl_serialize_attribute(sdlAttributePtr attr, HashTable *tmp_encoders, smart_str *out)
  1629. {
  1630. int i;
  1631. sdl_serialize_string(attr->name, out);
  1632. sdl_serialize_string(attr->namens, out);
  1633. sdl_serialize_string(attr->ref, out);
  1634. sdl_serialize_string(attr->def, out);
  1635. sdl_serialize_string(attr->fixed, out);
  1636. WSDL_CACHE_PUT_1(attr->form, out);
  1637. WSDL_CACHE_PUT_1(attr->use, out);
  1638. sdl_serialize_encoder_ref(attr->encode, tmp_encoders, out);
  1639. if (attr->extraAttributes) {
  1640. i = zend_hash_num_elements(attr->extraAttributes);
  1641. } else {
  1642. i = 0;
  1643. }
  1644. WSDL_CACHE_PUT_INT(i, out);
  1645. if (i > 0) {
  1646. sdlExtraAttributePtr tmp;
  1647. zend_string *key;
  1648. ZEND_HASH_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
  1649. sdl_serialize_key(key, out);
  1650. sdl_serialize_string(tmp->ns, out);
  1651. sdl_serialize_string(tmp->val, out);
  1652. } ZEND_HASH_FOREACH_END();
  1653. }
  1654. }
  1655. static void sdl_serialize_model(sdlContentModelPtr model, HashTable *tmp_types, HashTable *tmp_elements, smart_str *out)
  1656. {
  1657. WSDL_CACHE_PUT_1(model->kind, out);
  1658. WSDL_CACHE_PUT_INT(model->min_occurs, out);
  1659. WSDL_CACHE_PUT_INT(model->max_occurs, out);
  1660. switch (model->kind) {
  1661. case XSD_CONTENT_ELEMENT:
  1662. sdl_serialize_type_ref(model->u.element, tmp_elements, out);
  1663. break;
  1664. case XSD_CONTENT_SEQUENCE:
  1665. case XSD_CONTENT_ALL:
  1666. case XSD_CONTENT_CHOICE: {
  1667. sdlContentModelPtr tmp;
  1668. int i = zend_hash_num_elements(model->u.content);
  1669. WSDL_CACHE_PUT_INT(i, out);
  1670. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  1671. sdl_serialize_model(tmp, tmp_types, tmp_elements, out);
  1672. } ZEND_HASH_FOREACH_END();
  1673. }
  1674. break;
  1675. case XSD_CONTENT_GROUP_REF:
  1676. sdl_serialize_string(model->u.group_ref,out);
  1677. break;
  1678. case XSD_CONTENT_GROUP:
  1679. sdl_serialize_type_ref(model->u.group, tmp_types, out);
  1680. break;
  1681. default:
  1682. break;
  1683. }
  1684. }
  1685. static void sdl_serialize_resriction_int(sdlRestrictionIntPtr x, smart_str *out)
  1686. {
  1687. if (x) {
  1688. WSDL_CACHE_PUT_1(1, out);
  1689. WSDL_CACHE_PUT_INT(x->value, out);
  1690. WSDL_CACHE_PUT_1(x->fixed, out);
  1691. } else {
  1692. WSDL_CACHE_PUT_1(0, out);
  1693. }
  1694. }
  1695. static void sdl_serialize_resriction_char(sdlRestrictionCharPtr x, smart_str *out)
  1696. {
  1697. if (x) {
  1698. WSDL_CACHE_PUT_1(1, out);
  1699. sdl_serialize_string(x->value, out);
  1700. WSDL_CACHE_PUT_1(x->fixed, out);
  1701. } else {
  1702. WSDL_CACHE_PUT_1(0, out);
  1703. }
  1704. }
  1705. static void sdl_serialize_type(sdlTypePtr type, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1706. {
  1707. int i;
  1708. HashTable *tmp_elements = NULL;
  1709. WSDL_CACHE_PUT_1(type->kind, out);
  1710. sdl_serialize_string(type->name, out);
  1711. sdl_serialize_string(type->namens, out);
  1712. sdl_serialize_string(type->def, out);
  1713. sdl_serialize_string(type->fixed, out);
  1714. sdl_serialize_string(type->ref, out);
  1715. WSDL_CACHE_PUT_1(type->nillable, out);
  1716. WSDL_CACHE_PUT_1(type->form, out);
  1717. sdl_serialize_encoder_ref(type->encode, tmp_encoders, out);
  1718. if (type->restrictions) {
  1719. WSDL_CACHE_PUT_1(1, out);
  1720. sdl_serialize_resriction_int(type->restrictions->minExclusive,out);
  1721. sdl_serialize_resriction_int(type->restrictions->minInclusive,out);
  1722. sdl_serialize_resriction_int(type->restrictions->maxExclusive,out);
  1723. sdl_serialize_resriction_int(type->restrictions->maxInclusive,out);
  1724. sdl_serialize_resriction_int(type->restrictions->totalDigits,out);
  1725. sdl_serialize_resriction_int(type->restrictions->fractionDigits,out);
  1726. sdl_serialize_resriction_int(type->restrictions->length,out);
  1727. sdl_serialize_resriction_int(type->restrictions->minLength,out);
  1728. sdl_serialize_resriction_int(type->restrictions->maxLength,out);
  1729. sdl_serialize_resriction_char(type->restrictions->whiteSpace,out);
  1730. sdl_serialize_resriction_char(type->restrictions->pattern,out);
  1731. if (type->restrictions->enumeration) {
  1732. i = zend_hash_num_elements(type->restrictions->enumeration);
  1733. } else {
  1734. i = 0;
  1735. }
  1736. WSDL_CACHE_PUT_INT(i, out);
  1737. if (i > 0) {
  1738. sdlRestrictionCharPtr tmp;
  1739. zend_string *key;
  1740. ZEND_HASH_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
  1741. sdl_serialize_resriction_char(tmp, out);
  1742. sdl_serialize_key(key, out);
  1743. } ZEND_HASH_FOREACH_END();
  1744. }
  1745. } else {
  1746. WSDL_CACHE_PUT_1(0, out);
  1747. }
  1748. if (type->elements) {
  1749. i = zend_hash_num_elements(type->elements);
  1750. } else {
  1751. i = 0;
  1752. }
  1753. WSDL_CACHE_PUT_INT(i, out);
  1754. if (i > 0) {
  1755. sdlTypePtr tmp;
  1756. zend_string *key;
  1757. zval zv;
  1758. tmp_elements = emalloc(sizeof(HashTable));
  1759. zend_hash_init(tmp_elements, i, NULL, NULL, 0);
  1760. ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
  1761. sdl_serialize_key(key, out);
  1762. sdl_serialize_type(tmp, tmp_encoders, tmp_types, out);
  1763. ZVAL_LONG(&zv, i);
  1764. zend_hash_str_add(tmp_elements, (char*)&tmp, sizeof(tmp), &zv);
  1765. i--;
  1766. } ZEND_HASH_FOREACH_END();
  1767. }
  1768. if (type->attributes) {
  1769. i = zend_hash_num_elements(type->attributes);
  1770. } else {
  1771. i = 0;
  1772. }
  1773. WSDL_CACHE_PUT_INT(i, out);
  1774. if (i > 0) {
  1775. sdlAttributePtr tmp;
  1776. zend_string *key;
  1777. ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
  1778. sdl_serialize_key(key, out);
  1779. sdl_serialize_attribute(tmp, tmp_encoders, out);
  1780. } ZEND_HASH_FOREACH_END();
  1781. }
  1782. if (type->model) {
  1783. WSDL_CACHE_PUT_1(1, out);
  1784. sdl_serialize_model(type->model, tmp_types, tmp_elements, out);
  1785. } else {
  1786. WSDL_CACHE_PUT_1(0, out);
  1787. }
  1788. if (tmp_elements != NULL) {
  1789. zend_hash_destroy(tmp_elements);
  1790. efree(tmp_elements);
  1791. }
  1792. }
  1793. static void sdl_serialize_encoder(encodePtr enc, HashTable *tmp_types, smart_str *out)
  1794. {
  1795. WSDL_CACHE_PUT_INT(enc->details.type, out);
  1796. sdl_serialize_string(enc->details.type_str, out);
  1797. sdl_serialize_string(enc->details.ns, out);
  1798. sdl_serialize_type_ref(enc->details.sdl_type, tmp_types, out);
  1799. }
  1800. static void sdl_serialize_parameters(HashTable *ht, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1801. {
  1802. int i;
  1803. if (ht) {
  1804. i = zend_hash_num_elements(ht);
  1805. } else {
  1806. i = 0;
  1807. }
  1808. WSDL_CACHE_PUT_INT(i, out);
  1809. if (i > 0) {
  1810. sdlParamPtr tmp;
  1811. zend_string *key;
  1812. ZEND_HASH_FOREACH_STR_KEY_PTR(ht, key, tmp) {
  1813. sdl_serialize_key(key, out);
  1814. sdl_serialize_string(tmp->paramName, out);
  1815. WSDL_CACHE_PUT_INT(tmp->order, out);
  1816. sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
  1817. sdl_serialize_type_ref(tmp->element, tmp_types, out);
  1818. } ZEND_HASH_FOREACH_END();
  1819. }
  1820. }
  1821. static void sdl_serialize_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *tmp_encoders, HashTable *tmp_types, smart_str *out)
  1822. {
  1823. int i, j;
  1824. WSDL_CACHE_PUT_1(body->use, out);
  1825. if (body->use == SOAP_ENCODED) {
  1826. WSDL_CACHE_PUT_1(body->encodingStyle, out);
  1827. }
  1828. sdl_serialize_string(body->ns, out);
  1829. if (body->headers) {
  1830. i = zend_hash_num_elements(body->headers);
  1831. } else {
  1832. i = 0;
  1833. }
  1834. WSDL_CACHE_PUT_INT(i, out);
  1835. if (i > 0) {
  1836. sdlSoapBindingFunctionHeaderPtr tmp;
  1837. zend_string *key;
  1838. ZEND_HASH_FOREACH_STR_KEY_PTR(body->headers, key, tmp) {
  1839. sdl_serialize_key(key, out);
  1840. WSDL_CACHE_PUT_1(tmp->use, out);
  1841. if (tmp->use == SOAP_ENCODED) {
  1842. WSDL_CACHE_PUT_1(tmp->encodingStyle, out);
  1843. }
  1844. sdl_serialize_string(tmp->name, out);
  1845. sdl_serialize_string(tmp->ns, out);
  1846. sdl_serialize_encoder_ref(tmp->encode, tmp_encoders, out);
  1847. sdl_serialize_type_ref(tmp->element, tmp_types, out);
  1848. if (tmp->headerfaults) {
  1849. j = zend_hash_num_elements(tmp->headerfaults);
  1850. } else {
  1851. j = 0;
  1852. }
  1853. WSDL_CACHE_PUT_INT(j, out);
  1854. if (j > 0) {
  1855. sdlSoapBindingFunctionHeaderPtr tmp2;
  1856. zend_string *key;
  1857. ZEND_HASH_FOREACH_STR_KEY_PTR(body->headers, key, tmp2) {
  1858. sdl_serialize_key(key, out);
  1859. WSDL_CACHE_PUT_1(tmp2->use, out);
  1860. if (tmp2->use == SOAP_ENCODED) {
  1861. WSDL_CACHE_PUT_1(tmp2->encodingStyle, out);
  1862. }
  1863. sdl_serialize_string(tmp2->name, out);
  1864. sdl_serialize_string(tmp2->ns, out);
  1865. sdl_serialize_encoder_ref(tmp2->encode, tmp_encoders, out);
  1866. sdl_serialize_type_ref(tmp2->element, tmp_types, out);
  1867. } ZEND_HASH_FOREACH_END();
  1868. }
  1869. } ZEND_HASH_FOREACH_END();
  1870. }
  1871. }
  1872. static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr sdl)
  1873. {
  1874. smart_str buf = {0};
  1875. smart_str *out = &buf;
  1876. int i;
  1877. int type_num = 1;
  1878. int encoder_num = 1;
  1879. int f;
  1880. const encode *enc;
  1881. HashTable tmp_types;
  1882. HashTable tmp_encoders;
  1883. HashTable tmp_bindings;
  1884. HashTable tmp_functions;
  1885. #ifdef ZEND_WIN32
  1886. f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  1887. #else
  1888. f = open(fn,O_CREAT|O_WRONLY|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  1889. #endif
  1890. if (f < 0) {return;}
  1891. zend_hash_init(&tmp_types, 0, NULL, NULL, 0);
  1892. zend_hash_init(&tmp_encoders, 0, NULL, NULL, 0);
  1893. zend_hash_init(&tmp_bindings, 0, NULL, NULL, 0);
  1894. zend_hash_init(&tmp_functions, 0, NULL, NULL, 0);
  1895. WSDL_CACHE_PUT_N("wsdl", 4, out);
  1896. WSDL_CACHE_PUT_1(WSDL_CACHE_VERSION,out);
  1897. WSDL_CACHE_PUT_1(0,out);
  1898. WSDL_CACHE_PUT_N(&t, sizeof(t), out);
  1899. sdl_serialize_string(uri, out);
  1900. sdl_serialize_string(sdl->source, out);
  1901. sdl_serialize_string(sdl->target_ns, out);
  1902. if (sdl->groups) {
  1903. i = zend_hash_num_elements(sdl->groups);
  1904. } else {
  1905. i = 0;
  1906. }
  1907. WSDL_CACHE_PUT_INT(i, out);
  1908. if (i > 0) {
  1909. sdlTypePtr tmp;
  1910. zval zv;
  1911. ZEND_HASH_FOREACH_PTR(sdl->groups, tmp) {
  1912. ZVAL_LONG(&zv, type_num);
  1913. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1914. ++type_num;
  1915. } ZEND_HASH_FOREACH_END();
  1916. }
  1917. if (sdl->types) {
  1918. i = zend_hash_num_elements(sdl->types);
  1919. } else {
  1920. i = 0;
  1921. }
  1922. WSDL_CACHE_PUT_INT(i, out);
  1923. if (i > 0) {
  1924. sdlTypePtr tmp;
  1925. zval zv;
  1926. ZEND_HASH_FOREACH_PTR(sdl->types, tmp) {
  1927. ZVAL_LONG(&zv, type_num);
  1928. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1929. ++type_num;
  1930. } ZEND_HASH_FOREACH_END();
  1931. }
  1932. if (sdl->elements) {
  1933. i = zend_hash_num_elements(sdl->elements);
  1934. } else {
  1935. i = 0;
  1936. }
  1937. WSDL_CACHE_PUT_INT(i, out);
  1938. if (i > 0) {
  1939. sdlTypePtr tmp;
  1940. zval zv;
  1941. ZEND_HASH_FOREACH_PTR(sdl->elements, tmp) {
  1942. ZVAL_LONG(&zv, type_num);
  1943. zend_hash_str_add(&tmp_types, (char*)&tmp, sizeof(tmp), &zv);
  1944. ++type_num;
  1945. } ZEND_HASH_FOREACH_END();
  1946. }
  1947. if (sdl->encoders) {
  1948. i = zend_hash_num_elements(sdl->encoders);
  1949. } else {
  1950. i = 0;
  1951. }
  1952. WSDL_CACHE_PUT_INT(i, out);
  1953. if (i > 0) {
  1954. encodePtr tmp;
  1955. zval zv;
  1956. ZEND_HASH_FOREACH_PTR(sdl->encoders, tmp) {
  1957. ZVAL_LONG(&zv, encoder_num);
  1958. zend_hash_str_add(&tmp_encoders, (char*)&tmp, sizeof(tmp), &zv);
  1959. ++encoder_num;
  1960. } ZEND_HASH_FOREACH_END();
  1961. }
  1962. enc = defaultEncoding;
  1963. while (enc->details.type != END_KNOWN_TYPES) {
  1964. zval zv;
  1965. ZVAL_LONG(&zv, encoder_num);
  1966. zend_hash_str_add(&tmp_encoders, (char*)&enc, sizeof(encodePtr), &zv);
  1967. enc++;
  1968. ++encoder_num;
  1969. }
  1970. if (sdl->groups) {
  1971. sdlTypePtr tmp;
  1972. zend_string *key;
  1973. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
  1974. sdl_serialize_key(key, out);
  1975. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1976. } ZEND_HASH_FOREACH_END();
  1977. }
  1978. if (sdl->types) {
  1979. sdlTypePtr tmp;
  1980. zend_string *key;
  1981. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
  1982. sdl_serialize_key(key, out);
  1983. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1984. } ZEND_HASH_FOREACH_END();
  1985. }
  1986. if (sdl->elements) {
  1987. sdlTypePtr tmp;
  1988. zend_string *key;
  1989. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
  1990. sdl_serialize_key(key, out);
  1991. sdl_serialize_type(tmp, &tmp_encoders, &tmp_types, out);
  1992. } ZEND_HASH_FOREACH_END();
  1993. }
  1994. if (sdl->encoders) {
  1995. encodePtr tmp;
  1996. zend_string *key;
  1997. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
  1998. sdl_serialize_key(key, out);
  1999. sdl_serialize_encoder(tmp, &tmp_types, out);
  2000. } ZEND_HASH_FOREACH_END();
  2001. }
  2002. /* serialize bindings */
  2003. if (sdl->bindings) {
  2004. i = zend_hash_num_elements(sdl->bindings);
  2005. } else {
  2006. i = 0;
  2007. }
  2008. WSDL_CACHE_PUT_INT(i, out);
  2009. if (i > 0) {
  2010. sdlBindingPtr tmp;
  2011. int binding_num = 1;
  2012. zval zv;
  2013. zend_string *key;
  2014. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
  2015. sdl_serialize_key(key, out);
  2016. sdl_serialize_string(tmp->name, out);
  2017. sdl_serialize_string(tmp->location, out);
  2018. WSDL_CACHE_PUT_1(tmp->bindingType,out);
  2019. if (tmp->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
  2020. sdlSoapBindingPtr binding = (sdlSoapBindingPtr)tmp->bindingAttributes;
  2021. WSDL_CACHE_PUT_1(binding->style, out);
  2022. WSDL_CACHE_PUT_1(binding->transport, out);
  2023. } else {
  2024. WSDL_CACHE_PUT_1(0,out);
  2025. }
  2026. ZVAL_LONG(&zv, binding_num);
  2027. zend_hash_str_add(&tmp_bindings, (char*)&tmp, sizeof(tmp), &zv);
  2028. binding_num++;
  2029. } ZEND_HASH_FOREACH_END();
  2030. }
  2031. /* serialize functions */
  2032. i = zend_hash_num_elements(&sdl->functions);
  2033. WSDL_CACHE_PUT_INT(i, out);
  2034. if (i > 0) {
  2035. sdlFunctionPtr tmp;
  2036. zval *binding_num, zv;
  2037. int function_num = 1;
  2038. zend_string *key;
  2039. ZEND_HASH_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
  2040. sdl_serialize_key(key, out);
  2041. sdl_serialize_string(tmp->functionName, out);
  2042. sdl_serialize_string(tmp->requestName, out);
  2043. sdl_serialize_string(tmp->responseName, out);
  2044. if (tmp->binding) {
  2045. binding_num = zend_hash_str_find(&tmp_bindings,(char*)&tmp->binding, sizeof(tmp->binding));
  2046. if (binding_num) {
  2047. WSDL_CACHE_PUT_INT(Z_LVAL_P(binding_num), out);
  2048. if (Z_LVAL_P(binding_num) >= 0) {
  2049. if (tmp->binding->bindingType == BINDING_SOAP && tmp->bindingAttributes != NULL) {
  2050. sdlSoapBindingFunctionPtr binding = (sdlSoapBindingFunctionPtr)tmp->bindingAttributes;
  2051. WSDL_CACHE_PUT_1(binding->style, out);
  2052. sdl_serialize_string(binding->soapAction, out);
  2053. sdl_serialize_soap_body(&binding->input, &tmp_encoders, &tmp_types, out);
  2054. sdl_serialize_soap_body(&binding->output, &tmp_encoders, &tmp_types, out);
  2055. } else {
  2056. WSDL_CACHE_PUT_1(0,out);
  2057. }
  2058. }
  2059. }
  2060. }
  2061. sdl_serialize_parameters(tmp->requestParameters, &tmp_encoders, &tmp_types, out);
  2062. sdl_serialize_parameters(tmp->responseParameters, &tmp_encoders, &tmp_types, out);
  2063. if (tmp->faults) {
  2064. sdlFaultPtr fault;
  2065. zend_string *key;
  2066. WSDL_CACHE_PUT_INT(zend_hash_num_elements(tmp->faults), out);
  2067. ZEND_HASH_FOREACH_STR_KEY_PTR(tmp->faults, key, fault) {
  2068. sdl_serialize_key(key, out);
  2069. sdl_serialize_string(fault->name, out);
  2070. sdl_serialize_parameters(fault->details, &tmp_encoders, &tmp_types, out);
  2071. if (tmp->binding->bindingType == BINDING_SOAP && fault->bindingAttributes != NULL) {
  2072. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  2073. WSDL_CACHE_PUT_1(binding->use, out);
  2074. if (binding->use == SOAP_ENCODED) {
  2075. WSDL_CACHE_PUT_1(binding->encodingStyle, out);
  2076. }
  2077. sdl_serialize_string(binding->ns, out);
  2078. } else {
  2079. WSDL_CACHE_PUT_1(0, out);
  2080. }
  2081. } ZEND_HASH_FOREACH_END();
  2082. } else {
  2083. WSDL_CACHE_PUT_INT(0, out);
  2084. }
  2085. ZVAL_LONG(&zv, function_num);
  2086. zend_hash_str_add(&tmp_functions, (char*)&tmp, sizeof(tmp), &zv);
  2087. function_num++;
  2088. } ZEND_HASH_FOREACH_END();
  2089. }
  2090. /* serialize requests */
  2091. if (sdl->requests) {
  2092. i = zend_hash_num_elements(sdl->requests);
  2093. } else {
  2094. i = 0;
  2095. }
  2096. WSDL_CACHE_PUT_INT(i, out);
  2097. if (i > 0) {
  2098. sdlFunctionPtr tmp;
  2099. zval *function_num;
  2100. zend_string *key;
  2101. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->requests, key, tmp) {
  2102. function_num = zend_hash_str_find(&tmp_functions, (char*)&tmp, sizeof(tmp));
  2103. WSDL_CACHE_PUT_INT(Z_LVAL_P(function_num), out);
  2104. sdl_serialize_key(key, out);
  2105. } ZEND_HASH_FOREACH_END();
  2106. }
  2107. php_ignore_value(write(f, ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)));
  2108. close(f);
  2109. smart_str_free(&buf);
  2110. zend_hash_destroy(&tmp_functions);
  2111. zend_hash_destroy(&tmp_bindings);
  2112. zend_hash_destroy(&tmp_encoders);
  2113. zend_hash_destroy(&tmp_types);
  2114. }
  2115. static void make_persistent_restriction_int(void *data)
  2116. {
  2117. sdlRestrictionIntPtr *rest = (sdlRestrictionIntPtr *)data;
  2118. sdlRestrictionIntPtr prest = NULL;
  2119. prest = malloc(sizeof(sdlRestrictionInt));
  2120. *prest = **rest;
  2121. *rest = prest;
  2122. }
  2123. static void make_persistent_restriction_char_int(sdlRestrictionCharPtr *rest)
  2124. {
  2125. sdlRestrictionCharPtr prest = NULL;
  2126. prest = malloc(sizeof(sdlRestrictionChar));
  2127. memset(prest, 0, sizeof(sdlRestrictionChar));
  2128. prest->value = strdup((*rest)->value);
  2129. prest->fixed = (*rest)->fixed;
  2130. *rest = prest;
  2131. }
  2132. static void make_persistent_sdl_type_ref(sdlTypePtr *type, HashTable *ptr_map, HashTable *bp_types)
  2133. {
  2134. sdlTypePtr tmp;
  2135. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)type, sizeof(sdlTypePtr))) != NULL) {
  2136. *type = tmp;
  2137. } else {
  2138. zend_hash_next_index_insert_ptr(bp_types, *type);
  2139. }
  2140. }
  2141. static void make_persistent_sdl_encoder_ref(encodePtr *enc, HashTable *ptr_map, HashTable *bp_encoders)
  2142. {
  2143. encodePtr tmp;
  2144. /* do not process defaultEncoding's here */
  2145. if ((*enc) >= defaultEncoding && (*enc) < defaultEncoding + numDefaultEncodings) {
  2146. return;
  2147. }
  2148. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char *)enc, sizeof(encodePtr))) != NULL) {
  2149. *enc = tmp;
  2150. } else {
  2151. zend_hash_next_index_insert_ptr(bp_encoders, enc);
  2152. }
  2153. }
  2154. static HashTable* make_persistent_sdl_function_headers(HashTable *headers, HashTable *ptr_map)
  2155. {
  2156. HashTable *pheaders;
  2157. sdlSoapBindingFunctionHeaderPtr tmp, pheader;
  2158. encodePtr penc;
  2159. sdlTypePtr ptype;
  2160. zend_string *key;
  2161. pheaders = malloc(sizeof(HashTable));
  2162. zend_hash_init(pheaders, zend_hash_num_elements(headers), NULL, delete_header_persistent, 1);
  2163. ZEND_HASH_FOREACH_STR_KEY_PTR(headers, key, tmp) {
  2164. pheader = malloc(sizeof(sdlSoapBindingFunctionHeader));
  2165. memset(pheader, 0, sizeof(sdlSoapBindingFunctionHeader));
  2166. *pheader = *tmp;
  2167. if (pheader->name) {
  2168. pheader->name = strdup(pheader->name);
  2169. }
  2170. if (pheader->ns) {
  2171. pheader->ns = strdup(pheader->ns);
  2172. }
  2173. if (pheader->encode && pheader->encode->details.sdl_type) {
  2174. if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->encode, sizeof(encodePtr))) == NULL) {
  2175. assert(0);
  2176. }
  2177. pheader->encode = penc;
  2178. }
  2179. if (pheader->element) {
  2180. if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pheader->element, sizeof(sdlTypePtr))) == NULL) {
  2181. assert(0);
  2182. }
  2183. pheader->element = ptype;
  2184. }
  2185. if (pheader->headerfaults) {
  2186. pheader->headerfaults = make_persistent_sdl_function_headers(pheader->headerfaults, ptr_map);
  2187. }
  2188. if (key) {
  2189. /* We have to duplicate key emalloc->malloc */
  2190. zend_hash_str_add_ptr(pheaders, ZSTR_VAL(key), ZSTR_LEN(key), pheader);
  2191. } else {
  2192. zend_hash_next_index_insert_ptr(pheaders, pheader);
  2193. }
  2194. } ZEND_HASH_FOREACH_END();
  2195. return pheaders;
  2196. }
  2197. static void make_persistent_sdl_soap_body(sdlSoapBindingFunctionBodyPtr body, HashTable *ptr_map)
  2198. {
  2199. if (body->ns) {
  2200. body->ns = strdup(body->ns);
  2201. }
  2202. if (body->headers) {
  2203. body->headers = make_persistent_sdl_function_headers(body->headers, ptr_map);
  2204. }
  2205. }
  2206. static HashTable* make_persistent_sdl_parameters(HashTable *params, HashTable *ptr_map)
  2207. {
  2208. HashTable *pparams;
  2209. sdlParamPtr tmp, pparam;
  2210. sdlTypePtr ptype;
  2211. encodePtr penc;
  2212. zend_string *key;
  2213. pparams = malloc(sizeof(HashTable));
  2214. zend_hash_init(pparams, zend_hash_num_elements(params), NULL, delete_parameter_persistent, 1);
  2215. ZEND_HASH_FOREACH_STR_KEY_PTR(params, key, tmp) {
  2216. pparam = malloc(sizeof(sdlParam));
  2217. memset(pparam, 0, sizeof(sdlParam));
  2218. *pparam = *tmp;
  2219. if (pparam->paramName) {
  2220. pparam->paramName = strdup(pparam->paramName);
  2221. }
  2222. if (pparam->encode && pparam->encode->details.sdl_type) {
  2223. if ((penc = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->encode, sizeof(encodePtr))) == NULL) {
  2224. assert(0);
  2225. }
  2226. pparam->encode = penc;
  2227. }
  2228. if (pparam->element) {
  2229. if ((ptype = zend_hash_str_find_ptr(ptr_map, (char*)&pparam->element, sizeof(sdlTypePtr))) == NULL) {
  2230. assert(0);
  2231. }
  2232. pparam->element = ptype;
  2233. }
  2234. if (key) {
  2235. /* We have to duplicate key emalloc->malloc */
  2236. zend_hash_str_add_ptr(pparams, ZSTR_VAL(key), ZSTR_LEN(key), pparam);
  2237. } else {
  2238. zend_hash_next_index_insert_ptr(pparams, pparam);
  2239. }
  2240. } ZEND_HASH_FOREACH_END();
  2241. return pparams;
  2242. }
  2243. static HashTable* make_persistent_sdl_function_faults(sdlFunctionPtr func, HashTable *faults, HashTable *ptr_map)
  2244. {
  2245. HashTable *pfaults;
  2246. sdlFaultPtr tmp, pfault;
  2247. zend_string *key;
  2248. pfaults = malloc(sizeof(HashTable));
  2249. zend_hash_init(pfaults, zend_hash_num_elements(faults), NULL, delete_fault_persistent, 1);
  2250. ZEND_HASH_FOREACH_STR_KEY_PTR(faults, key, tmp) {
  2251. pfault = malloc(sizeof(sdlFault));
  2252. memset(pfault, 0, sizeof(sdlFault));
  2253. *pfault = *tmp;
  2254. if (pfault->name) {
  2255. pfault->name = strdup(pfault->name);
  2256. }
  2257. if (pfault->details) {
  2258. pfault->details = make_persistent_sdl_parameters(pfault->details, ptr_map);
  2259. }
  2260. if (func->binding->bindingType == BINDING_SOAP && pfault->bindingAttributes) {
  2261. sdlSoapBindingFunctionFaultPtr soap_binding;
  2262. soap_binding = malloc(sizeof(sdlSoapBindingFunctionFault));
  2263. memset(soap_binding, 0, sizeof(sdlSoapBindingFunctionFault));
  2264. *soap_binding = *(sdlSoapBindingFunctionFaultPtr)pfault->bindingAttributes;
  2265. if (soap_binding->ns) {
  2266. soap_binding->ns = strdup(soap_binding->ns);
  2267. }
  2268. pfault->bindingAttributes = soap_binding;
  2269. }
  2270. if (key) {
  2271. /* We have to duplicate key emalloc->malloc */
  2272. zend_hash_str_add_ptr(pfaults, ZSTR_VAL(key), ZSTR_LEN(key), pfault);
  2273. } else {
  2274. zend_hash_next_index_insert_ptr(pfaults, pfault);
  2275. }
  2276. } ZEND_HASH_FOREACH_END();
  2277. return pfaults;
  2278. }
  2279. static sdlAttributePtr make_persistent_sdl_attribute(sdlAttributePtr attr, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2280. {
  2281. sdlAttributePtr pattr;
  2282. zend_string *key;
  2283. pattr = malloc(sizeof(sdlAttribute));
  2284. memset(pattr, 0, sizeof(sdlAttribute));
  2285. *pattr = *attr;
  2286. if (pattr->name) {
  2287. pattr->name = strdup(pattr->name);
  2288. }
  2289. if (pattr->namens) {
  2290. pattr->namens = strdup(pattr->namens);
  2291. }
  2292. if (pattr->ref) {
  2293. pattr->ref = strdup(pattr->ref);
  2294. }
  2295. if (pattr->def) {
  2296. pattr->def = strdup(pattr->def);
  2297. }
  2298. if (pattr->fixed) {
  2299. pattr->fixed = strdup(pattr->fixed);
  2300. }
  2301. /* we do not want to process defaultEncoding's here */
  2302. if (pattr->encode) {
  2303. make_persistent_sdl_encoder_ref(&pattr->encode, ptr_map, bp_encoders);
  2304. }
  2305. if (pattr->extraAttributes) {
  2306. sdlExtraAttributePtr tmp, pextra;
  2307. pattr->extraAttributes = malloc(sizeof(HashTable));
  2308. zend_hash_init(pattr->extraAttributes, zend_hash_num_elements(attr->extraAttributes), NULL, delete_extra_attribute_persistent, 1);
  2309. ZEND_HASH_FOREACH_STR_KEY_PTR(attr->extraAttributes, key, tmp) {
  2310. if (key) {
  2311. pextra = malloc(sizeof(sdlExtraAttribute));
  2312. memset(pextra, 0, sizeof(sdlExtraAttribute));
  2313. if (tmp->ns) {
  2314. pextra->ns = strdup(tmp->ns);
  2315. }
  2316. if (tmp->val) {
  2317. pextra->val = strdup(tmp->val);
  2318. }
  2319. /* We have to duplicate key emalloc->malloc */
  2320. zend_hash_str_add_ptr(pattr->extraAttributes, ZSTR_VAL(key), ZSTR_LEN(key), pextra);
  2321. }
  2322. } ZEND_HASH_FOREACH_END();
  2323. }
  2324. return pattr;
  2325. }
  2326. static sdlContentModelPtr make_persistent_sdl_model(sdlContentModelPtr model, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2327. {
  2328. sdlContentModelPtr pmodel;
  2329. sdlContentModelPtr tmp, pcontent;
  2330. pmodel = malloc(sizeof(sdlContentModel));
  2331. memset(pmodel, 0, sizeof(sdlContentModel));
  2332. *pmodel = *model;
  2333. switch (pmodel->kind) {
  2334. case XSD_CONTENT_ELEMENT:
  2335. if (pmodel->u.element) {
  2336. make_persistent_sdl_type_ref(&pmodel->u.element, ptr_map, bp_types);
  2337. }
  2338. break;
  2339. case XSD_CONTENT_SEQUENCE:
  2340. case XSD_CONTENT_ALL:
  2341. case XSD_CONTENT_CHOICE:
  2342. pmodel->u.content = malloc(sizeof(HashTable));
  2343. zend_hash_init(pmodel->u.content, zend_hash_num_elements(model->u.content), NULL, delete_model_persistent, 1);
  2344. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  2345. pcontent = make_persistent_sdl_model(tmp, ptr_map, bp_types, bp_encoders);
  2346. zend_hash_next_index_insert_ptr(pmodel->u.content, pcontent);
  2347. } ZEND_HASH_FOREACH_END();
  2348. break;
  2349. case XSD_CONTENT_GROUP_REF:
  2350. if (pmodel->u.group_ref) {
  2351. pmodel->u.group_ref = strdup(pmodel->u.group_ref);
  2352. }
  2353. break;
  2354. case XSD_CONTENT_GROUP:
  2355. if (pmodel->u.group) {
  2356. make_persistent_sdl_type_ref(&pmodel->u.group, ptr_map, bp_types);
  2357. }
  2358. break;
  2359. default:
  2360. break;
  2361. }
  2362. return pmodel;
  2363. }
  2364. static sdlTypePtr make_persistent_sdl_type(sdlTypePtr type, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2365. {
  2366. zend_string *key;
  2367. sdlTypePtr ptype = NULL;
  2368. ptype = malloc(sizeof(sdlType));
  2369. memset(ptype, 0, sizeof(sdlType));
  2370. *ptype = *type;
  2371. if (ptype->name) {
  2372. ptype->name = strdup(ptype->name);
  2373. }
  2374. if (ptype->namens) {
  2375. ptype->namens = strdup(ptype->namens);
  2376. }
  2377. if (ptype->def) {
  2378. ptype->def = strdup(ptype->def);
  2379. }
  2380. if (ptype->fixed) {
  2381. ptype->fixed = strdup(ptype->fixed);
  2382. }
  2383. if (ptype->ref) {
  2384. ptype->ref = strdup(ptype->ref);
  2385. }
  2386. /* we do not want to process defaultEncoding's here */
  2387. if (ptype->encode) {
  2388. make_persistent_sdl_encoder_ref(&ptype->encode, ptr_map, bp_encoders);
  2389. }
  2390. if (ptype->restrictions) {
  2391. ptype->restrictions = malloc(sizeof(sdlRestrictions));
  2392. memset(ptype->restrictions, 0, sizeof(sdlRestrictions));
  2393. *ptype->restrictions = *type->restrictions;
  2394. if (ptype->restrictions->minExclusive) {
  2395. make_persistent_restriction_int(&ptype->restrictions->minExclusive);
  2396. }
  2397. if (ptype->restrictions->maxExclusive) {
  2398. make_persistent_restriction_int(&ptype->restrictions->maxExclusive);
  2399. }
  2400. if (ptype->restrictions->minInclusive) {
  2401. make_persistent_restriction_int(&ptype->restrictions->minInclusive);
  2402. }
  2403. if (ptype->restrictions->maxInclusive) {
  2404. make_persistent_restriction_int(&ptype->restrictions->maxInclusive);
  2405. }
  2406. if (ptype->restrictions->totalDigits) {
  2407. make_persistent_restriction_int(&ptype->restrictions->totalDigits);
  2408. }
  2409. if (ptype->restrictions->fractionDigits) {
  2410. make_persistent_restriction_int(&ptype->restrictions->fractionDigits);
  2411. }
  2412. if (ptype->restrictions->length) {
  2413. make_persistent_restriction_int(&ptype->restrictions->length);
  2414. }
  2415. if (ptype->restrictions->minLength) {
  2416. make_persistent_restriction_int(&ptype->restrictions->minLength);
  2417. }
  2418. if (ptype->restrictions->maxLength) {
  2419. make_persistent_restriction_int(&ptype->restrictions->maxLength);
  2420. }
  2421. if (ptype->restrictions->whiteSpace) {
  2422. make_persistent_restriction_char_int(&ptype->restrictions->whiteSpace);
  2423. }
  2424. if (ptype->restrictions->pattern) {
  2425. make_persistent_restriction_char_int(&ptype->restrictions->pattern);
  2426. }
  2427. if (type->restrictions->enumeration) {
  2428. sdlRestrictionCharPtr tmp, penum;
  2429. ptype->restrictions->enumeration = malloc(sizeof(HashTable));
  2430. zend_hash_init(ptype->restrictions->enumeration, zend_hash_num_elements(type->restrictions->enumeration), NULL, delete_restriction_var_char_persistent, 1);
  2431. ZEND_HASH_FOREACH_STR_KEY_PTR(type->restrictions->enumeration, key, tmp) {
  2432. penum = tmp;
  2433. make_persistent_restriction_char_int(&penum);
  2434. /* We have to duplicate key emalloc->malloc */
  2435. zend_hash_str_add_ptr(ptype->restrictions->enumeration, ZSTR_VAL(key), ZSTR_LEN(key), penum);
  2436. } ZEND_HASH_FOREACH_END();
  2437. }
  2438. }
  2439. if (ptype->elements) {
  2440. sdlTypePtr tmp, pelem;
  2441. ptype->elements = malloc(sizeof(HashTable));
  2442. zend_hash_init(ptype->elements, zend_hash_num_elements(type->elements), NULL, delete_type_persistent, 1);
  2443. ZEND_HASH_FOREACH_STR_KEY_PTR(type->elements, key, tmp) {
  2444. pelem = make_persistent_sdl_type(tmp, ptr_map, bp_types, bp_encoders);
  2445. if (key) {
  2446. /* We have to duplicate key emalloc->malloc */
  2447. zend_hash_str_add_ptr(ptype->elements, ZSTR_VAL(key), ZSTR_LEN(key), pelem);
  2448. } else {
  2449. zend_hash_next_index_insert_ptr(ptype->elements, pelem);
  2450. }
  2451. zend_hash_str_add_ptr(ptr_map, (char*)&tmp, sizeof(tmp), pelem);
  2452. } ZEND_HASH_FOREACH_END();
  2453. }
  2454. if (ptype->attributes) {
  2455. sdlAttributePtr tmp, pattr;
  2456. ptype->attributes = malloc(sizeof(HashTable));
  2457. zend_hash_init(ptype->attributes, zend_hash_num_elements(type->attributes), NULL, delete_attribute_persistent, 1);
  2458. ZEND_HASH_FOREACH_STR_KEY_PTR(type->attributes, key, tmp) {
  2459. pattr = make_persistent_sdl_attribute(tmp, ptr_map, bp_types, bp_encoders);
  2460. if (key) {
  2461. /* We have to duplicate key emalloc->malloc */
  2462. zend_hash_str_add_ptr(ptype->attributes, ZSTR_VAL(key), ZSTR_LEN(key), pattr);
  2463. } else {
  2464. zend_hash_next_index_insert_ptr(ptype->attributes, pattr);
  2465. }
  2466. } ZEND_HASH_FOREACH_END();
  2467. }
  2468. if (type->model) {
  2469. ptype->model = make_persistent_sdl_model(ptype->model, ptr_map, bp_types, bp_encoders);
  2470. }
  2471. return ptype;
  2472. }
  2473. static encodePtr make_persistent_sdl_encoder(encodePtr enc, HashTable *ptr_map, HashTable *bp_types, HashTable *bp_encoders)
  2474. {
  2475. encodePtr penc = NULL;
  2476. penc = malloc(sizeof(encode));
  2477. memset(penc, 0, sizeof(encode));
  2478. *penc = *enc;
  2479. if (penc->details.type_str) {
  2480. penc->details.type_str = strdup(penc->details.type_str);
  2481. }
  2482. if (penc->details.ns) {
  2483. penc->details.ns = strdup(penc->details.ns);
  2484. }
  2485. if (penc->details.sdl_type) {
  2486. make_persistent_sdl_type_ref(&penc->details.sdl_type, ptr_map, bp_types);
  2487. }
  2488. return penc;
  2489. }
  2490. static sdlBindingPtr make_persistent_sdl_binding(sdlBindingPtr bind, HashTable *ptr_map)
  2491. {
  2492. sdlBindingPtr pbind = NULL;
  2493. pbind = malloc(sizeof(sdlBinding));
  2494. memset(pbind, 0, sizeof(sdlBinding));
  2495. *pbind = *bind;
  2496. if (pbind->name) {
  2497. pbind->name = strdup(pbind->name);
  2498. }
  2499. if (pbind->location) {
  2500. pbind->location = strdup(pbind->location);
  2501. }
  2502. if (pbind->bindingType == BINDING_SOAP && pbind->bindingAttributes) {
  2503. sdlSoapBindingPtr soap_binding;
  2504. soap_binding = malloc(sizeof(sdlSoapBinding));
  2505. memset(soap_binding, 0, sizeof(sdlSoapBinding));
  2506. *soap_binding = *(sdlSoapBindingPtr)pbind->bindingAttributes;
  2507. pbind->bindingAttributes = soap_binding;
  2508. }
  2509. return pbind;
  2510. }
  2511. static sdlFunctionPtr make_persistent_sdl_function(sdlFunctionPtr func, HashTable *ptr_map)
  2512. {
  2513. sdlFunctionPtr pfunc = NULL;
  2514. pfunc = malloc(sizeof(sdlFunction));
  2515. memset(pfunc, 0, sizeof(sdlFunction));
  2516. *pfunc = *func;
  2517. if (pfunc->functionName) {
  2518. pfunc->functionName = strdup(pfunc->functionName);
  2519. }
  2520. if (pfunc->requestName) {
  2521. pfunc->requestName = strdup(pfunc->requestName);
  2522. }
  2523. if (pfunc->responseName) {
  2524. pfunc->responseName = strdup(pfunc->responseName);
  2525. }
  2526. if (pfunc->binding) {
  2527. sdlBindingPtr tmp;
  2528. if ((tmp = zend_hash_str_find_ptr(ptr_map, (char*)&pfunc->binding, sizeof(pfunc->binding))) == NULL) {
  2529. assert(0);
  2530. }
  2531. pfunc->binding = tmp;
  2532. if (pfunc->binding->bindingType == BINDING_SOAP && pfunc->bindingAttributes) {
  2533. sdlSoapBindingFunctionPtr soap_binding;
  2534. soap_binding = malloc(sizeof(sdlSoapBindingFunction));
  2535. memset(soap_binding, 0, sizeof(sdlSoapBindingFunction));
  2536. *soap_binding = *(sdlSoapBindingFunctionPtr)pfunc->bindingAttributes;
  2537. if (soap_binding->soapAction) {
  2538. soap_binding->soapAction = strdup(soap_binding->soapAction);
  2539. }
  2540. make_persistent_sdl_soap_body(&soap_binding->input, ptr_map);
  2541. make_persistent_sdl_soap_body(&soap_binding->output, ptr_map);
  2542. pfunc->bindingAttributes = soap_binding;
  2543. }
  2544. if (pfunc->requestParameters) {
  2545. pfunc->requestParameters = make_persistent_sdl_parameters(pfunc->requestParameters, ptr_map);
  2546. }
  2547. if (pfunc->responseParameters) {
  2548. pfunc->responseParameters = make_persistent_sdl_parameters(pfunc->responseParameters, ptr_map);
  2549. }
  2550. if (pfunc->faults) {
  2551. pfunc->faults = make_persistent_sdl_function_faults(pfunc, pfunc->faults, ptr_map);
  2552. }
  2553. }
  2554. return pfunc;
  2555. }
  2556. static sdlPtr make_persistent_sdl(sdlPtr sdl)
  2557. {
  2558. sdlPtr psdl = NULL;
  2559. HashTable ptr_map;
  2560. HashTable bp_types, bp_encoders;
  2561. zend_string *key;
  2562. zend_hash_init(&bp_types, 0, NULL, NULL, 0);
  2563. zend_hash_init(&bp_encoders, 0, NULL, NULL, 0);
  2564. zend_hash_init(&ptr_map, 0, NULL, NULL, 0);
  2565. psdl = malloc(sizeof(*sdl));
  2566. memset(psdl, 0, sizeof(*sdl));
  2567. if (sdl->source) {
  2568. psdl->source = strdup(sdl->source);
  2569. }
  2570. if (sdl->target_ns) {
  2571. psdl->target_ns = strdup(sdl->target_ns);
  2572. }
  2573. if (sdl->groups) {
  2574. sdlTypePtr tmp;
  2575. sdlTypePtr ptype;
  2576. psdl->groups = malloc(sizeof(HashTable));
  2577. zend_hash_init(psdl->groups, zend_hash_num_elements(sdl->groups), NULL, delete_type_persistent, 1);
  2578. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->groups, key, tmp) {
  2579. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2580. if (key) {
  2581. /* We have to duplicate key emalloc->malloc */
  2582. zend_hash_str_add_ptr(psdl->groups, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2583. } else {
  2584. zend_hash_next_index_insert_ptr(psdl->groups, ptype);
  2585. }
  2586. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2587. } ZEND_HASH_FOREACH_END();
  2588. }
  2589. if (sdl->types) {
  2590. sdlTypePtr tmp;
  2591. sdlTypePtr ptype;
  2592. psdl->types = malloc(sizeof(HashTable));
  2593. zend_hash_init(psdl->types, zend_hash_num_elements(sdl->types), NULL, delete_type_persistent, 1);
  2594. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->types, key, tmp) {
  2595. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2596. if (key) {
  2597. /* We have to duplicate key emalloc->malloc */
  2598. zend_hash_str_add_ptr(psdl->types, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2599. } else {
  2600. zend_hash_next_index_insert_ptr(psdl->types, ptype);
  2601. }
  2602. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2603. } ZEND_HASH_FOREACH_END();
  2604. }
  2605. if (sdl->elements) {
  2606. sdlTypePtr tmp;
  2607. sdlTypePtr ptype;
  2608. psdl->elements = malloc(sizeof(HashTable));
  2609. zend_hash_init(psdl->elements, zend_hash_num_elements(sdl->elements), NULL, delete_type_persistent, 1);
  2610. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->elements, key, tmp) {
  2611. ptype = make_persistent_sdl_type(tmp, &ptr_map, &bp_types, &bp_encoders);
  2612. if (key) {
  2613. /* We have to duplicate key emalloc->malloc */
  2614. zend_hash_str_add_ptr(psdl->elements, ZSTR_VAL(key), ZSTR_LEN(key), ptype);
  2615. } else {
  2616. zend_hash_next_index_insert_ptr(psdl->elements, ptype);
  2617. }
  2618. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), ptype);
  2619. } ZEND_HASH_FOREACH_END();
  2620. }
  2621. if (sdl->encoders) {
  2622. encodePtr tmp;
  2623. encodePtr penc;
  2624. psdl->encoders = malloc(sizeof(HashTable));
  2625. zend_hash_init(psdl->encoders, zend_hash_num_elements(sdl->encoders), NULL, delete_encoder_persistent, 1);
  2626. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->encoders, key, tmp) {
  2627. penc = make_persistent_sdl_encoder(tmp, &ptr_map, &bp_types, &bp_encoders);
  2628. if (key) {
  2629. /* We have to duplicate key emalloc->malloc */
  2630. zend_hash_str_add_ptr(psdl->encoders, ZSTR_VAL(key), ZSTR_LEN(key), penc);
  2631. } else {
  2632. zend_hash_next_index_insert_ptr(psdl->encoders, penc);
  2633. }
  2634. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), penc);
  2635. } ZEND_HASH_FOREACH_END();
  2636. }
  2637. /* do backpatching here */
  2638. if (zend_hash_num_elements(&bp_types)) {
  2639. sdlTypePtr *tmp, ptype = NULL;
  2640. ZEND_HASH_FOREACH_PTR(&bp_types, tmp) {
  2641. if ((ptype = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
  2642. assert(0);
  2643. }
  2644. *tmp = ptype;
  2645. } ZEND_HASH_FOREACH_END();
  2646. }
  2647. if (zend_hash_num_elements(&bp_encoders)) {
  2648. encodePtr *tmp, penc = NULL;
  2649. ZEND_HASH_FOREACH_PTR(&bp_encoders, tmp) {
  2650. if ((penc = zend_hash_str_find_ptr(&ptr_map, (char*)tmp, sizeof(*tmp))) == NULL) {
  2651. assert(0);
  2652. }
  2653. *tmp = penc;
  2654. } ZEND_HASH_FOREACH_END();
  2655. }
  2656. if (sdl->bindings) {
  2657. sdlBindingPtr tmp;
  2658. sdlBindingPtr pbind;
  2659. psdl->bindings = malloc(sizeof(HashTable));
  2660. zend_hash_init(psdl->bindings, zend_hash_num_elements(sdl->bindings), NULL, delete_binding_persistent, 1);
  2661. ZEND_HASH_FOREACH_STR_KEY_PTR(sdl->bindings, key, tmp) {
  2662. pbind = make_persistent_sdl_binding(tmp, &ptr_map);
  2663. if (key) {
  2664. /* We have to duplicate key emalloc->malloc */
  2665. zend_hash_str_add_ptr(psdl->bindings, ZSTR_VAL(key), ZSTR_LEN(key), pbind);
  2666. } else {
  2667. zend_hash_next_index_insert_ptr(psdl->bindings, pbind);
  2668. }
  2669. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pbind);
  2670. } ZEND_HASH_FOREACH_END();
  2671. }
  2672. zend_hash_init(&psdl->functions, zend_hash_num_elements(&sdl->functions), NULL, delete_function_persistent, 1);
  2673. if (zend_hash_num_elements(&sdl->functions)) {
  2674. sdlFunctionPtr tmp;
  2675. sdlFunctionPtr pfunc;
  2676. ZEND_HASH_FOREACH_STR_KEY_PTR(&sdl->functions, key, tmp) {
  2677. pfunc = make_persistent_sdl_function(tmp, &ptr_map);
  2678. if (key) {
  2679. /* We have to duplicate key emalloc->malloc */
  2680. zend_hash_str_add_ptr(&psdl->functions, ZSTR_VAL(key), ZSTR_LEN(key), pfunc);
  2681. } else {
  2682. zend_hash_next_index_insert_ptr(&psdl->functions, pfunc);
  2683. }
  2684. zend_hash_str_add_ptr(&ptr_map, (char*)&tmp, sizeof(tmp), pfunc);
  2685. } ZEND_HASH_FOREACH_END();
  2686. }
  2687. if (sdl->requests) {
  2688. zval *zv;
  2689. sdlFunctionPtr tmp;
  2690. sdlFunctionPtr preq;
  2691. psdl->requests = malloc(sizeof(HashTable));
  2692. zend_hash_init(psdl->requests, zend_hash_num_elements(sdl->requests), NULL, NULL, 1);
  2693. ZEND_HASH_FOREACH_STR_KEY_VAL(sdl->requests, key, zv) {
  2694. tmp = Z_PTR_P(zv);
  2695. if ((preq = zend_hash_str_find_ptr(&ptr_map, (char*)&tmp, sizeof(tmp))) == NULL) {
  2696. assert(0);
  2697. }
  2698. Z_PTR_P(zv) = preq;
  2699. if (key) {
  2700. /* We have to duplicate key emalloc->malloc */
  2701. zend_hash_str_add_ptr(psdl->requests, ZSTR_VAL(key), ZSTR_LEN(key), preq);
  2702. }
  2703. } ZEND_HASH_FOREACH_END();
  2704. }
  2705. zend_hash_destroy(&ptr_map);
  2706. zend_hash_destroy(&bp_encoders);
  2707. zend_hash_destroy(&bp_types);
  2708. return psdl;
  2709. }
  2710. typedef struct _sdl_cache_bucket {
  2711. sdlPtr sdl;
  2712. time_t time;
  2713. } sdl_cache_bucket;
  2714. static void delete_psdl_int(sdl_cache_bucket *p)
  2715. {
  2716. sdlPtr tmp = p->sdl;
  2717. zend_hash_destroy(&tmp->functions);
  2718. if (tmp->source) {
  2719. free(tmp->source);
  2720. }
  2721. if (tmp->target_ns) {
  2722. free(tmp->target_ns);
  2723. }
  2724. if (tmp->elements) {
  2725. zend_hash_destroy(tmp->elements);
  2726. free(tmp->elements);
  2727. }
  2728. if (tmp->encoders) {
  2729. zend_hash_destroy(tmp->encoders);
  2730. free(tmp->encoders);
  2731. }
  2732. if (tmp->types) {
  2733. zend_hash_destroy(tmp->types);
  2734. free(tmp->types);
  2735. }
  2736. if (tmp->groups) {
  2737. zend_hash_destroy(tmp->groups);
  2738. free(tmp->groups);
  2739. }
  2740. if (tmp->bindings) {
  2741. zend_hash_destroy(tmp->bindings);
  2742. free(tmp->bindings);
  2743. }
  2744. if (tmp->requests) {
  2745. zend_hash_destroy(tmp->requests);
  2746. free(tmp->requests);
  2747. }
  2748. free(tmp);
  2749. }
  2750. static void delete_psdl(zval *zv)
  2751. {
  2752. delete_psdl_int(Z_PTR_P(zv));
  2753. free(Z_PTR_P(zv));
  2754. }
  2755. sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
  2756. {
  2757. char fn[MAXPATHLEN];
  2758. sdlPtr sdl = NULL;
  2759. char* old_error_code = SOAP_GLOBAL(error_code);
  2760. size_t uri_len = 0;
  2761. php_stream_context *context=NULL;
  2762. zval *tmp, *proxy_host, *proxy_port, orig_context, new_context;
  2763. smart_str headers = {0};
  2764. char* key = NULL;
  2765. time_t t = time(0);
  2766. zend_bool has_proxy_authorization = 0;
  2767. zend_bool has_authorization = 0;
  2768. ZVAL_UNDEF(&orig_context);
  2769. ZVAL_UNDEF(&new_context);
  2770. if (strchr(uri,':') != NULL || IS_ABSOLUTE_PATH(uri, uri_len)) {
  2771. uri_len = strlen(uri);
  2772. } else if (VCWD_REALPATH(uri, fn) == NULL) {
  2773. cache_wsdl = WSDL_CACHE_NONE;
  2774. } else {
  2775. uri = fn;
  2776. uri_len = strlen(uri);
  2777. }
  2778. if ((cache_wsdl & WSDL_CACHE_MEMORY) && SOAP_GLOBAL(mem_cache)) {
  2779. sdl_cache_bucket *p;
  2780. if (NULL != (p = zend_hash_str_find_ptr(SOAP_GLOBAL(mem_cache), uri, uri_len))) {
  2781. if (p->time < t - SOAP_GLOBAL(cache_ttl)) {
  2782. /* in-memory cache entry is expired */
  2783. zend_hash_str_del(&EG(persistent_list), uri, uri_len);
  2784. } else {
  2785. return p->sdl;
  2786. }
  2787. }
  2788. }
  2789. if ((cache_wsdl & WSDL_CACHE_DISK) && (uri_len < MAXPATHLEN)) {
  2790. time_t t = time(0);
  2791. char md5str[33];
  2792. PHP_MD5_CTX context;
  2793. unsigned char digest[16];
  2794. int len = strlen(SOAP_GLOBAL(cache_dir));
  2795. time_t cached;
  2796. char *user = php_get_current_user();
  2797. int user_len = user ? strlen(user) + 1 : 0;
  2798. md5str[0] = '\0';
  2799. PHP_MD5Init(&context);
  2800. PHP_MD5Update(&context, (unsigned char*)uri, uri_len);
  2801. PHP_MD5Final(digest, &context);
  2802. make_digest(md5str, digest);
  2803. key = emalloc(len+sizeof("/wsdl-")-1+user_len+2+sizeof(md5str));
  2804. memcpy(key,SOAP_GLOBAL(cache_dir),len);
  2805. memcpy(key+len,"/wsdl-",sizeof("/wsdl-")-1);
  2806. len += sizeof("/wsdl-")-1;
  2807. if (user_len) {
  2808. memcpy(key+len, user, user_len-1);
  2809. len += user_len-1;
  2810. key[len++] = '-';
  2811. }
  2812. if (WSDL_CACHE_VERSION <= 0x9f) {
  2813. key[len++] = (WSDL_CACHE_VERSION >> 8) + '0';
  2814. } else {
  2815. key[len++] = (WSDL_CACHE_VERSION >> 8) - 10 + 'a';
  2816. }
  2817. if ((WSDL_CACHE_VERSION & 0xf) <= 0x9) {
  2818. key[len++] = (WSDL_CACHE_VERSION & 0xf) + '0';
  2819. } else {
  2820. key[len++] = (WSDL_CACHE_VERSION & 0xf) - 10 + 'a';
  2821. }
  2822. memcpy(key+len,md5str,sizeof(md5str));
  2823. if ((sdl = get_sdl_from_cache(key, uri, t-SOAP_GLOBAL(cache_ttl), &cached)) != NULL) {
  2824. t = cached;
  2825. efree(key);
  2826. goto cache_in_memory;
  2827. }
  2828. }
  2829. if (NULL != (tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr),
  2830. "_stream_context", sizeof("_stream_context")-1))) {
  2831. context = php_stream_context_from_zval(tmp, 0);
  2832. } else {
  2833. context = php_stream_context_alloc();
  2834. }
  2835. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_user_agent", sizeof("_user_agent")-1)) != NULL &&
  2836. Z_TYPE_P(tmp) == IS_STRING && Z_STRLEN_P(tmp) > 0) {
  2837. smart_str_appends(&headers, "User-Agent: ");
  2838. smart_str_appends(&headers, Z_STRVAL_P(tmp));
  2839. smart_str_appends(&headers, "\r\n");
  2840. }
  2841. if ((proxy_host = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_host", sizeof("_proxy_host")-1)) != NULL &&
  2842. Z_TYPE_P(proxy_host) == IS_STRING &&
  2843. (proxy_port = zend_hash_str_find(Z_OBJPROP_P(this_ptr), "_proxy_port", sizeof("_proxy_port")-1)) != NULL &&
  2844. Z_TYPE_P(proxy_port) == IS_LONG) {
  2845. zval str_proxy;
  2846. smart_str proxy = {0};
  2847. smart_str_appends(&proxy,"tcp://");
  2848. smart_str_appends(&proxy,Z_STRVAL_P(proxy_host));
  2849. smart_str_appends(&proxy,":");
  2850. smart_str_append_long(&proxy,Z_LVAL_P(proxy_port));
  2851. smart_str_0(&proxy);
  2852. ZVAL_NEW_STR(&str_proxy, proxy.s);
  2853. if (!context) {
  2854. context = php_stream_context_alloc();
  2855. }
  2856. php_stream_context_set_option(context, "http", "proxy", &str_proxy);
  2857. zval_ptr_dtor(&str_proxy);
  2858. if (uri_len < sizeof("https://")-1 ||
  2859. strncasecmp(uri, "https://", sizeof("https://")-1) != 0) {
  2860. ZVAL_TRUE(&str_proxy);
  2861. php_stream_context_set_option(context, "http", "request_fulluri", &str_proxy);
  2862. }
  2863. has_proxy_authorization = proxy_authentication(this_ptr, &headers);
  2864. }
  2865. has_authorization = basic_authentication(this_ptr, &headers);
  2866. /* Use HTTP/1.1 with "Connection: close" by default */
  2867. if ((tmp = php_stream_context_get_option(context, "http", "protocol_version")) == NULL) {
  2868. zval http_version;
  2869. ZVAL_DOUBLE(&http_version, 1.1);
  2870. php_stream_context_set_option(context, "http", "protocol_version", &http_version);
  2871. smart_str_appendl(&headers, "Connection: close\r\n", sizeof("Connection: close\r\n")-1);
  2872. }
  2873. if (headers.s && ZSTR_LEN(headers.s) > 0) {
  2874. zval str_headers;
  2875. if (!context) {
  2876. context = php_stream_context_alloc();
  2877. } else {
  2878. http_context_headers(context, has_authorization, has_proxy_authorization, 0, &headers);
  2879. }
  2880. smart_str_0(&headers);
  2881. ZVAL_NEW_STR(&str_headers, headers.s);
  2882. php_stream_context_set_option(context, "http", "header", &str_headers);
  2883. zval_ptr_dtor(&str_headers);
  2884. }
  2885. if (context) {
  2886. php_stream_context_to_zval(context, &new_context);
  2887. php_libxml_switch_context(&new_context, &orig_context);
  2888. }
  2889. SOAP_GLOBAL(error_code) = "WSDL";
  2890. sdl = load_wsdl(this_ptr, uri);
  2891. if (sdl) {
  2892. sdl->is_persistent = 0;
  2893. }
  2894. SOAP_GLOBAL(error_code) = old_error_code;
  2895. if (context) {
  2896. php_libxml_switch_context(&orig_context, NULL);
  2897. zval_ptr_dtor(&new_context);
  2898. }
  2899. if ((cache_wsdl & WSDL_CACHE_DISK) && key) {
  2900. if (sdl) {
  2901. add_sdl_to_cache(key, uri, t, sdl);
  2902. }
  2903. efree(key);
  2904. }
  2905. cache_in_memory:
  2906. if (cache_wsdl & WSDL_CACHE_MEMORY) {
  2907. if (sdl) {
  2908. sdlPtr psdl;
  2909. sdl_cache_bucket p;
  2910. if (SOAP_GLOBAL(mem_cache) == NULL) {
  2911. SOAP_GLOBAL(mem_cache) = malloc(sizeof(HashTable));
  2912. zend_hash_init(SOAP_GLOBAL(mem_cache), 0, NULL, delete_psdl, 1);
  2913. } else if (SOAP_GLOBAL(cache_limit) > 0 &&
  2914. SOAP_GLOBAL(cache_limit) <= (zend_long)zend_hash_num_elements(SOAP_GLOBAL(mem_cache))) {
  2915. /* in-memory cache overflow */
  2916. sdl_cache_bucket *q;
  2917. time_t latest = t;
  2918. zend_string *latest_key = NULL, *key;
  2919. ZEND_HASH_FOREACH_STR_KEY_PTR(SOAP_GLOBAL(mem_cache), key, q) {
  2920. if (q->time < latest) {
  2921. latest = q->time;
  2922. latest_key = key;
  2923. }
  2924. } ZEND_HASH_FOREACH_END();
  2925. if (latest_key) {
  2926. zend_hash_del(SOAP_GLOBAL(mem_cache), latest_key);
  2927. } else {
  2928. return sdl;
  2929. }
  2930. }
  2931. psdl = make_persistent_sdl(sdl);
  2932. psdl->is_persistent = 1;
  2933. p.time = t;
  2934. p.sdl = psdl;
  2935. zend_hash_str_update_mem(SOAP_GLOBAL(mem_cache), uri,
  2936. uri_len, &p, sizeof(sdl_cache_bucket));
  2937. /* remove non-persitent sdl structure */
  2938. delete_sdl_impl(sdl);
  2939. /* and replace it with persistent one */
  2940. sdl = psdl;
  2941. }
  2942. }
  2943. return sdl;
  2944. }
  2945. /* Deletes */
  2946. void delete_sdl_impl(void *handle)
  2947. {
  2948. sdlPtr tmp = (sdlPtr)handle;
  2949. zend_hash_destroy(&tmp->functions);
  2950. if (tmp->source) {
  2951. efree(tmp->source);
  2952. }
  2953. if (tmp->target_ns) {
  2954. efree(tmp->target_ns);
  2955. }
  2956. if (tmp->elements) {
  2957. zend_hash_destroy(tmp->elements);
  2958. efree(tmp->elements);
  2959. }
  2960. if (tmp->encoders) {
  2961. zend_hash_destroy(tmp->encoders);
  2962. efree(tmp->encoders);
  2963. }
  2964. if (tmp->types) {
  2965. zend_hash_destroy(tmp->types);
  2966. efree(tmp->types);
  2967. }
  2968. if (tmp->groups) {
  2969. zend_hash_destroy(tmp->groups);
  2970. efree(tmp->groups);
  2971. }
  2972. if (tmp->bindings) {
  2973. zend_hash_destroy(tmp->bindings);
  2974. efree(tmp->bindings);
  2975. }
  2976. if (tmp->requests) {
  2977. zend_hash_destroy(tmp->requests);
  2978. efree(tmp->requests);
  2979. }
  2980. efree(tmp);
  2981. }
  2982. void delete_sdl(void *handle)
  2983. {
  2984. sdlPtr tmp = (sdlPtr)handle;
  2985. if (!tmp->is_persistent) {
  2986. delete_sdl_impl(tmp);
  2987. }
  2988. }
  2989. static void delete_binding(zval *zv)
  2990. {
  2991. sdlBindingPtr binding = Z_PTR_P(zv);
  2992. if (binding->location) {
  2993. efree(binding->location);
  2994. }
  2995. if (binding->name) {
  2996. efree(binding->name);
  2997. }
  2998. if (binding->bindingType == BINDING_SOAP) {
  2999. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3000. if (soapBind) {
  3001. efree(soapBind);
  3002. }
  3003. }
  3004. efree(binding);
  3005. }
  3006. static void delete_binding_persistent(zval *zv)
  3007. {
  3008. sdlBindingPtr binding = Z_PTR_P(zv);
  3009. if (binding->location) {
  3010. free(binding->location);
  3011. }
  3012. if (binding->name) {
  3013. free(binding->name);
  3014. }
  3015. if (binding->bindingType == BINDING_SOAP) {
  3016. sdlSoapBindingPtr soapBind = binding->bindingAttributes;
  3017. if (soapBind) {
  3018. free(soapBind);
  3019. }
  3020. }
  3021. free(binding);
  3022. }
  3023. static void delete_sdl_soap_binding_function_body(sdlSoapBindingFunctionBody body)
  3024. {
  3025. if (body.ns) {
  3026. efree(body.ns);
  3027. }
  3028. if (body.headers) {
  3029. zend_hash_destroy(body.headers);
  3030. efree(body.headers);
  3031. }
  3032. }
  3033. static void delete_sdl_soap_binding_function_body_persistent(sdlSoapBindingFunctionBody body)
  3034. {
  3035. if (body.ns) {
  3036. free(body.ns);
  3037. }
  3038. if (body.headers) {
  3039. zend_hash_destroy(body.headers);
  3040. free(body.headers);
  3041. }
  3042. }
  3043. static void delete_function(zval *zv)
  3044. {
  3045. sdlFunctionPtr function = Z_PTR_P(zv);
  3046. if (function->functionName) {
  3047. efree(function->functionName);
  3048. }
  3049. if (function->requestName) {
  3050. efree(function->requestName);
  3051. }
  3052. if (function->responseName) {
  3053. efree(function->responseName);
  3054. }
  3055. if (function->requestParameters) {
  3056. zend_hash_destroy(function->requestParameters);
  3057. efree(function->requestParameters);
  3058. }
  3059. if (function->responseParameters) {
  3060. zend_hash_destroy(function->responseParameters);
  3061. efree(function->responseParameters);
  3062. }
  3063. if (function->faults) {
  3064. zend_hash_destroy(function->faults);
  3065. efree(function->faults);
  3066. }
  3067. if (function->bindingAttributes &&
  3068. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3069. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3070. if (soapFunction->soapAction) {
  3071. efree(soapFunction->soapAction);
  3072. }
  3073. delete_sdl_soap_binding_function_body(soapFunction->input);
  3074. delete_sdl_soap_binding_function_body(soapFunction->output);
  3075. efree(soapFunction);
  3076. }
  3077. efree(function);
  3078. }
  3079. static void delete_function_persistent(zval *zv)
  3080. {
  3081. sdlFunctionPtr function = Z_PTR_P(zv);
  3082. if (function->functionName) {
  3083. free(function->functionName);
  3084. }
  3085. if (function->requestName) {
  3086. free(function->requestName);
  3087. }
  3088. if (function->responseName) {
  3089. free(function->responseName);
  3090. }
  3091. if (function->requestParameters) {
  3092. zend_hash_destroy(function->requestParameters);
  3093. free(function->requestParameters);
  3094. }
  3095. if (function->responseParameters) {
  3096. zend_hash_destroy(function->responseParameters);
  3097. free(function->responseParameters);
  3098. }
  3099. if (function->faults) {
  3100. zend_hash_destroy(function->faults);
  3101. free(function->faults);
  3102. }
  3103. if (function->bindingAttributes &&
  3104. function->binding && function->binding->bindingType == BINDING_SOAP) {
  3105. sdlSoapBindingFunctionPtr soapFunction = function->bindingAttributes;
  3106. if (soapFunction->soapAction) {
  3107. free(soapFunction->soapAction);
  3108. }
  3109. delete_sdl_soap_binding_function_body_persistent(soapFunction->input);
  3110. delete_sdl_soap_binding_function_body_persistent(soapFunction->output);
  3111. free(soapFunction);
  3112. }
  3113. free(function);
  3114. }
  3115. static void delete_parameter(zval *zv)
  3116. {
  3117. sdlParamPtr param = Z_PTR_P(zv);
  3118. if (param->paramName) {
  3119. efree(param->paramName);
  3120. }
  3121. efree(param);
  3122. }
  3123. static void delete_parameter_persistent(zval *zv)
  3124. {
  3125. sdlParamPtr param = Z_PTR_P(zv);
  3126. if (param->paramName) {
  3127. free(param->paramName);
  3128. }
  3129. free(param);
  3130. }
  3131. static void delete_header_int(sdlSoapBindingFunctionHeaderPtr hdr)
  3132. {
  3133. if (hdr->name) {
  3134. efree(hdr->name);
  3135. }
  3136. if (hdr->ns) {
  3137. efree(hdr->ns);
  3138. }
  3139. if (hdr->headerfaults) {
  3140. zend_hash_destroy(hdr->headerfaults);
  3141. efree(hdr->headerfaults);
  3142. }
  3143. efree(hdr);
  3144. }
  3145. static void delete_header(zval *zv)
  3146. {
  3147. delete_header_int(Z_PTR_P(zv));
  3148. }
  3149. static void delete_header_persistent(zval *zv)
  3150. {
  3151. sdlSoapBindingFunctionHeaderPtr hdr = Z_PTR_P(zv);
  3152. if (hdr->name) {
  3153. free(hdr->name);
  3154. }
  3155. if (hdr->ns) {
  3156. free(hdr->ns);
  3157. }
  3158. if (hdr->headerfaults) {
  3159. zend_hash_destroy(hdr->headerfaults);
  3160. free(hdr->headerfaults);
  3161. }
  3162. free(hdr);
  3163. }
  3164. static void delete_fault(zval *zv)
  3165. {
  3166. sdlFaultPtr fault = Z_PTR_P(zv);
  3167. if (fault->name) {
  3168. efree(fault->name);
  3169. }
  3170. if (fault->details) {
  3171. zend_hash_destroy(fault->details);
  3172. efree(fault->details);
  3173. }
  3174. if (fault->bindingAttributes) {
  3175. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3176. if (binding->ns) {
  3177. efree(binding->ns);
  3178. }
  3179. efree(fault->bindingAttributes);
  3180. }
  3181. efree(fault);
  3182. }
  3183. static void delete_fault_persistent(zval *zv)
  3184. {
  3185. sdlFaultPtr fault = Z_PTR_P(zv);
  3186. if (fault->name) {
  3187. free(fault->name);
  3188. }
  3189. if (fault->details) {
  3190. zend_hash_destroy(fault->details);
  3191. free(fault->details);
  3192. }
  3193. if (fault->bindingAttributes) {
  3194. sdlSoapBindingFunctionFaultPtr binding = (sdlSoapBindingFunctionFaultPtr)fault->bindingAttributes;
  3195. if (binding->ns) {
  3196. free(binding->ns);
  3197. }
  3198. free(fault->bindingAttributes);
  3199. }
  3200. free(fault);
  3201. }
  3202. static void delete_document(zval *zv)
  3203. {
  3204. xmlDocPtr doc = Z_PTR_P(zv);
  3205. xmlFreeDoc(doc);
  3206. }