From 9656f553d10a16ff02d398e5a8e5f994aad5d7c9 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Fri, 5 Apr 2024 17:41:54 +0200 Subject: [PATCH] Throw deprecation notice about unregistered functions without changing the compilation flow (#985) Fixes #964 --- .../smarty_internal_templatecompilerbase.php | 23 ++++--- ...ArgumentMustBePassedByReference961Test.php | 67 +++++++++++++++++++ 2 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 tests/UnitTests/TemplateSource/_Issues/ArgumentMustBePassedByReference961Test.php diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 10caf5906..f5d2c4389 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -640,17 +640,18 @@ public function compilePHPFunctionCall($name, $parameter) return $func_name . '(' . $parameter[ 0 ] . ')'; } } else { - $first_param = array_shift($parameter); - $modifier = array_merge(array($name), $parameter); - // Now, compile the function call as a modifier - return $this->compileTag( - 'private_modifier', - array(), - array( - 'modifierlist' => array($modifier), - 'value' => $first_param - ) - ); + + if ( + !$this->smarty->loadPlugin('smarty_modifiercompiler_' . $name) + && !isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$name]) + && !in_array($name, ['time', 'join', 'is_array', 'in_array']) + ) { + trigger_error('Using unregistered function "' . $name . '" in a template is deprecated and will be ' . + 'removed in a future release. Use Smarty::registerPlugin to explicitly register ' . + 'a custom modifier.', E_USER_DEPRECATED); + } + + return $name . '(' . implode(',', $parameter) . ')'; } } else { $this->trigger_template_error("unknown function '{$name}'"); diff --git a/tests/UnitTests/TemplateSource/_Issues/ArgumentMustBePassedByReference961Test.php b/tests/UnitTests/TemplateSource/_Issues/ArgumentMustBePassedByReference961Test.php new file mode 100644 index 000000000..a637d76d4 --- /dev/null +++ b/tests/UnitTests/TemplateSource/_Issues/ArgumentMustBePassedByReference961Test.php @@ -0,0 +1,67 @@ +registerPlugin('modifier', 'reset', 'reset'); + $templateStr = "string:{reset(\$ar)}"; + $smarty->assign('ar', [1,2,3]); + $this->assertEquals( + '1', + $smarty->fetch($templateStr) + ); + } + + /** + * @group issue961 + * @deprecated + */ + public function testResetAsModifier() + { + $smarty = new Smarty(); + try { + $templateStr = "string:{\$ar|reset}"; + $smarty->assign('ar', [1,2,3]); + $this->assertEquals( + '1', + $smarty->fetch($templateStr) + ); + } catch (Exception $e) { + } + } + + /** + * @group issue961 + */ + public function testResetInExpression() + { + $smarty = new Smarty(); + $smarty->registerPlugin('modifier', 'reset', 'reset'); + $templateStr = "string:{if reset(\$ar)}ok{/if}"; + $smarty->assign('ar', [1,2,3]); + $this->assertEquals( + 'ok', + $smarty->fetch($templateStr) + ); + } + + /** + * @group issue961 + */ + public function testMatch() + { + $smarty = new Smarty(); + $smarty->registerPlugin('modifier', 'preg_match', 'preg_match'); + $templateStr = 'string:{assign var="match" value=null}{if preg_match(\'/([a-z]{4})/\', "a test", $match)}{$match.1}{/if}'; + $this->assertEquals( + 'test', + $smarty->fetch($templateStr) + ); + } +} \ No newline at end of file