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