From 2850cce19f6c9dcb0e8daf4ec11ad34e7bcd3d55 Mon Sep 17 00:00:00 2001 From: uwetews Date: Tue, 26 Jan 2016 21:34:11 +0100 Subject: [PATCH] - bugfix string resource could inject code at {block} or inline subtemplates through PHP comments https://github.com/smarty-php/smarty/issues/157 --- change_log.txt | 1 + libs/Smarty.class.php | 2 +- .../smarty_internal_compile_block.php | 214 +++++++++++------- .../smarty_internal_compile_include.php | 183 ++++++++------- 4 files changed, 225 insertions(+), 175 deletions(-) diff --git a/change_log.txt b/change_log.txt index d5103bff..aaab7dbc 100644 --- a/change_log.txt +++ b/change_log.txt @@ -3,6 +3,7 @@ - revert bugfix compiling {section} did create warning - bugfix {$smarty.section.customer.loop} did throw compiler error https://github.com/smarty-php/smarty/issues/161 update of yesterdays fix + - bugfix string resource could inject code at {block} or inline subtemplates through PHP comments https://github.com/smarty-php/smarty/issues/157 26.01.2016 - improvement observe Smarty::$_CHARSET in debugging console https://github.com/smarty-php/smarty/issues/169 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 8eec85d3..d6a802aa 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/21'; + const SMARTY_VERSION = '3.1.30-dev/22'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_block.php b/libs/sysplugins/smarty_internal_compile_block.php index 0d897b5f..0c926798 100644 --- a/libs/sysplugins/smarty_internal_compile_block.php +++ b/libs/sysplugins/smarty_internal_compile_block.php @@ -15,6 +15,13 @@ */ class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inheritance { + /** + * nesting level of block tags + * + * @var int + */ + public static $blockTagNestingLevel = 0; + /** * Attribute definition: Overwrites base class. * @@ -47,30 +54,66 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inher */ public $optional_attributes = array('assign'); - /** - * nesting level of block tags - * - * @var int - */ - public static $blockTagNestingLevel = 0; - /** * Saved compiler object * - * @var Smarty_Internal_TemplateCompilerBase + * @var Smarty_Internal_TemplateCompiler */ public $compiler = null; + /** + * Compile saved child block source + * + * @param \Smarty_Internal_TemplateCompiler compiler object + * @param string $_name optional name of child block + * + * @return string compiled code of child block + */ + static function compileChildBlock(Smarty_Internal_TemplateCompiler $compiler, $_name = null) + { + if (!isset($compiler->_cache[ 'blockNesting' ])) { + $compiler->trigger_template_error(' tag {$smarty.block.child} used outside {block} tags ', + $compiler->parser->lex->taglineno); + } + $compiler->has_code = true; + $compiler->suppressNocacheProcessing = true; + $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 1 ] = true; + $output = + "\$_smarty_tpl->_getExtension('_inheritance')->processBlock(\$_smarty_tpl, 2, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, null, \$_blockParentStack);\n"; + return $output; + } + + /** + * Compile $smarty.block.parent + * + * @param \Smarty_Internal_TemplateCompiler $compiler compiler object + * @param string $_name optional name of child block + * + * @return string compiled code of child block + */ + static function compileParentBlock(Smarty_Internal_TemplateCompiler $compiler, $_name = null) + { + if (!isset($compiler->_cache[ 'blockNesting' ])) { + $compiler->trigger_template_error(' tag {$smarty.block.parent} used outside {block} tags ', + $compiler->parser->lex->taglineno); + } + $compiler->suppressNocacheProcessing = true; + $compiler->has_code = true; + $output = + "\$_smarty_tpl->_getExtension('_inheritance')->processBlock(\$_smarty_tpl, 4, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, null, \$_blockParentStack);\n"; + return $output; + } + /** * Compiles code for the {block} tag * - * @param array $args array with attributes from parser - * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object - * @param array $parameter array with compilation parameter + * @param array $args array with attributes from parser + * @param \Smarty_Internal_TemplateCompiler $compiler compiler object + * @param array $parameter array with compilation parameter * * @return bool true */ - public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) + public function compile($args, Smarty_Internal_TemplateCompiler $compiler, $parameter = null) { if (!isset($compiler->_cache[ 'blockNesting' ])) { $compiler->_cache[ 'blockNesting' ] = 0; @@ -86,13 +129,15 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inher $_attr = $this->getAttributes($compiler, $args); $compiler->_cache[ 'blockNesting' ] ++; $compiler->_cache[ 'blockName' ][ $compiler->_cache[ 'blockNesting' ] ] = $_attr[ 'name' ]; - $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 'name' ] = "{$_attr['name']}"; + $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 0 ] = + 'block_' . preg_replace('![^\w]+!', '_', uniqid(rand(), true)); + $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 1 ] = false; $this->openTag($compiler, 'block', array($_attr, $compiler->nocache, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code, $compiler->template->caching)); // must whole block be nocache ? if ($compiler->tag_nocache) { - $i = 0; + // TODO rethink nocache handling } $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; // $compiler->suppressNocacheProcessing = true; @@ -103,47 +148,6 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inher $compiler->template->compiled->has_nocache_code = false; $compiler->suppressNocacheProcessing = true; } - - /** - * Compile saved child block source - * - * @param \Smarty_Internal_TemplateCompilerBase compiler object - * @param string $_name optional name of child block - * - * @return string compiled code of child block - */ - static function compileChildBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null) - { - if (!isset($compiler->_cache[ 'blockNesting' ])) { - $compiler->trigger_template_error(' tag {$smarty.block.child} used outside {block} tags ', - $compiler->parser->lex->taglineno); - } - $compiler->has_code = true; - $compiler->suppressNocacheProcessing = true; - $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 'callsChild' ] = 'true'; - $output = "callChild(\$_smarty_tpl);\n?>\n"; - return $output; - } - - /** - * Compile $smarty.block.parent - * - * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object - * @param string $_name optional name of child block - * - * @return string compiled code of child block - */ - static function compileParentBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null) - { - if (!isset($compiler->_cache[ 'blockNesting' ])) { - $compiler->trigger_template_error(' tag {$smarty.block.parent} used outside {block} tags ', - $compiler->parser->lex->taglineno); - } - $compiler->suppressNocacheProcessing = true; - $compiler->has_code = true; - $output = "callParent(\$_smarty_tpl);\n?>\n"; - return $output; - } } /** @@ -152,70 +156,91 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inher */ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_Inheritance { + /** + * @var Smarty_Internal_TemplateCompiler + */ + public $compiler = null; + /** * Compiles code for the {/block} tag * - * @param array $args array with attributes from parser - * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object - * @param array $parameter array with compilation parameter + * @param array $args array with attributes from parser + * @param \Smarty_Internal_TemplateCompiler $compiler compiler object + * @param array $parameter array with compilation parameter * * @return bool true */ - public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) + public function compile($args, Smarty_Internal_TemplateCompiler $compiler, $parameter = null) { list($_attr, $_nocache, $_buffer, $_has_nocache_code, $_caching) = $this->closeTag($compiler, array('block')); // init block parameter $_block = $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]; unset($compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]); + $_block[ 2 ] = $_block[ 3 ] = 0; $_name = trim($_attr[ 'name' ], "'\""); $_assign = isset($_attr[ 'assign' ]) ? $_attr[ 'assign' ] : null; unset($_attr[ 'assign' ], $_attr[ 'name' ]); foreach ($_attr as $name => $stat) { if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat != 'false')) { - $_block[ $name ] = 'true'; + $_block[ $name ] = is_string($stat) ? trim($stat, "'\"") : $stat; } } - $_className = 'Block_' . preg_replace('#[^\w\|]+#S', '_', $_name) . '_' . - preg_replace('![^\w]+!', '_', uniqid(rand(), true)); + if ($compiler->template->source->type == 'file') { + $sourceInfo = $compiler->template->source->filepath; + } else { + $basename = $compiler->template->source->handler->getBasename($compiler->template->source); + $sourceInfo = $compiler->template->source->type .':' . ($basename ? $basename : $compiler->template->source->name); + } + $_funcName = $_block[ 0 ]; // get compiled block code - $_functionCode = $compiler->parser->current_buffer; + $_functionCode = $compiler->parser->current_buffer->to_smarty_php($compiler->parser); // setup buffer for template function code $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template(); - $output = "template->source->type}:{$compiler->template->source->name} */\n"; - $output .= "class {$_className} extends Smarty_Internal_Block\n"; - $output .= "{\n"; - foreach ($_block as $property => $value) { - $output .= "public \${$property} = {$value};\n"; - } - $output .= "public function callBlock(Smarty_Internal_Template \$_smarty_tpl) {\n"; - //$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n"; if ($compiler->template->compiled->has_nocache_code) { + $_has_nocache_code = true; + $_block[ 6 ] = $_funcNameCaching = $_funcName . '_nocache'; + $output = + "/* {block '{$_name}'} {$sourceInfo} */\n"; + $output .= "function {$_funcNameCaching} (\$_smarty_tpl, \$_blockParentStack) {\n"; + $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n"; $output .= "\$_smarty_tpl->cached->hashes['{$compiler->template->compiled->nocache_hash}'] = true;\n"; + if (isset($_assign)) { + $output .= "ob_start();\n"; + } + $output .= $_functionCode; + if (isset($_assign)) { + $output .= "\$_smarty_tpl->tpl_vars[{$_assign}] = new Smarty_Variable(ob_get_clean());\n"; + } + $output .= "/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n"; + $output .= "}\n"; + $output .= "/* {/block '{$_name}'} */\n\n"; + $compiler->parser->current_buffer->append_subtree($compiler->parser, + new Smarty_Internal_ParseTree_Tag($compiler->parser, + $output)); + $compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser); + $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template(); + $this->compiler = $compiler; + $_functionCode = + preg_replace_callback("/(echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';)/", + array($this, 'removeNocache'), $_functionCode); + $this->compiler = null; } + $output = "/* {block '{$_name}'} {$sourceInfo} */\n"; + $output .= "function {$_funcName}(\$_smarty_tpl, \$_blockParentStack) {\n"; if (isset($_assign)) { $output .= "ob_start();\n"; } - $output .= "?>\n"; - $compiler->parser->current_buffer->append_subtree($compiler->parser, - new Smarty_Internal_ParseTree_Tag($compiler->parser, - $output)); - $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode); - $output = "tpl_vars[{$_assign}] = new Smarty_Variable(ob_get_clean());\n"; } - //$output .= "/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n"; - $output .= "}\n"; $output .= "}\n"; $output .= "/* {/block '{$_name}'} */\n\n"; - $output .= "?>\n"; $compiler->parser->current_buffer->append_subtree($compiler->parser, new Smarty_Internal_ParseTree_Tag($compiler->parser, $output)); - $compiler->blockOrFunctionCode .= $f = $compiler->parser->current_buffer->to_smarty_php($compiler->parser); - $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template(); + $compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser); // nocache plugins must be copied if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) { foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) { @@ -231,13 +256,14 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_ $compiler->tag_nocache = $compiler->nocache; $compiler->nocache = $_nocache; $compiler->parser->current_buffer = $_buffer; - $output = "_cache[ 'blockNesting' ] == 1) { - $output .= "new {$_className}(\$_smarty_tpl);\n"; + $output .= "\$_smarty_tpl->_getExtension('_inheritance')->processBlock(\$_smarty_tpl, 0, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, " . + var_export($_block, true) . ");\n"; } else { - $output .= "new {$_className}(\$_smarty_tpl, \$this->tplIndex);\n"; + $output .= "\$_smarty_tpl->_getExtension('_inheritance')->processBlock(\$_smarty_tpl, 0, {$compiler->_cache['blockName'][$compiler->_cache['blockNesting']]}, " . + var_export($_block, true) . ", \$_blockParentStack);\n"; } - $output .= "?>\n"; $compiler->_cache[ 'blockNesting' ] --; if ($compiler->_cache[ 'blockNesting' ] == 0) { unset($compiler->_cache[ 'blockNesting' ]); @@ -246,4 +272,18 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_ $compiler->suppressNocacheProcessing = true; return $output; } + + /** + * @param $match + * + * @return mixed + */ + function removeNocache($match) + { + $code = + preg_replace("/(echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';)/", + '', $match[ 0 ]); + $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code); + return $code; + } } diff --git a/libs/sysplugins/smarty_internal_compile_include.php b/libs/sysplugins/smarty_internal_compile_include.php index a87fbed8..1775ca81 100644 --- a/libs/sysplugins/smarty_internal_compile_include.php +++ b/libs/sysplugins/smarty_internal_compile_include.php @@ -58,32 +58,34 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase * * @var array */ - public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true, - 'tpl_root' => true); + public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true, 'global' => true, + 'smarty' => true, 'tpl_root' => true); /** * Compiles code for the {include} tag * - * @param array $args array with attributes from parser - * @param Smarty_Internal_SmartyTemplateCompiler $compiler compiler object - * @param array $parameter array with compilation parameter + * @param array $args array with attributes from parser + * @param Smarty_Internal_TemplateCompiler $compiler compiler object + * @param array $parameter array with compilation parameter * * @throws SmartyCompilerException * @return string compiled code */ - public function compile($args, Smarty_Internal_SmartyTemplateCompiler $compiler, $parameter) + public function compile($args, Smarty_Internal_TemplateCompiler $compiler, $parameter = null) { // check and get attributes $_attr = $this->getAttributes($compiler, $args); + $compiler->requestExtension('_subTemplate', $compiler->nocache || $compiler->tag_nocache); - $hashResourceName = $fullResourceName = $source_resource = $_attr['file']; + $hashResourceName = $fullResourceName = $source_resource = $_attr[ 'file' ]; $variable_template = false; $cache_tpl = false; // parse resource_name if (preg_match('/^([\'"])(([A-Za-z0-9_\-]{2,})[:])?(([^$()]+)|(.+))\1$/', $source_resource, $match)) { - $type = !empty($match[3]) ? $match[3] : $compiler->template->smarty->default_resource_type; - $name = !empty($match[5]) ? $match[5] : $match[6]; - $handler = Smarty_Resource::load($compiler->smarty, $type); + $type = !empty($match[ 3 ]) ? $match[ 3 ] : $compiler->template->smarty->default_resource_type; + $name = !empty($match[ 5 ]) ? $match[ 5 ] : $match[ 6 ]; + $handler = $compiler->smarty->_getExtension('_source') + ->getResourceHandler($compiler->smarty, $type); if ($handler->recompiled || $handler->uncompiled) { $variable_template = true; } @@ -91,48 +93,57 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase if ($type != 'string') { $fullResourceName = "{$type}:{$name}"; $compiled = $compiler->parent_compiler->template->compiled; - if (isset($compiled->includes[$fullResourceName])) { - $compiled->includes[$fullResourceName] ++; + if (isset($compiled->includes[ $fullResourceName ])) { + $compiled->includes[ $fullResourceName ] ++; $cache_tpl = true; } else { - $compiled->includes[$fullResourceName] = 1; + $compiled->includes[ $fullResourceName ] = 1; } $fullResourceName = '"' . $fullResourceName . '"'; } } - if (empty($match[5])) { + if (empty($match[ 5 ])) { $variable_template = true; } } else { $variable_template = true; } - if (isset($_attr['assign'])) { + if (isset($_attr[ 'assign' ])) { // output will be stored in a smarty variable instead of being displayed - $_assign = $_attr['assign']; + $_assign = $_attr[ 'assign' ]; } // scope setup $_scope = Smarty::SCOPE_LOCAL; - if (isset($_attr['scope'])) { - $_attr['scope'] = trim($_attr['scope'], "'\""); - if (!isset($this->valid_scopes[$_attr['scope']])) { - $compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, true); + if (isset($_attr[ 'scope' ])) { + $_attr[ 'scope' ] = trim($_attr[ 'scope' ], "'\""); + if (!isset($this->valid_scopes[ $_attr[ 'scope' ] ])) { + $compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, + true); } - if ($_attr['scope'] != 'local') { - if ($_attr['scope'] == 'parent') { + if ($_attr[ 'scope' ] != 'local') { + if ($_attr[ 'scope' ] == 'parent') { $_scope = Smarty::SCOPE_PARENT; - } elseif ($_attr['scope'] == 'root') { + } elseif ($_attr[ 'scope' ] == 'root') { $_scope = Smarty::SCOPE_ROOT; - } elseif ($_attr['scope'] == 'tpl_root') { + } elseif ($_attr[ 'scope' ] == 'global') { + $_scope = Smarty::SCOPE_GLOBAL; + } elseif ($_attr[ 'scope' ] == 'smarty') { + $_scope = Smarty::SCOPE_SMARTY; + } elseif ($_attr[ 'scope' ] == 'tpl_root') { $_scope = Smarty::SCOPE_TPL_ROOT; } - $_scope += (isset($_attr[ 'bubble_up' ]) && $_attr[ 'bubble_up' ] == 'false') ? 0 : - Smarty::SCOPE_BUBBLE_UP; + if ($_attr[ 'bubble_up' ] === true) { + $_scope = $_scope + Smarty::SCOPE_BUBBLE_UP; + } } } + if ($_scope) { + $compiler->requestExtension('_updateScope', $compiler->nocache || $compiler->tag_nocache); + } - // set flag to cache subtemplate object when called within loop or template name is variable. + // set flag to cache sub-template object when called within loop or template name is variable. if ($cache_tpl || $variable_template || $compiler->loopNesting > 0) { $_cache_tpl = 'true'; } else { @@ -141,7 +152,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase // assume caching is off $_caching = Smarty::CACHING_OFF; - if ($_attr['nocache'] === true) { + if ($_attr[ 'nocache' ] === true) { $compiler->tag_nocache = true; } @@ -153,10 +164,10 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase } // flag if included template code should be merged into caller - $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && + $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || $_attr[ 'inline' ] === true) && !$compiler->template->source->handler->recompiled; - if ($merge_compiled_includes && $_attr['inline'] !== true) { + if ($merge_compiled_includes && $_attr[ 'inline' ] !== true) { // variable template name ? if ($variable_template) { $merge_compiled_includes = false; @@ -166,10 +177,11 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase } } // variable compile_id? - if (isset($_attr['compile_id'])) { - if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2 || - is_numeric($_attr['compile_id']))) || substr_count($_attr['compile_id'], '(') != 0 || - substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0 + if (isset($_attr[ 'compile_id' ])) { + if (!((substr_count($_attr[ 'compile_id' ], '"') == 2 || + substr_count($_attr[ 'compile_id' ], "'") == 2 || is_numeric($_attr[ 'compile_id' ]))) || + substr_count($_attr[ 'compile_id' ], '(') != 0 || + substr_count($_attr[ 'compile_id' ], '$_smarty_tpl->') != 0 ) { $merge_compiled_includes = false; if ($compiler->template->caching) { @@ -182,47 +194,47 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase /* * if the {include} tag provides individual parameter for caching or compile_id - * the subtemplate must not be included into the common cache file and is treated like + * the sub-template must not be included into the common cache file and is treated like * a call in nocache mode. * */ - if ($_attr['nocache'] !== true && $_attr['caching']) { - $_caching = $_new_caching = (int) $_attr['caching']; + if ($_attr[ 'nocache' ] !== true && $_attr[ 'caching' ]) { + $_caching = $_new_caching = (int) $_attr[ 'caching' ]; $call_nocache = true; } else { $_new_caching = Smarty::CACHING_LIFETIME_CURRENT; } - if (isset($_attr['cache_lifetime'])) { - $_cache_lifetime = $_attr['cache_lifetime']; + if (isset($_attr[ 'cache_lifetime' ])) { + $_cache_lifetime = $_attr[ 'cache_lifetime' ]; $call_nocache = true; $_caching = $_new_caching; } else { $_cache_lifetime = '$_smarty_tpl->cache_lifetime'; } - if (isset($_attr['cache_id'])) { - $_cache_id = $_attr['cache_id']; + if (isset($_attr[ 'cache_id' ])) { + $_cache_id = $_attr[ 'cache_id' ]; $call_nocache = true; $_caching = $_new_caching; } else { $_cache_id = '$_smarty_tpl->cache_id'; } - if (isset($_attr['compile_id'])) { - $_compile_id = $_attr['compile_id']; + if (isset($_attr[ 'compile_id' ])) { + $_compile_id = $_attr[ 'compile_id' ]; } else { $_compile_id = '$_smarty_tpl->compile_id'; } - // if subtemplate will be called in nocache mode do not merge + // if sub-template will be called in nocache mode do not merge if ($compiler->template->caching && $call_nocache) { $merge_compiled_includes = false; } - + $t_hash = ''; $has_compiled_template = false; if ($merge_compiled_includes) { - $c_id = isset($_attr['compile_id']) ? $_attr['compile_id'] : $compiler->template->compile_id; + $c_id = isset($_attr[ 'compile_id' ]) ? $_attr[ 'compile_id' ] : $compiler->template->compile_id; // we must observe different compile_id and caching $t_hash = sha1($c_id . ($_caching ? '--caching' : '--nocaching')); - if (!isset($compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash])) { + if (!isset($compiler->parent_compiler->mergedSubTemplatesData[ $hashResourceName ][ $t_hash ])) { $has_compiled_template = $this->compileInlineTemplate($compiler, $fullResourceName, $_caching, $hashResourceName, $t_hash, $c_id); @@ -231,9 +243,10 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase } } // delete {include} standard attributes - unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline'], $_attr['bubble_up']); + unset($_attr[ 'file' ], $_attr[ 'assign' ], $_attr[ 'cache_id' ], $_attr[ 'compile_id' ], $_attr[ 'cache_lifetime' ], $_attr[ 'nocache' ], $_attr[ 'caching' ], $_attr[ 'scope' ], $_attr[ 'inline' ], $_attr[ 'bubble_up' ]); // remaining attributes must be assigned as smarty variable $_vars_nc = ''; + $_vars = 'array()'; if (!empty($_attr)) { if ($_scope == Smarty::SCOPE_LOCAL) { $_pairs = array(); @@ -246,19 +259,17 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase } else { $compiler->trigger_template_error('variable passing not allowed in parent/global scope', null, true); } - } else { - $_vars = 'array()'; } $update_compile_id = $compiler->template->caching && !$compiler->tag_nocache && !$compiler->nocache && $_compile_id != '$_smarty_tpl->compile_id'; if ($has_compiled_template && !$call_nocache) { - $_output = "makeNocacheCode("\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n"); } if (!empty($_vars_nc) && $_caching == 9999 && $compiler->template->caching) { //$compiler->suppressNocacheProcessing = false; - $_output .= substr($compiler->processNocacheCode('\n", true), 6, - 3); + $_output .= substr($compiler->processNocacheCode($_vars_nc, true), 6, - 3); //$compiler->suppressNocacheProcessing = true; } if (isset($_assign)) { @@ -271,7 +282,6 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase if ($update_compile_id) { $_output .= $compiler->makeNocacheCode("\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n"); } - $_output .= "?>\n"; return $_output; } @@ -279,7 +289,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase if ($call_nocache) { $compiler->tag_nocache = true; } - $_output = "compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n"; } @@ -294,14 +304,13 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase if ($update_compile_id) { $_output .= "\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n"; } - $_output .= "?>\n"; return $_output; } /** * Compile inline sub template * - * @param \Smarty_Internal_SmartyTemplateCompiler $compiler + * @param \Smarty_Internal_TemplateCompiler $compiler * @param $fullResourceName * @param $_caching * @param $hashResourceName @@ -310,49 +319,49 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase * * @return bool */ - public function compileInlineTemplate(Smarty_Internal_SmartyTemplateCompiler $compiler, $fullResourceName, - $_caching, $hashResourceName, $t_hash, $c_id) + public function compileInlineTemplate(Smarty_Internal_TemplateCompiler $compiler, $fullResourceName, $_caching, + $hashResourceName, $t_hash, $c_id) { $compiler->smarty->allow_ambiguous_resources = true; /* @var Smarty_Internal_Template $tpl */ - $tpl = - new $compiler->smarty->template_class (trim($fullResourceName, '"\''), $compiler->smarty, $compiler->template, - $compiler->template->cache_id, $c_id, $_caching); + $tpl = new $compiler->smarty->template_class (trim($fullResourceName, '"\''), $compiler->smarty, + $compiler->template, $compiler->template->cache_id, $c_id, + $_caching); if (!($tpl->source->handler->uncompiled) && $tpl->source->exists) { - $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid'] = $tpl->source->uid; - if (isset($compiler->template->ext->_inheritance)) { - $tpl->ext->_inheritance = clone $compiler->template->ext->_inheritance; + $compiler->parent_compiler->mergedSubTemplatesData[ $hashResourceName ][ $t_hash ][ 'uid' ] = + $tpl->source->uid; + if (isset($compiler->template->_extensions[ '_inheritance' ])) { + $tpl->_extensions[ '_inheritance' ] = clone $compiler->template->_extensions[ '_inheritance' ]; } - $tpl->compiled = new Smarty_Template_Compiled(); - $tpl->compiled->nocache_hash = $compiler->parent_compiler->template->compiled->nocache_hash; - $tpl->loadCompiler(); + $tpl->loadCompiled(); + /* @var Smarty_Internal_TemplateCompiler $comp */ + $comp = $tpl->_getExtension('_compile') + ->loadCompiler($tpl); // save unique function name - $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func'] = - $tpl->compiled->unifunc = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); + $compiler->parent_compiler->mergedSubTemplatesData[ $hashResourceName ][ $t_hash ][ 'func' ] = + $tpl->compiled->renderCallback = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); // make sure whole chain gets compiled $tpl->mustCompile = true; - $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['nocache_hash'] = - $tpl->compiled->nocache_hash; + if ($compiler->template->source->type == 'file') { + $sourceInfo = $compiler->template->source->filepath; + } else { + $basename = $compiler->template->source->handler->getBasename($compiler->template->source); + $sourceInfo = $compiler->template->source->type .':' . ($basename ? $basename : $compiler->template->source->name); + } // get compiled code - $compiled_code = "source->type}:{$tpl->source->name}\" =============================*/\n"; - $compiled_code .= "function {$tpl->compiled->unifunc} (\$_smarty_tpl) {\n"; - $compiled_code .= "?>\n" . $tpl->compiler->compileTemplateSource($tpl, null, $compiler->parent_compiler); - $compiled_code .= "\n"; - $compiled_code .= $tpl->compiler->postFilter($tpl->compiler->blockOrFunctionCode); - $compiled_code .= "source->type}:{$tpl->source->name}\" =============================*/\n"; - $compiled_code .= "?>"; - unset($tpl->compiler); + $compiled_code = "\n\n"; + $compiled_code .= "/* Start inline template \"{$sourceInfo}\" =============================*/\n"; + $compiled_code .= "function {$tpl->compiled->renderCallback} (\$_smarty_tpl) {\n"; + $compiled_code .= $comp->compileTemplateSource($tpl, null, $compiler->parent_compiler); + $compiled_code .= "}\n"; + $compiled_code .= $comp->postFilter($comp->blockOrFunctionCode); + $compiled_code .= "/* End inline template \"{$sourceInfo}\" =============================*/\n"; + $compiled_code .= "\n\n"; + unset($comp); if ($tpl->compiled->has_nocache_code) { - // replace nocache_hash - $compiled_code = - str_replace("{$tpl->compiled->nocache_hash}", $compiler->template->compiled->nocache_hash, - $compiled_code); $compiler->template->compiled->has_nocache_code = true; } - $compiler->parent_compiler->mergedSubTemplatesCode[$tpl->compiled->unifunc] = $compiled_code; + $compiler->parent_compiler->mergedSubTemplatesCode[ $tpl->compiled->renderCallback ] = $compiled_code; return true; } else { return false;