diff options
-rw-r--r-- | ext/simplexml/php_simplexml.h | 15 | ||||
-rw-r--r-- | ext/simplexml/simplexml.c | 167 | ||||
-rwxr-xr-x | ext/simplexml/tests/026.phpt | 40 | ||||
-rwxr-xr-x | ext/simplexml/tests/027.phpt | 74 | ||||
-rwxr-xr-x | ext/simplexml/tests/028.phpt | 42 | ||||
-rwxr-xr-x | ext/simplexml/tests/bug35785.phpt | 25 |
6 files changed, 305 insertions, 58 deletions
diff --git a/ext/simplexml/php_simplexml.h b/ext/simplexml/php_simplexml.h index cbd869c540..15e0221ec2 100644 --- a/ext/simplexml/php_simplexml.h +++ b/ext/simplexml/php_simplexml.h @@ -53,6 +53,13 @@ PHP_RINIT_FUNCTION(simplexml); #endif PHP_MINFO_FUNCTION(simplexml); +typedef enum { + SXE_ITER_NONE = 0, + SXE_ITER_ELEMENT = 1, + SXE_ITER_CHILD = 2, + SXE_ITER_ATTRLIST = 3 +} SXE_ITER; + typedef struct { zend_object zo; php_libxml_node_ptr *node; @@ -63,16 +70,12 @@ typedef struct { int itertype; char *name; char *nsprefix; - int type; + SXE_ITER type; zval *data; } iter; + zval *tmp; } php_sxe_object; -#define SXE_ITER_NONE 0 -#define SXE_ITER_ELEMENT 1 -#define SXE_ITER_CHILD 2 -#define SXE_ITER_ATTRLIST 3 - #ifdef ZTS #define SIMPLEXML_G(v) TSRMG(simplexml_globals_id, zend_simplexml_globals *, v) #else diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index edd29e13f1..7980c00bd5 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -130,10 +130,10 @@ static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name) } /* }}} */ -static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, long offset, xmlNodePtr node) /* {{{ */ +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) { return NULL; } @@ -151,12 +151,15 @@ static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, long offset, xm next_iter: node = node->next; } + + if (cnt) { + *cnt = nodendx; + } return node; } /* }}} */ -#if SXE_ELEMENT_BY_NAME static xmlNodePtr sxe_find_element_by_name(php_sxe_object *sxe, xmlNodePtr node, char *name TSRMLS_DC) /* {{{ */ { while (node) { @@ -172,7 +175,7 @@ next_iter: return NULL; } /* }}} */ -static xmlNodePtr sxe_get_element_by_name(php_sxe_object *sxe, xmlNodePtr node, char **name, int *type TSRMLS_DC) /* {{{ */ +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; @@ -219,7 +222,6 @@ next_iter: return NULL; } /* }}} */ -#endif /* SXE_ELEMENT_BY_NAME */ /* {{{ sxe_prop_dim_read() */ @@ -231,7 +233,7 @@ static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, xmlNodePtr node; xmlAttrPtr attr = NULL; zval tmp_zv; - int nodendx = 0; + int nodendx = 0; int test = 0; sxe = php_sxe_fetch_object(object TSRMLS_CC); @@ -303,7 +305,7 @@ static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, 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); + node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL); if (node) { _node_as_zval(sxe, node, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix TSRMLS_CC); } @@ -329,6 +331,10 @@ static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, if (member == &tmp_zv) { zval_dtor(&tmp_zv); } + if (Z_TYPE_P(return_value) == IS_NULL) { + zval_ptr_dtor(&return_value); + return_value = &EG(uninitialized_zval); + } return return_value; } @@ -356,6 +362,11 @@ static void change_node_zval(xmlNodePtr node, zval *value TSRMLS_DC) { zval value_copy; + if (!value) + { + xmlNodeSetContentLen(node, "", 0); + return; + } switch (Z_TYPE_P(value)) { case IS_LONG: case IS_BOOL: @@ -383,18 +394,20 @@ static void change_node_zval(xmlNodePtr node, zval *value TSRMLS_DC) /* {{{ sxe_property_write() */ -static void sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_bool elements, zend_bool attribs TSRMLS_DC) +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; char *name; 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; + long cnt; zval tmp_zv, trim_zv; if (!member) { @@ -444,9 +457,31 @@ static void sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_boo 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 (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: + convert_to_string(value); + break; + case IS_STRING: + break; + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "It is not yet possible to assign complex types to %s", attribs ? "attributes" : "properties"); + } } if (node) { @@ -478,7 +513,7 @@ static void sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_boo if (elements) { if (Z_TYPE_P(member) == IS_LONG) { - newnode = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node); + newnode = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt); if (newnode) { ++counter; } @@ -502,34 +537,35 @@ next_iter: if (is_attr) { newnode = (xmlNodePtr) attr; } - while ((tempnode = (xmlNodePtr) newnode->children)) { - xmlUnlinkNode(tempnode); - php_libxml_node_free_resource((xmlNodePtr) tempnode TSRMLS_CC); + 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); } - 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 (attribs) { - switch (Z_TYPE_P(value)) { - case IS_LONG: - case IS_BOOL: - case IS_DOUBLE: - case IS_NULL: - convert_to_string(value); - case IS_STRING: - newnode = (xmlNodePtr)xmlNewProp(node, name, Z_STRVAL_P(value)); - break; - default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "It is not yet possible to assign complex types to attributes"); + } else if (elements) { + if (!node) { + newnode = xmlNewTextChild(mynode, mynode->ns, name, value ? Z_STRVAL_P(value) : NULL); + } else if (Z_TYPE_P(member) == IS_LONG) { + if (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 ? Z_STRVAL_P(value) : NULL); } + } else if (attribs) { + newnode = (xmlNodePtr)xmlNewProp(node, name, value ? Z_STRVAL_P(value) : NULL); } } if (member == &tmp_zv) { zval_dtor(&tmp_zv); } + if (pnewnode) { + *pnewnode = newnode; + } } /* }}} */ @@ -537,7 +573,7 @@ next_iter: */ static void sxe_property_write(zval *object, zval *member, zval *value TSRMLS_DC) { - sxe_prop_dim_write(object, member, value, 1, 0 TSRMLS_CC); + sxe_prop_dim_write(object, member, value, 1, 0, NULL TSRMLS_CC); } /* }}} */ @@ -545,7 +581,37 @@ static void sxe_property_write(zval *object, zval *member, zval *value TSRMLS_DC */ static void sxe_dimension_write(zval *object, zval *offset, zval *value TSRMLS_DC) { - sxe_prop_dim_write(object, offset, value, 0, 1 TSRMLS_CC); + 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) { + 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 TSRMLS_CC); + + sxe = php_sxe_fetch_object(return_value TSRMLS_CC); + sxe->tmp = return_value; + return_value->is_ref = 1; + + return &sxe->tmp; } /* }}} */ @@ -554,7 +620,6 @@ static void sxe_dimension_write(zval *object, zval *offset, zval *value TSRMLS_D 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; - char *name; xmlNodePtr node; xmlAttrPtr attr = NULL; int exists = 0; @@ -562,8 +627,6 @@ static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend sxe = php_sxe_fetch_object(object TSRMLS_CC); - name = Z_STRVAL_P(member); - GET_NODE(sxe, node); if (Z_TYPE_P(member) == IS_LONG) { @@ -591,7 +654,7 @@ static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend if (node) { if (attribs) { while (attr) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { exists = 1; break; } @@ -605,7 +668,7 @@ static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend 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); + node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL); } else { zval tmp_zv; @@ -628,7 +691,7 @@ static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend if (member == &tmp_zv) { zval_dtor(&tmp_zv); } - } + } if (node) { exists = 1; } @@ -680,7 +743,7 @@ static void sxe_prop_dim_delete(zval *object, zval *member, zend_bool elements, if (sxe->iter.type == SXE_ITER_ATTRLIST) { attribs = 1; elements = 0; - node = php_sxe_get_first_node(sxe, node TSRMLS_CC); + node = php_sxe_get_first_node(sxe, node TSRMLS_CC); attr = (xmlAttrPtr)node; test = sxe->iter.name != NULL; } else { @@ -1000,7 +1063,7 @@ SXE_METHOD(xpath) _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_NONE, NULL, NULL TSRMLS_CC); } else if (nodeptr->type == XML_ATTRIBUTE_NODE) { _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, NULL TSRMLS_CC); - } else { + } else { _node_as_zval(sxe, nodeptr, value, SXE_ITER_NONE, NULL, NULL TSRMLS_CC); } @@ -1215,7 +1278,7 @@ SXE_METHOD(getDocNamespaces) } /* }}} */ -/* {{{ proto object SimpleXMLElement::children() +/* {{{ proto object SimpleXMLElement::children([string ns]) Finds children of given node */ SXE_METHOD(children) { @@ -1242,6 +1305,23 @@ SXE_METHOD(children) } /* }}} */ +/* {{{ 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); + + namelen = xmlStrlen(node->name); + RETURN_STRINGL((char*)node->name, namelen, 1); +} +/* }}} */ + /* {{{ proto array SimpleXMLElement::attributes([string ns]) Identifies an element's attributes */ SXE_METHOD(attributes) @@ -1396,7 +1476,7 @@ static zend_object_handlers sxe_object_handlers = { sxe_property_write, sxe_dimension_read, sxe_dimension_write, - NULL, + sxe_property_get_adr, sxe_get_value, /* get */ NULL, sxe_property_exists, @@ -1420,7 +1500,7 @@ static zend_object_handlers sxe_ze1_object_handlers = { sxe_property_write, sxe_dimension_read, sxe_dimension_write, - NULL, + sxe_property_get_adr, sxe_get_value, /* get */ NULL, sxe_property_exists, @@ -1495,6 +1575,10 @@ static void sxe_object_dtor(void *object, zend_object_handle handle TSRMLS_DC) xmlFree(sxe->iter.nsprefix); sxe->iter.nsprefix = NULL; } + if (sxe->tmp) { + zval_ptr_dtor(&sxe->tmp); + sxe->tmp = NULL; + } } /* }}} */ @@ -1961,11 +2045,12 @@ 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_ME(xpath, NULL, ZEND_ACC_PUBLIC) - SXE_ME(registerXPathNamespace, 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(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) {NULL, NULL, NULL} }; diff --git a/ext/simplexml/tests/026.phpt b/ext/simplexml/tests/026.phpt new file mode 100755 index 0000000000..d6de94be3d --- /dev/null +++ b/ext/simplexml/tests/026.phpt @@ -0,0 +1,40 @@ +--TEST-- +SimpleXML: getName() +--SKIPIF-- +<?php if (!extension_loaded("simplexml")) print "skip"; ?> +--FILE-- +<?php +$xml =<<<EOF +<people> + <person>Jane</person> +</people> +EOF; + +function traverse_xml($xml, $pad = '') +{ + $name = $xml->getName(); + echo "$pad<$name"; + foreach($xml->attributes() as $attr => $value) + { + echo " $attr=\"$value\""; + } + echo ">" . trim($xml) . "\n"; + foreach($xml->children() as $node) + { + traverse_xml($node, $pad.' '); + } + echo $pad."</$name>\n"; +} + + +$people = simplexml_load_string($xml); +traverse_xml($people); + +?> +===DONE=== +--EXPECTF-- +<people> + <person>Jane + </person> +</people> +===DONE=== diff --git a/ext/simplexml/tests/027.phpt b/ext/simplexml/tests/027.phpt new file mode 100755 index 0000000000..0c8f4e2847 --- /dev/null +++ b/ext/simplexml/tests/027.phpt @@ -0,0 +1,74 @@ +--TEST-- +SimpleXML: Adding an elements +--SKIPIF-- +<?php if (!extension_loaded("simplexml")) print "skip"; ?> +--FILE-- +<?php +$xml =<<<EOF +<people></people> +EOF; + +function traverse_xml($xml, $pad = '') +{ + $name = $xml->getName(); + echo "$pad<$name"; + foreach($xml->attributes() as $attr => $value) + { + echo " $attr=\"$value\""; + } + echo ">" . trim($xml) . "\n"; + foreach($xml->children() as $node) + { + traverse_xml($node, $pad.' '); + } + echo $pad."</$name>\n"; +} + + +$people = simplexml_load_string($xml); +traverse_xml($people); +$people->person = 'Joe'; +$people->person['gender'] = 'male'; +traverse_xml($people); +$people->person = 'Jane'; +traverse_xml($people); +$people->person['gender'] = 'female'; +$people->person[1] = 'Joe'; +$people->person[1]['gender'] = 'male'; +traverse_xml($people); +$people->person[3] = 'Minni-me'; +$people->person[2]['gender'] = 'male'; +traverse_xml($people); +$people->person[3]['gender'] = 'error'; + +?> +===DONE=== +--EXPECTF-- +<people> +</people> +<people> + <person gender="male">Joe + </person> +</people> +<people> + <person gender="male">Jane + </person> +</people> +<people> + <person gender="female">Jane + </person> + <person gender="male">Joe + </person> +</people> + +Warning: main(): Cannot add element person number 3 when only 2 such elements exist in %sext/simplexml/tests/027.php on line %d +<people> + <person gender="female">Jane + </person> + <person gender="male">Joe + </person> + <person gender="male">Minni-me + </person> +</people> + +Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in %sext/simplexml/tests/027.php on line %d diff --git a/ext/simplexml/tests/028.phpt b/ext/simplexml/tests/028.phpt new file mode 100755 index 0000000000..753056b9ad --- /dev/null +++ b/ext/simplexml/tests/028.phpt @@ -0,0 +1,42 @@ +--TEST-- +SimpleXML: Adding an elements without text +--SKIPIF-- +<?php if (!extension_loaded("simplexml")) print "skip"; ?> +--FILE-- +<?php +$xml =<<<EOF +<people></people> +EOF; + +function traverse_xml($xml, $pad = '') +{ + $name = $xml->getName(); + echo "$pad<$name"; + foreach($xml->attributes() as $attr => $value) + { + echo " $attr=\"$value\""; + } + echo ">" . trim($xml) . "\n"; + foreach($xml->children() as $node) + { + traverse_xml($node, $pad.' '); + } + echo $pad."</$name>\n"; +} + + +$people = simplexml_load_string($xml); +traverse_xml($people); +$people->person['name'] = 'John'; +traverse_xml($people); + +?> +===DONE=== +--EXPECTF-- +<people> +</people> +<people> + <person name="John"> + </person> +</people> +===DONE=== diff --git a/ext/simplexml/tests/bug35785.phpt b/ext/simplexml/tests/bug35785.phpt index 34489c5b10..fb3cea231f 100755 --- a/ext/simplexml/tests/bug35785.phpt +++ b/ext/simplexml/tests/bug35785.phpt @@ -3,21 +3,24 @@ Bug #35785 (SimpleXML memory read error) --FILE-- <?php -$options["database"] = "xmldatabase"; $xml = simplexml_load_string("<root></root>"); -$count = count($xml->posts) + 1; -$xml->bla->posts[$count]->name = $_POST["name"]; +$xml->bla->posts->name = "FooBar"; +echo $xml->asXML(); + +echo "===FAIL===\n"; + +$xml = simplexml_load_string("<root></root>"); +$count = count($xml->bla->posts); +var_dump($count); +$xml->bla->posts[++$count]->name = "FooBar"; echo $xml->asXML(); ?> ===DONE=== <?php exit(0); __halt_compiler(); ?> --EXPECTF-- - -Notice: Undefined index: name in %sbug35785.php on line %d - -Strict Standards: Creating default object from empty value in %sbug35785.php on line %d - -Warning: Attempt to assign property of non-object in %sbug35785.php on line %d <?xml version="1.0"?> -<root/> -===DONE=== +<root><bla><posts><name>FooBar</name></posts></bla></root> +===FAIL=== +int(0) + +Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in %sext/simplexml/tests/bug35785.php on line %d |