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 #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 *id = getThis(), *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(id);
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 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oss", &id, dom_xpath_class_entry, &prefix, &prefix_len, &ns_uri, &ns_uri_len) == FAILURE) {
319 return;
320 }
321
322 intern = Z_XPATHOBJ_P(id);
323
324 ctxp = (xmlXPathContextPtr) intern->dom.ptr;
325 if (ctxp == NULL) {
326 php_error_docref(NULL, E_WARNING, "Invalid XPath Context");
327 RETURN_FALSE;
328 }
329
330 if (xmlXPathRegisterNs(ctxp, prefix, ns_uri) != 0) {
331 RETURN_FALSE
332 }
333 RETURN_TRUE;
334 }
335 /* }}} */
336
dom_xpath_iter(zval * baseobj,dom_object * intern)337 static void dom_xpath_iter(zval *baseobj, dom_object *intern) /* {{{ */
338 {
339 dom_nnodemap_object *mapptr = (dom_nnodemap_object *) intern->ptr;
340
341 ZVAL_COPY_VALUE(&mapptr->baseobj_zv, baseobj);
342 mapptr->nodetype = DOM_NODESET;
343 }
344 /* }}} */
345
php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS,int type)346 static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
347 {
348 zval *id, retval, *context = NULL;
349 xmlXPathContextPtr ctxp;
350 xmlNodePtr nodep = NULL;
351 xmlXPathObjectPtr xpathobjp;
352 size_t expr_len, nsnbr = 0, xpath_type;
353 dom_xpath_object *intern;
354 dom_object *nodeobj;
355 char *expr;
356 xmlDoc *docp = NULL;
357 xmlNsPtr *ns = NULL;
358 zend_bool register_node_ns = 1;
359
360 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|O!b", &id, dom_xpath_class_entry, &expr, &expr_len, &context, dom_node_class_entry, ®ister_node_ns) == FAILURE) {
361 return;
362 }
363
364 intern = Z_XPATHOBJ_P(id);
365
366 ctxp = (xmlXPathContextPtr) intern->dom.ptr;
367 if (ctxp == NULL) {
368 php_error_docref(NULL, E_WARNING, "Invalid XPath Context");
369 RETURN_FALSE;
370 }
371
372 docp = (xmlDocPtr) ctxp->doc;
373 if (docp == NULL) {
374 php_error_docref(NULL, E_WARNING, "Invalid XPath Document Pointer");
375 RETURN_FALSE;
376 }
377
378 if (context != NULL) {
379 DOM_GET_OBJ(nodep, context, xmlNodePtr, nodeobj);
380 }
381
382 if (!nodep) {
383 nodep = xmlDocGetRootElement(docp);
384 }
385
386 if (nodep && docp != nodep->doc) {
387 php_error_docref(NULL, E_WARNING, "Node From Wrong Document");
388 RETURN_FALSE;
389 }
390
391 ctxp->node = nodep;
392
393 if (register_node_ns) {
394 /* Register namespaces in the node */
395 ns = xmlGetNsList(docp, nodep);
396
397 if (ns != NULL) {
398 while (ns[nsnbr] != NULL)
399 nsnbr++;
400 }
401 }
402
403
404 ctxp->namespaces = ns;
405 ctxp->nsNr = nsnbr;
406
407 xpathobjp = xmlXPathEvalExpression((xmlChar *) expr, ctxp);
408 ctxp->node = NULL;
409
410 if (ns != NULL) {
411 xmlFree(ns);
412 ctxp->namespaces = NULL;
413 ctxp->nsNr = 0;
414 }
415
416 if (! xpathobjp) {
417 RETURN_FALSE;
418 }
419
420 if (type == PHP_DOM_XPATH_QUERY) {
421 xpath_type = XPATH_NODESET;
422 } else {
423 xpath_type = xpathobjp->type;
424 }
425
426 switch (xpath_type) {
427
428 case XPATH_NODESET:
429 {
430 int i;
431 xmlNodeSetPtr nodesetp;
432
433 if (xpathobjp->type == XPATH_NODESET && NULL != (nodesetp = xpathobjp->nodesetval) && nodesetp->nodeNr) {
434
435 array_init(&retval);
436 for (i = 0; i < nodesetp->nodeNr; i++) {
437 xmlNodePtr node = nodesetp->nodeTab[i];
438 zval child;
439
440 if (node->type == XML_NAMESPACE_DECL) {
441 xmlNsPtr curns;
442 xmlNodePtr nsparent;
443
444 nsparent = node->_private;
445 curns = xmlNewNs(NULL, node->name, NULL);
446 if (node->children) {
447 curns->prefix = xmlStrdup((xmlChar *) node->children);
448 }
449 if (node->children) {
450 node = xmlNewDocNode(docp, NULL, (xmlChar *) node->children, node->name);
451 } else {
452 node = xmlNewDocNode(docp, NULL, (xmlChar *) "xmlns", node->name);
453 }
454 node->type = XML_NAMESPACE_DECL;
455 node->parent = nsparent;
456 node->ns = curns;
457 }
458 php_dom_create_object(node, &child, &intern->dom);
459 add_next_index_zval(&retval, &child);
460 }
461 } else {
462 ZVAL_EMPTY_ARRAY(&retval);
463 }
464 php_dom_create_interator(return_value, DOM_NODELIST);
465 nodeobj = Z_DOMOBJ_P(return_value);
466 dom_xpath_iter(&retval, nodeobj);
467 break;
468 }
469
470 case XPATH_BOOLEAN:
471 RETVAL_BOOL(xpathobjp->boolval);
472 break;
473
474 case XPATH_NUMBER:
475 RETVAL_DOUBLE(xpathobjp->floatval);
476 break;
477
478 case XPATH_STRING:
479 RETVAL_STRING((char *) xpathobjp->stringval);
480 break;
481
482 default:
483 RETVAL_NULL();
484 break;
485 }
486
487 xmlXPathFreeObject(xpathobjp);
488 }
489 /* }}} */
490
491 /* {{{ proto DOMNodeList dom_xpath_query(string expr [,DOMNode context [, bool registerNodeNS]]) */
PHP_FUNCTION(dom_xpath_query)492 PHP_FUNCTION(dom_xpath_query)
493 {
494 php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_QUERY);
495 }
496 /* }}} end dom_xpath_query */
497
498 /* {{{ proto mixed dom_xpath_evaluate(string expr [,DOMNode context [, bool registerNodeNS]]) */
PHP_FUNCTION(dom_xpath_evaluate)499 PHP_FUNCTION(dom_xpath_evaluate)
500 {
501 php_xpath_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_DOM_XPATH_EVALUATE);
502 }
503 /* }}} end dom_xpath_evaluate */
504
505 /* {{{ proto void dom_xpath_register_php_functions() */
PHP_FUNCTION(dom_xpath_register_php_functions)506 PHP_FUNCTION(dom_xpath_register_php_functions)
507 {
508 zval *id;
509 dom_xpath_object *intern;
510 zval *array_value, *entry, new_string;
511 zend_string *name;
512
513 DOM_GET_THIS(id);
514
515 if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "a", &array_value) == SUCCESS) {
516 intern = Z_XPATHOBJ_P(id);
517 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(array_value), entry) {
518 zend_string *str = zval_get_string(entry);
519 ZVAL_LONG(&new_string,1);
520 zend_hash_update(intern->registered_phpfunctions, str, &new_string);
521 zend_string_release_ex(str, 0);
522 } ZEND_HASH_FOREACH_END();
523 intern->registerPhpFunctions = 2;
524 RETURN_TRUE;
525
526 } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "S", &name) == SUCCESS) {
527 intern = Z_XPATHOBJ_P(id);
528
529 ZVAL_LONG(&new_string, 1);
530 zend_hash_update(intern->registered_phpfunctions, name, &new_string);
531 intern->registerPhpFunctions = 2;
532 } else {
533 intern = Z_XPATHOBJ_P(id);
534 intern->registerPhpFunctions = 1;
535 }
536
537 }
538 /* }}} end dom_xpath_register_php_functions */
539
540 #endif /* LIBXML_XPATH_ENABLED */
541
542 #endif
543
544 /*
545 * Local variables:
546 * tab-width: 4
547 * c-basic-offset: 4
548 * End:
549 * vim600: noet sw=4 ts=4 fdm=marker
550 * vim<600: noet sw=4 ts=4
551 */
552