xref: /PHP-7.4/ext/dom/namednodemap.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "php.h"
25 #if HAVE_LIBXML && HAVE_DOM
26 #include "php_dom.h"
27 
28 /* {{{ arginfo */
29 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_get_named_item, 0, 0, 1)
30 	ZEND_ARG_INFO(0, name)
31 ZEND_END_ARG_INFO();
32 
33 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_set_named_item, 0, 0, 1)
34 	ZEND_ARG_OBJ_INFO(0, arg, DOMNode, 0)
35 ZEND_END_ARG_INFO();
36 
37 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_remove_named_item, 0, 0, 0)
38 	ZEND_ARG_INFO(0, name)
39 ZEND_END_ARG_INFO();
40 
41 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_item, 0, 0, 0)
42 	ZEND_ARG_INFO(0, index)
43 ZEND_END_ARG_INFO();
44 
45 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_get_named_item_ns, 0, 0, 0)
46 	ZEND_ARG_INFO(0, namespaceURI)
47 	ZEND_ARG_INFO(0, localName)
48 ZEND_END_ARG_INFO();
49 
50 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_set_named_item_ns, 0, 0, 0)
51 	ZEND_ARG_OBJ_INFO(0, arg, DOMNode, 0)
52 ZEND_END_ARG_INFO();
53 
54 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_remove_named_item_ns, 0, 0, 0)
55 	ZEND_ARG_INFO(0, namespaceURI)
56 	ZEND_ARG_INFO(0, localName)
57 ZEND_END_ARG_INFO();
58 
59 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_namednodemap_count, 0, 0, 0)
60 ZEND_END_ARG_INFO();
61 /* }}} */
62 
63 /*
64 * class DOMNamedNodeMap
65 *
66 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1780488922
67 * Since:
68 */
69 
70 const zend_function_entry php_dom_namednodemap_class_functions[] = { /* {{{ */
71 	PHP_FALIAS(getNamedItem, dom_namednodemap_get_named_item, arginfo_dom_namednodemap_get_named_item)
72 	PHP_FALIAS(setNamedItem, dom_namednodemap_set_named_item, arginfo_dom_namednodemap_set_named_item)
73 	PHP_FALIAS(removeNamedItem, dom_namednodemap_remove_named_item, arginfo_dom_namednodemap_remove_named_item)
74 	PHP_FALIAS(item, dom_namednodemap_item, arginfo_dom_namednodemap_item)
75 	PHP_FALIAS(getNamedItemNS, dom_namednodemap_get_named_item_ns, arginfo_dom_namednodemap_get_named_item_ns)
76 	PHP_FALIAS(setNamedItemNS, dom_namednodemap_set_named_item_ns, arginfo_dom_namednodemap_set_named_item_ns)
77 	PHP_FALIAS(removeNamedItemNS, dom_namednodemap_remove_named_item_ns, arginfo_dom_namednodemap_remove_named_item_ns)
78 	PHP_FALIAS(count, dom_namednodemap_count, arginfo_dom_namednodemap_count)
79 	PHP_FE_END
80 };
81 /* }}} */
82 
83 /* {{{ length	int
84 readonly=yes
85 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6D0FB19E
86 Since:
87 */
dom_namednodemap_length_read(dom_object * obj,zval * retval)88 int dom_namednodemap_length_read(dom_object *obj, zval *retval)
89 {
90 	dom_nnodemap_object *objmap;
91 	xmlAttrPtr curnode;
92 	xmlNodePtr nodep;
93 	int count = 0;
94 
95 	objmap = (dom_nnodemap_object *)obj->ptr;
96 
97 	if (objmap != NULL) {
98 		if ((objmap->nodetype == XML_NOTATION_NODE) ||
99 			objmap->nodetype == XML_ENTITY_NODE) {
100 			if (objmap->ht) {
101 				count = xmlHashSize(objmap->ht);
102 			}
103 		} else {
104 			nodep = dom_object_get_node(objmap->baseobj);
105 			if (nodep) {
106 				curnode = nodep->properties;
107 				if (curnode) {
108 					count++;
109 					while (curnode->next != NULL) {
110 						count++;
111 						curnode = curnode->next;
112 					}
113 				}
114 			}
115 		}
116 	}
117 
118 	ZVAL_LONG(retval, count);
119 	return SUCCESS;
120 }
121 
122 /* }}} */
123 
124 /* {{{ proto DOMNode dom_namednodemap_get_named_item(string name);
125 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1074577549
126 Since:
127 */
PHP_FUNCTION(dom_namednodemap_get_named_item)128 PHP_FUNCTION(dom_namednodemap_get_named_item)
129 {
130 	zval *id;
131 	int ret;
132 	size_t namedlen=0;
133 	dom_object *intern;
134 	xmlNodePtr itemnode = NULL;
135 	char *named;
136 
137 	dom_nnodemap_object *objmap;
138 	xmlNodePtr nodep;
139 	xmlNotation *notep = NULL;
140 
141 	id = ZEND_THIS;
142 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &named, &namedlen) == FAILURE) {
143 		return;
144 	}
145 
146 	intern = Z_DOMOBJ_P(id);
147 
148 	objmap = (dom_nnodemap_object *)intern->ptr;
149 
150 	if (objmap != NULL) {
151 		if ((objmap->nodetype == XML_NOTATION_NODE) ||
152 			objmap->nodetype == XML_ENTITY_NODE) {
153 			if (objmap->ht) {
154 				if (objmap->nodetype == XML_ENTITY_NODE) {
155 					itemnode = (xmlNodePtr)xmlHashLookup(objmap->ht, (xmlChar *) named);
156 				} else {
157 					notep = (xmlNotation *)xmlHashLookup(objmap->ht, (xmlChar *) named);
158 					if (notep) {
159 						itemnode = create_notation(notep->name, notep->PublicID, notep->SystemID);
160 					}
161 				}
162 			}
163 		} else {
164 			nodep = dom_object_get_node(objmap->baseobj);
165 			if (nodep) {
166 				itemnode = (xmlNodePtr)xmlHasProp(nodep, (xmlChar *) named);
167 			}
168 		}
169 	}
170 
171 	if (itemnode) {
172 		DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
173 		return;
174 	} else {
175 		RETVAL_NULL();
176 	}
177 }
178 /* }}} end dom_namednodemap_get_named_item */
179 
180 /* {{{ proto DOMNode dom_namednodemap_set_named_item(DOMNode arg);
181 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1025163788
182 Since:
183 */
PHP_FUNCTION(dom_namednodemap_set_named_item)184 PHP_FUNCTION(dom_namednodemap_set_named_item)
185 {
186  DOM_NOT_IMPLEMENTED();
187 }
188 /* }}} end dom_namednodemap_set_named_item */
189 
190 /* {{{ proto DOMNode dom_namednodemap_remove_named_item(string name);
191 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-D58B193
192 Since:
193 */
PHP_FUNCTION(dom_namednodemap_remove_named_item)194 PHP_FUNCTION(dom_namednodemap_remove_named_item)
195 {
196  DOM_NOT_IMPLEMENTED();
197 }
198 /* }}} end dom_namednodemap_remove_named_item */
199 
200 /* {{{ proto DOMNode dom_namednodemap_item(int index);
201 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-349467F9
202 Since:
203 */
PHP_FUNCTION(dom_namednodemap_item)204 PHP_FUNCTION(dom_namednodemap_item)
205 {
206 	zval *id;
207 	zend_long index;
208 	int ret;
209 	dom_object *intern;
210 	xmlNodePtr itemnode = NULL;
211 
212 	dom_nnodemap_object *objmap;
213 	xmlNodePtr nodep, curnode;
214 	int count;
215 
216 	id = ZEND_THIS;
217 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
218 		return;
219 	}
220 	if (index >= 0) {
221 		if (ZEND_LONG_INT_OVFL(index)) {
222 			php_error_docref(NULL, E_WARNING, "Invalid index");
223 			RETURN_NULL();
224 		}
225 
226 		intern = Z_DOMOBJ_P(id);
227 
228 		objmap = (dom_nnodemap_object *)intern->ptr;
229 
230 		if (objmap != NULL) {
231 			if ((objmap->nodetype == XML_NOTATION_NODE) ||
232 				objmap->nodetype == XML_ENTITY_NODE) {
233 				if (objmap->ht) {
234 					if (objmap->nodetype == XML_ENTITY_NODE) {
235 						itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
236 					} else {
237 						itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
238 					}
239 				}
240 			} else {
241 				nodep = dom_object_get_node(objmap->baseobj);
242 				if (nodep) {
243 					curnode = (xmlNodePtr)nodep->properties;
244 					count = 0;
245 					while (count < index && curnode != NULL) {
246 						count++;
247 						curnode = (xmlNodePtr)curnode->next;
248 					}
249 					itemnode = curnode;
250 				}
251 			}
252 		}
253 
254 		if (itemnode) {
255 			DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
256 			return;
257 		}
258 	}
259 
260 	RETVAL_NULL();
261 }
262 /* }}} end dom_namednodemap_item */
263 
264 /* {{{ proto DOMNode dom_namednodemap_get_named_item_ns(string namespaceURI, string localName);
265 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-getNamedItemNS
266 Since: DOM Level 2
267 */
PHP_FUNCTION(dom_namednodemap_get_named_item_ns)268 PHP_FUNCTION(dom_namednodemap_get_named_item_ns)
269 {
270 	zval *id;
271 	int ret;
272 	size_t namedlen=0, urilen=0;
273 	dom_object *intern;
274 	xmlNodePtr itemnode = NULL;
275 	char *uri, *named;
276 
277 	dom_nnodemap_object *objmap;
278 	xmlNodePtr nodep;
279 	xmlNotation *notep = NULL;
280 
281 	id = ZEND_THIS;
282 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &urilen, &named, &namedlen) == FAILURE) {
283 		return;
284 	}
285 
286 	intern = Z_DOMOBJ_P(id);
287 
288 	objmap = (dom_nnodemap_object *)intern->ptr;
289 
290 	if (objmap != NULL) {
291 		if ((objmap->nodetype == XML_NOTATION_NODE) ||
292 			objmap->nodetype == XML_ENTITY_NODE) {
293 			if (objmap->ht) {
294 				if (objmap->nodetype == XML_ENTITY_NODE) {
295 					itemnode = (xmlNodePtr)xmlHashLookup(objmap->ht, (xmlChar *) named);
296 				} else {
297 					notep = (xmlNotation *)xmlHashLookup(objmap->ht, (xmlChar *) named);
298 					if (notep) {
299 						itemnode = create_notation(notep->name, notep->PublicID, notep->SystemID);
300 					}
301 				}
302 			}
303 		} else {
304 			nodep = dom_object_get_node(objmap->baseobj);
305 			if (nodep) {
306 				itemnode = (xmlNodePtr)xmlHasNsProp(nodep, (xmlChar *) named, (xmlChar *) uri);
307 			}
308 		}
309 	}
310 
311 	if (itemnode) {
312 		DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
313 		return;
314 	} else {
315 		RETVAL_NULL();
316 	}
317 }
318 /* }}} end dom_namednodemap_get_named_item_ns */
319 
320 /* {{{ proto DOMNode dom_namednodemap_set_named_item_ns(DOMNode arg);
321 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-setNamedItemNS
322 Since: DOM Level 2
323 */
PHP_FUNCTION(dom_namednodemap_set_named_item_ns)324 PHP_FUNCTION(dom_namednodemap_set_named_item_ns)
325 {
326  DOM_NOT_IMPLEMENTED();
327 }
328 /* }}} end dom_namednodemap_set_named_item_ns */
329 
330 /* {{{ proto DOMNode dom_namednodemap_remove_named_item_ns(string namespaceURI, string localName);
331 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-removeNamedItemNS
332 Since: DOM Level 2
333 */
PHP_FUNCTION(dom_namednodemap_remove_named_item_ns)334 PHP_FUNCTION(dom_namednodemap_remove_named_item_ns)
335 {
336  DOM_NOT_IMPLEMENTED();
337 }
338 /* }}} end dom_namednodemap_remove_named_item_ns */
339 
340 /* {{{ proto int|bool dom_namednodemap_count();
341 */
PHP_FUNCTION(dom_namednodemap_count)342 PHP_FUNCTION(dom_namednodemap_count)
343 {
344 	zval *id;
345 	dom_object *intern;
346 
347 	id = ZEND_THIS;
348 	if (zend_parse_parameters_none() == FAILURE) {
349 		return;
350 	}
351 
352 	intern = Z_DOMOBJ_P(id);
353 	if(dom_namednodemap_length_read(intern, return_value) == FAILURE) {
354 		RETURN_FALSE;
355 	}
356 }
357 /* }}} end dom_namednodemap_count */
358 
359 #endif
360