xref: /PHP-8.1/ext/dom/php_dom.h (revision 9f7d8880)
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 "zend_exceptions.h"
57 #include "dom_ce.h"
58 /* DOM API_VERSION, please bump it up, if you change anything in the API
59     therefore it's easier for the script-programmers to check, what's working how
60    Can be checked with phpversion("dom");
61 */
62 #define DOM_API_VERSION "20031129"
63 /* Define a custom type for iterating using an unused nodetype */
64 #define DOM_NODESET XML_XINCLUDE_START
65 
66 typedef struct _dom_xpath_object {
67 	int registerPhpFunctions;
68 	int register_node_ns;
69 	HashTable *registered_phpfunctions;
70 	HashTable *node_list;
71 	dom_object dom;
72 } dom_xpath_object;
73 
php_xpath_obj_from_obj(zend_object * obj)74 static inline dom_xpath_object *php_xpath_obj_from_obj(zend_object *obj) {
75 	return (dom_xpath_object*)((char*)(obj)
76 		- XtOffsetOf(dom_xpath_object, dom) - XtOffsetOf(dom_object, std));
77 }
78 
79 #define Z_XPATHOBJ_P(zv)  php_xpath_obj_from_obj(Z_OBJ_P((zv)))
80 
81 typedef struct _dom_nnodemap_object {
82 	dom_object *baseobj;
83 	zval baseobj_zv;
84 	int nodetype;
85 	xmlHashTable *ht;
86 	xmlChar *local;
87 	xmlChar *ns;
88 } dom_nnodemap_object;
89 
90 typedef struct {
91 	zend_object_iterator intern;
92 	zval curobj;
93 	HashPosition pos;
94 } php_dom_iterator;
95 
96 typedef struct {
97 	/* This may be a fake object that isn't actually in the children list of the parent.
98 	 * This is because some namespace declaration nodes aren't stored on the parent in libxml2, so we have to fake it.
99 	 * We could use a zval for this, but since this is always going to be an object let's save space... */
100 	dom_object *parent_intern;
101 	dom_object dom;
102 } dom_object_namespace_node;
103 
php_dom_namespace_node_obj_from_obj(zend_object * obj)104 static inline dom_object_namespace_node *php_dom_namespace_node_obj_from_obj(zend_object *obj) {
105 	return (dom_object_namespace_node*)((char*)(obj) - XtOffsetOf(dom_object_namespace_node, dom.std));
106 }
107 
108 #include "domexception.h"
109 
110 dom_object *dom_object_get_data(xmlNodePtr obj);
111 dom_doc_propsptr dom_get_doc_props(php_libxml_ref_obj *document);
112 zend_object *dom_objects_new(zend_class_entry *class_type);
113 zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type);
114 #ifdef LIBXML_XPATH_ENABLED
115 zend_object *dom_xpath_objects_new(zend_class_entry *class_type);
116 #endif
117 int dom_get_strict_error(php_libxml_ref_obj *document);
118 void php_dom_throw_error(int error_code, int strict_error);
119 void php_dom_throw_error_with_message(int error_code, char *error_message, int strict_error);
120 void node_list_unlink(xmlNodePtr node);
121 int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, int name_len);
122 xmlNsPtr dom_get_ns(xmlNodePtr node, char *uri, int *errorcode, char *prefix);
123 void dom_set_old_ns(xmlDoc *doc, xmlNs *ns);
124 void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep);
125 void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last);
126 xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName);
127 void dom_normalize (xmlNodePtr nodep);
128 xmlNode *dom_get_elements_by_tag_name_ns_raw(xmlNodePtr nodep, char *ns, char *local, int *cur, int index);
129 void php_dom_create_implementation(zval *retval);
130 int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child);
131 bool dom_has_feature(zend_string *feature, zend_string *version);
132 int dom_node_is_read_only(xmlNodePtr node);
133 int dom_node_children_valid(xmlNodePtr node);
134 void php_dom_create_iterator(zval *return_value, int ce_type);
135 void dom_namednode_iter(dom_object *basenode, int ntype, dom_object *intern, xmlHashTablePtr ht, xmlChar *local, xmlChar *ns);
136 xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID);
137 xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, int index);
138 xmlNode *php_dom_libxml_notation_iter(xmlHashTable *ht, int index);
139 zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
140 void dom_set_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece, zend_class_entry *ce);
141 xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern);
142 
143 void dom_parent_node_prepend(dom_object *context, zval *nodes, int nodesc);
144 void dom_parent_node_append(dom_object *context, zval *nodes, int nodesc);
145 void dom_parent_node_after(dom_object *context, zval *nodes, int nodesc);
146 void dom_parent_node_before(dom_object *context, zval *nodes, int nodesc);
147 void dom_child_node_remove(dom_object *context);
148 void dom_child_replace_with(dom_object *context, zval *nodes, int nodesc);
149 
150 /* nodemap and nodelist APIs */
151 xmlNodePtr php_dom_named_node_map_get_named_item(dom_nnodemap_object *objmap, const char *named, bool may_transform);
152 void php_dom_named_node_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const char *named, zval *return_value);
153 xmlNodePtr php_dom_named_node_map_get_item(dom_nnodemap_object *objmap, zend_long index);
154 void php_dom_named_node_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
155 void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
156 int php_dom_get_namednodemap_length(dom_object *obj);
157 int php_dom_get_nodelist_length(dom_object *obj);
158 
159 #define DOM_GET_OBJ(__ptr, __id, __prtype, __intern) { \
160 	__intern = Z_DOMOBJ_P(__id); \
161 	if (__intern->ptr == NULL || !(__ptr = (__prtype)((php_libxml_node_ptr *)__intern->ptr)->node)) { \
162 		zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(__intern->std.ce->name));\
163 		RETURN_THROWS();\
164   	} \
165 }
166 
167 #define DOM_NO_ARGS() \
168 	if (zend_parse_parameters_none() == FAILURE) { \
169 		RETURN_THROWS(); \
170 	}
171 
172 #define DOM_NOT_IMPLEMENTED() \
173 	zend_throw_error(NULL, "Not yet implemented"); \
174 	RETURN_THROWS();
175 
176 #define DOM_NODELIST 0
177 #define DOM_NAMEDNODEMAP 1
178 
179 PHP_MINIT_FUNCTION(dom);
180 PHP_MSHUTDOWN_FUNCTION(dom);
181 PHP_MINFO_FUNCTION(dom);
182 
183 #endif /* PHP_DOM_H */
184