xref: /PHP-8.1/ext/dom/domimplementation.c (revision f15f8fc5)
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 			php_dom_throw_error(WRONG_DOCUMENT_ERR, 1);
142 			RETURN_THROWS();
143 		}
144 	} else {
145 		doctobj = NULL;
146 	}
147 
148 	if (name_len > 0) {
149 		errorcode = dom_check_qname(name, &localname, &prefix, 1, name_len);
150 		if (errorcode == 0 && uri_len > 0
151 			&& ((nsptr = xmlNewNs(NULL, (xmlChar *) uri, (xmlChar *) prefix)) == NULL)
152 		) {
153 			errorcode = NAMESPACE_ERR;
154 		}
155 	}
156 
157 	if (prefix != NULL) {
158 		xmlFree(prefix);
159 	}
160 
161 	if (errorcode != 0) {
162 		if (localname != NULL) {
163 			xmlFree(localname);
164 		}
165 		php_dom_throw_error(errorcode, 1);
166 		RETURN_THROWS();
167 	}
168 
169 	/* currently letting libxml2 set the version string */
170 	docp = xmlNewDoc(NULL);
171 	if (!docp) {
172 		if (localname != NULL) {
173 			xmlFree(localname);
174 		}
175 		RETURN_FALSE;
176 	}
177 
178 	if (doctype != NULL) {
179 		docp->intSubset = doctype;
180 		doctype->parent = docp;
181 		doctype->doc = docp;
182 		docp->children = (xmlNodePtr) doctype;
183 		docp->last = (xmlNodePtr) doctype;
184 	}
185 
186 	if (localname != NULL) {
187 		nodep = xmlNewDocNode(docp, nsptr, (xmlChar *) localname, NULL);
188 		if (!nodep) {
189 			if (doctype != NULL) {
190 				docp->intSubset = NULL;
191 				doctype->parent = NULL;
192 				doctype->doc = NULL;
193 				docp->children = NULL;
194 				docp->last = NULL;
195 			}
196 			xmlFreeDoc(docp);
197 			xmlFree(localname);
198 			/* Need some better type of error here */
199 			php_dom_throw_error(PHP_ERR, 1);
200 			RETURN_THROWS();
201 		}
202 
203 		nodep->nsDef = nsptr;
204 
205 		xmlDocSetRootElement(docp, nodep);
206 		xmlFree(localname);
207 	}
208 
209 	DOM_RET_OBJ((xmlNodePtr) docp, &ret, NULL);
210 
211 	if (doctobj != NULL) {
212 		doctobj->document = ((dom_object *)((php_libxml_node_ptr *)docp->_private)->_private)->document;
213 		php_libxml_increment_doc_ref((php_libxml_node_object *)doctobj, docp);
214 	}
215 }
216 /* }}} end dom_domimplementation_create_document */
217 
218 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#DOMImplementation3-getFeature
219 Since: DOM Level 3
220 */
PHP_METHOD(DOMImplementation,getFeature)221 PHP_METHOD(DOMImplementation, getFeature)
222 {
223 	size_t feature_len, version_len;
224 	char *feature, *version;
225 
226 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
227 		RETURN_THROWS();
228 	}
229 
230 	DOM_NOT_IMPLEMENTED();
231 }
232 /* }}} end dom_domimplementation_get_feature */
233 
234 #endif
235