1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Christian Stocker <chregu@php.net> |
14 | Rob Richards <rrichards@php.net> |
15 | Marcus Borger <helly@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
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/random/php_random.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 zend_object_handlers dom_nodelist_object_handlers;
65 zend_object_handlers dom_object_namespace_node_handlers;
66 #ifdef LIBXML_XPATH_ENABLED
67 zend_object_handlers dom_xpath_object_handlers;
68 #endif
69
70 static HashTable classes;
71 /* {{{ prop handler tables */
72 static HashTable dom_document_prop_handlers;
73 static HashTable dom_documentfragment_prop_handlers;
74 static HashTable dom_node_prop_handlers;
75 static HashTable dom_entity_reference_prop_handlers;
76 static HashTable dom_nodelist_prop_handlers;
77 static HashTable dom_namednodemap_prop_handlers;
78 static HashTable dom_characterdata_prop_handlers;
79 static HashTable dom_attr_prop_handlers;
80 static HashTable dom_element_prop_handlers;
81 static HashTable dom_text_prop_handlers;
82 static HashTable dom_documenttype_prop_handlers;
83 static HashTable dom_notation_prop_handlers;
84 static HashTable dom_entity_prop_handlers;
85 static HashTable dom_processinginstruction_prop_handlers;
86 static HashTable dom_namespace_node_prop_handlers;
87 #ifdef LIBXML_XPATH_ENABLED
88 static HashTable dom_xpath_prop_handlers;
89 #endif
90 /* }}} */
91
92 static zend_object *dom_objects_namespace_node_new(zend_class_entry *class_type);
93 static void dom_object_namespace_node_free_storage(zend_object *object);
94 static xmlNodePtr php_dom_create_fake_namespace_decl_node_ptr(xmlNodePtr nodep, xmlNsPtr original);
95
96 typedef int (*dom_read_t)(dom_object *obj, zval *retval);
97 typedef int (*dom_write_t)(dom_object *obj, zval *newval);
98
99 typedef struct _dom_prop_handler {
100 dom_read_t read_func;
101 dom_write_t write_func;
102 } dom_prop_handler;
103
dom_get_obj_handlers(void)104 static zend_object_handlers* dom_get_obj_handlers(void) {
105 return &dom_object_handlers;
106 }
107
108 /* {{{ int dom_node_is_read_only(xmlNodePtr node) */
dom_node_is_read_only(xmlNodePtr node)109 int dom_node_is_read_only(xmlNodePtr node) {
110 switch (node->type) {
111 case XML_ENTITY_REF_NODE:
112 case XML_ENTITY_NODE:
113 case XML_DOCUMENT_TYPE_NODE:
114 case XML_NOTATION_NODE:
115 case XML_DTD_NODE:
116 case XML_ELEMENT_DECL:
117 case XML_ATTRIBUTE_DECL:
118 case XML_ENTITY_DECL:
119 case XML_NAMESPACE_DECL:
120 return SUCCESS;
121 break;
122 default:
123 if (node->doc == NULL) {
124 return SUCCESS;
125 } else {
126 return FAILURE;
127 }
128 }
129 }
130 /* }}} end dom_node_is_read_only */
131
132 /* {{{ int dom_node_children_valid(xmlNodePtr node) */
dom_node_children_valid(xmlNodePtr node)133 int dom_node_children_valid(xmlNodePtr node) {
134 switch (node->type) {
135 case XML_DOCUMENT_TYPE_NODE:
136 case XML_DTD_NODE:
137 case XML_PI_NODE:
138 case XML_COMMENT_NODE:
139 case XML_TEXT_NODE:
140 case XML_CDATA_SECTION_NODE:
141 case XML_NOTATION_NODE:
142 return FAILURE;
143 break;
144 default:
145 return SUCCESS;
146 }
147 }
148 /* }}} end dom_node_children_valid */
149
150 static const libxml_doc_props default_doc_props = {
151 .formatoutput = false,
152 .validateonparse = false,
153 .resolveexternals = false,
154 .preservewhitespace = true,
155 .substituteentities = false,
156 .stricterror = true,
157 .recover = false,
158 .classmap = NULL,
159 };
160
161 /* {{{ dom_get_doc_props() */
dom_get_doc_props(php_libxml_ref_obj * document)162 dom_doc_propsptr dom_get_doc_props(php_libxml_ref_obj *document)
163 {
164 dom_doc_propsptr doc_props;
165
166 if (document && document->doc_props) {
167 return document->doc_props;
168 } else {
169 doc_props = emalloc(sizeof(libxml_doc_props));
170 memcpy(doc_props, &default_doc_props, sizeof(libxml_doc_props));
171 if (document) {
172 document->doc_props = doc_props;
173 }
174 return doc_props;
175 }
176 }
177 /* }}} */
178
dom_get_doc_props_read_only(const php_libxml_ref_obj * document)179 libxml_doc_props const* dom_get_doc_props_read_only(const php_libxml_ref_obj *document)
180 {
181 if (document && document->doc_props) {
182 return document->doc_props;
183 } else {
184 return &default_doc_props;
185 }
186 }
187
dom_copy_doc_props(php_libxml_ref_obj * source_doc,php_libxml_ref_obj * dest_doc)188 static void dom_copy_doc_props(php_libxml_ref_obj *source_doc, php_libxml_ref_obj *dest_doc)
189 {
190 dom_doc_propsptr dest;
191
192 if (source_doc && dest_doc) {
193
194 libxml_doc_props const* source = dom_get_doc_props_read_only(source_doc);
195 dest = dom_get_doc_props(dest_doc);
196
197 dest->formatoutput = source->formatoutput;
198 dest->validateonparse = source->validateonparse;
199 dest->resolveexternals = source->resolveexternals;
200 dest->preservewhitespace = source->preservewhitespace;
201 dest->substituteentities = source->substituteentities;
202 dest->stricterror = source->stricterror;
203 dest->recover = source->recover;
204 if (source->classmap) {
205 ALLOC_HASHTABLE(dest->classmap);
206 zend_hash_init(dest->classmap, 0, NULL, NULL, 0);
207 zend_hash_copy(dest->classmap, source->classmap, NULL);
208 }
209
210 }
211 }
212
dom_set_doc_classmap(php_libxml_ref_obj * document,zend_class_entry * basece,zend_class_entry * ce)213 void dom_set_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece, zend_class_entry *ce)
214 {
215 dom_doc_propsptr doc_props;
216
217 if (document) {
218 doc_props = dom_get_doc_props(document);
219 if (doc_props->classmap == NULL) {
220 if (ce == NULL) {
221 return;
222 }
223 ALLOC_HASHTABLE(doc_props->classmap);
224 zend_hash_init(doc_props->classmap, 0, NULL, NULL, 0);
225 }
226 if (ce) {
227 zend_hash_update_ptr(doc_props->classmap, basece->name, ce);
228 } else {
229 zend_hash_del(doc_props->classmap, basece->name);
230 }
231 }
232 }
233
dom_get_doc_classmap(php_libxml_ref_obj * document,zend_class_entry * basece)234 zend_class_entry *dom_get_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece)
235 {
236 if (document) {
237 libxml_doc_props const* doc_props = dom_get_doc_props_read_only(document);
238 if (doc_props->classmap) {
239 zend_class_entry *ce = zend_hash_find_ptr(doc_props->classmap, basece->name);
240 if (ce) {
241 return ce;
242 }
243 }
244 }
245
246 return basece;
247 }
248 /* }}} */
249
250 /* {{{ dom_get_strict_error() */
dom_get_strict_error(php_libxml_ref_obj * document)251 int dom_get_strict_error(php_libxml_ref_obj *document) {
252 return dom_get_doc_props_read_only(document)->stricterror;
253 }
254 /* }}} */
255
256 /* {{{ xmlNodePtr dom_object_get_node(dom_object *obj) */
dom_object_get_node(dom_object * obj)257 PHP_DOM_EXPORT xmlNodePtr dom_object_get_node(dom_object *obj)
258 {
259 if (obj && obj->ptr != NULL) {
260 return ((php_libxml_node_ptr *)obj->ptr)->node;
261 } else {
262 return NULL;
263 }
264 }
265 /* }}} end dom_object_get_node */
266
267 /* {{{ dom_object *php_dom_object_get_data(xmlNodePtr obj) */
php_dom_object_get_data(xmlNodePtr obj)268 PHP_DOM_EXPORT dom_object *php_dom_object_get_data(xmlNodePtr obj)
269 {
270 if (obj && obj->_private != NULL) {
271 return (dom_object *) ((php_libxml_node_ptr *) obj->_private)->_private;
272 } else {
273 return NULL;
274 }
275 }
276 /* }}} end php_dom_object_get_data */
277
dom_register_prop_handler(HashTable * prop_handler,char * name,size_t name_len,dom_read_t read_func,dom_write_t write_func)278 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)
279 {
280 dom_prop_handler hnd;
281 zend_string *str;
282
283 hnd.read_func = read_func;
284 hnd.write_func = write_func;
285 str = zend_string_init_interned(name, name_len, 1);
286 zend_hash_add_mem(prop_handler, str, &hnd, sizeof(dom_prop_handler));
287 zend_string_release_ex(str, 1);
288 }
289
dom_override_prop_handler(HashTable * prop_handler,char * name,size_t name_len,dom_read_t read_func,dom_write_t write_func)290 static void dom_override_prop_handler(HashTable *prop_handler, char *name, size_t name_len, dom_read_t read_func, dom_write_t write_func)
291 {
292 dom_prop_handler hnd;
293 hnd.read_func = read_func;
294 hnd.write_func = write_func;
295 zend_hash_str_update_mem(prop_handler, name, name_len, &hnd, sizeof(dom_prop_handler));
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
302 if (!obj->prop_handler || !zend_hash_exists(obj->prop_handler, name)) {
303 return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
304 }
305
306 return NULL;
307 }
308
309 /* {{{ dom_read_property */
dom_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)310 zval *dom_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
311 {
312 dom_object *obj = php_dom_obj_from_obj(object);
313 zval *retval;
314 dom_prop_handler *hnd = NULL;
315
316 if (obj->prop_handler != NULL) {
317 hnd = zend_hash_find_ptr(obj->prop_handler, name);
318 }
319
320 if (hnd) {
321 int ret = hnd->read_func(obj, rv);
322 if (ret == SUCCESS) {
323 retval = rv;
324 } else {
325 retval = &EG(uninitialized_zval);
326 }
327 } else {
328 retval = zend_std_read_property(object, name, type, cache_slot, rv);
329 }
330
331 return retval;
332 }
333 /* }}} */
334
dom_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)335 zval *dom_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
336 {
337 dom_object *obj = php_dom_obj_from_obj(object);
338 dom_prop_handler *hnd = NULL;
339
340 if (obj->prop_handler != NULL) {
341 hnd = zend_hash_find_ptr(obj->prop_handler, name);
342 }
343
344 if (hnd) {
345 if (!hnd->write_func) {
346 zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
347 return &EG(error_zval);
348 }
349
350 zend_property_info *prop = zend_get_property_info(object->ce, name, /* silent */ true);
351 if (prop && ZEND_TYPE_IS_SET(prop->type)) {
352 zval tmp;
353 ZVAL_COPY(&tmp, value);
354 if (!zend_verify_property_type(prop, &tmp, ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))) {
355 zval_ptr_dtor(&tmp);
356 return &EG(error_zval);
357 }
358 hnd->write_func(obj, &tmp);
359 zval_ptr_dtor(&tmp);
360 } else {
361 hnd->write_func(obj, value);
362 }
363
364 return value;
365 }
366
367 return zend_std_write_property(object, name, value, cache_slot);
368 }
369
370 /* {{{ dom_property_exists */
dom_property_exists(zend_object * object,zend_string * name,int check_empty,void ** cache_slot)371 static int dom_property_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
372 {
373 dom_object *obj = php_dom_obj_from_obj(object);
374 dom_prop_handler *hnd = NULL;
375 int retval = 0;
376
377 if (obj->prop_handler != NULL) {
378 hnd = zend_hash_find_ptr(obj->prop_handler, name);
379 }
380 if (hnd) {
381 zval tmp;
382
383 if (check_empty == 2) {
384 retval = 1;
385 } else if (hnd->read_func(obj, &tmp) == SUCCESS) {
386 if (check_empty == 1) {
387 retval = zend_is_true(&tmp);
388 } else if (check_empty == 0) {
389 retval = (Z_TYPE(tmp) != IS_NULL);
390 }
391 zval_ptr_dtor(&tmp);
392 }
393 } else {
394 retval = zend_std_has_property(object, name, check_empty, cache_slot);
395 }
396
397 return retval;
398 }
399 /* }}} */
400
dom_get_debug_info_helper(zend_object * object,int * is_temp)401 static HashTable* dom_get_debug_info_helper(zend_object *object, int *is_temp) /* {{{ */
402 {
403 dom_object *obj = php_dom_obj_from_obj(object);
404 HashTable *debug_info,
405 *prop_handlers = obj->prop_handler,
406 *std_props;
407 zend_string *string_key;
408 dom_prop_handler *entry;
409 zend_string *object_str;
410
411 *is_temp = 1;
412
413 std_props = zend_std_get_properties(object);
414 debug_info = zend_array_dup(std_props);
415
416 if (!prop_handlers) {
417 return debug_info;
418 }
419
420 object_str = ZSTR_INIT_LITERAL("(object value omitted)", 0);
421
422 ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(prop_handlers, string_key, entry) {
423 zval value;
424
425 ZEND_ASSERT(string_key != NULL);
426
427 if (entry->read_func(obj, &value) == FAILURE) {
428 continue;
429 }
430
431 if (Z_TYPE(value) == IS_OBJECT) {
432 zval_ptr_dtor(&value);
433 ZVAL_NEW_STR(&value, object_str);
434 zend_string_addref(object_str);
435 }
436
437 zend_hash_update(debug_info, string_key, &value);
438 } ZEND_HASH_FOREACH_END();
439
440 zend_string_release_ex(object_str, 0);
441
442 return debug_info;
443 }
444 /* }}} */
445
dom_get_debug_info(zend_object * object,int * is_temp)446 static HashTable* dom_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
447 {
448 return dom_get_debug_info_helper(object, is_temp);
449 }
450 /* }}} */
451
php_dom_export_node(zval * object)452 void *php_dom_export_node(zval *object) /* {{{ */
453 {
454 php_libxml_node_object *intern;
455 xmlNodePtr nodep = NULL;
456
457 intern = (php_libxml_node_object *) Z_DOMOBJ_P(object);
458 if (intern->node) {
459 nodep = intern->node->node;
460 }
461
462 return nodep;
463 }
464 /* }}} */
465
466 /* {{{ Get a simplexml_element object from dom to allow for processing */
PHP_FUNCTION(dom_import_simplexml)467 PHP_FUNCTION(dom_import_simplexml)
468 {
469 zval *node;
470 xmlNodePtr nodep = NULL;
471 php_libxml_node_object *nodeobj;
472 int ret;
473
474 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &node) == FAILURE) {
475 RETURN_THROWS();
476 }
477
478 nodeobj = (php_libxml_node_object *) ((char *) Z_OBJ_P(node) - Z_OBJ_HT_P(node)->offset);
479 nodep = php_libxml_import_node(node);
480
481 if (nodep && nodeobj && (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE)) {
482 DOM_RET_OBJ((xmlNodePtr) nodep, &ret, (dom_object *)nodeobj);
483 } else {
484 zend_argument_value_error(1, "is not a valid node type");
485 RETURN_THROWS();
486 }
487 }
488 /* }}} */
489
490 static dom_object* dom_objects_set_class(zend_class_entry *class_type);
491
dom_update_refcount_after_clone(dom_object * original,xmlNodePtr original_node,dom_object * clone,xmlNodePtr cloned_node)492 static void dom_update_refcount_after_clone(dom_object *original, xmlNodePtr original_node, dom_object *clone, xmlNodePtr cloned_node)
493 {
494 /* If we cloned a document then we must create new doc proxy */
495 if (cloned_node->doc == original_node->doc) {
496 clone->document = original->document;
497 }
498 php_libxml_increment_doc_ref((php_libxml_node_object *)clone, cloned_node->doc);
499 php_libxml_increment_node_ptr((php_libxml_node_object *)clone, cloned_node, (void *)clone);
500 if (original->document != clone->document) {
501 dom_copy_doc_props(original->document, clone->document);
502 }
503 }
504
dom_objects_store_clone_obj(zend_object * zobject)505 static zend_object *dom_objects_store_clone_obj(zend_object *zobject) /* {{{ */
506 {
507 dom_object *intern = php_dom_obj_from_obj(zobject);
508 dom_object *clone = dom_objects_set_class(intern->std.ce);
509
510 clone->std.handlers = dom_get_obj_handlers();
511
512 if (instanceof_function(intern->std.ce, dom_node_class_entry)) {
513 xmlNodePtr node = (xmlNodePtr)dom_object_get_node(intern);
514 if (node != NULL) {
515 xmlNodePtr cloned_node = xmlDocCopyNode(node, node->doc, 1);
516 if (cloned_node != NULL) {
517 dom_update_refcount_after_clone(intern, node, clone, cloned_node);
518 }
519
520 }
521 }
522
523 zend_objects_clone_members(&clone->std, &intern->std);
524
525 return &clone->std;
526 }
527 /* }}} */
528
dom_object_namespace_node_clone_obj(zend_object * zobject)529 static zend_object *dom_object_namespace_node_clone_obj(zend_object *zobject)
530 {
531 dom_object_namespace_node *intern = php_dom_namespace_node_obj_from_obj(zobject);
532 zend_object *clone = dom_objects_namespace_node_new(intern->dom.std.ce);
533 dom_object_namespace_node *clone_intern = php_dom_namespace_node_obj_from_obj(clone);
534
535 xmlNodePtr original_node = dom_object_get_node(&intern->dom);
536 ZEND_ASSERT(original_node->type == XML_NAMESPACE_DECL);
537 xmlNodePtr cloned_node = php_dom_create_fake_namespace_decl_node_ptr(original_node->parent, original_node->ns);
538
539 if (intern->parent_intern) {
540 clone_intern->parent_intern = intern->parent_intern;
541 GC_ADDREF(&clone_intern->parent_intern->std);
542 }
543 dom_update_refcount_after_clone(&intern->dom, original_node, &clone_intern->dom, cloned_node);
544
545 zend_objects_clone_members(clone, &intern->dom.std);
546 return clone;
547 }
548
dom_copy_prop_handler(zval * zv)549 static void dom_copy_prop_handler(zval *zv) /* {{{ */
550 {
551 dom_prop_handler *hnd = Z_PTR_P(zv);
552 Z_PTR_P(zv) = malloc(sizeof(dom_prop_handler));
553 memcpy(Z_PTR_P(zv), hnd, sizeof(dom_prop_handler));
554 }
555 /* }}} */
556
dom_dtor_prop_handler(zval * zv)557 static void dom_dtor_prop_handler(zval *zv) /* {{{ */
558 {
559 free(Z_PTR_P(zv));
560 }
561
562 static const zend_module_dep dom_deps[] = {
563 ZEND_MOD_REQUIRED("libxml")
564 ZEND_MOD_CONFLICTS("domxml")
565 ZEND_MOD_END
566 };
567
568 zend_module_entry dom_module_entry = { /* {{{ */
569 STANDARD_MODULE_HEADER_EX, NULL,
570 dom_deps,
571 "dom",
572 ext_functions,
573 PHP_MINIT(dom),
574 PHP_MSHUTDOWN(dom),
575 NULL,
576 NULL,
577 PHP_MINFO(dom),
578 DOM_API_VERSION, /* Extension versionnumber */
579 STANDARD_MODULE_PROPERTIES
580 };
581 /* }}} */
582
583 #ifdef COMPILE_DL_DOM
584 ZEND_GET_MODULE(dom)
585 #endif
586
587 void dom_objects_free_storage(zend_object *object);
588 void dom_nnodemap_objects_free_storage(zend_object *object);
589 static zval *dom_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
590 static int dom_nodelist_has_dimension(zend_object *object, zval *member, int check_empty);
591 static zval *dom_nodemap_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
592 static int dom_nodemap_has_dimension(zend_object *object, zval *member, int check_empty);
593 static zend_object *dom_objects_store_clone_obj(zend_object *zobject);
594 #ifdef LIBXML_XPATH_ENABLED
595 void dom_xpath_objects_free_storage(zend_object *object);
596 #endif
597
598 /* {{{ PHP_MINIT_FUNCTION(dom) */
PHP_MINIT_FUNCTION(dom)599 PHP_MINIT_FUNCTION(dom)
600 {
601 memcpy(&dom_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
602 dom_object_handlers.offset = XtOffsetOf(dom_object, std);
603 dom_object_handlers.free_obj = dom_objects_free_storage;
604 dom_object_handlers.read_property = dom_read_property;
605 dom_object_handlers.write_property = dom_write_property;
606 dom_object_handlers.get_property_ptr_ptr = dom_get_property_ptr_ptr;
607 dom_object_handlers.clone_obj = dom_objects_store_clone_obj;
608 dom_object_handlers.has_property = dom_property_exists;
609 dom_object_handlers.get_debug_info = dom_get_debug_info;
610
611 memcpy(&dom_nnodemap_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
612 dom_nnodemap_object_handlers.free_obj = dom_nnodemap_objects_free_storage;
613 dom_nnodemap_object_handlers.read_dimension = dom_nodemap_read_dimension;
614 dom_nnodemap_object_handlers.has_dimension = dom_nodemap_has_dimension;
615
616 memcpy(&dom_nodelist_object_handlers, &dom_nnodemap_object_handlers, sizeof(zend_object_handlers));
617 dom_nodelist_object_handlers.read_dimension = dom_nodelist_read_dimension;
618 dom_nodelist_object_handlers.has_dimension = dom_nodelist_has_dimension;
619
620 memcpy(&dom_object_namespace_node_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
621 dom_object_namespace_node_handlers.offset = XtOffsetOf(dom_object_namespace_node, dom.std);
622 dom_object_namespace_node_handlers.free_obj = dom_object_namespace_node_free_storage;
623 dom_object_namespace_node_handlers.clone_obj = dom_object_namespace_node_clone_obj;
624
625 zend_hash_init(&classes, 0, NULL, NULL, 1);
626
627 dom_domexception_class_entry = register_class_DOMException(zend_ce_exception);
628
629 dom_parentnode_class_entry = register_class_DOMParentNode();
630
631 dom_childnode_class_entry = register_class_DOMChildNode();
632
633 dom_domimplementation_class_entry = register_class_DOMImplementation();
634 dom_domimplementation_class_entry->create_object = dom_objects_new;
635
636 dom_node_class_entry = register_class_DOMNode();
637 dom_node_class_entry->create_object = dom_objects_new;
638
639 zend_hash_init(&dom_node_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
640 dom_register_prop_handler(&dom_node_prop_handlers, "nodeName", sizeof("nodeName")-1, dom_node_node_name_read, NULL);
641 dom_register_prop_handler(&dom_node_prop_handlers, "nodeValue", sizeof("nodeValue")-1, dom_node_node_value_read, dom_node_node_value_write);
642 dom_register_prop_handler(&dom_node_prop_handlers, "nodeType", sizeof("nodeType")-1, dom_node_node_type_read, NULL);
643 dom_register_prop_handler(&dom_node_prop_handlers, "parentNode", sizeof("parentNode")-1, dom_node_parent_node_read, NULL);
644 dom_register_prop_handler(&dom_node_prop_handlers, "parentElement", sizeof("parentElement")-1, dom_node_parent_element_read, NULL);
645 dom_register_prop_handler(&dom_node_prop_handlers, "childNodes", sizeof("childNodes")-1, dom_node_child_nodes_read, NULL);
646 dom_register_prop_handler(&dom_node_prop_handlers, "firstChild", sizeof("firstChild")-1, dom_node_first_child_read, NULL);
647 dom_register_prop_handler(&dom_node_prop_handlers, "lastChild", sizeof("lastChild")-1, dom_node_last_child_read, NULL);
648 dom_register_prop_handler(&dom_node_prop_handlers, "previousSibling", sizeof("previousSibling")-1, dom_node_previous_sibling_read, NULL);
649 dom_register_prop_handler(&dom_node_prop_handlers, "nextSibling", sizeof("nextSibling")-1, dom_node_next_sibling_read, NULL);
650 dom_register_prop_handler(&dom_node_prop_handlers, "attributes", sizeof("attributes")-1, dom_node_attributes_read, NULL);
651 dom_register_prop_handler(&dom_node_prop_handlers, "isConnected", sizeof("isConnected")-1, dom_node_is_connected_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, dom_node_class_entry->name, &dom_node_prop_handlers);
659
660 dom_namespace_node_class_entry = register_class_DOMNameSpaceNode();
661 dom_namespace_node_class_entry->create_object = dom_objects_namespace_node_new;
662
663 zend_hash_init(&dom_namespace_node_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
664 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "nodeName", sizeof("nodeName")-1, dom_node_node_name_read, NULL);
665 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "nodeValue", sizeof("nodeValue")-1, dom_node_node_value_read, NULL);
666 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "nodeType", sizeof("nodeType")-1, dom_node_node_type_read, NULL);
667 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "prefix", sizeof("prefix")-1, dom_node_prefix_read, NULL);
668 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "localName", sizeof("localName")-1, dom_node_local_name_read, NULL);
669 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "namespaceURI", sizeof("namespaceURI")-1, dom_node_namespace_uri_read, NULL);
670 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "isConnected", sizeof("isConnected")-1, dom_node_is_connected_read, NULL);
671 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "ownerDocument", sizeof("ownerDocument")-1, dom_node_owner_document_read, NULL);
672 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "parentNode", sizeof("parentNode")-1, dom_node_parent_node_read, NULL);
673 dom_register_prop_handler(&dom_namespace_node_prop_handlers, "parentElement", sizeof("parentElement")-1, dom_node_parent_element_read, NULL);
674 zend_hash_add_ptr(&classes, dom_namespace_node_class_entry->name, &dom_namespace_node_prop_handlers);
675
676 dom_documentfragment_class_entry = register_class_DOMDocumentFragment(dom_node_class_entry, dom_parentnode_class_entry);
677 dom_documentfragment_class_entry->create_object = dom_objects_new;
678 zend_hash_init(&dom_documentfragment_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
679
680 dom_register_prop_handler(&dom_documentfragment_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
681 dom_register_prop_handler(&dom_documentfragment_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);
682 dom_register_prop_handler(&dom_documentfragment_prop_handlers, "childElementCount", sizeof("childElementCount")-1, dom_parent_node_child_element_count, NULL);
683
684 zend_hash_merge(&dom_documentfragment_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
685 zend_hash_add_ptr(&classes, dom_documentfragment_class_entry->name, &dom_documentfragment_prop_handlers);
686
687 dom_document_class_entry = register_class_DOMDocument(dom_node_class_entry, dom_parentnode_class_entry);
688 dom_document_class_entry->create_object = dom_objects_new;
689 zend_hash_init(&dom_document_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
690 dom_register_prop_handler(&dom_document_prop_handlers, "doctype", sizeof("doctype")-1, dom_document_doctype_read, NULL);
691 dom_register_prop_handler(&dom_document_prop_handlers, "implementation", sizeof("implementation")-1, dom_document_implementation_read, NULL);
692 dom_register_prop_handler(&dom_document_prop_handlers, "documentElement", sizeof("documentElement")-1, dom_document_document_element_read, NULL);
693 dom_register_prop_handler(&dom_document_prop_handlers, "actualEncoding", sizeof("actualEncoding")-1, dom_document_encoding_read, NULL);
694 dom_register_prop_handler(&dom_document_prop_handlers, "encoding", sizeof("encoding")-1, dom_document_encoding_read, dom_document_encoding_write);
695 dom_register_prop_handler(&dom_document_prop_handlers, "xmlEncoding", sizeof("xmlEncoding")-1, dom_document_encoding_read, NULL);
696 dom_register_prop_handler(&dom_document_prop_handlers, "standalone", sizeof("standalone")-1, dom_document_standalone_read, dom_document_standalone_write);
697 dom_register_prop_handler(&dom_document_prop_handlers, "xmlStandalone", sizeof("xmlStandalone")-1, dom_document_standalone_read, dom_document_standalone_write);
698 dom_register_prop_handler(&dom_document_prop_handlers, "version", sizeof("version")-1, dom_document_version_read, dom_document_version_write);
699 dom_register_prop_handler(&dom_document_prop_handlers, "xmlVersion", sizeof("xmlVersion")-1, dom_document_version_read, dom_document_version_write);
700 dom_register_prop_handler(&dom_document_prop_handlers, "strictErrorChecking", sizeof("strictErrorChecking")-1, dom_document_strict_error_checking_read, dom_document_strict_error_checking_write);
701 dom_register_prop_handler(&dom_document_prop_handlers, "documentURI", sizeof("documentURI")-1, dom_document_document_uri_read, dom_document_document_uri_write);
702 dom_register_prop_handler(&dom_document_prop_handlers, "config", sizeof("config")-1, dom_document_config_read, NULL);
703 dom_register_prop_handler(&dom_document_prop_handlers, "formatOutput", sizeof("formatOutput")-1, dom_document_format_output_read, dom_document_format_output_write);
704 dom_register_prop_handler(&dom_document_prop_handlers, "validateOnParse", sizeof("validateOnParse")-1, dom_document_validate_on_parse_read, dom_document_validate_on_parse_write);
705 dom_register_prop_handler(&dom_document_prop_handlers, "resolveExternals", sizeof("resolveExternals")-1, dom_document_resolve_externals_read, dom_document_resolve_externals_write);
706 dom_register_prop_handler(&dom_document_prop_handlers, "preserveWhiteSpace", sizeof("preserveWhitespace")-1, dom_document_preserve_whitespace_read, dom_document_preserve_whitespace_write);
707 dom_register_prop_handler(&dom_document_prop_handlers, "recover", sizeof("recover")-1, dom_document_recover_read, dom_document_recover_write);
708 dom_register_prop_handler(&dom_document_prop_handlers, "substituteEntities", sizeof("substituteEntities")-1, dom_document_substitue_entities_read, dom_document_substitue_entities_write);
709
710 dom_register_prop_handler(&dom_document_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
711 dom_register_prop_handler(&dom_document_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);
712 dom_register_prop_handler(&dom_document_prop_handlers, "childElementCount", sizeof("childElementCount")-1, dom_parent_node_child_element_count, NULL);
713
714 zend_hash_merge(&dom_document_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
715 zend_hash_add_ptr(&classes, dom_document_class_entry->name, &dom_document_prop_handlers);
716
717 dom_nodelist_class_entry = register_class_DOMNodeList(zend_ce_aggregate, zend_ce_countable);
718 dom_nodelist_class_entry->create_object = dom_nnodemap_objects_new;
719 dom_nodelist_class_entry->default_object_handlers = &dom_nodelist_object_handlers;
720 dom_nodelist_class_entry->get_iterator = php_dom_get_iterator;
721
722 zend_hash_init(&dom_nodelist_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
723 dom_register_prop_handler(&dom_nodelist_prop_handlers, "length", sizeof("length")-1, dom_nodelist_length_read, NULL);
724 zend_hash_add_ptr(&classes, dom_nodelist_class_entry->name, &dom_nodelist_prop_handlers);
725
726 dom_namednodemap_class_entry = register_class_DOMNamedNodeMap(zend_ce_aggregate, zend_ce_countable);
727 dom_namednodemap_class_entry->create_object = dom_nnodemap_objects_new;
728 dom_namednodemap_class_entry->default_object_handlers = &dom_nnodemap_object_handlers;
729 dom_namednodemap_class_entry->get_iterator = php_dom_get_iterator;
730
731 zend_hash_init(&dom_namednodemap_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
732 dom_register_prop_handler(&dom_namednodemap_prop_handlers, "length", sizeof("length")-1, dom_namednodemap_length_read, NULL);
733 zend_hash_add_ptr(&classes, dom_namednodemap_class_entry->name, &dom_namednodemap_prop_handlers);
734
735 dom_characterdata_class_entry = register_class_DOMCharacterData(dom_node_class_entry, dom_childnode_class_entry);
736 dom_characterdata_class_entry->create_object = dom_objects_new;
737
738 zend_hash_init(&dom_characterdata_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
739 dom_register_prop_handler(&dom_characterdata_prop_handlers, "data", sizeof("data")-1, dom_characterdata_data_read, dom_characterdata_data_write);
740 dom_register_prop_handler(&dom_characterdata_prop_handlers, "length", sizeof("length")-1, dom_characterdata_length_read, NULL);
741 dom_register_prop_handler(&dom_characterdata_prop_handlers, "previousElementSibling", sizeof("previousElementSibling")-1, dom_node_previous_element_sibling_read, NULL);
742 dom_register_prop_handler(&dom_characterdata_prop_handlers, "nextElementSibling", sizeof("nextElementSibling")-1, dom_node_next_element_sibling_read, NULL);
743 zend_hash_merge(&dom_characterdata_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
744 zend_hash_add_ptr(&classes, dom_characterdata_class_entry->name, &dom_characterdata_prop_handlers);
745
746 dom_attr_class_entry = register_class_DOMAttr(dom_node_class_entry);
747 dom_attr_class_entry->create_object = dom_objects_new;
748
749 zend_hash_init(&dom_attr_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
750 dom_register_prop_handler(&dom_attr_prop_handlers, "name", sizeof("name")-1, dom_attr_name_read, NULL);
751 dom_register_prop_handler(&dom_attr_prop_handlers, "specified", sizeof("specified")-1, dom_attr_specified_read, NULL);
752 dom_register_prop_handler(&dom_attr_prop_handlers, "value", sizeof("value")-1, dom_attr_value_read, dom_attr_value_write);
753 dom_register_prop_handler(&dom_attr_prop_handlers, "ownerElement", sizeof("ownerElement")-1, dom_attr_owner_element_read, NULL);
754 dom_register_prop_handler(&dom_attr_prop_handlers, "schemaTypeInfo", sizeof("schemaTypeInfo")-1, dom_attr_schema_type_info_read, NULL);
755 zend_hash_merge(&dom_attr_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
756 zend_hash_add_ptr(&classes, dom_attr_class_entry->name, &dom_attr_prop_handlers);
757
758 dom_element_class_entry = register_class_DOMElement(dom_node_class_entry, dom_parentnode_class_entry, dom_childnode_class_entry);
759 dom_element_class_entry->create_object = dom_objects_new;
760
761 zend_hash_init(&dom_element_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
762 dom_register_prop_handler(&dom_element_prop_handlers, "tagName", sizeof("tagName")-1, dom_element_tag_name_read, NULL);
763 dom_register_prop_handler(&dom_element_prop_handlers, "className", sizeof("className")-1, dom_element_class_name_read, dom_element_class_name_write);
764 dom_register_prop_handler(&dom_element_prop_handlers, "id", sizeof("id")-1, dom_element_id_read, dom_element_id_write);
765 dom_register_prop_handler(&dom_element_prop_handlers, "schemaTypeInfo", sizeof("schemaTypeInfo")-1, dom_element_schema_type_info_read, NULL);
766 dom_register_prop_handler(&dom_element_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
767 dom_register_prop_handler(&dom_element_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);
768 dom_register_prop_handler(&dom_element_prop_handlers, "childElementCount", sizeof("childElementCount")-1, dom_parent_node_child_element_count, NULL);
769 dom_register_prop_handler(&dom_element_prop_handlers, "previousElementSibling", sizeof("previousElementSibling")-1, dom_node_previous_element_sibling_read, NULL);
770 dom_register_prop_handler(&dom_element_prop_handlers, "nextElementSibling", sizeof("nextElementSibling")-1, dom_node_next_element_sibling_read, NULL);
771 zend_hash_merge(&dom_element_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
772 zend_hash_add_ptr(&classes, dom_element_class_entry->name, &dom_element_prop_handlers);
773
774 dom_text_class_entry = register_class_DOMText(dom_characterdata_class_entry);
775 dom_text_class_entry->create_object = dom_objects_new;
776
777 zend_hash_init(&dom_text_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
778 dom_register_prop_handler(&dom_text_prop_handlers, "wholeText", sizeof("wholeText")-1, dom_text_whole_text_read, NULL);
779 zend_hash_merge(&dom_text_prop_handlers, &dom_characterdata_prop_handlers, dom_copy_prop_handler, 0);
780 zend_hash_add_ptr(&classes, dom_text_class_entry->name, &dom_text_prop_handlers);
781
782 dom_comment_class_entry = register_class_DOMComment(dom_characterdata_class_entry);
783 dom_comment_class_entry->create_object = dom_objects_new;
784 zend_hash_add_ptr(&classes, dom_comment_class_entry->name, &dom_characterdata_prop_handlers);
785
786 dom_cdatasection_class_entry = register_class_DOMCdataSection(dom_text_class_entry);
787 dom_cdatasection_class_entry->create_object = dom_objects_new;
788 zend_hash_add_ptr(&classes, dom_cdatasection_class_entry->name, &dom_text_prop_handlers);
789
790 dom_documenttype_class_entry = register_class_DOMDocumentType(dom_node_class_entry);
791 dom_documenttype_class_entry->create_object = dom_objects_new;
792
793 zend_hash_init(&dom_documenttype_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
794 dom_register_prop_handler(&dom_documenttype_prop_handlers, "name", sizeof("name")-1, dom_documenttype_name_read, NULL);
795 dom_register_prop_handler(&dom_documenttype_prop_handlers, "entities", sizeof("entities")-1, dom_documenttype_entities_read, NULL);
796 dom_register_prop_handler(&dom_documenttype_prop_handlers, "notations", sizeof("notations")-1, dom_documenttype_notations_read, NULL);
797 dom_register_prop_handler(&dom_documenttype_prop_handlers, "publicId", sizeof("publicId")-1, dom_documenttype_public_id_read, NULL);
798 dom_register_prop_handler(&dom_documenttype_prop_handlers, "systemId", sizeof("systemId")-1, dom_documenttype_system_id_read, NULL);
799 dom_register_prop_handler(&dom_documenttype_prop_handlers, "internalSubset", sizeof("internalSubset")-1, dom_documenttype_internal_subset_read, NULL);
800 zend_hash_merge(&dom_documenttype_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
801 zend_hash_add_ptr(&classes, dom_documenttype_class_entry->name, &dom_documenttype_prop_handlers);
802
803 dom_notation_class_entry = register_class_DOMNotation(dom_node_class_entry);
804 dom_notation_class_entry->create_object = dom_objects_new;
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, dom_notation_class_entry->name, &dom_notation_prop_handlers);
811
812 dom_entity_class_entry = register_class_DOMEntity(dom_node_class_entry);
813 dom_entity_class_entry->create_object = dom_objects_new;
814
815 zend_hash_init(&dom_entity_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
816 dom_register_prop_handler(&dom_entity_prop_handlers, "publicId", sizeof("publicId")-1, dom_entity_public_id_read, NULL);
817 dom_register_prop_handler(&dom_entity_prop_handlers, "systemId", sizeof("systemId")-1, dom_entity_system_id_read, NULL);
818 dom_register_prop_handler(&dom_entity_prop_handlers, "notationName", sizeof("notationName")-1, dom_entity_notation_name_read, NULL);
819 dom_register_prop_handler(&dom_entity_prop_handlers, "actualEncoding", sizeof("actualEncoding")-1, dom_entity_actual_encoding_read, NULL);
820 dom_register_prop_handler(&dom_entity_prop_handlers, "encoding", sizeof("encoding")-1, dom_entity_encoding_read, NULL);
821 dom_register_prop_handler(&dom_entity_prop_handlers, "version", sizeof("version")-1, dom_entity_version_read, NULL);
822 zend_hash_merge(&dom_entity_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
823 zend_hash_add_ptr(&classes, dom_entity_class_entry->name, &dom_entity_prop_handlers);
824
825 dom_entityreference_class_entry = register_class_DOMEntityReference(dom_node_class_entry);
826 dom_entityreference_class_entry->create_object = dom_objects_new;
827
828 zend_hash_init(&dom_entity_reference_prop_handlers, 0, NULL, dom_dtor_prop_handler, true);
829 zend_hash_merge(&dom_entity_reference_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, false);
830 dom_override_prop_handler(&dom_entity_reference_prop_handlers, "firstChild", sizeof("firstChild")-1, dom_entity_reference_child_read, NULL);
831 dom_override_prop_handler(&dom_entity_reference_prop_handlers, "lastChild", sizeof("lastChild")-1, dom_entity_reference_child_read, NULL);
832 dom_override_prop_handler(&dom_entity_reference_prop_handlers, "textContent", sizeof("textContent")-1, dom_entity_reference_text_content_read, NULL);
833 dom_override_prop_handler(&dom_entity_reference_prop_handlers, "childNodes", sizeof("childNodes")-1, dom_entity_reference_child_nodes_read, NULL);
834 zend_hash_add_ptr(&classes, dom_entityreference_class_entry->name, &dom_entity_reference_prop_handlers);
835
836 dom_processinginstruction_class_entry = register_class_DOMProcessingInstruction(dom_node_class_entry);
837 dom_processinginstruction_class_entry->create_object = dom_objects_new;
838
839 zend_hash_init(&dom_processinginstruction_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
840 dom_register_prop_handler(&dom_processinginstruction_prop_handlers, "target", sizeof("target")-1, dom_processinginstruction_target_read, NULL);
841 dom_register_prop_handler(&dom_processinginstruction_prop_handlers, "data", sizeof("data")-1, dom_processinginstruction_data_read, dom_processinginstruction_data_write);
842 zend_hash_merge(&dom_processinginstruction_prop_handlers, &dom_node_prop_handlers, dom_copy_prop_handler, 0);
843 zend_hash_add_ptr(&classes, dom_processinginstruction_class_entry->name, &dom_processinginstruction_prop_handlers);
844
845 #ifdef LIBXML_XPATH_ENABLED
846 memcpy(&dom_xpath_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
847 dom_xpath_object_handlers.offset = XtOffsetOf(dom_xpath_object, dom) + XtOffsetOf(dom_object, std);
848 dom_xpath_object_handlers.free_obj = dom_xpath_objects_free_storage;
849
850 dom_xpath_class_entry = register_class_DOMXPath();
851 dom_xpath_class_entry->create_object = dom_xpath_objects_new;
852 dom_xpath_class_entry->default_object_handlers = &dom_xpath_object_handlers;
853
854 zend_hash_init(&dom_xpath_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
855 dom_register_prop_handler(&dom_xpath_prop_handlers, "document", sizeof("document")-1, dom_xpath_document_read, NULL);
856 dom_register_prop_handler(&dom_xpath_prop_handlers, "registerNodeNamespaces", sizeof("registerNodeNamespaces")-1, dom_xpath_register_node_ns_read, dom_xpath_register_node_ns_write);
857 zend_hash_add_ptr(&classes, dom_xpath_class_entry->name, &dom_xpath_prop_handlers);
858 #endif
859
860 register_php_dom_symbols(module_number);
861
862 php_libxml_register_export(dom_node_class_entry, php_dom_export_node);
863
864 return SUCCESS;
865 }
866 /* }}} */
867
868 /* {{{ */
PHP_MINFO_FUNCTION(dom)869 PHP_MINFO_FUNCTION(dom)
870 {
871 php_info_print_table_start();
872 php_info_print_table_row(2, "DOM/XML", "enabled");
873 php_info_print_table_row(2, "DOM/XML API Version", DOM_API_VERSION);
874 php_info_print_table_row(2, "libxml Version", LIBXML_DOTTED_VERSION);
875 #ifdef LIBXML_HTML_ENABLED
876 php_info_print_table_row(2, "HTML Support", "enabled");
877 #endif
878 #ifdef LIBXML_XPATH_ENABLED
879 php_info_print_table_row(2, "XPath Support", "enabled");
880 #endif
881 #ifdef LIBXML_XPTR_ENABLED
882 php_info_print_table_row(2, "XPointer Support", "enabled");
883 #endif
884 #ifdef LIBXML_SCHEMAS_ENABLED
885 php_info_print_table_row(2, "Schema Support", "enabled");
886 php_info_print_table_row(2, "RelaxNG Support", "enabled");
887 #endif
888 php_info_print_table_end();
889 }
890 /* }}} */
891
PHP_MSHUTDOWN_FUNCTION(dom)892 PHP_MSHUTDOWN_FUNCTION(dom) /* {{{ */
893 {
894 zend_hash_destroy(&dom_document_prop_handlers);
895 zend_hash_destroy(&dom_documentfragment_prop_handlers);
896 zend_hash_destroy(&dom_node_prop_handlers);
897 zend_hash_destroy(&dom_entity_reference_prop_handlers);
898 zend_hash_destroy(&dom_namespace_node_prop_handlers);
899 zend_hash_destroy(&dom_nodelist_prop_handlers);
900 zend_hash_destroy(&dom_namednodemap_prop_handlers);
901 zend_hash_destroy(&dom_characterdata_prop_handlers);
902 zend_hash_destroy(&dom_attr_prop_handlers);
903 zend_hash_destroy(&dom_element_prop_handlers);
904 zend_hash_destroy(&dom_text_prop_handlers);
905 zend_hash_destroy(&dom_documenttype_prop_handlers);
906 zend_hash_destroy(&dom_notation_prop_handlers);
907 zend_hash_destroy(&dom_entity_prop_handlers);
908 zend_hash_destroy(&dom_processinginstruction_prop_handlers);
909 #ifdef LIBXML_XPATH_ENABLED
910 zend_hash_destroy(&dom_xpath_prop_handlers);
911 #endif
912 zend_hash_destroy(&classes);
913
914 /* If you want do find memleaks in this module, compile libxml2 with --with-mem-debug and
915 uncomment the following line, this will tell you the amount of not freed memory
916 and the total used memory into apaches error_log */
917 /* xmlMemoryDump();*/
918
919 return SUCCESS;
920 }
921 /* }}} */
922
923 /* {{{ node_list_unlink */
node_list_unlink(xmlNodePtr node)924 void node_list_unlink(xmlNodePtr node)
925 {
926 dom_object *wrapper;
927
928 while (node != NULL) {
929
930 wrapper = php_dom_object_get_data(node);
931
932 if (wrapper != NULL ) {
933 xmlUnlinkNode(node);
934 } else {
935 if (node->type == XML_ENTITY_REF_NODE)
936 break;
937 node_list_unlink(node->children);
938
939 switch (node->type) {
940 case XML_ATTRIBUTE_DECL:
941 case XML_DTD_NODE:
942 case XML_DOCUMENT_TYPE_NODE:
943 case XML_ENTITY_DECL:
944 case XML_ATTRIBUTE_NODE:
945 case XML_TEXT_NODE:
946 break;
947 default:
948 node_list_unlink((xmlNodePtr) node->properties);
949 }
950
951 }
952
953 node = node->next;
954 }
955 }
956 /* }}} end node_list_unlink */
957
958 #ifdef LIBXML_XPATH_ENABLED
959 /* {{{ dom_xpath_objects_free_storage */
dom_xpath_objects_free_storage(zend_object * object)960 void dom_xpath_objects_free_storage(zend_object *object)
961 {
962 dom_xpath_object *intern = php_xpath_obj_from_obj(object);
963
964 zend_object_std_dtor(&intern->dom.std);
965
966 if (intern->dom.ptr != NULL) {
967 xmlXPathFreeContext((xmlXPathContextPtr) intern->dom.ptr);
968 php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom);
969 }
970
971 if (intern->registered_phpfunctions) {
972 zend_hash_destroy(intern->registered_phpfunctions);
973 FREE_HASHTABLE(intern->registered_phpfunctions);
974 }
975
976 if (intern->node_list) {
977 zend_hash_destroy(intern->node_list);
978 FREE_HASHTABLE(intern->node_list);
979 }
980 }
981 /* }}} */
982 #endif
983
984 /* {{{ dom_objects_free_storage */
dom_objects_free_storage(zend_object * object)985 void dom_objects_free_storage(zend_object *object)
986 {
987 dom_object *intern = php_dom_obj_from_obj(object);
988 #if defined(__GNUC__) && __GNUC__ >= 3
989 int retcount __attribute__((unused)); /* keep compiler quiet */
990 #else
991 int retcount;
992 #endif
993
994 zend_object_std_dtor(&intern->std);
995
996 if (intern->ptr != NULL && ((php_libxml_node_ptr *)intern->ptr)->node != NULL) {
997 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) {
998 php_libxml_node_decrement_resource((php_libxml_node_object *) intern);
999 } else {
1000 php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
1001 retcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1002 }
1003 intern->ptr = NULL;
1004 }
1005 }
1006 /* }}} */
1007
dom_namednode_iter(dom_object * basenode,int ntype,dom_object * intern,xmlHashTablePtr ht,const char * local,size_t local_len,const char * ns,size_t ns_len)1008 void dom_namednode_iter(dom_object *basenode, int ntype, dom_object *intern, xmlHashTablePtr ht, const char *local, size_t local_len, const char *ns, size_t ns_len) /* {{{ */
1009 {
1010 dom_nnodemap_object *mapptr = (dom_nnodemap_object *) intern->ptr;
1011
1012 ZEND_ASSERT(basenode != NULL);
1013
1014 ZVAL_OBJ_COPY(&mapptr->baseobj_zv, &basenode->std);
1015
1016 xmlDocPtr doc = basenode->document ? basenode->document->ptr : NULL;
1017
1018 mapptr->baseobj = basenode;
1019 mapptr->nodetype = ntype;
1020 mapptr->ht = ht;
1021
1022 const xmlChar* tmp;
1023
1024 if (local) {
1025 int len = local_len > INT_MAX ? -1 : (int) local_len;
1026 if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)local, len)) != NULL) {
1027 mapptr->local = (xmlChar*) tmp;
1028 } else {
1029 mapptr->local = xmlCharStrndup(local, len);
1030 mapptr->free_local = true;
1031 }
1032 }
1033
1034 if (ns) {
1035 int len = ns_len > INT_MAX ? -1 : (int) ns_len;
1036 if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ns, len)) != NULL) {
1037 mapptr->ns = (xmlChar*) tmp;
1038 } else {
1039 mapptr->ns = xmlCharStrndup(ns, len);
1040 mapptr->free_ns = true;
1041 }
1042 }
1043 }
1044 /* }}} */
1045
dom_objects_set_class_ex(zend_class_entry * class_type,dom_object * intern)1046 static void dom_objects_set_class_ex(zend_class_entry *class_type, dom_object *intern)
1047 {
1048 zend_class_entry *base_class = class_type;
1049 while ((base_class->type != ZEND_INTERNAL_CLASS || base_class->info.internal.module->module_number != dom_module_entry.module_number) && base_class->parent != NULL) {
1050 base_class = base_class->parent;
1051 }
1052
1053 intern->prop_handler = zend_hash_find_ptr(&classes, base_class->name);
1054
1055 zend_object_std_init(&intern->std, class_type);
1056 object_properties_init(&intern->std, class_type);
1057 }
1058
dom_objects_set_class(zend_class_entry * class_type)1059 static dom_object* dom_objects_set_class(zend_class_entry *class_type)
1060 {
1061 dom_object *intern = zend_object_alloc(sizeof(dom_object), class_type);
1062 dom_objects_set_class_ex(class_type, intern);
1063 return intern;
1064 }
1065
1066 /* {{{ dom_objects_new */
dom_objects_new(zend_class_entry * class_type)1067 zend_object *dom_objects_new(zend_class_entry *class_type)
1068 {
1069 dom_object *intern = dom_objects_set_class(class_type);
1070 intern->std.handlers = dom_get_obj_handlers();
1071 return &intern->std;
1072 }
1073 /* }}} */
1074
dom_objects_namespace_node_new(zend_class_entry * class_type)1075 static zend_object *dom_objects_namespace_node_new(zend_class_entry *class_type)
1076 {
1077 dom_object_namespace_node *intern = zend_object_alloc(sizeof(dom_object_namespace_node), class_type);
1078 dom_objects_set_class_ex(class_type, &intern->dom);
1079 intern->dom.std.handlers = &dom_object_namespace_node_handlers;
1080 return &intern->dom.std;
1081 }
1082
dom_object_namespace_node_free_storage(zend_object * object)1083 static void dom_object_namespace_node_free_storage(zend_object *object)
1084 {
1085 dom_object_namespace_node *intern = php_dom_namespace_node_obj_from_obj(object);
1086 if (intern->parent_intern != NULL) {
1087 zval tmp;
1088 ZVAL_OBJ(&tmp, &intern->parent_intern->std);
1089 zval_ptr_dtor(&tmp);
1090 }
1091 dom_objects_free_storage(object);
1092 }
1093
1094 #ifdef LIBXML_XPATH_ENABLED
1095 /* {{{ zend_object dom_xpath_objects_new(zend_class_entry *class_type) */
dom_xpath_objects_new(zend_class_entry * class_type)1096 zend_object *dom_xpath_objects_new(zend_class_entry *class_type)
1097 {
1098 dom_xpath_object *intern = zend_object_alloc(sizeof(dom_xpath_object), class_type);
1099
1100 intern->registered_phpfunctions = zend_new_array(0);
1101 intern->register_node_ns = 1;
1102
1103 intern->dom.prop_handler = &dom_xpath_prop_handlers;
1104
1105 zend_object_std_init(&intern->dom.std, class_type);
1106 object_properties_init(&intern->dom.std, class_type);
1107
1108 return &intern->dom.std;
1109 }
1110 /* }}} */
1111 #endif
1112
dom_nnodemap_objects_free_storage(zend_object * object)1113 void dom_nnodemap_objects_free_storage(zend_object *object) /* {{{ */
1114 {
1115 dom_object *intern = php_dom_obj_from_obj(object);
1116 dom_nnodemap_object *objmap = (dom_nnodemap_object *)intern->ptr;
1117
1118 if (objmap) {
1119 if (objmap->cached_obj && GC_DELREF(&objmap->cached_obj->std) == 0) {
1120 zend_objects_store_del(&objmap->cached_obj->std);
1121 }
1122 if (objmap->free_local) {
1123 xmlFree(objmap->local);
1124 }
1125 if (objmap->free_ns) {
1126 xmlFree(objmap->ns);
1127 }
1128 if (!Z_ISUNDEF(objmap->baseobj_zv)) {
1129 zval_ptr_dtor(&objmap->baseobj_zv);
1130 }
1131 efree(objmap);
1132 intern->ptr = NULL;
1133 }
1134
1135 php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1136
1137 zend_object_std_dtor(&intern->std);
1138 }
1139 /* }}} */
1140
dom_nnodemap_objects_new(zend_class_entry * class_type)1141 zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type)
1142 {
1143 dom_object *intern;
1144 dom_nnodemap_object *objmap;
1145
1146 intern = dom_objects_set_class(class_type);
1147 intern->ptr = emalloc(sizeof(dom_nnodemap_object));
1148 objmap = (dom_nnodemap_object *)intern->ptr;
1149 ZVAL_UNDEF(&objmap->baseobj_zv);
1150 objmap->baseobj = NULL;
1151 objmap->nodetype = 0;
1152 objmap->ht = NULL;
1153 objmap->local = NULL;
1154 objmap->free_local = false;
1155 objmap->ns = NULL;
1156 objmap->free_ns = false;
1157 objmap->cache_tag.modification_nr = 0;
1158 objmap->cached_length = -1;
1159 objmap->cached_obj = NULL;
1160 objmap->cached_obj_index = 0;
1161
1162 return &intern->std;
1163 }
1164
php_dom_create_iterator(zval * return_value,int ce_type)1165 void php_dom_create_iterator(zval *return_value, int ce_type) /* {{{ */
1166 {
1167 zend_class_entry *ce;
1168
1169 if (ce_type == DOM_NAMEDNODEMAP) {
1170 ce = dom_namednodemap_class_entry;
1171 } else {
1172 ce = dom_nodelist_class_entry;
1173 }
1174
1175 object_init_ex(return_value, ce);
1176 }
1177 /* }}} */
1178
1179 /* {{{ php_dom_create_object */
php_dom_create_object(xmlNodePtr obj,zval * return_value,dom_object * domobj)1180 PHP_DOM_EXPORT bool php_dom_create_object(xmlNodePtr obj, zval *return_value, dom_object *domobj)
1181 {
1182 zend_class_entry *ce;
1183 dom_object *intern;
1184
1185 if (!obj) {
1186 ZVAL_NULL(return_value);
1187 return 0;
1188 }
1189
1190 if ((intern = (dom_object *) php_dom_object_get_data((void *) obj))) {
1191 ZVAL_OBJ_COPY(return_value, &intern->std);
1192 return 1;
1193 }
1194
1195 switch (obj->type) {
1196 case XML_DOCUMENT_NODE:
1197 case XML_HTML_DOCUMENT_NODE:
1198 {
1199 ce = dom_document_class_entry;
1200 break;
1201 }
1202 case XML_DTD_NODE:
1203 case XML_DOCUMENT_TYPE_NODE:
1204 {
1205 ce = dom_documenttype_class_entry;
1206 break;
1207 }
1208 case XML_ELEMENT_NODE:
1209 {
1210 ce = dom_element_class_entry;
1211 break;
1212 }
1213 case XML_ATTRIBUTE_NODE:
1214 {
1215 ce = dom_attr_class_entry;
1216 break;
1217 }
1218 case XML_TEXT_NODE:
1219 {
1220 ce = dom_text_class_entry;
1221 break;
1222 }
1223 case XML_COMMENT_NODE:
1224 {
1225 ce = dom_comment_class_entry;
1226 break;
1227 }
1228 case XML_PI_NODE:
1229 {
1230 ce = dom_processinginstruction_class_entry;
1231 break;
1232 }
1233 case XML_ENTITY_REF_NODE:
1234 {
1235 ce = dom_entityreference_class_entry;
1236 break;
1237 }
1238 case XML_ENTITY_DECL:
1239 case XML_ELEMENT_DECL:
1240 {
1241 ce = dom_entity_class_entry;
1242 break;
1243 }
1244 case XML_CDATA_SECTION_NODE:
1245 {
1246 ce = dom_cdatasection_class_entry;
1247 break;
1248 }
1249 case XML_DOCUMENT_FRAG_NODE:
1250 {
1251 ce = dom_documentfragment_class_entry;
1252 break;
1253 }
1254 case XML_NOTATION_NODE:
1255 {
1256 ce = dom_notation_class_entry;
1257 break;
1258 }
1259 case XML_NAMESPACE_DECL:
1260 {
1261 ce = dom_namespace_node_class_entry;
1262 break;
1263 }
1264 default:
1265 /* TODO Convert to a ZEND assertion? */
1266 zend_throw_error(NULL, "Unsupported node type: %d", obj->type);
1267 ZVAL_NULL(return_value);
1268 return 0;
1269 }
1270
1271 if (domobj && domobj->document) {
1272 ce = dom_get_doc_classmap(domobj->document, ce);
1273 }
1274 object_init_ex(return_value, ce);
1275
1276 intern = Z_DOMOBJ_P(return_value);
1277 if (obj->doc != NULL) {
1278 if (domobj != NULL) {
1279 intern->document = domobj->document;
1280 }
1281 php_libxml_increment_doc_ref((php_libxml_node_object *)intern, obj->doc);
1282 }
1283
1284 php_libxml_increment_node_ptr((php_libxml_node_object *)intern, obj, (void *)intern);
1285 return 0;
1286 }
1287 /* }}} end php_domobject_new */
1288
php_dom_create_implementation(zval * retval)1289 void php_dom_create_implementation(zval *retval) {
1290 object_init_ex(retval, dom_domimplementation_class_entry);
1291 }
1292
1293 /* {{{ int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child) */
dom_hierarchy(xmlNodePtr parent,xmlNodePtr child)1294 int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child)
1295 {
1296 xmlNodePtr nodep;
1297
1298 if (parent == NULL || child == NULL || child->doc != parent->doc) {
1299 return SUCCESS;
1300 }
1301
1302 if (child->type == XML_DOCUMENT_NODE) {
1303 return FAILURE;
1304 }
1305
1306 nodep = parent;
1307
1308 while (nodep) {
1309 if (nodep == child) {
1310 return FAILURE;
1311 }
1312 nodep = nodep->parent;
1313 }
1314
1315 return SUCCESS;
1316 }
1317 /* }}} end dom_hierarchy */
1318
1319 /* {{{ */
dom_has_feature(zend_string * feature,zend_string * version)1320 bool dom_has_feature(zend_string *feature, zend_string *version)
1321 {
1322 if (zend_string_equals_literal(version, "1.0")
1323 || zend_string_equals_literal(version, "2.0")
1324 || zend_string_equals_literal(version, "")
1325 ) {
1326 if (zend_string_equals_literal_ci(feature, "XML")
1327 || (zend_string_equals_literal_ci(feature, "Core") && zend_string_equals_literal(version, "1.0"))
1328 ) {
1329 return true;
1330 }
1331 }
1332
1333 return false;
1334 }
1335 /* }}} end dom_has_feature */
1336
dom_get_elements_by_tag_name_ns_raw(xmlNodePtr basep,xmlNodePtr nodep,char * ns,char * local,zend_long * cur,zend_long index)1337 xmlNode *dom_get_elements_by_tag_name_ns_raw(xmlNodePtr basep, xmlNodePtr nodep, char *ns, char *local, zend_long *cur, zend_long index) /* {{{ */
1338 {
1339 /* Can happen with detached document */
1340 if (UNEXPECTED(nodep == NULL)) {
1341 return NULL;
1342 }
1343
1344 xmlNodePtr ret = NULL;
1345 bool local_match_any = local[0] == '*' && local[1] == '\0';
1346
1347 /* Note: The spec says that ns == '' must be transformed to ns == NULL. In other words, they are equivalent.
1348 * PHP however does not do this and internally uses the empty string everywhere when the user provides ns == NULL.
1349 * This is because for PHP ns == NULL has another meaning: "match every namespace" instead of "match the empty namespace". */
1350 bool ns_match_any = ns == NULL || (ns[0] == '*' && ns[1] == '\0');
1351
1352 while (*cur <= index) {
1353 if (nodep->type == XML_ELEMENT_NODE) {
1354 if (local_match_any || xmlStrEqual(nodep->name, (xmlChar *)local)) {
1355 if (ns_match_any || (ns[0] == '\0' && nodep->ns == NULL) || (nodep->ns != NULL && xmlStrEqual(nodep->ns->href, (xmlChar *)ns))) {
1356 if (*cur == index) {
1357 ret = nodep;
1358 break;
1359 }
1360 (*cur)++;
1361 }
1362 }
1363
1364 if (nodep->children) {
1365 nodep = nodep->children;
1366 continue;
1367 }
1368 }
1369
1370 if (nodep->next) {
1371 nodep = nodep->next;
1372 } else {
1373 /* Go upwards, until we find a parent node with a next sibling, or until we hit the base. */
1374 do {
1375 nodep = nodep->parent;
1376 if (nodep == basep) {
1377 return NULL;
1378 }
1379 /* This shouldn't happen, unless there's an invalidation bug somewhere. */
1380 if (UNEXPECTED(nodep == NULL)) {
1381 zend_throw_error(NULL, "Current node in traversal is not in the document. Please report this as a bug in php-src.");
1382 return NULL;
1383 }
1384 } while (nodep->next == NULL);
1385 nodep = nodep->next;
1386 }
1387 }
1388 return ret;
1389 }
1390 /* }}} end dom_element_get_elements_by_tag_name_ns_raw */
1391
is_empty_node(xmlNodePtr nodep)1392 static inline bool is_empty_node(xmlNodePtr nodep)
1393 {
1394 xmlChar *strContent = xmlNodeGetContent(nodep);
1395 bool ret = strContent == NULL || *strContent == '\0';
1396 xmlFree(strContent);
1397 return ret;
1398 }
1399
1400 /* {{{ void dom_normalize (xmlNodePtr nodep) */
dom_normalize(xmlNodePtr nodep)1401 void dom_normalize (xmlNodePtr nodep)
1402 {
1403 xmlNodePtr child, nextp, newnextp;
1404 xmlAttrPtr attr;
1405 xmlChar *strContent;
1406
1407 child = nodep->children;
1408 while(child != NULL) {
1409 switch (child->type) {
1410 case XML_TEXT_NODE:
1411 nextp = child->next;
1412 while (nextp != NULL) {
1413 if (nextp->type == XML_TEXT_NODE) {
1414 newnextp = nextp->next;
1415 strContent = xmlNodeGetContent(nextp);
1416 xmlNodeAddContent(child, strContent);
1417 xmlFree(strContent);
1418 xmlUnlinkNode(nextp);
1419 php_libxml_node_free_resource(nextp);
1420 nextp = newnextp;
1421 } else {
1422 break;
1423 }
1424 }
1425 if (is_empty_node(child)) {
1426 nextp = child->next;
1427 xmlUnlinkNode(child);
1428 php_libxml_node_free_resource(child);
1429 child = nextp;
1430 continue;
1431 }
1432 break;
1433 case XML_ELEMENT_NODE:
1434 dom_normalize (child);
1435 attr = child->properties;
1436 while (attr != NULL) {
1437 dom_normalize((xmlNodePtr) attr);
1438 attr = attr->next;
1439 }
1440 break;
1441 case XML_ATTRIBUTE_NODE:
1442 dom_normalize (child);
1443 break;
1444 default:
1445 break;
1446 }
1447 child = child->next;
1448 }
1449 }
1450 /* }}} end dom_normalize */
1451
dom_reconcile_ns_internal(xmlDocPtr doc,xmlNodePtr nodep,xmlNodePtr search_parent)1452 static void dom_reconcile_ns_internal(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr search_parent)
1453 {
1454 xmlNsPtr nsptr, nsdftptr, curns, prevns = NULL;
1455
1456 /* Following if block primarily used for inserting nodes created via createElementNS */
1457 if (nodep->nsDef != NULL) {
1458 curns = nodep->nsDef;
1459 while (curns) {
1460 nsdftptr = curns->next;
1461 if (curns->href != NULL) {
1462 if((nsptr = xmlSearchNsByHref(doc, search_parent, curns->href)) &&
1463 (curns->prefix == NULL || xmlStrEqual(nsptr->prefix, curns->prefix))) {
1464 curns->next = NULL;
1465 if (prevns == NULL) {
1466 nodep->nsDef = nsdftptr;
1467 } else {
1468 prevns->next = nsdftptr;
1469 }
1470 /* Note: we can't get here if the ns is already on the oldNs list.
1471 * This is because in that case the definition won't be on the node, and
1472 * therefore won't be in the nodep->nsDef list. */
1473 php_libxml_set_old_ns(doc, curns);
1474 curns = prevns;
1475 }
1476 }
1477 prevns = curns;
1478 curns = nsdftptr;
1479 }
1480 }
1481 }
1482
dom_libxml_reconcile_ensure_namespaces_are_declared(xmlNodePtr nodep)1483 static void dom_libxml_reconcile_ensure_namespaces_are_declared(xmlNodePtr nodep)
1484 {
1485 /* Ideally we'd use the DOM-wrapped version, but we cannot: https://github.com/php/php-src/pull/12308. */
1486 #if 0
1487 /* Put on stack to avoid allocation.
1488 * Although libxml2 currently does not use this for the reconciliation, it still
1489 * makes sense to do this just in case libxml2's internal change in the future. */
1490 xmlDOMWrapCtxt dummy_ctxt = {0};
1491 xmlDOMWrapReconcileNamespaces(&dummy_ctxt, nodep, /* options */ 0);
1492 #else
1493 xmlReconciliateNs(nodep->doc, nodep);
1494 #endif
1495 }
1496
dom_reconcile_ns(xmlDocPtr doc,xmlNodePtr nodep)1497 void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep) /* {{{ */
1498 {
1499 /* Although the node type will be checked by the libxml2 API,
1500 * we still want to do the internal reconciliation conditionally. */
1501 if (nodep->type == XML_ELEMENT_NODE) {
1502 dom_reconcile_ns_internal(doc, nodep, nodep->parent);
1503 dom_libxml_reconcile_ensure_namespaces_are_declared(nodep);
1504 }
1505 }
1506 /* }}} */
1507
dom_reconcile_ns_list_internal(xmlDocPtr doc,xmlNodePtr nodep,xmlNodePtr last,xmlNodePtr search_parent)1508 static void dom_reconcile_ns_list_internal(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last, xmlNodePtr search_parent)
1509 {
1510 ZEND_ASSERT(nodep != NULL);
1511 while (true) {
1512 if (nodep->type == XML_ELEMENT_NODE) {
1513 dom_reconcile_ns_internal(doc, nodep, search_parent);
1514 if (nodep->children) {
1515 dom_reconcile_ns_list_internal(doc, nodep->children, nodep->last /* process the whole children list */, search_parent);
1516 }
1517 }
1518 if (nodep == last) {
1519 break;
1520 }
1521 nodep = nodep->next;
1522 }
1523 }
1524
dom_reconcile_ns_list(xmlDocPtr doc,xmlNodePtr nodep,xmlNodePtr last)1525 void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last)
1526 {
1527 dom_reconcile_ns_list_internal(doc, nodep, last, nodep->parent);
1528 /* The loop is outside of the recursion in the above call because
1529 * dom_libxml_reconcile_ensure_namespaces_are_declared() performs its own recursion. */
1530 while (true) {
1531 /* The internal libxml2 call will already check the node type, no need for us to do it here. */
1532 dom_libxml_reconcile_ensure_namespaces_are_declared(nodep);
1533 if (nodep == last) {
1534 break;
1535 }
1536 nodep = nodep->next;
1537 }
1538 }
1539
1540 /*
1541 http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-DocCrElNS
1542
1543 NAMESPACE_ERR: Raised if
1544
1545 1. the qualifiedName is a malformed qualified name
1546 2. the qualifiedName has a prefix and the namespaceURI is null
1547 */
1548
1549 /* {{{ 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)1550 int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, int name_len) {
1551 if (name_len == 0) {
1552 return NAMESPACE_ERR;
1553 }
1554
1555 *localname = (char *)xmlSplitQName2((xmlChar *)qname, (xmlChar **) prefix);
1556 if (*localname == NULL) {
1557 *localname = (char *)xmlStrdup((xmlChar *)qname);
1558 if (*prefix == NULL && uri_len == 0) {
1559 return 0;
1560 }
1561 }
1562
1563 /* 1 */
1564 if (xmlValidateQName((xmlChar *) qname, 0) != 0) {
1565 return NAMESPACE_ERR;
1566 }
1567
1568 /* 2 */
1569 if (*prefix != NULL && uri_len == 0) {
1570 return NAMESPACE_ERR;
1571 }
1572
1573 return 0;
1574 }
1575 /* }}} */
1576
1577 /* Creates a new namespace declaration with a random prefix with the given uri on the tree.
1578 * This is used to resolve a namespace prefix conflict in cases where spec does not want a
1579 * namespace error in case of conflicts, but demands a resolution. */
dom_get_ns_resolve_prefix_conflict(xmlNodePtr tree,const char * uri)1580 xmlNsPtr dom_get_ns_resolve_prefix_conflict(xmlNodePtr tree, const char *uri)
1581 {
1582 ZEND_ASSERT(tree != NULL);
1583 xmlDocPtr doc = tree->doc;
1584
1585 if (UNEXPECTED(doc == NULL)) {
1586 return NULL;
1587 }
1588
1589 /* Code adapted from libxml2 (2.10.4) */
1590 char prefix[50];
1591 int counter = 1;
1592 snprintf(prefix, sizeof(prefix), "default");
1593 xmlNsPtr nsptr = xmlSearchNs(doc, tree, (const xmlChar *) prefix);
1594 while (nsptr != NULL) {
1595 if (counter > 1000) {
1596 return NULL;
1597 }
1598 snprintf(prefix, sizeof(prefix), "default%d", counter++);
1599 nsptr = xmlSearchNs(doc, tree, (const xmlChar *) prefix);
1600 }
1601
1602 /* Search yielded no conflict */
1603 return xmlNewNs(tree, (const xmlChar *) uri, (const xmlChar *) prefix);
1604 }
1605
1606 /*
1607 http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-DocCrElNS
1608
1609 NAMESPACE_ERR: Raised if
1610
1611 3. the qualifiedName has a prefix that is "xml" and the namespaceURI is different from "http://www.w3.org/XML/1998/namespace" [XML Namespaces]
1612 4. the qualifiedName or its prefix is "xmlns" and the namespaceURI is different from "http://www.w3.org/2000/xmlns/"
1613 5. the namespaceURI is "http://www.w3.org/2000/xmlns/" and neither the qualifiedName nor its prefix is "xmlns".
1614 */
1615
dom_get_ns_unchecked(xmlNodePtr nodep,char * uri,char * prefix)1616 xmlNsPtr dom_get_ns_unchecked(xmlNodePtr nodep, char *uri, char *prefix)
1617 {
1618 xmlNsPtr nsptr = xmlNewNs(nodep, (xmlChar *)uri, (xmlChar *)prefix);
1619 if (UNEXPECTED(nsptr == NULL)) {
1620 /* Either memory allocation failure, or it's because of a prefix conflict.
1621 * We'll assume a conflict and try again. If it was a memory allocation failure we'll just fail again, whatever.
1622 * This isn't needed for every caller (such as createElementNS & DOMElement::__construct), but isn't harmful and simplifies the mental model "when do I use which function?".
1623 * This branch will also be taken unlikely anyway as in those cases it'll be for allocation failure. */
1624 return dom_get_ns_resolve_prefix_conflict(nodep, uri);
1625 }
1626
1627 return nsptr;
1628 }
1629
1630 /* {{{ xmlNsPtr dom_get_ns(xmlNodePtr nodep, char *uri, int *errorcode, char *prefix) */
dom_get_ns(xmlNodePtr nodep,char * uri,int * errorcode,char * prefix)1631 xmlNsPtr dom_get_ns(xmlNodePtr nodep, char *uri, int *errorcode, char *prefix) {
1632 xmlNsPtr nsptr;
1633
1634 if (! ((prefix && !strcmp (prefix, "xml") && strcmp(uri, (char *)XML_XML_NAMESPACE)) ||
1635 (prefix && !strcmp (prefix, "xmlns") && strcmp(uri, (char *)DOM_XMLNS_NAMESPACE)) ||
1636 (prefix && !strcmp(uri, (char *)DOM_XMLNS_NAMESPACE) && strcmp (prefix, "xmlns")))) {
1637 nsptr = dom_get_ns_unchecked(nodep, uri, prefix);
1638 if (UNEXPECTED(nsptr == NULL)) {
1639 goto err;
1640 }
1641 } else {
1642 goto err;
1643 }
1644
1645 *errorcode = 0;
1646 return nsptr;
1647 err:
1648 *errorcode = NAMESPACE_ERR;
1649 return NULL;
1650 }
1651 /* }}} end dom_get_ns */
1652
1653 /* {{{ xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName) */
dom_get_nsdecl(xmlNode * node,xmlChar * localName)1654 xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName) {
1655 xmlNsPtr cur;
1656 xmlNs *ret = NULL;
1657 if (node == NULL)
1658 return NULL;
1659
1660 if (localName == NULL || localName[0] == '\0') {
1661 cur = node->nsDef;
1662 while (cur != NULL) {
1663 if (cur->prefix == NULL && cur->href != NULL) {
1664 ret = cur;
1665 break;
1666 }
1667 cur = cur->next;
1668 }
1669 } else {
1670 cur = node->nsDef;
1671 while (cur != NULL) {
1672 if (cur->prefix != NULL && xmlStrEqual(localName, cur->prefix)) {
1673 ret = cur;
1674 break;
1675 }
1676 cur = cur->next;
1677 }
1678 }
1679 return ret;
1680 }
1681 /* }}} end dom_get_nsdecl */
1682
php_dom_create_fake_namespace_decl_node_ptr(xmlNodePtr nodep,xmlNsPtr original)1683 static xmlNodePtr php_dom_create_fake_namespace_decl_node_ptr(xmlNodePtr nodep, xmlNsPtr original)
1684 {
1685 xmlNodePtr attrp;
1686 xmlNsPtr curns = xmlNewNs(NULL, original->href, NULL);
1687 if (original->prefix) {
1688 curns->prefix = xmlStrdup(original->prefix);
1689 attrp = xmlNewDocNode(nodep->doc, NULL, (xmlChar *) original->prefix, original->href);
1690 } else {
1691 attrp = xmlNewDocNode(nodep->doc, NULL, (xmlChar *)"xmlns", original->href);
1692 }
1693 attrp->type = XML_NAMESPACE_DECL;
1694 attrp->parent = nodep;
1695 attrp->ns = curns;
1696 return attrp;
1697 }
1698
1699 /* Note: Assumes the additional lifetime was already added in the caller. */
php_dom_create_fake_namespace_decl(xmlNodePtr nodep,xmlNsPtr original,zval * return_value,dom_object * parent_intern)1700 xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern)
1701 {
1702 xmlNodePtr attrp = php_dom_create_fake_namespace_decl_node_ptr(nodep, original);
1703 php_dom_create_object(attrp, return_value, parent_intern);
1704 /* This object must exist, because we just created an object for it via php_dom_create_object(). */
1705 php_dom_namespace_node_obj_from_obj(Z_OBJ_P(return_value))->parent_intern = parent_intern;
1706 return attrp;
1707 }
1708
dom_nodemap_or_nodelist_process_offset_as_named(zval * offset,zend_long * lval)1709 static bool dom_nodemap_or_nodelist_process_offset_as_named(zval *offset, zend_long *lval)
1710 {
1711 if (Z_TYPE_P(offset) == IS_STRING) {
1712 /* See zval_get_long_func() */
1713 double dval;
1714 zend_uchar is_numeric_string_type;
1715 if (0 == (is_numeric_string_type = is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), lval, &dval, true))) {
1716 return true;
1717 } else if (is_numeric_string_type == IS_DOUBLE) {
1718 *lval = zend_dval_to_lval_cap(dval);
1719 }
1720 } else {
1721 *lval = zval_get_long(offset);
1722 }
1723 return false;
1724 }
1725
dom_nodelist_read_dimension(zend_object * object,zval * offset,int type,zval * rv)1726 static zval *dom_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
1727 {
1728 if (UNEXPECTED(!offset)) {
1729 zend_throw_error(NULL, "Cannot access DOMNodeList without offset");
1730 return NULL;
1731 }
1732
1733 ZVAL_DEREF(offset);
1734
1735 zend_long lval;
1736 if (dom_nodemap_or_nodelist_process_offset_as_named(offset, &lval)) {
1737 /* does not support named lookup */
1738 ZVAL_NULL(rv);
1739 return rv;
1740 }
1741
1742 php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, lval, rv);
1743 return rv;
1744 } /* }}} end dom_nodelist_read_dimension */
1745
dom_nodelist_has_dimension(zend_object * object,zval * member,int check_empty)1746 static int dom_nodelist_has_dimension(zend_object *object, zval *member, int check_empty)
1747 {
1748 ZVAL_DEREF(member);
1749
1750 zend_long offset;
1751 if (dom_nodemap_or_nodelist_process_offset_as_named(member, &offset)) {
1752 /* does not support named lookup */
1753 return 0;
1754 }
1755
1756 return offset >= 0 && offset < php_dom_get_nodelist_length(php_dom_obj_from_obj(object));
1757 } /* }}} end dom_nodelist_has_dimension */
1758
dom_remove_all_children(xmlNodePtr nodep)1759 void dom_remove_all_children(xmlNodePtr nodep)
1760 {
1761 if (nodep->children) {
1762 node_list_unlink(nodep->children);
1763 php_libxml_node_free_list((xmlNodePtr) nodep->children);
1764 nodep->children = NULL;
1765 nodep->last = NULL;
1766 }
1767 }
1768
php_dom_get_content_into_zval(const xmlNode * nodep,zval * retval,bool default_is_null)1769 void php_dom_get_content_into_zval(const xmlNode *nodep, zval *retval, bool default_is_null)
1770 {
1771 ZEND_ASSERT(nodep != NULL);
1772
1773 if (nodep->type == XML_TEXT_NODE
1774 || nodep->type == XML_CDATA_SECTION_NODE
1775 || nodep->type == XML_PI_NODE
1776 || nodep->type == XML_COMMENT_NODE) {
1777 char *str = (char * ) nodep->content;
1778 if (str != NULL) {
1779 ZVAL_STRING(retval, str);
1780 } else {
1781 goto failure;
1782 }
1783 return;
1784 }
1785
1786 char *str = (char *) xmlNodeGetContent(nodep);
1787
1788 if (str != NULL) {
1789 ZVAL_STRING(retval, str);
1790 xmlFree(str);
1791 return;
1792 }
1793
1794 failure:
1795 if (default_is_null) {
1796 ZVAL_NULL(retval);
1797 } else {
1798 ZVAL_EMPTY_STRING(retval);
1799 }
1800 }
1801
dom_nodemap_read_dimension(zend_object * object,zval * offset,int type,zval * rv)1802 static zval *dom_nodemap_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
1803 {
1804 if (UNEXPECTED(!offset)) {
1805 zend_throw_error(NULL, "Cannot access DOMNamedNodeMap without offset");
1806 return NULL;
1807 }
1808
1809 ZVAL_DEREF(offset);
1810
1811 zend_long lval;
1812 if (dom_nodemap_or_nodelist_process_offset_as_named(offset, &lval)) {
1813 /* exceptional case, switch to named lookup */
1814 php_dom_named_node_map_get_named_item_into_zval(php_dom_obj_from_obj(object)->ptr, Z_STRVAL_P(offset), rv);
1815 return rv;
1816 }
1817
1818 /* see PHP_METHOD(DOMNamedNodeMap, item) */
1819 if (UNEXPECTED(lval < 0 || ZEND_LONG_INT_OVFL(lval))) {
1820 zend_value_error("must be between 0 and %d", INT_MAX);
1821 return NULL;
1822 }
1823
1824 php_dom_named_node_map_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, lval, rv);
1825 return rv;
1826 } /* }}} end dom_nodemap_read_dimension */
1827
dom_nodemap_has_dimension(zend_object * object,zval * member,int check_empty)1828 static int dom_nodemap_has_dimension(zend_object *object, zval *member, int check_empty)
1829 {
1830 ZVAL_DEREF(member);
1831
1832 zend_long offset;
1833 if (dom_nodemap_or_nodelist_process_offset_as_named(member, &offset)) {
1834 /* exceptional case, switch to named lookup */
1835 return php_dom_named_node_map_get_named_item(php_dom_obj_from_obj(object)->ptr, Z_STRVAL_P(member), false) != NULL;
1836 }
1837
1838 return offset >= 0 && offset < php_dom_get_namednodemap_length(php_dom_obj_from_obj(object));
1839 } /* }}} end dom_nodemap_has_dimension */
1840
1841 #endif /* HAVE_DOM */
1842