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