From 0fabe627ad380521aaf553eb5c8a7d21eea3edf5 Mon Sep 17 00:00:00 2001 From: "uwe.tews@googlemail.com" Date: Tue, 11 Oct 2011 15:49:19 +0000 Subject: [PATCH] - add runtime checks for not matching {capture}/{/capture} calls (Forum Topic 20120) --- change_log.txt | 3 +++ .../smarty_internal_compile_capture.php | 20 +++++++++---------- libs/sysplugins/smarty_internal_template.php | 13 ++++++++++++ .../smarty_internal_templatebase.php | 6 ++++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/change_log.txt b/change_log.txt index 1b090914..dc9775ea 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@ ===== trunk ===== +11.10.2011 +- add runtime checks for not matching {capture}/{/capture} calls (Forum Topic 20120) + 10.10.2011 - bugfix variable name typo in {html_options} and {html_checkboxes} (Issue #54) - bugfix tag did create wrong output when caching enabled and the tag was in included subtemplate diff --git a/libs/sysplugins/smarty_internal_compile_capture.php b/libs/sysplugins/smarty_internal_compile_capture.php index da4ad172..fe54b36b 100644 --- a/libs/sysplugins/smarty_internal_compile_capture.php +++ b/libs/sysplugins/smarty_internal_compile_capture.php @@ -45,13 +45,13 @@ class Smarty_Internal_Compile_Capture extends Smarty_Internal_CompileBase { $_attr = $this->getAttributes($compiler, $args); $buffer = isset($_attr['name']) ? $_attr['name'] : "'default'"; - $assign = isset($_attr['assign']) ? $_attr['assign'] : null; - $append = isset($_attr['append']) ? $_attr['append'] : null; + $assign = isset($_attr['assign']) ? $_attr['assign'] : 'null'; + $append = isset($_attr['append']) ? $_attr['append'] : 'null'; $compiler->_capture_stack[] = array($buffer, $assign, $append, $compiler->nocache); // maybe nocache because of nocache variables $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; - $_output = ""; + $_output = "_capture_stack[] = array($buffer, $assign, $append); ob_start(); ?>"; return $_output; } @@ -84,14 +84,12 @@ class Smarty_Internal_Compile_CaptureClose extends Smarty_Internal_CompileBase { list($buffer, $assign, $append, $compiler->nocache) = array_pop($compiler->_capture_stack); - $_output = "assign($assign, ob_get_contents());"; - } - if (isset($append)) { - $_output .= " \$_smarty_tpl->append($append, ob_get_contents());"; - } - $_output .= " Smarty::\$_smarty_vars['capture'][$buffer]=ob_get_clean();?>"; + $_output = "_capture_stack);\n"; + $_output .= "if (!empty(\$_capture_buffer)) {\n"; + $_output .= " if (isset(\$_capture_assign)) \$_smarty_tpl->assign(\$_capture_assign, ob_get_contents());\n"; + $_output .= " if (isset( \$_capture_append)) \$_smarty_tpl->append( \$_capture_append, ob_get_contents());\n"; + $_output .= " Smarty::\$_smarty_vars['capture'][\$_capture_buffer]=ob_get_clean();\n"; + $_output .= "} else \$_smarty_tpl->capture_error();?>"; return $_output; } diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index c5204a73..8bb4c4b0 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -93,6 +93,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { * @var bool */ public $allow_relative_path = false; + /** + * internal capture runtime stack + * @var array + */ + public $_capture_stack = array(); /** * Create template data object @@ -551,6 +556,14 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { } /** + * runtime error not matching capture tags + * + */ + public function capture_error() + { + throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\""); + } + /** * set Smarty property in template context * * @param string $property_name property name diff --git a/libs/sysplugins/smarty_internal_templatebase.php b/libs/sysplugins/smarty_internal_templatebase.php index 97cb1a71..ab12f938 100644 --- a/libs/sysplugins/smarty_internal_templatebase.php +++ b/libs/sysplugins/smarty_internal_templatebase.php @@ -170,6 +170,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data { throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'"); } $_template->properties['unifunc']($_template); + if (isset($_template->_capture_stack[0])) { + $_template->capture_error(); + } } catch (Exception $e) { ob_get_clean(); throw $e; @@ -262,6 +265,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data { try { ob_start(); $_template->properties['unifunc']($_template); + if (isset($_template->_capture_stack[0])) { + $_template->capture_error(); + } $_output = ob_get_clean(); } catch (Exception $e) { ob_get_clean();