diff --git a/ext/dom/config.m4 b/ext/dom/config.m4 index 041ebff7eb918..8a7d5daac61fa 100644 --- a/ext/dom/config.m4 +++ b/ext/dom/config.m4 @@ -35,7 +35,7 @@ if test "$PHP_DOM" != "no"; then nodelist.c text.c comment.c \ entityreference.c \ notation.c xpath.c dom_iterators.c \ - namednodemap.c \ + namednodemap.c xpath_callbacks.c \ $LEXBOR_SOURCES], $ext_shared,,$PHP_LEXBOR_CFLAGS) PHP_ADD_BUILD_DIR($ext_builddir/$LEXBOR_DIR/ports/posix/lexbor/core) @@ -49,7 +49,7 @@ if test "$PHP_DOM" != "no"; then PHP_ADD_BUILD_DIR($ext_builddir/$LEXBOR_DIR/ns) PHP_ADD_BUILD_DIR($ext_builddir/$LEXBOR_DIR/tag) PHP_SUBST(DOM_SHARED_LIBADD) - PHP_INSTALL_HEADERS([ext/dom/xml_common.h]) + PHP_INSTALL_HEADERS([ext/dom/xml_common.h ext/dom/xpath_callbacks.h]) PHP_ADD_EXTENSION_DEP(dom, libxml) ]) fi diff --git a/ext/dom/config.w32 b/ext/dom/config.w32 index a18e8ebe3a60f..bb0101b960b2f 100644 --- a/ext/dom/config.w32 +++ b/ext/dom/config.w32 @@ -15,7 +15,7 @@ if (PHP_DOM == "yes") { entity.c nodelist.c text.c comment.c \ entityreference.c \ notation.c xpath.c dom_iterators.c \ - namednodemap.c", null, "-Iext/dom/lexbor"); + namednodemap.c xpath_callbacks.c", null, "-Iext/dom/lexbor"); ADD_SOURCES("ext/dom/lexbor/lexbor/ports/windows_nt/lexbor/core", "memory.c", "dom"); ADD_SOURCES("ext/dom/lexbor/lexbor/core", "array_obj.c array.c avl.c bst.c diyfp.c conv.c dobject.c dtoa.c hash.c mem.c mraw.c print.c serialize.c shs.c str.c strtod.c", "dom"); @@ -41,7 +41,7 @@ if (PHP_DOM == "yes") { WARNING("dom support can't be enabled, libxml is not found") } } - PHP_INSTALL_HEADERS("ext/dom", "xml_common.h"); + PHP_INSTALL_HEADERS("ext/dom", "xml_common.h xpath_callbacks.h"); } else { WARNING("dom support can't be enabled, libxml is not enabled") PHP_DOM = "no" diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c index 41dbff36d14be..e473d4b81ac5b 100644 --- a/ext/dom/php_dom.c +++ b/ext/dom/php_dom.c @@ -596,8 +596,10 @@ static int dom_nodelist_has_dimension(zend_object *object, zval *member, int che static zval *dom_nodemap_read_dimension(zend_object *object, zval *offset, int type, zval *rv); static int dom_nodemap_has_dimension(zend_object *object, zval *member, int check_empty); static zend_object *dom_objects_store_clone_obj(zend_object *zobject); + #ifdef LIBXML_XPATH_ENABLED void dom_xpath_objects_free_storage(zend_object *object); +HashTable *dom_xpath_get_gc(zend_object *object, zval **table, int *n); #endif static void *dom_malloc(size_t size) { @@ -889,6 +891,7 @@ PHP_MINIT_FUNCTION(dom) memcpy(&dom_xpath_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers)); dom_xpath_object_handlers.offset = XtOffsetOf(dom_xpath_object, dom) + XtOffsetOf(dom_object, std); dom_xpath_object_handlers.free_obj = dom_xpath_objects_free_storage; + dom_xpath_object_handlers.get_gc = dom_xpath_get_gc; dom_xpath_class_entry = register_class_DOMXPath(); dom_xpath_class_entry->create_object = dom_xpath_objects_new; @@ -1001,32 +1004,6 @@ void node_list_unlink(xmlNodePtr node) } /* }}} end node_list_unlink */ -#ifdef LIBXML_XPATH_ENABLED -/* {{{ dom_xpath_objects_free_storage */ -void dom_xpath_objects_free_storage(zend_object *object) -{ - dom_xpath_object *intern = php_xpath_obj_from_obj(object); - - zend_object_std_dtor(&intern->dom.std); - - if (intern->dom.ptr != NULL) { - xmlXPathFreeContext((xmlXPathContextPtr) intern->dom.ptr); - php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom); - } - - if (intern->registered_phpfunctions) { - zend_hash_destroy(intern->registered_phpfunctions); - FREE_HASHTABLE(intern->registered_phpfunctions); - } - - if (intern->node_list) { - zend_hash_destroy(intern->node_list); - FREE_HASHTABLE(intern->node_list); - } -} -/* }}} */ -#endif - /* {{{ dom_objects_free_storage */ void dom_objects_free_storage(zend_object *object) { @@ -1133,12 +1110,13 @@ static void dom_object_namespace_node_free_storage(zend_object *object) } #ifdef LIBXML_XPATH_ENABLED + /* {{{ zend_object dom_xpath_objects_new(zend_class_entry *class_type) */ zend_object *dom_xpath_objects_new(zend_class_entry *class_type) { dom_xpath_object *intern = zend_object_alloc(sizeof(dom_xpath_object), class_type); - intern->registered_phpfunctions = zend_new_array(0); + php_dom_xpath_callbacks_ctor(&intern->xpath_callbacks); intern->register_node_ns = 1; intern->dom.prop_handler = &dom_xpath_prop_handlers; @@ -1149,6 +1127,7 @@ zend_object *dom_xpath_objects_new(zend_class_entry *class_type) return &intern->dom.std; } /* }}} */ + #endif void dom_nnodemap_objects_free_storage(zend_object *object) /* {{{ */ diff --git a/ext/dom/php_dom.h b/ext/dom/php_dom.h index dc28d21061103..97d6d4b03d960 100644 --- a/ext/dom/php_dom.h +++ b/ext/dom/php_dom.h @@ -53,6 +53,7 @@ extern zend_module_entry dom_module_entry; #include "xml_common.h" #include "ext/libxml/php_libxml.h" +#include "xpath_callbacks.h" #include "zend_exceptions.h" #include "dom_ce.h" /* DOM API_VERSION, please bump it up, if you change anything in the API @@ -64,10 +65,8 @@ extern zend_module_entry dom_module_entry; #define DOM_NODESET XML_XINCLUDE_START typedef struct _dom_xpath_object { - int registerPhpFunctions; + php_dom_xpath_callbacks xpath_callbacks; int register_node_ns; - HashTable *registered_phpfunctions; - HashTable *node_list; dom_object dom; } dom_xpath_object; diff --git a/ext/dom/tests/DOMXPath_callables.phpt b/ext/dom/tests/DOMXPath_callables.phpt new file mode 100644 index 0000000000000..44068577520b0 --- /dev/null +++ b/ext/dom/tests/DOMXPath_callables.phpt @@ -0,0 +1,127 @@ +--TEST-- +registerPHPFunctions() with callables +--EXTENSIONS-- +dom +--FILE-- +registerPhpFunctions(["cycle" => array($this, "dummy")]); + } + + public function dummy(string $var) { + echo "dummy: $var\n"; + } +} + +$doc = new DOMDocument(); +$doc->loadHTML('hello'); + +echo "--- Legit cases: none ---\n"; + +$xpath = new DOMXPath($doc); +$xpath->registerNamespace("php", "http://php.net/xpath"); +try { + $xpath->evaluate("//a[php:function('var_dump', string(@href))]"); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +echo "--- Legit cases: all ---\n"; + +$xpath->registerPHPFunctions(null); +$xpath->evaluate("//a[php:function('var_dump', string(@href))]"); +$xpath->evaluate("//a[php:function('MyClass::dump', string(@href))]"); + +echo "--- Legit cases: set ---\n"; + +$xpath = new DOMXPath($doc); +$xpath->registerNamespace("php", "http://php.net/xpath"); +$xpath->registerPhpFunctions([]); +$xpath->registerPHPFunctions(["xyz" => MyClass::dump(...), "mydump" => function (string $x) { + var_dump($x); +}]); +$xpath->registerPhpFunctions(str_repeat("var_dump", mt_rand(1, 1) /* defeat SCCP */)); +$xpath->evaluate("//a[php:function('mydump', string(@href))]"); +$xpath->evaluate("//a[php:function('xyz', string(@href))]"); +$xpath->evaluate("//a[php:function('var_dump', string(@href))]"); +try { + $xpath->evaluate("//a[php:function('notinset', string(@href))]"); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +echo "--- Legit cases: set with cycle ---\n"; + +$xpath = new MyDOMXPath($doc); +$xpath->registerNamespace("php", "http://php.net/xpath"); +$xpath->registerCycle(); +$xpath->evaluate("//a[php:function('cycle', string(@href))]"); + +echo "--- Error cases ---\n"; + +$xpath = new DOMXPath($doc); +try { + $xpath->registerPhpFunctions("nonexistent"); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +try { + $xpath->registerPhpFunctions(function () {}); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +try { + $xpath->registerPhpFunctions([function () {}]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +try { + $xpath->registerPhpFunctions([var_dump(...)]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +try { + $xpath->registerPhpFunctions(["nonexistent"]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +try { + $xpath->registerPhpFunctions(["" => var_dump(...)]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +--- Legit cases: none --- +No callbacks were registered +--- Legit cases: all --- +string(15) "https://php.net" +string(15) "https://php.net" +--- Legit cases: set --- +string(15) "https://php.net" +string(15) "https://php.net" +string(15) "https://php.net" +No callback handler "notinset" registered +--- Legit cases: set with cycle --- +dummy: https://php.net +--- Error cases --- +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be a callable, function "nonexistent" not found or invalid function name +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be of type array|string|null, Closure given +Object of class Closure could not be converted to string +Object of class Closure could not be converted to string +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be an array with valid callbacks as values, function "nonexistent" not found or invalid function name +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) array key must not be empty diff --git a/ext/dom/tests/domxpath.phpt b/ext/dom/tests/domxpath.phpt index 79c82cccc685e..cead1f8d5b7c6 100644 --- a/ext/dom/tests/domxpath.phpt +++ b/ext/dom/tests/domxpath.phpt @@ -69,5 +69,5 @@ myval float(1) bool(true) float(4) -Unable to call handler non_existent() -Unable to call handler non_existent() +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be a callable, function "non_existent" not found or invalid function name +DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be an array with valid callbacks as values, function "non_existant" not found or invalid function name diff --git a/ext/dom/xpath.c b/ext/dom/xpath.c index 7522ec3f1df86..dddd944882cdc 100644 --- a/ext/dom/xpath.c +++ b/ext/dom/xpath.c @@ -32,176 +32,74 @@ #ifdef LIBXML_XPATH_ENABLED -static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */ +void dom_xpath_objects_free_storage(zend_object *object) { - zval retval; - int result, i; - int error = 0; - zend_fcall_info fci; - xmlXPathObjectPtr obj; - char *str; - zend_string *callable = NULL; - dom_xpath_object *intern; + dom_xpath_object *intern = php_xpath_obj_from_obj(object); + + zend_object_std_dtor(&intern->dom.std); + + if (intern->dom.ptr != NULL) { + xmlXPathFreeContext((xmlXPathContextPtr) intern->dom.ptr); + php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom); + } + + php_dom_xpath_callbacks_dtor(&intern->xpath_callbacks); +} + +HashTable *dom_xpath_get_gc(zend_object *object, zval **table, int *n) +{ + dom_xpath_object *intern = php_xpath_obj_from_obj(object); + return php_dom_xpath_callbacks_get_gc_for_whole_object(&intern->xpath_callbacks, object, table, n); +} + +static void dom_xpath_proxy_factory(xmlNodePtr node, zval *child, dom_object *intern, xmlXPathParserContextPtr ctxt) +{ + (void) ctxt; + + ZEND_ASSERT(node->type != XML_NAMESPACE_DECL); + php_dom_create_object(node, child, intern); +} + +static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, php_dom_xpath_nodeset_evaluation_mode evaluation_mode) /* {{{ */ +{ + bool error = false; + dom_xpath_object *intern; if (! zend_is_executing()) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: Function called from outside of PHP\n"); - error = 1; + error = true; } else { intern = (dom_xpath_object *) ctxt->context->userData; if (intern == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: failed to get the internal object\n"); - error = 1; - } - else if (intern->registerPhpFunctions == 0) { - xmlGenericError(xmlGenericErrorContext, - "xmlExtFunctionTest: PHP Object did not register PHP functions\n"); - error = 1; - } - } - - if (error == 1) { - for (i = nargs - 1; i >= 0; i--) { - obj = valuePop(ctxt); - xmlXPathFreeObject(obj); + error = true; } - return; - } - - if (UNEXPECTED(nargs == 0)) { - zend_throw_error(NULL, "Function name must be passed as the first argument"); - return; - } - - fci.param_count = nargs - 1; - if (fci.param_count > 0) { - fci.params = safe_emalloc(fci.param_count, sizeof(zval), 0); - } - /* Reverse order to pop values off ctxt stack */ - for (i = fci.param_count - 1; i >= 0; i--) { - obj = valuePop(ctxt); - switch (obj->type) { - case XPATH_STRING: - ZVAL_STRING(&fci.params[i], (char *)obj->stringval); - break; - case XPATH_BOOLEAN: - ZVAL_BOOL(&fci.params[i], obj->boolval); - break; - case XPATH_NUMBER: - ZVAL_DOUBLE(&fci.params[i], obj->floatval); - break; - case XPATH_NODESET: - if (type == 1) { - str = (char *)xmlXPathCastToString(obj); - ZVAL_STRING(&fci.params[i], str); - xmlFree(str); - } else if (type == 2) { - int j; - if (obj->nodesetval && obj->nodesetval->nodeNr > 0) { - array_init_size(&fci.params[i], obj->nodesetval->nodeNr); - zend_hash_real_init_packed(Z_ARRVAL_P(&fci.params[i])); - for (j = 0; j < obj->nodesetval->nodeNr; j++) { - xmlNodePtr node = obj->nodesetval->nodeTab[j]; - zval child; - if (node->type == XML_NAMESPACE_DECL) { - xmlNodePtr nsparent = node->_private; - xmlNsPtr original = (xmlNsPtr) node; - - /* Make sure parent dom object exists, so we can take an extra reference. */ - zval parent_zval; /* don't destroy me, my lifetime is transfered to the fake namespace decl */ - php_dom_create_object(nsparent, &parent_zval, &intern->dom); - dom_object *parent_intern = Z_DOMOBJ_P(&parent_zval); - - node = php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern); - } else { - php_dom_create_object(node, &child, &intern->dom); - } - add_next_index_zval(&fci.params[i], &child); - } - } else { - ZVAL_EMPTY_ARRAY(&fci.params[i]); - } - } - break; - default: - ZVAL_STRING(&fci.params[i], (char *)xmlXPathCastToString(obj)); + else if (intern->xpath_callbacks.mode == PHP_DOM_REG_FUNC_MODE_NONE) { + zend_throw_error(NULL, "No callbacks were registered"); + error = true; } - xmlXPathFreeObject(obj); } - fci.size = sizeof(fci); - - /* Last element of the stack is the function name */ - obj = valuePop(ctxt); - if (obj->stringval == NULL) { - zend_type_error("Handler name must be a string"); - xmlXPathFreeObject(obj); - goto cleanup_no_callable; - } - ZVAL_STRING(&fci.function_name, (char *) obj->stringval); - xmlXPathFreeObject(obj); - - fci.object = NULL; - fci.named_params = NULL; - fci.retval = &retval; - - if (!zend_make_callable(&fci.function_name, &callable)) { - zend_throw_error(NULL, "Unable to call handler %s()", ZSTR_VAL(callable)); - goto cleanup; - } else if (intern->registerPhpFunctions == 2 && zend_hash_exists(intern->registered_phpfunctions, callable) == 0) { - zend_throw_error(NULL, "Not allowed to call handler '%s()'.", ZSTR_VAL(callable)); - goto cleanup; + if (error) { + php_dom_xpath_callbacks_clean_argument_stack(ctxt, nargs); } else { - result = zend_call_function(&fci, NULL); - if (result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { - if (Z_TYPE(retval) == IS_OBJECT && instanceof_function(Z_OBJCE(retval), dom_node_class_entry)) { - xmlNode *nodep; - dom_object *obj; - if (intern->node_list == NULL) { - intern->node_list = zend_new_array(0); - } - Z_ADDREF(retval); - zend_hash_next_index_insert(intern->node_list, &retval); - obj = Z_DOMOBJ_P(&retval); - nodep = dom_object_get_node(obj); - valuePush(ctxt, xmlXPathNewNodeSet(nodep)); - } else if (Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE) { - valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(retval) == IS_TRUE)); - } else if (Z_TYPE(retval) == IS_OBJECT) { - zend_type_error("A PHP Object cannot be converted to a XPath-string"); - return; - } else { - zend_string *str = zval_get_string(&retval); - valuePush(ctxt, xmlXPathNewString((xmlChar *) ZSTR_VAL(str))); - zend_string_release_ex(str, 0); - } - zval_ptr_dtor(&retval); - } - } -cleanup: - zend_string_release_ex(callable, 0); - zval_ptr_dtor_nogc(&fci.function_name); -cleanup_no_callable: - if (fci.param_count > 0) { - for (i = 0; i < nargs - 1; i++) { - zval_ptr_dtor(&fci.params[i]); - } - efree(fci.params); + php_dom_xpath_callbacks_call(&intern->xpath_callbacks, ctxt, nargs, evaluation_mode, &intern->dom, dom_xpath_proxy_factory); } } /* }}} */ static void dom_xpath_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */ { - dom_xpath_ext_function_php(ctxt, nargs, 1); + dom_xpath_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_STRING); } /* }}} */ static void dom_xpath_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */ { - dom_xpath_ext_function_php(ctxt, nargs, 2); + dom_xpath_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET); } /* }}} */ @@ -482,33 +380,8 @@ PHP_METHOD(DOMXPath, evaluate) /* {{{ */ PHP_METHOD(DOMXPath, registerPhpFunctions) { - zval *id = ZEND_THIS; - dom_xpath_object *intern = Z_XPATHOBJ_P(id); - zval *entry, new_string; - zend_string *name = NULL; - HashTable *ht = NULL; - - ZEND_PARSE_PARAMETERS_START(0, 1) - Z_PARAM_OPTIONAL - Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(ht, name) - ZEND_PARSE_PARAMETERS_END(); - - if (ht) { - ZEND_HASH_FOREACH_VAL(ht, entry) { - zend_string *str = zval_get_string(entry); - ZVAL_LONG(&new_string, 1); - zend_hash_update(intern->registered_phpfunctions, str, &new_string); - zend_string_release_ex(str, 0); - } ZEND_HASH_FOREACH_END(); - intern->registerPhpFunctions = 2; - } else if (name) { - ZVAL_LONG(&new_string, 1); - zend_hash_update(intern->registered_phpfunctions, name, &new_string); - intern->registerPhpFunctions = 2; - } else { - intern->registerPhpFunctions = 1; - } - + dom_xpath_object *intern = Z_XPATHOBJ_P(ZEND_THIS); + php_dom_xpath_callbacks_update_method_handler(&intern->xpath_callbacks, INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} end dom_xpath_register_php_functions */ diff --git a/ext/dom/xpath_callbacks.c b/ext/dom/xpath_callbacks.c new file mode 100644 index 0000000000000..f1b42a0883989 --- /dev/null +++ b/ext/dom/xpath_callbacks.c @@ -0,0 +1,314 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Christian Stocker | + | Rob Richards | + | Niels Dossche | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#if defined(HAVE_LIBXML) && defined(HAVE_DOM) + +#include "php_dom.h" +#include + +static void xpath_callbacks_entry_dtor(zval *zv) +{ + zend_fcall_info_cache *fcc = Z_PTR_P(zv); + zend_fcc_dtor(fcc); + efree(fcc); +} + +PHP_DOM_EXPORT void php_dom_xpath_callbacks_ctor(php_dom_xpath_callbacks *registry) +{ + ALLOC_HASHTABLE(registry->functions); + zend_hash_init(registry->functions, 0, NULL, xpath_callbacks_entry_dtor, false); +} + +PHP_DOM_EXPORT void php_dom_xpath_callbacks_clean_node_list(php_dom_xpath_callbacks *registry) +{ + if (registry->node_list) { + zend_hash_destroy(registry->node_list); + FREE_HASHTABLE(registry->node_list); + registry->node_list = NULL; + } +} + +PHP_DOM_EXPORT void php_dom_xpath_callbacks_clean_argument_stack(xmlXPathParserContextPtr ctxt, int num_args) +{ + for (int i = 0; i < num_args; i++) { + xmlXPathObjectPtr obj = valuePop(ctxt); + xmlXPathFreeObject(obj); + } + /* Push sentinel value */ + valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); +} + +PHP_DOM_EXPORT void php_dom_xpath_callbacks_dtor(php_dom_xpath_callbacks *registry) +{ + if (registry->functions) { + zend_hash_destroy(registry->functions); + FREE_HASHTABLE(registry->functions); + } + php_dom_xpath_callbacks_clean_node_list(registry); +} + +PHP_DOM_EXPORT void php_dom_xpath_callbacks_get_gc(php_dom_xpath_callbacks *registry, zend_get_gc_buffer *gc_buffer) +{ + zval *entry; + ZEND_HASH_FOREACH_VAL(registry->functions, entry) { + ZEND_ASSERT(Z_TYPE_P(entry) == IS_PTR); + zend_get_gc_buffer_add_fcc(gc_buffer, Z_PTR_P(entry)); + } ZEND_HASH_FOREACH_END(); +} + +PHP_DOM_EXPORT HashTable *php_dom_xpath_callbacks_get_gc_for_whole_object(php_dom_xpath_callbacks *registry, zend_object *object, zval **table, int *n) +{ + if (registry->mode == PHP_DOM_REG_FUNC_MODE_SET) { + zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create(); + php_dom_xpath_callbacks_get_gc(registry, gc_buffer); + zend_get_gc_buffer_use(gc_buffer, table, n); + + if (object->properties == NULL && object->ce->default_properties_count == 0) { + return NULL; + } else { + return zend_std_get_properties(object); + } + } else { + return zend_std_get_gc(object, table, n); + } +} + +PHP_DOM_EXPORT void php_dom_xpath_callbacks_update_method_handler(php_dom_xpath_callbacks* registry, INTERNAL_FUNCTION_PARAMETERS) +{ + zval *entry, registered_value; + zend_string *name = NULL; + HashTable *ht = NULL; + + ZEND_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(ht, name) + ZEND_PARSE_PARAMETERS_END(); + + if (ht) { + zend_string *key; + ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, entry) { + zend_fcall_info_cache* fcc = emalloc(sizeof(zend_fcall_info)); + char *error; + if (!zend_is_callable_ex(entry, NULL, 0, NULL, fcc, &error)) { + zend_argument_type_error(1, "must be an array with valid callbacks as values, %s", error); + efree(fcc); + efree(error); + RETURN_THROWS(); + } + + zend_fcc_addref(fcc); + ZVAL_PTR(®istered_value, fcc); + + if (!key) { + zend_string *str = zval_try_get_string(entry); + if (str) { + zend_hash_update(registry->functions, str, ®istered_value); + zend_string_release_ex(str, 0); + } else { + zend_fcc_dtor(fcc); + efree(fcc); + RETURN_THROWS(); + } + } else { + if (ZSTR_LEN(key) == 0) { + zend_argument_value_error(1, "array key must not be empty"); + zend_fcc_dtor(fcc); + efree(fcc); + RETURN_THROWS(); + } + zend_hash_update(registry->functions, key, ®istered_value); + } + } ZEND_HASH_FOREACH_END(); + registry->mode = PHP_DOM_REG_FUNC_MODE_SET; + } else if (name) { + zend_fcall_info_cache* fcc = emalloc(sizeof(zend_fcall_info)); + char *error; + zval tmp; + ZVAL_STR(&tmp, name); + if (!zend_is_callable_ex(&tmp, NULL, 0, NULL, fcc, &error)) { + zend_argument_type_error(1, "must be a callable, %s", error); + efree(fcc); + efree(error); + RETURN_THROWS(); + } + zend_fcc_addref(fcc); + ZVAL_PTR(®istered_value, fcc); + zend_hash_update(registry->functions, name, ®istered_value); + registry->mode = PHP_DOM_REG_FUNC_MODE_SET; + } else { + registry->mode = PHP_DOM_REG_FUNC_MODE_ALL; + } +} + +PHP_DOM_EXPORT zend_result php_dom_xpath_callbacks_call(php_dom_xpath_callbacks *xpath_callbacks, xmlXPathParserContextPtr ctxt, int num_args, php_dom_xpath_nodeset_evaluation_mode evaluation_mode, dom_object *intern, php_dom_xpath_callbacks_proxy_factory proxy_factory) +{ + zval callback_retval; + zval *params = NULL; + zend_result result = FAILURE; + + if (UNEXPECTED(num_args == 0)) { + zend_throw_error(NULL, "Function name must be passed as the first argument"); + goto cleanup_no_obj; + } + + uint32_t param_count = num_args - 1; + if (param_count > 0) { + params = safe_emalloc(param_count, sizeof(zval), 0); + } + + /* Reverse order to pop values off ctxt stack */ + for (int i = param_count - 1; i >= 0; i--) { + xmlXPathObjectPtr obj = valuePop(ctxt); + ZEND_ASSERT(obj != NULL); + switch (obj->type) { + case XPATH_STRING: + ZVAL_STRING(¶ms[i], (char *)obj->stringval); + break; + case XPATH_BOOLEAN: + ZVAL_BOOL(¶ms[i], obj->boolval); + break; + case XPATH_NUMBER: + ZVAL_DOUBLE(¶ms[i], obj->floatval); + break; + case XPATH_NODESET: + if (evaluation_mode == PHP_DOM_XPATH_EVALUATE_NODESET_TO_STRING) { + char *str = (char *)xmlXPathCastToString(obj); + ZVAL_STRING(¶ms[i], str); + xmlFree(str); + } else if (evaluation_mode == PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET) { + if (obj->nodesetval && obj->nodesetval->nodeNr > 0) { + array_init_size(¶ms[i], obj->nodesetval->nodeNr); + zend_hash_real_init_packed(Z_ARRVAL_P(¶ms[i])); + for (int j = 0; j < obj->nodesetval->nodeNr; j++) { + xmlNodePtr node = obj->nodesetval->nodeTab[j]; + zval child; + if (UNEXPECTED(node->type == XML_NAMESPACE_DECL)) { + xmlNodePtr nsparent = node->_private; + xmlNsPtr original = (xmlNsPtr) node; + + /* Make sure parent dom object exists, so we can take an extra reference. */ + zval parent_zval; /* don't destroy me, my lifetime is transfered to the fake namespace decl */ + php_dom_create_object(nsparent, &parent_zval, intern); + dom_object *parent_intern = Z_DOMOBJ_P(&parent_zval); + + php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern); + } else { + proxy_factory(node, &child, intern, ctxt); + } + add_next_index_zval(¶ms[i], &child); + } + } else { + ZVAL_EMPTY_ARRAY(¶ms[i]); + } + } + break; + default: + ZVAL_STRING(¶ms[i], (char *)xmlXPathCastToString(obj)); + break; + } + xmlXPathFreeObject(obj); + } + + /* Last element of the stack is the function name */ + xmlXPathObjectPtr obj = valuePop(ctxt); + if (obj->stringval == NULL) { + zend_type_error("Handler name must be a string"); + goto cleanup; + } + + const char *function_name = (const char *) obj->stringval; + size_t function_name_length = strlen(function_name); + + if (xpath_callbacks->mode == PHP_DOM_REG_FUNC_MODE_ALL) { + zend_fcall_info fci; + fci.size = sizeof(fci); + fci.object = NULL; + fci.retval = &callback_retval; + fci.param_count = param_count; + fci.params = params; + fci.named_params = NULL; + ZVAL_STRINGL(&fci.function_name, function_name, function_name_length); + + zend_result result = zend_call_function(&fci, NULL); + zend_string_release_ex(Z_STR(fci.function_name), false); + if (UNEXPECTED(result == FAILURE)) { + goto cleanup; + } + } else { + ZEND_ASSERT(xpath_callbacks->mode == PHP_DOM_REG_FUNC_MODE_SET); + + zval *fcc_wrapper = zend_hash_str_find(xpath_callbacks->functions, function_name, function_name_length); + if (fcc_wrapper) { + zend_call_known_fcc(Z_PTR_P(fcc_wrapper), &callback_retval, param_count, params, NULL); + } else { + zend_throw_error(NULL, "No callback handler \"%s\" registered", function_name); + goto cleanup; + } + } + + if (Z_TYPE(callback_retval) != IS_UNDEF) { + if (Z_TYPE(callback_retval) == IS_OBJECT && instanceof_function(Z_OBJCE(callback_retval), dom_node_class_entry)) { + xmlNode *nodep; + dom_object *obj; + if (xpath_callbacks->node_list == NULL) { + xpath_callbacks->node_list = zend_new_array(0); + } + Z_ADDREF_P(&callback_retval); + zend_hash_next_index_insert(xpath_callbacks->node_list, &callback_retval); + obj = Z_DOMOBJ_P(&callback_retval); + nodep = dom_object_get_node(obj); + valuePush(ctxt, xmlXPathNewNodeSet(nodep)); + } else if (Z_TYPE(callback_retval) == IS_FALSE || Z_TYPE(callback_retval) == IS_TRUE) { + valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(callback_retval) == IS_TRUE)); + } else if (Z_TYPE(callback_retval) == IS_OBJECT) { + zend_type_error("Only objects that are instances of DOMNode can be converted to an XPath expression"); + zval_ptr_dtor(&callback_retval); + goto cleanup; + } else { + zend_string *str = zval_get_string(&callback_retval); + valuePush(ctxt, xmlXPathNewString((xmlChar *) ZSTR_VAL(str))); + zend_string_release_ex(str, 0); + } + zval_ptr_dtor(&callback_retval); + } + + result = SUCCESS; + +cleanup: + xmlXPathFreeObject(obj); +cleanup_no_obj: + if (UNEXPECTED(result != SUCCESS)) { + /* Push sentinel value */ + valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); + } + if (params) { + for (int i = 0; i < param_count; i++) { + zval_ptr_dtor(¶ms[i]); + } + efree(params); + } + + return result; +} + +#endif diff --git a/ext/dom/xpath_callbacks.h b/ext/dom/xpath_callbacks.h new file mode 100644 index 0000000000000..9f949754f639f --- /dev/null +++ b/ext/dom/xpath_callbacks.h @@ -0,0 +1,55 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Niels Dossche | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_DOM_XPATH_CALLBACKS_H +#define PHP_DOM_XPATH_CALLBACKS_H + +#if defined(HAVE_LIBXML) && defined(HAVE_DOM) + +#include +#include "xml_common.h" + +typedef enum { + PHP_DOM_REG_FUNC_MODE_NONE = 0, + PHP_DOM_REG_FUNC_MODE_ALL, + PHP_DOM_REG_FUNC_MODE_SET, +} php_dom_register_functions_mode; + +typedef enum { + PHP_DOM_XPATH_EVALUATE_NODESET_TO_STRING, + PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET, +} php_dom_xpath_nodeset_evaluation_mode; + +typedef void (*php_dom_xpath_callbacks_proxy_factory)(xmlNodePtr node, zval *proxy, dom_object *intern, xmlXPathParserContextPtr ctxt); + +typedef struct { + php_dom_register_functions_mode mode; + HashTable *functions; + HashTable *node_list; +} php_dom_xpath_callbacks; + +PHP_DOM_EXPORT void php_dom_xpath_callbacks_ctor(php_dom_xpath_callbacks *registry); +PHP_DOM_EXPORT void php_dom_xpath_callbacks_dtor(php_dom_xpath_callbacks *registry); +PHP_DOM_EXPORT void php_dom_xpath_callbacks_clean_node_list(php_dom_xpath_callbacks *registry); +PHP_DOM_EXPORT void php_dom_xpath_callbacks_clean_argument_stack(xmlXPathParserContextPtr ctxt, int num_args); +PHP_DOM_EXPORT void php_dom_xpath_callbacks_get_gc(php_dom_xpath_callbacks *registry, zend_get_gc_buffer *gc_buffer); +PHP_DOM_EXPORT HashTable *php_dom_xpath_callbacks_get_gc_for_whole_object(php_dom_xpath_callbacks *registry, zend_object *object, zval **table, int *n); +PHP_DOM_EXPORT void php_dom_xpath_callbacks_update_method_handler(php_dom_xpath_callbacks* registry, INTERNAL_FUNCTION_PARAMETERS); +PHP_DOM_EXPORT zend_result php_dom_xpath_callbacks_call(php_dom_xpath_callbacks *xpath_callbacks, xmlXPathParserContextPtr ctxt, int num_args, php_dom_xpath_nodeset_evaluation_mode evaluation_mode, dom_object *intern, php_dom_xpath_callbacks_proxy_factory proxy_factory); + +#endif +#endif + diff --git a/ext/xsl/php_xsl.c b/ext/xsl/php_xsl.c index 93945002b17e1..b294401e9c5a9 100644 --- a/ext/xsl/php_xsl.c +++ b/ext/xsl/php_xsl.c @@ -53,6 +53,12 @@ zend_module_entry xsl_module_entry = { ZEND_GET_MODULE(xsl) #endif +static HashTable *xsl_objects_get_gc(zend_object *object, zval **table, int *n) +{ + xsl_object *intern = php_xsl_fetch_object(object); + return php_dom_xpath_callbacks_get_gc_for_whole_object(&intern->xpath_callbacks, object, table, n); +} + /* {{{ xsl_objects_free_storage */ void xsl_objects_free_storage(zend_object *object) { @@ -65,15 +71,7 @@ void xsl_objects_free_storage(zend_object *object) FREE_HASHTABLE(intern->parameter); } - if (intern->registered_phpfunctions) { - zend_hash_destroy(intern->registered_phpfunctions); - FREE_HASHTABLE(intern->registered_phpfunctions); - } - - if (intern->node_list) { - zend_hash_destroy(intern->node_list); - FREE_HASHTABLE(intern->node_list); - } + php_dom_xpath_callbacks_dtor(&intern->xpath_callbacks); if (intern->doc) { php_libxml_decrement_doc_ref(intern->doc); @@ -106,7 +104,7 @@ zend_object *xsl_objects_new(zend_class_entry *class_type) zend_object_std_init(&intern->std, class_type); object_properties_init(&intern->std, class_type); intern->parameter = zend_new_array(0); - intern->registered_phpfunctions = zend_new_array(0); + php_dom_xpath_callbacks_ctor(&intern->xpath_callbacks); return &intern->std; } @@ -119,6 +117,7 @@ PHP_MINIT_FUNCTION(xsl) xsl_object_handlers.offset = XtOffsetOf(xsl_object, std); xsl_object_handlers.clone_obj = NULL; xsl_object_handlers.free_obj = xsl_objects_free_storage; + xsl_object_handlers.get_gc = xsl_objects_get_gc; xsl_xsltprocessor_class_entry = register_class_XSLTProcessor(); xsl_xsltprocessor_class_entry->create_object = xsl_objects_new; diff --git a/ext/xsl/php_xsl.h b/ext/xsl/php_xsl.h index ed8dc9874bb90..2d264b2976d16 100644 --- a/ext/xsl/php_xsl.h +++ b/ext/xsl/php_xsl.h @@ -38,6 +38,7 @@ extern zend_module_entry xsl_module_entry; #endif #include "../dom/xml_common.h" +#include "../dom/xpath_callbacks.h" #include #include @@ -53,18 +54,14 @@ extern zend_module_entry xsl_module_entry; typedef struct _xsl_object { void *ptr; - HashTable *prop_handler; - zval handle; HashTable *parameter; int hasKeys; - int registerPhpFunctions; - HashTable *registered_phpfunctions; - HashTable *node_list; + int securityPrefsSet; + zend_long securityPrefs; + php_dom_xpath_callbacks xpath_callbacks; php_libxml_node_object *doc; char *profiling; - zend_long securityPrefs; - int securityPrefsSet; - zend_object std; + zend_object std; } xsl_object; static inline xsl_object *php_xsl_fetch_object(zend_object *obj) { diff --git a/ext/xsl/tests/XSLTProcessor_callables.phpt b/ext/xsl/tests/XSLTProcessor_callables.phpt new file mode 100644 index 0000000000000..1603fd4436451 --- /dev/null +++ b/ext/xsl/tests/XSLTProcessor_callables.phpt @@ -0,0 +1,150 @@ +--TEST-- +registerPhpFunctions() with callables +--EXTENSIONS-- +xsl +--FILE-- +registerPhpFunctions(["cycle" => array($this, "dummy")]); + } + + public function dummy(string $var) { + return "dummy: $var"; + } +} + +function createProcessor($inputs, $class = "XSLTProcessor") { + $xsl = new DomDocument(); + $xsl->loadXML(' + + ' + . implode('', array_map(fn($input) => '', $inputs)) . + ' + '); + + $proc = new $class(); + $proc->importStylesheet($xsl); + return $proc; +} + +$inputdom = new DomDocument(); +$inputdom->loadXML('hello'); + +echo "--- Legit cases: none ---\n"; + +$proc = createProcessor(["'var_dump', string(@href)"]); +try { + $proc->transformToXml($inputdom); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} + +echo "--- Legit cases: all ---\n"; + +$proc = createProcessor(["'var_dump', string(@href)", "'MyClass::dump', string(@href)"]); +$proc->registerPHPFunctions(); +var_dump($proc->transformToXml($inputdom)); + +echo "--- Legit cases: set ---\n"; + +$proc = createProcessor(["'mydump', string(@href)", "'xyz', string(@href)", "'var_dump', string(@href)"]); +$proc->registerPhpFunctions([]); +$proc->registerPHPFunctions(["xyz" => MyClass::dump(...), "mydump" => function (string $x) { + var_dump($x); +}]); +$proc->registerPhpFunctions(str_repeat("var_dump", mt_rand(1, 1) /* defeat SCCP */)); +var_dump($proc->transformToXml($inputdom)); + +$proc = createProcessor(["'notinset', string(@href)"]); +$proc->registerPhpFunctions([]); +try { + var_dump($proc->transformToXml($inputdom)); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +echo "--- Legit cases: set with cycle ---\n"; + +$proc = createProcessor(["'cycle', string(@href)"], 'MyXSLTProcessor'); +$proc->registerCycle(); +var_dump($proc->transformToXml($inputdom)); + +echo "--- Error cases ---\n"; + +$proc = createProcessor([]); +try { + $proc->registerPhpFunctions("nonexistent"); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +try { + $proc->registerPhpFunctions(function () {}); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +try { + $proc->registerPhpFunctions([function () {}]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +try { + $proc->registerPhpFunctions([var_dump(...)]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +try { + $proc->registerPhpFunctions(["nonexistent"]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +try { + $proc->registerPhpFunctions(["" => var_dump(...)]); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +--- Legit cases: none --- +No callbacks were registered +--- Legit cases: all --- +string(15) "https://php.net" +string(15) "https://php.net" +string(44) " +dump: https://php.net +" +--- Legit cases: set --- +string(15) "https://php.net" +string(15) "https://php.net" +string(15) "https://php.net" +string(44) " +dump: https://php.net +" +No callback handler "notinset" registered +--- Legit cases: set with cycle --- +string(45) " +dummy: https://php.net +" +--- Error cases --- +XSLTProcessor::registerPHPFunctions(): Argument #1 ($functions) must be a callable, function "nonexistent" not found or invalid function name +XSLTProcessor::registerPHPFunctions(): Argument #1 ($functions) must be of type array|string|null, Closure given +Object of class Closure could not be converted to string +Object of class Closure could not be converted to string +XSLTProcessor::registerPHPFunctions(): Argument #1 ($functions) must be an array with valid callbacks as values, function "nonexistent" not found or invalid function name +XSLTProcessor::registerPHPFunctions(): Argument #1 ($functions) array key must not be empty diff --git a/ext/xsl/tests/php_function_edge_cases.phpt b/ext/xsl/tests/php_function_edge_cases.phpt index 23a06b111bb50..a8e0eb098d162 100644 --- a/ext/xsl/tests/php_function_edge_cases.phpt +++ b/ext/xsl/tests/php_function_edge_cases.phpt @@ -22,12 +22,8 @@ function test($input) { $proc = new XsltProcessor(); $proc->registerPhpFunctions(); - $xsl = $proc->importStylesheet($xsl); - try { - $proc->transformToDoc($inputdom); - } catch (Exception $e) { - echo $e->getMessage(), "\n"; - } + $proc->importStylesheet($xsl); + $proc->transformToDoc($inputdom); } try { @@ -36,10 +32,13 @@ try { echo $e->getMessage(), "\n"; } -test("3"); +try { + test("3"); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- +--EXPECT-- Function name must be passed as the first argument - -Warning: XSLTProcessor::transformToDoc(): Handler name must be a string in %s on line %d +Handler name must be a string diff --git a/ext/xsl/tests/throw_in_autoload.phpt b/ext/xsl/tests/throw_in_autoload.phpt index 90ab0098b8e40..2df7d3690bed3 100644 --- a/ext/xsl/tests/throw_in_autoload.phpt +++ b/ext/xsl/tests/throw_in_autoload.phpt @@ -28,12 +28,14 @@ $proc->registerPhpFunctions(); $xsl = $proc->importStylesheet($xsl); try { $newdom = $proc->transformToDoc($inputdom); -} catch (Exception $e) { +} catch (Error $e) { echo $e->getMessage(), "\n"; + echo $e->getPrevious()->getMessage(), "\n"; } ?> ===DONE=== --EXPECT-- string(4) "TeSt" +Invalid callback TeSt::dateLang, class "TeSt" not found Autoload exception ===DONE=== diff --git a/ext/xsl/tests/xslt011.phpt b/ext/xsl/tests/xslt011.phpt index 50279492b7bd3..d7b42f0395ce6 100644 --- a/ext/xsl/tests/xslt011.phpt +++ b/ext/xsl/tests/xslt011.phpt @@ -5,51 +5,42 @@ xsl --FILE-- load(__DIR__."/xslt011.xsl"); - $proc = new xsltprocessor; - $xsl = $proc->importStylesheet($dom); +$dom->load(__DIR__."/xslt011.xsl"); +$proc = new xsltprocessor; +$xsl = $proc->importStylesheet($dom); - $xml = new DomDocument(); - $xml->load(__DIR__."/xslt011.xml"); - $proc->registerPHPFunctions(); - print $proc->transformToXml($xml); +$xml = new DomDocument(); +$xml->load(__DIR__."/xslt011.xml"); +$proc->registerPHPFunctions(); +print $proc->transformToXml($xml); - function foobar($id, $secondArg = "" ) { - if (is_array($id)) { - return $id[0]->value . " - " . $secondArg; +function foobar($id, $secondArg = "" ) { + if (is_array($id)) { + return $id[0]->value . " - " . $secondArg; + } else { + return $id . " - " . $secondArg; + } +} +function nodeSet($id = null) { + if ($id and is_array($id)) { + return $id[0]; } else { - return $id . " - " . $secondArg; + $dom = new domdocument; + $dom->loadXML("this is from an external DomDocument"); + return $dom->documentElement; } - } - function nodeSet($id = null) { - if ($id and is_array($id)) { - return $id[0]; - } else { - $dom = new domdocument; - $dom->loadXML("this is from an external DomDocument"); - return $dom->documentElement; - } - } - function nonDomNode() { - return new foo(); - } +} - class aClass { - static function aStaticFunction($id) { - return $id; - } +class aClass { + static function aStaticFunction($id) { + return $id; } +} ?> --EXPECTF-- Test 11: php:function Support - -Warning: XSLTProcessor::transformToXml(): A PHP Object cannot be converted to a XPath-string in %s on line 16 foobar - secondArg foobar - diff --git a/ext/xsl/tests/xslt011.xsl b/ext/xsl/tests/xslt011.xsl index e1960e57d3beb..39330d5eede17 100644 --- a/ext/xsl/tests/xslt011.xsl +++ b/ext/xsl/tests/xslt011.xsl @@ -19,7 +19,5 @@ - - diff --git a/ext/xsl/tests/xslt_non_dom_node.phpt b/ext/xsl/tests/xslt_non_dom_node.phpt new file mode 100644 index 0000000000000..9ead8a67fb581 --- /dev/null +++ b/ext/xsl/tests/xslt_non_dom_node.phpt @@ -0,0 +1,31 @@ +--TEST-- +php:function Support - non-DOMNode +--EXTENSIONS-- +xsl +--FILE-- +load(__DIR__."/xslt_non_dom_node.xsl"); +$proc = new xsltprocessor; +$xsl = $proc->importStylesheet($dom); + +$xml = new DomDocument(); +$xml->load(__DIR__."/xslt011.xml"); +$proc->registerPHPFunctions(); +try { + $proc->transformToXml($xml); +} catch (TypeError $e) { + echo $e->getMessage(); +} +?> +--EXPECT-- +Only objects that are instances of DOMNode can be converted to an XPath expression diff --git a/ext/xsl/tests/xslt_non_dom_node.xsl b/ext/xsl/tests/xslt_non_dom_node.xsl new file mode 100644 index 0000000000000..e88cb6efe0554 --- /dev/null +++ b/ext/xsl/tests/xslt_non_dom_node.xsl @@ -0,0 +1,9 @@ + + + + + + diff --git a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-array-notallowed.phpt b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-array-notallowed.phpt index 668fef2f56784..6c3761f004310 100644 --- a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-array-notallowed.phpt +++ b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-array-notallowed.phpt @@ -13,13 +13,15 @@ if(!$phpfuncxsl) { } $proc->importStylesheet($phpfuncxsl); var_dump($proc->registerPHPFunctions(array())); -var_dump($proc->transformToXml($dom)); +try { + var_dump($proc->transformToXml($dom)); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- -NULL - -Warning: XSLTProcessor::transformToXml(): Not allowed to call handler 'ucwords()' in %s on line %d +--EXPECT-- NULL +No callback handler "ucwords" registered --CREDITS-- Christian Weiske, cweiske@php.net PHP Testfest Berlin 2009-05-09 diff --git a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcnostring.phpt b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcnostring.phpt index d00b207b7652f..5356ff39e87d0 100644 --- a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcnostring.phpt +++ b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcnostring.phpt @@ -16,13 +16,15 @@ if(!$phpfuncxsl) { } $proc->importStylesheet($phpfuncxsl); var_dump($proc->registerPHPFunctions()); -var_dump($proc->transformToXml($dom)); +try { + var_dump($proc->transformToXml($dom)); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- -NULL - -Warning: XSLTProcessor::transformToXml(): Handler name must be a string in %s on line %d +--EXPECT-- NULL +Handler name must be a string --CREDITS-- Christian Weiske, cweiske@php.net PHP Testfest Berlin 2009-05-09 diff --git a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcundef.phpt b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcundef.phpt index 933d3da82c003..42ebcbb6c0867 100644 --- a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcundef.phpt +++ b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-funcundef.phpt @@ -15,13 +15,15 @@ if(!$phpfuncxsl) { } $proc->importStylesheet($phpfuncxsl); var_dump($proc->registerPHPFunctions()); -var_dump($proc->transformToXml($dom)); +try { + var_dump($proc->transformToXml($dom)); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- -NULL - -Warning: XSLTProcessor::transformToXml(): Unable to call handler undefinedfunc() in %s on line %d +--EXPECT-- NULL +Invalid callback undefinedfunc, function "undefinedfunc" not found or invalid function name --CREDITS-- Christian Weiske, cweiske@php.net PHP Testfest Berlin 2009-05-09 diff --git a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-string-notallowed.phpt b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-string-notallowed.phpt index be661ee410d33..17323d769fe36 100644 --- a/ext/xsl/tests/xsltprocessor_registerPHPFunctions-string-notallowed.phpt +++ b/ext/xsl/tests/xsltprocessor_registerPHPFunctions-string-notallowed.phpt @@ -13,13 +13,15 @@ if(!$phpfuncxsl) { } $proc->importStylesheet($phpfuncxsl); var_dump($proc->registerPHPFunctions('strpos')); -var_dump($proc->transformToXml($dom)); +try { + var_dump($proc->transformToXml($dom)); +} catch (Throwable $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- -NULL - -Warning: XSLTProcessor::transformToXml(): Not allowed to call handler 'ucwords()' in %s on line %d +--EXPECT-- NULL +No callback handler "ucwords" registered --CREDITS-- Christian Weiske, cweiske@php.net PHP Testfest Berlin 2009-05-09 diff --git a/ext/xsl/xsltprocessor.c b/ext/xsl/xsltprocessor.c index 552193d5e0dee..200dabaf66be5 100644 --- a/ext/xsl/xsltprocessor.c +++ b/ext/xsl/xsltprocessor.c @@ -45,228 +45,72 @@ static zend_result php_xsl_xslt_apply_params(xsltTransformContextPtr ctxt, HashT return SUCCESS; } -static void xsl_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */ +static void xsl_proxy_factory(xmlNodePtr node, zval *child, dom_object *intern, xmlXPathParserContextPtr ctxt) { - xsltTransformContextPtr tctxt; - zval *args = NULL; - zval retval; - int i; - int error = 0; - zend_fcall_info fci; - zval handler; - xmlXPathObjectPtr obj; - char *str; - xsl_object *intern; - zend_string *callable = NULL; + ZEND_ASSERT(node->type != XML_NAMESPACE_DECL); + + /** + * Upon freeing libxslt's context, every document that is not the *main* document will be freed by libxslt. + * If a node of a document that is *not the main* document gets returned to userland, we'd free the node twice: + * first by the cleanup of the xslt context, and then by our own refcounting mechanism. + * To prevent this, we'll take a copy if the node is not from the main document. + * It is important that we do not copy the node unconditionally, because that means that: + * - modifications to the node will only modify the copy, and not the original + * - accesses to the parent, path, ... will not work + */ + xsltTransformContextPtr transform_ctxt = (xsltTransformContextPtr) ctxt->context->extra; + if (node->doc != transform_ctxt->document->doc) { + node = xmlDocCopyNode(node, intern->document->ptr, 1); + } + php_dom_create_object(node, child, intern); +} +static void xsl_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, php_dom_xpath_nodeset_evaluation_mode evaluation_mode) /* {{{ */ +{ + bool error = false; + xsl_object *intern; if (! zend_is_executing()) { xsltGenericError(xsltGenericErrorContext, "xsltExtFunctionTest: Function called from outside of PHP\n"); - error = 1; + error = true; } else { - tctxt = xsltXPathGetTransformContext(ctxt); + xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt); if (tctxt == NULL) { xsltGenericError(xsltGenericErrorContext, "xsltExtFunctionTest: failed to get the transformation context\n"); - error = 1; + error = true; } else { intern = (xsl_object*)tctxt->_private; if (intern == NULL) { xsltGenericError(xsltGenericErrorContext, "xsltExtFunctionTest: failed to get the internal object\n"); - error = 1; - } - else if (intern->registerPhpFunctions == 0) { - xsltGenericError(xsltGenericErrorContext, - "xsltExtFunctionTest: PHP Object did not register PHP functions\n"); - error = 1; + error = true; } - } - } - - if (error == 1) { - for (i = nargs - 1; i >= 0; i--) { - obj = valuePop(ctxt); - if (obj) { - xmlXPathFreeObject(obj); - } - } - return; - } - - if (UNEXPECTED(nargs == 0)) { - zend_throw_error(NULL, "Function name must be passed as the first argument"); - return; - } - - fci.param_count = nargs - 1; - if (fci.param_count > 0) { - args = safe_emalloc(fci.param_count, sizeof(zval), 0); - } - /* Reverse order to pop values off ctxt stack */ - for (i = fci.param_count - 1; i >= 0; i--) { - obj = valuePop(ctxt); - if (obj == NULL) { - ZVAL_NULL(&args[i]); - continue; - } - switch (obj->type) { - case XPATH_STRING: - ZVAL_STRING(&args[i], (char *)obj->stringval); - break; - case XPATH_BOOLEAN: - ZVAL_BOOL(&args[i], obj->boolval); - break; - case XPATH_NUMBER: - ZVAL_DOUBLE(&args[i], obj->floatval); - break; - case XPATH_NODESET: - if (type == 1) { - str = (char*)xmlXPathCastToString(obj); - ZVAL_STRING(&args[i], str); - xmlFree(str); - } else if (type == 2) { - int j; - dom_object *domintern = (dom_object *)intern->doc; - if (obj->nodesetval && obj->nodesetval->nodeNr > 0) { - array_init(&args[i]); - for (j = 0; j < obj->nodesetval->nodeNr; j++) { - xmlNodePtr node = obj->nodesetval->nodeTab[j]; - zval child; - /* not sure, if we need this... it's copied from xpath.c */ - if (node->type == XML_NAMESPACE_DECL) { - xmlNsPtr curns; - xmlNodePtr nsparent; - - nsparent = node->_private; - curns = xmlNewNs(NULL, node->name, NULL); - if (node->children) { - curns->prefix = xmlStrdup((xmlChar *)node->children); - } - if (node->children) { - node = xmlNewDocNode(node->doc, NULL, (xmlChar *) node->children, node->name); - } else { - node = xmlNewDocNode(node->doc, NULL, (const xmlChar *) "xmlns", node->name); - } - node->type = XML_NAMESPACE_DECL; - node->parent = nsparent; - node->ns = curns; - } else { - /** - * Upon freeing libxslt's context, every document which is not the *main* document will be freed by libxslt. - * If a node of a document which is *not the main* document gets returned to userland, we'd free the node twice: - * first by the cleanup of the xslt context, and then by our own refcounting mechanism. - * To prevent this, we'll take a copy if the node is not from the main document. - * It is important that we do not copy the node unconditionally, because that means that: - * - modifications to the node will only modify the copy, and not the original - * - accesses to the parent, path, ... will not work - */ - xsltTransformContextPtr transform_ctxt = (xsltTransformContextPtr) ctxt->context->extra; - if (node->doc != transform_ctxt->document->doc) { - node = xmlDocCopyNode(node, domintern->document->ptr, 1); - } - } - - php_dom_create_object(node, &child, domintern); - add_next_index_zval(&args[i], &child); - } - } else { - ZVAL_EMPTY_ARRAY(&args[i]); - } - } - break; - default: - str = (char *) xmlXPathCastToString(obj); - ZVAL_STRING(&args[i], str); - xmlFree(str); - } - xmlXPathFreeObject(obj); - } - - fci.size = sizeof(fci); - fci.named_params = NULL; - if (fci.param_count > 0) { - fci.params = args; - } else { - fci.params = NULL; - } - - /* Last element of the stack is the function name */ - obj = valuePop(ctxt); - if (obj == NULL || obj->stringval == NULL) { - php_error_docref(NULL, E_WARNING, "Handler name must be a string"); - xmlXPathFreeObject(obj); - valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); - if (fci.param_count > 0) { - for (i = 0; i < nargs - 1; i++) { - zval_ptr_dtor(&args[i]); + else if (intern->xpath_callbacks.mode == PHP_DOM_REG_FUNC_MODE_NONE) { + zend_throw_error(NULL, "No callbacks were registered"); + error = true; } - efree(args); } - return; } - ZVAL_STRING(&handler, (char *) obj->stringval); - xmlXPathFreeObject(obj); - ZVAL_COPY_VALUE(&fci.function_name, &handler); - fci.object = NULL; - fci.retval = &retval; - if (!zend_make_callable(&handler, &callable)) { - if (!EG(exception)) { - php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", ZSTR_VAL(callable)); - } - valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); - } else if ( intern->registerPhpFunctions == 2 && zend_hash_exists(intern->registered_phpfunctions, callable) == 0) { - php_error_docref(NULL, E_WARNING, "Not allowed to call handler '%s()'", ZSTR_VAL(callable)); - /* Push an empty string, so that we at least have an xslt result... */ - valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); + if (error) { + php_dom_xpath_callbacks_clean_argument_stack(ctxt, nargs); } else { - zend_call_function(&fci, NULL); - if (Z_ISUNDEF(retval)) { - /* Exception thrown, don't do anything further. */ - } else if (Z_TYPE(retval) == IS_OBJECT && instanceof_function(Z_OBJCE(retval), dom_node_class_entry)) { - xmlNode *nodep; - dom_object *obj; - if (intern->node_list == NULL) { - intern->node_list = zend_new_array(0); - } - Z_ADDREF(retval); - zend_hash_next_index_insert(intern->node_list, &retval); - obj = Z_DOMOBJ_P(&retval); - nodep = dom_object_get_node(obj); - valuePush(ctxt, xmlXPathNewNodeSet(nodep)); - } else if (Z_TYPE(retval) == IS_TRUE || Z_TYPE(retval) == IS_FALSE) { - valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(retval) == IS_TRUE)); - } else if (Z_TYPE(retval) == IS_OBJECT) { - php_error_docref(NULL, E_WARNING, "A PHP Object cannot be converted to a XPath-string"); - valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); - } else { - convert_to_string(&retval); - valuePush(ctxt, xmlXPathNewString((xmlChar *) Z_STRVAL(retval))); - } - zval_ptr_dtor(&retval); - } - zend_string_release_ex(callable, 0); - zval_ptr_dtor_nogc(&handler); - if (fci.param_count > 0) { - for (i = 0; i < nargs - 1; i++) { - zval_ptr_dtor(&args[i]); - } - efree(args); + php_dom_xpath_callbacks_call(&intern->xpath_callbacks, ctxt, nargs, evaluation_mode, (dom_object *) intern->doc, xsl_proxy_factory); } } /* }}} */ void xsl_ext_function_string_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */ { - xsl_ext_function_php(ctxt, nargs, 1); + xsl_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_STRING); } /* }}} */ void xsl_ext_function_object_php(xmlXPathParserContextPtr ctxt, int nargs) /* {{{ */ { - xsl_ext_function_php(ctxt, nargs, 2); + xsl_ext_function_php(ctxt, nargs, PHP_DOM_XPATH_EVALUATE_NODESET_TO_NODESET); } /* }}} */ @@ -473,11 +317,7 @@ static xmlDocPtr php_xsl_apply_stylesheet(zval *id, xsl_object *intern, xsltStyl xsltFreeSecurityPrefs(secPrefs); } - if (intern->node_list != NULL) { - zend_hash_destroy(intern->node_list); - FREE_HASHTABLE(intern->node_list); - intern->node_list = NULL; - } + php_dom_xpath_callbacks_clean_node_list(&intern->xpath_callbacks); php_libxml_decrement_doc_ref(intern->doc); efree(intern->doc); @@ -732,38 +572,8 @@ PHP_METHOD(XSLTProcessor, removeParameter) /* {{{ */ PHP_METHOD(XSLTProcessor, registerPHPFunctions) { - zval *id = ZEND_THIS; - xsl_object *intern; - zval *entry, new_string; - zend_string *restrict_str = NULL; - HashTable *restrict_ht = NULL; - - ZEND_PARSE_PARAMETERS_START(0, 1) - Z_PARAM_OPTIONAL - Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(restrict_ht, restrict_str) - ZEND_PARSE_PARAMETERS_END(); - - intern = Z_XSL_P(id); - - if (restrict_ht) { - ZEND_HASH_FOREACH_VAL(restrict_ht, entry) { - zend_string *str = zval_try_get_string(entry); - if (UNEXPECTED(!str)) { - return; - } - ZVAL_LONG(&new_string, 1); - zend_hash_update(intern->registered_phpfunctions, str, &new_string); - zend_string_release(str); - } ZEND_HASH_FOREACH_END(); - - intern->registerPhpFunctions = 2; - } else if (restrict_str) { - ZVAL_LONG(&new_string, 1); - zend_hash_update(intern->registered_phpfunctions, restrict_str, &new_string); - intern->registerPhpFunctions = 2; - } else { - intern->registerPhpFunctions = 1; - } + xsl_object *intern = Z_XSL_P(ZEND_THIS); + php_dom_xpath_callbacks_update_method_handler(&intern->xpath_callbacks, INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} end XSLTProcessor::registerPHPFunctions(); */