Skip to content

Commit 6c96369

Browse files
committed
ext/dom: Add global registerNodeNS flag on DOMXPath ctor and property.
1 parent 375ceef commit 6c96369

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

ext/dom/dom_properties.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ int dom_text_whole_text_read(dom_object *obj, zval *retval);
128128
#if defined(LIBXML_XPATH_ENABLED)
129129
/* xpath properties */
130130
int dom_xpath_document_read(dom_object *obj, zval *retval);
131+
int dom_xpath_register_node_ns_read(dom_object *obj, zval *retval);
132+
int dom_xpath_register_node_ns_write(dom_object *obj, zval *newval);
131133
#endif
132134

133135
#endif /* DOM_PROPERTIERS_H */

ext/dom/php_dom.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ PHP_MINIT_FUNCTION(dom)
768768

769769
zend_hash_init(&dom_xpath_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
770770
dom_register_prop_handler(&dom_xpath_prop_handlers, "document", sizeof("document")-1, dom_xpath_document_read, NULL);
771+
dom_register_prop_handler(&dom_xpath_prop_handlers, "registerNodeNamespaces", sizeof("registerNodeNamespaces")-1, dom_xpath_register_node_ns_read, dom_xpath_register_node_ns_write);
771772
zend_hash_add_ptr(&classes, ce.name, &dom_xpath_prop_handlers);
772773
#endif
773774

@@ -1017,6 +1018,7 @@ zend_object *dom_xpath_objects_new(zend_class_entry *class_type)
10171018
dom_xpath_object *intern = zend_object_alloc(sizeof(dom_xpath_object), class_type);
10181019

10191020
intern->registered_phpfunctions = zend_new_array(0);
1021+
intern->register_node_ns = 1;
10201022

10211023
intern->dom.prop_handler = &dom_xpath_prop_handlers;
10221024
intern->dom.std.handlers = &dom_xpath_object_handlers;

ext/dom/php_dom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extern zend_module_entry dom_module_entry;
6565

6666
typedef struct _dom_xpath_object {
6767
int registerPhpFunctions;
68+
int register_node_ns;
6869
HashTable *registered_phpfunctions;
6970
HashTable *node_list;
7071
dom_object dom;

ext/dom/tests/bug55700.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Bug #55700 (XPath namespace prefix conflict, global registerNodeNS flag)
3+
--SKIPIF--
4+
<?php require_once('skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument();
8+
$doc->loadXML('<prefix:root xmlns:prefix="urn:a" />');
9+
10+
$xp = new DOMXPath($doc, true);
11+
$xp->registerNamespace('prefix', 'urn:b');
12+
13+
echo($xp->query('//prefix:root')->length . "\n");
14+
15+
$xp = new DOMXPath($doc, false);
16+
$xp->registerNamespace('prefix', 'urn:b');
17+
18+
echo($xp->query('//prefix:root')->length . "\n");
19+
20+
var_dump($xp->registerNodeNamespaces);
21+
$xp->registerNodeNamespaces = true;
22+
23+
var_dump($xp->registerNodeNamespaces);
24+
25+
echo($xp->query('//prefix:root')->length . "\n");
26+
27+
var_dump($xp);
28+
?>
29+
--EXPECT--
30+
1
31+
0
32+
bool(false)
33+
bool(true)
34+
1
35+
object(DOMXPath)#4 (2) {
36+
["document"]=>
37+
string(22) "(object value omitted)"
38+
["registerNodeNamespaces"]=>
39+
bool(true)
40+
}

ext/dom/xpath.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
/* {{{ arginfo */
3636
ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_construct, 0, 0, 1)
3737
ZEND_ARG_OBJ_INFO(0, doc, DOMDocument, 0)
38+
ZEND_ARG_INFO(0, registerNodeNS)
3839
ZEND_END_ARG_INFO();
3940

4041
ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_xpath_register_ns, 0, 0, 2)
@@ -249,12 +250,13 @@ static void dom_xpath_ext_function_object_php(xmlXPathParserContextPtr ctxt, int
249250
PHP_METHOD(domxpath, __construct)
250251
{
251252
zval *doc;
253+
zend_bool register_node_ns = 1;
252254
xmlDocPtr docp = NULL;
253255
dom_object *docobj;
254256
dom_xpath_object *intern;
255257
xmlXPathContextPtr ctx, oldctx;
256258

257-
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "O", &doc, dom_document_class_entry) == FAILURE) {
259+
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "O|b", &doc, dom_document_class_entry, &register_node_ns) == FAILURE) {
258260
return;
259261
}
260262

@@ -284,6 +286,7 @@ PHP_METHOD(domxpath, __construct)
284286
intern->dom.ptr = ctx;
285287
ctx->userData = (void *)intern;
286288
intern->dom.document = docobj->document;
289+
intern->register_node_ns = register_node_ns;
287290
php_libxml_increment_doc_ref((php_libxml_node_object *) &intern->dom, docp);
288291
}
289292
}
@@ -304,6 +307,26 @@ int dom_xpath_document_read(dom_object *obj, zval *retval)
304307
}
305308
/* }}} */
306309

310+
/* {{{ registerNodeNamespaces bool*/
311+
static inline dom_xpath_object *php_xpath_obj_from_dom_obj(dom_object *obj) {
312+
return (dom_xpath_object*)((char*)(obj) - XtOffsetOf(dom_xpath_object, dom));
313+
}
314+
315+
int dom_xpath_register_node_ns_read(dom_object *obj, zval *retval)
316+
{
317+
ZVAL_BOOL(retval, php_xpath_obj_from_dom_obj(obj)->register_node_ns);
318+
319+
return SUCCESS;
320+
}
321+
322+
int dom_xpath_register_node_ns_write(dom_object *obj, zval *newval)
323+
{
324+
php_xpath_obj_from_dom_obj(obj)->register_node_ns = zend_is_true(newval);
325+
326+
return SUCCESS;
327+
}
328+
/* }}} */
329+
307330
/* {{{ proto bool dom_xpath_register_ns(string prefix, string uri) */
308331
PHP_FUNCTION(dom_xpath_register_ns)
309332
{
@@ -354,15 +377,16 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
354377
char *expr;
355378
xmlDoc *docp = NULL;
356379
xmlNsPtr *ns = NULL;
357-
zend_bool register_node_ns = 1;
380+
zend_bool register_node_ns;
358381

359382
id = ZEND_THIS;
383+
intern = Z_XPATHOBJ_P(id);
384+
register_node_ns = intern->register_node_ns;
385+
360386
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|O!b", &expr, &expr_len, &context, dom_node_class_entry, &register_node_ns) == FAILURE) {
361387
return;
362388
}
363389

364-
intern = Z_XPATHOBJ_P(id);
365-
366390
ctxp = (xmlXPathContextPtr) intern->dom.ptr;
367391
if (ctxp == NULL) {
368392
php_error_docref(NULL, E_WARNING, "Invalid XPath Context");

0 commit comments

Comments
 (0)