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