xref: /php-src/ext/dom/node.c (revision b112d27f)
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    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 #include "php.h"
23 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 #include "namespace_compat.h"
26 #include "private_data.h"
27 #include "internal_helpers.h"
28 #include "dom_properties.h"
29 
30 /*
31 * class DOMNode
32 *
33 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1950641247
34 * Since:
35 */
36 
dom_node_concatenated_name_helper(size_t name_len,const char * name,size_t prefix_len,const char * prefix)37 zend_string *dom_node_concatenated_name_helper(size_t name_len, const char *name, size_t prefix_len, const char *prefix)
38 {
39 	/* prefix_len can't overflow because it would need to occupy the entire address space */
40 	zend_string *str = zend_string_safe_alloc(1, name_len, prefix_len + 1, false);
41 	memcpy(ZSTR_VAL(str), prefix, prefix_len);
42 	ZSTR_VAL(str)[prefix_len] = ':';
43 	memcpy(ZSTR_VAL(str) + prefix_len + 1, name, name_len + 1 /* include \0 */);
44 	return str;
45 }
46 
dom_node_get_node_name_attribute_or_element(const xmlNode * nodep,bool uppercase)47 zend_string *dom_node_get_node_name_attribute_or_element(const xmlNode *nodep, bool uppercase)
48 {
49 	zend_string *ret;
50 	size_t name_len = strlen((const char *) nodep->name);
51 	if (nodep->ns != NULL && nodep->ns->prefix != NULL) {
52 		ret = dom_node_concatenated_name_helper(name_len, (const char *) nodep->name, strlen((const char *) nodep->ns->prefix), (const char *) nodep->ns->prefix);
53 	} else {
54 		ret = zend_string_init((const char *) nodep->name, name_len, false);
55 	}
56 	if (uppercase) {
57 		zend_str_toupper(ZSTR_VAL(ret), ZSTR_LEN(ret));
58 	}
59 	return ret;
60 }
61 
php_dom_is_node_connected(const xmlNode * node)62 bool php_dom_is_node_connected(const xmlNode *node)
63 {
64 	ZEND_ASSERT(node != NULL);
65 	do {
66 		if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
67 			return true;
68 		}
69 		node = node->parent;
70 	} while (node != NULL);
71 	return false;
72 }
73 
74 /* {{{ nodeName	string
75 readonly=yes
76 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D095
77 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-nodename
78 Since:
79 */
dom_node_node_name_read(dom_object * obj,zval * retval)80 zend_result dom_node_node_name_read(dom_object *obj, zval *retval)
81 {
82 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
83 
84 	bool uppercase = false;
85 
86 	switch (nodep->type) {
87 		case XML_ELEMENT_NODE:
88 			uppercase = php_dom_follow_spec_intern(obj) && php_dom_ns_is_html_and_document_is_html(nodep);
89 			ZEND_FALLTHROUGH;
90 		case XML_ATTRIBUTE_NODE:
91 			ZVAL_NEW_STR(retval, dom_node_get_node_name_attribute_or_element(nodep, uppercase));
92 			break;
93 		case XML_NAMESPACE_DECL: {
94 			xmlNsPtr ns = nodep->ns;
95 			if (ns != NULL && ns->prefix) {
96 				zend_string *str = dom_node_concatenated_name_helper(strlen((const char *) ns->prefix), (const char *) ns->prefix, strlen("xmlns"), "xmlns");
97 				ZVAL_NEW_STR(retval, str);
98 			} else {
99 				ZVAL_STRING(retval, (const char *) nodep->name);
100 			}
101 			break;
102 		}
103 		case XML_DOCUMENT_TYPE_NODE:
104 		case XML_DTD_NODE:
105 		case XML_PI_NODE:
106 		case XML_ENTITY_DECL:
107 		case XML_ENTITY_REF_NODE:
108 		case XML_NOTATION_NODE:
109 			ZVAL_STRING(retval, (char *) nodep->name);
110 			break;
111 		case XML_CDATA_SECTION_NODE:
112 			ZVAL_STRING(retval, "#cdata-section");
113 			break;
114 		case XML_COMMENT_NODE:
115 			ZVAL_STRING(retval, "#comment");
116 			break;
117 		case XML_HTML_DOCUMENT_NODE:
118 		case XML_DOCUMENT_NODE:
119 			ZVAL_STRING(retval, "#document");
120 			break;
121 		case XML_DOCUMENT_FRAG_NODE:
122 			ZVAL_STRING(retval, "#document-fragment");
123 			break;
124 		case XML_TEXT_NODE:
125 			ZVAL_STRING(retval, "#text");
126 			break;
127 		EMPTY_SWITCH_DEFAULT_CASE();
128 	}
129 
130 	return SUCCESS;
131 }
132 
133 /* }}} */
134 
135 /* {{{ nodeValue	string
136 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D080
137 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-nodevalue
138 Since:
139 */
dom_node_node_value_read(dom_object * obj,zval * retval)140 zend_result dom_node_node_value_read(dom_object *obj, zval *retval)
141 {
142 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
143 
144 	/* Access to Element node is implemented as a convenience method */
145 	switch (nodep->type) {
146 		case XML_ELEMENT_NODE: {
147 			if (php_dom_follow_spec_intern(obj)) {
148 				ZVAL_NULL(retval);
149 				break;
150 			}
151 			ZEND_FALLTHROUGH;
152 		}
153 		case XML_ATTRIBUTE_NODE:
154 		case XML_TEXT_NODE:
155 		case XML_COMMENT_NODE:
156 		case XML_CDATA_SECTION_NODE:
157 		case XML_PI_NODE:
158 			php_dom_get_content_into_zval(nodep, retval, true);
159 			break;
160 		case XML_NAMESPACE_DECL: {
161 			char *str = (char *) xmlNodeGetContent(nodep->children);
162 			if (str != NULL) {
163 				ZVAL_STRING(retval, str);
164 				xmlFree(str);
165 			} else {
166 				ZVAL_NULL(retval);
167 			}
168 			break;
169 		}
170 		default:
171 			ZVAL_NULL(retval);
172 			break;
173 	}
174 
175 	return SUCCESS;
176 }
177 
dom_node_node_value_write(dom_object * obj,zval * newval)178 zend_result dom_node_node_value_write(dom_object *obj, zval *newval)
179 {
180 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
181 
182 	/* Cannot fail because the type is either null or a string. */
183 	zend_string *str = zval_get_string(newval);
184 
185 	/* Access to Element node is implemented as a convenience method */
186 	switch (nodep->type) {
187 		case XML_ATTRIBUTE_NODE:
188 			dom_attr_value_will_change(obj, (xmlAttrPtr) nodep);
189 			if (php_dom_follow_spec_intern(obj)) {
190 				dom_remove_all_children(nodep);
191 				xmlAddChild(nodep, xmlNewTextLen(BAD_CAST ZSTR_VAL(str), ZSTR_LEN(str)));
192 				break;
193 			}
194 			ZEND_FALLTHROUGH;
195 		case XML_ELEMENT_NODE:
196 			dom_remove_all_children(nodep);
197 			ZEND_FALLTHROUGH;
198 		case XML_TEXT_NODE:
199 		case XML_COMMENT_NODE:
200 		case XML_CDATA_SECTION_NODE:
201 		case XML_PI_NODE:
202 			xmlNodeSetContentLen(nodep, BAD_CAST ZSTR_VAL(str), ZSTR_LEN(str));
203 			break;
204 		default:
205 			break;
206 	}
207 
208 	php_libxml_invalidate_node_list_cache(obj->document);
209 
210 	zend_string_release_ex(str, 0);
211 	return SUCCESS;
212 }
213 
214 /* }}} */
215 
216 /* {{{ nodeType	int
217 readonly=yes
218 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-111237558
219 Since:
220 */
dom_node_node_type_read(dom_object * obj,zval * retval)221 zend_result dom_node_node_type_read(dom_object *obj, zval *retval)
222 {
223 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
224 
225 	/* Specs dictate that they are both type XML_DOCUMENT_TYPE_NODE */
226 	if (nodep->type == XML_DTD_NODE) {
227 		ZVAL_LONG(retval, XML_DOCUMENT_TYPE_NODE);
228 	} else {
229 		ZVAL_LONG(retval, nodep->type);
230 	}
231 
232 	return SUCCESS;
233 }
234 
235 /* }}} */
236 
dom_node_parent_get(dom_object * obj,zval * retval,bool only_element)237 static zend_result dom_node_parent_get(dom_object *obj, zval *retval, bool only_element)
238 {
239 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
240 
241 	xmlNodePtr nodeparent = nodep->parent;
242 	if (!nodeparent || (only_element && nodeparent->type != XML_ELEMENT_NODE)) {
243 		ZVAL_NULL(retval);
244 		return SUCCESS;
245 	}
246 
247 	php_dom_create_object(nodeparent, retval, obj);
248 	return SUCCESS;
249 }
250 
251 /* {{{ parentNode	?DomNode
252 readonly=yes
253 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1060184317
254 Since:
255 */
dom_node_parent_node_read(dom_object * obj,zval * retval)256 zend_result dom_node_parent_node_read(dom_object *obj, zval *retval)
257 {
258 	return dom_node_parent_get(obj, retval, false);
259 }
260 
261 /* }}} */
262 
263 /* {{{ parentElement	?DomElement
264 readonly=yes
265 URL: https://dom.spec.whatwg.org/#parent-element
266 Since:
267 */
dom_node_parent_element_read(dom_object * obj,zval * retval)268 zend_result dom_node_parent_element_read(dom_object *obj, zval *retval)
269 {
270 	return dom_node_parent_get(obj, retval, true);
271 }
272 
273 /* }}} */
274 
275 /* {{{ childNodes	DomNodeList
276 readonly=yes
277 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1451460987
278 Since:
279 */
dom_node_child_nodes_read(dom_object * obj,zval * retval)280 zend_result dom_node_child_nodes_read(dom_object *obj, zval *retval)
281 {
282 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
283 
284 	php_dom_create_iterator(retval, DOM_NODELIST, php_dom_follow_spec_intern(obj));
285 	dom_object *intern = Z_DOMOBJ_P(retval);
286 	dom_namednode_iter(obj, XML_ELEMENT_NODE, intern, NULL, NULL, 0, NULL, 0);
287 
288 	return SUCCESS;
289 }
290 /* }}} */
291 
292 /* {{{ firstChild DomNode
293 readonly=yes
294 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-169727388
295 Since:
296 */
dom_node_first_child_read(dom_object * obj,zval * retval)297 zend_result dom_node_first_child_read(dom_object *obj, zval *retval)
298 {
299 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
300 
301 	xmlNodePtr first = NULL;
302 	if (dom_node_children_valid(nodep)) {
303 		first = nodep->children;
304 	}
305 
306 	php_dom_create_nullable_object(first, retval, obj);
307 	return SUCCESS;
308 }
309 
310 /* }}} */
311 
312 /* {{{ lastChild	DomNode
313 readonly=yes
314 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-61AD09FB
315 Since:
316 */
dom_node_last_child_read(dom_object * obj,zval * retval)317 zend_result dom_node_last_child_read(dom_object *obj, zval *retval)
318 {
319 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
320 
321 	xmlNodePtr last = NULL;
322 	if (dom_node_children_valid(nodep)) {
323 		last = nodep->last;
324 	}
325 
326 	php_dom_create_nullable_object(last, retval, obj);
327 	return SUCCESS;
328 }
329 
330 /* }}} */
331 
332 /* {{{ previousSibling	DomNode
333 readonly=yes
334 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-640FB3C8
335 Since:
336 */
dom_node_previous_sibling_read(dom_object * obj,zval * retval)337 zend_result dom_node_previous_sibling_read(dom_object *obj, zval *retval)
338 {
339 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
340 
341 	xmlNodePtr prevsib = nodep->prev;
342 
343 	php_dom_create_nullable_object(prevsib, retval, obj);
344 	return SUCCESS;
345 }
346 
347 /* }}} */
348 
349 /* {{{ nextSibling	DomNode
350 readonly=yes
351 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6AC54C2F
352 Since:
353 */
dom_node_next_sibling_read(dom_object * obj,zval * retval)354 zend_result dom_node_next_sibling_read(dom_object *obj, zval *retval)
355 {
356 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
357 
358 	xmlNodePtr nextsib = nodep->next;
359 
360 	php_dom_create_nullable_object(nextsib, retval, obj);
361 	return SUCCESS;
362 }
363 
364 /* }}} */
365 
366 /* {{{ previousElementSibling	DomNode
367 readonly=yes
368 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-640FB3C8
369 Since:
370 */
dom_node_previous_element_sibling_read(dom_object * obj,zval * retval)371 zend_result dom_node_previous_element_sibling_read(dom_object *obj, zval *retval)
372 {
373 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
374 
375 	xmlNodePtr prevsib = nodep->prev;
376 
377 	while (prevsib && prevsib->type != XML_ELEMENT_NODE) {
378 		prevsib = prevsib->prev;
379 	}
380 
381 	php_dom_create_nullable_object(prevsib, retval, obj);
382 	return SUCCESS;
383 }
384 
385 /* }}} */
386 
387 /* {{{ nextElementSibling	DomNode
388 readonly=yes
389 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6AC54C2F
390 Since:
391 */
dom_node_next_element_sibling_read(dom_object * obj,zval * retval)392 zend_result dom_node_next_element_sibling_read(dom_object *obj, zval *retval)
393 {
394 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
395 
396 	xmlNodePtr nextsib = nodep->next;
397 
398 	while (nextsib != NULL && nextsib->type != XML_ELEMENT_NODE) {
399 		nextsib = nextsib->next;
400 	}
401 
402 	php_dom_create_nullable_object(nextsib, retval, obj);
403 	return SUCCESS;
404 }
405 
406 /* }}} */
407 
408 /* {{{ attributes	DomNamedNodeMap
409 readonly=yes
410 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-84CF096
411 Since:
412 */
dom_node_attributes_read(dom_object * obj,zval * retval)413 zend_result dom_node_attributes_read(dom_object *obj, zval *retval)
414 {
415 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
416 
417 	if (nodep->type == XML_ELEMENT_NODE) {
418 		php_dom_create_iterator(retval, DOM_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
419 		dom_object *intern = Z_DOMOBJ_P(retval);
420 		dom_namednode_iter(obj, XML_ATTRIBUTE_NODE, intern, NULL, NULL, 0, NULL, 0);
421 	} else {
422 		ZVAL_NULL(retval);
423 	}
424 
425 	return SUCCESS;
426 }
427 
428 /* }}} */
429 
430 /* {{{ isConnected	boolean
431 readonly=yes
432 URL: https://dom.spec.whatwg.org/#dom-node-isconnected
433 Since:
434 */
dom_node_is_connected_read(dom_object * obj,zval * retval)435 zend_result dom_node_is_connected_read(dom_object *obj, zval *retval)
436 {
437 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
438 	ZVAL_BOOL(retval, php_dom_is_node_connected(nodep));
439 	return SUCCESS;
440 }
441 /* }}} */
442 
443 /* {{{ ownerDocument	DomDocument
444 readonly=yes
445 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-node-ownerDoc
446 Since:
447 */
dom_node_owner_document_read(dom_object * obj,zval * retval)448 zend_result dom_node_owner_document_read(dom_object *obj, zval *retval)
449 {
450 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
451 
452 	if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
453 		ZVAL_NULL(retval);
454 		return SUCCESS;
455 	}
456 
457 	xmlDocPtr docp = nodep->doc;
458 	if (!docp) {
459 		return FAILURE;
460 	}
461 
462 	php_dom_create_object((xmlNodePtr) docp, retval, obj);
463 	return SUCCESS;
464 }
465 
466 /* }}} */
467 
468 /* {{{ namespaceUri	string
469 readonly=yes
470 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSname
471 Since: DOM Level 2
472 */
dom_node_namespace_uri_read(dom_object * obj,zval * retval)473 zend_result dom_node_namespace_uri_read(dom_object *obj, zval *retval)
474 {
475 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
476 
477 	const char *str = NULL;
478 	switch (nodep->type) {
479 		case XML_ELEMENT_NODE:
480 		case XML_ATTRIBUTE_NODE:
481 		case XML_NAMESPACE_DECL:
482 			if (nodep->ns != NULL) {
483 				str = (const char *) nodep->ns->href;
484 			}
485 			break;
486 		default:
487 			str = NULL;
488 			break;
489 	}
490 
491 	if (str != NULL) {
492 		ZVAL_STRING(retval, str);
493 	} else {
494 		ZVAL_NULL(retval);
495 	}
496 
497 	return SUCCESS;
498 }
499 
500 /* }}} */
501 
502 /* {{{ prefix	string
503 readonly=no
504 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSPrefix
505 Modern spec URL: https://dom.spec.whatwg.org/#concept-element-namespace-prefix
506 Since: DOM Level 2
507 */
dom_node_prefix_read(dom_object * obj,zval * retval)508 zend_result dom_node_prefix_read(dom_object *obj, zval *retval)
509 {
510 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
511 
512 	const char *str = NULL;
513 	switch (nodep->type) {
514 		case XML_ELEMENT_NODE:
515 		case XML_ATTRIBUTE_NODE:
516 		case XML_NAMESPACE_DECL: {
517 			xmlNsPtr ns = nodep->ns;
518 			if (ns != NULL && ns->prefix) {
519 				str = (char *) ns->prefix;
520 			}
521 			break;
522 		}
523 		default:
524 			str = NULL;
525 			break;
526 	}
527 
528 	if (str == NULL) {
529 		ZVAL_EMPTY_STRING(retval);
530 	} else {
531 		ZVAL_STRING(retval, str);
532 	}
533 	return SUCCESS;
534 }
535 
dom_modern_node_prefix_read(dom_object * obj,zval * retval)536 zend_result dom_modern_node_prefix_read(dom_object *obj, zval *retval)
537 {
538 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
539 
540 	xmlNsPtr ns = nodep->ns;
541 	if (ns != NULL && ns->prefix != NULL) {
542 		ZVAL_STRING(retval, (const char *) ns->prefix);
543 	} else {
544 		ZVAL_NULL(retval);
545 	}
546 	return SUCCESS;
547 }
548 
dom_node_prefix_write(dom_object * obj,zval * newval)549 zend_result dom_node_prefix_write(dom_object *obj, zval *newval)
550 {
551 	zend_string *prefix_str;
552 	xmlNode *nsnode = NULL;
553 	xmlNsPtr ns = NULL, curns;
554 	char *strURI;
555 	char *prefix;
556 
557 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
558 
559 	switch (nodep->type) {
560 		case XML_ELEMENT_NODE:
561 			nsnode = nodep;
562 			ZEND_FALLTHROUGH;
563 		case XML_ATTRIBUTE_NODE:
564 			if (nsnode == NULL) {
565 				nsnode = nodep->parent;
566 				if (nsnode == NULL) {
567 					nsnode = xmlDocGetRootElement(nodep->doc);
568 				}
569 			}
570 			/* Typed property, this is already a string */
571 			ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
572 			prefix_str = Z_STR_P(newval);
573 
574 			prefix = ZSTR_VAL(prefix_str);
575 			if (*prefix == '\0') {
576 				/* The empty string namespace prefix does not exist.
577 				 * We should fall back to the default namespace in this case. */
578 				prefix = NULL;
579 			}
580 			if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, BAD_CAST prefix)) {
581 				strURI = (char *) nodep->ns->href;
582 				/* Validate namespace naming constraints */
583 				if (strURI == NULL ||
584 					(zend_string_equals_literal(prefix_str, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) ||
585 					(nodep->type == XML_ATTRIBUTE_NODE && zend_string_equals_literal(prefix_str, "xmlns") &&
586 					 strcmp(strURI, DOM_XMLNS_NS_URI)) ||
587 					(nodep->type == XML_ATTRIBUTE_NODE && !strcmp((char *) nodep->name, "xmlns"))) {
588 					php_dom_throw_error(NAMESPACE_ERR, dom_get_strict_error(obj->document));
589 					return FAILURE;
590 				} else {
591 					curns = nsnode->nsDef;
592 					while (curns != NULL) {
593 						if (xmlStrEqual(BAD_CAST prefix, curns->prefix) && xmlStrEqual(nodep->ns->href, curns->href)) {
594 							ns = curns;
595 							break;
596 						}
597 						curns = curns->next;
598 					}
599 					if (ns == NULL) {
600 						ns = xmlNewNs(nsnode, nodep->ns->href, BAD_CAST prefix);
601 						/* Sadly, we cannot distinguish between OOM and namespace conflict.
602 						 * But OOM will almost never happen. */
603 						if (UNEXPECTED(ns == NULL)) {
604 							php_dom_throw_error(NAMESPACE_ERR, /* strict */ true);
605 							return FAILURE;
606 						}
607 					}
608 				}
609 
610 				xmlSetNs(nodep, ns);
611 			}
612 			break;
613 		default:
614 			break;
615 	}
616 
617 	return SUCCESS;
618 }
619 
620 /* }}} */
621 
622 /* {{{ localName	string
623 readonly=yes
624 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSLocalN
625 Since: DOM Level 2
626 */
dom_node_local_name_read(dom_object * obj,zval * retval)627 zend_result dom_node_local_name_read(dom_object *obj, zval *retval)
628 {
629 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
630 
631 	if (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE || nodep->type == XML_NAMESPACE_DECL) {
632 		ZVAL_STRING(retval, (char *) (nodep->name));
633 	} else {
634 		ZVAL_NULL(retval);
635 	}
636 
637 	return SUCCESS;
638 }
639 
640 /* }}} */
641 
642 /* {{{ baseURI	string
643 readonly=yes
644 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-baseURI
645 Since: DOM Level 3
646 */
dom_node_base_uri_read(dom_object * obj,zval * retval)647 zend_result dom_node_base_uri_read(dom_object *obj, zval *retval)
648 {
649 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
650 
651 	xmlChar *baseuri = xmlNodeGetBase(nodep->doc, nodep);
652 	if (baseuri) {
653 		ZVAL_STRING(retval, (const char *) baseuri);
654 		xmlFree(baseuri);
655 	} else {
656 		if (php_dom_follow_spec_intern(obj)) {
657 			if (nodep->doc->URL) {
658 				ZVAL_STRING(retval, (const char *) nodep->doc->URL);
659 			} else {
660 				ZVAL_STRING(retval, "about:blank");
661 			}
662 		} else {
663 			ZVAL_NULL(retval);
664 		}
665 	}
666 
667 	return SUCCESS;
668 }
669 
670 /* }}} */
671 
672 /* {{{ textContent	string
673 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-textContent
674 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-textcontent
675 Since: DOM Level 3
676 */
677 /* Determines when the operation is a no-op. */
dom_skip_text_content(dom_object * obj,xmlNodePtr nodep)678 static bool dom_skip_text_content(dom_object *obj, xmlNodePtr nodep)
679 {
680 	if (php_dom_follow_spec_intern(obj)) {
681 		int type = nodep->type;
682 		if (type != XML_DOCUMENT_FRAG_NODE && type != XML_ELEMENT_NODE && type != XML_ATTRIBUTE_NODE
683 			&& type != XML_TEXT_NODE && type != XML_CDATA_SECTION_NODE && type != XML_COMMENT_NODE && type != XML_PI_NODE) {
684 			/* Yes, success... It's a no-op for these cases. */
685 			return true;
686 		}
687 	}
688 	return false;
689 }
690 
dom_node_text_content_read(dom_object * obj,zval * retval)691 zend_result dom_node_text_content_read(dom_object *obj, zval *retval)
692 {
693 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
694 
695 	if (dom_skip_text_content(obj, nodep)) {
696 		ZVAL_NULL(retval);
697 	} else {
698 		php_dom_get_content_into_zval(nodep, retval, false);
699 	}
700 
701 	return SUCCESS;
702 }
703 
dom_node_text_content_write(dom_object * obj,zval * newval)704 zend_result dom_node_text_content_write(dom_object *obj, zval *newval)
705 {
706 	DOM_PROP_NODE(xmlNodePtr, nodep, obj);
707 
708 	php_libxml_invalidate_node_list_cache(obj->document);
709 
710 	/* Typed property, this is already a string */
711 	ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING || Z_TYPE_P(newval) == IS_NULL);
712 	const xmlChar *xmlChars;
713 	size_t len;
714 	if (Z_TYPE_P(newval) == IS_NULL) {
715 		xmlChars = (const xmlChar *) "";
716 		len = 0;
717 	} else {
718 		xmlChars = (const xmlChar *) Z_STRVAL_P(newval);
719 		len = Z_STRLEN_P(newval);
720 	}
721 
722 	int type = nodep->type;
723 
724 	/* We can't directly call xmlNodeSetContent, because it might encode the string through
725 	 * xmlStringLenGetNodeList for types XML_DOCUMENT_FRAG_NODE, XML_ELEMENT_NODE, XML_ATTRIBUTE_NODE.
726 	 * See tree.c:xmlNodeSetContent in libxml.
727 	 * In these cases we need to use a text node to avoid the encoding.
728 	 * For the other cases, we *can* rely on xmlNodeSetContent because it is either a no-op, or handles
729 	 * the content without encoding. */
730 	if (type == XML_DOCUMENT_FRAG_NODE || type == XML_ELEMENT_NODE || type == XML_ATTRIBUTE_NODE) {
731 		dom_remove_all_children(nodep);
732 		xmlNode *textNode = xmlNewDocTextLen(nodep->doc, xmlChars, len);
733 		xmlAddChild(nodep, textNode);
734 	} else {
735 		xmlNodeSetContent(nodep, xmlChars);
736 	}
737 
738 	return SUCCESS;
739 }
740 
741 /* }}} */
742 
743 /* Returns true if the node had the same document reference, false otherwise. */
dom_set_document_ref_obj_single(xmlNodePtr node,php_libxml_ref_obj * document)744 static bool dom_set_document_ref_obj_single(xmlNodePtr node, php_libxml_ref_obj *document)
745 {
746 	dom_object *childobj = php_dom_object_get_data(node);
747 	if (!childobj) {
748 		return true;
749 	}
750 	if (!childobj->document) {
751 		childobj->document = document;
752 		document->refcount++;
753 		return true;
754 	}
755 	return false;
756 }
757 
dom_set_document_ref_pointers_attr(xmlAttrPtr attr,php_libxml_ref_obj * document)758 void dom_set_document_ref_pointers_attr(xmlAttrPtr attr, php_libxml_ref_obj *document)
759 {
760 	ZEND_ASSERT(document != NULL);
761 
762 	dom_set_document_ref_obj_single((xmlNodePtr) attr, document);
763 	for (xmlNodePtr attr_child = attr->children; attr_child; attr_child = attr_child->next) {
764 		dom_set_document_ref_obj_single(attr_child, document);
765 	}
766 }
767 
dom_set_document_ref_pointers_node(xmlNodePtr node,php_libxml_ref_obj * document)768 static bool dom_set_document_ref_pointers_node(xmlNodePtr node, php_libxml_ref_obj *document)
769 {
770 	ZEND_ASSERT(document != NULL);
771 
772 	if (!dom_set_document_ref_obj_single(node, document)) {
773 		return false;
774 	}
775 
776 	if (node->type == XML_ELEMENT_NODE) {
777 		for (xmlAttrPtr attr = node->properties; attr; attr = attr->next) {
778 			dom_set_document_ref_pointers_attr(attr, document);
779 		}
780 	}
781 
782 	return true;
783 }
784 
dom_set_document_ref_pointers(xmlNodePtr node,php_libxml_ref_obj * document)785 void dom_set_document_ref_pointers(xmlNodePtr node, php_libxml_ref_obj *document)
786 {
787 	if (!document) {
788 		return;
789 	}
790 
791 	if (!dom_set_document_ref_pointers_node(node, document)) {
792 		return;
793 	}
794 
795 	xmlNodePtr base = node;
796 	node = node->children;
797 	while (node != NULL && dom_set_document_ref_pointers_node(node, document)) {
798 		node = php_dom_next_in_tree_order(node, base);
799 	}
800 }
801 
dom_insert_fragment(xmlNodePtr nodep,xmlNodePtr prevsib,xmlNodePtr nextsib,xmlNodePtr fragment,dom_object * intern)802 static xmlNodePtr dom_insert_fragment(xmlNodePtr nodep, xmlNodePtr prevsib, xmlNodePtr nextsib, xmlNodePtr fragment, dom_object *intern) /* {{{ */
803 {
804 	xmlNodePtr newchild = fragment->children;
805 
806 	if (newchild) {
807 		if (prevsib == NULL) {
808 			nodep->children = newchild;
809 		} else {
810 			prevsib->next = newchild;
811 		}
812 		newchild->prev = prevsib;
813 		if (nextsib == NULL) {
814 			nodep->last = fragment->last;
815 		} else {
816 			fragment->last->next = nextsib;
817 			nextsib->prev = fragment->last;
818 		}
819 
820 		/* Assign parent node pointer */
821 		xmlNodePtr node = newchild;
822 		while (node != NULL) {
823 			node->parent = nodep;
824 			if (node == fragment->last) {
825 				break;
826 			}
827 			node = node->next;
828 		}
829 
830 		fragment->children = NULL;
831 		fragment->last = NULL;
832 	}
833 
834 	return newchild;
835 }
836 /* }}} */
837 
dom_node_check_legacy_insertion_validity(xmlNodePtr parentp,xmlNodePtr child,bool stricterror,bool warn_empty_fragment)838 static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNodePtr child, bool stricterror, bool warn_empty_fragment)
839 {
840 	if (dom_node_is_read_only(parentp) ||
841 		(child->parent != NULL && dom_node_is_read_only(child->parent))) {
842 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
843 		return false;
844 	}
845 
846 	if (dom_hierarchy(parentp, child) == FAILURE) {
847 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
848 		return false;
849 	}
850 
851 	if (child->doc != parentp->doc && child->doc != NULL) {
852 		php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
853 		return false;
854 	}
855 
856 	if (warn_empty_fragment && child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
857 		/* TODO Drop Warning? */
858 		php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
859 		return false;
860 	}
861 
862 	/* In old DOM only text nodes and entity nodes can be added as children to attributes. */
863 	if (parentp->type == XML_ATTRIBUTE_NODE && child->type != XML_TEXT_NODE && child->type != XML_ENTITY_REF_NODE) {
864 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
865 		return false;
866 	}
867 	/* Attributes must be in elements. */
868 	if (child->type == XML_ATTRIBUTE_NODE && parentp->type != XML_ELEMENT_NODE) {
869 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
870 		return false;
871 	}
872 
873 	/* Documents can never be a child. */
874 	if (child->type == XML_DOCUMENT_NODE || child->type == XML_HTML_DOCUMENT_NODE) {
875 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
876 		return false;
877 	}
878 
879 	return true;
880 }
881 
882 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727
883 Since:
884 */
dom_node_insert_before_legacy(zval * return_value,zval * ref,dom_object * intern,dom_object * childobj,xmlNodePtr parentp,xmlNodePtr child)885 static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_object *intern, dom_object *childobj, xmlNodePtr parentp, xmlNodePtr child)
886 {
887 	if (!dom_node_children_valid(parentp)) {
888 		RETURN_FALSE;
889 	}
890 
891 	xmlNodePtr new_child = NULL;
892 	bool stricterror = dom_get_strict_error(intern->document);
893 
894 	if (!dom_node_check_legacy_insertion_validity(parentp, child, stricterror, true)) {
895 		RETURN_FALSE;
896 	}
897 
898 	xmlNodePtr refp = NULL;
899 	if (ref != NULL) {
900 		dom_object *refpobj;
901 		DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj);
902 		if (refp->parent != parentp) {
903 			php_dom_throw_error(NOT_FOUND_ERR, stricterror);
904 			RETURN_FALSE;
905 		}
906 	}
907 
908 	if (child->doc == NULL && parentp->doc != NULL) {
909 		xmlSetTreeDoc(child, parentp->doc);
910 		dom_set_document_ref_pointers(child, intern->document);
911 	}
912 
913 	php_libxml_invalidate_node_list_cache(intern->document);
914 
915 	if (ref != NULL) {
916 		if (child->parent != NULL) {
917 			xmlUnlinkNode(child);
918 		}
919 
920 		if (child->type == XML_TEXT_NODE && (refp->type == XML_TEXT_NODE ||
921 			(refp->prev != NULL && refp->prev->type == XML_TEXT_NODE))) {
922 			new_child = child;
923 			new_child->parent = refp->parent;
924 			new_child->next = refp;
925 			new_child->prev = refp->prev;
926 			refp->prev = new_child;
927 			if (new_child->prev != NULL) {
928 				new_child->prev->next = new_child;
929 			}
930 			if (new_child->parent != NULL) {
931 				if (new_child->parent->children == refp) {
932 					new_child->parent->children = new_child;
933 				}
934 			}
935 
936 		} else if (child->type == XML_ATTRIBUTE_NODE) {
937 			xmlAttrPtr lastattr;
938 
939 			if (child->ns == NULL)
940 				lastattr = xmlHasProp(refp->parent, child->name);
941 			else
942 				lastattr = xmlHasNsProp(refp->parent, child->name, child->ns->href);
943 			if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
944 				if (lastattr != (xmlAttrPtr) child) {
945 					xmlUnlinkNode((xmlNodePtr) lastattr);
946 					php_libxml_node_free_resource((xmlNodePtr) lastattr);
947 				} else {
948 					DOM_RET_OBJ(child, intern);
949 					return;
950 				}
951 			}
952 			new_child = xmlAddPrevSibling(refp, child);
953 			if (UNEXPECTED(NULL == new_child)) {
954 				goto cannot_add;
955 			}
956 		} else if (child->type == XML_DOCUMENT_FRAG_NODE) {
957 			xmlNodePtr last = child->last;
958 			new_child = dom_insert_fragment(parentp, refp->prev, refp, child, intern);
959 			dom_reconcile_ns_list(parentp->doc, new_child, last);
960 		} else {
961 			new_child = xmlAddPrevSibling(refp, child);
962 			if (UNEXPECTED(NULL == new_child)) {
963 				goto cannot_add;
964 			}
965 			dom_reconcile_ns(parentp->doc, new_child);
966 		}
967 	} else {
968 		if (child->parent != NULL){
969 			xmlUnlinkNode(child);
970 		}
971 		if (child->type == XML_TEXT_NODE && parentp->last != NULL && parentp->last->type == XML_TEXT_NODE) {
972 			child->parent = parentp;
973 			new_child = child;
974 			if (parentp->children == NULL) {
975 				parentp->children = child;
976 				parentp->last = child;
977 			} else {
978 				child = parentp->last;
979 				child->next = new_child;
980 				new_child->prev = child;
981 				parentp->last = new_child;
982 			}
983 		} else 	if (child->type == XML_ATTRIBUTE_NODE) {
984 			xmlAttrPtr lastattr;
985 
986 			if (child->ns == NULL)
987 				lastattr = xmlHasProp(parentp, child->name);
988 			else
989 				lastattr = xmlHasNsProp(parentp, child->name, child->ns->href);
990 			if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
991 				if (lastattr != (xmlAttrPtr) child) {
992 					xmlUnlinkNode((xmlNodePtr) lastattr);
993 					php_libxml_node_free_resource((xmlNodePtr) lastattr);
994 				} else {
995 					DOM_RET_OBJ(child, intern);
996 					return;
997 				}
998 			}
999 			new_child = xmlAddChild(parentp, child);
1000 			if (UNEXPECTED(NULL == new_child)) {
1001 				goto cannot_add;
1002 			}
1003 		} else if (child->type == XML_DOCUMENT_FRAG_NODE) {
1004 			xmlNodePtr last = child->last;
1005 			new_child = dom_insert_fragment(parentp, parentp->last, NULL, child, intern);
1006 			dom_reconcile_ns_list(parentp->doc, new_child, last);
1007 		} else {
1008 			new_child = xmlAddChild(parentp, child);
1009 			if (UNEXPECTED(NULL == new_child)) {
1010 				goto cannot_add;
1011 			}
1012 			dom_reconcile_ns(parentp->doc, new_child);
1013 		}
1014 	}
1015 
1016 	DOM_RET_OBJ(new_child, intern);
1017 	return;
1018 cannot_add:
1019 	zend_throw_error(NULL, "Cannot add newnode as the previous sibling of refnode");
1020 	RETURN_THROWS();
1021 }
1022 /* }}} end dom_node_insert_before */
1023 
1024 /* https://dom.spec.whatwg.org/#dom-node-insertbefore */
dom_node_insert_before_modern(zval * return_value,zval * ref,dom_object * intern,xmlNodePtr parentp,xmlNodePtr child)1025 static void dom_node_insert_before_modern(zval *return_value, zval *ref, dom_object *intern, xmlNodePtr parentp, xmlNodePtr child)
1026 {
1027 	xmlNodePtr refp = NULL;
1028 	dom_object *refobjp;
1029 	if (php_dom_pre_insert_is_parent_invalid(parentp)) {
1030 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1031 		RETURN_THROWS();
1032 	}
1033 	if (ref != NULL) {
1034 		DOM_GET_OBJ(refp, ref, xmlNodePtr, refobjp);
1035 	}
1036 	php_libxml_invalidate_node_list_cache(intern->document);
1037 	php_dom_pre_insert(intern->document, child, parentp, refp);
1038 	DOM_RET_OBJ(child, intern);
1039 }
1040 
dom_node_insert_before(INTERNAL_FUNCTION_PARAMETERS,bool modern)1041 static void dom_node_insert_before(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1042 {
1043 	zval *id, *node, *ref = NULL;
1044 	xmlNodePtr child, parentp;
1045 	dom_object *intern, *childobj;
1046 
1047 	id = ZEND_THIS;
1048 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|O!", &node, dom_get_node_ce(modern), &ref, dom_get_node_ce(modern)) == FAILURE) {
1049 		RETURN_THROWS();
1050 	}
1051 
1052 	DOM_GET_OBJ(parentp, id, xmlNodePtr, intern);
1053 
1054 	DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1055 
1056 	if (modern) {
1057 		dom_node_insert_before_modern(return_value, ref, intern, parentp, child);
1058 	} else {
1059 		dom_node_insert_before_legacy(return_value, ref, intern, childobj, parentp, child);
1060 	}
1061 }
1062 
PHP_METHOD(DOMNode,insertBefore)1063 PHP_METHOD(DOMNode, insertBefore)
1064 {
1065 	dom_node_insert_before(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1066 }
1067 
PHP_METHOD(Dom_Node,insertBefore)1068 PHP_METHOD(Dom_Node, insertBefore)
1069 {
1070 	dom_node_insert_before(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1071 }
1072 
1073 /* https://dom.spec.whatwg.org/#concept-node-replace */
dom_replace_node_validity_checks(xmlNodePtr parent,xmlNodePtr node,xmlNodePtr child)1074 static zend_result dom_replace_node_validity_checks(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child)
1075 {
1076 	/* 1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException. */
1077 	if (php_dom_pre_insert_is_parent_invalid(parent)) {
1078 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1079 		return FAILURE;
1080 	}
1081 
1082 	/* 2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException. */
1083 	if (dom_hierarchy(parent, node) != SUCCESS) {
1084 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1085 		return FAILURE;
1086 	}
1087 
1088 	/* 3. If child’s parent is not parent, then throw a "NotFoundError" DOMException. */
1089 	if (child->parent != parent) {
1090 		php_dom_throw_error(NOT_FOUND_ERR, /* strict */ true);
1091 		return FAILURE;
1092 	}
1093 
1094 	/* 4. If node is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException. */
1095 	if (node->type != XML_DOCUMENT_FRAG_NODE
1096 		&& node->type != XML_DTD_NODE
1097 		&& node->type != XML_ELEMENT_NODE
1098 		&& node->type != XML_TEXT_NODE
1099 		&& node->type != XML_CDATA_SECTION_NODE
1100 		&& node->type != XML_COMMENT_NODE
1101 		&& node->type != XML_PI_NODE) {
1102 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1103 		return FAILURE;
1104 	}
1105 
1106 	/* 5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document,
1107 	 *    then throw a "HierarchyRequestError" DOMException. */
1108 	bool parent_is_document = parent->type == XML_DOCUMENT_NODE || parent->type == XML_HTML_DOCUMENT_NODE;
1109 	if (parent_is_document && (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)) {
1110 		php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert text as a child of a document", /* strict */ true);
1111 		return FAILURE;
1112 	}
1113 	if (!parent_is_document && node->type == XML_DTD_NODE) {
1114 		php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert a document type into anything other than a document", /* strict */ true);
1115 		return FAILURE;
1116 	}
1117 
1118 	/* 6. If parent is a document, and any of the statements below, switched on the interface node implements, are true,
1119 	 *    then throw a "HierarchyRequestError" DOMException.
1120 	 *    Spec note: These statements _slightly_ differ from the pre-insert algorithm. */
1121 	if (parent_is_document) {
1122 		/* DocumentFragment */
1123 		if (node->type == XML_DOCUMENT_FRAG_NODE) {
1124 			if (!php_dom_fragment_insertion_hierarchy_check_replace(parent, node, child)) {
1125 				return FAILURE;
1126 			}
1127 		}
1128 		/* Element */
1129 		else if (node->type == XML_ELEMENT_NODE) {
1130 			/* parent has an element child that is not child ... */
1131 			if (xmlDocGetRootElement((xmlDocPtr) parent) != child) {
1132 				php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one element child in a document", /* strict */ true);
1133 				return FAILURE;
1134 			}
1135 			/* ... or a doctype is following child. */
1136 			if (php_dom_has_sibling_following_node(child, XML_DTD_NODE)) {
1137 				php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
1138 				return FAILURE;
1139 			}
1140 		}
1141 		/* DocumentType */
1142 		else if (node->type == XML_DTD_NODE) {
1143 			/* parent has a doctype child that is not child, or an element is preceding child. */
1144 			xmlDocPtr doc = (xmlDocPtr) parent;
1145 			if (doc->intSubset != (xmlDtdPtr) child || php_dom_has_sibling_preceding_node(child, XML_ELEMENT_NODE)) {
1146 				php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
1147 				return FAILURE;
1148 			}
1149 		}
1150 	}
1151 
1152 	/* Steps 7 and onward perform the removal and insertion, and also track changes for mutation records.
1153 	 * We don't implement mutation records so we can just skip straight to the replace part. */
1154 
1155 	return SUCCESS;
1156 }
1157 
1158 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-785887307
1159 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-replacechild
1160 Since:
1161 */
dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS,bool modern)1162 static void dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1163 {
1164 	zval *id, *newnode, *oldnode;
1165 	xmlNodePtr newchild, oldchild, nodep;
1166 	dom_object *intern, *newchildobj, *oldchildobj;
1167 
1168 	id = ZEND_THIS;
1169 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &newnode, dom_get_node_ce(modern), &oldnode, dom_get_node_ce(modern)) == FAILURE) {
1170 		RETURN_THROWS();
1171 	}
1172 
1173 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1174 
1175 	DOM_GET_OBJ(newchild, newnode, xmlNodePtr, newchildobj);
1176 	DOM_GET_OBJ(oldchild, oldnode, xmlNodePtr, oldchildobj);
1177 
1178 	bool stricterror = dom_get_strict_error(intern->document);
1179 
1180 	if (newchild->doc != nodep->doc && newchild->doc != NULL) {
1181 		php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1182 		RETURN_FALSE;
1183 	}
1184 
1185 	if (modern) {
1186 		if (dom_replace_node_validity_checks(nodep, newchild, oldchild) != SUCCESS) {
1187 			RETURN_THROWS();
1188 		}
1189 	} else {
1190 		if (!dom_node_children_valid(nodep)) {
1191 			RETURN_FALSE;
1192 		}
1193 
1194 		if (!nodep->children) {
1195 			RETURN_FALSE;
1196 		}
1197 
1198 		if (!dom_node_check_legacy_insertion_validity(nodep, newchild, stricterror, false)) {
1199 			RETURN_FALSE;
1200 		}
1201 
1202 		/* This is already disallowed by libxml, but we should check it here to avoid
1203 		 * breaking assumptions and assertions. */
1204 		if ((oldchild->type == XML_ATTRIBUTE_NODE) != (newchild->type == XML_ATTRIBUTE_NODE)) {
1205 			php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1206 			RETURN_FALSE;
1207 		}
1208 
1209 		if (oldchild->parent != nodep) {
1210 			php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1211 			RETURN_FALSE;
1212 		}
1213 	}
1214 
1215 	if (newchild->doc == NULL && nodep->doc != NULL) {
1216 		xmlSetTreeDoc(newchild, nodep->doc);
1217 		dom_set_document_ref_pointers(newchild, intern->document);
1218 	}
1219 
1220 	if (newchild->type == XML_DOCUMENT_FRAG_NODE) {
1221 		xmlNodePtr prevsib, nextsib;
1222 		prevsib = oldchild->prev;
1223 		nextsib = oldchild->next;
1224 
1225 		xmlUnlinkNode(oldchild);
1226 
1227 		xmlNodePtr last = newchild->last;
1228 		newchild = dom_insert_fragment(nodep, prevsib, nextsib, newchild, intern);
1229 		if (newchild && !modern) {
1230 			dom_reconcile_ns_list(nodep->doc, newchild, last);
1231 		}
1232 	} else if (oldchild != newchild) {
1233 		xmlDtdPtr intSubset = xmlGetIntSubset(nodep->doc);
1234 		bool replacedoctype = (intSubset == (xmlDtd *) oldchild);
1235 
1236 		xmlReplaceNode(oldchild, newchild);
1237 		if (!modern) {
1238 			dom_reconcile_ns(nodep->doc, newchild);
1239 		}
1240 
1241 		if (replacedoctype) {
1242 			nodep->doc->intSubset = (xmlDtd *) newchild;
1243 		}
1244 	}
1245 	php_libxml_invalidate_node_list_cache(intern->document);
1246 	DOM_RET_OBJ(oldchild, intern);
1247 }
1248 
PHP_METHOD(DOMNode,replaceChild)1249 PHP_METHOD(DOMNode, replaceChild)
1250 {
1251 	dom_node_replace_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1252 }
1253 
PHP_METHOD(Dom_Node,replaceChild)1254 PHP_METHOD(Dom_Node, replaceChild)
1255 {
1256 	dom_node_replace_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1257 }
1258 /* }}} end dom_node_replace_child */
1259 
1260 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1734834066
1261 Since:
1262 */
dom_node_remove_child(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * node_ce)1263 static void dom_node_remove_child(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *node_ce)
1264 {
1265 	zval *node;
1266 	xmlNodePtr child, nodep;
1267 	dom_object *intern, *childobj;
1268 
1269 	ZEND_PARSE_PARAMETERS_START(1, 1)
1270 		Z_PARAM_OBJECT_OF_CLASS(node, node_ce)
1271 	ZEND_PARSE_PARAMETERS_END();
1272 
1273 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1274 
1275 	DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1276 
1277 	bool stricterror = dom_get_strict_error(intern->document);
1278 
1279 	if (!nodep->children || child->parent != nodep) {
1280 		php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1281 		RETURN_FALSE;
1282 	}
1283 
1284 	if (dom_node_is_read_only(nodep) ||
1285 		(child->parent != NULL && dom_node_is_read_only(child->parent))) {
1286 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1287 		RETURN_FALSE;
1288 	}
1289 
1290 	xmlUnlinkNode(child);
1291 	php_libxml_invalidate_node_list_cache(intern->document);
1292 	DOM_RET_OBJ(child, intern);
1293 }
1294 
PHP_METHOD(DOMNode,removeChild)1295 PHP_METHOD(DOMNode, removeChild)
1296 {
1297 	dom_node_remove_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_node_class_entry);
1298 }
1299 
PHP_METHOD(Dom_Node,removeChild)1300 PHP_METHOD(Dom_Node, removeChild)
1301 {
1302 	dom_node_remove_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_node_class_entry);
1303 }
1304 /* }}} end dom_node_remove_child */
1305 
1306 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-184E7107
1307 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-appendchild
1308 Since:
1309 */
dom_node_append_child_legacy(zval * return_value,dom_object * intern,dom_object * childobj,xmlNodePtr nodep,xmlNodePtr child)1310 static void dom_node_append_child_legacy(zval *return_value, dom_object *intern, dom_object *childobj, xmlNodePtr nodep, xmlNodePtr child)
1311 {
1312 	xmlNodePtr new_child = NULL;
1313 
1314 	if (!dom_node_children_valid(nodep)) {
1315 		RETURN_FALSE;
1316 	}
1317 
1318 	bool stricterror = dom_get_strict_error(intern->document);
1319 
1320 	if (!dom_node_check_legacy_insertion_validity(nodep, child, stricterror, true)) {
1321 		RETURN_FALSE;
1322 	}
1323 
1324 	if (child->doc == NULL && nodep->doc != NULL) {
1325 		xmlSetTreeDoc(child, nodep->doc);
1326 		dom_set_document_ref_pointers(child, intern->document);
1327 	}
1328 
1329 	if (child->parent != NULL){
1330 		xmlUnlinkNode(child);
1331 	}
1332 
1333 	if (child->type == XML_TEXT_NODE && nodep->last != NULL && nodep->last->type == XML_TEXT_NODE) {
1334 		child->parent = nodep;
1335 		new_child = child;
1336 		if (nodep->children == NULL) {
1337 			nodep->children = child;
1338 			nodep->last = child;
1339 		} else {
1340 			child = nodep->last;
1341 			child->next = new_child;
1342 			new_child->prev = child;
1343 			nodep->last = new_child;
1344 		}
1345 	} else 	if (child->type == XML_ATTRIBUTE_NODE) {
1346 		xmlAttrPtr lastattr;
1347 
1348 		if (child->ns == NULL)
1349 			lastattr = xmlHasProp(nodep, child->name);
1350 		else
1351 			lastattr = xmlHasNsProp(nodep, child->name, child->ns->href);
1352 		if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
1353 			if (lastattr != (xmlAttrPtr) child) {
1354 				xmlUnlinkNode((xmlNodePtr) lastattr);
1355 				php_libxml_node_free_resource((xmlNodePtr) lastattr);
1356 			}
1357 		}
1358 		new_child = xmlAddChild(nodep, child);
1359 		if (UNEXPECTED(new_child == NULL)) {
1360 			goto cannot_add;
1361 		}
1362 		php_dom_reconcile_attribute_namespace_after_insertion((xmlAttrPtr) new_child);
1363 	} else if (child->type == XML_DOCUMENT_FRAG_NODE) {
1364 		xmlNodePtr last = child->last;
1365 		new_child = dom_insert_fragment(nodep, nodep->last, NULL, child, intern);
1366 		dom_reconcile_ns_list(nodep->doc, new_child, last);
1367 	} else if (child->type == XML_DTD_NODE) {
1368 		if (nodep->doc->intSubset != NULL) {
1369 			php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "A document may only contain one document type", stricterror);
1370 			RETURN_FALSE;
1371 		}
1372 		new_child = xmlAddChild(nodep, child);
1373 		if (UNEXPECTED(new_child == NULL)) {
1374 			goto cannot_add;
1375 		}
1376 		nodep->doc->intSubset = (xmlDtdPtr) new_child;
1377 	} else {
1378 		new_child = xmlAddChild(nodep, child);
1379 		if (UNEXPECTED(new_child == NULL)) {
1380 			goto cannot_add;
1381 		}
1382 		dom_reconcile_ns(nodep->doc, new_child);
1383 	}
1384 
1385 	php_libxml_invalidate_node_list_cache(intern->document);
1386 
1387 	DOM_RET_OBJ(new_child, intern);
1388 	return;
1389 cannot_add:
1390 	php_dom_throw_error(INVALID_STATE_ERR, stricterror);
1391 	RETURN_FALSE;
1392 }
1393 /* }}} end dom_node_append_child */
1394 
PHP_METHOD(DOMNode,appendChild)1395 PHP_METHOD(DOMNode, appendChild)
1396 {
1397 	zval *node;
1398 	xmlNodePtr nodep, child;
1399 	dom_object *intern, *childobj;
1400 
1401 	ZEND_PARSE_PARAMETERS_START(1, 1)
1402 		Z_PARAM_OBJECT_OF_CLASS(node, dom_node_class_entry)
1403 	ZEND_PARSE_PARAMETERS_END();
1404 
1405 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1406 	DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1407 
1408 	dom_node_append_child_legacy(return_value, intern, childobj, nodep, child);
1409 }
1410 
PHP_METHOD(Dom_Node,appendChild)1411 PHP_METHOD(Dom_Node, appendChild)
1412 {
1413 	zval *node;
1414 	xmlNodePtr nodep, child;
1415 	dom_object *intern, *childobj;
1416 
1417 	ZEND_PARSE_PARAMETERS_START(1, 1)
1418 		Z_PARAM_OBJECT_OF_CLASS(node, dom_modern_node_class_entry)
1419 	ZEND_PARSE_PARAMETERS_END();
1420 
1421 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1422 	DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1423 
1424 	/* Parent check from pre-insertion validation done here:
1425 	 * If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException. */
1426 	if (php_dom_pre_insert_is_parent_invalid(nodep)) {
1427 		php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1428 		RETURN_THROWS();
1429 	}
1430 	/* Append, this doesn't do the parent check so we do it here. */
1431 	php_libxml_invalidate_node_list_cache(intern->document);
1432 	php_dom_node_append(intern->document, child, nodep);
1433 	DOM_RET_OBJ(child, intern);
1434 }
1435 
1436 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-810594187
1437 Since:
1438 */
PHP_METHOD(DOMNode,hasChildNodes)1439 PHP_METHOD(DOMNode, hasChildNodes)
1440 {
1441 	xmlNode *nodep;
1442 	dom_object *intern;
1443 
1444 	ZEND_PARSE_PARAMETERS_NONE();
1445 
1446 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1447 
1448 	RETURN_BOOL(dom_node_children_valid(nodep) && nodep->children != NULL);
1449 }
1450 /* }}} end dom_node_has_child_nodes */
1451 
1452 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3A0ED0A4
1453 Since:
1454 */
PHP_METHOD(DOMNode,cloneNode)1455 PHP_METHOD(DOMNode, cloneNode)
1456 {
1457 	zval *id;
1458 	xmlNode *n, *node;
1459 	dom_object *intern;
1460 	bool recursive = 0;
1461 
1462 	id = ZEND_THIS;
1463 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &recursive) == FAILURE) {
1464 		RETURN_THROWS();
1465 	}
1466 
1467 	DOM_GET_OBJ(n, id, xmlNodePtr, intern);
1468 
1469 	php_dom_private_data *private_data = NULL;
1470 	bool clone_document = n->type == XML_DOCUMENT_NODE || n->type == XML_HTML_DOCUMENT_NODE;
1471 	if (php_dom_follow_spec_intern(intern)) {
1472 		if (clone_document) {
1473 			private_data = php_dom_private_data_create();
1474 		} else {
1475 			private_data = php_dom_get_private_data(intern);
1476 		}
1477 	}
1478 
1479 	node = dom_clone_node(php_dom_ns_mapper_from_private(private_data), n, n->doc, recursive);
1480 
1481 	if (!node) {
1482 		if (clone_document && private_data != NULL) {
1483 			php_dom_private_data_destroy(private_data);
1484 		}
1485 		RETURN_FALSE;
1486 	}
1487 
1488 	/* If document cloned we want a new document proxy */
1489 	if (clone_document) {
1490 		dom_object *new_intern;
1491 		if (private_data) {
1492 			/* We have the issue here that we can't create a modern node without an intern.
1493 			 * Fortunately, it's impossible to have a custom document class for the modern DOM (final base class),
1494 			 * so we can solve this by invoking the instantiation helper directly. */
1495 			zend_class_entry *ce = n->type == XML_DOCUMENT_NODE ? dom_xml_document_class_entry : dom_html_document_class_entry;
1496 			new_intern = php_dom_instantiate_object_helper(return_value, ce, node, NULL);
1497 		} else {
1498 			DOM_RET_OBJ(node, NULL);
1499 			new_intern = Z_DOMOBJ_P(return_value);
1500 		}
1501 		php_dom_update_document_after_clone(intern, n, new_intern, node);
1502 		ZEND_ASSERT(new_intern->document->private_data == NULL);
1503 		new_intern->document->private_data = php_dom_libxml_private_data_header(private_data);
1504 	} else {
1505 		if (node->type == XML_ATTRIBUTE_NODE && n->ns != NULL && node->ns == NULL) {
1506 			/* Let reconciliation deal with this. The lifetime of the namespace poses no problem
1507 			 * because we're increasing the refcount of the document proxy at the return.
1508 			 * libxml2 doesn't set the ns because it can't know that this is safe. */
1509 			node->ns = n->ns;
1510 		}
1511 
1512 		DOM_RET_OBJ(node, intern);
1513 	}
1514 }
1515 /* }}} end dom_node_clone_node */
1516 
1517 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-normalize
1518 Since:
1519 */
PHP_METHOD(DOMNode,normalize)1520 PHP_METHOD(DOMNode, normalize)
1521 {
1522 	zval *id;
1523 	xmlNode *nodep;
1524 	dom_object *intern;
1525 
1526 	id = ZEND_THIS;
1527 	ZEND_PARSE_PARAMETERS_NONE();
1528 
1529 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1530 
1531 	if (php_dom_follow_spec_intern(intern)) {
1532 		php_dom_normalize_modern(nodep);
1533 	} else {
1534 		php_dom_normalize_legacy(nodep);
1535 	}
1536 }
1537 /* }}} end dom_node_normalize */
1538 
1539 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Level-2-Core-Node-supports
1540 Since: DOM Level 2
1541 */
PHP_METHOD(DOMNode,isSupported)1542 PHP_METHOD(DOMNode, isSupported)
1543 {
1544 	zend_string *feature, *version;
1545 
1546 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &feature, &version) == FAILURE) {
1547 		RETURN_THROWS();
1548 	}
1549 
1550 	RETURN_BOOL(dom_has_feature(feature, version));
1551 }
1552 /* }}} end dom_node_is_supported */
1553 
1554 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeHasAttrs
1555 Since: DOM Level 2
1556 */
PHP_METHOD(DOMNode,hasAttributes)1557 PHP_METHOD(DOMNode, hasAttributes)
1558 {
1559 	xmlNode *nodep;
1560 	dom_object *intern;
1561 
1562 	ZEND_PARSE_PARAMETERS_NONE();
1563 
1564 	DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1565 
1566 	RETURN_BOOL(nodep->type == XML_ELEMENT_NODE && nodep->properties != NULL);
1567 }
1568 /* }}} end dom_node_has_attributes */
1569 
1570 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-isSameNode
1571 Since: DOM Level 3
1572 */
dom_node_is_same_node(INTERNAL_FUNCTION_PARAMETERS,zval * node)1573 static void dom_node_is_same_node(INTERNAL_FUNCTION_PARAMETERS, zval *node)
1574 {
1575 	zval *id;
1576 	xmlNodePtr nodeotherp, nodep;
1577 	dom_object *intern, *nodeotherobj;
1578 
1579 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1580 
1581 	DOM_GET_OBJ(nodeotherp, node, xmlNodePtr, nodeotherobj);
1582 
1583 	if (nodep == nodeotherp) {
1584 		RETURN_TRUE;
1585 	} else {
1586 		RETURN_FALSE;
1587 	}
1588 }
1589 
PHP_METHOD(DOMNode,isSameNode)1590 PHP_METHOD(DOMNode, isSameNode)
1591 {
1592 	zval *node;
1593 	ZEND_PARSE_PARAMETERS_START(1, 1)
1594 		Z_PARAM_OBJECT_OF_CLASS(node, dom_node_class_entry)
1595 	ZEND_PARSE_PARAMETERS_END();
1596 
1597 	dom_node_is_same_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, node);
1598 }
1599 
PHP_METHOD(Dom_Node,isSameNode)1600 PHP_METHOD(Dom_Node, isSameNode)
1601 {
1602 	zval *node;
1603 	ZEND_PARSE_PARAMETERS_START(1, 1)
1604 		Z_PARAM_OBJECT_OF_CLASS_OR_NULL(node, dom_modern_node_class_entry)
1605 	ZEND_PARSE_PARAMETERS_END();
1606 
1607 	if (node == NULL) {
1608 		RETURN_FALSE;
1609 	}
1610 
1611 	dom_node_is_same_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, node);
1612 }
1613 /* }}} end dom_node_is_same_node */
1614 
php_dom_node_is_content_equal(const xmlNode * this,const xmlNode * other)1615 static bool php_dom_node_is_content_equal(const xmlNode *this, const xmlNode *other)
1616 {
1617 	xmlChar *this_content = xmlNodeGetContent(this);
1618 	xmlChar *other_content = xmlNodeGetContent(other);
1619 	bool result = xmlStrEqual(this_content, other_content);
1620 	xmlFree(this_content);
1621 	xmlFree(other_content);
1622 	return result;
1623 }
1624 
php_dom_node_is_ns_uri_equal(const xmlNode * this,const xmlNode * other)1625 static bool php_dom_node_is_ns_uri_equal(const xmlNode *this, const xmlNode *other)
1626 {
1627 	const xmlChar *this_ns = this->ns ? this->ns->href : NULL;
1628 	const xmlChar *other_ns = other->ns ? other->ns->href : NULL;
1629 	return xmlStrEqual(this_ns, other_ns);
1630 }
1631 
php_dom_node_is_ns_prefix_equal(const xmlNode * this,const xmlNode * other)1632 static bool php_dom_node_is_ns_prefix_equal(const xmlNode *this, const xmlNode *other)
1633 {
1634 	const xmlChar *this_ns = this->ns ? this->ns->prefix : NULL;
1635 	const xmlChar *other_ns = other->ns ? other->ns->prefix : NULL;
1636 	return xmlStrEqual(this_ns, other_ns);
1637 }
1638 
1639 static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other, bool spec_compliant);
1640 
1641 #define PHP_DOM_FUNC_CAT(prefix, suffix) prefix##_##suffix
1642 /* xmlNode and xmlNs have incompatible struct layouts, i.e. the next field is in a different offset */
1643 #define PHP_DOM_DEFINE_LIST_COUNTER_HELPER(type)																							\
1644 	static size_t PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(const type *node)													\
1645 	{																																		\
1646 		size_t counter = 0;																													\
1647 		while (node) {																														\
1648 			counter++;																														\
1649 			node = node->next;																												\
1650 		}																																	\
1651 		return counter;																														\
1652 	}
1653 #define PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(type)																					\
1654 	static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_ordered, type)(const type *list1, const type *list2, bool spec_compliant)	\
1655 	{																																		\
1656 		size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1);															\
1657 		if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) {															\
1658 			return false;																													\
1659 		}																																	\
1660 		for (size_t i = 0; i < count; i++) {																								\
1661 			if (!php_dom_node_is_equal_node((const xmlNode *) list1, (const xmlNode *) list2, spec_compliant)) {							\
1662 				return false;																												\
1663 			}																																\
1664 			list1 = list1->next;																											\
1665 			list2 = list2->next;																											\
1666 		}																																	\
1667 		return true;																														\
1668 	}
1669 #define PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(type)																					\
1670 	static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_unordered, type)(const type *list1, const type *list2, bool spec_compliant)\
1671 	{																																		\
1672 		size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1);															\
1673 		if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) {															\
1674 			return false;																													\
1675 		}																																	\
1676 		for (const type *n1 = list1; n1 != NULL; n1 = n1->next) {																			\
1677 			bool found = false;																												\
1678 			for (const type *n2 = list2; n2 != NULL && !found; n2 = n2->next) {																\
1679 				if (php_dom_node_is_equal_node((const xmlNode *) n1, (const xmlNode *) n2, spec_compliant)) {								\
1680 					found = true;																											\
1681 				}																															\
1682 			}																																\
1683 			if (!found) {																													\
1684 				return false;																												\
1685 			}																																\
1686 		}																																	\
1687 		return true;																														\
1688 	}
1689 
1690 PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNode)
PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNs)1691 PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNs)
1692 PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(xmlNode)
1693 PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNode)
1694 PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNs)
1695 
1696 static bool php_dom_is_equal_attr(const xmlAttr *this_attr, const xmlAttr *other_attr)
1697 {
1698 	ZEND_ASSERT(this_attr != NULL);
1699 	ZEND_ASSERT(other_attr != NULL);
1700 	return xmlStrEqual(this_attr->name, other_attr->name)
1701 		&& php_dom_node_is_ns_uri_equal((const xmlNode *) this_attr, (const xmlNode *) other_attr)
1702 		&& php_dom_node_is_content_equal((const xmlNode *) this_attr, (const xmlNode *) other_attr);
1703 }
1704 
php_dom_node_is_equal_node(const xmlNode * this,const xmlNode * other,bool spec_compliant)1705 static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other, bool spec_compliant)
1706 {
1707 	ZEND_ASSERT(this != NULL);
1708 	ZEND_ASSERT(other != NULL);
1709 
1710 	if (this->type != other->type) {
1711 		return false;
1712 	}
1713 
1714 	/* Notes:
1715 	 *   - XML_DOCUMENT_TYPE_NODE is no longer created by libxml2, we only have to support XML_DTD_NODE.
1716 	 *   - element and attribute declarations are not exposed as nodes in DOM, so no comparison is needed for those. */
1717 	if (this->type == XML_ELEMENT_NODE) {
1718 		return xmlStrEqual(this->name, other->name)
1719 			&& php_dom_node_is_ns_prefix_equal(this, other)
1720 			&& php_dom_node_is_ns_uri_equal(this, other)
1721 			/* Check attributes first, then namespace declarations, then children */
1722 			&& php_dom_node_list_equality_check_unordered_xmlNode((const xmlNode *) this->properties, (const xmlNode *) other->properties, spec_compliant)
1723 			&& (spec_compliant || php_dom_node_list_equality_check_unordered_xmlNs(this->nsDef, other->nsDef, false))
1724 			&& php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children, spec_compliant);
1725 	} else if (this->type == XML_DTD_NODE) {
1726 		/* Note: in the living spec entity declarations and notations are no longer compared because they're considered obsolete. */
1727 		const xmlDtd *this_dtd = (const xmlDtd *) this;
1728 		const xmlDtd *other_dtd = (const xmlDtd *) other;
1729 		return xmlStrEqual(this_dtd->name, other_dtd->name)
1730 			&& xmlStrEqual(this_dtd->ExternalID, other_dtd->ExternalID)
1731 			&& xmlStrEqual(this_dtd->SystemID, other_dtd->SystemID);
1732 	} else if (this->type == XML_PI_NODE) {
1733 		return xmlStrEqual(this->name, other->name) && xmlStrEqual(this->content, other->content);
1734 	} else if (this->type == XML_TEXT_NODE || this->type == XML_COMMENT_NODE || this->type == XML_CDATA_SECTION_NODE) {
1735 		return xmlStrEqual(this->content, other->content);
1736 	} else if (this->type == XML_ATTRIBUTE_NODE) {
1737 		const xmlAttr *this_attr = (const xmlAttr *) this;
1738 		const xmlAttr *other_attr = (const xmlAttr *) other;
1739 		return php_dom_is_equal_attr(this_attr, other_attr);
1740 	} else if (this->type == XML_ENTITY_REF_NODE) {
1741 		return xmlStrEqual(this->name, other->name);
1742 	} else if (this->type == XML_ENTITY_DECL || this->type == XML_NOTATION_NODE || this->type == XML_ENTITY_NODE) {
1743 		const xmlEntity *this_entity = (const xmlEntity *) this;
1744 		const xmlEntity *other_entity = (const xmlEntity *) other;
1745 		return this_entity->etype == other_entity->etype
1746 			&& xmlStrEqual(this_entity->name, other_entity->name)
1747 			&& xmlStrEqual(this_entity->ExternalID, other_entity->ExternalID)
1748 			&& xmlStrEqual(this_entity->SystemID, other_entity->SystemID)
1749 			&& php_dom_node_is_content_equal(this, other);
1750 	} else if (this->type == XML_NAMESPACE_DECL) {
1751 		const xmlNs *this_ns = (const xmlNs *) this;
1752 		const xmlNs *other_ns = (const xmlNs *) other;
1753 		return xmlStrEqual(this_ns->prefix, other_ns->prefix) && xmlStrEqual(this_ns->href, other_ns->href);
1754 	} else if (this->type == XML_DOCUMENT_FRAG_NODE || this->type == XML_HTML_DOCUMENT_NODE || this->type == XML_DOCUMENT_NODE) {
1755 		return php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children, spec_compliant);
1756 	}
1757 
1758 	return false;
1759 }
1760 
1761 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-isequalnode (for everything still in the living spec)
1762 *      URL: https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-isEqualNode (for old nodes removed from the living spec)
1763 Since: DOM Level 3
1764 */
dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS,bool modern)1765 static void dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1766 {
1767 	zval *id, *node;
1768 	xmlNodePtr otherp, nodep;
1769 	dom_object *intern;
1770 
1771 	id = ZEND_THIS;
1772 	ZEND_PARSE_PARAMETERS_START(1, 1)
1773 		Z_PARAM_OBJECT_OF_CLASS_OR_NULL(node, dom_get_node_ce(modern))
1774 	ZEND_PARSE_PARAMETERS_END();
1775 
1776 	if (node == NULL) {
1777 		RETURN_FALSE;
1778 	}
1779 
1780 	DOM_GET_OBJ(otherp, node, xmlNodePtr, intern);
1781 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1782 
1783 	if (nodep == otherp) {
1784 		RETURN_TRUE;
1785 	}
1786 
1787 	/* Empty fragments/documents only match if they're both empty */
1788 	if (nodep == NULL || otherp == NULL) {
1789 		RETURN_BOOL(nodep == NULL && otherp == NULL);
1790 	}
1791 
1792 	RETURN_BOOL(php_dom_node_is_equal_node(nodep, otherp, modern));
1793 }
1794 
PHP_METHOD(DOMNode,isEqualNode)1795 PHP_METHOD(DOMNode, isEqualNode)
1796 {
1797 	dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1798 }
1799 
PHP_METHOD(Dom_Node,isEqualNode)1800 PHP_METHOD(Dom_Node, isEqualNode)
1801 {
1802 	dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1803 }
1804 /* }}} end DOMNode::isEqualNode */
1805 
1806 /* https://dom.spec.whatwg.org/#locate-a-namespace-prefix */
dom_locate_a_namespace_prefix(xmlNodePtr elem,const char * uri)1807 static const xmlChar *dom_locate_a_namespace_prefix(xmlNodePtr elem, const char *uri)
1808 {
1809 	do {
1810 		/* 1. If element’s namespace is namespace and its namespace prefix is non-null, then return its namespace prefix. */
1811 		if (elem->ns != NULL && elem->ns->prefix != NULL && xmlStrEqual(elem->ns->href, BAD_CAST uri)) {
1812 			return elem->ns->prefix;
1813 		}
1814 
1815 		/* 2. If element has an attribute whose namespace prefix is "xmlns" and value is namespace,
1816 		*     then return element’s first such attribute’s local name. */
1817 		for (xmlAttrPtr attr = elem->properties; attr != NULL; attr = attr->next) {
1818 			if (attr->ns != NULL && attr->children != NULL
1819 				&& xmlStrEqual(attr->ns->prefix, BAD_CAST "xmlns") && xmlStrEqual(attr->children->content, BAD_CAST uri)) {
1820 				return attr->name;
1821 			}
1822 		}
1823 
1824 		/* 3. If element’s parent element is not null, then return the result of running locate a namespace prefix on that element using namespace. */
1825 		elem = elem->parent;
1826 	} while (elem != NULL && elem->type == XML_ELEMENT_NODE);
1827 
1828 	/* 4. Return null. */
1829 	return NULL;
1830 }
1831 
1832 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-lookupNamespacePrefix
1833 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-lookupprefix
1834 Since: DOM Level 3
1835 */
dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAMETERS,bool modern)1836 static void dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1837 {
1838 	zval *id;
1839 	xmlNodePtr nodep, lookupp = NULL;
1840 	dom_object *intern;
1841 	xmlNsPtr nsptr;
1842 	size_t uri_len = 0;
1843 	char *uri;
1844 
1845 	id = ZEND_THIS;
1846 	if (zend_parse_parameters(ZEND_NUM_ARGS(), modern ? "s!" : "s", &uri, &uri_len) == FAILURE) {
1847 		RETURN_THROWS();
1848 	}
1849 
1850 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1851 
1852 	/* 1. If namespace is null or the empty string, then return null. */
1853 	if (uri_len > 0) {
1854 		/* 2. Switch on the interface this implements: */
1855 		switch (nodep->type) {
1856 			case XML_ELEMENT_NODE:
1857 				lookupp = nodep;
1858 				break;
1859 			case XML_DOCUMENT_NODE:
1860 			case XML_HTML_DOCUMENT_NODE:
1861 				lookupp = xmlDocGetRootElement((xmlDocPtr) nodep);
1862 				break;
1863 			case XML_ENTITY_NODE :
1864 			case XML_NOTATION_NODE:
1865 			case XML_DOCUMENT_FRAG_NODE:
1866 			case XML_DOCUMENT_TYPE_NODE:
1867 			case XML_DTD_NODE:
1868 				RETURN_NULL();
1869 				break;
1870 			default:
1871 				lookupp = nodep->parent;
1872 		}
1873 
1874 		if (lookupp != NULL) {
1875 			if (modern) {
1876 				const char * result = (const char *) dom_locate_a_namespace_prefix(lookupp, uri);
1877 				if (result != NULL) {
1878 					RETURN_STRING(result);
1879 				}
1880 			} else {
1881 				nsptr = xmlSearchNsByHref(lookupp->doc, lookupp, BAD_CAST uri);
1882 				if (nsptr && nsptr->prefix != NULL) {
1883 					RETURN_STRING((const char *) nsptr->prefix);
1884 				}
1885 			}
1886 		}
1887 	}
1888 
1889 	RETURN_NULL();
1890 }
1891 
PHP_METHOD(DOMNode,lookupPrefix)1892 PHP_METHOD(DOMNode, lookupPrefix)
1893 {
1894 	dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1895 }
1896 
PHP_METHOD(Dom_Node,lookupPrefix)1897 PHP_METHOD(Dom_Node, lookupPrefix)
1898 {
1899 	dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1900 }
1901 /* }}} end dom_node_lookup_prefix */
1902 
1903 /* https://dom.spec.whatwg.org/#locate-a-namespace */
dom_locate_a_namespace(const xmlNode * node,const zend_string * prefix)1904 const char *dom_locate_a_namespace(const xmlNode *node, const zend_string *prefix)
1905 {
1906 	/* switch on the interface node implements: */
1907 	if (node->type == XML_ELEMENT_NODE) {
1908 		if (prefix != NULL) {
1909 			/* 1. If prefix is "xml", then return the XML namespace. */
1910 			if (zend_string_equals_literal_ci(prefix, "xml")) {
1911 				return DOM_XML_NS_URI;
1912 			}
1913 
1914 			/* 2. If prefix is "xmlns", then return the XMLNS namespace. */
1915 			if (zend_string_equals_literal_ci(prefix, "xmlns")) {
1916 				return DOM_XMLNS_NS_URI;
1917 			}
1918 		}
1919 
1920 		do {
1921 			/* 3. If its namespace is non-null and its namespace prefix is prefix, then return namespace. */
1922 			if (node->ns != NULL && xmlStrEqual(node->ns->prefix, BAD_CAST (prefix ? ZSTR_VAL(prefix) : NULL))) {
1923 				return (const char *) node->ns->href;
1924 			}
1925 
1926 			/* 4. If it has an attribute whose namespace is the XMLNS namespace, namespace prefix is "xmlns", and local name is prefix,
1927 			*     or if prefix is null and it has an attribute whose namespace is the XMLNS namespace, namespace prefix is null, and local name is "xmlns",
1928 			*     then return its value if it is not the empty string, and null otherwise. */
1929 			for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
1930 				if (attr->ns == NULL || !php_dom_ns_is_fast_ex(attr->ns, php_dom_ns_is_xmlns_magic_token)) {
1931 					continue;
1932 				}
1933 				if ((prefix != NULL && xmlStrEqual(attr->ns->prefix, BAD_CAST "xmlns") && xmlStrEqual(attr->name, BAD_CAST ZSTR_VAL(prefix)))
1934 					|| (prefix == NULL && attr->ns->prefix == NULL && xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
1935 					if (attr->children != NULL && attr->children->content[0] != '\0') {
1936 						return (const char *) attr->children->content;
1937 					} else {
1938 						return NULL;
1939 					}
1940 				}
1941 			}
1942 
1943 			/* 5. If its parent element is null, then return null. */
1944 			if (node->parent == NULL || node->parent->type != XML_ELEMENT_NODE) {
1945 				return NULL;
1946 			}
1947 
1948 			/* 6. Return the result of running locate a namespace on its parent element using prefix. */
1949 			node = node->parent;
1950 		} while (true);
1951 	} else if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
1952 		/* 1. If its document element is null, then return null. */
1953 		node = xmlDocGetRootElement((xmlDocPtr) node);
1954 		if (UNEXPECTED(node == NULL)) {
1955 			return NULL;
1956 		}
1957 
1958 		/* 2. Return the result of running locate a namespace on its document element using prefix. */
1959 		return dom_locate_a_namespace(node, prefix);
1960 	} else if (node->type == XML_DTD_NODE || node->type == XML_DOCUMENT_FRAG_NODE) {
1961 		return NULL;
1962 	} else {
1963 		/* 1. If its element is null, then return null / If its parent element is null, then return null. */
1964 		if (node->parent == NULL || node->parent->type != XML_ELEMENT_NODE) {
1965 			return NULL;
1966 		}
1967 
1968 		/* 2. Return the result of running locate a namespace on its element using prefix. */
1969 		return dom_locate_a_namespace(node->parent, prefix);
1970 	}
1971 }
1972 
1973 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isDefaultNamespace
1974 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-isdefaultnamespace
1975 Since: DOM Level 3
1976 */
PHP_METHOD(DOMNode,isDefaultNamespace)1977 PHP_METHOD(DOMNode, isDefaultNamespace)
1978 {
1979 	zval *id;
1980 	xmlNodePtr nodep;
1981 	dom_object *intern;
1982 	xmlNsPtr nsptr;
1983 	size_t uri_len = 0;
1984 	char *uri;
1985 
1986 	ZEND_PARSE_PARAMETERS_START(1, 1)
1987 		Z_PARAM_STRING(uri, uri_len)
1988 	ZEND_PARSE_PARAMETERS_END();
1989 
1990 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1991 
1992 	if (uri_len > 0) {
1993 		if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
1994 			nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
1995 			if (nodep == NULL) {
1996 				RETURN_FALSE;
1997 			}
1998 		}
1999 
2000 		nsptr = xmlSearchNs(nodep->doc, nodep, NULL);
2001 		if (nsptr && xmlStrEqual(nsptr->href, BAD_CAST uri)) {
2002 			RETURN_TRUE;
2003 		}
2004 	}
2005 
2006 	RETURN_FALSE;
2007 }
2008 
PHP_METHOD(Dom_Node,isDefaultNamespace)2009 PHP_METHOD(Dom_Node, isDefaultNamespace)
2010 {
2011 	zval *id;
2012 	xmlNodePtr nodep;
2013 	dom_object *intern;
2014 	size_t uri_len = 0;
2015 	char *uri;
2016 
2017 	ZEND_PARSE_PARAMETERS_START(1, 1)
2018 		Z_PARAM_STRING_OR_NULL(uri, uri_len)
2019 	ZEND_PARSE_PARAMETERS_END();
2020 
2021 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2022 
2023 	if (uri_len == 0) {
2024 		uri = NULL;
2025 	}
2026 	const char *ns_uri = dom_locate_a_namespace(nodep, NULL);
2027 	RETURN_BOOL(xmlStrEqual(BAD_CAST uri, BAD_CAST ns_uri));
2028 }
2029 /* }}} end dom_node_is_default_namespace */
2030 
2031 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI
2032 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-lookupnamespaceuri
2033 Since: DOM Level 3
2034 */
PHP_METHOD(DOMNode,lookupNamespaceURI)2035 PHP_METHOD(DOMNode, lookupNamespaceURI)
2036 {
2037 	zval *id;
2038 	xmlNodePtr nodep;
2039 	dom_object *intern;
2040 	xmlNsPtr nsptr;
2041 	zend_string *prefix;
2042 
2043 	id = ZEND_THIS;
2044 	ZEND_PARSE_PARAMETERS_START(1, 1)
2045 		Z_PARAM_STR_OR_NULL(prefix)
2046 	ZEND_PARSE_PARAMETERS_END();
2047 
2048 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
2049 
2050 	if (php_dom_follow_spec_intern(intern)) {
2051 		if (prefix != NULL && ZSTR_LEN(prefix) == 0) {
2052 			prefix = NULL;
2053 		}
2054 		const char *ns_uri = dom_locate_a_namespace(nodep, prefix);
2055 		if (ns_uri == NULL) {
2056 			RETURN_NULL();
2057 		} else {
2058 			RETURN_STRING(ns_uri);
2059 		}
2060 	} else {
2061 		if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
2062 			nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
2063 			if (nodep == NULL) {
2064 				RETURN_NULL();
2065 			}
2066 		}
2067 
2068 		nsptr = xmlSearchNs(nodep->doc, nodep, BAD_CAST (prefix ? ZSTR_VAL(prefix) : NULL));
2069 		if (nsptr && nsptr->href != NULL) {
2070 			RETURN_STRING((char *) nsptr->href);
2071 		}
2072 	}
2073 
2074 	RETURN_NULL();
2075 }
2076 /* }}} end dom_node_lookup_namespace_uri */
2077 
dom_canonicalize_node_parent_lookup_cb(void * user_data,xmlNodePtr node,xmlNodePtr parent)2078 static int dom_canonicalize_node_parent_lookup_cb(void *user_data, xmlNodePtr node, xmlNodePtr parent)
2079 {
2080 	xmlNodePtr root = user_data;
2081 	/* We have to unroll the first iteration because node->parent
2082 	 * is not necessarily equal to parent due to libxml2 tree rules (ns decls out of the tree for example). */
2083 	if (node == root) {
2084 		return 1;
2085 	}
2086 	node = parent;
2087 	while (node != NULL) {
2088 		if (node == root) {
2089 			return 1;
2090 		}
2091 		node = node->parent;
2092 	}
2093 
2094 	return 0;
2095 }
2096 
dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS,int mode)2097 static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
2098 {
2099 	zval *id;
2100 	zval *xpath_array=NULL, *ns_prefixes=NULL;
2101 	xmlNodePtr nodep;
2102 	xmlDocPtr docp;
2103 	xmlNodeSetPtr nodeset = NULL;
2104 	dom_object *intern;
2105 	bool exclusive=0, with_comments=0;
2106 	xmlChar **inclusive_ns_prefixes = NULL;
2107 	char *file = NULL;
2108 	int ret = -1;
2109 	size_t file_len = 0;
2110 	xmlOutputBufferPtr buf;
2111 	xmlXPathContextPtr ctxp=NULL;
2112 	xmlXPathObjectPtr xpathobjp=NULL;
2113 
2114 	id = ZEND_THIS;
2115 	if (mode == 0) {
2116 		if (zend_parse_parameters(ZEND_NUM_ARGS(),
2117 			"|bba!a!", &exclusive, &with_comments,
2118 			&xpath_array, &ns_prefixes) == FAILURE) {
2119 			RETURN_THROWS();
2120 		}
2121 	} else {
2122 		if (zend_parse_parameters(ZEND_NUM_ARGS(),
2123 			"s|bba!a!", &file, &file_len, &exclusive,
2124 			&with_comments, &xpath_array, &ns_prefixes) == FAILURE) {
2125 			RETURN_THROWS();
2126 		}
2127 	}
2128 
2129 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
2130 
2131 	docp = nodep->doc;
2132 
2133 	if (! docp) {
2134 		zend_throw_error(NULL, "Node must be associated with a document");
2135 		RETURN_THROWS();
2136 	}
2137 
2138 	bool simple_node_parent_lookup_callback = false;
2139 	if (xpath_array == NULL) {
2140 		/* Optimization: if the node is a document, all nodes may be included, no extra filtering or nodeset necessary. */
2141 		if (nodep->type != XML_DOCUMENT_NODE && nodep->type != XML_HTML_DOCUMENT_NODE) {
2142 			simple_node_parent_lookup_callback = true;
2143 		}
2144 	} else {
2145 		/*xpath query from xpath_array */
2146 		HashTable *ht = Z_ARRVAL_P(xpath_array);
2147 		zval *tmp;
2148 		char *xquery;
2149 
2150 		/* Find "query" key */
2151 		tmp = zend_hash_find_deref(ht, ZSTR_KNOWN(ZEND_STR_QUERY));
2152 		if (!tmp) {
2153 			/* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
2154 			zend_argument_value_error(3 + mode, "must have a \"query\" key");
2155 			RETURN_THROWS();
2156 		}
2157 		if (Z_TYPE_P(tmp) != IS_STRING) {
2158 			/* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
2159 			zend_argument_type_error(3 + mode, "\"query\" option must be a string, %s given", zend_zval_value_name(tmp));
2160 			RETURN_THROWS();
2161 		}
2162 		xquery = Z_STRVAL_P(tmp);
2163 
2164 		ctxp = xmlXPathNewContext(docp);
2165 		ctxp->node = nodep;
2166 
2167 		tmp = zend_hash_str_find_deref(ht, "namespaces", sizeof("namespaces")-1);
2168 		if (tmp && Z_TYPE_P(tmp) == IS_ARRAY && !HT_IS_PACKED(Z_ARRVAL_P(tmp))) {
2169 			zval *tmpns;
2170 			zend_string *prefix;
2171 
2172 			ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), prefix, tmpns) {
2173 				ZVAL_DEREF(tmpns);
2174 				if (Z_TYPE_P(tmpns) == IS_STRING) {
2175 					if (prefix) {
2176 						xmlXPathRegisterNs(ctxp, BAD_CAST ZSTR_VAL(prefix), BAD_CAST Z_STRVAL_P(tmpns));
2177 					}
2178 				}
2179 			} ZEND_HASH_FOREACH_END();
2180 		}
2181 
2182 		xpathobjp = xmlXPathEvalExpression(BAD_CAST xquery, ctxp);
2183 		ctxp->node = NULL;
2184 		if (xpathobjp && xpathobjp->type == XPATH_NODESET) {
2185 			nodeset = xpathobjp->nodesetval;
2186 		} else {
2187 			if (xpathobjp) {
2188 				xmlXPathFreeObject(xpathobjp);
2189 			}
2190 			xmlXPathFreeContext(ctxp);
2191 			zend_throw_error(NULL, "XPath query did not return a nodeset");
2192 			RETURN_THROWS();
2193 		}
2194 	}
2195 
2196 	if (ns_prefixes != NULL) {
2197 		if (exclusive) {
2198 			zval *tmpns;
2199 			int nscount = 0;
2200 
2201 			inclusive_ns_prefixes = safe_emalloc(zend_hash_num_elements(Z_ARRVAL_P(ns_prefixes)) + 1,
2202 				sizeof(xmlChar *), 0);
2203 			ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(ns_prefixes), tmpns) {
2204 				ZVAL_DEREF(tmpns);
2205 				if (Z_TYPE_P(tmpns) == IS_STRING) {
2206 					inclusive_ns_prefixes[nscount++] = BAD_CAST Z_STRVAL_P(tmpns);
2207 				}
2208 			} ZEND_HASH_FOREACH_END();
2209 			inclusive_ns_prefixes[nscount] = NULL;
2210 		} else {
2211 			php_error_docref(NULL, E_NOTICE,
2212 				"Inclusive namespace prefixes only allowed in exclusive mode.");
2213 		}
2214 	}
2215 
2216 	if (mode == 1) {
2217 		buf = xmlOutputBufferCreateFilename(file, NULL, 0);
2218 	} else {
2219 		buf = xmlAllocOutputBuffer(NULL);
2220 	}
2221 
2222 	if (buf != NULL) {
2223 		if (simple_node_parent_lookup_callback) {
2224 			ret = xmlC14NExecute(docp, dom_canonicalize_node_parent_lookup_cb, nodep, exclusive, inclusive_ns_prefixes, with_comments, buf);
2225 		} else {
2226 			ret = xmlC14NDocSaveTo(docp, nodeset, exclusive, inclusive_ns_prefixes, with_comments, buf);
2227 		}
2228 	}
2229 
2230 	if (inclusive_ns_prefixes != NULL) {
2231 		efree(inclusive_ns_prefixes);
2232 	}
2233 	if (xpathobjp != NULL) {
2234 		xmlXPathFreeObject(xpathobjp);
2235 	}
2236 	if (ctxp != NULL) {
2237 		xmlXPathFreeContext(ctxp);
2238 	}
2239 
2240 	if (buf == NULL || ret < 0) {
2241 		RETVAL_FALSE;
2242 	} else {
2243 		if (mode == 0) {
2244 			size_t size = xmlOutputBufferGetSize(buf);
2245 			if (size > 0) {
2246 				RETVAL_STRINGL((char *) xmlOutputBufferGetContent(buf), size);
2247 			} else {
2248 				RETVAL_EMPTY_STRING();
2249 			}
2250 		}
2251 	}
2252 
2253 	if (buf) {
2254 		int bytes;
2255 
2256 		bytes = xmlOutputBufferClose(buf);
2257 		if (mode == 1 && (ret >= 0)) {
2258 			RETURN_LONG(bytes);
2259 		}
2260 	}
2261 }
2262 /* }}} */
2263 
2264 /* {{{ Canonicalize nodes to a string */
PHP_METHOD(DOMNode,C14N)2265 PHP_METHOD(DOMNode, C14N)
2266 {
2267 	dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2268 }
2269 /* }}} */
2270 
2271 /* {{{ Canonicalize nodes to a file */
PHP_METHOD(DOMNode,C14NFile)2272 PHP_METHOD(DOMNode, C14NFile)
2273 {
2274 	dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2275 }
2276 /* }}} */
2277 
2278 /* {{{ Gets an xpath for a node */
dom_node_get_node_path(INTERNAL_FUNCTION_PARAMETERS,bool throw)2279 static void dom_node_get_node_path(INTERNAL_FUNCTION_PARAMETERS, bool throw)
2280 {
2281 	zval *id;
2282 	xmlNode *nodep;
2283 	dom_object *intern;
2284 	char *value;
2285 
2286 	ZEND_PARSE_PARAMETERS_NONE();
2287 
2288 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2289 
2290 	value = (char *) xmlGetNodePath(nodep);
2291 	if (value == NULL) {
2292 		/* This is only possible when an invalid argument is passed (e.g. namespace declaration, but that's not the case for this call site),
2293 		 * or on allocation failure. So in other words, this only happens on allocation failure. */
2294 		if (throw) {
2295 			php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
2296 			RETURN_THROWS();
2297 		}
2298 		RETURN_NULL();
2299 	} else {
2300 		RETVAL_STRING(value);
2301 		xmlFree(value);
2302 	}
2303 }
2304 
PHP_METHOD(DOMNode,getNodePath)2305 PHP_METHOD(DOMNode, getNodePath)
2306 {
2307 	dom_node_get_node_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
2308 }
2309 
PHP_METHOD(Dom_Node,getNodePath)2310 PHP_METHOD(Dom_Node, getNodePath)
2311 {
2312 	dom_node_get_node_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
2313 }
2314 /* }}} */
2315 
2316 /* {{{ Gets line number for a node */
PHP_METHOD(DOMNode,getLineNo)2317 PHP_METHOD(DOMNode, getLineNo)
2318 {
2319 	zval *id;
2320 	xmlNode *nodep;
2321 	dom_object *intern;
2322 
2323 	ZEND_PARSE_PARAMETERS_NONE();
2324 
2325 	DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2326 
2327 	RETURN_LONG(xmlGetLineNo(nodep));
2328 }
2329 /* }}} */
2330 
2331 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-contains
2332 Since:
2333 */
dom_node_contains(xmlNodePtr thisp,xmlNodePtr otherp)2334 static bool dom_node_contains(xmlNodePtr thisp, xmlNodePtr otherp)
2335 {
2336 	do {
2337 		if (otherp == thisp) {
2338 			return true;
2339 		}
2340 		otherp = otherp->parent;
2341 	} while (otherp);
2342 
2343 	return false;
2344 }
2345 
PHP_METHOD(DOMNode,contains)2346 PHP_METHOD(DOMNode, contains)
2347 {
2348 	zval *other, *id;
2349 	xmlNodePtr otherp, thisp;
2350 	dom_object *unused_intern;
2351 
2352 	ZEND_PARSE_PARAMETERS_START(1, 1)
2353 		Z_PARAM_OBJECT_OR_NULL(other)
2354 	ZEND_PARSE_PARAMETERS_END();
2355 
2356 	if (other == NULL) {
2357 		RETURN_FALSE;
2358 	}
2359 
2360 	if (UNEXPECTED(!instanceof_function(Z_OBJCE_P(other), dom_node_class_entry) && !instanceof_function(Z_OBJCE_P(other), dom_namespace_node_class_entry))) {
2361 		zend_argument_type_error(1, "must be of type DOMNode|DOMNameSpaceNode|null, %s given", zend_zval_value_name(other));
2362 		RETURN_THROWS();
2363 	}
2364 
2365 	DOM_GET_OBJ(otherp, other, xmlNodePtr, unused_intern);
2366 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, unused_intern);
2367 
2368 	RETURN_BOOL(dom_node_contains(thisp, otherp));
2369 }
2370 
PHP_METHOD(Dom_Node,contains)2371 PHP_METHOD(Dom_Node, contains)
2372 {
2373 	zval *other, *id;
2374 	xmlNodePtr otherp, thisp;
2375 	dom_object *unused_intern;
2376 
2377 	ZEND_PARSE_PARAMETERS_START(1, 1)
2378 		Z_PARAM_OBJECT_OF_CLASS_OR_NULL(other, dom_modern_node_class_entry)
2379 	ZEND_PARSE_PARAMETERS_END();
2380 
2381 	if (other == NULL) {
2382 		RETURN_FALSE;
2383 	}
2384 
2385 	DOM_GET_OBJ(otherp, other, xmlNodePtr, unused_intern);
2386 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, unused_intern);
2387 
2388 	RETURN_BOOL(dom_node_contains(thisp, otherp));
2389 }
2390 /* }}} */
2391 
2392 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-getrootnode
2393 Since:
2394 */
PHP_METHOD(DOMNode,getRootNode)2395 PHP_METHOD(DOMNode, getRootNode)
2396 {
2397 	zval *id;
2398 	xmlNodePtr thisp;
2399 	dom_object *intern;
2400 	/* Unused now because we don't support the shadow DOM nodes. Options only influence shadow DOM nodes. */
2401 	zval *options;
2402 
2403 	ZEND_PARSE_PARAMETERS_START(0, 1)
2404 		Z_PARAM_OPTIONAL
2405 		Z_PARAM_ARRAY_OR_NULL(options)
2406 	ZEND_PARSE_PARAMETERS_END();
2407 
2408 	DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
2409 
2410 	while (thisp->parent) {
2411 		thisp = thisp->parent;
2412 	}
2413 
2414 	DOM_RET_OBJ(thisp, intern);
2415 }
2416 /* }}} */
2417 
2418 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-comparedocumentposition (last check date 2024-11-17)
2419 Since:
2420 */
2421 
2422 #define DOCUMENT_POSITION_DISCONNECTED 0x01
2423 #define DOCUMENT_POSITION_PRECEDING 0x02
2424 #define DOCUMENT_POSITION_FOLLOWING 0x04
2425 #define DOCUMENT_POSITION_CONTAINS 0x08
2426 #define DOCUMENT_POSITION_CONTAINED_BY 0x10
2427 #define DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 0x20
2428 
dom_node_compare_document_position(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * node_ce)2429 static void dom_node_compare_document_position(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *node_ce)
2430 {
2431 	zval *id, *node_zval;
2432 	xmlNodePtr other, this;
2433 	dom_object *this_intern, *other_intern;
2434 
2435 	ZEND_PARSE_PARAMETERS_START(1, 1)
2436 		Z_PARAM_OBJECT_OF_CLASS(node_zval, node_ce)
2437 	ZEND_PARSE_PARAMETERS_END();
2438 
2439 	DOM_GET_THIS_OBJ(this, id, xmlNodePtr, this_intern);
2440 	DOM_GET_OBJ(other, node_zval, xmlNodePtr, other_intern);
2441 
2442 	/* Step 1 */
2443 	if (this == other) {
2444 		RETURN_LONG(0);
2445 	}
2446 
2447 	/* Step 2 */
2448 	xmlNodePtr node1 = other;
2449 	xmlNodePtr node2 = this;
2450 
2451 	/* Step 3 */
2452 	xmlNodePtr attr1 = NULL;
2453 	xmlNodePtr attr2 = NULL;
2454 
2455 	/* Step 4 */
2456 	if (node1->type == XML_ATTRIBUTE_NODE) {
2457 		attr1 = node1;
2458 		node1 = attr1->parent;
2459 	}
2460 
2461 	/* Step 5 */
2462 	if (node2->type == XML_ATTRIBUTE_NODE) {
2463 		/* 5.1 */
2464 		attr2 = node2;
2465 		node2 = attr2->parent;
2466 
2467 		/* 5.2 */
2468 		if (attr1 != NULL && node1 != NULL && node2 == node1) {
2469 			for (const xmlAttr *attr = node2->properties; attr != NULL; attr = attr->next) {
2470 				if (php_dom_is_equal_attr(attr, (const xmlAttr *) attr1)) {
2471 					RETURN_LONG(DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_PRECEDING);
2472 				} else if (php_dom_is_equal_attr(attr, (const xmlAttr *) attr2)) {
2473 					RETURN_LONG(DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_FOLLOWING);
2474 				}
2475 			}
2476 		}
2477 	}
2478 
2479 	/* Step 6 */
2480 	/* We first check the first condition,
2481 	 * and as we need the root later anyway we'll cache the root and perform the root check after this if. */
2482 	if (node1 == NULL || node2 == NULL) {
2483 		goto disconnected;
2484 	}
2485 	bool node2_is_ancestor_of_node1 = false;
2486 	size_t node1_depth = 0;
2487 	xmlNodePtr node1_root = node1;
2488 	while (node1_root->parent) {
2489 		node1_root = node1_root->parent;
2490 		if (node1_root == node2) {
2491 			node2_is_ancestor_of_node1 = true;
2492 		}
2493 		node1_depth++;
2494 	}
2495 	bool node1_is_ancestor_of_node2 = false;
2496 	size_t node2_depth = 0;
2497 	xmlNodePtr node2_root = node2;
2498 	while (node2_root->parent) {
2499 		node2_root = node2_root->parent;
2500 		if (node2_root == node1) {
2501 			node1_is_ancestor_of_node2 = true;
2502 		}
2503 		node2_depth++;
2504 	}
2505 	/* Second condition from step 6 */
2506 	if (node1_root != node2_root) {
2507 		goto disconnected;
2508 	}
2509 
2510 	/* Step 7 */
2511 	if ((node1_is_ancestor_of_node2 && attr1 == NULL) || (node1 == node2 && attr2 != NULL)) {
2512 		RETURN_LONG(DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING);
2513 	}
2514 
2515 	/* Step 8 */
2516 	if ((node2_is_ancestor_of_node1 && attr2 == NULL) || (node1 == node2 && attr1 != NULL)) {
2517 		RETURN_LONG(DOCUMENT_POSITION_CONTAINED_BY | DOCUMENT_POSITION_FOLLOWING);
2518 	}
2519 
2520 	/* Special case: comparing children and attributes.
2521 	 * They belong to a different tree and are therefore hard to compare, but spec demands attributes to precede children
2522 	 * according to the pre-order depth-first search ordering.
2523 	 * Because their tree is different, the node parents only meet at the common element instead of earlier.
2524 	 * Therefore, it seems that one is the ancestor of the other. */
2525 	if (node1_is_ancestor_of_node2) {
2526 		ZEND_ASSERT(attr1 != NULL); /* Would've been handled in step 7 otherwise */
2527 		RETURN_LONG(DOCUMENT_POSITION_PRECEDING);
2528 	} else if (node2_is_ancestor_of_node1) {
2529 		ZEND_ASSERT(attr2 != NULL); /* Would've been handled in step 8 otherwise */
2530 		RETURN_LONG(DOCUMENT_POSITION_FOLLOWING);
2531 	}
2532 
2533 	/* Step 9 */
2534 
2535 	/* We'll use the following strategy (which was already prepared during step 6) to implement this efficiently:
2536 	 * 1. Move nodes upwards such that they are at the same depth.
2537 	 * 2. Then we move both nodes upwards simultaneously until their parents are equal.
2538 	 * 3. If we then move node1 to the next entry repeatedly and we encounter node2,
2539 	 *    then we know node1 precedes node2. Otherwise, node2 must precede node1. */
2540 	/* 1. */
2541 	if (node1_depth > node2_depth) {
2542 		do {
2543 			node1 = node1->parent;
2544 			node1_depth--;
2545 		} while (node1_depth > node2_depth);
2546 	} else if (node2_depth > node1_depth) {
2547 		do {
2548 			node2 = node2->parent;
2549 			node2_depth--;
2550 		} while (node2_depth > node1_depth);
2551 	}
2552 	/* 2. */
2553 	while (node1->parent != node2->parent) {
2554 		node1 = node1->parent;
2555 		node2 = node2->parent;
2556 	}
2557 	/* 3. */
2558 	ZEND_ASSERT(node1 != node2);
2559 	ZEND_ASSERT(node1 != NULL);
2560 	ZEND_ASSERT(node2 != NULL);
2561 	do {
2562 		node1 = node1->next;
2563 		if (node1 == node2) {
2564 			RETURN_LONG(DOCUMENT_POSITION_PRECEDING);
2565 		}
2566 	} while (node1 != NULL);
2567 
2568 	/* Step 10 */
2569 	RETURN_LONG(DOCUMENT_POSITION_FOLLOWING);
2570 
2571 disconnected:;
2572 	zend_long ordering;
2573 	if (node1 == node2) {
2574 		/* Degenerate case, they're both NULL, but the ordering must be consistent... */
2575 		ZEND_ASSERT(node1 == NULL);
2576 		ordering = other_intern < this_intern ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
2577 	} else {
2578 		ordering = node1 < node2 ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
2579 	}
2580 	RETURN_LONG(DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | ordering);
2581 }
2582 
PHP_METHOD(DOMNode,compareDocumentPosition)2583 PHP_METHOD(DOMNode, compareDocumentPosition)
2584 {
2585 	dom_node_compare_document_position(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_node_class_entry);
2586 }
2587 
PHP_METHOD(Dom_Node,compareDocumentPosition)2588 PHP_METHOD(Dom_Node, compareDocumentPosition)
2589 {
2590 	dom_node_compare_document_position(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_node_class_entry);
2591 }
2592 /* }}} */
2593 
2594 /**
2595  * We want to block the serialization and unserialization of DOM classes.
2596  * However, using @not-serializable makes the child classes also not serializable, even if the user implements the methods.
2597  * So instead, we implement the methods wherein we throw exceptions.
2598  * The reason we choose these methods is because:
2599  *   - If the user implements __serialize / __unserialize, the respective throwing methods are not called.
2600  *   - If the user implements __sleep / __wakeup, then it's also not a problem because they will not enter the throwing methods.
2601  */
2602 
PHP_METHOD(Dom_Node,__construct)2603 PHP_METHOD(Dom_Node, __construct)
2604 {
2605 	zend_throw_error(NULL, "Cannot directly construct %s, use document methods instead", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
2606 }
2607 
PHP_METHOD(DOMNode,__sleep)2608 PHP_METHOD(DOMNode, __sleep)
2609 {
2610 	ZEND_PARSE_PARAMETERS_NONE();
2611 
2612 	zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed, unless serialization methods are implemented in a subclass", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
2613 	RETURN_THROWS();
2614 }
2615 
PHP_METHOD(DOMNode,__wakeup)2616 PHP_METHOD(DOMNode, __wakeup)
2617 {
2618 	ZEND_PARSE_PARAMETERS_NONE();
2619 
2620 	zend_throw_exception_ex(NULL, 0, "Unserialization of '%s' is not allowed, unless unserialization methods are implemented in a subclass", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
2621 	RETURN_THROWS();
2622 }
2623 
2624 #endif
2625