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 /* $Id$ */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "php.h"
27 #if HAVE_LIBXML && HAVE_DOM
28 #include "php_dom.h"
29
30 #define PHP_DOM_XPATH_QUERY 0
31 #define PHP_DOM_XPATH_EVALUATE 1
32
33 /*
34 * class DOMXPath
35 */
36
37 #if defined(LIBXML_XPATH_ENABLED)
38
39 /* {{{ arginfo */
40 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_construct, 0, 0, 1)
41 ZEND_ARG_OBJ_INFO(0, doc, DOMDocument, 0)
42 ZEND_END_ARG_INFO();
43
44 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_register_ns, 0, 0, 2)
45 ZEND_ARG_INFO(0, prefix)
46 ZEND_ARG_INFO(0, uri)
47 ZEND_END_ARG_INFO();
48
49 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_query, 0, 0, 1)
50 ZEND_ARG_INFO(0, expr)
51 ZEND_ARG_OBJ_INFO(0, context, DOMNode, 1)
52 ZEND_ARG_INFO(0, registerNodeNS)
53 ZEND_END_ARG_INFO();
54
55 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_evaluate, 0, 0, 1)
56 ZEND_ARG_INFO(0, expr)
57 ZEND_ARG_OBJ_INFO(0, context, DOMNode, 1)
58 ZEND_ARG_INFO(0, registerNodeNS)
59 ZEND_END_ARG_INFO();
60
61 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_register_php_functions, 0, 0, 0)
62 ZEND_END_ARG_INFO();
63 /* }}} */
64
65 const zend_function_entry php_dom_xpath_class_functions[] = {
66 PHP_ME(domxpath, __construct, arginfo_dom_xpath_construct, ZEND_ACC_PUBLIC)
67 PHP_FALIAS(registerNamespace, dom_xpath_register_ns, arginfo_dom_xpath_register_ns)
68 PHP_FALIAS(query, dom_xpath_query, arginfo_dom_xpath_query)
69 PHP_FALIAS(evaluate, dom_xpath_evaluate, arginfo_dom_xpath_evaluate)
70 PHP_FALIAS(registerPhpFunctions, dom_xpath_register_php_functions, arginfo_dom_xpath_register_php_functions)
71 PHP_FE_END
72 };
73
74
dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt,int nargs,int type)75 static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */
76 {
77 zval retval;
78 int result, i;
79 int error = 0;
80 zend_fcall_info fci;
81 xmlXPathObjectPtr obj;
82 char *str;
83 zend_string *callable = NULL;
84 dom_xpath_object *intern;
85
86
87 if (! zend_is_executing()) {
88 xmlGenericError(xmlGenericErrorContext,
89 "xmlExtFunctionTest: Function called from outside of PHP\n");
90 error = 1;
91 } else {
92 intern = (dom_xpath_object *) ctxt->context->userData;
93 if (intern == NULL) {
94 xmlGenericError(xmlGenericErrorContext,
95 "xmlExtFunctionTest: failed to get the internal object\n");
96 error = 1;
97 }
98 else if (intern->registerPhpFunctions == 0) {
99 xmlGenericError(xmlGenericErrorContext,
100 "xmlExtFunctionTest: PHP Object did not register PHP functions\n");
101 error = 1;
102 }
103 }
104
105 if (error == 1) {
106 for (i = nargs - 1; i >= 0; i--) {
107 obj = valuePop(ctxt);
108 xmlXPathFreeObject(obj);
109 }
110 return;
111 }
112
113 fci.param_count = nargs - 1;
114 if (fci.param_count > 0) {
115 fci.params = safe_emalloc(fci.param_count, sizeof(zval), 0);
116 }
117 /* Reverse order to pop values off ctxt stack */
118 for (i = nargs - 2; i >= 0; i--) {
119 obj = valuePop(ctxt);
120 switch (obj->type) {
121 case XPATH_STRING:
122 ZVAL_STRING(&fci.params[i], (char *)obj->stringval);
123 break;
124 case XPATH_BOOLEAN:
125 ZVAL_BOOL(&fci.params[i], obj->boolval);
126 break;
127 case XPATH_NUMBER:
128 ZVAL_DOUBLE(&fci.params[i], obj->floatval);
129 break;
130 case XPATH_NODESET:
131 if (type == 1) {
132 str = (char *)xmlXPathCastToString(obj);
133 ZVAL_STRING(&fci.params[i], str);
134 xmlFree(str);
135 } else if (type == 2) {
136 int j;
137 array_init(&fci.params[i]);
138 if (obj->nodesetval && obj->nodesetval->nodeNr > 0) {
139 for (j = 0; j < obj->nodesetval->nodeNr; j++) {
140 xmlNodePtr node = obj->nodesetval->nodeTab[j];
141 zval child;
142 /* not sure, if we need this... it's copied from xpath.c */
143 if (node->type == XML_NAMESPACE_DECL) {
144 xmlNsPtr curns;
145 xmlNodePtr nsparent;
146
147 nsparent = node->_private;
148 curns = xmlNewNs(NULL, node->name, NULL);
149 if (node->children) {
150 curns->prefix = xmlStrdup((xmlChar *) node->children);
151 }
152 if (node->children) {
153 node = xmlNewDocNode(node->doc, NULL, (xmlChar *) node->children, node->name);
154 } else {
155 node = xmlNewDocNode(node->doc, NULL, (xmlChar *) "xmlns", node->name);
156 }
157 node->type = XML_NAMESPACE_DECL;
158 node->parent = nsparent;
159 node->ns = curns;
160 }
161 php_dom_create_object(node, &child, &intern->dom);
162 add_next_index_zval(&fci.params[i], &child);
163 }
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 ALLOC_HASHTABLE(intern->node_list);
208 zend_hash_init(intern->node_list, 0, NULL, ZVAL_PTR_DTOR, 0);
209 }
210 Z_ADDREF(retval);
211 zend_hash_next_index_insert(intern->node_list, &retval);
212 obj = Z_DOMOBJ_P(&retval);
213 nodep = dom_object_get_node(obj);
214 valuePush(ctxt, xmlXPathNewNodeSet(nodep));
215 } else if (Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE) {
216 valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(retval) == IS_TRUE));
217 } else if (Z_TYPE(retval) == IS_OBJECT) {
218 php_error_docref(NULL, E_WARNING, "A PHP Object cannot be converted to a XPath-string");
219 valuePush(ctxt, xmlXPathNewString((xmlChar *)""));
220 } else {
221 zend_string *str = zval_get_string(&retval);
222 valuePush(ctxt, xmlXPathNewString((xmlChar *) ZSTR_VAL(str)));
223 zend_string_release(str);
224 }
225 zval_ptr_dtor(&retval);
226 }
227 }
228 zend_string_release(callable);
229 zval_dtor(&fci.function_name);
230 if (fci.param_count > 0) {
231 for (i = 0; i < nargs - 1; i++) {
232 zval_ptr_dtor(&fci.params[i]);
233 }
234 efree(fci.params);
235 }
236 }
237 /* }}} */
238
dom_xpath_ext_function_string_php(xmlXPathParserContextPtr ctxt,int nargs)239 static void dom_xpath_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
240 {
241 dom_xpath_ext_function_php(ctxt, nargs, 1);
242 }
243 /* }}} */
244
dom_xpath_ext_function_object_php(xmlXPathParserContextPtr ctxt,int nargs)245 static void dom_xpath_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */
246 {
247 dom_xpath_ext_function_php(ctxt, nargs, 2);
248 }
249 /* }}} */
250
251 /* {{{ proto void DOMXPath::__construct(DOMDocument doc) U */
PHP_METHOD(domxpath,__construct)252 PHP_METHOD(domxpath, __construct)
253 {
254 zval *id = getThis(), *doc;
255 xmlDocPtr docp = NULL;
256 dom_object *docobj;
257 dom_xpath_object *intern;
258 xmlXPathContextPtr ctx, oldctx;
259
260 if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "O", &doc, dom_document_class_entry) == FAILURE) {
261 return;
262 }
263
264 DOM_GET_OBJ(docp, doc, xmlDocPtr, docobj);
265
266 ctx = xmlXPathNewContext(docp);
267 if (ctx == NULL) {
268 php_dom_throw_error(INVALID_STATE_ERR, 1);
269 RETURN_FALSE;
270 }
271
272 intern = Z_XPATHOBJ_P(id);
273 if (intern != NULL) {
274 oldctx = (xmlXPathContextPtr)intern->dom.ptr;
275 if (oldctx != NULL) {
276 php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom);
277 xmlXPathFreeContext(oldctx);
278 }
279
280 xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "functionString",
281 (const xmlChar *) "http://php.net/xpath",
282 dom_xpath_ext_function_string_php);
283 xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "function",
284 (const xmlChar *) "http://php.net/xpath",
285 dom_xpath_ext_function_object_php);
286
287 intern->dom.ptr = ctx;
288 ctx->userData = (void *)intern;
289 intern->dom.document = docobj->document;
290 php_libxml_increment_doc_ref((php_libxml_node_object *) &intern->dom, docp);
291 }
292 }
293 /* }}} end DOMXPath::__construct */
294
295 /* {{{ document DOMDocument*/
dom_xpath_document_read(dom_object * obj,zval * retval)296 int dom_xpath_document_read(dom_object *obj, zval *retval)
297 {
298 xmlDoc *docp = NULL;
299 xmlXPathContextPtr ctx = (xmlXPathContextPtr) obj->ptr;
300
301 if (ctx) {
302 docp = (xmlDocPtr) ctx->doc;
303 }
304
305 php_dom_create_object((xmlNodePtr) docp, retval, obj);
306 return SUCCESS;
307 }
308 /* }}} */
309
310 /* {{{ proto boolean dom_xpath_register_ns(string prefix, string uri) */
PHP_FUNCTION(dom_xpath_register_ns)311 PHP_FUNCTION(dom_xpath_register_ns)
312 {
313 zval *id;
314 xmlXPathContextPtr ctxp;
315 size_t prefix_len, ns_uri_len;
316 dom_xpath_object *intern;
317 unsigned char *prefix, *ns_uri;
318
319 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oss", &id, dom_xpath_class_entry, &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 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) {
362 return;
363 }
364
365 intern = Z_XPATHOBJ_P(id);
366
367 ctxp = (xmlXPathContextPtr) intern->dom.ptr;
368 if (ctxp == NULL) {
369 php_error_docref(NULL, E_WARNING, "Invalid XPath Context");
370 RETURN_FALSE;
371 }
372
373 docp = (xmlDocPtr) ctxp->doc;
374 if (docp == NULL) {
375 php_error_docref(NULL, E_WARNING, "Invalid XPath Document Pointer");
376 RETURN_FALSE;
377 }
378
379 if (context != NULL) {
380 DOM_GET_OBJ(nodep, context, xmlNodePtr, nodeobj);
381 }
382
383 if (!nodep) {
384 nodep = xmlDocGetRootElement(docp);
385 }
386
387 if (nodep && docp != nodep->doc) {
388 php_error_docref(NULL, E_WARNING, "Node From Wrong Document");
389 RETURN_FALSE;
390 }
391
392 ctxp->node = nodep;
393
394 if (register_node_ns) {
395 /* Register namespaces in the node */
396 ns = xmlGetNsList(docp, nodep);
397
398 if (ns != NULL) {
399 while (ns[nsnbr] != NULL)
400 nsnbr++;
401 }
402 }
403
404
405 ctxp->namespaces = ns;
406 ctxp->nsNr = nsnbr;
407
408 xpathobjp = xmlXPathEvalExpression((xmlChar *) expr, ctxp);
409 ctxp->node = NULL;
410
411 if (ns != NULL) {
412 xmlFree(ns);
413 ctxp->namespaces = NULL;
414 ctxp->nsNr = 0;
415 }
416
417 if (! xpathobjp) {
418 RETURN_FALSE;
419 }
420
421 if (type == PHP_DOM_XPATH_QUERY) {
422 xpath_type = XPATH_NODESET;
423 } else {
424 xpath_type = xpathobjp->type;
425 }
426
427 switch (xpath_type) {
428
429 case XPATH_NODESET:
430 {
431 int i;
432 xmlNodeSetPtr nodesetp;
433
434 array_init(&retval);
435
436 if (xpathobjp->type == XPATH_NODESET && NULL != (nodesetp = xpathobjp->nodesetval)) {
437
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 }
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 [, boolean 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 [, boolean 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(str);
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