1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: document.c 281924 2009-06-10 12:25:41Z iliaa $ */
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 : #include <libxml/SAX.h>
30 : #ifdef LIBXML_SCHEMAS_ENABLED
31 : #include <libxml/relaxng.h>
32 : #include <libxml/xmlschemas.h>
33 : #endif
34 :
35 : #include "ext/libxml/php_libxml.h"
36 :
37 : typedef struct _idsIterator idsIterator;
38 : struct _idsIterator {
39 : xmlChar *elementId;
40 : xmlNode *element;
41 : };
42 :
43 : #define DOM_LOAD_STRING 0
44 : #define DOM_LOAD_FILE 1
45 :
46 : /* {{{ arginfo */
47 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_element, 0, 0, 1)
48 : ZEND_ARG_INFO(0, tagName)
49 : ZEND_ARG_INFO(0, value)
50 : ZEND_END_ARG_INFO();
51 :
52 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_document_fragment, 0, 0, 0)
53 : ZEND_END_ARG_INFO();
54 :
55 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_text_node, 0, 0, 1)
56 : ZEND_ARG_INFO(0, data)
57 : ZEND_END_ARG_INFO();
58 :
59 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_comment, 0, 0, 1)
60 : ZEND_ARG_INFO(0, data)
61 : ZEND_END_ARG_INFO();
62 :
63 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_cdatasection, 0, 0, 1)
64 : ZEND_ARG_INFO(0, data)
65 : ZEND_END_ARG_INFO();
66 :
67 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_processing_instruction, 0, 0, 2)
68 : ZEND_ARG_INFO(0, target)
69 : ZEND_ARG_INFO(0, data)
70 : ZEND_END_ARG_INFO();
71 :
72 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_attribute, 0, 0, 1)
73 : ZEND_ARG_INFO(0, name)
74 : ZEND_END_ARG_INFO();
75 :
76 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_entity_reference, 0, 0, 1)
77 : ZEND_ARG_INFO(0, name)
78 : ZEND_END_ARG_INFO();
79 :
80 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_get_elements_by_tag_name, 0, 0, 1)
81 : ZEND_ARG_INFO(0, tagName)
82 : ZEND_END_ARG_INFO();
83 :
84 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_import_node, 0, 0, 2)
85 : ZEND_ARG_OBJ_INFO(0, importedNode, DOMNode, 0)
86 : ZEND_ARG_INFO(0, deep)
87 : ZEND_END_ARG_INFO();
88 :
89 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_element_ns, 0, 0, 2)
90 : ZEND_ARG_INFO(0, namespaceURI)
91 : ZEND_ARG_INFO(0, qualifiedName)
92 : ZEND_ARG_INFO(0, value)
93 : ZEND_END_ARG_INFO();
94 :
95 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_create_attribute_ns, 0, 0, 2)
96 : ZEND_ARG_INFO(0, namespaceURI)
97 : ZEND_ARG_INFO(0, qualifiedName)
98 : ZEND_END_ARG_INFO();
99 :
100 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_get_elements_by_tag_name_ns, 0, 0, 2)
101 : ZEND_ARG_INFO(0, namespaceURI)
102 : ZEND_ARG_INFO(0, localName)
103 : ZEND_END_ARG_INFO();
104 :
105 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_get_element_by_id, 0, 0, 1)
106 : ZEND_ARG_INFO(0, elementId)
107 : ZEND_END_ARG_INFO();
108 :
109 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_adopt_node, 0, 0, 1)
110 : ZEND_ARG_OBJ_INFO(0, source, DOMNode, 0)
111 : ZEND_END_ARG_INFO();
112 :
113 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_normalize_document, 0, 0, 0)
114 : ZEND_END_ARG_INFO();
115 :
116 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_rename_node, 0, 0, 3)
117 : ZEND_ARG_OBJ_INFO(0, node, DOMNode, 0)
118 : ZEND_ARG_INFO(0, namespaceURI)
119 : ZEND_ARG_INFO(0, qualifiedName)
120 : ZEND_END_ARG_INFO();
121 :
122 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_load, 0, 0, 1)
123 : ZEND_ARG_INFO(0, source)
124 : ZEND_ARG_INFO(0, options)
125 : ZEND_END_ARG_INFO();
126 :
127 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_save, 0, 0, 1)
128 : ZEND_ARG_INFO(0, file)
129 : ZEND_END_ARG_INFO();
130 :
131 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_loadxml, 0, 0, 1)
132 : ZEND_ARG_INFO(0, source)
133 : ZEND_ARG_INFO(0, options)
134 : ZEND_END_ARG_INFO();
135 :
136 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_savexml, 0, 0, 0)
137 : ZEND_ARG_OBJ_INFO(0, node, DOMNode, 1)
138 : ZEND_END_ARG_INFO();
139 :
140 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_construct, 0, 0, 0)
141 : ZEND_ARG_INFO(0, version)
142 : ZEND_ARG_INFO(0, encoding)
143 : ZEND_END_ARG_INFO();
144 :
145 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_validate, 0, 0, 0)
146 : ZEND_END_ARG_INFO();
147 :
148 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_xinclude, 0, 0, 0)
149 : ZEND_ARG_INFO(0, options)
150 : ZEND_END_ARG_INFO();
151 :
152 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_loadhtml, 0, 0, 1)
153 : ZEND_ARG_INFO(0, source)
154 : ZEND_END_ARG_INFO();
155 :
156 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_loadhtmlfile, 0, 0, 1)
157 : ZEND_ARG_INFO(0, source)
158 : ZEND_END_ARG_INFO();
159 :
160 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_savehtml, 0, 0, 0)
161 : ZEND_END_ARG_INFO();
162 :
163 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_savehtmlfile, 0, 0, 1)
164 : ZEND_ARG_INFO(0, file)
165 : ZEND_END_ARG_INFO();
166 :
167 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_schema_validate_file, 0, 0, 1)
168 : ZEND_ARG_INFO(0, filename)
169 : ZEND_END_ARG_INFO();
170 :
171 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_schema_validate_xml, 0, 0, 1)
172 : ZEND_ARG_INFO(0, source)
173 : ZEND_END_ARG_INFO();
174 :
175 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_relaxNG_validate_file, 0, 0, 1)
176 : ZEND_ARG_INFO(0, filename)
177 : ZEND_END_ARG_INFO();
178 :
179 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_relaxNG_validate_xml, 0, 0, 1)
180 : ZEND_ARG_INFO(0, source)
181 : ZEND_END_ARG_INFO();
182 :
183 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_registernodeclass, 0, 0, 2)
184 : ZEND_ARG_INFO(0, baseClass)
185 : ZEND_ARG_INFO(0, extendedClass)
186 : ZEND_END_ARG_INFO();
187 : /* }}} */
188 :
189 : /*
190 : * class DOMDocument extends DOMNode
191 : *
192 : * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-i-Document
193 : * Since:
194 : */
195 :
196 : const zend_function_entry php_dom_document_class_functions[] = { /* {{{ */
197 : PHP_FALIAS(createElement, dom_document_create_element, arginfo_dom_document_create_element)
198 : PHP_FALIAS(createDocumentFragment, dom_document_create_document_fragment, arginfo_dom_document_create_document_fragment)
199 : PHP_FALIAS(createTextNode, dom_document_create_text_node, arginfo_dom_document_create_text_node)
200 : PHP_FALIAS(createComment, dom_document_create_comment, arginfo_dom_document_create_comment)
201 : PHP_FALIAS(createCDATASection, dom_document_create_cdatasection, arginfo_dom_document_create_cdatasection)
202 : PHP_FALIAS(createProcessingInstruction, dom_document_create_processing_instruction, arginfo_dom_document_create_processing_instruction)
203 : PHP_FALIAS(createAttribute, dom_document_create_attribute, arginfo_dom_document_create_attribute)
204 : PHP_FALIAS(createEntityReference, dom_document_create_entity_reference, arginfo_dom_document_create_entity_reference)
205 : PHP_FALIAS(getElementsByTagName, dom_document_get_elements_by_tag_name, arginfo_dom_document_get_elements_by_tag_name)
206 : PHP_FALIAS(importNode, dom_document_import_node, arginfo_dom_document_import_node)
207 : PHP_FALIAS(createElementNS, dom_document_create_element_ns, arginfo_dom_document_create_element_ns)
208 : PHP_FALIAS(createAttributeNS, dom_document_create_attribute_ns, arginfo_dom_document_create_attribute_ns)
209 : PHP_FALIAS(getElementsByTagNameNS, dom_document_get_elements_by_tag_name_ns, arginfo_dom_document_get_elements_by_tag_name_ns)
210 : PHP_FALIAS(getElementById, dom_document_get_element_by_id, arginfo_dom_document_get_element_by_id)
211 : PHP_FALIAS(adoptNode, dom_document_adopt_node, arginfo_dom_document_adopt_node)
212 : PHP_FALIAS(normalizeDocument, dom_document_normalize_document, arginfo_dom_document_normalize_document)
213 : PHP_FALIAS(renameNode, dom_document_rename_node, arginfo_dom_document_rename_node)
214 : PHP_ME(domdocument, load, arginfo_dom_document_load, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
215 : PHP_FALIAS(save, dom_document_save, arginfo_dom_document_save)
216 : PHP_ME(domdocument, loadXML, arginfo_dom_document_loadxml, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
217 : PHP_FALIAS(saveXML, dom_document_savexml, arginfo_dom_document_savexml)
218 : PHP_ME(domdocument, __construct, arginfo_dom_document_construct, ZEND_ACC_PUBLIC)
219 : PHP_FALIAS(validate, dom_document_validate, arginfo_dom_document_validate)
220 : PHP_FALIAS(xinclude, dom_document_xinclude, arginfo_dom_document_xinclude)
221 : #if defined(LIBXML_HTML_ENABLED)
222 : PHP_ME(domdocument, loadHTML, arginfo_dom_document_loadhtml, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
223 : PHP_ME(domdocument, loadHTMLFile, arginfo_dom_document_loadhtmlfile, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
224 : PHP_FALIAS(saveHTML, dom_document_save_html, arginfo_dom_document_savehtml)
225 : PHP_FALIAS(saveHTMLFile, dom_document_save_html_file, arginfo_dom_document_savehtmlfile)
226 : #endif /* defined(LIBXML_HTML_ENABLED) */
227 : #if defined(LIBXML_SCHEMAS_ENABLED)
228 : PHP_FALIAS(schemaValidate, dom_document_schema_validate_file, arginfo_dom_document_schema_validate_file)
229 : PHP_FALIAS(schemaValidateSource, dom_document_schema_validate_xml, arginfo_dom_document_schema_validate_xml)
230 : PHP_FALIAS(relaxNGValidate, dom_document_relaxNG_validate_file, arginfo_dom_document_relaxNG_validate_file)
231 : PHP_FALIAS(relaxNGValidateSource, dom_document_relaxNG_validate_xml, arginfo_dom_document_relaxNG_validate_xml)
232 : #endif
233 : PHP_ME(domdocument, registerNodeClass, arginfo_dom_document_registernodeclass, ZEND_ACC_PUBLIC)
234 : {NULL, NULL, NULL}
235 : };
236 : /* }}} */
237 :
238 : /* {{{ docType DOMDocumentType
239 : readonly=yes
240 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-B63ED1A31
241 : Since:
242 : */
243 : int dom_document_doctype_read(dom_object *obj, zval **retval TSRMLS_DC)
244 5 : {
245 : xmlDoc *docp;
246 : xmlDtdPtr dtdptr;
247 : int ret;
248 :
249 5 : docp = (xmlDocPtr) dom_object_get_node(obj);
250 :
251 5 : if (docp == NULL) {
252 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
253 0 : return FAILURE;
254 : }
255 :
256 5 : ALLOC_ZVAL(*retval);
257 :
258 5 : dtdptr = xmlGetIntSubset(docp);
259 5 : if (!dtdptr) {
260 0 : ZVAL_NULL(*retval);
261 0 : return SUCCESS;
262 : }
263 :
264 5 : if (NULL == (*retval = php_dom_create_object((xmlNodePtr) dtdptr, &ret, NULL, *retval, obj TSRMLS_CC))) {
265 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object");
266 0 : return FAILURE;
267 : }
268 5 : return SUCCESS;
269 :
270 : }
271 :
272 : /* }}} */
273 :
274 : /* {{{ implementation DOMImplementation
275 : readonly=yes
276 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1B793EBA
277 : Since:
278 : */
279 : int dom_document_implementation_read(dom_object *obj, zval **retval TSRMLS_DC)
280 1 : {
281 1 : ALLOC_ZVAL(*retval);
282 1 : php_dom_create_implementation(retval TSRMLS_CC);
283 1 : return SUCCESS;
284 : }
285 :
286 : /* }}} */
287 :
288 : /* {{{ documentElement DOMElement
289 : readonly=yes
290 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-87CD092
291 : Since:
292 : */
293 : int dom_document_document_element_read(dom_object *obj, zval **retval TSRMLS_DC)
294 49 : {
295 : xmlDoc *docp;
296 : xmlNode *root;
297 : int ret;
298 :
299 49 : docp = (xmlDocPtr) dom_object_get_node(obj);
300 :
301 49 : if (docp == NULL) {
302 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
303 0 : return FAILURE;
304 : }
305 :
306 49 : ALLOC_ZVAL(*retval);
307 :
308 49 : root = xmlDocGetRootElement(docp);
309 49 : if (!root) {
310 0 : ZVAL_NULL(*retval);
311 0 : return SUCCESS;
312 : }
313 :
314 49 : if (NULL == (*retval = php_dom_create_object(root, &ret, NULL, *retval, obj TSRMLS_CC))) {
315 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object");
316 0 : return FAILURE;
317 : }
318 49 : return SUCCESS;
319 : }
320 :
321 : /* }}} */
322 :
323 : /* {{{ encoding string
324 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-encoding
325 : Since: DOM Level 3
326 : */
327 : int dom_document_encoding_read(dom_object *obj, zval **retval TSRMLS_DC)
328 4 : {
329 : xmlDoc *docp;
330 : char *encoding;
331 :
332 4 : docp = (xmlDocPtr) dom_object_get_node(obj);
333 :
334 4 : if (docp == NULL) {
335 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
336 0 : return FAILURE;
337 : }
338 :
339 4 : encoding = (char *) docp->encoding;
340 4 : ALLOC_ZVAL(*retval);
341 :
342 4 : if (encoding != NULL) {
343 3 : ZVAL_XML_STRING(*retval, encoding, ZSTR_DUPLICATE);
344 : } else {
345 1 : ZVAL_NULL(*retval);
346 : }
347 :
348 4 : return SUCCESS;
349 : }
350 :
351 : int dom_document_encoding_write(dom_object *obj, zval *newval TSRMLS_DC)
352 4 : {
353 : zval value_copy;
354 : xmlDoc *docp;
355 : xmlCharEncodingHandlerPtr handler;
356 :
357 4 : docp = (xmlDocPtr) dom_object_get_node(obj);
358 :
359 4 : if (docp == NULL) {
360 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
361 0 : return FAILURE;
362 : }
363 :
364 4 : if (newval->type != IS_STRING) {
365 4 : if(Z_REFCOUNT_P(newval) > 1) {
366 0 : value_copy = *newval;
367 0 : zval_copy_ctor(&value_copy);
368 0 : newval = &value_copy;
369 : }
370 4 : convert_to_string_with_converter(newval, UG(utf8_conv));
371 : }
372 :
373 4 : handler = xmlFindCharEncodingHandler(Z_STRVAL_P(newval));
374 :
375 4 : if (handler != NULL) {
376 3 : xmlCharEncCloseFunc(handler);
377 3 : if (docp->encoding != NULL) {
378 2 : xmlFree((xmlChar *)docp->encoding);
379 : }
380 3 : docp->encoding = xmlStrdup((const xmlChar *) Z_STRVAL_P(newval));
381 : } else {
382 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Document Encoding");
383 : }
384 :
385 4 : if (newval == &value_copy) {
386 0 : zval_dtor(newval);
387 : }
388 :
389 4 : return SUCCESS;
390 : }
391 :
392 : /* }}} */
393 :
394 : /* {{{ standalone boolean
395 : readonly=no
396 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-standalone
397 : Since: DOM Level 3
398 : */
399 : int dom_document_standalone_read(dom_object *obj, zval **retval TSRMLS_DC)
400 2 : {
401 : xmlDoc *docp;
402 : int standalone;
403 :
404 2 : docp = (xmlDocPtr) dom_object_get_node(obj);
405 :
406 2 : if (docp == NULL) {
407 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
408 0 : return FAILURE;
409 : }
410 :
411 2 : ALLOC_ZVAL(*retval);
412 2 : standalone = docp->standalone;
413 2 : ZVAL_BOOL(*retval, standalone);
414 :
415 2 : return SUCCESS;
416 : }
417 :
418 : int dom_document_standalone_write(dom_object *obj, zval *newval TSRMLS_DC)
419 1 : {
420 : zval value_copy;
421 : xmlDoc *docp;
422 : int standalone;
423 :
424 1 : docp = (xmlDocPtr) dom_object_get_node(obj);
425 :
426 1 : if (docp == NULL) {
427 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
428 0 : return FAILURE;
429 : }
430 :
431 1 : if(Z_REFCOUNT_P(newval) > 1) {
432 0 : value_copy = *newval;
433 0 : zval_copy_ctor(&value_copy);
434 0 : newval = &value_copy;
435 : }
436 1 : convert_to_long(newval);
437 :
438 1 : standalone = Z_LVAL_P(newval);
439 1 : if (standalone > 0) {
440 0 : docp->standalone = 1;
441 : }
442 1 : else if (standalone < 0) {
443 0 : docp->standalone = -1;
444 : }
445 : else {
446 1 : docp->standalone = 0;
447 : }
448 :
449 1 : if (newval == &value_copy) {
450 0 : zval_dtor(newval);
451 : }
452 :
453 1 : return SUCCESS;
454 : }
455 :
456 : /* }}} */
457 :
458 : /* {{{ version string
459 : readonly=no
460 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-version
461 : Since: DOM Level 3
462 : */
463 : int dom_document_version_read(dom_object *obj, zval **retval TSRMLS_DC)
464 0 : {
465 : xmlDoc *docp;
466 : char *version;
467 :
468 0 : docp = (xmlDocPtr) dom_object_get_node(obj);
469 :
470 0 : if (docp == NULL) {
471 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
472 0 : return FAILURE;
473 : }
474 :
475 0 : version = (char *) docp->version;
476 0 : ALLOC_ZVAL(*retval);
477 :
478 0 : if (version != NULL) {
479 0 : ZVAL_XML_STRING(*retval, version, ZSTR_DUPLICATE);
480 : } else {
481 0 : ZVAL_NULL(*retval);
482 : }
483 :
484 0 : return SUCCESS;
485 : }
486 :
487 : int dom_document_version_write(dom_object *obj, zval *newval TSRMLS_DC)
488 0 : {
489 : zval value_copy;
490 : xmlDoc *docp;
491 :
492 0 : docp = (xmlDocPtr) dom_object_get_node(obj);
493 :
494 0 : if (docp == NULL) {
495 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
496 0 : return FAILURE;
497 : }
498 :
499 0 : if (docp->version != NULL) {
500 0 : xmlFree((xmlChar *) docp->version );
501 : }
502 :
503 0 : if (newval->type != IS_STRING) {
504 0 : if(Z_REFCOUNT_P(newval) > 1) {
505 0 : value_copy = *newval;
506 0 : zval_copy_ctor(&value_copy);
507 0 : newval = &value_copy;
508 : }
509 0 : convert_to_string_with_converter(newval, UG(utf8_conv));
510 : }
511 :
512 0 : docp->version = xmlStrdup((const xmlChar *) Z_STRVAL_P(newval));
513 :
514 0 : if (newval == &value_copy) {
515 0 : zval_dtor(newval);
516 : }
517 :
518 0 : return SUCCESS;
519 : }
520 :
521 : /* }}} */
522 :
523 : /* {{{ strictErrorChecking boolean
524 : readonly=no
525 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-strictErrorChecking
526 : Since: DOM Level 3
527 : */
528 : int dom_document_strict_error_checking_read(dom_object *obj, zval **retval TSRMLS_DC)
529 4 : {
530 : dom_doc_propsptr doc_prop;
531 :
532 4 : ALLOC_ZVAL(*retval);
533 4 : if (obj->document) {
534 4 : doc_prop = dom_get_doc_props(obj->document);
535 4 : ZVAL_BOOL(*retval, doc_prop->stricterror);
536 : } else {
537 0 : ZVAL_FALSE(*retval);
538 : }
539 4 : return SUCCESS;
540 : }
541 :
542 : int dom_document_strict_error_checking_write(dom_object *obj, zval *newval TSRMLS_DC)
543 2 : {
544 : zval value_copy;
545 : dom_doc_propsptr doc_prop;
546 :
547 2 : if(Z_REFCOUNT_P(newval) > 1) {
548 0 : value_copy = *newval;
549 0 : zval_copy_ctor(&value_copy);
550 0 : newval = &value_copy;
551 : }
552 2 : convert_to_boolean(newval);
553 :
554 2 : if (obj->document) {
555 2 : doc_prop = dom_get_doc_props(obj->document);
556 2 : doc_prop->stricterror = Z_LVAL_P(newval);
557 : }
558 :
559 2 : if (newval == &value_copy) {
560 0 : zval_dtor(newval);
561 : }
562 :
563 2 : return SUCCESS;
564 : }
565 :
566 : /* }}} */
567 :
568 : /* {{{ formatOutput boolean
569 : readonly=no
570 : */
571 : int dom_document_format_output_read(dom_object *obj, zval **retval TSRMLS_DC)
572 2 : {
573 : dom_doc_propsptr doc_prop;
574 :
575 2 : ALLOC_ZVAL(*retval);
576 2 : if (obj->document) {
577 2 : doc_prop = dom_get_doc_props(obj->document);
578 2 : ZVAL_BOOL(*retval, doc_prop->formatoutput);
579 : } else {
580 0 : ZVAL_FALSE(*retval);
581 : }
582 2 : return SUCCESS;
583 : }
584 :
585 : int dom_document_format_output_write(dom_object *obj, zval *newval TSRMLS_DC)
586 4 : {
587 : zval value_copy;
588 : dom_doc_propsptr doc_prop;
589 :
590 4 : if(Z_REFCOUNT_P(newval) > 1) {
591 0 : value_copy = *newval;
592 0 : zval_copy_ctor(&value_copy);
593 0 : newval = &value_copy;
594 : }
595 4 : convert_to_boolean(newval);
596 :
597 4 : if (obj->document) {
598 4 : doc_prop = dom_get_doc_props(obj->document);
599 4 : doc_prop->formatoutput = Z_LVAL_P(newval);
600 : }
601 :
602 4 : if (newval == &value_copy) {
603 0 : zval_dtor(newval);
604 : }
605 :
606 4 : return SUCCESS;
607 : }
608 : /* }}} */
609 :
610 : /* {{{ validateOnParse boolean
611 : readonly=no
612 : */
613 : int dom_document_validate_on_parse_read(dom_object *obj, zval **retval TSRMLS_DC)
614 3 : {
615 : dom_doc_propsptr doc_prop;
616 :
617 3 : ALLOC_ZVAL(*retval);
618 3 : if (obj->document) {
619 3 : doc_prop = dom_get_doc_props(obj->document);
620 3 : ZVAL_BOOL(*retval, doc_prop->validateonparse);
621 : } else {
622 0 : ZVAL_FALSE(*retval);
623 : }
624 3 : return SUCCESS;
625 : }
626 :
627 : int dom_document_validate_on_parse_write(dom_object *obj, zval *newval TSRMLS_DC)
628 4 : {
629 : zval value_copy;
630 : dom_doc_propsptr doc_prop;
631 :
632 4 : if(Z_REFCOUNT_P(newval) > 1) {
633 0 : value_copy = *newval;
634 0 : zval_copy_ctor(&value_copy);
635 0 : newval = &value_copy;
636 : }
637 4 : convert_to_boolean(newval);
638 :
639 4 : if (obj->document) {
640 4 : doc_prop = dom_get_doc_props(obj->document);
641 4 : doc_prop->validateonparse = Z_LVAL_P(newval);
642 : }
643 :
644 4 : if (newval == &value_copy) {
645 0 : zval_dtor(newval);
646 : }
647 :
648 4 : return SUCCESS;
649 : }
650 : /* }}} */
651 :
652 : /* {{{ resolveExternals boolean
653 : readonly=no
654 : */
655 : int dom_document_resolve_externals_read(dom_object *obj, zval **retval TSRMLS_DC)
656 2 : {
657 : dom_doc_propsptr doc_prop;
658 :
659 2 : ALLOC_ZVAL(*retval);
660 2 : if (obj->document) {
661 2 : doc_prop = dom_get_doc_props(obj->document);
662 2 : ZVAL_BOOL(*retval, doc_prop->resolveexternals);
663 : } else {
664 0 : ZVAL_FALSE(*retval);
665 : }
666 2 : return SUCCESS;
667 : }
668 :
669 : int dom_document_resolve_externals_write(dom_object *obj, zval *newval TSRMLS_DC)
670 3 : {
671 : zval value_copy;
672 : dom_doc_propsptr doc_prop;
673 :
674 3 : if(Z_REFCOUNT_P(newval) > 1) {
675 0 : value_copy = *newval;
676 0 : zval_copy_ctor(&value_copy);
677 0 : newval = &value_copy;
678 : }
679 3 : convert_to_boolean(newval);
680 :
681 3 : if (obj->document) {
682 3 : doc_prop = dom_get_doc_props(obj->document);
683 3 : doc_prop->resolveexternals = Z_LVAL_P(newval);
684 : }
685 :
686 3 : if (newval == &value_copy) {
687 0 : zval_dtor(newval);
688 : }
689 :
690 3 : return SUCCESS;
691 : }
692 : /* }}} */
693 :
694 : /* {{{ preserveWhiteSpace boolean
695 : readonly=no
696 : */
697 : int dom_document_preserve_whitespace_read(dom_object *obj, zval **retval TSRMLS_DC)
698 2 : {
699 : dom_doc_propsptr doc_prop;
700 :
701 2 : ALLOC_ZVAL(*retval);
702 2 : if (obj->document) {
703 2 : doc_prop = dom_get_doc_props(obj->document);
704 2 : ZVAL_BOOL(*retval, doc_prop->preservewhitespace);
705 : } else {
706 0 : ZVAL_FALSE(*retval);
707 : }
708 2 : return SUCCESS;
709 : }
710 :
711 : int dom_document_preserve_whitespace_write(dom_object *obj, zval *newval TSRMLS_DC)
712 3 : {
713 : zval value_copy;
714 : dom_doc_propsptr doc_prop;
715 :
716 3 : if(Z_REFCOUNT_P(newval) > 1) {
717 0 : value_copy = *newval;
718 0 : zval_copy_ctor(&value_copy);
719 0 : newval = &value_copy;
720 : }
721 3 : convert_to_boolean(newval);
722 :
723 3 : if (obj->document) {
724 3 : doc_prop = dom_get_doc_props(obj->document);
725 3 : doc_prop->preservewhitespace = Z_LVAL_P(newval);
726 : }
727 :
728 3 : if (newval == &value_copy) {
729 0 : zval_dtor(newval);
730 : }
731 :
732 3 : return SUCCESS;
733 : }
734 : /* }}} */
735 :
736 : /* {{{ recover boolean
737 : readonly=no
738 : */
739 : int dom_document_recover_read(dom_object *obj, zval **retval TSRMLS_DC)
740 0 : {
741 : dom_doc_propsptr doc_prop;
742 :
743 0 : ALLOC_ZVAL(*retval);
744 0 : if (obj->document) {
745 0 : doc_prop = dom_get_doc_props(obj->document);
746 0 : ZVAL_BOOL(*retval, doc_prop->recover);
747 : } else {
748 0 : ZVAL_FALSE(*retval);
749 : }
750 0 : return SUCCESS;
751 : }
752 :
753 : int dom_document_recover_write(dom_object *obj, zval *newval TSRMLS_DC)
754 0 : {
755 : zval value_copy;
756 : dom_doc_propsptr doc_prop;
757 :
758 0 : if(Z_REFCOUNT_P(newval) > 1) {
759 0 : value_copy = *newval;
760 0 : zval_copy_ctor(&value_copy);
761 0 : newval = &value_copy;
762 : }
763 0 : convert_to_boolean(newval);
764 :
765 0 : if (obj->document) {
766 0 : doc_prop = dom_get_doc_props(obj->document);
767 0 : doc_prop->recover = Z_LVAL_P(newval);
768 : }
769 :
770 0 : if (newval == &value_copy) {
771 0 : zval_dtor(newval);
772 : }
773 :
774 0 : return SUCCESS;
775 : }
776 : /* }}} */
777 :
778 : /* {{{ substituteEntities boolean
779 : readonly=no
780 : */
781 : int dom_document_substitue_entities_read(dom_object *obj, zval **retval TSRMLS_DC)
782 0 : {
783 : dom_doc_propsptr doc_prop;
784 :
785 0 : ALLOC_ZVAL(*retval);
786 0 : if (obj->document) {
787 0 : doc_prop = dom_get_doc_props(obj->document);
788 0 : ZVAL_BOOL(*retval, doc_prop->substituteentities);
789 : } else {
790 0 : ZVAL_FALSE(*retval);
791 : }
792 0 : return SUCCESS;
793 : }
794 :
795 : int dom_document_substitue_entities_write(dom_object *obj, zval *newval TSRMLS_DC)
796 0 : {
797 : zval value_copy;
798 : dom_doc_propsptr doc_prop;
799 :
800 0 : if(Z_REFCOUNT_P(newval) > 1) {
801 0 : value_copy = *newval;
802 0 : zval_copy_ctor(&value_copy);
803 0 : newval = &value_copy;
804 : }
805 0 : convert_to_boolean(newval);
806 :
807 0 : if (obj->document) {
808 0 : doc_prop = dom_get_doc_props(obj->document);
809 0 : doc_prop->substituteentities = Z_LVAL_P(newval);
810 : }
811 :
812 0 : if (newval == &value_copy) {
813 0 : zval_dtor(newval);
814 : }
815 :
816 0 : return SUCCESS;
817 : }
818 : /* }}} */
819 :
820 : /* {{{ documentURI string
821 : readonly=no
822 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-documentURI
823 : Since: DOM Level 3
824 : */
825 : int dom_document_document_uri_read(dom_object *obj, zval **retval TSRMLS_DC)
826 2 : {
827 : xmlDoc *docp;
828 : char *url;
829 :
830 2 : docp = (xmlDocPtr) dom_object_get_node(obj);
831 :
832 2 : if (docp == NULL) {
833 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
834 0 : return FAILURE;
835 : }
836 :
837 2 : ALLOC_ZVAL(*retval);
838 2 : url = (char *) docp->URL;
839 2 : if (url != NULL) {
840 2 : ZVAL_XML_STRING(*retval, url, ZSTR_DUPLICATE);
841 : } else {
842 0 : ZVAL_NULL(*retval);
843 : }
844 :
845 2 : return SUCCESS;
846 : }
847 :
848 : int dom_document_document_uri_write(dom_object *obj, zval *newval TSRMLS_DC)
849 1 : {
850 : zval value_copy;
851 : xmlDoc *docp;
852 :
853 1 : docp = (xmlDocPtr) dom_object_get_node(obj);
854 :
855 1 : if (docp == NULL) {
856 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
857 0 : return FAILURE;
858 : }
859 :
860 1 : if (docp->URL != NULL) {
861 1 : xmlFree((xmlChar *) docp->URL);
862 : }
863 :
864 1 : if (newval->type != IS_STRING) {
865 1 : if(Z_REFCOUNT_P(newval) > 1) {
866 0 : value_copy = *newval;
867 0 : zval_copy_ctor(&value_copy);
868 0 : newval = &value_copy;
869 : }
870 1 : convert_to_string_with_converter(newval, UG(utf8_conv));
871 : }
872 :
873 1 : docp->URL = xmlStrdup((const xmlChar *) Z_STRVAL_P(newval));
874 :
875 1 : if (newval == &value_copy) {
876 0 : zval_dtor(newval);
877 : }
878 :
879 1 : return SUCCESS;
880 : }
881 :
882 : /* }}} */
883 :
884 : /* {{{ config DOMConfiguration
885 : readonly=yes
886 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-config
887 : Since: DOM Level 3
888 : */
889 : int dom_document_config_read(dom_object *obj, zval **retval TSRMLS_DC)
890 1 : {
891 1 : ALLOC_ZVAL(*retval);
892 1 : ZVAL_NULL(*retval);
893 1 : return SUCCESS;
894 : }
895 :
896 : /* }}} */
897 :
898 : /* {{{ proto DOMElement dom_document_create_element(string tagName [, string value]) U
899 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-2141741547
900 : Since:
901 : */
902 : PHP_FUNCTION(dom_document_create_element)
903 67 : {
904 67 : zval *id, *rv = NULL;
905 : xmlNode *node;
906 : xmlDocPtr docp;
907 : dom_object *intern;
908 : int ret, name_len, value_len;
909 67 : char *name, *value = NULL;
910 :
911 67 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&|s&", &id, dom_document_class_entry, &name, &name_len, UG(utf8_conv), &value, &value_len, UG(utf8_conv)) == FAILURE) {
912 0 : return;
913 : }
914 :
915 67 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
916 :
917 67 : if (xmlValidateName((xmlChar *) name, 0) != 0) {
918 2 : php_dom_throw_error(INVALID_CHARACTER_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
919 2 : RETURN_FALSE;
920 : }
921 :
922 65 : node = xmlNewDocNode(docp, NULL, name, value);
923 65 : if (!node) {
924 0 : RETURN_FALSE;
925 : }
926 :
927 65 : DOM_RET_OBJ(rv, node, &ret, intern);
928 : }
929 : /* }}} end dom_document_create_element */
930 :
931 : /* {{{ proto DOMDocumentFragment dom_document_create_document_fragment() U
932 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-35CB04B5
933 : Since:
934 : */
935 : PHP_FUNCTION(dom_document_create_document_fragment)
936 9 : {
937 9 : zval *id, *rv = NULL;
938 : xmlNode *node;
939 : xmlDocPtr docp;
940 : dom_object *intern;
941 : int ret;
942 :
943 9 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) {
944 0 : return;
945 : }
946 :
947 9 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
948 :
949 9 : node = xmlNewDocFragment(docp);
950 9 : if (!node) {
951 0 : RETURN_FALSE;
952 : }
953 :
954 9 : DOM_RET_OBJ(rv, node, &ret, intern);
955 : }
956 : /* }}} end dom_document_create_document_fragment */
957 :
958 : /* {{{ proto DOMText dom_document_create_text_node(string data) U
959 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1975348127
960 : Since:
961 : */
962 : PHP_FUNCTION(dom_document_create_text_node)
963 31 : {
964 31 : zval *id, *rv = NULL;
965 : xmlNode *node;
966 : xmlDocPtr docp;
967 : int ret, value_len;
968 : dom_object *intern;
969 : char *value;
970 :
971 31 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_document_class_entry, &value, &value_len, UG(utf8_conv)) == FAILURE) {
972 0 : return;
973 : }
974 :
975 31 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
976 :
977 31 : node = xmlNewDocText(docp, (xmlChar *) value);
978 31 : if (!node) {
979 0 : RETURN_FALSE;
980 : }
981 :
982 31 : DOM_RET_OBJ(rv, node, &ret, intern);
983 : }
984 : /* }}} end dom_document_create_text_node */
985 :
986 : /* {{{ proto DOMComment dom_document_create_comment(string data) U
987 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1334481328
988 : Since:
989 : */
990 : PHP_FUNCTION(dom_document_create_comment)
991 11 : {
992 11 : zval *id, *rv = NULL;
993 : xmlNode *node;
994 : xmlDocPtr docp;
995 : int ret, value_len;
996 : dom_object *intern;
997 : char *value;
998 :
999 11 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_document_class_entry, &value, &value_len, UG(utf8_conv)) == FAILURE) {
1000 1 : return;
1001 : }
1002 :
1003 10 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1004 :
1005 10 : node = xmlNewDocComment(docp, (xmlChar *) value);
1006 10 : if (!node) {
1007 0 : RETURN_FALSE;
1008 : }
1009 :
1010 10 : DOM_RET_OBJ(rv, node, &ret, intern);
1011 : }
1012 : /* }}} end dom_document_create_comment */
1013 :
1014 : /* {{{ proto DOMCdataSection dom_document_create_cdatasection(string data) U
1015 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-D26C0AF8
1016 : Since:
1017 : */
1018 : PHP_FUNCTION(dom_document_create_cdatasection)
1019 10 : {
1020 10 : zval *id, *rv = NULL;
1021 : xmlNode *node;
1022 : xmlDocPtr docp;
1023 : int ret, value_len;
1024 : dom_object *intern;
1025 : char *value;
1026 :
1027 10 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_document_class_entry, &value, &value_len, UG(utf8_conv)) == FAILURE) {
1028 0 : return;
1029 : }
1030 :
1031 10 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1032 :
1033 10 : node = xmlNewCDataBlock(docp, (xmlChar *) value, value_len);
1034 10 : if (!node) {
1035 0 : RETURN_FALSE;
1036 : }
1037 :
1038 10 : DOM_RET_OBJ(rv, node, &ret, intern);
1039 : }
1040 : /* }}} end dom_document_create_cdatasection */
1041 :
1042 : /* {{{ proto DOMProcessingInstruction dom_document_create_processing_instruction(string target, string data) U
1043 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-135944439
1044 : Since:
1045 : */
1046 : PHP_FUNCTION(dom_document_create_processing_instruction)
1047 4 : {
1048 4 : zval *id, *rv = NULL;
1049 : xmlNode *node;
1050 : xmlDocPtr docp;
1051 4 : int ret, value_len, name_len = 0;
1052 : dom_object *intern;
1053 4 : char *name, *value = NULL;
1054 :
1055 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&|s&", &id, dom_document_class_entry, &name, &name_len, UG(utf8_conv), &value, &value_len, UG(utf8_conv)) == FAILURE) {
1056 0 : return;
1057 : }
1058 :
1059 4 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1060 :
1061 4 : if (xmlValidateName((xmlChar *) name, 0) != 0) {
1062 1 : php_dom_throw_error(INVALID_CHARACTER_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1063 1 : RETURN_FALSE;
1064 : }
1065 :
1066 3 : node = xmlNewPI((xmlChar *) name, (xmlChar *) value);
1067 3 : if (!node) {
1068 0 : RETURN_FALSE;
1069 : }
1070 :
1071 3 : node->doc = docp;
1072 :
1073 3 : DOM_RET_OBJ(rv, node, &ret, intern);
1074 : }
1075 : /* }}} end dom_document_create_processing_instruction */
1076 :
1077 : /* {{{ proto DOMAttr dom_document_create_attribute(string name) U
1078 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1084891198
1079 : Since:
1080 : */
1081 : PHP_FUNCTION(dom_document_create_attribute)
1082 9 : {
1083 9 : zval *id, *rv = NULL;
1084 : xmlAttrPtr node;
1085 : xmlDocPtr docp;
1086 : int ret, name_len;
1087 : dom_object *intern;
1088 : char *name;
1089 :
1090 9 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_document_class_entry, &name, &name_len, UG(utf8_conv)) == FAILURE) {
1091 0 : return;
1092 : }
1093 :
1094 9 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1095 :
1096 9 : if (xmlValidateName((xmlChar *) name, 0) != 0) {
1097 4 : php_dom_throw_error(INVALID_CHARACTER_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1098 4 : RETURN_FALSE;
1099 : }
1100 :
1101 5 : node = xmlNewDocProp(docp, (xmlChar *) name, NULL);
1102 5 : if (!node) {
1103 0 : RETURN_FALSE;
1104 : }
1105 :
1106 5 : DOM_RET_OBJ(rv, (xmlNodePtr) node, &ret, intern);
1107 :
1108 : }
1109 : /* }}} end dom_document_create_attribute */
1110 :
1111 : /* {{{ proto DOMEntityReference dom_document_create_entity_reference(string name) U
1112 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-392B75AE
1113 : Since:
1114 : */
1115 : PHP_FUNCTION(dom_document_create_entity_reference)
1116 4 : {
1117 4 : zval *id, *rv = NULL;
1118 : xmlNode *node;
1119 4 : xmlDocPtr docp = NULL;
1120 : dom_object *intern;
1121 : int ret, name_len;
1122 : char *name;
1123 :
1124 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_document_class_entry, &name, &name_len, UG(utf8_conv)) == FAILURE) {
1125 1 : return;
1126 : }
1127 :
1128 3 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1129 :
1130 3 : if (xmlValidateName((xmlChar *) name, 0) != 0) {
1131 0 : php_dom_throw_error(INVALID_CHARACTER_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1132 0 : RETURN_FALSE;
1133 : }
1134 :
1135 3 : node = xmlNewReference(docp, name);
1136 3 : if (!node) {
1137 0 : RETURN_FALSE;
1138 : }
1139 :
1140 3 : DOM_RET_OBJ(rv, (xmlNodePtr) node, &ret, intern);
1141 : }
1142 : /* }}} end dom_document_create_entity_reference */
1143 :
1144 : /* {{{ proto DOMNodeList dom_document_get_elements_by_tag_name(string tagname) U
1145 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-A6C9094
1146 : Since:
1147 : */
1148 : PHP_FUNCTION(dom_document_get_elements_by_tag_name)
1149 14 : {
1150 : zval *id;
1151 : xmlDocPtr docp;
1152 : int name_len;
1153 : dom_object *intern, *namednode;
1154 : char *name;
1155 : xmlChar *local;
1156 :
1157 14 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_document_class_entry, &name, &name_len, UG(utf8_conv)) == FAILURE) {
1158 0 : return;
1159 : }
1160 :
1161 14 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1162 :
1163 14 : php_dom_create_interator(return_value, DOM_NODELIST TSRMLS_CC);
1164 14 : namednode = (dom_object *)zend_objects_get_address(return_value TSRMLS_CC);
1165 14 : local = xmlCharStrndup(name, name_len);
1166 14 : dom_namednode_iter(intern, 0, namednode, NULL, local, NULL TSRMLS_CC);
1167 : }
1168 : /* }}} end dom_document_get_elements_by_tag_name */
1169 :
1170 : /* {{{ proto DOMNode dom_document_import_node(DOMNode importedNode, boolean deep) U
1171 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Core-Document-importNode
1172 : Since: DOM Level 2
1173 : */
1174 : PHP_FUNCTION(dom_document_import_node)
1175 4 : {
1176 4 : zval *rv = NULL;
1177 : zval *id, *node;
1178 : xmlDocPtr docp;
1179 : xmlNodePtr nodep, retnodep;
1180 : dom_object *intern, *nodeobj;
1181 : int ret;
1182 4 : long recursive = 0;
1183 :
1184 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO|l", &id, dom_document_class_entry, &node, dom_node_class_entry, &recursive) == FAILURE) {
1185 0 : return;
1186 : }
1187 :
1188 4 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1189 :
1190 4 : DOM_GET_OBJ(nodep, node, xmlNodePtr, nodeobj);
1191 :
1192 4 : if (nodep->type == XML_HTML_DOCUMENT_NODE || nodep->type == XML_DOCUMENT_NODE
1193 : || nodep->type == XML_DOCUMENT_TYPE_NODE) {
1194 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot import: Node Type Not Supported");
1195 0 : RETURN_FALSE;
1196 : }
1197 :
1198 4 : if (nodep->doc == docp) {
1199 0 : retnodep = nodep;
1200 : } else {
1201 4 : if ((recursive == 0) && (nodep->type == XML_ELEMENT_NODE)) {
1202 1 : recursive = 2;
1203 : }
1204 4 : retnodep = xmlDocCopyNode(nodep, docp, recursive);
1205 4 : if (!retnodep) {
1206 0 : RETURN_FALSE;
1207 : }
1208 :
1209 : }
1210 :
1211 4 : DOM_RET_OBJ(rv, (xmlNodePtr) retnodep, &ret, intern);
1212 : }
1213 : /* }}} end dom_document_import_node */
1214 :
1215 : /* {{{ proto DOMElement dom_document_create_element_ns(string namespaceURI, string qualifiedName [,string value]) U
1216 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-DocCrElNS
1217 : Since: DOM Level 2
1218 : */
1219 : PHP_FUNCTION(dom_document_create_element_ns)
1220 21 : {
1221 21 : zval *id, *rv = NULL;
1222 : xmlDocPtr docp;
1223 21 : xmlNodePtr nodep = NULL;
1224 21 : xmlNsPtr nsptr = NULL;
1225 21 : int ret, uri_len = 0, name_len = 0, value_len = 0;
1226 21 : char *uri, *name, *value = NULL;
1227 21 : char *localname = NULL, *prefix = NULL;
1228 : int errorcode;
1229 : dom_object *intern;
1230 :
1231 21 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!&s&|s&", &id, dom_document_class_entry, &uri, &uri_len, UG(utf8_conv), &name, &name_len, UG(utf8_conv), &value, &value_len, UG(utf8_conv)) == FAILURE) {
1232 0 : return;
1233 : }
1234 :
1235 21 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1236 :
1237 21 : errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
1238 :
1239 21 : if (errorcode == 0) {
1240 15 : if (xmlValidateName((xmlChar *) localname, 0) == 0) {
1241 15 : nodep = xmlNewDocNode (docp, NULL, localname, value);
1242 15 : if (nodep != NULL && uri != NULL) {
1243 15 : nsptr = xmlSearchNsByHref (nodep->doc, nodep, uri);
1244 15 : if (nsptr == NULL) {
1245 14 : nsptr = dom_get_ns(nodep, uri, &errorcode, prefix);
1246 : }
1247 15 : xmlSetNs(nodep, nsptr);
1248 : }
1249 : } else {
1250 0 : errorcode = INVALID_CHARACTER_ERR;
1251 : }
1252 : }
1253 :
1254 21 : xmlFree(localname);
1255 21 : if (prefix != NULL) {
1256 15 : xmlFree(prefix);
1257 : }
1258 :
1259 21 : if (errorcode != 0) {
1260 9 : if (nodep != NULL) {
1261 3 : xmlFreeNode(nodep);
1262 : }
1263 9 : php_dom_throw_error(errorcode, dom_get_strict_error(intern->document) TSRMLS_CC);
1264 9 : RETURN_FALSE;
1265 : }
1266 :
1267 12 : if (nodep == NULL) {
1268 0 : RETURN_FALSE;
1269 : }
1270 :
1271 :
1272 12 : nodep->ns = nsptr;
1273 :
1274 12 : DOM_RET_OBJ(rv, nodep, &ret, intern);
1275 : }
1276 : /* }}} end dom_document_create_element_ns */
1277 :
1278 : /* {{{ proto DOMAttr dom_document_create_attribute_ns(string namespaceURI, string qualifiedName) U
1279 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-DocCrAttrNS
1280 : Since: DOM Level 2
1281 : */
1282 : PHP_FUNCTION(dom_document_create_attribute_ns)
1283 3 : {
1284 3 : zval *id, *rv = NULL;
1285 : xmlDocPtr docp;
1286 3 : xmlNodePtr nodep = NULL, root;
1287 : xmlNsPtr nsptr;
1288 3 : int ret, uri_len = 0, name_len = 0;
1289 : char *uri, *name;
1290 3 : char *localname = NULL, *prefix = NULL;
1291 : dom_object *intern;
1292 : int errorcode;
1293 :
1294 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os!&s&", &id, dom_document_class_entry, &uri, &uri_len, UG(utf8_conv), &name, &name_len, UG(utf8_conv)) == FAILURE) {
1295 0 : return;
1296 : }
1297 :
1298 3 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1299 :
1300 3 : root = xmlDocGetRootElement(docp);
1301 3 : if (root != NULL) {
1302 3 : errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
1303 3 : if (errorcode == 0) {
1304 3 : if (xmlValidateName((xmlChar *) localname, 0) == 0) {
1305 3 : nodep = (xmlNodePtr) xmlNewDocProp(docp, localname, NULL);
1306 3 : if (nodep != NULL && uri_len > 0) {
1307 3 : nsptr = xmlSearchNsByHref (nodep->doc, root, uri);
1308 3 : if (nsptr == NULL) {
1309 1 : nsptr = dom_get_ns(root, uri, &errorcode, prefix);
1310 : }
1311 3 : xmlSetNs(nodep, nsptr);
1312 : }
1313 : } else {
1314 0 : errorcode = INVALID_CHARACTER_ERR;
1315 : }
1316 : }
1317 : } else {
1318 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Document Missing Root Element");
1319 0 : RETURN_FALSE;
1320 : }
1321 :
1322 3 : xmlFree(localname);
1323 3 : if (prefix != NULL) {
1324 3 : xmlFree(prefix);
1325 : }
1326 :
1327 3 : if (errorcode != 0) {
1328 0 : if (nodep != NULL) {
1329 0 : xmlFreeProp((xmlAttrPtr) nodep);
1330 : }
1331 0 : php_dom_throw_error(errorcode, dom_get_strict_error(intern->document) TSRMLS_CC);
1332 0 : RETURN_FALSE;
1333 : }
1334 :
1335 3 : if (nodep == NULL) {
1336 0 : RETURN_FALSE;
1337 : }
1338 :
1339 3 : DOM_RET_OBJ(rv, nodep, &ret, intern);
1340 : }
1341 : /* }}} end dom_document_create_attribute_ns */
1342 :
1343 : /* {{{ proto DOMNodeList dom_document_get_elements_by_tag_name_ns(string namespaceURI, string localName) U
1344 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-getElBTNNS
1345 : Since: DOM Level 2
1346 : */
1347 : PHP_FUNCTION(dom_document_get_elements_by_tag_name_ns)
1348 4 : {
1349 : zval *id;
1350 : xmlDocPtr docp;
1351 : int uri_len, name_len;
1352 : dom_object *intern, *namednode;
1353 : char *uri, *name;
1354 : xmlChar *local, *nsuri;
1355 :
1356 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&s&", &id, dom_document_class_entry, &uri, &uri_len, UG(utf8_conv), &name, &name_len, UG(utf8_conv)) == FAILURE) {
1357 0 : return;
1358 : }
1359 :
1360 4 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1361 :
1362 4 : php_dom_create_interator(return_value, DOM_NODELIST TSRMLS_CC);
1363 4 : namednode = (dom_object *)zend_objects_get_address(return_value TSRMLS_CC);
1364 4 : local = xmlCharStrndup(name, name_len);
1365 4 : nsuri = xmlCharStrndup(uri, uri_len);
1366 4 : dom_namednode_iter(intern, 0, namednode, NULL, local, nsuri TSRMLS_CC);
1367 : }
1368 : /* }}} end dom_document_get_elements_by_tag_name_ns */
1369 :
1370 : /* {{{ proto DOMElement dom_document_get_element_by_id(string elementId) U
1371 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-getElBId
1372 : Since: DOM Level 2
1373 : */
1374 : PHP_FUNCTION(dom_document_get_element_by_id)
1375 7 : {
1376 7 : zval *id, *rv = NULL;
1377 : xmlDocPtr docp;
1378 : xmlAttrPtr attrp;
1379 : int ret, idname_len;
1380 : dom_object *intern;
1381 : char *idname;
1382 :
1383 7 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_document_class_entry, &idname, &idname_len, UG(utf8_conv)) == FAILURE) {
1384 0 : return;
1385 : }
1386 :
1387 7 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1388 :
1389 7 : attrp = xmlGetID(docp, (xmlChar *) idname);
1390 :
1391 11 : if (attrp && attrp->parent) {
1392 4 : DOM_RET_OBJ(rv, (xmlNodePtr) attrp->parent, &ret, intern);
1393 : } else {
1394 3 : RETVAL_NULL();
1395 : }
1396 :
1397 : }
1398 : /* }}} end dom_document_get_element_by_id */
1399 :
1400 : /* {{{ proto DOMNode dom_document_adopt_node(DOMNode source) U
1401 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-adoptNode
1402 : Since: DOM Level 3
1403 : */
1404 : PHP_FUNCTION(dom_document_adopt_node)
1405 0 : {
1406 0 : DOM_NOT_IMPLEMENTED();
1407 : }
1408 : /* }}} end dom_document_adopt_node */
1409 :
1410 : /* {{{ proto void dom_document_normalize_document() U
1411 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-normalizeDocument
1412 : Since: DOM Level 3
1413 : */
1414 : PHP_FUNCTION(dom_document_normalize_document)
1415 0 : {
1416 : zval *id;
1417 : xmlDocPtr docp;
1418 : dom_object *intern;
1419 :
1420 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) {
1421 0 : return;
1422 : }
1423 :
1424 0 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1425 :
1426 0 : dom_normalize((xmlNodePtr) docp TSRMLS_CC);
1427 : }
1428 : /* }}} end dom_document_normalize_document */
1429 :
1430 : /* {{{ proto DOMNode dom_document_rename_node(node n, string namespaceURI, string qualifiedName) U
1431 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Document3-renameNode
1432 : Since: DOM Level 3
1433 : */
1434 : PHP_FUNCTION(dom_document_rename_node)
1435 0 : {
1436 0 : DOM_NOT_IMPLEMENTED();
1437 : }
1438 : /* }}} end dom_document_rename_node */
1439 :
1440 : /* {{{ proto void DOMDocument::__construct([string version], [string encoding]) U */
1441 : PHP_METHOD(domdocument, __construct)
1442 222 : {
1443 :
1444 : zval *id;
1445 222 : xmlDoc *docp = NULL, *olddoc;
1446 : dom_object *intern;
1447 222 : char *encoding, *version = NULL;
1448 222 : int encoding_len = 0, version_len = 0, refcount;
1449 : zend_error_handling error_handling;
1450 :
1451 222 : zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
1452 222 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|s&s&", &id, dom_document_class_entry, &version, &version_len, UG(utf8_conv), &encoding, &encoding_len, UG(utf8_conv)) == FAILURE) {
1453 0 : zend_restore_error_handling(&error_handling TSRMLS_CC);
1454 0 : return;
1455 : }
1456 222 : zend_restore_error_handling(&error_handling TSRMLS_CC);
1457 :
1458 222 : docp = xmlNewDoc(version);
1459 :
1460 222 : if (!docp) {
1461 0 : php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
1462 0 : RETURN_FALSE;
1463 : }
1464 :
1465 222 : if (encoding_len > 0) {
1466 6 : docp->encoding = (const xmlChar*)xmlStrdup(encoding);
1467 : }
1468 :
1469 222 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
1470 222 : if (intern != NULL) {
1471 222 : olddoc = (xmlDocPtr) dom_object_get_node(intern);
1472 222 : if (olddoc != NULL) {
1473 0 : php_libxml_decrement_node_ptr((php_libxml_node_object *) intern TSRMLS_CC);
1474 0 : refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern TSRMLS_CC);
1475 0 : if (refcount != 0) {
1476 0 : olddoc->_private = NULL;
1477 : }
1478 : }
1479 222 : intern->document = NULL;
1480 222 : if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, docp TSRMLS_CC) == -1) {
1481 0 : RETURN_FALSE;
1482 : }
1483 222 : php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)docp, (void *)intern TSRMLS_CC);
1484 : }
1485 : }
1486 : /* }}} end DOMDocument::__construct */
1487 :
1488 : char *_dom_get_valid_file_path(char *source, char *resolved_path, int resolved_path_len TSRMLS_DC) /* {{{ */
1489 97 : {
1490 : xmlURI *uri;
1491 : xmlChar *escsource;
1492 : char *file_dest;
1493 97 : int isFileUri = 0;
1494 :
1495 97 : uri = xmlCreateURI();
1496 97 : escsource = xmlURIEscapeStr(source, ":");
1497 97 : xmlParseURIReference(uri, escsource);
1498 97 : xmlFree(escsource);
1499 :
1500 97 : if (uri->scheme != NULL) {
1501 : /* absolute file uris - libxml only supports localhost or empty host */
1502 1 : if (strncasecmp(source, "file:///",8) == 0) {
1503 0 : isFileUri = 1;
1504 : #ifdef PHP_WIN32
1505 : source += 8;
1506 : #else
1507 0 : source += 7;
1508 : #endif
1509 1 : } else if (strncasecmp(source, "file://localhost/",17) == 0) {
1510 0 : isFileUri = 1;
1511 : #ifdef PHP_WIN32
1512 : source += 17;
1513 : #else
1514 0 : source += 16;
1515 : #endif
1516 : }
1517 : }
1518 :
1519 97 : file_dest = source;
1520 :
1521 97 : if ((uri->scheme == NULL || isFileUri)) {
1522 : /* XXX possible buffer overflow if VCWD_REALPATH does not know size of resolved_path */
1523 96 : if (!VCWD_REALPATH(source, resolved_path) && !expand_filepath(source, resolved_path TSRMLS_CC)) {
1524 0 : xmlFreeURI(uri);
1525 0 : return NULL;
1526 : }
1527 96 : file_dest = resolved_path;
1528 : }
1529 :
1530 97 : xmlFreeURI(uri);
1531 :
1532 97 : return file_dest;
1533 : }
1534 : /* }}} */
1535 :
1536 : static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, int options TSRMLS_DC) /* {{{ */
1537 154 : {
1538 : xmlDocPtr ret;
1539 154 : xmlParserCtxtPtr ctxt = NULL;
1540 : dom_doc_propsptr doc_props;
1541 : dom_object *intern;
1542 154 : php_libxml_ref_obj *document = NULL;
1543 : int validate, recover, resolve_externals, keep_blanks, substitute_ent;
1544 : int resolved_path_len;
1545 154 : int old_error_reporting = 0;
1546 154 : char *directory=NULL, resolved_path[MAXPATHLEN];
1547 :
1548 154 : if (id != NULL) {
1549 154 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
1550 154 : document = intern->document;
1551 : }
1552 :
1553 154 : doc_props = dom_get_doc_props(document);
1554 154 : validate = doc_props->validateonparse;
1555 154 : resolve_externals = doc_props->resolveexternals;
1556 154 : keep_blanks = doc_props->preservewhitespace;
1557 154 : substitute_ent = doc_props->substituteentities;
1558 154 : recover = doc_props->recover;
1559 :
1560 154 : if (document == NULL) {
1561 0 : efree(doc_props);
1562 : }
1563 :
1564 154 : xmlInitParser();
1565 :
1566 154 : if (mode == DOM_LOAD_FILE) {
1567 93 : char *file_dest = _dom_get_valid_file_path(source, resolved_path, MAXPATHLEN TSRMLS_CC);
1568 93 : if (file_dest) {
1569 93 : ctxt = xmlCreateFileParserCtxt(file_dest);
1570 : }
1571 :
1572 : } else {
1573 61 : ctxt = xmlCreateDocParserCtxt(source);
1574 : }
1575 :
1576 154 : if (ctxt == NULL) {
1577 0 : return(NULL);
1578 : }
1579 :
1580 : /* If loading from memory, we need to set the base directory for the document */
1581 154 : if (mode != DOM_LOAD_FILE) {
1582 : #if HAVE_GETCWD
1583 61 : directory = VCWD_GETCWD(resolved_path, MAXPATHLEN);
1584 : #elif HAVE_GETWD
1585 : directory = VCWD_GETWD(resolved_path);
1586 : #endif
1587 61 : if (directory) {
1588 61 : if(ctxt->directory != NULL) {
1589 0 : xmlFree((char *) ctxt->directory);
1590 : }
1591 61 : resolved_path_len = strlen(resolved_path);
1592 61 : if (resolved_path[resolved_path_len - 1] != DEFAULT_SLASH) {
1593 61 : resolved_path[resolved_path_len] = DEFAULT_SLASH;
1594 61 : resolved_path[++resolved_path_len] = '\0';
1595 : }
1596 61 : ctxt->directory = (char *) xmlCanonicPath((const xmlChar *) resolved_path);
1597 : }
1598 : }
1599 :
1600 154 : ctxt->vctxt.error = php_libxml_ctx_error;
1601 154 : ctxt->vctxt.warning = php_libxml_ctx_warning;
1602 :
1603 154 : if (ctxt->sax != NULL) {
1604 154 : ctxt->sax->error = php_libxml_ctx_error;
1605 154 : ctxt->sax->warning = php_libxml_ctx_warning;
1606 : }
1607 :
1608 154 : if (validate && ! (options & XML_PARSE_DTDVALID)) {
1609 1 : options |= XML_PARSE_DTDVALID;
1610 : }
1611 154 : if (resolve_externals && ! (options & XML_PARSE_DTDATTR)) {
1612 3 : options |= XML_PARSE_DTDATTR;
1613 : }
1614 154 : if (substitute_ent && ! (options & XML_PARSE_NOENT)) {
1615 0 : options |= XML_PARSE_NOENT;
1616 : }
1617 154 : if (keep_blanks == 0 && ! (options & XML_PARSE_NOBLANKS)) {
1618 2 : options |= XML_PARSE_NOBLANKS;
1619 : }
1620 :
1621 154 : xmlCtxtUseOptions(ctxt, options);
1622 :
1623 154 : ctxt->recovery = recover;
1624 154 : if (recover) {
1625 0 : old_error_reporting = EG(error_reporting);
1626 0 : EG(error_reporting) = old_error_reporting | E_WARNING;
1627 : }
1628 :
1629 154 : xmlParseDocument(ctxt);
1630 :
1631 308 : if (ctxt->wellFormed || recover) {
1632 154 : ret = ctxt->myDoc;
1633 154 : if (ctxt->recovery) {
1634 0 : EG(error_reporting) = old_error_reporting;
1635 : }
1636 : /* If loading from memory, set the base reference uri for the document */
1637 154 : if (ret && ret->URL == NULL && ctxt->directory != NULL) {
1638 61 : ret->URL = xmlStrdup(ctxt->directory);
1639 : }
1640 : } else {
1641 0 : ret = NULL;
1642 0 : xmlFreeDoc(ctxt->myDoc);
1643 0 : ctxt->myDoc = NULL;
1644 : }
1645 :
1646 154 : xmlFreeParserCtxt(ctxt);
1647 :
1648 154 : return(ret);
1649 : }
1650 : /* }}} */
1651 :
1652 : /* {{{ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) */
1653 154 : static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) {
1654 154 : zval *id, *rv = NULL;
1655 154 : xmlDoc *docp = NULL, *newdoc;
1656 : dom_doc_propsptr doc_prop;
1657 : dom_object *intern;
1658 : zstr source;
1659 : int source_len, refcount, ret;
1660 154 : zend_uchar source_type = IS_STRING;
1661 154 : long options = 0;
1662 :
1663 154 : id = getThis();
1664 154 : if (id != NULL && ! instanceof_function(Z_OBJCE_P(id), dom_document_class_entry TSRMLS_CC)) {
1665 0 : id = NULL;
1666 : }
1667 :
1668 154 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|l", &source, &source_len, &source_type, &options) == FAILURE) {
1669 0 : return;
1670 : }
1671 :
1672 154 : if (!source_len) {
1673 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string supplied as input");
1674 0 : RETURN_FALSE;
1675 : }
1676 :
1677 154 : if (source_type == IS_UNICODE) {
1678 130 : if (mode == DOM_LOAD_FILE) {
1679 93 : if (php_stream_path_encode(NULL, &source.s, &source_len, source.u, source_len, REPORT_ERRORS, NULL) == FAILURE) {
1680 0 : RETURN_FALSE;
1681 : }
1682 : } else {
1683 37 : source.s = php_libxml_unicode_to_string(source.u, source_len, &source_len TSRMLS_CC);
1684 : }
1685 : }
1686 :
1687 154 : newdoc = dom_document_parser(id, mode, source.s, options TSRMLS_CC);
1688 :
1689 154 : if (source_type == IS_UNICODE) {
1690 130 : efree(source.s);
1691 : }
1692 :
1693 154 : if (!newdoc)
1694 0 : RETURN_FALSE;
1695 :
1696 154 : if (id != NULL) {
1697 154 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
1698 154 : if (intern != NULL) {
1699 154 : docp = (xmlDocPtr) dom_object_get_node(intern);
1700 154 : doc_prop = NULL;
1701 154 : if (docp != NULL) {
1702 154 : php_libxml_decrement_node_ptr((php_libxml_node_object *) intern TSRMLS_CC);
1703 154 : doc_prop = intern->document->doc_props;
1704 154 : intern->document->doc_props = NULL;
1705 154 : refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern TSRMLS_CC);
1706 154 : if (refcount != 0) {
1707 4 : docp->_private = NULL;
1708 : }
1709 : }
1710 154 : intern->document = NULL;
1711 154 : if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, newdoc TSRMLS_CC) == -1) {
1712 0 : RETURN_FALSE;
1713 : }
1714 154 : intern->document->doc_props = doc_prop;
1715 : }
1716 :
1717 154 : php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)newdoc, (void *)intern TSRMLS_CC);
1718 :
1719 154 : RETURN_TRUE;
1720 : } else {
1721 0 : DOM_RET_OBJ(rv, (xmlNodePtr) newdoc, &ret, NULL);
1722 : }
1723 : }
1724 : /* }}} end dom_parser_document */
1725 :
1726 : /* {{{ proto DOMNode dom_document_load(string source [, int options]) U
1727 : URL: http://www.w3.org/TR/DOM-Level-3-LS/load-save.html#LS-DocumentLS-load
1728 : Since: DOM Level 3
1729 : */
1730 : PHP_METHOD(domdocument, load)
1731 93 : {
1732 93 : dom_parse_document(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_FILE);
1733 93 : }
1734 : /* }}} end dom_document_load */
1735 :
1736 : /* {{{ proto DOMNode dom_document_loadxml(string source [, int options]) U
1737 : URL: http://www.w3.org/TR/DOM-Level-3-LS/load-save.html#LS-DocumentLS-loadXML
1738 : Since: DOM Level 3
1739 : */
1740 : PHP_METHOD(domdocument, loadXML)
1741 61 : {
1742 61 : dom_parse_document(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_STRING);
1743 61 : }
1744 : /* }}} end dom_document_loadxml */
1745 :
1746 : /* {{{ proto int dom_document_save(string file) U
1747 : Convenience method to save to file
1748 : */
1749 : PHP_FUNCTION(dom_document_save)
1750 3 : {
1751 : zval *id;
1752 : xmlDoc *docp;
1753 3 : int file_len = 0, bytes, format, saveempty = 0;
1754 : dom_object *intern;
1755 : dom_doc_propsptr doc_props;
1756 : char *file;
1757 : zend_uchar file_type;
1758 3 : long options = 0;
1759 :
1760 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ot|l", &id, dom_document_class_entry, &file, &file_len, &file_type, &options) == FAILURE) {
1761 0 : return;
1762 : }
1763 :
1764 3 : if (file_len == 0) {
1765 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Filename");
1766 0 : RETURN_FALSE;
1767 : }
1768 :
1769 3 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1770 :
1771 : /* encoding handled by property on doc */
1772 :
1773 3 : doc_props = dom_get_doc_props(intern->document);
1774 3 : format = doc_props->formatoutput;
1775 3 : if (options & LIBXML_SAVE_NOEMPTYTAG) {
1776 0 : saveempty = xmlSaveNoEmptyTags;
1777 0 : xmlSaveNoEmptyTags = 1;
1778 : }
1779 :
1780 3 : if (file_type == IS_UNICODE) {
1781 3 : if (php_stream_path_encode(NULL, &file, &file_len, (UChar*)file, file_len, REPORT_ERRORS, NULL) == FAILURE) {
1782 0 : RETURN_FALSE;
1783 : }
1784 : }
1785 :
1786 3 : bytes = xmlSaveFormatFileEnc(file, docp, NULL, format);
1787 :
1788 3 : if (file_type == IS_UNICODE) {
1789 3 : efree(file);
1790 : }
1791 :
1792 3 : if (options & LIBXML_SAVE_NOEMPTYTAG) {
1793 0 : xmlSaveNoEmptyTags = saveempty;
1794 : }
1795 3 : if (bytes == -1) {
1796 0 : RETURN_FALSE;
1797 : }
1798 3 : RETURN_LONG(bytes);
1799 : }
1800 : /* }}} end dom_document_save */
1801 :
1802 : /* {{{ proto string dom_document_savexml([node n]) U
1803 : URL: http://www.w3.org/TR/DOM-Level-3-LS/load-save.html#LS-DocumentLS-saveXML
1804 : Since: DOM Level 3
1805 : */
1806 : PHP_FUNCTION(dom_document_savexml)
1807 48 : {
1808 48 : zval *id, *nodep = NULL;
1809 : xmlDoc *docp;
1810 : xmlNode *node;
1811 : xmlBufferPtr buf;
1812 : xmlChar *mem;
1813 : dom_object *intern, *nodeobj;
1814 : dom_doc_propsptr doc_props;
1815 48 : int size, format, saveempty = 0;
1816 48 : long options = 0;
1817 :
1818 48 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|O!l", &id, dom_document_class_entry, &nodep, dom_node_class_entry, &options) == FAILURE) {
1819 0 : return;
1820 : }
1821 :
1822 48 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1823 :
1824 48 : doc_props = dom_get_doc_props(intern->document);
1825 48 : format = doc_props->formatoutput;
1826 :
1827 48 : if (nodep != NULL) {
1828 : /* Dump contents of Node */
1829 3 : DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
1830 3 : if (node->doc != docp) {
1831 0 : php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
1832 0 : RETURN_FALSE;
1833 : }
1834 3 : buf = xmlBufferCreate();
1835 3 : if (!buf) {
1836 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch buffer");
1837 0 : RETURN_FALSE;
1838 : }
1839 3 : if (options & LIBXML_SAVE_NOEMPTYTAG) {
1840 0 : saveempty = xmlSaveNoEmptyTags;
1841 0 : xmlSaveNoEmptyTags = 1;
1842 : }
1843 3 : xmlNodeDump(buf, docp, node, 0, format);
1844 3 : if (options & LIBXML_SAVE_NOEMPTYTAG) {
1845 0 : xmlSaveNoEmptyTags = saveempty;
1846 : }
1847 3 : mem = (xmlChar*) xmlBufferContent(buf);
1848 3 : if (!mem) {
1849 0 : xmlBufferFree(buf);
1850 0 : RETURN_FALSE;
1851 : }
1852 3 : RETVAL_STRING(mem, 1);
1853 3 : xmlBufferFree(buf);
1854 : } else {
1855 45 : if (options & LIBXML_SAVE_NOEMPTYTAG) {
1856 0 : saveempty = xmlSaveNoEmptyTags;
1857 0 : xmlSaveNoEmptyTags = 1;
1858 : }
1859 : /* Encoding is handled from the encoding property set on the document */
1860 45 : xmlDocDumpFormatMemory(docp, &mem, &size, format);
1861 45 : if (options & LIBXML_SAVE_NOEMPTYTAG) {
1862 0 : xmlSaveNoEmptyTags = saveempty;
1863 : }
1864 45 : if (!size) {
1865 0 : RETURN_FALSE;
1866 : }
1867 45 : RETVAL_STRINGL(mem, size, 1);
1868 45 : xmlFree(mem);
1869 : }
1870 : }
1871 : /* }}} end dom_document_savexml */
1872 :
1873 : static xmlNodePtr php_dom_free_xinclude_node(xmlNodePtr cur TSRMLS_DC) /* {{{ */
1874 6 : {
1875 : xmlNodePtr xincnode;
1876 :
1877 6 : xincnode = cur;
1878 6 : cur = cur->next;
1879 6 : xmlUnlinkNode(xincnode);
1880 6 : php_libxml_node_free_resource(xincnode TSRMLS_CC);
1881 :
1882 6 : return cur;
1883 : }
1884 : /* }}} */
1885 :
1886 : static void php_dom_remove_xinclude_nodes(xmlNodePtr cur TSRMLS_DC) /* {{{ */
1887 14 : {
1888 60 : while(cur) {
1889 32 : if (cur->type == XML_XINCLUDE_START) {
1890 3 : cur = php_dom_free_xinclude_node(cur TSRMLS_CC);
1891 :
1892 : /* XML_XINCLUDE_END node will be a sibling of XML_XINCLUDE_START */
1893 9 : while(cur && cur->type != XML_XINCLUDE_END) {
1894 : /* remove xinclude processing nodes from recursive xincludes */
1895 3 : if (cur->type == XML_ELEMENT_NODE) {
1896 3 : php_dom_remove_xinclude_nodes(cur->children TSRMLS_CC);
1897 : }
1898 3 : cur = cur->next;
1899 : }
1900 :
1901 3 : if (cur && cur->type == XML_XINCLUDE_END) {
1902 3 : cur = php_dom_free_xinclude_node(cur TSRMLS_CC);
1903 : }
1904 : } else {
1905 29 : if (cur->type == XML_ELEMENT_NODE) {
1906 10 : php_dom_remove_xinclude_nodes(cur->children TSRMLS_CC);
1907 : }
1908 29 : cur = cur->next;
1909 : }
1910 : }
1911 14 : }
1912 : /* }}} */
1913 :
1914 : /* {{{ proto int dom_document_xinclude([int options]) U
1915 : Substitutues xincludes in a DomDocument */
1916 : PHP_FUNCTION(dom_document_xinclude)
1917 1 : {
1918 : zval *id;
1919 : xmlDoc *docp;
1920 : xmlNodePtr root;
1921 1 : long flags = 0;
1922 : int err;
1923 : dom_object *intern;
1924 :
1925 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l", &id, dom_document_class_entry, &flags) == FAILURE) {
1926 0 : return;
1927 : }
1928 :
1929 1 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1930 :
1931 1 : err = xmlXIncludeProcessFlags(docp, flags);
1932 :
1933 : /* XML_XINCLUDE_START and XML_XINCLUDE_END nodes need to be removed as these
1934 : are added via xmlXIncludeProcess to mark beginning and ending of xincluded document
1935 : but are not wanted in resulting document - must be done even if err as it could fail after
1936 : having processed some xincludes */
1937 1 : root = (xmlNodePtr) docp->children;
1938 2 : while(root && root->type != XML_ELEMENT_NODE && root->type != XML_XINCLUDE_START) {
1939 0 : root = root->next;
1940 : }
1941 1 : if (root) {
1942 1 : php_dom_remove_xinclude_nodes(root TSRMLS_CC);
1943 : }
1944 :
1945 1 : if (err) {
1946 1 : RETVAL_LONG(err);
1947 : } else {
1948 0 : RETVAL_FALSE;
1949 : }
1950 :
1951 : }
1952 : /* }}} */
1953 :
1954 : /* {{{ proto boolean dom_document_validate() U
1955 : Since: DOM extended
1956 : */
1957 : PHP_FUNCTION(dom_document_validate)
1958 3 : {
1959 : zval *id;
1960 : xmlDoc *docp;
1961 : dom_object *intern;
1962 : xmlValidCtxt *cvp;
1963 :
1964 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) {
1965 1 : return;
1966 : }
1967 :
1968 2 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
1969 :
1970 2 : cvp = xmlNewValidCtxt();
1971 :
1972 2 : cvp->userData = NULL;
1973 2 : cvp->error = (xmlValidityErrorFunc) php_libxml_error_handler;
1974 2 : cvp->warning = (xmlValidityErrorFunc) php_libxml_error_handler;
1975 :
1976 2 : if (xmlValidateDocument(cvp, docp)) {
1977 2 : RETVAL_TRUE;
1978 : } else {
1979 0 : RETVAL_FALSE;
1980 : }
1981 :
1982 2 : xmlFreeValidCtxt(cvp);
1983 :
1984 : }
1985 :
1986 :
1987 : #if defined(LIBXML_SCHEMAS_ENABLED)
1988 : static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type)
1989 11 : {
1990 : zval *id;
1991 : xmlDoc *docp;
1992 : dom_object *intern;
1993 11 : zstr source = NULL_ZSTR;
1994 11 : char *valid_file = NULL;
1995 11 : int source_len = 0;
1996 : xmlSchemaParserCtxtPtr parser;
1997 : xmlSchemaPtr sptr;
1998 : xmlSchemaValidCtxtPtr vptr;
1999 : int is_valid;
2000 : char resolved_path[MAXPATHLEN + 1];
2001 11 : zend_uchar source_type = IS_STRING;
2002 :
2003 11 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ot", &id, dom_document_class_entry, &source, &source_len, &source_type) == FAILURE) {
2004 2 : return;
2005 : }
2006 :
2007 9 : if (source_len == 0) {
2008 2 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema source");
2009 2 : RETURN_FALSE;
2010 : }
2011 :
2012 7 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
2013 :
2014 7 : switch (type) {
2015 : case DOM_LOAD_FILE:
2016 4 : if (source_type == IS_UNICODE) {
2017 4 : if (php_stream_path_encode(NULL, &source.s, &source_len, source.u, source_len, REPORT_ERRORS, NULL) == FAILURE) {
2018 0 : RETURN_FALSE;
2019 : }
2020 : }
2021 :
2022 4 : valid_file = _dom_get_valid_file_path(source.s, resolved_path, MAXPATHLEN TSRMLS_CC);
2023 4 : if (!valid_file) {
2024 0 : if (source_type == IS_UNICODE) {
2025 0 : efree(source.s);
2026 : }
2027 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema file source");
2028 0 : RETURN_FALSE;
2029 : }
2030 4 : parser = xmlSchemaNewParserCtxt(valid_file);
2031 :
2032 4 : if (source_type == IS_UNICODE) {
2033 4 : efree(source.s);
2034 : }
2035 4 : break;
2036 : case DOM_LOAD_STRING:
2037 3 : if (source_type == IS_UNICODE) {
2038 1 : source.s = php_libxml_unicode_to_string(source.u, source_len, &source_len TSRMLS_CC);
2039 : }
2040 3 : parser = xmlSchemaNewMemParserCtxt(source.s, source_len);
2041 : /* If loading from memory, we need to set the base directory for the document
2042 : but it is not apparent how to do that for schema's */
2043 3 : if (source_type == IS_UNICODE) {
2044 1 : efree(source.s);
2045 : }
2046 3 : break;
2047 : default:
2048 0 : return;
2049 : }
2050 :
2051 7 : xmlSchemaSetParserErrors(parser,
2052 : (xmlSchemaValidityErrorFunc) php_libxml_error_handler,
2053 : (xmlSchemaValidityWarningFunc) php_libxml_error_handler,
2054 : parser);
2055 7 : sptr = xmlSchemaParse(parser);
2056 7 : xmlSchemaFreeParserCtxt(parser);
2057 7 : if (!sptr) {
2058 3 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema");
2059 3 : RETURN_FALSE;
2060 : }
2061 :
2062 4 : docp = (xmlDocPtr) dom_object_get_node(intern);
2063 :
2064 4 : vptr = xmlSchemaNewValidCtxt(sptr);
2065 4 : if (!vptr) {
2066 0 : xmlSchemaFree(sptr);
2067 0 : php_error(E_ERROR, "Invalid Schema Validation Context");
2068 0 : RETURN_FALSE;
2069 : }
2070 :
2071 4 : xmlSchemaSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr);
2072 4 : is_valid = xmlSchemaValidateDoc(vptr, docp);
2073 4 : xmlSchemaFree(sptr);
2074 4 : xmlSchemaFreeValidCtxt(vptr);
2075 :
2076 4 : if (is_valid == 0) {
2077 2 : RETURN_TRUE;
2078 : } else {
2079 2 : RETURN_FALSE;
2080 : }
2081 : }
2082 : /* }}} */
2083 :
2084 : /* {{{ proto boolean dom_document_schema_validate_file(string filename) U */
2085 : PHP_FUNCTION(dom_document_schema_validate_file)
2086 6 : {
2087 6 : _dom_document_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_FILE);
2088 6 : }
2089 : /* }}} end dom_document_schema_validate_file */
2090 :
2091 : /* {{{ proto boolean dom_document_schema_validate(string source) U */
2092 : PHP_FUNCTION(dom_document_schema_validate_xml)
2093 5 : {
2094 5 : _dom_document_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_STRING);
2095 5 : }
2096 : /* }}} end dom_document_schema_validate */
2097 :
2098 : static void _dom_document_relaxNG_validate(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
2099 0 : {
2100 : zval *id;
2101 : xmlDoc *docp;
2102 : dom_object *intern;
2103 0 : zstr source = NULL_ZSTR;
2104 0 : char *valid_file = NULL;
2105 0 : int source_len = 0;
2106 : xmlRelaxNGParserCtxtPtr parser;
2107 : xmlRelaxNGPtr sptr;
2108 : xmlRelaxNGValidCtxtPtr vptr;
2109 : int is_valid;
2110 : char resolved_path[MAXPATHLEN + 1];
2111 0 : zend_uchar source_type = IS_STRING;
2112 :
2113 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ot", &id, dom_document_class_entry, &source, &source_len, &source_type) == FAILURE) {
2114 0 : return;
2115 : }
2116 :
2117 0 : if (source_len == 0) {
2118 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema source");
2119 0 : RETURN_FALSE;
2120 : }
2121 :
2122 0 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
2123 :
2124 0 : switch (type) {
2125 : case DOM_LOAD_FILE:
2126 0 : if (source_type == IS_UNICODE) {
2127 0 : if (php_stream_path_encode(NULL, &source.s, &source_len, source.u, source_len, REPORT_ERRORS, NULL) == FAILURE) {
2128 0 : RETURN_FALSE;
2129 : }
2130 : }
2131 0 : valid_file = _dom_get_valid_file_path(source.s, resolved_path, MAXPATHLEN TSRMLS_CC);
2132 0 : if (!valid_file) {
2133 0 : if (source_type == IS_UNICODE) {
2134 0 : efree(source.s);
2135 : }
2136 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid RelaxNG file source");
2137 0 : RETURN_FALSE;
2138 : }
2139 0 : parser = xmlRelaxNGNewParserCtxt(valid_file);
2140 0 : if (source_type == IS_UNICODE) {
2141 0 : efree(source.s);
2142 : }
2143 0 : break;
2144 : case DOM_LOAD_STRING:
2145 0 : if (source_type == IS_UNICODE) {
2146 0 : source.s = php_libxml_unicode_to_string(source.u, source_len, &source_len TSRMLS_CC);
2147 : }
2148 0 : parser = xmlRelaxNGNewMemParserCtxt(source.s, source_len);
2149 : /* If loading from memory, we need to set the base directory for the document
2150 : but it is not apparent how to do that for schema's */
2151 0 : if (source_type == IS_UNICODE) {
2152 0 : efree(source.s);
2153 : }
2154 0 : break;
2155 : default:
2156 0 : return;
2157 : }
2158 :
2159 0 : xmlRelaxNGSetParserErrors(parser,
2160 : (xmlRelaxNGValidityErrorFunc) php_libxml_error_handler,
2161 : (xmlRelaxNGValidityWarningFunc) php_libxml_error_handler,
2162 : parser);
2163 0 : sptr = xmlRelaxNGParse(parser);
2164 0 : xmlRelaxNGFreeParserCtxt(parser);
2165 0 : if (!sptr) {
2166 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid RelaxNG");
2167 0 : RETURN_FALSE;
2168 : }
2169 :
2170 0 : docp = (xmlDocPtr) dom_object_get_node(intern);
2171 :
2172 0 : vptr = xmlRelaxNGNewValidCtxt(sptr);
2173 0 : if (!vptr) {
2174 0 : xmlRelaxNGFree(sptr);
2175 0 : php_error(E_ERROR, "Invalid RelaxNG Validation Context");
2176 0 : RETURN_FALSE;
2177 : }
2178 :
2179 0 : xmlRelaxNGSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr);
2180 0 : is_valid = xmlRelaxNGValidateDoc(vptr, docp);
2181 0 : xmlRelaxNGFree(sptr);
2182 0 : xmlRelaxNGFreeValidCtxt(vptr);
2183 :
2184 0 : if (is_valid == 0) {
2185 0 : RETURN_TRUE;
2186 : } else {
2187 0 : RETURN_FALSE;
2188 : }
2189 : }
2190 : /* }}} */
2191 :
2192 : /* {{{ proto boolean dom_document_relaxNG_validate_file(string filename) U */
2193 : PHP_FUNCTION(dom_document_relaxNG_validate_file)
2194 0 : {
2195 0 : _dom_document_relaxNG_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_FILE);
2196 0 : }
2197 : /* }}} end dom_document_relaxNG_validate_file */
2198 :
2199 : /* {{{ proto boolean dom_document_relaxNG_validate_xml(string source) U */
2200 : PHP_FUNCTION(dom_document_relaxNG_validate_xml)
2201 0 : {
2202 0 : _dom_document_relaxNG_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_STRING);
2203 0 : }
2204 : /* }}} end dom_document_relaxNG_validate_xml */
2205 :
2206 : #endif
2207 :
2208 : #if defined(LIBXML_HTML_ENABLED)
2209 :
2210 : static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
2211 4 : {
2212 4 : zval *id, *rv = NULL;
2213 4 : xmlDoc *docp = NULL, *newdoc;
2214 : dom_object *intern;
2215 : dom_doc_propsptr doc_prop;
2216 : zstr source;
2217 : int source_len, refcount, ret;
2218 : htmlParserCtxtPtr ctxt;
2219 4 : zend_uchar source_type = IS_STRING;
2220 :
2221 4 : id = getThis();
2222 :
2223 4 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &source, &source_len, &source_type) == FAILURE) {
2224 0 : return;
2225 : }
2226 :
2227 4 : if (!source_len) {
2228 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string supplied as input");
2229 0 : RETURN_FALSE;
2230 : }
2231 :
2232 4 : if (mode == DOM_LOAD_FILE) {
2233 3 : if (source_type == IS_UNICODE) {
2234 3 : if (php_stream_path_encode(NULL, &source.s, &source_len, source.u, source_len, REPORT_ERRORS, NULL) == FAILURE) {
2235 0 : RETURN_FALSE;
2236 : }
2237 : }
2238 :
2239 3 : ctxt = htmlCreateFileParserCtxt(source.s, NULL);
2240 :
2241 3 : if (source_type == IS_UNICODE) {
2242 3 : efree(source.s);
2243 : }
2244 : } else {
2245 1 : if (source_type == IS_UNICODE) {
2246 1 : source.s = php_libxml_unicode_to_string(source.u, source_len, &source_len TSRMLS_CC);
2247 : }
2248 :
2249 1 : ctxt = htmlCreateMemoryParserCtxt(source.s, source_len);
2250 :
2251 1 : if (source_type == IS_UNICODE) {
2252 1 : efree(source.s);
2253 : }
2254 : }
2255 :
2256 4 : if (!ctxt) {
2257 0 : RETURN_FALSE;
2258 : }
2259 :
2260 4 : ctxt->vctxt.error = php_libxml_ctx_error;
2261 4 : ctxt->vctxt.warning = php_libxml_ctx_warning;
2262 4 : if (ctxt->sax != NULL) {
2263 4 : ctxt->sax->error = php_libxml_ctx_error;
2264 4 : ctxt->sax->warning = php_libxml_ctx_warning;
2265 : }
2266 4 : htmlParseDocument(ctxt);
2267 4 : newdoc = ctxt->myDoc;
2268 4 : htmlFreeParserCtxt(ctxt);
2269 :
2270 4 : if (!newdoc)
2271 0 : RETURN_FALSE;
2272 :
2273 4 : if (id != NULL && instanceof_function(Z_OBJCE_P(id), dom_document_class_entry TSRMLS_CC)) {
2274 4 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
2275 4 : if (intern != NULL) {
2276 4 : docp = (xmlDocPtr) dom_object_get_node(intern);
2277 4 : doc_prop = NULL;
2278 4 : if (docp != NULL) {
2279 4 : php_libxml_decrement_node_ptr((php_libxml_node_object *) intern TSRMLS_CC);
2280 4 : doc_prop = intern->document->doc_props;
2281 4 : intern->document->doc_props = NULL;
2282 4 : refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern TSRMLS_CC);
2283 4 : if (refcount != 0) {
2284 2 : docp->_private = NULL;
2285 : }
2286 : }
2287 4 : intern->document = NULL;
2288 4 : if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, newdoc TSRMLS_CC) == -1) {
2289 0 : RETURN_FALSE;
2290 : }
2291 4 : intern->document->doc_props = doc_prop;
2292 : }
2293 :
2294 4 : php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)newdoc, (void *)intern TSRMLS_CC);
2295 :
2296 4 : RETURN_TRUE;
2297 : } else {
2298 0 : DOM_RET_OBJ(rv, (xmlNodePtr) newdoc, &ret, NULL);
2299 : }
2300 : }
2301 : /* }}} */
2302 :
2303 : /* {{{ proto DOMNode dom_document_load_html_file(string source) U
2304 : Since: DOM extended
2305 : */
2306 : PHP_METHOD(domdocument, loadHTMLFile)
2307 3 : {
2308 3 : dom_load_html(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_FILE);
2309 3 : }
2310 : /* }}} end dom_document_load_html_file */
2311 :
2312 : /* {{{ proto DOMNode dom_document_load_html(string source) U
2313 : Since: DOM extended
2314 : */
2315 : PHP_METHOD(domdocument, loadHTML)
2316 1 : {
2317 1 : dom_load_html(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_STRING);
2318 1 : }
2319 : /* }}} end dom_document_load_html */
2320 :
2321 : /* {{{ proto int dom_document_save_html_file(string file) U
2322 : Convenience method to save to file as html
2323 : */
2324 : PHP_FUNCTION(dom_document_save_html_file)
2325 5 : {
2326 : zval *id;
2327 : xmlDoc *docp;
2328 : int file_len, bytes, format;
2329 : dom_object *intern;
2330 : dom_doc_propsptr doc_props;
2331 : char *file;
2332 : zend_uchar file_type;
2333 :
2334 5 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ot", &id, dom_document_class_entry, &file, &file_len, &file_type) == FAILURE) {
2335 1 : return;
2336 : }
2337 :
2338 4 : if (file_len == 0) {
2339 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Filename");
2340 1 : RETURN_FALSE;
2341 : }
2342 :
2343 3 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
2344 :
2345 3 : if (file_type == IS_UNICODE) {
2346 3 : if (php_stream_path_encode(NULL, &file, &file_len, (UChar*)file, file_len, REPORT_ERRORS, NULL) == FAILURE) {
2347 0 : RETURN_FALSE;
2348 : }
2349 : }
2350 :
2351 : /* encoding handled by property on doc */
2352 :
2353 3 : doc_props = dom_get_doc_props(intern->document);
2354 3 : format = doc_props->formatoutput;
2355 :
2356 3 : bytes = htmlSaveFileFormat(file, docp, NULL, format);
2357 :
2358 3 : if (file_type == IS_UNICODE) {
2359 3 : efree(file);
2360 : }
2361 :
2362 3 : if (bytes == -1) {
2363 0 : RETURN_FALSE;
2364 : }
2365 3 : RETURN_LONG(bytes);
2366 : }
2367 : /* }}} end dom_document_save_html_file */
2368 :
2369 : /* {{{ proto string dom_document_save_html() U
2370 : Convenience method to output as html
2371 : */
2372 : PHP_FUNCTION(dom_document_save_html)
2373 6 : {
2374 : zval *id;
2375 : xmlDoc *docp;
2376 : dom_object *intern;
2377 : xmlChar *mem;
2378 : int size, format;
2379 : dom_doc_propsptr doc_props;
2380 :
2381 6 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) {
2382 1 : return;
2383 : }
2384 :
2385 5 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
2386 :
2387 : #if LIBXML_VERSION >= 20623
2388 5 : doc_props = dom_get_doc_props(intern->document);
2389 5 : format = doc_props->formatoutput;
2390 :
2391 5 : htmlDocDumpMemoryFormat(docp, &mem, &size, format);
2392 : #else
2393 : htmlDocDumpMemory(docp, &mem, &size);
2394 : #endif
2395 :
2396 5 : if (!size) {
2397 0 : if (mem)
2398 0 : xmlFree(mem);
2399 0 : RETURN_FALSE;
2400 : }
2401 5 : RETVAL_STRINGL(mem, size, 1);
2402 5 : xmlFree(mem);
2403 : }
2404 : /* }}} end dom_document_save_html */
2405 :
2406 : #endif /* defined(LIBXML_HTML_ENABLED) */
2407 :
2408 : /* {{{ proto boolean DOMDocument::registerNodeClass(string baseclass, string extendedclass) U
2409 : Register extended class used to create base node type */
2410 : PHP_METHOD(domdocument, registerNodeClass)
2411 3 : {
2412 : zval *id;
2413 : xmlDoc *docp;
2414 3 : char *baseclass = NULL, *extendedclass = NULL;
2415 3 : int baseclass_len = 0, extendedclass_len = 0;
2416 3 : zend_class_entry *basece = NULL, *ce = NULL;
2417 : dom_object *intern;
2418 :
2419 3 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss!", &id, dom_document_class_entry, &baseclass, &baseclass_len, &extendedclass, &extendedclass_len) == FAILURE) {
2420 0 : return;
2421 : }
2422 :
2423 3 : if (baseclass_len) {
2424 : zend_class_entry **pce;
2425 3 : if (zend_lookup_class(baseclass, baseclass_len, &pce TSRMLS_CC) == FAILURE) {
2426 0 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s does not exist", baseclass);
2427 0 : return;
2428 : }
2429 3 : basece = *pce;
2430 : }
2431 :
2432 3 : if (basece == NULL || ! instanceof_function(basece, dom_node_class_entry TSRMLS_CC)) {
2433 0 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s is not derived from DOMNode", baseclass);
2434 0 : return;
2435 : }
2436 :
2437 3 : if (extendedclass_len) {
2438 : zend_class_entry **pce;
2439 2 : if (zend_lookup_class(extendedclass, extendedclass_len, &pce TSRMLS_CC) == FAILURE) {
2440 0 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s does not exist", extendedclass);
2441 : }
2442 2 : ce = *pce;
2443 : }
2444 :
2445 3 : if (ce == NULL || instanceof_function(ce, basece TSRMLS_CC)) {
2446 :
2447 3 : DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
2448 :
2449 3 : if (dom_set_doc_classmap(intern->document, basece, ce TSRMLS_CC) == FAILURE) {
2450 0 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s could not be registered", extendedclass);
2451 : }
2452 3 : RETURN_TRUE;
2453 : } else {
2454 0 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s is not derived from %s", extendedclass, baseclass);
2455 : }
2456 :
2457 0 : RETURN_FALSE;
2458 : }
2459 : /* }}} */
2460 :
2461 : #endif /* HAVE_LIBXML && HAVE_DOM */
2462 :
2463 : /*
2464 : * Local variables:
2465 : * tab-width: 4
2466 : * c-basic-offset: 4
2467 : * End:
2468 : * vim600: noet sw=4 ts=4 fdm=marker
2469 : * vim<600: noet sw=4 ts=4
2470 : */
|