diff options
Diffstat (limited to 'library/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php')
-rw-r--r-- | library/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php | 641 |
1 files changed, 365 insertions, 276 deletions
diff --git a/library/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php b/library/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php index 7fc6c3252..9487c3454 100644 --- a/library/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/library/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -14,6 +14,11 @@ * * @package Smarty * @subpackage Compiler + * + * @property Smarty_Internal_SmartyTemplateCompiler $prefixCompiledCode = '' + * @property Smarty_Internal_SmartyTemplateCompiler $postfixCompiledCode = '' + * @method Smarty_Internal_SmartyTemplateCompiler registerPostCompileCallback($callback, $parameter = array(), $key = null, $replace = false) + * @method Smarty_Internal_SmartyTemplateCompiler unregisterPostCompileCallback($key) */ abstract class Smarty_Internal_TemplateCompilerBase { @@ -26,6 +31,13 @@ abstract class Smarty_Internal_TemplateCompilerBase public $smarty = null; /** + * Parser object + * + * @var Smarty_Internal_Templateparser + */ + public $parser = null; + + /** * hash for nocache sections * * @var mixed @@ -40,11 +52,11 @@ abstract class Smarty_Internal_TemplateCompilerBase public $suppressNocacheProcessing = false; /** - * compile tag objects + * compile tag objects cache * * @var array */ - public static $_tag_objects = array(); + public $_tag_objects = array(); /** * tag stack @@ -82,34 +94,6 @@ abstract class Smarty_Internal_TemplateCompilerBase public $templateProperties = array(); /** - * sources which must be compiled - * - * @var array - */ - public $sources = array(); - - /** - * flag that we are inside {block} - * - * @var bool - */ - public $inheritance = false; - - /** - * flag when compiling inheritance child template - * - * @var bool - */ - public $inheritance_child = false; - - /** - * uid of templates called by {extends} for recursion check - * - * @var array - */ - public $extends_uid = array(); - - /** * source line offset for error messages * * @var int @@ -159,27 +143,6 @@ abstract class Smarty_Internal_TemplateCompilerBase public $forceNocache = false; /** - * suppress Smarty header code in compiled template - * - * @var bool - */ - public $suppressHeader = false; - - /** - * suppress template property header code in compiled template - * - * @var bool - */ - public $suppressTemplatePropertyHeader = false; - - /** - * suppress pre and post filter - * - * @var bool - */ - public $suppressFilter = false; - - /** * flag if compiled template file shall we written * * @var bool @@ -187,13 +150,6 @@ abstract class Smarty_Internal_TemplateCompilerBase public $write_compiled_code = true; /** - * flag if currently a template function is compiled - * - * @var bool - */ - public $compiles_template_function = false; - - /** * called sub functions from template function * * @var array @@ -201,11 +157,11 @@ abstract class Smarty_Internal_TemplateCompilerBase public $called_functions = array(); /** - * compiled template function code + * compiled template or block function code * * @var string */ - public $templateFunctionCode = ''; + public $blockOrFunctionCode = ''; /** * php_handling setting either from Smarty or security @@ -250,13 +206,6 @@ abstract class Smarty_Internal_TemplateCompilerBase public $tag_nocache = false; /** - * Flag to restart parsing - * - * @var bool - */ - public $abort_and_recompile = false; - - /** * Compiled tag prefix code * * @var array @@ -292,6 +241,27 @@ abstract class Smarty_Internal_TemplateCompilerBase public $has_output = false; /** + * Stack for {setfilter} {/setfilter} + * + * @var array + */ + public $variable_filter_stack = array(); + + /** + * variable filters for {setfilter} {/setfilter} + * + * @var array + */ + public $variable_filters = array(); + + /** + * Nesting count of looping tags like {foreach}, {for}, {section}, {while} + * + * @var int + */ + public $loopNesting = 0; + + /** * Strip preg pattern * * @var string @@ -299,19 +269,37 @@ abstract class Smarty_Internal_TemplateCompilerBase public $stripRegEx = '![\t ]*[\r\n]+[\t ]*!'; /** + * plugin search order + * + * @var array + */ + public $plugin_search_order = array('function', 'block', 'compiler', 'class'); + + /** + * General storage area for tag compiler plugins + * + * @var array + */ + public $_cache = array(); + + /** * method to compile a Smarty template * - * @param mixed $_content template source + * @param mixed $_content template source + * @param bool $isTemplateSource * - * @return bool true if compiling succeeded, false if it failed + * @return bool true if compiling succeeded, false if it failed */ - abstract protected function doCompile($_content); + abstract protected function doCompile($_content, $isTemplateSource = false); /** * Initialize compiler + * + * @param Smarty $smarty global instance */ - public function __construct() + public function __construct(Smarty $smarty) { + $this->smarty = $smarty; $this->nocache_hash = str_replace(array('.', ','), '_', uniqid(rand(), true)); } @@ -323,117 +311,140 @@ abstract class Smarty_Internal_TemplateCompilerBase * @param null|Smarty_Internal_TemplateCompilerBase $parent_compiler * * @return bool true if compiling succeeded, false if it failed + * @throws \Exception */ - public function compileTemplate(Smarty_Internal_Template $template, $nocache = null, $parent_compiler = null) + public function compileTemplate(Smarty_Internal_Template $template, $nocache = null, + Smarty_Internal_TemplateCompilerBase $parent_compiler = null) { - // save template object in compiler class - $this->template = $template; - if (isset($this->template->smarty->security_policy)) { - $this->php_handling = $this->template->smarty->security_policy->php_handling; - } else { - $this->php_handling = $this->template->smarty->php_handling; - } - $this->parent_compiler = $parent_compiler ? $parent_compiler : $this; - $nocache = isset($nocache) ? $nocache : false; - if (empty($template->properties['nocache_hash'])) { - $template->properties['nocache_hash'] = $this->nocache_hash; - } else { - $this->nocache_hash = $template->properties['nocache_hash']; - } - $save_source = $this->template->source; - // template header code - $template_header = ''; - if (!$this->suppressHeader) { - $template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n"; - $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n"; - } + // get code frame of compiled template + $_compiled_code = $template->smarty->ext->_codeFrame->create($template, + $this->compileTemplateSource($template, $nocache, + $parent_compiler), + $this->postFilter($this->blockOrFunctionCode) . + join('', $this->mergedSubTemplatesCode)); + return $_compiled_code; + } - if (empty($this->template->source->components)) { - $this->sources = array($template->source); - } else { - // we have array of inheritance templates by extends: resource - $this->sources = array_reverse($template->source->components); - } - $loop = 0; - // the $this->sources array can get additional elements while compiling by the {extends} tag - while ($this->template->source = array_shift($this->sources)) { - $this->smarty->_current_file = $this->template->source->filepath; + /** + * Compile template source and run optional post filter + * + * @param \Smarty_Internal_Template $template + * @param null|bool $nocache flag if template must be compiled in nocache mode + * @param \Smarty_Internal_TemplateCompilerBase $parent_compiler + * + * @return string + * @throws \Exception + */ + public function compileTemplateSource(Smarty_Internal_Template $template, $nocache = null, + Smarty_Internal_TemplateCompilerBase $parent_compiler = null) + { + try { + // save template object in compiler class + $this->template = $template; + if (property_exists($this->template->smarty, 'plugin_search_order')) { + $this->plugin_search_order = $this->template->smarty->plugin_search_order; + } if ($this->smarty->debugging) { - Smarty_Internal_Debug::start_compile($this->template); + $this->smarty->_debug->start_compile($this->template); + } + if (isset($this->template->smarty->security_policy)) { + $this->php_handling = $this->template->smarty->security_policy->php_handling; + } else { + $this->php_handling = $this->template->smarty->php_handling; } - $no_sources = count($this->sources); - $this->parent_compiler->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->filepath, $this->template->source->timestamp, $this->template->source->type); - $loop ++; - if ($no_sources) { - $this->inheritance_child = true; + $this->parent_compiler = $parent_compiler ? $parent_compiler : $this; + $nocache = isset($nocache) ? $nocache : false; + if (empty($template->compiled->nocache_hash)) { + $template->compiled->nocache_hash = $this->nocache_hash; } else { - $this->inheritance_child = false; + $this->nocache_hash = $template->compiled->nocache_hash; } - do { - // flag for nochache sections - $this->nocache = $nocache; - $this->tag_nocache = false; - // reset has nocache code flag - $this->template->has_nocache_code = false; - $this->has_variable_string = false; - $this->prefix_code = array(); - $_compiled_code = ''; - // flag for aborting current and start recompile - $this->abort_and_recompile = false; + // flag for nocache sections + $this->nocache = $nocache; + $this->tag_nocache = false; + // reset has nocache code flag + $this->template->compiled->has_nocache_code = false; + $this->has_variable_string = false; + $this->prefix_code = array(); + // add file dependency + $this->parent_compiler->template->compiled->file_dependency[$this->template->source->uid] = + array($this->template->source->filepath, $this->template->source->getTimeStamp(), + $this->template->source->type); + $this->smarty->_current_file = $this->template->source->filepath; + // get template source + if (!empty($this->template->source->components)) { + // we have array of inheritance templates by extends: resource + // generate corresponding source code sequence + $_content = + Smarty_Internal_Compile_Extends::extendsSourceArrayCode($this->template->source->components); + } else { // get template source - $_content = $this->template->source->content; - if ($_content != '') { - // run prefilter if required - if ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) && !$this->suppressFilter) { - $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template); - } - // call compiler - $_compiled_code = $this->doCompile($_content, true); - } - } while ($this->abort_and_recompile); - if ($this->smarty->debugging) { - Smarty_Internal_Debug::end_compile($this->template); + $_content = $this->template->source->getContent(); } + $_compiled_code = $this->postFilter($this->doCompile($this->preFilter($_content), true)); } - // restore source - $this->template->source = $save_source; - unset($save_source); - $this->smarty->_current_file = $this->template->source->filepath; - // free memory - unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex); - self::$_tag_objects = array(); - // return compiled code to template object - $merged_code = ''; - if (!empty($this->mergedSubTemplatesCode)) { - foreach ($this->mergedSubTemplatesCode as $code) { - $merged_code .= $code; + catch (Exception $e) { + if ($this->smarty->debugging) { + $this->smarty->_debug->end_compile($this->template); } + $this->_tag_stack = array(); + $this->_tag_objects = array(); + // free memory + $this->parent_compiler = null; + $this->template = null; + $this->parser = null; + throw $e; } - // run postfilter if required on compiled template code - if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter && $_compiled_code != '') { - $_compiled_code = Smarty_Internal_Filter_Handler::runFilter('post', $_compiled_code, $template); - } - if ($this->suppressTemplatePropertyHeader) { - $_compiled_code .= $merged_code; - } else { - $_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 - if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter) { - $_compiled_code .= Smarty_Internal_Filter_Handler::runFilter('post', $this->templateFunctionCode, $template); - } else { - $_compiled_code .= $this->templateFunctionCode; - } + if ($this->smarty->debugging) { + $this->smarty->_debug->end_compile($this->template); } - // unset content because template inheritance could have replace source with parent code - unset ($template->source->content); $this->parent_compiler = null; $this->template = null; + $this->parser = null; return $_compiled_code; } /** + * Optionally process compiled code by post filter + * + * @param string $code compiled code + * + * @return string + * @throws \SmartyException + */ + public function postFilter($code) + { + // run post filter if on code + if (!empty($code) && + (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) + ) { + return $this->smarty->ext->_filterHandler->runFilter('post', $code, $this->template); + } else { + return $code; + } + } + + /** + * Run optional prefilter + * + * @param string $_content template source + * + * @return string + * @throws \SmartyException + */ + public function preFilter($_content) + { + // run pre filter if required + if ($_content != '' && + ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre']))) + ) { + return $this->smarty->ext->_filterHandler->runFilter('pre', $_content, $this->template); + } else { + return $_content; + } + } + + /** * Compile Tag * This is a call back from the lexer/parser * @@ -478,17 +489,18 @@ abstract class Smarty_Internal_TemplateCompilerBase $this->has_code = true; $this->has_output = false; // log tag/attributes - if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) { - $this->template->used_tags[] = array($tag, $args); + if (isset($this->smarty->_cache['get_used_tags'])) { + $this->template->_cache['used_tags'][] = array($tag, $args); } // check nocache option flag - if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args) || in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args) + if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args) || + in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args) ) { $this->tag_nocache = true; } - // compile the smarty tag (required compile classes to compile the tag are autoloaded) + // compile the smarty tag (required compile classes to compile the tag are auto loaded) if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) { - if (isset($this->parent_compiler->templateProperties['tpl_function'][$tag])) { + if (isset($this->parent_compiler->template->tpl_function[$tag])) { // template defined by {template} tag $args['_attr']['name'] = "'" . $tag . "'"; $_output = $this->callTagCompiler('call', $args, $parameter); @@ -522,18 +534,23 @@ abstract class Smarty_Internal_TemplateCompilerBase // check if tag is a registered object if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_method'])) { $method = $parameter['object_method']; - if (!in_array($method, $this->smarty->registered_objects[$tag][3]) && (empty($this->smarty->registered_objects[$tag][1]) || in_array($method, $this->smarty->registered_objects[$tag][1])) + if (!in_array($method, $this->smarty->registered_objects[$tag][3]) && + (empty($this->smarty->registered_objects[$tag][1]) || + in_array($method, $this->smarty->registered_objects[$tag][1])) ) { return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $method); } elseif (in_array($method, $this->smarty->registered_objects[$tag][3])) { - return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $method); + return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, + $method); } else { // throw exception - $this->trigger_template_error('not allowed method "' . $method . '" in registered object "' . $tag . '"', $this->lex->taglineno); + $this->trigger_template_error('not allowed method "' . $method . '" in registered object "' . + $tag . '"', null, true); } } // check if tag is registered - foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $plugin_type) { + foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $plugin_type) + { if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) { // if compiler function plugin call it now if ($plugin_type == Smarty::PLUGIN_COMPILER) { @@ -552,20 +569,26 @@ abstract class Smarty_Internal_TemplateCompilerBase if (!is_array($function)) { return $function($new_args, $this); } elseif (is_object($function[0])) { - return $this->smarty->registered_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this); + return $this->smarty->registered_plugins[$plugin_type][$tag][0][0]->{$function[1]}($new_args, + $this); } else { return call_user_func_array($function, array($new_args, $this)); } } // compile registered function or block function if ($plugin_type == Smarty::PLUGIN_FUNCTION || $plugin_type == Smarty::PLUGIN_BLOCK) { - return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag); + return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, + $tag); } } } // check plugins from plugins folder - foreach ($this->smarty->plugin_search_order as $plugin_type) { - if ($plugin_type == Smarty::PLUGIN_COMPILER && $this->smarty->loadPlugin('smarty_compiler_' . $tag) && (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))) { + foreach ($this->plugin_search_order as $plugin_type) { + if ($plugin_type == Smarty::PLUGIN_COMPILER && + $this->smarty->loadPlugin('smarty_compiler_' . $tag) && + (!isset($this->smarty->security_policy) || + $this->smarty->security_policy->isTrustedTag($tag, $this)) + ) { $plugin = 'smarty_compiler_' . $tag; if (is_callable($plugin)) { // convert arguments format for old compiler plugins @@ -589,8 +612,11 @@ abstract class Smarty_Internal_TemplateCompilerBase throw new SmartyException("Plugin \"{$tag}\" not callable"); } else { if ($function = $this->getPlugin($tag, $plugin_type)) { - if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) { - return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, $tag, $function); + if (!isset($this->smarty->security_policy) || + $this->smarty->security_policy->isTrustedTag($tag, $this) + ) { + return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, + $tag, $function); } } } @@ -598,7 +624,7 @@ abstract class Smarty_Internal_TemplateCompilerBase if (is_callable($this->smarty->default_plugin_handler_func)) { $found = false; // look for already resolved tags - foreach ($this->smarty->plugin_search_order as $plugin_type) { + foreach ($this->plugin_search_order as $plugin_type) { if (isset($this->default_handler_plugins[$plugin_type][$tag])) { $found = true; break; @@ -606,7 +632,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } if (!$found) { // call default handler - foreach ($this->smarty->plugin_search_order as $plugin_type) { + foreach ($this->plugin_search_order as $plugin_type) { if ($this->getPluginFromDefaultHandler($tag, $plugin_type)) { $found = true; break; @@ -624,12 +650,14 @@ abstract class Smarty_Internal_TemplateCompilerBase if (!is_array($function)) { return $function($new_args, $this); } elseif (is_object($function[0])) { - return $this->default_handler_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this); + return $this->default_handler_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, + $this); } else { return call_user_func_array($function, array($new_args, $this)); } } else { - return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag); + return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, + $tag); } } } @@ -640,20 +668,36 @@ abstract class Smarty_Internal_TemplateCompilerBase if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_method'])) { $method = $parameter['object_method']; if (in_array($method, $this->smarty->registered_objects[$base_tag][3])) { - return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $method); + return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, + $method); } else { // throw exception - $this->trigger_template_error('not allowed closing tag method "' . $method . '" in registered object "' . $base_tag . '"', $this->lex->taglineno); + $this->trigger_template_error('not allowed closing tag method "' . $method . + '" in registered object "' . $base_tag . '"', null, true); } } // registered block tag ? - if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag]) || isset($this->default_handler_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) { + if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag]) || + isset($this->default_handler_plugins[Smarty::PLUGIN_BLOCK][$base_tag]) + ) { return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag); } + // registered function tag ? + if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION][$tag])) { + return $this->callTagCompiler('private_registered_function', $args, $parameter, $tag); + } // block plugin? if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) { return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function); } + // function plugin? + if ($function = $this->getPlugin($tag, Smarty::PLUGIN_FUNCTION)) { + if (!isset($this->smarty->security_policy) || + $this->smarty->security_policy->isTrustedTag($tag, $this) + ) { + return $this->callTagCompiler('private_function_plugin', $args, $parameter, $tag, $function); + } + } // registered compiler plugin ? if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag])) { // if compiler function plugin call it now @@ -665,7 +709,8 @@ abstract class Smarty_Internal_TemplateCompilerBase if (!is_array($function)) { return $function($args, $this); } elseif (is_object($function[0])) { - return $this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][0][0]->$function[1]($args, $this); + return $this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][0][0]->$function[1]($args, + $this); } else { return call_user_func_array($function, array($args, $this)); } @@ -684,7 +729,7 @@ abstract class Smarty_Internal_TemplateCompilerBase throw new SmartyException("Plugin \"{$tag}\" not callable"); } } - $this->trigger_template_error("unknown tag \"" . $tag . "\"", $this->lex->taglineno); + $this->trigger_template_error("unknown tag \"" . $tag . "\"", null, true); } } @@ -700,13 +745,27 @@ abstract class Smarty_Internal_TemplateCompilerBase if (strpos($variable, '(') == 0) { // not a variable variable $var = trim($variable, '\''); - $this->tag_nocache = $this->tag_nocache | $this->template->getVariable($var, null, true, false)->nocache; - $this->template->properties['variables'][$var] = $this->tag_nocache | $this->nocache; + $this->tag_nocache = $this->tag_nocache | + $this->template->ext->getTemplateVars->_getVariable($this->template, $var, null, true, false)->nocache; + // todo $this->template->compiled->properties['variables'][$var] = $this->tag_nocache | $this->nocache; } return '$_smarty_tpl->tpl_vars[' . $variable . ']->value'; } /** + * compile config variable + * + * @param string $variable + * + * @return string + */ + public function compileConfigVariable($variable) + { + // return '$_smarty_tpl->config_vars[' . $variable . ']'; + return '$_smarty_tpl->smarty->ext->configLoad->_getConfigVariable($_smarty_tpl, ' . $variable . ')'; + } + + /** * This method is called from parser to process a text content section * - remove text from inheritance child templates as they may generate output * - strip text if strip is enabled @@ -718,9 +777,9 @@ abstract class Smarty_Internal_TemplateCompilerBase public function processText($text) { if ($this->parser->strip) { - return new Smarty_Internal_ParseTree_Text($this->parser, preg_replace($this->stripRegEx, '', $text)); + return new Smarty_Internal_ParseTree_Text(preg_replace($this->stripRegEx, ' ', $text)); } else { - return new Smarty_Internal_ParseTree_Text($this->parser, $text); + return new Smarty_Internal_ParseTree_Text($text); } } @@ -728,7 +787,7 @@ abstract class Smarty_Internal_TemplateCompilerBase * lazy loads internal compile plugin for tag and calls the compile method * compile objects cached for reuse. * class name format: Smarty_Internal_Compile_TagName - * plugin filename format: Smarty_Internal_Tagname.php + * plugin filename format: Smarty_Internal_TagName.php * * @param string $tag tag name * @param array $args list of tag attributes @@ -740,23 +799,24 @@ abstract class Smarty_Internal_TemplateCompilerBase */ public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) { - // 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; - } + // re-use object if already exists + if (!isset($this->_tag_objects[$tag])) { + // lazy load internal compiler plugin + $_tag = explode('_', $tag); + $_tag = array_map('ucfirst', $_tag); + $class_name = 'Smarty_Internal_Compile_' . implode('_', $_tag); + if (class_exists($class_name) && + (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) + ) { + $this->_tag_objects[$tag] = new $class_name; + } else { + $this->_tag_objects[$tag] = false; + return false; } - // compile this tag - return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3); } - // no internal compile plugin for this tag - return false; + // compile this tag + return $this->_tag_objects[$tag] === false ? false : + $this->_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3); } /** @@ -771,18 +831,24 @@ abstract class Smarty_Internal_TemplateCompilerBase { $function = null; if ($this->template->caching && ($this->nocache || $this->tag_nocache)) { - if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) { - $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function']; - } elseif (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) { - $this->template->required_plugins['nocache'][$plugin_name][$plugin_type] = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]; - $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function']; + if (isset($this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type])) { + $function = + $this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type]['function']; + } elseif (isset($this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type])) { + $this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type] = + $this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type]; + $function = + $this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type]['function']; } } else { - if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) { - $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function']; - } elseif (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) { - $this->template->required_plugins['compiled'][$plugin_name][$plugin_type] = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]; - $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function']; + if (isset($this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type])) { + $function = + $this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type]['function']; + } elseif (isset($this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type])) { + $this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type] = + $this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type]; + $function = + $this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type]['function']; } } if (isset($function)) { @@ -798,11 +864,15 @@ abstract class Smarty_Internal_TemplateCompilerBase if (is_string($file)) { if ($this->template->caching && ($this->nocache || $this->tag_nocache)) { - $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['file'] = $file; - $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'] = $function; + $this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type]['file'] = + $file; + $this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name][$plugin_type]['function'] = + $function; } else { - $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['file'] = $file; - $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'] = $function; + $this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type]['file'] = + $file; + $this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name][$plugin_type]['function'] = + $function; } if ($plugin_type == 'modifier') { $this->modifier_plugins[$plugin_name] = true; @@ -831,17 +901,22 @@ abstract class Smarty_Internal_TemplateCompilerBase $callback = null; $script = null; $cacheable = true; - $result = call_user_func_array($this->smarty->default_plugin_handler_func, array($tag, $plugin_type, $this->template, &$callback, &$script, &$cacheable)); + $result = call_user_func_array($this->smarty->default_plugin_handler_func, + array($tag, $plugin_type, $this->template, &$callback, &$script, &$cacheable)); if ($result) { $this->tag_nocache = $this->tag_nocache || !$cacheable; if ($script !== null) { if (is_file($script)) { if ($this->template->caching && ($this->nocache || $this->tag_nocache)) { - $this->template->required_plugins['nocache'][$tag][$plugin_type]['file'] = $script; - $this->template->required_plugins['nocache'][$tag][$plugin_type]['function'] = $callback; + $this->parent_compiler->template->compiled->required_plugins['nocache'][$tag][$plugin_type]['file'] = + $script; + $this->parent_compiler->template->compiled->required_plugins['nocache'][$tag][$plugin_type]['function'] = + $callback; } else { - $this->template->required_plugins['compiled'][$tag][$plugin_type]['file'] = $script; - $this->template->required_plugins['compiled'][$tag][$plugin_type]['function'] = $callback; + $this->parent_compiler->template->compiled->required_plugins['compiled'][$tag][$plugin_type]['file'] = + $script; + $this->parent_compiler->template->compiled->required_plugins['compiled'][$tag][$plugin_type]['function'] = + $callback; } require_once $script; } else { @@ -898,16 +973,19 @@ abstract class Smarty_Internal_TemplateCompilerBase // If the template is not evaluated and we have a nocache section and or a nocache tag if ($is_code && !empty($content)) { // generate replacement code - if ((!($this->template->source->recompiled) || $this->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing && ($this->nocache || $this->tag_nocache) + if ((!($this->template->source->handler->recompiled) || $this->forceNocache) && $this->template->caching && + !$this->suppressNocacheProcessing && ($this->nocache || $this->tag_nocache) ) { - $this->template->has_nocache_code = true; + $this->template->compiled->has_nocache_code = true; $_output = addcslashes($content, '\'\\'); $_output = str_replace("^#^", "'", $_output); - $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n"; + $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . + "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n"; // make sure we include modifier plugins for nocache code foreach ($this->modifier_plugins as $plugin_name => $dummy) { - if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) { - $this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier']; + if (isset($this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name]['modifier'])) { + $this->parent_compiler->template->compiled->required_plugins['nocache'][$plugin_name]['modifier'] = + $this->parent_compiler->template->compiled->required_plugins['compiled'][$plugin_name]['modifier']; } } } else { @@ -924,55 +1002,46 @@ abstract class Smarty_Internal_TemplateCompilerBase } /** - * Generate nocache code string + * Get Id * - * @param string $code PHP code + * @param string $input * - * @return string + * @return bool|string */ - public function makeNocacheCode($code) + public function getId($input) { - return "echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/<?php " . str_replace("^#^", "'", addcslashes($code, '\'\\')) . "?>/*/%%SmartyNocache:{$this->nocache_hash}%%*/';\n"; + if (preg_match('~^[\'"]*([0-9]*[a-zA-Z_]\w*)[\'"]*$~', $input, $match)) { + return $match[1]; + } + return false; } /** - * push current file and line offset on stack for tracing {block} source lines + * Get variable name from string + * + * @param string $input * - * @param string $file new filename - * @param string $uid uid of file - * @param int $line line offset to source - * @param bool $debug false debug end_compile shall not be called + * @return bool|string */ - public function pushTrace($file, $uid, $line, $debug = true) + public function getVariableName($input) { - if ($this->smarty->debugging && $debug) { - Smarty_Internal_Debug::end_compile($this->template); - } - array_push($this->trace_stack, array($this->smarty->_current_file, $this->trace_filepath, $this->trace_uid, $this->trace_line_offset)); - $this->trace_filepath = $this->smarty->_current_file = $file; - $this->trace_uid = $uid; - $this->trace_line_offset = $line; - if ($this->smarty->debugging) { - Smarty_Internal_Debug::start_compile($this->template); + if (preg_match('~^[$]_smarty_tpl->tpl_vars\[[\'"]*([0-9]*[a-zA-Z_]\w*)[\'"]*\]->value$~', $input, $match)) { + return $match[1]; } + return false; } /** - * restore file and line offset + * Generate nocache code string + * + * @param string $code PHP code + * + * @return string */ - public function popTrace() + public function makeNocacheCode($code) { - if ($this->smarty->debugging) { - Smarty_Internal_Debug::end_compile($this->template); - } - $r = array_pop($this->trace_stack); - $this->smarty->_current_file = $r[0]; - $this->trace_filepath = $r[1]; - $this->trace_uid = $r[2]; - $this->trace_line_offset = $r[3]; - if ($this->smarty->debugging) { - Smarty_Internal_Debug::start_compile($this->template); - } + return "echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/<?php " . + str_replace("^#^", "'", addcslashes($code, '\'\\')) . "?>/*/%%SmartyNocache:{$this->nocache_hash}%%*/';\n"; } /** @@ -981,33 +1050,53 @@ abstract class Smarty_Internal_TemplateCompilerBase * In this case the parser is called to obtain information about expected tokens. * If parameter $args contains a string this is used as error message * - * @param string $args individual error message or null - * @param string $line line-number + * @param string $args individual error message or null + * @param string $line line-number + * @param null|bool $tagline if true the line number of last tag * - * @throws SmartyCompilerException when an unexpected token is found + * @throws \SmartyCompilerException when an unexpected token is found */ - public function trigger_template_error($args = null, $line = null) + public function trigger_template_error($args = null, $line = null, $tagline = null) { - // get template source line which has error - if (!isset($line)) { - $line = $this->lex->line; + $lex = $this->parser->lex; + if ($tagline === true) { + // get line number of Tag + $line = $lex->taglineno; + } elseif (!isset($line)) { + // get template source line which has error + $line = $lex->line; + } else { + $line = (int) $line; } + + if (in_array($this->template->source->type, array('eval', 'string'))) { + $templateName = $this->template->source->type . ':' . trim(preg_replace('![\t\r\n]+!', ' ', + strlen($lex->data) > 40 ? + substr($lex->data, 0, 40) . + '...' : $lex->data)); + } else { + $templateName = $this->template->source->type . ':' . $this->template->source->filepath; + } + // $line += $this->trace_line_offset; - $match = preg_split("/\n/", $this->lex->data); - $error_text = 'Syntax error in template "' . (empty($this->trace_filepath) ? $this->template->source->filepath : $this->trace_filepath) . '" on line ' . ($line + $this->trace_line_offset) . ' "' . trim(preg_replace('![\t\r\n]+!', ' ', $match[$line - 1])) . '" '; + $match = preg_split("/\n/", $lex->data); + $error_text = + 'Syntax error in template "' . (empty($this->trace_filepath) ? $templateName : $this->trace_filepath) . + '" on line ' . ($line + $this->trace_line_offset) . ' "' . + trim(preg_replace('![\t\r\n]+!', ' ', $match[$line - 1])) . '" '; if (isset($args)) { // individual error message $error_text .= $args; } else { $expect = array(); // expected token from parser - $error_text .= ' - Unexpected "' . $this->lex->value . '"'; + $error_text .= ' - Unexpected "' . $lex->value . '"'; if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4) { foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) { $exp_token = $this->parser->yyTokenName[$token]; - if (isset($this->lex->smarty_token_names[$exp_token])) { + if (isset($lex->smarty_token_names[$exp_token])) { // token type from lexer - $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"'; + $expect[] = '"' . $lex->smarty_token_names[$exp_token] . '"'; } else { // otherwise internal token name $expect[] = $this->parser->yyTokenName[$token]; |