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