From dd0f4c7c36c349931684723323220889eca635a3 Mon Sep 17 00:00:00 2001 From: uwetews Date: Sat, 2 Jan 2016 17:58:58 +0100 Subject: [PATCH] - improvement runtime checks if registered block plugins are callabel --- change_log.txt | 1 + libs/Smarty.class.php | 2 +- ..._internal_compile_private_block_plugin.php | 22 +++++++++----- ..._compile_private_object_block_function.php | 4 +-- ...ernal_compile_private_registered_block.php | 30 +++++++++++++------ 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/change_log.txt b/change_log.txt index 9df95b8f..9d19c9da 100644 --- a/change_log.txt +++ b/change_log.txt @@ -2,6 +2,7 @@ 02.01.2016 - update scope handling - optimize block plugin compiler + - improvement runtime checks if registered block plugins are callabel 01.01.2016 - remove Smarty::$resource_cache_mode property diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index f9ed3271..f45e4143 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.30-dev/17'; + const SMARTY_VERSION = '3.1.30-dev/18'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_private_block_plugin.php b/libs/sysplugins/smarty_internal_compile_private_block_plugin.php index 47c1b0f5..4910b72f 100644 --- a/libs/sysplugins/smarty_internal_compile_private_block_plugin.php +++ b/libs/sysplugins/smarty_internal_compile_private_block_plugin.php @@ -29,7 +29,7 @@ class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_Compi * * @var int */ - private $nesting = 0; + public $nesting = 0; /** * Compiles code for the execution of block plugin @@ -53,16 +53,24 @@ class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_Compi $compiler->tag_nocache = true; } unset($_attr[ 'nocache' ]); - list($callback, $_paramsArray) = $this->setup($compiler, $_attr, $tag, $function); + list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function); $_params = 'array(' . implode(",", $_paramsArray) . ')'; + // compile code + $output = "nesting} = isset({$callback[0]}) ? {$callback[0]} : null;\n"; + $callback = "\$_block_plugin{$this->nesting}{$callback[1]}"; + } + if (isset($callable)) { + $output .= "if (!is_callable({$callable})) {\nthrow new SmartyException('block tag \'{$tag}\' not callable or registered');\n}\n"; + } + $output .= + "\$_block_repeat{$this->nesting}=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat{$this->nesting});\nwhile (\$_block_repeat{$this->nesting}) {\nob_start();\n?>"; $this->openTag($compiler, $tag, array($_params, $compiler->nocache, $callback)); // maybe nocache because of nocache variables or nocache plugin $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; - // compile code - $output = - "nesting}=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat{$this->nesting});\nwhile (\$_block_repeat{$this->nesting}) {\nob_start();\n?>"; - } else { + } else { // must endblock be nocache? if ($compiler->nocache) { $compiler->tag_nocache = true; @@ -111,6 +119,6 @@ class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_Compi $_paramsArray[] = "'$_key'=>$_value"; } } - return array($function, $_paramsArray); + return array($function, $_paramsArray, null); } } diff --git a/libs/sysplugins/smarty_internal_compile_private_object_block_function.php b/libs/sysplugins/smarty_internal_compile_private_object_block_function.php index 42ea452b..5c7fb626 100644 --- a/libs/sysplugins/smarty_internal_compile_private_object_block_function.php +++ b/libs/sysplugins/smarty_internal_compile_private_object_block_function.php @@ -36,7 +36,7 @@ class Smarty_Internal_Compile_Private_Object_Block_Function extends Smarty_Inter $_paramsArray[] = "'$_key'=>$_value"; } } - $callback = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$method}"; - return array($callback, $_paramsArray); + $callback = array("\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]", "->{$method}"); + return array($callback, $_paramsArray, "array(\$_block_plugin{$this->nesting}, '{$method}')"); } } diff --git a/libs/sysplugins/smarty_internal_compile_private_registered_block.php b/libs/sysplugins/smarty_internal_compile_private_registered_block.php index 846051ad..ee253f5c 100644 --- a/libs/sysplugins/smarty_internal_compile_private_registered_block.php +++ b/libs/sysplugins/smarty_internal_compile_private_registered_block.php @@ -30,18 +30,30 @@ class Smarty_Internal_Compile_Private_Registered_Block extends Smarty_Internal_C { if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$tag])) { $tag_info = $compiler->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$tag]; + $callback = $tag_info[ 0 ]; + if (is_array($callback)) { + if (is_object($callback[ 0 ])) { + $callable = "array(\$_block_plugin{$this->nesting}, '{$callback[1]}')"; + $callback = array("\$_smarty_tpl->smarty->registered_plugins['block']['{$tag}'][0][0]", "->{$callback[1]}"); + } else { + $callable = "array(\$_block_plugin{$this->nesting}, '{$callback[1]}')"; + $callback = array("\$_smarty_tpl->smarty->registered_plugins['block']['{$tag}'][0][0]", "::{$callback[1]}"); + } + } else { + $callable = "\$_block_plugin{$this->nesting}"; + $callback = array("\$_smarty_tpl->smarty->registered_plugins['block']['{$tag}'][0]", ''); + } } else { $tag_info = $compiler->default_handler_plugins[Smarty::PLUGIN_BLOCK][$tag]; - } - $compiler->tag_nocache = !$tag_info[ 1 ] | $compiler->tag_nocache; - $callback = $tag_info[ 0 ]; - if (is_array($callback)) { - if (is_object($callback[ 0 ])) { - $callback = "\$_smarty_tpl->smarty->registered_plugins['block']['{$tag}'][0][0]->{$callback[1]}"; - } else { - $callback = "{$callback[0]}::{$callback[1]}"; + $callback = $tag_info[ 0 ]; + if (is_array($callback)) { + $callable = "array('{$callback[0]}', '{$callback[1]}')"; + $callback = "{$callback[1]}::{$callback[1]}"; + } else { + $callable = null; } } + $compiler->tag_nocache = !$tag_info[ 1 ] | $compiler->tag_nocache; $_paramsArray = array(); foreach ($_attr as $_key => $_value) { if (is_int($_key)) { @@ -53,6 +65,6 @@ class Smarty_Internal_Compile_Private_Registered_Block extends Smarty_Internal_C $_paramsArray[] = "'$_key'=>$_value"; } } - return array($callback, $_paramsArray); + return array($callback, $_paramsArray, $callable); } }