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