1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Christian Stocker <chregu@php.net>                          |
16    |          Rob Richards <rrichards@php.net>                            |
17    +----------------------------------------------------------------------+
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 #if HAVE_LIBXML && HAVE_DOM
26 #include "php_dom.h"
27 
28 /* {{{ arginfo */
29 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_construct, 0, 0, 0)
30 ZEND_END_ARG_INFO();
31 
32 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_appendXML, 0, 0, 1)
33 	ZEND_ARG_INFO(0, data)
34 ZEND_END_ARG_INFO();
35 /* }}} */
36 
37 /*
38 * class DOMDocumentFragment extends DOMNode
39 *
40 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-B63ED1A3
41 * Since:
42 */
43 
44 const zend_function_entry php_dom_documentfragment_class_functions[] = {
45 	PHP_ME(domdocumentfragment, __construct, arginfo_dom_documentfragement_construct, ZEND_ACC_PUBLIC)
46 	PHP_ME(domdocumentfragment, appendXML, arginfo_dom_documentfragement_appendXML, ZEND_ACC_PUBLIC)
47 	PHP_FE_END
48 };
49 
50 /* {{{ proto DOMDocumentFragment::__construct() */
PHP_METHOD(domdocumentfragment,__construct)51 PHP_METHOD(domdocumentfragment, __construct)
52 {
53 	xmlNodePtr nodep = NULL, oldnode = NULL;
54 	dom_object *intern;
55 
56 	if (zend_parse_parameters_none_throw() == FAILURE) {
57 		return;
58 	}
59 
60 	nodep = xmlNewDocFragment(NULL);
61 
62 	if (!nodep) {
63 		php_dom_throw_error(INVALID_STATE_ERR, 1);
64 		RETURN_FALSE;
65 	}
66 
67 	intern = Z_DOMOBJ_P(ZEND_THIS);
68 	oldnode = dom_object_get_node(intern);
69 	if (oldnode != NULL) {
70 		php_libxml_node_free_resource(oldnode );
71 	}
72 	/* php_dom_set_object(intern, nodep); */
73 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
74 }
75 /* }}} end DOMDocumentFragment::__construct */
76 
77 /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
78  needed for hack in appendXML due to libxml bug - no need to share this function */
php_dom_xmlSetTreeDoc(xmlNodePtr tree,xmlDocPtr doc)79 static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
80 {
81     xmlAttrPtr prop;
82 	xmlNodePtr cur;
83 
84     if (tree) {
85 		if(tree->type == XML_ELEMENT_NODE) {
86 			prop = tree->properties;
87 			while (prop != NULL) {
88 				prop->doc = doc;
89 				if (prop->children) {
90 					cur = prop->children;
91 					while (cur != NULL) {
92 						php_dom_xmlSetTreeDoc(cur, doc);
93 						cur = cur->next;
94 					}
95 				}
96 				prop = prop->next;
97 			}
98 		}
99 		if (tree->children != NULL) {
100 			cur = tree->children;
101 			while (cur != NULL) {
102 				php_dom_xmlSetTreeDoc(cur, doc);
103 				cur = cur->next;
104 			}
105 		}
106 		tree->doc = doc;
107     }
108 }
109 /* }}} */
110 
111 /* {{{ proto void DOMDocumentFragment::appendXML(string data) */
PHP_METHOD(domdocumentfragment,appendXML)112 PHP_METHOD(domdocumentfragment, appendXML) {
113 	zval *id;
114 	xmlNode *nodep;
115 	dom_object *intern;
116 	char *data = NULL;
117 	size_t data_len = 0;
118 	int err;
119 	xmlNodePtr lst;
120 
121 	id = ZEND_THIS;
122 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data, &data_len) == FAILURE) {
123 		return;
124 	}
125 
126 	DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
127 
128 	if (dom_node_is_read_only(nodep) == SUCCESS) {
129 		php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document));
130 		RETURN_FALSE;
131 	}
132 
133 	if (data) {
134 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, (xmlChar *) data, &lst);
135 		if (err != 0) {
136 			RETURN_FALSE;
137 		}
138 		/* Following needed due to bug in libxml2 <= 2.6.14
139 		ifdef after next libxml release as bug is fixed in their cvs */
140 		php_dom_xmlSetTreeDoc(lst, nodep->doc);
141 		/* End stupid hack */
142 
143 		xmlAddChildList(nodep,lst);
144 	}
145 
146 	RETURN_TRUE;
147 }
148 /* }}} */
149 
150 #endif
151