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