1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Christian Stocker <chregu@php.net> |
14 | Rob Richards <rrichards@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "php.h"
23
24 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
25 #include "php_dom.h"
26
27 /*
28 * class DOMNode
29 *
30 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1950641247
31 * Since:
32 */
33
34 /* {{{ nodeName string
35 readonly=yes
36 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D095
37 Since:
38 */
dom_node_node_name_read(dom_object * obj,zval * retval)39 int dom_node_node_name_read(dom_object *obj, zval *retval)
40 {
41 xmlNode *nodep;
42 xmlNsPtr ns;
43 char *str = NULL;
44 xmlChar *qname = NULL;
45
46 nodep = dom_object_get_node(obj);
47
48 if (nodep == NULL) {
49 php_dom_throw_error(INVALID_STATE_ERR, 0);
50 return FAILURE;
51 }
52
53 switch (nodep->type) {
54 case XML_ATTRIBUTE_NODE:
55 case XML_ELEMENT_NODE:
56 ns = nodep->ns;
57 if (ns != NULL && ns->prefix) {
58 qname = xmlStrdup(ns->prefix);
59 qname = xmlStrcat(qname, (xmlChar *) ":");
60 qname = xmlStrcat(qname, nodep->name);
61 str = (char *) qname;
62 } else {
63 str = (char *) nodep->name;
64 }
65 break;
66 case XML_NAMESPACE_DECL:
67 ns = nodep->ns;
68 if (ns != NULL && ns->prefix) {
69 qname = xmlStrdup((xmlChar *) "xmlns");
70 qname = xmlStrcat(qname, (xmlChar *) ":");
71 qname = xmlStrcat(qname, nodep->name);
72 str = (char *) qname;
73 } else {
74 str = (char *) nodep->name;
75 }
76 break;
77 case XML_DOCUMENT_TYPE_NODE:
78 case XML_DTD_NODE:
79 case XML_PI_NODE:
80 case XML_ENTITY_DECL:
81 case XML_ENTITY_REF_NODE:
82 case XML_NOTATION_NODE:
83 str = (char *) nodep->name;
84 break;
85 case XML_CDATA_SECTION_NODE:
86 str = "#cdata-section";
87 break;
88 case XML_COMMENT_NODE:
89 str = "#comment";
90 break;
91 case XML_HTML_DOCUMENT_NODE:
92 case XML_DOCUMENT_NODE:
93 str = "#document";
94 break;
95 case XML_DOCUMENT_FRAG_NODE:
96 str = "#document-fragment";
97 break;
98 case XML_TEXT_NODE:
99 str = "#text";
100 break;
101 EMPTY_SWITCH_DEFAULT_CASE();
102 }
103
104 if (str != NULL) {
105 ZVAL_STRING(retval, str);
106 } else {
107 ZVAL_EMPTY_STRING(retval);
108 }
109
110 if (qname != NULL) {
111 xmlFree(qname);
112 }
113
114 return SUCCESS;
115
116 }
117
118 /* }}} */
119
120 /* {{{ nodeValue string
121 readonly=no
122 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D080
123 Since:
124 */
dom_node_node_value_read(dom_object * obj,zval * retval)125 int dom_node_node_value_read(dom_object *obj, zval *retval)
126 {
127 xmlNode *nodep = dom_object_get_node(obj);
128 char *str = NULL;
129
130 if (nodep == NULL) {
131 php_dom_throw_error(INVALID_STATE_ERR, 0);
132 return FAILURE;
133 }
134
135 /* Access to Element node is implemented as a convenience method */
136 switch (nodep->type) {
137 case XML_ATTRIBUTE_NODE:
138 case XML_TEXT_NODE:
139 case XML_ELEMENT_NODE:
140 case XML_COMMENT_NODE:
141 case XML_CDATA_SECTION_NODE:
142 case XML_PI_NODE:
143 str = (char *) xmlNodeGetContent(nodep);
144 break;
145 case XML_NAMESPACE_DECL:
146 str = (char *) xmlNodeGetContent(nodep->children);
147 break;
148 default:
149 str = NULL;
150 break;
151 }
152
153 if(str != NULL) {
154 ZVAL_STRING(retval, str);
155 xmlFree(str);
156 } else {
157 ZVAL_NULL(retval);
158 }
159
160 return SUCCESS;
161
162 }
163
dom_node_node_value_write(dom_object * obj,zval * newval)164 int dom_node_node_value_write(dom_object *obj, zval *newval)
165 {
166 xmlNode *nodep = dom_object_get_node(obj);
167 zend_string *str;
168
169 if (nodep == NULL) {
170 php_dom_throw_error(INVALID_STATE_ERR, 0);
171 return FAILURE;
172 }
173
174 str = zval_try_get_string(newval);
175 if (UNEXPECTED(!str)) {
176 return FAILURE;
177 }
178
179 /* Access to Element node is implemented as a convenience method */
180 switch (nodep->type) {
181 case XML_ELEMENT_NODE:
182 case XML_ATTRIBUTE_NODE:
183 if (nodep->children) {
184 node_list_unlink(nodep->children);
185 php_libxml_node_free_list((xmlNodePtr) nodep->children);
186 nodep->children = NULL;
187 }
188 case XML_TEXT_NODE:
189 case XML_COMMENT_NODE:
190 case XML_CDATA_SECTION_NODE:
191 case XML_PI_NODE:
192 xmlNodeSetContentLen(nodep, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
193 break;
194 default:
195 break;
196 }
197
198 zend_string_release_ex(str, 0);
199 return SUCCESS;
200 }
201
202 /* }}} */
203
204 /* {{{ nodeType int
205 readonly=yes
206 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-111237558
207 Since:
208 */
dom_node_node_type_read(dom_object * obj,zval * retval)209 int dom_node_node_type_read(dom_object *obj, zval *retval)
210 {
211 xmlNode *nodep;
212
213 nodep = dom_object_get_node(obj);
214
215 if (nodep == NULL) {
216 php_dom_throw_error(INVALID_STATE_ERR, 0);
217 return FAILURE;
218 }
219
220 /* Specs dictate that they are both type XML_DOCUMENT_TYPE_NODE */
221 if (nodep->type == XML_DTD_NODE) {
222 ZVAL_LONG(retval, XML_DOCUMENT_TYPE_NODE);
223 } else {
224 ZVAL_LONG(retval, nodep->type);
225 }
226
227 return SUCCESS;
228 }
229
230 /* }}} */
231
232 /* {{{ parentNode DomNode
233 readonly=yes
234 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1060184317
235 Since:
236 */
dom_node_parent_node_read(dom_object * obj,zval * retval)237 int dom_node_parent_node_read(dom_object *obj, zval *retval)
238 {
239 xmlNode *nodep, *nodeparent;
240
241 nodep = dom_object_get_node(obj);
242
243 if (nodep == NULL) {
244 php_dom_throw_error(INVALID_STATE_ERR, 0);
245 return FAILURE;
246 }
247
248 nodeparent = nodep->parent;
249 if (!nodeparent) {
250 ZVAL_NULL(retval);
251 return SUCCESS;
252 }
253
254 php_dom_create_object(nodeparent, retval, obj);
255 return SUCCESS;
256 }
257
258 /* }}} */
259
260 /* {{{ childNodes DomNodeList
261 readonly=yes
262 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1451460987
263 Since:
264 */
dom_node_child_nodes_read(dom_object * obj,zval * retval)265 int dom_node_child_nodes_read(dom_object *obj, zval *retval)
266 {
267 xmlNode *nodep = dom_object_get_node(obj);
268 dom_object *intern;
269
270 if (nodep == NULL) {
271 php_dom_throw_error(INVALID_STATE_ERR, 0);
272 return FAILURE;
273 }
274
275 php_dom_create_iterator(retval, DOM_NODELIST);
276 intern = Z_DOMOBJ_P(retval);
277 dom_namednode_iter(obj, XML_ELEMENT_NODE, intern, NULL, NULL, NULL);
278
279 return SUCCESS;
280 }
281 /* }}} */
282
283 /* {{{ firstChild DomNode
284 readonly=yes
285 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-169727388
286 Since:
287 */
dom_node_first_child_read(dom_object * obj,zval * retval)288 int dom_node_first_child_read(dom_object *obj, zval *retval)
289 {
290 xmlNode *nodep, *first = NULL;
291
292 nodep = dom_object_get_node(obj);
293
294 if (nodep == NULL) {
295 php_dom_throw_error(INVALID_STATE_ERR, 0);
296 return FAILURE;
297 }
298
299 if (dom_node_children_valid(nodep) == SUCCESS) {
300 first = nodep->children;
301 }
302
303 if (!first) {
304 ZVAL_NULL(retval);
305 return SUCCESS;
306 }
307
308 php_dom_create_object(first, retval, obj);
309 return SUCCESS;
310 }
311
312 /* }}} */
313
314 /* {{{ lastChild DomNode
315 readonly=yes
316 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-61AD09FB
317 Since:
318 */
dom_node_last_child_read(dom_object * obj,zval * retval)319 int dom_node_last_child_read(dom_object *obj, zval *retval)
320 {
321 xmlNode *nodep, *last = NULL;
322
323 nodep = dom_object_get_node(obj);
324
325 if (nodep == NULL) {
326 php_dom_throw_error(INVALID_STATE_ERR, 0);
327 return FAILURE;
328 }
329
330 if (dom_node_children_valid(nodep) == SUCCESS) {
331 last = nodep->last;
332 }
333
334 if (!last) {
335 ZVAL_NULL(retval);
336 return SUCCESS;
337 }
338
339 php_dom_create_object(last, retval, obj);
340 return SUCCESS;
341 }
342
343 /* }}} */
344
345 /* {{{ previousSibling DomNode
346 readonly=yes
347 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-640FB3C8
348 Since:
349 */
dom_node_previous_sibling_read(dom_object * obj,zval * retval)350 int dom_node_previous_sibling_read(dom_object *obj, zval *retval)
351 {
352 xmlNode *nodep, *prevsib;
353
354 nodep = dom_object_get_node(obj);
355
356 if (nodep == NULL) {
357 php_dom_throw_error(INVALID_STATE_ERR, 0);
358 return FAILURE;
359 }
360
361 prevsib = nodep->prev;
362 if (!prevsib) {
363 ZVAL_NULL(retval);
364 return SUCCESS;
365 }
366
367 php_dom_create_object(prevsib, retval, obj);
368 return SUCCESS;
369 }
370
371 /* }}} */
372
373 /* {{{ nextSibling DomNode
374 readonly=yes
375 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6AC54C2F
376 Since:
377 */
dom_node_next_sibling_read(dom_object * obj,zval * retval)378 int dom_node_next_sibling_read(dom_object *obj, zval *retval)
379 {
380 xmlNode *nodep, *nextsib;
381
382 nodep = dom_object_get_node(obj);
383
384 if (nodep == NULL) {
385 php_dom_throw_error(INVALID_STATE_ERR, 0);
386 return FAILURE;
387 }
388
389 nextsib = nodep->next;
390 if (!nextsib) {
391 ZVAL_NULL(retval);
392 return SUCCESS;
393 }
394
395 php_dom_create_object(nextsib, retval, obj);
396 return SUCCESS;
397 }
398
399 /* }}} */
400
401 /* {{{ previousElementSibling DomNode
402 readonly=yes
403 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-640FB3C8
404 Since:
405 */
dom_node_previous_element_sibling_read(dom_object * obj,zval * retval)406 int dom_node_previous_element_sibling_read(dom_object *obj, zval *retval)
407 {
408 xmlNode *nodep, *prevsib;
409
410 nodep = dom_object_get_node(obj);
411
412 if (nodep == NULL) {
413 php_dom_throw_error(INVALID_STATE_ERR, 0);
414 return FAILURE;
415 }
416
417 prevsib = nodep->prev;
418
419 while (prevsib && prevsib->type != XML_ELEMENT_NODE) {
420 prevsib = prevsib->prev;
421 }
422
423 if (!prevsib) {
424 ZVAL_NULL(retval);
425 return SUCCESS;
426 }
427
428 php_dom_create_object(prevsib, retval, obj);
429 return SUCCESS;
430 }
431
432 /* }}} */
433
434 /* {{{ nextElementSibling DomNode
435 readonly=yes
436 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6AC54C2F
437 Since:
438 */
dom_node_next_element_sibling_read(dom_object * obj,zval * retval)439 int dom_node_next_element_sibling_read(dom_object *obj, zval *retval)
440 {
441 xmlNode *nodep, *nextsib;
442
443 nodep = dom_object_get_node(obj);
444
445 if (nodep == NULL) {
446 php_dom_throw_error(INVALID_STATE_ERR, 0);
447 return FAILURE;
448 }
449
450 nextsib = nodep->next;
451
452 while (nextsib != NULL && nextsib->type != XML_ELEMENT_NODE) {
453 nextsib = nextsib->next;
454 }
455
456 if (!nextsib) {
457 ZVAL_NULL(retval);
458 return SUCCESS;
459 }
460
461 php_dom_create_object(nextsib, retval, obj);
462 return SUCCESS;
463 }
464
465 /* }}} */
466
467 /* {{{ attributes DomNamedNodeMap
468 readonly=yes
469 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-84CF096
470 Since:
471 */
dom_node_attributes_read(dom_object * obj,zval * retval)472 int dom_node_attributes_read(dom_object *obj, zval *retval)
473 {
474 xmlNode *nodep = dom_object_get_node(obj);
475 dom_object *intern;
476
477 if (nodep == NULL) {
478 php_dom_throw_error(INVALID_STATE_ERR, 0);
479 return FAILURE;
480 }
481
482 if (nodep->type == XML_ELEMENT_NODE) {
483 php_dom_create_iterator(retval, DOM_NAMEDNODEMAP);
484 intern = Z_DOMOBJ_P(retval);
485 dom_namednode_iter(obj, XML_ATTRIBUTE_NODE, intern, NULL, NULL, NULL);
486 } else {
487 ZVAL_NULL(retval);
488 }
489
490 return SUCCESS;
491 }
492
493 /* }}} */
494
495 /* {{{ ownerDocument DomDocument
496 readonly=yes
497 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-node-ownerDoc
498 Since:
499 */
dom_node_owner_document_read(dom_object * obj,zval * retval)500 int dom_node_owner_document_read(dom_object *obj, zval *retval)
501 {
502 xmlNode *nodep = dom_object_get_node(obj);
503 xmlDocPtr docp;
504
505 if (nodep == NULL) {
506 php_dom_throw_error(INVALID_STATE_ERR, 0);
507 return FAILURE;
508 }
509
510 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
511 ZVAL_NULL(retval);
512 return SUCCESS;
513 }
514
515 docp = nodep->doc;
516 if (!docp) {
517 return FAILURE;
518 }
519
520 php_dom_create_object((xmlNodePtr) docp, retval, obj);
521 return SUCCESS;
522 }
523
524 /* }}} */
525
526 /* {{{ namespaceUri string
527 readonly=yes
528 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSname
529 Since: DOM Level 2
530 */
dom_node_namespace_uri_read(dom_object * obj,zval * retval)531 int dom_node_namespace_uri_read(dom_object *obj, zval *retval)
532 {
533 xmlNode *nodep = dom_object_get_node(obj);
534 char *str = NULL;
535
536 if (nodep == NULL) {
537 php_dom_throw_error(INVALID_STATE_ERR, 0);
538 return FAILURE;
539 }
540
541 switch (nodep->type) {
542 case XML_ELEMENT_NODE:
543 case XML_ATTRIBUTE_NODE:
544 case XML_NAMESPACE_DECL:
545 if (nodep->ns != NULL) {
546 str = (char *) nodep->ns->href;
547 }
548 break;
549 default:
550 str = NULL;
551 break;
552 }
553
554 if (str != NULL) {
555 ZVAL_STRING(retval, str);
556 } else {
557 ZVAL_NULL(retval);
558 }
559
560 return SUCCESS;
561 }
562
563 /* }}} */
564
565 /* {{{ prefix string
566 readonly=no
567 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSPrefix
568 Since: DOM Level 2
569 */
dom_node_prefix_read(dom_object * obj,zval * retval)570 int dom_node_prefix_read(dom_object *obj, zval *retval)
571 {
572 xmlNode *nodep = dom_object_get_node(obj);
573 xmlNsPtr ns;
574 char *str = NULL;
575
576 if (nodep == NULL) {
577 php_dom_throw_error(INVALID_STATE_ERR, 0);
578 return FAILURE;
579 }
580
581 switch (nodep->type) {
582 case XML_ELEMENT_NODE:
583 case XML_ATTRIBUTE_NODE:
584 case XML_NAMESPACE_DECL:
585 ns = nodep->ns;
586 if (ns != NULL && ns->prefix) {
587 str = (char *) ns->prefix;
588 }
589 break;
590 default:
591 str = NULL;
592 break;
593 }
594
595 if (str == NULL) {
596 ZVAL_EMPTY_STRING(retval);
597 } else {
598 ZVAL_STRING(retval, str);
599 }
600 return SUCCESS;
601
602 }
603
dom_node_prefix_write(dom_object * obj,zval * newval)604 int dom_node_prefix_write(dom_object *obj, zval *newval)
605 {
606 zend_string *str;
607 xmlNode *nodep, *nsnode = NULL;
608 xmlNsPtr ns = NULL, curns;
609 char *strURI;
610 char *prefix;
611
612 nodep = dom_object_get_node(obj);
613
614 if (nodep == NULL) {
615 php_dom_throw_error(INVALID_STATE_ERR, 0);
616 return FAILURE;
617 }
618
619 switch (nodep->type) {
620 case XML_ELEMENT_NODE:
621 nsnode = nodep;
622 case XML_ATTRIBUTE_NODE:
623 if (nsnode == NULL) {
624 nsnode = nodep->parent;
625 if (nsnode == NULL) {
626 nsnode = xmlDocGetRootElement(nodep->doc);
627 }
628 }
629 str = zval_try_get_string(newval);
630 if (UNEXPECTED(!str)) {
631 return FAILURE;
632 }
633
634 prefix = ZSTR_VAL(str);
635 if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) {
636 strURI = (char *) nodep->ns->href;
637 if (strURI == NULL ||
638 (!strcmp(prefix, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) ||
639 (nodep->type == XML_ATTRIBUTE_NODE && !strcmp(prefix, "xmlns") &&
640 strcmp(strURI, (char *) DOM_XMLNS_NAMESPACE)) ||
641 (nodep->type == XML_ATTRIBUTE_NODE && !strcmp((char *) nodep->name, "xmlns"))) {
642 ns = NULL;
643 } else {
644 curns = nsnode->nsDef;
645 while (curns != NULL) {
646 if (xmlStrEqual((xmlChar *)prefix, curns->prefix) && xmlStrEqual(nodep->ns->href, curns->href)) {
647 ns = curns;
648 break;
649 }
650 curns = curns->next;
651 }
652 if (ns == NULL) {
653 ns = xmlNewNs(nsnode, nodep->ns->href, (xmlChar *)prefix);
654 }
655 }
656
657 if (ns == NULL) {
658 zend_string_release_ex(str, 0);
659 php_dom_throw_error(NAMESPACE_ERR, dom_get_strict_error(obj->document));
660 return FAILURE;
661 }
662
663 xmlSetNs(nodep, ns);
664 }
665 zend_string_release_ex(str, 0);
666 break;
667 default:
668 break;
669 }
670
671 return SUCCESS;
672 }
673
674 /* }}} */
675
676 /* {{{ localName string
677 readonly=yes
678 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSLocalN
679 Since: DOM Level 2
680 */
dom_node_local_name_read(dom_object * obj,zval * retval)681 int dom_node_local_name_read(dom_object *obj, zval *retval)
682 {
683 xmlNode *nodep = dom_object_get_node(obj);
684
685 if (nodep == NULL) {
686 php_dom_throw_error(INVALID_STATE_ERR, 0);
687 return FAILURE;
688 }
689
690 if (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE || nodep->type == XML_NAMESPACE_DECL) {
691 ZVAL_STRING(retval, (char *) (nodep->name));
692 } else {
693 ZVAL_NULL(retval);
694 }
695
696 return SUCCESS;
697 }
698
699 /* }}} */
700
701 /* {{{ baseURI string
702 readonly=yes
703 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-baseURI
704 Since: DOM Level 3
705 */
dom_node_base_uri_read(dom_object * obj,zval * retval)706 int dom_node_base_uri_read(dom_object *obj, zval *retval)
707 {
708 xmlNode *nodep = dom_object_get_node(obj);
709 xmlChar *baseuri;
710
711 if (nodep == NULL) {
712 php_dom_throw_error(INVALID_STATE_ERR, 0);
713 return FAILURE;
714 }
715
716 baseuri = xmlNodeGetBase(nodep->doc, nodep);
717 if (baseuri) {
718 ZVAL_STRING(retval, (char *) (baseuri));
719 xmlFree(baseuri);
720 } else {
721 ZVAL_NULL(retval);
722 }
723
724 return SUCCESS;
725 }
726
727 /* }}} */
728
729 /* {{{ textContent string
730 readonly=no
731 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-textContent
732 Since: DOM Level 3
733 */
dom_node_text_content_read(dom_object * obj,zval * retval)734 int dom_node_text_content_read(dom_object *obj, zval *retval)
735 {
736 xmlNode *nodep = dom_object_get_node(obj);
737 char *str = NULL;
738
739 if (nodep == NULL) {
740 php_dom_throw_error(INVALID_STATE_ERR, 0);
741 return FAILURE;
742 }
743
744 str = (char *) xmlNodeGetContent(nodep);
745
746 if (str != NULL) {
747 ZVAL_STRING(retval, str);
748 xmlFree(str);
749 } else {
750 ZVAL_EMPTY_STRING(retval);
751 }
752
753 return SUCCESS;
754 }
755
dom_node_text_content_write(dom_object * obj,zval * newval)756 int dom_node_text_content_write(dom_object *obj, zval *newval)
757 {
758 xmlNode *nodep = dom_object_get_node(obj);
759 zend_string *str;
760
761 if (nodep == NULL) {
762 php_dom_throw_error(INVALID_STATE_ERR, 0);
763 return FAILURE;
764 }
765
766 str = zval_try_get_string(newval);
767 if (UNEXPECTED(!str)) {
768 return FAILURE;
769 }
770
771 if (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE) {
772 if (nodep->children) {
773 node_list_unlink(nodep->children);
774 php_libxml_node_free_list((xmlNodePtr) nodep->children);
775 nodep->children = NULL;
776 }
777 }
778
779 /* we have to use xmlNodeAddContent() to get the same behavior as with xmlNewText() */
780 xmlNodeSetContent(nodep, (xmlChar *) "");
781 xmlNodeAddContent(nodep, (xmlChar *) ZSTR_VAL(str));
782 zend_string_release_ex(str, 0);
783
784 return SUCCESS;
785 }
786
787 /* }}} */
788
_php_dom_insert_fragment(xmlNodePtr nodep,xmlNodePtr prevsib,xmlNodePtr nextsib,xmlNodePtr fragment,dom_object * intern,dom_object * childobj)789 static xmlNodePtr _php_dom_insert_fragment(xmlNodePtr nodep, xmlNodePtr prevsib, xmlNodePtr nextsib, xmlNodePtr fragment, dom_object *intern, dom_object *childobj) /* {{{ */
790 {
791 xmlNodePtr newchild, node;
792
793 newchild = fragment->children;
794
795 if (newchild) {
796 if (prevsib == NULL) {
797 nodep->children = newchild;
798 } else {
799 prevsib->next = newchild;
800 }
801 newchild->prev = prevsib;
802 if (nextsib == NULL) {
803 nodep->last = fragment->last;
804 } else {
805 fragment->last->next = nextsib;
806 nextsib->prev = fragment->last;
807 }
808
809 node = newchild;
810 while (node != NULL) {
811 node->parent = nodep;
812 if (node->doc != nodep->doc) {
813 xmlSetTreeDoc(node, nodep->doc);
814 if (node->_private != NULL) {
815 childobj = node->_private;
816 childobj->document = intern->document;
817 php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
818 }
819 }
820 if (node == fragment->last) {
821 break;
822 }
823 node = node->next;
824 }
825
826 fragment->children = NULL;
827 fragment->last = NULL;
828 }
829
830 return newchild;
831 }
832 /* }}} */
833
834 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727
835 Since:
836 */
PHP_METHOD(DOMNode,insertBefore)837 PHP_METHOD(DOMNode, insertBefore)
838 {
839 zval *id, *node, *ref = NULL;
840 xmlNodePtr child, new_child, parentp, refp;
841 dom_object *intern, *childobj, *refpobj;
842 int ret, stricterror;
843
844 id = ZEND_THIS;
845 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|O!", &node, dom_node_class_entry, &ref, dom_node_class_entry) == FAILURE) {
846 RETURN_THROWS();
847 }
848
849 DOM_GET_OBJ(parentp, id, xmlNodePtr, intern);
850
851 if (dom_node_children_valid(parentp) == FAILURE) {
852 RETURN_FALSE;
853 }
854
855 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
856
857 new_child = NULL;
858
859 stricterror = dom_get_strict_error(intern->document);
860
861 if (dom_node_is_read_only(parentp) == SUCCESS ||
862 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
863 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
864 RETURN_FALSE;
865 }
866
867 if (dom_hierarchy(parentp, child) == FAILURE) {
868 php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
869 RETURN_FALSE;
870 }
871
872 if (child->doc != parentp->doc && child->doc != NULL) {
873 php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
874 RETURN_FALSE;
875 }
876
877 if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
878 /* TODO Drop Warning? */
879 php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
880 RETURN_FALSE;
881 }
882
883 if (child->doc == NULL && parentp->doc != NULL) {
884 childobj->document = intern->document;
885 php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
886 }
887
888 if (ref != NULL) {
889 DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj);
890 if (refp->parent != parentp) {
891 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
892 RETURN_FALSE;
893 }
894
895 if (child->parent != NULL) {
896 xmlUnlinkNode(child);
897 }
898
899 if (child->type == XML_TEXT_NODE && (refp->type == XML_TEXT_NODE ||
900 (refp->prev != NULL && refp->prev->type == XML_TEXT_NODE))) {
901 if (child->doc == NULL) {
902 xmlSetTreeDoc(child, parentp->doc);
903 }
904 new_child = child;
905 new_child->parent = refp->parent;
906 new_child->next = refp;
907 new_child->prev = refp->prev;
908 refp->prev = new_child;
909 if (new_child->prev != NULL) {
910 new_child->prev->next = new_child;
911 }
912 if (new_child->parent != NULL) {
913 if (new_child->parent->children == refp) {
914 new_child->parent->children = new_child;
915 }
916 }
917
918 } else if (child->type == XML_ATTRIBUTE_NODE) {
919 xmlAttrPtr lastattr;
920
921 if (child->ns == NULL)
922 lastattr = xmlHasProp(refp->parent, child->name);
923 else
924 lastattr = xmlHasNsProp(refp->parent, child->name, child->ns->href);
925 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
926 if (lastattr != (xmlAttrPtr) child) {
927 xmlUnlinkNode((xmlNodePtr) lastattr);
928 php_libxml_node_free_resource((xmlNodePtr) lastattr);
929 } else {
930 DOM_RET_OBJ(child, &ret, intern);
931 return;
932 }
933 }
934 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
935 new_child = _php_dom_insert_fragment(parentp, refp->prev, refp, child, intern, childobj);
936 }
937
938 if (new_child == NULL) {
939 new_child = xmlAddPrevSibling(refp, child);
940 }
941 } else {
942 if (child->parent != NULL){
943 xmlUnlinkNode(child);
944 }
945 if (child->type == XML_TEXT_NODE && parentp->last != NULL && parentp->last->type == XML_TEXT_NODE) {
946 child->parent = parentp;
947 if (child->doc == NULL) {
948 xmlSetTreeDoc(child, parentp->doc);
949 }
950 new_child = child;
951 if (parentp->children == NULL) {
952 parentp->children = child;
953 parentp->last = child;
954 } else {
955 child = parentp->last;
956 child->next = new_child;
957 new_child->prev = child;
958 parentp->last = new_child;
959 }
960 } else if (child->type == XML_ATTRIBUTE_NODE) {
961 xmlAttrPtr lastattr;
962
963 if (child->ns == NULL)
964 lastattr = xmlHasProp(parentp, child->name);
965 else
966 lastattr = xmlHasNsProp(parentp, child->name, child->ns->href);
967 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
968 if (lastattr != (xmlAttrPtr) child) {
969 xmlUnlinkNode((xmlNodePtr) lastattr);
970 php_libxml_node_free_resource((xmlNodePtr) lastattr);
971 } else {
972 DOM_RET_OBJ(child, &ret, intern);
973 return;
974 }
975 }
976 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
977 new_child = _php_dom_insert_fragment(parentp, parentp->last, NULL, child, intern, childobj);
978 }
979 if (new_child == NULL) {
980 new_child = xmlAddChild(parentp, child);
981 }
982 }
983
984 if (NULL == new_child) {
985 zend_throw_error(NULL, "Cannot add newnode as the previous sibling of refnode");
986 RETURN_THROWS();
987 }
988
989 dom_reconcile_ns(parentp->doc, new_child);
990
991 DOM_RET_OBJ(new_child, &ret, intern);
992
993 }
994 /* }}} end dom_node_insert_before */
995
996 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-785887307
997 Since:
998 */
PHP_METHOD(DOMNode,replaceChild)999 PHP_METHOD(DOMNode, replaceChild)
1000 {
1001 zval *id, *newnode, *oldnode;
1002 xmlNodePtr children, newchild, oldchild, nodep;
1003 dom_object *intern, *newchildobj, *oldchildobj;
1004 int foundoldchild = 0, stricterror;
1005 bool replacedoctype = false;
1006
1007 int ret;
1008
1009 id = ZEND_THIS;
1010 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &newnode, dom_node_class_entry, &oldnode, dom_node_class_entry) == FAILURE) {
1011 RETURN_THROWS();
1012 }
1013
1014 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1015
1016 if (dom_node_children_valid(nodep) == FAILURE) {
1017 RETURN_FALSE;
1018 }
1019
1020 DOM_GET_OBJ(newchild, newnode, xmlNodePtr, newchildobj);
1021 DOM_GET_OBJ(oldchild, oldnode, xmlNodePtr, oldchildobj);
1022
1023 children = nodep->children;
1024 if (!children) {
1025 RETURN_FALSE;
1026 }
1027
1028 stricterror = dom_get_strict_error(intern->document);
1029
1030 if (dom_node_is_read_only(nodep) == SUCCESS ||
1031 (newchild->parent != NULL && dom_node_is_read_only(newchild->parent) == SUCCESS)) {
1032 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1033 RETURN_FALSE;
1034 }
1035
1036 if (newchild->doc != nodep->doc && newchild->doc != NULL) {
1037 php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1038 RETURN_FALSE;
1039 }
1040
1041 if (dom_hierarchy(nodep, newchild) == FAILURE) {
1042 php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1043 RETURN_FALSE;
1044 }
1045
1046 /* check for the old child and whether the new child is already a child */
1047 while (children) {
1048 if (children == oldchild) {
1049 foundoldchild = 1;
1050 break;
1051 }
1052 children = children->next;
1053 }
1054
1055 if (foundoldchild) {
1056 if (newchild->type == XML_DOCUMENT_FRAG_NODE) {
1057 xmlNodePtr prevsib, nextsib;
1058 prevsib = oldchild->prev;
1059 nextsib = oldchild->next;
1060
1061 xmlUnlinkNode(oldchild);
1062
1063 newchild = _php_dom_insert_fragment(nodep, prevsib, nextsib, newchild, intern, newchildobj);
1064 if (newchild) {
1065 dom_reconcile_ns(nodep->doc, newchild);
1066 }
1067 } else if (oldchild != newchild) {
1068 xmlDtdPtr intSubset = xmlGetIntSubset(nodep->doc);
1069 replacedoctype = (intSubset == (xmlDtd *) oldchild);
1070
1071 if (newchild->doc == NULL && nodep->doc != NULL) {
1072 xmlSetTreeDoc(newchild, nodep->doc);
1073 newchildobj->document = intern->document;
1074 php_libxml_increment_doc_ref((php_libxml_node_object *)newchildobj, NULL);
1075 }
1076
1077 xmlReplaceNode(oldchild, newchild);
1078 dom_reconcile_ns(nodep->doc, newchild);
1079
1080 if (replacedoctype) {
1081 nodep->doc->intSubset = (xmlDtd *) newchild;
1082 }
1083 }
1084 DOM_RET_OBJ(oldchild, &ret, intern);
1085 return;
1086 } else {
1087 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1088 RETURN_FALSE;
1089 }
1090 }
1091 /* }}} end dom_node_replace_child */
1092
1093 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1734834066
1094 Since:
1095 */
PHP_METHOD(DOMNode,removeChild)1096 PHP_METHOD(DOMNode, removeChild)
1097 {
1098 zval *id, *node;
1099 xmlNodePtr children, child, nodep;
1100 dom_object *intern, *childobj;
1101 int ret, stricterror;
1102
1103 id = ZEND_THIS;
1104 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &node, dom_node_class_entry) == FAILURE) {
1105 RETURN_THROWS();
1106 }
1107
1108 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1109
1110 if (dom_node_children_valid(nodep) == FAILURE) {
1111 RETURN_FALSE;
1112 }
1113
1114 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1115
1116 stricterror = dom_get_strict_error(intern->document);
1117
1118 if (dom_node_is_read_only(nodep) == SUCCESS ||
1119 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
1120 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1121 RETURN_FALSE;
1122 }
1123
1124 children = nodep->children;
1125 if (!children) {
1126 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1127 RETURN_FALSE;
1128 }
1129
1130 while (children) {
1131 if (children == child) {
1132 xmlUnlinkNode(child);
1133 DOM_RET_OBJ(child, &ret, intern);
1134 return;
1135 }
1136 children = children->next;
1137 }
1138
1139 php_dom_throw_error(NOT_FOUND_ERR, stricterror);
1140 RETURN_FALSE;
1141 }
1142 /* }}} end dom_node_remove_child */
1143
1144 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-184E7107
1145 Since:
1146 */
PHP_METHOD(DOMNode,appendChild)1147 PHP_METHOD(DOMNode, appendChild)
1148 {
1149 zval *id, *node;
1150 xmlNodePtr child, nodep, new_child = NULL;
1151 dom_object *intern, *childobj;
1152 int ret, stricterror;
1153
1154 id = ZEND_THIS;
1155 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &node, dom_node_class_entry) == FAILURE) {
1156 RETURN_THROWS();
1157 }
1158
1159 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1160
1161 if (dom_node_children_valid(nodep) == FAILURE) {
1162 RETURN_FALSE;
1163 }
1164
1165 DOM_GET_OBJ(child, node, xmlNodePtr, childobj);
1166
1167 stricterror = dom_get_strict_error(intern->document);
1168
1169 if (dom_node_is_read_only(nodep) == SUCCESS ||
1170 (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
1171 php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1172 RETURN_FALSE;
1173 }
1174
1175 if (dom_hierarchy(nodep, child) == FAILURE) {
1176 php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1177 RETURN_FALSE;
1178 }
1179
1180 if (!(child->doc == NULL || child->doc == nodep->doc)) {
1181 php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1182 RETURN_FALSE;
1183 }
1184
1185 if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
1186 /* TODO Drop Warning? */
1187 php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
1188 RETURN_FALSE;
1189 }
1190
1191 if (child->doc == NULL && nodep->doc != NULL) {
1192 childobj->document = intern->document;
1193 php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
1194 }
1195
1196 if (child->parent != NULL){
1197 xmlUnlinkNode(child);
1198 }
1199
1200 if (child->type == XML_TEXT_NODE && nodep->last != NULL && nodep->last->type == XML_TEXT_NODE) {
1201 child->parent = nodep;
1202 if (child->doc == NULL) {
1203 xmlSetTreeDoc(child, nodep->doc);
1204 }
1205 new_child = child;
1206 if (nodep->children == NULL) {
1207 nodep->children = child;
1208 nodep->last = child;
1209 } else {
1210 child = nodep->last;
1211 child->next = new_child;
1212 new_child->prev = child;
1213 nodep->last = new_child;
1214 }
1215 } else if (child->type == XML_ATTRIBUTE_NODE) {
1216 xmlAttrPtr lastattr;
1217
1218 if (child->ns == NULL)
1219 lastattr = xmlHasProp(nodep, child->name);
1220 else
1221 lastattr = xmlHasNsProp(nodep, child->name, child->ns->href);
1222 if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
1223 if (lastattr != (xmlAttrPtr) child) {
1224 xmlUnlinkNode((xmlNodePtr) lastattr);
1225 php_libxml_node_free_resource((xmlNodePtr) lastattr);
1226 }
1227 }
1228 } else if (child->type == XML_DOCUMENT_FRAG_NODE) {
1229 new_child = _php_dom_insert_fragment(nodep, nodep->last, NULL, child, intern, childobj);
1230 }
1231
1232 if (new_child == NULL) {
1233 new_child = xmlAddChild(nodep, child);
1234 if (new_child == NULL) {
1235 // TODO Convert to Error?
1236 php_error_docref(NULL, E_WARNING, "Couldn't append node");
1237 RETURN_FALSE;
1238 }
1239 }
1240
1241 dom_reconcile_ns(nodep->doc, new_child);
1242
1243 DOM_RET_OBJ(new_child, &ret, intern);
1244 }
1245 /* }}} end dom_node_append_child */
1246
1247 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-810594187
1248 Since:
1249 */
PHP_METHOD(DOMNode,hasChildNodes)1250 PHP_METHOD(DOMNode, hasChildNodes)
1251 {
1252 zval *id;
1253 xmlNode *nodep;
1254 dom_object *intern;
1255
1256 id = ZEND_THIS;
1257 if (zend_parse_parameters_none() == FAILURE) {
1258 RETURN_THROWS();
1259 }
1260
1261 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1262
1263 if (dom_node_children_valid(nodep) == FAILURE) {
1264 RETURN_FALSE;
1265 }
1266
1267 if (nodep->children) {
1268 RETURN_TRUE;
1269 } else {
1270 RETURN_FALSE;
1271 }
1272 }
1273 /* }}} end dom_node_has_child_nodes */
1274
1275 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3A0ED0A4
1276 Since:
1277 */
PHP_METHOD(DOMNode,cloneNode)1278 PHP_METHOD(DOMNode, cloneNode)
1279 {
1280 zval *id;
1281 xmlNode *n, *node;
1282 int ret;
1283 dom_object *intern;
1284 zend_bool recursive = 0;
1285
1286 id = ZEND_THIS;
1287 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &recursive) == FAILURE) {
1288 RETURN_THROWS();
1289 }
1290
1291 DOM_GET_OBJ(n, id, xmlNodePtr, intern);
1292
1293 node = xmlDocCopyNode(n, n->doc, recursive);
1294
1295 if (!node) {
1296 RETURN_FALSE;
1297 }
1298
1299 /* When deep is false Element nodes still require the attributes
1300 Following taken from libxml as xmlDocCopyNode doesn't do this */
1301 if (n->type == XML_ELEMENT_NODE && recursive == 0) {
1302 if (n->nsDef != NULL) {
1303 node->nsDef = xmlCopyNamespaceList(n->nsDef);
1304 }
1305 if (n->ns != NULL) {
1306 xmlNsPtr ns;
1307 ns = xmlSearchNs(n->doc, node, n->ns->prefix);
1308 if (ns == NULL) {
1309 ns = xmlSearchNs(n->doc, n, n->ns->prefix);
1310 if (ns != NULL) {
1311 xmlNodePtr root = node;
1312
1313 while (root->parent != NULL) {
1314 root = root->parent;
1315 }
1316 node->ns = xmlNewNs(root, ns->href, ns->prefix);
1317 }
1318 } else {
1319 node->ns = ns;
1320 }
1321 }
1322 if (n->properties != NULL) {
1323 node->properties = xmlCopyPropList(node, n->properties);
1324 }
1325 }
1326
1327 /* If document cloned we want a new document proxy */
1328 if (node->doc != n->doc) {
1329 intern = NULL;
1330 }
1331
1332 DOM_RET_OBJ(node, &ret, intern);
1333 }
1334 /* }}} end dom_node_clone_node */
1335
1336 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-normalize
1337 Since:
1338 */
PHP_METHOD(DOMNode,normalize)1339 PHP_METHOD(DOMNode, normalize)
1340 {
1341 zval *id;
1342 xmlNode *nodep;
1343 dom_object *intern;
1344
1345 id = ZEND_THIS;
1346 if (zend_parse_parameters_none() == FAILURE) {
1347 RETURN_THROWS();
1348 }
1349
1350 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1351
1352 dom_normalize(nodep);
1353
1354 }
1355 /* }}} end dom_node_normalize */
1356
1357 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Level-2-Core-Node-supports
1358 Since: DOM Level 2
1359 */
PHP_METHOD(DOMNode,isSupported)1360 PHP_METHOD(DOMNode, isSupported)
1361 {
1362 size_t feature_len, version_len;
1363 char *feature, *version;
1364
1365 if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
1366 RETURN_THROWS();
1367 }
1368
1369 if (dom_has_feature(feature, version)) {
1370 RETURN_TRUE;
1371 } else {
1372 RETURN_FALSE;
1373 }
1374 }
1375 /* }}} end dom_node_is_supported */
1376
1377 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeHasAttrs
1378 Since: DOM Level 2
1379 */
PHP_METHOD(DOMNode,hasAttributes)1380 PHP_METHOD(DOMNode, hasAttributes)
1381 {
1382 zval *id;
1383 xmlNode *nodep;
1384 dom_object *intern;
1385
1386 id = ZEND_THIS;
1387 if (zend_parse_parameters_none() == FAILURE) {
1388 RETURN_THROWS();
1389 }
1390
1391 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1392
1393 if (nodep->type != XML_ELEMENT_NODE)
1394 RETURN_FALSE;
1395
1396 if (nodep->properties) {
1397 RETURN_TRUE;
1398 } else {
1399 RETURN_FALSE;
1400 }
1401 }
1402 /* }}} end dom_node_has_attributes */
1403
1404 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-isSameNode
1405 Since: DOM Level 3
1406 */
PHP_METHOD(DOMNode,isSameNode)1407 PHP_METHOD(DOMNode, isSameNode)
1408 {
1409 zval *id, *node;
1410 xmlNodePtr nodeotherp, nodep;
1411 dom_object *intern, *nodeotherobj;
1412
1413 id = ZEND_THIS;
1414 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &node, dom_node_class_entry) == FAILURE) {
1415 RETURN_THROWS();
1416 }
1417
1418 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1419
1420 DOM_GET_OBJ(nodeotherp, node, xmlNodePtr, nodeotherobj);
1421
1422 if (nodep == nodeotherp) {
1423 RETURN_TRUE;
1424 } else {
1425 RETURN_FALSE;
1426 }
1427 }
1428 /* }}} end dom_node_is_same_node */
1429
1430 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-lookupNamespacePrefix
1431 Since: DOM Level 3
1432 */
PHP_METHOD(DOMNode,lookupPrefix)1433 PHP_METHOD(DOMNode, lookupPrefix)
1434 {
1435 zval *id;
1436 xmlNodePtr nodep, lookupp = NULL;
1437 dom_object *intern;
1438 xmlNsPtr nsptr;
1439 size_t uri_len = 0;
1440 char *uri;
1441
1442 id = ZEND_THIS;
1443 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uri, &uri_len) == FAILURE) {
1444 RETURN_THROWS();
1445 }
1446
1447 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1448
1449 if (uri_len > 0) {
1450 switch (nodep->type) {
1451 case XML_ELEMENT_NODE:
1452 lookupp = nodep;
1453 break;
1454 case XML_DOCUMENT_NODE:
1455 case XML_HTML_DOCUMENT_NODE:
1456 lookupp = xmlDocGetRootElement((xmlDocPtr) nodep);
1457 break;
1458 case XML_ENTITY_NODE :
1459 case XML_NOTATION_NODE:
1460 case XML_DOCUMENT_FRAG_NODE:
1461 case XML_DOCUMENT_TYPE_NODE:
1462 case XML_DTD_NODE:
1463 RETURN_NULL();
1464 break;
1465 default:
1466 lookupp = nodep->parent;
1467 }
1468
1469 if (lookupp != NULL) {
1470 nsptr = xmlSearchNsByHref(lookupp->doc, lookupp, (xmlChar *) uri);
1471 if (nsptr && nsptr->prefix != NULL) {
1472 RETURN_STRING((char *) nsptr->prefix);
1473 }
1474 }
1475 }
1476
1477 RETURN_NULL();
1478 }
1479 /* }}} end dom_node_lookup_prefix */
1480
1481 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isDefaultNamespace
1482 Since: DOM Level 3
1483 */
PHP_METHOD(DOMNode,isDefaultNamespace)1484 PHP_METHOD(DOMNode, isDefaultNamespace)
1485 {
1486 zval *id;
1487 xmlNodePtr nodep;
1488 dom_object *intern;
1489 xmlNsPtr nsptr;
1490 size_t uri_len = 0;
1491 char *uri;
1492
1493 id = ZEND_THIS;
1494 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &uri, &uri_len) == FAILURE) {
1495 RETURN_THROWS();
1496 }
1497
1498 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1499 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
1500 nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
1501 }
1502
1503 if (nodep && uri_len > 0) {
1504 nsptr = xmlSearchNs(nodep->doc, nodep, NULL);
1505 if (nsptr && xmlStrEqual(nsptr->href, (xmlChar *) uri)) {
1506 RETURN_TRUE;
1507 }
1508 }
1509
1510 RETURN_FALSE;
1511 }
1512 /* }}} end dom_node_is_default_namespace */
1513
1514 /* {{{ URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI
1515 Since: DOM Level 3
1516 */
PHP_METHOD(DOMNode,lookupNamespaceURI)1517 PHP_METHOD(DOMNode, lookupNamespaceURI)
1518 {
1519 zval *id;
1520 xmlNodePtr nodep;
1521 dom_object *intern;
1522 xmlNsPtr nsptr;
1523 size_t prefix_len;
1524 char *prefix;
1525
1526 id = ZEND_THIS;
1527 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &prefix, &prefix_len) == FAILURE) {
1528 RETURN_THROWS();
1529 }
1530
1531 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1532 if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
1533 nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
1534 if (nodep == NULL) {
1535 RETURN_NULL();
1536 }
1537 }
1538
1539 nsptr = xmlSearchNs(nodep->doc, nodep, (xmlChar *) prefix);
1540 if (nsptr && nsptr->href != NULL) {
1541 RETURN_STRING((char *) nsptr->href);
1542 }
1543
1544 RETURN_NULL();
1545 }
1546 /* }}} end dom_node_lookup_namespace_uri */
1547
dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS,int mode)1548 static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
1549 {
1550 zval *id;
1551 zval *xpath_array=NULL, *ns_prefixes=NULL;
1552 xmlNodePtr nodep;
1553 xmlDocPtr docp;
1554 xmlNodeSetPtr nodeset = NULL;
1555 dom_object *intern;
1556 zend_bool exclusive=0, with_comments=0;
1557 xmlChar **inclusive_ns_prefixes = NULL;
1558 char *file = NULL;
1559 int ret = -1;
1560 size_t file_len = 0;
1561 xmlOutputBufferPtr buf;
1562 xmlXPathContextPtr ctxp=NULL;
1563 xmlXPathObjectPtr xpathobjp=NULL;
1564
1565 id = ZEND_THIS;
1566 if (mode == 0) {
1567 if (zend_parse_parameters(ZEND_NUM_ARGS(),
1568 "|bba!a!", &exclusive, &with_comments,
1569 &xpath_array, &ns_prefixes) == FAILURE) {
1570 RETURN_THROWS();
1571 }
1572 } else {
1573 if (zend_parse_parameters(ZEND_NUM_ARGS(),
1574 "s|bba!a!", &file, &file_len, &exclusive,
1575 &with_comments, &xpath_array, &ns_prefixes) == FAILURE) {
1576 RETURN_THROWS();
1577 }
1578 }
1579
1580 DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1581
1582 docp = nodep->doc;
1583
1584 if (! docp) {
1585 zend_throw_error(NULL, "Node must be associated with a document");
1586 RETURN_THROWS();
1587 }
1588
1589 if (xpath_array == NULL) {
1590 if (nodep->type != XML_DOCUMENT_NODE) {
1591 ctxp = xmlXPathNewContext(docp);
1592 ctxp->node = nodep;
1593 xpathobjp = xmlXPathEvalExpression((xmlChar *) "(.//. | .//@* | .//namespace::*)", ctxp);
1594 ctxp->node = NULL;
1595 if (xpathobjp && xpathobjp->type == XPATH_NODESET) {
1596 nodeset = xpathobjp->nodesetval;
1597 } else {
1598 if (xpathobjp) {
1599 xmlXPathFreeObject(xpathobjp);
1600 }
1601 xmlXPathFreeContext(ctxp);
1602 zend_throw_error(NULL, "XPath query did not return a nodeset");
1603 RETURN_THROWS();
1604 }
1605 }
1606 } else {
1607 /*xpath query from xpath_array */
1608 HashTable *ht = Z_ARRVAL_P(xpath_array);
1609 zval *tmp;
1610 char *xquery;
1611
1612 tmp = zend_hash_str_find(ht, "query", sizeof("query")-1);
1613 if (!tmp) {
1614 /* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
1615 zend_argument_value_error(3 + mode, "must have a \"query\" key");
1616 RETURN_THROWS();
1617 }
1618 if (Z_TYPE_P(tmp) != IS_STRING) {
1619 /* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
1620 zend_argument_type_error(3 + mode, "\"query\" option must be a string, %s given", zend_zval_type_name(tmp));
1621 RETURN_THROWS();
1622 }
1623 xquery = Z_STRVAL_P(tmp);
1624
1625 ctxp = xmlXPathNewContext(docp);
1626 ctxp->node = nodep;
1627
1628 tmp = zend_hash_str_find(ht, "namespaces", sizeof("namespaces")-1);
1629 if (tmp && Z_TYPE_P(tmp) == IS_ARRAY) {
1630 zval *tmpns;
1631 zend_string *prefix;
1632
1633 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), prefix, tmpns) {
1634 if (Z_TYPE_P(tmpns) == IS_STRING) {
1635 if (prefix) {
1636 xmlXPathRegisterNs(ctxp, (xmlChar *) ZSTR_VAL(prefix), (xmlChar *) Z_STRVAL_P(tmpns));
1637 }
1638 }
1639 } ZEND_HASH_FOREACH_END();
1640 }
1641
1642 xpathobjp = xmlXPathEvalExpression((xmlChar *) xquery, ctxp);
1643 ctxp->node = NULL;
1644 if (xpathobjp && xpathobjp->type == XPATH_NODESET) {
1645 nodeset = xpathobjp->nodesetval;
1646 } else {
1647 if (xpathobjp) {
1648 xmlXPathFreeObject(xpathobjp);
1649 }
1650 xmlXPathFreeContext(ctxp);
1651 zend_throw_error(NULL, "XPath query did not return a nodeset");
1652 RETURN_THROWS();
1653 }
1654 }
1655
1656 if (ns_prefixes != NULL) {
1657 if (exclusive) {
1658 zval *tmpns;
1659 int nscount = 0;
1660
1661 inclusive_ns_prefixes = safe_emalloc(zend_hash_num_elements(Z_ARRVAL_P(ns_prefixes)) + 1,
1662 sizeof(xmlChar *), 0);
1663 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(ns_prefixes), tmpns) {
1664 if (Z_TYPE_P(tmpns) == IS_STRING) {
1665 inclusive_ns_prefixes[nscount++] = (xmlChar *) Z_STRVAL_P(tmpns);
1666 }
1667 } ZEND_HASH_FOREACH_END();
1668 inclusive_ns_prefixes[nscount] = NULL;
1669 } else {
1670 php_error_docref(NULL, E_NOTICE,
1671 "Inclusive namespace prefixes only allowed in exclusive mode.");
1672 }
1673 }
1674
1675 if (mode == 1) {
1676 buf = xmlOutputBufferCreateFilename(file, NULL, 0);
1677 } else {
1678 buf = xmlAllocOutputBuffer(NULL);
1679 }
1680
1681 if (buf != NULL) {
1682 ret = xmlC14NDocSaveTo(docp, nodeset, exclusive, inclusive_ns_prefixes,
1683 with_comments, buf);
1684 }
1685
1686 if (inclusive_ns_prefixes != NULL) {
1687 efree(inclusive_ns_prefixes);
1688 }
1689 if (xpathobjp != NULL) {
1690 xmlXPathFreeObject(xpathobjp);
1691 }
1692 if (ctxp != NULL) {
1693 xmlXPathFreeContext(ctxp);
1694 }
1695
1696 if (buf == NULL || ret < 0) {
1697 RETVAL_FALSE;
1698 } else {
1699 if (mode == 0) {
1700 #ifdef LIBXML2_NEW_BUFFER
1701 ret = xmlOutputBufferGetSize(buf);
1702 #else
1703 ret = buf->buffer->use;
1704 #endif
1705 if (ret > 0) {
1706 #ifdef LIBXML2_NEW_BUFFER
1707 RETVAL_STRINGL((char *) xmlOutputBufferGetContent(buf), ret);
1708 #else
1709 RETVAL_STRINGL((char *) buf->buffer->content, ret);
1710 #endif
1711 } else {
1712 RETVAL_EMPTY_STRING();
1713 }
1714 }
1715 }
1716
1717 if (buf) {
1718 int bytes;
1719
1720 bytes = xmlOutputBufferClose(buf);
1721 if (mode == 1 && (ret >= 0)) {
1722 RETURN_LONG(bytes);
1723 }
1724 }
1725 }
1726 /* }}} */
1727
1728 /* {{{ Canonicalize nodes to a string */
PHP_METHOD(DOMNode,C14N)1729 PHP_METHOD(DOMNode, C14N)
1730 {
1731 dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1732 }
1733 /* }}} */
1734
1735 /* {{{ Canonicalize nodes to a file */
PHP_METHOD(DOMNode,C14NFile)1736 PHP_METHOD(DOMNode, C14NFile)
1737 {
1738 dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1739 }
1740 /* }}} */
1741
1742 /* {{{ Gets an xpath for a node */
PHP_METHOD(DOMNode,getNodePath)1743 PHP_METHOD(DOMNode, getNodePath)
1744 {
1745 zval *id;
1746 xmlNode *nodep;
1747 dom_object *intern;
1748 char *value;
1749
1750 if (zend_parse_parameters_none() == FAILURE) {
1751 RETURN_THROWS();
1752 }
1753
1754 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1755
1756 value = (char *) xmlGetNodePath(nodep);
1757 if (value == NULL) {
1758 /* TODO Research if can return empty string */
1759 RETURN_NULL();
1760 } else {
1761 RETVAL_STRING(value);
1762 xmlFree(value);
1763 }
1764 }
1765 /* }}} */
1766
1767 /* {{{ Gets line number for a node */
PHP_METHOD(DOMNode,getLineNo)1768 PHP_METHOD(DOMNode, getLineNo)
1769 {
1770 zval *id;
1771 xmlNode *nodep;
1772 dom_object *intern;
1773
1774 if (zend_parse_parameters_none() == FAILURE) {
1775 RETURN_THROWS();
1776 }
1777
1778 DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
1779
1780 RETURN_LONG(xmlGetLineNo(nodep));
1781 }
1782 /* }}} */
1783
1784 #endif
1785