xref: /PHP-8.2/ext/dom/domimplementation.c (revision 9c306470)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Christian Stocker <chregu@php.net>                          |
14    |          Rob Richards <rrichards@php.net>                            |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php.h"
23 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 
26 /*
27 * class DOMImplementation
28 *
29 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-102161490
30 * Since:
31 */
32 
33 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-5CED94D7
34 Since:
35 */
PHP_METHOD(DOMImplementation,hasFeature)36 PHP_METHOD(DOMImplementation, hasFeature)
37 {
38 	zend_string *feature, *version;
39 
40 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &feature, &version) == FAILURE) {
41 		RETURN_THROWS();
42 	}
43 
44 	RETURN_BOOL(dom_has_feature(feature, version));
45 }
46 /* }}} end dom_domimplementation_has_feature */
47 
48 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocType
49 Since: DOM Level 2
50 */
PHP_METHOD(DOMImplementation,createDocumentType)51 PHP_METHOD(DOMImplementation, createDocumentType)
52 {
53 	xmlDtd *doctype;
54 	int ret;
55 	size_t name_len = 0, publicid_len = 0, systemid_len = 0;
56 	char *name = NULL, *publicid = NULL, *systemid = NULL;
57 	xmlChar *pch1 = NULL, *pch2 = NULL, *localname = NULL;
58 	xmlURIPtr uri;
59 
60 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ss", &name, &name_len, &publicid, &publicid_len, &systemid, &systemid_len) == FAILURE) {
61 		RETURN_THROWS();
62 	}
63 
64 	if (name_len == 0) {
65 		zend_argument_value_error(1, "cannot be empty");
66 		RETURN_THROWS();
67 	}
68 
69 	if (publicid_len > 0) {
70 		pch1 = (xmlChar *) publicid;
71 	}
72 	if (systemid_len > 0) {
73 		pch2 = (xmlChar *) systemid;
74 	}
75 
76 	if (strstr(name, "%00")) {
77 		php_error_docref(NULL, E_WARNING, "URI must not contain percent-encoded NUL bytes");
78 		RETURN_FALSE;
79 	}
80 
81 	uri = xmlParseURI(name);
82 	if (uri != NULL && uri->opaque != NULL) {
83 		localname = xmlStrdup((xmlChar *) uri->opaque);
84 		if (xmlStrchr(localname, (xmlChar) ':') != NULL) {
85 			php_dom_throw_error(NAMESPACE_ERR, 1);
86 			xmlFreeURI(uri);
87 			xmlFree(localname);
88 			RETURN_FALSE;
89 		}
90 	} else {
91 		localname = xmlStrdup((xmlChar *) name);
92 	}
93 
94 	/* TODO: Test that localname has no invalid chars
95 	php_dom_throw_error(INVALID_CHARACTER_ERR,);
96 	*/
97 
98 	if (uri) {
99 		xmlFreeURI(uri);
100 	}
101 
102 	doctype = xmlCreateIntSubset(NULL, localname, pch1, pch2);
103 	xmlFree(localname);
104 
105 	if (doctype == NULL) {
106 		php_error_docref(NULL, E_WARNING, "Unable to create DocumentType");
107 		RETURN_FALSE;
108 	}
109 
110 	DOM_RET_OBJ((xmlNodePtr) doctype, &ret, NULL);
111 }
112 /* }}} end dom_domimplementation_create_document_type */
113 
114 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocument
115 Since: DOM Level 2
116 */
PHP_METHOD(DOMImplementation,createDocument)117 PHP_METHOD(DOMImplementation, createDocument)
118 {
119 	zval *node = NULL;
120 	xmlDoc *docp;
121 	xmlNode *nodep;
122 	xmlDtdPtr doctype = NULL;
123 	xmlNsPtr nsptr = NULL;
124 	int ret, errorcode = 0;
125 	size_t uri_len = 0, name_len = 0;
126 	char *uri = NULL, *name = NULL;
127 	char *prefix = NULL, *localname = NULL;
128 	dom_object *doctobj;
129 
130 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!sO!", &uri, &uri_len, &name, &name_len, &node, dom_documenttype_class_entry) == FAILURE) {
131 		RETURN_THROWS();
132 	}
133 
134 	if (node != NULL) {
135 		DOM_GET_OBJ(doctype, node, xmlDtdPtr, doctobj);
136 		if (doctype->type == XML_DOCUMENT_TYPE_NODE) {
137 			zend_argument_value_error(3, "is an invalid DocumentType object");
138 			RETURN_THROWS();
139 		}
140 		if (doctype->doc != NULL) {
141 			/* As the new document is the context node, and the default for strict error checking
142 			 * is true, this will always throw. */
143 			php_dom_throw_error(WRONG_DOCUMENT_ERR, 1);
144 			RETURN_THROWS();
145 		}
146 	} else {
147 		doctobj = NULL;
148 	}
149 
150 	if (name_len > 0) {
151 		errorcode = dom_check_qname(name, &localname, &prefix, 1, name_len);
152 		if (errorcode == 0 && uri_len > 0
153 			&& ((nsptr = xmlNewNs(NULL, (xmlChar *) uri, (xmlChar *) prefix)) == NULL)
154 		) {
155 			errorcode = NAMESPACE_ERR;
156 		}
157 	}
158 
159 	if (prefix != NULL) {
160 		xmlFree(prefix);
161 	}
162 
163 	if (errorcode != 0) {
164 		if (localname != NULL) {
165 			xmlFree(localname);
166 		}
167 		php_dom_throw_error(errorcode, 1);
168 		RETURN_THROWS();
169 	}
170 
171 	/* currently letting libxml2 set the version string */
172 	docp = xmlNewDoc(NULL);
173 	if (!docp) {
174 		if (localname != NULL) {
175 			xmlFree(localname);
176 		}
177 		/* See above for strict error checking argument. */
178 		php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
179 		RETURN_THROWS();
180 	}
181 
182 	if (doctype != NULL) {
183 		docp->intSubset = doctype;
184 		doctype->parent = docp;
185 		doctype->doc = docp;
186 		docp->children = (xmlNodePtr) doctype;
187 		docp->last = (xmlNodePtr) doctype;
188 	}
189 
190 	if (localname != NULL) {
191 		nodep = xmlNewDocNode(docp, nsptr, (xmlChar *) localname, NULL);
192 		if (!nodep) {
193 			if (doctype != NULL) {
194 				docp->intSubset = NULL;
195 				doctype->parent = NULL;
196 				doctype->doc = NULL;
197 				docp->children = NULL;
198 				docp->last = NULL;
199 			}
200 			xmlFreeDoc(docp);
201 			xmlFree(localname);
202 			php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
203 			RETURN_THROWS();
204 		}
205 
206 		nodep->nsDef = nsptr;
207 
208 		xmlDocSetRootElement(docp, nodep);
209 		xmlFree(localname);
210 	}
211 
212 	DOM_RET_OBJ((xmlNodePtr) docp, &ret, NULL);
213 
214 	if (doctobj != NULL) {
215 		doctobj->document = ((dom_object *)((php_libxml_node_ptr *)docp->_private)->_private)->document;
216 		php_libxml_increment_doc_ref((php_libxml_node_object *)doctobj, docp);
217 	}
218 }
219 /* }}} end dom_domimplementation_create_document */
220 
221 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#DOMImplementation3-getFeature
222 Since: DOM Level 3
223 */
PHP_METHOD(DOMImplementation,getFeature)224 PHP_METHOD(DOMImplementation, getFeature)
225 {
226 	size_t feature_len, version_len;
227 	char *feature, *version;
228 
229 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
230 		RETURN_THROWS();
231 	}
232 
233 	zend_throw_error(NULL, "Not yet implemented");
234 	RETURN_THROWS();
235 }
236 /* }}} end dom_domimplementation_get_feature */
237 
238 #endif
239