From 80bf76cc6e879015d3a4a8335cdc0c931e1ef5d5 Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Thu, 1 Jan 2015 22:59:07 +0100 Subject: [PATCH] move internal createTemplateCodeFrame() method into an extension class --- .../smarty_internal_extension_codeframe.php | 76 ++++++++++++++++++ libs/sysplugins/smarty_internal_template.php | 77 +------------------ .../smarty_internal_templatecompilerbase.php | 28 ++++--- 3 files changed, 90 insertions(+), 91 deletions(-) create mode 100644 libs/sysplugins/smarty_internal_extension_codeframe.php diff --git a/libs/sysplugins/smarty_internal_extension_codeframe.php b/libs/sysplugins/smarty_internal_extension_codeframe.php new file mode 100644 index 00000000..34d5df76 --- /dev/null +++ b/libs/sysplugins/smarty_internal_extension_codeframe.php @@ -0,0 +1,76 @@ +properties['has_nocache_code'] = $_template->has_nocache_code || !empty($_template->required_plugins['nocache']); + $_template->properties['version'] = Smarty::SMARTY_VERSION; + if (!isset($_template->properties['unifunc'])) { + $_template->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); + } + $output = "properties['nocache_hash']}%%*/\n"; + if ($_template->smarty->direct_access_security) { + $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n"; + } + $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($_template->properties, true) . ',' . ($cache ? 'true' : 'false') . ");\n/*/%%SmartyHeaderCode%%*/\n"; + $output .= "if (\$_valid && !is_callable('{$_template->properties['unifunc']}')) {function {$_template->properties['unifunc']} (\$_smarty_tpl) {\n"; + // include code for plugins + if (!$cache) { + if (!empty($_template->required_plugins['compiled'])) { + foreach ($_template->required_plugins['compiled'] as $tmp) { + foreach ($tmp as $data) { + $file = addslashes($data['file']); + if (is_Array($data['function'])) { + $output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n"; + } else { + $output .= "if (!is_callable('{$data['function']}')) include '{$file}';\n"; + } + } + } + } + if (!empty($_template->required_plugins['nocache'])) { + $_template->has_nocache_code = true; + $output .= "echo '/*%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/smarty; "; + foreach ($_template->required_plugins['nocache'] as $tmp) { + foreach ($tmp as $data) { + $file = addslashes($data['file']); + if (is_Array($data['function'])) { + $output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n"); + } else { + $output .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n"); + } + } + } + $output .= "?>/*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/';\n"; + } + } + $output .= "?>\n" . $content; + $output .= ""; + return $output; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index aad7e9a0..d7969fb3 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -388,82 +388,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase } throw new SmartyException("Unable to find template function '{$name}'"); } - - /** - * Create code frame for compiled and cached templates - * - * @param string $content optional template content - * @param bool $cache flag for cache file - * - * @return string - */ - public function createTemplateCodeFrame($content = '', $cache = false) - { - $plugins_string = ''; - // include code for plugins - if (!$cache) { - if (!empty($this->required_plugins['compiled'])) { - $plugins_string = 'required_plugins['compiled'] as $tmp) { - foreach ($tmp as $data) { - $file = addslashes($data['file']); - if (is_Array($data['function'])) { - $plugins_string .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n"; - } else { - $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$file}';\n"; - } - } - } - $plugins_string .= '?>'; - } - if (!empty($this->required_plugins['nocache'])) { - $this->has_nocache_code = true; - $plugins_string .= "properties['nocache_hash']}%%*/smarty; "; - foreach ($this->required_plugins['nocache'] as $tmp) { - foreach ($tmp as $data) { - $file = addslashes($data['file']); - if (is_Array($data['function'])) { - $plugins_string .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n"); - } else { - $plugins_string .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n"); - } - } - } - $plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n"; - } - } - // build property code - $this->properties['has_nocache_code'] = $this->has_nocache_code; - $output = 'source->recompiled) { - $output .= "/*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/"; - if ($this->smarty->direct_access_security) { - $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n"; - } - } - if ($cache) { - $this->properties['type'] = 'cache'; - } else { - $this->properties['type'] = 'compiled'; - } - $this->properties['version'] = Smarty::SMARTY_VERSION; - if (!isset($this->properties['unifunc'])) { - $this->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); - } - if (!$this->source->recompiled) { - $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . ',' . ($cache ? 'true' : 'false') . "); /*/%%SmartyHeaderCode%%*/?>\n"; - $output .= "properties['unifunc']}')) {function {$this->properties['unifunc']} (\$_smarty_tpl) {\n"; - } - $output .= "\$_saved_type = \$_smarty_tpl->properties['type'];\n"; - $output .= "\$_smarty_tpl->properties['type'] = \$_smarty_tpl->caching ? 'cache' : 'compiled';?>\n"; - $output .= $plugins_string . $content; - $output .= "properties['type'] = \$_saved_type;?>\n"; - if (!$this->source->recompiled) { - $output .= ""; - } - return $output; - } - + /** * This function is executed automatically when a compiled or cached template file is included * - Decode saved properties from compiled template and cache files diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 0041a9d1..04e538e9 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -252,7 +252,6 @@ abstract class Smarty_Internal_TemplateCompilerBase } else { $this->nocache_hash = $template->properties['nocache_hash']; } - $template->properties['type'] = $template->caching ? 'cache' : 'compiled'; // flag for nochache sections $this->nocache = $nocache; $this->tag_nocache = false; @@ -332,7 +331,7 @@ abstract class Smarty_Internal_TemplateCompilerBase if ($this->suppressTemplatePropertyHeader) { $_compiled_code .= $merged_code; } else { - $_compiled_code = $template_header . $template->createTemplateCodeFrame($_compiled_code) . $merged_code; + $_compiled_code = $template_header . Smarty_Internal_Extension_CodeFrame::create($template, $_compiled_code) . $merged_code; } if (!empty($this->templateFunctionCode)) { // run postfilter if required on compiled template code @@ -597,22 +596,21 @@ abstract class Smarty_Internal_TemplateCompilerBase */ public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) { - // re-use object if already exists - if (isset(self::$_tag_objects[$tag])) { + // check if tag allowed by security + if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) { + // re-use object if already exists + if (!isset(self::$_tag_objects[$tag])) { + // lazy load internal compiler plugin + $class_name = 'Smarty_Internal_Compile_' . $tag; + if ($this->smarty->loadPlugin($class_name)) { + self::$_tag_objects[$tag] = new $class_name; + } else { + return false; + } + } // compile this tag return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3); } - // lazy load internal compiler plugin - $class_name = 'Smarty_Internal_Compile_' . $tag; - if ($this->smarty->loadPlugin($class_name)) { - // check if tag allowed by security - if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) { - // use plugin if found - self::$_tag_objects[$tag] = new $class_name; - // compile this tag - return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3); - } - } // no internal compile plugin for this tag return false; }