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) == SUCCESS ||
841 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
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 dom_set_document_ref_pointers(child, intern->document);
910 }
911
912 php_libxml_invalidate_node_list_cache(intern->document);
913
914 if (ref != NULL) {
915 if (child->parent != NULL) {
916 xmlUnlinkNode(child);
917 }
918
919 if (child->type == XML_TEXT_NODE && (refp->type == XML_TEXT_NODE ||
920 (refp->prev != NULL && refp->prev->type == XML_TEXT_NODE))) {
921 new_child = child;
922 new_child->parent = refp->parent;
923 new_child->next = refp;
924 new_child->prev = refp->prev;
925 refp->prev = new_child;
926 if (new_child->prev != NULL) {
927 new_child->prev->next = new_child;
928 }
929 if (new_child->parent != NULL) {
930 if (new_child->parent->children == refp) {
931 new_child->parent->children = new_child;
932 }
933 }
934
935 } else if (child->type == XML_ATTRIBUTE_NODE) {
936 xmlAttrPtr lastattr;
937
938 if (child->ns == NULL)
939 lastattr = xmlHasProp(refp->parent, child->name);
940 else
941 lastattr = xmlHasNsProp(refp->parent, child->name, child->ns->href);
942 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
943 if (lastattr != (xmlAttrPtr) child) {
944 xmlUnlinkNode((xmlNodePtr) lastattr);
945 php_libxml_node_free_resource((xmlNodePtr) lastattr);
946 } else {
947 DOM_RET_OBJ(child, intern);
948 return;
949 }
950 }
951 new_child = xmlAddPrevSibling(refp, child);
952 if (UNEXPECTED(NULL == new_child)) {
953 goto cannot_add;
954 }
955 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
956 xmlNodePtr last = child->last;
957 new_child = dom_insert_fragment(parentp, refp->prev, refp, child, intern);
958 dom_reconcile_ns_list(parentp->doc, new_child, last);
959 } else {
960 new_child = xmlAddPrevSibling(refp, child);
961 if (UNEXPECTED(NULL == new_child)) {
962 goto cannot_add;
963 }
964 dom_reconcile_ns(parentp->doc, new_child);
965 }
966 } else {
967 if (child->parent != NULL){
968 xmlUnlinkNode(child);
969 }
970 if (child->type == XML_TEXT_NODE && parentp->last != NULL && parentp->last->type == XML_TEXT_NODE) {
971 child->parent = parentp;
972 new_child = child;
973 if (parentp->children == NULL) {
974 parentp->children = child;
975 parentp->last = child;
976 } else {
977 child = parentp->last;
978 child->next = new_child;
979 new_child->prev = child;
980 parentp->last = new_child;
981 }
982 } else if (child->type == XML_ATTRIBUTE_NODE) {
983 xmlAttrPtr lastattr;
984
985 if (child->ns == NULL)
986 lastattr = xmlHasProp(parentp, child->name);
987 else
988 lastattr = xmlHasNsProp(parentp, child->name, child->ns->href);
989 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
990 if (lastattr != (xmlAttrPtr) child) {
991 xmlUnlinkNode((xmlNodePtr) lastattr);
992 php_libxml_node_free_resource((xmlNodePtr) lastattr);
993 } else {
994 DOM_RET_OBJ(child, intern);
995 return;
996 }
997 }
998 new_child = xmlAddChild(parentp, child);
999 if (UNEXPECTED(NULL == new_child)) {
1000 goto cannot_add;
1001 }
1002 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
1003 xmlNodePtr last = child->last;
1004 new_child = dom_insert_fragment(parentp, parentp->last, NULL, child, intern);
1005 dom_reconcile_ns_list(parentp->doc, new_child, last);
1006 } else {
1007 new_child = xmlAddChild(parentp, child);
1008 if (UNEXPECTED(NULL == new_child)) {
1009 goto cannot_add;
1010 }
1011 dom_reconcile_ns(parentp->doc, new_child);
1012 }
1013 }
1014
1015 DOM_RET_OBJ(new_child, intern);
1016 return;
1017 cannot_add:
1018 zend_throw_error(NULL, "Cannot add newnode as the previous sibling of refnode");
1019 RETURN_THROWS();
1020 }
1021 /* }}} end dom_node_insert_before */
1022
1023 /* 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)1024 static void dom_node_insert_before_modern(zval *return_value, zval *ref, dom_object *intern, xmlNodePtr parentp, xmlNodePtr child)
1025 {
1026 xmlNodePtr refp = NULL;
1027 dom_object *refobjp;
1028 if (php_dom_pre_insert_is_parent_invalid(parentp)) {
1029 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1030 RETURN_THROWS();
1031 }
1032 if (ref != NULL) {
1033 DOM_GET_OBJ(refp, ref, xmlNodePtr, refobjp);
1034 }
1035 php_libxml_invalidate_node_list_cache(intern->document);
1036 php_dom_pre_insert(intern->document, child, parentp, refp);
1037 DOM_RET_OBJ(child, intern);
1038 }
1039
dom_node_insert_before(INTERNAL_FUNCTION_PARAMETERS,bool modern)1040 static void dom_node_insert_before(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1041 {
1042 zval *id, *node, *ref = NULL;
1043 xmlNodePtr child, parentp;
1044 dom_object *intern, *childobj;
1045
1046 id = ZEND_THIS;
1047 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|O!", &node, dom_get_node_ce(modern), &ref, dom_get_node_ce(modern)) == FAILURE) {
1048 RETURN_THROWS();
1049 }
1050
1051 DOM_GET_OBJ(parentp, id, xmlNodePtr, intern);
1052
1053 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1054
1055 if (modern) {
1056 dom_node_insert_before_modern(return_value, ref, intern, parentp, child);
1057 } else {
1058 dom_node_insert_before_legacy(return_value, ref, intern, childobj, parentp, child);
1059 }
1060 }
1061
PHP_METHOD(DOMNode,insertBefore)1062 PHP_METHOD(DOMNode, insertBefore)
1063 {
1064 dom_node_insert_before(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1065 }
1066
PHP_METHOD(Dom_Node,insertBefore)1067 PHP_METHOD(Dom_Node, insertBefore)
1068 {
1069 dom_node_insert_before(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1070 }
1071
1072 /* https://dom.spec.whatwg.org/#concept-node-replace */
dom_replace_node_validity_checks(xmlNodePtr parent,xmlNodePtr node,xmlNodePtr child)1073 static zend_result dom_replace_node_validity_checks(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child)
1074 {
1075 /* 1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException. */
1076 if (php_dom_pre_insert_is_parent_invalid(parent)) {
1077 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1078 return FAILURE;
1079 }
1080
1081 /* 2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException. */
1082 if (dom_hierarchy(parent, node) != SUCCESS) {
1083 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1084 return FAILURE;
1085 }
1086
1087 /* 3. If child’s parent is not parent, then throw a "NotFoundError" DOMException. */
1088 if (child->parent != parent) {
1089 php_dom_throw_error(NOT_FOUND_ERR, /* strict */ true);
1090 return FAILURE;
1091 }
1092
1093 /* 4. If node is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException. */
1094 if (node->type != XML_DOCUMENT_FRAG_NODE
1095 && node->type != XML_DTD_NODE
1096 && node->type != XML_ELEMENT_NODE
1097 && node->type != XML_TEXT_NODE
1098 && node->type != XML_CDATA_SECTION_NODE
1099 && node->type != XML_COMMENT_NODE
1100 && node->type != XML_PI_NODE) {
1101 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1102 return FAILURE;
1103 }
1104
1105 /* 5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document,
1106 * then throw a "HierarchyRequestError" DOMException. */
1107 bool parent_is_document = parent->type == XML_DOCUMENT_NODE || parent->type == XML_HTML_DOCUMENT_NODE;
1108 if (parent_is_document && (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)) {
1109 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert text as a child of a document", /* strict */ true);
1110 return FAILURE;
1111 }
1112 if (!parent_is_document && node->type == XML_DTD_NODE) {
1113 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot insert a document type into anything other than a document", /* strict */ true);
1114 return FAILURE;
1115 }
1116
1117 /* 6. If parent is a document, and any of the statements below, switched on the interface node implements, are true,
1118 * then throw a "HierarchyRequestError" DOMException.
1119 * Spec note: These statements _slightly_ differ from the pre-insert algorithm. */
1120 if (parent_is_document) {
1121 /* DocumentFragment */
1122 if (node->type == XML_DOCUMENT_FRAG_NODE) {
1123 if (!php_dom_fragment_insertion_hierarchy_check_replace(parent, node, child)) {
1124 return FAILURE;
1125 }
1126 }
1127 /* Element */
1128 else if (node->type == XML_ELEMENT_NODE) {
1129 /* parent has an element child that is not child ... */
1130 if (xmlDocGetRootElement((xmlDocPtr) parent) != child) {
1131 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Cannot have more than one element child in a document", /* strict */ true);
1132 return FAILURE;
1133 }
1134 /* ... or a doctype is following child. */
1135 if (php_dom_has_sibling_following_node(child, XML_DTD_NODE)) {
1136 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
1137 return FAILURE;
1138 }
1139 }
1140 /* DocumentType */
1141 else if (node->type == XML_DTD_NODE) {
1142 /* parent has a doctype child that is not child, or an element is preceding child. */
1143 xmlDocPtr doc = (xmlDocPtr) parent;
1144 if (doc->intSubset != (xmlDtdPtr) child || php_dom_has_sibling_preceding_node(child, XML_ELEMENT_NODE)) {
1145 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Document types must be the first child in a document", /* strict */ true);
1146 return FAILURE;
1147 }
1148 }
1149 }
1150
1151 /* Steps 7 and onward perform the removal and insertion, and also track changes for mutation records.
1152 * We don't implement mutation records so we can just skip straight to the replace part. */
1153
1154 return SUCCESS;
1155 }
1156
1157 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-785887307
1158 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-replacechild
1159 Since:
1160 */
dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS,bool modern)1161 static void dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1162 {
1163 zval *id, *newnode, *oldnode;
1164 xmlNodePtr newchild, oldchild, nodep;
1165 dom_object *intern, *newchildobj, *oldchildobj;
1166
1167 id = ZEND_THIS;
1168 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &newnode, dom_get_node_ce(modern), &oldnode, dom_get_node_ce(modern)) == FAILURE) {
1169 RETURN_THROWS();
1170 }
1171
1172 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1173
1174 DOM_GET_OBJ(newchild, newnode, xmlNodePtr, newchildobj);
1175 DOM_GET_OBJ(oldchild, oldnode, xmlNodePtr, oldchildobj);
1176
1177 bool stricterror = dom_get_strict_error(intern->document);
1178
1179 if (newchild->doc != nodep->doc && newchild->doc != NULL) {
1180 php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1181 RETURN_FALSE;
1182 }
1183
1184 if (modern) {
1185 if (dom_replace_node_validity_checks(nodep, newchild, oldchild) != SUCCESS) {
1186 RETURN_THROWS();
1187 }
1188 } else {
1189 if (!dom_node_children_valid(nodep)) {
1190 RETURN_FALSE;
1191 }
1192
1193 if (!nodep->children) {
1194 RETURN_FALSE;
1195 }
1196
1197 if (!dom_node_check_legacy_insertion_validity(nodep, newchild, stricterror, false)) {
1198 RETURN_FALSE;
1199 }
1200
1201 /* This is already disallowed by libxml, but we should check it here to avoid
1202 * breaking assumptions and assertions. */
1203 if ((oldchild->type == XML_ATTRIBUTE_NODE) != (newchild->type == XML_ATTRIBUTE_NODE)) {
1204 php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1205 RETURN_FALSE;
1206 }
1207
1208 if (oldchild->parent != nodep) {
1209 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1210 RETURN_FALSE;
1211 }
1212 }
1213
1214 if (newchild->doc == NULL && nodep->doc != NULL) {
1215 dom_set_document_ref_pointers(newchild, intern->document);
1216 }
1217
1218 if (newchild->type == XML_DOCUMENT_FRAG_NODE) {
1219 xmlNodePtr prevsib, nextsib;
1220 prevsib = oldchild->prev;
1221 nextsib = oldchild->next;
1222
1223 xmlUnlinkNode(oldchild);
1224
1225 xmlNodePtr last = newchild->last;
1226 newchild = dom_insert_fragment(nodep, prevsib, nextsib, newchild, intern);
1227 if (newchild && !modern) {
1228 dom_reconcile_ns_list(nodep->doc, newchild, last);
1229 }
1230 } else if (oldchild != newchild) {
1231 xmlDtdPtr intSubset = xmlGetIntSubset(nodep->doc);
1232 bool replacedoctype = (intSubset == (xmlDtd *) oldchild);
1233
1234 xmlReplaceNode(oldchild, newchild);
1235 if (!modern) {
1236 dom_reconcile_ns(nodep->doc, newchild);
1237 }
1238
1239 if (replacedoctype) {
1240 nodep->doc->intSubset = (xmlDtd *) newchild;
1241 }
1242 }
1243 php_libxml_invalidate_node_list_cache(intern->document);
1244 DOM_RET_OBJ(oldchild, intern);
1245 }
1246
PHP_METHOD(DOMNode,replaceChild)1247 PHP_METHOD(DOMNode, replaceChild)
1248 {
1249 dom_node_replace_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1250 }
1251
PHP_METHOD(Dom_Node,replaceChild)1252 PHP_METHOD(Dom_Node, replaceChild)
1253 {
1254 dom_node_replace_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1255 }
1256 /* }}} end dom_node_replace_child */
1257
1258 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1734834066
1259 Since:
1260 */
dom_node_remove_child(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * node_ce)1261 static void dom_node_remove_child(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *node_ce)
1262 {
1263 zval *node;
1264 xmlNodePtr child, nodep;
1265 dom_object *intern, *childobj;
1266
1267 ZEND_PARSE_PARAMETERS_START(1, 1)
1268 Z_PARAM_OBJECT_OF_CLASS(node, node_ce)
1269 ZEND_PARSE_PARAMETERS_END();
1270
1271 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1272
1273 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1274
1275 bool stricterror = dom_get_strict_error(intern->document);
1276
1277 if (!nodep->children || child->parent != nodep) {
1278 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1279 RETURN_FALSE;
1280 }
1281
1282 if (dom_node_is_read_only(nodep) == SUCCESS ||
1283 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
1284 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1285 RETURN_FALSE;
1286 }
1287
1288 xmlUnlinkNode(child);
1289 php_libxml_invalidate_node_list_cache(intern->document);
1290 DOM_RET_OBJ(child, intern);
1291 }
1292
PHP_METHOD(DOMNode,removeChild)1293 PHP_METHOD(DOMNode, removeChild)
1294 {
1295 dom_node_remove_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_node_class_entry);
1296 }
1297
PHP_METHOD(Dom_Node,removeChild)1298 PHP_METHOD(Dom_Node, removeChild)
1299 {
1300 dom_node_remove_child(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_node_class_entry);
1301 }
1302 /* }}} end dom_node_remove_child */
1303
1304 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-184E7107
1305 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-appendchild
1306 Since:
1307 */
dom_node_append_child_legacy(zval * return_value,dom_object * intern,dom_object * childobj,xmlNodePtr nodep,xmlNodePtr child)1308 static void dom_node_append_child_legacy(zval *return_value, dom_object *intern, dom_object *childobj, xmlNodePtr nodep, xmlNodePtr child)
1309 {
1310 xmlNodePtr new_child = NULL;
1311
1312 if (!dom_node_children_valid(nodep)) {
1313 RETURN_FALSE;
1314 }
1315
1316 bool stricterror = dom_get_strict_error(intern->document);
1317
1318 if (!dom_node_check_legacy_insertion_validity(nodep, child, stricterror, true)) {
1319 RETURN_FALSE;
1320 }
1321
1322 if (child->doc == NULL && nodep->doc != NULL) {
1323 dom_set_document_ref_pointers(child, intern->document);
1324 }
1325
1326 if (child->parent != NULL){
1327 xmlUnlinkNode(child);
1328 }
1329
1330 if (child->type == XML_TEXT_NODE && nodep->last != NULL && nodep->last->type == XML_TEXT_NODE) {
1331 child->parent = nodep;
1332 new_child = child;
1333 if (nodep->children == NULL) {
1334 nodep->children = child;
1335 nodep->last = child;
1336 } else {
1337 child = nodep->last;
1338 child->next = new_child;
1339 new_child->prev = child;
1340 nodep->last = new_child;
1341 }
1342 } else if (child->type == XML_ATTRIBUTE_NODE) {
1343 xmlAttrPtr lastattr;
1344
1345 if (child->ns == NULL)
1346 lastattr = xmlHasProp(nodep, child->name);
1347 else
1348 lastattr = xmlHasNsProp(nodep, child->name, child->ns->href);
1349 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
1350 if (lastattr != (xmlAttrPtr) child) {
1351 xmlUnlinkNode((xmlNodePtr) lastattr);
1352 php_libxml_node_free_resource((xmlNodePtr) lastattr);
1353 }
1354 }
1355 new_child = xmlAddChild(nodep, child);
1356 if (UNEXPECTED(new_child == NULL)) {
1357 goto cannot_add;
1358 }
1359 php_dom_reconcile_attribute_namespace_after_insertion((xmlAttrPtr) new_child);
1360 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
1361 xmlNodePtr last = child->last;
1362 new_child = dom_insert_fragment(nodep, nodep->last, NULL, child, intern);
1363 dom_reconcile_ns_list(nodep->doc, new_child, last);
1364 } else if (child->type == XML_DTD_NODE) {
1365 if (nodep->doc->intSubset != NULL) {
1366 php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "A document may only contain one document type", stricterror);
1367 RETURN_FALSE;
1368 }
1369 new_child = xmlAddChild(nodep, child);
1370 if (UNEXPECTED(new_child == NULL)) {
1371 goto cannot_add;
1372 }
1373 nodep->doc->intSubset = (xmlDtdPtr) new_child;
1374 } else {
1375 new_child = xmlAddChild(nodep, child);
1376 if (UNEXPECTED(new_child == NULL)) {
1377 goto cannot_add;
1378 }
1379 dom_reconcile_ns(nodep->doc, new_child);
1380 }
1381
1382 php_libxml_invalidate_node_list_cache(intern->document);
1383
1384 DOM_RET_OBJ(new_child, intern);
1385 return;
1386 cannot_add:
1387 php_dom_throw_error(INVALID_STATE_ERR, stricterror);
1388 RETURN_FALSE;
1389 }
1390 /* }}} end dom_node_append_child */
1391
PHP_METHOD(DOMNode,appendChild)1392 PHP_METHOD(DOMNode, appendChild)
1393 {
1394 zval *node;
1395 xmlNodePtr nodep, child;
1396 dom_object *intern, *childobj;
1397
1398 ZEND_PARSE_PARAMETERS_START(1, 1)
1399 Z_PARAM_OBJECT_OF_CLASS(node, dom_node_class_entry)
1400 ZEND_PARSE_PARAMETERS_END();
1401
1402 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1403 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1404
1405 dom_node_append_child_legacy(return_value, intern, childobj, nodep, child);
1406 }
1407
PHP_METHOD(Dom_Node,appendChild)1408 PHP_METHOD(Dom_Node, appendChild)
1409 {
1410 zval *node;
1411 xmlNodePtr nodep, child;
1412 dom_object *intern, *childobj;
1413
1414 ZEND_PARSE_PARAMETERS_START(1, 1)
1415 Z_PARAM_OBJECT_OF_CLASS(node, dom_modern_node_class_entry)
1416 ZEND_PARSE_PARAMETERS_END();
1417
1418 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1419 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1420
1421 /* Parent check from pre-insertion validation done here:
1422 * If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException. */
1423 if (php_dom_pre_insert_is_parent_invalid(nodep)) {
1424 php_dom_throw_error(HIERARCHY_REQUEST_ERR, /* strict */ true);
1425 RETURN_THROWS();
1426 }
1427 /* Append, this doesn't do the parent check so we do it here. */
1428 php_libxml_invalidate_node_list_cache(intern->document);
1429 php_dom_node_append(intern->document, child, nodep);
1430 DOM_RET_OBJ(child, intern);
1431 }
1432
1433 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-810594187
1434 Since:
1435 */
PHP_METHOD(DOMNode,hasChildNodes)1436 PHP_METHOD(DOMNode, hasChildNodes)
1437 {
1438 xmlNode *nodep;
1439 dom_object *intern;
1440
1441 ZEND_PARSE_PARAMETERS_NONE();
1442
1443 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1444
1445 RETURN_BOOL(dom_node_children_valid(nodep) && nodep->children != NULL);
1446 }
1447 /* }}} end dom_node_has_child_nodes */
1448
1449 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3A0ED0A4
1450 Since:
1451 */
PHP_METHOD(DOMNode,cloneNode)1452 PHP_METHOD(DOMNode, cloneNode)
1453 {
1454 zval *id;
1455 xmlNode *n, *node;
1456 dom_object *intern;
1457 bool recursive = 0;
1458
1459 id = ZEND_THIS;
1460 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &recursive) == FAILURE) {
1461 RETURN_THROWS();
1462 }
1463
1464 DOM_GET_OBJ(n, id, xmlNodePtr, intern);
1465
1466 php_dom_private_data *private_data = NULL;
1467 bool clone_document = n->type == XML_DOCUMENT_NODE || n->type == XML_HTML_DOCUMENT_NODE;
1468 if (php_dom_follow_spec_intern(intern)) {
1469 if (clone_document) {
1470 private_data = php_dom_private_data_create();
1471 } else {
1472 private_data = php_dom_get_private_data(intern);
1473 }
1474 }
1475
1476 node = dom_clone_node(php_dom_ns_mapper_from_private(private_data), n, n->doc, recursive);
1477
1478 if (!node) {
1479 if (clone_document && private_data != NULL) {
1480 php_dom_private_data_destroy(private_data);
1481 }
1482 RETURN_FALSE;
1483 }
1484
1485 /* If document cloned we want a new document proxy */
1486 if (clone_document) {
1487 dom_object *new_intern;
1488 if (private_data) {
1489 /* We have the issue here that we can't create a modern node without an intern.
1490 * Fortunately, it's impossible to have a custom document class for the modern DOM (final base class),
1491 * so we can solve this by invoking the instantiation helper directly. */
1492 zend_class_entry *ce = n->type == XML_DOCUMENT_NODE ? dom_xml_document_class_entry : dom_html_document_class_entry;
1493 new_intern = php_dom_instantiate_object_helper(return_value, ce, node, NULL);
1494 } else {
1495 DOM_RET_OBJ(node, NULL);
1496 new_intern = Z_DOMOBJ_P(return_value);
1497 }
1498 php_dom_update_document_after_clone(intern, n, new_intern, node);
1499 ZEND_ASSERT(new_intern->document->private_data == NULL);
1500 new_intern->document->private_data = php_dom_libxml_private_data_header(private_data);
1501 } else {
1502 if (node->type == XML_ATTRIBUTE_NODE && n->ns != NULL && node->ns == NULL) {
1503 /* Let reconciliation deal with this. The lifetime of the namespace poses no problem
1504 * because we're increasing the refcount of the document proxy at the return.
1505 * libxml2 doesn't set the ns because it can't know that this is safe. */
1506 node->ns = n->ns;
1507 }
1508
1509 DOM_RET_OBJ(node, intern);
1510 }
1511 }
1512 /* }}} end dom_node_clone_node */
1513
1514 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-normalize
1515 Since:
1516 */
PHP_METHOD(DOMNode,normalize)1517 PHP_METHOD(DOMNode, normalize)
1518 {
1519 zval *id;
1520 xmlNode *nodep;
1521 dom_object *intern;
1522
1523 id = ZEND_THIS;
1524 ZEND_PARSE_PARAMETERS_NONE();
1525
1526 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1527
1528 if (php_dom_follow_spec_intern(intern)) {
1529 php_dom_normalize_modern(nodep);
1530 } else {
1531 php_dom_normalize_legacy(nodep);
1532 }
1533 }
1534 /* }}} end dom_node_normalize */
1535
1536 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Level-2-Core-Node-supports
1537 Since: DOM Level 2
1538 */
PHP_METHOD(DOMNode,isSupported)1539 PHP_METHOD(DOMNode, isSupported)
1540 {
1541 zend_string *feature, *version;
1542
1543 if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &feature, &version) == FAILURE) {
1544 RETURN_THROWS();
1545 }
1546
1547 RETURN_BOOL(dom_has_feature(feature, version));
1548 }
1549 /* }}} end dom_node_is_supported */
1550
1551 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeHasAttrs
1552 Since: DOM Level 2
1553 */
PHP_METHOD(DOMNode,hasAttributes)1554 PHP_METHOD(DOMNode, hasAttributes)
1555 {
1556 xmlNode *nodep;
1557 dom_object *intern;
1558
1559 ZEND_PARSE_PARAMETERS_NONE();
1560
1561 DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
1562
1563 RETURN_BOOL(nodep->type == XML_ELEMENT_NODE && nodep->properties != NULL);
1564 }
1565 /* }}} end dom_node_has_attributes */
1566
1567 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-isSameNode
1568 Since: DOM Level 3
1569 */
dom_node_is_same_node(INTERNAL_FUNCTION_PARAMETERS,zval * node)1570 static void dom_node_is_same_node(INTERNAL_FUNCTION_PARAMETERS, zval *node)
1571 {
1572 zval *id;
1573 xmlNodePtr nodeotherp, nodep;
1574 dom_object *intern, *nodeotherobj;
1575
1576 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1577
1578 DOM_GET_OBJ(nodeotherp, node, xmlNodePtr, nodeotherobj);
1579
1580 if (nodep == nodeotherp) {
1581 RETURN_TRUE;
1582 } else {
1583 RETURN_FALSE;
1584 }
1585 }
1586
PHP_METHOD(DOMNode,isSameNode)1587 PHP_METHOD(DOMNode, isSameNode)
1588 {
1589 zval *node;
1590 ZEND_PARSE_PARAMETERS_START(1, 1)
1591 Z_PARAM_OBJECT_OF_CLASS(node, dom_node_class_entry)
1592 ZEND_PARSE_PARAMETERS_END();
1593
1594 dom_node_is_same_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, node);
1595 }
1596
PHP_METHOD(Dom_Node,isSameNode)1597 PHP_METHOD(Dom_Node, isSameNode)
1598 {
1599 zval *node;
1600 ZEND_PARSE_PARAMETERS_START(1, 1)
1601 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(node, dom_modern_node_class_entry)
1602 ZEND_PARSE_PARAMETERS_END();
1603
1604 if (node == NULL) {
1605 RETURN_FALSE;
1606 }
1607
1608 dom_node_is_same_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, node);
1609 }
1610 /* }}} end dom_node_is_same_node */
1611
php_dom_node_is_content_equal(const xmlNode * this,const xmlNode * other)1612 static bool php_dom_node_is_content_equal(const xmlNode *this, const xmlNode *other)
1613 {
1614 xmlChar *this_content = xmlNodeGetContent(this);
1615 xmlChar *other_content = xmlNodeGetContent(other);
1616 bool result = xmlStrEqual(this_content, other_content);
1617 xmlFree(this_content);
1618 xmlFree(other_content);
1619 return result;
1620 }
1621
php_dom_node_is_ns_uri_equal(const xmlNode * this,const xmlNode * other)1622 static bool php_dom_node_is_ns_uri_equal(const xmlNode *this, const xmlNode *other)
1623 {
1624 const xmlChar *this_ns = this->ns ? this->ns->href : NULL;
1625 const xmlChar *other_ns = other->ns ? other->ns->href : NULL;
1626 return xmlStrEqual(this_ns, other_ns);
1627 }
1628
php_dom_node_is_ns_prefix_equal(const xmlNode * this,const xmlNode * other)1629 static bool php_dom_node_is_ns_prefix_equal(const xmlNode *this, const xmlNode *other)
1630 {
1631 const xmlChar *this_ns = this->ns ? this->ns->prefix : NULL;
1632 const xmlChar *other_ns = other->ns ? other->ns->prefix : NULL;
1633 return xmlStrEqual(this_ns, other_ns);
1634 }
1635
1636 static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other, bool spec_compliant);
1637
1638 #define PHP_DOM_FUNC_CAT(prefix, suffix) prefix##_##suffix
1639 /* xmlNode and xmlNs have incompatible struct layouts, i.e. the next field is in a different offset */
1640 #define PHP_DOM_DEFINE_LIST_COUNTER_HELPER(type) \
1641 static size_t PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(const type *node) \
1642 { \
1643 size_t counter = 0; \
1644 while (node) { \
1645 counter++; \
1646 node = node->next; \
1647 } \
1648 return counter; \
1649 }
1650 #define PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(type) \
1651 static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_ordered, type)(const type *list1, const type *list2, bool spec_compliant) \
1652 { \
1653 size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1); \
1654 if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) { \
1655 return false; \
1656 } \
1657 for (size_t i = 0; i < count; i++) { \
1658 if (!php_dom_node_is_equal_node((const xmlNode *) list1, (const xmlNode *) list2, spec_compliant)) { \
1659 return false; \
1660 } \
1661 list1 = list1->next; \
1662 list2 = list2->next; \
1663 } \
1664 return true; \
1665 }
1666 #define PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(type) \
1667 static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_unordered, type)(const type *list1, const type *list2, bool spec_compliant)\
1668 { \
1669 size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1); \
1670 if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) { \
1671 return false; \
1672 } \
1673 for (const type *n1 = list1; n1 != NULL; n1 = n1->next) { \
1674 bool found = false; \
1675 for (const type *n2 = list2; n2 != NULL && !found; n2 = n2->next) { \
1676 if (php_dom_node_is_equal_node((const xmlNode *) n1, (const xmlNode *) n2, spec_compliant)) { \
1677 found = true; \
1678 } \
1679 } \
1680 if (!found) { \
1681 return false; \
1682 } \
1683 } \
1684 return true; \
1685 }
1686
1687 PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNode)
PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNs)1688 PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNs)
1689 PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(xmlNode)
1690 PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNode)
1691 PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNs)
1692
1693 static bool php_dom_is_equal_attr(const xmlAttr *this_attr, const xmlAttr *other_attr)
1694 {
1695 ZEND_ASSERT(this_attr != NULL);
1696 ZEND_ASSERT(other_attr != NULL);
1697 return xmlStrEqual(this_attr->name, other_attr->name)
1698 && php_dom_node_is_ns_uri_equal((const xmlNode *) this_attr, (const xmlNode *) other_attr)
1699 && php_dom_node_is_content_equal((const xmlNode *) this_attr, (const xmlNode *) other_attr);
1700 }
1701
php_dom_node_is_equal_node(const xmlNode * this,const xmlNode * other,bool spec_compliant)1702 static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other, bool spec_compliant)
1703 {
1704 ZEND_ASSERT(this != NULL);
1705 ZEND_ASSERT(other != NULL);
1706
1707 if (this->type != other->type) {
1708 return false;
1709 }
1710
1711 /* Notes:
1712 * - XML_DOCUMENT_TYPE_NODE is no longer created by libxml2, we only have to support XML_DTD_NODE.
1713 * - element and attribute declarations are not exposed as nodes in DOM, so no comparison is needed for those. */
1714 if (this->type == XML_ELEMENT_NODE) {
1715 return xmlStrEqual(this->name, other->name)
1716 && php_dom_node_is_ns_prefix_equal(this, other)
1717 && php_dom_node_is_ns_uri_equal(this, other)
1718 /* Check attributes first, then namespace declarations, then children */
1719 && php_dom_node_list_equality_check_unordered_xmlNode((const xmlNode *) this->properties, (const xmlNode *) other->properties, spec_compliant)
1720 && (spec_compliant || php_dom_node_list_equality_check_unordered_xmlNs(this->nsDef, other->nsDef, false))
1721 && php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children, spec_compliant);
1722 } else if (this->type == XML_DTD_NODE) {
1723 /* Note: in the living spec entity declarations and notations are no longer compared because they're considered obsolete. */
1724 const xmlDtd *this_dtd = (const xmlDtd *) this;
1725 const xmlDtd *other_dtd = (const xmlDtd *) other;
1726 return xmlStrEqual(this_dtd->name, other_dtd->name)
1727 && xmlStrEqual(this_dtd->ExternalID, other_dtd->ExternalID)
1728 && xmlStrEqual(this_dtd->SystemID, other_dtd->SystemID);
1729 } else if (this->type == XML_PI_NODE) {
1730 return xmlStrEqual(this->name, other->name) && xmlStrEqual(this->content, other->content);
1731 } else if (this->type == XML_TEXT_NODE || this->type == XML_COMMENT_NODE || this->type == XML_CDATA_SECTION_NODE) {
1732 return xmlStrEqual(this->content, other->content);
1733 } else if (this->type == XML_ATTRIBUTE_NODE) {
1734 const xmlAttr *this_attr = (const xmlAttr *) this;
1735 const xmlAttr *other_attr = (const xmlAttr *) other;
1736 return php_dom_is_equal_attr(this_attr, other_attr);
1737 } else if (this->type == XML_ENTITY_REF_NODE) {
1738 return xmlStrEqual(this->name, other->name);
1739 } else if (this->type == XML_ENTITY_DECL || this->type == XML_NOTATION_NODE || this->type == XML_ENTITY_NODE) {
1740 const xmlEntity *this_entity = (const xmlEntity *) this;
1741 const xmlEntity *other_entity = (const xmlEntity *) other;
1742 return this_entity->etype == other_entity->etype
1743 && xmlStrEqual(this_entity->name, other_entity->name)
1744 && xmlStrEqual(this_entity->ExternalID, other_entity->ExternalID)
1745 && xmlStrEqual(this_entity->SystemID, other_entity->SystemID)
1746 && php_dom_node_is_content_equal(this, other);
1747 } else if (this->type == XML_NAMESPACE_DECL) {
1748 const xmlNs *this_ns = (const xmlNs *) this;
1749 const xmlNs *other_ns = (const xmlNs *) other;
1750 return xmlStrEqual(this_ns->prefix, other_ns->prefix) && xmlStrEqual(this_ns->href, other_ns->href);
1751 } else if (this->type == XML_DOCUMENT_FRAG_NODE || this->type == XML_HTML_DOCUMENT_NODE || this->type == XML_DOCUMENT_NODE) {
1752 return php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children, spec_compliant);
1753 }
1754
1755 return false;
1756 }
1757
1758 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-isequalnode (for everything still in the living spec)
1759 * 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)
1760 Since: DOM Level 3
1761 */
dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS,bool modern)1762 static void dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1763 {
1764 zval *id, *node;
1765 xmlNodePtr otherp, nodep;
1766 dom_object *intern;
1767
1768 id = ZEND_THIS;
1769 ZEND_PARSE_PARAMETERS_START(1, 1)
1770 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(node, dom_get_node_ce(modern))
1771 ZEND_PARSE_PARAMETERS_END();
1772
1773 if (node == NULL) {
1774 RETURN_FALSE;
1775 }
1776
1777 DOM_GET_OBJ(otherp, node, xmlNodePtr, intern);
1778 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1779
1780 if (nodep == otherp) {
1781 RETURN_TRUE;
1782 }
1783
1784 /* Empty fragments/documents only match if they're both empty */
1785 if (nodep == NULL || otherp == NULL) {
1786 RETURN_BOOL(nodep == NULL && otherp == NULL);
1787 }
1788
1789 RETURN_BOOL(php_dom_node_is_equal_node(nodep, otherp, modern));
1790 }
1791
PHP_METHOD(DOMNode,isEqualNode)1792 PHP_METHOD(DOMNode, isEqualNode)
1793 {
1794 dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1795 }
1796
PHP_METHOD(Dom_Node,isEqualNode)1797 PHP_METHOD(Dom_Node, isEqualNode)
1798 {
1799 dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1800 }
1801 /* }}} end DOMNode::isEqualNode */
1802
1803 /* https://dom.spec.whatwg.org/#locate-a-namespace-prefix */
dom_locate_a_namespace_prefix(xmlNodePtr elem,const char * uri)1804 static const xmlChar *dom_locate_a_namespace_prefix(xmlNodePtr elem, const char *uri)
1805 {
1806 do {
1807 /* 1. If element’s namespace is namespace and its namespace prefix is non-null, then return its namespace prefix. */
1808 if (elem->ns != NULL && elem->ns->prefix != NULL && xmlStrEqual(elem->ns->href, BAD_CAST uri)) {
1809 return elem->ns->prefix;
1810 }
1811
1812 /* 2. If element has an attribute whose namespace prefix is "xmlns" and value is namespace,
1813 * then return element’s first such attribute’s local name. */
1814 for (xmlAttrPtr attr = elem->properties; attr != NULL; attr = attr->next) {
1815 if (attr->ns != NULL && attr->children != NULL
1816 && xmlStrEqual(attr->ns->prefix, BAD_CAST "xmlns") && xmlStrEqual(attr->children->content, BAD_CAST uri)) {
1817 return attr->name;
1818 }
1819 }
1820
1821 /* 3. If element’s parent element is not null, then return the result of running locate a namespace prefix on that element using namespace. */
1822 elem = elem->parent;
1823 } while (elem != NULL && elem->type == XML_ELEMENT_NODE);
1824
1825 /* 4. Return null. */
1826 return NULL;
1827 }
1828
1829 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-lookupNamespacePrefix
1830 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-lookupprefix
1831 Since: DOM Level 3
1832 */
dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAMETERS,bool modern)1833 static void dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1834 {
1835 zval *id;
1836 xmlNodePtr nodep, lookupp = NULL;
1837 dom_object *intern;
1838 xmlNsPtr nsptr;
1839 size_t uri_len = 0;
1840 char *uri;
1841
1842 id = ZEND_THIS;
1843 if (zend_parse_parameters(ZEND_NUM_ARGS(), modern ? "s!" : "s", &uri, &uri_len) == FAILURE) {
1844 RETURN_THROWS();
1845 }
1846
1847 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1848
1849 /* 1. If namespace is null or the empty string, then return null. */
1850 if (uri_len > 0) {
1851 /* 2. Switch on the interface this implements: */
1852 switch (nodep->type) {
1853 case XML_ELEMENT_NODE:
1854 lookupp = nodep;
1855 break;
1856 case XML_DOCUMENT_NODE:
1857 case XML_HTML_DOCUMENT_NODE:
1858 lookupp = xmlDocGetRootElement((xmlDocPtr) nodep);
1859 break;
1860 case XML_ENTITY_NODE :
1861 case XML_NOTATION_NODE:
1862 case XML_DOCUMENT_FRAG_NODE:
1863 case XML_DOCUMENT_TYPE_NODE:
1864 case XML_DTD_NODE:
1865 RETURN_NULL();
1866 break;
1867 default:
1868 lookupp = nodep->parent;
1869 }
1870
1871 if (lookupp != NULL) {
1872 if (modern) {
1873 const char * result = (const char *) dom_locate_a_namespace_prefix(lookupp, uri);
1874 if (result != NULL) {
1875 RETURN_STRING(result);
1876 }
1877 } else {
1878 nsptr = xmlSearchNsByHref(lookupp->doc, lookupp, BAD_CAST uri);
1879 if (nsptr && nsptr->prefix != NULL) {
1880 RETURN_STRING((const char *) nsptr->prefix);
1881 }
1882 }
1883 }
1884 }
1885
1886 RETURN_NULL();
1887 }
1888
PHP_METHOD(DOMNode,lookupPrefix)1889 PHP_METHOD(DOMNode, lookupPrefix)
1890 {
1891 dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1892 }
1893
PHP_METHOD(Dom_Node,lookupPrefix)1894 PHP_METHOD(Dom_Node, lookupPrefix)
1895 {
1896 dom_node_lookup_prefix(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1897 }
1898 /* }}} end dom_node_lookup_prefix */
1899
1900 /* https://dom.spec.whatwg.org/#locate-a-namespace */
dom_locate_a_namespace(const xmlNode * node,const zend_string * prefix)1901 const char *dom_locate_a_namespace(const xmlNode *node, const zend_string *prefix)
1902 {
1903 /* switch on the interface node implements: */
1904 if (node->type == XML_ELEMENT_NODE) {
1905 if (prefix != NULL) {
1906 /* 1. If prefix is "xml", then return the XML namespace. */
1907 if (zend_string_equals_literal_ci(prefix, "xml")) {
1908 return DOM_XML_NS_URI;
1909 }
1910
1911 /* 2. If prefix is "xmlns", then return the XMLNS namespace. */
1912 if (zend_string_equals_literal_ci(prefix, "xmlns")) {
1913 return DOM_XMLNS_NS_URI;
1914 }
1915 }
1916
1917 do {
1918 /* 3. If its namespace is non-null and its namespace prefix is prefix, then return namespace. */
1919 if (node->ns != NULL && xmlStrEqual(node->ns->prefix, BAD_CAST (prefix ? ZSTR_VAL(prefix) : NULL))) {
1920 return (const char *) node->ns->href;
1921 }
1922
1923 /* 4. If it has an attribute whose namespace is the XMLNS namespace, namespace prefix is "xmlns", and local name is prefix,
1924 * 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",
1925 * then return its value if it is not the empty string, and null otherwise. */
1926 for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
1927 if (attr->ns == NULL || !php_dom_ns_is_fast_ex(attr->ns, php_dom_ns_is_xmlns_magic_token)) {
1928 continue;
1929 }
1930 if ((prefix != NULL && xmlStrEqual(attr->ns->prefix, BAD_CAST "xmlns") && xmlStrEqual(attr->name, BAD_CAST ZSTR_VAL(prefix)))
1931 || (prefix == NULL && attr->ns->prefix == NULL && xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
1932 if (attr->children != NULL && attr->children->content[0] != '\0') {
1933 return (const char *) attr->children->content;
1934 } else {
1935 return NULL;
1936 }
1937 }
1938 }
1939
1940 /* 5. If its parent element is null, then return null. */
1941 if (node->parent == NULL || node->parent->type != XML_ELEMENT_NODE) {
1942 return NULL;
1943 }
1944
1945 /* 6. Return the result of running locate a namespace on its parent element using prefix. */
1946 node = node->parent;
1947 } while (true);
1948 } else if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
1949 /* 1. If its document element is null, then return null. */
1950 node = xmlDocGetRootElement((xmlDocPtr) node);
1951 if (UNEXPECTED(node == NULL)) {
1952 return NULL;
1953 }
1954
1955 /* 2. Return the result of running locate a namespace on its document element using prefix. */
1956 return dom_locate_a_namespace(node, prefix);
1957 } else if (node->type == XML_DTD_NODE || node->type == XML_DOCUMENT_FRAG_NODE) {
1958 return NULL;
1959 } else {
1960 /* 1. If its element is null, then return null / If its parent element is null, then return null. */
1961 if (node->parent == NULL || node->parent->type != XML_ELEMENT_NODE) {
1962 return NULL;
1963 }
1964
1965 /* 2. Return the result of running locate a namespace on its element using prefix. */
1966 return dom_locate_a_namespace(node->parent, prefix);
1967 }
1968 }
1969
1970 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isDefaultNamespace
1971 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-isdefaultnamespace
1972 Since: DOM Level 3
1973 */
PHP_METHOD(DOMNode,isDefaultNamespace)1974 PHP_METHOD(DOMNode, isDefaultNamespace)
1975 {
1976 zval *id;
1977 xmlNodePtr nodep;
1978 dom_object *intern;
1979 xmlNsPtr nsptr;
1980 size_t uri_len = 0;
1981 char *uri;
1982
1983 ZEND_PARSE_PARAMETERS_START(1, 1)
1984 Z_PARAM_STRING(uri, uri_len)
1985 ZEND_PARSE_PARAMETERS_END();
1986
1987 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1988
1989 if (uri_len > 0) {
1990 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
1991 nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
1992 if (nodep == NULL) {
1993 RETURN_FALSE;
1994 }
1995 }
1996
1997 nsptr = xmlSearchNs(nodep->doc, nodep, NULL);
1998 if (nsptr && xmlStrEqual(nsptr->href, BAD_CAST uri)) {
1999 RETURN_TRUE;
2000 }
2001 }
2002
2003 RETURN_FALSE;
2004 }
2005
PHP_METHOD(Dom_Node,isDefaultNamespace)2006 PHP_METHOD(Dom_Node, isDefaultNamespace)
2007 {
2008 zval *id;
2009 xmlNodePtr nodep;
2010 dom_object *intern;
2011 size_t uri_len = 0;
2012 char *uri;
2013
2014 ZEND_PARSE_PARAMETERS_START(1, 1)
2015 Z_PARAM_STRING_OR_NULL(uri, uri_len)
2016 ZEND_PARSE_PARAMETERS_END();
2017
2018 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2019
2020 if (uri_len == 0) {
2021 uri = NULL;
2022 }
2023 const char *ns_uri = dom_locate_a_namespace(nodep, NULL);
2024 RETURN_BOOL(xmlStrEqual(BAD_CAST uri, BAD_CAST ns_uri));
2025 }
2026 /* }}} end dom_node_is_default_namespace */
2027
2028 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI
2029 Modern spec URL: https://dom.spec.whatwg.org/#dom-node-lookupnamespaceuri
2030 Since: DOM Level 3
2031 */
PHP_METHOD(DOMNode,lookupNamespaceURI)2032 PHP_METHOD(DOMNode, lookupNamespaceURI)
2033 {
2034 zval *id;
2035 xmlNodePtr nodep;
2036 dom_object *intern;
2037 xmlNsPtr nsptr;
2038 zend_string *prefix;
2039
2040 id = ZEND_THIS;
2041 ZEND_PARSE_PARAMETERS_START(1, 1)
2042 Z_PARAM_STR_OR_NULL(prefix)
2043 ZEND_PARSE_PARAMETERS_END();
2044
2045 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
2046
2047 if (php_dom_follow_spec_intern(intern)) {
2048 if (prefix != NULL && ZSTR_LEN(prefix) == 0) {
2049 prefix = NULL;
2050 }
2051 const char *ns_uri = dom_locate_a_namespace(nodep, prefix);
2052 if (ns_uri == NULL) {
2053 RETURN_NULL();
2054 } else {
2055 RETURN_STRING(ns_uri);
2056 }
2057 } else {
2058 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
2059 nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
2060 if (nodep == NULL) {
2061 RETURN_NULL();
2062 }
2063 }
2064
2065 nsptr = xmlSearchNs(nodep->doc, nodep, BAD_CAST (prefix ? ZSTR_VAL(prefix) : NULL));
2066 if (nsptr && nsptr->href != NULL) {
2067 RETURN_STRING((char *) nsptr->href);
2068 }
2069 }
2070
2071 RETURN_NULL();
2072 }
2073 /* }}} end dom_node_lookup_namespace_uri */
2074
dom_canonicalize_node_parent_lookup_cb(void * user_data,xmlNodePtr node,xmlNodePtr parent)2075 static int dom_canonicalize_node_parent_lookup_cb(void *user_data, xmlNodePtr node, xmlNodePtr parent)
2076 {
2077 xmlNodePtr root = user_data;
2078 /* We have to unroll the first iteration because node->parent
2079 * is not necessarily equal to parent due to libxml2 tree rules (ns decls out of the tree for example). */
2080 if (node == root) {
2081 return 1;
2082 }
2083 node = parent;
2084 while (node != NULL) {
2085 if (node == root) {
2086 return 1;
2087 }
2088 node = node->parent;
2089 }
2090
2091 return 0;
2092 }
2093
dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS,int mode)2094 static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
2095 {
2096 zval *id;
2097 zval *xpath_array=NULL, *ns_prefixes=NULL;
2098 xmlNodePtr nodep;
2099 xmlDocPtr docp;
2100 xmlNodeSetPtr nodeset = NULL;
2101 dom_object *intern;
2102 bool exclusive=0, with_comments=0;
2103 xmlChar **inclusive_ns_prefixes = NULL;
2104 char *file = NULL;
2105 int ret = -1;
2106 size_t file_len = 0;
2107 xmlOutputBufferPtr buf;
2108 xmlXPathContextPtr ctxp=NULL;
2109 xmlXPathObjectPtr xpathobjp=NULL;
2110
2111 id = ZEND_THIS;
2112 if (mode == 0) {
2113 if (zend_parse_parameters(ZEND_NUM_ARGS(),
2114 "|bba!a!", &exclusive, &with_comments,
2115 &xpath_array, &ns_prefixes) == FAILURE) {
2116 RETURN_THROWS();
2117 }
2118 } else {
2119 if (zend_parse_parameters(ZEND_NUM_ARGS(),
2120 "s|bba!a!", &file, &file_len, &exclusive,
2121 &with_comments, &xpath_array, &ns_prefixes) == FAILURE) {
2122 RETURN_THROWS();
2123 }
2124 }
2125
2126 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
2127
2128 docp = nodep->doc;
2129
2130 if (! docp) {
2131 zend_throw_error(NULL, "Node must be associated with a document");
2132 RETURN_THROWS();
2133 }
2134
2135 bool simple_node_parent_lookup_callback = false;
2136 if (xpath_array == NULL) {
2137 /* Optimization: if the node is a document, all nodes may be included, no extra filtering or nodeset necessary. */
2138 if (nodep->type != XML_DOCUMENT_NODE && nodep->type != XML_HTML_DOCUMENT_NODE) {
2139 simple_node_parent_lookup_callback = true;
2140 }
2141 } else {
2142 /*xpath query from xpath_array */
2143 HashTable *ht = Z_ARRVAL_P(xpath_array);
2144 zval *tmp;
2145 char *xquery;
2146
2147 /* Find "query" key */
2148 tmp = zend_hash_find_deref(ht, ZSTR_KNOWN(ZEND_STR_QUERY));
2149 if (!tmp) {
2150 /* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
2151 zend_argument_value_error(3 + mode, "must have a \"query\" key");
2152 RETURN_THROWS();
2153 }
2154 if (Z_TYPE_P(tmp) != IS_STRING) {
2155 /* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
2156 zend_argument_type_error(3 + mode, "\"query\" option must be a string, %s given", zend_zval_value_name(tmp));
2157 RETURN_THROWS();
2158 }
2159 xquery = Z_STRVAL_P(tmp);
2160
2161 ctxp = xmlXPathNewContext(docp);
2162 ctxp->node = nodep;
2163
2164 tmp = zend_hash_str_find_deref(ht, "namespaces", sizeof("namespaces")-1);
2165 if (tmp && Z_TYPE_P(tmp) == IS_ARRAY && !HT_IS_PACKED(Z_ARRVAL_P(tmp))) {
2166 zval *tmpns;
2167 zend_string *prefix;
2168
2169 ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), prefix, tmpns) {
2170 ZVAL_DEREF(tmpns);
2171 if (Z_TYPE_P(tmpns) == IS_STRING) {
2172 if (prefix) {
2173 xmlXPathRegisterNs(ctxp, BAD_CAST ZSTR_VAL(prefix), BAD_CAST Z_STRVAL_P(tmpns));
2174 }
2175 }
2176 } ZEND_HASH_FOREACH_END();
2177 }
2178
2179 xpathobjp = xmlXPathEvalExpression(BAD_CAST xquery, ctxp);
2180 ctxp->node = NULL;
2181 if (xpathobjp && xpathobjp->type == XPATH_NODESET) {
2182 nodeset = xpathobjp->nodesetval;
2183 } else {
2184 if (xpathobjp) {
2185 xmlXPathFreeObject(xpathobjp);
2186 }
2187 xmlXPathFreeContext(ctxp);
2188 zend_throw_error(NULL, "XPath query did not return a nodeset");
2189 RETURN_THROWS();
2190 }
2191 }
2192
2193 if (ns_prefixes != NULL) {
2194 if (exclusive) {
2195 zval *tmpns;
2196 int nscount = 0;
2197
2198 inclusive_ns_prefixes = safe_emalloc(zend_hash_num_elements(Z_ARRVAL_P(ns_prefixes)) + 1,
2199 sizeof(xmlChar *), 0);
2200 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(ns_prefixes), tmpns) {
2201 ZVAL_DEREF(tmpns);
2202 if (Z_TYPE_P(tmpns) == IS_STRING) {
2203 inclusive_ns_prefixes[nscount++] = BAD_CAST Z_STRVAL_P(tmpns);
2204 }
2205 } ZEND_HASH_FOREACH_END();
2206 inclusive_ns_prefixes[nscount] = NULL;
2207 } else {
2208 php_error_docref(NULL, E_NOTICE,
2209 "Inclusive namespace prefixes only allowed in exclusive mode.");
2210 }
2211 }
2212
2213 if (mode == 1) {
2214 buf = xmlOutputBufferCreateFilename(file, NULL, 0);
2215 } else {
2216 buf = xmlAllocOutputBuffer(NULL);
2217 }
2218
2219 if (buf != NULL) {
2220 if (simple_node_parent_lookup_callback) {
2221 ret = xmlC14NExecute(docp, dom_canonicalize_node_parent_lookup_cb, nodep, exclusive, inclusive_ns_prefixes, with_comments, buf);
2222 } else {
2223 ret = xmlC14NDocSaveTo(docp, nodeset, exclusive, inclusive_ns_prefixes, with_comments, buf);
2224 }
2225 }
2226
2227 if (inclusive_ns_prefixes != NULL) {
2228 efree(inclusive_ns_prefixes);
2229 }
2230 if (xpathobjp != NULL) {
2231 xmlXPathFreeObject(xpathobjp);
2232 }
2233 if (ctxp != NULL) {
2234 xmlXPathFreeContext(ctxp);
2235 }
2236
2237 if (buf == NULL || ret < 0) {
2238 RETVAL_FALSE;
2239 } else {
2240 if (mode == 0) {
2241 size_t size = xmlOutputBufferGetSize(buf);
2242 if (size > 0) {
2243 RETVAL_STRINGL((char *) xmlOutputBufferGetContent(buf), size);
2244 } else {
2245 RETVAL_EMPTY_STRING();
2246 }
2247 }
2248 }
2249
2250 if (buf) {
2251 int bytes;
2252
2253 bytes = xmlOutputBufferClose(buf);
2254 if (mode == 1 && (ret >= 0)) {
2255 RETURN_LONG(bytes);
2256 }
2257 }
2258 }
2259 /* }}} */
2260
2261 /* {{{ Canonicalize nodes to a string */
PHP_METHOD(DOMNode,C14N)2262 PHP_METHOD(DOMNode, C14N)
2263 {
2264 dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2265 }
2266 /* }}} */
2267
2268 /* {{{ Canonicalize nodes to a file */
PHP_METHOD(DOMNode,C14NFile)2269 PHP_METHOD(DOMNode, C14NFile)
2270 {
2271 dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2272 }
2273 /* }}} */
2274
2275 /* {{{ Gets an xpath for a node */
dom_node_get_node_path(INTERNAL_FUNCTION_PARAMETERS,bool throw)2276 static void dom_node_get_node_path(INTERNAL_FUNCTION_PARAMETERS, bool throw)
2277 {
2278 zval *id;
2279 xmlNode *nodep;
2280 dom_object *intern;
2281 char *value;
2282
2283 ZEND_PARSE_PARAMETERS_NONE();
2284
2285 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2286
2287 value = (char *) xmlGetNodePath(nodep);
2288 if (value == NULL) {
2289 /* This is only possible when an invalid argument is passed (e.g. namespace declaration, but that's not the case for this call site),
2290 * or on allocation failure. So in other words, this only happens on allocation failure. */
2291 if (throw) {
2292 php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
2293 RETURN_THROWS();
2294 }
2295 RETURN_NULL();
2296 } else {
2297 RETVAL_STRING(value);
2298 xmlFree(value);
2299 }
2300 }
2301
PHP_METHOD(DOMNode,getNodePath)2302 PHP_METHOD(DOMNode, getNodePath)
2303 {
2304 dom_node_get_node_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
2305 }
2306
PHP_METHOD(Dom_Node,getNodePath)2307 PHP_METHOD(Dom_Node, getNodePath)
2308 {
2309 dom_node_get_node_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
2310 }
2311 /* }}} */
2312
2313 /* {{{ Gets line number for a node */
PHP_METHOD(DOMNode,getLineNo)2314 PHP_METHOD(DOMNode, getLineNo)
2315 {
2316 zval *id;
2317 xmlNode *nodep;
2318 dom_object *intern;
2319
2320 ZEND_PARSE_PARAMETERS_NONE();
2321
2322 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
2323
2324 RETURN_LONG(xmlGetLineNo(nodep));
2325 }
2326 /* }}} */
2327
2328 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-contains
2329 Since:
2330 */
dom_node_contains(xmlNodePtr thisp,xmlNodePtr otherp)2331 static bool dom_node_contains(xmlNodePtr thisp, xmlNodePtr otherp)
2332 {
2333 do {
2334 if (otherp == thisp) {
2335 return true;
2336 }
2337 otherp = otherp->parent;
2338 } while (otherp);
2339
2340 return false;
2341 }
2342
PHP_METHOD(DOMNode,contains)2343 PHP_METHOD(DOMNode, contains)
2344 {
2345 zval *other, *id;
2346 xmlNodePtr otherp, thisp;
2347 dom_object *unused_intern;
2348
2349 ZEND_PARSE_PARAMETERS_START(1, 1)
2350 Z_PARAM_OBJECT_OR_NULL(other)
2351 ZEND_PARSE_PARAMETERS_END();
2352
2353 if (other == NULL) {
2354 RETURN_FALSE;
2355 }
2356
2357 if (UNEXPECTED(!instanceof_function(Z_OBJCE_P(other), dom_node_class_entry) && !instanceof_function(Z_OBJCE_P(other), dom_namespace_node_class_entry))) {
2358 zend_argument_type_error(1, "must be of type DOMNode|DOMNameSpaceNode|null, %s given", zend_zval_value_name(other));
2359 RETURN_THROWS();
2360 }
2361
2362 DOM_GET_OBJ(otherp, other, xmlNodePtr, unused_intern);
2363 DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, unused_intern);
2364
2365 RETURN_BOOL(dom_node_contains(thisp, otherp));
2366 }
2367
PHP_METHOD(Dom_Node,contains)2368 PHP_METHOD(Dom_Node, contains)
2369 {
2370 zval *other, *id;
2371 xmlNodePtr otherp, thisp;
2372 dom_object *unused_intern;
2373
2374 ZEND_PARSE_PARAMETERS_START(1, 1)
2375 Z_PARAM_OBJECT_OF_CLASS_OR_NULL(other, dom_modern_node_class_entry)
2376 ZEND_PARSE_PARAMETERS_END();
2377
2378 if (other == NULL) {
2379 RETURN_FALSE;
2380 }
2381
2382 DOM_GET_OBJ(otherp, other, xmlNodePtr, unused_intern);
2383 DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, unused_intern);
2384
2385 RETURN_BOOL(dom_node_contains(thisp, otherp));
2386 }
2387 /* }}} */
2388
2389 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-getrootnode
2390 Since:
2391 */
PHP_METHOD(DOMNode,getRootNode)2392 PHP_METHOD(DOMNode, getRootNode)
2393 {
2394 zval *id;
2395 xmlNodePtr thisp;
2396 dom_object *intern;
2397 /* Unused now because we don't support the shadow DOM nodes. Options only influence shadow DOM nodes. */
2398 zval *options;
2399
2400 ZEND_PARSE_PARAMETERS_START(0, 1)
2401 Z_PARAM_OPTIONAL
2402 Z_PARAM_ARRAY_OR_NULL(options)
2403 ZEND_PARSE_PARAMETERS_END();
2404
2405 DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
2406
2407 while (thisp->parent) {
2408 thisp = thisp->parent;
2409 }
2410
2411 DOM_RET_OBJ(thisp, intern);
2412 }
2413 /* }}} */
2414
2415 /* {{{ URL: https://dom.spec.whatwg.org/#dom-node-comparedocumentposition (last check date 2023-07-24)
2416 Since:
2417 */
2418
2419 #define DOCUMENT_POSITION_DISCONNECTED 0x01
2420 #define DOCUMENT_POSITION_PRECEDING 0x02
2421 #define DOCUMENT_POSITION_FOLLOWING 0x04
2422 #define DOCUMENT_POSITION_CONTAINS 0x08
2423 #define DOCUMENT_POSITION_CONTAINED_BY 0x10
2424 #define DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 0x20
2425
dom_node_compare_document_position(INTERNAL_FUNCTION_PARAMETERS,zend_class_entry * node_ce)2426 static void dom_node_compare_document_position(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *node_ce)
2427 {
2428 zval *id, *node_zval;
2429 xmlNodePtr other, this;
2430 dom_object *this_intern, *other_intern;
2431
2432 ZEND_PARSE_PARAMETERS_START(1, 1)
2433 Z_PARAM_OBJECT_OF_CLASS(node_zval, node_ce)
2434 ZEND_PARSE_PARAMETERS_END();
2435
2436 DOM_GET_THIS_OBJ(this, id, xmlNodePtr, this_intern);
2437 DOM_GET_OBJ(other, node_zval, xmlNodePtr, other_intern);
2438
2439 /* Step 1 */
2440 if (this == other) {
2441 RETURN_LONG(0);
2442 }
2443
2444 /* Step 2 */
2445 xmlNodePtr node1 = other;
2446 xmlNodePtr node2 = this;
2447
2448 /* Step 3 */
2449 xmlNodePtr attr1 = NULL;
2450 xmlNodePtr attr2 = NULL;
2451
2452 /* Step 4 */
2453 if (node1->type == XML_ATTRIBUTE_NODE) {
2454 attr1 = node1;
2455 node1 = attr1->parent;
2456 }
2457
2458 /* Step 5 */
2459 if (node2->type == XML_ATTRIBUTE_NODE) {
2460 /* 5.1 */
2461 attr2 = node2;
2462 node2 = attr2->parent;
2463
2464 /* 5.2 */
2465 if (attr1 != NULL && node1 != NULL && node2 == node1) {
2466 for (const xmlAttr *attr = node2->properties; attr != NULL; attr = attr->next) {
2467 if (php_dom_is_equal_attr(attr, (const xmlAttr *) attr1)) {
2468 RETURN_LONG(DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_PRECEDING);
2469 } else if (php_dom_is_equal_attr(attr, (const xmlAttr *) attr2)) {
2470 RETURN_LONG(DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_FOLLOWING);
2471 }
2472 }
2473 }
2474 }
2475
2476 /* Step 6 */
2477 /* We first check the first condition,
2478 * and as we need the root later anyway we'll cache the root and perform the root check after this if. */
2479 if (node1 == NULL || node2 == NULL) {
2480 goto disconnected;
2481 }
2482 bool node2_is_ancestor_of_node1 = false;
2483 size_t node1_depth = 0;
2484 xmlNodePtr node1_root = node1;
2485 while (node1_root->parent) {
2486 node1_root = node1_root->parent;
2487 if (node1_root == node2) {
2488 node2_is_ancestor_of_node1 = true;
2489 }
2490 node1_depth++;
2491 }
2492 bool node1_is_ancestor_of_node2 = false;
2493 size_t node2_depth = 0;
2494 xmlNodePtr node2_root = node2;
2495 while (node2_root->parent) {
2496 node2_root = node2_root->parent;
2497 if (node2_root == node1) {
2498 node1_is_ancestor_of_node2 = true;
2499 }
2500 node2_depth++;
2501 }
2502 /* Second condition from step 6 */
2503 if (node1_root != node2_root) {
2504 goto disconnected;
2505 }
2506
2507 /* Step 7 */
2508 if ((node1_is_ancestor_of_node2 && attr1 == NULL) || (node1 == node2 && attr2 != NULL)) {
2509 RETURN_LONG(DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING);
2510 }
2511
2512 /* Step 8 */
2513 if ((node2_is_ancestor_of_node1 && attr2 == NULL) || (node1 == node2 && attr1 != NULL)) {
2514 RETURN_LONG(DOCUMENT_POSITION_CONTAINED_BY | DOCUMENT_POSITION_FOLLOWING);
2515 }
2516
2517 /* Special case: comparing children and attributes.
2518 * They belong to a different tree and are therefore hard to compare, but spec demands attributes to precede children
2519 * according to the pre-order depth-first search ordering.
2520 * Because their tree is different, the node parents only meet at the common element instead of earlier.
2521 * Therefore, it seems that one is the ancestor of the other. */
2522 if (node1_is_ancestor_of_node2) {
2523 ZEND_ASSERT(attr1 != NULL); /* Would've been handled in step 7 otherwise */
2524 RETURN_LONG(DOCUMENT_POSITION_PRECEDING);
2525 } else if (node2_is_ancestor_of_node1) {
2526 ZEND_ASSERT(attr2 != NULL); /* Would've been handled in step 8 otherwise */
2527 RETURN_LONG(DOCUMENT_POSITION_FOLLOWING);
2528 }
2529
2530 /* Step 9 */
2531
2532 /* We'll use the following strategy (which was already prepared during step 6) to implement this efficiently:
2533 * 1. Move nodes upwards such that they are at the same depth.
2534 * 2. Then we move both nodes upwards simultaneously until their parents are equal.
2535 * 3. If we then move node1 to the next entry repeatedly and we encounter node2,
2536 * then we know node1 precedes node2. Otherwise, node2 must precede node1. */
2537 /* 1. */
2538 if (node1_depth > node2_depth) {
2539 do {
2540 node1 = node1->parent;
2541 node1_depth--;
2542 } while (node1_depth > node2_depth);
2543 } else if (node2_depth > node1_depth) {
2544 do {
2545 node2 = node2->parent;
2546 node2_depth--;
2547 } while (node2_depth > node1_depth);
2548 }
2549 /* 2. */
2550 while (node1->parent != node2->parent) {
2551 node1 = node1->parent;
2552 node2 = node2->parent;
2553 }
2554 /* 3. */
2555 ZEND_ASSERT(node1 != node2);
2556 ZEND_ASSERT(node1 != NULL);
2557 ZEND_ASSERT(node2 != NULL);
2558 do {
2559 node1 = node1->next;
2560 if (node1 == node2) {
2561 RETURN_LONG(DOCUMENT_POSITION_PRECEDING);
2562 }
2563 } while (node1 != NULL);
2564
2565 /* Step 10 */
2566 RETURN_LONG(DOCUMENT_POSITION_FOLLOWING);
2567
2568 disconnected:;
2569 zend_long ordering;
2570 if (node1 == node2) {
2571 /* Degenerate case, they're both NULL, but the ordering must be consistent... */
2572 ZEND_ASSERT(node1 == NULL);
2573 ordering = other_intern < this_intern ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
2574 } else {
2575 ordering = node1 < node2 ? DOCUMENT_POSITION_PRECEDING : DOCUMENT_POSITION_FOLLOWING;
2576 }
2577 RETURN_LONG(DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | ordering);
2578 }
2579
PHP_METHOD(DOMNode,compareDocumentPosition)2580 PHP_METHOD(DOMNode, compareDocumentPosition)
2581 {
2582 dom_node_compare_document_position(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_node_class_entry);
2583 }
2584
PHP_METHOD(Dom_Node,compareDocumentPosition)2585 PHP_METHOD(Dom_Node, compareDocumentPosition)
2586 {
2587 dom_node_compare_document_position(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_node_class_entry);
2588 }
2589 /* }}} */
2590
2591 /**
2592 * We want to block the serialization and unserialization of DOM classes.
2593 * However, using @not-serializable makes the child classes also not serializable, even if the user implements the methods.
2594 * So instead, we implement the methods wherein we throw exceptions.
2595 * The reason we choose these methods is because:
2596 * - If the user implements __serialize / __unserialize, the respective throwing methods are not called.
2597 * - If the user implements __sleep / __wakeup, then it's also not a problem because they will not enter the throwing methods.
2598 */
2599
PHP_METHOD(Dom_Node,__construct)2600 PHP_METHOD(Dom_Node, __construct)
2601 {
2602 zend_throw_error(NULL, "Cannot directly construct %s, use document methods instead", ZSTR_VAL(Z_OBJCE_P(ZEND_THIS)->name));
2603 }
2604
PHP_METHOD(DOMNode,__sleep)2605 PHP_METHOD(DOMNode, __sleep)
2606 {
2607 ZEND_PARSE_PARAMETERS_NONE();
2608
2609 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));
2610 RETURN_THROWS();
2611 }
2612
PHP_METHOD(DOMNode,__wakeup)2613 PHP_METHOD(DOMNode, __wakeup)
2614 {
2615 ZEND_PARSE_PARAMETERS_NONE();
2616
2617 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));
2618 RETURN_THROWS();
2619 }
2620
2621 #endif
2622