diff --git a/libs/sysplugins/smarty_internal_compile_foreach.php b/libs/sysplugins/smarty_internal_compile_foreach.php index 9f62bc84..246554d2 100644 --- a/libs/sysplugins/smarty_internal_compile_foreach.php +++ b/libs/sysplugins/smarty_internal_compile_foreach.php @@ -282,6 +282,29 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase return $output; } + + /** + * Compiles code for the {$smarty.foreach} tag + * + * @param array $args array with attributes from parser + * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object + * @param array $parameter array with compilation parameter + * + * @return string compiled code + * @throws \SmartyCompilerException + */ + public static function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) + { + if (!isset($parameter[1]) || false === $name = $compiler->getId($parameter[1])) { + $compiler->trigger_template_error("missing or illegal \$Smarty.foreach name attribute", $compiler->lex->taglineno); + } + if ((!isset($parameter[2]) || false === $property = $compiler->getId($parameter[2])) || !in_array(strtolower($property), array('first', 'last', 'index', 'iteration', 'show', 'total'))) { + $compiler->trigger_template_error("missing or illegal \$Smarty.foreach property attribute", $compiler->lex->taglineno); + } + $property = strtolower($property); + $foreachVar = "'__foreach_{$name}'"; + return "(isset(\$_smarty_tpl->tpl_vars[{$foreachVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$foreachVar}]->value['{$property}'] : null)"; + } } /** diff --git a/libs/sysplugins/smarty_internal_compile_private_special_variable.php b/libs/sysplugins/smarty_internal_compile_private_special_variable.php index 525bc0db..f99c233f 100644 --- a/libs/sysplugins/smarty_internal_compile_private_special_variable.php +++ b/libs/sysplugins/smarty_internal_compile_private_special_variable.php @@ -25,17 +25,18 @@ class Smarty_Internal_Compile_Private_Special_Variable extends Smarty_Internal_C * * @return string compiled code */ - public function compile($args, $compiler, $parameter) + public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) { $_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2)); $compiled_ref = ' '; - $variable = trim($_index[0], "'"); + $variable = $compiler->getId($_index[0]); + if ($variable === false) { + $compiler->trigger_template_error("special \$Smarty variable name index can not be variable", $compiler->lex->taglineno); + } if (!isset($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)) { - switch ($variable) { + switch ($variable) { case 'foreach': - $name = trim($_index[1], "'"); - $foreachVar = "'__foreach_{$name}'"; - return "(isset(\$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}]) ? \$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}] : null)"; + return Smarty_Internal_Compile_Foreach::compileSpecialVariable(array(), $compiler, $_index); case 'section': return "\$_smarty_tpl->getVariable('smarty')->value$parameter"; case 'capture':