xref: /PHP-7.0/ext/simplexml/simplexml.c (revision 3a7b0027)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2017 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Sterling Hughes <sterling@php.net>                          |
16   |          Marcus Boerger <helly@php.net>                              |
17   |          Rob Richards <rrichards@php.net>                            |
18   +----------------------------------------------------------------------+
19 */
20 
21 /* $Id: 0637e06af859ca1d0dea9c2f1530e51b98f1970e $ */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "php.h"
28 #if HAVE_LIBXML && HAVE_SIMPLEXML
29 
30 #include "php_ini.h"
31 #include "ext/standard/info.h"
32 #include "ext/standard/php_string.h"
33 #include "php_simplexml.h"
34 #include "php_simplexml_exports.h"
35 #include "zend_exceptions.h"
36 #include "zend_interfaces.h"
37 #include "sxe.h"
38 
39 #define SXE_ELEMENT_BY_NAME 0
40 
41 zend_class_entry *sxe_class_entry = NULL;
42 
sxe_get_element_class_entry()43 PHP_SXE_API zend_class_entry *sxe_get_element_class_entry() /* {{{ */
44 {
45 	return sxe_class_entry;
46 }
47 /* }}} */
48 
49 #define SXE_ME(func, arg_info, flags) PHP_ME(simplexml_element, func, arg_info, flags)
50 #define SXE_MALIAS(func, alias, arg_info, flags) PHP_MALIAS(simplexml_element, func, alias, arg_info, flags)
51 
52 #define SXE_METHOD(func) PHP_METHOD(simplexml_element, func)
53 
54 static php_sxe_object* php_sxe_object_new(zend_class_entry *ce, zend_function *fptr_count);
55 static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data);
56 static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data);
57 static zval *sxe_get_value(zval *z, zval *rv);
58 static void php_sxe_iterator_dtor(zend_object_iterator *iter);
59 static int php_sxe_iterator_valid(zend_object_iterator *iter);
60 static zval *php_sxe_iterator_current_data(zend_object_iterator *iter);
61 static void php_sxe_iterator_current_key(zend_object_iterator *iter, zval *key);
62 static void php_sxe_iterator_move_forward(zend_object_iterator *iter);
63 static void php_sxe_iterator_rewind(zend_object_iterator *iter);
64 
65 /* {{{ _node_as_zval()
66  */
_node_as_zval(php_sxe_object * sxe,xmlNodePtr node,zval * value,SXE_ITER itertype,char * name,const xmlChar * nsprefix,int isprefix)67 static void _node_as_zval(php_sxe_object *sxe, xmlNodePtr node, zval *value, SXE_ITER itertype, char *name, const xmlChar *nsprefix, int isprefix)
68 {
69 	php_sxe_object *subnode;
70 
71 	subnode = php_sxe_object_new(sxe->zo.ce, sxe->fptr_count);
72 	subnode->document = sxe->document;
73 	subnode->document->refcount++;
74 	subnode->iter.type = itertype;
75 	if (name) {
76 		subnode->iter.name = (xmlChar*)estrdup(name);
77 	}
78 	if (nsprefix && *nsprefix) {
79 		subnode->iter.nsprefix = (xmlChar*)estrdup((char*)nsprefix);
80 		subnode->iter.isprefix = isprefix;
81 	}
82 
83 	php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL);
84 
85 	ZVAL_OBJ(value, &subnode->zo);
86 }
87 /* }}} */
88 
89 #define GET_NODE(__s, __n) { \
90 	if ((__s)->node && (__s)->node->node) { \
91 		__n = (__s)->node->node; \
92 	} else { \
93 		__n = NULL; \
94 		php_error_docref(NULL, E_WARNING, "Node no longer exists"); \
95 	} \
96 }
97 
php_sxe_get_first_node(php_sxe_object * sxe,xmlNodePtr node)98 static xmlNodePtr php_sxe_get_first_node(php_sxe_object *sxe, xmlNodePtr node) /* {{{ */
99 {
100 	php_sxe_object *intern;
101 	xmlNodePtr retnode = NULL;
102 
103 	if (sxe && sxe->iter.type != SXE_ITER_NONE) {
104 		php_sxe_reset_iterator(sxe, 1);
105 		if (!Z_ISUNDEF(sxe->iter.data)) {
106 			intern = Z_SXEOBJ_P(&sxe->iter.data);
107 			GET_NODE(intern, retnode)
108 		}
109 		return retnode;
110 	} else {
111 		return node;
112 	}
113 }
114 /* }}} */
115 
match_ns(php_sxe_object * sxe,xmlNodePtr node,xmlChar * name,int prefix)116 static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name, int prefix) /* {{{ */
117 {
118 	if (name == NULL && (node->ns == NULL || node->ns->prefix == NULL)) {
119 		return 1;
120 	}
121 
122 	if (node->ns && !xmlStrcmp(prefix ? node->ns->prefix : node->ns->href, name)) {
123 		return 1;
124 	}
125 
126 	return 0;
127 }
128 /* }}} */
129 
sxe_get_element_by_offset(php_sxe_object * sxe,zend_long offset,xmlNodePtr node,zend_long * cnt)130 static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, zend_long offset, xmlNodePtr node, zend_long *cnt) /* {{{ */
131 {
132 	zend_long nodendx = 0;
133 
134 	if (sxe->iter.type == SXE_ITER_NONE) {
135 		if (offset == 0) {
136 			if (cnt) {
137 				*cnt = 0;
138 			}
139 			return node;
140 		} else {
141 			return NULL;
142 		}
143 	}
144 	while (node && nodendx <= offset) {
145 		SKIP_TEXT(node)
146 		if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
147 			if (sxe->iter.type == SXE_ITER_CHILD || (
148 				sxe->iter.type == SXE_ITER_ELEMENT && !xmlStrcmp(node->name, sxe->iter.name))) {
149 				if (nodendx == offset) {
150 					break;
151 				}
152 				nodendx++;
153 			}
154 		}
155 next_iter:
156 		node = node->next;
157 	}
158 
159 	if (cnt) {
160 		*cnt = nodendx;
161 	}
162 
163 	return node;
164 }
165 /* }}} */
166 
sxe_find_element_by_name(php_sxe_object * sxe,xmlNodePtr node,xmlChar * name)167 static xmlNodePtr sxe_find_element_by_name(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name) /* {{{ */
168 {
169 	while (node) {
170 		SKIP_TEXT(node)
171 		if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
172 			if (!xmlStrcmp(node->name, name)) {
173 				return node;
174 			}
175 		}
176 next_iter:
177 		node = node->next;
178 	}
179 	return NULL;
180 } /* }}} */
181 
sxe_get_element_by_name(php_sxe_object * sxe,xmlNodePtr node,char ** name,SXE_ITER * type)182 static xmlNodePtr sxe_get_element_by_name(php_sxe_object *sxe, xmlNodePtr node, char **name, SXE_ITER *type) /* {{{ */
183 {
184 	int         orgtype;
185 	xmlNodePtr  orgnode = node;
186 	xmlNodePtr  retnode = NULL;
187 
188 	if (sxe->iter.type != SXE_ITER_ATTRLIST)
189 	{
190 		orgtype = sxe->iter.type;
191 		if (sxe->iter.type == SXE_ITER_NONE) {
192 			sxe->iter.type = SXE_ITER_CHILD;
193 		}
194 		node = php_sxe_get_first_node(sxe, node);
195 		sxe->iter.type = orgtype;
196 	}
197 
198 	if (sxe->iter.type == SXE_ITER_ELEMENT) {
199 		orgnode = sxe_find_element_by_name(sxe, node, sxe->iter.name);
200 		if (!orgnode) {
201 			return NULL;
202 		}
203 		node = orgnode->children;
204 	}
205 
206 	while (node) {
207 		SKIP_TEXT(node)
208 		if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
209 			if (!xmlStrcmp(node->name, (xmlChar *)*name)) {
210 				if (1||retnode)
211 				{
212 					*type = SXE_ITER_ELEMENT;
213 					return orgnode;
214 				}
215 				retnode = node;
216 			}
217 		}
218 next_iter:
219 		node = node->next;
220 	}
221 
222 	if (retnode)
223 	{
224 		*type = SXE_ITER_NONE;
225 		*name = NULL;
226 		return retnode;
227 	}
228 
229 	return NULL;
230 }
231 /* }}} */
232 
233 /* {{{ sxe_prop_dim_read()
234  */
sxe_prop_dim_read(zval * object,zval * member,zend_bool elements,zend_bool attribs,int type,zval * rv)235 static zval *sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, zend_bool attribs, int type, zval *rv)
236 {
237 	php_sxe_object *sxe;
238 	char           *name;
239 	xmlNodePtr      node;
240 	xmlAttrPtr      attr = NULL;
241 	zval            tmp_zv;
242 	int             nodendx = 0;
243 	int             test = 0;
244 
245 	sxe = Z_SXEOBJ_P(object);
246 
247 	if (!member) {
248 		if (sxe->iter.type == SXE_ITER_ATTRLIST) {
249 			/* This happens when the user did: $sxe[]->foo = $value */
250 			php_error_docref(NULL, E_ERROR, "Cannot create unnamed attribute");
251 			return NULL;
252 		}
253 		goto long_dim;
254 	} else {
255 		ZVAL_DEREF(member);
256 		if (Z_TYPE_P(member) == IS_LONG) {
257 			if (sxe->iter.type != SXE_ITER_ATTRLIST) {
258 long_dim:
259 				attribs = 0;
260 				elements = 1;
261 			}
262 			name = NULL;
263 		} else {
264 			if (Z_TYPE_P(member) != IS_STRING) {
265 				ZVAL_STR(&tmp_zv, zval_get_string(member));
266 				member = &tmp_zv;
267 			}
268 			name = Z_STRVAL_P(member);
269 		}
270 	}
271 
272 	GET_NODE(sxe, node);
273 
274 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
275 		attribs = 1;
276 		elements = 0;
277 		node = php_sxe_get_first_node(sxe, node);
278 		attr = (xmlAttrPtr)node;
279 		test = sxe->iter.name != NULL;
280 	} else if (sxe->iter.type != SXE_ITER_CHILD) {
281 		node = php_sxe_get_first_node(sxe, node);
282 		attr = node ? node->properties : NULL;
283 		test = 0;
284 		if (!member && node && node->parent &&
285 		    node->parent->type == XML_DOCUMENT_NODE) {
286 			/* This happens when the user did: $sxe[]->foo = $value */
287 			php_error_docref(NULL, E_ERROR, "Cannot create unnamed attribute");
288 			return NULL;
289 		}
290 	}
291 
292 	ZVAL_UNDEF(rv);
293 
294 	if (node) {
295 		if (attribs) {
296 			if (Z_TYPE_P(member) != IS_LONG || sxe->iter.type == SXE_ITER_ATTRLIST) {
297 				if (Z_TYPE_P(member) == IS_LONG) {
298 					while (attr && nodendx <= Z_LVAL_P(member)) {
299 						if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
300 							if (nodendx == Z_LVAL_P(member)) {
301 								_node_as_zval(sxe, (xmlNodePtr) attr, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
302 								break;
303 							}
304 							nodendx++;
305 						}
306 						attr = attr->next;
307 					}
308 				} else {
309 					while (attr) {
310 						if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, (xmlChar *)name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
311 							_node_as_zval(sxe, (xmlNodePtr) attr, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
312 							break;
313 						}
314 						attr = attr->next;
315 					}
316 				}
317 			}
318 		}
319 
320 		if (elements) {
321 			if (!sxe->node) {
322 				php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, node, NULL);
323 			}
324 			if (!member || Z_TYPE_P(member) == IS_LONG) {
325 				zend_long cnt = 0;
326 				xmlNodePtr mynode = node;
327 
328 				if (sxe->iter.type == SXE_ITER_CHILD) {
329 					node = php_sxe_get_first_node(sxe, node);
330 				}
331 				if (sxe->iter.type == SXE_ITER_NONE) {
332 					if (member && Z_LVAL_P(member) > 0) {
333 						php_error_docref(NULL, E_WARNING, "Cannot add element %s number %pd when only 0 such elements exist", mynode->name, Z_LVAL_P(member));
334 					}
335 				} else if (member) {
336 					node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt);
337 				} else {
338 					node = NULL;
339 				}
340 				if (node) {
341 					_node_as_zval(sxe, node, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
342 				} else if (type == BP_VAR_W || type == BP_VAR_RW) {
343 					if (member && cnt < Z_LVAL_P(member)) {
344 						php_error_docref(NULL, E_WARNING, "Cannot add element %s number %pd when only %pd such elements exist", mynode->name, Z_LVAL_P(member), cnt);
345 					}
346 					node = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, NULL);
347 					_node_as_zval(sxe, node, rv, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix);
348 				}
349 			} else {
350 #if SXE_ELEMENT_BY_NAME
351 				int newtype;
352 
353 				GET_NODE(sxe, node);
354 				node = sxe_get_element_by_name(sxe, node, &name, &newtype);
355 				if (node) {
356 					_node_as_zval(sxe, node, rv, newtype, name, sxe->iter.nsprefix, sxe->iter.isprefix);
357 				}
358 #else
359 				/* In BP_VAR_IS mode only return a proper node if it actually exists. */
360 				if (type != BP_VAR_IS || sxe_find_element_by_name(sxe, node->children, (xmlChar *) name)) {
361 					_node_as_zval(sxe, node, rv, SXE_ITER_ELEMENT, name, sxe->iter.nsprefix, sxe->iter.isprefix);
362 				}
363 #endif
364 			}
365 		}
366 	}
367 
368 	if (member == &tmp_zv) {
369 		zval_dtor(&tmp_zv);
370 	}
371 
372 	if (Z_ISUNDEF_P(rv)) {
373 		ZVAL_COPY_VALUE(rv, &EG(uninitialized_zval));
374 	}
375 
376 	return rv;
377 }
378 /* }}} */
379 
380 /* {{{ sxe_property_read()
381  */
sxe_property_read(zval * object,zval * member,int type,void ** cache_slot,zval * rv)382 static zval *sxe_property_read(zval *object, zval *member, int type, void **cache_slot, zval *rv)
383 {
384 	return sxe_prop_dim_read(object, member, 1, 0, type, rv);
385 }
386 /* }}} */
387 
388 /* {{{ sxe_dimension_read()
389  */
sxe_dimension_read(zval * object,zval * offset,int type,zval * rv)390 static zval *sxe_dimension_read(zval *object, zval *offset, int type, zval *rv)
391 {
392 	return sxe_prop_dim_read(object, offset, 0, 1, type, rv);
393 }
394 /* }}} */
395 
396 /* {{{ change_node_zval()
397  */
change_node_zval(xmlNodePtr node,zval * value)398 static void change_node_zval(xmlNodePtr node, zval *value)
399 {
400 	zval value_copy;
401 	xmlChar *buffer;
402 	int buffer_len;
403 
404 	if (!value)
405 	{
406 		xmlNodeSetContentLen(node, (xmlChar *)"", 0);
407 		return;
408 	}
409 	switch (Z_TYPE_P(value)) {
410 		case IS_LONG:
411 		case IS_FALSE:
412 		case IS_TRUE:
413 		case IS_DOUBLE:
414 		case IS_NULL:
415 			if (Z_REFCOUNT_P(value) > 1) {
416 				value_copy = *value;
417 				zval_copy_ctor(&value_copy);
418 				value = &value_copy;
419 			}
420 			convert_to_string(value);
421 			/* break missing intentionally */
422 		case IS_STRING:
423 			buffer = xmlEncodeEntitiesReentrant(node->doc, (xmlChar *)Z_STRVAL_P(value));
424 			buffer_len = xmlStrlen(buffer);
425 			/* check for NULL buffer in case of memory error in xmlEncodeEntitiesReentrant */
426 			if (buffer) {
427 				xmlNodeSetContentLen(node, buffer, buffer_len);
428 				xmlFree(buffer);
429 			}
430 			if (value == &value_copy) {
431 				zval_dtor(value);
432 			}
433 			break;
434 		default:
435 			php_error_docref(NULL, E_WARNING, "It is not possible to assign complex types to nodes");
436 			break;
437 	}
438 }
439 /* }}} */
440 
441 /* {{{ sxe_property_write()
442  */
sxe_prop_dim_write(zval * object,zval * member,zval * value,zend_bool elements,zend_bool attribs,xmlNodePtr * pnewnode)443 static int sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_bool elements, zend_bool attribs, xmlNodePtr *pnewnode)
444 {
445 	php_sxe_object *sxe;
446 	xmlNodePtr      node;
447 	xmlNodePtr      newnode = NULL;
448 	xmlNodePtr      mynode;
449 	xmlNodePtr		tempnode;
450 	xmlAttrPtr      attr = NULL;
451 	int             counter = 0;
452 	int             is_attr = 0;
453 	int				nodendx = 0;
454 	int             test = 0;
455 	int				new_value = 0;
456 	zend_long            cnt = 0;
457 	int				retval = SUCCESS;
458 	zval            tmp_zv, zval_copy;
459 	zend_string    *trim_str;
460 
461 	sxe = Z_SXEOBJ_P(object);
462 
463 	if (!member) {
464 		if (sxe->iter.type == SXE_ITER_ATTRLIST) {
465 			/* This happens when the user did: $sxe[] = $value
466 			 * and could also be E_PARSE, but we use this only during parsing
467 			 * and this is during runtime.
468 			 */
469 			php_error_docref(NULL, E_ERROR, "Cannot create unnamed attribute");
470 			return FAILURE;
471 		}
472 		goto long_dim;
473 	} else {
474 		ZVAL_DEREF(member);
475 		if (Z_TYPE_P(member) == IS_LONG) {
476 			if (sxe->iter.type != SXE_ITER_ATTRLIST) {
477 long_dim:
478 				attribs = 0;
479 				elements = 1;
480 			}
481 		} else {
482 			if (Z_TYPE_P(member) != IS_STRING) {
483 				trim_str = zval_get_string(member);
484 				ZVAL_STR(&tmp_zv, php_trim(trim_str, NULL, 0, 3));
485 				zend_string_release(trim_str);
486 				member = &tmp_zv;
487 			}
488 
489 			if (!Z_STRLEN_P(member)) {
490 				php_error_docref(NULL, E_WARNING, "Cannot write or create unnamed %s", attribs ? "attribute" : "element");
491 				if (member == &tmp_zv) {
492 					zval_dtor(&tmp_zv);
493 				}
494 				return FAILURE;
495 			}
496 		}
497 	}
498 
499 	GET_NODE(sxe, node);
500 
501 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
502 		attribs = 1;
503 		elements = 0;
504 		node = php_sxe_get_first_node(sxe, node);
505 		attr = (xmlAttrPtr)node;
506 		test = sxe->iter.name != NULL;
507 	} else if (sxe->iter.type != SXE_ITER_CHILD) {
508 		mynode = node;
509 		node = php_sxe_get_first_node(sxe, node);
510 		attr = node ? node->properties : NULL;
511 		test = 0;
512 		if (!member && node && node->parent &&
513 		    node->parent->type == XML_DOCUMENT_NODE) {
514 			/* This happens when the user did: $sxe[] = $value
515 			 * and could also be E_PARSE, but we use this only during parsing
516 			 * and this is during runtime.
517 			 */
518 			php_error_docref(NULL, E_ERROR, "Cannot create unnamed attribute");
519 			return FAILURE;
520 		}
521 		if (attribs && !node && sxe->iter.type == SXE_ITER_ELEMENT) {
522 			node = xmlNewChild(mynode, mynode->ns, sxe->iter.name, NULL);
523 			attr = node->properties;
524 		}
525 	}
526 
527 	mynode = node;
528 
529 	if (value) {
530 		switch (Z_TYPE_P(value)) {
531 			case IS_LONG:
532 			case IS_FALSE:
533 			case IS_TRUE:
534 			case IS_DOUBLE:
535 			case IS_NULL:
536 				if (Z_TYPE_P(value) != IS_STRING) {
537 					ZVAL_COPY(&zval_copy, value);
538 					value = &zval_copy;
539 					convert_to_string(value);
540 					new_value = 1;
541 				}
542 				break;
543 			case IS_STRING:
544 				break;
545 			case IS_OBJECT:
546 				if (Z_OBJCE_P(value) == sxe_class_entry) {
547 					value = sxe_get_value(value, &zval_copy);
548 					new_value = 1;
549 					break;
550 				}
551 				/* break is missing intentionally */
552 			default:
553 				if (member == &tmp_zv) {
554 					zval_dtor(&tmp_zv);
555 				}
556 				zend_error(E_WARNING, "It is not yet possible to assign complex types to %s", attribs ? "attributes" : "properties");
557 				return FAILURE;
558 		}
559 	}
560 
561 	if (node) {
562 		if (attribs) {
563 			if (Z_TYPE_P(member) == IS_LONG) {
564 				while (attr && nodendx <= Z_LVAL_P(member)) {
565 					if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
566 						if (nodendx == Z_LVAL_P(member)) {
567 							is_attr = 1;
568 							++counter;
569 							break;
570 						}
571 						nodendx++;
572 					}
573 					attr = attr->next;
574 				}
575 			} else {
576 				while (attr) {
577 					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)) {
578 						is_attr = 1;
579 						++counter;
580 						break;
581 					}
582 					attr = attr->next;
583 				}
584 			}
585 
586 		}
587 
588 		if (elements) {
589 			if (!member || Z_TYPE_P(member) == IS_LONG) {
590 				if (node->type == XML_ATTRIBUTE_NODE) {
591 					php_error_docref(NULL, E_ERROR, "Cannot create duplicate attribute");
592 					return FAILURE;
593 				}
594 
595 				if (sxe->iter.type == SXE_ITER_NONE) {
596 					newnode = node;
597 					++counter;
598 					if (member && Z_LVAL_P(member) > 0) {
599 						php_error_docref(NULL, E_WARNING, "Cannot add element %s number %pd when only 0 such elements exist", mynode->name, Z_LVAL_P(member));
600 						retval = FAILURE;
601 					}
602 				} else if (member) {
603 					newnode = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, &cnt);
604 					if (newnode) {
605 						++counter;
606 					}
607 				}
608 			} else {
609 				node = node->children;
610 				while (node) {
611 					SKIP_TEXT(node);
612 
613 					if (!xmlStrcmp(node->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
614 						newnode = node;
615 						++counter;
616 					}
617 
618 next_iter:
619 					node = node->next;
620 				}
621 			}
622 		}
623 
624 		if (counter == 1) {
625 			if (is_attr) {
626 				newnode = (xmlNodePtr) attr;
627 			}
628 			if (value) {
629 				while ((tempnode = (xmlNodePtr) newnode->children)) {
630 					xmlUnlinkNode(tempnode);
631 					php_libxml_node_free_resource((xmlNodePtr) tempnode);
632 				}
633 				change_node_zval(newnode, value);
634 			}
635 		} else if (counter > 1) {
636 			php_error_docref(NULL, E_WARNING, "Cannot assign to an array of nodes (duplicate subnodes or attr detected)");
637 			retval = FAILURE;
638 		} else if (elements) {
639 			if (!node) {
640 				if (!member || Z_TYPE_P(member) == IS_LONG) {
641 					newnode = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, value ? (xmlChar *)Z_STRVAL_P(value) : NULL);
642 				} else {
643 					newnode = xmlNewTextChild(mynode, mynode->ns, (xmlChar *)Z_STRVAL_P(member), value ? (xmlChar *)Z_STRVAL_P(value) : NULL);
644 				}
645 			} else if (!member || Z_TYPE_P(member) == IS_LONG) {
646 				if (member && cnt < Z_LVAL_P(member)) {
647 					php_error_docref(NULL, E_WARNING, "Cannot add element %s number %pd when only %pd such elements exist", mynode->name, Z_LVAL_P(member), cnt);
648 					retval = FAILURE;
649 				}
650 				newnode = xmlNewTextChild(mynode->parent, mynode->ns, mynode->name, value ? (xmlChar *)Z_STRVAL_P(value) : NULL);
651 			}
652 		} else if (attribs) {
653 			if (Z_TYPE_P(member) == IS_LONG) {
654 				php_error_docref(NULL, E_WARNING, "Cannot change attribute number %pd when only %d attributes exist", Z_LVAL_P(member), nodendx);
655 				retval = FAILURE;
656 			} else {
657 				newnode = (xmlNodePtr)xmlNewProp(node, (xmlChar *)Z_STRVAL_P(member), value ? (xmlChar *)Z_STRVAL_P(value) : NULL);
658 			}
659 		}
660 	}
661 
662 	if (member == &tmp_zv) {
663 		zval_dtor(&tmp_zv);
664 	}
665 	if (pnewnode) {
666 		*pnewnode = newnode;
667 	}
668 	if (new_value) {
669 		zval_ptr_dtor(value);
670 	}
671 	return retval;
672 }
673 /* }}} */
674 
675 /* {{{ sxe_property_write()
676  */
sxe_property_write(zval * object,zval * member,zval * value,void ** cache_slot)677 static void sxe_property_write(zval *object, zval *member, zval *value, void **cache_slot)
678 {
679 	sxe_prop_dim_write(object, member, value, 1, 0, NULL);
680 }
681 /* }}} */
682 
683 /* {{{ sxe_dimension_write()
684  */
sxe_dimension_write(zval * object,zval * offset,zval * value)685 static void sxe_dimension_write(zval *object, zval *offset, zval *value)
686 {
687 	sxe_prop_dim_write(object, offset, value, 0, 1, NULL);
688 }
689 /* }}} */
690 
sxe_property_get_adr(zval * object,zval * member,int fetch_type,void ** cache_slot)691 static zval *sxe_property_get_adr(zval *object, zval *member, int fetch_type, void **cache_slot) /* {{{ */
692 {
693 	php_sxe_object *sxe;
694 	xmlNodePtr      node;
695 	zval            ret;
696 	char           *name;
697 	SXE_ITER        type;
698 
699 	sxe = Z_SXEOBJ_P(object);
700 
701 	GET_NODE(sxe, node);
702 	convert_to_string(member);
703 	name = Z_STRVAL_P(member);
704 	node = sxe_get_element_by_name(sxe, node, &name, &type);
705 	if (node) {
706 		return NULL;
707 	}
708 	if (sxe_prop_dim_write(object, member, NULL, 1, 0, &node) != SUCCESS) {
709 		return NULL;
710 	}
711 	type = SXE_ITER_NONE;
712 	name = NULL;
713 
714 	_node_as_zval(sxe, node, &ret, type, name, sxe->iter.nsprefix, sxe->iter.isprefix);
715 
716 	sxe = Z_SXEOBJ_P(&ret);
717 	if (!Z_ISUNDEF(sxe->tmp)) {
718 		zval_ptr_dtor(&sxe->tmp);
719 	}
720 
721 	ZVAL_COPY_VALUE(&sxe->tmp, &ret);
722 
723 	return &sxe->tmp;
724 }
725 /* }}} */
726 
727 /* {{{ sxe_prop_dim_exists()
728  */
sxe_prop_dim_exists(zval * object,zval * member,int check_empty,zend_bool elements,zend_bool attribs)729 static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend_bool elements, zend_bool attribs)
730 {
731 	php_sxe_object *sxe;
732 	xmlNodePtr      node;
733 	xmlAttrPtr      attr = NULL;
734 	int				exists = 0;
735 	int             test = 0;
736 	zval            tmp_zv;
737 
738 	if (Z_TYPE_P(member) != IS_STRING && Z_TYPE_P(member) != IS_LONG) {
739 		ZVAL_STR(&tmp_zv, zval_get_string(member));
740 		member = &tmp_zv;
741 	}
742 
743 	sxe = Z_SXEOBJ_P(object);
744 
745 	GET_NODE(sxe, node);
746 
747 	if (Z_TYPE_P(member) == IS_LONG) {
748 		if (sxe->iter.type != SXE_ITER_ATTRLIST) {
749 			attribs = 0;
750 			elements = 1;
751 			if (sxe->iter.type == SXE_ITER_CHILD) {
752 				node = php_sxe_get_first_node(sxe, node);
753 			}
754 		}
755 	}
756 
757 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
758 		attribs = 1;
759 		elements = 0;
760 		node = php_sxe_get_first_node(sxe, node);
761 		attr = (xmlAttrPtr)node;
762 		test = sxe->iter.name != NULL;
763 	} else if (sxe->iter.type != SXE_ITER_CHILD) {
764 		node = php_sxe_get_first_node(sxe, node);
765 		attr = node ? node->properties : NULL;
766 		test = 0;
767 	}
768 
769 	if (node) {
770 		if (attribs) {
771 			if (Z_TYPE_P(member) == IS_LONG) {
772 				int	nodendx = 0;
773 
774 				while (attr && nodendx <= Z_LVAL_P(member)) {
775 					if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
776 						if (nodendx == Z_LVAL_P(member)) {
777 							exists = 1;
778 							break;
779 						}
780 						nodendx++;
781 					}
782 					attr = attr->next;
783 				}
784 			} else {
785 				while (attr) {
786 					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)) {
787 						exists = 1;
788 						break;
789 					}
790 
791 					attr = attr->next;
792 				}
793 			}
794 			if (exists && check_empty == 1 &&
795 				(!attr->children || !attr->children->content || !attr->children->content[0] || !xmlStrcmp(attr->children->content, (const xmlChar *) "0")) ) {
796 				/* Attribute with no content in it's text node */
797 				exists = 0;
798 			}
799 		}
800 
801 		if (elements) {
802 			if (Z_TYPE_P(member) == IS_LONG) {
803 				if (sxe->iter.type == SXE_ITER_CHILD) {
804 					node = php_sxe_get_first_node(sxe, node);
805 				}
806 				node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL);
807 			} else {
808 				node = sxe_find_element_by_name(sxe, node->children, (xmlChar *)Z_STRVAL_P(member));
809 			}
810 			if (node) {
811 				exists = 1;
812 				if (check_empty == 1 &&
813 					(!node->children || (node->children->type == XML_TEXT_NODE && !node->children->next &&
814 					 (!node->children->content || !node->children->content[0] || !xmlStrcmp(node->children->content, (const xmlChar *) "0")))) ) {
815 					exists = 0;
816 				}
817 			}
818 		}
819 	}
820 
821 	if (member == &tmp_zv) {
822 		zval_dtor(&tmp_zv);
823 	}
824 
825 	return exists;
826 }
827 /* }}} */
828 
829 /* {{{ sxe_property_exists()
830  */
sxe_property_exists(zval * object,zval * member,int check_empty,void ** cache_slot)831 static int sxe_property_exists(zval *object, zval *member, int check_empty, void **cache_slot)
832 {
833 	return sxe_prop_dim_exists(object, member, check_empty, 1, 0);
834 }
835 /* }}} */
836 
837 /* {{{ sxe_dimension_exists()
838  */
sxe_dimension_exists(zval * object,zval * member,int check_empty)839 static int sxe_dimension_exists(zval *object, zval *member, int check_empty)
840 {
841 	return sxe_prop_dim_exists(object, member, check_empty, 0, 1);
842 }
843 /* }}} */
844 
845 /* {{{ sxe_prop_dim_delete()
846  */
sxe_prop_dim_delete(zval * object,zval * member,zend_bool elements,zend_bool attribs)847 static void sxe_prop_dim_delete(zval *object, zval *member, zend_bool elements, zend_bool attribs)
848 {
849 	php_sxe_object *sxe;
850 	xmlNodePtr      node;
851 	xmlNodePtr      nnext;
852 	xmlAttrPtr      attr = NULL;
853 	xmlAttrPtr      anext;
854 	zval            tmp_zv;
855 	int             test = 0;
856 
857 	if (Z_TYPE_P(member) != IS_STRING && Z_TYPE_P(member) != IS_LONG) {
858 		ZVAL_STR(&tmp_zv, zval_get_string(member));
859 		member = &tmp_zv;
860 	}
861 
862 	sxe = Z_SXEOBJ_P(object);
863 
864 	GET_NODE(sxe, node);
865 
866 	if (Z_TYPE_P(member) == IS_LONG) {
867 		if (sxe->iter.type != SXE_ITER_ATTRLIST) {
868 			attribs = 0;
869 			elements = 1;
870 			if (sxe->iter.type == SXE_ITER_CHILD) {
871 				node = php_sxe_get_first_node(sxe, node);
872 			}
873 		}
874 	}
875 
876 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
877 		attribs = 1;
878 		elements = 0;
879 		node = php_sxe_get_first_node(sxe, node);
880 		attr = (xmlAttrPtr)node;
881 		test = sxe->iter.name != NULL;
882 	} else if (sxe->iter.type != SXE_ITER_CHILD) {
883 		node = php_sxe_get_first_node(sxe, node);
884 		attr = node ? node->properties : NULL;
885 		test = 0;
886 	}
887 
888 	if (node) {
889 		if (attribs) {
890 			if (Z_TYPE_P(member) == IS_LONG) {
891 				int	nodendx = 0;
892 
893 				while (attr && nodendx <= Z_LVAL_P(member)) {
894 					if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
895 						if (nodendx == Z_LVAL_P(member)) {
896 							xmlUnlinkNode((xmlNodePtr) attr);
897 							php_libxml_node_free_resource((xmlNodePtr) attr);
898 							break;
899 						}
900 						nodendx++;
901 					}
902 					attr = attr->next;
903 				}
904 			} else {
905 				while (attr) {
906 					anext = attr->next;
907 					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)) {
908 						xmlUnlinkNode((xmlNodePtr) attr);
909 						php_libxml_node_free_resource((xmlNodePtr) attr);
910 						break;
911 					}
912 					attr = anext;
913 				}
914 			}
915 		}
916 
917 		if (elements) {
918 			if (Z_TYPE_P(member) == IS_LONG) {
919 				if (sxe->iter.type == SXE_ITER_CHILD) {
920 					node = php_sxe_get_first_node(sxe, node);
921 				}
922 				node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL);
923 				if (node) {
924 					xmlUnlinkNode(node);
925 					php_libxml_node_free_resource(node);
926 				}
927 			} else {
928 				node = node->children;
929 				while (node) {
930 					nnext = node->next;
931 
932 					SKIP_TEXT(node);
933 
934 					if (!xmlStrcmp(node->name, (xmlChar *)Z_STRVAL_P(member)) && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) {
935 						xmlUnlinkNode(node);
936 						php_libxml_node_free_resource(node);
937 					}
938 
939 next_iter:
940 					node = nnext;
941 				}
942 			}
943 		}
944 	}
945 
946 	if (member == &tmp_zv) {
947 		zval_dtor(&tmp_zv);
948 	}
949 }
950 /* }}} */
951 
952 /* {{{ sxe_property_delete()
953  */
sxe_property_delete(zval * object,zval * member,void ** cache_slot)954 static void sxe_property_delete(zval *object, zval *member, void **cache_slot)
955 {
956 	sxe_prop_dim_delete(object, member, 1, 0);
957 }
958 /* }}} */
959 
960 /* {{{ sxe_dimension_unset()
961  */
sxe_dimension_delete(zval * object,zval * offset)962 static void sxe_dimension_delete(zval *object, zval *offset)
963 {
964 	sxe_prop_dim_delete(object, offset, 0, 1);
965 }
966 /* }}} */
967 
sxe_xmlNodeListGetString(xmlDocPtr doc,xmlNodePtr list,int inLine)968 static inline zend_string *sxe_xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) /* {{{ */
969 {
970 	xmlChar *tmp = xmlNodeListGetString(doc, list, inLine);
971 	zend_string *res;
972 
973 	if (tmp) {
974 		res = zend_string_init((char*)tmp, strlen((char *)tmp), 0);
975 		xmlFree(tmp);
976 	} else {
977 		res = ZSTR_EMPTY_ALLOC();
978 	}
979 
980 	return res;
981 }
982 /* }}} */
983 
984 /* {{{ _get_base_node_value()
985  */
_get_base_node_value(php_sxe_object * sxe_ref,xmlNodePtr node,zval * value,xmlChar * nsprefix,int isprefix)986 static void _get_base_node_value(php_sxe_object *sxe_ref, xmlNodePtr node, zval *value, xmlChar *nsprefix, int isprefix)
987 {
988 	php_sxe_object *subnode;
989 	xmlChar        *contents;
990 
991 	if (node->children && node->children->type == XML_TEXT_NODE && !xmlIsBlankNode(node->children)) {
992 		contents = xmlNodeListGetString(node->doc, node->children, 1);
993 		if (contents) {
994 			ZVAL_STRING(value, (char *)contents);
995 			xmlFree(contents);
996 		}
997 	} else {
998 		subnode = php_sxe_object_new(sxe_ref->zo.ce, sxe_ref->fptr_count);
999 		subnode->document = sxe_ref->document;
1000 		subnode->document->refcount++;
1001 		if (nsprefix && *nsprefix) {
1002 			subnode->iter.nsprefix = (xmlChar*)estrdup((char *)nsprefix);
1003 			subnode->iter.isprefix = isprefix;
1004 		}
1005 		php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL);
1006 
1007 		ZVAL_OBJ(value, &subnode->zo);
1008 		/*zval_add_ref(value);*/
1009 	}
1010 }
1011 /* }}} */
1012 
sxe_properties_add(HashTable * rv,char * name,int namelen,zval * value)1013 static void sxe_properties_add(HashTable *rv, char *name, int namelen, zval *value) /* {{{ */
1014 {
1015 	zend_string *key;
1016 	zval  *data_ptr;
1017 	zval  newptr;
1018 
1019 	key = zend_string_init(name, namelen, 0);
1020 	if ((data_ptr = zend_hash_find(rv, key)) != NULL) {
1021 		if (Z_TYPE_P(data_ptr) == IS_ARRAY) {
1022 			zend_hash_next_index_insert_new(Z_ARRVAL_P(data_ptr), value);
1023 		} else {
1024 			array_init(&newptr);
1025 			zend_hash_next_index_insert_new(Z_ARRVAL(newptr), data_ptr);
1026 			zend_hash_next_index_insert_new(Z_ARRVAL(newptr), value);
1027 			ZVAL_ARR(data_ptr, Z_ARR(newptr));
1028 		}
1029 	} else {
1030 		zend_hash_add_new(rv, key, value);
1031 	}
1032 	zend_string_release(key);
1033 }
1034 /* }}} */
1035 
sxe_prop_is_empty(zval * object)1036 static int sxe_prop_is_empty(zval *object) /* {{{ */
1037 {
1038 	php_sxe_object  *sxe;
1039 	xmlNodePtr       node;
1040 	xmlAttrPtr       attr;
1041 	zval             iter_data;
1042 	int              test;
1043 	int              is_empty;
1044 
1045 	sxe = Z_SXEOBJ_P(object);
1046 
1047 	GET_NODE(sxe, node);
1048 	if (!node) {
1049 		return 1;
1050 	}
1051 
1052 	if (sxe->iter.type == SXE_ITER_ELEMENT) {
1053 		node = php_sxe_get_first_node(sxe, node);
1054 	}
1055 	if (!node || node->type != XML_ENTITY_DECL) {
1056 		attr = node ? (xmlAttrPtr)node->properties : NULL;
1057 		test = sxe->iter.name && sxe->iter.type == SXE_ITER_ATTRLIST;
1058 		while (attr) {
1059 			if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr)attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
1060 				return 0;
1061 			}
1062 			attr = attr->next;
1063 		}
1064 	}
1065 
1066 	GET_NODE(sxe, node);
1067 	node = php_sxe_get_first_node(sxe, node);
1068 	is_empty = 1;
1069 	ZVAL_UNDEF(&iter_data);
1070 	if (node && sxe->iter.type != SXE_ITER_ATTRLIST) {
1071 		if (node->type == XML_ATTRIBUTE_NODE) {
1072 			return 0;
1073 		} else if (sxe->iter.type != SXE_ITER_CHILD) {
1074 			if (sxe->iter.type == SXE_ITER_NONE || !node->children || !node->parent || node->children->next || node->children->children || node->parent->children == node->parent->last) {
1075 				node = node->children;
1076 			} else {
1077 				ZVAL_COPY_VALUE(&iter_data, &sxe->iter.data);
1078 				ZVAL_UNDEF(&sxe->iter.data);
1079 				node = php_sxe_reset_iterator(sxe, 0);
1080 			}
1081 		}
1082 
1083 		while (node) {
1084 			if (node->children != NULL || node->prev != NULL || node->next != NULL) {
1085 				SKIP_TEXT(node);
1086 			} else {
1087 				if (node->type == XML_TEXT_NODE) {
1088 					const xmlChar *cur = node->content;
1089 					if (*cur != 0) {
1090 						is_empty = 0;
1091 						break;
1092 					}
1093 					goto next_iter;
1094 				}
1095 			}
1096 
1097 			if (node->type == XML_ELEMENT_NODE && (! match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix))) {
1098 				goto next_iter;
1099 			}
1100 
1101 			if (!node->name) {
1102 				goto next_iter;
1103 			}
1104 
1105 			is_empty = 0;
1106 			break;
1107 next_iter:
1108 			if (!Z_ISUNDEF(iter_data)) {
1109 				node = php_sxe_iterator_fetch(sxe, node->next, 0);
1110 			} else {
1111 				node = node->next;
1112 			}
1113 		}
1114 	}
1115 
1116 	if (!Z_ISUNDEF(iter_data)) {
1117 		if (!Z_ISUNDEF(sxe->iter.data)) {
1118 			zval_ptr_dtor(&sxe->iter.data);
1119 		}
1120 		ZVAL_COPY_VALUE(&sxe->iter.data, &iter_data);
1121 	}
1122 
1123 	return is_empty;
1124 }
1125 /* }}} */
1126 
sxe_get_prop_hash(zval * object,int is_debug)1127 static HashTable *sxe_get_prop_hash(zval *object, int is_debug) /* {{{ */
1128 {
1129 	zval            value;
1130 	zval            zattr;
1131 	HashTable       *rv;
1132 	php_sxe_object  *sxe;
1133 	char            *name;
1134 	xmlNodePtr       node;
1135 	xmlAttrPtr       attr;
1136 	int              namelen;
1137 	int              test;
1138 	char 		 	 use_iter;
1139 	zval             iter_data;
1140 
1141 	use_iter = 0;
1142 
1143 	sxe = Z_SXEOBJ_P(object);
1144 
1145 	if (is_debug) {
1146 		ALLOC_HASHTABLE(rv);
1147 		zend_hash_init(rv, 0, NULL, ZVAL_PTR_DTOR, 0);
1148 	} else if (sxe->properties) {
1149 		zend_hash_clean(sxe->properties);
1150 		rv = sxe->properties;
1151 	} else {
1152 		ALLOC_HASHTABLE(rv);
1153 		zend_hash_init(rv, 0, NULL, ZVAL_PTR_DTOR, 0);
1154 		sxe->properties = rv;
1155 	}
1156 
1157 	GET_NODE(sxe, node);
1158 	if (!node) {
1159 		return rv;
1160 	}
1161 	if (is_debug || sxe->iter.type != SXE_ITER_CHILD) {
1162 		if (sxe->iter.type == SXE_ITER_ELEMENT) {
1163 			node = php_sxe_get_first_node(sxe, node);
1164 		}
1165 		if (!node || node->type != XML_ENTITY_DECL) {
1166 			attr = node ? (xmlAttrPtr)node->properties : NULL;
1167 			ZVAL_UNDEF(&zattr);
1168 			test = sxe->iter.name && sxe->iter.type == SXE_ITER_ATTRLIST;
1169 			while (attr) {
1170 				if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr)attr, sxe->iter.nsprefix, sxe->iter.isprefix)) {
1171 					ZVAL_STR(&value, sxe_xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, attr->children, 1));
1172 					namelen = xmlStrlen(attr->name);
1173 					if (Z_ISUNDEF(zattr)) {
1174 						array_init(&zattr);
1175 						sxe_properties_add(rv, "@attributes", sizeof("@attributes") - 1, &zattr);
1176 					}
1177 					add_assoc_zval_ex(&zattr, (char*)attr->name, namelen, &value);
1178 				}
1179 				attr = attr->next;
1180 			}
1181 		}
1182 	}
1183 
1184 	GET_NODE(sxe, node);
1185 	node = php_sxe_get_first_node(sxe, node);
1186 
1187 	if (node && sxe->iter.type != SXE_ITER_ATTRLIST) {
1188 		if (node->type == XML_ATTRIBUTE_NODE) {
1189 			ZVAL_STR(&value, sxe_xmlNodeListGetString(node->doc, node->children, 1));
1190 			zend_hash_next_index_insert(rv, &value);
1191 			node = NULL;
1192 		} else if (sxe->iter.type != SXE_ITER_CHILD) {
1193 
1194 			if ( sxe->iter.type == SXE_ITER_NONE || !node->children || !node->parent || !node->next || node->children->next || node->children->children || node->parent->children == node->parent->last ) {
1195 				node = node->children;
1196 			} else {
1197 				ZVAL_COPY_VALUE(&iter_data, &sxe->iter.data);
1198 				ZVAL_UNDEF(&sxe->iter.data);
1199 
1200 				node = php_sxe_reset_iterator(sxe, 0);
1201 
1202 				use_iter = 1;
1203 			}
1204 		}
1205 
1206 		while (node) {
1207 			if (node->children != NULL || node->prev != NULL || node->next != NULL) {
1208 				SKIP_TEXT(node);
1209 			} else {
1210 				if (node->type == XML_TEXT_NODE) {
1211 					const xmlChar *cur = node->content;
1212 
1213 					if (*cur != 0) {
1214 						ZVAL_STR(&value, sxe_xmlNodeListGetString(node->doc, node, 1));
1215 						zend_hash_next_index_insert(rv, &value);
1216 					}
1217 					goto next_iter;
1218 				}
1219 			}
1220 
1221 			if (node->type == XML_ELEMENT_NODE && (! match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix))) {
1222 				goto next_iter;
1223 			}
1224 
1225 			name = (char *) node->name;
1226 			if (!name) {
1227 				goto next_iter;
1228 			} else {
1229 				namelen = xmlStrlen(node->name);
1230 			}
1231 
1232 			_get_base_node_value(sxe, node, &value, sxe->iter.nsprefix, sxe->iter.isprefix);
1233 
1234 			if ( use_iter ) {
1235 				zend_hash_next_index_insert(rv, &value);
1236 			} else {
1237 				sxe_properties_add(rv, name, namelen, &value);
1238 			}
1239 next_iter:
1240 			if (use_iter) {
1241 				node = php_sxe_iterator_fetch(sxe, node->next, 0);
1242 			} else {
1243 				node = node->next;
1244 			}
1245 		}
1246 	}
1247 
1248 	if (use_iter) {
1249 		if (!Z_ISUNDEF(sxe->iter.data)) {
1250 			zval_ptr_dtor(&sxe->iter.data);
1251 		}
1252 		ZVAL_COPY_VALUE(&sxe->iter.data, &iter_data);
1253 	}
1254 
1255 	return rv;
1256 }
1257 /* }}} */
1258 
sxe_get_gc(zval * object,zval ** table,int * n)1259 static HashTable *sxe_get_gc(zval *object, zval **table, int *n) /* {{{ */ {
1260 	php_sxe_object *sxe;
1261 	sxe = Z_SXEOBJ_P(object);
1262 
1263 	*table = NULL;
1264 	*n = 0;
1265 	return sxe->properties;
1266 }
1267 /* }}} */
1268 
sxe_get_properties(zval * object)1269 static HashTable *sxe_get_properties(zval *object) /* {{{ */
1270 {
1271 	return sxe_get_prop_hash(object, 0);
1272 }
1273 /* }}} */
1274 
sxe_get_debug_info(zval * object,int * is_temp)1275 static HashTable * sxe_get_debug_info(zval *object, int *is_temp) /* {{{ */
1276 {
1277 	*is_temp = 1;
1278 	return sxe_get_prop_hash(object, 1);
1279 }
1280 /* }}} */
1281 
sxe_objects_compare(zval * object1,zval * object2)1282 static int sxe_objects_compare(zval *object1, zval *object2) /* {{{ */
1283 {
1284 	php_sxe_object *sxe1;
1285 	php_sxe_object *sxe2;
1286 
1287 	sxe1 = Z_SXEOBJ_P(object1);
1288 	sxe2 = Z_SXEOBJ_P(object2);
1289 
1290 	if (sxe1->node == NULL) {
1291 		if (sxe2->node) {
1292 			return 1;
1293 		} else if (sxe1->document->ptr == sxe2->document->ptr) {
1294 			return 0;
1295 		}
1296 	} else {
1297 		return !(sxe1->node == sxe2->node);
1298 	}
1299 	return 1;
1300 }
1301 /* }}} */
1302 
1303 /* {{{ proto array SimpleXMLElement::xpath(string path)
1304    Runs XPath query on the XML data */
SXE_METHOD(xpath)1305 SXE_METHOD(xpath)
1306 {
1307 	php_sxe_object    *sxe;
1308 	zval               value;
1309 	char              *query;
1310 	size_t                query_len;
1311 	int                i;
1312 	int                nsnbr = 0;
1313 	xmlNsPtr          *ns = NULL;
1314 	xmlXPathObjectPtr  retval;
1315 	xmlNodeSetPtr      result;
1316 	xmlNodePtr		   nodeptr;
1317 
1318 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query, &query_len) == FAILURE) {
1319 		return;
1320 	}
1321 
1322 	sxe = Z_SXEOBJ_P(getThis());
1323 
1324 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
1325 		return; /* attributes don't have attributes */
1326 	}
1327 
1328 	if (!sxe->xpath) {
1329 		sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr);
1330 	}
1331 	if (!sxe->node) {
1332 		php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement((xmlDocPtr) sxe->document->ptr), NULL);
1333 		if (!sxe->node) {
1334 			RETURN_FALSE;
1335 		}
1336 	}
1337 
1338 	nodeptr = php_sxe_get_first_node(sxe, sxe->node->node);
1339 
1340 	sxe->xpath->node = nodeptr;
1341 
1342  	ns = xmlGetNsList((xmlDocPtr) sxe->document->ptr, nodeptr);
1343 	if (ns != NULL) {
1344 		while (ns[nsnbr] != NULL) {
1345 			nsnbr++;
1346 		}
1347 	}
1348 
1349 	sxe->xpath->namespaces = ns;
1350 	sxe->xpath->nsNr = nsnbr;
1351 
1352 	retval = xmlXPathEval((xmlChar *)query, sxe->xpath);
1353 	if (ns != NULL) {
1354 		xmlFree(ns);
1355 		sxe->xpath->namespaces = NULL;
1356 		sxe->xpath->nsNr = 0;
1357 	}
1358 
1359 	if (!retval) {
1360 		RETURN_FALSE;
1361 	}
1362 
1363 	result = retval->nodesetval;
1364 
1365 	array_init(return_value);
1366 
1367 	if (result != NULL) {
1368 		for (i = 0; i < result->nodeNr; ++i) {
1369 			nodeptr = result->nodeTab[i];
1370 			if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE) {
1371 				/**
1372 				 * Detect the case where the last selector is text(), simplexml
1373 				 * always accesses the text() child by default, therefore we assign
1374 				 * to the parent node.
1375 				 */
1376 				if (nodeptr->type == XML_TEXT_NODE) {
1377 					_node_as_zval(sxe, nodeptr->parent, &value, SXE_ITER_NONE, NULL, NULL, 0);
1378 				} else if (nodeptr->type == XML_ATTRIBUTE_NODE) {
1379 					_node_as_zval(sxe, nodeptr->parent, &value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, nodeptr->ns ? (xmlChar *)nodeptr->ns->href : NULL, 0);
1380 				} else {
1381 					_node_as_zval(sxe, nodeptr, &value, SXE_ITER_NONE, NULL, NULL, 0);
1382 				}
1383 
1384 				add_next_index_zval(return_value, &value);
1385 			}
1386 		}
1387 	}
1388 
1389 	xmlXPathFreeObject(retval);
1390 }
1391 /* }}} */
1392 
1393 /* {{{ proto bool SimpleXMLElement::registerXPathNamespace(string prefix, string ns)
1394    Creates a prefix/ns context for the next XPath query */
SXE_METHOD(registerXPathNamespace)1395 SXE_METHOD(registerXPathNamespace)
1396 {
1397 	php_sxe_object    *sxe;
1398 	size_t prefix_len, ns_uri_len;
1399 	char *prefix, *ns_uri;
1400 
1401 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &prefix, &prefix_len, &ns_uri, &ns_uri_len) == FAILURE) {
1402 		return;
1403 	}
1404 
1405 	sxe = Z_SXEOBJ_P(getThis());
1406 	if (!sxe->xpath) {
1407 		sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr);
1408 	}
1409 
1410 	if (xmlXPathRegisterNs(sxe->xpath, (xmlChar *)prefix, (xmlChar *)ns_uri) != 0) {
1411 		RETURN_FALSE
1412 	}
1413 	RETURN_TRUE;
1414 }
1415 
1416 /* }}} */
1417 
1418 /* {{{ proto string SimpleXMLElement::asXML([string filename])
1419    Return a well-formed XML string based on SimpleXML element */
SXE_METHOD(asXML)1420 SXE_METHOD(asXML)
1421 {
1422 	php_sxe_object     *sxe;
1423 	xmlNodePtr          node;
1424 	xmlOutputBufferPtr  outbuf;
1425 	xmlChar            *strval;
1426 	int                 strval_len;
1427 	char               *filename;
1428 	size_t                 filename_len;
1429 
1430 	if (ZEND_NUM_ARGS() > 1) {
1431 		RETURN_FALSE;
1432 	}
1433 
1434 	if (ZEND_NUM_ARGS() == 1) {
1435 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
1436 			RETURN_FALSE;
1437 		}
1438 
1439 		sxe = Z_SXEOBJ_P(getThis());
1440 		GET_NODE(sxe, node);
1441 		node = php_sxe_get_first_node(sxe, node);
1442 
1443 		if (node) {
1444 			if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
1445 				int bytes;
1446 				bytes = xmlSaveFile(filename, (xmlDocPtr) sxe->document->ptr);
1447 				if (bytes == -1) {
1448 					RETURN_FALSE;
1449 				} else {
1450 					RETURN_TRUE;
1451 				}
1452 			} else {
1453 				outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
1454 
1455 				if (outbuf == NULL) {
1456 					RETURN_FALSE;
1457 				}
1458 
1459 				xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL);
1460 				xmlOutputBufferClose(outbuf);
1461 				RETURN_TRUE;
1462 			}
1463 		} else {
1464 			RETURN_FALSE;
1465 		}
1466 	}
1467 
1468 	sxe = Z_SXEOBJ_P(getThis());
1469 	GET_NODE(sxe, node);
1470 	node = php_sxe_get_first_node(sxe, node);
1471 
1472 	if (node) {
1473 		if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
1474 			xmlDocDumpMemoryEnc((xmlDocPtr) sxe->document->ptr, &strval, &strval_len, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
1475 			if (!strval) {
1476 				RETVAL_FALSE;
1477 			} else {
1478 				RETVAL_STRINGL((char *)strval, strval_len);
1479 			}
1480 			xmlFree(strval);
1481 		} else {
1482 			char *return_content;
1483 			size_t return_len;
1484 			/* Should we be passing encoding information instead of NULL? */
1485 			outbuf = xmlAllocOutputBuffer(NULL);
1486 
1487 			if (outbuf == NULL) {
1488 				RETURN_FALSE;
1489 			}
1490 
1491 			xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
1492 			xmlOutputBufferFlush(outbuf);
1493 #ifdef LIBXML2_NEW_BUFFER
1494 			return_content = (char *)xmlOutputBufferGetContent(outbuf);
1495 			return_len = xmlOutputBufferGetSize(outbuf);
1496 #else
1497 			return_content = (char *)outbuf->buffer->content;
1498 			return_len = outbuf->buffer->use;
1499 #endif
1500 			if (!return_content) {
1501 				RETVAL_FALSE;
1502 			} else {
1503 				RETVAL_STRINGL(return_content, return_len);
1504 			}
1505 			xmlOutputBufferClose(outbuf);
1506 		}
1507 	} else {
1508 		RETVAL_FALSE;
1509 	}
1510 }
1511 /* }}} */
1512 
1513 #define SXE_NS_PREFIX(ns) (ns->prefix ? (char*)ns->prefix : "")
1514 
sxe_add_namespace_name(zval * return_value,xmlNsPtr ns)1515 static inline void sxe_add_namespace_name(zval *return_value, xmlNsPtr ns) /* {{{ */
1516 {
1517 	char *prefix = SXE_NS_PREFIX(ns);
1518 	zend_string *key = zend_string_init(prefix, strlen(prefix), 0);
1519 	zval zv;
1520 
1521 	if (!zend_hash_exists(Z_ARRVAL_P(return_value), key)) {
1522 		ZVAL_STRING(&zv, (char*)ns->href);
1523 		zend_hash_add_new(Z_ARRVAL_P(return_value), key, &zv);
1524 	}
1525 	zend_string_release(key);
1526 }
1527 /* }}} */
1528 
sxe_add_namespaces(php_sxe_object * sxe,xmlNodePtr node,zend_bool recursive,zval * return_value)1529 static void sxe_add_namespaces(php_sxe_object *sxe, xmlNodePtr node, zend_bool recursive, zval *return_value) /* {{{ */
1530 {
1531 	xmlAttrPtr  attr;
1532 
1533 	if (node->ns) {
1534 		sxe_add_namespace_name(return_value, node->ns);
1535 	}
1536 
1537 	attr = node->properties;
1538 	while (attr) {
1539 		if (attr->ns) {
1540 			sxe_add_namespace_name(return_value, attr->ns);
1541 		}
1542 		attr = attr->next;
1543 	}
1544 
1545 	if (recursive) {
1546 		node = node->children;
1547 		while (node) {
1548 			if (node->type == XML_ELEMENT_NODE) {
1549 				sxe_add_namespaces(sxe, node, recursive, return_value);
1550 			}
1551 			node = node->next;
1552 		}
1553 	}
1554 } /* }}} */
1555 
1556 /* {{{ proto string SimpleXMLElement::getNamespaces([bool recursve])
1557    Return all namespaces in use */
SXE_METHOD(getNamespaces)1558 SXE_METHOD(getNamespaces)
1559 {
1560 	zend_bool           recursive = 0;
1561 	php_sxe_object     *sxe;
1562 	xmlNodePtr          node;
1563 
1564 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &recursive) == FAILURE) {
1565 		return;
1566 	}
1567 
1568 	array_init(return_value);
1569 
1570 	sxe = Z_SXEOBJ_P(getThis());
1571 	GET_NODE(sxe, node);
1572 	node = php_sxe_get_first_node(sxe, node);
1573 
1574 	if (node) {
1575 		if (node->type == XML_ELEMENT_NODE) {
1576 			sxe_add_namespaces(sxe, node, recursive, return_value);
1577 		} else if (node->type == XML_ATTRIBUTE_NODE && node->ns) {
1578 			sxe_add_namespace_name(return_value, node->ns);
1579 		}
1580 	}
1581 }
1582 /* }}} */
1583 
sxe_add_registered_namespaces(php_sxe_object * sxe,xmlNodePtr node,zend_bool recursive,zval * return_value)1584 static void sxe_add_registered_namespaces(php_sxe_object *sxe, xmlNodePtr node, zend_bool recursive, zval *return_value) /* {{{ */
1585 {
1586 	xmlNsPtr ns;
1587 
1588 	if (node->type == XML_ELEMENT_NODE) {
1589 		ns = node->nsDef;
1590 		while (ns != NULL) {
1591 			sxe_add_namespace_name(return_value, ns);
1592 			ns = ns->next;
1593 		}
1594 		if (recursive) {
1595 			node = node->children;
1596 			while (node) {
1597 				sxe_add_registered_namespaces(sxe, node, recursive, return_value);
1598 				node = node->next;
1599 			}
1600 		}
1601 	}
1602 }
1603 /* }}} */
1604 
1605 /* {{{ proto string SimpleXMLElement::getDocNamespaces([bool recursive [, bool from_root])
1606    Return all namespaces registered with document */
SXE_METHOD(getDocNamespaces)1607 SXE_METHOD(getDocNamespaces)
1608 {
1609 	zend_bool           recursive = 0, from_root = 1;
1610 	php_sxe_object     *sxe;
1611 	xmlNodePtr          node;
1612 
1613 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|bb", &recursive, &from_root) == FAILURE) {
1614 		return;
1615 	}
1616 
1617 	sxe = Z_SXEOBJ_P(getThis());
1618 	if(from_root){
1619 		node = xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr);
1620 	}else{
1621 		GET_NODE(sxe, node);
1622 	}
1623 
1624 	if (node == NULL) {
1625 		RETURN_FALSE;
1626 	}
1627 
1628 	array_init(return_value);
1629 	sxe_add_registered_namespaces(sxe, node, recursive, return_value);
1630 }
1631 /* }}} */
1632 
1633 /* {{{ proto object SimpleXMLElement::children([string ns [, bool is_prefix]])
1634    Finds children of given node */
SXE_METHOD(children)1635 SXE_METHOD(children)
1636 {
1637 	php_sxe_object *sxe;
1638 	char           *nsprefix = NULL;
1639 	size_t             nsprefix_len = 0;
1640 	xmlNodePtr      node;
1641 	zend_bool       isprefix = 0;
1642 
1643 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) {
1644 		return;
1645 	}
1646 
1647 	sxe = Z_SXEOBJ_P(getThis());
1648 
1649 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
1650 		return; /* attributes don't have attributes */
1651 	}
1652 
1653 	GET_NODE(sxe, node);
1654 	node = php_sxe_get_first_node(sxe, node);
1655 
1656 	_node_as_zval(sxe, node, return_value, SXE_ITER_CHILD, NULL, (xmlChar *)nsprefix, isprefix);
1657 
1658 }
1659 /* }}} */
1660 
1661 /* {{{ proto object SimpleXMLElement::getName()
1662    Finds children of given node */
SXE_METHOD(getName)1663 SXE_METHOD(getName)
1664 {
1665 	php_sxe_object *sxe;
1666 	xmlNodePtr      node;
1667 	int             namelen;
1668 
1669 	sxe = Z_SXEOBJ_P(getThis());
1670 
1671 	GET_NODE(sxe, node);
1672 	node = php_sxe_get_first_node(sxe, node);
1673 	if (node) {
1674 		namelen = xmlStrlen(node->name);
1675 		RETURN_STRINGL((char*)node->name, namelen);
1676 	} else {
1677 		RETURN_EMPTY_STRING();
1678 	}
1679 }
1680 /* }}} */
1681 
1682 /* {{{ proto array SimpleXMLElement::attributes([string ns [, bool is_prefix]])
1683    Identifies an element's attributes */
SXE_METHOD(attributes)1684 SXE_METHOD(attributes)
1685 {
1686 	php_sxe_object *sxe;
1687 	char           *nsprefix = NULL;
1688 	size_t             nsprefix_len = 0;
1689 	xmlNodePtr      node;
1690 	zend_bool       isprefix = 0;
1691 
1692 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) {
1693 		return;
1694 	}
1695 
1696 	sxe = Z_SXEOBJ_P(getThis());
1697 	GET_NODE(sxe, node);
1698 
1699 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
1700 		return; /* attributes don't have attributes */
1701 	}
1702 
1703 	node = php_sxe_get_first_node(sxe, node);
1704 
1705 	_node_as_zval(sxe, node, return_value, SXE_ITER_ATTRLIST, NULL, (xmlChar *)nsprefix, isprefix);
1706 }
1707 /* }}} */
1708 
1709 /* {{{ proto void SimpleXMLElement::addChild(string qName [, string value [, string ns]])
1710    Add Element with optional namespace information */
SXE_METHOD(addChild)1711 SXE_METHOD(addChild)
1712 {
1713 	php_sxe_object *sxe;
1714 	char           *qname, *value = NULL, *nsuri = NULL;
1715 	size_t             qname_len, value_len = 0, nsuri_len = 0;
1716 	xmlNodePtr      node, newnode;
1717 	xmlNsPtr        nsptr = NULL;
1718 	xmlChar        *localname, *prefix = NULL;
1719 
1720 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!s!",
1721 		&qname, &qname_len, &value, &value_len, &nsuri, &nsuri_len) == FAILURE) {
1722 		return;
1723 	}
1724 
1725 	if (qname_len == 0) {
1726 		php_error_docref(NULL, E_WARNING, "Element name is required");
1727 		return;
1728 	}
1729 
1730 	sxe = Z_SXEOBJ_P(getThis());
1731 	GET_NODE(sxe, node);
1732 
1733 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
1734 		php_error_docref(NULL, E_WARNING, "Cannot add element to attributes");
1735 		return;
1736 	}
1737 
1738 	node = php_sxe_get_first_node(sxe, node);
1739 
1740 	if (node == NULL) {
1741 		php_error_docref(NULL, E_WARNING, "Cannot add child. Parent is not a permanent member of the XML tree");
1742 		return;
1743 	}
1744 
1745 	localname = xmlSplitQName2((xmlChar *)qname, &prefix);
1746 	if (localname == NULL) {
1747 		localname = xmlStrdup((xmlChar *)qname);
1748 	}
1749 
1750 	newnode = xmlNewChild(node, NULL, localname, (xmlChar *)value);
1751 
1752 	if (nsuri != NULL) {
1753 		if (nsuri_len == 0) {
1754 			newnode->ns = NULL;
1755 			nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix);
1756 		} else {
1757 			nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri);
1758 			if (nsptr == NULL) {
1759 				nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix);
1760 			}
1761 			newnode->ns = nsptr;
1762 		}
1763 	}
1764 
1765 	_node_as_zval(sxe, newnode, return_value, SXE_ITER_NONE, (char *)localname, prefix, 0);
1766 
1767 	xmlFree(localname);
1768 	if (prefix != NULL) {
1769 		xmlFree(prefix);
1770 	}
1771 }
1772 /* }}} */
1773 
1774 /* {{{ proto void SimpleXMLElement::addAttribute(string qName, string value [,string ns])
1775    Add Attribute with optional namespace information */
SXE_METHOD(addAttribute)1776 SXE_METHOD(addAttribute)
1777 {
1778 	php_sxe_object *sxe;
1779 	char           *qname, *value = NULL, *nsuri = NULL;
1780 	size_t             qname_len, value_len = 0, nsuri_len = 0;
1781 	xmlNodePtr      node;
1782 	xmlAttrPtr      attrp = NULL;
1783 	xmlNsPtr        nsptr = NULL;
1784 	xmlChar        *localname, *prefix = NULL;
1785 
1786 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|s!",
1787 		&qname, &qname_len, &value, &value_len, &nsuri, &nsuri_len) == FAILURE) {
1788 		return;
1789 	}
1790 
1791 	if (qname_len == 0) {
1792 		php_error_docref(NULL, E_WARNING, "Attribute name is required");
1793 		return;
1794 	}
1795 
1796 	sxe = Z_SXEOBJ_P(getThis());
1797 	GET_NODE(sxe, node);
1798 
1799 	node = php_sxe_get_first_node(sxe, node);
1800 
1801 	if (node && node->type != XML_ELEMENT_NODE) {
1802 		node = node->parent;
1803 	}
1804 
1805 	if (node == NULL) {
1806 		php_error_docref(NULL, E_WARNING, "Unable to locate parent Element");
1807 		return;
1808 	}
1809 
1810 	localname = xmlSplitQName2((xmlChar *)qname, &prefix);
1811 	if (localname == NULL) {
1812 		if (nsuri_len > 0) {
1813 			if (prefix != NULL) {
1814 				xmlFree(prefix);
1815 			}
1816 			php_error_docref(NULL, E_WARNING, "Attribute requires prefix for namespace");
1817 			return;
1818 		}
1819 		localname = xmlStrdup((xmlChar *)qname);
1820 	}
1821 
1822 	attrp = xmlHasNsProp(node, localname, (xmlChar *)nsuri);
1823 	if (attrp != NULL && attrp->type != XML_ATTRIBUTE_DECL) {
1824 		xmlFree(localname);
1825 		if (prefix != NULL) {
1826 			xmlFree(prefix);
1827 		}
1828 		php_error_docref(NULL, E_WARNING, "Attribute already exists");
1829 		return;
1830 	}
1831 
1832 	if (nsuri != NULL) {
1833 		nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri);
1834 		if (nsptr == NULL) {
1835 			nsptr = xmlNewNs(node, (xmlChar *)nsuri, prefix);
1836 		}
1837 	}
1838 
1839 	attrp = xmlNewNsProp(node, nsptr, localname, (xmlChar *)value);
1840 
1841 	xmlFree(localname);
1842 	if (prefix != NULL) {
1843 		xmlFree(prefix);
1844 	}
1845 }
1846 /* }}} */
1847 
1848 /* {{{ cast_object()
1849  */
cast_object(zval * object,int type,char * contents)1850 static int cast_object(zval *object, int type, char *contents)
1851 {
1852 	if (contents) {
1853 		ZVAL_STRINGL(object, contents, strlen(contents));
1854 	} else {
1855 		ZVAL_NULL(object);
1856 	}
1857 
1858 	switch (type) {
1859 		case IS_STRING:
1860 			convert_to_string(object);
1861 			break;
1862 		case _IS_BOOL:
1863 			convert_to_boolean(object);
1864 			break;
1865 		case IS_LONG:
1866 			convert_to_long(object);
1867 			break;
1868 		case IS_DOUBLE:
1869 			convert_to_double(object);
1870 			break;
1871 		default:
1872 			return FAILURE;
1873 	}
1874 	return SUCCESS;
1875 }
1876 /* }}} */
1877 
1878 /* {{{ sxe_object_cast()
1879  */
sxe_object_cast_ex(zval * readobj,zval * writeobj,int type)1880 static int sxe_object_cast_ex(zval *readobj, zval *writeobj, int type)
1881 {
1882 	php_sxe_object *sxe;
1883 	xmlChar        *contents = NULL;
1884 	xmlNodePtr	    node;
1885 	int rv;
1886 
1887 	sxe = Z_SXEOBJ_P(readobj);
1888 
1889 	if (type == _IS_BOOL) {
1890 		node = php_sxe_get_first_node(sxe, NULL);
1891 		if (node) {
1892 			ZVAL_TRUE(writeobj);
1893 		} else {
1894 			ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
1895 		}
1896 		return SUCCESS;
1897 	}
1898 
1899 	if (sxe->iter.type != SXE_ITER_NONE) {
1900 		node = php_sxe_get_first_node(sxe, NULL);
1901 		if (node) {
1902 			contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, node->children, 1);
1903 		}
1904 	} else {
1905 		if (!sxe->node) {
1906 			if (sxe->document) {
1907 				php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement((xmlDocPtr) sxe->document->ptr), NULL);
1908 			}
1909 		}
1910 
1911 		if (sxe->node && sxe->node->node) {
1912 			if (sxe->node->node->children) {
1913 				contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, sxe->node->node->children, 1);
1914 			}
1915 		}
1916 	}
1917 
1918 	if (readobj == writeobj) {
1919 		zval_ptr_dtor(readobj);
1920 	}
1921 
1922 	rv = cast_object(writeobj, type, (char *)contents);
1923 
1924 	if (contents) {
1925 		xmlFree(contents);
1926 	}
1927 
1928 	return rv;
1929 }
1930 /* }}} */
1931 
1932 /*  {{{ Variant of sxe_object_cast_ex that handles overwritten __toString() method */
sxe_object_cast(zval * readobj,zval * writeobj,int type)1933 static int sxe_object_cast(zval *readobj, zval *writeobj, int type)
1934 {
1935 	if (type == IS_STRING
1936 		&& zend_std_cast_object_tostring(readobj, writeobj, IS_STRING) == SUCCESS
1937 	) {
1938 		return SUCCESS;
1939 	}
1940 
1941 	return sxe_object_cast_ex(readobj, writeobj, type);
1942 }
1943 /* }}} */
1944 
1945 /* {{{ proto object SimpleXMLElement::__toString()
1946    Returns the string content */
SXE_METHOD(__toString)1947 SXE_METHOD(__toString)
1948 {
1949 	if (sxe_object_cast_ex(getThis(), return_value, IS_STRING) != SUCCESS) {
1950 		zval_ptr_dtor(return_value);
1951 		RETURN_EMPTY_STRING();
1952 	}
1953 }
1954 /* }}} */
1955 
php_sxe_count_elements_helper(php_sxe_object * sxe,zend_long * count)1956 static int php_sxe_count_elements_helper(php_sxe_object *sxe, zend_long *count) /* {{{ */
1957 {
1958 	xmlNodePtr      node;
1959 	zval            data;
1960 
1961 	*count = 0;
1962 
1963 	ZVAL_COPY_VALUE(&data, &sxe->iter.data);
1964 	ZVAL_UNDEF(&sxe->iter.data);
1965 
1966 	node = php_sxe_reset_iterator(sxe, 0);
1967 
1968 	while (node)
1969 	{
1970 		(*count)++;
1971 		node = php_sxe_iterator_fetch(sxe, node->next, 0);
1972 	}
1973 
1974 	if (!Z_ISUNDEF(sxe->iter.data)) {
1975 		zval_ptr_dtor(&sxe->iter.data);
1976 	}
1977 	ZVAL_COPY_VALUE(&sxe->iter.data, &data);
1978 
1979 	return SUCCESS;
1980 }
1981 /* }}} */
1982 
sxe_count_elements(zval * object,zend_long * count)1983 static int sxe_count_elements(zval *object, zend_long *count) /* {{{ */
1984 {
1985 	php_sxe_object  *intern;
1986 	intern = Z_SXEOBJ_P(object);
1987 	if (intern->fptr_count) {
1988 		zval rv;
1989 		zend_call_method_with_0_params(object, intern->zo.ce, &intern->fptr_count, "count", &rv);
1990 		if (!Z_ISUNDEF(rv)) {
1991 			if (!Z_ISUNDEF(intern->tmp)) {
1992 				zval_ptr_dtor(&intern->tmp);
1993 			}
1994 			ZVAL_LONG(&intern->tmp, zval_get_long(&rv));
1995 			zval_ptr_dtor(&rv);
1996 			*count = Z_LVAL(intern->tmp);
1997 			return SUCCESS;
1998 		}
1999 		return FAILURE;
2000 	}
2001 	return php_sxe_count_elements_helper(intern, count);
2002 }
2003 /* }}} */
2004 
2005 /* {{{ proto int SimpleXMLElement::count()
2006  Get number of child elements */
SXE_METHOD(count)2007 SXE_METHOD(count)
2008 {
2009 	zend_long count = 0;
2010 	php_sxe_object *sxe = Z_SXEOBJ_P(getThis());
2011 
2012 	if (zend_parse_parameters_none() == FAILURE) {
2013 		return;
2014 	}
2015 
2016 	php_sxe_count_elements_helper(sxe, &count);
2017 
2018 	RETURN_LONG(count);
2019 }
2020 /* }}} */
2021 
sxe_get_value(zval * z,zval * rv)2022 static zval *sxe_get_value(zval *z, zval *rv) /* {{{ */
2023 {
2024 	if (sxe_object_cast_ex(z, rv, IS_STRING) == FAILURE) {
2025 		zend_error(E_ERROR, "Unable to cast node to string");
2026 		/* FIXME: Should not be fatal */
2027 	}
2028 
2029 	return rv;
2030 }
2031 /* }}} */
2032 
2033 static zend_object_handlers sxe_object_handlers = { /* {{{ */
2034 	ZEND_OBJECTS_STORE_HANDLERS,
2035 	sxe_property_read,
2036 	sxe_property_write,
2037 	sxe_dimension_read,
2038 	sxe_dimension_write,
2039 	sxe_property_get_adr,
2040 	sxe_get_value,			/* get */
2041 	NULL,
2042 	sxe_property_exists,
2043 	sxe_property_delete,
2044 	sxe_dimension_exists,
2045 	sxe_dimension_delete,
2046 	sxe_get_properties,
2047 	NULL, /* zend_get_std_object_handlers()->get_method,*/
2048 	NULL, /* zend_get_std_object_handlers()->call_method,*/
2049 	NULL, /* zend_get_std_object_handlers()->get_constructor, */
2050 	NULL, /* zend_get_std_object_handlers()->get_class_name,*/
2051 	sxe_objects_compare,
2052 	sxe_object_cast,
2053 	sxe_count_elements,
2054 	sxe_get_debug_info,
2055 	NULL,
2056 	sxe_get_gc
2057 };
2058 /* }}} */
2059 
2060 /* {{{ sxe_object_clone()
2061  */
2062 static zend_object *
sxe_object_clone(zval * object)2063 sxe_object_clone(zval *object)
2064 {
2065 	php_sxe_object *sxe = Z_SXEOBJ_P(object);
2066 	php_sxe_object *clone;
2067 	xmlNodePtr nodep = NULL;
2068 	xmlDocPtr docp = NULL;
2069 
2070 	clone = php_sxe_object_new(sxe->zo.ce, sxe->fptr_count);
2071 	clone->document = sxe->document;
2072 	if (clone->document) {
2073 		clone->document->refcount++;
2074 		docp = clone->document->ptr;
2075 	}
2076 
2077 	clone->iter.isprefix = sxe->iter.isprefix;
2078 	if (sxe->iter.name != NULL) {
2079 		clone->iter.name = (xmlChar*)estrdup((char*)sxe->iter.name);
2080 	}
2081 	if (sxe->iter.nsprefix != NULL) {
2082 		clone->iter.nsprefix = (xmlChar*)estrdup((char*)sxe->iter.nsprefix);
2083 	}
2084 	clone->iter.type = sxe->iter.type;
2085 
2086 	if (sxe->node) {
2087 		nodep = xmlDocCopyNode(sxe->node->node, docp, 1);
2088 	}
2089 
2090 	php_libxml_increment_node_ptr((php_libxml_node_object *)clone, nodep, NULL);
2091 
2092 	return &clone->zo;
2093 }
2094 /* }}} */
2095 
2096 /* {{{ sxe_object_dtor()
2097  */
sxe_object_dtor(zend_object * object)2098 static void sxe_object_dtor(zend_object *object)
2099 {
2100 	/* dtor required to cleanup iterator related data properly */
2101 	php_sxe_object *sxe;
2102 
2103 	sxe = php_sxe_fetch_object(object);
2104 
2105 	if (!Z_ISUNDEF(sxe->iter.data)) {
2106 		zval_ptr_dtor(&sxe->iter.data);
2107 		ZVAL_UNDEF(&sxe->iter.data);
2108 	}
2109 
2110 	if (sxe->iter.name) {
2111 		efree(sxe->iter.name);
2112 		sxe->iter.name = NULL;
2113 	}
2114 	if (sxe->iter.nsprefix) {
2115 		efree(sxe->iter.nsprefix);
2116 		sxe->iter.nsprefix = NULL;
2117 	}
2118 	if (!Z_ISUNDEF(sxe->tmp)) {
2119 		zval_ptr_dtor(&sxe->tmp);
2120 		ZVAL_UNDEF(&sxe->tmp);
2121 	}
2122 }
2123 /* }}} */
2124 
2125 /* {{{ sxe_object_free_storage()
2126  */
sxe_object_free_storage(zend_object * object)2127 static void sxe_object_free_storage(zend_object *object)
2128 {
2129 	php_sxe_object *sxe;
2130 
2131 	sxe = php_sxe_fetch_object(object);
2132 
2133 	zend_object_std_dtor(&sxe->zo);
2134 
2135 	php_libxml_node_decrement_resource((php_libxml_node_object *)sxe);
2136 
2137 	if (sxe->xpath) {
2138 		xmlXPathFreeContext(sxe->xpath);
2139 	}
2140 
2141 	if (sxe->properties) {
2142 		zend_hash_destroy(sxe->properties);
2143 		FREE_HASHTABLE(sxe->properties);
2144 	}
2145 }
2146 /* }}} */
2147 
2148 /* {{{ php_sxe_find_fptr_count()
2149  */
php_sxe_find_fptr_count(zend_class_entry * ce)2150 static zend_function* php_sxe_find_fptr_count(zend_class_entry *ce)
2151 {
2152 	zend_function *fptr_count = NULL;
2153 	zend_class_entry *parent = ce;
2154 	int inherited = 0;
2155 
2156 	while (parent) {
2157 		if (parent == sxe_class_entry) {
2158 			break;
2159 		}
2160 		parent = parent->parent;
2161 		inherited = 1;
2162 	}
2163 
2164 	if (inherited) {
2165 		fptr_count = zend_hash_str_find_ptr(&ce->function_table, "count", sizeof("count") - 1);
2166 		if (fptr_count->common.scope == parent) {
2167 			fptr_count = NULL;
2168 		}
2169 	}
2170 
2171 	return fptr_count;
2172 }
2173 /* }}} */
2174 
2175 /* {{{ php_sxe_object_new()
2176  */
php_sxe_object_new(zend_class_entry * ce,zend_function * fptr_count)2177 static php_sxe_object* php_sxe_object_new(zend_class_entry *ce, zend_function *fptr_count)
2178 {
2179 	php_sxe_object *intern;
2180 
2181 	intern = ecalloc(1, sizeof(php_sxe_object) + zend_object_properties_size(ce));
2182 
2183 	intern->iter.type = SXE_ITER_NONE;
2184 	intern->iter.nsprefix = NULL;
2185 	intern->iter.name = NULL;
2186 	intern->fptr_count = fptr_count;
2187 
2188 	zend_object_std_init(&intern->zo, ce);
2189 	object_properties_init(&intern->zo, ce);
2190 	intern->zo.handlers = &sxe_object_handlers;
2191 
2192 	return intern;
2193 }
2194 /* }}} */
2195 
2196 /* {{{ sxe_object_new()
2197  */
2198 PHP_SXE_API zend_object *
sxe_object_new(zend_class_entry * ce)2199 sxe_object_new(zend_class_entry *ce)
2200 {
2201 	php_sxe_object    *intern;
2202 
2203 	intern = php_sxe_object_new(ce, php_sxe_find_fptr_count(ce));
2204 	return &intern->zo;
2205 }
2206 /* }}} */
2207 
2208 /* {{{ proto simplemxml_element simplexml_load_file(string filename [, string class_name [, int options [, string ns [, bool is_prefix]]]])
2209    Load a filename and return a simplexml_element object to allow for processing */
PHP_FUNCTION(simplexml_load_file)2210 PHP_FUNCTION(simplexml_load_file)
2211 {
2212 	php_sxe_object *sxe;
2213 	char           *filename;
2214 	size_t             filename_len;
2215 	xmlDocPtr       docp;
2216 	char           *ns = NULL;
2217 	size_t             ns_len = 0;
2218 	zend_long            options = 0;
2219 	zend_class_entry *ce= sxe_class_entry;
2220 	zend_function    *fptr_count;
2221 	zend_bool       isprefix = 0;
2222 
2223 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|C!lsb", &filename, &filename_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) {
2224 		return;
2225 	}
2226 
2227 	if (ZEND_LONG_EXCEEDS_INT(options)) {
2228 		php_error_docref(NULL, E_WARNING, "Invalid options");
2229 		RETURN_FALSE;
2230 	}
2231 
2232 	docp = xmlReadFile(filename, NULL, (int)options);
2233 
2234 	if (!docp) {
2235 		RETURN_FALSE;
2236 	}
2237 
2238 	if (!ce) {
2239 		ce = sxe_class_entry;
2240 		fptr_count = NULL;
2241 	} else {
2242 		fptr_count = php_sxe_find_fptr_count(ce);
2243 	}
2244 	sxe = php_sxe_object_new(ce, fptr_count);
2245 	sxe->iter.nsprefix = ns_len ? (xmlChar*)estrdup(ns) : NULL;
2246 	sxe->iter.isprefix = isprefix;
2247 	php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp);
2248 	php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL);
2249 
2250 	ZVAL_OBJ(return_value, &sxe->zo);
2251 }
2252 /* }}} */
2253 
2254 /* {{{ proto simplemxml_element simplexml_load_string(string data [, string class_name [, int options [, string ns [, bool is_prefix]]]])
2255    Load a string and return a simplexml_element object to allow for processing */
PHP_FUNCTION(simplexml_load_string)2256 PHP_FUNCTION(simplexml_load_string)
2257 {
2258 	php_sxe_object *sxe;
2259 	char           *data;
2260 	size_t             data_len;
2261 	xmlDocPtr       docp;
2262 	char           *ns = NULL;
2263 	size_t             ns_len = 0;
2264 	zend_long            options = 0;
2265 	zend_class_entry *ce= sxe_class_entry;
2266 	zend_function    *fptr_count;
2267 	zend_bool       isprefix = 0;
2268 
2269 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!lsb", &data, &data_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) {
2270 		return;
2271 	}
2272 
2273 	if (ZEND_SIZE_T_INT_OVFL(data_len)) {
2274 		php_error_docref(NULL, E_WARNING, "Data is too long");
2275 		RETURN_FALSE;
2276 	}
2277 	if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
2278 		php_error_docref(NULL, E_WARNING, "Namespace is too long");
2279 		RETURN_FALSE;
2280 	}
2281 	if (ZEND_LONG_EXCEEDS_INT(options)) {
2282 		php_error_docref(NULL, E_WARNING, "Invalid options");
2283 		RETURN_FALSE;
2284 	}
2285 
2286 	docp = xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);
2287 
2288 	if (!docp) {
2289 		RETURN_FALSE;
2290 	}
2291 
2292 	if (!ce) {
2293 		ce = sxe_class_entry;
2294 		fptr_count = NULL;
2295 	} else {
2296 		fptr_count = php_sxe_find_fptr_count(ce);
2297 	}
2298 	sxe = php_sxe_object_new(ce, fptr_count);
2299 	sxe->iter.nsprefix = ns_len ? (xmlChar*)estrdup(ns) : NULL;
2300 	sxe->iter.isprefix = isprefix;
2301 	php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp);
2302 	php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL);
2303 
2304 	ZVAL_OBJ(return_value, &sxe->zo);
2305 }
2306 /* }}} */
2307 
2308 /* {{{ proto SimpleXMLElement::__construct(string data [, int options [, bool data_is_url [, string ns [, bool is_prefix]]]])
2309    SimpleXMLElement constructor */
SXE_METHOD(__construct)2310 SXE_METHOD(__construct)
2311 {
2312 	php_sxe_object *sxe = Z_SXEOBJ_P(getThis());
2313 	char           *data, *ns = NULL;
2314 	size_t             data_len, ns_len = 0;
2315 	xmlDocPtr       docp;
2316 	zend_long            options = 0;
2317 	zend_bool       is_url = 0, isprefix = 0;
2318 
2319 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|lbsb", &data, &data_len, &options, &is_url, &ns, &ns_len, &isprefix) == FAILURE) {
2320 		return;
2321 	}
2322 
2323 	if (ZEND_SIZE_T_INT_OVFL(data_len)) {
2324 		zend_throw_exception(zend_ce_exception, "Data is too long", 0);
2325 		return;
2326 	}
2327 	if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
2328 		zend_throw_exception(zend_ce_exception, "Namespace is too long", 0);
2329 		return;
2330 	}
2331 	if (ZEND_LONG_EXCEEDS_INT(options)) {
2332 		zend_throw_exception(zend_ce_exception, "Invalid options", 0);
2333 		return;
2334 	}
2335 
2336 	docp = is_url ? xmlReadFile(data, NULL, (int)options) : xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);
2337 
2338 	if (!docp) {
2339 		((php_libxml_node_object *)sxe)->document = NULL;
2340 		zend_throw_exception(zend_ce_exception, "String could not be parsed as XML", 0);
2341 		return;
2342 	}
2343 
2344 	sxe->iter.nsprefix = ns_len ? (xmlChar*)estrdup(ns) : NULL;
2345 	sxe->iter.isprefix = isprefix;
2346 	php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp);
2347 	php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, xmlDocGetRootElement(docp), NULL);
2348 }
2349 /* }}} */
2350 
2351 zend_object_iterator_funcs php_sxe_iterator_funcs = { /* {{{ */
2352 	php_sxe_iterator_dtor,
2353 	php_sxe_iterator_valid,
2354 	php_sxe_iterator_current_data,
2355 	php_sxe_iterator_current_key,
2356 	php_sxe_iterator_move_forward,
2357 	php_sxe_iterator_rewind,
2358 };
2359 /* }}} */
2360 
php_sxe_iterator_fetch(php_sxe_object * sxe,xmlNodePtr node,int use_data)2361 static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data) /* {{{ */
2362 {
2363 	xmlChar *prefix  = sxe->iter.nsprefix;
2364 	int isprefix  = sxe->iter.isprefix;
2365 
2366 	if (sxe->iter.type == SXE_ITER_ATTRLIST) {
2367 		if (sxe->iter.name) {
2368 			while (node) {
2369 				if (node->type == XML_ATTRIBUTE_NODE) {
2370 					if (!xmlStrcmp(node->name, sxe->iter.name) && match_ns(sxe, node, prefix, isprefix)) {
2371 						break;
2372 					}
2373 				}
2374 				node = node->next;
2375 			}
2376 		} else {
2377 			while (node) {
2378 				if (node->type == XML_ATTRIBUTE_NODE) {
2379 					if (match_ns(sxe, node, prefix, isprefix)) {
2380 						break;
2381 					}
2382 				}
2383 				node = node->next;
2384 			}
2385 		}
2386 	} else if (sxe->iter.type == SXE_ITER_ELEMENT && sxe->iter.name) {
2387 		while (node) {
2388 			if (node->type == XML_ELEMENT_NODE) {
2389 				if (!xmlStrcmp(node->name, sxe->iter.name) && match_ns(sxe, node, prefix, isprefix)) {
2390 					break;
2391 				}
2392 			}
2393 			node = node->next;
2394 		}
2395 	} else {
2396 		while (node) {
2397 			if (node->type == XML_ELEMENT_NODE) {
2398 				if (match_ns(sxe, node, prefix, isprefix)) {
2399 					break;
2400 				}
2401 			}
2402 			node = node->next;
2403 		}
2404 	}
2405 
2406 	if (node && use_data) {
2407 		_node_as_zval(sxe, node, &sxe->iter.data, SXE_ITER_NONE, NULL, prefix, isprefix);
2408 	}
2409 
2410 	return node;
2411 }
2412 /* }}} */
2413 
php_sxe_reset_iterator(php_sxe_object * sxe,int use_data)2414 static xmlNodePtr php_sxe_reset_iterator(php_sxe_object *sxe, int use_data) /* {{{ */
2415 {
2416 	xmlNodePtr node;
2417 
2418 	if (!Z_ISUNDEF(sxe->iter.data)) {
2419 		zval_ptr_dtor(&sxe->iter.data);
2420 		ZVAL_UNDEF(&sxe->iter.data);
2421 	}
2422 
2423 	GET_NODE(sxe, node)
2424 
2425 	if (node) {
2426 		switch (sxe->iter.type) {
2427 			case SXE_ITER_ELEMENT:
2428 			case SXE_ITER_CHILD:
2429 			case SXE_ITER_NONE:
2430 				node = node->children;
2431 				break;
2432 			case SXE_ITER_ATTRLIST:
2433 				node = (xmlNodePtr) node->properties;
2434 		}
2435 		return php_sxe_iterator_fetch(sxe, node, use_data);
2436 	}
2437 	return NULL;
2438 }
2439 /* }}} */
2440 
php_sxe_get_iterator(zend_class_entry * ce,zval * object,int by_ref)2441 zend_object_iterator *php_sxe_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
2442 {
2443 	php_sxe_iterator *iterator;
2444 
2445 	if (by_ref) {
2446 		zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
2447 	}
2448 	iterator = emalloc(sizeof(php_sxe_iterator));
2449 	zend_iterator_init(&iterator->intern);
2450 
2451 	ZVAL_COPY(&iterator->intern.data, object);
2452 	iterator->intern.funcs = &php_sxe_iterator_funcs;
2453 	iterator->sxe = Z_SXEOBJ_P(object);
2454 
2455 	return (zend_object_iterator*)iterator;
2456 }
2457 /* }}} */
2458 
php_sxe_iterator_dtor(zend_object_iterator * iter)2459 static void php_sxe_iterator_dtor(zend_object_iterator *iter) /* {{{ */
2460 {
2461 	php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
2462 
2463 	/* cleanup handled in sxe_object_dtor as we dont always have an iterator wrapper */
2464 	if (!Z_ISUNDEF(iterator->intern.data)) {
2465 		zval_ptr_dtor(&iterator->intern.data);
2466 	}
2467 }
2468 /* }}} */
2469 
php_sxe_iterator_valid(zend_object_iterator * iter)2470 static int php_sxe_iterator_valid(zend_object_iterator *iter) /* {{{ */
2471 {
2472 	php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
2473 
2474 	return Z_ISUNDEF(iterator->sxe->iter.data) ? FAILURE : SUCCESS;
2475 }
2476 /* }}} */
2477 
php_sxe_iterator_current_data(zend_object_iterator * iter)2478 static zval *php_sxe_iterator_current_data(zend_object_iterator *iter) /* {{{ */
2479 {
2480 	php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
2481 
2482 	return &iterator->sxe->iter.data;
2483 }
2484 /* }}} */
2485 
php_sxe_iterator_current_key(zend_object_iterator * iter,zval * key)2486 static void php_sxe_iterator_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
2487 {
2488 	php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
2489 	zval *curobj = &iterator->sxe->iter.data;
2490 	php_sxe_object *intern = Z_SXEOBJ_P(curobj);
2491 
2492 	xmlNodePtr curnode = NULL;
2493 	if (intern != NULL && intern->node != NULL) {
2494 		curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
2495 	}
2496 
2497 	if (curnode) {
2498 		ZVAL_STRINGL(key, (char *) curnode->name, xmlStrlen(curnode->name));
2499 	} else {
2500 		ZVAL_NULL(key);
2501 	}
2502 }
2503 /* }}} */
2504 
php_sxe_move_forward_iterator(php_sxe_object * sxe)2505 PHP_SXE_API void php_sxe_move_forward_iterator(php_sxe_object *sxe) /* {{{ */
2506 {
2507 	xmlNodePtr      node = NULL;
2508 	php_sxe_object  *intern;
2509 
2510 	if (!Z_ISUNDEF(sxe->iter.data)) {
2511 		intern = Z_SXEOBJ_P(&sxe->iter.data);
2512 		GET_NODE(intern, node)
2513 		zval_ptr_dtor(&sxe->iter.data);
2514 		ZVAL_UNDEF(&sxe->iter.data);
2515 	}
2516 
2517 	if (node) {
2518 		php_sxe_iterator_fetch(sxe, node->next, 1);
2519 	}
2520 }
2521 /* }}} */
2522 
php_sxe_iterator_move_forward(zend_object_iterator * iter)2523 static void php_sxe_iterator_move_forward(zend_object_iterator *iter) /* {{{ */
2524 {
2525 	php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
2526 	php_sxe_move_forward_iterator(iterator->sxe);
2527 }
2528 /* }}} */
2529 
php_sxe_iterator_rewind(zend_object_iterator * iter)2530 static void php_sxe_iterator_rewind(zend_object_iterator *iter) /* {{{ */
2531 {
2532 	php_sxe_object	*sxe;
2533 
2534 	php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
2535 	sxe = iterator->sxe;
2536 
2537 	php_sxe_reset_iterator(sxe, 1);
2538 }
2539 /* }}} */
2540 
simplexml_export_node(zval * object)2541 void *simplexml_export_node(zval *object) /* {{{ */
2542 {
2543 	php_sxe_object *sxe;
2544 	xmlNodePtr node;
2545 
2546 	sxe = Z_SXEOBJ_P(object);
2547 	GET_NODE(sxe, node);
2548 	return php_sxe_get_first_node(sxe, node);
2549 }
2550 /* }}} */
2551 
2552 /* {{{ proto simplemxml_element simplexml_import_dom(domNode node [, string class_name])
2553    Get a simplexml_element object from dom to allow for processing */
PHP_FUNCTION(simplexml_import_dom)2554 PHP_FUNCTION(simplexml_import_dom)
2555 {
2556 	php_sxe_object *sxe;
2557 	zval *node;
2558 	php_libxml_node_object *object;
2559 	xmlNodePtr		nodep = NULL;
2560 	zend_class_entry *ce = sxe_class_entry;
2561 	zend_function    *fptr_count;
2562 
2563 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|C!", &node, &ce) == FAILURE) {
2564 		return;
2565 	}
2566 
2567 	object = Z_LIBXML_NODE_P(node);
2568 
2569 	nodep = php_libxml_import_node(node);
2570 
2571 	if (nodep) {
2572 		if (nodep->doc == NULL) {
2573 			php_error_docref(NULL, E_WARNING, "Imported Node must have associated Document");
2574 			RETURN_NULL();
2575 		}
2576 		if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
2577 			nodep = xmlDocGetRootElement((xmlDocPtr) nodep);
2578 		}
2579 	}
2580 
2581 	if (nodep && nodep->type == XML_ELEMENT_NODE) {
2582 		if (!ce) {
2583 			ce = sxe_class_entry;
2584 			fptr_count = NULL;
2585 		} else {
2586 			fptr_count = php_sxe_find_fptr_count(ce);
2587 		}
2588 		sxe = php_sxe_object_new(ce, fptr_count);
2589 		sxe->document = object->document;
2590 		php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, nodep->doc);
2591 		php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, nodep, NULL);
2592 
2593 		ZVAL_OBJ(return_value, &sxe->zo);
2594 	} else {
2595 		php_error_docref(NULL, E_WARNING, "Invalid Nodetype to import");
2596 		RETVAL_NULL();
2597 	}
2598 }
2599 /* }}} */
2600 
2601 /* {{{ arginfo */
2602 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexml_load_file, 0, 0, 1)
2603 	ZEND_ARG_INFO(0, filename)
2604 	ZEND_ARG_INFO(0, class_name)
2605 	ZEND_ARG_INFO(0, options)
2606 	ZEND_ARG_INFO(0, ns)
2607 	ZEND_ARG_INFO(0, is_prefix)
2608 ZEND_END_ARG_INFO()
2609 
2610 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexml_load_string, 0, 0, 1)
2611 	ZEND_ARG_INFO(0, data)
2612 	ZEND_ARG_INFO(0, class_name)
2613 	ZEND_ARG_INFO(0, options)
2614 	ZEND_ARG_INFO(0, ns)
2615 	ZEND_ARG_INFO(0, is_prefix)
2616 ZEND_END_ARG_INFO()
2617 
2618 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexml_import_dom, 0, 0, 1)
2619 	ZEND_ARG_INFO(0, node)
2620 	ZEND_ARG_INFO(0, class_name)
2621 ZEND_END_ARG_INFO()
2622 
2623 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_xpath, 0, 0, 1)
2624 	ZEND_ARG_INFO(0, path)
2625 ZEND_END_ARG_INFO()
2626 
2627 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_registerxpathnamespace, 0, 0, 2)
2628 	ZEND_ARG_INFO(0, prefix)
2629 	ZEND_ARG_INFO(0, ns)
2630 ZEND_END_ARG_INFO()
2631 
2632 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_asxml, 0, 0, 0)
2633 	ZEND_ARG_INFO(0, filename)
2634 ZEND_END_ARG_INFO()
2635 
2636 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_getnamespaces, 0, 0, 0)
2637 	ZEND_ARG_INFO(0, recursve)
2638 ZEND_END_ARG_INFO()
2639 
2640 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_getdocnamespaces, 0, 0, 0)
2641 	ZEND_ARG_INFO(0, recursve)
2642 	ZEND_ARG_INFO(0, from_root)
2643 ZEND_END_ARG_INFO()
2644 
2645 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_children, 0, 0, 0)
2646 	ZEND_ARG_INFO(0, ns)
2647 	ZEND_ARG_INFO(0, is_prefix)
2648 ZEND_END_ARG_INFO()
2649 
2650 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement__construct, 0, 0, 1)
2651 	ZEND_ARG_INFO(0, data)
2652 	ZEND_ARG_INFO(0, options)
2653 	ZEND_ARG_INFO(0, data_is_url)
2654 	ZEND_ARG_INFO(0, ns)
2655 	ZEND_ARG_INFO(0, is_prefix)
2656 ZEND_END_ARG_INFO()
2657 
2658 ZEND_BEGIN_ARG_INFO(arginfo_simplexmlelement__void, 0)
2659 ZEND_END_ARG_INFO()
2660 
2661 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_addchild, 0, 0, 1)
2662 	ZEND_ARG_INFO(0, name)
2663 	ZEND_ARG_INFO(0, value)
2664 	ZEND_ARG_INFO(0, ns)
2665 ZEND_END_ARG_INFO()
2666 /* }}} */
2667 
2668 const zend_function_entry simplexml_functions[] = { /* {{{ */
2669 	PHP_FE(simplexml_load_file, 	arginfo_simplexml_load_file)
2670 	PHP_FE(simplexml_load_string,	arginfo_simplexml_load_string)
2671 	PHP_FE(simplexml_import_dom,	arginfo_simplexml_import_dom)
2672 	PHP_FE_END
2673 };
2674 /* }}} */
2675 
2676 static const zend_module_dep simplexml_deps[] = { /* {{{ */
2677 	ZEND_MOD_REQUIRED("libxml")
2678 	ZEND_MOD_REQUIRED("spl")
2679 	ZEND_MOD_END
2680 };
2681 /* }}} */
2682 
2683 zend_module_entry simplexml_module_entry = { /* {{{ */
2684 	STANDARD_MODULE_HEADER_EX, NULL,
2685 	simplexml_deps,
2686 	"SimpleXML",
2687 	simplexml_functions,
2688 	PHP_MINIT(simplexml),
2689 	PHP_MSHUTDOWN(simplexml),
2690 	NULL,
2691 	NULL,
2692 	PHP_MINFO(simplexml),
2693 	PHP_SIMPLEXML_VERSION,
2694 	STANDARD_MODULE_PROPERTIES
2695 };
2696 /* }}} */
2697 
2698 #ifdef COMPILE_DL_SIMPLEXML
2699 ZEND_GET_MODULE(simplexml)
2700 #endif
2701 
2702 /* the method table */
2703 /* each method can have its own parameters and visibility */
2704 static const zend_function_entry sxe_functions[] = { /* {{{ */
2705 	SXE_ME(__construct,            arginfo_simplexmlelement__construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) /* must be called */
2706 	SXE_ME(asXML,                  arginfo_simplexmlelement_asxml, ZEND_ACC_PUBLIC)
2707 	SXE_MALIAS(saveXML, asXML,	   arginfo_simplexmlelement_asxml, ZEND_ACC_PUBLIC)
2708 	SXE_ME(xpath,                  arginfo_simplexmlelement_xpath, ZEND_ACC_PUBLIC)
2709 	SXE_ME(registerXPathNamespace, arginfo_simplexmlelement_registerxpathnamespace, ZEND_ACC_PUBLIC)
2710 	SXE_ME(attributes,             arginfo_simplexmlelement_children, ZEND_ACC_PUBLIC)
2711 	SXE_ME(children,               arginfo_simplexmlelement_children, ZEND_ACC_PUBLIC)
2712 	SXE_ME(getNamespaces,          arginfo_simplexmlelement_getnamespaces, ZEND_ACC_PUBLIC)
2713 	SXE_ME(getDocNamespaces,       arginfo_simplexmlelement_getdocnamespaces, ZEND_ACC_PUBLIC)
2714 	SXE_ME(getName,                arginfo_simplexmlelement__void, ZEND_ACC_PUBLIC)
2715 	SXE_ME(addChild,               arginfo_simplexmlelement_addchild, ZEND_ACC_PUBLIC)
2716 	SXE_ME(addAttribute,           arginfo_simplexmlelement_addchild, ZEND_ACC_PUBLIC)
2717 	SXE_ME(__toString,             arginfo_simplexmlelement__void, ZEND_ACC_PUBLIC)
2718 	SXE_ME(count,                  arginfo_simplexmlelement__void, ZEND_ACC_PUBLIC)
2719 	PHP_FE_END
2720 };
2721 /* }}} */
2722 
2723 /* {{{ PHP_MINIT_FUNCTION(simplexml)
2724  */
PHP_MINIT_FUNCTION(simplexml)2725 PHP_MINIT_FUNCTION(simplexml)
2726 {
2727 	zend_class_entry sxe;
2728 
2729 	INIT_CLASS_ENTRY(sxe, "SimpleXMLElement", sxe_functions);
2730 	sxe.create_object = sxe_object_new;
2731 	sxe_class_entry = zend_register_internal_class(&sxe);
2732 	sxe_class_entry->get_iterator = php_sxe_get_iterator;
2733 	sxe_class_entry->iterator_funcs.funcs = &php_sxe_iterator_funcs;
2734 	zend_class_implements(sxe_class_entry, 1, zend_ce_traversable);
2735 	sxe_object_handlers.offset = XtOffsetOf(php_sxe_object, zo);
2736 	sxe_object_handlers.dtor_obj = sxe_object_dtor;
2737 	sxe_object_handlers.free_obj = sxe_object_free_storage;
2738 	sxe_object_handlers.clone_obj = sxe_object_clone;
2739 	sxe_object_handlers.get_method = zend_get_std_object_handlers()->get_method;
2740 	sxe_object_handlers.get_constructor = zend_get_std_object_handlers()->get_constructor;
2741 	sxe_object_handlers.get_class_name = zend_get_std_object_handlers()->get_class_name;
2742 	sxe_class_entry->serialize = zend_class_serialize_deny;
2743 	sxe_class_entry->unserialize = zend_class_unserialize_deny;
2744 
2745 	php_libxml_register_export(sxe_class_entry, simplexml_export_node);
2746 
2747 	PHP_MINIT(sxe)(INIT_FUNC_ARGS_PASSTHRU);
2748 
2749 	return SUCCESS;
2750 }
2751 /* }}} */
2752 
2753 /* {{{ PHP_MSHUTDOWN_FUNCTION(simplexml)
2754  */
PHP_MSHUTDOWN_FUNCTION(simplexml)2755 PHP_MSHUTDOWN_FUNCTION(simplexml)
2756 {
2757 	sxe_class_entry = NULL;
2758 	return SUCCESS;
2759 }
2760 /* }}} */
2761 
2762 /* {{{ PHP_MINFO_FUNCTION(simplexml)
2763  */
PHP_MINFO_FUNCTION(simplexml)2764 PHP_MINFO_FUNCTION(simplexml)
2765 {
2766 	php_info_print_table_start();
2767 	php_info_print_table_header(2, "Simplexml support", "enabled");
2768 	php_info_print_table_row(2, "Revision", "$Id: 0637e06af859ca1d0dea9c2f1530e51b98f1970e $");
2769 	php_info_print_table_row(2, "Schema support",
2770 #ifdef LIBXML_SCHEMAS_ENABLED
2771 		"enabled");
2772 #else
2773 		"not available");
2774 #endif
2775 	php_info_print_table_end();
2776 }
2777 /* }}} */
2778 
2779 #endif
2780 
2781 /**
2782  * Local Variables:
2783  * c-basic-offset: 4
2784  * tab-width: 4
2785  * indent-tabs-mode: t
2786  * End:
2787  * vim600: fdm=marker
2788  * vim: noet sw=4 ts=4
2789  */
2790