xref: /PHP-8.2/ext/dom/xpath.c (revision 1d0fbdf4)
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 	if (UNEXPECTED(!docp)) {
268 		php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
269 		return FAILURE;
270 	}
271 
272 	php_dom_create_object((xmlNodePtr) docp, retval, obj);
273 	return SUCCESS;
274 }
275 /* }}} */
276 
277 /* {{{ registerNodeNamespaces bool*/
php_xpath_obj_from_dom_obj(dom_object * obj)278 static inline dom_xpath_object *php_xpath_obj_from_dom_obj(dom_object *obj) {
279 	return (dom_xpath_object*)((char*)(obj) - XtOffsetOf(dom_xpath_object, dom));
280 }
281 
dom_xpath_register_node_ns_read(dom_object * obj,zval * retval)282 int dom_xpath_register_node_ns_read(dom_object *obj, zval *retval)
283 {
284 	ZVAL_BOOL(retval, php_xpath_obj_from_dom_obj(obj)->register_node_ns);
285 
286 	return SUCCESS;
287 }
288 
dom_xpath_register_node_ns_write(dom_object * obj,zval * newval)289 int dom_xpath_register_node_ns_write(dom_object *obj, zval *newval)
290 {
291 	php_xpath_obj_from_dom_obj(obj)->register_node_ns = zend_is_true(newval);
292 
293 	return SUCCESS;
294 }
295 /* }}} */
296 
297 /* {{{ */
PHP_METHOD(DOMXPath,registerNamespace)298 PHP_METHOD(DOMXPath, registerNamespace)
299 {
300 	zval *id;
301 	xmlXPathContextPtr ctxp;
302 	size_t prefix_len, ns_uri_len;
303 	dom_xpath_object *intern;
304 	unsigned char *prefix, *ns_uri;
305 
306 	id = ZEND_THIS;
307 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &prefix, &prefix_len, &ns_uri, &ns_uri_len) == FAILURE) {
308 		RETURN_THROWS();
309 	}
310 
311 	intern = Z_XPATHOBJ_P(id);
312 
313 	ctxp = (xmlXPathContextPtr) intern->dom.ptr;
314 	if (ctxp == NULL) {
315 		zend_throw_error(NULL, "Invalid XPath Context");
316 		RETURN_THROWS();
317 	}
318 
319 	if (xmlXPathRegisterNs(ctxp, prefix, ns_uri) != 0) {
320 		RETURN_FALSE;
321 	}
322 	RETURN_TRUE;
323 }
324 /* }}} */
325 
dom_xpath_iter(zval * baseobj,dom_object * intern)326 static void dom_xpath_iter(zval *baseobj, dom_object *intern) /* {{{ */
327 {
328 	dom_nnodemap_object *mapptr = (dom_nnodemap_object *) intern->ptr;
329 
330 	ZVAL_COPY_VALUE(&mapptr->baseobj_zv, baseobj);
331 	mapptr->nodetype = DOM_NODESET;
332 }
333 /* }}} */
334 
php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS,int type)335 static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
336 {
337 	zval *id, retval, *context = NULL;
338 	xmlXPathContextPtr ctxp;
339 	xmlNodePtr nodep = NULL;
340 	xmlXPathObjectPtr xpathobjp;
341 	size_t expr_len, nsnbr = 0, xpath_type;
342 	dom_xpath_object *intern;
343 	dom_object *nodeobj;
344 	char *expr;
345 	xmlDoc *docp = NULL;
346 	xmlNsPtr *ns = NULL;
347 	bool register_node_ns;
348 
349 	id = ZEND_THIS;
350 	intern = Z_XPATHOBJ_P(id);
351 	register_node_ns = intern->register_node_ns;
352 
353 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|O!b", &expr, &expr_len, &context, dom_node_class_entry, &register_node_ns) == FAILURE) {
354 		RETURN_THROWS();
355 	}
356 
357 	ctxp = (xmlXPathContextPtr) intern->dom.ptr;
358 	if (ctxp == NULL) {
359 		zend_throw_error(NULL, "Invalid XPath Context");
360 		RETURN_THROWS();
361 	}
362 
363 	docp = (xmlDocPtr) ctxp->doc;
364 	if (docp == NULL) {
365 		php_error_docref(NULL, E_WARNING, "Invalid XPath Document Pointer");
366 		RETURN_FALSE;
367 	}
368 
369 	if (context != NULL) {
370 		DOM_GET_OBJ(nodep, context, xmlNodePtr, nodeobj);
371 	}
372 
373 	if (!nodep) {
374 		nodep = xmlDocGetRootElement(docp);
375 	}
376 
377 	if (nodep && docp != nodep->doc) {
378 		zend_throw_error(NULL, "Node from wrong document");
379 		RETURN_THROWS();
380 	}
381 
382 	ctxp->node = nodep;
383 
384 	if (register_node_ns) {
385 		/* Register namespaces in the node */
386 		ns = xmlGetNsList(docp, nodep);
387 
388 		if (ns != NULL) {
389 			while (ns[nsnbr] != NULL)
390 			nsnbr++;
391 		}
392 	}
393 
394 
395 	ctxp->namespaces = ns;
396 	ctxp->nsNr = nsnbr;
397 
398 	xpathobjp = xmlXPathEvalExpression((xmlChar *) expr, ctxp);
399 	ctxp->node = NULL;
400 
401 	if (ns != NULL) {
402 		xmlFree(ns);
403 		ctxp->namespaces = NULL;
404 		ctxp->nsNr = 0;
405 	}
406 
407 	if (! xpathobjp) {
408 		/* TODO Add Warning? */
409 		RETURN_FALSE;
410 	}
411 
412 	if (type == PHP_DOM_XPATH_QUERY) {
413 		xpath_type = XPATH_NODESET;
414 	} else {
415 		xpath_type = xpathobjp->type;
416 	}
417 
418 	switch (xpath_type) {
419 
420 		case  XPATH_NODESET:
421 		{
422 			int i;
423 			xmlNodeSetPtr nodesetp;
424 
425 			if (xpathobjp->type == XPATH_NODESET && NULL != (nodesetp = xpathobjp->nodesetval) && nodesetp->nodeNr) {
426 
427 				array_init(&retval);
428 				for (i = 0; i < nodesetp->nodeNr; i++) {
429 					xmlNodePtr node = nodesetp->nodeTab[i];
430 					zval child;
431 
432 					if (node->type == XML_NAMESPACE_DECL) {
433 						xmlNodePtr nsparent = node->_private;
434 						xmlNsPtr original = (xmlNsPtr) node;
435 
436 						/* Make sure parent dom object exists, so we can take an extra reference. */
437 						zval parent_zval; /* don't destroy me, my lifetime is transfered to the fake namespace decl */
438 						php_dom_create_object(nsparent, &parent_zval, &intern->dom);
439 						dom_object *parent_intern = Z_DOMOBJ_P(&parent_zval);
440 
441 						node = php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern);
442 					} else {
443 						php_dom_create_object(node, &child, &intern->dom);
444 					}
445 					add_next_index_zval(&retval, &child);
446 				}
447 			} else {
448 				ZVAL_EMPTY_ARRAY(&retval);
449 			}
450 			php_dom_create_iterator(return_value, DOM_NODELIST);
451 			nodeobj = Z_DOMOBJ_P(return_value);
452 			dom_xpath_iter(&retval, nodeobj);
453 			break;
454 		}
455 
456 		case XPATH_BOOLEAN:
457 			RETVAL_BOOL(xpathobjp->boolval);
458 			break;
459 
460 		case XPATH_NUMBER:
461 			RETVAL_DOUBLE(xpathobjp->floatval);
462 			break;
463 
464 		case XPATH_STRING:
465 			RETVAL_STRING((char *) xpathobjp->stringval);
466 			break;
467 
468 		default:
469 			RETVAL_NULL();
470 			break;
471 	}
472 
473 	xmlXPathFreeObject(xpathobjp);
474 }
475 /* }}} */
476 
477 /* {{{ */
PHP_METHOD(DOMXPath,query)478 PHP_METHOD(DOMXPath, query)
479 {
480 	php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_QUERY);
481 }
482 /* }}} end dom_xpath_query */
483 
484 /* {{{ */
PHP_METHOD(DOMXPath,evaluate)485 PHP_METHOD(DOMXPath, evaluate)
486 {
487 	php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_EVALUATE);
488 }
489 /* }}} end dom_xpath_evaluate */
490 
491 /* {{{ */
PHP_METHOD(DOMXPath,registerPhpFunctions)492 PHP_METHOD(DOMXPath, registerPhpFunctions)
493 {
494 	zval *id = ZEND_THIS;
495 	dom_xpath_object *intern = Z_XPATHOBJ_P(id);
496 	zval *entry, new_string;
497 	zend_string *name = NULL;
498 	HashTable *ht = NULL;
499 
500 	ZEND_PARSE_PARAMETERS_START(0, 1)
501 		Z_PARAM_OPTIONAL
502 		Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(ht, name)
503 	ZEND_PARSE_PARAMETERS_END();
504 
505 	if (ht) {
506 		ZEND_HASH_FOREACH_VAL(ht, entry) {
507 			zend_string *str = zval_get_string(entry);
508 			ZVAL_LONG(&new_string, 1);
509 			zend_hash_update(intern->registered_phpfunctions, str, &new_string);
510 			zend_string_release_ex(str, 0);
511 		} ZEND_HASH_FOREACH_END();
512 		intern->registerPhpFunctions = 2;
513 	} else if (name) {
514 		ZVAL_LONG(&new_string, 1);
515 		zend_hash_update(intern->registered_phpfunctions, name, &new_string);
516 		intern->registerPhpFunctions = 2;
517 	} else {
518 		intern->registerPhpFunctions = 1;
519 	}
520 
521 }
522 /* }}} end dom_xpath_register_php_functions */
523 
524 #endif /* LIBXML_XPATH_ENABLED */
525 
526 #endif
527