diff --git a/plugins/php/versions/52/install.sh b/plugins/php/versions/52/install.sh index 23e4c6aa7..4f523c865 100755 --- a/plugins/php/versions/52/install.sh +++ b/plugins/php/versions/52/install.sh @@ -71,6 +71,9 @@ else OPTIONS="${OPTIONS} --with-curl" fi +\cp -rf ${curPath}/lib/node.c $sourcePath/php/php${PHP_VER}/ext/dom/node.c +\cp -rf ${curPath}/lib/documenttype.c $sourcePath/php/php${PHP_VER}/ext/dom/documenttype.c + if [ ! -d $serverPath/php/${PHP_VER} ];then ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so @@ -83,7 +86,6 @@ if [ ! -d $serverPath/php/${PHP_VER} ];then --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ - --disable-fileinfo \ $OPTIONS \ --enable-fastcgi \ --enable-fpm \ diff --git a/plugins/php/versions/52/lib/documenttype.c b/plugins/php/versions/52/lib/documenttype.c new file mode 100644 index 000000000..64f0d07b0 --- /dev/null +++ b/plugins/php/versions/52/lib/documenttype.c @@ -0,0 +1,239 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Christian Stocker | + | Rob Richards | + +----------------------------------------------------------------------+ +*/ + +/* $Id: documenttype.c 293036 2010-01-03 09:23:27Z sebastian $ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#if HAVE_LIBXML && HAVE_DOM +#include "php_dom.h" + +/* +* class DOMDocumentType extends DOMNode +* +* URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-412266927 +* Since: +*/ + +zend_function_entry php_dom_documenttype_class_functions[] = { + {NULL, NULL, NULL} +}; + +/* {{{ name string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1844763134 +Since: +*/ +int dom_documenttype_name_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlDtdPtr dtdptr; + + dtdptr = (xmlDtdPtr) dom_object_get_node(obj); + + if (dtdptr == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + ZVAL_STRING(*retval, (char *) (dtdptr->name), 1); + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ entities DOMNamedNodeMap +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1788794630 +Since: +*/ +int dom_documenttype_entities_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlDtdPtr doctypep; + xmlHashTable *entityht; + dom_object *intern; + + doctypep = (xmlDtdPtr) dom_object_get_node(obj); + + if (doctypep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + MAKE_STD_ZVAL(*retval); + php_dom_create_interator(*retval, DOM_NAMEDNODEMAP TSRMLS_CC); + + entityht = (xmlHashTable *) doctypep->entities; + + intern = (dom_object *)zend_objects_get_address(*retval TSRMLS_CC); + dom_namednode_iter(obj, XML_ENTITY_NODE, intern, entityht, NULL, NULL TSRMLS_CC); + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ notations DOMNamedNodeMap +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-D46829EF +Since: +*/ +int dom_documenttype_notations_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlDtdPtr doctypep; + xmlHashTable *notationht; + dom_object *intern; + + doctypep = (xmlDtdPtr) dom_object_get_node(obj); + + if (doctypep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + MAKE_STD_ZVAL(*retval); + php_dom_create_interator(*retval, DOM_NAMEDNODEMAP TSRMLS_CC); + + notationht = (xmlHashTable *) doctypep->notations; + + intern = (dom_object *)zend_objects_get_address(*retval TSRMLS_CC); + dom_namednode_iter(obj, XML_NOTATION_NODE, intern, notationht, NULL, NULL TSRMLS_CC); + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ publicId string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-publicId +Since: DOM Level 2 +*/ +int dom_documenttype_public_id_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlDtdPtr dtdptr; + + dtdptr = (xmlDtdPtr) dom_object_get_node(obj); + + if (dtdptr == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + if (dtdptr->ExternalID) { + ZVAL_STRING(*retval, (char *) (dtdptr->ExternalID), 1); + } else { + ZVAL_EMPTY_STRING(*retval); + } + return SUCCESS; + +} + +/* }}} */ + + + +/* {{{ systemId string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-systemId +Since: DOM Level 2 +*/ +int dom_documenttype_system_id_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlDtdPtr dtdptr; + + dtdptr = (xmlDtdPtr) dom_object_get_node(obj); + + if (dtdptr == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + if (dtdptr->SystemID) { + ZVAL_STRING(*retval, (char *) (dtdptr->SystemID), 1); + } else { + ZVAL_EMPTY_STRING(*retval); + } + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ internalSubset string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-internalSubset +Since: DOM Level 2 +*/ +int dom_documenttype_internal_subset_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + + xmlDtdPtr dtdptr; + xmlDtd *intsubset; + xmlOutputBuffer *buff = NULL; + + dtdptr = (xmlDtdPtr) dom_object_get_node(obj); + + if (dtdptr == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + if (dtdptr->doc != NULL && ((intsubset = dtdptr->doc->intSubset) != NULL)) { + buff = xmlAllocOutputBuffer(NULL); + if (buff != NULL) { + xmlNodeDumpOutput (buff, NULL, (xmlNodePtr) intsubset, 0, 0, NULL); + xmlOutputBufferFlush(buff); + +#ifdef LIBXML2_NEW_BUFFER + ZVAL_STRINGL(*retval, xmlOutputBufferGetContent(buff), + xmlOutputBufferGetSize(buff), 1); +#else + ZVAL_STRINGL(*retval, buff->buffer->content, buff->buffer->use, 1); +#endif + + (void)xmlOutputBufferClose(buff); + return SUCCESS; + } + } + + ZVAL_EMPTY_STRING(*retval); + + return SUCCESS; + +} + +/* }}} */ + +#endif diff --git a/plugins/php/versions/52/lib/node.c b/plugins/php/versions/52/lib/node.c new file mode 100644 index 000000000..e881c7301 --- /dev/null +++ b/plugins/php/versions/52/lib/node.c @@ -0,0 +1,2020 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Christian Stocker | + | Rob Richards | + +----------------------------------------------------------------------+ +*/ + +/* $Id: node.c 298841 2010-05-01 18:30:38Z geissert $ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#if HAVE_LIBXML && HAVE_DOM +#include "php_dom.h" + +/* {{{ arginfo */ +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_insert_before, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, newChild, DOMNode, 0) + ZEND_ARG_OBJ_INFO(0, refChild, DOMNode, 1) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_replace_child, 0, 0, 2) + ZEND_ARG_OBJ_INFO(0, newChild, DOMNode, 0) + ZEND_ARG_OBJ_INFO(0, oldChild, DOMNode, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_remove_child, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, oldChild, DOMNode, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_append_child, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, newChild, DOMNode, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_has_child_nodes, 0, 0, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_clone_node, 0, 0, 1) + ZEND_ARG_INFO(0, deep) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_normalize, 0, 0, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_is_supported, 0, 0, 2) + ZEND_ARG_INFO(0, feature) + ZEND_ARG_INFO(0, version) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_has_attributes, 0, 0, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_compare_document_position, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, other, DOMNode, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_is_same_node, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, other, DOMNode, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_lookup_prefix, 0, 0, 1) + ZEND_ARG_INFO(0, namespaceURI) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_is_default_namespace, 0, 0, 1) + ZEND_ARG_INFO(0, namespaceURI) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_lookup_namespace_uri, 0, 0, 1) + ZEND_ARG_INFO(0, prefix) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_is_equal_node, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, arg, DOMNode, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_get_feature, 0, 0, 2) + ZEND_ARG_INFO(0, feature) + ZEND_ARG_INFO(0, version) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_set_user_data, 0, 0, 3) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, handler) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_get_user_data, 0, 0, 1) + ZEND_ARG_INFO(0, key) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_getNodePath, 0, 0, 0) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_C14N, 0, 0, 0) + ZEND_ARG_INFO(0, exclusive) + ZEND_ARG_INFO(0, with_comments) + ZEND_ARG_ARRAY_INFO(0, xpath, 1) + ZEND_ARG_ARRAY_INFO(0, ns_prefixes, 1) +ZEND_END_ARG_INFO(); + +static +ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_node_C14NFile, 0, 0, 1) + ZEND_ARG_INFO(0, uri) + ZEND_ARG_INFO(0, exclusive) + ZEND_ARG_INFO(0, with_comments) + ZEND_ARG_ARRAY_INFO(0, xpath, 1) + ZEND_ARG_ARRAY_INFO(0, ns_prefixes, 1) +ZEND_END_ARG_INFO(); +/* }}} */ + +/* +* class DOMNode +* +* URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1950641247 +* Since: +*/ + +zend_function_entry php_dom_node_class_functions[] = { + PHP_FALIAS(insertBefore, dom_node_insert_before, arginfo_dom_node_insert_before) + PHP_FALIAS(replaceChild, dom_node_replace_child, arginfo_dom_node_replace_child) + PHP_FALIAS(removeChild, dom_node_remove_child, arginfo_dom_node_remove_child) + PHP_FALIAS(appendChild, dom_node_append_child, arginfo_dom_node_append_child) + PHP_FALIAS(hasChildNodes, dom_node_has_child_nodes, arginfo_dom_node_has_child_nodes) + PHP_FALIAS(cloneNode, dom_node_clone_node, arginfo_dom_node_clone_node) + PHP_FALIAS(normalize, dom_node_normalize, arginfo_dom_node_normalize) + PHP_FALIAS(isSupported, dom_node_is_supported, arginfo_dom_node_is_supported) + PHP_FALIAS(hasAttributes, dom_node_has_attributes, arginfo_dom_node_has_attributes) + PHP_FALIAS(compareDocumentPosition, dom_node_compare_document_position, arginfo_dom_node_compare_document_position) + PHP_FALIAS(isSameNode, dom_node_is_same_node, arginfo_dom_node_is_same_node) + PHP_FALIAS(lookupPrefix, dom_node_lookup_prefix, arginfo_dom_node_lookup_prefix) + PHP_FALIAS(isDefaultNamespace, dom_node_is_default_namespace, arginfo_dom_node_is_default_namespace) + PHP_FALIAS(lookupNamespaceUri, dom_node_lookup_namespace_uri, arginfo_dom_node_lookup_namespace_uri) + PHP_FALIAS(isEqualNode, dom_node_is_equal_node, arginfo_dom_node_is_equal_node) + PHP_FALIAS(getFeature, dom_node_get_feature, arginfo_dom_node_get_feature) + PHP_FALIAS(setUserData, dom_node_set_user_data, arginfo_dom_node_set_user_data) + PHP_FALIAS(getUserData, dom_node_get_user_data, arginfo_dom_node_get_user_data) + PHP_ME(domnode, getNodePath, arginfo_dom_node_getNodePath, ZEND_ACC_PUBLIC) + PHP_ME(domnode, C14N, arginfo_dom_node_C14N, ZEND_ACC_PUBLIC) + PHP_ME(domnode, C14NFile, arginfo_dom_node_C14NFile, ZEND_ACC_PUBLIC) + {NULL, NULL, NULL} +}; + +static void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep) { + xmlNsPtr nsptr; + + if (nodep->type == XML_ELEMENT_NODE) { + /* Following if block primarily used for inserting nodes created via createElementNS */ + if (nodep->nsDef != NULL && nodep->nsDef->href != NULL) { + if((nsptr = xmlSearchNsByHref(doc, nodep->parent, nodep->nsDef->href)) && + (nodep->nsDef->prefix == NULL || xmlStrEqual(nsptr->prefix, nodep->nsDef->prefix))) { + dom_set_old_ns(doc, nodep->nsDef); + nodep->nsDef = NULL; + } + } + xmlReconciliateNs(doc, nodep); + } +} + +/* {{{ nodeName string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D095 +Since: +*/ +int dom_node_node_name_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + xmlNsPtr ns; + char *str = NULL; + xmlChar *qname = NULL; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + switch (nodep->type) { + case XML_ATTRIBUTE_NODE: + case XML_ELEMENT_NODE: + ns = nodep->ns; + if (ns != NULL && ns->prefix) { + qname = xmlStrdup(ns->prefix); + qname = xmlStrcat(qname, ":"); + qname = xmlStrcat(qname, nodep->name); + str = qname; + } else { + str = (char *) nodep->name; + } + break; + case XML_NAMESPACE_DECL: + ns = nodep->ns; + if (ns != NULL && ns->prefix) { + qname = xmlStrdup("xmlns"); + qname = xmlStrcat(qname, ":"); + qname = xmlStrcat(qname, nodep->name); + str = qname; + } else { + str = (char *) nodep->name; + } + break; + case XML_DOCUMENT_TYPE_NODE: + case XML_DTD_NODE: + case XML_PI_NODE: + case XML_ENTITY_DECL: + case XML_ENTITY_REF_NODE: + case XML_NOTATION_NODE: + str = (char *) nodep->name; + break; + case XML_CDATA_SECTION_NODE: + str = "#cdata-section"; + break; + case XML_COMMENT_NODE: + str = "#comment"; + break; + case XML_HTML_DOCUMENT_NODE: + case XML_DOCUMENT_NODE: + str = "#document"; + break; + case XML_DOCUMENT_FRAG_NODE: + str = "#document-fragment"; + break; + case XML_TEXT_NODE: + str = "#text"; + break; + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Node Type"); + } + + ALLOC_ZVAL(*retval); + + if(str != NULL) { + ZVAL_STRING(*retval, str, 1); + } else { + ZVAL_EMPTY_STRING(*retval); + } + + if (qname != NULL) { + xmlFree(qname); + } + + return SUCCESS; + +} + +/* }}} */ + + + +/* {{{ nodeValue string +readonly=no +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D080 +Since: +*/ +int dom_node_node_value_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + char *str = NULL; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + /* Access to Element node is implemented as a convience method */ + switch (nodep->type) { + case XML_ATTRIBUTE_NODE: + case XML_TEXT_NODE: + case XML_ELEMENT_NODE: + case XML_COMMENT_NODE: + case XML_CDATA_SECTION_NODE: + case XML_PI_NODE: + str = xmlNodeGetContent(nodep); + break; + case XML_NAMESPACE_DECL: + str = xmlNodeGetContent(nodep->children); + break; + default: + str = NULL; + break; + } + + ALLOC_ZVAL(*retval); + + if(str != NULL) { + ZVAL_STRING(*retval, str, 1); + xmlFree(str); + } else { + ZVAL_NULL(*retval); + } + + + return SUCCESS; + +} + +int dom_node_node_value_write(dom_object *obj, zval *newval TSRMLS_DC) +{ + xmlNode *nodep; + zval value_copy; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + /* Access to Element node is implemented as a convience method */ + switch (nodep->type) { + case XML_ELEMENT_NODE: + case XML_ATTRIBUTE_NODE: + if (nodep->children) { + node_list_unlink(nodep->children TSRMLS_CC); + } + case XML_TEXT_NODE: + case XML_COMMENT_NODE: + case XML_CDATA_SECTION_NODE: + case XML_PI_NODE: + if (newval->type != IS_STRING) { + if(newval->refcount > 1) { + value_copy = *newval; + zval_copy_ctor(&value_copy); + newval = &value_copy; + } + convert_to_string(newval); + } + xmlNodeSetContentLen(nodep, Z_STRVAL_P(newval), Z_STRLEN_P(newval) + 1); + if (newval == &value_copy) { + zval_dtor(newval); + } + break; + default: + break; + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ nodeType int +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-111237558 +Since: +*/ +int dom_node_node_type_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + /* Specs dictate that they are both type XML_DOCUMENT_TYPE_NODE */ + if (nodep->type == XML_DTD_NODE) { + ZVAL_LONG(*retval, XML_DOCUMENT_TYPE_NODE); + } else { + ZVAL_LONG(*retval, nodep->type); + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ parentNode DomNode +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1060184317 +Since: +*/ +int dom_node_parent_node_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep, *nodeparent; + int ret; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + nodeparent = nodep->parent; + if (!nodeparent) { + ZVAL_NULL(*retval); + return SUCCESS; + } + + if (NULL == (*retval = php_dom_create_object(nodeparent, &ret, NULL, *retval, obj TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object"); + return FAILURE; + } + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ childNodes DomNodeList +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1451460987 +Since: +*/ +int dom_node_child_nodes_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + dom_object *intern; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + if (dom_node_children_valid(nodep) == FAILURE) { + ZVAL_NULL(*retval); + } else { + php_dom_create_interator(*retval, DOM_NODELIST TSRMLS_CC); + intern = (dom_object *)zend_objects_get_address(*retval TSRMLS_CC); + dom_namednode_iter(obj, XML_ELEMENT_NODE, intern, NULL, NULL, NULL TSRMLS_CC); + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ firstChild DomNode +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-169727388 +Since: +*/ +int dom_node_first_child_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep, *first = NULL; + int ret; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + if (dom_node_children_valid(nodep) == SUCCESS) { + first = nodep->children; + } + + ALLOC_ZVAL(*retval); + + if (!first) { + ZVAL_NULL(*retval); + return SUCCESS; + } + + if (NULL == (*retval = php_dom_create_object(first, &ret, NULL, *retval, obj TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object"); + return FAILURE; + } + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ lastChild DomNode +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-61AD09FB +Since: +*/ +int dom_node_last_child_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep, *last = NULL; + int ret; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + if (dom_node_children_valid(nodep) == SUCCESS) { + last = nodep->last; + } + + ALLOC_ZVAL(*retval); + + if (!last) { + ZVAL_NULL(*retval); + return SUCCESS; + } + + if (NULL == (*retval = php_dom_create_object(last, &ret, NULL, *retval, obj TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object"); + return FAILURE; + } + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ previousSibling DomNode +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-640FB3C8 +Since: +*/ +int dom_node_previous_sibling_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep, *prevsib; + int ret; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + prevsib = nodep->prev; + if (!prevsib) { + ZVAL_NULL(*retval); + return SUCCESS; + } + + if (NULL == (*retval = php_dom_create_object(prevsib, &ret, NULL, *retval, obj TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object"); + return FAILURE; + } + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ nextSibling DomNode +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6AC54C2F +Since: +*/ +int dom_node_next_sibling_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep, *nextsib; + int ret; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + nextsib = nodep->next; + if (!nextsib) { + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + if (NULL == (*retval = php_dom_create_object(nextsib, &ret, NULL, *retval, obj TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object"); + return FAILURE; + } + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ attributes DomNamedNodeMap +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-84CF096 +Since: +*/ +int dom_node_attributes_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + dom_object *intern; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + if (nodep->type == XML_ELEMENT_NODE) { + php_dom_create_interator(*retval, DOM_NAMEDNODEMAP TSRMLS_CC); + intern = (dom_object *)zend_objects_get_address(*retval TSRMLS_CC); + dom_namednode_iter(obj, XML_ATTRIBUTE_NODE, intern, NULL, NULL, NULL TSRMLS_CC); + } else { + ZVAL_NULL(*retval); + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ ownerDocument DomDocument +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-node-ownerDoc +Since: +*/ +int dom_node_owner_document_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + xmlDocPtr docp; + int ret; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) { + ALLOC_ZVAL(*retval); + ZVAL_NULL(*retval); + return SUCCESS; + } + + docp = nodep->doc; + if (!docp) { + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + if (NULL == (*retval = php_dom_create_object((xmlNodePtr) docp, &ret, NULL, *retval, obj TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object"); + return FAILURE; + } + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ namespaceUri string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSname +Since: DOM Level 2 +*/ +int dom_node_namespace_uri_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + char *str = NULL; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + switch (nodep->type) { + case XML_ELEMENT_NODE: + case XML_ATTRIBUTE_NODE: + case XML_NAMESPACE_DECL: + if (nodep->ns != NULL) { + str = (char *) nodep->ns->href; + } + break; + default: + str = NULL; + break; + } + + ALLOC_ZVAL(*retval); + + if(str != NULL) { + ZVAL_STRING(*retval, str, 1); + } else { + ZVAL_NULL(*retval); + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ prefix string +readonly=no +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSPrefix +Since: DOM Level 2 +*/ +int dom_node_prefix_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + xmlNsPtr ns; + char *str = NULL; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + switch (nodep->type) { + case XML_ELEMENT_NODE: + case XML_ATTRIBUTE_NODE: + case XML_NAMESPACE_DECL: + ns = nodep->ns; + if (ns != NULL && ns->prefix) { + str = (char *) ns->prefix; + } + break; + default: + str = NULL; + break; + } + + ALLOC_ZVAL(*retval); + + if (str == NULL) { + ZVAL_EMPTY_STRING(*retval); + } else { + ZVAL_STRING(*retval, str, 1); + } + return SUCCESS; + +} + +int dom_node_prefix_write(dom_object *obj, zval *newval TSRMLS_DC) +{ + zval value_copy; + xmlNode *nodep, *nsnode = NULL; + xmlNsPtr ns = NULL, curns; + char *strURI; + char *prefix; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + switch (nodep->type) { + case XML_ELEMENT_NODE: + nsnode = nodep; + case XML_ATTRIBUTE_NODE: + if (nsnode == NULL) { + nsnode = nodep->parent; + if (nsnode == NULL) { + nsnode = xmlDocGetRootElement(nodep->doc); + } + } + if (newval->type != IS_STRING) { + if(newval->refcount > 1) { + value_copy = *newval; + zval_copy_ctor(&value_copy); + newval = &value_copy; + } + convert_to_string(newval); + } + prefix = Z_STRVAL_P(newval); + if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) { + strURI = (char *) nodep->ns->href; + if (strURI == NULL || + (!strcmp (prefix, "xml") && strcmp(strURI, XML_XML_NAMESPACE)) || + (nodep->type == XML_ATTRIBUTE_NODE && !strcmp (prefix, "xmlns") && + strcmp (strURI, DOM_XMLNS_NAMESPACE)) || + (nodep->type == XML_ATTRIBUTE_NODE && !strcmp (nodep->name, "xmlns"))) { + ns = NULL; + } else { + curns = nsnode->nsDef; + while (curns != NULL) { + if (xmlStrEqual((xmlChar *)prefix, curns->prefix) && xmlStrEqual(nodep->ns->href, curns->href)) { + ns = curns; + break; + } + curns = curns->next; + } + if (ns == NULL) { + ns = xmlNewNs(nsnode, nodep->ns->href, (xmlChar *)prefix); + } + } + + if (ns == NULL) { + if (newval == &value_copy) { + zval_dtor(newval); + } + php_dom_throw_error(NAMESPACE_ERR, dom_get_strict_error(obj->document) TSRMLS_CC); + return FAILURE; + } + + xmlSetNs(nodep, ns); + } + if (newval == &value_copy) { + zval_dtor(newval); + } + break; + default: + break; + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ localName string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeNSLocalN +Since: DOM Level 2 +*/ +int dom_node_local_name_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + if (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE || nodep->type == XML_NAMESPACE_DECL) { + ZVAL_STRING(*retval, (char *) (nodep->name), 1); + } else { + ZVAL_NULL(*retval); + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ baseURI string +readonly=yes +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-baseURI +Since: DOM Level 3 +*/ +int dom_node_base_uri_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + xmlChar *baseuri; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + ALLOC_ZVAL(*retval); + + baseuri = xmlNodeGetBase(nodep->doc, nodep); + if (baseuri) { + ZVAL_STRING(*retval, (char *) (baseuri), 1); + xmlFree(baseuri); + } else { + ZVAL_NULL(*retval); + } + + return SUCCESS; +} + +/* }}} */ + + + +/* {{{ textContent string +readonly=no +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-textContent +Since: DOM Level 3 +*/ +int dom_node_text_content_read(dom_object *obj, zval **retval TSRMLS_DC) +{ + xmlNode *nodep; + char *str = NULL; + + nodep = dom_object_get_node(obj); + + if (nodep == NULL) { + php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); + return FAILURE; + } + + str = xmlNodeGetContent(nodep); + + ALLOC_ZVAL(*retval); + + if(str != NULL) { + ZVAL_STRING(*retval, str, 1); + xmlFree(str); + } else { + ZVAL_EMPTY_STRING(*retval); + } + + return SUCCESS; +} + +int dom_node_text_content_write(dom_object *obj, zval *newval TSRMLS_DC) +{ + return SUCCESS; +} + +/* }}} */ + + +static xmlNodePtr _php_dom_insert_fragment(xmlNodePtr nodep, xmlNodePtr prevsib, + xmlNodePtr nextsib, xmlNodePtr fragment, + dom_object *intern, dom_object *childobj TSRMLS_DC) +{ + xmlNodePtr newchild, node; + + newchild = fragment->children; + + if (newchild) { + if (prevsib == NULL) { + nodep->children = newchild; + } else { + prevsib->next = newchild; + } + newchild->prev = prevsib; + if (nextsib == NULL) { + nodep->last = fragment->last; + } else { + fragment->last->next = nextsib; + nextsib->prev = fragment->last; + } + + node = newchild; + while (node != NULL) { + node->parent = nodep; + if (node->doc != nodep->doc) { + xmlSetTreeDoc(node, nodep->doc); + if (node->_private != NULL) { + childobj = node->_private; + childobj->document = intern->document; + php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL TSRMLS_CC); + } + } + if (node == fragment->last) { + break; + } + node = node->next; + } + + fragment->children = NULL; + fragment->last = NULL; + } + + return newchild; +} + +/* {{{ proto domnode dom_node_insert_before(DomNode newChild, DomNode refChild); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727 +Since: +*/ +PHP_FUNCTION(dom_node_insert_before) +{ + zval *id, *node, *ref = NULL, *rv = NULL; + xmlNodePtr child, new_child, parentp, refp; + dom_object *intern, *childobj, *refpobj; + int ret, stricterror; + + 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) { + return; + } + + DOM_GET_OBJ(parentp, id, xmlNodePtr, intern); + + if (dom_node_children_valid(parentp) == FAILURE) { + RETURN_FALSE; + } + + DOM_GET_OBJ(child, node, xmlNodePtr, childobj); + + new_child = NULL; + + stricterror = dom_get_strict_error(intern->document); + + if (dom_node_is_read_only(parentp) == SUCCESS || + (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) { + php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (dom_hierarchy(parentp, child) == FAILURE) { + php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (child->doc != parentp->doc && child->doc != NULL) { + php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Document Fragment is empty"); + RETURN_FALSE; + } + + if (child->doc == NULL && parentp->doc != NULL) { + childobj->document = intern->document; + php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL TSRMLS_CC); + } + + if (ref != NULL) { + DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj); + if (refp->parent != parentp) { + php_dom_throw_error(NOT_FOUND_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (child->parent != NULL) { + xmlUnlinkNode(child); + } + + if (child->type == XML_TEXT_NODE && (refp->type == XML_TEXT_NODE || + (refp->prev != NULL && refp->prev->type == XML_TEXT_NODE))) { + if (child->doc == NULL) { + xmlSetTreeDoc(child, parentp->doc); + } + new_child = child; + new_child->parent = refp->parent; + new_child->next = refp; + new_child->prev = refp->prev; + refp->prev = new_child; + if (new_child->prev != NULL) { + new_child->prev->next = new_child; + } + if (new_child->parent != NULL) { + if (new_child->parent->children == refp) { + new_child->parent->children = new_child; + } + } + + } else if (child->type == XML_ATTRIBUTE_NODE) { + xmlAttrPtr lastattr; + + if (child->ns == NULL) + lastattr = xmlHasProp(refp->parent, child->name); + else + lastattr = xmlHasNsProp(refp->parent, child->name, child->ns->href); + if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) { + if (lastattr != (xmlAttrPtr) child) { + xmlUnlinkNode((xmlNodePtr) lastattr); + php_libxml_node_free_resource((xmlNodePtr) lastattr TSRMLS_CC); + } else { + DOM_RET_OBJ(rv, child, &ret, intern); + return; + } + } + } else if (child->type == XML_DOCUMENT_FRAG_NODE) { + new_child = _php_dom_insert_fragment(parentp, refp->prev, refp, child, intern, childobj TSRMLS_CC); + } + + if (new_child == NULL) { + new_child = xmlAddPrevSibling(refp, child); + } + } else { + if (child->parent != NULL){ + xmlUnlinkNode(child); + } + if (child->type == XML_TEXT_NODE && parentp->last != NULL && parentp->last->type == XML_TEXT_NODE) { + child->parent = parentp; + if (child->doc == NULL) { + xmlSetTreeDoc(child, parentp->doc); + } + new_child = child; + if (parentp->children == NULL) { + parentp->children = child; + parentp->last = child; + } else { + child = parentp->last; + child->next = new_child; + new_child->prev = child; + parentp->last = new_child; + } + } else if (child->type == XML_ATTRIBUTE_NODE) { + xmlAttrPtr lastattr; + + if (child->ns == NULL) + lastattr = xmlHasProp(parentp, child->name); + else + lastattr = xmlHasNsProp(parentp, child->name, child->ns->href); + if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) { + if (lastattr != (xmlAttrPtr) child) { + xmlUnlinkNode((xmlNodePtr) lastattr); + php_libxml_node_free_resource((xmlNodePtr) lastattr TSRMLS_CC); + } else { + DOM_RET_OBJ(rv, child, &ret, intern); + return; + } + } + } else if (child->type == XML_DOCUMENT_FRAG_NODE) { + new_child = _php_dom_insert_fragment(parentp, parentp->last, NULL, child, intern, childobj TSRMLS_CC); + } + if (new_child == NULL) { + new_child = xmlAddChild(parentp, child); + } + } + + if (NULL == new_child) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't add newnode as the previous sibling of refnode"); + RETURN_FALSE; + } + + dom_reconcile_ns(parentp->doc, new_child); + + DOM_RET_OBJ(rv, new_child, &ret, intern); + +} +/* }}} end dom_node_insert_before */ + + +/* {{{ proto DomNode dom_node_replace_child(DomNode newChild, DomNode oldChild); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-785887307 +Since: +*/ +PHP_FUNCTION(dom_node_replace_child) +{ + zval *id, *newnode, *oldnode; + xmlNodePtr children, newchild, oldchild, nodep; + dom_object *intern, *newchildobj, *oldchildobj; + int foundoldchild = 0, stricterror; + + int ret; + + 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) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + if (dom_node_children_valid(nodep) == FAILURE) { + RETURN_FALSE; + } + + DOM_GET_OBJ(newchild, newnode, xmlNodePtr, newchildobj); + DOM_GET_OBJ(oldchild, oldnode, xmlNodePtr, oldchildobj); + + children = nodep->children; + if (!children) { + RETURN_FALSE; + } + + stricterror = dom_get_strict_error(intern->document); + + if (dom_node_is_read_only(nodep) == SUCCESS || + (newchild->parent != NULL && dom_node_is_read_only(newchild->parent) == SUCCESS)) { + php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (newchild->doc != nodep->doc && newchild->doc != NULL) { + php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (dom_hierarchy(nodep, newchild) == FAILURE) { + php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + /* check for the old child and whether the new child is already a child */ + while (children) { + if (children == oldchild) { + foundoldchild = 1; + break; + } + children = children->next; + } + + if (foundoldchild) { + xmlNodePtr node; + zval *rv = NULL; + + if (newchild->type == XML_DOCUMENT_FRAG_NODE) { + xmlNodePtr prevsib, nextsib; + prevsib = oldchild->prev; + nextsib = oldchild->next; + + xmlUnlinkNode(oldchild); + + newchild = _php_dom_insert_fragment(nodep, prevsib, nextsib, newchild, intern, newchildobj TSRMLS_CC); + if (newchild) { + dom_reconcile_ns(nodep->doc, newchild); + } + } else if (oldchild != newchild) { + if (newchild->doc == NULL && nodep->doc != NULL) { + xmlSetTreeDoc(newchild, nodep->doc); + newchildobj->document = intern->document; + php_libxml_increment_doc_ref((php_libxml_node_object *)newchildobj, NULL TSRMLS_CC); + } + node = xmlReplaceNode(oldchild, newchild); + dom_reconcile_ns(nodep->doc, newchild); + } + DOM_RET_OBJ(rv, oldchild, &ret, intern); + return; + } else { + php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document) TSRMLS_CC); + RETURN_FALSE; + } +} +/* }}} end dom_node_replace_child */ + + +/* {{{ proto DomNode dom_node_remove_child(DomNode oldChild); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1734834066 +Since: +*/ +PHP_FUNCTION(dom_node_remove_child) +{ + zval *id, *node; + xmlNodePtr children, child, nodep; + dom_object *intern, *childobj; + int ret, stricterror; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_node_class_entry, &node, dom_node_class_entry) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + if (dom_node_children_valid(nodep) == FAILURE) { + RETURN_FALSE; + } + + DOM_GET_OBJ(child, node, xmlNodePtr, childobj); + + stricterror = dom_get_strict_error(intern->document); + + if (dom_node_is_read_only(nodep) == SUCCESS || + (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) { + php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + children = nodep->children; + if (!children) { + php_dom_throw_error(NOT_FOUND_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + while (children) { + if (children == child) { + zval *rv = NULL; + xmlUnlinkNode(child); + DOM_RET_OBJ(rv, child, &ret, intern); + return; + } + children = children->next; + } + + php_dom_throw_error(NOT_FOUND_ERR, stricterror TSRMLS_CC); + RETURN_FALSE +} +/* }}} end dom_node_remove_child */ + + +/* {{{ proto DomNode dom_node_append_child(DomNode newChild); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-184E7107 +Since: +*/ +PHP_FUNCTION(dom_node_append_child) +{ + zval *id, *node, *rv = NULL; + xmlNodePtr child, nodep, new_child = NULL; + dom_object *intern, *childobj; + int ret, stricterror; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_node_class_entry, &node, dom_node_class_entry) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + if (dom_node_children_valid(nodep) == FAILURE) { + RETURN_FALSE; + } + + DOM_GET_OBJ(child, node, xmlNodePtr, childobj); + + stricterror = dom_get_strict_error(intern->document); + + if (dom_node_is_read_only(nodep) == SUCCESS || + (child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) { + php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (dom_hierarchy(nodep, child) == FAILURE) { + php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (!(child->doc == NULL || child->doc == nodep->doc)) { + php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror TSRMLS_CC); + RETURN_FALSE; + } + + if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Document Fragment is empty"); + RETURN_FALSE; + } + + if (child->doc == NULL && nodep->doc != NULL) { + childobj->document = intern->document; + php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL TSRMLS_CC); + } + + if (child->parent != NULL){ + xmlUnlinkNode(child); + } + + if (child->type == XML_TEXT_NODE && nodep->last != NULL && nodep->last->type == XML_TEXT_NODE) { + child->parent = nodep; + if (child->doc == NULL) { + xmlSetTreeDoc(child, nodep->doc); + } + new_child = child; + if (nodep->children == NULL) { + nodep->children = child; + nodep->last = child; + } else { + child = nodep->last; + child->next = new_child; + new_child->prev = child; + nodep->last = new_child; + } + } else if (child->type == XML_ATTRIBUTE_NODE) { + xmlAttrPtr lastattr; + + if (child->ns == NULL) + lastattr = xmlHasProp(nodep, child->name); + else + lastattr = xmlHasNsProp(nodep, child->name, child->ns->href); + if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) { + if (lastattr != (xmlAttrPtr) child) { + xmlUnlinkNode((xmlNodePtr) lastattr); + php_libxml_node_free_resource((xmlNodePtr) lastattr TSRMLS_CC); + } + } + } else if (child->type == XML_DOCUMENT_FRAG_NODE) { + new_child = _php_dom_insert_fragment(nodep, nodep->last, NULL, child, intern, childobj TSRMLS_CC); + } + + if (new_child == NULL) { + new_child = xmlAddChild(nodep, child); + if (new_child == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't append node"); + RETURN_FALSE; + } + } + + dom_reconcile_ns(nodep->doc, new_child); + + DOM_RET_OBJ(rv, new_child, &ret, intern); +} +/* }}} end dom_node_append_child */ + + +/* {{{ proto boolean dom_node_has_child_nodes(); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-810594187 +Since: +*/ +PHP_FUNCTION(dom_node_has_child_nodes) +{ + zval *id; + xmlNode *nodep; + dom_object *intern; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_node_class_entry) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + if (dom_node_children_valid(nodep) == FAILURE) { + RETURN_FALSE; + } + + if (nodep->children) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} end dom_node_has_child_nodes */ + + +/* {{{ proto DomNode dom_node_clone_node(boolean deep); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3A0ED0A4 +Since: +*/ +PHP_FUNCTION(dom_node_clone_node) +{ + zval *rv = NULL; + zval *id; + xmlNode *n, *node; + int ret; + dom_object *intern; + long recursive = 0; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l", &id, dom_node_class_entry, &recursive) == FAILURE) { + return; + } + + DOM_GET_OBJ(n, id, xmlNodePtr, intern); + + node = xmlDocCopyNode(n, n->doc, recursive); + + if (!node) { + RETURN_FALSE; + } + + /* When deep is false Element nodes still require the attributes + Following taken from libxml as xmlDocCopyNode doesnt do this */ + if (n->type == XML_ELEMENT_NODE && recursive == 0) { + if (n->nsDef != NULL) { + node->nsDef = xmlCopyNamespaceList(n->nsDef); + } + if (n->ns != NULL) { + xmlNsPtr ns; + ns = xmlSearchNs(n->doc, node, n->ns->prefix); + if (ns == NULL) { + ns = xmlSearchNs(n->doc, n, n->ns->prefix); + if (ns != NULL) { + xmlNodePtr root = node; + + while (root->parent != NULL) { + root = root->parent; + } + node->ns = xmlNewNs(root, ns->href, ns->prefix); + } + } else { + node->ns = ns; + } + } + if (n->properties != NULL) { + node->properties = xmlCopyPropList(node, n->properties); + } + } + + /* If document cloned we want a new document proxy */ + if (node->doc != n->doc) { + intern = NULL; + } + + DOM_RET_OBJ(rv, node, &ret, intern); +} +/* }}} end dom_node_clone_node */ + + + +/* {{{ proto void dom_node_normalize(); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-normalize +Since: +*/ +PHP_FUNCTION(dom_node_normalize) +{ + zval *id; + xmlNode *nodep; + dom_object *intern; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_node_class_entry) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + dom_normalize(nodep TSRMLS_CC); + +} +/* }}} end dom_node_normalize */ + + +/* {{{ proto boolean dom_node_is_supported(string feature, string version); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Level-2-Core-Node-supports +Since: DOM Level 2 +*/ +PHP_FUNCTION(dom_node_is_supported) +{ + zval *id; + int feature_len, version_len; + char *feature, *version; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss", &id, dom_node_class_entry, &feature, &feature_len, &version, &version_len) == FAILURE) { + return; + } + + if (dom_has_feature(feature, version)) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} end dom_node_is_supported */ + + +/* {{{ proto boolean dom_node_has_attributes(); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-NodeHasAttrs +Since: DOM Level 2 +*/ +PHP_FUNCTION(dom_node_has_attributes) +{ + zval *id; + xmlNode *nodep; + dom_object *intern; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_node_class_entry) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + if (nodep->type != XML_ELEMENT_NODE) + RETURN_FALSE; + + if (nodep->properties) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} end dom_node_has_attributes */ + +/* {{{ proto short dom_node_compare_document_position(DomNode other); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-compareDocumentPosition +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_compare_document_position) +{ + DOM_NOT_IMPLEMENTED(); +} +/* }}} end dom_node_compare_document_position */ + + +/* {{{ proto boolean dom_node_is_same_node(DomNode other); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-isSameNode +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_is_same_node) +{ + zval *id, *node; + xmlNodePtr nodeotherp, nodep; + dom_object *intern, *nodeotherobj; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_node_class_entry, &node, dom_node_class_entry) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + DOM_GET_OBJ(nodeotherp, node, xmlNodePtr, nodeotherobj); + + if (nodep == nodeotherp) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} end dom_node_is_same_node */ + + +/* {{{ proto string dom_node_lookup_prefix(string namespaceURI); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-lookupNamespacePrefix +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_lookup_prefix) +{ + zval *id; + xmlNodePtr nodep, lookupp = NULL; + dom_object *intern; + xmlNsPtr nsptr; + int uri_len = 0; + char *uri; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_node_class_entry, &uri, &uri_len) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + if (uri_len > 0) { + switch (nodep->type) { + case XML_ELEMENT_NODE: + lookupp = nodep; + break; + case XML_DOCUMENT_NODE: + case XML_HTML_DOCUMENT_NODE: + lookupp = xmlDocGetRootElement((xmlDocPtr) nodep); + break; + case XML_ENTITY_NODE : + case XML_NOTATION_NODE: + case XML_DOCUMENT_FRAG_NODE: + case XML_DOCUMENT_TYPE_NODE: + case XML_DTD_NODE: + RETURN_NULL(); + break; + default: + lookupp = nodep->parent; + } + + if (lookupp != NULL && (nsptr = xmlSearchNsByHref(lookupp->doc, lookupp, uri))) { + if (nsptr->prefix != NULL) { + RETURN_STRING((char *) nsptr->prefix, 1); + } + } + } + + RETURN_NULL(); +} +/* }}} end dom_node_lookup_prefix */ + + +/* {{{ proto boolean dom_node_is_default_namespace(string namespaceURI); +URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isDefaultNamespace +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_is_default_namespace) +{ + zval *id; + xmlNodePtr nodep; + dom_object *intern; + xmlNsPtr nsptr; + int uri_len = 0; + char *uri; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_node_class_entry, &uri, &uri_len) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) { + nodep = xmlDocGetRootElement((xmlDocPtr) nodep); + } + + if (nodep && uri_len > 0) { + nsptr = xmlSearchNs(nodep->doc, nodep, NULL); + if (nsptr && xmlStrEqual(nsptr->href, uri)) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} end dom_node_is_default_namespace */ + + +/* {{{ proto string dom_node_lookup_namespace_uri(string prefix); +URL: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_lookup_namespace_uri) +{ + zval *id; + xmlNodePtr nodep; + dom_object *intern; + xmlNsPtr nsptr; + int prefix_len = 0; + char *prefix=NULL; + + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!", &id, dom_node_class_entry, &prefix, &prefix_len) == FAILURE) { + return; + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) { + nodep = xmlDocGetRootElement((xmlDocPtr) nodep); + if (nodep == NULL) { + RETURN_NULL(); + } + } + + nsptr = xmlSearchNs(nodep->doc, nodep, prefix); + if (nsptr && nsptr->href != NULL) { + RETURN_STRING((char *) nsptr->href, 1); + } + + RETURN_NULL(); +} +/* }}} end dom_node_lookup_namespace_uri */ + + +/* {{{ proto boolean dom_node_is_equal_node(DomNode arg); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-isEqualNode +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_is_equal_node) +{ + DOM_NOT_IMPLEMENTED(); +} +/* }}} end dom_node_is_equal_node */ + + +/* {{{ proto DomNode dom_node_get_feature(string feature, string version); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-getFeature +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_get_feature) +{ + DOM_NOT_IMPLEMENTED(); +} +/* }}} end dom_node_get_feature */ + + +/* {{{ proto mixed dom_node_set_user_data(string key, mixed data, userdatahandler handler); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-setUserData +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_set_user_data) +{ + DOM_NOT_IMPLEMENTED(); +} +/* }}} end dom_node_set_user_data */ + + +/* {{{ proto mixed dom_node_get_user_data(string key); +URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-getUserData +Since: DOM Level 3 +*/ +PHP_FUNCTION(dom_node_get_user_data) +{ + DOM_NOT_IMPLEMENTED(); +} +/* }}} end dom_node_get_user_data */ + + +static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) +{ + zval *id; + zval *xpath_array=NULL, *ns_prefixes=NULL; + xmlNodePtr nodep; + xmlDocPtr docp; + xmlNodeSetPtr nodeset = NULL; + dom_object *intern; + zend_bool exclusive=0, with_comments=0; + xmlChar **inclusive_ns_prefixes = NULL; + char *file = NULL; + int ret = -1, file_len = 0; + xmlOutputBufferPtr buf; + xmlXPathContextPtr ctxp=NULL; + xmlXPathObjectPtr xpathobjp=NULL; + + if (mode == 0) { + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "O|bba!a!", &id, dom_node_class_entry, &exclusive, &with_comments, + &xpath_array, &ns_prefixes) == FAILURE) { + return; + } + } else { + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), + "Os|bba!a!", &id, dom_node_class_entry, &file, &file_len, &exclusive, + &with_comments, &xpath_array, &ns_prefixes) == FAILURE) { + return; + } + } + + DOM_GET_OBJ(nodep, id, xmlNodePtr, intern); + + docp = nodep->doc; + + if (! docp) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Node must be associated with a document"); + RETURN_FALSE; + } + + if (xpath_array == NULL) { + if (nodep->type != XML_DOCUMENT_NODE) { + ctxp = xmlXPathNewContext(docp); + ctxp->node = nodep; + xpathobjp = xmlXPathEvalExpression("(.//. | .//@* | .//namespace::*)", ctxp); + ctxp->node = NULL; + if (xpathobjp && xpathobjp->type == XPATH_NODESET) { + nodeset = xpathobjp->nodesetval; + } else { + if (xpathobjp) { + xmlXPathFreeObject(xpathobjp); + } + xmlXPathFreeContext(ctxp); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "XPath query did not return a nodeset."); + RETURN_FALSE; + } + } + } else { + /*xpath query from xpath_array */ + HashTable *ht = Z_ARRVAL_P(xpath_array); + zval **tmp; + char *xquery; + + if (zend_hash_find(ht, "query", sizeof("query"), (void**)&tmp) == SUCCESS && + Z_TYPE_PP(tmp) == IS_STRING) { + xquery = Z_STRVAL_PP(tmp); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "'query' missing from xpath array or is not a string"); + RETURN_FALSE; + } + + ctxp = xmlXPathNewContext(docp); + ctxp->node = nodep; + + if (zend_hash_find(ht, "namespaces", sizeof("namespaces"), (void**)&tmp) == SUCCESS && + Z_TYPE_PP(tmp) == IS_ARRAY) { + zval **tmpns; + while (zend_hash_get_current_data(Z_ARRVAL_PP(tmp), (void **)&tmpns) == SUCCESS) { + if (Z_TYPE_PP(tmpns) == IS_STRING) { + char *prefix; + ulong idx; + int prefix_key_len; + + if (zend_hash_get_current_key_ex(Z_ARRVAL_PP(tmp), + &prefix, &prefix_key_len, &idx, 0, NULL) == HASH_KEY_IS_STRING) { + xmlXPathRegisterNs(ctxp, prefix, Z_STRVAL_PP(tmpns)); + } + } + zend_hash_move_forward(Z_ARRVAL_PP(tmp)); + } + } + + xpathobjp = xmlXPathEvalExpression(xquery, ctxp); + ctxp->node = NULL; + if (xpathobjp && xpathobjp->type == XPATH_NODESET) { + nodeset = xpathobjp->nodesetval; + } else { + if (xpathobjp) { + xmlXPathFreeObject(xpathobjp); + } + xmlXPathFreeContext(ctxp); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "XPath query did not return a nodeset."); + RETURN_FALSE; + } + } + + if (ns_prefixes != NULL) { + if (exclusive) { + zval **tmpns; + int nscount = 0; + + inclusive_ns_prefixes = safe_emalloc(zend_hash_num_elements(Z_ARRVAL_P(ns_prefixes)) + 1, + sizeof(xmlChar *), 0); + while (zend_hash_get_current_data(Z_ARRVAL_P(ns_prefixes), (void **)&tmpns) == SUCCESS) { + if (Z_TYPE_PP(tmpns) == IS_STRING) { + inclusive_ns_prefixes[nscount++] = Z_STRVAL_PP(tmpns); + } + zend_hash_move_forward(Z_ARRVAL_P(ns_prefixes)); + } + inclusive_ns_prefixes[nscount] = NULL; + } else { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, + "Inclusive namespace prefixes only allowed in exclusive mode."); + } + } + + if (mode == 1) { + buf = xmlOutputBufferCreateFilename(file, NULL, 0); + } else { + buf = xmlAllocOutputBuffer(NULL); + } + + if (buf != NULL) { + ret = xmlC14NDocSaveTo(docp, nodeset, exclusive, inclusive_ns_prefixes, + with_comments, buf); + } + + if (inclusive_ns_prefixes != NULL) { + efree(inclusive_ns_prefixes); + } + if (xpathobjp != NULL) { + xmlXPathFreeObject(xpathobjp); + } + if (ctxp != NULL) { + xmlXPathFreeContext(ctxp); + } + + if (buf == NULL || ret < 0) { + RETVAL_FALSE; + } else { + if (mode == 0) { +#ifdef LIBXML2_NEW_BUFFER + ret = xmlOutputBufferGetSize(buf); +#else + ret = buf->buffer->use; +#endif + if (ret > 0) { +#ifdef LIBXML2_NEW_BUFFER + RETVAL_STRINGL((char *) xmlOutputBufferGetContent(buf), ret, 1); +#else + RETVAL_STRINGL((char *) buf->buffer->content, ret, 1); +#endif + } else { + RETVAL_EMPTY_STRING(); + } + } + } + + if (buf) { + int bytes; + + bytes = xmlOutputBufferClose(buf); + if (mode == 1 && (ret >= 0)) { + RETURN_LONG(bytes); + } + } +} + +/* {{{ proto string DOMNode::C14N([bool exclusive [, bool with_comments [, array xpath [, array ns_prefixes]]]]) + Canonicalize nodes to a string */ +PHP_METHOD(domnode, C14N) +{ + dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} + +/* {{{ proto int DOMNode::C14NFile(string uri [, bool exclusive [, bool with_comments [, array xpath [, array ns_prefixes]]]]) + Canonicalize nodes to a file */ +PHP_METHOD(domnode, C14NFile) +{ + dom_canonicalization(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); +} + +#endif + +/* {{{ proto int DOMNode::getNodePath() + Gets an xpath for a node */ + +PHP_METHOD(domnode, getNodePath) +{ + zval *id; + xmlNode *nodep; + dom_object *intern; + char *value; + + + + DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern); + + value = xmlGetNodePath(nodep); + if (value == NULL) { + RETURN_NULL(); + } else { + RETVAL_STRING(value, 1); + xmlFree(value); + } + + +} + diff --git a/plugins/php/versions/52/lib/php_xmlwriter.c b/plugins/php/versions/52/lib/php_xmlwriter.c new file mode 100644 index 000000000..c401f7fab --- /dev/null +++ b/plugins/php/versions/52/lib/php_xmlwriter.c @@ -0,0 +1,1696 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Rob Richards | + | Pierre-A. Joye | + +----------------------------------------------------------------------+ +*/ + +/* $Id: php_xmlwriter.c 293036 2010-01-03 09:23:27Z sebastian $ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + + +#include "php.h" +#include "php_ini.h" +#include "ext/standard/info.h" +#include "ext/standard/php_string.h" +#include "php_xmlwriter.h" + + +#if LIBXML_VERSION >= 20605 +static PHP_FUNCTION(xmlwriter_set_indent); +static PHP_FUNCTION(xmlwriter_set_indent_string); +#endif +static PHP_FUNCTION(xmlwriter_start_attribute); +static PHP_FUNCTION(xmlwriter_end_attribute); +static PHP_FUNCTION(xmlwriter_write_attribute); +#if LIBXML_VERSION > 20617 +static PHP_FUNCTION(xmlwriter_start_attribute_ns); +static PHP_FUNCTION(xmlwriter_write_attribute_ns); +#endif +static PHP_FUNCTION(xmlwriter_start_element); +static PHP_FUNCTION(xmlwriter_end_element); +static PHP_FUNCTION(xmlwriter_full_end_element); +static PHP_FUNCTION(xmlwriter_start_element_ns); +static PHP_FUNCTION(xmlwriter_write_element); +static PHP_FUNCTION(xmlwriter_write_element_ns); +static PHP_FUNCTION(xmlwriter_start_pi); +static PHP_FUNCTION(xmlwriter_end_pi); +static PHP_FUNCTION(xmlwriter_write_pi); +static PHP_FUNCTION(xmlwriter_start_cdata); +static PHP_FUNCTION(xmlwriter_end_cdata); +static PHP_FUNCTION(xmlwriter_write_cdata); +static PHP_FUNCTION(xmlwriter_text); +static PHP_FUNCTION(xmlwriter_write_raw); +static PHP_FUNCTION(xmlwriter_start_document); +static PHP_FUNCTION(xmlwriter_end_document); +#if LIBXML_VERSION >= 20607 +static PHP_FUNCTION(xmlwriter_start_comment); +static PHP_FUNCTION(xmlwriter_end_comment); +#endif +static PHP_FUNCTION(xmlwriter_write_comment); +static PHP_FUNCTION(xmlwriter_start_dtd); +static PHP_FUNCTION(xmlwriter_end_dtd); +static PHP_FUNCTION(xmlwriter_write_dtd); +static PHP_FUNCTION(xmlwriter_start_dtd_element); +static PHP_FUNCTION(xmlwriter_end_dtd_element); +static PHP_FUNCTION(xmlwriter_write_dtd_element); +#if LIBXML_VERSION > 20608 +static PHP_FUNCTION(xmlwriter_start_dtd_attlist); +static PHP_FUNCTION(xmlwriter_end_dtd_attlist); +static PHP_FUNCTION(xmlwriter_write_dtd_attlist); +static PHP_FUNCTION(xmlwriter_start_dtd_entity); +static PHP_FUNCTION(xmlwriter_end_dtd_entity); +static PHP_FUNCTION(xmlwriter_write_dtd_entity); +#endif +static PHP_FUNCTION(xmlwriter_open_uri); +static PHP_FUNCTION(xmlwriter_open_memory); +static PHP_FUNCTION(xmlwriter_output_memory); +static PHP_FUNCTION(xmlwriter_flush); + +static zend_class_entry *xmlwriter_class_entry_ce; + +static void xmlwriter_free_resource_ptr(xmlwriter_object *intern TSRMLS_DC); +static void xmlwriter_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC); + +typedef int (*xmlwriter_read_one_char_t)(xmlTextWriterPtr writer, const xmlChar *content); +typedef int (*xmlwriter_read_int_t)(xmlTextWriterPtr writer); + +/* {{{ xmlwriter_object_free_storage */ +static void xmlwriter_free_resource_ptr(xmlwriter_object *intern TSRMLS_DC) +{ + if (intern) { + if (intern->ptr) { + xmlFreeTextWriter(intern->ptr); + intern->ptr = NULL; + } + if (intern->output) { + xmlBufferFree(intern->output); + intern->output = NULL; + } + efree(intern); + } +} +/* }}} */ + +#ifdef ZEND_ENGINE_2 +/* {{{ XMLWRITER_FROM_OBJECT */ +#define XMLWRITER_FROM_OBJECT(intern, object) \ + { \ + ze_xmlwriter_object *obj = (ze_xmlwriter_object*) zend_object_store_get_object(object TSRMLS_CC); \ + intern = obj->xmlwriter_ptr; \ + if (!intern) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or unitialized XMLWriter object"); \ + RETURN_FALSE; \ + } \ + } +/* }}} */ + +static zend_object_handlers xmlwriter_object_handlers; + +/* {{{ xmlwriter_object_free_storage */ +static void xmlwriter_object_free_storage(void *object TSRMLS_DC) +{ + ze_xmlwriter_object * intern = (ze_xmlwriter_object *) object; + if (!intern) { + return; + } + if (intern->xmlwriter_ptr) { + xmlwriter_free_resource_ptr(intern->xmlwriter_ptr TSRMLS_CC); + } + intern->xmlwriter_ptr = NULL; + zend_object_std_dtor(&intern->zo TSRMLS_CC); + + efree(intern); +} +/* }}} */ + + +/* {{{ xmlwriter_object_new */ +static zend_object_value xmlwriter_object_new(zend_class_entry *class_type TSRMLS_DC) +{ + ze_xmlwriter_object *intern; + zval *tmp; + zend_object_value retval; + + intern = emalloc(sizeof(ze_xmlwriter_object)); + memset(&intern->zo, 0, sizeof(zend_object)); + intern->xmlwriter_ptr = NULL; + + zend_object_std_init(&intern->zo, class_type TSRMLS_CC); + zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, + (void *) &tmp, sizeof(zval *)); + + retval.handle = zend_objects_store_put(intern, + NULL, + (zend_objects_free_object_storage_t) xmlwriter_object_free_storage, + NULL TSRMLS_CC); + + retval.handlers = (zend_object_handlers *) & xmlwriter_object_handlers; + + return retval; +} +/* }}} */ +#endif + +#define XMLW_NAME_CHK(__err) \ + if (xmlValidateName((xmlChar *) name, 0) != 0) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", __err); \ + RETURN_FALSE; \ + } \ + +/* {{{ xmlwriter_functions */ +static zend_function_entry xmlwriter_functions[] = { + PHP_FE(xmlwriter_open_uri, NULL) + PHP_FE(xmlwriter_open_memory, NULL) +#if LIBXML_VERSION >= 20605 + PHP_FE(xmlwriter_set_indent, NULL) + PHP_FE(xmlwriter_set_indent_string, NULL) +#endif +#if LIBXML_VERSION >= 20607 + PHP_FE(xmlwriter_start_comment, NULL) + PHP_FE(xmlwriter_end_comment, NULL) +#endif + PHP_FE(xmlwriter_start_attribute, NULL) + PHP_FE(xmlwriter_end_attribute, NULL) + PHP_FE(xmlwriter_write_attribute, NULL) +#if LIBXML_VERSION > 20617 + PHP_FE(xmlwriter_start_attribute_ns,NULL) + PHP_FE(xmlwriter_write_attribute_ns,NULL) +#endif + PHP_FE(xmlwriter_start_element, NULL) + PHP_FE(xmlwriter_end_element, NULL) + PHP_FE(xmlwriter_full_end_element, NULL) + PHP_FE(xmlwriter_start_element_ns, NULL) + PHP_FE(xmlwriter_write_element, NULL) + PHP_FE(xmlwriter_write_element_ns, NULL) + PHP_FE(xmlwriter_start_pi, NULL) + PHP_FE(xmlwriter_end_pi, NULL) + PHP_FE(xmlwriter_write_pi, NULL) + PHP_FE(xmlwriter_start_cdata, NULL) + PHP_FE(xmlwriter_end_cdata, NULL) + PHP_FE(xmlwriter_write_cdata, NULL) + PHP_FE(xmlwriter_text, NULL) + PHP_FE(xmlwriter_write_raw, NULL) + PHP_FE(xmlwriter_start_document, NULL) + PHP_FE(xmlwriter_end_document, NULL) + PHP_FE(xmlwriter_write_comment, NULL) + PHP_FE(xmlwriter_start_dtd, NULL) + PHP_FE(xmlwriter_end_dtd, NULL) + PHP_FE(xmlwriter_write_dtd, NULL) + PHP_FE(xmlwriter_start_dtd_element, NULL) + PHP_FE(xmlwriter_end_dtd_element, NULL) + PHP_FE(xmlwriter_write_dtd_element, NULL) +#if LIBXML_VERSION > 20608 + PHP_FE(xmlwriter_start_dtd_attlist, NULL) + PHP_FE(xmlwriter_end_dtd_attlist, NULL) + PHP_FE(xmlwriter_write_dtd_attlist, NULL) + PHP_FE(xmlwriter_start_dtd_entity, NULL) + PHP_FE(xmlwriter_end_dtd_entity, NULL) + PHP_FE(xmlwriter_write_dtd_entity, NULL) +#endif + PHP_FE(xmlwriter_output_memory, NULL) + PHP_FE(xmlwriter_flush, NULL) + {NULL, NULL, NULL} +}; +/* }}} */ + +#ifdef ZEND_ENGINE_2 +/* {{{ xmlwriter_class_functions */ +static zend_function_entry xmlwriter_class_functions[] = { + PHP_ME_MAPPING(openUri, xmlwriter_open_uri, NULL, 0) + PHP_ME_MAPPING(openMemory, xmlwriter_open_memory, NULL, 0) +#if LIBXML_VERSION >= 20605 + PHP_ME_MAPPING(setIndent, xmlwriter_set_indent, NULL, 0) + PHP_ME_MAPPING(setIndentString, xmlwriter_set_indent_string, NULL, 0) +#endif +#if LIBXML_VERSION >= 20607 + PHP_ME_MAPPING(startComment, xmlwriter_start_comment, NULL, 0) + PHP_ME_MAPPING(endComment, xmlwriter_end_comment, NULL, 0) +#endif + PHP_ME_MAPPING(startAttribute, xmlwriter_start_attribute, NULL, 0) + PHP_ME_MAPPING(endAttribute, xmlwriter_end_attribute, NULL, 0) + PHP_ME_MAPPING(writeAttribute, xmlwriter_write_attribute, NULL, 0) +#if LIBXML_VERSION > 20617 + PHP_ME_MAPPING(startAttributeNs, xmlwriter_start_attribute_ns,NULL, 0) + PHP_ME_MAPPING(writeAttributeNs, xmlwriter_write_attribute_ns,NULL, 0) +#endif + PHP_ME_MAPPING(startElement, xmlwriter_start_element, NULL, 0) + PHP_ME_MAPPING(endElement, xmlwriter_end_element, NULL, 0) + PHP_ME_MAPPING(fullEndElement, xmlwriter_full_end_element, NULL, 0) + PHP_ME_MAPPING(startElementNs, xmlwriter_start_element_ns, NULL, 0) + PHP_ME_MAPPING(writeElement, xmlwriter_write_element, NULL, 0) + PHP_ME_MAPPING(writeElementNs, xmlwriter_write_element_ns, NULL, 0) + PHP_ME_MAPPING(startPi, xmlwriter_start_pi, NULL, 0) + PHP_ME_MAPPING(endPi, xmlwriter_end_pi, NULL, 0) + PHP_ME_MAPPING(writePi, xmlwriter_write_pi, NULL, 0) + PHP_ME_MAPPING(startCdata, xmlwriter_start_cdata, NULL, 0) + PHP_ME_MAPPING(endCdata, xmlwriter_end_cdata, NULL, 0) + PHP_ME_MAPPING(writeCdata, xmlwriter_write_cdata, NULL, 0) + PHP_ME_MAPPING(text, xmlwriter_text, NULL, 0) + PHP_ME_MAPPING(writeRaw, xmlwriter_write_raw, NULL, 0) + PHP_ME_MAPPING(startDocument, xmlwriter_start_document, NULL, 0) + PHP_ME_MAPPING(endDocument, xmlwriter_end_document, NULL, 0) + PHP_ME_MAPPING(writeComment, xmlwriter_write_comment, NULL, 0) + PHP_ME_MAPPING(startDtd, xmlwriter_start_dtd, NULL, 0) + PHP_ME_MAPPING(endDtd, xmlwriter_end_dtd, NULL, 0) + PHP_ME_MAPPING(writeDtd, xmlwriter_write_dtd, NULL, 0) + PHP_ME_MAPPING(startDtdElement, xmlwriter_start_dtd_element, NULL, 0) + PHP_ME_MAPPING(endDtdElement, xmlwriter_end_dtd_element, NULL, 0) + PHP_ME_MAPPING(writeDtdElement, xmlwriter_write_dtd_element, NULL, 0) +#if LIBXML_VERSION > 20608 + PHP_ME_MAPPING(startDtdAttlist, xmlwriter_start_dtd_attlist, NULL, 0) + PHP_ME_MAPPING(endDtdAttlist, xmlwriter_end_dtd_attlist, NULL, 0) + PHP_ME_MAPPING(writeDtdAttlist, xmlwriter_write_dtd_attlist, NULL, 0) + PHP_ME_MAPPING(startDtdEntity, xmlwriter_start_dtd_entity, NULL, 0) + PHP_ME_MAPPING(endDtdEntity, xmlwriter_end_dtd_entity, NULL, 0) + PHP_ME_MAPPING(writeDtdEntity, xmlwriter_write_dtd_entity, NULL, 0) +#endif + PHP_ME_MAPPING(outputMemory, xmlwriter_output_memory, NULL, 0) + PHP_ME_MAPPING(flush, xmlwriter_flush, NULL, 0) + {NULL, NULL, NULL} +}; +/* }}} */ +#endif + +/* {{{ function prototypes */ +static PHP_MINIT_FUNCTION(xmlwriter); +static PHP_MSHUTDOWN_FUNCTION(xmlwriter); +static PHP_MINFO_FUNCTION(xmlwriter); + +static int le_xmlwriter; +/* }}} */ + +/* _xmlwriter_get_valid_file_path should be made a + common function in libxml extension as code is common to a few xml extensions */ +/* {{{ _xmlwriter_get_valid_file_path */ +static char *_xmlwriter_get_valid_file_path(char *source, char *resolved_path, int resolved_path_len TSRMLS_DC) { + xmlURI *uri; + xmlChar *escsource; + char *file_dest; + int isFileUri = 0; + + uri = xmlCreateURI(); + escsource = xmlURIEscapeStr((xmlChar *)source, (xmlChar *) ":"); + xmlParseURIReference(uri, (char *)escsource); + xmlFree(escsource); + + if (uri->scheme != NULL) { + /* absolute file uris - libxml only supports localhost or empty host */ + if (strncasecmp(source, "file:///", 8) == 0) { + if (source[sizeof("file:///") - 1] == '\0') { + return NULL; + } + isFileUri = 1; +#ifdef PHP_WIN32 + source += 8; +#else + source += 7; +#endif + } else if (strncasecmp(source, "file://localhost/",17) == 0) { + if (source[sizeof("file://localhost/") - 1] == '\0') { + return NULL; + } + + isFileUri = 1; +#ifdef PHP_WIN32 + source += 17; +#else + source += 16; +#endif + } + } + + file_dest = source; + + if ((uri->scheme == NULL || isFileUri)) { + char file_dirname[MAXPATHLEN]; + size_t dir_len; + + if (!VCWD_REALPATH(source, resolved_path) && !expand_filepath(source, resolved_path TSRMLS_CC)) { + xmlFreeURI(uri); + return NULL; + } + + memcpy(file_dirname, source, strlen(source)); + dir_len = php_dirname(file_dirname, strlen(source)); + + if (dir_len > 0) { + struct stat buf; + if (php_sys_stat(file_dirname, &buf) != 0) { + xmlFreeURI(uri); + return NULL; + } + } + + file_dest = resolved_path; + } else { + file_dest = source; + } + + xmlFreeURI(uri); + + return file_dest; +} +/* }}} */ + +#ifndef ZEND_ENGINE_2 +/* Channel libxml file io layer through the PHP streams subsystem. + * This allows use of ftps:// and https:// urls */ + +/* {{{ php_xmlwriter_streams_IO_open_write_wrapper */ +static void *php_xmlwriter_streams_IO_open_write_wrapper(const char *filename TSRMLS_DC) +{ + php_stream_wrapper *wrapper = NULL; + void *ret_val = NULL; + + ret_val = php_stream_open_wrapper_ex((char *)filename, "wb", ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL, NULL); + return ret_val; +} +/* }}} */ + +/* {{{ php_xmlwriter_streams_IO_write */ +static int php_xmlwriter_streams_IO_write(void *context, const char *buffer, int len) +{ + TSRMLS_FETCH(); + return php_stream_write((php_stream*)context, buffer, len); +} +/* }}} */ + +/* {{{ php_xmlwriter_streams_IO_close */ +static int php_xmlwriter_streams_IO_close(void *context) +{ + TSRMLS_FETCH(); + return php_stream_close((php_stream*)context); +} +/* }}} */ +#endif + +/* {{{ xmlwriter_module_entry + */ +zend_module_entry xmlwriter_module_entry = { + STANDARD_MODULE_HEADER, + "xmlwriter", + xmlwriter_functions, + PHP_MINIT(xmlwriter), + PHP_MSHUTDOWN(xmlwriter), + NULL, + NULL, + PHP_MINFO(xmlwriter), + "0.1", + STANDARD_MODULE_PROPERTIES +}; +/* }}} */ + +#ifdef COMPILE_DL_XMLWRITER +ZEND_GET_MODULE(xmlwriter) +#endif + +/* {{{ xmlwriter_objects_clone +static void xmlwriter_objects_clone(void *object, void **object_clone TSRMLS_DC) +{ + TODO +} +}}} */ + +/* {{{ xmlwriter_dtor */ +static void xmlwriter_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { + xmlwriter_object *intern; + + intern = (xmlwriter_object *) rsrc->ptr; + xmlwriter_free_resource_ptr(intern TSRMLS_CC); +} +/* }}} */ + +static void php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAMETERS, xmlwriter_read_one_char_t internal_function, char *err_string) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name; + int name_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pind, &name, &name_len) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + if (err_string != NULL) { + XMLW_NAME_CHK(err_string); + } + + ptr = intern->ptr; + + if (ptr) { + retval = internal_function(ptr, (xmlChar *) name); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} + +static void php_xmlwriter_end(INTERNAL_FUNCTION_PARAMETERS, xmlwriter_read_int_t internal_function) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + int retval; +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + XMLWRITER_FROM_OBJECT(intern, this); + if (ZEND_NUM_ARGS()) { + WRONG_PARAM_COUNT; + } + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pind) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + ptr = intern->ptr; + + if (ptr) { + retval = internal_function(ptr); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} + +#if LIBXML_VERSION >= 20605 +/* {{{ proto bool xmlwriter_set_indent(resource xmlwriter, bool indent) +Toggle indentation on/off - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_set_indent) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + int retval; + zend_bool indent; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &indent) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &pind, &indent) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + + ptr = intern->ptr; + if (ptr) { + retval = xmlTextWriterSetIndent(ptr, indent); + if (retval == 0) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_set_indent_string(resource xmlwriter, string indentString) +Set string used for indenting - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_set_indent_string) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterSetIndentString, NULL); +} +/* }}} */ + +#endif + +/* {{{ proto bool xmlwriter_start_attribute(resource xmlwriter, string name) +Create start attribute - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_attribute) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartAttribute, "Invalid Attribute Name"); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_attribute(resource xmlwriter) +End attribute - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_attribute) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndAttribute); +} +/* }}} */ + +#if LIBXML_VERSION > 20617 +/* {{{ proto bool xmlwriter_start_attribute_ns(resource xmlwriter, string prefix, string name, string uri) +Create start namespaced attribute - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_attribute_ns) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *prefix, *uri; + int name_len, prefix_len, uri_len, retval; +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss!", + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsss!", &pind, + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Attribute Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterStartAttributeNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ +#endif + +/* {{{ proto bool xmlwriter_write_attribute(resource xmlwriter, string name, string content) +Write full attribute - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_attribute) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *content; + int name_len, content_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &pind, + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Attribute Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterWriteAttribute(ptr, (xmlChar *)name, (xmlChar *)content); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +#if LIBXML_VERSION > 20617 +/* {{{ proto bool xmlwriter_write_attribute_ns(resource xmlwriter, string prefix, string name, string uri, string content) +Write full namespaced attribute - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_attribute_ns) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *prefix, *uri, *content; + int name_len, prefix_len, uri_len, content_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss!s", + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsss!s", &pind, + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Attribute Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterWriteAttributeNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri, (xmlChar *)content); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ +#endif + +/* {{{ proto bool xmlwriter_start_element(resource xmlwriter, string name) +Create start element tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_element) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartElement, "Invalid Element Name"); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_start_element_ns(resource xmlwriter, string prefix, string name, string uri) +Create start namespaced element tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_element_ns) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *prefix, *uri; + int name_len, prefix_len, uri_len, retval; +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!ss!", + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!ss!", &pind, + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Element Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterStartElementNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri); + if (retval != -1) { + RETURN_TRUE; + } + + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_element(resource xmlwriter) +End current element - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_element) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndElement); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_full_end_element(resource xmlwriter) +End current element - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_full_end_element) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterFullEndElement); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_element(resource xmlwriter, string name[, string content]) +Write full element tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_element) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *content = NULL; + int name_len, content_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s!", &pind, + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Element Name"); + + ptr = intern->ptr; + + if (ptr) { + if (!content) { + retval = xmlTextWriterStartElement(ptr, (xmlChar *)name); + if (retval == -1) { + RETURN_FALSE; + } + xmlTextWriterEndElement(ptr); + if (retval == -1) { + RETURN_FALSE; + } + } else { + retval = xmlTextWriterWriteElement(ptr, (xmlChar *)name, (xmlChar *)content); + } + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_element_ns(resource xmlwriter, string prefix, string name, string uri[, string content]) +Write full namesapced element tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_element_ns) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *prefix, *uri, *content = NULL; + int name_len, prefix_len, uri_len, content_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!ss!|s!", + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!ss!|s!", &pind, + &prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Element Name"); + + ptr = intern->ptr; + + if (ptr) { + if (!content) { + retval = xmlTextWriterStartElementNS(ptr,(xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri); + if (retval == -1) { + RETURN_FALSE; + } + retval = xmlTextWriterEndElement(ptr); + if (retval == -1) { + RETURN_FALSE; + } + } else { + retval = xmlTextWriterWriteElementNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri, (xmlChar *)content); + } + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_start_pi(resource xmlwriter, string target) +Create start PI tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_pi) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartPI, "Invalid PI Target"); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_pi(resource xmlwriter) +End current PI - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_pi) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndPI); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_pi(resource xmlwriter, string target, string content) +Write full PI tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_pi) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *content; + int name_len, content_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &pind, + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid PI Target"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterWritePI(ptr, (xmlChar *)name, (xmlChar *)content); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_start_cdata(resource xmlwriter) +Create start CDATA tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_cdata) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + int retval; +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pind) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterStartCDATA(ptr); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_cdata(resource xmlwriter) +End current CDATA - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_cdata) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndCDATA); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_cdata(resource xmlwriter, string content) +Write full CDATA tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_cdata) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteCDATA, NULL); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_raw(resource xmlwriter, string content) +Write text - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_raw) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteRaw, NULL); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_text(resource xmlwriter, string content) +Write text - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_text) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteString, NULL); +} +/* }}} */ + +#if LIBXML_VERSION >= 20607 +/* {{{ proto bool xmlwriter_start_comment(resource xmlwriter) +Create start comment - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_comment) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + int retval; +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pind) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterStartComment(ptr); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_comment(resource xmlwriter) +Create end comment - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_comment) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndComment); +} +/* }}} */ +#endif /* LIBXML_VERSION >= 20607 */ + + +/* {{{ proto bool xmlwriter_write_comment(resource xmlwriter, string content) +Write full comment tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_comment) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteComment, NULL); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_start_document(resource xmlwriter, string version, string encoding, string standalone) +Create document tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_document) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *version = NULL, *enc = NULL, *alone = NULL; + int version_len, enc_len, alone_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!", &version, &version_len, &enc, &enc_len, &alone, &alone_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!s!s!", &pind, &version, &version_len, &enc, &enc_len, &alone, &alone_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterStartDocument(ptr, version, enc, alone); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_document(resource xmlwriter) +End current document - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_document) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDocument); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_start_dtd(resource xmlwriter, string name, string pubid, string sysid) +Create start DTD tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_dtd) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *pubid = NULL, *sysid = NULL; + int name_len, pubid_len, sysid_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!s!", &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len) == FAILURE) { + return; + } + + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s!s!", &pind, &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterStartDTD(ptr, (xmlChar *)name, (xmlChar *)pubid, (xmlChar *)sysid); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_dtd(resource xmlwriter) +End current DTD - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_dtd) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTD); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_dtd(resource xmlwriter, string name, string pubid, string sysid, string subset) +Write full DTD tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_dtd) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *pubid = NULL, *sysid = NULL, *subset = NULL; + int name_len, pubid_len, sysid_len, subset_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!s!s!", &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len, &subset, &subset_len) == FAILURE) { + return; + } + + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s!s!s!", &pind, &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len, &subset, &subset_len) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterWriteDTD(ptr, (xmlChar *)name, (xmlChar *)pubid, (xmlChar *)sysid, (xmlChar *)subset); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_start_dtd_element(resource xmlwriter, string name) +Create start DTD element - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_dtd_element) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartDTDElement, "Invalid Element Name"); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_dtd_element(resource xmlwriter) +End current DTD element - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_dtd_element) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTDElement); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_dtd_element(resource xmlwriter, string name, string content) +Write full DTD element tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_dtd_element) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *content; + int name_len, content_len, retval; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &pind, + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Element Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterWriteDTDElement(ptr, (xmlChar *)name, (xmlChar *)content); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +#if LIBXML_VERSION > 20608 +/* {{{ proto bool xmlwriter_start_dtd_attlist(resource xmlwriter, string name) +Create start DTD AttList - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_dtd_attlist) +{ + php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartDTDAttlist, "Invalid Element Name"); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_dtd_attlist(resource xmlwriter) +End current DTD AttList - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_dtd_attlist) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTDAttlist); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_dtd_attlist(resource xmlwriter, string name, string content) +Write full DTD AttList tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_dtd_attlist) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *content; + int name_len, content_len, retval; + + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &pind, + &name, &name_len, &content, &content_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Element Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterWriteDTDAttlist(ptr, (xmlChar *)name, (xmlChar *)content); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_start_dtd_entity(resource xmlwriter, string name, bool isparam) +Create start DTD Entity - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_start_dtd_entity) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name; + int name_len, retval; + zend_bool isparm; + + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sb", &name, &name_len, &isparm) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsb", &pind, &name, &name_len, &isparm) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Attribute Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterStartDTDEntity(ptr, isparm, (xmlChar *)name); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool xmlwriter_end_dtd_entity(resource xmlwriter) +End current DTD Entity - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_end_dtd_entity) +{ + php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTDEntity); +} +/* }}} */ + +/* {{{ proto bool xmlwriter_write_dtd_entity(resource xmlwriter, string name, string content [, int pe [, string pubid [, string sysid [, string ndataid]]]]) +Write full DTD Entity tag - returns FALSE on error */ +static PHP_FUNCTION(xmlwriter_write_dtd_entity) +{ + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *name, *content; + int name_len, content_len, retval; + /* Optional parameters */ + char *pubid = NULL, *sysid = NULL, *ndataid = NULL; + zend_bool pe = 0; + int pubid_len, sysid_len, ndataid_len; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bsss", + &name, &name_len, &content, &content_len, &pe, &pubid, &pubid_len, + &sysid, &sysid_len, &ndataid, &ndataid_len) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss|bsss", &pind, + &name, &name_len, &content, &content_len, &pe, &pubid, &pubid_len, + &sysid, &sysid_len, &ndataid, &ndataid_len) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + + XMLW_NAME_CHK("Invalid Element Name"); + + ptr = intern->ptr; + + if (ptr) { + retval = xmlTextWriterWriteDTDEntity(ptr, pe, (xmlChar *)name, (xmlChar *)pubid, (xmlChar *)sysid, (xmlChar *)ndataid, (xmlChar *)content); + if (retval != -1) { + RETURN_TRUE; + } + } + + RETURN_FALSE; +} +/* }}} */ +#endif + +/* {{{ proto resource xmlwriter_open_uri(resource xmlwriter, string source) +Create new xmlwriter using source uri for output */ +static PHP_FUNCTION(xmlwriter_open_uri) +{ + char *valid_file = NULL; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + char *source; + char resolved_path[MAXPATHLEN + 1]; + int source_len; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + ze_xmlwriter_object *ze_obj = NULL; +#endif + +#ifndef ZEND_ENGINE_2 + xmlOutputBufferPtr out_buffer; + void *ioctx; +#endif + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &source, &source_len) == FAILURE) { + return; + } + +#ifdef ZEND_ENGINE_2 + if (this) { + /* We do not use XMLWRITER_FROM_OBJECT, xmlwriter init function here */ + ze_obj = (ze_xmlwriter_object*) zend_object_store_get_object(this TSRMLS_CC); + } +#endif + + if (source_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string as source"); + RETURN_FALSE; + } + + valid_file = _xmlwriter_get_valid_file_path(source, resolved_path, MAXPATHLEN TSRMLS_CC); + if (!valid_file) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to resolve file path"); + RETURN_FALSE; + } + + /* TODO: Fix either the PHP stream or libxml APIs: it can then detect when a given + path is valid and not report out of memory error. Once it is done, remove the + directory check in _xmlwriter_get_valid_file_path */ +#ifndef ZEND_ENGINE_2 + ioctx = php_xmlwriter_streams_IO_open_write_wrapper(valid_file TSRMLS_CC); + if (ioctx == NULL) { + RETURN_FALSE; + } + + out_buffer = xmlOutputBufferCreateIO(php_xmlwriter_streams_IO_write, + php_xmlwriter_streams_IO_close, ioctx, NULL); + + if (out_buffer == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create output buffer"); + RETURN_FALSE; + } + ptr = xmlNewTextWriter(out_buffer); +#else + ptr = xmlNewTextWriterFilename(valid_file, 0); +#endif + + if (!ptr) { + RETURN_FALSE; + } + + intern = emalloc(sizeof(xmlwriter_object)); + intern->ptr = ptr; + intern->output = NULL; +#ifndef ZEND_ENGINE_2 + intern->uri_output = out_buffer; +#else + if (this) { + if (ze_obj->xmlwriter_ptr) { + xmlwriter_free_resource_ptr(ze_obj->xmlwriter_ptr TSRMLS_CC); + } + ze_obj->xmlwriter_ptr = intern; + RETURN_TRUE; + } else +#endif + { + ZEND_REGISTER_RESOURCE(return_value,intern,le_xmlwriter); + } +} +/* }}} */ + +/* {{{ proto resource xmlwriter_open_memory() +Create new xmlwriter using memory for string output */ +static PHP_FUNCTION(xmlwriter_open_memory) +{ + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + xmlBufferPtr buffer; + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + ze_xmlwriter_object *ze_obj = NULL; +#endif + +#ifdef ZEND_ENGINE_2 + if (this) { + /* We do not use XMLWRITER_FROM_OBJECT, xmlwriter init function here */ + ze_obj = (ze_xmlwriter_object*) zend_object_store_get_object(this TSRMLS_CC); + } +#endif + + buffer = xmlBufferCreate(); + + if (buffer == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create output buffer"); + RETURN_FALSE; + } + + ptr = xmlNewTextWriterMemory(buffer, 0); + if (! ptr) { + xmlBufferFree(buffer); + RETURN_FALSE; + } + + intern = emalloc(sizeof(xmlwriter_object)); + intern->ptr = ptr; + intern->output = buffer; +#ifndef ZEND_ENGINE_2 + intern->uri_output = NULL; +#else + if (this) { + if (ze_obj->xmlwriter_ptr) { + xmlwriter_free_resource_ptr(ze_obj->xmlwriter_ptr TSRMLS_CC); + } + ze_obj->xmlwriter_ptr = intern; + RETURN_TRUE; + } else +#endif + { + ZEND_REGISTER_RESOURCE(return_value,intern,le_xmlwriter); + } + +} +/* }}} */ + +/* {{{ php_xmlwriter_flush */ +static void php_xmlwriter_flush(INTERNAL_FUNCTION_PARAMETERS, int force_string) { + zval *pind; + xmlwriter_object *intern; + xmlTextWriterPtr ptr; + xmlBufferPtr buffer; + zend_bool empty = 1; + int output_bytes; + + +#ifdef ZEND_ENGINE_2 + zval *this = getThis(); + + if (this) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &empty) == FAILURE) { + return; + } + XMLWRITER_FROM_OBJECT(intern, this); + } else +#endif + { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &pind, &empty) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(intern,xmlwriter_object *, &pind, -1, "XMLWriter", le_xmlwriter); + } + ptr = intern->ptr; + + if (ptr) { + buffer = intern->output; + if (force_string == 1 && buffer == NULL) { + RETURN_EMPTY_STRING(); + } + output_bytes = xmlTextWriterFlush(ptr); + if (buffer) { + RETVAL_STRING((char *) buffer->content, 1); + if (empty) { + xmlBufferEmpty(buffer); + } + } else { + RETVAL_LONG(output_bytes); + } + return; + } + + RETURN_EMPTY_STRING(); +} +/* }}} */ + +/* {{{ proto string xmlwriter_output_memory(resource xmlwriter [,bool flush]) +Output current buffer as string */ +static PHP_FUNCTION(xmlwriter_output_memory) +{ + php_xmlwriter_flush(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); +} +/* }}} */ + +/* {{{ proto mixed xmlwriter_flush(resource xmlwriter [,bool empty]) +Output current buffer */ +static PHP_FUNCTION(xmlwriter_flush) +{ + php_xmlwriter_flush(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} +/* }}} */ + +/* {{{ PHP_MINIT_FUNCTION + */ +static PHP_MINIT_FUNCTION(xmlwriter) +{ +#ifdef ZEND_ENGINE_2 + zend_class_entry ce; +#endif + + le_xmlwriter = zend_register_list_destructors_ex(xmlwriter_dtor, NULL, "xmlwriter", module_number); + +#ifdef ZEND_ENGINE_2 + memcpy(&xmlwriter_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + xmlwriter_object_handlers.clone_obj = NULL; + INIT_CLASS_ENTRY(ce, "XMLWriter", xmlwriter_class_functions); + ce.create_object = xmlwriter_object_new; + xmlwriter_class_entry_ce = zend_register_internal_class(&ce TSRMLS_CC); +#endif + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MSHUTDOWN_FUNCTION + */ +static PHP_MSHUTDOWN_FUNCTION(xmlwriter) +{ + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MINFO_FUNCTION + */ +static PHP_MINFO_FUNCTION(xmlwriter) +{ + php_info_print_table_start(); + { + php_info_print_table_row(2, "XMLWriter", "enabled"); + } + php_info_print_table_end(); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/plugins/php/versions/52/lib/reentrancy.c b/plugins/php/versions/52/lib/reentrancy.c new file mode 100644 index 000000000..ff1a4095b --- /dev/null +++ b/plugins/php/versions/52/lib/reentrancy.c @@ -0,0 +1,450 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Sascha Schumann | + +----------------------------------------------------------------------+ + */ + +/* $Id: reentrancy.c 293036 2010-01-03 09:23:27Z sebastian $ */ + +#include +#include +#include +#ifdef HAVE_DIRENT_H +#include +#endif + +#include "php_reentrancy.h" +#include "ext/standard/php_rand.h" /* for PHP_RAND_MAX */ + +enum { + LOCALTIME_R, + CTIME_R, + ASCTIME_R, + GMTIME_R, + READDIR_R, + NUMBER_OF_LOCKS +}; + +#if defined(PHP_NEED_REENTRANCY) + +#include + +static MUTEX_T reentrant_locks[NUMBER_OF_LOCKS]; + +#define local_lock(x) tsrm_mutex_lock(reentrant_locks[x]) +#define local_unlock(x) tsrm_mutex_unlock(reentrant_locks[x]) + +#else + +#define local_lock(x) +#define local_unlock(x) + +#endif + +#if defined(PHP_IRIX_TIME_R) + +#define HAVE_CTIME_R 1 +#define HAVE_ASCTIME_R 1 + +PHPAPI char *php_ctime_r(const time_t *clock, char *buf) +{ + if (ctime_r(clock, buf, 26) == buf) + return (buf); + return (NULL); +} + +PHPAPI char *php_asctime_r(const struct tm *tm, char *buf) +{ + if (asctime_r(tm, buf, 26) == buf) + return (buf); + return (NULL); +} + +#endif + +#if defined(PHP_HPUX_TIME_R) + +#define HAVE_LOCALTIME_R 1 +#define HAVE_CTIME_R 1 +#define HAVE_ASCTIME_R 1 +#define HAVE_GMTIME_R 1 + +PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm) +{ + if (localtime_r(timep, p_tm) == 0) + return (p_tm); + return (NULL); +} + +PHPAPI char *php_ctime_r(const time_t *clock, char *buf) +{ + if (ctime_r(clock, buf, 26) != -1) + return (buf); + return (NULL); +} + +PHPAPI char *php_asctime_r(const struct tm *tm, char *buf) +{ + if (asctime_r(tm, buf, 26) != -1) + return (buf); + return (NULL); +} + +PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm) +{ + if (gmtime_r(timep, p_tm) == 0) + return (p_tm); + return (NULL); +} + +#endif + +#if defined(__BEOS__) + +PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm) +{ + /* Modified according to LibC definition */ + if (((struct tm*)gmtime_r(timep, p_tm)) == p_tm) + return (p_tm); + return (NULL); +} + +#endif /* BEOS */ + +#if !defined(HAVE_POSIX_READDIR_R) + +PHPAPI int php_readdir_r(DIR *dirp, struct dirent *entry, + struct dirent **result) +{ +#if defined(HAVE_OLD_READDIR_R) + int ret = 0; + + /* We cannot rely on the return value of readdir_r + as it differs between various platforms + (HPUX returns 0 on success whereas Solaris returns non-zero) + */ + entry->d_name[0] = '\0'; + readdir_r(dirp, entry,result); + + if (entry->d_name[0] == '\0') { + *result = NULL; + ret = errno; + } else { + *result = entry; + } + return ret; +#else + struct dirent *ptr; + int ret = 0; + + local_lock(READDIR_R); + + errno = 0; + + ptr = readdir(dirp); + + if (!ptr && errno != 0) + ret = errno; + + if (ptr) + memcpy(entry, ptr, sizeof(*ptr)); + + *result = ptr; + + local_unlock(READDIR_R); + + return ret; +#endif +} + +#endif + +#if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME) + +PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm) +{ + struct tm *tmp; + + local_lock(LOCALTIME_R); + + tmp = localtime(timep); + if (tmp) { + memcpy(p_tm, tmp, sizeof(struct tm)); + tmp = p_tm; + } + + local_unlock(LOCALTIME_R); + + return tmp; +} + +#endif + +#if !defined(HAVE_CTIME_R) && defined(HAVE_CTIME) + +PHPAPI char *php_ctime_r(const time_t *clock, char *buf) +{ + char *tmp; + + local_lock(CTIME_R); + + tmp = ctime(clock); + strcpy(buf, tmp); + + local_unlock(CTIME_R); + + return buf; +} + +#endif + +#if !defined(HAVE_ASCTIME_R) && defined(HAVE_ASCTIME) + +PHPAPI char *php_asctime_r(const struct tm *tm, char *buf) +{ + char *tmp; + + local_lock(ASCTIME_R); + + tmp = asctime(tm); + strcpy(buf, tmp); + + local_unlock(ASCTIME_R); + + return buf; +} + +#endif + +#if !defined(HAVE_GMTIME_R) && defined(HAVE_GMTIME) + +PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm) +{ + struct tm *tmp; + + local_lock(GMTIME_R); + + tmp = gmtime(timep); + if (tmp) { + memcpy(p_tm, tmp, sizeof(struct tm)); + tmp = p_tm; + } + + local_unlock(GMTIME_R); + + return tmp; +} + +#endif + +#if defined(PHP_NEED_REENTRANCY) + +void reentrancy_startup(void) +{ + int i; + + for (i = 0; i < NUMBER_OF_LOCKS; i++) { + reentrant_locks[i] = tsrm_mutex_alloc(); + } +} + +void reentrancy_shutdown(void) +{ + int i; + + for (i = 0; i < NUMBER_OF_LOCKS; i++) { + tsrm_mutex_free(reentrant_locks[i]); + } +} + +#endif + +#ifndef HAVE_RAND_R + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Posix rand_r function added May 1999 by Wes Peters . + */ + +#include +#include + +static int +do_rand(unsigned long *ctx) +{ + return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)PHP_RAND_MAX + 1)); +} + + +PHPAPI int +php_rand_r(unsigned int *ctx) +{ + u_long val = (u_long) *ctx; + *ctx = do_rand(&val); + return (int) *ctx; +} + +#endif + + +#ifndef HAVE_STRTOK_R + +/* + * Copyright (c) 1998 Softweyr LLC. All rights reserved. + * + * strtok_r, from Berkeley strtok + * Oct 13, 1998 by Wes Peters + * + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notices, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notices, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * + * This product includes software developed by Softweyr LLC, the + * University of California, Berkeley, and its contributors. + * + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE + * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +PHPAPI char * +php_strtok_r(char *s, const char *delim, char **last) +{ + char *spanp; + int c, sc; + char *tok; + + if (s == NULL && (s = *last) == NULL) + { + return NULL; + } + + /* + * Skip (span) leading delimiters (s += strspn(s, delim), sort of). + */ +cont: + c = *s++; + for (spanp = (char *)delim; (sc = *spanp++) != 0; ) + { + if (c == sc) + { + goto cont; + } + } + + if (c == 0) /* no non-delimiter characters */ + { + *last = NULL; + return NULL; + } + tok = s - 1; + + /* + * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). + * Note that delim must have one NUL; we stop if we see that, too. + */ + for (;;) + { + c = *s++; + spanp = (char *)delim; + do + { + if ((sc = *spanp++) == c) + { + if (c == 0) + { + s = NULL; + } + else + { + char *w = s - 1; + *w = '\0'; + } + *last = s; + return tok; + } + } + while (sc != 0); + } + /* NOTREACHED */ +} + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/plugins/php/versions/52/lib/simplexml.c b/plugins/php/versions/52/lib/simplexml.c new file mode 100644 index 000000000..a9473d93f --- /dev/null +++ b/plugins/php/versions/52/lib/simplexml.c @@ -0,0 +1,2482 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sterling Hughes | + | Marcus Boerger | + | Rob Richards | + +----------------------------------------------------------------------+ +*/ + +/* $Id: simplexml.c 299016 2010-05-05 11:40:11Z rrichards $ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#if HAVE_LIBXML && HAVE_SIMPLEXML + +#include "php_ini.h" +#include "ext/standard/info.h" +#include "ext/standard/php_string.h" +#include "php_simplexml.h" +#include "php_simplexml_exports.h" +#include "zend_exceptions.h" +#include "zend_interfaces.h" +#ifdef HAVE_SPL +#include "ext/spl/spl_sxe.h" +#endif + +#define SXE_ELEMENT_BY_NAME 0 + +zend_class_entry *sxe_class_entry = NULL; + +ZEND_API zend_class_entry *sxe_get_element_class_entry() +{ + return sxe_class_entry; +} + +#define SXE_ME(func, arg_info, flags) PHP_ME(simplexml_element, func, arg_info, flags) +#define SXE_MALIAS(func, alias, arg_info, flags) PHP_MALIAS(simplexml_element, func, alias, arg_info, flags) + +#define SXE_METHOD(func) PHP_METHOD(simplexml_element, func) + +static php_sxe_object* php_sxe_object_new(zend_class_entry *ce TSRMLS_DC); +static zend_object_value php_sxe_register_object(php_sxe_object * TSRMLS_DC); +static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data TSRMLS_DC); +static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data TSRMLS_DC); +static zval *sxe_get_value(zval *z TSRMLS_DC); + +/* {{{ _node_as_zval() + */ +static void _node_as_zval(php_sxe_object *sxe, xmlNodePtr node, zval *value, SXE_ITER itertype, char *name, xmlChar *nsprefix, int isprefix TSRMLS_DC) +{ + php_sxe_object *subnode; + + subnode = php_sxe_object_new(sxe->zo.ce TSRMLS_CC); + subnode->document = sxe->document; + subnode->document->refcount++; + subnode->iter.type = itertype; + if (name) { + subnode->iter.name = xmlStrdup((xmlChar *)name); + } + if (nsprefix && *nsprefix) { + subnode->iter.nsprefix = xmlStrdup((xmlChar *)nsprefix); + subnode->iter.isprefix = isprefix; + } + + php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL TSRMLS_CC); + + value->type = IS_OBJECT; + value->value.obj = php_sxe_register_object(subnode TSRMLS_CC); +} +/* }}} */ + +#define APPEND_PREV_ELEMENT(__c, __v) \ + if ((__c) == 1) { \ + array_init(return_value); \ + add_next_index_zval(return_value, __v); \ + } + +#define APPEND_CUR_ELEMENT(__c, __v) \ + if (++(__c) > 1) { \ + add_next_index_zval(return_value, __v); \ + } + +#define GET_NODE(__s, __n) { \ + if ((__s)->node && (__s)->node->node) { \ + __n = (__s)->node->node; \ + } else { \ + __n = NULL; \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Node no longer exists"); \ + } \ +} + +static xmlNodePtr php_sxe_get_first_node(php_sxe_object *sxe, xmlNodePtr node TSRMLS_DC) { + php_sxe_object *intern; + xmlNodePtr retnode = NULL; + + if (sxe && sxe->iter.type != SXE_ITER_NONE) { + php_sxe_reset_iterator(sxe, 1 TSRMLS_CC); + if (sxe->iter.data) { + intern = (php_sxe_object *)zend_object_store_get_object(sxe->iter.data TSRMLS_CC); + GET_NODE(intern, retnode) + } + return retnode; + } else { + return node; + } +} + +static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name, int prefix) /* {{{ */ +{ + if (name == NULL && (node->ns == NULL || node->ns->prefix == NULL)) { + return 1; + } + + if (node->ns && !xmlStrcmp(prefix ? node->ns->prefix : node->ns->href, name)) { + return 1; + } + + return 0; +} +/* }}} */ + +static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, long offset, xmlNodePtr node, long *cnt) /* {{{ */ +{ + long nodendx = 0; + + if (sxe->iter.type == SXE_ITER_NONE) { + if (offset == 0) { + if (cnt) { + *cnt = 0; + } + return node; + } else { + return NULL; + } + } + while (node && nodendx <= offset) { + SKIP_TEXT(node) + if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) { + if (sxe->iter.type == SXE_ITER_CHILD || ( + sxe->iter.type == SXE_ITER_ELEMENT && !xmlStrcmp(node->name, sxe->iter.name))) { + if (nodendx == offset) { + break; + } + nodendx++; + } + } +next_iter: + node = node->next; + } + + if (cnt) { + *cnt = nodendx; + } + + return node; +} +/* }}} */ + +static xmlNodePtr sxe_find_element_by_name(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name TSRMLS_DC) /* {{{ */ +{ + while (node) { + SKIP_TEXT(node) + if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) { + if (!xmlStrcmp(node->name, name)) { + return node; + } + } +next_iter: + node = node->next; + } + return NULL; +} /* }}} */ + +static xmlNodePtr sxe_get_element_by_name(php_sxe_object *sxe, xmlNodePtr node, char **name, SXE_ITER *type TSRMLS_DC) /* {{{ */ +{ + int orgtype; + xmlNodePtr orgnode = node; + xmlNodePtr retnode = NULL; + + if (sxe->iter.type != SXE_ITER_ATTRLIST) + { + orgtype = sxe->iter.type; + if (sxe->iter.type == SXE_ITER_NONE) { + sxe->iter.type = SXE_ITER_CHILD; + } + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + sxe->iter.type = orgtype; + } + + if (sxe->iter.type == SXE_ITER_ELEMENT) { + orgnode = sxe_find_element_by_name(sxe, node, sxe->iter.name TSRMLS_CC); + if (!orgnode) { + return NULL; + } + node = orgnode->children; + } + + while (node) { + SKIP_TEXT(node) + if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) { + if (!xmlStrcmp(node->name, (xmlChar *)*name)) { + if (1||retnode) + { + *type = SXE_ITER_ELEMENT; + return orgnode; + } + retnode = node; + } + } +next_iter: + node = node->next; + } + + if (retnode) + { + *type = SXE_ITER_NONE; + *name = NULL; + return retnode; + } + + return NULL; +} +/* }}} */ + +/* {{{ sxe_prop_dim_read() + */ +static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, zend_bool attribs, int type TSRMLS_DC) +{ + zval *return_value; + php_sxe_object *sxe; + char *name; + xmlNodePtr node; + xmlAttrPtr attr = NULL; + zval tmp_zv; + int nodendx = 0; + int test = 0; + + sxe = php_sxe_fetch_object(object TSRMLS_CC); + + if (!member || Z_TYPE_P(member) == IS_LONG) { + if (sxe->iter.type != SXE_ITER_ATTRLIST) { + attribs = 0; + elements = 1; + } else if (!member) { + /* This happens when the user did: $sxe[]->foo = $value */ + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot create unnamed attribute"); + return NULL; + } + name = NULL; + } else { + if (Z_TYPE_P(member) != IS_STRING) { + tmp_zv = *member; + zval_copy_ctor(&tmp_zv); + member = &tmp_zv; + convert_to_string(member); + } + name = Z_STRVAL_P(member); + } + + GET_NODE(sxe, node); + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + attribs = 1; + elements = 0; + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = (xmlAttrPtr)node; + test = sxe->iter.name != NULL; + } else if (sxe->iter.type != SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = node ? node->properties : NULL; + test = 0; + if (!member && node && node->parent && + node->parent->type == XML_DOCUMENT_NODE) { + /* This happens when the user did: $sxe[]->foo = $value */ + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot create unnamed attribute"); + return NULL; + } + } + + MAKE_STD_ZVAL(return_value); + ZVAL_NULL(return_value); + + if (node) { + if (attribs) { + if (Z_TYPE_P(member) != IS_LONG || sxe->iter.type == SXE_ITER_ATTRLIST) { + if (Z_TYPE_P(member) == IS_LONG) { + while (attr && nodendx <= Z_LVAL_P(member)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + if (nodendx == Z_LVAL_P(member)) { + _node_as_zval(sxe, (xmlNodePtr) attr, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); + break; + } + nodendx++; + } + attr = attr->next; + } + } else { + while (attr) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + _node_as_zval(sxe, (xmlNodePtr) attr, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); + break; + } + attr = attr->next; + } + } + } + } + + if (elements) { + if (!sxe->node) { + php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, node, NULL TSRMLS_CC); + } + if (!member || Z_TYPE_P(member) == IS_LONG) { + long cnt = 0; + xmlNodePtr mynode = node; + + if (sxe->iter.type == SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + } + if (sxe->iter.type == SXE_ITER_NONE) { + if (member && Z_LVAL_P(member) > 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add element %s number %ld when only 0 such elements exist", mynode->name, Z_LVAL_P(member)); + } + } else if (member) { + node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt); + } else { + node = NULL; + } + if (node) { + _node_as_zval(sxe, node, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); + } else if (type == BP_VAR_W || type == BP_VAR_RW) { + if (member && cnt < Z_LVAL_P(member)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add element %s number %ld when only %ld such elements exist", mynode->name, Z_LVAL_P(member), cnt); + } + node = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, NULL); + _node_as_zval(sxe, node, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); + } + } else { +#if SXE_ELEMENT_BY_NAME + int newtype; + + GET_NODE(sxe, node); + node = sxe_get_element_by_name(sxe, node, &name, &newtype TSRMLS_CC); + if (node) { + _node_as_zval(sxe, node, return_value, newtype, name, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); + } +#else + _node_as_zval(sxe, node, return_value, SXE_ITER_ELEMENT, name, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); +#endif + } + } + } + + return_value->refcount = 0; + return_value->is_ref = 0; + + if (member == &tmp_zv) { + zval_dtor(&tmp_zv); + } + if (Z_TYPE_P(return_value) == IS_NULL) { + FREE_ZVAL(return_value); + return_value = &EG(uninitialized_zval); + } + + return return_value; +} +/* }}} */ + +/* {{{ sxe_property_read() + */ +static zval * sxe_property_read(zval *object, zval *member, int type TSRMLS_DC) +{ + return sxe_prop_dim_read(object, member, 1, 0, type TSRMLS_CC); +} +/* }}} */ + +/* {{{ sxe_dimension_read() + */ +static zval * sxe_dimension_read(zval *object, zval *offset, int type TSRMLS_DC) +{ + return sxe_prop_dim_read(object, offset, 0, 1, type TSRMLS_CC); +} +/* }}} */ + +/* {{{ change_node_zval() + */ +static void change_node_zval(xmlNodePtr node, zval *value TSRMLS_DC) +{ + zval value_copy; + xmlChar *buffer; + int buffer_len; + + if (!value) + { + xmlNodeSetContentLen(node, (xmlChar *)"", 0); + return; + } + switch (Z_TYPE_P(value)) { + case IS_LONG: + case IS_BOOL: + case IS_DOUBLE: + case IS_NULL: + if (value->refcount > 1) { + value_copy = *value; + zval_copy_ctor(&value_copy); + value = &value_copy; + } + convert_to_string(value); + /* break missing intentionally */ + case IS_STRING: + buffer = xmlEncodeEntitiesReentrant(node->doc, (xmlChar *)Z_STRVAL_P(value)); + buffer_len = xmlStrlen(buffer); + /* check for NULL buffer in case of memory error in xmlEncodeEntitiesReentrant */ + if (buffer) { + xmlNodeSetContentLen(node, buffer, buffer_len); + xmlFree(buffer); + } + if (value == &value_copy) { + zval_dtor(value); + } + break; + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "It is not possible to assign complex types to nodes"); + break; + } +} +/* }}} */ + +/* {{{ sxe_property_write() + */ +static void sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_bool elements, zend_bool attribs, xmlNodePtr *pnewnode TSRMLS_DC) +{ + php_sxe_object *sxe; + xmlNodePtr node; + xmlNodePtr newnode = NULL; + xmlNodePtr mynode; + xmlNodePtr tempnode; + xmlAttrPtr attr = NULL; + int counter = 0; + int is_attr = 0; + int nodendx = 0; + int test = 0; + int new_value = 0; + long cnt = 0; + zval tmp_zv, trim_zv, value_copy; + + sxe = php_sxe_fetch_object(object TSRMLS_CC); + + if (!member || Z_TYPE_P(member) == IS_LONG) { + if (sxe->iter.type != SXE_ITER_ATTRLIST) { + attribs = 0; + elements = 1; + } else if (!member) { + /* This happens when the user did: $sxe[] = $value + * and could also be E_PARSE, but we use this only during parsing + * and this is during runtime. + */ + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot create unnamed attribute"); + return; + } + } else { + if (Z_TYPE_P(member) != IS_STRING) { + trim_zv = *member; + zval_copy_ctor(&trim_zv); + convert_to_string(&trim_zv); + php_trim(Z_STRVAL(trim_zv), Z_STRLEN(trim_zv), NULL, 0, &tmp_zv, 3 TSRMLS_CC); + zval_dtor(&trim_zv); + member = &tmp_zv; + } + + if (!Z_STRLEN_P(member)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write or create unnamed %s", attribs ? "attribute" : "element"); + if (member == &tmp_zv) { + zval_dtor(&tmp_zv); + } + return; + } + } + + GET_NODE(sxe, node); + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + attribs = 1; + elements = 0; + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = (xmlAttrPtr)node; + test = sxe->iter.name != NULL; + } else if (sxe->iter.type != SXE_ITER_CHILD) { + mynode = node; + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = node ? node->properties : NULL; + test = 0; + if (!member && node && node->parent && + node->parent->type == XML_DOCUMENT_NODE) { + /* This happens when the user did: $sxe[] = $value + * and could also be E_PARSE, but we use this only during parsing + * and this is during runtime. + */ + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot create unnamed attribute"); + return; + } + if (attribs && !node && sxe->iter.type == SXE_ITER_ELEMENT) { + node = xmlNewChild(mynode, mynode->ns, sxe->iter.name, NULL); + attr = node->properties; + } + } + + mynode = node; + + if (value) { + switch (Z_TYPE_P(value)) { + case IS_LONG: + case IS_BOOL: + case IS_DOUBLE: + case IS_NULL: + if (value->refcount > 1) { + value_copy = *value; + zval_copy_ctor(&value_copy); + value = &value_copy; + } + convert_to_string(value); + break; + case IS_STRING: + break; + case IS_OBJECT: + if (Z_OBJCE_P(value) == sxe_class_entry) { + value = sxe_get_value(value TSRMLS_CC); + INIT_PZVAL(value); + new_value = 1; + break; + } + /* break is missing intentionally */ + default: + if (member == &tmp_zv) { + zval_dtor(&tmp_zv); + } + zend_error(E_WARNING, "It is not yet possible to assign complex types to %s", attribs ? "attributes" : "properties"); + return; + } + } + + if (node) { + if (attribs) { + if (Z_TYPE_P(member) == IS_LONG) { + while (attr && nodendx <= Z_LVAL_P(member)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + if (nodendx == Z_LVAL_P(member)) { + is_attr = 1; + ++counter; + break; + } + nodendx++; + } + attr = attr->next; + } + } else { + while (attr) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + is_attr = 1; + ++counter; + break; + } + attr = attr->next; + } + } + + } + + if (elements) { + if (!member || Z_TYPE_P(member) == IS_LONG) { + if (node->type == XML_ATTRIBUTE_NODE) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot create duplicate attribute"); + return; + } + + if (sxe->iter.type == SXE_ITER_NONE) { + newnode = node; + ++counter; + if (member && Z_LVAL_P(member) > 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add element %s number %ld when only 0 such elements exist", mynode->name, Z_LVAL_P(member)); + } + } else if (member) { + newnode = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt); + if (newnode) { + ++counter; + } + } + } else { + node = node->children; + while (node) { + SKIP_TEXT(node); + + if (!xmlStrcmp(node->name, (xmlChar *)Z_STRVAL_P(member))) { + newnode = node; + ++counter; + } + +next_iter: + node = node->next; + } + } + } + + if (counter == 1) { + if (is_attr) { + newnode = (xmlNodePtr) attr; + } + if (value) { + while ((tempnode = (xmlNodePtr) newnode->children)) { + xmlUnlinkNode(tempnode); + php_libxml_node_free_resource((xmlNodePtr) tempnode TSRMLS_CC); + } + change_node_zval(newnode, value TSRMLS_CC); + } + } else if (counter > 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot assign to an array of nodes (duplicate subnodes or attr detected)"); + } else if (elements) { + if (!node) { + if (!member || Z_TYPE_P(member) == IS_LONG) { + newnode = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, value ? (xmlChar *)Z_STRVAL_P(value) : NULL); + } else { + newnode = xmlNewTextChild(mynode, mynode->ns, (xmlChar *)Z_STRVAL_P(member), value ? (xmlChar *)Z_STRVAL_P(value) : NULL); + } + } else if (!member || Z_TYPE_P(member) == IS_LONG) { + if (member && cnt < Z_LVAL_P(member)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add element %s number %ld when only %ld such elements exist", mynode->name, Z_LVAL_P(member), cnt); + } + newnode = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, value ? (xmlChar *)Z_STRVAL_P(value) : NULL); + } + } else if (attribs) { + if (Z_TYPE_P(member) == IS_LONG) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot change attribute number %ld when only %d attributes exist", Z_LVAL_P(member), nodendx); + } else { + newnode = (xmlNodePtr)xmlNewProp(node, (xmlChar *)Z_STRVAL_P(member), value ? (xmlChar *)Z_STRVAL_P(value) : NULL); + } + } + } + + if (member == &tmp_zv) { + zval_dtor(&tmp_zv); + } + if (pnewnode) { + *pnewnode = newnode; + } + if (value && value == &value_copy) { + zval_dtor(value); + } + if (new_value) { + zval_ptr_dtor(&value); + } +} +/* }}} */ + +/* {{{ sxe_property_write() + */ +static void sxe_property_write(zval *object, zval *member, zval *value TSRMLS_DC) +{ + sxe_prop_dim_write(object, member, value, 1, 0, NULL TSRMLS_CC); +} +/* }}} */ + +/* {{{ sxe_dimension_write() + */ +static void sxe_dimension_write(zval *object, zval *offset, zval *value TSRMLS_DC) +{ + sxe_prop_dim_write(object, offset, value, 0, 1, NULL TSRMLS_CC); +} +/* }}} */ + +static zval** sxe_property_get_adr(zval *object, zval *member TSRMLS_DC) /* {{{ */ +{ + php_sxe_object *sxe; + xmlNodePtr node; + zval *return_value; + char *name; + SXE_ITER type; + + sxe = php_sxe_fetch_object(object TSRMLS_CC); + + GET_NODE(sxe, node); + convert_to_string(member); + name = Z_STRVAL_P(member); + node = sxe_get_element_by_name(sxe, node, &name, &type TSRMLS_CC); + if (node) { + return NULL; + } + sxe_prop_dim_write(object, member, NULL, 1, 0, &node TSRMLS_CC); + type = SXE_ITER_NONE; + name = NULL; + MAKE_STD_ZVAL(return_value); + _node_as_zval(sxe, node, return_value, type, name, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); + + sxe = php_sxe_fetch_object(return_value TSRMLS_CC); + sxe->tmp = return_value; + return_value->is_ref = 1; + + return &sxe->tmp; +} +/* }}} */ + +/* {{{ sxe_prop_dim_exists() + */ +static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend_bool elements, zend_bool attribs TSRMLS_DC) +{ + php_sxe_object *sxe; + xmlNodePtr node; + xmlAttrPtr attr = NULL; + int exists = 0; + int test = 0; + zval tmp_zv; + + if (Z_TYPE_P(member) != IS_STRING && Z_TYPE_P(member) != IS_LONG) { + tmp_zv = *member; + zval_copy_ctor(&tmp_zv); + member = &tmp_zv; + convert_to_string(member); + } + + sxe = php_sxe_fetch_object(object TSRMLS_CC); + + GET_NODE(sxe, node); + + if (Z_TYPE_P(member) == IS_LONG) { + if (sxe->iter.type != SXE_ITER_ATTRLIST) { + attribs = 0; + elements = 1; + if (sxe->iter.type == SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + } + } + } + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + attribs = 1; + elements = 0; + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = (xmlAttrPtr)node; + test = sxe->iter.name != NULL; + } else if (sxe->iter.type != SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = node ? node->properties : NULL; + test = 0; + } + + if (node) { + if (attribs) { + if (Z_TYPE_P(member) == IS_LONG) { + int nodendx = 0; + + while (attr && nodendx <= Z_LVAL_P(member)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + if (nodendx == Z_LVAL_P(member)) { + exists = 1; + break; + } + nodendx++; + } + attr = attr->next; + } + } else { + while (attr) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + exists = 1; + break; + } + + attr = attr->next; + } + } + if (exists && check_empty == 1 && + (!attr->children || !attr->children->content || !attr->children->content[0] || !xmlStrcmp(attr->children->content, "0")) ) { + /* Attribute with no content in it's text node */ + exists = 0; + } + } + + if (elements) { + if (Z_TYPE_P(member) == IS_LONG) { + if (sxe->iter.type == SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + } + node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL); + } + else { + node = node->children; + while (node) { + xmlNodePtr nnext; + nnext = node->next; + if ((node->type == XML_ELEMENT_NODE) && !xmlStrcmp(node->name, (xmlChar *)Z_STRVAL_P(member))) { + break; + } + node = nnext; + } + } + if (node) { + exists = 1; + if (check_empty == 1 && + (!node->children || (node->children->type == XML_TEXT_NODE && !node->children->next && + (!node->children->content || !node->children->content[0] || !xmlStrcmp(node->children->content, "0")))) ) { + exists = 0; + } + } + } + } + + if (member == &tmp_zv) { + zval_dtor(&tmp_zv); + } + + return exists; +} +/* }}} */ + +/* {{{ sxe_property_exists() + */ +static int sxe_property_exists(zval *object, zval *member, int check_empty TSRMLS_DC) +{ + return sxe_prop_dim_exists(object, member, check_empty, 1, 0 TSRMLS_CC); +} +/* }}} */ + +/* {{{ sxe_property_exists() + */ +static int sxe_dimension_exists(zval *object, zval *member, int check_empty TSRMLS_DC) +{ + return sxe_prop_dim_exists(object, member, check_empty, 0, 1 TSRMLS_CC); +} +/* }}} */ + +/* {{{ sxe_prop_dim_delete() + */ +static void sxe_prop_dim_delete(zval *object, zval *member, zend_bool elements, zend_bool attribs TSRMLS_DC) +{ + php_sxe_object *sxe; + xmlNodePtr node; + xmlNodePtr nnext; + xmlAttrPtr attr = NULL; + xmlAttrPtr anext; + zval tmp_zv; + int test = 0; + + if (Z_TYPE_P(member) != IS_STRING && Z_TYPE_P(member) != IS_LONG) { + tmp_zv = *member; + zval_copy_ctor(&tmp_zv); + member = &tmp_zv; + convert_to_string(member); + } + + sxe = php_sxe_fetch_object(object TSRMLS_CC); + + GET_NODE(sxe, node); + + if (Z_TYPE_P(member) == IS_LONG) { + if (sxe->iter.type != SXE_ITER_ATTRLIST) { + attribs = 0; + elements = 1; + if (sxe->iter.type == SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + } + } + } + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + attribs = 1; + elements = 0; + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = (xmlAttrPtr)node; + test = sxe->iter.name != NULL; + } else if (sxe->iter.type != SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + attr = node ? node->properties : NULL; + test = 0; + } + + if (node) { + if (attribs) { + if (Z_TYPE_P(member) == IS_LONG) { + int nodendx = 0; + + while (attr && nodendx <= Z_LVAL_P(member)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + if (nodendx == Z_LVAL_P(member)) { + xmlUnlinkNode((xmlNodePtr) attr); + php_libxml_node_free_resource((xmlNodePtr) attr TSRMLS_CC); + break; + } + nodendx++; + } + attr = attr->next; + } + } else { + while (attr) { + anext = attr->next; + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + xmlUnlinkNode((xmlNodePtr) attr); + php_libxml_node_free_resource((xmlNodePtr) attr TSRMLS_CC); + break; + } + attr = anext; + } + } + } + + if (elements) { + if (Z_TYPE_P(member) == IS_LONG) { + if (sxe->iter.type == SXE_ITER_CHILD) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + } + node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL); + if (node) { + xmlUnlinkNode(node); + php_libxml_node_free_resource(node TSRMLS_CC); + } + } else { + node = node->children; + while (node) { + nnext = node->next; + + SKIP_TEXT(node); + + if (!xmlStrcmp(node->name, (xmlChar *)Z_STRVAL_P(member))) { + xmlUnlinkNode(node); + php_libxml_node_free_resource(node TSRMLS_CC); + } + +next_iter: + node = nnext; + } + } + } + } + + if (member == &tmp_zv) { + zval_dtor(&tmp_zv); + } +} +/* }}} */ + +/* {{{ sxe_property_delete() + */ +static void sxe_property_delete(zval *object, zval *member TSRMLS_DC) +{ + sxe_prop_dim_delete(object, member, 1, 0 TSRMLS_CC); +} +/* }}} */ + +/* {{{ sxe_dimension_unset() + */ +static void sxe_dimension_delete(zval *object, zval *offset TSRMLS_DC) +{ + sxe_prop_dim_delete(object, offset, 0, 1 TSRMLS_CC); +} +/* }}} */ + +static inline char * sxe_xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) +{ + xmlChar *tmp = xmlNodeListGetString(doc, list, inLine); + char *res; + + if (tmp) { + res = estrdup((char*)tmp); + xmlFree(tmp); + } else { + res = STR_EMPTY_ALLOC(); + } + + return res; +} + +/* {{{ _get_base_node_value() + */ +static void _get_base_node_value(php_sxe_object *sxe_ref, xmlNodePtr node, zval **value, xmlChar *nsprefix, int isprefix TSRMLS_DC) +{ + php_sxe_object *subnode; + xmlChar *contents; + + MAKE_STD_ZVAL(*value); + + if (node->children && node->children->type == XML_TEXT_NODE && !xmlIsBlankNode(node->children)) { + contents = xmlNodeListGetString(node->doc, node->children, 1); + if (contents) { + ZVAL_STRING(*value, (char *)contents, 1); + xmlFree(contents); + } + } else { + subnode = php_sxe_object_new(sxe_ref->zo.ce TSRMLS_CC); + subnode->document = sxe_ref->document; + subnode->document->refcount++; + if (nsprefix && *nsprefix) { + subnode->iter.nsprefix = xmlStrdup((xmlChar *)nsprefix); + subnode->iter.isprefix = isprefix; + } + php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL TSRMLS_CC); + + (*value)->type = IS_OBJECT; + (*value)->value.obj = php_sxe_register_object(subnode TSRMLS_CC); + /*zval_add_ref(value);*/ + } +} +/* }}} */ + +static void sxe_properties_add(HashTable *rv, char *name, int namelen, zval *value TSRMLS_DC) +{ + zval **data_ptr; + zval *newptr; + ulong h = zend_hash_func(name, namelen); + + if (zend_hash_quick_find(rv, name, namelen, h, (void **) &data_ptr) == SUCCESS) { + if (Z_TYPE_PP(data_ptr) == IS_ARRAY) { + zend_hash_next_index_insert(Z_ARRVAL_PP(data_ptr), &value, sizeof(zval *), NULL); + } else { + MAKE_STD_ZVAL(newptr); + array_init(newptr); + + zval_add_ref(data_ptr); + zend_hash_next_index_insert(Z_ARRVAL_P(newptr), data_ptr, sizeof(zval *), NULL); + zend_hash_next_index_insert(Z_ARRVAL_P(newptr), &value, sizeof(zval *), NULL); + + zend_hash_quick_update(rv, name, namelen, h, &newptr, sizeof(zval *), NULL); + } + } else { + zend_hash_quick_update(rv, name, namelen, h, &value, sizeof(zval *), NULL); + } +} + +/* {{{ sxe_properties_get() + */ +static HashTable * sxe_properties_get(zval *object TSRMLS_DC) +{ + zval *value; + zval *zattr; + HashTable *rv; + php_sxe_object *sxe; + char *name; + xmlNodePtr node; + xmlAttrPtr attr; + int namelen; + int test; + + sxe = php_sxe_fetch_object(object TSRMLS_CC); + + if (sxe->properties) { + zend_hash_clean(sxe->properties); + rv = sxe->properties; + } else { + ALLOC_HASHTABLE(rv); + zend_hash_init(rv, 0, NULL, ZVAL_PTR_DTOR, 0); + sxe->properties = rv; + } + + GET_NODE(sxe, node); + if (!node) { + return rv; + } + if (1||sxe->iter.type != SXE_ITER_CHILD) { + if (sxe->iter.type == SXE_ITER_ELEMENT) { + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + } + attr = node ? (xmlAttrPtr)node->properties : NULL; + zattr = NULL; + test = sxe->iter.name && sxe->iter.type == SXE_ITER_ATTRLIST; + while (attr) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr)attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + MAKE_STD_ZVAL(value); + ZVAL_STRING(value, sxe_xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, attr->children, 1), 0); + namelen = xmlStrlen(attr->name) + 1; + if (!zattr) { + MAKE_STD_ZVAL(zattr); + array_init(zattr); + sxe_properties_add(rv, "@attributes", sizeof("@attributes"), zattr TSRMLS_CC); + } + add_assoc_zval_ex(zattr, (char*)attr->name, namelen, value); + } + attr = attr->next; + } + } + + GET_NODE(sxe, node); + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + if (node && sxe->iter.type != SXE_ITER_ATTRLIST) { + if (node->type == XML_ATTRIBUTE_NODE) { + MAKE_STD_ZVAL(value); + ZVAL_STRING(value, sxe_xmlNodeListGetString(node->doc, node->children, 1), 0); + zend_hash_next_index_insert(rv, &value, sizeof(zval *), NULL); + node = NULL; + } else if (sxe->iter.type != SXE_ITER_CHILD) { + node = node->children; + } + + while (node) { + if (node->children != NULL || node->prev != NULL || node->next != NULL) { + SKIP_TEXT(node); + } else { + if (node->type == XML_TEXT_NODE) { + const xmlChar *cur = node->content; + + if (*cur != 0) { + MAKE_STD_ZVAL(value); + ZVAL_STRING(value, sxe_xmlNodeListGetString(node->doc, node, 1), 0); + zend_hash_next_index_insert(rv, &value, sizeof(zval *), NULL); + } + goto next_iter; + } + } + + if (node->type == XML_ELEMENT_NODE && (! match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix))) { + goto next_iter; + } + + name = (char *) node->name; + if (!name) { + goto next_iter; + } else { + namelen = xmlStrlen(node->name) + 1; + } + + _get_base_node_value(sxe, node, &value, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); + + sxe_properties_add(rv, name, namelen, value TSRMLS_CC); +next_iter: + node = node->next; + } + } + + return rv; +} +/* }}} */ + +static int sxe_objects_compare(zval *object1, zval *object2 TSRMLS_DC) /* {{{ */ +{ + php_sxe_object *sxe1; + php_sxe_object *sxe2; + + sxe1 = php_sxe_fetch_object(object1 TSRMLS_CC); + sxe2 = php_sxe_fetch_object(object2 TSRMLS_CC); + + if (sxe1->node == NULL) { + if (sxe2->node) { + return 1; + } else if (sxe1->document->ptr == sxe2->document->ptr) { + return 0; + } + } else { + return !(sxe1->node == sxe2->node); + } + return 1; +} +/* }}} */ + +/* {{{ proto array SimpleXMLElement::xpath(string path) + Runs XPath query on the XML data */ +SXE_METHOD(xpath) +{ + php_sxe_object *sxe; + zval *value; + char *query; + int query_len; + int i; + int nsnbr = 0; + xmlNsPtr *ns = NULL; + xmlXPathObjectPtr retval; + xmlNodeSetPtr result; + xmlNodePtr nodeptr; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query, &query_len) == FAILURE) { + return; + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + return; /* attributes don't have attributes */ + } + + if (!sxe->xpath) { + sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr); + } + if (!sxe->node) { + php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement((xmlDocPtr) sxe->document->ptr), NULL TSRMLS_CC); + } + + nodeptr = php_sxe_get_first_node(sxe, sxe->node->node TSRMLS_CC); + + sxe->xpath->node = nodeptr; + + ns = xmlGetNsList((xmlDocPtr) sxe->document->ptr, nodeptr); + if (ns != NULL) { + while (ns[nsnbr] != NULL) { + nsnbr++; + } + } + + sxe->xpath->namespaces = ns; + sxe->xpath->nsNr = nsnbr; + + retval = xmlXPathEval((xmlChar *)query, sxe->xpath); + if (ns != NULL) { + xmlFree(ns); + sxe->xpath->namespaces = NULL; + sxe->xpath->nsNr = 0; + } + + if (!retval) { + RETURN_FALSE; + } + + result = retval->nodesetval; + + array_init(return_value); + + if (result != NULL) { + for (i = 0; i < result->nodeNr; ++i) { + nodeptr = result->nodeTab[i]; + if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE) { + MAKE_STD_ZVAL(value); + /** + * Detect the case where the last selector is text(), simplexml + * always accesses the text() child by default, therefore we assign + * to the parent node. + */ + if (nodeptr->type == XML_TEXT_NODE) { + _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC); + } else if (nodeptr->type == XML_ATTRIBUTE_NODE) { + _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, nodeptr->ns ? (xmlChar *)nodeptr->ns->href : NULL, 0 TSRMLS_CC); + } else { + _node_as_zval(sxe, nodeptr, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC); + } + + add_next_index_zval(return_value, value); + } + } + } + + xmlXPathFreeObject(retval); +} +/* }}} */ + +/* {{{ proto bool SimpleXMLElement::registerXPathNamespace(string prefix, string ns) + Creates a prefix/ns context for the next XPath query */ +SXE_METHOD(registerXPathNamespace) +{ + php_sxe_object *sxe; + int prefix_len, ns_uri_len; + char *prefix, *ns_uri; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &prefix, &prefix_len, &ns_uri, &ns_uri_len) == FAILURE) { + return; + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + if (!sxe->xpath) { + sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr); + } + + if (xmlXPathRegisterNs(sxe->xpath, (xmlChar *)prefix, (xmlChar *)ns_uri) != 0) { + RETURN_FALSE + } + RETURN_TRUE; +} + +/* }}} */ + +/* {{{ proto string SimpleXMLElement::asXML([string filename]) + Return a well-formed XML string based on SimpleXML element */ +SXE_METHOD(asXML) +{ + php_sxe_object *sxe; + xmlNodePtr node; + xmlOutputBufferPtr outbuf; + xmlChar *strval; + int strval_len; + char *filename; + int filename_len; + + if (ZEND_NUM_ARGS() > 1) { + RETURN_FALSE; + } + + if (ZEND_NUM_ARGS() == 1) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) { + RETURN_FALSE; + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + GET_NODE(sxe, node); + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + + if (node) { + if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) { + int bytes; + bytes = xmlSaveFile(filename, (xmlDocPtr) sxe->document->ptr); + if (bytes == -1) { + RETURN_FALSE; + } else { + RETURN_TRUE; + } + } else { + outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0); + + if (outbuf == NULL) { + RETURN_FALSE; + } + + xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL); + xmlOutputBufferClose(outbuf); + RETURN_TRUE; + } + } else { + RETURN_FALSE; + } + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + GET_NODE(sxe, node); + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + + if (node) { + if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) { + xmlDocDumpMemoryEnc((xmlDocPtr) sxe->document->ptr, &strval, &strval_len, ((xmlDocPtr) sxe->document->ptr)->encoding); + RETVAL_STRINGL((char *)strval, strval_len, 1); + xmlFree(strval); + } else { + /* Should we be passing encoding information instead of NULL? */ + outbuf = xmlAllocOutputBuffer(NULL); + + if (outbuf == NULL) { + RETURN_FALSE; + } + + xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, ((xmlDocPtr) sxe->document->ptr)->encoding); + xmlOutputBufferFlush(outbuf); +#ifdef LIBXML2_NEW_BUFFER + RETVAL_STRINGL((char *)xmlOutputBufferGetContent(outbuf), + xmlOutputBufferGetSize(outbuf), 1); +#else + RETVAL_STRINGL((char *)outbuf->buffer->content, outbuf->buffer->use, 1); +#endif + xmlOutputBufferClose(outbuf); + } + } else { + RETVAL_FALSE; + } +} +/* }}} */ + +#define SXE_NS_PREFIX(ns) (ns->prefix ? (char*)ns->prefix : "") + +static inline void sxe_add_namespace_name(zval *return_value, xmlNsPtr ns) +{ + char *prefix = SXE_NS_PREFIX(ns); + if (zend_hash_exists(Z_ARRVAL_P(return_value), prefix, strlen(prefix) + 1) == 0) { + add_assoc_string(return_value, prefix, (char*)ns->href, 1); + } +} + +static void sxe_add_namespaces(php_sxe_object *sxe, xmlNodePtr node, zend_bool recursive, zval *return_value TSRMLS_DC) /* {{{ */ +{ + xmlAttrPtr attr; + + if (node->ns) { + sxe_add_namespace_name(return_value, node->ns); + } + + attr = node->properties; + while (attr) { + if (attr->ns) { + sxe_add_namespace_name(return_value, attr->ns); + } + attr = attr->next; + } + + if (recursive) { + node = node->children; + while (node) { + if (node->type == XML_ELEMENT_NODE) { + sxe_add_namespaces(sxe, node, recursive, return_value TSRMLS_CC); + } + node = node->next; + } + } +} /* }}} */ + +/* {{{ proto string SimpleXMLElement::getNamespaces([bool recursve]) + Return all namespaces in use */ +SXE_METHOD(getNamespaces) +{ + zend_bool recursive = 0; + php_sxe_object *sxe; + xmlNodePtr node; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &recursive) == FAILURE) { + return; + } + + array_init(return_value); + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + GET_NODE(sxe, node); + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + + if (node) { + if (node->type == XML_ELEMENT_NODE) { + sxe_add_namespaces(sxe, node, recursive, return_value TSRMLS_CC); + } else if (node->type == XML_ATTRIBUTE_NODE && node->ns) { + sxe_add_namespace_name(return_value, node->ns); + } + } +} +/* }}} */ + +static void sxe_add_registered_namespaces(php_sxe_object *sxe, xmlNodePtr node, zend_bool recursive, zval *return_value TSRMLS_DC) /* {{{ */ +{ + xmlNsPtr ns; + + if (node->type == XML_ELEMENT_NODE) { + ns = node->nsDef; + while (ns != NULL) { + sxe_add_namespace_name(return_value, ns); + ns = ns->next; + } + if (recursive) { + node = node->children; + while (node) { + sxe_add_registered_namespaces(sxe, node, recursive, return_value TSRMLS_CC); + node = node->next; + } + } + } +} +/* }}} */ + +/* {{{ proto string SimpleXMLElement::getDocNamespaces([bool recursive]) + Return all namespaces registered with document */ +SXE_METHOD(getDocNamespaces) +{ + zend_bool recursive = 0; + php_sxe_object *sxe; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &recursive) == FAILURE) { + return; + } + + array_init(return_value); + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + + sxe_add_registered_namespaces(sxe, xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr), recursive, return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto object SimpleXMLElement::children([string ns [, bool is_prefix]]) + Finds children of given node */ +SXE_METHOD(children) +{ + php_sxe_object *sxe; + char *nsprefix = NULL; + int nsprefix_len; + xmlNodePtr node; + zend_bool isprefix = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) { + return; + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + return; /* attributes don't have attributes */ + } + + GET_NODE(sxe, node); + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + + _node_as_zval(sxe, node, return_value, SXE_ITER_CHILD, NULL, (xmlChar *)nsprefix, isprefix TSRMLS_CC); + +} +/* }}} */ + +/* {{{ proto object SimpleXMLElement::getName() + Finds children of given node */ +SXE_METHOD(getName) +{ + php_sxe_object *sxe; + xmlNodePtr node; + int namelen; + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + + GET_NODE(sxe, node); + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + if (node) { + namelen = xmlStrlen(node->name); + RETURN_STRINGL((char*)node->name, namelen, 1); + } else { + RETURN_EMPTY_STRING(); + } +} +/* }}} */ + +/* {{{ proto array SimpleXMLElement::attributes([string ns [, bool is_prefix]]) + Identifies an element's attributes */ +SXE_METHOD(attributes) +{ + php_sxe_object *sxe; + char *nsprefix = NULL; + int nsprefix_len; + xmlNodePtr node; + zend_bool isprefix = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) { + return; + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + GET_NODE(sxe, node); + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + return; /* attributes don't have attributes */ + } + + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + + _node_as_zval(sxe, node, return_value, SXE_ITER_ATTRLIST, NULL, (xmlChar *)nsprefix, isprefix TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto void SimpleXMLElement::addChild(string qName [, string value [, string ns]]) + Add Element with optional namespace information */ +SXE_METHOD(addChild) +{ + php_sxe_object *sxe; + char *qname, *value = NULL, *nsuri = NULL; + int qname_len, value_len = 0, nsuri_len = 0; + xmlNodePtr node, newnode; + xmlNsPtr nsptr = NULL; + xmlChar *localname, *prefix = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!s!", + &qname, &qname_len, &value, &value_len, &nsuri, &nsuri_len) == FAILURE) { + return; + } + + if (qname_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Element name is required"); + return; + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + GET_NODE(sxe, node); + + if (sxe->iter.type == SXE_ITER_ATTRLIST) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add element to attributes"); + return; + } + + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + + if (node == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot add child. Parent is not a permanent member of the XML tree"); + return; + } + + localname = xmlSplitQName2((xmlChar *)qname, &prefix); + if (localname == NULL) { + localname = xmlStrdup((xmlChar *)qname); + } + + newnode = xmlNewChild(node, NULL, localname, (xmlChar *)value); + + if (nsuri != NULL) { + if (nsuri_len == 0) { + newnode->ns = NULL; + nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix); + } else { + nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri); + if (nsptr == NULL) { + nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix); + } + newnode->ns = nsptr; + } + } + + _node_as_zval(sxe, newnode, return_value, SXE_ITER_NONE, (char *)localname, prefix, 0 TSRMLS_CC); + + xmlFree(localname); + if (prefix != NULL) { + xmlFree(prefix); + } +} +/* }}} */ + +/* {{{ proto void SimpleXMLElement::addAttribute(string qName, string value [,string ns]) + Add Attribute with optional namespace information */ +SXE_METHOD(addAttribute) +{ + php_sxe_object *sxe; + char *qname, *value = NULL, *nsuri = NULL; + int qname_len, value_len = 0, nsuri_len = 0; + xmlNodePtr node; + xmlAttrPtr attrp = NULL; + xmlNsPtr nsptr = NULL; + xmlChar *localname, *prefix = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s!", + &qname, &qname_len, &value, &value_len, &nsuri, &nsuri_len) == FAILURE) { + return; + } + + if (qname_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute name is required"); + return; + } + + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + GET_NODE(sxe, node); + + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + + if (node && node->type != XML_ELEMENT_NODE) { + node = node->parent; + } + + if (node == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate parent Element"); + return; + } + + localname = xmlSplitQName2((xmlChar *)qname, &prefix); + if (localname == NULL) { + if (nsuri_len > 0) { + if (prefix != NULL) { + xmlFree(prefix); + } + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute requires prefix for namespace"); + return; + } + localname = xmlStrdup((xmlChar *)qname); + } + + attrp = xmlHasNsProp(node, localname, (xmlChar *)nsuri); + if (attrp != NULL && attrp->type != XML_ATTRIBUTE_DECL) { + xmlFree(localname); + if (prefix != NULL) { + xmlFree(prefix); + } + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute already exists"); + return; + } + + if (nsuri != NULL) { + nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri); + if (nsptr == NULL) { + nsptr = xmlNewNs(node, (xmlChar *)nsuri, prefix); + } + } + + attrp = xmlNewNsProp(node, nsptr, localname, (xmlChar *)value); + + xmlFree(localname); + if (prefix != NULL) { + xmlFree(prefix); + } +} +/* }}} */ + +/* {{{ cast_object() + */ +static int cast_object(zval *object, int type, char *contents TSRMLS_DC) +{ + if (contents) { + ZVAL_STRINGL(object, contents, strlen(contents), 1); + } else { + ZVAL_NULL(object); + } + object->refcount = 1; + object->is_ref = 0; + + switch (type) { + case IS_STRING: + convert_to_string(object); + break; + case IS_BOOL: + convert_to_boolean(object); + break; + case IS_LONG: + convert_to_long(object); + break; + case IS_DOUBLE: + convert_to_double(object); + break; + default: + return FAILURE; + } + return SUCCESS; +} +/* }}} */ + +/* {{{ sxe_object_cast() + */ +static int sxe_object_cast(zval *readobj, zval *writeobj, int type TSRMLS_DC) +{ + php_sxe_object *sxe; + xmlChar *contents = NULL; + xmlNodePtr node; + int rv; + + sxe = php_sxe_fetch_object(readobj TSRMLS_CC); + + if (type == IS_BOOL) { + node = php_sxe_get_first_node(sxe, NULL TSRMLS_CC); + INIT_PZVAL(writeobj); + ZVAL_BOOL(writeobj, node != NULL || zend_hash_num_elements(sxe_properties_get(readobj TSRMLS_CC)) > 0); + return SUCCESS; + } + + if (sxe->iter.type != SXE_ITER_NONE) { + node = php_sxe_get_first_node(sxe, NULL TSRMLS_CC); + if (node) { + contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, node->children, 1); + } + } else { + if (!sxe->node) { + if (sxe->document) { + php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement((xmlDocPtr) sxe->document->ptr), NULL TSRMLS_CC); + } + } + + if (sxe->node && sxe->node->node) { + if (sxe->node->node->children) { + contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, sxe->node->node->children, 1); + } + } + } + + if (readobj == writeobj) { + INIT_PZVAL(writeobj); + zval_dtor(readobj); + } + + rv = cast_object(writeobj, type, (char *)contents TSRMLS_CC); + + if (contents) { + xmlFree(contents); + } + return rv; +} +/* }}} */ + +static int sxe_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */ +{ + php_sxe_object *sxe; + xmlNodePtr node; + zval *data; + + *count = 0; + sxe = php_sxe_fetch_object(object TSRMLS_CC); + + data = sxe->iter.data; + sxe->iter.data = NULL; + + node = php_sxe_reset_iterator(sxe, 0 TSRMLS_CC); + + while (node) + { + (*count)++; + node = php_sxe_iterator_fetch(sxe, node->next, 0 TSRMLS_CC); + } + + if (sxe->iter.data) { + zval_ptr_dtor(&sxe->iter.data); + } + sxe->iter.data = data; + + return SUCCESS; +} +/* }}} */ + +static zval *sxe_get_value(zval *z TSRMLS_DC) +{ + zval *retval; + + MAKE_STD_ZVAL(retval); + + if (sxe_object_cast(z, retval, IS_STRING TSRMLS_CC)==FAILURE) { + zend_error(E_ERROR, "Unable to cast node to string"); + /* FIXME: Should not be fatal */ + } + + retval->refcount = 0; + return retval; +} + + +static zend_object_handlers sxe_object_handlers = { + ZEND_OBJECTS_STORE_HANDLERS, + sxe_property_read, + sxe_property_write, + sxe_dimension_read, + sxe_dimension_write, + sxe_property_get_adr, + sxe_get_value, /* get */ + NULL, + sxe_property_exists, + sxe_property_delete, + sxe_dimension_exists, + sxe_dimension_delete, + sxe_properties_get, + NULL, /* zend_get_std_object_handlers()->get_method,*/ + NULL, /* zend_get_std_object_handlers()->call_method,*/ + NULL, /* zend_get_std_object_handlers()->get_constructor, */ + NULL, /* zend_get_std_object_handlers()->get_class_entry,*/ + NULL, /* zend_get_std_object_handlers()->get_class_name,*/ + sxe_objects_compare, + sxe_object_cast, + sxe_count_elements +}; + +static zend_object_handlers sxe_ze1_object_handlers = { + ZEND_OBJECTS_STORE_HANDLERS, + sxe_property_read, + sxe_property_write, + sxe_dimension_read, + sxe_dimension_write, + sxe_property_get_adr, + sxe_get_value, /* get */ + NULL, + sxe_property_exists, + sxe_property_delete, + sxe_dimension_exists, + sxe_dimension_delete, + sxe_properties_get, + NULL, /* zend_get_std_object_handlers()->get_method,*/ + NULL, /* zend_get_std_object_handlers()->call_method,*/ + NULL, /* zend_get_std_object_handlers()->get_constructor, */ + NULL, /* zend_get_std_object_handlers()->get_class_entry,*/ + NULL, /* zend_get_std_object_handlers()->get_class_name,*/ + sxe_objects_compare, + sxe_object_cast, + sxe_count_elements +}; + +static zend_object_value sxe_object_ze1_clone(zval *zobject TSRMLS_DC) +{ + php_error(E_ERROR, "Cannot clone object of class %s due to 'zend.ze1_compatibility_mode'", Z_OBJCE_P(zobject)->name); + /* Return zobject->value.obj just to satisfy compiler */ + /* FIXME: Should not be a fatal */ + return zobject->value.obj; +} + +/* {{{ sxe_object_clone() + */ +static void +sxe_object_clone(void *object, void **clone_ptr TSRMLS_DC) +{ + php_sxe_object *sxe = (php_sxe_object *) object; + php_sxe_object *clone; + xmlNodePtr nodep = NULL; + xmlDocPtr docp = NULL; + + clone = php_sxe_object_new(sxe->zo.ce TSRMLS_CC); + clone->document = sxe->document; + if (clone->document) { + clone->document->refcount++; + docp = clone->document->ptr; + } + + clone->iter.isprefix = sxe->iter.isprefix; + if (sxe->iter.name != NULL) { + clone->iter.name = xmlStrdup((xmlChar *)sxe->iter.name); + } + if (sxe->iter.nsprefix != NULL) { + clone->iter.nsprefix = xmlStrdup((xmlChar *)sxe->iter.nsprefix); + } + clone->iter.type = sxe->iter.type; + + if (sxe->node) { + nodep = xmlDocCopyNode(sxe->node->node, docp, 1); + } + + php_libxml_increment_node_ptr((php_libxml_node_object *)clone, nodep, NULL TSRMLS_CC); + + *clone_ptr = (void *) clone; +} +/* }}} */ + +/* {{{ sxe_object_dtor() + */ +static void sxe_object_dtor(void *object, zend_object_handle handle TSRMLS_DC) +{ + /* dtor required to cleanup iterator related data properly */ + + php_sxe_object *sxe; + + sxe = (php_sxe_object *) object; + + if (sxe->iter.data) { + zval_ptr_dtor(&sxe->iter.data); + sxe->iter.data = NULL; + } + + if (sxe->iter.name) { + xmlFree(sxe->iter.name); + sxe->iter.name = NULL; + } + if (sxe->iter.nsprefix) { + xmlFree(sxe->iter.nsprefix); + sxe->iter.nsprefix = NULL; + } + if (sxe->tmp) { + zval_ptr_dtor(&sxe->tmp); + sxe->tmp = NULL; + } +} +/* }}} */ + +/* {{{ sxe_object_free_storage() + */ +static void sxe_object_free_storage(void *object TSRMLS_DC) +{ + php_sxe_object *sxe; + + sxe = (php_sxe_object *) object; + +#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 && PHP_RELEASE_VERSION > 2) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION > 5) + zend_object_std_dtor(&sxe->zo TSRMLS_CC); +#else + if (sxe->zo.guards) { + zend_hash_destroy(sxe->zo.guards); + FREE_HASHTABLE(sxe->zo.guards); + } + + if (sxe->zo.properties) { + zend_hash_destroy(sxe->zo.properties); + FREE_HASHTABLE(sxe->zo.properties); + } +#endif + + php_libxml_node_decrement_resource((php_libxml_node_object *)sxe TSRMLS_CC); + + if (sxe->xpath) { + xmlXPathFreeContext(sxe->xpath); + } + + if (sxe->properties) { + zend_hash_destroy(sxe->properties); + FREE_HASHTABLE(sxe->properties); + } + + efree(object); +} +/* }}} */ + +/* {{{ php_sxe_object_new() + */ +static php_sxe_object* php_sxe_object_new(zend_class_entry *ce TSRMLS_DC) +{ + php_sxe_object *intern; + + intern = ecalloc(1, sizeof(php_sxe_object)); + + intern->iter.type = SXE_ITER_NONE; + intern->iter.nsprefix = NULL; + intern->iter.name = NULL; + +#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 && PHP_RELEASE_VERSION > 2) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION > 5) + zend_object_std_init(&intern->zo, ce TSRMLS_CC); +#else + ALLOC_HASHTABLE(intern->zo.properties); + zend_hash_init(intern->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0); + + intern->zo.ce = ce; + intern->zo.guards = NULL; +#endif + + return intern; +} +/* }}} */ + +/* {{{ php_sxe_register_object + */ +static zend_object_value +php_sxe_register_object(php_sxe_object *intern TSRMLS_DC) +{ + zend_object_value rv; + + rv.handle = zend_objects_store_put(intern, sxe_object_dtor, (zend_objects_free_object_storage_t)sxe_object_free_storage, sxe_object_clone TSRMLS_CC); + if (EG(ze1_compatibility_mode)) { + rv.handlers = (zend_object_handlers *) &sxe_ze1_object_handlers; + } else { + rv.handlers = (zend_object_handlers *) &sxe_object_handlers; + } + + return rv; +} +/* }}} */ + +/* {{{ sxe_object_new() + */ +ZEND_API zend_object_value +sxe_object_new(zend_class_entry *ce TSRMLS_DC) +{ + php_sxe_object *intern; + + intern = php_sxe_object_new(ce TSRMLS_CC); + return php_sxe_register_object(intern TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto simplemxml_element simplexml_load_file(string filename [, string class_name [, int options [, string ns [, bool is_prefix]]]]) + Load a filename and return a simplexml_element object to allow for processing */ +PHP_FUNCTION(simplexml_load_file) +{ + php_sxe_object *sxe; + char *filename; + int filename_len; + xmlDocPtr docp; + char *ns = NULL; + int ns_len = 0; + long options = 0; + zend_class_entry *ce= sxe_class_entry; + zend_bool isprefix = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C!lsb", &filename, &filename_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) { + return; + } + + docp = xmlReadFile(filename, NULL, options); + + if (! docp) { + RETURN_FALSE; + } + + if (!ce) { + ce = sxe_class_entry; + } + sxe = php_sxe_object_new(ce TSRMLS_CC); + sxe->iter.nsprefix = ns_len ? xmlStrdup((xmlChar *)ns) : NULL; + sxe->iter.isprefix = isprefix; + php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp TSRMLS_CC); + php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL TSRMLS_CC); + + return_value->type = IS_OBJECT; + return_value->value.obj = php_sxe_register_object(sxe TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto simplemxml_element simplexml_load_string(string data [, string class_name [, int options [, string ns [, bool is_prefix]]]]) + Load a string and return a simplexml_element object to allow for processing */ +PHP_FUNCTION(simplexml_load_string) +{ + php_sxe_object *sxe; + char *data; + int data_len; + xmlDocPtr docp; + char *ns = NULL; + int ns_len = 0; + long options = 0; + zend_class_entry *ce= sxe_class_entry; + zend_bool isprefix = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C!lsb", &data, &data_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) { + return; + } + + docp = xmlReadMemory(data, data_len, NULL, NULL, options); + + if (! docp) { + RETURN_FALSE; + } + + if (!ce) { + ce = sxe_class_entry; + } + sxe = php_sxe_object_new(ce TSRMLS_CC); + sxe->iter.nsprefix = ns_len ? xmlStrdup((xmlChar *)ns) : NULL; + sxe->iter.isprefix = isprefix; + php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp TSRMLS_CC); + php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL TSRMLS_CC); + + return_value->type = IS_OBJECT; + return_value->value.obj = php_sxe_register_object(sxe TSRMLS_CC); +} +/* }}} */ + + +/* {{{ proto SimpleXMLElement::__construct(string data [, int options [, bool data_is_url [, string ns [, bool is_prefix]]]]) + SimpleXMLElement constructor */ +SXE_METHOD(__construct) +{ + php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + char *data, *ns = NULL; + int data_len, ns_len = 0; + xmlDocPtr docp; + long options = 0; + zend_bool is_url = 0, isprefix = 0; + + php_set_error_handling(EH_THROW, zend_exception_get_default(TSRMLS_C) TSRMLS_CC); + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lbsb", &data, &data_len, &options, &is_url, &ns, &ns_len, &isprefix) == FAILURE) { + php_std_error_handling(); + return; + } + + php_std_error_handling(); + + docp = is_url ? xmlReadFile(data, NULL, options) : xmlReadMemory(data, data_len, NULL, NULL, options); + + if (!docp) { + ((php_libxml_node_object *)sxe)->document = NULL; + zend_throw_exception(zend_exception_get_default(TSRMLS_C), "String could not be parsed as XML", 0 TSRMLS_CC); + return; + } + + sxe->iter.nsprefix = ns_len ? xmlStrdup((xmlChar *)ns) : NULL; + sxe->iter.isprefix = isprefix; + php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp TSRMLS_CC); + php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL TSRMLS_CC); +} +/* }}} */ + + +static void php_sxe_iterator_dtor(zend_object_iterator *iter TSRMLS_DC); +static int php_sxe_iterator_valid(zend_object_iterator *iter TSRMLS_DC); +static void php_sxe_iterator_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC); +static int php_sxe_iterator_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC); +static void php_sxe_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC); +static void php_sxe_iterator_rewind(zend_object_iterator *iter TSRMLS_DC); + +zend_object_iterator_funcs php_sxe_iterator_funcs = { + php_sxe_iterator_dtor, + php_sxe_iterator_valid, + php_sxe_iterator_current_data, + php_sxe_iterator_current_key, + php_sxe_iterator_move_forward, + php_sxe_iterator_rewind, +}; + +static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data TSRMLS_DC) +{ + xmlChar *prefix = sxe->iter.nsprefix; + int isprefix = sxe->iter.isprefix; + int test_elem = sxe->iter.type == SXE_ITER_ELEMENT && sxe->iter.name; + int test_attr = sxe->iter.type == SXE_ITER_ATTRLIST && sxe->iter.name; + + while (node) { + SKIP_TEXT(node); + if (sxe->iter.type != SXE_ITER_ATTRLIST && node->type == XML_ELEMENT_NODE) { + if ((!test_elem || !xmlStrcmp(node->name, sxe->iter.name)) && match_ns(sxe, node, prefix, isprefix)) { + break; + } + } else if (node->type == XML_ATTRIBUTE_NODE) { + if ((!test_attr || !xmlStrcmp(node->name, sxe->iter.name)) && match_ns(sxe, node, prefix, isprefix)) { + break; + } + } +next_iter: + node = node->next; + } + + if (node && use_data) { + ALLOC_INIT_ZVAL(sxe->iter.data); + _node_as_zval(sxe, node, sxe->iter.data, SXE_ITER_NONE, NULL, prefix, isprefix TSRMLS_CC); + } + + return node; +} + +static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data TSRMLS_DC) +{ + xmlNodePtr node; + + if (sxe->iter.data) { + zval_ptr_dtor(&sxe->iter.data); + sxe->iter.data = NULL; + } + + GET_NODE(sxe, node) + + if (node) { + switch (sxe->iter.type) { + case SXE_ITER_ELEMENT: + case SXE_ITER_CHILD: + case SXE_ITER_NONE: + node = node->children; + break; + case SXE_ITER_ATTRLIST: + node = (xmlNodePtr) node->properties; + } + return php_sxe_iterator_fetch(sxe, node, use_data TSRMLS_CC); + } + return NULL; +} + +zend_object_iterator *php_sxe_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) +{ + php_sxe_iterator *iterator; + + if (by_ref) { + zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); + } + iterator = emalloc(sizeof(php_sxe_iterator)); + + object->refcount++; + iterator->intern.data = (void*)object; + iterator->intern.funcs = &php_sxe_iterator_funcs; + iterator->sxe = php_sxe_fetch_object(object TSRMLS_CC); + + return (zend_object_iterator*)iterator; +} + +static void php_sxe_iterator_dtor(zend_object_iterator *iter TSRMLS_DC) +{ + php_sxe_iterator *iterator = (php_sxe_iterator *)iter; + + /* cleanup handled in sxe_object_dtor as we dont always have an iterator wrapper */ + if (iterator->intern.data) { + zval_ptr_dtor((zval**)&iterator->intern.data); + } + + efree(iterator); +} + +static int php_sxe_iterator_valid(zend_object_iterator *iter TSRMLS_DC) +{ + php_sxe_iterator *iterator = (php_sxe_iterator *)iter; + + return iterator->sxe->iter.data ? SUCCESS : FAILURE; +} + +static void php_sxe_iterator_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) +{ + php_sxe_iterator *iterator = (php_sxe_iterator *)iter; + + *data = &iterator->sxe->iter.data; +} + +static int php_sxe_iterator_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC) +{ + zval *curobj; + xmlNodePtr curnode = NULL; + php_sxe_object *intern; + int namelen; + + php_sxe_iterator *iterator = (php_sxe_iterator *)iter; + curobj = iterator->sxe->iter.data; + + intern = (php_sxe_object *)zend_object_store_get_object(curobj TSRMLS_CC); + if (intern != NULL && intern->node != NULL) { + curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node; + } + if (!curnode) { + return HASH_KEY_NON_EXISTANT; + } + + namelen = xmlStrlen(curnode->name); + *str_key = estrndup((char *)curnode->name, namelen); + *str_key_len = namelen + 1; + return HASH_KEY_IS_STRING; + +} + +ZEND_API void php_sxe_move_forward_iterator(php_sxe_object *sxe TSRMLS_DC) +{ + xmlNodePtr node = NULL; + php_sxe_object *intern; + + if (sxe->iter.data) { + intern = (php_sxe_object *)zend_object_store_get_object(sxe->iter.data TSRMLS_CC); + GET_NODE(intern, node) + zval_ptr_dtor(&sxe->iter.data); + sxe->iter.data = NULL; + } + + if (node) { + php_sxe_iterator_fetch(sxe, node->next, 1 TSRMLS_CC); + } +} + +static void php_sxe_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC) +{ + php_sxe_iterator *iterator = (php_sxe_iterator *)iter; + php_sxe_move_forward_iterator(iterator->sxe TSRMLS_CC); +} + +static void php_sxe_iterator_rewind(zend_object_iterator *iter TSRMLS_DC) +{ + php_sxe_object *sxe; + + php_sxe_iterator *iterator = (php_sxe_iterator *)iter; + sxe = iterator->sxe; + + php_sxe_reset_iterator(sxe, 1 TSRMLS_CC); +} + +void *simplexml_export_node(zval *object TSRMLS_DC) +{ + php_sxe_object *sxe; + xmlNodePtr node; + + sxe = php_sxe_fetch_object(object TSRMLS_CC); + GET_NODE(sxe, node); + return php_sxe_get_first_node(sxe, node TSRMLS_CC); +} + +/* {{{ proto simplemxml_element simplexml_import_dom(domNode node [, string class_name]) + Get a simplexml_element object from dom to allow for processing */ +PHP_FUNCTION(simplexml_import_dom) +{ + php_sxe_object *sxe; + zval *node; + php_libxml_node_object *object; + xmlNodePtr nodep = NULL; + zend_class_entry *ce= sxe_class_entry; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o|C!", &node, &ce) == FAILURE) { + return; + } + + object = (php_libxml_node_object *)zend_object_store_get_object(node TSRMLS_CC); + + nodep = php_libxml_import_node(node TSRMLS_CC); + + if (nodep) { + if (nodep->doc == NULL) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Imported Node must have associated Document"); + RETURN_NULL(); + } + if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) { + nodep = xmlDocGetRootElement((xmlDocPtr) nodep); + } + } + + if (nodep && nodep->type == XML_ELEMENT_NODE) { + if (!ce) { + ce = sxe_class_entry; + } + sxe = php_sxe_object_new(ce TSRMLS_CC); + sxe->document = object->document; + php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, nodep->doc TSRMLS_CC); + php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, nodep, NULL TSRMLS_CC); + + return_value->type = IS_OBJECT; + return_value->value.obj = php_sxe_register_object(sxe TSRMLS_CC); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Nodetype to import"); + RETVAL_NULL(); + } +} +/* }}} */ + +zend_function_entry simplexml_functions[] = { + PHP_FE(simplexml_load_file, NULL) + PHP_FE(simplexml_load_string, NULL) + PHP_FE(simplexml_import_dom, NULL) + {NULL, NULL, NULL} +}; + +static zend_module_dep simplexml_deps[] = { + ZEND_MOD_REQUIRED("libxml") + {NULL, NULL, NULL} +}; + +zend_module_entry simplexml_module_entry = { + STANDARD_MODULE_HEADER_EX, NULL, + simplexml_deps, + "SimpleXML", + simplexml_functions, + PHP_MINIT(simplexml), + PHP_MSHUTDOWN(simplexml), + NULL, + NULL, + PHP_MINFO(simplexml), + "0.1", + STANDARD_MODULE_PROPERTIES +}; + +#ifdef COMPILE_DL_SIMPLEXML +ZEND_GET_MODULE(simplexml) +#endif + +/* the method table */ +/* each method can have its own parameters and visibility */ +static zend_function_entry sxe_functions[] = { + SXE_ME(__construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) /* must be called */ + SXE_ME(asXML, NULL, ZEND_ACC_PUBLIC) + SXE_MALIAS(saveXML, asXML, NULL, ZEND_ACC_PUBLIC) + SXE_ME(xpath, NULL, ZEND_ACC_PUBLIC) + SXE_ME(registerXPathNamespace, NULL, ZEND_ACC_PUBLIC) + SXE_ME(attributes, NULL, ZEND_ACC_PUBLIC) + SXE_ME(children, NULL, ZEND_ACC_PUBLIC) + SXE_ME(getNamespaces, NULL, ZEND_ACC_PUBLIC) + SXE_ME(getDocNamespaces, NULL, ZEND_ACC_PUBLIC) + SXE_ME(getName, NULL, ZEND_ACC_PUBLIC) + SXE_ME(addChild, NULL, ZEND_ACC_PUBLIC) + SXE_ME(addAttribute, NULL, ZEND_ACC_PUBLIC) + {NULL, NULL, NULL} +}; + +/* {{{ PHP_MINIT_FUNCTION(simplexml) + */ +PHP_MINIT_FUNCTION(simplexml) +{ + zend_class_entry sxe; + + INIT_CLASS_ENTRY(sxe, "SimpleXMLElement", sxe_functions); + sxe.create_object = sxe_object_new; + sxe_class_entry = zend_register_internal_class(&sxe TSRMLS_CC); + sxe_class_entry->get_iterator = php_sxe_get_iterator; + sxe_class_entry->iterator_funcs.funcs = &php_sxe_iterator_funcs; + zend_class_implements(sxe_class_entry TSRMLS_CC, 1, zend_ce_traversable); + sxe_object_handlers.get_method = zend_get_std_object_handlers()->get_method; + sxe_object_handlers.get_constructor = zend_get_std_object_handlers()->get_constructor; + sxe_object_handlers.get_class_entry = zend_get_std_object_handlers()->get_class_entry; + sxe_object_handlers.get_class_name = zend_get_std_object_handlers()->get_class_name; + + sxe_ze1_object_handlers.get_method = zend_get_std_object_handlers()->get_method; + sxe_ze1_object_handlers.get_constructor = zend_get_std_object_handlers()->get_constructor; + sxe_ze1_object_handlers.get_class_entry = zend_get_std_object_handlers()->get_class_entry; + sxe_ze1_object_handlers.get_class_name = zend_get_std_object_handlers()->get_class_name; + sxe_ze1_object_handlers.clone_obj = sxe_object_ze1_clone; + +#ifdef HAVE_SPL + if (zend_get_module_started("spl") == SUCCESS) { + PHP_MINIT(spl_sxe)(INIT_FUNC_ARGS_PASSTHRU); + } +#endif /* HAVE_SPL */ + + php_libxml_register_export(sxe_class_entry, simplexml_export_node); + + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MSHUTDOWN_FUNCTION(simplexml) + */ +PHP_MSHUTDOWN_FUNCTION(simplexml) +{ + sxe_class_entry = NULL; + return SUCCESS; +} +/* }}} */ +/* {{{ PHP_MINFO_FUNCTION(simplexml) + */ +PHP_MINFO_FUNCTION(simplexml) +{ + php_info_print_table_start(); + php_info_print_table_header(2, "Simplexml support", "enabled"); + php_info_print_table_row(2, "Revision", "$Revision: 299016 $"); + php_info_print_table_row(2, "Schema support", +#ifdef LIBXML_SCHEMAS_ENABLED + "enabled"); +#else + "not available"); +#endif + php_info_print_table_end(); +} +/* }}} */ + +#endif + +/** + * Local Variables: + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: t + * End: + * vim600: fdm=marker + * vim: noet sw=4 ts=4 + */