3  * Smarty Internal Plugin Smarty Template Compiler Base
 
   5  * This file contains the basic classes and methodes for compiling Smarty templates with lexer/parser
 
  15 class Smarty_Internal_TemplateCompilerBase {
 
  16     // hash for nocache sections
 
  17     private $nocache_hash = null; 
 
  18     // suppress generation of nocache code
 
  19     public $suppressNocacheProcessing = false; 
 
  20     // compile tag objects
 
  21     static $_tag_objects = array(); 
 
  23     public $_tag_stack = array(); 
 
  25     public $template = null;
 
  26     // optional log of tag/attributes
 
  27     public $used_tags = array();
 
  32     public function __construct()
 
  34         $this->nocache_hash = str_replace('.', '-', uniqid(rand(), true));
 
  38      * Methode to compile a Smarty template
 
  40      * @param  $template template object to compile
 
  41      * @return bool true if compiling succeeded, false if it failed
 
  43     public function compileTemplate($template)
 
  45         if (empty($template->properties['nocache_hash'])) {
 
  46             $template->properties['nocache_hash'] = $this->nocache_hash;
 
  48             $this->nocache_hash = $template->properties['nocache_hash'];
 
  50         // flag for nochache sections
 
  51         $this->nocache = false;
 
  52         $this->tag_nocache = false; 
 
  53         // save template object in compiler class
 
  54         $this->template = $template;
 
  55         $this->smarty->_current_file = $this->template->getTemplateFilepath(); 
 
  56         // template header code
 
  57         $template_header = '';
 
  58         if (!$template->suppressHeader) {
 
  59             $template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
 
  60             $template_header .= "         compiled from \"" . $this->template->getTemplateFilepath() . "\" */ ?>\n";
 
  64             // flag for aborting current and start recompile
 
  65             $this->abort_and_recompile = false; 
 
  66             // get template source
 
  67             $_content = $template->getTemplateSource(); 
 
  68             // run prefilter if required
 
  69             if (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) {
 
  70                 $template->template_source = $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
 
  72             // on empty template just return header
 
  73             if ($_content == '') {
 
  74                 if ($template->suppressFileDependency) {
 
  75                     $template->compiled_template = '';
 
  77                     $template->compiled_template = $template_header . $template->createPropertyHeader();
 
  82             $_compiled_code = $this->doCompile($_content);
 
  83         } while ($this->abort_and_recompile); 
 
  84         // return compiled code to template object
 
  85         if ($template->suppressFileDependency) {
 
  86             $template->compiled_template = $_compiled_code;
 
  88             $template->compiled_template = $template_header . $template->createPropertyHeader() . $_compiled_code;
 
  90         // run postfilter if required
 
  91         if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
 
  92             $template->compiled_template = Smarty_Internal_Filter_Handler::runFilter('post', $template->compiled_template, $template);
 
  99      * This is a call back from the lexer/parser
 
 100      * It executes the required compile plugin for the Smarty tag
 
 102      * @param string $tag tag name
 
 103      * @param array $args array with tag attributes
 
 104      * @param array $parameter array with compilation parameter
 
 105      * @return string compiled code
 
 107     public function compileTag($tag, $args, $parameter = array())
 
 109         // $args contains the attributes parsed and compiled by the lexer/parser
 
 110         // assume that tag does compile into code, but creates no HTML output
 
 111         $this->has_code = true;
 
 112         $this->has_output = false;
 
 113         // log tag/attributes
 
 114         if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) {
 
 115                 $this->used_tags[] = array($tag,$args);
 
 117                 // check nocache option flag
 
 118         if (in_array("'nocache'",$args) || in_array(array('nocache'=>'true'),$args)
 
 119                         || in_array(array('nocache'=>'"true"'),$args) || in_array(array('nocache'=>"'true'"),$args)) {
 
 120                 $this->tag_nocache = true;
 
 122         // compile the smarty tag (required compile classes to compile the tag are autoloaded)
 
 123         if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
 
 124             if (isset($this->smarty->template_functions[$tag])) {
 
 125                 // template defined by {template} tag
 
 126                 $args['_attr']['name'] = "'" . $tag . "'";
 
 127                 $_output = $this->callTagCompiler('call', $args, $parameter);
 
 130         if ($_output !== false) {
 
 131             if ($_output !== true) {
 
 132                 // did we get compiled code
 
 133                 if ($this->has_code) {
 
 134                     // Does it create output?
 
 135                     if ($this->has_output) {
 
 138                     // return compiled code
 
 142             // tag did not produce compiled code
 
 145             // map_named attributes
 
 146             if (isset($args['_attr'])) {
 
 147                 foreach ($args['_attr'] as $key => $attribute) {
 
 148                     if (is_array($attribute)) {
 
 149                         $args = array_merge($args, $attribute);
 
 153             // not an internal compiler tag
 
 154             if (strlen($tag) < 6 || substr($tag, -5) != 'close') {
 
 155                 // check if tag is a registered object
 
 156                 if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_methode'])) {
 
 157                     $methode = $parameter['object_methode'];
 
 158                     if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
 
 159                             (empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
 
 160                         return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $methode);
 
 161                     } elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
 
 162                         return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
 
 164                         return $this->trigger_template_error ('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
 
 167                 // check if tag is registered
 
 168                 foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $type) {
 
 169                     if (isset($this->smarty->registered_plugins[$type][$tag])) {
 
 170                         // if compiler function plugin call it now
 
 171                         if ($type == Smarty::PLUGIN_COMPILER) {
 
 173                             foreach ($args as $mixed) {
 
 174                                 $new_args = array_merge($new_args, $mixed);
 
 176                             if (!$this->smarty->registered_plugins[$type][$tag][1]) {
 
 177                                 $this->tag_nocache = true;
 
 179                             $function = $this->smarty->registered_plugins[$type][$tag][0];
 
 180                             if (!is_array($function)) {
 
 181                                 return $function($new_args, $this);
 
 182                             } else if (is_object($function[0])) {
 
 183                                 return $this->smarty->registered_plugins[$type][$tag][0][0]->$function[1]($new_args, $this);
 
 185                                 return call_user_func_array($this->smarty->registered_plugins[$type][$tag][0], array($new_args, $this));
 
 188                         // compile registered function or block function
 
 189                         if ($type == Smarty::PLUGIN_FUNCTION || $type == Smarty::PLUGIN_BLOCK) {
 
 190                             return $this->callTagCompiler('private_registered_' . $type, $args, $parameter, $tag);
 
 194                 // check plugins from plugins folder
 
 195                 foreach ($this->smarty->plugin_search_order as $plugin_type) {
 
 196                     if ($plugin_type == Smarty::PLUGIN_BLOCK && $this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
 
 197                         $plugin = 'smarty_compiler_' . $tag;
 
 198                         if (is_callable($plugin)) {
 
 199                                 // convert arguments format for old compiler plugins
 
 201                             foreach ($args as $mixed) {
 
 202                                 $new_args = array_merge($new_args, $mixed);
 
 204                             return $plugin($new_args, $this->smarty);
 
 206                         if (class_exists($plugin, false)) {
 
 207                             $plugin_object = new $plugin;
 
 208                             if (method_exists($plugin_object, 'compile')) {
 
 209                                 return $plugin_object->compile($args, $this);
 
 212                         throw new SmartyException("Plugin \"{$tag}\" not callable");
 
 214                         if ($function = $this->getPlugin($tag, $plugin_type)) {
 
 215                             return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, $tag, $function);
 
 220                 // compile closing tag of block function
 
 221                 $base_tag = substr($tag, 0, -5); 
 
 222                 // check if closing tag is a registered object
 
 223                 if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_methode'])) {
 
 224                     $methode = $parameter['object_methode'];
 
 225                     if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
 
 226                         return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
 
 228                         return $this->trigger_template_error ('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
 
 231                 // registered block tag ?
 
 232                 if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) {
 
 233                     return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag);
 
 236                 if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) {
 
 237                     return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function);
 
 239                 if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
 
 240                     $plugin = 'smarty_compiler_' . $tag;
 
 241                     if (is_callable($plugin)) {
 
 242                         return $plugin($args, $this->smarty);
 
 244                     if (class_exists($plugin, false)) {
 
 245                         $plugin_object = new $plugin;
 
 246                         if (method_exists($plugin_object, 'compile')) {
 
 247                             return $plugin_object->compile($args, $this);
 
 250                     throw new SmartyException("Plugin \"{$tag}\" not callable");
 
 253             $this->trigger_template_error ("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
 
 258      * lazy loads internal compile plugin for tag and calls the compile methode
 
 260      * compile objects cached for reuse.
 
 261      * class name format:  Smarty_Internal_Compile_TagName
 
 262      * plugin filename format: Smarty_Internal_Tagname.php
 
 264      * @param  $tag string tag name
 
 265      * @param  $args array with tag attributes
 
 266      * @param  $param1 optional parameter
 
 267      * @param  $param2 optional parameter
 
 268      * @param  $param3 optional parameter
 
 269      * @return string compiled code
 
 271     public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
 
 273         // re-use object if already exists
 
 274         if (isset(self::$_tag_objects[$tag])) {
 
 276             return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
 
 278         // lazy load internal compiler plugin
 
 279         $class_name = 'Smarty_Internal_Compile_' . $tag;
 
 280         if ($this->smarty->loadPlugin($class_name)) {
 
 281             // use plugin if found
 
 282             self::$_tag_objects[$tag] = new $class_name; 
 
 284             return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
 
 286         // no internal compile plugin for this tag
 
 291      * Check for plugins and return function name
 
 293      * @param  $pugin_name string name of plugin or function
 
 294      * @param  $type string type of plugin
 
 295      * @return string call name of function
 
 297     public function getPlugin($plugin_name, $type)
 
 300         if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
 
 301             if (isset($this->template->required_plugins['nocache'][$plugin_name][$type])) {
 
 302                 $function = $this->template->required_plugins['nocache'][$plugin_name][$type]['function'];
 
 303             } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
 
 304                 $this->template->required_plugins['nocache'][$plugin_name][$type] = $this->template->required_plugins['compiled'][$plugin_name][$type];
 
 305                 $function = $this->template->required_plugins['nocache'][$plugin_name][$type]['function'];
 
 308             if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
 
 309                 $function = $this->template->required_plugins['compiled'][$plugin_name][$type]['function'];
 
 310             } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
 
 311                 $this->template->required_plugins['compiled'][$plugin_name][$type] = $this->template->required_plugins['nocache'][$plugin_name][$type];
 
 312                 $function = $this->template->required_plugins['compiled'][$plugin_name][$type]['function'];
 
 315         if (isset($function)) {
 
 316             if ($type == 'modifier') {
 
 317                 $this->template->saved_modifier[$plugin_name] = true;
 
 321         // loop through plugin dirs and find the plugin
 
 322         $function = 'smarty_' . $type . '_' . $plugin_name;
 
 324         foreach((array)$this->smarty->plugins_dir as $_plugin_dir) {
 
 325             $file = rtrim($_plugin_dir, '/\\') . DS . $type . '.' . $plugin_name . '.php';
 
 326             if (file_exists($file)) {
 
 327                 // require_once($file);
 
 333             if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
 
 334                 $this->template->required_plugins['nocache'][$plugin_name][$type]['file'] = $file;
 
 335                 $this->template->required_plugins['nocache'][$plugin_name][$type]['function'] = $function;
 
 337                 $this->template->required_plugins['compiled'][$plugin_name][$type]['file'] = $file;
 
 338                 $this->template->required_plugins['compiled'][$plugin_name][$type]['function'] = $function;
 
 340             if ($type == 'modifier') {
 
 341                 $this->template->saved_modifier[$plugin_name] = true;
 
 345         if (is_callable($function)) {
 
 346             // plugin function is defined in the script
 
 352      * Inject inline code for nocache template sections
 
 354      * This method gets the content of each template element from the parser.
 
 355      * If the content is compiled code and it should be not cached the code is injected
 
 356      * into the rendered output.
 
 358      * @param string $content content of template element
 
 359      * @param boolean $tag_nocache true if the parser detected a nocache situation
 
 360      * @param boolean $is_code true if content is compiled code
 
 361      * @return string content
 
 363     public function processNocacheCode ($content, $is_code)
 
 365         // If the template is not evaluated and we have a nocache section and or a nocache tag
 
 366         if ($is_code && !empty($content)) {
 
 367             // generate replacement code
 
 368             if ((!$this->template->resource_object->isEvaluated || $this->template->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing &&
 
 369                     ($this->nocache || $this->tag_nocache || $this->template->forceNocache == 2)) {
 
 370                 $this->template->has_nocache_code = true;
 
 371                 $_output = str_replace("'", "\'", $content);
 
 372                 $_output = str_replace("^#^", "'", $_output);
 
 373                 $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>"; 
 
 374                 // make sure we include modifer plugins for nocache code
 
 375                 if (isset($this->template->saved_modifier)) {
 
 376                     foreach ($this->template->saved_modifier as $plugin_name => $dummy) {
 
 377                         if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) {
 
 378                             $this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier'];
 
 381                     $this->template->saved_modifier = null;
 
 389         $this->suppressNocacheProcessing = false;
 
 390         $this->tag_nocache = false;
 
 394      * display compiler error messages without dying
 
 396      * If parameter $args is empty it is a parser detected syntax error.
 
 397      * In this case the parser is called to obtain information about expected tokens.
 
 399      * If parameter $args contains a string this is used as error message
 
 401      * @param  $args string individual error message or null
 
 403     public function trigger_template_error($args = null, $line = null)
 
 405         // get template source line which has error
 
 407             $line = $this->lex->line;
 
 409         $match = preg_split("/\n/", $this->lex->data);
 
 410         $error_text = 'Syntax Error in template "' . $this->template->getTemplateFilepath() . '"  on line ' . $line . ' "' . htmlspecialchars(trim(preg_replace('![\t\r\n]+!',' ',$match[$line-1]))) . '" ';
 
 412             // individual error message
 
 413             $error_text .= $args;
 
 415             // expected token from parser
 
 416             $error_text .= ' - Unexpected "' . $this->lex->value.'"';
 
 417             if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4 ) {
 
 418                 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
 
 419                     $exp_token = $this->parser->yyTokenName[$token];
 
 420                     if (isset($this->lex->smarty_token_names[$exp_token])) {
 
 421                         // token type from lexer
 
 422                         $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
 
 424                         // otherwise internal token name
 
 425                         $expect[] = $this->parser->yyTokenName[$token];
 
 428                 $error_text .= ', expected one of: ' . implode(' , ', $expect);
 
 431         throw new SmartyCompilerException($error_text);