xref: /PHP-8.2/ext/dom/entityreference.c (revision e878b9f3)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Christian Stocker <chregu@php.net>                          |
14    |          Rob Richards <rrichards@php.net>                            |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php.h"
23 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 #include "dom_properties.h"
26 
27 /*
28 * class DOMEntityReference extends DOMNode
29 *
30 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-11C98490
31 * Since:
32 */
33 
34 /* {{{ */
PHP_METHOD(DOMEntityReference,__construct)35 PHP_METHOD(DOMEntityReference, __construct)
36 {
37 	xmlNode *node;
38 	xmlNodePtr oldnode = NULL;
39 	dom_object *intern;
40 	char *name;
41 	size_t name_len, name_valid;
42 
43 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
44 		RETURN_THROWS();
45 	}
46 
47 	name_valid = xmlValidateName((xmlChar *) name, 0);
48 	if (name_valid != 0) {
49 		php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
50 		RETURN_THROWS();
51 	}
52 
53 	node = xmlNewReference(NULL, (xmlChar *) name);
54 
55 	if (!node) {
56 		php_dom_throw_error(INVALID_STATE_ERR, 1);
57 		RETURN_THROWS();
58 	}
59 
60 	intern = Z_DOMOBJ_P(ZEND_THIS);
61 	oldnode = dom_object_get_node(intern);
62 	if (oldnode != NULL) {
63 		php_libxml_node_decrement_resource((php_libxml_node_object *)intern);
64 	}
65 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, node, (void *)intern);
66 }
67 /* }}} end DOMEntityReference::__construct */
68 
69 /* The following property handlers are necessary because of special lifetime management with entities and entity
70  * references. The issue is that entity references hold a reference to an entity declaration, but don't
71  * register that reference anywhere. When the entity declaration disappears we have no way of notifying the
72  * entity references. Override the property handlers for the declaration-accessing properties to fix this problem. */
73 
dom_entity_reference_fetch_and_sync_declaration(xmlNodePtr reference)74 xmlEntityPtr dom_entity_reference_fetch_and_sync_declaration(xmlNodePtr reference)
75 {
76 	xmlEntityPtr entity = xmlGetDocEntity(reference->doc, reference->name);
77 	reference->children = (xmlNodePtr) entity;
78 	reference->last = (xmlNodePtr) entity;
79 	reference->content = entity ? entity->content : NULL;
80 	return entity;
81 }
82 
dom_entity_reference_child_read(dom_object * obj,zval * retval)83 int dom_entity_reference_child_read(dom_object *obj, zval *retval)
84 {
85 	xmlNodePtr nodep = dom_object_get_node(obj);
86 
87 	if (nodep == NULL) {
88 		php_dom_throw_error(INVALID_STATE_ERR, true);
89 		return FAILURE;
90 	}
91 
92 	xmlEntityPtr entity = dom_entity_reference_fetch_and_sync_declaration(nodep);
93 	if (entity == NULL) {
94 		ZVAL_NULL(retval);
95 		return SUCCESS;
96 	}
97 
98 	php_dom_create_object((xmlNodePtr) entity, retval, obj);
99 	return SUCCESS;
100 }
101 
dom_entity_reference_text_content_read(dom_object * obj,zval * retval)102 int dom_entity_reference_text_content_read(dom_object *obj, zval *retval)
103 {
104 	xmlNodePtr nodep = dom_object_get_node(obj);
105 
106 	if (nodep == NULL) {
107 		php_dom_throw_error(INVALID_STATE_ERR, true);
108 		return FAILURE;
109 	}
110 
111 	dom_entity_reference_fetch_and_sync_declaration(nodep);
112 	return dom_node_text_content_read(obj, retval);
113 }
114 
dom_entity_reference_child_nodes_read(dom_object * obj,zval * retval)115 int dom_entity_reference_child_nodes_read(dom_object *obj, zval *retval)
116 {
117 	xmlNodePtr nodep = dom_object_get_node(obj);
118 
119 	if (nodep == NULL) {
120 		php_dom_throw_error(INVALID_STATE_ERR, true);
121 		return FAILURE;
122 	}
123 
124 	dom_entity_reference_fetch_and_sync_declaration(nodep);
125 	return dom_node_child_nodes_read(obj, retval);
126 }
127 
128 #endif
129