Bugfix/1100 modifier called like a function compiled to modifier name not callback (#1101)

* Fixed that modifiers called like function would be compiled to modifier name instead of calling the registered callback
This commit is contained in:
Simon Wisselink
2025-02-13 23:21:21 +01:00
committed by GitHub
parent 272a407e9d
commit 5cb412d040
4 changed files with 120 additions and 33 deletions

1
changelog/1100.md Normal file
View File

@@ -0,0 +1 @@
- Fixed that modifiers called like function would be compiled to modifier name instead of calling the registered callback [#1100](https://github.com/smarty-php/smarty/issues/1100)

View File

@@ -617,7 +617,8 @@ abstract class Smarty_Internal_TemplateCompilerBase
{ {
if (!$this->smarty->security_policy || $this->smarty->security_policy->isTrustedPhpFunction($name, $this)) { if (!$this->smarty->security_policy || $this->smarty->security_policy->isTrustedPhpFunction($name, $this)) {
if (strcasecmp($name, 'isset') === 0 || strcasecmp($name, 'empty') === 0 if (strcasecmp($name, 'isset') === 0 || strcasecmp($name, 'empty') === 0
|| strcasecmp($name, 'array') === 0 || is_callable($name) || strcasecmp($name, 'array') === 0
|| (is_callable($name) && !isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$name]))
) { ) {
$func_name = smarty_strtolower_ascii($name); $func_name = smarty_strtolower_ascii($name);
@@ -667,11 +668,25 @@ abstract class Smarty_Internal_TemplateCompilerBase
return $name . '(' . implode(',', $parameter) . ')'; return $name . '(' . implode(',', $parameter) . ')';
} }
} else {
}
}
if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$name])) {
if ($name === $this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$name][0]) {
return $name . '(' . implode(',', $parameter) . ')';
}
return sprintf(
'call_user_func_array($_smarty_tpl->registered_plugins[ \'%s\' ][ %s ][ 0 ], array( %s ))',
Smarty::PLUGIN_MODIFIER,
var_export($name, true),
implode(',', $parameter)
);
}
$this->trigger_template_error("unknown function '{$name}'"); $this->trigger_template_error("unknown function '{$name}'");
} }
}
}
/** /**
* Determines whether the passed string represents a valid (PHP) variable. * Determines whether the passed string represents a valid (PHP) variable.

View File

@@ -0,0 +1,55 @@
<?php
// first class callables where introduced in PHP 8.1
if (PHP_VERSION_ID >= 80100) {
/**
* class for register modifier with (first class) callables tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class RegisterModifierFirstClassCallablesTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testInit()
{
$this->cleanDirs();
}
public function testRegisterFirstClassCallable()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier', eval('return strrev(...);'));
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|testmodifier}'));
}
public function testRegisterFirstClassCallableSameName()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'mymodifier', eval('return strrev(...);'));
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|mymodifier}'));
}
public function testRegisterFirstClassCallableAsFunc()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'kprint_r_out', eval('return strrev(...);'));
$this->smarty->assign('myVar', 'andersom');
$this->assertEquals('mosredna', $this->smarty->fetch('string:{kprint_r_out($myVar)}'));
}
public function testRegisterFirstClassCallableSameNameAsPhpFunc()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'mymodifierfcc', eval('return strrev(...);'));
$this->assertEquals('mosredna', $this->smarty->fetch('string:{mymodifierfcc("andersom")}'));
}
}
}
function mymodifierfcc($a, $b, $c)
{
return "$a function $b $c";
}

View File

@@ -88,6 +88,22 @@ class RegisterModifierTest extends PHPUnit_Smarty
$this->smarty->unregisterPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier'); $this->smarty->unregisterPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertTrue(isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK]['testmodifier'])); $this->assertTrue(isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK]['testmodifier']));
} }
public function testRegisterNativePhpFuncAsString()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'strrev', 'strrev');
$this->smarty->assign('myVar', 'andersom');
$this->assertEquals('mosredna', $this->smarty->fetch('string:{strrev($myVar)}'));
}
public function testRegisterNativePhpFuncUnderDifferentName()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'k_xyz_a', 'strrev');
$this->smarty->assign('myVar', 'andersom');
$this->assertEquals('mosredna', $this->smarty->fetch('string:{k_xyz_a($myVar)}'));
}
} }
function mymodifier($a, $b, $c) function mymodifier($a, $b, $c)