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_element_get_attribute, 0, 0, 1)
32    ZEND_ARG_INFO(0, name)
33ZEND_END_ARG_INFO();
34
35ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_set_attribute, 0, 0, 2)
36    ZEND_ARG_INFO(0, name)
37    ZEND_ARG_INFO(0, value)
38ZEND_END_ARG_INFO();
39
40ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_remove_attribute, 0, 0, 1)
41    ZEND_ARG_INFO(0, name)
42ZEND_END_ARG_INFO();
43
44ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_get_attribute_node, 0, 0, 1)
45    ZEND_ARG_INFO(0, name)
46ZEND_END_ARG_INFO();
47
48ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_set_attribute_node, 0, 0, 1)
49    ZEND_ARG_OBJ_INFO(0, newAttr, DOMAttr, 0)
50ZEND_END_ARG_INFO();
51
52ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_remove_attribute_node, 0, 0, 1)
53    ZEND_ARG_OBJ_INFO(0, oldAttr, DOMAttr, 0)
54ZEND_END_ARG_INFO();
55
56ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_get_elements_by_tag_name, 0, 0, 1)
57    ZEND_ARG_INFO(0, name)
58ZEND_END_ARG_INFO();
59
60ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_get_attribute_ns, 0, 0, 2)
61    ZEND_ARG_INFO(0, namespaceURI)
62    ZEND_ARG_INFO(0, localName)
63ZEND_END_ARG_INFO();
64
65ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_set_attribute_ns, 0, 0, 3)
66    ZEND_ARG_INFO(0, namespaceURI)
67    ZEND_ARG_INFO(0, qualifiedName)
68    ZEND_ARG_INFO(0, value)
69ZEND_END_ARG_INFO();
70
71ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_remove_attribute_ns, 0, 0, 2)
72    ZEND_ARG_INFO(0, namespaceURI)
73    ZEND_ARG_INFO(0, localName)
74ZEND_END_ARG_INFO();
75
76ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_get_attribute_node_ns, 0, 0, 2)
77    ZEND_ARG_INFO(0, namespaceURI)
78    ZEND_ARG_INFO(0, localName)
79ZEND_END_ARG_INFO();
80
81ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_set_attribute_node_ns, 0, 0, 1)
82    ZEND_ARG_OBJ_INFO(0, newAttr, DOMAttr, 0)
83ZEND_END_ARG_INFO();
84
85ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_get_elements_by_tag_name_ns, 0, 0, 2)
86    ZEND_ARG_INFO(0, namespaceURI)
87    ZEND_ARG_INFO(0, localName)
88ZEND_END_ARG_INFO();
89
90ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_has_attribute, 0, 0, 1)
91    ZEND_ARG_INFO(0, name)
92ZEND_END_ARG_INFO();
93
94ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_has_attribute_ns, 0, 0, 2)
95    ZEND_ARG_INFO(0, namespaceURI)
96    ZEND_ARG_INFO(0, localName)
97ZEND_END_ARG_INFO();
98
99ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_set_id_attribute, 0, 0, 2)
100    ZEND_ARG_INFO(0, name)
101    ZEND_ARG_INFO(0, isId)
102ZEND_END_ARG_INFO();
103
104ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_set_id_attribute_ns, 0, 0, 3)
105    ZEND_ARG_INFO(0, namespaceURI)
106    ZEND_ARG_INFO(0, localName)
107    ZEND_ARG_INFO(0, isId)
108ZEND_END_ARG_INFO();
109
110ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_set_id_attribute_node, 0, 0, 2)
111    ZEND_ARG_OBJ_INFO(0, attr, DOMAttr, 0)
112    ZEND_ARG_INFO(0, isId)
113ZEND_END_ARG_INFO();
114
115ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_element_construct, 0, 0, 1)
116    ZEND_ARG_INFO(0, name)
117    ZEND_ARG_INFO(0, value)
118    ZEND_ARG_INFO(0, uri)
119ZEND_END_ARG_INFO();
120/* }}} */
121
122/*
123* class DOMElement extends DOMNode
124*
125* URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-745549614
126* Since:
127*/
128
129const zend_function_entry php_dom_element_class_functions[] = { /* {{{ */
130    PHP_FALIAS(getAttribute, dom_element_get_attribute, arginfo_dom_element_get_attribute)
131    PHP_FALIAS(setAttribute, dom_element_set_attribute, arginfo_dom_element_set_attribute)
132    PHP_FALIAS(removeAttribute, dom_element_remove_attribute, arginfo_dom_element_remove_attribute)
133    PHP_FALIAS(getAttributeNode, dom_element_get_attribute_node, arginfo_dom_element_get_attribute_node)
134    PHP_FALIAS(setAttributeNode, dom_element_set_attribute_node, arginfo_dom_element_set_attribute_node)
135    PHP_FALIAS(removeAttributeNode, dom_element_remove_attribute_node, arginfo_dom_element_remove_attribute_node)
136    PHP_FALIAS(getElementsByTagName, dom_element_get_elements_by_tag_name, arginfo_dom_element_get_elements_by_tag_name)
137    PHP_FALIAS(getAttributeNS, dom_element_get_attribute_ns, arginfo_dom_element_get_attribute_ns)
138    PHP_FALIAS(setAttributeNS, dom_element_set_attribute_ns, arginfo_dom_element_set_attribute_ns)
139    PHP_FALIAS(removeAttributeNS, dom_element_remove_attribute_ns, arginfo_dom_element_remove_attribute_ns)
140    PHP_FALIAS(getAttributeNodeNS, dom_element_get_attribute_node_ns, arginfo_dom_element_get_attribute_node_ns)
141    PHP_FALIAS(setAttributeNodeNS, dom_element_set_attribute_node_ns, arginfo_dom_element_set_attribute_node_ns)
142    PHP_FALIAS(getElementsByTagNameNS, dom_element_get_elements_by_tag_name_ns, arginfo_dom_element_get_elements_by_tag_name_ns)
143    PHP_FALIAS(hasAttribute, dom_element_has_attribute, arginfo_dom_element_has_attribute)
144    PHP_FALIAS(hasAttributeNS, dom_element_has_attribute_ns, arginfo_dom_element_has_attribute_ns)
145    PHP_FALIAS(setIdAttribute, dom_element_set_id_attribute, arginfo_dom_element_set_id_attribute)
146    PHP_FALIAS(setIdAttributeNS, dom_element_set_id_attribute_ns, arginfo_dom_element_set_id_attribute_ns)
147    PHP_FALIAS(setIdAttributeNode, dom_element_set_id_attribute_node, arginfo_dom_element_set_id_attribute_node)
148    PHP_ME(domelement, __construct, arginfo_dom_element_construct, ZEND_ACC_PUBLIC)
149    PHP_FE_END
150};
151/* }}} */
152
153/* {{{ proto void DOMElement::__construct(string name, [string value], [string uri]); */
154PHP_METHOD(domelement, __construct)
155{
156
157    zval *id;
158    xmlNodePtr nodep = NULL, oldnode = NULL;
159    dom_object *intern;
160    char *name, *value = NULL, *uri = NULL;
161    char *localname = NULL, *prefix = NULL;
162    int errorcode = 0, uri_len = 0;
163    int name_len, value_len = 0, name_valid;
164    xmlNsPtr nsptr = NULL;
165    zend_error_handling error_handling;
166
167    zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
168    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s!s", &id, dom_element_class_entry, &name, &name_len, &value, &value_len, &uri, &uri_len) == FAILURE) {
169        zend_restore_error_handling(&error_handling TSRMLS_CC);
170        return;
171    }
172    zend_restore_error_handling(&error_handling TSRMLS_CC);
173
174    name_valid = xmlValidateName((xmlChar *) name, 0);
175    if (name_valid != 0) {
176        php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC);
177        RETURN_FALSE;
178    }
179
180    /* Namespace logic is separate and only when uri passed in to insure no BC breakage */
181    if (uri_len > 0) {
182        errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
183        if (errorcode == 0) {
184            nodep = xmlNewNode (NULL, (xmlChar *)localname);
185            if (nodep != NULL && uri != NULL) {
186                nsptr = dom_get_ns(nodep, uri, &errorcode, prefix);
187                xmlSetNs(nodep, nsptr);
188            }
189        }
190        xmlFree(localname);
191        if (prefix != NULL) {
192            xmlFree(prefix);
193        }
194        if (errorcode != 0) {
195            if (nodep != NULL) {
196                xmlFreeNode(nodep);
197            }
198            php_dom_throw_error(errorcode, 1 TSRMLS_CC);
199            RETURN_FALSE;
200        }
201    } else {
202        /* If you don't pass a namespace uri, then you can't set a prefix */
203        localname = xmlSplitQName2((xmlChar *)name, (xmlChar **) &prefix);
204        if (prefix != NULL) {
205            xmlFree(localname);
206            xmlFree(prefix);
207            php_dom_throw_error(NAMESPACE_ERR, 1 TSRMLS_CC);
208            RETURN_FALSE;
209        }
210        nodep = xmlNewNode(NULL, (xmlChar *) name);
211    }
212
213    if (!nodep) {
214        php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
215        RETURN_FALSE;
216    }
217
218    if (value_len > 0) {
219        xmlNodeSetContentLen(nodep, (xmlChar *) value, value_len);
220    }
221
222    intern = Z_DOMOBJ_P(id);
223    oldnode = dom_object_get_node(intern);
224    if (oldnode != NULL) {
225        php_libxml_node_free_resource(oldnode  TSRMLS_CC);
226    }
227    php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
228}
229/* }}} end DOMElement::__construct */
230
231/* {{{ tagName  string
232readonly=yes
233URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-104682815
234Since:
235*/
236int dom_element_tag_name_read(dom_object *obj, zval *retval TSRMLS_DC)
237{
238    xmlNodePtr nodep;
239    xmlNsPtr ns;
240    xmlChar *qname;
241
242    nodep = dom_object_get_node(obj);
243
244    if (nodep == NULL) {
245        php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
246        return FAILURE;
247    }
248
249    ns = nodep->ns;
250    if (ns != NULL && ns->prefix) {
251        qname = xmlStrdup(ns->prefix);
252        qname = xmlStrcat(qname, (xmlChar *)":");
253        qname = xmlStrcat(qname, nodep->name);
254        ZVAL_STRING(retval, (char *)qname);
255        xmlFree(qname);
256    } else {
257        ZVAL_STRING(retval, (char *) nodep->name);
258    }
259
260    return SUCCESS;
261}
262
263/* }}} */
264
265/* {{{ schemaTypeInfo   typeinfo
266readonly=yes
267URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Element-schemaTypeInfo
268Since: DOM Level 3
269*/
270int dom_element_schema_type_info_read(dom_object *obj, zval *retval TSRMLS_DC)
271{
272    ZVAL_NULL(retval);
273    return SUCCESS;
274}
275
276/* }}} */
277
278static xmlNodePtr dom_get_dom1_attribute(xmlNodePtr elem, xmlChar *name) /* {{{ */
279{
280    int len;
281    const xmlChar *nqname;
282
283    nqname = xmlSplitQName3(name, &len);
284    if (nqname != NULL) {
285        xmlNsPtr ns;
286        xmlChar *prefix = xmlStrndup(name, len);
287        if (prefix && xmlStrEqual(prefix, (xmlChar *)"xmlns")) {
288            ns = elem->nsDef;
289            while (ns) {
290                if (xmlStrEqual(ns->prefix, nqname)) {
291                    break;
292                }
293                ns = ns->next;
294            }
295            xmlFree(prefix);
296            return (xmlNodePtr)ns;
297        }
298        ns = xmlSearchNs(elem->doc, elem, prefix);
299        if (prefix != NULL) {
300            xmlFree(prefix);
301        }
302        if (ns != NULL) {
303            return (xmlNodePtr)xmlHasNsProp(elem, nqname, ns->href);
304        }
305    } else {
306        if (xmlStrEqual(name, (xmlChar *)"xmlns")) {
307            xmlNsPtr nsPtr = elem->nsDef;
308            while (nsPtr) {
309                if (nsPtr->prefix == NULL) {
310                    return (xmlNodePtr)nsPtr;
311                }
312                nsPtr = nsPtr->next;
313            }
314            return NULL;
315        }
316    }
317    return (xmlNodePtr)xmlHasNsProp(elem, name, NULL);
318}
319/* }}} */
320
321/* {{{ proto string dom_element_get_attribute(string name);
322URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-666EE0F9
323Since:
324*/
325PHP_FUNCTION(dom_element_get_attribute)
326{
327    zval *id;
328    xmlNode *nodep;
329    char *name;
330    xmlChar *value = NULL;
331    dom_object *intern;
332    xmlNodePtr attr;
333    int name_len;
334
335    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
336        return;
337    }
338
339    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
340
341    attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
342    if (attr) {
343        switch (attr->type) {
344            case XML_ATTRIBUTE_NODE:
345                value = xmlNodeListGetString(attr->doc, attr->children, 1);
346                break;
347            case XML_NAMESPACE_DECL:
348                value = xmlStrdup(((xmlNsPtr)attr)->href);
349                break;
350            default:
351                value = xmlStrdup(((xmlAttributePtr)attr)->defaultValue);
352        }
353    }
354
355    if (value == NULL) {
356        RETURN_EMPTY_STRING();
357    } else {
358        RETVAL_STRING((char *)value);
359        xmlFree(value);
360    }
361}
362/* }}} end dom_element_get_attribute */
363
364/* {{{ proto void dom_element_set_attribute(string name, string value);
365URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68F082
366Since:
367*/
368PHP_FUNCTION(dom_element_set_attribute)
369{
370    zval *id;
371    xmlNode *nodep;
372    xmlNodePtr attr = NULL;
373    int ret, name_len, value_len, name_valid;
374    dom_object *intern;
375    char *name, *value;
376
377    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss", &id, dom_element_class_entry, &name, &name_len, &value, &value_len) == FAILURE) {
378        return;
379    }
380
381    if (name_len == 0) {
382        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute Name is required");
383        RETURN_FALSE;
384    }
385
386    name_valid = xmlValidateName((xmlChar *) name, 0);
387    if (name_valid != 0) {
388        php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC);
389        RETURN_FALSE;
390    }
391
392    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
393
394    if (dom_node_is_read_only(nodep) == SUCCESS) {
395        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
396        RETURN_FALSE;
397    }
398
399    attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
400    if (attr != NULL) {
401        switch (attr->type) {
402            case XML_ATTRIBUTE_NODE:
403                node_list_unlink(attr->children TSRMLS_CC);
404                break;
405            case XML_NAMESPACE_DECL:
406                RETURN_FALSE;
407            default:
408                break;
409        }
410
411    }
412
413    if (xmlStrEqual((xmlChar *)name, (xmlChar *)"xmlns")) {
414        if (xmlNewNs(nodep, (xmlChar *)value, NULL)) {
415            RETURN_TRUE;
416        }
417    } else {
418        attr = (xmlNodePtr)xmlSetProp(nodep, (xmlChar *) name, (xmlChar *)value);
419    }
420    if (!attr) {
421        php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such attribute '%s'", name);
422        RETURN_FALSE;
423    }
424
425    DOM_RET_OBJ(attr, &ret, intern);
426
427}
428/* }}} end dom_element_set_attribute */
429
430/* {{{ proto void dom_element_remove_attribute(string name);
431URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6D6AC0F9
432Since:
433*/
434PHP_FUNCTION(dom_element_remove_attribute)
435{
436    zval *id;
437    xmlNodePtr nodep, attrp;
438    dom_object *intern;
439    int name_len;
440    char *name;
441
442    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
443        return;
444    }
445
446    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
447
448    if (dom_node_is_read_only(nodep) == SUCCESS) {
449        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
450        RETURN_FALSE;
451    }
452
453    attrp = dom_get_dom1_attribute(nodep, (xmlChar *)name);
454    if (attrp == NULL) {
455        RETURN_FALSE;
456    }
457
458    switch (attrp->type) {
459        case XML_ATTRIBUTE_NODE:
460            if (php_dom_object_get_data(attrp) == NULL) {
461            node_list_unlink(attrp->children TSRMLS_CC);
462                xmlUnlinkNode(attrp);
463                xmlFreeProp((xmlAttrPtr)attrp);
464        } else {
465                xmlUnlinkNode(attrp);
466        }
467            break;
468        case XML_NAMESPACE_DECL:
469            RETURN_FALSE;
470        default:
471            break;
472    }
473
474    RETURN_TRUE;
475}
476/* }}} end dom_element_remove_attribute */
477
478/* {{{ proto DOMAttr dom_element_get_attribute_node(string name);
479URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-217A91B8
480Since:
481*/
482PHP_FUNCTION(dom_element_get_attribute_node)
483{
484    zval *id;
485    xmlNodePtr nodep, attrp;
486    int name_len, ret;
487    dom_object *intern;
488    char *name;
489
490    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
491        return;
492    }
493
494    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
495
496    attrp = dom_get_dom1_attribute(nodep, (xmlChar *)name);
497    if (attrp == NULL) {
498        RETURN_FALSE;
499    }
500
501    if (attrp->type == XML_NAMESPACE_DECL) {
502        xmlNsPtr curns;
503        xmlNodePtr nsparent;
504
505        nsparent = attrp->_private;
506        curns = xmlNewNs(NULL, attrp->name, NULL);
507        if (attrp->children) {
508            curns->prefix = xmlStrdup((xmlChar *) attrp->children);
509        }
510        if (attrp->children) {
511            attrp = xmlNewDocNode(nodep->doc, NULL, (xmlChar *) attrp->children, attrp->name);
512        } else {
513            attrp = xmlNewDocNode(nodep->doc, NULL, (xmlChar *)"xmlns", attrp->name);
514        }
515        attrp->type = XML_NAMESPACE_DECL;
516        attrp->parent = nsparent;
517        attrp->ns = curns;
518    }
519
520    DOM_RET_OBJ((xmlNodePtr) attrp, &ret, intern);
521}
522/* }}} end dom_element_get_attribute_node */
523
524/* {{{ proto DOMAttr dom_element_set_attribute_node(DOMAttr newAttr);
525URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-887236154
526Since:
527*/
528PHP_FUNCTION(dom_element_set_attribute_node)
529{
530    zval *id, *node;
531    xmlNode *nodep;
532    xmlAttr *attrp, *existattrp = NULL;
533    dom_object *intern, *attrobj, *oldobj;
534    int ret;
535
536    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_element_class_entry, &node, dom_attr_class_entry) == FAILURE) {
537        return;
538    }
539
540    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
541
542    if (dom_node_is_read_only(nodep) == SUCCESS) {
543        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
544        RETURN_FALSE;
545    }
546
547    DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
548
549    if (attrp->type != XML_ATTRIBUTE_NODE) {
550        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute node is required");
551        RETURN_FALSE;
552    }
553
554    if (!(attrp->doc == NULL || attrp->doc == nodep->doc)) {
555        php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
556        RETURN_FALSE;
557    }
558
559    existattrp = xmlHasProp(nodep, attrp->name);
560    if (existattrp != NULL && existattrp->type != XML_ATTRIBUTE_DECL) {
561        if ((oldobj = php_dom_object_get_data((xmlNodePtr) existattrp)) != NULL &&
562            ((php_libxml_node_ptr *)oldobj->ptr)->node == (xmlNodePtr) attrp)
563        {
564            RETURN_NULL();
565        }
566        xmlUnlinkNode((xmlNodePtr) existattrp);
567    }
568
569    if (attrp->parent != NULL) {
570        xmlUnlinkNode((xmlNodePtr) attrp);
571    }
572
573    if (attrp->doc == NULL && nodep->doc != NULL) {
574        attrobj->document = intern->document;
575        php_libxml_increment_doc_ref((php_libxml_node_object *)attrobj, NULL TSRMLS_CC);
576    }
577
578    xmlAddChild(nodep, (xmlNodePtr) attrp);
579
580    /* Returns old property if removed otherwise NULL */
581    if (existattrp != NULL) {
582        DOM_RET_OBJ((xmlNodePtr) existattrp, &ret, intern);
583    } else {
584        RETVAL_NULL();
585    }
586
587}
588/* }}} end dom_element_set_attribute_node */
589
590/* {{{ proto DOMAttr dom_element_remove_attribute_node(DOMAttr oldAttr);
591URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-D589198
592Since:
593*/
594PHP_FUNCTION(dom_element_remove_attribute_node)
595{
596    zval *id, *node;
597    xmlNode *nodep;
598    xmlAttr *attrp;
599    dom_object *intern, *attrobj;
600    int ret;
601
602    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_element_class_entry, &node, dom_attr_class_entry) == FAILURE) {
603        return;
604    }
605
606    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
607
608    if (dom_node_is_read_only(nodep) == SUCCESS) {
609        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
610        RETURN_FALSE;
611    }
612
613    DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
614
615    if (attrp->type != XML_ATTRIBUTE_NODE || attrp->parent != nodep) {
616        php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
617        RETURN_FALSE;
618    }
619
620    xmlUnlinkNode((xmlNodePtr) attrp);
621
622    DOM_RET_OBJ((xmlNodePtr) attrp, &ret, intern);
623
624}
625/* }}} end dom_element_remove_attribute_node */
626
627/* {{{ proto DOMNodeList dom_element_get_elements_by_tag_name(string name);
628URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1938918D
629Since:
630*/
631PHP_FUNCTION(dom_element_get_elements_by_tag_name)
632{
633    zval *id;
634    xmlNodePtr elemp;
635    int name_len;
636    dom_object *intern, *namednode;
637    char *name;
638    xmlChar *local;
639
640    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
641        return;
642    }
643
644    DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
645
646    php_dom_create_interator(return_value, DOM_NODELIST TSRMLS_CC);
647    namednode = Z_DOMOBJ_P(return_value);
648    local = xmlCharStrndup(name, name_len);
649    dom_namednode_iter(intern, 0, namednode, NULL, local, NULL TSRMLS_CC);
650}
651/* }}} end dom_element_get_elements_by_tag_name */
652
653/* {{{ proto string dom_element_get_attribute_ns(string namespaceURI, string localName);
654URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElGetAttrNS
655Since: DOM Level 2
656*/
657PHP_FUNCTION(dom_element_get_attribute_ns)
658{
659    zval *id;
660    xmlNodePtr elemp;
661    xmlNsPtr nsptr;
662    dom_object *intern;
663    int uri_len = 0, name_len = 0;
664    char *uri, *name;
665    xmlChar *strattr;
666
667    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!s", &id, dom_element_class_entry, &uri, &uri_len, &name, &name_len) == FAILURE) {
668        return;
669    }
670
671    DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
672
673    strattr = xmlGetNsProp(elemp, (xmlChar *) name, (xmlChar *) uri);
674
675    if (strattr != NULL) {
676        RETVAL_STRING((char *)strattr);
677        xmlFree(strattr);
678    } else {
679        if (xmlStrEqual((xmlChar *) uri, (xmlChar *)DOM_XMLNS_NAMESPACE)) {
680            nsptr = dom_get_nsdecl(elemp, (xmlChar *)name);
681            if (nsptr != NULL) {
682                RETVAL_STRING((char *) nsptr->href);
683            } else {
684                RETVAL_EMPTY_STRING();
685            }
686        } else {
687            RETVAL_EMPTY_STRING();
688        }
689    }
690
691}
692/* }}} end dom_element_get_attribute_ns */
693
694static xmlNsPtr _dom_new_reconNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) /* {{{ */
695{
696    xmlNsPtr def;
697    xmlChar prefix[50];
698    int counter = 1;
699
700    if ((tree == NULL) || (ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
701        return NULL;
702    }
703
704    /* Code taken from libxml2 (2.6.20) xmlNewReconciliedNs
705     *
706     * Find a close prefix which is not already in use.
707     * Let's strip namespace prefixes longer than 20 chars !
708     */
709    if (ns->prefix == NULL)
710        snprintf((char *) prefix, sizeof(prefix), "default");
711    else
712        snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
713
714    def = xmlSearchNs(doc, tree, prefix);
715    while (def != NULL) {
716        if (counter > 1000) return(NULL);
717        if (ns->prefix == NULL)
718            snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
719        else
720            snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
721            (char *)ns->prefix, counter++);
722        def = xmlSearchNs(doc, tree, prefix);
723    }
724
725    /*
726     * OK, now we are ready to create a new one.
727     */
728    def = xmlNewNs(tree, ns->href, prefix);
729    return(def);
730}
731/* }}} */
732
733/* {{{ proto void dom_element_set_attribute_ns(string namespaceURI, string qualifiedName, string value);
734URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetAttrNS
735Since: DOM Level 2
736*/
737PHP_FUNCTION(dom_element_set_attribute_ns)
738{
739    zval *id;
740    xmlNodePtr elemp, nodep = NULL;
741    xmlNsPtr nsptr;
742    xmlAttr *attr;
743    int uri_len = 0, name_len = 0, value_len = 0;
744    char *uri, *name, *value;
745    char *localname = NULL, *prefix = NULL;
746    dom_object *intern;
747    int errorcode = 0, stricterror, is_xmlns = 0, name_valid;
748
749    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!ss", &id, dom_element_class_entry, &uri, &uri_len, &name, &name_len, &value, &value_len) == FAILURE) {
750        return;
751    }
752
753    if (name_len == 0) {
754        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute Name is required");
755        RETURN_FALSE;
756    }
757
758    DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
759
760    stricterror = dom_get_strict_error(intern->document);
761
762    if (dom_node_is_read_only(elemp) == SUCCESS) {
763        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror TSRMLS_CC);
764        RETURN_NULL();
765    }
766
767    errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
768
769    if (errorcode == 0) {
770        if (uri_len > 0) {
771            nodep = (xmlNodePtr) xmlHasNsProp(elemp, (xmlChar *) localname, (xmlChar *) uri);
772            if (nodep != NULL && nodep->type != XML_ATTRIBUTE_DECL) {
773                node_list_unlink(nodep->children TSRMLS_CC);
774            }
775
776            if ((xmlStrEqual((xmlChar *) prefix, (xmlChar *)"xmlns") ||
777                (prefix == NULL && xmlStrEqual((xmlChar *) localname, (xmlChar *)"xmlns"))) &&
778                xmlStrEqual((xmlChar *) uri, (xmlChar *)DOM_XMLNS_NAMESPACE)) {
779                is_xmlns = 1;
780                if (prefix == NULL) {
781                    nsptr = dom_get_nsdecl(elemp, NULL);
782                } else {
783                    nsptr = dom_get_nsdecl(elemp, (xmlChar *)localname);
784                }
785            } else {
786                nsptr = xmlSearchNsByHref(elemp->doc, elemp, (xmlChar *)uri);
787                if (nsptr && nsptr->prefix == NULL) {
788                    xmlNsPtr tmpnsptr;
789
790                    tmpnsptr = nsptr->next;
791                    while (tmpnsptr) {
792                        if ((tmpnsptr->prefix != NULL) && (tmpnsptr->href != NULL) &&
793                            (xmlStrEqual(tmpnsptr->href, (xmlChar *) uri))) {
794                            nsptr = tmpnsptr;
795                            break;
796                        }
797                        tmpnsptr = tmpnsptr->next;
798                    }
799                    if (tmpnsptr == NULL) {
800                        nsptr = _dom_new_reconNs(elemp->doc, elemp, nsptr);
801                    }
802                }
803            }
804
805            if (nsptr == NULL) {
806                if (prefix == NULL) {
807                    if (is_xmlns == 1) {
808                        xmlNewNs(elemp, (xmlChar *)value, NULL);
809                        xmlReconciliateNs(elemp->doc, elemp);
810                    } else {
811                        errorcode = NAMESPACE_ERR;
812                    }
813                } else {
814                    if (is_xmlns == 1) {
815                        xmlNewNs(elemp, (xmlChar *)value, (xmlChar *)localname);
816                    } else {
817                        nsptr = dom_get_ns(elemp, uri, &errorcode, prefix);
818                    }
819                    xmlReconciliateNs(elemp->doc, elemp);
820                }
821            } else {
822                if (is_xmlns == 1) {
823                    if (nsptr->href) {
824                        xmlFree((xmlChar *) nsptr->href);
825                    }
826                    nsptr->href = xmlStrdup((xmlChar *)value);
827                }
828            }
829
830            if (errorcode == 0 && is_xmlns == 0) {
831                xmlSetNsProp(elemp, nsptr, (xmlChar *)localname, (xmlChar *)value);
832            }
833        } else {
834            name_valid = xmlValidateName((xmlChar *) localname, 0);
835            if (name_valid != 0) {
836                errorcode = INVALID_CHARACTER_ERR;
837                stricterror = 1;
838            } else {
839                attr = xmlHasProp(elemp, (xmlChar *)localname);
840                if (attr != NULL && attr->type != XML_ATTRIBUTE_DECL) {
841                    node_list_unlink(attr->children TSRMLS_CC);
842                }
843                xmlSetProp(elemp, (xmlChar *)localname, (xmlChar *)value);
844            }
845        }
846    }
847
848    xmlFree(localname);
849    if (prefix != NULL) {
850        xmlFree(prefix);
851    }
852
853    if (errorcode != 0) {
854        php_dom_throw_error(errorcode, stricterror TSRMLS_CC);
855    }
856
857    RETURN_NULL();
858}
859/* }}} end dom_element_set_attribute_ns */
860
861/* {{{ proto void dom_element_remove_attribute_ns(string namespaceURI, string localName);
862URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElRemAtNS
863Since: DOM Level 2
864*/
865PHP_FUNCTION(dom_element_remove_attribute_ns)
866{
867    zval *id;
868    xmlNode *nodep;
869    xmlAttr *attrp;
870    xmlNsPtr nsptr;
871    dom_object *intern;
872    int name_len, uri_len;
873    char *name, *uri;
874
875    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!s", &id, dom_element_class_entry, &uri, &uri_len, &name, &name_len) == FAILURE) {
876        return;
877    }
878
879    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
880
881    if (dom_node_is_read_only(nodep) == SUCCESS) {
882        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
883        RETURN_NULL();
884    }
885
886    attrp = xmlHasNsProp(nodep, (xmlChar *)name, (xmlChar *)uri);
887
888    nsptr = dom_get_nsdecl(nodep, (xmlChar *)name);
889    if (nsptr != NULL) {
890        if (xmlStrEqual((xmlChar *)uri, nsptr->href)) {
891            if (nsptr->href != NULL) {
892                xmlFree((char *) nsptr->href);
893                nsptr->href = NULL;
894            }
895            if (nsptr->prefix != NULL) {
896                xmlFree((char *) nsptr->prefix);
897                nsptr->prefix = NULL;
898            }
899        } else {
900            RETURN_NULL();
901        }
902    }
903
904    if (attrp && attrp->type != XML_ATTRIBUTE_DECL) {
905        if (php_dom_object_get_data((xmlNodePtr) attrp) == NULL) {
906            node_list_unlink(attrp->children TSRMLS_CC);
907            xmlUnlinkNode((xmlNodePtr) attrp);
908            xmlFreeProp(attrp);
909        } else {
910            xmlUnlinkNode((xmlNodePtr) attrp);
911        }
912    }
913
914    RETURN_NULL();
915}
916/* }}} end dom_element_remove_attribute_ns */
917
918/* {{{ proto DOMAttr dom_element_get_attribute_node_ns(string namespaceURI, string localName);
919URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElGetAtNodeNS
920Since: DOM Level 2
921*/
922PHP_FUNCTION(dom_element_get_attribute_node_ns)
923{
924    zval *id;
925    xmlNodePtr elemp;
926    xmlAttrPtr attrp;
927    dom_object *intern;
928    int uri_len, name_len, ret;
929    char *uri, *name;
930
931    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!s", &id, dom_element_class_entry, &uri, &uri_len, &name, &name_len) == FAILURE) {
932        return;
933    }
934
935    DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
936
937    attrp = xmlHasNsProp(elemp, (xmlChar *)name, (xmlChar *)uri);
938
939    if (attrp == NULL) {
940        RETURN_NULL();
941    }
942
943    DOM_RET_OBJ((xmlNodePtr) attrp, &ret, intern);
944
945}
946/* }}} end dom_element_get_attribute_node_ns */
947
948/* {{{ proto DOMAttr dom_element_set_attribute_node_ns(DOMAttr newAttr);
949URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetAtNodeNS
950Since: DOM Level 2
951*/
952PHP_FUNCTION(dom_element_set_attribute_node_ns)
953{
954    zval *id, *node;
955    xmlNode *nodep;
956    xmlNs *nsp;
957    xmlAttr *attrp, *existattrp = NULL;
958    dom_object *intern, *attrobj, *oldobj;
959    int ret;
960
961    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_element_class_entry, &node, dom_attr_class_entry) == FAILURE) {
962        return;
963    }
964
965    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
966
967    if (dom_node_is_read_only(nodep) == SUCCESS) {
968        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
969        RETURN_FALSE;
970    }
971
972    DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
973
974    if (attrp->type != XML_ATTRIBUTE_NODE) {
975        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute node is required");
976        RETURN_FALSE;
977    }
978
979    if (!(attrp->doc == NULL || attrp->doc == nodep->doc)) {
980        php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
981        RETURN_FALSE;
982    }
983
984    nsp = attrp->ns;
985    if (nsp != NULL) {
986        existattrp = xmlHasNsProp(nodep, nsp->href, attrp->name);
987    } else {
988        existattrp = xmlHasProp(nodep, attrp->name);
989    }
990
991    if (existattrp != NULL && existattrp->type != XML_ATTRIBUTE_DECL) {
992        if ((oldobj = php_dom_object_get_data((xmlNodePtr) existattrp)) != NULL &&
993            ((php_libxml_node_ptr *)oldobj->ptr)->node == (xmlNodePtr) attrp)
994        {
995            RETURN_NULL();
996        }
997        xmlUnlinkNode((xmlNodePtr) existattrp);
998    }
999
1000    if (attrp->parent != NULL) {
1001        xmlUnlinkNode((xmlNodePtr) attrp);
1002    }
1003
1004    if (attrp->doc == NULL && nodep->doc != NULL) {
1005        attrobj->document = intern->document;
1006        php_libxml_increment_doc_ref((php_libxml_node_object *)attrobj, NULL TSRMLS_CC);
1007    }
1008
1009    xmlAddChild(nodep, (xmlNodePtr) attrp);
1010
1011    /* Returns old property if removed otherwise NULL */
1012    if (existattrp != NULL) {
1013        DOM_RET_OBJ((xmlNodePtr) existattrp, &ret, intern);
1014    } else {
1015        RETVAL_NULL();
1016    }
1017
1018}
1019/* }}} end dom_element_set_attribute_node_ns */
1020
1021/* {{{ proto DOMNodeList dom_element_get_elements_by_tag_name_ns(string namespaceURI, string localName);
1022URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-A6C90942
1023Since: DOM Level 2
1024*/
1025PHP_FUNCTION(dom_element_get_elements_by_tag_name_ns)
1026{
1027    zval *id;
1028    xmlNodePtr elemp;
1029    int uri_len, name_len;
1030    dom_object *intern, *namednode;
1031    char *uri, *name;
1032    xmlChar *local, *nsuri;
1033
1034    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss", &id, dom_element_class_entry, &uri, &uri_len, &name, &name_len) == FAILURE) {
1035        return;
1036    }
1037
1038    DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
1039
1040    php_dom_create_interator(return_value, DOM_NODELIST TSRMLS_CC);
1041    namednode = Z_DOMOBJ_P(return_value);
1042    local = xmlCharStrndup(name, name_len);
1043    nsuri = xmlCharStrndup(uri, uri_len);
1044    dom_namednode_iter(intern, 0, namednode, NULL, local, nsuri TSRMLS_CC);
1045
1046}
1047/* }}} end dom_element_get_elements_by_tag_name_ns */
1048
1049/* {{{ proto boolean dom_element_has_attribute(string name);
1050URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElHasAttr
1051Since: DOM Level 2
1052*/
1053PHP_FUNCTION(dom_element_has_attribute)
1054{
1055    zval *id;
1056    xmlNode *nodep;
1057    dom_object *intern;
1058    char *name;
1059    int name_len;
1060    xmlNodePtr attr;
1061
1062    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
1063        return;
1064    }
1065
1066    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1067
1068    attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
1069    if (attr == NULL) {
1070        RETURN_FALSE;
1071    } else {
1072        RETURN_TRUE;
1073    }
1074}
1075/* }}} end dom_element_has_attribute */
1076
1077/* {{{ proto boolean dom_element_has_attribute_ns(string namespaceURI, string localName);
1078URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElHasAttrNS
1079Since: DOM Level 2
1080*/
1081PHP_FUNCTION(dom_element_has_attribute_ns)
1082{
1083    zval *id;
1084    xmlNodePtr elemp;
1085    xmlNs *nsp;
1086    dom_object *intern;
1087    int uri_len, name_len;
1088    char *uri, *name;
1089    xmlChar *value;
1090
1091    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!s", &id, dom_element_class_entry, &uri, &uri_len, &name, &name_len) == FAILURE) {
1092        return;
1093    }
1094
1095    DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
1096
1097    value = xmlGetNsProp(elemp, (xmlChar *)name, (xmlChar *)uri);
1098
1099    if (value != NULL) {
1100        xmlFree(value);
1101        RETURN_TRUE;
1102    } else {
1103        if (xmlStrEqual((xmlChar *)uri, (xmlChar *)DOM_XMLNS_NAMESPACE)) {
1104            nsp = dom_get_nsdecl(elemp, (xmlChar *)name);
1105            if (nsp != NULL) {
1106                RETURN_TRUE;
1107            }
1108        }
1109    }
1110
1111    RETURN_FALSE;
1112}
1113/* }}} end dom_element_has_attribute_ns */
1114
1115static void php_set_attribute_id(xmlAttrPtr attrp, zend_bool is_id) /* {{{ */
1116{
1117    if (is_id == 1 && attrp->atype != XML_ATTRIBUTE_ID) {
1118        xmlChar *id_val;
1119
1120        id_val = xmlNodeListGetString(attrp->doc, attrp->children, 1);
1121        if (id_val != NULL) {
1122            xmlAddID(NULL, attrp->doc, id_val, attrp);
1123            xmlFree(id_val);
1124        }
1125    } else {
1126        if (attrp->atype == XML_ATTRIBUTE_ID) {
1127            xmlRemoveID(attrp->doc, attrp);
1128            attrp->atype = 0;
1129        }
1130    }
1131}
1132/* }}} */
1133
1134/* {{{ proto void dom_element_set_id_attribute(string name, boolean isId);
1135URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetIdAttr
1136Since: DOM Level 3
1137*/
1138PHP_FUNCTION(dom_element_set_id_attribute)
1139{
1140    zval *id;
1141    xmlNode *nodep;
1142    xmlAttrPtr attrp;
1143    dom_object *intern;
1144    char *name;
1145    int name_len;
1146    zend_bool is_id;
1147
1148    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osb", &id, dom_element_class_entry, &name, &name_len, &is_id) == FAILURE) {
1149        return;
1150    }
1151
1152    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1153
1154    if (dom_node_is_read_only(nodep) == SUCCESS) {
1155        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1156        RETURN_NULL();
1157    }
1158
1159    attrp = xmlHasNsProp(nodep, (xmlChar *)name, NULL);
1160    if (attrp == NULL || attrp->type == XML_ATTRIBUTE_DECL) {
1161        php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1162    } else {
1163        php_set_attribute_id(attrp, is_id);
1164    }
1165
1166    RETURN_NULL();
1167}
1168/* }}} end dom_element_set_id_attribute */
1169
1170/* {{{ proto void dom_element_set_id_attribute_ns(string namespaceURI, string localName, boolean isId);
1171URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetIdAttrNS
1172Since: DOM Level 3
1173*/
1174PHP_FUNCTION(dom_element_set_id_attribute_ns)
1175{
1176    zval *id;
1177    xmlNodePtr elemp;
1178    xmlAttrPtr attrp;
1179    dom_object *intern;
1180    int uri_len, name_len;
1181    char *uri, *name;
1182    zend_bool is_id;
1183
1184    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ossb", &id, dom_element_class_entry, &uri, &uri_len, &name, &name_len, &is_id) == FAILURE) {
1185        return;
1186    }
1187
1188    DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
1189
1190    if (dom_node_is_read_only(elemp) == SUCCESS) {
1191        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1192        RETURN_NULL();
1193    }
1194
1195    attrp = xmlHasNsProp(elemp, (xmlChar *)name, (xmlChar *)uri);
1196    if (attrp == NULL || attrp->type == XML_ATTRIBUTE_DECL) {
1197        php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1198    } else {
1199        php_set_attribute_id(attrp, is_id);
1200    }
1201
1202    RETURN_NULL();
1203}
1204/* }}} end dom_element_set_id_attribute_ns */
1205
1206/* {{{ proto void dom_element_set_id_attribute_node(attr idAttr, boolean isId);
1207URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetIdAttrNode
1208Since: DOM Level 3
1209*/
1210PHP_FUNCTION(dom_element_set_id_attribute_node)
1211{
1212    zval *id, *node;
1213    xmlNode *nodep;
1214    xmlAttrPtr attrp;
1215    dom_object *intern, *attrobj;
1216    zend_bool is_id;
1217
1218    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OOb", &id, dom_element_class_entry, &node, dom_attr_class_entry, &is_id) == FAILURE) {
1219        return;
1220    }
1221
1222    DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
1223
1224    if (dom_node_is_read_only(nodep) == SUCCESS) {
1225        php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1226        RETURN_NULL();
1227    }
1228
1229    DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
1230
1231    if (attrp->parent != nodep) {
1232        php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1233    } else {
1234        php_set_attribute_id(attrp, is_id);
1235    }
1236
1237    RETURN_NULL();
1238}
1239/* }}} end dom_element_set_id_attribute_node */
1240
1241#endif
1242
1243/*
1244 * Local variables:
1245 * tab-width: 4
1246 * c-basic-offset: 4
1247 * End:
1248 * vim600: noet sw=4 ts=4 fdm=marker
1249 * vim<600: noet sw=4 ts=4
1250 */
1251