xref: /PHP-8.2/ext/dom/php_dom.h (revision 90785dd8)
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    |          Marcus Borger <helly@php.net>                               |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef PHP_DOM_H
20 #define PHP_DOM_H
21 
22 extern zend_module_entry dom_module_entry;
23 #define phpext_dom_ptr &dom_module_entry
24 
25 #ifdef ZTS
26 #include "TSRM.h"
27 #endif
28 
29 #include <libxml/parser.h>
30 #include <libxml/parserInternals.h>
31 #include <libxml/tree.h>
32 #include <libxml/uri.h>
33 #include <libxml/xmlerror.h>
34 #include <libxml/xinclude.h>
35 #include <libxml/hash.h>
36 #include <libxml/c14n.h>
37 #ifdef LIBXML_HTML_ENABLED
38 #include <libxml/HTMLparser.h>
39 #include <libxml/HTMLtree.h>
40 #endif
41 #ifdef LIBXML_XPATH_ENABLED
42 #include <libxml/xpath.h>
43 #include <libxml/xpathInternals.h>
44 #endif
45 #ifdef LIBXML_XPTR_ENABLED
46 #include <libxml/xpointer.h>
47 #endif
48 #ifdef PHP_WIN32
49 #ifndef DOM_EXPORTS
50 #define DOM_EXPORTS
51 #endif
52 #endif
53 
54 #include "xml_common.h"
55 #include "ext/libxml/php_libxml.h"
56 #include "xpath_callbacks.h"
57 #include "zend_exceptions.h"
58 #include "dom_ce.h"
59 /* DOM API_VERSION, please bump it up, if you change anything in the API
60     therefore it's easier for the script-programmers to check, what's working how
61    Can be checked with phpversion("dom");
62 */
63 #define DOM_API_VERSION "20031129"
64 /* Define a custom type for iterating using an unused nodetype */
65 #define DOM_NODESET XML_XINCLUDE_START
66 
67 typedef struct _dom_xpath_object {
68 	php_dom_xpath_callbacks xpath_callbacks;
69 	int register_node_ns;
70 	dom_object dom;
71 } dom_xpath_object;
72 
php_xpath_obj_from_obj(zend_object * obj)73 static inline dom_xpath_object *php_xpath_obj_from_obj(zend_object *obj) {
74 	return (dom_xpath_object*)((char*)(obj)
75 		- XtOffsetOf(dom_xpath_object, dom) - XtOffsetOf(dom_object, std));
76 }
77 
78 #define Z_XPATHOBJ_P(zv)  php_xpath_obj_from_obj(Z_OBJ_P((zv)))
79 
80 typedef struct _dom_nnodemap_object {
81 	dom_object *baseobj;
82 	zval baseobj_zv;
83 	int nodetype;
84 	int cached_length;
85 	xmlHashTable *ht;
86 	xmlChar *local;
87 	xmlChar *ns;
88 	php_libxml_cache_tag cache_tag;
89 	dom_object *cached_obj;
90 	int cached_obj_index;
91 	bool free_local : 1;
92 	bool free_ns : 1;
93 } dom_nnodemap_object;
94 
95 typedef struct {
96 	zend_object_iterator intern;
97 	zval curobj;
98 	HashPosition pos;
99 	php_libxml_cache_tag cache_tag;
100 } php_dom_iterator;
101 
102 typedef struct {
103 	/* This may be a fake object that isn't actually in the children list of the parent.
104 	 * This is because some namespace declaration nodes aren't stored on the parent in libxml2, so we have to fake it.
105 	 * We could use a zval for this, but since this is always going to be an object let's save space... */
106 	dom_object *parent_intern;
107 	dom_object dom;
108 } dom_object_namespace_node;
109 
php_dom_namespace_node_obj_from_obj(zend_object * obj)110 static inline dom_object_namespace_node *php_dom_namespace_node_obj_from_obj(zend_object *obj) {
111 	return (dom_object_namespace_node*)((char*)(obj) - XtOffsetOf(dom_object_namespace_node, dom.std));
112 }
113 
114 #include "domexception.h"
115 
116 #define DOM_HTML_NO_DEFAULT_NS (1U << 31)
117 
118 dom_object *dom_object_get_data(xmlNodePtr obj);
119 dom_doc_propsptr dom_get_doc_props(php_libxml_ref_obj *document);
120 libxml_doc_props const* dom_get_doc_props_read_only(const php_libxml_ref_obj *document);
121 zend_object *dom_objects_new(zend_class_entry *class_type);
122 zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type);
123 #ifdef LIBXML_XPATH_ENABLED
124 zend_object *dom_xpath_objects_new(zend_class_entry *class_type);
125 #endif
126 int dom_get_strict_error(php_libxml_ref_obj *document);
127 void php_dom_throw_error(int error_code, int strict_error);
128 void php_dom_throw_error_with_message(int error_code, char *error_message, int strict_error);
129 void node_list_unlink(xmlNodePtr node);
130 int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, int name_len);
131 xmlNsPtr dom_get_ns(xmlNodePtr node, char *uri, int *errorcode, char *prefix);
132 xmlNsPtr dom_get_ns_unchecked(xmlNodePtr nodep, char *uri, char *prefix);
133 void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep);
134 void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last);
135 xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName);
136 void dom_normalize (xmlNodePtr nodep);
137 xmlNode *dom_get_elements_by_tag_name_ns_raw(xmlNodePtr basep, xmlNodePtr nodep, char *ns, char *local, int *cur, int index);
138 void php_dom_create_implementation(zval *retval);
139 int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child);
140 bool dom_has_feature(zend_string *feature, zend_string *version);
141 int dom_node_is_read_only(xmlNodePtr node);
142 int dom_node_children_valid(xmlNodePtr node);
143 void php_dom_create_iterator(zval *return_value, int ce_type);
144 void dom_namednode_iter(dom_object *basenode, int ntype, dom_object *intern, xmlHashTablePtr ht, const char *local, size_t local_len, const char *ns, size_t ns_len);
145 xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID);
146 xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, int index);
147 xmlNode *php_dom_libxml_notation_iter(xmlHashTable *ht, int index);
148 zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
149 void dom_set_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece, zend_class_entry *ce);
150 xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern);
151 void php_dom_get_content_into_zval(const xmlNode *nodep, zval *target, bool default_is_null);
152 zend_string *dom_node_concatenated_name_helper(size_t name_len, const char *name, size_t prefix_len, const char *prefix);
153 zend_string *dom_node_get_node_name_attribute_or_element(const xmlNode *nodep);
154 bool php_dom_is_node_connected(const xmlNode *node);
155 bool php_dom_adopt_node(xmlNodePtr nodep, dom_object *dom_object_new_document, xmlDocPtr new_document);
156 xmlNsPtr dom_get_ns_resolve_prefix_conflict(xmlNodePtr tree, const char *uri);
157 void php_dom_reconcile_attribute_namespace_after_insertion(xmlAttrPtr attrp);
158 
159 void php_dom_document_constructor(INTERNAL_FUNCTION_PARAMETERS);
160 
161 dom_object *php_dom_instantiate_object_helper(zval *return_value, zend_class_entry *ce, xmlNodePtr obj, dom_object *parent);
162 
163 typedef enum {
164 	DOM_LOAD_STRING = 0,
165 	DOM_LOAD_FILE = 1,
166 } dom_load_mode;
167 
168 xmlDocPtr dom_document_parser(zval *id, dom_load_mode mode, const char *source, size_t source_len, size_t options, xmlCharEncodingHandlerPtr encoding);
169 
170 /* parentnode */
171 void dom_parent_node_prepend(dom_object *context, zval *nodes, uint32_t nodesc);
172 void dom_parent_node_append(dom_object *context, zval *nodes, uint32_t nodesc);
173 void dom_parent_node_after(dom_object *context, zval *nodes, uint32_t nodesc);
174 void dom_parent_node_before(dom_object *context, zval *nodes, uint32_t nodesc);
175 void dom_parent_node_replace_children(dom_object *context, zval *nodes, uint32_t nodesc);
176 void dom_child_node_remove(dom_object *context);
177 void dom_child_replace_with(dom_object *context, zval *nodes, uint32_t nodesc);
178 
179 void dom_remove_all_children(xmlNodePtr nodep);
180 
181 /* nodemap and nodelist APIs */
182 xmlNodePtr php_dom_named_node_map_get_named_item(dom_nnodemap_object *objmap, const char *named, bool may_transform);
183 void php_dom_named_node_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const char *named, zval *return_value);
184 xmlNodePtr php_dom_named_node_map_get_item(dom_nnodemap_object *objmap, zend_long index);
185 void php_dom_named_node_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
186 void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
187 int php_dom_get_namednodemap_length(dom_object *obj);
188 int php_dom_get_nodelist_length(dom_object *obj);
189 
190 xmlNodePtr dom_clone_node(xmlNodePtr node, xmlDocPtr doc, const dom_object *intern, bool recursive);
191 void dom_mark_namespaces_for_copy_based_on_copy(xmlNodePtr copy, const xmlNode *original);
192 
193 #define DOM_GET_INTERN(__id, __intern) { \
194 	__intern = Z_DOMOBJ_P(__id); \
195 	if (UNEXPECTED(__intern->ptr == NULL)) { \
196 		zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(__intern->std.ce->name));\
197 		RETURN_THROWS();\
198   	} \
199 }
200 
201 #define DOM_GET_THIS_INTERN(__intern) DOM_GET_INTERN(ZEND_THIS, __intern)
202 
203 #define DOM_GET_OBJ(__ptr, __id, __prtype, __intern) { \
204 	DOM_GET_INTERN(__id, __intern); \
205 	__ptr = (__prtype)((php_libxml_node_ptr *)__intern->ptr)->node; \
206 }
207 
208 #define DOM_NODELIST 0
209 #define DOM_NAMEDNODEMAP 1
210 
php_dom_is_cache_tag_stale_from_doc_ptr(const php_libxml_cache_tag * cache_tag,const php_libxml_ref_obj * doc_ptr)211 static zend_always_inline bool php_dom_is_cache_tag_stale_from_doc_ptr(const php_libxml_cache_tag *cache_tag, const php_libxml_ref_obj *doc_ptr)
212 {
213 	ZEND_ASSERT(cache_tag != NULL);
214 	ZEND_ASSERT(doc_ptr != NULL);
215 	/* See overflow comment in php_libxml_invalidate_node_list_cache(). */
216 #if SIZEOF_SIZE_T == 8
217 	return cache_tag->modification_nr != doc_ptr->cache_tag.modification_nr;
218 #else
219 	return cache_tag->modification_nr != doc_ptr->cache_tag.modification_nr || UNEXPECTED(doc_ptr->cache_tag.modification_nr == SIZE_MAX);
220 #endif
221 }
222 
php_dom_is_cache_tag_stale_from_node(const php_libxml_cache_tag * cache_tag,const xmlNodePtr node)223 static zend_always_inline bool php_dom_is_cache_tag_stale_from_node(const php_libxml_cache_tag *cache_tag, const xmlNodePtr node)
224 {
225 	ZEND_ASSERT(node != NULL);
226 	php_libxml_node_ptr *private = node->_private;
227 	if (!private) {
228 		return true;
229 	}
230 	php_libxml_node_object *object_private = private->_private;
231 	if (!object_private || !object_private->document) {
232 		return true;
233 	}
234 	return php_dom_is_cache_tag_stale_from_doc_ptr(cache_tag, object_private->document);
235 }
236 
php_dom_mark_cache_tag_up_to_date_from_node(php_libxml_cache_tag * cache_tag,const xmlNodePtr node)237 static zend_always_inline void php_dom_mark_cache_tag_up_to_date_from_node(php_libxml_cache_tag *cache_tag, const xmlNodePtr node)
238 {
239 	ZEND_ASSERT(cache_tag != NULL);
240 	php_libxml_node_ptr *private = node->_private;
241 	if (private) {
242 		php_libxml_node_object *object_private = private->_private;
243 		if (object_private->document) {
244 			cache_tag->modification_nr = object_private->document->cache_tag.modification_nr;
245 		}
246 	}
247 }
248 
249 PHP_MINIT_FUNCTION(dom);
250 PHP_MSHUTDOWN_FUNCTION(dom);
251 PHP_MINFO_FUNCTION(dom);
252 
253 #endif /* PHP_DOM_H */
254