home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Smarty_Compiler.class.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  65.6 KB  |  1,856 lines

  1. <?php
  2.  
  3. /**
  4.  * Project:     Smarty: the PHP compiling template engine
  5.  * File:        Smarty_Compiler.class.php
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  *
  21.  * You may contact the authors of Smarty by e-mail at:
  22.  * monte@ispi.net
  23.  * andrei@php.net
  24.  *
  25.  * Or, write to:
  26.  * Monte Ohrt
  27.  * Director of Technology, ispi
  28.  * 237 S. 70th suite 220
  29.  * Lincoln, NE 68510
  30.  *
  31.  * The latest version of Smarty can be obtained from:
  32.  * http://smarty.php.net/
  33.  *
  34.  * @link http://smarty.php.net/
  35.  * @author Monte Ohrt <monte@ispi.net>
  36.  * @author Andrei Zmievski <andrei@php.net>
  37.  * @version 2.5.0
  38.  * @copyright 2001,2002 ispi of Lincoln, Inc.
  39.  * @package Smarty
  40.  */
  41.  
  42. /* $Id: Smarty_Compiler.class.php,v 1.2 2003/04/20 21:30:18 CelloG Exp $ */
  43. /**
  44.  * Template compiling class
  45.  * @package Smarty
  46.  */
  47. class Smarty_Compiler extends Smarty {
  48.  
  49.     // internal vars
  50.     /**#@+
  51.      * @access private
  52.      */
  53.     var $_sectionelse_stack     =   array();    // keeps track of whether section had 'else' part
  54.     var $_foreachelse_stack     =   array();    // keeps track of whether foreach had 'else' part
  55.     var $_literal_blocks        =   array();    // keeps literal template blocks
  56.     var $_php_blocks            =   array();    // keeps php code blocks
  57.     var $_current_file          =   null;       // the current template being compiled
  58.     var $_current_line_no       =   1;          // line number for error messages
  59.     var $_capture_stack         =   array();    // keeps track of nested capture buffers
  60.     var $_plugin_info           =   array();    // keeps track of plugins to load
  61.     var $_init_smarty_vars      =   false;
  62.     var $_permitted_tokens        =    array('true','false','yes','no','on','off','null');
  63.     var $_db_qstr_regexp        =    null;        // regexps are setup in the constructor
  64.     var $_si_qstr_regexp        =    null;
  65.     var $_qstr_regexp            =    null;
  66.     var $_func_regexp            =    null;
  67.     var $_var_bracket_regexp    =    null;
  68.     var $_dvar_guts_regexp        =    null;
  69.     var $_dvar_regexp            =    null;
  70.     var $_cvar_regexp            =    null;
  71.     var $_svar_regexp            =    null;
  72.     var $_avar_regexp            =    null;
  73.     var $_mod_regexp            =    null;
  74.     var $_var_regexp            =    null;
  75.     var $_parenth_param_regexp    =    null;
  76.     var $_func_call_regexp        =    null;
  77.     var $_obj_ext_regexp        =    null;
  78.     var $_obj_start_regexp        =    null;
  79.     var $_obj_params_regexp        =    null;
  80.     var $_obj_call_regexp        =    null;
  81.     /**#@-*/
  82.     /**
  83.      * The class constructor.
  84.      */
  85.     function Smarty_Compiler()
  86.     {
  87.         // matches double quoted strings:
  88.         // "foobar"
  89.         // "foo\"bar"
  90.         $this->_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  91.  
  92.         // matches single quoted strings:
  93.         // 'foobar'
  94.         // 'foo\'bar'
  95.         $this->_si_qstr_regexp = '\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
  96.  
  97.         // matches single or double quoted strings
  98.         $this->_qstr_regexp = '(?:' . $this->_db_qstr_regexp . '|' . $this->_si_qstr_regexp . ')';
  99.  
  100.         // matches bracket portion of vars
  101.         // [0]
  102.         // [foo]
  103.         // [$bar]
  104.         $this->_var_bracket_regexp = '\[\$?[\w\.]+\]';
  105.                 
  106.         // matches $ vars (not objects):
  107.         // $foo
  108.         // $foo.bar
  109.         // $foo.bar.foobar
  110.         // $foo[0]
  111.         // $foo[$bar]
  112.         // $foo[5][blah]
  113.         // $foo[5].bar[$foobar][4]
  114.         $this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp
  115.                 . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*';
  116.         $this->_dvar_regexp = '\$' . $this->_dvar_guts_regexp;
  117.  
  118.         // matches config vars:
  119.         // #foo#
  120.         // #foobar123_foo#
  121.         $this->_cvar_regexp = '\#\w+\#';
  122.  
  123.         // matches section vars:
  124.         // %foo.bar%
  125.         $this->_svar_regexp = '\%\w+\.\w+\%';
  126.  
  127.         // matches all valid variables (no quotes, no modifiers)
  128.         $this->_avar_regexp = '(?:' . $this->_dvar_regexp . '|'
  129.            . $this->_cvar_regexp . '|' . $this->_svar_regexp . ')';
  130.  
  131.         // matches valid variable syntax:
  132.         // $foo
  133.         // $foo
  134.         // #foo#
  135.         // #foo#
  136.         // "text"
  137.         // "text"
  138.         $this->_var_regexp = '(?:' . $this->_avar_regexp . '|' . $this->_qstr_regexp . ')';
  139.                 
  140.         // matches valid object call (no objects allowed in parameters):
  141.         // $foo->bar
  142.         // $foo->bar()
  143.         // $foo->bar("text")
  144.         // $foo->bar($foo, $bar, "text")
  145.         // $foo->bar($foo, "foo")
  146.         // $foo->bar->foo()
  147.         // $foo->bar->foo->bar()
  148.         $this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')';
  149.         $this->_obj_params_regexp = '\((?:\w+|'
  150.                 . $this->_var_regexp . '(?:\s*,\s*(?:(?:\w+|'
  151.                 . $this->_var_regexp . ')))*)?\)';        
  152.         $this->_obj_start_regexp = '(?:' . $this->_dvar_regexp . '(?:' . $this->_obj_ext_regexp . ')+)';
  153.         $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . ')?)';
  154.  
  155.         // matches valid modifier syntax:
  156.         // |foo
  157.         // |@foo
  158.         // |foo:"bar"
  159.         // |foo:$bar
  160.         // |foo:"bar":$foobar
  161.         // |foo|bar
  162.         // |foo:$foo->bar
  163.         $this->_mod_regexp = '(?:\|@?\w+(?::(?>-?\w+|'
  164.            . $this->_obj_call_regexp . '|' . $this->_avar_regexp . '|' . $this->_qstr_regexp .'))*)';        
  165.                 
  166.         // matches valid function name:
  167.         // foo123
  168.         // _foo_bar
  169.         $this->_func_regexp = '[a-zA-Z_]\w*';
  170.  
  171.         // matches valid registered object:
  172.         // foo->bar
  173.         $this->_reg_obj_regexp = '[a-zA-Z_]\w*->[a-zA-Z_]\w*';
  174.                 
  175.         // matches valid parameter values:
  176.         // true
  177.         // $foo
  178.         // $foo|bar
  179.         // #foo#
  180.         // #foo#|bar
  181.         // "text"
  182.         // "text"|bar
  183.         // $foo->bar
  184.         $this->_param_regexp = '(?:\s*(?:' . $this->_obj_call_regexp . '|'
  185.            . $this->_var_regexp  . '|\w+)(?>' . $this->_mod_regexp . '*)\s*)';        
  186.         
  187.         // matches valid parenthesised function parameters:
  188.         // 
  189.         // "text"
  190.         //    $foo, $bar, "text"
  191.         // $foo|bar, "foo"|bar, $foo->bar($foo)|bar
  192.         $this->_parenth_param_regexp = '(?:\((?:\w+|'
  193.                 . $this->_param_regexp . '(?:\s*,\s*(?:(?:\w+|'
  194.                 . $this->_param_regexp . ')))*)?\))';
  195.     
  196.         // matches valid function call:
  197.         // foo()
  198.         // foo_bar($foo)
  199.         // _foo_bar($foo,"bar")
  200.         // foo123($foo,$foo->bar(),"foo")
  201.         $this->_func_call_regexp = '(?:' . $this->_func_regexp . '\s*(?:'
  202.            . $this->_parenth_param_regexp . '))';        
  203.     }            
  204.             
  205.     /**
  206.      * compile a template file
  207.      *
  208.      * sets $template_compiled to the compiled source
  209.      * @param string $tpl_file
  210.      * @param string $template_source
  211.      * @param string $template_compiled
  212.      * @return true
  213.      */
  214.     function _compile_file($tpl_file, $template_source, &$template_compiled)
  215.     {
  216.         if ($this->security) {
  217.             // do not allow php syntax to be executed unless specified
  218.             if ($this->php_handling == SMARTY_PHP_ALLOW &&
  219.                 !$this->security_settings['PHP_HANDLING']) {
  220.                 $this->php_handling = SMARTY_PHP_PASSTHRU;
  221.             }
  222.         }
  223.  
  224.         $this->_load_filters();
  225.  
  226.         $this->_current_file = $tpl_file;
  227.         $this->_current_line_no = 1;
  228.         $ldq = preg_quote($this->left_delimiter, '!');
  229.         $rdq = preg_quote($this->right_delimiter, '!');
  230.  
  231.         // run template source through prefilter functions
  232.         if (count($this->_plugins['prefilter']) > 0) {
  233.             foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
  234.                 if ($prefilter === false) continue;
  235.                 if ($prefilter[3] || $this->_plugin_implementation_exists($prefilter[0])) {
  236.                     $template_source = call_user_func_array($prefilter[0],
  237.                                                             array($template_source, &$this));
  238.                     $this->_plugins['prefilter'][$filter_name][3] = true;
  239.                 } else {
  240.                     $this->_trigger_fatal_error("[plugin] prefilter '$filter_name' is not implemented");
  241.                 }
  242.             }
  243.         }
  244.  
  245.         /* Annihilate the comments. */
  246.         $template_source = preg_replace("!({$ldq})\*(.*?)\*({$rdq})!se",
  247.                                         "'\\1*'.str_repeat(\"\n\", substr_count('\\2', \"\n\")) .'*\\3'",
  248.                                         $template_source);
  249.  
  250.         /* Pull out the literal blocks. */
  251.         preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_source, $_match);
  252.         $this->_literal_blocks = $_match[1];
  253.         $template_source = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s",
  254.                                         $this->quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $template_source);
  255.  
  256.         /* Pull out the php code blocks. */
  257.         preg_match_all("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s", $template_source, $_match);
  258.         $this->_php_blocks = $_match[1];
  259.         $template_source = preg_replace("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s",
  260.                                         $this->quote_replace($this->left_delimiter.'php'.$this->right_delimiter), $template_source);
  261.  
  262.         /* Gather all template tags. */
  263.         preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_source, $_match);
  264.         $template_tags = $_match[1];
  265.         /* Split content by template tags to obtain non-template content. */
  266.         $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $template_source);
  267.         
  268.         /* loop through text blocks */
  269.         for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) {
  270.             /* match anything resembling php tags */
  271.             if (preg_match_all('!(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?php[\"\']?)!is', $text_blocks[$curr_tb], $sp_match)) {
  272.                 /* replace tags with placeholders to prevent recursive replacements */
  273.                 $sp_match[1] = array_unique($sp_match[1]);
  274.                 usort($sp_match[1], '_smarty_sort_length');
  275.                 for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) {
  276.                     $text_blocks[$curr_tb] = str_replace($sp_match[1][$curr_sp],'%%%SMARTYSP'.$curr_sp.'%%%',$text_blocks[$curr_tb]);
  277.                 }
  278.                 /* process each one */
  279.                 for ($curr_sp = 0, $for_max2 = count($sp_match[0]); $curr_sp < $for_max2; $curr_sp++) {
  280.                     if ($this->php_handling == SMARTY_PHP_PASSTHRU) {
  281.                         /* echo php contents */
  282.                         $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."\n", $text_blocks[$curr_tb]);
  283.                     } else if ($this->php_handling == SMARTY_PHP_QUOTE) {
  284.                         /* quote php tags */
  285.                         $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', htmlspecialchars($sp_match[1][$curr_sp]), $text_blocks[$curr_tb]);
  286.                     } else if ($this->php_handling == SMARTY_PHP_REMOVE) {
  287.                         /* remove php tags */
  288.                         $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '', $text_blocks[$curr_tb]);
  289.                     } else {
  290.                         /* SMARTY_PHP_ALLOW, but echo non php starting tags */
  291.                         $sp_match[1][$curr_sp] = preg_replace('%(<\?(?!php|=|$))%i', '<?php echo \'\\1\'?>'."\n", $sp_match[1][$curr_sp]);
  292.                         $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', $sp_match[1][$curr_sp], $text_blocks[$curr_tb]);
  293.                     }
  294.                 }
  295.             }
  296.         }
  297.  
  298.         /* Compile the template tags into PHP code. */
  299.         $compiled_tags = array();
  300.         for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
  301.             $this->_current_line_no += substr_count($text_blocks[$i], "\n");
  302.             $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
  303.             $this->_current_line_no += substr_count($template_tags[$i], "\n");
  304.         }
  305.  
  306.         $template_compiled = '';
  307.  
  308.         /* Interleave the compiled contents and text blocks to get the final result. */
  309.         for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
  310.             $template_compiled .= $text_blocks[$i].$compiled_tags[$i];
  311.         }
  312.         $template_compiled .= $text_blocks[$i];
  313.  
  314.         // remove \n from the end of the file, if any
  315.         if ($template_compiled{strlen($template_compiled) - 1} == "\n" ) {
  316.             $template_compiled = substr($template_compiled, 0, -1);
  317.         }
  318.  
  319.         // run compiled template through postfilter functions
  320.         if (count($this->_plugins['postfilter']) > 0) {
  321.             foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
  322.                 if ($postfilter === false) continue;
  323.                 if ($postfilter[3] || $this->_plugin_implementation_exists($postfilter[0])) {
  324.                     $template_compiled = call_user_func_array($postfilter[0],
  325.                                                               array($template_compiled, &$this));
  326.                     $this->_plugins['postfilter'][$filter_name][3] = true;
  327.                 } else {
  328.                     $this->_trigger_fatal_error("Smarty plugin error: postfilter '$filter_name' is not implemented");
  329.                 }
  330.             }
  331.         }
  332.  
  333.         // put header at the top of the compiled template
  334.         $template_header = "<?php /* Smarty version ".$this->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
  335.         $template_header .= "         compiled from ".$tpl_file." */ ?>\n";
  336.  
  337.         /* Emit code to load needed plugins. */
  338.         if (count($this->_plugin_info)) {
  339.             $plugins_code = '<?php $this->_load_plugins(array(';
  340.             foreach ($this->_plugin_info as $plugin_type => $plugins) {
  341.                 foreach ($plugins as $plugin_name => $plugin_info) {
  342.                     $plugins_code .= "\narray('$plugin_type', '$plugin_name', '$plugin_info[0]', $plugin_info[1], ";
  343.                     $plugins_code .= $plugin_info[2] ? 'true),' : 'false),';
  344.                 }
  345.             }
  346.             $plugins_code .= ")); ?>";
  347.             $template_header .= $plugins_code;
  348.             $this->_plugin_info = array();
  349.         }
  350.  
  351.         if ($this->_init_smarty_vars) {
  352.             $template_header .= "<?php \$this->_assign_smarty_interface(); ?>\n";
  353.             $this->_init_smarty_vars = false;
  354.         }
  355.  
  356.         $template_compiled = $template_header . $template_compiled;
  357.  
  358.         return true;
  359.     }
  360.  
  361.     /**
  362.      * Compile a template tag
  363.      *
  364.      * @param string $template_tag
  365.      * @return string
  366.      */
  367.     function _compile_tag($template_tag)
  368.     {        
  369.         
  370.         /* Matched comment. */
  371.         if ($template_tag{0} == '*' && $template_tag{strlen($template_tag) - 1} == '*')
  372.             return '';
  373.         
  374.         /* Split tag into two three parts: command, command modifiers and the arguments. */
  375.         if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp
  376.                 . '|' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
  377.                       (?:\s+(.*))?$
  378.                     /xs', $template_tag, $match)) {
  379.             $this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
  380.         }
  381.  
  382.         $tag_command = $match[1];
  383.         $tag_modifier = isset($match[2]) ? $match[2] : null;
  384.         $tag_args = isset($match[3]) ? $match[3] : null;
  385.         
  386.         
  387.         /* If the tag name is a variable or object, we process it. */
  388.         if (preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$!', $tag_command)) {
  389.             $_return = $this->_parse_var_props($tag_command . $tag_modifier, $this->_parse_attrs($tag_args));
  390.             if(isset($_tag_attrs['assign'])) {
  391.                 return "<?php \$this->assign('" . $this->_dequote($_tag_attrs['assign']) . "', $_return ); ?>\n";  
  392.             } else {
  393.                 return "<?php echo $_return; ?>\n";
  394.             }
  395.         }
  396.         
  397.         /* If the tag name is a registered object, we process it. */
  398.         if (preg_match('!^' . $this->_reg_obj_regexp . '$!', $tag_command)) {
  399.             return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
  400.         }
  401.  
  402.         switch ($tag_command) {
  403.             case 'include':
  404.                 return $this->_compile_include_tag($tag_args);
  405.  
  406.             case 'include_php':
  407.                 return $this->_compile_include_php_tag($tag_args);
  408.  
  409.             case 'if':
  410.                 return $this->_compile_if_tag($tag_args);
  411.  
  412.             case 'else':
  413.                 return '<?php else: ?>';
  414.  
  415.             case 'elseif':
  416.                 return $this->_compile_if_tag($tag_args, true);
  417.  
  418.             case '/if':
  419.                 return '<?php endif; ?>';
  420.  
  421.             case 'capture':
  422.                 return $this->_compile_capture_tag(true, $tag_args);
  423.  
  424.             case '/capture':
  425.                 return $this->_compile_capture_tag(false);
  426.  
  427.             case 'ldelim':
  428.                 return $this->left_delimiter;
  429.  
  430.             case 'rdelim':
  431.                 return $this->right_delimiter;
  432.  
  433.             case 'section':
  434.                 array_push($this->_sectionelse_stack, false);
  435.                 return $this->_compile_section_start($tag_args);
  436.  
  437.             case 'sectionelse':
  438.                 $this->_sectionelse_stack[count($this->_sectionelse_stack)-1] = true;
  439.                 return "<?php endfor; else: ?>";
  440.  
  441.             case '/section':
  442.                 if (array_pop($this->_sectionelse_stack))
  443.                     return "<?php endif; ?>";
  444.                 else
  445.                     return "<?php endfor; endif; ?>";
  446.  
  447.             case 'foreach':
  448.                 array_push($this->_foreachelse_stack, false);
  449.                 return $this->_compile_foreach_start($tag_args);
  450.                 break;
  451.  
  452.             case 'foreachelse':
  453.                 $this->_foreachelse_stack[count($this->_foreachelse_stack)-1] = true;
  454.                 return "<?php endforeach; else: ?>";
  455.  
  456.             case '/foreach':
  457.                 if (array_pop($this->_foreachelse_stack))
  458.                     return "<?php endif; ?>";
  459.                 else
  460.                     return "<?php endforeach; endif; ?>";
  461.  
  462.             case 'literal':
  463.                 list (,$literal_block) = each($this->_literal_blocks);
  464.                 $this->_current_line_no += substr_count($literal_block, "\n");
  465.                 return "<?php echo '".str_replace("'", "\'", str_replace("\\", "\\\\", $literal_block))."'; ?>\n";
  466.  
  467.             case 'php':
  468.                 if ($this->security && !$this->security_settings['PHP_TAGS']) {
  469.                     $this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING, __FILE__, __LINE__);
  470.                     return;
  471.                 }
  472.                 list (,$php_block) = each($this->_php_blocks);
  473.                 $this->_current_line_no += substr_count($php_block, "\n");
  474.                 return '<?php '.$php_block.' ?>';
  475.  
  476.             case 'insert':
  477.                 return $this->_compile_insert_tag($tag_args);
  478.  
  479.             default:
  480.                 if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) {
  481.                     return $output;
  482.                 } else if ($this->_compile_block_tag($tag_command, $tag_args, $tag_modifier, $output)) {
  483.                     return $output;
  484.                 } else {
  485.                     return $this->_compile_custom_tag($tag_command, $tag_args, $tag_modifier);
  486.                 }
  487.         }
  488.     }
  489.  
  490.  
  491.     /**
  492.      * compile the custom compiler tag
  493.      *
  494.      * sets $output to the compiled custom compiler tag
  495.      * @param string $tag_command
  496.      * @param string $tag_args
  497.      * @param string $output
  498.      * @return boolean
  499.      */
  500.     function _compile_compiler_tag($tag_command, $tag_args, &$output)
  501.     {
  502.         $found = false;
  503.         $have_function = true;
  504.  
  505.         /*
  506.          * First we check if the compiler function has already been registered
  507.          * or loaded from a plugin file.
  508.          */
  509.         if (isset($this->_plugins['compiler'][$tag_command])) {
  510.             $found = true;
  511.             $plugin_func = $this->_plugins['compiler'][$tag_command][0];
  512.             if (!$this->_plugin_implementation_exists($plugin_func)) {
  513.                 $message = "compiler function '$tag_command' is not implemented";
  514.                 $have_function = false;
  515.             }
  516.         }
  517.         /*
  518.          * Otherwise we need to load plugin file and look for the function
  519.          * inside it.
  520.          */
  521.         else if ($plugin_file = $this->_get_plugin_filepath('compiler', $tag_command)) {
  522.             $found = true;
  523.  
  524.             include_once $plugin_file;
  525.  
  526.             $plugin_func = 'smarty_compiler_' . $tag_command;
  527.             if (!$this->_plugin_implementation_exists($plugin_func)) {
  528.                 $message = "plugin function $plugin_func() not found in $plugin_file\n";
  529.                 $have_function = false;
  530.             } else {
  531.                 $this->_plugins['compiler'][$tag_command] = array($plugin_func, null, null);
  532.             }
  533.         }
  534.  
  535.         /*
  536.          * True return value means that we either found a plugin or a
  537.          * dynamically registered function. False means that we didn't and the
  538.          * compiler should now emit code to load custom function plugin for this
  539.          * tag.
  540.          */
  541.         if ($found) {
  542.             if ($have_function) {
  543.                 $output = '<?php ' . call_user_func_array($plugin_func,
  544.                                                           array($tag_args, &$this)) . ' ?>';
  545.             } else {
  546.                 $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  547.             }
  548.             return true;
  549.         } else {
  550.             return false;
  551.         }
  552.     }
  553.  
  554.  
  555.     /**
  556.      * compile block function tag
  557.      *
  558.      * sets $output to compiled block function tag
  559.      * @param string $tag_command
  560.      * @param string $tag_args
  561.      * @param string $tag_modifier
  562.      * @param string $output
  563.      * @return boolean
  564.      */
  565.     function _compile_block_tag($tag_command, $tag_args, $tag_modifier, &$output)
  566.     {
  567.         if ($tag_command{0} == '/') {
  568.             $start_tag = false;
  569.             $tag_command = substr($tag_command, 1);
  570.         } else
  571.             $start_tag = true;
  572.  
  573.         $found = false;
  574.         $have_function = true;
  575.  
  576.         /*
  577.          * First we check if the block function has already been registered
  578.          * or loaded from a plugin file.
  579.          */
  580.         if (isset($this->_plugins['block'][$tag_command])) {
  581.             $found = true;
  582.             $plugin_func = $this->_plugins['block'][$tag_command][0];
  583.             if (!$this->_plugin_implementation_exists($plugin_func)) {
  584.                 $message = "block function '$tag_command' is not implemented";
  585.                 $have_function = false;
  586.             }
  587.         }
  588.         /*
  589.          * Otherwise we need to load plugin file and look for the function
  590.          * inside it.
  591.          */
  592.         else if ($plugin_file = $this->_get_plugin_filepath('block', $tag_command)) {
  593.             $found = true;
  594.  
  595.             include_once $plugin_file;
  596.  
  597.             $plugin_func = 'smarty_block_' . $tag_command;
  598.             if (!function_exists($plugin_func)) {
  599.                 $message = "plugin function $plugin_func() not found in $plugin_file\n";
  600.                 $have_function = false;
  601.             } else {
  602.                 $this->_plugins['block'][$tag_command] = array($plugin_func, null, null);
  603.             }
  604.         }
  605.  
  606.         if (!$found) {
  607.             return false;
  608.         } else if (!$have_function) {
  609.             $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  610.             return true;
  611.         }
  612.  
  613.         /*
  614.          * Even though we've located the plugin function, compilation
  615.          * happens only once, so the plugin will still need to be loaded
  616.          * at runtime for future requests.
  617.          */
  618.         $this->_add_plugin('block', $tag_command);
  619.  
  620.         if ($start_tag) {
  621.             $arg_list = array();
  622.             $attrs = $this->_parse_attrs($tag_args);
  623.             foreach ($attrs as $arg_name => $arg_value) {
  624.                 if (is_bool($arg_value))
  625.                     $arg_value = $arg_value ? 'true' : 'false';
  626.                 $arg_list[] = "'$arg_name' => $arg_value";
  627.             }
  628.  
  629.             $output = "<?php \$this->_tag_stack[] = array('$tag_command', array(".implode(',', (array)$arg_list).')); ';
  630.             $output .= $this->_compile_plugin_call('block', $tag_command).'(array('.implode(',', (array)$arg_list).'), null, $this, $_block_repeat=true);';
  631.             $output .= 'while ($_block_repeat) { ob_start(); ?>';
  632.         } else {
  633.             $output = '<?php $this->_block_content = ob_get_contents(); ob_end_clean(); ';
  634.             $_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $this->_block_content, $this, $_block_repeat=false)';
  635.             if ($tag_modifier != '') {
  636.                 $this->_parse_modifiers($_out_tag_text, $tag_modifier);
  637.             }
  638.             $output .= 'echo '.$_out_tag_text.'; } ';
  639.             $output .= " array_pop(\$this->_tag_stack); ?>";
  640.         }
  641.  
  642.         return true;
  643.     }
  644.  
  645.  
  646.     /**
  647.      * compile custom function tag
  648.      *
  649.      * @param string $tag_command
  650.      * @param string $tag_args
  651.      * @param string $tag_modifier
  652.      * @return string
  653.      */
  654.     function _compile_custom_tag($tag_command, $tag_args, $tag_modifier)
  655.     {
  656.         $this->_add_plugin('function', $tag_command);
  657.  
  658.         $arg_list = array();
  659.         $attrs = $this->_parse_attrs($tag_args);
  660.  
  661.         foreach ($attrs as $arg_name => $arg_value) {
  662.             if (is_bool($arg_value))
  663.                 $arg_value = $arg_value ? 'true' : 'false';
  664.             if (is_null($arg_value))
  665.                 $arg_value = 'null';
  666.             $arg_list[] = "'$arg_name' => $arg_value";
  667.         }
  668.         
  669.         $return = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', (array)$arg_list)."), \$this)";
  670.         
  671.         if($tag_modifier != '') {
  672.             $this->_parse_modifiers($return, $tag_modifier);
  673.         }
  674.         
  675.         return '<?php echo ' . $return . " ; ?>\n";
  676.     }
  677.  
  678.     /**
  679.      * compile a registered object tag
  680.      *
  681.      * @param string $tag_command
  682.      * @param array $attrs
  683.      * @param string $tag_modifier
  684.      * @return string
  685.      */
  686.     function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
  687.     {
  688.         list($object, $obj_comp) = explode('->', $tag_command);
  689.  
  690.         $arg_list = array();
  691.         if(count($attrs)) {
  692.             $_assign_var = false;
  693.             foreach ($attrs as $arg_name => $arg_value) {
  694.                 if($arg_name == 'assign') {
  695.                     $_assign_var = $arg_value;
  696.                     unset($attrs['assign']);
  697.                     continue;
  698.                 }
  699.                 if (is_bool($arg_value))
  700.                     $arg_value = $arg_value ? 'true' : 'false';
  701.                 $arg_list[] = "'$arg_name' => $arg_value";
  702.             }
  703.         }
  704.                 
  705.         if(!is_object($this->_reg_objects[$object][0])) {
  706.             $this->_trigger_fatal_error("registered '$object' is not an object");
  707.         } elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
  708.             $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'");
  709.         } elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
  710.             // method
  711.             if($this->_reg_objects[$object][2]) {
  712.                 // smarty object argument format
  713.                 $return = "\$this->_reg_objects['$object'][0]->$obj_comp(array(".implode(',', (array)$arg_list)."), \$this)";
  714.             } else {
  715.                 // traditional argument format
  716.                 $return = "\$this->_reg_objects['$object'][0]->$obj_comp(".implode(',', array_values($attrs)).")";
  717.             }
  718.         } else {
  719.             // property
  720.             $return = "\$this->_reg_objects['$object'][0]->$obj_comp";
  721.         }
  722.         
  723.         if($tag_modifier != '') {
  724.             $this->_parse_modifiers($return, $tag_modifier);
  725.         }
  726.         
  727.         if($_assign_var) {
  728.             return "<?php \$this->assign('" . $this->_dequote($_assign_var) ."',  $return); ?>\n";
  729.         } else {
  730.             return '<?php echo ' . $return . "; ?>\n";
  731.         }
  732.     }
  733.     
  734.     
  735.  
  736.     /**
  737.      * Compile {insert ...} tag
  738.      *
  739.      * @param string $tag_args
  740.      * @return string
  741.      */
  742.     function _compile_insert_tag($tag_args)
  743.     {
  744.         $attrs = $this->_parse_attrs($tag_args);
  745.         $name = $this->_dequote($attrs['name']);
  746.  
  747.         if (empty($name)) {
  748.             $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
  749.         }
  750.  
  751.         if (!empty($attrs['script'])) {
  752.             $delayed_loading = true;
  753.         } else {
  754.             $delayed_loading = false;            
  755.         }
  756.  
  757.         foreach ($attrs as $arg_name => $arg_value) {
  758.             if (is_bool($arg_value))
  759.                 $arg_value = $arg_value ? 'true' : 'false';
  760.             $arg_list[] = "'$arg_name' => $arg_value";
  761.         }
  762.  
  763.         $this->_add_plugin('insert', $name, $delayed_loading);
  764.  
  765.         return "<?php echo \$this->_run_insert_handler(array(".implode(', ', (array)$arg_list).")); ?>\n";
  766.     }
  767.  
  768.     /**
  769.      * Compile {include ...} tag
  770.      *
  771.      * @param string $tag_args
  772.      * @return string
  773.      */
  774.     function _compile_include_tag($tag_args)
  775.     {
  776.         $attrs = $this->_parse_attrs($tag_args);
  777.         $arg_list = array();
  778.  
  779.         if (empty($attrs['file'])) {
  780.             $this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__);
  781.         }
  782.  
  783.         foreach ($attrs as $arg_name => $arg_value) {
  784.             if ($arg_name == 'file') {
  785.                 $include_file = $arg_value;
  786.                 continue;
  787.             } else if ($arg_name == 'assign') {
  788.                 $assign_var = $arg_value;
  789.                 continue;
  790.             }
  791.             if (is_bool($arg_value))
  792.                 $arg_value = $arg_value ? 'true' : 'false';
  793.             $arg_list[] = "'$arg_name' => $arg_value";
  794.         }
  795.  
  796.         $output = '<?php ';
  797.  
  798.         if (isset($assign_var)) {
  799.             $output .= "ob_start();\n";
  800.         }
  801.  
  802.         $output .=  
  803.             "\$_smarty_tpl_vars = \$this->_tpl_vars;\n" .
  804.             "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" .
  805.             "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
  806.             "unset(\$_smarty_tpl_vars);\n";
  807.  
  808.         if (isset($assign_var)) {
  809.             $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
  810.         }
  811.  
  812.         $output .= ' ?>';
  813.  
  814.         return $output;
  815.  
  816.     }
  817.  
  818.     /**
  819.      * Compile {include ...} tag
  820.      *
  821.      * @param string $tag_args
  822.      * @return string
  823.      */
  824.     function _compile_include_php_tag($tag_args)
  825.     {
  826.         $attrs = $this->_parse_attrs($tag_args);
  827.  
  828.         if (empty($attrs['file'])) {
  829.             $this->_syntax_error("missing 'file' attribute in include_php tag", E_USER_ERROR, __FILE__, __LINE__);
  830.         }
  831.  
  832.         $assign_var = $this->_dequote($attrs['assign']);        
  833.         $once_var = ( $attrs['once'] == 'false' ) ? 'false' : 'true';
  834.         
  835.         foreach($attrs as $arg_name => $arg_value) {
  836.             if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {
  837.                 if(is_bool($arg_value))
  838.                     $arg_value = $arg_value ? 'true' : 'false';
  839.                 $arg_list[] = "'$arg_name' => $arg_value";
  840.             }
  841.         }
  842.  
  843.         $output =
  844.             "<?php \$this->_smarty_include_php($attrs[file], '$assign_var', $once_var, " .
  845.             "array(".implode(',', (array)$arg_list).")); ?>";
  846.                 
  847.         return $output;
  848.     }
  849.     
  850.  
  851.     /**
  852.      * Compile {section ...} tag
  853.      *
  854.      * @param string $tag_args
  855.      * @return string
  856.      */
  857.     function _compile_section_start($tag_args)
  858.     {
  859.         $attrs = $this->_parse_attrs($tag_args);
  860.         $arg_list = array();
  861.  
  862.         $output = '<?php ';
  863.         $section_name = $attrs['name'];
  864.         if (empty($section_name)) {
  865.             $this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
  866.         }
  867.  
  868.         $output .= "if (isset(\$this->_sections[$section_name])) unset(\$this->_sections[$section_name]);\n";
  869.         $section_props = "\$this->_sections[$section_name]";
  870.  
  871.         foreach ($attrs as $attr_name => $attr_value) {
  872.             switch ($attr_name) {
  873.                 case 'loop':
  874.                     $output .= "{$section_props}['loop'] = is_array($attr_value) ? count($attr_value) : max(0, (int)$attr_value);\n";
  875.                     break;
  876.  
  877.                 case 'show':
  878.                     if (is_bool($attr_value))
  879.                         $show_attr_value = $attr_value ? 'true' : 'false';
  880.                     else
  881.                         $show_attr_value = "(bool)$attr_value";
  882.                     $output .= "{$section_props}['show'] = $show_attr_value;\n";
  883.                     break;
  884.  
  885.                 case 'name':
  886.                     $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  887.                     break;
  888.  
  889.                 case 'max':
  890.                 case 'start':
  891.                     $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  892.                     break;
  893.  
  894.                 case 'step':
  895.                     $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  896.                     break;
  897.  
  898.                 default:
  899.                     $this->_syntax_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);
  900.                     break;
  901.             }
  902.         }
  903.  
  904.         if (!isset($attrs['show']))
  905.             $output .= "{$section_props}['show'] = true;\n";
  906.  
  907.         if (!isset($attrs['loop']))
  908.             $output .= "{$section_props}['loop'] = 1;\n";
  909.  
  910.         if (!isset($attrs['max']))
  911.             $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  912.         else
  913.             $output .= "if ({$section_props}['max'] < 0)\n" .
  914.                        "    {$section_props}['max'] = {$section_props}['loop'];\n";
  915.  
  916.         if (!isset($attrs['step']))
  917.             $output .= "{$section_props}['step'] = 1;\n";
  918.  
  919.         if (!isset($attrs['start']))
  920.             $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  921.         else {
  922.             $output .= "if ({$section_props}['start'] < 0)\n" .
  923.                        "    {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
  924.                        "else\n" .
  925.                        "    {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  926.         }
  927.  
  928.         $output .= "if ({$section_props}['show']) {\n";
  929.         if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max'])) {
  930.             $output .= "    {$section_props}['total'] = {$section_props}['loop'];\n";
  931.         } else {
  932.             $output .= "    {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
  933.         }
  934.         $output .= "    if ({$section_props}['total'] == 0)\n" .
  935.                    "        {$section_props}['show'] = false;\n" .
  936.                    "} else\n" .
  937.                    "    {$section_props}['total'] = 0;\n";
  938.  
  939.         $output .= "if ({$section_props}['show']):\n";
  940.         $output .= "
  941.             for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  942.                  {$section_props}['iteration'] <= {$section_props}['total'];
  943.                  {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  944.         $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  945.         $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  946.         $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  947.         $output .= "{$section_props}['first']      = ({$section_props}['iteration'] == 1);\n";
  948.         $output .= "{$section_props}['last']       = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  949.  
  950.         $output .= "?>";
  951.  
  952.         return $output;
  953.     }
  954.  
  955.     
  956.     /**
  957.      * Compile {foreach ...} tag.
  958.      *
  959.      * @param string $tag_args
  960.      * @return string
  961.      */
  962.     function _compile_foreach_start($tag_args)
  963.     {
  964.         $attrs = $this->_parse_attrs($tag_args);
  965.         $arg_list = array();
  966.  
  967.         if (empty($attrs['from'])) {
  968.             $this->_syntax_error("missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
  969.         }
  970.  
  971.         if (empty($attrs['item'])) {
  972.             $this->_syntax_error("missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
  973.         }
  974.  
  975.         $from = $attrs['from'];
  976.         $item = $this->_dequote($attrs['item']);
  977.         if (isset($attrs['name']))
  978.             $name = $attrs['name'];
  979.  
  980.         $output = '<?php ';
  981.         if (isset($name)) {
  982.             $output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n";
  983.             $foreach_props = "\$this->_foreach[$name]";
  984.         }
  985.  
  986.         $key_part = '';
  987.  
  988.         foreach ($attrs as $attr_name => $attr_value) {
  989.             switch ($attr_name) {
  990.                 case 'key':
  991.                     $key  = $this->_dequote($attrs['key']);
  992.                     $key_part = "\$this->_tpl_vars['$key'] => ";
  993.                     break;
  994.  
  995.                 case 'name':
  996.                     $output .= "{$foreach_props}['$attr_name'] = $attr_value;\n";
  997.                     break;
  998.             }
  999.         }
  1000.  
  1001.         if (isset($name)) {
  1002.             $output .= "{$foreach_props}['total'] = count((array)$from);\n";
  1003.             $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n";
  1004.             $output .= "if ({$foreach_props}['show']):\n";
  1005.             $output .= "{$foreach_props}['iteration'] = 0;\n";
  1006.             $output .= "    foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n";
  1007.             $output .= "        {$foreach_props}['iteration']++;\n";
  1008.             $output .= "        {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n";
  1009.             $output .= "        {$foreach_props}['last']  = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n";
  1010.         } else {
  1011.             $output .= "if (count((array)$from)):\n";
  1012.             $output .= "    foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n";
  1013.         }
  1014.         $output .= '?>';
  1015.  
  1016.         return $output;
  1017.     }
  1018.  
  1019.  
  1020.     /**
  1021.      * Compile {capture} .. {/capture} tags
  1022.      *
  1023.      * @param boolean $start true if this is the {capture} tag
  1024.      * @param string $tag_args
  1025.      * @return string
  1026.      */
  1027.     function _compile_capture_tag($start, $tag_args = '')
  1028.     {
  1029.         $attrs = $this->_parse_attrs($tag_args);
  1030.  
  1031.         if ($start) {
  1032.             if (isset($attrs['name']))
  1033.                 $buffer = $attrs['name'];
  1034.             else
  1035.                 $buffer = "'default'";
  1036.  
  1037.             $output = "<?php ob_start(); ?>";
  1038.             $this->_capture_stack[] = $buffer;
  1039.         } else {
  1040.             $buffer = array_pop($this->_capture_stack);
  1041.             $output = "<?php \$this->_smarty_vars['capture'][$buffer] = ob_get_contents(); ob_end_clean(); ?>";
  1042.         }
  1043.  
  1044.         return $output;
  1045.     }
  1046.  
  1047.     /**
  1048.      * Compile {if ...} tag
  1049.      *
  1050.      * @param string $tag_args
  1051.      * @param boolean $elseif if true, uses elseif instead of if
  1052.      * @return string
  1053.      */
  1054.     function _compile_if_tag($tag_args, $elseif = false)
  1055.     {
  1056.  
  1057.         /* Tokenize args for 'if' tag. */
  1058.         preg_match_all('/(?>
  1059.                 ' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)? | # valid object call
  1060.                 ' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)?    | # var or quoted string
  1061.                 \-?0[xX][0-9a-fA-F]+|\-?\d+(?:\.\d+)?|\.\d+|!==|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\||\%|\+|\-|\/|\*|\@    | # valid non-word token
  1062.                 \b\w+\b                                                        | # valid word token
  1063.                 \S+                                                           # anything else
  1064.                 )/x', $tag_args, $match);
  1065.                 
  1066.         $tokens = $match[0];
  1067.                 
  1068.         // make sure we have balanced parenthesis
  1069.         $token_count = array_count_values($tokens);
  1070.         if(isset($token_count['(']) && $token_count['('] != $token_count[')']) {
  1071.             $this->_syntax_error("unbalanced parenthesis in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1072.         }
  1073.                                                 
  1074.         $is_arg_stack = array();
  1075.  
  1076.         for ($i = 0; $i < count($tokens); $i++) {
  1077.  
  1078.             $token = &$tokens[$i];
  1079.             
  1080.             switch (strtolower($token)) {
  1081.                 case '!':
  1082.                 case '%':
  1083.                 case '!==':
  1084.                 case '==':
  1085.                 case '===':
  1086.                 case '>':
  1087.                 case '<':
  1088.                 case '!=':
  1089.                 case '<>':
  1090.                 case '<<':
  1091.                 case '>>':
  1092.                 case '<=':
  1093.                 case '>=':
  1094.                 case '&&':
  1095.                 case '||':
  1096.                 case '|':
  1097.                 case '^':
  1098.                 case '&':
  1099.                 case '~':
  1100.                 case ')':
  1101.                 case ',':
  1102.                 case '+':
  1103.                 case '-':
  1104.                 case '*':
  1105.                 case '/':
  1106.                 case '@':
  1107.                     break;                    
  1108.  
  1109.                 case 'eq':
  1110.                     $token = '==';
  1111.                     break;
  1112.  
  1113.                 case 'ne':
  1114.                 case 'neq':
  1115.                     $token = '!=';
  1116.                     break;
  1117.  
  1118.                 case 'lt':
  1119.                     $token = '<';
  1120.                     break;
  1121.  
  1122.                 case 'le':
  1123.                 case 'lte':
  1124.                     $token = '<=';
  1125.                     break;
  1126.  
  1127.                 case 'gt':
  1128.                     $token = '>';
  1129.                     break;
  1130.  
  1131.                 case 'ge':
  1132.                 case 'gte':
  1133.                     $token = '>=';
  1134.                     break;
  1135.  
  1136.                 case 'and':
  1137.                     $token = '&&';
  1138.                     break;
  1139.  
  1140.                 case 'or':
  1141.                     $token = '||';
  1142.                     break;
  1143.  
  1144.                 case 'not':
  1145.                     $token = '!';
  1146.                     break;
  1147.  
  1148.                 case 'mod':
  1149.                     $token = '%';
  1150.                     break;
  1151.  
  1152.                 case '(':
  1153.                     array_push($is_arg_stack, $i);
  1154.                     break;
  1155.                     
  1156.                 case 'is':
  1157.                     /* If last token was a ')', we operate on the parenthesized
  1158.                        expression. The start of the expression is on the stack.
  1159.                        Otherwise, we operate on the last encountered token. */
  1160.                     if ($tokens[$i-1] == ')')
  1161.                         $is_arg_start = array_pop($is_arg_stack);
  1162.                     else
  1163.                         $is_arg_start = $i-1;
  1164.                     /* Construct the argument for 'is' expression, so it knows
  1165.                        what to operate on. */
  1166.                     $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start));
  1167.                     
  1168.                     /* Pass all tokens from next one until the end to the
  1169.                        'is' expression parsing function. The function will
  1170.                        return modified tokens, where the first one is the result
  1171.                        of the 'is' expression and the rest are the tokens it
  1172.                        didn't touch. */                    
  1173.                     $new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
  1174.                                         
  1175.                     /* Replace the old tokens with the new ones. */
  1176.                     array_splice($tokens, $is_arg_start, count($tokens), $new_tokens);
  1177.  
  1178.                     /* Adjust argument start so that it won't change from the
  1179.                        current position for the next iteration. */
  1180.                     $i = $is_arg_start;
  1181.                     break;
  1182.                     
  1183.                 default:
  1184.                     if(preg_match('!^' . $this->_func_regexp . '$!', $token) ) {
  1185.                             // function call    
  1186.                             if($this->security &&
  1187.                                !in_array($token, $this->security_settings['IF_FUNCS'])) {
  1188.                                 $this->_syntax_error("(secure mode) '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1189.                             }                            
  1190.                     } elseif(preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
  1191.                         // object or variable
  1192.                         $token = $this->_parse_var_props($token);
  1193.                     } elseif(is_numeric($token)) {
  1194.                         // number, skip it
  1195.                     } else {
  1196.                         $this->_syntax_error("unidentified token '$token'", E_USER_ERROR, __FILE__, __LINE__);
  1197.                     }
  1198.                     break;
  1199.             }
  1200.         }
  1201.                 
  1202.         if ($elseif)
  1203.             return '<?php elseif ('.implode(' ', $tokens).'): ?>';
  1204.         else
  1205.             return '<?php if ('.implode(' ', $tokens).'): ?>';
  1206.     }
  1207.  
  1208.  
  1209.     /**
  1210.      * Parse is expression
  1211.      *
  1212.      * @param string $is_arg
  1213.      * @param array $tokens
  1214.      * @return array
  1215.      */
  1216.     function _parse_is_expr($is_arg, $tokens)
  1217.     {
  1218.         $expr_end = 0;
  1219.         $negate_expr = false;
  1220.  
  1221.         if (($first_token = array_shift($tokens)) == 'not') {
  1222.             $negate_expr = true;
  1223.             $expr_type = array_shift($tokens);
  1224.         } else
  1225.             $expr_type = $first_token;
  1226.  
  1227.         switch ($expr_type) {
  1228.             case 'even':
  1229.                 if (@$tokens[$expr_end] == 'by') {
  1230.                     $expr_end++;
  1231.                     $expr_arg = $tokens[$expr_end++];
  1232.                     $expr = "!(($is_arg / $expr_arg) % " . $this->_parse_var_props($expr_arg) . ")";
  1233.                 } else
  1234.                     $expr = "!($is_arg % 2)";
  1235.                 break;
  1236.  
  1237.             case 'odd':
  1238.                 if (@$tokens[$expr_end] == 'by') {
  1239.                     $expr_end++;
  1240.                     $expr_arg = $tokens[$expr_end++];
  1241.                     $expr = "(($is_arg / $expr_arg) % ". $this->_parse_var_props($expr_arg) . ")";
  1242.                 } else
  1243.                     $expr = "($is_arg % 2)";
  1244.                 break;
  1245.  
  1246.             case 'div':
  1247.                 if (@$tokens[$expr_end] == 'by') {
  1248.                     $expr_end++;
  1249.                     $expr_arg = $tokens[$expr_end++];
  1250.                     $expr = "!($is_arg % " . $this->_parse_var_props($expr_arg) . ")";
  1251.                 } else {
  1252.                     $this->_syntax_error("expecting 'by' after 'div'", E_USER_ERROR, __FILE__, __LINE__);
  1253.                 }
  1254.                 break;
  1255.  
  1256.             default:
  1257.                 $this->_syntax_error("unknown 'is' expression - '$expr_type'", E_USER_ERROR, __FILE__, __LINE__);
  1258.                 break;
  1259.         }
  1260.  
  1261.         if ($negate_expr) {
  1262.             $expr = "!($expr)";
  1263.         }
  1264.         
  1265.         array_splice($tokens, 0, $expr_end, $expr);        
  1266.         
  1267.         return $tokens;
  1268.     }
  1269.  
  1270.  
  1271.     /**
  1272.      * Parse attribute string
  1273.      *
  1274.      * @param string $tag_args
  1275.      * @param true $quote unused?
  1276.      * @return array
  1277.      */
  1278.     function _parse_attrs($tag_args, $quote = true)
  1279.     {
  1280.                 
  1281.         /* Tokenize tag attributes. */
  1282.         preg_match_all('/(?:' . $this->_obj_call_regexp . '|' . $this->_qstr_regexp . ' | (?>[^"\'=\s]+)
  1283.                          )+ |
  1284.                          [=]
  1285.                         /x', $tag_args, $match);
  1286.         $tokens       = $match[0];        
  1287.                         
  1288.         $attrs = array();
  1289.         /* Parse state:
  1290.             0 - expecting attribute name
  1291.             1 - expecting '='
  1292.             2 - expecting attribute value (not '=') */
  1293.         $state = 0;
  1294.                 
  1295.         foreach ($tokens as $token) {
  1296.             switch ($state) {
  1297.                 case 0:
  1298.                     /* If the token is a valid identifier, we set attribute name
  1299.                        and go to state 1. */
  1300.                     if (preg_match('!^\w+$!', $token)) {
  1301.                         $attr_name = $token;
  1302.                         $state = 1;
  1303.                     } else
  1304.                         $this->_syntax_error("invalid attribute name: '$token'", E_USER_ERROR, __FILE__, __LINE__);
  1305.                     break;
  1306.  
  1307.                 case 1:
  1308.                     /* If the token is '=', then we go to state 2. */
  1309.                     if ($token == '=') {
  1310.                         $state = 2;
  1311.                     } else
  1312.                         $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
  1313.                     break;
  1314.  
  1315.                 case 2:
  1316.                     /* If token is not '=', we set the attribute value and go to
  1317.                        state 0. */
  1318.                     if ($token != '=') {
  1319.                         /* We booleanize the token if it's a non-quoted possible
  1320.                            boolean value. */
  1321.                         if (preg_match('!^(on|yes|true)$!', $token)) {
  1322.                             $token = 'true';
  1323.                         } else if (preg_match('!^(off|no|false)$!', $token)) {
  1324.                             $token = 'false';
  1325.                         } else if ($token == 'null') {
  1326.                             $token = 'null';
  1327.                         } else if (preg_match('!^-?([0-9]+|0[xX][0-9a-fA-F]+)$!', $token)) {
  1328.                             /* treat integer literally */
  1329.                         } else if (!preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . ')*$!', $token)) {
  1330.                             /* treat as a string, double-quote it escaping quotes */
  1331.                             $token = '"'.addslashes($token).'"';
  1332.                         }
  1333.  
  1334.                         $attrs[$attr_name] = $token;
  1335.                         $state = 0;
  1336.                     } else
  1337.                         $this->_syntax_error("'=' cannot be an attribute value", E_USER_ERROR, __FILE__, __LINE__);
  1338.                     break;
  1339.             }
  1340.             $last_token = $token;
  1341.         }
  1342.  
  1343.         if($state != 0) {
  1344.             if($state == 1) {
  1345.                 $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);                
  1346.             } else {
  1347.                 $this->_syntax_error("missing attribute value", E_USER_ERROR, __FILE__, __LINE__);                                
  1348.             }
  1349.         }
  1350.         
  1351.         $this->_parse_vars_props($attrs);
  1352.         
  1353.         return $attrs;
  1354.     }
  1355.  
  1356.     /**
  1357.      * compile multiple variables and section properties tokens into
  1358.      * PHP code
  1359.      *
  1360.      * @param array $tokens
  1361.      */
  1362.     function _parse_vars_props(&$tokens)
  1363.     {
  1364.         foreach($tokens as $key => $val) {            
  1365.             $tokens[$key] = $this->_parse_var_props($val);
  1366.         }
  1367.     }
  1368.         
  1369.     /**
  1370.      * compile single variable and section properties token into
  1371.      * PHP code
  1372.      *
  1373.      * @param string $val
  1374.      * @param string $tag_attrs
  1375.      * @return string
  1376.      */
  1377.     function _parse_var_props($val, $tag_attrs = null)
  1378.     {                    
  1379.  
  1380.         $val = trim($val);
  1381.  
  1382.         if(preg_match('!^(' . $this->_obj_call_regexp . '|' . $this->_dvar_regexp . ')(?:' . $this->_mod_regexp . '*)$!', $val)) {
  1383.                 // $ variable or object
  1384.                 return $this->_parse_var($val);
  1385.             }            
  1386.         elseif(preg_match('!^' . $this->_db_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
  1387.                 // double quoted text
  1388.                 preg_match('!^(' . $this->_db_qstr_regexp . ')('. $this->_mod_regexp . '*)$!', $val, $match);
  1389.                 $return = $this->_expand_quoted_text($match[1]);
  1390.                 if($match[2] != '') {
  1391.                     $this->_parse_modifiers($return, $match[2]);
  1392.                 }
  1393.                 return $return;
  1394.             }            
  1395.         elseif(preg_match('!^' . $this->_si_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
  1396.                 // single quoted text
  1397.                 preg_match('!^(' . $this->_si_qstr_regexp . ')('. $this->_mod_regexp . '*)$!', $val, $match);
  1398.                 if($match[2] != '') {
  1399.                     $this->_parse_modifiers($match[1], $match[2]);
  1400.                     return $match[1];
  1401.                 }    
  1402.             }            
  1403.         elseif(preg_match('!^' . $this->_cvar_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
  1404.                 // config var
  1405.                 return $this->_parse_conf_var($val);
  1406.             }            
  1407.         elseif(preg_match('!^' . $this->_svar_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
  1408.                 // section var
  1409.                 return $this->_parse_section_prop($val);
  1410.             }
  1411.         elseif(!in_array($val, $this->_permitted_tokens) && !is_numeric($val)) {
  1412.             // literal string
  1413.             return $this->_expand_quoted_text('"' . $val .'"');
  1414.         }
  1415.         return $val;
  1416.     }
  1417.  
  1418.     /**
  1419.      * expand quoted text with embedded variables
  1420.      *
  1421.      * @param string $var_expr
  1422.      * @return string
  1423.      */
  1424.     function _expand_quoted_text($var_expr)
  1425.     {
  1426.         // if contains unescaped $, expand it
  1427.         if(preg_match_all('%(?:\`(?<!\\\\)\$' . $this->_dvar_guts_regexp . '\`)|(?:(?<!\\\\)\$\w+(\[[a-zA-Z0-9]+\])*)%', $var_expr, $_match)) {
  1428.             $_match = $_match[0];
  1429.             rsort($_match);
  1430.             reset($_match);
  1431.             foreach($_match as $_var) {
  1432.                 $var_expr = str_replace ($_var, '".' . $this->_parse_var(str_replace('`','',$_var)) . '."', $var_expr);
  1433.             }
  1434.             $_return = preg_replace('%\.""|(?<!\\\\)""\.%', '', $var_expr);
  1435.         } else {
  1436.             $_return = $var_expr;
  1437.         }
  1438.         // replace double quoted literal string with single quotes
  1439.         $_return = preg_replace('!^"([\s\w]+)"$!',"'\\1'",$_return);
  1440.         return $_return;
  1441.     }
  1442.     
  1443.     /**
  1444.      * parse variable expression into PHP code
  1445.      *
  1446.      * @param string $var_expr
  1447.      * @return string
  1448.      */
  1449.     function _parse_var($var_expr)
  1450.     {
  1451.         preg_match('!(' . $this->_obj_call_regexp . '|' . $this->_var_regexp . ')(' . $this->_mod_regexp . '*)$!', $var_expr, $match);
  1452.                         
  1453.         $var_ref = substr($match[1],1);
  1454.         $modifiers = $match[2];
  1455.                         
  1456.         if(!empty($this->default_modifiers) && !preg_match('!(^|\|)smarty:nodefaults($|\|)!',$modifiers)) {
  1457.             $_default_mod_string = implode('|',(array)$this->default_modifiers);
  1458.             $modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers;
  1459.         }
  1460.  
  1461.         // get [foo] and .foo and ->foo and (...) pieces            
  1462.         preg_match_all('!(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\w+|\.\$?\w+|\S+!', $var_ref, $match);        
  1463.                 
  1464.         $indexes = $match[0];
  1465.         $var_name = array_shift($indexes);
  1466.         
  1467.         /* Handle $smarty.* variable references as a special case. */
  1468.         if ($var_name == 'smarty') {
  1469.             /*
  1470.              * If the reference could be compiled, use the compiled output;
  1471.              * otherwise, fall back on the $smarty variable generated at
  1472.              * run-time.
  1473.              */
  1474.             if (($smarty_ref = $this->_compile_smarty_ref($indexes)) !== null) {
  1475.                 $output = $smarty_ref;
  1476.             } else {
  1477.                 $var_name = substr(array_shift($indexes), 1);
  1478.                 $output = "\$this->_smarty_vars['$var_name']";
  1479.             }
  1480.         } else {
  1481.             $output = "\$this->_tpl_vars['$var_name']";
  1482.         }
  1483.         
  1484.         foreach ($indexes as $index) {            
  1485.             if ($index{0} == '[') {
  1486.                 $index = substr($index, 1, -1);
  1487.                 if (is_numeric($index)) {
  1488.                     $output .= "[$index]";
  1489.                 } elseif ($index{0} == '$') {
  1490.                     $output .= "[\$this->_tpl_vars['" . substr($index, 1) . "']]";
  1491.                 } else {
  1492.                     $parts = explode('.', $index);
  1493.                     $section = $parts[0];
  1494.                     $section_prop = isset($parts[1]) ? $parts[1] : 'index';
  1495.                     $output .= "[\$this->_sections['$section']['$section_prop']]";
  1496.                 }
  1497.             } else if ($index{0} == '.') {
  1498.                 if ($index{1} == '$')
  1499.                     $output .= "[\$this->_tpl_vars['" . substr($index, 2) . "']]";
  1500.                 else
  1501.                     $output .= "['" . substr($index, 1) . "']";
  1502.             } else if (substr($index,0,2) == '->') {
  1503.                 if(substr($index,2,2) == '__') {
  1504.                     $this->_syntax_error('call to internal object members is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1505.                 } elseif($this->security && substr($index,2,1) == '_') {
  1506.                     $this->_syntax_error('(secure) call to private object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1507.                 }
  1508.                 $output .= $index;
  1509.             } elseif ($index{0} == '(') {
  1510.                 $index = $this->_parse_parenth_args($index);
  1511.                 $output .= $index;
  1512.             } else {
  1513.                 $output .= $index;
  1514.             }
  1515.         }
  1516.         
  1517.         $this->_parse_modifiers($output, $modifiers);
  1518.  
  1519.         return $output;
  1520.     }
  1521.  
  1522.     /**
  1523.      * parse arguments in function call parenthesis
  1524.      *
  1525.      * @param string $parenth_args
  1526.      * @return string
  1527.      */
  1528.     function _parse_parenth_args($parenth_args)
  1529.     {
  1530.         preg_match_all('!' . $this->_param_regexp . '!',$parenth_args, $match);
  1531.         $match = $match[0];
  1532.         rsort($match);
  1533.         reset($match);                        
  1534.         $orig_vals = $match;
  1535.         $this->_parse_vars_props($match);
  1536.         return str_replace($orig_vals, $match, $parenth_args);
  1537.     }    
  1538.         
  1539.     /**
  1540.      * parse configuration variable expression into PHP code
  1541.      *
  1542.      * @param string $conf_var_expr
  1543.      * @return string
  1544.      */
  1545.     function _parse_conf_var($conf_var_expr)
  1546.     {
  1547.         $parts = explode('|', $conf_var_expr, 2);
  1548.         $var_ref = $parts[0];
  1549.         $modifiers = isset($parts[1]) ? $parts[1] : '';
  1550.  
  1551.         $var_name = substr($var_ref, 1, -1);
  1552.  
  1553.         $output = "\$this->_config[0]['vars']['$var_name']";
  1554.  
  1555.         $this->_parse_modifiers($output, $modifiers);
  1556.  
  1557.         return $output;
  1558.     }
  1559.  
  1560.  
  1561.     /**
  1562.      * parse section property expression into PHP code
  1563.      *
  1564.      * @param string $section_prop_expr
  1565.      * @return string
  1566.      */
  1567.     function _parse_section_prop($section_prop_expr)
  1568.     {
  1569.         $parts = explode('|', $section_prop_expr, 2);
  1570.         $var_ref = $parts[0];
  1571.         $modifiers = isset($parts[1]) ? $parts[1] : '';
  1572.  
  1573.         preg_match('!%(\w+)\.(\w+)%!', $var_ref, $match);
  1574.         $section_name = $match[1];
  1575.         $prop_name = $match[2];
  1576.  
  1577.         $output = "\$this->_sections['$section_name']['$prop_name']";
  1578.  
  1579.         $this->_parse_modifiers($output, $modifiers);
  1580.  
  1581.         return $output;
  1582.     }
  1583.  
  1584.  
  1585.     /**
  1586.      * parse modifier chain into PHP code
  1587.      *
  1588.      * sets $output to parsed modified chain
  1589.      * @param string $output
  1590.      * @param string $modifier_string
  1591.      */
  1592.     function _parse_modifiers(&$output, $modifier_string)
  1593.     {
  1594.         preg_match_all('!\|(@?\w+)((?>:(?:'. $this->_qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $_match);
  1595.         list(, $_modifiers, $modifier_arg_strings) = $_match;
  1596.  
  1597.         for ($_i = 0, $_for_max = count($_modifiers); $_i < $_for_max; $_i++) {
  1598.             $_modifier_name = $_modifiers[$_i];
  1599.             
  1600.             if($_modifier_name == 'smarty') {
  1601.                 // skip smarty modifier
  1602.                 continue;
  1603.             }
  1604.             
  1605.             preg_match_all('!:(' . $this->_qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$_i], $_match);
  1606.             $_modifier_args = $_match[1];
  1607.  
  1608.             if ($_modifier_name{0} == '@') {
  1609.                 $_map_array = 'false';
  1610.                 $_modifier_name = substr($_modifier_name, 1);
  1611.             } else {
  1612.                 $_map_array = 'true';
  1613.             }
  1614.             
  1615.             $this->_add_plugin('modifier', $_modifier_name);
  1616.             $this->_parse_vars_props($_modifier_args);
  1617.  
  1618.             if($_modifier_name == 'default') {
  1619.                 // supress notifications of default modifier vars and args
  1620.                 if($output{0} == '$') {
  1621.                     $output = '@' . $output;
  1622.                 }
  1623.                 if(isset($_modifier_args[0]) && $_modifier_args[0]{0} == '$') {
  1624.                     $_modifier_args[0] = '@' . $_modifier_args[0];
  1625.                 }
  1626.             }
  1627.             if (count($_modifier_args) > 0)
  1628.                 $_modifier_args = ', '.implode(', ', $_modifier_args);
  1629.             else
  1630.                 $_modifier_args = '';
  1631.  
  1632.             $output = "\$this->_run_mod_handler('$_modifier_name', $_map_array, $output$_modifier_args)";
  1633.         }
  1634.     }
  1635.  
  1636.  
  1637.     /**
  1638.      * add plugin
  1639.      *
  1640.      * @param string $type
  1641.      * @param string $name
  1642.      * @param boolean? $delayed_loading
  1643.      */
  1644.     function _add_plugin($type, $name, $delayed_loading = null)
  1645.     {
  1646.         if (!isset($this->_plugin_info[$type])) {
  1647.             $this->_plugin_info[$type] = array();
  1648.         }
  1649.         if (!isset($this->_plugin_info[$type][$name])) {
  1650.             $this->_plugin_info[$type][$name] = array($this->_current_file,
  1651.                                                       $this->_current_line_no,
  1652.                                                       $delayed_loading);
  1653.         }
  1654.     }
  1655.     
  1656.  
  1657.     /**
  1658.      * Compiles references of type $smarty.foo
  1659.      *
  1660.      * @param string $indexes
  1661.      * @return string
  1662.      */
  1663.     function _compile_smarty_ref(&$indexes)
  1664.     {
  1665.         /* Extract the reference name. */
  1666.         $_ref = substr($indexes[0], 1);
  1667.  
  1668.         foreach($indexes as $_index) {        
  1669.             if ($_index{0} != '.') {
  1670.                 $this->_syntax_error('$smarty' . implode('', array_slice($indexes, 0, 2)) . ' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
  1671.             }
  1672.         }
  1673.         
  1674.         switch ($_ref) {
  1675.             case 'now':
  1676.                 $compiled_ref = 'time()';
  1677.                 $_max_index = 1;
  1678.                 break;
  1679.  
  1680.             case 'foreach':
  1681.             case 'section':
  1682.                 array_shift($indexes);
  1683.                 $_var = $this->_parse_var_props(substr($indexes[0], 1));
  1684.                 if ($_ref == 'foreach')
  1685.                     $compiled_ref = "\$this->_foreach[$_var]";
  1686.                 else
  1687.                     $compiled_ref = "\$this->_sections[$_var]";
  1688.                 break;
  1689.  
  1690.             case 'get':
  1691.                 $compiled_ref = "\$GLOBALS['HTTP_GET_VARS']";
  1692.                 break;
  1693.  
  1694.             case 'post':
  1695.                 $compiled_ref = "\$GLOBALS['HTTP_POST_VARS']";
  1696.                 break;
  1697.  
  1698.             case 'cookies':
  1699.                 $compiled_ref = "\$GLOBALS['HTTP_COOKIE_VARS']";
  1700.                 break;
  1701.  
  1702.             case 'env':
  1703.                 $compiled_ref = "\$GLOBALS['HTTP_ENV_VARS']";
  1704.                 break;
  1705.  
  1706.             case 'server':
  1707.                 $compiled_ref = "\$GLOBALS['HTTP_SERVER_VARS']";
  1708.                 break;
  1709.  
  1710.             case 'session':
  1711.                 $compiled_ref = "\$GLOBALS['HTTP_SESSION_VARS']";
  1712.                 break;
  1713.  
  1714.             /*
  1715.              * These cases are handled either at run-time or elsewhere in the
  1716.              * compiler.
  1717.              */
  1718.             case 'request':
  1719.                 $this->_init_smarty_vars = true;
  1720.                 return null;
  1721.  
  1722.             case 'capture':
  1723.                 return null;
  1724.  
  1725.             case 'template':
  1726.                 $compiled_ref = "'$this->_current_file'";
  1727.                 $_max_index = 1;
  1728.                 break;
  1729.                 
  1730.             case 'version':
  1731.                 $compiled_ref = "'$this->_version'";
  1732.                 $_max_index = 1;
  1733.                 break;
  1734.  
  1735.             case 'const':
  1736.                 array_shift($indexes);
  1737.                 $_val = $this->_parse_var_props(substr($indexes[0],1));
  1738.                 $compiled_ref = '@constant(' . $_val . ')';
  1739.                 $_max_index = 1;
  1740.                 break;
  1741.  
  1742.             case 'config':
  1743.                 $compiled_ref = "\$this->_config[0]['vars']";
  1744.                 $_max_index = 2;
  1745.                 break;
  1746.  
  1747.             default:
  1748.                 $this->_syntax_error('$smarty.' . $_ref . ' is an unknown reference', E_USER_ERROR, __FILE__, __LINE__);
  1749.                 break;
  1750.         }
  1751.  
  1752.         if (isset($_max_index) && count($indexes) > $_max_index) {
  1753.             $this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
  1754.         }
  1755.  
  1756.         array_shift($indexes);
  1757.         return $compiled_ref;
  1758.     }
  1759.  
  1760.     /**
  1761.      * compiles call to plugin of type $type with name $name
  1762.      * returns a string containing the function-name or method call
  1763.      * without the paramter-list that would have follow to make the
  1764.      * call valid php-syntax
  1765.      *
  1766.      * @param string $type
  1767.      * @param string $name
  1768.      * @return string
  1769.      */
  1770.     function _compile_plugin_call($type, $name) {
  1771.         if (isset($this->_plugins[$type][$name])) {
  1772.             /* plugin loaded */
  1773.             if (is_array($this->_plugins[$type][$name][0])) {
  1774.                 /* method callback */
  1775.                 return "\$this->_plugins['$type']['$name'][0][0]->".$this->_plugins[$type][$name][0][1];
  1776.                 
  1777.             } else {
  1778.                 /* function callback */
  1779.                 return $this->_plugins[$type][$name][0];
  1780.                 
  1781.             }
  1782.         } else {
  1783.             /* plugin not loaded -> auto-loadable-plugin */
  1784.             return 'smarty_'.$type.'_'.$name;
  1785.             
  1786.         }
  1787.     }
  1788.  
  1789.     /**
  1790.      * load pre- and post-filters
  1791.      */
  1792.     function _load_filters()
  1793.     {
  1794.         if (count($this->_plugins['prefilter']) > 0) {
  1795.             foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
  1796.                 if ($prefilter === false) {
  1797.                     unset($this->_plugins['prefilter'][$filter_name]);
  1798.                     $this->_load_plugins(array(array('prefilter', $filter_name, null, null, false)));
  1799.                 }
  1800.             }
  1801.         }
  1802.         if (count($this->_plugins['postfilter']) > 0) {
  1803.             foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
  1804.                 if ($postfilter === false) {
  1805.                     unset($this->_plugins['postfilter'][$filter_name]);
  1806.                     $this->_load_plugins(array(array('postfilter', $filter_name, null, null, false)));
  1807.                 }
  1808.             }
  1809.         }
  1810.     }
  1811.  
  1812.  
  1813.     /**
  1814.      * display Smarty syntax error
  1815.      *
  1816.      * @param string $error_msg
  1817.      * @param integer $error_type
  1818.      * @param string $file
  1819.      * @param integer $line
  1820.      */
  1821.     function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null)
  1822.     {
  1823.         if(isset($file) && isset($line)) {
  1824.             $info = ' ('.basename($file).", line $line)";
  1825.         } else {
  1826.             $info = null;
  1827.         }
  1828.         trigger_error('Smarty: [in ' . $this->_current_file . ' line ' .
  1829.                       $this->_current_line_no . "]: syntax error: $error_msg$info", $error_type);
  1830.     }
  1831. }
  1832.  
  1833. /**
  1834.  * compare to values by their string length
  1835.  *
  1836.  * @access private
  1837.  * @param string $a
  1838.  * @param string $b
  1839.  * @return 0|-1|1
  1840.  */
  1841. function _smarty_sort_length($a, $b)
  1842. {
  1843.     if($a == $b)
  1844.         return 0;
  1845.  
  1846.     if(strlen($a) == strlen($b))
  1847.         return ($a > $b) ? -1 : 1;
  1848.  
  1849.     return (strlen($a) > strlen($b)) ? -1 : 1;
  1850. }
  1851.  
  1852.  
  1853. /* vim: set et: */
  1854.  
  1855. ?>
  1856.