simplexml.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Sterling Hughes <sterling@php.net> |
  14. | Marcus Boerger <helly@php.net> |
  15. | Rob Richards <rrichards@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #if defined(HAVE_LIBXML) && defined(HAVE_SIMPLEXML)
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "ext/standard/php_string.h"
  26. #include "php_simplexml.h"
  27. #include "php_simplexml_exports.h"
  28. #include "simplexml_arginfo.h"
  29. #include "zend_exceptions.h"
  30. #include "zend_interfaces.h"
  31. #include "ext/spl/spl_iterators.h"
  32. zend_class_entry *sxe_class_entry = NULL;
  33. PHP_SXE_API zend_class_entry *ce_SimpleXMLIterator;
  34. PHP_SXE_API zend_class_entry *ce_SimpleXMLElement;
  35. PHP_SXE_API zend_class_entry *sxe_get_element_class_entry(void) /* {{{ */
  36. {
  37. return sxe_class_entry;
  38. }
  39. /* }}} */
  40. static php_sxe_object* php_sxe_object_new(zend_class_entry *ce, zend_function *fptr_count);
  41. static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data);
  42. static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data);
  43. static void php_sxe_iterator_dtor(zend_object_iterator *iter);
  44. static int php_sxe_iterator_valid(zend_object_iterator *iter);
  45. static zval *php_sxe_iterator_current_data(zend_object_iterator *iter);
  46. static void php_sxe_iterator_current_key(zend_object_iterator *iter, zval *key);
  47. static void php_sxe_iterator_move_forward(zend_object_iterator *iter);
  48. static void php_sxe_iterator_rewind(zend_object_iterator *iter);
  49. static int sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int type);
  50. /* {{{ _node_as_zval() */
  51. static void _node_as_zval(php_sxe_object *sxe, xmlNodePtr node, zval *value, SXE_ITER itertype, char *name, const xmlChar *nsprefix, int isprefix)
  52. {
  53. php_sxe_object *subnode;
  54. subnode = php_sxe_object_new(sxe->zo.ce, sxe->fptr_count);
  55. subnode->document = sxe->document;
  56. subnode->document->refcount++;
  57. subnode->iter.type = itertype;
  58. if (name) {
  59. subnode->iter.name = (xmlChar*)estrdup(name);
  60. }
  61. if (nsprefix && *nsprefix) {
  62. subnode->iter.nsprefix = (xmlChar*)estrdup((char*)nsprefix);
  63. subnode->iter.isprefix = isprefix;
  64. }
  65. php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL);
  66. ZVAL_OBJ(value, &subnode->zo);
  67. }
  68. /* }}} */
  69. static xmlNodePtr php_sxe_get_first_node(php_sxe_object *sxe, xmlNodePtr node) /* {{{ */
  70. {
  71. php_sxe_object *intern;
  72. xmlNodePtr retnode = NULL;
  73. if (sxe && sxe->iter.type != SXE_ITER_NONE) {
  74. php_sxe_reset_iterator(sxe, 1);
  75. if (!Z_ISUNDEF(sxe->iter.data)) {
  76. intern = Z_SXEOBJ_P(&sxe->iter.data);
  77. GET_NODE(intern, retnode)
  78. }
  79. return retnode;
  80. } else {
  81. return node;
  82. }
  83. }
  84. /* }}} */
  85. static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name, int prefix) /* {{{ */
  86. {
  87. if (name == NULL && (node->ns == NULL || node->ns->prefix == NULL)) {
  88. return 1;
  89. }
  90. if (node->ns && !xmlStrcmp(prefix ? node->ns->prefix : node->ns->href, name)) {
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. /* }}} */
  96. static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, zend_long offset, xmlNodePtr node, zend_long *cnt) /* {{{ */
  97. {
  98. zend_long nodendx = 0;
  99. if (sxe->iter.type == SXE_ITER_NONE) {
  100. if (offset == 0) {
  101. if (cnt) {
  102. *cnt = 0;
  103. }
  104. return node;
  105. } else {
  106. return NULL;
  107. }
  108. }
  109. while (node && nodendx <= offset) {
  110. SKIP_TEXT(node)
  111. if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  112. if (sxe->iter.type == SXE_ITER_CHILD || (
  113. sxe->iter.type == SXE_ITER_ELEMENT && !xmlStrcmp(node->name, sxe->iter.name))) {
  114. if (nodendx == offset) {
  115. break;
  116. }
  117. nodendx++;
  118. }
  119. }
  120. next_iter:
  121. node = node->next;
  122. }
  123. if (cnt) {
  124. *cnt = nodendx;
  125. }
  126. return node;
  127. }
  128. /* }}} */
  129. static xmlNodePtr sxe_find_element_by_name(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name) /* {{{ */
  130. {
  131. while (node) {
  132. SKIP_TEXT(node)
  133. if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  134. if (!xmlStrcmp(node->name, name)) {
  135. return node;
  136. }
  137. }
  138. next_iter:
  139. node = node->next;
  140. }
  141. return NULL;
  142. } /* }}} */
  143. static xmlNodePtr sxe_get_element_by_name(php_sxe_object *sxe, xmlNodePtr node, char **name, SXE_ITER *type) /* {{{ */
  144. {
  145. int orgtype;
  146. xmlNodePtr orgnode = node;
  147. xmlNodePtr retnode = NULL;
  148. if (sxe->iter.type != SXE_ITER_ATTRLIST)
  149. {
  150. orgtype = sxe->iter.type;
  151. if (sxe->iter.type == SXE_ITER_NONE) {
  152. sxe->iter.type = SXE_ITER_CHILD;
  153. }
  154. node = php_sxe_get_first_node(sxe, node);
  155. sxe->iter.type = orgtype;
  156. }
  157. if (sxe->iter.type == SXE_ITER_ELEMENT) {
  158. orgnode = sxe_find_element_by_name(sxe, node, sxe->iter.name);
  159. if (!orgnode) {
  160. return NULL;
  161. }
  162. node = orgnode->children;
  163. }
  164. while (node) {
  165. SKIP_TEXT(node)
  166. if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  167. if (!xmlStrcmp(node->name, (xmlChar *)*name)) {
  168. if (1||retnode)
  169. {
  170. *type = SXE_ITER_ELEMENT;
  171. return orgnode;
  172. }
  173. retnode = node;
  174. }
  175. }
  176. next_iter:
  177. node = node->next;
  178. }
  179. if (retnode)
  180. {
  181. *type = SXE_ITER_NONE;
  182. *name = NULL;
  183. return retnode;
  184. }
  185. return NULL;
  186. }
  187. /* }}} */
  188. /* {{{ sxe_prop_dim_read() */
  189. static zval *sxe_prop_dim_read(zend_object *object, zval *member, bool elements, bool attribs, int type, zval *rv)
  190. {
  191. php_sxe_object *sxe;
  192. char *name;
  193. xmlNodePtr node;
  194. xmlAttrPtr attr = NULL;
  195. zval tmp_zv;
  196. int nodendx = 0;
  197. int test = 0;
  198. sxe = php_sxe_fetch_object(object);
  199. if (!member) {
  200. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  201. /* This happens when the user did: $sxe[]->foo = $value */
  202. zend_throw_error(NULL, "Cannot create unnamed attribute");
  203. return &EG(uninitialized_zval);
  204. }
  205. goto long_dim;
  206. } else {
  207. ZVAL_DEREF(member);
  208. if (Z_TYPE_P(member) == IS_LONG) {
  209. if (sxe->iter.type != SXE_ITER_ATTRLIST) {
  210. long_dim:
  211. attribs = 0;
  212. elements = 1;
  213. }
  214. name = NULL;
  215. } else {
  216. if (Z_TYPE_P(member) != IS_STRING) {
  217. zend_string *str = zval_try_get_string_func(member);
  218. if (UNEXPECTED(!str)) {
  219. return &EG(uninitialized_zval);
  220. }
  221. ZVAL_STR(&tmp_zv, str);
  222. member = &tmp_zv;
  223. }
  224. name = Z_STRVAL_P(member);
  225. }
  226. }
  227. GET_NODE(sxe, node);
  228. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  229. attribs = 1;
  230. elements = 0;
  231. node = php_sxe_get_first_node(sxe, node);
  232. attr = (xmlAttrPtr)node;
  233. test = sxe->iter.name != NULL;
  234. } else if (sxe->iter.type != SXE_ITER_CHILD) {
  235. node = php_sxe_get_first_node(sxe, node);
  236. attr = node ? node->properties : NULL;
  237. test = 0;
  238. if (!member && node && node->parent &&
  239. node->parent->type == XML_DOCUMENT_NODE) {
  240. /* This happens when the user did: $sxe[]->foo = $value */
  241. zend_throw_error(NULL, "Cannot create unnamed attribute");
  242. return &EG(uninitialized_zval);
  243. }
  244. }
  245. ZVAL_UNDEF(rv);
  246. if (node) {
  247. if (attribs) {
  248. if (Z_TYPE_P(member) != IS_LONG || sxe->iter.type == SXE_ITER_ATTRLIST) {
  249. if (Z_TYPE_P(member) == IS_LONG) {
  250. while (attr && nodendx <= Z_LVAL_P(member)) {
  251. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  252. if (nodendx == Z_LVAL_P(member)) {
  253. _node_as_zval(sxe, (xmlNodePtr) attr, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
  254. break;
  255. }
  256. nodendx++;
  257. }
  258. attr = attr->next;
  259. }
  260. } else {
  261. while (attr) {
  262. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  263. _node_as_zval(sxe, (xmlNodePtr) attr, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
  264. break;
  265. }
  266. attr = attr->next;
  267. }
  268. }
  269. }
  270. }
  271. if (elements) {
  272. if (!sxe->node) {
  273. php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, node, NULL);
  274. }
  275. if (!member || Z_TYPE_P(member) == IS_LONG) {
  276. zend_long cnt = 0;
  277. xmlNodePtr mynode = node;
  278. if (sxe->iter.type == SXE_ITER_CHILD) {
  279. node = php_sxe_get_first_node(sxe, node);
  280. }
  281. if (sxe->iter.type == SXE_ITER_NONE) {
  282. if (member && Z_LVAL_P(member) > 0) {
  283. php_error_docref(NULL, E_WARNING, "Cannot add element %s number " ZEND_LONG_FMT " when only 0 such elements exist", mynode->name, Z_LVAL_P(member));
  284. }
  285. } else if (member) {
  286. node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt);
  287. } else {
  288. node = NULL;
  289. }
  290. if (node) {
  291. _node_as_zval(sxe, node, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
  292. } else if (type == BP_VAR_W || type == BP_VAR_RW) {
  293. if (member && cnt < Z_LVAL_P(member)) {
  294. php_error_docref(NULL, E_WARNING, "Cannot add element %s number " ZEND_LONG_FMT " when only " ZEND_LONG_FMT " such elements exist", mynode->name, Z_LVAL_P(member), cnt);
  295. }
  296. node = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, NULL);
  297. _node_as_zval(sxe, node, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
  298. }
  299. } else {
  300. /* In BP_VAR_IS mode only return a proper node if it actually exists. */
  301. if (type != BP_VAR_IS || sxe_find_element_by_name(sxe, node->children, (xmlChar *) name)) {
  302. _node_as_zval(sxe, node, rv, SXE_ITER_ELEMENT, name, sxe->iter.nsprefix, sxe->iter.isprefix);
  303. }
  304. }
  305. }
  306. }
  307. if (member == &tmp_zv) {
  308. zval_ptr_dtor_str(&tmp_zv);
  309. }
  310. if (Z_ISUNDEF_P(rv)) {
  311. ZVAL_NULL(rv);
  312. }
  313. return rv;
  314. }
  315. /* }}} */
  316. /* {{{ sxe_property_read() */
  317. static zval *sxe_property_read(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
  318. {
  319. zval member;
  320. ZVAL_STR(&member, name);
  321. return sxe_prop_dim_read(object, &member, 1, 0, type, rv);
  322. }
  323. /* }}} */
  324. /* {{{ sxe_dimension_read() */
  325. static zval *sxe_dimension_read(zend_object *object, zval *offset, int type, zval *rv)
  326. {
  327. return sxe_prop_dim_read(object, offset, 0, 1, type, rv);
  328. }
  329. /* }}} */
  330. /* {{{ change_node_zval() */
  331. static void change_node_zval(xmlNodePtr node, zend_string *value)
  332. {
  333. xmlChar *buffer = xmlEncodeEntitiesReentrant(node->doc, (xmlChar *)ZSTR_VAL(value));
  334. /* check for NULL buffer in case of memory error in xmlEncodeEntitiesReentrant */
  335. if (buffer) {
  336. xmlNodeSetContent(node, buffer);
  337. xmlFree(buffer);
  338. }
  339. }
  340. /* }}} */
  341. /* {{{ sxe_property_write() */
  342. static zval *sxe_prop_dim_write(zend_object *object, zval *member, zval *value, bool elements, bool attribs, xmlNodePtr *pnewnode)
  343. {
  344. php_sxe_object *sxe;
  345. xmlNodePtr node;
  346. xmlNodePtr newnode = NULL;
  347. xmlNodePtr mynode;
  348. xmlNodePtr tempnode;
  349. xmlAttrPtr attr = NULL;
  350. int counter = 0;
  351. int is_attr = 0;
  352. int nodendx = 0;
  353. int test = 0;
  354. zend_long cnt = 0;
  355. zval tmp_zv;
  356. zend_string *trim_str;
  357. zend_string *value_str = NULL;
  358. sxe = php_sxe_fetch_object(object);
  359. if (!member) {
  360. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  361. /* This happens when the user did: $sxe[] = $value
  362. * and could also be E_PARSE, but we use this only during parsing
  363. * and this is during runtime.
  364. */
  365. zend_throw_error(NULL, "Cannot append to an attribute list");
  366. return &EG(error_zval);
  367. }
  368. goto long_dim;
  369. } else {
  370. ZVAL_DEREF(member);
  371. if (Z_TYPE_P(member) == IS_LONG) {
  372. if (sxe->iter.type != SXE_ITER_ATTRLIST) {
  373. long_dim:
  374. attribs = 0;
  375. elements = 1;
  376. }
  377. } else {
  378. if (Z_TYPE_P(member) != IS_STRING) {
  379. trim_str = zval_try_get_string_func(member);
  380. if (UNEXPECTED(!trim_str)) {
  381. return &EG(error_zval);
  382. }
  383. ZVAL_STR(&tmp_zv, php_trim(trim_str, NULL, 0, 3));
  384. zend_string_release_ex(trim_str, 0);
  385. member = &tmp_zv;
  386. }
  387. if (!Z_STRLEN_P(member)) {
  388. zend_value_error("Cannot create %s with an empty name", attribs ? "attribute" : "element");
  389. if (member == &tmp_zv) {
  390. zval_ptr_dtor_str(&tmp_zv);
  391. }
  392. return &EG(error_zval);
  393. }
  394. }
  395. }
  396. GET_NODE(sxe, node);
  397. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  398. attribs = 1;
  399. elements = 0;
  400. node = php_sxe_get_first_node(sxe, node);
  401. attr = (xmlAttrPtr)node;
  402. test = sxe->iter.name != NULL;
  403. } else if (sxe->iter.type != SXE_ITER_CHILD) {
  404. mynode = node;
  405. node = php_sxe_get_first_node(sxe, node);
  406. attr = node ? node->properties : NULL;
  407. test = 0;
  408. if (!member && node && node->parent &&
  409. node->parent->type == XML_DOCUMENT_NODE) {
  410. /* This happens when the user did: $sxe[] = $value
  411. * and could also be E_PARSE, but we use this only during parsing
  412. * and this is during runtime.
  413. */
  414. zend_value_error("Cannot append to an attribute list");
  415. return &EG(error_zval);
  416. }
  417. if (attribs && !node && sxe->iter.type == SXE_ITER_ELEMENT) {
  418. node = xmlNewChild(mynode, mynode->ns, sxe->iter.name, NULL);
  419. attr = node->properties;
  420. }
  421. }
  422. mynode = node;
  423. if (value) {
  424. switch (Z_TYPE_P(value)) {
  425. case IS_LONG:
  426. case IS_FALSE:
  427. case IS_TRUE:
  428. case IS_DOUBLE:
  429. case IS_NULL:
  430. case IS_STRING:
  431. value_str = zval_get_string(value);
  432. break;
  433. case IS_OBJECT:
  434. if (Z_OBJCE_P(value) == sxe_class_entry) {
  435. zval zval_copy;
  436. if (sxe_object_cast_ex(Z_OBJ_P(value), &zval_copy, IS_STRING) == FAILURE) {
  437. zend_throw_error(NULL, "Unable to cast node to string");
  438. return &EG(error_zval);
  439. }
  440. value_str = Z_STR(zval_copy);
  441. break;
  442. }
  443. ZEND_FALLTHROUGH;
  444. default:
  445. if (member == &tmp_zv) {
  446. zval_ptr_dtor_str(&tmp_zv);
  447. }
  448. zend_type_error("It's not possible to assign a complex type to %s, %s given", attribs ? "attributes" : "properties", zend_zval_type_name(value));
  449. return &EG(error_zval);
  450. }
  451. }
  452. if (node) {
  453. if (attribs) {
  454. if (Z_TYPE_P(member) == IS_LONG) {
  455. while (attr && nodendx <= Z_LVAL_P(member)) {
  456. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  457. if (nodendx == Z_LVAL_P(member)) {
  458. is_attr = 1;
  459. ++counter;
  460. break;
  461. }
  462. nodendx++;
  463. }
  464. attr = attr->next;
  465. }
  466. } else {
  467. while (attr) {
  468. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  469. is_attr = 1;
  470. ++counter;
  471. break;
  472. }
  473. attr = attr->next;
  474. }
  475. }
  476. }
  477. if (elements) {
  478. if (!member || Z_TYPE_P(member) == IS_LONG) {
  479. if (node->type == XML_ATTRIBUTE_NODE) {
  480. zend_throw_error(NULL, "Cannot create duplicate attribute");
  481. if (value_str) {
  482. zend_string_release(value_str);
  483. }
  484. return &EG(error_zval);
  485. }
  486. if (sxe->iter.type == SXE_ITER_NONE) {
  487. newnode = node;
  488. ++counter;
  489. if (member && Z_LVAL_P(member) > 0) {
  490. php_error_docref(NULL, E_WARNING, "Cannot add element %s number " ZEND_LONG_FMT " when only 0 such elements exist", mynode->name, Z_LVAL_P(member));
  491. value = &EG(error_zval);
  492. }
  493. } else if (member) {
  494. newnode = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt);
  495. if (newnode) {
  496. ++counter;
  497. }
  498. }
  499. } else {
  500. node = node->children;
  501. while (node) {
  502. SKIP_TEXT(node);
  503. if (!xmlStrcmp(node->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  504. newnode = node;
  505. ++counter;
  506. }
  507. next_iter:
  508. node = node->next;
  509. }
  510. }
  511. }
  512. if (counter == 1) {
  513. if (is_attr) {
  514. newnode = (xmlNodePtr) attr;
  515. }
  516. if (value_str) {
  517. while ((tempnode = (xmlNodePtr) newnode->children)) {
  518. xmlUnlinkNode(tempnode);
  519. php_libxml_node_free_resource((xmlNodePtr) tempnode);
  520. }
  521. change_node_zval(newnode, value_str);
  522. }
  523. } else if (counter > 1) {
  524. php_error_docref(NULL, E_WARNING, "Cannot assign to an array of nodes (duplicate subnodes or attr detected)");
  525. value = &EG(error_zval);
  526. } else if (elements) {
  527. if (!node) {
  528. if (!member || Z_TYPE_P(member) == IS_LONG) {
  529. newnode = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, value_str ? (xmlChar *)ZSTR_VAL(value_str) : NULL);
  530. } else {
  531. newnode = xmlNewTextChild(mynode, mynode->ns, (xmlChar *)Z_STRVAL_P(member), value_str ? (xmlChar *)ZSTR_VAL(value_str) : NULL);
  532. }
  533. } else if (!member || Z_TYPE_P(member) == IS_LONG) {
  534. if (member && cnt < Z_LVAL_P(member)) {
  535. php_error_docref(NULL, E_WARNING, "Cannot add element %s number " ZEND_LONG_FMT " when only " ZEND_LONG_FMT " such elements exist", mynode->name, Z_LVAL_P(member), cnt);
  536. }
  537. newnode = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, value_str ? (xmlChar *)ZSTR_VAL(value_str) : NULL);
  538. }
  539. } else if (attribs) {
  540. if (Z_TYPE_P(member) == IS_LONG) {
  541. php_error_docref(NULL, E_WARNING, "Cannot change attribute number " ZEND_LONG_FMT " when only %d attributes exist", Z_LVAL_P(member), nodendx);
  542. } else {
  543. newnode = (xmlNodePtr)xmlNewProp(node, (xmlChar *)Z_STRVAL_P(member), value_str ? (xmlChar *)ZSTR_VAL(value_str) : NULL);
  544. }
  545. }
  546. }
  547. if (member == &tmp_zv) {
  548. zval_ptr_dtor_str(&tmp_zv);
  549. }
  550. if (pnewnode) {
  551. *pnewnode = newnode;
  552. }
  553. if (value_str) {
  554. zend_string_release(value_str);
  555. }
  556. return value;
  557. }
  558. /* }}} */
  559. /* {{{ sxe_property_write() */
  560. static zval *sxe_property_write(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  561. {
  562. zval member;
  563. ZVAL_STR(&member, name);
  564. zval *retval = sxe_prop_dim_write(object, &member, value, 1, 0, NULL);
  565. return retval == &EG(error_zval) ? &EG(uninitialized_zval) : retval;
  566. }
  567. /* }}} */
  568. /* {{{ sxe_dimension_write() */
  569. static void sxe_dimension_write(zend_object *object, zval *offset, zval *value)
  570. {
  571. sxe_prop_dim_write(object, offset, value, 0, 1, NULL);
  572. }
  573. /* }}} */
  574. static zval *sxe_property_get_adr(zend_object *object, zend_string *zname, int fetch_type, void **cache_slot) /* {{{ */
  575. {
  576. php_sxe_object *sxe;
  577. xmlNodePtr node;
  578. zval ret;
  579. char *name;
  580. SXE_ITER type;
  581. zval member;
  582. sxe = php_sxe_fetch_object(object);
  583. GET_NODE(sxe, node);
  584. name = ZSTR_VAL(zname);
  585. node = sxe_get_element_by_name(sxe, node, &name, &type);
  586. if (node) {
  587. return NULL;
  588. }
  589. ZVAL_STR(&member, zname);
  590. if (sxe_prop_dim_write(object, &member, NULL, 1, 0, &node) == &EG(error_zval)) {
  591. return &EG(error_zval);
  592. }
  593. type = SXE_ITER_NONE;
  594. name = NULL;
  595. _node_as_zval(sxe, node, &ret, type, name, sxe->iter.nsprefix, sxe->iter.isprefix);
  596. if (!Z_ISUNDEF(sxe->tmp)) {
  597. zval_ptr_dtor(&sxe->tmp);
  598. }
  599. ZVAL_COPY_VALUE(&sxe->tmp, &ret);
  600. return &sxe->tmp;
  601. }
  602. /* }}} */
  603. /* {{{ sxe_prop_dim_exists() */
  604. static int sxe_prop_dim_exists(zend_object *object, zval *member, int check_empty, bool elements, bool attribs)
  605. {
  606. php_sxe_object *sxe;
  607. xmlNodePtr node;
  608. xmlAttrPtr attr = NULL;
  609. int exists = 0;
  610. int test = 0;
  611. zval tmp_zv;
  612. if (Z_TYPE_P(member) != IS_STRING && Z_TYPE_P(member) != IS_LONG) {
  613. zend_string *str = zval_try_get_string_func(member);
  614. if (UNEXPECTED(!str)) {
  615. return 0;
  616. }
  617. ZVAL_STR(&tmp_zv, str);
  618. member = &tmp_zv;
  619. }
  620. sxe = php_sxe_fetch_object(object);
  621. GET_NODE(sxe, node);
  622. if (Z_TYPE_P(member) == IS_LONG) {
  623. if (sxe->iter.type != SXE_ITER_ATTRLIST) {
  624. attribs = 0;
  625. elements = 1;
  626. if (sxe->iter.type == SXE_ITER_CHILD) {
  627. node = php_sxe_get_first_node(sxe, node);
  628. }
  629. }
  630. }
  631. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  632. attribs = 1;
  633. elements = 0;
  634. node = php_sxe_get_first_node(sxe, node);
  635. attr = (xmlAttrPtr)node;
  636. test = sxe->iter.name != NULL;
  637. } else if (sxe->iter.type != SXE_ITER_CHILD) {
  638. node = php_sxe_get_first_node(sxe, node);
  639. attr = node ? node->properties : NULL;
  640. test = 0;
  641. }
  642. if (node) {
  643. if (attribs) {
  644. if (Z_TYPE_P(member) == IS_LONG) {
  645. int nodendx = 0;
  646. while (attr && nodendx <= Z_LVAL_P(member)) {
  647. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  648. if (nodendx == Z_LVAL_P(member)) {
  649. exists = 1;
  650. break;
  651. }
  652. nodendx++;
  653. }
  654. attr = attr->next;
  655. }
  656. } else {
  657. while (attr) {
  658. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  659. exists = 1;
  660. break;
  661. }
  662. attr = attr->next;
  663. }
  664. }
  665. if (exists && check_empty == 1 &&
  666. (!attr->children || !attr->children->content || !attr->children->content[0] || !xmlStrcmp(attr->children->content, (const xmlChar *) "0")) ) {
  667. /* Attribute with no content in it's text node */
  668. exists = 0;
  669. }
  670. }
  671. if (elements) {
  672. if (Z_TYPE_P(member) == IS_LONG) {
  673. if (sxe->iter.type == SXE_ITER_CHILD) {
  674. node = php_sxe_get_first_node(sxe, node);
  675. }
  676. node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL);
  677. } else {
  678. node = sxe_find_element_by_name(sxe, node->children, (xmlChar *)Z_STRVAL_P(member));
  679. }
  680. if (node) {
  681. exists = 1;
  682. if (check_empty == 1 &&
  683. (!node->children || (node->children->type == XML_TEXT_NODE && !node->children->next &&
  684. (!node->children->content || !node->children->content[0] || !xmlStrcmp(node->children->content, (const xmlChar *) "0")))) ) {
  685. exists = 0;
  686. }
  687. }
  688. }
  689. }
  690. if (member == &tmp_zv) {
  691. zval_ptr_dtor_str(&tmp_zv);
  692. }
  693. return exists;
  694. }
  695. /* }}} */
  696. /* {{{ sxe_property_exists() */
  697. static int sxe_property_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
  698. {
  699. zval member;
  700. ZVAL_STR(&member, name);
  701. return sxe_prop_dim_exists(object, &member, check_empty, 1, 0);
  702. }
  703. /* }}} */
  704. /* {{{ sxe_dimension_exists() */
  705. static int sxe_dimension_exists(zend_object *object, zval *member, int check_empty)
  706. {
  707. return sxe_prop_dim_exists(object, member, check_empty, 0, 1);
  708. }
  709. /* }}} */
  710. /* {{{ sxe_prop_dim_delete() */
  711. static void sxe_prop_dim_delete(zend_object *object, zval *member, bool elements, bool attribs)
  712. {
  713. php_sxe_object *sxe;
  714. xmlNodePtr node;
  715. xmlNodePtr nnext;
  716. xmlAttrPtr attr = NULL;
  717. xmlAttrPtr anext;
  718. zval tmp_zv;
  719. int test = 0;
  720. if (Z_TYPE_P(member) != IS_STRING && Z_TYPE_P(member) != IS_LONG) {
  721. zend_string *str = zval_try_get_string_func(member);
  722. if (UNEXPECTED(!str)) {
  723. return;
  724. }
  725. ZVAL_STR(&tmp_zv, str);
  726. member = &tmp_zv;
  727. }
  728. sxe = php_sxe_fetch_object(object);
  729. GET_NODE(sxe, node);
  730. if (Z_TYPE_P(member) == IS_LONG) {
  731. if (sxe->iter.type != SXE_ITER_ATTRLIST) {
  732. attribs = 0;
  733. elements = 1;
  734. if (sxe->iter.type == SXE_ITER_CHILD) {
  735. node = php_sxe_get_first_node(sxe, node);
  736. }
  737. }
  738. }
  739. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  740. attribs = 1;
  741. elements = 0;
  742. node = php_sxe_get_first_node(sxe, node);
  743. attr = (xmlAttrPtr)node;
  744. test = sxe->iter.name != NULL;
  745. } else if (sxe->iter.type != SXE_ITER_CHILD) {
  746. node = php_sxe_get_first_node(sxe, node);
  747. attr = node ? node->properties : NULL;
  748. test = 0;
  749. }
  750. if (node) {
  751. if (attribs) {
  752. if (Z_TYPE_P(member) == IS_LONG) {
  753. int nodendx = 0;
  754. while (attr && nodendx <= Z_LVAL_P(member)) {
  755. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  756. if (nodendx == Z_LVAL_P(member)) {
  757. xmlUnlinkNode((xmlNodePtr) attr);
  758. php_libxml_node_free_resource((xmlNodePtr) attr);
  759. break;
  760. }
  761. nodendx++;
  762. }
  763. attr = attr->next;
  764. }
  765. } else {
  766. while (attr) {
  767. anext = attr->next;
  768. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  769. xmlUnlinkNode((xmlNodePtr) attr);
  770. php_libxml_node_free_resource((xmlNodePtr) attr);
  771. break;
  772. }
  773. attr = anext;
  774. }
  775. }
  776. }
  777. if (elements) {
  778. if (Z_TYPE_P(member) == IS_LONG) {
  779. if (sxe->iter.type == SXE_ITER_CHILD) {
  780. node = php_sxe_get_first_node(sxe, node);
  781. }
  782. node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL);
  783. if (node) {
  784. xmlUnlinkNode(node);
  785. php_libxml_node_free_resource(node);
  786. }
  787. } else {
  788. node = node->children;
  789. while (node) {
  790. nnext = node->next;
  791. SKIP_TEXT(node);
  792. if (!xmlStrcmp(node->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  793. xmlUnlinkNode(node);
  794. php_libxml_node_free_resource(node);
  795. }
  796. next_iter:
  797. node = nnext;
  798. }
  799. }
  800. }
  801. }
  802. if (member == &tmp_zv) {
  803. zval_ptr_dtor_str(&tmp_zv);
  804. }
  805. }
  806. /* }}} */
  807. /* {{{ sxe_property_delete() */
  808. static void sxe_property_delete(zend_object *object, zend_string *name, void **cache_slot)
  809. {
  810. zval member;
  811. ZVAL_STR(&member, name);
  812. sxe_prop_dim_delete(object, &member, 1, 0);
  813. }
  814. /* }}} */
  815. /* {{{ sxe_dimension_unset() */
  816. static void sxe_dimension_delete(zend_object *object, zval *offset)
  817. {
  818. sxe_prop_dim_delete(object, offset, 0, 1);
  819. }
  820. /* }}} */
  821. static inline zend_string *sxe_xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) /* {{{ */
  822. {
  823. xmlChar *tmp = xmlNodeListGetString(doc, list, inLine);
  824. zend_string *res;
  825. if (tmp) {
  826. res = zend_string_init((char*)tmp, strlen((char *)tmp), 0);
  827. xmlFree(tmp);
  828. } else {
  829. res = ZSTR_EMPTY_ALLOC();
  830. }
  831. return res;
  832. }
  833. /* }}} */
  834. /* {{{ _get_base_node_value() */
  835. static void _get_base_node_value(php_sxe_object *sxe_ref, xmlNodePtr node, zval *value, xmlChar *nsprefix, int isprefix)
  836. {
  837. php_sxe_object *subnode;
  838. xmlChar *contents;
  839. if (node->children && node->children->type == XML_TEXT_NODE && !xmlIsBlankNode(node->children)) {
  840. contents = xmlNodeListGetString(node->doc, node->children, 1);
  841. if (contents) {
  842. ZVAL_STRING(value, (char *)contents);
  843. xmlFree(contents);
  844. }
  845. } else {
  846. subnode = php_sxe_object_new(sxe_ref->zo.ce, sxe_ref->fptr_count);
  847. subnode->document = sxe_ref->document;
  848. subnode->document->refcount++;
  849. if (nsprefix && *nsprefix) {
  850. subnode->iter.nsprefix = (xmlChar*)estrdup((char *)nsprefix);
  851. subnode->iter.isprefix = isprefix;
  852. }
  853. php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL);
  854. ZVAL_OBJ(value, &subnode->zo);
  855. /*zval_add_ref(value);*/
  856. }
  857. }
  858. /* }}} */
  859. static void sxe_properties_add(HashTable *rv, char *name, int namelen, zval *value) /* {{{ */
  860. {
  861. zend_string *key;
  862. zval *data_ptr;
  863. zval newptr;
  864. key = zend_string_init(name, namelen, 0);
  865. if ((data_ptr = zend_hash_find(rv, key)) != NULL) {
  866. if (Z_TYPE_P(data_ptr) == IS_ARRAY) {
  867. zend_hash_next_index_insert_new(Z_ARRVAL_P(data_ptr), value);
  868. } else {
  869. array_init(&newptr);
  870. zend_hash_next_index_insert_new(Z_ARRVAL(newptr), data_ptr);
  871. zend_hash_next_index_insert_new(Z_ARRVAL(newptr), value);
  872. ZVAL_ARR(data_ptr, Z_ARR(newptr));
  873. }
  874. } else {
  875. zend_hash_add_new(rv, key, value);
  876. }
  877. zend_string_release_ex(key, 0);
  878. }
  879. /* }}} */
  880. static int sxe_prop_is_empty(zend_object *object) /* {{{ */
  881. {
  882. php_sxe_object *sxe;
  883. xmlNodePtr node;
  884. xmlAttrPtr attr;
  885. zval iter_data;
  886. int test;
  887. int is_empty;
  888. int use_iter = 0;
  889. sxe = php_sxe_fetch_object(object);
  890. GET_NODE(sxe, node);
  891. if (!node) {
  892. return 1;
  893. }
  894. if (sxe->iter.type == SXE_ITER_ELEMENT) {
  895. node = php_sxe_get_first_node(sxe, node);
  896. }
  897. if (!node || node->type != XML_ENTITY_DECL) {
  898. attr = node ? (xmlAttrPtr)node->properties : NULL;
  899. test = sxe->iter.name && sxe->iter.type == SXE_ITER_ATTRLIST;
  900. while (attr) {
  901. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr)attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  902. return 0;
  903. }
  904. attr = attr->next;
  905. }
  906. }
  907. GET_NODE(sxe, node);
  908. node = php_sxe_get_first_node(sxe, node);
  909. is_empty = 1;
  910. ZVAL_UNDEF(&iter_data);
  911. if (node && sxe->iter.type != SXE_ITER_ATTRLIST) {
  912. if (node->type == XML_ATTRIBUTE_NODE) {
  913. return 0;
  914. } else if (sxe->iter.type != SXE_ITER_CHILD) {
  915. if (sxe->iter.type == SXE_ITER_NONE || !node->children || !node->parent || node->children->next || node->children->children || node->parent->children == node->parent->last) {
  916. node = node->children;
  917. } else {
  918. ZVAL_COPY_VALUE(&iter_data, &sxe->iter.data);
  919. ZVAL_UNDEF(&sxe->iter.data);
  920. node = php_sxe_reset_iterator(sxe, 0);
  921. use_iter = 1;
  922. }
  923. }
  924. while (node) {
  925. if (node->children != NULL || node->prev != NULL || node->next != NULL) {
  926. SKIP_TEXT(node);
  927. } else {
  928. if (node->type == XML_TEXT_NODE) {
  929. const xmlChar *cur = node->content;
  930. if (*cur != 0) {
  931. is_empty = 0;
  932. break;
  933. }
  934. goto next_iter;
  935. }
  936. }
  937. if (node->type == XML_ELEMENT_NODE && (! match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix))) {
  938. goto next_iter;
  939. }
  940. if (!node->name) {
  941. goto next_iter;
  942. }
  943. is_empty = 0;
  944. break;
  945. next_iter:
  946. if (use_iter) {
  947. node = php_sxe_iterator_fetch(sxe, node->next, 0);
  948. } else {
  949. node = node->next;
  950. }
  951. }
  952. }
  953. if (use_iter) {
  954. if (!Z_ISUNDEF(sxe->iter.data)) {
  955. zval_ptr_dtor(&sxe->iter.data);
  956. }
  957. ZVAL_COPY_VALUE(&sxe->iter.data, &iter_data);
  958. }
  959. return is_empty;
  960. }
  961. /* }}} */
  962. static HashTable *sxe_get_prop_hash(zend_object *object, int is_debug) /* {{{ */
  963. {
  964. zval value;
  965. zval zattr;
  966. HashTable *rv;
  967. php_sxe_object *sxe;
  968. char *name;
  969. xmlNodePtr node;
  970. xmlAttrPtr attr;
  971. int namelen;
  972. int test;
  973. char use_iter;
  974. zval iter_data;
  975. use_iter = 0;
  976. sxe = php_sxe_fetch_object(object);
  977. if (is_debug) {
  978. rv = zend_new_array(0);
  979. } else if (sxe->properties) {
  980. zend_hash_clean(sxe->properties);
  981. rv = sxe->properties;
  982. } else {
  983. rv = zend_new_array(0);
  984. sxe->properties = rv;
  985. }
  986. GET_NODE(sxe, node);
  987. if (!node) {
  988. return rv;
  989. }
  990. if (is_debug || sxe->iter.type != SXE_ITER_CHILD) {
  991. if (sxe->iter.type == SXE_ITER_ELEMENT) {
  992. node = php_sxe_get_first_node(sxe, node);
  993. }
  994. if (!node || node->type != XML_ENTITY_DECL) {
  995. attr = node ? (xmlAttrPtr)node->properties : NULL;
  996. ZVAL_UNDEF(&zattr);
  997. test = sxe->iter.name && sxe->iter.type == SXE_ITER_ATTRLIST;
  998. while (attr) {
  999. if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr)attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
  1000. ZVAL_STR(&value, sxe_xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, attr->children, 1));
  1001. namelen = xmlStrlen(attr->name);
  1002. if (Z_ISUNDEF(zattr)) {
  1003. array_init(&zattr);
  1004. sxe_properties_add(rv, "@attributes", sizeof("@attributes") - 1, &zattr);
  1005. }
  1006. add_assoc_zval_ex(&zattr, (char*)attr->name, namelen, &value);
  1007. }
  1008. attr = attr->next;
  1009. }
  1010. }
  1011. }
  1012. GET_NODE(sxe, node);
  1013. node = php_sxe_get_first_node(sxe, node);
  1014. if (node && sxe->iter.type != SXE_ITER_ATTRLIST) {
  1015. if (node->type == XML_ATTRIBUTE_NODE) {
  1016. ZVAL_STR(&value, sxe_xmlNodeListGetString(node->doc, node->children, 1));
  1017. zend_hash_next_index_insert(rv, &value);
  1018. node = NULL;
  1019. } else if (sxe->iter.type != SXE_ITER_CHILD) {
  1020. if ( sxe->iter.type == SXE_ITER_NONE || !node->children || !node->parent || !node->next || node->children->next || node->children->children || node->parent->children == node->parent->last ) {
  1021. node = node->children;
  1022. } else {
  1023. ZVAL_COPY_VALUE(&iter_data, &sxe->iter.data);
  1024. ZVAL_UNDEF(&sxe->iter.data);
  1025. node = php_sxe_reset_iterator(sxe, 0);
  1026. use_iter = 1;
  1027. }
  1028. }
  1029. while (node) {
  1030. if (node->children != NULL || node->prev != NULL || node->next != NULL || xmlIsBlankNode(node)) {
  1031. SKIP_TEXT(node);
  1032. } else {
  1033. if (node->type == XML_TEXT_NODE) {
  1034. const xmlChar *cur = node->content;
  1035. if (*cur != 0) {
  1036. ZVAL_STR(&value, sxe_xmlNodeListGetString(node->doc, node, 1));
  1037. zend_hash_next_index_insert(rv, &value);
  1038. }
  1039. goto next_iter;
  1040. }
  1041. }
  1042. if (node->type == XML_ELEMENT_NODE && (! match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix))) {
  1043. goto next_iter;
  1044. }
  1045. name = (char *) node->name;
  1046. if (!name) {
  1047. goto next_iter;
  1048. } else {
  1049. namelen = xmlStrlen(node->name);
  1050. }
  1051. _get_base_node_value(sxe, node, &value, sxe->iter.nsprefix, sxe->iter.isprefix);
  1052. if ( use_iter ) {
  1053. zend_hash_next_index_insert(rv, &value);
  1054. } else {
  1055. sxe_properties_add(rv, name, namelen, &value);
  1056. }
  1057. next_iter:
  1058. if (use_iter) {
  1059. node = php_sxe_iterator_fetch(sxe, node->next, 0);
  1060. } else {
  1061. node = node->next;
  1062. }
  1063. }
  1064. }
  1065. if (use_iter) {
  1066. if (!Z_ISUNDEF(sxe->iter.data)) {
  1067. zval_ptr_dtor(&sxe->iter.data);
  1068. }
  1069. ZVAL_COPY_VALUE(&sxe->iter.data, &iter_data);
  1070. }
  1071. return rv;
  1072. }
  1073. /* }}} */
  1074. static HashTable *sxe_get_gc(zend_object *object, zval **table, int *n) /* {{{ */ {
  1075. php_sxe_object *sxe;
  1076. sxe = php_sxe_fetch_object(object);
  1077. *table = NULL;
  1078. *n = 0;
  1079. return sxe->properties;
  1080. }
  1081. /* }}} */
  1082. static HashTable *sxe_get_properties(zend_object *object) /* {{{ */
  1083. {
  1084. return sxe_get_prop_hash(object, 0);
  1085. }
  1086. /* }}} */
  1087. static HashTable * sxe_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
  1088. {
  1089. *is_temp = 1;
  1090. return sxe_get_prop_hash(object, 1);
  1091. }
  1092. /* }}} */
  1093. static int sxe_objects_compare(zval *object1, zval *object2) /* {{{ */
  1094. {
  1095. php_sxe_object *sxe1;
  1096. php_sxe_object *sxe2;
  1097. ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
  1098. sxe1 = Z_SXEOBJ_P(object1);
  1099. sxe2 = Z_SXEOBJ_P(object2);
  1100. if (sxe1->node != NULL && sxe2->node != NULL) {
  1101. /* Both nodes set: Only support equality comparison between nodes. */
  1102. if (sxe1->node == sxe2->node) {
  1103. return 0;
  1104. }
  1105. return ZEND_UNCOMPARABLE;
  1106. }
  1107. if (sxe1->node == NULL && sxe2->node == NULL) {
  1108. /* Both nodes not set: Only support equality comparison between documents. */
  1109. if (sxe1->document->ptr == sxe2->document->ptr) {
  1110. return 0;
  1111. }
  1112. return ZEND_UNCOMPARABLE;
  1113. }
  1114. /* Only one of the nodes set: Cannot compare. */
  1115. return ZEND_UNCOMPARABLE;
  1116. }
  1117. /* }}} */
  1118. /* {{{ Runs XPath query on the XML data */
  1119. PHP_METHOD(SimpleXMLElement, xpath)
  1120. {
  1121. php_sxe_object *sxe;
  1122. zval value;
  1123. char *query;
  1124. size_t query_len;
  1125. int i;
  1126. int nsnbr = 0;
  1127. xmlNsPtr *ns = NULL;
  1128. xmlXPathObjectPtr retval;
  1129. xmlNodeSetPtr result;
  1130. xmlNodePtr nodeptr;
  1131. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query, &query_len) == FAILURE) {
  1132. RETURN_THROWS();
  1133. }
  1134. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1135. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  1136. return; /* attributes don't have attributes */
  1137. }
  1138. GET_NODE(sxe, nodeptr);
  1139. nodeptr = php_sxe_get_first_node(sxe, nodeptr);
  1140. if (!nodeptr) {
  1141. return;
  1142. }
  1143. if (!sxe->xpath) {
  1144. sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr);
  1145. }
  1146. sxe->xpath->node = nodeptr;
  1147. ns = xmlGetNsList((xmlDocPtr) sxe->document->ptr, nodeptr);
  1148. if (ns != NULL) {
  1149. while (ns[nsnbr] != NULL) {
  1150. nsnbr++;
  1151. }
  1152. }
  1153. sxe->xpath->namespaces = ns;
  1154. sxe->xpath->nsNr = nsnbr;
  1155. retval = xmlXPathEval((xmlChar *)query, sxe->xpath);
  1156. if (ns != NULL) {
  1157. xmlFree(ns);
  1158. sxe->xpath->namespaces = NULL;
  1159. sxe->xpath->nsNr = 0;
  1160. }
  1161. if (!retval) {
  1162. RETURN_FALSE;
  1163. }
  1164. result = retval->nodesetval;
  1165. if (result != NULL) {
  1166. array_init(return_value);
  1167. for (i = 0; i < result->nodeNr; ++i) {
  1168. nodeptr = result->nodeTab[i];
  1169. if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE) {
  1170. /**
  1171. * Detect the case where the last selector is text(), simplexml
  1172. * always accesses the text() child by default, therefore we assign
  1173. * to the parent node.
  1174. */
  1175. if (nodeptr->type == XML_TEXT_NODE) {
  1176. _node_as_zval(sxe, nodeptr->parent, &value, SXE_ITER_NONE, NULL, NULL, 0);
  1177. } else if (nodeptr->type == XML_ATTRIBUTE_NODE) {
  1178. _node_as_zval(sxe, nodeptr->parent, &value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, nodeptr->ns ? (xmlChar *)nodeptr->ns->href : NULL, 0);
  1179. } else {
  1180. _node_as_zval(sxe, nodeptr, &value, SXE_ITER_NONE, NULL, NULL, 0);
  1181. }
  1182. add_next_index_zval(return_value, &value);
  1183. }
  1184. }
  1185. } else {
  1186. RETVAL_EMPTY_ARRAY();
  1187. }
  1188. xmlXPathFreeObject(retval);
  1189. }
  1190. /* }}} */
  1191. /* {{{ Creates a prefix/ns context for the next XPath query */
  1192. PHP_METHOD(SimpleXMLElement, registerXPathNamespace)
  1193. {
  1194. php_sxe_object *sxe;
  1195. size_t prefix_len, ns_uri_len;
  1196. char *prefix, *ns_uri;
  1197. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &prefix, &prefix_len, &ns_uri, &ns_uri_len) == FAILURE) {
  1198. RETURN_THROWS();
  1199. }
  1200. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1201. if (!sxe->document) {
  1202. zend_throw_error(NULL, "SimpleXMLElement is not properly initialized");
  1203. RETURN_THROWS();
  1204. }
  1205. if (!sxe->xpath) {
  1206. sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr);
  1207. }
  1208. if (xmlXPathRegisterNs(sxe->xpath, (xmlChar *)prefix, (xmlChar *)ns_uri) != 0) {
  1209. RETURN_FALSE;
  1210. }
  1211. RETURN_TRUE;
  1212. }
  1213. /* }}} */
  1214. /* {{{ Return a well-formed XML string based on SimpleXML element */
  1215. PHP_METHOD(SimpleXMLElement, asXML)
  1216. {
  1217. php_sxe_object *sxe;
  1218. xmlNodePtr node;
  1219. xmlOutputBufferPtr outbuf;
  1220. xmlChar *strval;
  1221. int strval_len;
  1222. char *filename = NULL;
  1223. size_t filename_len;
  1224. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|p!", &filename, &filename_len) == FAILURE) {
  1225. RETURN_THROWS();
  1226. }
  1227. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1228. GET_NODE(sxe, node);
  1229. node = php_sxe_get_first_node(sxe, node);
  1230. if (!node) {
  1231. RETURN_FALSE;
  1232. }
  1233. if (filename) {
  1234. if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
  1235. int bytes;
  1236. bytes = xmlSaveFile(filename, (xmlDocPtr) sxe->document->ptr);
  1237. if (bytes == -1) {
  1238. RETURN_FALSE;
  1239. } else {
  1240. RETURN_TRUE;
  1241. }
  1242. } else {
  1243. outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
  1244. if (outbuf == NULL) {
  1245. RETURN_FALSE;
  1246. }
  1247. xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL);
  1248. xmlOutputBufferClose(outbuf);
  1249. RETURN_TRUE;
  1250. }
  1251. }
  1252. if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
  1253. xmlDocDumpMemoryEnc((xmlDocPtr) sxe->document->ptr, &strval, &strval_len, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
  1254. if (!strval) {
  1255. RETVAL_FALSE;
  1256. } else {
  1257. RETVAL_STRINGL((char *)strval, strval_len);
  1258. }
  1259. xmlFree(strval);
  1260. } else {
  1261. char *return_content;
  1262. size_t return_len;
  1263. /* Should we be passing encoding information instead of NULL? */
  1264. outbuf = xmlAllocOutputBuffer(NULL);
  1265. if (outbuf == NULL) {
  1266. RETURN_FALSE;
  1267. }
  1268. xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
  1269. xmlOutputBufferFlush(outbuf);
  1270. #ifdef LIBXML2_NEW_BUFFER
  1271. return_content = (char *)xmlOutputBufferGetContent(outbuf);
  1272. return_len = xmlOutputBufferGetSize(outbuf);
  1273. #else
  1274. return_content = (char *)outbuf->buffer->content;
  1275. return_len = outbuf->buffer->use;
  1276. #endif
  1277. if (!return_content) {
  1278. RETVAL_FALSE;
  1279. } else {
  1280. RETVAL_STRINGL(return_content, return_len);
  1281. }
  1282. xmlOutputBufferClose(outbuf);
  1283. }
  1284. }
  1285. /* }}} */
  1286. #define SXE_NS_PREFIX(ns) (ns->prefix ? (char*)ns->prefix : "")
  1287. static inline void sxe_add_namespace_name(zval *return_value, xmlNsPtr ns) /* {{{ */
  1288. {
  1289. char *prefix = SXE_NS_PREFIX(ns);
  1290. zend_string *key = zend_string_init(prefix, strlen(prefix), 0);
  1291. zval zv;
  1292. if (!zend_hash_exists(Z_ARRVAL_P(return_value), key)) {
  1293. ZVAL_STRING(&zv, (char*)ns->href);
  1294. zend_hash_add_new(Z_ARRVAL_P(return_value), key, &zv);
  1295. }
  1296. zend_string_release_ex(key, 0);
  1297. }
  1298. /* }}} */
  1299. static void sxe_add_namespaces(php_sxe_object *sxe, xmlNodePtr node, bool recursive, zval *return_value) /* {{{ */
  1300. {
  1301. xmlAttrPtr attr;
  1302. if (node->ns) {
  1303. sxe_add_namespace_name(return_value, node->ns);
  1304. }
  1305. attr = node->properties;
  1306. while (attr) {
  1307. if (attr->ns) {
  1308. sxe_add_namespace_name(return_value, attr->ns);
  1309. }
  1310. attr = attr->next;
  1311. }
  1312. if (recursive) {
  1313. node = node->children;
  1314. while (node) {
  1315. if (node->type == XML_ELEMENT_NODE) {
  1316. sxe_add_namespaces(sxe, node, recursive, return_value);
  1317. }
  1318. node = node->next;
  1319. }
  1320. }
  1321. } /* }}} */
  1322. /* {{{ Return all namespaces in use */
  1323. PHP_METHOD(SimpleXMLElement, getNamespaces)
  1324. {
  1325. bool recursive = 0;
  1326. php_sxe_object *sxe;
  1327. xmlNodePtr node;
  1328. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &recursive) == FAILURE) {
  1329. RETURN_THROWS();
  1330. }
  1331. array_init(return_value);
  1332. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1333. GET_NODE(sxe, node);
  1334. node = php_sxe_get_first_node(sxe, node);
  1335. if (node) {
  1336. if (node->type == XML_ELEMENT_NODE) {
  1337. sxe_add_namespaces(sxe, node, recursive, return_value);
  1338. } else if (node->type == XML_ATTRIBUTE_NODE && node->ns) {
  1339. sxe_add_namespace_name(return_value, node->ns);
  1340. }
  1341. }
  1342. }
  1343. /* }}} */
  1344. static void sxe_add_registered_namespaces(php_sxe_object *sxe, xmlNodePtr node, bool recursive, zval *return_value) /* {{{ */
  1345. {
  1346. xmlNsPtr ns;
  1347. if (node->type == XML_ELEMENT_NODE) {
  1348. ns = node->nsDef;
  1349. while (ns != NULL) {
  1350. sxe_add_namespace_name(return_value, ns);
  1351. ns = ns->next;
  1352. }
  1353. if (recursive) {
  1354. node = node->children;
  1355. while (node) {
  1356. sxe_add_registered_namespaces(sxe, node, recursive, return_value);
  1357. node = node->next;
  1358. }
  1359. }
  1360. }
  1361. }
  1362. /* }}} */
  1363. /* {{{ Return all namespaces registered with document */
  1364. PHP_METHOD(SimpleXMLElement, getDocNamespaces)
  1365. {
  1366. bool recursive = 0, from_root = 1;
  1367. php_sxe_object *sxe;
  1368. xmlNodePtr node;
  1369. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|bb", &recursive, &from_root) == FAILURE) {
  1370. RETURN_THROWS();
  1371. }
  1372. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1373. if (from_root) {
  1374. if (!sxe->document) {
  1375. zend_throw_error(NULL, "SimpleXMLElement is not properly initialized");
  1376. RETURN_THROWS();
  1377. }
  1378. node = xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr);
  1379. } else {
  1380. GET_NODE(sxe, node);
  1381. }
  1382. if (node == NULL) {
  1383. RETURN_FALSE;
  1384. }
  1385. array_init(return_value);
  1386. sxe_add_registered_namespaces(sxe, node, recursive, return_value);
  1387. }
  1388. /* }}} */
  1389. /* {{{ Finds children of given node */
  1390. PHP_METHOD(SimpleXMLElement, children)
  1391. {
  1392. php_sxe_object *sxe;
  1393. char *nsprefix = NULL;
  1394. size_t nsprefix_len = 0;
  1395. xmlNodePtr node;
  1396. bool isprefix = 0;
  1397. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) {
  1398. RETURN_THROWS();
  1399. }
  1400. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1401. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  1402. return; /* attributes don't have attributes */
  1403. }
  1404. GET_NODE(sxe, node);
  1405. node = php_sxe_get_first_node(sxe, node);
  1406. if (!node) {
  1407. return;
  1408. }
  1409. _node_as_zval(sxe, node, return_value, SXE_ITER_CHILD, NULL, (xmlChar *)nsprefix, isprefix);
  1410. }
  1411. /* }}} */
  1412. /* {{{ Finds children of given node */
  1413. PHP_METHOD(SimpleXMLElement, getName)
  1414. {
  1415. php_sxe_object *sxe;
  1416. xmlNodePtr node;
  1417. int namelen;
  1418. if (zend_parse_parameters_none() == FAILURE) {
  1419. RETURN_THROWS();
  1420. }
  1421. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1422. GET_NODE(sxe, node);
  1423. node = php_sxe_get_first_node(sxe, node);
  1424. if (node) {
  1425. namelen = xmlStrlen(node->name);
  1426. RETURN_STRINGL((char*)node->name, namelen);
  1427. } else {
  1428. RETURN_EMPTY_STRING();
  1429. }
  1430. }
  1431. /* }}} */
  1432. /* {{{ Identifies an element's attributes */
  1433. PHP_METHOD(SimpleXMLElement, attributes)
  1434. {
  1435. php_sxe_object *sxe;
  1436. char *nsprefix = NULL;
  1437. size_t nsprefix_len = 0;
  1438. xmlNodePtr node;
  1439. bool isprefix = 0;
  1440. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) {
  1441. RETURN_THROWS();
  1442. }
  1443. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1444. GET_NODE(sxe, node);
  1445. node = php_sxe_get_first_node(sxe, node);
  1446. if (!node) {
  1447. return;
  1448. }
  1449. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  1450. return; /* attributes don't have attributes */
  1451. }
  1452. _node_as_zval(sxe, node, return_value, SXE_ITER_ATTRLIST, NULL, (xmlChar *)nsprefix, isprefix);
  1453. }
  1454. /* }}} */
  1455. /* {{{ Add Element with optional namespace information */
  1456. PHP_METHOD(SimpleXMLElement, addChild)
  1457. {
  1458. php_sxe_object *sxe;
  1459. char *qname, *value = NULL, *nsuri = NULL;
  1460. size_t qname_len, value_len = 0, nsuri_len = 0;
  1461. xmlNodePtr node, newnode;
  1462. xmlNsPtr nsptr = NULL;
  1463. xmlChar *localname, *prefix = NULL;
  1464. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!s!",
  1465. &qname, &qname_len, &value, &value_len, &nsuri, &nsuri_len) == FAILURE) {
  1466. RETURN_THROWS();
  1467. }
  1468. if (qname_len == 0) {
  1469. zend_argument_value_error(1, "cannot be empty");
  1470. RETURN_THROWS();
  1471. }
  1472. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1473. GET_NODE(sxe, node);
  1474. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  1475. php_error_docref(NULL, E_WARNING, "Cannot add element to attributes");
  1476. return;
  1477. }
  1478. node = php_sxe_get_first_node(sxe, node);
  1479. if (node == NULL) {
  1480. php_error_docref(NULL, E_WARNING, "Cannot add child. Parent is not a permanent member of the XML tree");
  1481. return;
  1482. }
  1483. localname = xmlSplitQName2((xmlChar *)qname, &prefix);
  1484. if (localname == NULL) {
  1485. localname = xmlStrdup((xmlChar *)qname);
  1486. }
  1487. newnode = xmlNewChild(node, NULL, localname, (xmlChar *)value);
  1488. if (nsuri != NULL) {
  1489. if (nsuri_len == 0) {
  1490. newnode->ns = NULL;
  1491. nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix);
  1492. } else {
  1493. nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri);
  1494. if (nsptr == NULL) {
  1495. nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix);
  1496. }
  1497. newnode->ns = nsptr;
  1498. }
  1499. }
  1500. _node_as_zval(sxe, newnode, return_value, SXE_ITER_NONE, (char *)localname, prefix, 0);
  1501. xmlFree(localname);
  1502. if (prefix != NULL) {
  1503. xmlFree(prefix);
  1504. }
  1505. }
  1506. /* }}} */
  1507. /* {{{ Add Attribute with optional namespace information */
  1508. PHP_METHOD(SimpleXMLElement, addAttribute)
  1509. {
  1510. php_sxe_object *sxe;
  1511. char *qname, *value = NULL, *nsuri = NULL;
  1512. size_t qname_len, value_len = 0, nsuri_len = 0;
  1513. xmlNodePtr node;
  1514. xmlAttrPtr attrp = NULL;
  1515. xmlNsPtr nsptr = NULL;
  1516. xmlChar *localname, *prefix = NULL;
  1517. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|s!",
  1518. &qname, &qname_len, &value, &value_len, &nsuri, &nsuri_len) == FAILURE) {
  1519. RETURN_THROWS();
  1520. }
  1521. if (qname_len == 0) {
  1522. zend_argument_value_error(1, "cannot be empty");
  1523. RETURN_THROWS();
  1524. }
  1525. sxe = Z_SXEOBJ_P(ZEND_THIS);
  1526. GET_NODE(sxe, node);
  1527. node = php_sxe_get_first_node(sxe, node);
  1528. if (node && node->type != XML_ELEMENT_NODE) {
  1529. node = node->parent;
  1530. }
  1531. if (node == NULL) {
  1532. php_error_docref(NULL, E_WARNING, "Unable to locate parent Element");
  1533. return;
  1534. }
  1535. localname = xmlSplitQName2((xmlChar *)qname, &prefix);
  1536. if (localname == NULL) {
  1537. if (nsuri_len > 0) {
  1538. if (prefix != NULL) {
  1539. xmlFree(prefix);
  1540. }
  1541. php_error_docref(NULL, E_WARNING, "Attribute requires prefix for namespace");
  1542. return;
  1543. }
  1544. localname = xmlStrdup((xmlChar *)qname);
  1545. }
  1546. attrp = xmlHasNsProp(node, localname, (xmlChar *)nsuri);
  1547. if (attrp != NULL && attrp->type != XML_ATTRIBUTE_DECL) {
  1548. xmlFree(localname);
  1549. if (prefix != NULL) {
  1550. xmlFree(prefix);
  1551. }
  1552. php_error_docref(NULL, E_WARNING, "Attribute already exists");
  1553. return;
  1554. }
  1555. if (nsuri != NULL) {
  1556. nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri);
  1557. if (nsptr == NULL) {
  1558. nsptr = xmlNewNs(node, (xmlChar *)nsuri, prefix);
  1559. }
  1560. }
  1561. attrp = xmlNewNsProp(node, nsptr, localname, (xmlChar *)value);
  1562. xmlFree(localname);
  1563. if (prefix != NULL) {
  1564. xmlFree(prefix);
  1565. }
  1566. }
  1567. /* }}} */
  1568. /* {{{ cast_object() */
  1569. static int cast_object(zval *object, int type, char *contents)
  1570. {
  1571. if (contents) {
  1572. ZVAL_STRINGL(object, contents, strlen(contents));
  1573. } else {
  1574. ZVAL_NULL(object);
  1575. }
  1576. switch (type) {
  1577. case IS_STRING:
  1578. convert_to_string(object);
  1579. break;
  1580. case _IS_BOOL:
  1581. convert_to_boolean(object);
  1582. break;
  1583. case IS_LONG:
  1584. convert_to_long(object);
  1585. break;
  1586. case IS_DOUBLE:
  1587. convert_to_double(object);
  1588. break;
  1589. case _IS_NUMBER:
  1590. convert_scalar_to_number(object);
  1591. break;
  1592. default:
  1593. return FAILURE;
  1594. }
  1595. return SUCCESS;
  1596. }
  1597. /* }}} */
  1598. /* {{{ sxe_object_cast() */
  1599. static int sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int type)
  1600. {
  1601. php_sxe_object *sxe;
  1602. xmlChar *contents = NULL;
  1603. xmlNodePtr node;
  1604. int rv;
  1605. sxe = php_sxe_fetch_object(readobj);
  1606. if (type == _IS_BOOL) {
  1607. node = php_sxe_get_first_node(sxe, NULL);
  1608. if (node) {
  1609. ZVAL_TRUE(writeobj);
  1610. } else {
  1611. ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
  1612. }
  1613. return SUCCESS;
  1614. }
  1615. if (sxe->iter.type != SXE_ITER_NONE) {
  1616. node = php_sxe_get_first_node(sxe, NULL);
  1617. if (node) {
  1618. contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, node->children, 1);
  1619. }
  1620. } else {
  1621. if (!sxe->node) {
  1622. if (sxe->document) {
  1623. php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement((xmlDocPtr) sxe->document->ptr), NULL);
  1624. }
  1625. }
  1626. if (sxe->node && sxe->node->node) {
  1627. if (sxe->node->node->children) {
  1628. contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, sxe->node->node->children, 1);
  1629. }
  1630. }
  1631. }
  1632. rv = cast_object(writeobj, type, (char *)contents);
  1633. if (contents) {
  1634. xmlFree(contents);
  1635. }
  1636. return rv;
  1637. }
  1638. /* }}} */
  1639. /* {{{ Variant of sxe_object_cast_ex that handles overwritten __toString() method */
  1640. static int sxe_object_cast(zend_object *readobj, zval *writeobj, int type)
  1641. {
  1642. if (type == IS_STRING
  1643. && zend_std_cast_object_tostring(readobj, writeobj, IS_STRING) == SUCCESS
  1644. ) {
  1645. return SUCCESS;
  1646. }
  1647. return sxe_object_cast_ex(readobj, writeobj, type);
  1648. }
  1649. /* }}} */
  1650. /* {{{ Returns the string content */
  1651. PHP_METHOD(SimpleXMLElement, __toString)
  1652. {
  1653. if (zend_parse_parameters_none() == FAILURE) {
  1654. RETURN_THROWS();
  1655. }
  1656. if (sxe_object_cast_ex(Z_OBJ_P(ZEND_THIS), return_value, IS_STRING) != SUCCESS) {
  1657. zval_ptr_dtor(return_value);
  1658. RETURN_EMPTY_STRING();
  1659. }
  1660. }
  1661. /* }}} */
  1662. static int php_sxe_count_elements_helper(php_sxe_object *sxe, zend_long *count) /* {{{ */
  1663. {
  1664. xmlNodePtr node;
  1665. zval data;
  1666. *count = 0;
  1667. ZVAL_COPY_VALUE(&data, &sxe->iter.data);
  1668. ZVAL_UNDEF(&sxe->iter.data);
  1669. node = php_sxe_reset_iterator(sxe, 0);
  1670. while (node)
  1671. {
  1672. (*count)++;
  1673. node = php_sxe_iterator_fetch(sxe, node->next, 0);
  1674. }
  1675. if (!Z_ISUNDEF(sxe->iter.data)) {
  1676. zval_ptr_dtor(&sxe->iter.data);
  1677. }
  1678. ZVAL_COPY_VALUE(&sxe->iter.data, &data);
  1679. return SUCCESS;
  1680. }
  1681. /* }}} */
  1682. static int sxe_count_elements(zend_object *object, zend_long *count) /* {{{ */
  1683. {
  1684. php_sxe_object *intern;
  1685. intern = php_sxe_fetch_object(object);
  1686. if (intern->fptr_count) {
  1687. zval rv;
  1688. zend_call_method_with_0_params(object, intern->zo.ce, &intern->fptr_count, "count", &rv);
  1689. if (!Z_ISUNDEF(rv)) {
  1690. *count = zval_get_long(&rv);
  1691. zval_ptr_dtor(&rv);
  1692. return SUCCESS;
  1693. }
  1694. return FAILURE;
  1695. }
  1696. return php_sxe_count_elements_helper(intern, count);
  1697. }
  1698. /* }}} */
  1699. /* {{{ Get number of child elements */
  1700. PHP_METHOD(SimpleXMLElement, count)
  1701. {
  1702. zend_long count = 0;
  1703. php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
  1704. if (zend_parse_parameters_none() == FAILURE) {
  1705. RETURN_THROWS();
  1706. }
  1707. php_sxe_count_elements_helper(sxe, &count);
  1708. RETURN_LONG(count);
  1709. }
  1710. /* }}} */
  1711. /* {{{ Rewind to first element */
  1712. PHP_METHOD(SimpleXMLElement, rewind)
  1713. {
  1714. if (zend_parse_parameters_none() == FAILURE) {
  1715. RETURN_THROWS();
  1716. }
  1717. php_sxe_rewind_iterator(Z_SXEOBJ_P(ZEND_THIS));
  1718. }
  1719. /* }}} */
  1720. /* {{{ Check whether iteration is valid */
  1721. PHP_METHOD(SimpleXMLElement, valid)
  1722. {
  1723. php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
  1724. if (zend_parse_parameters_none() == FAILURE) {
  1725. RETURN_THROWS();
  1726. }
  1727. RETURN_BOOL(!Z_ISUNDEF(sxe->iter.data));
  1728. }
  1729. /* }}} */
  1730. /* {{{ Get current element */
  1731. PHP_METHOD(SimpleXMLElement, current)
  1732. {
  1733. php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
  1734. if (zend_parse_parameters_none() == FAILURE) {
  1735. RETURN_THROWS();
  1736. }
  1737. if (Z_ISUNDEF(sxe->iter.data)) {
  1738. zend_throw_error(NULL, "Iterator not initialized or already consumed");
  1739. RETURN_THROWS();
  1740. }
  1741. RETURN_COPY_DEREF(&sxe->iter.data);
  1742. }
  1743. /* }}} */
  1744. /* {{{ Get name of current child element */
  1745. PHP_METHOD(SimpleXMLElement, key)
  1746. {
  1747. xmlNodePtr curnode;
  1748. php_sxe_object *intern;
  1749. php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
  1750. if (zend_parse_parameters_none() == FAILURE) {
  1751. RETURN_THROWS();
  1752. }
  1753. if (Z_ISUNDEF(sxe->iter.data)) {
  1754. zend_throw_error(NULL, "Iterator not initialized or already consumed");
  1755. RETURN_THROWS();
  1756. }
  1757. intern = Z_SXEOBJ_P(&sxe->iter.data);
  1758. if (intern == NULL || intern->node == NULL) {
  1759. zend_throw_error(NULL, "Iterator not initialized or already consumed");
  1760. RETURN_THROWS();
  1761. }
  1762. curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
  1763. RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name));
  1764. }
  1765. /* }}} */
  1766. /* {{{ Move to next element */
  1767. PHP_METHOD(SimpleXMLElement, next)
  1768. {
  1769. if (zend_parse_parameters_none() == FAILURE) {
  1770. RETURN_THROWS();
  1771. }
  1772. php_sxe_move_forward_iterator(Z_SXEOBJ_P(ZEND_THIS));
  1773. }
  1774. /* }}} */
  1775. /* {{{ Check whether element has children (elements) */
  1776. PHP_METHOD(SimpleXMLElement, hasChildren)
  1777. {
  1778. php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
  1779. php_sxe_object *child;
  1780. xmlNodePtr node;
  1781. if (zend_parse_parameters_none() == FAILURE) {
  1782. RETURN_THROWS();
  1783. }
  1784. if (Z_ISUNDEF(sxe->iter.data) || sxe->iter.type == SXE_ITER_ATTRLIST) {
  1785. RETURN_FALSE;
  1786. }
  1787. child = Z_SXEOBJ_P(&sxe->iter.data);
  1788. GET_NODE(child, node);
  1789. if (node) {
  1790. node = node->children;
  1791. }
  1792. while (node && node->type != XML_ELEMENT_NODE) {
  1793. node = node->next;
  1794. }
  1795. RETURN_BOOL(node ? 1 : 0);
  1796. }
  1797. /* }}} */
  1798. /* {{{ Get child element iterator */
  1799. PHP_METHOD(SimpleXMLElement, getChildren)
  1800. {
  1801. php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
  1802. if (zend_parse_parameters_none() == FAILURE) {
  1803. RETURN_THROWS();
  1804. }
  1805. if (Z_ISUNDEF(sxe->iter.data) || sxe->iter.type == SXE_ITER_ATTRLIST) {
  1806. return; /* return NULL */
  1807. }
  1808. RETURN_COPY_DEREF(&sxe->iter.data);
  1809. }
  1810. static zend_object_handlers sxe_object_handlers;
  1811. /* {{{ sxe_object_clone() */
  1812. static zend_object *
  1813. sxe_object_clone(zend_object *object)
  1814. {
  1815. php_sxe_object *sxe = php_sxe_fetch_object(object);
  1816. php_sxe_object *clone;
  1817. xmlNodePtr nodep = NULL;
  1818. xmlDocPtr docp = NULL;
  1819. bool is_root_element = sxe->node && sxe->node->node && sxe->node->node->parent
  1820. && (sxe->node->node->parent->type == XML_DOCUMENT_NODE || sxe->node->node->parent->type == XML_HTML_DOCUMENT_NODE);
  1821. clone = php_sxe_object_new(sxe->zo.ce, sxe->fptr_count);
  1822. if (is_root_element) {
  1823. docp = xmlCopyDoc(sxe->document->ptr, 1);
  1824. php_libxml_increment_doc_ref((php_libxml_node_object *)clone, docp);
  1825. } else {
  1826. clone->document = sxe->document;
  1827. if (clone->document) {
  1828. clone->document->refcount++;
  1829. docp = clone->document->ptr;
  1830. }
  1831. }
  1832. clone->iter.isprefix = sxe->iter.isprefix;
  1833. if (sxe->iter.name != NULL) {
  1834. clone->iter.name = (xmlChar*)estrdup((char*)sxe->iter.name);
  1835. }
  1836. if (sxe->iter.nsprefix != NULL) {
  1837. clone->iter.nsprefix = (xmlChar*)estrdup((char*)sxe->iter.nsprefix);
  1838. }
  1839. clone->iter.type = sxe->iter.type;
  1840. if (sxe->node) {
  1841. if (is_root_element) {
  1842. nodep = xmlDocGetRootElement(docp);
  1843. } else {
  1844. nodep = xmlDocCopyNode(sxe->node->node, docp, 1);
  1845. }
  1846. }
  1847. php_libxml_increment_node_ptr((php_libxml_node_object *)clone, nodep, NULL);
  1848. return &clone->zo;
  1849. }
  1850. /* }}} */
  1851. /* {{{ sxe_object_free_storage() */
  1852. static void sxe_object_free_storage(zend_object *object)
  1853. {
  1854. php_sxe_object *sxe;
  1855. sxe = php_sxe_fetch_object(object);
  1856. zend_object_std_dtor(&sxe->zo);
  1857. if (!Z_ISUNDEF(sxe->iter.data)) {
  1858. zval_ptr_dtor(&sxe->iter.data);
  1859. ZVAL_UNDEF(&sxe->iter.data);
  1860. }
  1861. if (sxe->iter.name) {
  1862. efree(sxe->iter.name);
  1863. sxe->iter.name = NULL;
  1864. }
  1865. if (sxe->iter.nsprefix) {
  1866. efree(sxe->iter.nsprefix);
  1867. sxe->iter.nsprefix = NULL;
  1868. }
  1869. if (!Z_ISUNDEF(sxe->tmp)) {
  1870. zval_ptr_dtor(&sxe->tmp);
  1871. ZVAL_UNDEF(&sxe->tmp);
  1872. }
  1873. php_libxml_node_decrement_resource((php_libxml_node_object *)sxe);
  1874. if (sxe->xpath) {
  1875. xmlXPathFreeContext(sxe->xpath);
  1876. }
  1877. if (sxe->properties) {
  1878. zend_hash_destroy(sxe->properties);
  1879. FREE_HASHTABLE(sxe->properties);
  1880. }
  1881. }
  1882. /* }}} */
  1883. /* {{{ php_sxe_find_fptr_count() */
  1884. static zend_function* php_sxe_find_fptr_count(zend_class_entry *ce)
  1885. {
  1886. zend_function *fptr_count = NULL;
  1887. zend_class_entry *parent = ce;
  1888. int inherited = 0;
  1889. while (parent) {
  1890. if (parent == sxe_class_entry) {
  1891. break;
  1892. }
  1893. parent = parent->parent;
  1894. inherited = 1;
  1895. }
  1896. if (inherited) {
  1897. fptr_count = zend_hash_str_find_ptr(&ce->function_table, "count", sizeof("count") - 1);
  1898. if (fptr_count->common.scope == parent) {
  1899. fptr_count = NULL;
  1900. }
  1901. }
  1902. return fptr_count;
  1903. }
  1904. /* }}} */
  1905. /* {{{ php_sxe_object_new() */
  1906. static php_sxe_object* php_sxe_object_new(zend_class_entry *ce, zend_function *fptr_count)
  1907. {
  1908. php_sxe_object *intern;
  1909. intern = zend_object_alloc(sizeof(php_sxe_object), ce);
  1910. intern->iter.type = SXE_ITER_NONE;
  1911. intern->iter.nsprefix = NULL;
  1912. intern->iter.name = NULL;
  1913. intern->fptr_count = fptr_count;
  1914. zend_object_std_init(&intern->zo, ce);
  1915. object_properties_init(&intern->zo, ce);
  1916. intern->zo.handlers = &sxe_object_handlers;
  1917. return intern;
  1918. }
  1919. /* }}} */
  1920. /* {{{ sxe_object_new() */
  1921. PHP_SXE_API zend_object *
  1922. sxe_object_new(zend_class_entry *ce)
  1923. {
  1924. php_sxe_object *intern;
  1925. intern = php_sxe_object_new(ce, php_sxe_find_fptr_count(ce));
  1926. return &intern->zo;
  1927. }
  1928. /* }}} */
  1929. /* {{{ Load a filename and return a simplexml_element object to allow for processing */
  1930. PHP_FUNCTION(simplexml_load_file)
  1931. {
  1932. php_sxe_object *sxe;
  1933. char *filename;
  1934. size_t filename_len;
  1935. xmlDocPtr docp;
  1936. char *ns = NULL;
  1937. size_t ns_len = 0;
  1938. zend_long options = 0;
  1939. zend_class_entry *ce= sxe_class_entry;
  1940. zend_function *fptr_count;
  1941. bool isprefix = 0;
  1942. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|C!lsb", &filename, &filename_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) {
  1943. RETURN_THROWS();
  1944. }
  1945. if (ZEND_LONG_EXCEEDS_INT(options)) {
  1946. zend_argument_value_error(3, "is too large");
  1947. RETURN_THROWS();
  1948. }
  1949. docp = xmlReadFile(filename, NULL, (int)options);
  1950. if (!docp) {
  1951. RETURN_FALSE;
  1952. }
  1953. if (!ce) {
  1954. ce = sxe_class_entry;
  1955. fptr_count = NULL;
  1956. } else {
  1957. fptr_count = php_sxe_find_fptr_count(ce);
  1958. }
  1959. sxe = php_sxe_object_new(ce, fptr_count);
  1960. sxe->iter.nsprefix = ns_len ? (xmlChar*)estrdup(ns) : NULL;
  1961. sxe->iter.isprefix = isprefix;
  1962. php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp);
  1963. php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL);
  1964. RETURN_OBJ(&sxe->zo);
  1965. }
  1966. /* }}} */
  1967. /* {{{ Load a string and return a simplexml_element object to allow for processing */
  1968. PHP_FUNCTION(simplexml_load_string)
  1969. {
  1970. php_sxe_object *sxe;
  1971. char *data;
  1972. size_t data_len;
  1973. xmlDocPtr docp;
  1974. char *ns = NULL;
  1975. size_t ns_len = 0;
  1976. zend_long options = 0;
  1977. zend_class_entry *ce= sxe_class_entry;
  1978. zend_function *fptr_count;
  1979. bool isprefix = 0;
  1980. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!lsb", &data, &data_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) {
  1981. RETURN_THROWS();
  1982. }
  1983. if (ZEND_SIZE_T_INT_OVFL(data_len)) {
  1984. zend_argument_value_error(1, "is too long");
  1985. RETURN_THROWS();
  1986. }
  1987. if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
  1988. zend_argument_value_error(4, "is too long");
  1989. RETURN_THROWS();
  1990. }
  1991. if (ZEND_LONG_EXCEEDS_INT(options)) {
  1992. zend_argument_value_error(3, "is too large");
  1993. RETURN_THROWS();
  1994. }
  1995. docp = xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);
  1996. if (!docp) {
  1997. RETURN_FALSE;
  1998. }
  1999. if (!ce) {
  2000. ce = sxe_class_entry;
  2001. fptr_count = NULL;
  2002. } else {
  2003. fptr_count = php_sxe_find_fptr_count(ce);
  2004. }
  2005. sxe = php_sxe_object_new(ce, fptr_count);
  2006. sxe->iter.nsprefix = ns_len ? (xmlChar*)estrdup(ns) : NULL;
  2007. sxe->iter.isprefix = isprefix;
  2008. php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp);
  2009. php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL);
  2010. RETURN_OBJ(&sxe->zo);
  2011. }
  2012. /* }}} */
  2013. /* {{{ SimpleXMLElement constructor */
  2014. PHP_METHOD(SimpleXMLElement, __construct)
  2015. {
  2016. php_sxe_object *sxe = Z_SXEOBJ_P(ZEND_THIS);
  2017. char *data, *ns = NULL;
  2018. size_t data_len, ns_len = 0;
  2019. xmlDocPtr docp;
  2020. zend_long options = 0;
  2021. bool is_url = 0, isprefix = 0;
  2022. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lbsb", &data, &data_len, &options, &is_url, &ns, &ns_len, &isprefix) == FAILURE) {
  2023. RETURN_THROWS();
  2024. }
  2025. if (ZEND_SIZE_T_INT_OVFL(data_len)) {
  2026. zend_argument_error(zend_ce_exception, 1, "is too long");
  2027. RETURN_THROWS();
  2028. }
  2029. if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
  2030. zend_argument_error(zend_ce_exception, 4, "is too long");
  2031. RETURN_THROWS();
  2032. }
  2033. if (ZEND_LONG_EXCEEDS_INT(options)) {
  2034. zend_argument_error(zend_ce_exception, 2, "is invalid");
  2035. RETURN_THROWS();
  2036. }
  2037. docp = is_url ? xmlReadFile(data, NULL, (int)options) : xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);
  2038. if (!docp) {
  2039. ((php_libxml_node_object *)sxe)->document = NULL;
  2040. zend_throw_exception(zend_ce_exception, "String could not be parsed as XML", 0);
  2041. RETURN_THROWS();
  2042. }
  2043. sxe->iter.nsprefix = ns_len ? (xmlChar*)estrdup(ns) : NULL;
  2044. sxe->iter.isprefix = isprefix;
  2045. php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp);
  2046. php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL);
  2047. }
  2048. /* }}} */
  2049. static const zend_object_iterator_funcs php_sxe_iterator_funcs = { /* {{{ */
  2050. php_sxe_iterator_dtor,
  2051. php_sxe_iterator_valid,
  2052. php_sxe_iterator_current_data,
  2053. php_sxe_iterator_current_key,
  2054. php_sxe_iterator_move_forward,
  2055. php_sxe_iterator_rewind,
  2056. NULL,
  2057. NULL, /* get_gc */
  2058. };
  2059. /* }}} */
  2060. static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data) /* {{{ */
  2061. {
  2062. xmlChar *prefix = sxe->iter.nsprefix;
  2063. int isprefix = sxe->iter.isprefix;
  2064. if (sxe->iter.type == SXE_ITER_ATTRLIST) {
  2065. if (sxe->iter.name) {
  2066. while (node) {
  2067. if (node->type == XML_ATTRIBUTE_NODE) {
  2068. if (!xmlStrcmp(node->name, sxe->iter.name) && match_ns(sxe, node, prefix, isprefix)) {
  2069. break;
  2070. }
  2071. }
  2072. node = node->next;
  2073. }
  2074. } else {
  2075. while (node) {
  2076. if (node->type == XML_ATTRIBUTE_NODE) {
  2077. if (match_ns(sxe, node, prefix, isprefix)) {
  2078. break;
  2079. }
  2080. }
  2081. node = node->next;
  2082. }
  2083. }
  2084. } else if (sxe->iter.type == SXE_ITER_ELEMENT && sxe->iter.name) {
  2085. while (node) {
  2086. if (node->type == XML_ELEMENT_NODE) {
  2087. if (!xmlStrcmp(node->name, sxe->iter.name) && match_ns(sxe, node, prefix, isprefix)) {
  2088. break;
  2089. }
  2090. }
  2091. node = node->next;
  2092. }
  2093. } else {
  2094. while (node) {
  2095. if (node->type == XML_ELEMENT_NODE) {
  2096. if (match_ns(sxe, node, prefix, isprefix)) {
  2097. break;
  2098. }
  2099. }
  2100. node = node->next;
  2101. }
  2102. }
  2103. if (node && use_data) {
  2104. _node_as_zval(sxe, node, &sxe->iter.data, SXE_ITER_NONE, NULL, prefix, isprefix);
  2105. }
  2106. return node;
  2107. }
  2108. /* }}} */
  2109. static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {{{ */
  2110. {
  2111. xmlNodePtr node;
  2112. if (!Z_ISUNDEF(sxe->iter.data)) {
  2113. zval_ptr_dtor(&sxe->iter.data);
  2114. ZVAL_UNDEF(&sxe->iter.data);
  2115. }
  2116. GET_NODE(sxe, node)
  2117. if (node) {
  2118. switch (sxe->iter.type) {
  2119. case SXE_ITER_ELEMENT:
  2120. case SXE_ITER_CHILD:
  2121. case SXE_ITER_NONE:
  2122. node = node->children;
  2123. break;
  2124. case SXE_ITER_ATTRLIST:
  2125. node = (xmlNodePtr) node->properties;
  2126. }
  2127. return php_sxe_iterator_fetch(sxe, node, use_data);
  2128. }
  2129. return NULL;
  2130. }
  2131. /* }}} */
  2132. zend_object_iterator *php_sxe_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  2133. {
  2134. php_sxe_iterator *iterator;
  2135. if (by_ref) {
  2136. zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
  2137. return NULL;
  2138. }
  2139. iterator = emalloc(sizeof(php_sxe_iterator));
  2140. zend_iterator_init(&iterator->intern);
  2141. ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
  2142. iterator->intern.funcs = &php_sxe_iterator_funcs;
  2143. iterator->sxe = Z_SXEOBJ_P(object);
  2144. return (zend_object_iterator*)iterator;
  2145. }
  2146. /* }}} */
  2147. static void php_sxe_iterator_dtor(zend_object_iterator *iter) /* {{{ */
  2148. {
  2149. php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
  2150. /* cleanup handled in sxe_object_dtor as we don't always have an iterator wrapper */
  2151. if (!Z_ISUNDEF(iterator->intern.data)) {
  2152. zval_ptr_dtor(&iterator->intern.data);
  2153. }
  2154. }
  2155. /* }}} */
  2156. static int php_sxe_iterator_valid(zend_object_iterator *iter) /* {{{ */
  2157. {
  2158. php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
  2159. return Z_ISUNDEF(iterator->sxe->iter.data) ? FAILURE : SUCCESS;
  2160. }
  2161. /* }}} */
  2162. static zval *php_sxe_iterator_current_data(zend_object_iterator *iter) /* {{{ */
  2163. {
  2164. php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
  2165. return &iterator->sxe->iter.data;
  2166. }
  2167. /* }}} */
  2168. static void php_sxe_iterator_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
  2169. {
  2170. php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
  2171. zval *curobj = &iterator->sxe->iter.data;
  2172. php_sxe_object *intern = Z_SXEOBJ_P(curobj);
  2173. xmlNodePtr curnode = NULL;
  2174. if (intern != NULL && intern->node != NULL) {
  2175. curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
  2176. }
  2177. if (curnode) {
  2178. ZVAL_STRINGL(key, (char *) curnode->name, xmlStrlen(curnode->name));
  2179. } else {
  2180. ZVAL_NULL(key);
  2181. }
  2182. }
  2183. /* }}} */
  2184. PHP_SXE_API void php_sxe_move_forward_iterator(php_sxe_object *sxe) /* {{{ */
  2185. {
  2186. xmlNodePtr node = NULL;
  2187. php_sxe_object *intern;
  2188. if (!Z_ISUNDEF(sxe->iter.data)) {
  2189. intern = Z_SXEOBJ_P(&sxe->iter.data);
  2190. GET_NODE(intern, node)
  2191. zval_ptr_dtor(&sxe->iter.data);
  2192. ZVAL_UNDEF(&sxe->iter.data);
  2193. }
  2194. if (node) {
  2195. php_sxe_iterator_fetch(sxe, node->next, 1);
  2196. }
  2197. }
  2198. /* }}} */
  2199. static void php_sxe_iterator_move_forward(zend_object_iterator *iter) /* {{{ */
  2200. {
  2201. php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
  2202. php_sxe_move_forward_iterator(iterator->sxe);
  2203. }
  2204. /* }}} */
  2205. PHP_SXE_API void php_sxe_rewind_iterator(php_sxe_object *sxe) /* {{{ */
  2206. {
  2207. php_sxe_reset_iterator(sxe, 1);
  2208. }
  2209. /* }}} */
  2210. static void php_sxe_iterator_rewind(zend_object_iterator *iter) /* {{{ */
  2211. {
  2212. php_sxe_object *sxe;
  2213. php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
  2214. sxe = iterator->sxe;
  2215. php_sxe_reset_iterator(sxe, 1);
  2216. }
  2217. /* }}} */
  2218. void *simplexml_export_node(zval *object) /* {{{ */
  2219. {
  2220. php_sxe_object *sxe;
  2221. xmlNodePtr node;
  2222. sxe = Z_SXEOBJ_P(object);
  2223. GET_NODE(sxe, node);
  2224. return php_sxe_get_first_node(sxe, node);
  2225. }
  2226. /* }}} */
  2227. /* {{{ Get a simplexml_element object from dom to allow for processing */
  2228. PHP_FUNCTION(simplexml_import_dom)
  2229. {
  2230. php_sxe_object *sxe;
  2231. zval *node;
  2232. php_libxml_node_object *object;
  2233. xmlNodePtr nodep = NULL;
  2234. zend_class_entry *ce = sxe_class_entry;
  2235. zend_function *fptr_count;
  2236. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|C!", &node, &ce) == FAILURE) {
  2237. RETURN_THROWS();
  2238. }
  2239. nodep = php_libxml_import_node(node);
  2240. if (!nodep) {
  2241. zend_argument_type_error(1, "must be of type SimpleXMLElement|DOMNode, %s given", zend_zval_type_name(node));
  2242. RETURN_THROWS();
  2243. }
  2244. if (nodep->doc == NULL) {
  2245. php_error_docref(NULL, E_WARNING, "Imported Node must have associated Document");
  2246. RETURN_NULL();
  2247. }
  2248. if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
  2249. nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
  2250. }
  2251. if (nodep && nodep->type == XML_ELEMENT_NODE) {
  2252. if (!ce) {
  2253. ce = sxe_class_entry;
  2254. fptr_count = NULL;
  2255. } else {
  2256. fptr_count = php_sxe_find_fptr_count(ce);
  2257. }
  2258. object = Z_LIBXML_NODE_P(node);
  2259. sxe = php_sxe_object_new(ce, fptr_count);
  2260. sxe->document = object->document;
  2261. php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, nodep->doc);
  2262. php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, nodep, NULL);
  2263. RETURN_OBJ(&sxe->zo);
  2264. } else {
  2265. php_error_docref(NULL, E_WARNING, "Invalid Nodetype to import");
  2266. RETVAL_NULL();
  2267. }
  2268. }
  2269. /* }}} */
  2270. static const zend_module_dep simplexml_deps[] = { /* {{{ */
  2271. ZEND_MOD_REQUIRED("libxml")
  2272. ZEND_MOD_REQUIRED("spl")
  2273. ZEND_MOD_END
  2274. };
  2275. /* }}} */
  2276. zend_module_entry simplexml_module_entry = { /* {{{ */
  2277. STANDARD_MODULE_HEADER_EX, NULL,
  2278. simplexml_deps,
  2279. "SimpleXML",
  2280. ext_functions,
  2281. PHP_MINIT(simplexml),
  2282. PHP_MSHUTDOWN(simplexml),
  2283. NULL,
  2284. NULL,
  2285. PHP_MINFO(simplexml),
  2286. PHP_SIMPLEXML_VERSION,
  2287. STANDARD_MODULE_PROPERTIES
  2288. };
  2289. /* }}} */
  2290. #ifdef COMPILE_DL_SIMPLEXML
  2291. ZEND_GET_MODULE(simplexml)
  2292. #endif
  2293. /* {{{ PHP_MINIT_FUNCTION(simplexml) */
  2294. PHP_MINIT_FUNCTION(simplexml)
  2295. {
  2296. sxe_class_entry = register_class_SimpleXMLElement(zend_ce_stringable, zend_ce_countable, spl_ce_RecursiveIterator);
  2297. sxe_class_entry->create_object = sxe_object_new;
  2298. sxe_class_entry->get_iterator = php_sxe_get_iterator;
  2299. memcpy(&sxe_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  2300. sxe_object_handlers.offset = XtOffsetOf(php_sxe_object, zo);
  2301. sxe_object_handlers.free_obj = sxe_object_free_storage;
  2302. sxe_object_handlers.clone_obj = sxe_object_clone;
  2303. sxe_object_handlers.read_property = sxe_property_read;
  2304. sxe_object_handlers.write_property = sxe_property_write;
  2305. sxe_object_handlers.read_dimension = sxe_dimension_read;
  2306. sxe_object_handlers.write_dimension = sxe_dimension_write;
  2307. sxe_object_handlers.get_property_ptr_ptr = sxe_property_get_adr;
  2308. sxe_object_handlers.has_property = sxe_property_exists;
  2309. sxe_object_handlers.unset_property = sxe_property_delete;
  2310. sxe_object_handlers.has_dimension = sxe_dimension_exists;
  2311. sxe_object_handlers.unset_dimension = sxe_dimension_delete;
  2312. sxe_object_handlers.get_properties = sxe_get_properties;
  2313. sxe_object_handlers.compare = sxe_objects_compare;
  2314. sxe_object_handlers.cast_object = sxe_object_cast;
  2315. sxe_object_handlers.count_elements = sxe_count_elements;
  2316. sxe_object_handlers.get_debug_info = sxe_get_debug_info;
  2317. sxe_object_handlers.get_closure = NULL;
  2318. sxe_object_handlers.get_gc = sxe_get_gc;
  2319. /* TODO: Why do we have two variables for this? */
  2320. ce_SimpleXMLElement = sxe_class_entry;
  2321. ce_SimpleXMLIterator = register_class_SimpleXMLIterator(ce_SimpleXMLElement);
  2322. php_libxml_register_export(sxe_class_entry, simplexml_export_node);
  2323. return SUCCESS;
  2324. }
  2325. /* }}} */
  2326. /* {{{ PHP_MSHUTDOWN_FUNCTION(simplexml) */
  2327. PHP_MSHUTDOWN_FUNCTION(simplexml)
  2328. {
  2329. sxe_class_entry = NULL;
  2330. return SUCCESS;
  2331. }
  2332. /* }}} */
  2333. /* {{{ PHP_MINFO_FUNCTION(simplexml) */
  2334. PHP_MINFO_FUNCTION(simplexml)
  2335. {
  2336. php_info_print_table_start();
  2337. php_info_print_table_row(2, "SimpleXML support", "enabled");
  2338. php_info_print_table_row(2, "Schema support",
  2339. #ifdef LIBXML_SCHEMAS_ENABLED
  2340. "enabled");
  2341. #else
  2342. "not available");
  2343. #endif
  2344. php_info_print_table_end();
  2345. }
  2346. /* }}} */
  2347. #endif