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 bool 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, *local_lower;
87 xmlChar *ns;
88 php_libxml_cache_tag cache_tag;
89 dom_object *cached_obj;
90 zend_long 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
110 typedef enum dom_iterator_type {
111 DOM_NODELIST,
112 DOM_NAMEDNODEMAP,
113 DOM_DTD_NAMEDNODEMAP,
114 DOM_HTMLCOLLECTION,
115 } dom_iterator_type;
116
117 struct php_dom_libxml_ns_mapper;
118 typedef struct php_dom_libxml_ns_mapper php_dom_libxml_ns_mapper;
119
php_dom_namespace_node_obj_from_obj(zend_object * obj)120 static inline dom_object_namespace_node *php_dom_namespace_node_obj_from_obj(zend_object *obj) {
121 return (dom_object_namespace_node*)((char*)(obj) - XtOffsetOf(dom_object_namespace_node, dom.std));
122 }
123
124 #include "domexception.h"
125
126 #define DOM_HTML_NO_DEFAULT_NS (1U << 31)
127
128 void dom_objects_free_storage(zend_object *object);
129 dom_doc_propsptr dom_get_doc_props(php_libxml_ref_obj *document);
130 libxml_doc_props const* dom_get_doc_props_read_only(const php_libxml_ref_obj *document);
131 zend_object *dom_objects_new(zend_class_entry *class_type);
132 zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type);
133 #ifdef LIBXML_XPATH_ENABLED
134 zend_object *dom_xpath_objects_new(zend_class_entry *class_type);
135 #endif
136 bool dom_get_strict_error(php_libxml_ref_obj *document);
137 void node_list_unlink(xmlNodePtr node);
138 int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, int name_len);
139 xmlNsPtr dom_get_ns(xmlNodePtr node, char *uri, int *errorcode, char *prefix);
140 xmlNsPtr dom_get_ns_unchecked(xmlNodePtr nodep, char *uri, char *prefix);
141 void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep);
142 void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last);
143 xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName);
144 void php_dom_normalize_legacy(xmlNodePtr nodep);
145 void php_dom_normalize_modern(xmlNodePtr nodep);
146 xmlNode *dom_get_elements_by_tag_name_ns_raw(xmlNodePtr basep, xmlNodePtr nodep, xmlChar *ns, xmlChar *local, xmlChar *local_lower, zend_long *cur, zend_long index);
147 void php_dom_create_implementation(zval *retval, bool modern);
148 int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child);
149 bool dom_has_feature(zend_string *feature, zend_string *version);
150 int dom_node_is_read_only(const xmlNode *node);
151 bool dom_node_children_valid(const xmlNode *node);
152 void php_dom_create_iterator(zval *return_value, dom_iterator_type iterator_type, bool modern);
153 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);
154 xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID);
155 xmlNode *php_dom_libxml_hash_iter(dom_nnodemap_object *objmap, int index);
156 zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
157 void dom_set_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece, zend_class_entry *ce);
158 xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern);
159 void php_dom_get_content_into_zval(const xmlNode *nodep, zval *target, bool default_is_null);
160 zend_string *dom_node_concatenated_name_helper(size_t name_len, const char *name, size_t prefix_len, const char *prefix);
161 zend_string *dom_node_get_node_name_attribute_or_element(const xmlNode *nodep, bool uppercase);
162 bool php_dom_is_node_connected(const xmlNode *node);
163 bool php_dom_adopt_node(xmlNodePtr nodep, dom_object *dom_object_new_document, xmlDocPtr new_document);
164 xmlNsPtr dom_get_ns_resolve_prefix_conflict(xmlNodePtr tree, const char *uri);
165 int dom_validate_and_extract(const zend_string *namespace, const zend_string *qname, xmlChar **localName, xmlChar **prefix);
166 bool dom_match_qualified_name_according_to_spec(const xmlChar *qname, const xmlNode *nodep);
167 bool php_dom_has_sibling_following_node(xmlNodePtr node, xmlElementType type);
168 bool php_dom_has_sibling_preceding_node(xmlNodePtr node, xmlElementType type);
169 bool php_dom_has_child_of_type(xmlNodePtr node, xmlElementType type);
170 void php_dom_update_document_after_clone(dom_object *original, xmlNodePtr original_node, dom_object *clone, xmlNodePtr cloned_node);
171 xmlAttrPtr php_dom_get_attribute_node(xmlNodePtr elem, const xmlChar *name, size_t name_len);
172 xmlChar *php_dom_libxml_fix_file_path(xmlChar *path);
173 void dom_document_convert_to_modern(php_libxml_ref_obj *document, xmlDocPtr lxml_doc);
174 dom_object *php_dom_instantiate_object_helper(zval *return_value, zend_class_entry *ce, xmlNodePtr obj, dom_object *parent);
175 xmlDocPtr php_dom_create_html_doc(void);
176 xmlEntityPtr dom_entity_reference_fetch_and_sync_declaration(xmlNodePtr reference);
177 void dom_set_xml_class(php_libxml_ref_obj *document);
178 const char *dom_locate_a_namespace(const xmlNode *node, const zend_string *prefix);
179 void dom_mark_namespaces_as_attributes_too(php_dom_libxml_ns_mapper *ns_mapper, xmlDocPtr doc);
180 bool dom_compare_value(const xmlAttr *attr, const xmlChar *value);
181 void dom_attr_value_will_change(dom_object *obj, xmlAttrPtr attrp);
182 bool php_dom_create_nullable_object(xmlNodePtr obj, zval *return_value, dom_object *domobj);
183 xmlNodePtr dom_clone_node(php_dom_libxml_ns_mapper *ns_mapper, xmlNodePtr node, xmlDocPtr doc, bool recursive);
184
185 typedef enum {
186 DOM_LOAD_STRING = 0,
187 DOM_LOAD_FILE = 1,
188 } dom_load_mode;
189
190 #define DOM_DOCUMENT_MALFORMED ((xmlDocPtr) -1)
191
192 xmlDocPtr dom_document_parser(zval *id, dom_load_mode mode, const char *source, size_t source_len, size_t options, xmlCharEncodingHandlerPtr encoding);
193
194 /* parentnode */
195 void dom_parent_node_prepend(dom_object *context, zval *nodes, uint32_t nodesc);
196 void dom_parent_node_append(dom_object *context, zval *nodes, uint32_t nodesc);
197 void dom_parent_node_after(dom_object *context, zval *nodes, uint32_t nodesc);
198 void dom_parent_node_before(dom_object *context, zval *nodes, uint32_t nodesc);
199 void dom_parent_node_replace_children(dom_object *context, zval *nodes, uint32_t nodesc);
200 void dom_child_node_remove(dom_object *context);
201 void dom_child_replace_with(dom_object *context, zval *nodes, uint32_t nodesc);
202 void dom_remove_all_children(xmlNodePtr nodep);
203 bool php_dom_fragment_insertion_hierarchy_check_pre_insertion(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child);
204 bool php_dom_fragment_insertion_hierarchy_check_replace(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child);
205 void php_dom_node_append(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent);
206 bool php_dom_pre_insert(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent, xmlNodePtr insertion_point);
207 bool php_dom_pre_insert_is_parent_invalid(xmlNodePtr parent);
208 void dom_parent_node_query_selector(xmlNodePtr thisp, dom_object *intern, zval *return_value, const zend_string *selectors_str);
209 void dom_parent_node_query_selector_all(xmlNodePtr thisp, dom_object *intern, zval *return_value, const zend_string *selectors_str);
210 void dom_element_matches(xmlNodePtr thisp, dom_object *intern, zval *return_value, const zend_string *selectors_str);
211 void dom_element_closest(xmlNodePtr thisp, dom_object *intern, zval *return_value, const zend_string *selectors_str);
212
213 /* nodemap and nodelist APIs */
214 xmlNodePtr php_dom_named_node_map_get_named_item(dom_nnodemap_object *objmap, const zend_string *named, bool may_transform);
215 void php_dom_named_node_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, zval *return_value);
216 xmlNodePtr php_dom_named_node_map_get_item(dom_nnodemap_object *objmap, zend_long index);
217 void php_dom_named_node_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
218 int php_dom_get_namednodemap_length(dom_object *obj);
219 xmlNodePtr dom_nodelist_iter_start_first_child(xmlNodePtr nodep);
220
221 #define DOM_GET_INTERN(__id, __intern) { \
222 __intern = Z_DOMOBJ_P(__id); \
223 if (UNEXPECTED(__intern->ptr == NULL)) { \
224 zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(__intern->std.ce->name));\
225 RETURN_THROWS();\
226 } \
227 }
228
229 #define DOM_GET_THIS_INTERN(__intern) DOM_GET_INTERN(ZEND_THIS, __intern)
230
231 #define DOM_GET_OBJ(__ptr, __id, __prtype, __intern) { \
232 DOM_GET_INTERN(__id, __intern); \
233 __ptr = (__prtype)((php_libxml_node_ptr *)__intern->ptr)->node; \
234 }
235
php_dom_is_cache_tag_stale_from_doc_ptr(const php_libxml_cache_tag * cache_tag,const php_libxml_ref_obj * doc_ptr)236 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)
237 {
238 ZEND_ASSERT(doc_ptr != NULL);
239 return php_libxml_is_cache_tag_stale(cache_tag, &doc_ptr->cache_tag);
240 }
241
php_dom_is_cache_tag_stale_from_node(const php_libxml_cache_tag * cache_tag,const xmlNode * node)242 static zend_always_inline bool php_dom_is_cache_tag_stale_from_node(const php_libxml_cache_tag *cache_tag, const xmlNode *node)
243 {
244 ZEND_ASSERT(node != NULL);
245 php_libxml_node_ptr *_private = node->_private;
246 if (!_private) {
247 return true;
248 }
249 php_libxml_node_object *object_private = _private->_private;
250 if (!object_private || !object_private->document) {
251 return true;
252 }
253 return php_dom_is_cache_tag_stale_from_doc_ptr(cache_tag, object_private->document);
254 }
255
php_dom_mark_cache_tag_up_to_date_from_node(php_libxml_cache_tag * cache_tag,const xmlNode * node)256 static zend_always_inline void php_dom_mark_cache_tag_up_to_date_from_node(php_libxml_cache_tag *cache_tag, const xmlNode *node)
257 {
258 ZEND_ASSERT(cache_tag != NULL);
259 php_libxml_node_ptr *_private = node->_private;
260 if (_private) {
261 php_libxml_node_object *object_private = _private->_private;
262 if (object_private->document) {
263 cache_tag->modification_nr = object_private->document->cache_tag.modification_nr;
264 }
265 }
266 }
267
php_dom_mark_cache_tag_up_to_date_from_doc_ref(php_libxml_cache_tag * cache_tag,const php_libxml_ref_obj * document)268 static zend_always_inline void php_dom_mark_cache_tag_up_to_date_from_doc_ref(php_libxml_cache_tag *cache_tag, const php_libxml_ref_obj *document)
269 {
270 ZEND_ASSERT(cache_tag != NULL);
271 ZEND_ASSERT(document != NULL);
272 cache_tag->modification_nr = document->cache_tag.modification_nr;
273 }
274
php_dom_follow_spec_node(const xmlNode * node)275 static zend_always_inline bool php_dom_follow_spec_node(const xmlNode *node)
276 {
277 ZEND_ASSERT(node != NULL);
278 php_libxml_node_ptr *_private = node->_private;
279 if (_private) {
280 php_libxml_node_object *object_private = _private->_private;
281 if (object_private->document) {
282 return php_dom_follow_spec_doc_ref(object_private->document);
283 }
284 }
285 return false;
286 }
287
288 /* Returns the first child of a container node (e.g. elements, fragments, documents, ...). */
php_dom_first_child_of_container_node(xmlNodePtr parent)289 static zend_always_inline xmlNodePtr php_dom_first_child_of_container_node(xmlNodePtr parent)
290 {
291 if (parent->type == XML_DOCUMENT_NODE || parent->type == XML_HTML_DOCUMENT_NODE) {
292 return xmlDocGetRootElement((xmlDoc *) parent);
293 } else {
294 return parent->children;
295 }
296 }
297
php_dom_get_content_or_empty(const xmlNode * node)298 static zend_always_inline const xmlChar *php_dom_get_content_or_empty(const xmlNode *node)
299 {
300 return node->content ? node->content : BAD_CAST "";
301 }
302
303 #define PHP_DOM_DEPRECATED_PROPERTY(message) do { \
304 if (EXPECTED(!DOM_G(suppress_warnings))) {\
305 zend_error(E_DEPRECATED, message); \
306 if (UNEXPECTED(EG(exception))) { \
307 return FAILURE; \
308 } \
309 } \
310 } while (0)
311
312 ZEND_BEGIN_MODULE_GLOBALS(dom)
313 bool suppress_warnings;
314 ZEND_END_MODULE_GLOBALS(dom)
315
316 ZEND_EXTERN_MODULE_GLOBALS(dom)
317
318 #define DOM_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(dom, v)
319
320 PHP_MINIT_FUNCTION(dom);
321 PHP_MSHUTDOWN_FUNCTION(dom);
322 PHP_MINFO_FUNCTION(dom);
323
324 #endif /* PHP_DOM_H */
325