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