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