1--TEST-- 2registerPHPFunctions() with callables - legit cases 3--EXTENSIONS-- 4dom 5--FILE-- 6<?php 7 8class MyClass { 9 public static function dump(string $var) { 10 var_dump($var); 11 } 12} 13 14class MyDOMXPath extends DOMXPath { 15 public function registerCycle() { 16 $this->registerPhpFunctions(["cycle" => array($this, "dummy")]); 17 } 18 19 public function dummy(string $var) { 20 echo "dummy: $var\n"; 21 } 22} 23 24$doc = new DOMDocument(); 25$doc->loadHTML('<a href="https://php.net">hello</a>'); 26 27echo "--- Legit cases: none ---\n"; 28 29$xpath = new DOMXPath($doc); 30$xpath->registerNamespace("php", "http://php.net/xpath"); 31try { 32 $xpath->evaluate("//a[php:function('var_dump', string(@href))]"); 33} catch (Error $e) { 34 echo $e->getMessage(), "\n"; 35} 36 37echo "--- Legit cases: all ---\n"; 38 39$xpath->registerPHPFunctions(null); 40$xpath->evaluate("//a[php:function('var_dump', string(@href))]"); 41$xpath->evaluate("//a[php:function('MyClass::dump', string(@href))]"); 42 43echo "--- Legit cases: set ---\n"; 44 45$xpath = new DOMXPath($doc); 46$xpath->registerNamespace("php", "http://php.net/xpath"); 47$xpath->registerPhpFunctions([]); 48$xpath->registerPHPFunctions(["xyz" => MyClass::dump(...), "mydump" => function (string $x) { 49 var_dump($x); 50}]); 51$xpath->registerPhpFunctions(str_repeat("var_dump", mt_rand(1, 1) /* defeat SCCP */)); 52$xpath->evaluate("//a[php:function('mydump', string(@href))]"); 53$xpath->evaluate("//a[php:function('xyz', string(@href))]"); 54$xpath->evaluate("//a[php:function('var_dump', string(@href))]"); 55try { 56 $xpath->evaluate("//a[php:function('notinset', string(@href))]"); 57} catch (Throwable $e) { 58 echo $e->getMessage(), "\n"; 59} 60 61echo "--- Legit cases: set with cycle ---\n"; 62 63$xpath = new MyDOMXPath($doc); 64$xpath->registerNamespace("php", "http://php.net/xpath"); 65$xpath->registerCycle(); 66$xpath->evaluate("//a[php:function('cycle', string(@href))]"); 67 68echo "--- Legit cases: reset to null ---\n"; 69 70$xpath->registerPhpFunctions(null); 71$xpath->evaluate("//a[php:function('var_dump', string(@href))]"); 72 73?> 74--EXPECT-- 75--- Legit cases: none --- 76No callbacks were registered 77--- Legit cases: all --- 78string(15) "https://php.net" 79string(15) "https://php.net" 80--- Legit cases: set --- 81string(15) "https://php.net" 82string(15) "https://php.net" 83string(15) "https://php.net" 84No callback handler "notinset" registered 85--- Legit cases: set with cycle --- 86dummy: https://php.net 87--- Legit cases: reset to null --- 88string(15) "https://php.net" 89