xref: /PHP-8.1/ext/dom/xpath.c (revision 20c9c4a3)
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    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php.h"
23 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 
26 #define PHP_DOM_XPATH_QUERY 0
27 #define PHP_DOM_XPATH_EVALUATE 1
28 
29 /*
30 * class DOMXPath
31 */
32 
33 #ifdef LIBXML_XPATH_ENABLED
34 
dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt,int nargs,int type)35 static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */
36 {
37 	zval retval;
38 	int result, i;
39 	int error = 0;
40 	zend_fcall_info fci;
41 	xmlXPathObjectPtr obj;
42 	char *str;
43 	zend_string *callable = NULL;
44 	dom_xpath_object *intern;
45 
46 
47 	if (! zend_is_executing()) {
48 		xmlGenericError(xmlGenericErrorContext,
49 		"xmlExtFunctionTest: Function called from outside of PHP\n");
50 		error = 1;
51 	} else {
52 		intern = (dom_xpath_object *) ctxt->context->userData;
53 		if (intern == NULL) {
54 			xmlGenericError(xmlGenericErrorContext,
55 			"xmlExtFunctionTest: failed to get the internal object\n");
56 			error = 1;
57 		}
58 		else if (intern->registerPhpFunctions == 0) {
59 			xmlGenericError(xmlGenericErrorContext,
60 			"xmlExtFunctionTest: PHP Object did not register PHP functions\n");
61 			error = 1;
62 		}
63 	}
64 
65 	if (error == 1) {
66 		for (i = nargs - 1; i >= 0; i--) {
67 			obj = valuePop(ctxt);
68 			xmlXPathFreeObject(obj);
69 		}
70 		return;
71 	}
72 
73 	if (UNEXPECTED(nargs == 0)) {
74 		zend_throw_error(NULL, "Function name must be passed as the first argument");
75 		return;
76 	}
77 
78 	fci.param_count = nargs - 1;
79 	if (fci.param_count > 0) {
80 		fci.params = safe_emalloc(fci.param_count, sizeof(zval), 0);
81 	}
82 	/* Reverse order to pop values off ctxt stack */
83 	for (i = fci.param_count - 1; i >= 0; i--) {
84 		obj = valuePop(ctxt);
85 		switch (obj->type) {
86 			case XPATH_STRING:
87 				ZVAL_STRING(&fci.params[i],  (char *)obj->stringval);
88 				break;
89 			case XPATH_BOOLEAN:
90 				ZVAL_BOOL(&fci.params[i],  obj->boolval);
91 				break;
92 			case XPATH_NUMBER:
93 				ZVAL_DOUBLE(&fci.params[i], obj->floatval);
94 				break;
95 			case XPATH_NODESET:
96 				if (type == 1) {
97 					str = (char *)xmlXPathCastToString(obj);
98 					ZVAL_STRING(&fci.params[i], str);
99 					xmlFree(str);
100 				} else if (type == 2) {
101 					int j;
102 					if (obj->nodesetval && obj->nodesetval->nodeNr > 0) {
103 						array_init(&fci.params[i]);
104 						for (j = 0; j < obj->nodesetval->nodeNr; j++) {
105 							xmlNodePtr node = obj->nodesetval->nodeTab[j];
106 							zval child;
107 							/* not sure, if we need this... it's copied from xpath.c */
108 							if (node->type == XML_NAMESPACE_DECL) {
109 								xmlNodePtr nsparent = node->_private;
110 								xmlNsPtr original = (xmlNsPtr) node;
111 
112 								/* Make sure parent dom object exists, so we can take an extra reference. */
113 								zval parent_zval; /* don't destroy me, my lifetime is transfered to the fake namespace decl */
114 								php_dom_create_object(nsparent, &parent_zval, &intern->dom);
115 								dom_object *parent_intern = Z_DOMOBJ_P(&parent_zval);
116 
117 								node = php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern);
118 							} else {
119 								php_dom_create_object(node, &child, &intern->dom);
120 							}
121 							add_next_index_zval(&fci.params[i], &child);
122 						}
123 					} else {
124 						ZVAL_EMPTY_ARRAY(&fci.params[i]);
125 					}
126 				}
127 				break;
128 			default:
129 			ZVAL_STRING(&fci.params[i], (char *)xmlXPathCastToString(obj));
130 		}
131 		xmlXPathFreeObject(obj);
132 	}
133 
134 	fci.size = sizeof(fci);
135 
136 	/* Last element of the stack is the function name */
137 	obj = valuePop(ctxt);
138 	if (obj->stringval == NULL) {
139 		zend_type_error("Handler name must be a string");
140 		xmlXPathFreeObject(obj);
141 		goto cleanup_no_callable;
142 	}
143 	ZVAL_STRING(&fci.function_name, (char *) obj->stringval);
144 	xmlXPathFreeObject(obj);
145 
146 	fci.object = NULL;
147 	fci.named_params = NULL;
148 	fci.retval = &retval;
149 
150 	if (!zend_make_callable(&fci.function_name, &callable)) {
151 		zend_throw_error(NULL, "Unable to call handler %s()", ZSTR_VAL(callable));
152 		goto cleanup;
153 	} else if (intern->registerPhpFunctions == 2 && zend_hash_exists(intern->registered_phpfunctions, callable) == 0) {
154 		zend_throw_error(NULL, "Not allowed to call handler '%s()'.", ZSTR_VAL(callable));
155 		goto cleanup;
156 	} else {
157 		result = zend_call_function(&fci, NULL);
158 		if (result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
159 			if (Z_TYPE(retval) == IS_OBJECT && instanceof_function(Z_OBJCE(retval), dom_node_class_entry)) {
160 				xmlNode *nodep;
161 				dom_object *obj;
162 				if (intern->node_list == NULL) {
163 					intern->node_list = zend_new_array(0);
164 				}
165 				Z_ADDREF(retval);
166 				zend_hash_next_index_insert(intern->node_list, &retval);
167 				obj = Z_DOMOBJ_P(&retval);
168 				nodep = dom_object_get_node(obj);
169 				valuePush(ctxt, xmlXPathNewNodeSet(nodep));
170 			} else if (Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE) {
171 				valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(retval) == IS_TRUE));
172 			} else if (Z_TYPE(retval) == IS_OBJECT) {
173 				zend_type_error("A PHP Object cannot be converted to a XPath-string");
174 				return;
175 			} else {
176 				zend_string *str = zval_get_string(&retval);
177 				valuePush(ctxt, xmlXPathNewString((xmlChar *) ZSTR_VAL(str)));
178 				zend_string_release_ex(str, 0);
179 			}
180 			zval_ptr_dtor(&retval);
181 		}
182 	}
183 cleanup:
184 	zend_string_release_ex(callable, 0);
185 	zval_ptr_dtor_nogc(&fci.function_name);
186 cleanup_no_callable:
187 	if (fci.param_count > 0) {
188 		for (i = 0; i < nargs - 1; i++) {
189 			zval_ptr_dtor(&fci.params[i]);
190 		}
191 		efree(fci.params);
192 	}
193 }
194 /* }}} */
195 
dom_xpath_ext_function_string_php(xmlXPathParserContextPtr ctxt,int nargs)196 static void dom_xpath_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
197 {
198 	dom_xpath_ext_function_php(ctxt, nargs, 1);
199 }
200 /* }}} */
201 
dom_xpath_ext_function_object_php(xmlXPathParserContextPtr ctxt,int nargs)202 static void dom_xpath_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
203 {
204 	dom_xpath_ext_function_php(ctxt, nargs, 2);
205 }
206 /* }}} */
207 
208 /* {{{ */
PHP_METHOD(DOMXPath,__construct)209 PHP_METHOD(DOMXPath, __construct)
210 {
211 	zval *doc;
212 	bool register_node_ns = 1;
213 	xmlDocPtr docp = NULL;
214 	dom_object *docobj;
215 	dom_xpath_object *intern;
216 	xmlXPathContextPtr ctx, oldctx;
217 
218 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &doc, dom_document_class_entry, &register_node_ns) == FAILURE) {
219 		RETURN_THROWS();
220 	}
221 
222 	DOM_GET_OBJ(docp, doc, xmlDocPtr, docobj);
223 
224 	ctx = xmlXPathNewContext(docp);
225 	if (ctx == NULL) {
226 		php_dom_throw_error(INVALID_STATE_ERR, 1);
227 		RETURN_THROWS();
228 	}
229 
230 	intern = Z_XPATHOBJ_P(ZEND_THIS);
231 	if (intern != NULL) {
232 		oldctx = (xmlXPathContextPtr)intern->dom.ptr;
233 		if (oldctx != NULL) {
234 			php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom);
235 			xmlXPathFreeContext(oldctx);
236 		}
237 
238 		xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "functionString",
239 					   (const xmlChar *) "http://php.net/xpath",
240 					   dom_xpath_ext_function_string_php);
241 		xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "function",
242 					   (const xmlChar *) "http://php.net/xpath",
243 					   dom_xpath_ext_function_object_php);
244 
245 		intern->dom.ptr = ctx;
246 		ctx->userData = (void *)intern;
247 		intern->dom.document = docobj->document;
248 		intern->register_node_ns = register_node_ns;
249 		php_libxml_increment_doc_ref((php_libxml_node_object *) &intern->dom, docp);
250 	}
251 }
252 /* }}} end DOMXPath::__construct */
253 
254 /* {{{ document DOMDocument*/
dom_xpath_document_read(dom_object * obj,zval * retval)255 int dom_xpath_document_read(dom_object *obj, zval *retval)
256 {
257 	xmlDoc *docp = NULL;
258 	xmlXPathContextPtr ctx = (xmlXPathContextPtr) obj->ptr;
259 
260 	if (ctx) {
261 		docp = (xmlDocPtr) ctx->doc;
262 	}
263 
264 	php_dom_create_object((xmlNodePtr) docp, retval, obj);
265 	return SUCCESS;
266 }
267 /* }}} */
268 
269 /* {{{ registerNodeNamespaces bool*/
php_xpath_obj_from_dom_obj(dom_object * obj)270 static inline dom_xpath_object *php_xpath_obj_from_dom_obj(dom_object *obj) {
271 	return (dom_xpath_object*)((char*)(obj) - XtOffsetOf(dom_xpath_object, dom));
272 }
273 
dom_xpath_register_node_ns_read(dom_object * obj,zval * retval)274 int dom_xpath_register_node_ns_read(dom_object *obj, zval *retval)
275 {
276 	ZVAL_BOOL(retval, php_xpath_obj_from_dom_obj(obj)->register_node_ns);
277 
278 	return SUCCESS;
279 }
280 
dom_xpath_register_node_ns_write(dom_object * obj,zval * newval)281 int dom_xpath_register_node_ns_write(dom_object *obj, zval *newval)
282 {
283 	php_xpath_obj_from_dom_obj(obj)->register_node_ns = zend_is_true(newval);
284 
285 	return SUCCESS;
286 }
287 /* }}} */
288 
289 /* {{{ */
PHP_METHOD(DOMXPath,registerNamespace)290 PHP_METHOD(DOMXPath, registerNamespace)
291 {
292 	zval *id;
293 	xmlXPathContextPtr ctxp;
294 	size_t prefix_len, ns_uri_len;
295 	dom_xpath_object *intern;
296 	unsigned char *prefix, *ns_uri;
297 
298 	id = ZEND_THIS;
299 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &prefix, &prefix_len, &ns_uri, &ns_uri_len) == FAILURE) {
300 		RETURN_THROWS();
301 	}
302 
303 	intern = Z_XPATHOBJ_P(id);
304 
305 	ctxp = (xmlXPathContextPtr) intern->dom.ptr;
306 	if (ctxp == NULL) {
307 		zend_throw_error(NULL, "Invalid XPath Context");
308 		RETURN_THROWS();
309 	}
310 
311 	if (xmlXPathRegisterNs(ctxp, prefix, ns_uri) != 0) {
312 		RETURN_FALSE;
313 	}
314 	RETURN_TRUE;
315 }
316 /* }}} */
317 
dom_xpath_iter(zval * baseobj,dom_object * intern)318 static void dom_xpath_iter(zval *baseobj, dom_object *intern) /* {{{ */
319 {
320 	dom_nnodemap_object *mapptr = (dom_nnodemap_object *) intern->ptr;
321 
322 	ZVAL_COPY_VALUE(&mapptr->baseobj_zv, baseobj);
323 	mapptr->nodetype = DOM_NODESET;
324 }
325 /* }}} */
326 
php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS,int type)327 static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
328 {
329 	zval *id, retval, *context = NULL;
330 	xmlXPathContextPtr ctxp;
331 	xmlNodePtr nodep = NULL;
332 	xmlXPathObjectPtr xpathobjp;
333 	size_t expr_len, nsnbr = 0, xpath_type;
334 	dom_xpath_object *intern;
335 	dom_object *nodeobj;
336 	char *expr;
337 	xmlDoc *docp = NULL;
338 	xmlNsPtr *ns = NULL;
339 	bool register_node_ns;
340 
341 	id = ZEND_THIS;
342 	intern = Z_XPATHOBJ_P(id);
343 	register_node_ns = intern->register_node_ns;
344 
345 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|O!b", &expr, &expr_len, &context, dom_node_class_entry, &register_node_ns) == FAILURE) {
346 		RETURN_THROWS();
347 	}
348 
349 	ctxp = (xmlXPathContextPtr) intern->dom.ptr;
350 	if (ctxp == NULL) {
351 		zend_throw_error(NULL, "Invalid XPath Context");
352 		RETURN_THROWS();
353 	}
354 
355 	docp = (xmlDocPtr) ctxp->doc;
356 	if (docp == NULL) {
357 		php_error_docref(NULL, E_WARNING, "Invalid XPath Document Pointer");
358 		RETURN_FALSE;
359 	}
360 
361 	if (context != NULL) {
362 		DOM_GET_OBJ(nodep, context, xmlNodePtr, nodeobj);
363 	}
364 
365 	if (!nodep) {
366 		nodep = xmlDocGetRootElement(docp);
367 	}
368 
369 	if (nodep && docp != nodep->doc) {
370 		zend_throw_error(NULL, "Node from wrong document");
371 		RETURN_THROWS();
372 	}
373 
374 	ctxp->node = nodep;
375 
376 	if (register_node_ns) {
377 		/* Register namespaces in the node */
378 		ns = xmlGetNsList(docp, nodep);
379 
380 		if (ns != NULL) {
381 			while (ns[nsnbr] != NULL)
382 			nsnbr++;
383 		}
384 	}
385 
386 
387 	ctxp->namespaces = ns;
388 	ctxp->nsNr = nsnbr;
389 
390 	xpathobjp = xmlXPathEvalExpression((xmlChar *) expr, ctxp);
391 	ctxp->node = NULL;
392 
393 	if (ns != NULL) {
394 		xmlFree(ns);
395 		ctxp->namespaces = NULL;
396 		ctxp->nsNr = 0;
397 	}
398 
399 	if (! xpathobjp) {
400 		/* TODO Add Warning? */
401 		RETURN_FALSE;
402 	}
403 
404 	if (type == PHP_DOM_XPATH_QUERY) {
405 		xpath_type = XPATH_NODESET;
406 	} else {
407 		xpath_type = xpathobjp->type;
408 	}
409 
410 	switch (xpath_type) {
411 
412 		case  XPATH_NODESET:
413 		{
414 			int i;
415 			xmlNodeSetPtr nodesetp;
416 
417 			if (xpathobjp->type == XPATH_NODESET && NULL != (nodesetp = xpathobjp->nodesetval) && nodesetp->nodeNr) {
418 
419 				array_init(&retval);
420 				for (i = 0; i < nodesetp->nodeNr; i++) {
421 					xmlNodePtr node = nodesetp->nodeTab[i];
422 					zval child;
423 
424 					if (node->type == XML_NAMESPACE_DECL) {
425 						xmlNodePtr nsparent = node->_private;
426 						xmlNsPtr original = (xmlNsPtr) node;
427 
428 						/* Make sure parent dom object exists, so we can take an extra reference. */
429 						zval parent_zval; /* don't destroy me, my lifetime is transfered to the fake namespace decl */
430 						php_dom_create_object(nsparent, &parent_zval, &intern->dom);
431 						dom_object *parent_intern = Z_DOMOBJ_P(&parent_zval);
432 
433 						node = php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern);
434 					} else {
435 						php_dom_create_object(node, &child, &intern->dom);
436 					}
437 					add_next_index_zval(&retval, &child);
438 				}
439 			} else {
440 				ZVAL_EMPTY_ARRAY(&retval);
441 			}
442 			php_dom_create_iterator(return_value, DOM_NODELIST);
443 			nodeobj = Z_DOMOBJ_P(return_value);
444 			dom_xpath_iter(&retval, nodeobj);
445 			break;
446 		}
447 
448 		case XPATH_BOOLEAN:
449 			RETVAL_BOOL(xpathobjp->boolval);
450 			break;
451 
452 		case XPATH_NUMBER:
453 			RETVAL_DOUBLE(xpathobjp->floatval);
454 			break;
455 
456 		case XPATH_STRING:
457 			RETVAL_STRING((char *) xpathobjp->stringval);
458 			break;
459 
460 		default:
461 			RETVAL_NULL();
462 			break;
463 	}
464 
465 	xmlXPathFreeObject(xpathobjp);
466 }
467 /* }}} */
468 
469 /* {{{ */
PHP_METHOD(DOMXPath,query)470 PHP_METHOD(DOMXPath, query)
471 {
472 	php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_QUERY);
473 }
474 /* }}} end dom_xpath_query */
475 
476 /* {{{ */
PHP_METHOD(DOMXPath,evaluate)477 PHP_METHOD(DOMXPath, evaluate)
478 {
479 	php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_EVALUATE);
480 }
481 /* }}} end dom_xpath_evaluate */
482 
483 /* {{{ */
PHP_METHOD(DOMXPath,registerPhpFunctions)484 PHP_METHOD(DOMXPath, registerPhpFunctions)
485 {
486 	zval *id = ZEND_THIS;
487 	dom_xpath_object *intern = Z_XPATHOBJ_P(id);
488 	zval *entry, new_string;
489 	zend_string *name = NULL;
490 	HashTable *ht = NULL;
491 
492 	ZEND_PARSE_PARAMETERS_START(0, 1)
493 		Z_PARAM_OPTIONAL
494 		Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(ht, name)
495 	ZEND_PARSE_PARAMETERS_END();
496 
497 	if (ht) {
498 		ZEND_HASH_FOREACH_VAL(ht, entry) {
499 			zend_string *str = zval_get_string(entry);
500 			ZVAL_LONG(&new_string, 1);
501 			zend_hash_update(intern->registered_phpfunctions, str, &new_string);
502 			zend_string_release_ex(str, 0);
503 		} ZEND_HASH_FOREACH_END();
504 		intern->registerPhpFunctions = 2;
505 	} else if (name) {
506 		ZVAL_LONG(&new_string, 1);
507 		zend_hash_update(intern->registered_phpfunctions, name, &new_string);
508 		intern->registerPhpFunctions = 2;
509 	} else {
510 		intern->registerPhpFunctions = 1;
511 	}
512 
513 }
514 /* }}} end dom_xpath_register_php_functions */
515 
516 #endif /* LIBXML_XPATH_ENABLED */
517 
518 #endif
519