xref: /PHP-5.5/ext/dom/domimplementation.c (revision 73c1be26)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2015 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 /* $Id$ */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "php.h"
27 #if HAVE_LIBXML && HAVE_DOM
28 #include "php_dom.h"
29 
30 /* {{{ arginfo */
31 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_get_feature, 0, 0, 2)
32 	ZEND_ARG_INFO(0, feature)
33 	ZEND_ARG_INFO(0, version)
34 ZEND_END_ARG_INFO();
35 
36 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_has_feature, 0, 0, 0)
37 ZEND_END_ARG_INFO();
38 
39 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_create_documenttype, 0, 0, 3)
40 	ZEND_ARG_INFO(0, qualifiedName)
41 	ZEND_ARG_INFO(0, publicId)
42 	ZEND_ARG_INFO(0, systemId)
43 ZEND_END_ARG_INFO();
44 
45 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_create_document, 0, 0, 3)
46 	ZEND_ARG_INFO(0, namespaceURI)
47 	ZEND_ARG_INFO(0, qualifiedName)
48 	ZEND_ARG_OBJ_INFO(0, docType, DOMDocumentType, 0)
49 ZEND_END_ARG_INFO();
50 /* }}} */
51 
52 /*
53 * class DOMImplementation
54 *
55 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-102161490
56 * Since:
57 */
58 
59 const zend_function_entry php_dom_domimplementation_class_functions[] = {
60 	PHP_ME(domimplementation, getFeature, arginfo_dom_implementation_get_feature, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
61 	PHP_ME(domimplementation, hasFeature, arginfo_dom_implementation_has_feature, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
62 	PHP_ME(domimplementation, createDocumentType, arginfo_dom_implementation_create_documenttype, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
63 	PHP_ME(domimplementation, createDocument, arginfo_dom_implementation_create_document, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
64 	PHP_FE_END
65 };
66 
67 /* {{{ proto boolean dom_domimplementation_has_feature(string feature, string version);
68 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-5CED94D7
69 Since:
70 */
PHP_METHOD(domimplementation,hasFeature)71 PHP_METHOD(domimplementation, hasFeature)
72 {
73 	int feature_len, version_len;
74 	char *feature, *version;
75 
76 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
77 		return;
78 	}
79 
80 	if (dom_has_feature(feature, version)) {
81 		RETURN_TRUE;
82 	} else {
83 		RETURN_FALSE;
84 	}
85 }
86 /* }}} end dom_domimplementation_has_feature */
87 
88 /* {{{ proto DOMDocumentType dom_domimplementation_create_document_type(string qualifiedName, string publicId, string systemId);
89 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocType
90 Since: DOM Level 2
91 */
PHP_METHOD(domimplementation,createDocumentType)92 PHP_METHOD(domimplementation, createDocumentType)
93 {
94 	xmlDtd *doctype;
95 	int ret, name_len = 0, publicid_len = 0, systemid_len = 0;
96 	char *name = NULL, *publicid = NULL, *systemid = NULL;
97 	xmlChar *pch1 = NULL, *pch2 = NULL, *localname = NULL;
98 	xmlURIPtr uri;
99 
100 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sss", &name, &name_len, &publicid, &publicid_len, &systemid, &systemid_len) == FAILURE) {
101 		return;
102 	}
103 
104 	if (name_len == 0) {
105 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "qualifiedName is required");
106 		RETURN_FALSE;
107 	}
108 
109 	if (publicid_len > 0)
110 		pch1 = publicid;
111 	if (systemid_len > 0)
112 		pch2 = systemid;
113 
114 	uri = xmlParseURI(name);
115 	if (uri != NULL && uri->opaque != NULL) {
116 		localname = xmlStrdup(uri->opaque);
117 		if (xmlStrchr(localname, (xmlChar) ':') != NULL) {
118 			php_dom_throw_error(NAMESPACE_ERR, 1 TSRMLS_CC);
119 			xmlFreeURI(uri);
120 			xmlFree(localname);
121 			RETURN_FALSE;
122 		}
123 	} else {
124 		localname = xmlStrdup(name);
125 	}
126 
127 	/* TODO: Test that localname has no invalid chars
128 	php_dom_throw_error(INVALID_CHARACTER_ERR, TSRMLS_CC);
129 	*/
130 
131 	if (uri) {
132 		xmlFreeURI(uri);
133 	}
134 
135 	doctype = xmlCreateIntSubset(NULL, localname, pch1, pch2);
136 	xmlFree(localname);
137 
138 	if (doctype == NULL) {
139 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create DocumentType");
140 		RETURN_FALSE;
141 	}
142 
143 	DOM_RET_OBJ((xmlNodePtr) doctype, &ret, NULL);
144 }
145 /* }}} end dom_domimplementation_create_document_type */
146 
147 /* {{{ proto DOMDocument dom_domimplementation_create_document(string namespaceURI, string qualifiedName, DOMDocumentType doctype);
148 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocument
149 Since: DOM Level 2
150 */
PHP_METHOD(domimplementation,createDocument)151 PHP_METHOD(domimplementation, createDocument)
152 {
153 	zval *node = NULL;
154 	xmlDoc *docp;
155 	xmlNode *nodep;
156 	xmlDtdPtr doctype = NULL;
157 	xmlNsPtr nsptr = NULL;
158 	int ret, uri_len = 0, name_len = 0, errorcode = 0;
159 	char *uri = NULL, *name = NULL;
160 	char *prefix = NULL, *localname = NULL;
161 	dom_object *doctobj;
162 
163 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssO", &uri, &uri_len, &name, &name_len, &node, dom_documenttype_class_entry) == FAILURE) {
164 		return;
165 	}
166 
167 	if (node != NULL) {
168 		DOM_GET_OBJ(doctype, node, xmlDtdPtr, doctobj);
169 		if (doctype->type == XML_DOCUMENT_TYPE_NODE) {
170 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid DocumentType object");
171 			RETURN_FALSE;
172 		}
173 		if (doctype->doc != NULL) {
174 			php_dom_throw_error(WRONG_DOCUMENT_ERR, 1 TSRMLS_CC);
175 			RETURN_FALSE;
176 		}
177 	} else {
178 		doctobj = NULL;
179 	}
180 
181 	if (name_len > 0) {
182 		errorcode = dom_check_qname(name, &localname, &prefix, 1, name_len);
183 		if (errorcode == 0 && uri_len > 0 && ((nsptr = xmlNewNs(NULL, uri, prefix)) == NULL)) {
184 			errorcode = NAMESPACE_ERR;
185 		}
186 	}
187 
188 	if (prefix != NULL) {
189 		xmlFree(prefix);
190 	}
191 
192 	if (errorcode != 0) {
193 		if (localname != NULL) {
194 			xmlFree(localname);
195 		}
196 		php_dom_throw_error(errorcode, 1 TSRMLS_CC);
197 		RETURN_FALSE;
198 	}
199 
200 	/* currently letting libxml2 set the version string */
201 	docp = xmlNewDoc(NULL);
202 	if (!docp) {
203 		if (localname != NULL) {
204 			xmlFree(localname);
205 		}
206 		RETURN_FALSE;
207 	}
208 
209 	if (doctype != NULL) {
210 		docp->intSubset = doctype;
211 		doctype->parent = docp;
212 		doctype->doc = docp;
213 		docp->children = (xmlNodePtr) doctype;
214 		docp->last = (xmlNodePtr) doctype;
215 	}
216 
217 	if (localname != NULL) {
218 		nodep = xmlNewDocNode (docp, nsptr, localname, NULL);
219 		if (!nodep) {
220 			if (doctype != NULL) {
221 				docp->intSubset = NULL;
222 				doctype->parent = NULL;
223 				doctype->doc = NULL;
224 				docp->children = NULL;
225 				docp->last = NULL;
226 			}
227 			xmlFreeDoc(docp);
228 			xmlFree(localname);
229 			/* Need some type of error here */
230 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected Error");
231 			RETURN_FALSE;
232 		}
233 
234 		nodep->nsDef = nsptr;
235 
236 		xmlDocSetRootElement(docp, nodep);
237 		xmlFree(localname);
238 	}
239 
240 	DOM_RET_OBJ((xmlNodePtr) docp, &ret, NULL);
241 
242 	if (doctobj != NULL) {
243 		doctobj->document = ((dom_object *)((php_libxml_node_ptr *)docp->_private)->_private)->document;
244 		php_libxml_increment_doc_ref((php_libxml_node_object *)doctobj, docp TSRMLS_CC);
245 	}
246 }
247 /* }}} end dom_domimplementation_create_document */
248 
249 /* {{{ proto DOMNode dom_domimplementation_get_feature(string feature, string version);
250 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#DOMImplementation3-getFeature
251 Since: DOM Level 3
252 */
PHP_METHOD(domimplementation,getFeature)253 PHP_METHOD(domimplementation, getFeature)
254 {
255  DOM_NOT_IMPLEMENTED();
256 }
257 /* }}} end dom_domimplementation_get_feature */
258 
259 #endif
260 
261 /*
262  * Local variables:
263  * tab-width: 4
264  * c-basic-offset: 4
265  * End:
266  * vim600: noet sw=4 ts=4 fdm=marker
267  * vim<600: noet sw=4 ts=4
268  */
269