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