php_dom.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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: Christian Stocker <chregu@php.net> |
  14. | Rob Richards <rrichards@php.net> |
  15. | Marcus Borger <helly@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_DOM)
  23. #include "ext/standard/php_rand.h"
  24. #include "php_dom.h"
  25. #include "php_dom_arginfo.h"
  26. #include "dom_properties.h"
  27. #include "zend_interfaces.h"
  28. #include "ext/standard/info.h"
  29. #define PHP_XPATH 1
  30. #define PHP_XPTR 2
  31. /* {{{ class entries */
  32. PHP_DOM_EXPORT zend_class_entry *dom_node_class_entry;
  33. PHP_DOM_EXPORT zend_class_entry *dom_domexception_class_entry;
  34. PHP_DOM_EXPORT zend_class_entry *dom_parentnode_class_entry;
  35. PHP_DOM_EXPORT zend_class_entry *dom_childnode_class_entry;
  36. PHP_DOM_EXPORT zend_class_entry *dom_domimplementation_class_entry;
  37. PHP_DOM_EXPORT zend_class_entry *dom_documentfragment_class_entry;
  38. PHP_DOM_EXPORT zend_class_entry *dom_document_class_entry;
  39. PHP_DOM_EXPORT zend_class_entry *dom_nodelist_class_entry;
  40. PHP_DOM_EXPORT zend_class_entry *dom_namednodemap_class_entry;
  41. PHP_DOM_EXPORT zend_class_entry *dom_characterdata_class_entry;
  42. PHP_DOM_EXPORT zend_class_entry *dom_attr_class_entry;
  43. PHP_DOM_EXPORT zend_class_entry *dom_element_class_entry;
  44. PHP_DOM_EXPORT zend_class_entry *dom_text_class_entry;
  45. PHP_DOM_EXPORT zend_class_entry *dom_comment_class_entry;
  46. PHP_DOM_EXPORT zend_class_entry *dom_cdatasection_class_entry;
  47. PHP_DOM_EXPORT zend_class_entry *dom_documenttype_class_entry;
  48. PHP_DOM_EXPORT zend_class_entry *dom_notation_class_entry;
  49. PHP_DOM_EXPORT zend_class_entry *dom_entity_class_entry;
  50. PHP_DOM_EXPORT zend_class_entry *dom_entityreference_class_entry;
  51. PHP_DOM_EXPORT zend_class_entry *dom_processinginstruction_class_entry;
  52. #ifdef LIBXML_XPATH_ENABLED
  53. PHP_DOM_EXPORT zend_class_entry *dom_xpath_class_entry;
  54. #endif
  55. PHP_DOM_EXPORT zend_class_entry *dom_namespace_node_class_entry;
  56. /* }}} */
  57. zend_object_handlers dom_object_handlers;
  58. zend_object_handlers dom_nnodemap_object_handlers;
  59. #ifdef LIBXML_XPATH_ENABLED
  60. zend_object_handlers dom_xpath_object_handlers;
  61. #endif
  62. static HashTable classes;
  63. /* {{{ prop handler tables */
  64. static HashTable dom_document_prop_handlers;
  65. static HashTable dom_documentfragment_prop_handlers;
  66. static HashTable dom_node_prop_handlers;
  67. static HashTable dom_nodelist_prop_handlers;
  68. static HashTable dom_namednodemap_prop_handlers;
  69. static HashTable dom_characterdata_prop_handlers;
  70. static HashTable dom_attr_prop_handlers;
  71. static HashTable dom_element_prop_handlers;
  72. static HashTable dom_text_prop_handlers;
  73. static HashTable dom_documenttype_prop_handlers;
  74. static HashTable dom_notation_prop_handlers;
  75. static HashTable dom_entity_prop_handlers;
  76. static HashTable dom_processinginstruction_prop_handlers;
  77. static HashTable dom_namespace_node_prop_handlers;
  78. #ifdef LIBXML_XPATH_ENABLED
  79. static HashTable dom_xpath_prop_handlers;
  80. #endif
  81. /* }}} */
  82. typedef int (*dom_read_t)(dom_object *obj, zval *retval);
  83. typedef int (*dom_write_t)(dom_object *obj, zval *newval);
  84. typedef struct _dom_prop_handler {
  85. dom_read_t read_func;
  86. dom_write_t write_func;
  87. } dom_prop_handler;
  88. static zend_object_handlers* dom_get_obj_handlers(void) {
  89. return &dom_object_handlers;
  90. }
  91. /* {{{ int dom_node_is_read_only(xmlNodePtr node) */
  92. int dom_node_is_read_only(xmlNodePtr node) {
  93. switch (node->type) {
  94. case XML_ENTITY_REF_NODE:
  95. case XML_ENTITY_NODE:
  96. case XML_DOCUMENT_TYPE_NODE:
  97. case XML_NOTATION_NODE:
  98. case XML_DTD_NODE:
  99. case XML_ELEMENT_DECL:
  100. case XML_ATTRIBUTE_DECL:
  101. case XML_ENTITY_DECL:
  102. case XML_NAMESPACE_DECL:
  103. return SUCCESS;
  104. break;
  105. default:
  106. if (node->doc == NULL) {
  107. return SUCCESS;
  108. } else {
  109. return FAILURE;
  110. }
  111. }
  112. }
  113. /* }}} end dom_node_is_read_only */
  114. /* {{{ int dom_node_children_valid(xmlNodePtr node) */
  115. int dom_node_children_valid(xmlNodePtr node) {
  116. switch (node->type) {
  117. case XML_DOCUMENT_TYPE_NODE:
  118. case XML_DTD_NODE:
  119. case XML_PI_NODE:
  120. case XML_COMMENT_NODE:
  121. case XML_TEXT_NODE:
  122. case XML_CDATA_SECTION_NODE:
  123. case XML_NOTATION_NODE:
  124. return FAILURE;
  125. break;
  126. default:
  127. return SUCCESS;
  128. }
  129. }
  130. /* }}} end dom_node_children_valid */
  131. /* {{{ dom_get_doc_props() */
  132. dom_doc_propsptr dom_get_doc_props(php_libxml_ref_obj *document)
  133. {
  134. dom_doc_propsptr doc_props;
  135. if (document && document->doc_props) {
  136. return document->doc_props;
  137. } else {
  138. doc_props = emalloc(sizeof(libxml_doc_props));
  139. doc_props->formatoutput = 0;
  140. doc_props->validateonparse = 0;
  141. doc_props->resolveexternals = 0;
  142. doc_props->preservewhitespace = 1;
  143. doc_props->substituteentities = 0;
  144. doc_props->stricterror = 1;
  145. doc_props->recover = 0;
  146. doc_props->classmap = NULL;
  147. if (document) {
  148. document->doc_props = doc_props;
  149. }
  150. return doc_props;
  151. }
  152. }
  153. static void dom_copy_doc_props(php_libxml_ref_obj *source_doc, php_libxml_ref_obj *dest_doc)
  154. {
  155. dom_doc_propsptr source, dest;
  156. if (source_doc && dest_doc) {
  157. source = dom_get_doc_props(source_doc);
  158. dest = dom_get_doc_props(dest_doc);
  159. dest->formatoutput = source->formatoutput;
  160. dest->validateonparse = source->validateonparse;
  161. dest->resolveexternals = source->resolveexternals;
  162. dest->preservewhitespace = source->preservewhitespace;
  163. dest->substituteentities = source->substituteentities;
  164. dest->stricterror = source->stricterror;
  165. dest->recover = source->recover;
  166. if (source->classmap) {
  167. ALLOC_HASHTABLE(dest->classmap);
  168. zend_hash_init(dest->classmap, 0, NULL, NULL, 0);
  169. zend_hash_copy(dest->classmap, source->classmap, NULL);
  170. }
  171. }
  172. }
  173. void dom_set_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece, zend_class_entry *ce)
  174. {
  175. dom_doc_propsptr doc_props;
  176. if (document) {
  177. doc_props = dom_get_doc_props(document);
  178. if (doc_props->classmap == NULL) {
  179. if (ce == NULL) {
  180. return;
  181. }
  182. ALLOC_HASHTABLE(doc_props->classmap);
  183. zend_hash_init(doc_props->classmap, 0, NULL, NULL, 0);
  184. }
  185. if (ce) {
  186. zend_hash_update_ptr(doc_props->classmap, basece->name, ce);
  187. } else {
  188. zend_hash_del(doc_props->classmap, basece->name);
  189. }
  190. }
  191. }
  192. zend_class_entry *dom_get_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece)
  193. {
  194. dom_doc_propsptr doc_props;
  195. if (document) {
  196. doc_props = dom_get_doc_props(document);
  197. if (doc_props->classmap) {
  198. zend_class_entry *ce = zend_hash_find_ptr(doc_props->classmap, basece->name);
  199. if (ce) {
  200. return ce;
  201. }
  202. }
  203. }
  204. return basece;
  205. }
  206. /* }}} */
  207. /* {{{ dom_get_strict_error() */
  208. int dom_get_strict_error(php_libxml_ref_obj *document) {
  209. int stricterror;
  210. dom_doc_propsptr doc_props;
  211. doc_props = dom_get_doc_props(document);
  212. stricterror = doc_props->stricterror;
  213. if (document == NULL) {
  214. efree(doc_props);
  215. }
  216. return stricterror;
  217. }
  218. /* }}} */
  219. /* {{{ xmlNodePtr dom_object_get_node(dom_object *obj) */
  220. PHP_DOM_EXPORT xmlNodePtr dom_object_get_node(dom_object *obj)
  221. {
  222. if (obj && obj->ptr != NULL) {
  223. return ((php_libxml_node_ptr *)obj->ptr)->node;
  224. } else {
  225. return NULL;
  226. }
  227. }
  228. /* }}} end dom_object_get_node */
  229. /* {{{ dom_object *php_dom_object_get_data(xmlNodePtr obj) */
  230. PHP_DOM_EXPORT dom_object *php_dom_object_get_data(xmlNodePtr obj)
  231. {
  232. if (obj && obj->_private != NULL) {
  233. return (dom_object *) ((php_libxml_node_ptr *) obj->_private)->_private;
  234. } else {
  235. return NULL;
  236. }
  237. }
  238. /* }}} end php_dom_object_get_data */
  239. static void dom_register_prop_handler(HashTable *prop_handler, char *name, size_t name_len, dom_read_t read_func, dom_write_t write_func)
  240. {
  241. dom_prop_handler hnd;
  242. zend_string *str;
  243. hnd.read_func = read_func;
  244. hnd.write_func = write_func;
  245. str = zend_string_init_interned(name, name_len, 1);
  246. zend_hash_add_mem(prop_handler, str, &hnd, sizeof(dom_prop_handler));
  247. zend_string_release_ex(str, 1);
  248. }
  249. static zval *dom_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
  250. {
  251. dom_object *obj = php_dom_obj_from_obj(object);
  252. if (!obj->prop_handler || !zend_hash_exists(obj->prop_handler, name)) {
  253. return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
  254. }
  255. return NULL;
  256. }
  257. /* {{{ dom_read_property */
  258. zval *dom_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
  259. {
  260. dom_object *obj = php_dom_obj_from_obj(object);
  261. zval *retval;
  262. dom_prop_handler *hnd = NULL;
  263. if (obj->prop_handler != NULL) {
  264. hnd = zend_hash_find_ptr(obj->prop_handler, name);
  265. } else if (instanceof_function(obj->std.ce, dom_node_class_entry)) {
  266. zend_throw_error(NULL, "Couldn't fetch %s. Node no longer exists", ZSTR_VAL(obj->std.ce->name));
  267. retval = &EG(uninitialized_zval);
  268. return retval;
  269. }
  270. if (hnd) {
  271. int ret = hnd->read_func(obj, rv);
  272. if (ret == SUCCESS) {
  273. retval = rv;
  274. } else {
  275. retval = &EG(uninitialized_zval);
  276. }
  277. } else {
  278. retval = zend_std_read_property(object, name, type, cache_slot, rv);
  279. }
  280. return retval;
  281. }
  282. /* }}} */
  283. zval *dom_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  284. {
  285. dom_object *obj = php_dom_obj_from_obj(object);
  286. dom_prop_handler *hnd = NULL;
  287. if (obj->prop_handler != NULL) {
  288. hnd = zend_hash_find_ptr(obj->prop_handler, name);
  289. }
  290. if (hnd) {
  291. if (!hnd->write_func) {
  292. zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
  293. return &EG(error_zval);
  294. }
  295. zend_property_info *prop = zend_get_property_info(object->ce, name, /* silent */ true);
  296. if (prop && ZEND_TYPE_IS_SET(prop->type)) {
  297. zval tmp;
  298. ZVAL_COPY(&tmp, value);
  299. if (!zend_verify_property_type(prop, &tmp, ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))) {
  300. zval_ptr_dtor(&tmp);
  301. return &EG(error_zval);
  302. }
  303. hnd->write_func(obj, &tmp);
  304. zval_ptr_dtor(&tmp);
  305. } else {
  306. hnd->write_func(obj, value);
  307. }
  308. return value;
  309. }
  310. return zend_std_write_property(object, name, value, cache_slot);
  311. }
  312. /* {{{ dom_property_exists */
  313. static int dom_property_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
  314. {
  315. dom_object *obj = php_dom_obj_from_obj(object);
  316. dom_prop_handler *hnd = NULL;
  317. int retval = 0;
  318. if (obj->prop_handler != NULL) {
  319. hnd = zend_hash_find_ptr(obj->prop_handler, name);
  320. }
  321. if (hnd) {
  322. zval tmp;
  323. if (check_empty == 2) {
  324. retval = 1;
  325. } else if (hnd->read_func(obj, &tmp) == SUCCESS) {
  326. if (check_empty == 1) {
  327. retval = zend_is_true(&tmp);
  328. } else if (check_empty == 0) {
  329. retval = (Z_TYPE(tmp) != IS_NULL);
  330. }
  331. zval_ptr_dtor(&tmp);
  332. }
  333. } else {
  334. retval = zend_std_has_property(object, name, check_empty, cache_slot);
  335. }
  336. return retval;
  337. }
  338. /* }}} */
  339. static HashTable* dom_get_debug_info_helper(zend_object *object, int *is_temp) /* {{{ */
  340. {
  341. dom_object *obj = php_dom_obj_from_obj(object);
  342. HashTable *debug_info,
  343. *prop_handlers = obj->prop_handler,
  344. *std_props;
  345. zend_string *string_key;
  346. dom_prop_handler *entry;
  347. zend_string *object_str;
  348. *is_temp = 1;
  349. std_props = zend_std_get_properties(object);
  350. debug_info = zend_array_dup(std_props);
  351. if (!prop_handlers) {
  352. return debug_info;
  353. }
  354. object_str = zend_string_init("(object value omitted)", sizeof("(object value omitted)")-1, 0);
  355. ZEND_HASH_FOREACH_STR_KEY_PTR(prop_handlers, string_key, entry) {
  356. zval value;
  357. if (entry->read_func(obj, &value) == FAILURE || !string_key) {
  358. continue;
  359. }
  360. if (Z_TYPE(value) == IS_OBJECT) {
  361. zval_ptr_dtor(&value);
  362. ZVAL_NEW_STR(&value, object_str);
  363. zend_string_addref(object_str);
  364. }
  365. zend_hash_update(debug_info, string_key, &value);
  366. } ZEND_HASH_FOREACH_END();
  367. zend_string_release_ex(object_str, 0);
  368. return debug_info;
  369. }
  370. /* }}} */
  371. static HashTable* dom_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
  372. {
  373. return dom_get_debug_info_helper(object, is_temp);
  374. }
  375. /* }}} */
  376. void *php_dom_export_node(zval *object) /* {{{ */
  377. {
  378. php_libxml_node_object *intern;
  379. xmlNodePtr nodep = NULL;
  380. intern = (php_libxml_node_object *) Z_DOMOBJ_P(object);
  381. if (intern->node) {
  382. nodep = intern->node->node;
  383. }
  384. return nodep;
  385. }
  386. /* }}} */
  387. /* {{{ Get a simplexml_element object from dom to allow for processing */
  388. PHP_FUNCTION(dom_import_simplexml)
  389. {
  390. zval *node;
  391. xmlNodePtr nodep = NULL;
  392. php_libxml_node_object *nodeobj;
  393. int ret;
  394. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &node) == FAILURE) {
  395. RETURN_THROWS();
  396. }
  397. nodeobj = (php_libxml_node_object *) ((char *) Z_OBJ_P(node) - Z_OBJ_HT_P(node)->offset);
  398. nodep = php_libxml_import_node(node);
  399. if (nodep && nodeobj && (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE)) {
  400. DOM_RET_OBJ((xmlNodePtr) nodep, &ret, (dom_object *)nodeobj);
  401. } else {
  402. zend_argument_value_error(1, "is not a valid node type");
  403. RETURN_THROWS();
  404. }
  405. }
  406. /* }}} */
  407. static dom_object* dom_objects_set_class(zend_class_entry *class_type);
  408. static zend_object *dom_objects_store_clone_obj(zend_object *zobject) /* {{{ */
  409. {
  410. dom_object *intern = php_dom_obj_from_obj(zobject);
  411. dom_object *clone = dom_objects_set_class(intern->std.ce);
  412. clone->std.handlers = dom_get_obj_handlers();
  413. if (instanceof_function(intern->std.ce, dom_node_class_entry)) {
  414. xmlNodePtr node = (xmlNodePtr)dom_object_get_node(intern);
  415. if (node != NULL) {
  416. xmlNodePtr cloned_node = xmlDocCopyNode(node, node->doc, 1);
  417. if (cloned_node != NULL) {
  418. /* If we cloned a document then we must create new doc proxy */
  419. if (cloned_node->doc == node->doc) {
  420. clone->document = intern->document;
  421. }
  422. php_libxml_increment_doc_ref((php_libxml_node_object *)clone, cloned_node->doc);
  423. php_libxml_increment_node_ptr((php_libxml_node_object *)clone, cloned_node, (void *)clone);
  424. if (intern->document != clone->document) {
  425. dom_copy_doc_props(intern->document, clone->document);
  426. }
  427. }
  428. }
  429. }
  430. zend_objects_clone_members(&clone->std, &intern->std);
  431. return &clone->std;
  432. }
  433. /* }}} */
  434. static void dom_copy_prop_handler(zval *zv) /* {{{ */
  435. {
  436. dom_prop_handler *hnd = Z_PTR_P(zv);
  437. Z_PTR_P(zv) = malloc(sizeof(dom_prop_handler));
  438. memcpy(Z_PTR_P(zv), hnd, sizeof(dom_prop_handler));
  439. }
  440. /* }}} */
  441. static void dom_dtor_prop_handler(zval *zv) /* {{{ */
  442. {
  443. free(Z_PTR_P(zv));
  444. }
  445. static const zend_module_dep dom_deps[] = {
  446. ZEND_MOD_REQUIRED("libxml")
  447. ZEND_MOD_CONFLICTS("domxml")
  448. ZEND_MOD_END
  449. };
  450. zend_module_entry dom_module_entry = { /* {{{ */
  451. STANDARD_MODULE_HEADER_EX, NULL,
  452. dom_deps,
  453. "dom",
  454. ext_functions,
  455. PHP_MINIT(dom),
  456. PHP_MSHUTDOWN(dom),
  457. NULL,
  458. NULL,
  459. PHP_MINFO(dom),
  460. DOM_API_VERSION, /* Extension versionnumber */
  461. STANDARD_MODULE_PROPERTIES
  462. };
  463. /* }}} */
  464. #ifdef COMPILE_DL_DOM
  465. ZEND_GET_MODULE(dom)
  466. #endif
  467. void dom_objects_free_storage(zend_object *object);
  468. void dom_nnodemap_objects_free_storage(zend_object *object);
  469. static zval *dom_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
  470. static int dom_nodelist_has_dimension(zend_object *object, zval *member, int check_empty);
  471. static zend_object *dom_objects_store_clone_obj(zend_object *zobject);
  472. #ifdef LIBXML_XPATH_ENABLED
  473. void dom_xpath_objects_free_storage(zend_object *object);
  474. #endif
  475. /* {{{ PHP_MINIT_FUNCTION(dom) */
  476. PHP_MINIT_FUNCTION(dom)
  477. {
  478. memcpy(&dom_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  479. dom_object_handlers.offset = XtOffsetOf(dom_object, std);
  480. dom_object_handlers.free_obj = dom_objects_free_storage;
  481. dom_object_handlers.read_property = dom_read_property;
  482. dom_object_handlers.write_property = dom_write_property;
  483. dom_object_handlers.get_property_ptr_ptr = dom_get_property_ptr_ptr;
  484. dom_object_handlers.clone_obj = dom_objects_store_clone_obj;
  485. dom_object_handlers.has_property = dom_property_exists;
  486. dom_object_handlers.get_debug_info = dom_get_debug_info;
  487. memcpy(&dom_nnodemap_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
  488. dom_nnodemap_object_handlers.free_obj = dom_nnodemap_objects_free_storage;
  489. dom_nnodemap_object_handlers.read_dimension = dom_nodelist_read_dimension;
  490. dom_nnodemap_object_handlers.has_dimension = dom_nodelist_has_dimension;
  491. zend_hash_init(&classes, 0, NULL, NULL, 1);
  492. dom_domexception_class_entry = register_class_DOMException(zend_ce_exception);
  493. dom_parentnode_class_entry = register_class_DOMParentNode();
  494. dom_childnode_class_entry = register_class_DOMChildNode();
  495. dom_domimplementation_class_entry = register_class_DOMImplementation();
  496. dom_domimplementation_class_entry->create_object = dom_objects_new;
  497. dom_node_class_entry = register_class_DOMNode();
  498. dom_node_class_entry->create_object = dom_objects_new;
  499. zend_hash_init(&dom_node_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  500. dom_register_prop_handler(&dom_node_prop_handlers, "nodeName", sizeof("nodeName")-1, dom_node_node_name_read, NULL);
  501. dom_register_prop_handler(&dom_node_prop_handlers, "nodeValue", sizeof("nodeValue")-1, dom_node_node_value_read, dom_node_node_value_write);
  502. dom_register_prop_handler(&dom_node_prop_handlers, "nodeType", sizeof("nodeType")-1, dom_node_node_type_read, NULL);
  503. dom_register_prop_handler(&dom_node_prop_handlers, "parentNode", sizeof("parentNode")-1, dom_node_parent_node_read, NULL);
  504. dom_register_prop_handler(&dom_node_prop_handlers, "childNodes", sizeof("childNodes")-1, dom_node_child_nodes_read, NULL);
  505. dom_register_prop_handler(&dom_node_prop_handlers, "firstChild", sizeof("firstChild")-1, dom_node_first_child_read, NULL);
  506. dom_register_prop_handler(&dom_node_prop_handlers, "lastChild", sizeof("lastChild")-1, dom_node_last_child_read, NULL);
  507. dom_register_prop_handler(&dom_node_prop_handlers, "previousSibling", sizeof("previousSibling")-1, dom_node_previous_sibling_read, NULL);
  508. dom_register_prop_handler(&dom_node_prop_handlers, "nextSibling", sizeof("nextSibling")-1, dom_node_next_sibling_read, NULL);
  509. dom_register_prop_handler(&dom_node_prop_handlers, "attributes", sizeof("attributes")-1, dom_node_attributes_read, NULL);
  510. dom_register_prop_handler(&dom_node_prop_handlers, "ownerDocument", sizeof("ownerDocument")-1, dom_node_owner_document_read, NULL);
  511. dom_register_prop_handler(&dom_node_prop_handlers, "namespaceURI", sizeof("namespaceURI")-1, dom_node_namespace_uri_read, NULL);
  512. dom_register_prop_handler(&dom_node_prop_handlers, "prefix", sizeof("prefix")-1, dom_node_prefix_read, dom_node_prefix_write);
  513. dom_register_prop_handler(&dom_node_prop_handlers, "localName", sizeof("localName")-1, dom_node_local_name_read, NULL);
  514. dom_register_prop_handler(&dom_node_prop_handlers, "baseURI", sizeof("baseURI")-1, dom_node_base_uri_read, NULL);
  515. dom_register_prop_handler(&dom_node_prop_handlers, "textContent", sizeof("textContent")-1, dom_node_text_content_read, dom_node_text_content_write);
  516. zend_hash_add_ptr(&classes, dom_node_class_entry->name, &dom_node_prop_handlers);
  517. dom_namespace_node_class_entry = register_class_DOMNameSpaceNode();
  518. dom_namespace_node_class_entry->create_object = dom_objects_new;
  519. zend_hash_init(&dom_namespace_node_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  520. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "nodeName", sizeof("nodeName")-1, dom_node_node_name_read, NULL);
  521. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "nodeValue", sizeof("nodeValue")-1, dom_node_node_value_read, NULL);
  522. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "nodeType", sizeof("nodeType")-1, dom_node_node_type_read, NULL);
  523. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "prefix", sizeof("prefix")-1, dom_node_prefix_read, NULL);
  524. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "localName", sizeof("localName")-1, dom_node_local_name_read, NULL);
  525. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "namespaceURI", sizeof("namespaceURI")-1, dom_node_namespace_uri_read, NULL);
  526. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "ownerDocument", sizeof("ownerDocument")-1, dom_node_owner_document_read, NULL);
  527. dom_register_prop_handler(&dom_namespace_node_prop_handlers, "parentNode", sizeof("parentNode")-1, dom_node_parent_node_read, NULL);
  528. zend_hash_add_ptr(&classes, dom_namespace_node_class_entry->name, &dom_namespace_node_prop_handlers);
  529. dom_documentfragment_class_entry = register_class_DOMDocumentFragment(dom_node_class_entry, dom_parentnode_class_entry);
  530. dom_documentfragment_class_entry->create_object = dom_objects_new;
  531. zend_hash_init(&dom_documentfragment_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  532. dom_register_prop_handler(&dom_documentfragment_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
  533. dom_register_prop_handler(&dom_documentfragment_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);
  534. dom_register_prop_handler(&dom_documentfragment_prop_handlers, "childElementCount", sizeof("childElementCount")-1, dom_parent_node_child_element_count, NULL);
  535. zend_hash_merge(&dom_documentfragment_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  536. zend_hash_add_ptr(&classes, dom_documentfragment_class_entry->name, &dom_documentfragment_prop_handlers);
  537. dom_document_class_entry = register_class_DOMDocument(dom_node_class_entry, dom_parentnode_class_entry);
  538. dom_document_class_entry->create_object = dom_objects_new;
  539. zend_hash_init(&dom_document_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  540. dom_register_prop_handler(&dom_document_prop_handlers, "doctype", sizeof("doctype")-1, dom_document_doctype_read, NULL);
  541. dom_register_prop_handler(&dom_document_prop_handlers, "implementation", sizeof("implementation")-1, dom_document_implementation_read, NULL);
  542. dom_register_prop_handler(&dom_document_prop_handlers, "documentElement", sizeof("documentElement")-1, dom_document_document_element_read, NULL);
  543. dom_register_prop_handler(&dom_document_prop_handlers, "actualEncoding", sizeof("actualEncoding")-1, dom_document_encoding_read, NULL);
  544. dom_register_prop_handler(&dom_document_prop_handlers, "encoding", sizeof("encoding")-1, dom_document_encoding_read, dom_document_encoding_write);
  545. dom_register_prop_handler(&dom_document_prop_handlers, "xmlEncoding", sizeof("xmlEncoding")-1, dom_document_encoding_read, NULL);
  546. dom_register_prop_handler(&dom_document_prop_handlers, "standalone", sizeof("standalone")-1, dom_document_standalone_read, dom_document_standalone_write);
  547. dom_register_prop_handler(&dom_document_prop_handlers, "xmlStandalone", sizeof("xmlStandalone")-1, dom_document_standalone_read, dom_document_standalone_write);
  548. dom_register_prop_handler(&dom_document_prop_handlers, "version", sizeof("version")-1, dom_document_version_read, dom_document_version_write);
  549. dom_register_prop_handler(&dom_document_prop_handlers, "xmlVersion", sizeof("xmlVersion")-1, dom_document_version_read, dom_document_version_write);
  550. dom_register_prop_handler(&dom_document_prop_handlers, "strictErrorChecking", sizeof("strictErrorChecking")-1, dom_document_strict_error_checking_read, dom_document_strict_error_checking_write);
  551. dom_register_prop_handler(&dom_document_prop_handlers, "documentURI", sizeof("documentURI")-1, dom_document_document_uri_read, dom_document_document_uri_write);
  552. dom_register_prop_handler(&dom_document_prop_handlers, "config", sizeof("config")-1, dom_document_config_read, NULL);
  553. dom_register_prop_handler(&dom_document_prop_handlers, "formatOutput", sizeof("formatOutput")-1, dom_document_format_output_read, dom_document_format_output_write);
  554. dom_register_prop_handler(&dom_document_prop_handlers, "validateOnParse", sizeof("validateOnParse")-1, dom_document_validate_on_parse_read, dom_document_validate_on_parse_write);
  555. dom_register_prop_handler(&dom_document_prop_handlers, "resolveExternals", sizeof("resolveExternals")-1, dom_document_resolve_externals_read, dom_document_resolve_externals_write);
  556. dom_register_prop_handler(&dom_document_prop_handlers, "preserveWhiteSpace", sizeof("preserveWhitespace")-1, dom_document_preserve_whitespace_read, dom_document_preserve_whitespace_write);
  557. dom_register_prop_handler(&dom_document_prop_handlers, "recover", sizeof("recover")-1, dom_document_recover_read, dom_document_recover_write);
  558. dom_register_prop_handler(&dom_document_prop_handlers, "substituteEntities", sizeof("substituteEntities")-1, dom_document_substitue_entities_read, dom_document_substitue_entities_write);
  559. dom_register_prop_handler(&dom_document_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
  560. dom_register_prop_handler(&dom_document_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);
  561. dom_register_prop_handler(&dom_document_prop_handlers, "childElementCount", sizeof("childElementCount")-1, dom_parent_node_child_element_count, NULL);
  562. zend_hash_merge(&dom_document_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  563. zend_hash_add_ptr(&classes, dom_document_class_entry->name, &dom_document_prop_handlers);
  564. dom_nodelist_class_entry = register_class_DOMNodeList(zend_ce_aggregate, zend_ce_countable);
  565. dom_nodelist_class_entry->create_object = dom_nnodemap_objects_new;
  566. dom_nodelist_class_entry->get_iterator = php_dom_get_iterator;
  567. zend_hash_init(&dom_nodelist_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  568. dom_register_prop_handler(&dom_nodelist_prop_handlers, "length", sizeof("length")-1, dom_nodelist_length_read, NULL);
  569. zend_hash_add_ptr(&classes, dom_nodelist_class_entry->name, &dom_nodelist_prop_handlers);
  570. dom_namednodemap_class_entry = register_class_DOMNamedNodeMap(zend_ce_aggregate, zend_ce_countable);
  571. dom_namednodemap_class_entry->create_object = dom_nnodemap_objects_new;
  572. dom_namednodemap_class_entry->get_iterator = php_dom_get_iterator;
  573. zend_hash_init(&dom_namednodemap_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  574. dom_register_prop_handler(&dom_namednodemap_prop_handlers, "length", sizeof("length")-1, dom_namednodemap_length_read, NULL);
  575. zend_hash_add_ptr(&classes, dom_namednodemap_class_entry->name, &dom_namednodemap_prop_handlers);
  576. dom_characterdata_class_entry = register_class_DOMCharacterData(dom_node_class_entry, dom_childnode_class_entry);
  577. dom_characterdata_class_entry->create_object = dom_objects_new;
  578. zend_hash_init(&dom_characterdata_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  579. dom_register_prop_handler(&dom_characterdata_prop_handlers, "data", sizeof("data")-1, dom_characterdata_data_read, dom_characterdata_data_write);
  580. dom_register_prop_handler(&dom_characterdata_prop_handlers, "length", sizeof("length")-1, dom_characterdata_length_read, NULL);
  581. dom_register_prop_handler(&dom_characterdata_prop_handlers, "previousElementSibling", sizeof("previousElementSibling")-1, dom_node_previous_element_sibling_read, NULL);
  582. dom_register_prop_handler(&dom_characterdata_prop_handlers, "nextElementSibling", sizeof("nextElementSibling")-1, dom_node_next_element_sibling_read, NULL);
  583. zend_hash_merge(&dom_characterdata_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  584. zend_hash_add_ptr(&classes, dom_characterdata_class_entry->name, &dom_characterdata_prop_handlers);
  585. dom_attr_class_entry = register_class_DOMAttr(dom_node_class_entry);
  586. dom_attr_class_entry->create_object = dom_objects_new;
  587. zend_hash_init(&dom_attr_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  588. dom_register_prop_handler(&dom_attr_prop_handlers, "name", sizeof("name")-1, dom_attr_name_read, NULL);
  589. dom_register_prop_handler(&dom_attr_prop_handlers, "specified", sizeof("specified")-1, dom_attr_specified_read, NULL);
  590. dom_register_prop_handler(&dom_attr_prop_handlers, "value", sizeof("value")-1, dom_attr_value_read, dom_attr_value_write);
  591. dom_register_prop_handler(&dom_attr_prop_handlers, "ownerElement", sizeof("ownerElement")-1, dom_attr_owner_element_read, NULL);
  592. dom_register_prop_handler(&dom_attr_prop_handlers, "schemaTypeInfo", sizeof("schemaTypeInfo")-1, dom_attr_schema_type_info_read, NULL);
  593. zend_hash_merge(&dom_attr_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  594. zend_hash_add_ptr(&classes, dom_attr_class_entry->name, &dom_attr_prop_handlers);
  595. dom_element_class_entry = register_class_DOMElement(dom_node_class_entry, dom_parentnode_class_entry, dom_childnode_class_entry);
  596. dom_element_class_entry->create_object = dom_objects_new;
  597. zend_hash_init(&dom_element_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  598. dom_register_prop_handler(&dom_element_prop_handlers, "tagName", sizeof("tagName")-1, dom_element_tag_name_read, NULL);
  599. dom_register_prop_handler(&dom_element_prop_handlers, "schemaTypeInfo", sizeof("schemaTypeInfo")-1, dom_element_schema_type_info_read, NULL);
  600. dom_register_prop_handler(&dom_element_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
  601. dom_register_prop_handler(&dom_element_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);
  602. dom_register_prop_handler(&dom_element_prop_handlers, "childElementCount", sizeof("childElementCount")-1, dom_parent_node_child_element_count, NULL);
  603. dom_register_prop_handler(&dom_element_prop_handlers, "previousElementSibling", sizeof("previousElementSibling")-1, dom_node_previous_element_sibling_read, NULL);
  604. dom_register_prop_handler(&dom_element_prop_handlers, "nextElementSibling", sizeof("nextElementSibling")-1, dom_node_next_element_sibling_read, NULL);
  605. zend_hash_merge(&dom_element_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  606. zend_hash_add_ptr(&classes, dom_element_class_entry->name, &dom_element_prop_handlers);
  607. dom_text_class_entry = register_class_DOMText(dom_characterdata_class_entry);
  608. dom_text_class_entry->create_object = dom_objects_new;
  609. zend_hash_init(&dom_text_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  610. dom_register_prop_handler(&dom_text_prop_handlers, "wholeText", sizeof("wholeText")-1, dom_text_whole_text_read, NULL);
  611. zend_hash_merge(&dom_text_prop_handlers, &dom_characterdata_prop_handlers, dom_copy_prop_handler, 0);
  612. zend_hash_add_ptr(&classes, dom_text_class_entry->name, &dom_text_prop_handlers);
  613. dom_comment_class_entry = register_class_DOMComment(dom_characterdata_class_entry);
  614. dom_comment_class_entry->create_object = dom_objects_new;
  615. zend_hash_add_ptr(&classes, dom_comment_class_entry->name, &dom_characterdata_prop_handlers);
  616. dom_cdatasection_class_entry = register_class_DOMCdataSection(dom_text_class_entry);
  617. dom_cdatasection_class_entry->create_object = dom_objects_new;
  618. zend_hash_add_ptr(&classes, dom_cdatasection_class_entry->name, &dom_text_prop_handlers);
  619. dom_documenttype_class_entry = register_class_DOMDocumentType(dom_node_class_entry);
  620. dom_documenttype_class_entry->create_object = dom_objects_new;
  621. zend_hash_init(&dom_documenttype_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  622. dom_register_prop_handler(&dom_documenttype_prop_handlers, "name", sizeof("name")-1, dom_documenttype_name_read, NULL);
  623. dom_register_prop_handler(&dom_documenttype_prop_handlers, "entities", sizeof("entities")-1, dom_documenttype_entities_read, NULL);
  624. dom_register_prop_handler(&dom_documenttype_prop_handlers, "notations", sizeof("notations")-1, dom_documenttype_notations_read, NULL);
  625. dom_register_prop_handler(&dom_documenttype_prop_handlers, "publicId", sizeof("publicId")-1, dom_documenttype_public_id_read, NULL);
  626. dom_register_prop_handler(&dom_documenttype_prop_handlers, "systemId", sizeof("systemId")-1, dom_documenttype_system_id_read, NULL);
  627. dom_register_prop_handler(&dom_documenttype_prop_handlers, "internalSubset", sizeof("internalSubset")-1, dom_documenttype_internal_subset_read, NULL);
  628. zend_hash_merge(&dom_documenttype_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  629. zend_hash_add_ptr(&classes, dom_documenttype_class_entry->name, &dom_documenttype_prop_handlers);
  630. dom_notation_class_entry = register_class_DOMNotation(dom_node_class_entry);
  631. dom_notation_class_entry->create_object = dom_objects_new;
  632. zend_hash_init(&dom_notation_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  633. dom_register_prop_handler(&dom_notation_prop_handlers, "publicId", sizeof("publicId")-1, dom_notation_public_id_read, NULL);
  634. dom_register_prop_handler(&dom_notation_prop_handlers, "systemId", sizeof("systemId")-1, dom_notation_system_id_read, NULL);
  635. zend_hash_merge(&dom_notation_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  636. zend_hash_add_ptr(&classes, dom_notation_class_entry->name, &dom_notation_prop_handlers);
  637. dom_entity_class_entry = register_class_DOMEntity(dom_node_class_entry);
  638. dom_entity_class_entry->create_object = dom_objects_new;
  639. zend_hash_init(&dom_entity_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  640. dom_register_prop_handler(&dom_entity_prop_handlers, "publicId", sizeof("publicId")-1, dom_entity_public_id_read, NULL);
  641. dom_register_prop_handler(&dom_entity_prop_handlers, "systemId", sizeof("systemId")-1, dom_entity_system_id_read, NULL);
  642. dom_register_prop_handler(&dom_entity_prop_handlers, "notationName", sizeof("notationName")-1, dom_entity_notation_name_read, NULL);
  643. dom_register_prop_handler(&dom_entity_prop_handlers, "actualEncoding", sizeof("actualEncoding")-1, dom_entity_actual_encoding_read, NULL);
  644. dom_register_prop_handler(&dom_entity_prop_handlers, "encoding", sizeof("encoding")-1, dom_entity_encoding_read, NULL);
  645. dom_register_prop_handler(&dom_entity_prop_handlers, "version", sizeof("version")-1, dom_entity_version_read, NULL);
  646. zend_hash_merge(&dom_entity_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  647. zend_hash_add_ptr(&classes, dom_entity_class_entry->name, &dom_entity_prop_handlers);
  648. dom_entityreference_class_entry = register_class_DOMEntityReference(dom_node_class_entry);
  649. dom_entityreference_class_entry->create_object = dom_objects_new;
  650. zend_hash_add_ptr(&classes, dom_entityreference_class_entry->name, &dom_node_prop_handlers);
  651. dom_processinginstruction_class_entry = register_class_DOMProcessingInstruction(dom_node_class_entry);
  652. dom_processinginstruction_class_entry->create_object = dom_objects_new;
  653. zend_hash_init(&dom_processinginstruction_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  654. dom_register_prop_handler(&dom_processinginstruction_prop_handlers, "target", sizeof("target")-1, dom_processinginstruction_target_read, NULL);
  655. dom_register_prop_handler(&dom_processinginstruction_prop_handlers, "data", sizeof("data")-1, dom_processinginstruction_data_read, dom_processinginstruction_data_write);
  656. zend_hash_merge(&dom_processinginstruction_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
  657. zend_hash_add_ptr(&classes, dom_processinginstruction_class_entry->name, &dom_processinginstruction_prop_handlers);
  658. #ifdef LIBXML_XPATH_ENABLED
  659. memcpy(&dom_xpath_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
  660. dom_xpath_object_handlers.offset = XtOffsetOf(dom_xpath_object, dom) + XtOffsetOf(dom_object, std);
  661. dom_xpath_object_handlers.free_obj = dom_xpath_objects_free_storage;
  662. dom_xpath_class_entry = register_class_DOMXPath();
  663. dom_xpath_class_entry->create_object = dom_xpath_objects_new;
  664. zend_hash_init(&dom_xpath_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
  665. dom_register_prop_handler(&dom_xpath_prop_handlers, "document", sizeof("document")-1, dom_xpath_document_read, NULL);
  666. dom_register_prop_handler(&dom_xpath_prop_handlers, "registerNodeNamespaces", sizeof("registerNodeNamespaces")-1, dom_xpath_register_node_ns_read, dom_xpath_register_node_ns_write);
  667. zend_hash_add_ptr(&classes, dom_xpath_class_entry->name, &dom_xpath_prop_handlers);
  668. #endif
  669. REGISTER_LONG_CONSTANT("XML_ELEMENT_NODE", XML_ELEMENT_NODE, CONST_CS | CONST_PERSISTENT);
  670. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_NODE", XML_ATTRIBUTE_NODE, CONST_CS | CONST_PERSISTENT);
  671. REGISTER_LONG_CONSTANT("XML_TEXT_NODE", XML_TEXT_NODE, CONST_CS | CONST_PERSISTENT);
  672. REGISTER_LONG_CONSTANT("XML_CDATA_SECTION_NODE", XML_CDATA_SECTION_NODE, CONST_CS | CONST_PERSISTENT);
  673. REGISTER_LONG_CONSTANT("XML_ENTITY_REF_NODE", XML_ENTITY_REF_NODE, CONST_CS | CONST_PERSISTENT);
  674. REGISTER_LONG_CONSTANT("XML_ENTITY_NODE", XML_ENTITY_NODE, CONST_CS | CONST_PERSISTENT);
  675. REGISTER_LONG_CONSTANT("XML_PI_NODE", XML_PI_NODE, CONST_CS | CONST_PERSISTENT);
  676. REGISTER_LONG_CONSTANT("XML_COMMENT_NODE", XML_COMMENT_NODE, CONST_CS | CONST_PERSISTENT);
  677. REGISTER_LONG_CONSTANT("XML_DOCUMENT_NODE", XML_DOCUMENT_NODE, CONST_CS | CONST_PERSISTENT);
  678. REGISTER_LONG_CONSTANT("XML_DOCUMENT_TYPE_NODE", XML_DOCUMENT_TYPE_NODE, CONST_CS | CONST_PERSISTENT);
  679. REGISTER_LONG_CONSTANT("XML_DOCUMENT_FRAG_NODE", XML_DOCUMENT_FRAG_NODE, CONST_CS | CONST_PERSISTENT);
  680. REGISTER_LONG_CONSTANT("XML_NOTATION_NODE", XML_NOTATION_NODE, CONST_CS | CONST_PERSISTENT);
  681. REGISTER_LONG_CONSTANT("XML_HTML_DOCUMENT_NODE", XML_HTML_DOCUMENT_NODE, CONST_CS | CONST_PERSISTENT);
  682. REGISTER_LONG_CONSTANT("XML_DTD_NODE", XML_DTD_NODE, CONST_CS | CONST_PERSISTENT);
  683. REGISTER_LONG_CONSTANT("XML_ELEMENT_DECL_NODE", XML_ELEMENT_DECL, CONST_CS | CONST_PERSISTENT);
  684. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_DECL_NODE", XML_ATTRIBUTE_DECL, CONST_CS | CONST_PERSISTENT);
  685. REGISTER_LONG_CONSTANT("XML_ENTITY_DECL_NODE", XML_ENTITY_DECL, CONST_CS | CONST_PERSISTENT);
  686. REGISTER_LONG_CONSTANT("XML_NAMESPACE_DECL_NODE", XML_NAMESPACE_DECL, CONST_CS | CONST_PERSISTENT);
  687. #ifdef XML_GLOBAL_NAMESPACE
  688. REGISTER_LONG_CONSTANT("XML_GLOBAL_NAMESPACE", XML_GLOBAL_NAMESPACE, CONST_CS | CONST_PERSISTENT);
  689. #endif
  690. REGISTER_LONG_CONSTANT("XML_LOCAL_NAMESPACE", XML_LOCAL_NAMESPACE, CONST_CS | CONST_PERSISTENT);
  691. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_CDATA", XML_ATTRIBUTE_CDATA, CONST_CS | CONST_PERSISTENT);
  692. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_ID", XML_ATTRIBUTE_ID, CONST_CS | CONST_PERSISTENT);
  693. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_IDREF", XML_ATTRIBUTE_IDREF, CONST_CS | CONST_PERSISTENT);
  694. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_IDREFS", XML_ATTRIBUTE_IDREFS, CONST_CS | CONST_PERSISTENT);
  695. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_ENTITY", XML_ATTRIBUTE_ENTITIES, CONST_CS | CONST_PERSISTENT);
  696. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_NMTOKEN", XML_ATTRIBUTE_NMTOKEN, CONST_CS | CONST_PERSISTENT);
  697. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_NMTOKENS", XML_ATTRIBUTE_NMTOKENS, CONST_CS | CONST_PERSISTENT);
  698. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_ENUMERATION", XML_ATTRIBUTE_ENUMERATION, CONST_CS | CONST_PERSISTENT);
  699. REGISTER_LONG_CONSTANT("XML_ATTRIBUTE_NOTATION", XML_ATTRIBUTE_NOTATION, CONST_CS | CONST_PERSISTENT);
  700. /* DOMException Codes */
  701. REGISTER_LONG_CONSTANT("DOM_PHP_ERR", PHP_ERR, CONST_CS | CONST_PERSISTENT);
  702. REGISTER_LONG_CONSTANT("DOM_INDEX_SIZE_ERR", INDEX_SIZE_ERR, CONST_CS | CONST_PERSISTENT);
  703. REGISTER_LONG_CONSTANT("DOMSTRING_SIZE_ERR", DOMSTRING_SIZE_ERR, CONST_CS | CONST_PERSISTENT);
  704. REGISTER_LONG_CONSTANT("DOM_HIERARCHY_REQUEST_ERR", HIERARCHY_REQUEST_ERR, CONST_CS | CONST_PERSISTENT);
  705. REGISTER_LONG_CONSTANT("DOM_WRONG_DOCUMENT_ERR", WRONG_DOCUMENT_ERR, CONST_CS | CONST_PERSISTENT);
  706. REGISTER_LONG_CONSTANT("DOM_INVALID_CHARACTER_ERR", INVALID_CHARACTER_ERR, CONST_CS | CONST_PERSISTENT);
  707. REGISTER_LONG_CONSTANT("DOM_NO_DATA_ALLOWED_ERR", NO_DATA_ALLOWED_ERR, CONST_CS | CONST_PERSISTENT);
  708. REGISTER_LONG_CONSTANT("DOM_NO_MODIFICATION_ALLOWED_ERR", NO_MODIFICATION_ALLOWED_ERR, CONST_CS | CONST_PERSISTENT);
  709. REGISTER_LONG_CONSTANT("DOM_NOT_FOUND_ERR", NOT_FOUND_ERR, CONST_CS | CONST_PERSISTENT);
  710. REGISTER_LONG_CONSTANT("DOM_NOT_SUPPORTED_ERR", NOT_SUPPORTED_ERR, CONST_CS | CONST_PERSISTENT);
  711. REGISTER_LONG_CONSTANT("DOM_INUSE_ATTRIBUTE_ERR", INUSE_ATTRIBUTE_ERR, CONST_CS | CONST_PERSISTENT);
  712. REGISTER_LONG_CONSTANT("DOM_INVALID_STATE_ERR", INVALID_STATE_ERR, CONST_CS | CONST_PERSISTENT);
  713. REGISTER_LONG_CONSTANT("DOM_SYNTAX_ERR", SYNTAX_ERR, CONST_CS | CONST_PERSISTENT);
  714. REGISTER_LONG_CONSTANT("DOM_INVALID_MODIFICATION_ERR", INVALID_MODIFICATION_ERR, CONST_CS | CONST_PERSISTENT);
  715. REGISTER_LONG_CONSTANT("DOM_NAMESPACE_ERR", NAMESPACE_ERR, CONST_CS | CONST_PERSISTENT);
  716. REGISTER_LONG_CONSTANT("DOM_INVALID_ACCESS_ERR", INVALID_ACCESS_ERR, CONST_CS | CONST_PERSISTENT);
  717. REGISTER_LONG_CONSTANT("DOM_VALIDATION_ERR", VALIDATION_ERR, CONST_CS | CONST_PERSISTENT);
  718. php_libxml_register_export(dom_node_class_entry, php_dom_export_node);
  719. return SUCCESS;
  720. }
  721. /* }}} */
  722. /* {{{ */
  723. PHP_MINFO_FUNCTION(dom)
  724. {
  725. php_info_print_table_start();
  726. php_info_print_table_row(2, "DOM/XML", "enabled");
  727. php_info_print_table_row(2, "DOM/XML API Version", DOM_API_VERSION);
  728. php_info_print_table_row(2, "libxml Version", LIBXML_DOTTED_VERSION);
  729. #ifdef LIBXML_HTML_ENABLED
  730. php_info_print_table_row(2, "HTML Support", "enabled");
  731. #endif
  732. #ifdef LIBXML_XPATH_ENABLED
  733. php_info_print_table_row(2, "XPath Support", "enabled");
  734. #endif
  735. #ifdef LIBXML_XPTR_ENABLED
  736. php_info_print_table_row(2, "XPointer Support", "enabled");
  737. #endif
  738. #ifdef LIBXML_SCHEMAS_ENABLED
  739. php_info_print_table_row(2, "Schema Support", "enabled");
  740. php_info_print_table_row(2, "RelaxNG Support", "enabled");
  741. #endif
  742. php_info_print_table_end();
  743. }
  744. /* }}} */
  745. PHP_MSHUTDOWN_FUNCTION(dom) /* {{{ */
  746. {
  747. zend_hash_destroy(&dom_document_prop_handlers);
  748. zend_hash_destroy(&dom_documentfragment_prop_handlers);
  749. zend_hash_destroy(&dom_node_prop_handlers);
  750. zend_hash_destroy(&dom_namespace_node_prop_handlers);
  751. zend_hash_destroy(&dom_nodelist_prop_handlers);
  752. zend_hash_destroy(&dom_namednodemap_prop_handlers);
  753. zend_hash_destroy(&dom_characterdata_prop_handlers);
  754. zend_hash_destroy(&dom_attr_prop_handlers);
  755. zend_hash_destroy(&dom_element_prop_handlers);
  756. zend_hash_destroy(&dom_text_prop_handlers);
  757. zend_hash_destroy(&dom_documenttype_prop_handlers);
  758. zend_hash_destroy(&dom_notation_prop_handlers);
  759. zend_hash_destroy(&dom_entity_prop_handlers);
  760. zend_hash_destroy(&dom_processinginstruction_prop_handlers);
  761. #ifdef LIBXML_XPATH_ENABLED
  762. zend_hash_destroy(&dom_xpath_prop_handlers);
  763. #endif
  764. zend_hash_destroy(&classes);
  765. /* If you want do find memleaks in this module, compile libxml2 with --with-mem-debug and
  766. uncomment the following line, this will tell you the amount of not freed memory
  767. and the total used memory into apaches error_log */
  768. /* xmlMemoryDump();*/
  769. return SUCCESS;
  770. }
  771. /* }}} */
  772. /* {{{ node_list_unlink */
  773. void node_list_unlink(xmlNodePtr node)
  774. {
  775. dom_object *wrapper;
  776. while (node != NULL) {
  777. wrapper = php_dom_object_get_data(node);
  778. if (wrapper != NULL ) {
  779. xmlUnlinkNode(node);
  780. } else {
  781. if (node->type == XML_ENTITY_REF_NODE)
  782. break;
  783. node_list_unlink(node->children);
  784. switch (node->type) {
  785. case XML_ATTRIBUTE_DECL:
  786. case XML_DTD_NODE:
  787. case XML_DOCUMENT_TYPE_NODE:
  788. case XML_ENTITY_DECL:
  789. case XML_ATTRIBUTE_NODE:
  790. case XML_TEXT_NODE:
  791. break;
  792. default:
  793. node_list_unlink((xmlNodePtr) node->properties);
  794. }
  795. }
  796. node = node->next;
  797. }
  798. }
  799. /* }}} end node_list_unlink */
  800. #ifdef LIBXML_XPATH_ENABLED
  801. /* {{{ dom_xpath_objects_free_storage */
  802. void dom_xpath_objects_free_storage(zend_object *object)
  803. {
  804. dom_xpath_object *intern = php_xpath_obj_from_obj(object);
  805. zend_object_std_dtor(&intern->dom.std);
  806. if (intern->dom.ptr != NULL) {
  807. xmlXPathFreeContext((xmlXPathContextPtr) intern->dom.ptr);
  808. php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom);
  809. }
  810. if (intern->registered_phpfunctions) {
  811. zend_hash_destroy(intern->registered_phpfunctions);
  812. FREE_HASHTABLE(intern->registered_phpfunctions);
  813. }
  814. if (intern->node_list) {
  815. zend_hash_destroy(intern->node_list);
  816. FREE_HASHTABLE(intern->node_list);
  817. }
  818. }
  819. /* }}} */
  820. #endif
  821. /* {{{ dom_objects_free_storage */
  822. void dom_objects_free_storage(zend_object *object)
  823. {
  824. dom_object *intern = php_dom_obj_from_obj(object);
  825. #if defined(__GNUC__) && __GNUC__ >= 3
  826. int retcount __attribute__((unused)); /* keep compiler quiet */
  827. #else
  828. int retcount;
  829. #endif
  830. zend_object_std_dtor(&intern->std);
  831. if (intern->ptr != NULL && ((php_libxml_node_ptr *)intern->ptr)->node != NULL) {
  832. if (((xmlNodePtr) ((php_libxml_node_ptr *)intern->ptr)->node)->type != XML_DOCUMENT_NODE && ((xmlNodePtr) ((php_libxml_node_ptr *)intern->ptr)->node)->type != XML_HTML_DOCUMENT_NODE) {
  833. php_libxml_node_decrement_resource((php_libxml_node_object *) intern);
  834. } else {
  835. php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
  836. retcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
  837. }
  838. intern->ptr = NULL;
  839. }
  840. }
  841. /* }}} */
  842. void dom_namednode_iter(dom_object *basenode, int ntype, dom_object *intern, xmlHashTablePtr ht, xmlChar *local, xmlChar *ns) /* {{{ */
  843. {
  844. dom_nnodemap_object *mapptr = (dom_nnodemap_object *) intern->ptr;
  845. ZEND_ASSERT(basenode != NULL);
  846. ZVAL_OBJ_COPY(&mapptr->baseobj_zv, &basenode->std);
  847. mapptr->baseobj = basenode;
  848. mapptr->nodetype = ntype;
  849. mapptr->ht = ht;
  850. mapptr->local = local;
  851. mapptr->ns = ns;
  852. }
  853. /* }}} */
  854. static dom_object* dom_objects_set_class(zend_class_entry *class_type) /* {{{ */
  855. {
  856. dom_object *intern = zend_object_alloc(sizeof(dom_object), class_type);
  857. zend_class_entry *base_class = class_type;
  858. while ((base_class->type != ZEND_INTERNAL_CLASS || base_class->info.internal.module->module_number != dom_module_entry.module_number) && base_class->parent != NULL) {
  859. base_class = base_class->parent;
  860. }
  861. intern->prop_handler = zend_hash_find_ptr(&classes, base_class->name);
  862. zend_object_std_init(&intern->std, class_type);
  863. object_properties_init(&intern->std, class_type);
  864. return intern;
  865. }
  866. /* }}} */
  867. /* {{{ dom_objects_new */
  868. zend_object *dom_objects_new(zend_class_entry *class_type)
  869. {
  870. dom_object *intern = dom_objects_set_class(class_type);
  871. intern->std.handlers = dom_get_obj_handlers();
  872. return &intern->std;
  873. }
  874. /* }}} */
  875. #ifdef LIBXML_XPATH_ENABLED
  876. /* {{{ zend_object dom_xpath_objects_new(zend_class_entry *class_type) */
  877. zend_object *dom_xpath_objects_new(zend_class_entry *class_type)
  878. {
  879. dom_xpath_object *intern = zend_object_alloc(sizeof(dom_xpath_object), class_type);
  880. intern->registered_phpfunctions = zend_new_array(0);
  881. intern->register_node_ns = 1;
  882. intern->dom.prop_handler = &dom_xpath_prop_handlers;
  883. intern->dom.std.handlers = &dom_xpath_object_handlers;
  884. zend_object_std_init(&intern->dom.std, class_type);
  885. object_properties_init(&intern->dom.std, class_type);
  886. return &intern->dom.std;
  887. }
  888. /* }}} */
  889. #endif
  890. void dom_nnodemap_objects_free_storage(zend_object *object) /* {{{ */
  891. {
  892. dom_object *intern = php_dom_obj_from_obj(object);
  893. dom_nnodemap_object *objmap = (dom_nnodemap_object *)intern->ptr;
  894. if (objmap) {
  895. if (objmap->local) {
  896. xmlFree(objmap->local);
  897. }
  898. if (objmap->ns) {
  899. xmlFree(objmap->ns);
  900. }
  901. if (!Z_ISUNDEF(objmap->baseobj_zv)) {
  902. zval_ptr_dtor(&objmap->baseobj_zv);
  903. }
  904. efree(objmap);
  905. intern->ptr = NULL;
  906. }
  907. php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
  908. zend_object_std_dtor(&intern->std);
  909. }
  910. /* }}} */
  911. zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type) /* {{{ */
  912. {
  913. dom_object *intern;
  914. dom_nnodemap_object *objmap;
  915. intern = dom_objects_set_class(class_type);
  916. intern->ptr = emalloc(sizeof(dom_nnodemap_object));
  917. objmap = (dom_nnodemap_object *)intern->ptr;
  918. ZVAL_UNDEF(&objmap->baseobj_zv);
  919. objmap->baseobj = NULL;
  920. objmap->nodetype = 0;
  921. objmap->ht = NULL;
  922. objmap->local = NULL;
  923. objmap->ns = NULL;
  924. intern->std.handlers = &dom_nnodemap_object_handlers;
  925. return &intern->std;
  926. }
  927. /* }}} */
  928. void php_dom_create_iterator(zval *return_value, int ce_type) /* {{{ */
  929. {
  930. zend_class_entry *ce;
  931. if (ce_type == DOM_NAMEDNODEMAP) {
  932. ce = dom_namednodemap_class_entry;
  933. } else {
  934. ce = dom_nodelist_class_entry;
  935. }
  936. object_init_ex(return_value, ce);
  937. }
  938. /* }}} */
  939. /* {{{ php_dom_create_object */
  940. PHP_DOM_EXPORT bool php_dom_create_object(xmlNodePtr obj, zval *return_value, dom_object *domobj)
  941. {
  942. zend_class_entry *ce;
  943. dom_object *intern;
  944. if (!obj) {
  945. ZVAL_NULL(return_value);
  946. return 0;
  947. }
  948. if ((intern = (dom_object *) php_dom_object_get_data((void *) obj))) {
  949. ZVAL_OBJ_COPY(return_value, &intern->std);
  950. return 1;
  951. }
  952. switch (obj->type) {
  953. case XML_DOCUMENT_NODE:
  954. case XML_HTML_DOCUMENT_NODE:
  955. {
  956. ce = dom_document_class_entry;
  957. break;
  958. }
  959. case XML_DTD_NODE:
  960. case XML_DOCUMENT_TYPE_NODE:
  961. {
  962. ce = dom_documenttype_class_entry;
  963. break;
  964. }
  965. case XML_ELEMENT_NODE:
  966. {
  967. ce = dom_element_class_entry;
  968. break;
  969. }
  970. case XML_ATTRIBUTE_NODE:
  971. {
  972. ce = dom_attr_class_entry;
  973. break;
  974. }
  975. case XML_TEXT_NODE:
  976. {
  977. ce = dom_text_class_entry;
  978. break;
  979. }
  980. case XML_COMMENT_NODE:
  981. {
  982. ce = dom_comment_class_entry;
  983. break;
  984. }
  985. case XML_PI_NODE:
  986. {
  987. ce = dom_processinginstruction_class_entry;
  988. break;
  989. }
  990. case XML_ENTITY_REF_NODE:
  991. {
  992. ce = dom_entityreference_class_entry;
  993. break;
  994. }
  995. case XML_ENTITY_DECL:
  996. case XML_ELEMENT_DECL:
  997. {
  998. ce = dom_entity_class_entry;
  999. break;
  1000. }
  1001. case XML_CDATA_SECTION_NODE:
  1002. {
  1003. ce = dom_cdatasection_class_entry;
  1004. break;
  1005. }
  1006. case XML_DOCUMENT_FRAG_NODE:
  1007. {
  1008. ce = dom_documentfragment_class_entry;
  1009. break;
  1010. }
  1011. case XML_NOTATION_NODE:
  1012. {
  1013. ce = dom_notation_class_entry;
  1014. break;
  1015. }
  1016. case XML_NAMESPACE_DECL:
  1017. {
  1018. ce = dom_namespace_node_class_entry;
  1019. break;
  1020. }
  1021. default:
  1022. /* TODO Convert to a ZEND assertion? */
  1023. zend_throw_error(NULL, "Unsupported node type: %d", obj->type);
  1024. ZVAL_NULL(return_value);
  1025. return 0;
  1026. }
  1027. if (domobj && domobj->document) {
  1028. ce = dom_get_doc_classmap(domobj->document, ce);
  1029. }
  1030. object_init_ex(return_value, ce);
  1031. intern = Z_DOMOBJ_P(return_value);
  1032. if (obj->doc != NULL) {
  1033. if (domobj != NULL) {
  1034. intern->document = domobj->document;
  1035. }
  1036. php_libxml_increment_doc_ref((php_libxml_node_object *)intern, obj->doc);
  1037. }
  1038. php_libxml_increment_node_ptr((php_libxml_node_object *)intern, obj, (void *)intern);
  1039. return 0;
  1040. }
  1041. /* }}} end php_domobject_new */
  1042. void php_dom_create_implementation(zval *retval) {
  1043. object_init_ex(retval, dom_domimplementation_class_entry);
  1044. }
  1045. /* {{{ int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child) */
  1046. int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child)
  1047. {
  1048. xmlNodePtr nodep;
  1049. if (parent == NULL || child == NULL || child->doc != parent->doc) {
  1050. return SUCCESS;
  1051. }
  1052. if (child->type == XML_DOCUMENT_NODE) {
  1053. return FAILURE;
  1054. }
  1055. nodep = parent;
  1056. while (nodep) {
  1057. if (nodep == child) {
  1058. return FAILURE;
  1059. }
  1060. nodep = nodep->parent;
  1061. }
  1062. return SUCCESS;
  1063. }
  1064. /* }}} end dom_hierarchy */
  1065. /* {{{ */
  1066. bool dom_has_feature(zend_string *feature, zend_string *version)
  1067. {
  1068. if (zend_string_equals_literal(version, "1.0")
  1069. || zend_string_equals_literal(version, "2.0")
  1070. || zend_string_equals_literal(version, "")
  1071. ) {
  1072. if (zend_string_equals_literal_ci(feature, "XML")
  1073. || (zend_string_equals_literal_ci(feature, "Core") && zend_string_equals_literal(version, "1.0"))
  1074. ) {
  1075. return true;
  1076. }
  1077. }
  1078. return false;
  1079. }
  1080. /* }}} end dom_has_feature */
  1081. xmlNode *dom_get_elements_by_tag_name_ns_raw(xmlNodePtr nodep, char *ns, char *local, int *cur, int index) /* {{{ */
  1082. {
  1083. xmlNodePtr ret = NULL;
  1084. while (nodep != NULL && (*cur <= index || index == -1)) {
  1085. if (nodep->type == XML_ELEMENT_NODE) {
  1086. if (xmlStrEqual(nodep->name, (xmlChar *)local) || xmlStrEqual((xmlChar *)"*", (xmlChar *)local)) {
  1087. if (ns == NULL || (!strcmp(ns, "") && nodep->ns == NULL) || (nodep->ns != NULL && (xmlStrEqual(nodep->ns->href, (xmlChar *)ns) || xmlStrEqual((xmlChar *)"*", (xmlChar *)ns)))) {
  1088. if (*cur == index) {
  1089. ret = nodep;
  1090. break;
  1091. }
  1092. (*cur)++;
  1093. }
  1094. }
  1095. ret = dom_get_elements_by_tag_name_ns_raw(nodep->children, ns, local, cur, index);
  1096. if (ret != NULL) {
  1097. break;
  1098. }
  1099. }
  1100. nodep = nodep->next;
  1101. }
  1102. return ret;
  1103. }
  1104. /* }}} */
  1105. /* }}} end dom_element_get_elements_by_tag_name_ns_raw */
  1106. static inline bool is_empty_node(xmlNodePtr nodep)
  1107. {
  1108. xmlChar *strContent = xmlNodeGetContent(nodep);
  1109. bool ret = strContent == NULL || *strContent == '\0';
  1110. xmlFree(strContent);
  1111. return ret;
  1112. }
  1113. /* {{{ void dom_normalize (xmlNodePtr nodep) */
  1114. void dom_normalize (xmlNodePtr nodep)
  1115. {
  1116. xmlNodePtr child, nextp, newnextp;
  1117. xmlAttrPtr attr;
  1118. xmlChar *strContent;
  1119. child = nodep->children;
  1120. while(child != NULL) {
  1121. switch (child->type) {
  1122. case XML_TEXT_NODE:
  1123. nextp = child->next;
  1124. while (nextp != NULL) {
  1125. if (nextp->type == XML_TEXT_NODE) {
  1126. newnextp = nextp->next;
  1127. strContent = xmlNodeGetContent(nextp);
  1128. xmlNodeAddContent(child, strContent);
  1129. xmlFree(strContent);
  1130. xmlUnlinkNode(nextp);
  1131. php_libxml_node_free_resource(nextp);
  1132. nextp = newnextp;
  1133. } else {
  1134. break;
  1135. }
  1136. }
  1137. if (is_empty_node(child)) {
  1138. nextp = child->next;
  1139. xmlUnlinkNode(child);
  1140. php_libxml_node_free_resource(child);
  1141. child = nextp;
  1142. continue;
  1143. }
  1144. break;
  1145. case XML_ELEMENT_NODE:
  1146. dom_normalize (child);
  1147. attr = child->properties;
  1148. while (attr != NULL) {
  1149. dom_normalize((xmlNodePtr) attr);
  1150. attr = attr->next;
  1151. }
  1152. break;
  1153. case XML_ATTRIBUTE_NODE:
  1154. dom_normalize (child);
  1155. break;
  1156. default:
  1157. break;
  1158. }
  1159. child = child->next;
  1160. }
  1161. }
  1162. /* }}} end dom_normalize */
  1163. /* {{{ void dom_set_old_ns(xmlDoc *doc, xmlNs *ns) */
  1164. void dom_set_old_ns(xmlDoc *doc, xmlNs *ns) {
  1165. xmlNs *cur;
  1166. if (doc == NULL)
  1167. return;
  1168. if (doc->oldNs == NULL) {
  1169. doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
  1170. if (doc->oldNs == NULL) {
  1171. return;
  1172. }
  1173. memset(doc->oldNs, 0, sizeof(xmlNs));
  1174. doc->oldNs->type = XML_LOCAL_NAMESPACE;
  1175. doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
  1176. doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
  1177. }
  1178. cur = doc->oldNs;
  1179. while (cur->next != NULL) {
  1180. cur = cur->next;
  1181. }
  1182. cur->next = ns;
  1183. }
  1184. /* }}} end dom_set_old_ns */
  1185. void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep) /* {{{ */
  1186. {
  1187. xmlNsPtr nsptr, nsdftptr, curns, prevns = NULL;
  1188. if (nodep->type == XML_ELEMENT_NODE) {
  1189. /* Following if block primarily used for inserting nodes created via createElementNS */
  1190. if (nodep->nsDef != NULL) {
  1191. curns = nodep->nsDef;
  1192. while (curns) {
  1193. nsdftptr = curns->next;
  1194. if (curns->href != NULL) {
  1195. if((nsptr = xmlSearchNsByHref(doc, nodep->parent, curns->href)) &&
  1196. (curns->prefix == NULL || xmlStrEqual(nsptr->prefix, curns->prefix))) {
  1197. curns->next = NULL;
  1198. if (prevns == NULL) {
  1199. nodep->nsDef = nsdftptr;
  1200. } else {
  1201. prevns->next = nsdftptr;
  1202. }
  1203. dom_set_old_ns(doc, curns);
  1204. curns = prevns;
  1205. }
  1206. }
  1207. prevns = curns;
  1208. curns = nsdftptr;
  1209. }
  1210. }
  1211. xmlReconciliateNs(doc, nodep);
  1212. }
  1213. }
  1214. /* }}} */
  1215. /*
  1216. http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-DocCrElNS
  1217. NAMESPACE_ERR: Raised if
  1218. 1. the qualifiedName is a malformed qualified name
  1219. 2. the qualifiedName has a prefix and the namespaceURI is null
  1220. */
  1221. /* {{{ int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, int name_len) */
  1222. int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, int name_len) {
  1223. if (name_len == 0) {
  1224. return NAMESPACE_ERR;
  1225. }
  1226. *localname = (char *)xmlSplitQName2((xmlChar *)qname, (xmlChar **) prefix);
  1227. if (*localname == NULL) {
  1228. *localname = (char *)xmlStrdup((xmlChar *)qname);
  1229. if (*prefix == NULL && uri_len == 0) {
  1230. return 0;
  1231. }
  1232. }
  1233. /* 1 */
  1234. if (xmlValidateQName((xmlChar *) qname, 0) != 0) {
  1235. return NAMESPACE_ERR;
  1236. }
  1237. /* 2 */
  1238. if (*prefix != NULL && uri_len == 0) {
  1239. return NAMESPACE_ERR;
  1240. }
  1241. return 0;
  1242. }
  1243. /* }}} */
  1244. /*
  1245. http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-DocCrElNS
  1246. NAMESPACE_ERR: Raised if
  1247. 3. the qualifiedName has a prefix that is "xml" and the namespaceURI is different from "http://www.w3.org/XML/1998/namespace" [XML Namespaces]
  1248. 4. the qualifiedName or its prefix is "xmlns" and the namespaceURI is different from "http://www.w3.org/2000/xmlns/"
  1249. 5. the namespaceURI is "http://www.w3.org/2000/xmlns/" and neither the qualifiedName nor its prefix is "xmlns".
  1250. */
  1251. /* {{{ xmlNsPtr dom_get_ns(xmlNodePtr nodep, char *uri, int *errorcode, char *prefix) */
  1252. xmlNsPtr dom_get_ns(xmlNodePtr nodep, char *uri, int *errorcode, char *prefix) {
  1253. xmlNsPtr nsptr = NULL;
  1254. *errorcode = 0;
  1255. if (! ((prefix && !strcmp (prefix, "xml") && strcmp(uri, (char *)XML_XML_NAMESPACE)) ||
  1256. (prefix && !strcmp (prefix, "xmlns") && strcmp(uri, (char *)DOM_XMLNS_NAMESPACE)) ||
  1257. (prefix && !strcmp(uri, (char *)DOM_XMLNS_NAMESPACE) && strcmp (prefix, "xmlns")))) {
  1258. nsptr = xmlNewNs(nodep, (xmlChar *)uri, (xmlChar *)prefix);
  1259. }
  1260. if (nsptr == NULL) {
  1261. *errorcode = NAMESPACE_ERR;
  1262. }
  1263. return nsptr;
  1264. }
  1265. /* }}} end dom_get_ns */
  1266. /* {{{ xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName) */
  1267. xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName) {
  1268. xmlNsPtr cur;
  1269. xmlNs *ret = NULL;
  1270. if (node == NULL)
  1271. return NULL;
  1272. if (localName == NULL || xmlStrEqual(localName, (xmlChar *)"")) {
  1273. cur = node->nsDef;
  1274. while (cur != NULL) {
  1275. if (cur->prefix == NULL && cur->href != NULL) {
  1276. ret = cur;
  1277. break;
  1278. }
  1279. cur = cur->next;
  1280. }
  1281. } else {
  1282. cur = node->nsDef;
  1283. while (cur != NULL) {
  1284. if (cur->prefix != NULL && xmlStrEqual(localName, cur->prefix)) {
  1285. ret = cur;
  1286. break;
  1287. }
  1288. cur = cur->next;
  1289. }
  1290. }
  1291. return ret;
  1292. }
  1293. /* }}} end dom_get_nsdecl */
  1294. static zval *dom_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
  1295. {
  1296. zval offset_copy;
  1297. if (!offset) {
  1298. zend_throw_error(NULL, "Cannot access node list without offset");
  1299. return NULL;
  1300. }
  1301. ZVAL_LONG(&offset_copy, zval_get_long(offset));
  1302. zend_call_method_with_1_params(object, object->ce, NULL, "item", rv, &offset_copy);
  1303. return rv;
  1304. } /* }}} end dom_nodelist_read_dimension */
  1305. static int dom_nodelist_has_dimension(zend_object *object, zval *member, int check_empty)
  1306. {
  1307. zend_long offset = zval_get_long(member);
  1308. zval rv;
  1309. if (offset < 0) {
  1310. return 0;
  1311. } else {
  1312. zval *length = zend_read_property(
  1313. object->ce, object, "length", sizeof("length") - 1, 0, &rv);
  1314. return length && offset < Z_LVAL_P(length);
  1315. }
  1316. } /* }}} end dom_nodelist_has_dimension */
  1317. #endif /* HAVE_DOM */