* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
- * You may contact the authors of Smarty by e-mail at:
- * monte@ispi.net
- * andrei@php.net
- *
- * Or, write to:
- * Monte Ohrt
- * Director of Technology, ispi
- * 237 S. 70th suite 220
- * Lincoln, NE 68510
- *
- * The latest version of Smarty can be obtained from:
- * http://smarty.php.net/
- *
* @link http://smarty.php.net/
- * @author Monte Ohrt <monte@ispi.net>
+ * @author Monte Ohrt <monte at ohrt dot com>
* @author Andrei Zmievski <andrei@php.net>
- * @version 2.6.0
- * @copyright 2001-2003 ispi of Lincoln, Inc.
+ * @version 2.6.18
+ * @copyright 2001-2005 New Digital Group, Inc.
* @package Smarty
*/
-/* $Id: Smarty_Compiler.class.php,v 1.1.1.1 2004/01/13 16:02:46 gohr Exp $ */
+/* $Id: Smarty_Compiler.class.php,v 1.395 2007/03/06 10:40:06 messju Exp $ */
/**
* Template compiling class
/**#@+
* @access private
*/
- var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part
- var $_foreachelse_stack = array(); // keeps track of whether foreach had 'else' part
- var $_literal_blocks = array(); // keeps literal template blocks
- var $_php_blocks = array(); // keeps php code blocks
+ var $_folded_blocks = array(); // keeps folded template blocks
var $_current_file = null; // the current template being compiled
var $_current_line_no = 1; // line number for error messages
var $_capture_stack = array(); // keeps track of nested capture buffers
var $_si_qstr_regexp = null;
var $_qstr_regexp = null;
var $_func_regexp = null;
+ var $_reg_obj_regexp = null;
var $_var_bracket_regexp = null;
+ var $_num_const_regexp = null;
var $_dvar_guts_regexp = null;
var $_dvar_regexp = null;
var $_cvar_regexp = null;
// [$bar]
$this->_var_bracket_regexp = '\[\$?[\w\.]+\]';
+ // matches numerical constants
+ // 30
+ // -12
+ // 13.22
+ $this->_num_const_regexp = '(?:\-?\d+(?:\.\d+)?)';
+
// matches $ vars (not objects):
// $foo
// $foo.bar
// $foo[$bar]
// $foo[5][blah]
// $foo[5].bar[$foobar][4]
- $this->_dvar_math_regexp = '[\+\-\*\/\%]';
+ $this->_dvar_math_regexp = '(?:[\+\*\/\%]|(?:-(?!>)))';
$this->_dvar_math_var_regexp = '[\$\w\.\+\-\*\/\%\d\>\[\]]';
- $this->_dvar_num_var_regexp = '\-?\d+(?:\.\d+)?' . $this->_dvar_math_var_regexp;
$this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp
- . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:\-?\d+(?:\.\d+)?|' . $this->_dvar_math_var_regexp . ')*)?';
+ . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?';
$this->_dvar_regexp = '\$' . $this->_dvar_guts_regexp;
// matches config vars:
// "text"
$this->_var_regexp = '(?:' . $this->_avar_regexp . '|' . $this->_qstr_regexp . ')';
- // matches valid object call (no objects allowed in parameters):
+ // matches valid object call (one level of object nesting allowed in parameters):
// $foo->bar
// $foo->bar()
// $foo->bar("text")
// $foo->bar($foo, "foo")
// $foo->bar->foo()
// $foo->bar->foo->bar()
+ // $foo->bar($foo->bar)
+ // $foo->bar($foo->bar())
+ // $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar))
$this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')';
- $this->_obj_params_regexp = '\((?:\w+|'
- . $this->_var_regexp . '(?:\s*,\s*(?:(?:\w+|'
- . $this->_var_regexp . ')))*)?\)';
+ $this->_obj_restricted_param_regexp = '(?:'
+ . '(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')(?:' . $this->_obj_ext_regexp . '(?:\((?:(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')'
+ . '(?:\s*,\s*(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . '))*)?\))?)*)';
+ $this->_obj_single_param_regexp = '(?:\w+|' . $this->_obj_restricted_param_regexp . '(?:\s*,\s*(?:(?:\w+|'
+ . $this->_var_regexp . $this->_obj_restricted_param_regexp . ')))*)';
+ $this->_obj_params_regexp = '\((?:' . $this->_obj_single_param_regexp
+ . '(?:\s*,\s*' . $this->_obj_single_param_regexp . ')*)?\)';
$this->_obj_start_regexp = '(?:' . $this->_dvar_regexp . '(?:' . $this->_obj_ext_regexp . ')+)';
- $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . ')?)';
-
+ $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';
+
// matches valid modifier syntax:
// |foo
// |@foo
// |foo:"bar":$foobar
// |foo|bar
// |foo:$foo->bar
- $this->_mod_regexp = '(?:\|@?\w+(?::(?>-?\w+|'
+ $this->_mod_regexp = '(?:\|@?\w+(?::(?:\w+|' . $this->_num_const_regexp . '|'
. $this->_obj_call_regexp . '|' . $this->_avar_regexp . '|' . $this->_qstr_regexp .'))*)';
// matches valid function name:
// "text"|bar
// $foo->bar
$this->_param_regexp = '(?:\s*(?:' . $this->_obj_call_regexp . '|'
- . $this->_var_regexp . '|\w+)(?>' . $this->_mod_regexp . '*)\s*)';
+ . $this->_var_regexp . '|' . $this->_num_const_regexp . '|\w+)(?>' . $this->_mod_regexp . '*)\s*)';
// matches valid parenthesised function parameters:
//
$this->_current_file = $resource_name;
$this->_current_line_no = 1;
- $ldq = preg_quote($this->left_delimiter, '!');
- $rdq = preg_quote($this->right_delimiter, '!');
+ $ldq = preg_quote($this->left_delimiter, '~');
+ $rdq = preg_quote($this->right_delimiter, '~');
// run template source through prefilter functions
if (count($this->_plugins['prefilter']) > 0) {
}
}
- /* Annihilate the comments. */
- $source_content = preg_replace("!({$ldq})\*(.*?)\*({$rdq})!se",
- "'\\1*'.str_repeat(\"\n\", substr_count('\\2', \"\n\")) .'*\\3'",
- $source_content);
+ /* fetch all special blocks */
+ $search = "~{$ldq}\*(.*?)\*{$rdq}|{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}|{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}~s";
- /* Pull out the literal blocks. */
- preg_match_all("!{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}!s", $source_content, $_match);
- $this->_literal_blocks = $_match[1];
- $source_content = preg_replace("!{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}!s",
- $this->_quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $source_content);
+ preg_match_all($search, $source_content, $match, PREG_SET_ORDER);
+ $this->_folded_blocks = $match;
+ reset($this->_folded_blocks);
- /* Pull out the php code blocks. */
- preg_match_all("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s", $source_content, $_match);
- $this->_php_blocks = $_match[1];
- $source_content = preg_replace("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s",
- $this->_quote_replace($this->left_delimiter.'php'.$this->right_delimiter), $source_content);
+ /* replace special blocks by "{php}" */
+ $source_content = preg_replace($search.'e', "'"
+ . $this->_quote_replace($this->left_delimiter) . 'php'
+ . "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
+ . $this->_quote_replace($this->right_delimiter)
+ . "'"
+ , $source_content);
/* Gather all template tags. */
- preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $source_content, $_match);
+ preg_match_all("~{$ldq}\s*(.*?)\s*{$rdq}~s", $source_content, $_match);
$template_tags = $_match[1];
/* Split content by template tags to obtain non-template content. */
- $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $source_content);
+ $text_blocks = preg_split("~{$ldq}.*?{$rdq}~s", $source_content);
/* loop through text blocks */
for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) {
/* match anything resembling php tags */
- if (preg_match_all('!(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?php[\"\']?)!is', $text_blocks[$curr_tb], $sp_match)) {
+ if (preg_match_all('~(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?\s*php\s*[\"\']?)~is', $text_blocks[$curr_tb], $sp_match)) {
/* replace tags with placeholders to prevent recursive replacements */
$sp_match[1] = array_unique($sp_match[1]);
usort($sp_match[1], '_smarty_sort_length');
$text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '', $text_blocks[$curr_tb]);
} else {
/* SMARTY_PHP_ALLOW, but echo non php starting tags */
- $sp_match[1][$curr_sp] = preg_replace('%(<\?(?!php|=|$))%i', '<?php echo \'\\1\'?>'."\n", $sp_match[1][$curr_sp]);
+ $sp_match[1][$curr_sp] = preg_replace('~(<\?(?!php|=|$))~i', '<?php echo \'\\1\'?>'."\n", $sp_match[1][$curr_sp]);
$text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', $sp_match[1][$curr_sp], $text_blocks[$curr_tb]);
}
}
}
}
-
+
/* Compile the template tags into PHP code. */
$compiled_tags = array();
for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
$compiled_tags[] = $this->_compile_tag($template_tags[$i]);
$this->_current_line_no += substr_count($template_tags[$i], "\n");
}
+ if (count($this->_tag_stack)>0) {
+ list($_open_tag, $_line_no) = end($this->_tag_stack);
+ $this->_syntax_error("unclosed tag \{$_open_tag} (opened line $_line_no).", E_USER_ERROR, __FILE__, __LINE__);
+ return;
+ }
+ /* Reformat $text_blocks between 'strip' and '/strip' tags,
+ removing spaces, tabs and newlines. */
+ $strip = false;
+ for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
+ if ($compiled_tags[$i] == '{strip}') {
+ $compiled_tags[$i] = '';
+ $strip = true;
+ /* remove leading whitespaces */
+ $text_blocks[$i + 1] = ltrim($text_blocks[$i + 1]);
+ }
+ if ($strip) {
+ /* strip all $text_blocks before the next '/strip' */
+ for ($j = $i + 1; $j < $for_max; $j++) {
+ /* remove leading and trailing whitespaces of each line */
+ $text_blocks[$j] = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $text_blocks[$j]);
+ if ($compiled_tags[$j] == '{/strip}') {
+ /* remove trailing whitespaces from the last text_block */
+ $text_blocks[$j] = rtrim($text_blocks[$j]);
+ }
+ $text_blocks[$j] = "<?php echo '" . strtr($text_blocks[$j], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>";
+ if ($compiled_tags[$j] == '{/strip}') {
+ $compiled_tags[$j] = "\n"; /* slurped by php, but necessary
+ if a newline is following the closing strip-tag */
+ $strip = false;
+ $i = $j;
+ break;
+ }
+ }
+ }
+ }
$compiled_content = '';
-
+
+ $tag_guard = '%%%SMARTYOTG' . md5(uniqid(rand(), true)) . '%%%';
+
/* Interleave the compiled contents and text blocks to get the final result. */
for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
if ($compiled_tags[$i] == '') {
// tag result empty, remove first newline from following text block
- $text_blocks[$i+1] = preg_replace('!^(\r\n|\r|\n)!', '', $text_blocks[$i+1]);
+ $text_blocks[$i+1] = preg_replace('~^(\r\n|\r|\n)~', '', $text_blocks[$i+1]);
}
- $compiled_content .= $text_blocks[$i].$compiled_tags[$i];
+ // replace legit PHP tags with placeholder
+ $text_blocks[$i] = str_replace('<?', $tag_guard, $text_blocks[$i]);
+ $compiled_tags[$i] = str_replace('<?', $tag_guard, $compiled_tags[$i]);
+
+ $compiled_content .= $text_blocks[$i] . $compiled_tags[$i];
}
- $compiled_content .= $text_blocks[$i];
+ $compiled_content .= str_replace('<?', $tag_guard, $text_blocks[$i]);
- /* Reformat data between 'strip' and '/strip' tags, removing spaces, tabs and newlines. */
- if (preg_match_all("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $compiled_content, $_match)) {
- $strip_tags = $_match[0];
- $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags);
- $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified);
- for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++)
- $compiled_content = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
- $this->_quote_replace($strip_tags_modified[$i]),
- $compiled_content, 1);
- }
+ // escape php tags created by interleaving
+ $compiled_content = str_replace('<?', "<?php echo '<?' ?>\n", $compiled_content);
+ $compiled_content = preg_replace("~(?<!')language\s*=\s*[\"\']?\s*php\s*[\"\']?~", "<?php echo 'language=php' ?>\n", $compiled_content);
+ // recover legit tags
+ $compiled_content = str_replace($tag_guard, '<?', $compiled_content);
+
// remove \n from the end of the file, if any
- if (($_len=strlen($compiled_content)) && ($compiled_content{$_len - 1} == "\n" )) {
+ if (strlen($compiled_content) && (substr($compiled_content, -1) == "\n") ) {
$compiled_content = substr($compiled_content, 0, -1);
}
$compiled_content = "<?php \$this->_cache_serials['".$this->_cache_include."'] = '".$this->_cache_serial."'; ?>" . $compiled_content;
}
- // remove unnecessary close/open tags
- $compiled_content = preg_replace('!\?>\n?<\?php!', '', $compiled_content);
-
// run compiled template through postfilter functions
if (count($this->_plugins['postfilter']) > 0) {
foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
$_plugins_params = "array('plugins' => array(";
foreach ($this->_plugin_info as $plugin_type => $plugins) {
foreach ($plugins as $plugin_name => $plugin_info) {
- $_plugins_params .= "array('$plugin_type', '$plugin_name', '$plugin_info[0]', $plugin_info[1], ";
+ $_plugins_params .= "array('$plugin_type', '$plugin_name', '" . strtr($plugin_info[0], array("'" => "\\'", "\\" => "\\\\")) . "', $plugin_info[1], ";
$_plugins_params .= $plugin_info[2] ? 'true),' : 'false),';
}
}
$_plugins_params .= '))';
- $plugins_code = "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php');\nsmarty_core_load_plugins($_plugins_params, \$this); ?>\n";
+ $plugins_code = "<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');\nsmarty_core_load_plugins($_plugins_params, \$this); ?>\n";
$template_header .= $plugins_code;
$this->_plugin_info = array();
$this->_plugins_code = $plugins_code;
}
if ($this->_init_smarty_vars) {
- $template_header .= "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.assign_smarty_interface.php');\nsmarty_core_assign_smarty_interface(null, \$this); ?>\n";
+ $template_header .= "<?php require_once(SMARTY_CORE_DIR . 'core.assign_smarty_interface.php');\nsmarty_core_assign_smarty_interface(null, \$this); ?>\n";
$this->_init_smarty_vars = false;
}
$compiled_content = $template_header . $compiled_content;
-
return true;
}
function _compile_tag($template_tag)
{
/* Matched comment. */
- if ($template_tag{0} == '*' && $template_tag{strlen($template_tag) - 1} == '*')
+ if (substr($template_tag, 0, 1) == '*' && substr($template_tag, -1) == '*')
return '';
-
+
/* Split tag into two three parts: command, command modifiers and the arguments. */
- if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp
+ if(! preg_match('~^(?:(' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp
. '|\/?' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
(?:\s+(.*))?$
- /xs', $template_tag, $match)) {
+ ~xs', $template_tag, $match)) {
$this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
}
-
+
$tag_command = $match[1];
$tag_modifier = isset($match[2]) ? $match[2] : null;
$tag_args = isset($match[3]) ? $match[3] : null;
- if (preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$!', $tag_command)) {
+ if (preg_match('~^' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$~', $tag_command)) {
/* tag name is a variable or object */
- $_return = $this->_parse_var_props($tag_command . $tag_modifier, $this->_parse_attrs($tag_args));
- if(isset($_tag_attrs['assign'])) {
- return "<?php \$this->assign('" . $this->_dequote($_tag_attrs['assign']) . "', $_return ); ?>\n";
- } else {
- return "<?php echo $_return; ?>" . $this->_additional_newline;
- }
+ $_return = $this->_parse_var_props($tag_command . $tag_modifier);
+ return "<?php echo $_return; ?>" . $this->_additional_newline;
}
/* If the tag name is a registered object, we process it. */
- if (preg_match('!^\/?' . $this->_reg_obj_regexp . '$!', $tag_command)) {
+ if (preg_match('~^\/?' . $this->_reg_obj_regexp . '$~', $tag_command)) {
return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
}
return $this->_compile_include_php_tag($tag_args);
case 'if':
+ $this->_push_tag('if');
return $this->_compile_if_tag($tag_args);
case 'else':
+ list($_open_tag) = end($this->_tag_stack);
+ if ($_open_tag != 'if' && $_open_tag != 'elseif')
+ $this->_syntax_error('unexpected {else}', E_USER_ERROR, __FILE__, __LINE__);
+ else
+ $this->_push_tag('else');
return '<?php else: ?>';
case 'elseif':
+ list($_open_tag) = end($this->_tag_stack);
+ if ($_open_tag != 'if' && $_open_tag != 'elseif')
+ $this->_syntax_error('unexpected {elseif}', E_USER_ERROR, __FILE__, __LINE__);
+ if ($_open_tag == 'if')
+ $this->_push_tag('elseif');
return $this->_compile_if_tag($tag_args, true);
case '/if':
+ $this->_pop_tag('if');
return '<?php endif; ?>';
case 'capture':
return $this->right_delimiter;
case 'section':
- array_push($this->_sectionelse_stack, false);
+ $this->_push_tag('section');
return $this->_compile_section_start($tag_args);
case 'sectionelse':
- $this->_sectionelse_stack[count($this->_sectionelse_stack)-1] = true;
+ $this->_push_tag('sectionelse');
return "<?php endfor; else: ?>";
+ break;
case '/section':
- if (array_pop($this->_sectionelse_stack))
+ $_open_tag = $this->_pop_tag('section');
+ if ($_open_tag == 'sectionelse')
return "<?php endif; ?>";
else
return "<?php endfor; endif; ?>";
case 'foreach':
- array_push($this->_foreachelse_stack, false);
+ $this->_push_tag('foreach');
return $this->_compile_foreach_start($tag_args);
break;
case 'foreachelse':
- $this->_foreachelse_stack[count($this->_foreachelse_stack)-1] = true;
- return "<?php endforeach; unset(\$_from); else: ?>";
+ $this->_push_tag('foreachelse');
+ return "<?php endforeach; else: ?>";
case '/foreach':
- if (array_pop($this->_foreachelse_stack))
- return "<?php endif; ?>";
+ $_open_tag = $this->_pop_tag('foreach');
+ if ($_open_tag == 'foreachelse')
+ return "<?php endif; unset(\$_from); ?>";
else
- return "<?php endforeach; unset(\$_from); endif; ?>";
+ return "<?php endforeach; endif; unset(\$_from); ?>";
+ break;
case 'strip':
case '/strip':
- if ($tag_command{0}=='/') {
+ if (substr($tag_command, 0, 1)=='/') {
+ $this->_pop_tag('strip');
if (--$this->_strip_depth==0) { /* outermost closing {/strip} */
$this->_additional_newline = "\n";
- return $this->left_delimiter.$tag_command.$this->right_delimiter;
+ return '{' . $tag_command . '}';
}
} else {
+ $this->_push_tag('strip');
if ($this->_strip_depth++==0) { /* outermost opening {strip} */
$this->_additional_newline = "";
- return $this->left_delimiter.$tag_command.$this->right_delimiter;
+ return '{' . $tag_command . '}';
}
}
return '';
- case 'literal':
- list (,$literal_block) = each($this->_literal_blocks);
- $this->_current_line_no += substr_count($literal_block, "\n");
- return "<?php echo '".str_replace("'", "\'", str_replace("\\", "\\\\", $literal_block))."'; ?>" . $this->_additional_newline;
-
case 'php':
- if ($this->security && !$this->security_settings['PHP_TAGS']) {
- $this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING, __FILE__, __LINE__);
- return;
+ /* handle folded tags replaced by {php} */
+ list(, $block) = each($this->_folded_blocks);
+ $this->_current_line_no += substr_count($block[0], "\n");
+ /* the number of matched elements in the regexp in _compile_file()
+ determins the type of folded tag that was found */
+ switch (count($block)) {
+ case 2: /* comment */
+ return '';
+
+ case 3: /* literal */
+ return "<?php echo '" . strtr($block[2], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>" . $this->_additional_newline;
+
+ case 4: /* php */
+ if ($this->security && !$this->security_settings['PHP_TAGS']) {
+ $this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING, __FILE__, __LINE__);
+ return;
+ }
+ return '<?php ' . $block[3] .' ?>';
}
- list (,$php_block) = each($this->_php_blocks);
- $this->_current_line_no += substr_count($php_block, "\n");
- return '<?php '.$php_block.' ?>';
+ break;
case 'insert':
return $this->_compile_insert_tag($tag_args);
return $output;
} else if ($this->_compile_block_tag($tag_command, $tag_args, $tag_modifier, $output)) {
return $output;
+ } else if ($this->_compile_custom_tag($tag_command, $tag_args, $tag_modifier, $output)) {
+ return $output;
} else {
- return $this->_compile_custom_tag($tag_command, $tag_args, $tag_modifier);
+ $this->_syntax_error("unrecognized tag '$tag_command'", E_USER_ERROR, __FILE__, __LINE__);
}
+
}
}
*/
function _compile_block_tag($tag_command, $tag_args, $tag_modifier, &$output)
{
- if ($tag_command{0} == '/') {
+ if (substr($tag_command, 0, 1) == '/') {
$start_tag = false;
$tag_command = substr($tag_command, 1);
} else
*/
$this->_add_plugin('block', $tag_command);
+ if ($start_tag)
+ $this->_push_tag($tag_command);
+ else
+ $this->_pop_tag($tag_command);
+
if ($start_tag) {
$output = '<?php ' . $this->_push_cacheable_state('block', $tag_command);
$attrs = $this->_parse_attrs($tag_args);
- $arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs='');
- $output .= "$_cache_attrs\$_params = \$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); ';
- $output .= $this->_compile_plugin_call('block', $tag_command).'($_params[1], null, $this, $_block_repeat=true); unset($_params);';
+ $_cache_attrs='';
+ $arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs);
+ $output .= "$_cache_attrs\$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); ';
+ $output .= '$_block_repeat=true;' . $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat);';
$output .= 'while ($_block_repeat) { ob_start(); ?>';
} else {
- $output = '<?php $this->_block_content = ob_get_contents(); ob_end_clean(); ';
- $_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)';
+ $output = '<?php $_block_content = ob_get_contents(); ob_end_clean(); ';
+ $_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat)';
if ($tag_modifier != '') {
$this->_parse_modifiers($_out_tag_text, $tag_modifier);
}
- $output .= 'echo '.$_out_tag_text.'; } ';
+ $output .= '$_block_repeat=false;echo ' . $_out_tag_text . '; } ';
$output .= " array_pop(\$this->_tag_stack); " . $this->_pop_cacheable_state('block', $tag_command) . '?>';
}
* @param string $tag_modifier
* @return string
*/
- function _compile_custom_tag($tag_command, $tag_args, $tag_modifier)
+ function _compile_custom_tag($tag_command, $tag_args, $tag_modifier, &$output)
{
+ $found = false;
+ $have_function = true;
+
+ /*
+ * First we check if the custom function has already been registered
+ * or loaded from a plugin file.
+ */
+ if (isset($this->_plugins['function'][$tag_command])) {
+ $found = true;
+ $plugin_func = $this->_plugins['function'][$tag_command][0];
+ if (!is_callable($plugin_func)) {
+ $message = "custom function '$tag_command' is not implemented";
+ $have_function = false;
+ }
+ }
+ /*
+ * Otherwise we need to load plugin file and look for the function
+ * inside it.
+ */
+ else if ($plugin_file = $this->_get_plugin_filepath('function', $tag_command)) {
+ $found = true;
+
+ include_once $plugin_file;
+
+ $plugin_func = 'smarty_function_' . $tag_command;
+ if (!function_exists($plugin_func)) {
+ $message = "plugin function $plugin_func() not found in $plugin_file\n";
+ $have_function = false;
+ } else {
+ $this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, true);
+
+ }
+ }
+
+ if (!$found) {
+ return false;
+ } else if (!$have_function) {
+ $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
+ return true;
+ }
+
+ /* declare plugin to be loaded on display of the template that
+ we compile right now */
$this->_add_plugin('function', $tag_command);
$_cacheable_state = $this->_push_cacheable_state('function', $tag_command);
$attrs = $this->_parse_attrs($tag_args);
- $arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs='');
+ $_cache_attrs = '';
+ $arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs);
- $_return = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)";
+ $output = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)";
if($tag_modifier != '') {
- $this->_parse_modifiers($_return, $tag_modifier);
+ $this->_parse_modifiers($output, $tag_modifier);
}
- if($_return != '') {
- $_return = '<?php ' . $_cacheable_state . $_cache_attrs . 'echo ' . $_return . ';'
+ if($output != '') {
+ $output = '<?php ' . $_cacheable_state . $_cache_attrs . 'echo ' . $output . ';'
. $this->_pop_cacheable_state('function', $tag_command) . "?>" . $this->_additional_newline;
}
- return $_return;
+ return true;
}
/**
*/
function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
{
- if ($tag_command{0} == '/') {
+ if (substr($tag_command, 0, 1) == '/') {
$start_tag = false;
$tag_command = substr($tag_command, 1);
} else {
$postfix = '';
$newline = '';
if(!is_object($this->_reg_objects[$object][0])) {
- $this->_trigger_fatal_error("registered '$object' is not an object");
+ $this->_trigger_fatal_error("registered '$object' is not an object" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
} elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
- $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'");
+ $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'", $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
} elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
// method
if(in_array($obj_comp, $this->_reg_objects[$object][3])) {
// block method
if ($start_tag) {
$prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); ";
- $prefix .= "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat=true); ";
+ $prefix .= "\$_block_repeat=true; \$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat); ";
$prefix .= "while (\$_block_repeat) { ob_start();";
$return = null;
$postfix = '';
- } else {
- $prefix = "\$this->_obj_block_content = ob_get_contents(); ob_end_clean(); ";
- $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_obj_block_content, \$this, \$_block_repeat=false)";
+ } else {
+ $prefix = "\$_obj_block_content = ob_get_contents(); ob_end_clean(); \$_block_repeat=false;";
+ $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$_obj_block_content, \$this, \$_block_repeat)";
$postfix = "} array_pop(\$this->_tag_stack);";
}
} else {
$name = $this->_dequote($attrs['name']);
if (empty($name)) {
- $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
+ return $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
+ }
+
+ if (!preg_match('~^\w+$~', $name)) {
+ return $this->_syntax_error("'insert: 'name' must be an insert function name", E_USER_ERROR, __FILE__, __LINE__);
}
if (!empty($attrs['script'])) {
$_params = "array('args' => array(".implode(', ', (array)$arg_list)."))";
- return "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline;
+ return "<?php require_once(SMARTY_CORE_DIR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline;
}
/**
$assign_var = (empty($attrs['assign'])) ? '' : $this->_dequote($attrs['assign']);
$once_var = (empty($attrs['once']) || $attrs['once']=='false') ? 'false' : 'true';
+ $arg_list = array();
foreach($attrs as $arg_name => $arg_value) {
if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {
if(is_bool($arg_value))
}
}
- $_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."))";
+ $_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', $arg_list)."))";
- return "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;
+ return "<?php require_once(SMARTY_CORE_DIR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;
}
$this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
}
- $output .= "if (isset(\$this->_sections[$section_name])) unset(\$this->_sections[$section_name]);\n";
+ $output .= "unset(\$this->_sections[$section_name]);\n";
$section_props = "\$this->_sections[$section_name]";
foreach ($attrs as $attr_name => $attr_value) {
$arg_list = array();
if (empty($attrs['from'])) {
- $this->_syntax_error("missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
+ return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
+ $from = $attrs['from'];
if (empty($attrs['item'])) {
- $this->_syntax_error("missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
+ return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
-
- $from = $attrs['from'];
$item = $this->_dequote($attrs['item']);
- if (isset($attrs['name']))
- $name = $attrs['name'];
-
- $output = '<?php ';
- if (isset($name)) {
- $output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n";
- $foreach_props = "\$this->_foreach[$name]";
+ if (!preg_match('~^\w+$~', $item)) {
+ return $this->_syntax_error("'foreach: 'item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
}
- $key_part = '';
-
- foreach ($attrs as $attr_name => $attr_value) {
- switch ($attr_name) {
- case 'key':
- $key = $this->_dequote($attrs['key']);
- $key_part = "\$this->_tpl_vars['$key'] => ";
- break;
-
- case 'name':
- $output .= "{$foreach_props}['$attr_name'] = $attr_value;\n";
- break;
+ if (isset($attrs['key'])) {
+ $key = $this->_dequote($attrs['key']);
+ if (!preg_match('~^\w+$~', $key)) {
+ return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
}
+ $key_part = "\$this->_tpl_vars['$key'] => ";
+ } else {
+ $key = null;
+ $key_part = '';
}
+ if (isset($attrs['name'])) {
+ $name = $attrs['name'];
+ } else {
+ $name = null;
+ }
+
+ $output = '<?php ';
+ $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
if (isset($name)) {
- $output .= "{$foreach_props}['total'] = count(\$_from = (array)$from);\n";
- $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n";
- $output .= "if ({$foreach_props}['show']):\n";
- $output .= "{$foreach_props}['iteration'] = 0;\n";
+ $foreach_props = "\$this->_foreach[$name]";
+ $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
+ $output .= "if ({$foreach_props}['total'] > 0):\n";
$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
$output .= " {$foreach_props}['iteration']++;\n";
- $output .= " {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n";
- $output .= " {$foreach_props}['last'] = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n";
} else {
- $output .= "if (count(\$_from = (array)$from)):\n";
+ $output .= "if (count(\$_from)):\n";
$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
}
$output .= '?>';
{
/* Tokenize args for 'if' tag. */
- preg_match_all('/(?>
+ preg_match_all('~(?>
' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)? | # valid object call
' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)? | # var or quoted string
\-?0[xX][0-9a-fA-F]+|\-?\d+(?:\.\d+)?|\.\d+|!==|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\||\%|\+|\-|\/|\*|\@ | # valid non-word token
\b\w+\b | # valid word token
\S+ # anything else
- )/x', $tag_args, $match);
+ )~x', $tag_args, $match);
$tokens = $match[0];
+ if(empty($tokens)) {
+ $_error_msg = $elseif ? "'elseif'" : "'if'";
+ $_error_msg .= ' statement requires arguments';
+ $this->_syntax_error($_error_msg, E_USER_ERROR, __FILE__, __LINE__);
+ }
+
+
// make sure we have balanced parenthesis
$token_count = array_count_values($tokens);
if(isset($token_count['(']) && $token_count['('] != $token_count[')']) {
break;
default:
- if(preg_match('!^' . $this->_func_regexp . '$!', $token) ) {
+ if(preg_match('~^' . $this->_func_regexp . '$~', $token) ) {
// function call
if($this->security &&
!in_array($token, $this->security_settings['IF_FUNCS'])) {
$this->_syntax_error("(secure mode) '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
}
- } elseif(preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
+ } elseif(preg_match('~^' . $this->_var_regexp . '$~', $token) && (strpos('+-*/^%&|', substr($token, -1)) === false) && isset($tokens[$i+1]) && $tokens[$i+1] == '(') {
+ // variable function call
+ $this->_syntax_error("variable function call '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
+ } elseif(preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$~', $token)) {
// object or variable
$token = $this->_parse_var_props($token);
} elseif(is_numeric($token)) {
switch ($expr_type) {
case 'even':
- if (@$tokens[$expr_end] == 'by') {
+ if (isset($tokens[$expr_end]) && $tokens[$expr_end] == 'by') {
$expr_end++;
$expr_arg = $tokens[$expr_end++];
- $expr = "!(($is_arg / $expr_arg) % " . $this->_parse_var_props($expr_arg) . ")";
+ $expr = "!(1 & ($is_arg / " . $this->_parse_var_props($expr_arg) . "))";
} else
- $expr = "!($is_arg % 2)";
+ $expr = "!(1 & $is_arg)";
break;
case 'odd':
- if (@$tokens[$expr_end] == 'by') {
+ if (isset($tokens[$expr_end]) && $tokens[$expr_end] == 'by') {
$expr_end++;
$expr_arg = $tokens[$expr_end++];
- $expr = "(($is_arg / $expr_arg) % ". $this->_parse_var_props($expr_arg) . ")";
+ $expr = "(1 & ($is_arg / " . $this->_parse_var_props($expr_arg) . "))";
} else
- $expr = "($is_arg % 2)";
+ $expr = "(1 & $is_arg)";
break;
case 'div':
{
/* Tokenize tag attributes. */
- preg_match_all('/(?:' . $this->_obj_call_regexp . '|' . $this->_qstr_regexp . ' | (?>[^"\'=\s]+)
+ preg_match_all('~(?:' . $this->_obj_call_regexp . '|' . $this->_qstr_regexp . ' | (?>[^"\'=\s]+)
)+ |
[=]
- /x', $tag_args, $match);
+ ~x', $tag_args, $match);
$tokens = $match[0];
$attrs = array();
case 0:
/* If the token is a valid identifier, we set attribute name
and go to state 1. */
- if (preg_match('!^\w+$!', $token)) {
+ if (preg_match('~^\w+$~', $token)) {
$attr_name = $token;
$state = 1;
} else
if ($token != '=') {
/* We booleanize the token if it's a non-quoted possible
boolean value. */
- if (preg_match('!^(on|yes|true)$!', $token)) {
+ if (preg_match('~^(on|yes|true)$~', $token)) {
$token = 'true';
- } else if (preg_match('!^(off|no|false)$!', $token)) {
+ } else if (preg_match('~^(off|no|false)$~', $token)) {
$token = 'false';
} else if ($token == 'null') {
$token = 'null';
- } else if (preg_match('!^-?([0-9]+|0[xX][0-9a-fA-F]+)$!', $token)) {
+ } else if (preg_match('~^' . $this->_num_const_regexp . '|0[xX][0-9a-fA-F]+$~', $token)) {
/* treat integer literally */
- } else if (!preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . ')*$!', $token)) {
+ } else if (!preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . ')*$~', $token)) {
/* treat as a string, double-quote it escaping quotes */
$token = '"'.addslashes($token).'"';
}
{
$val = trim($val);
- if(preg_match('!^(' . $this->_obj_call_regexp . '|' . $this->_dvar_regexp . ')(' . $this->_mod_regexp . '*)$!', $val, $match)) {
- // $ variable or object
- $return = $this->_parse_var($match[1]);
- if($match[2] != '') {
- $this->_parse_modifiers($return, $match[2]);
- }
- return $return;
+ if(preg_match('~^(' . $this->_obj_call_regexp . '|' . $this->_dvar_regexp . ')(' . $this->_mod_regexp . '*)$~', $val, $match)) {
+ // $ variable or object
+ $return = $this->_parse_var($match[1]);
+ $modifiers = $match[2];
+ if (!empty($this->default_modifiers) && !preg_match('~(^|\|)smarty:nodefaults($|\|)~',$modifiers)) {
+ $_default_mod_string = implode('|',(array)$this->default_modifiers);
+ $modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers;
}
- elseif(preg_match('!^' . $this->_db_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
+ $this->_parse_modifiers($return, $modifiers);
+ return $return;
+ } elseif (preg_match('~^' . $this->_db_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
// double quoted text
- preg_match('!^(' . $this->_db_qstr_regexp . ')('. $this->_mod_regexp . '*)$!', $val, $match);
+ preg_match('~^(' . $this->_db_qstr_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
$return = $this->_expand_quoted_text($match[1]);
if($match[2] != '') {
$this->_parse_modifiers($return, $match[2]);
}
return $return;
}
- elseif(preg_match('!^' . $this->_si_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
+ elseif(preg_match('~^' . $this->_num_const_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
+ // numerical constant
+ preg_match('~^(' . $this->_num_const_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
+ if($match[2] != '') {
+ $this->_parse_modifiers($match[1], $match[2]);
+ return $match[1];
+ }
+ }
+ elseif(preg_match('~^' . $this->_si_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
// single quoted text
- preg_match('!^(' . $this->_si_qstr_regexp . ')('. $this->_mod_regexp . '*)$!', $val, $match);
+ preg_match('~^(' . $this->_si_qstr_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
if($match[2] != '') {
$this->_parse_modifiers($match[1], $match[2]);
return $match[1];
}
}
- elseif(preg_match('!^' . $this->_cvar_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
+ elseif(preg_match('~^' . $this->_cvar_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
// config var
return $this->_parse_conf_var($val);
}
- elseif(preg_match('!^' . $this->_svar_regexp . '(?:' . $this->_mod_regexp . '*)$!', $val)) {
+ elseif(preg_match('~^' . $this->_svar_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
// section var
return $this->_parse_section_prop($val);
}
elseif(!in_array($val, $this->_permitted_tokens) && !is_numeric($val)) {
// literal string
- return $this->_expand_quoted_text('"' . $val .'"');
+ return $this->_expand_quoted_text('"' . strtr($val, array('\\' => '\\\\', '"' => '\\"')) .'"');
}
return $val;
}
function _expand_quoted_text($var_expr)
{
// if contains unescaped $, expand it
- if(preg_match_all('%(?:\`(?<!\\\\)\$' . $this->_dvar_guts_regexp . '\`)|(?:(?<!\\\\)\$\w+(\[[a-zA-Z0-9]+\])*)%', $var_expr, $_match)) {
+ if(preg_match_all('~(?:\`(?<!\\\\)\$' . $this->_dvar_guts_regexp . '(?:' . $this->_obj_ext_regexp . ')*\`)|(?:(?<!\\\\)\$\w+(\[[a-zA-Z0-9]+\])*)~', $var_expr, $_match)) {
$_match = $_match[0];
- rsort($_match);
- reset($_match);
+ $_replace = array();
foreach($_match as $_var) {
- $var_expr = str_replace ($_var, '".(' . $this->_parse_var(str_replace('`','',$_var)) . ')."', $var_expr);
+ $_replace[$_var] = '".(' . $this->_parse_var(str_replace('`','',$_var)) . ')."';
}
- $_return = preg_replace('%\.""|(?<!\\\\)""\.%', '', $var_expr);
+ $var_expr = strtr($var_expr, $_replace);
+ $_return = preg_replace('~\.""|(?<!\\\\)""\.~', '', $var_expr);
} else {
$_return = $var_expr;
}
// replace double quoted literal string with single quotes
- $_return = preg_replace('!^"([\s\w]+)"$!',"'\\1'",$_return);
+ $_return = preg_replace('~^"([\s\w]+)"$~',"'\\1'",$_return);
return $_return;
}
function _parse_var($var_expr)
{
$_has_math = false;
- $_math_vars = preg_split('!('.$this->_dvar_math_regexp.'|'.$this->_qstr_regexp.')!', $var_expr, -1, PREG_SPLIT_DELIM_CAPTURE);
+ $_math_vars = preg_split('~('.$this->_dvar_math_regexp.'|'.$this->_qstr_regexp.')~', $var_expr, -1, PREG_SPLIT_DELIM_CAPTURE);
if(count($_math_vars) > 1) {
$_first_var = "";
$_complete_var = "";
+ $_output = "";
// simple check if there is any math, to stop recursion (due to modifiers with "xx % yy" as parameter)
foreach($_math_vars as $_k => $_math_var) {
$_math_var = $_math_vars[$_k];
if(!empty($_math_var) || is_numeric($_math_var)) {
// hit a math operator, so process the stuff which came before it
- if(preg_match('!^' . $this->_dvar_math_regexp . '$!', $_math_var)) {
+ if(preg_match('~^' . $this->_dvar_math_regexp . '$~', $_math_var)) {
$_has_math = true;
if(!empty($_complete_var) || is_numeric($_complete_var)) {
$_output .= $this->_parse_var($_complete_var);
$_complete_var = "";
} else {
- // fetch multiple -> (like $foo->bar->baz ) which wouldn't get fetched else, because it would only get $foo->bar and treat the ->baz as "-" ">baz" then
- for($_i = $_k + 1; $_i <= count($_math_vars); $_i += 2) {
- // fetch -> because it gets splitted at - and move it back together
- if( /* prevent notice */ (isset($_math_vars[$_i]) && isset($_math_vars[$_i+1])) && ($_math_vars[$_i] === '-' && $_math_vars[$_i+1]{0} === '>')) {
- $_math_var .= $_math_vars[$_i].$_math_vars[$_i+1];
- $_math_vars[$_i] = $_math_vars[$_i+1] = '';
- } else {
- break;
- }
- }
$_complete_var .= $_math_var;
}
}
}
if($_has_math) {
if(!empty($_complete_var) || is_numeric($_complete_var))
- $_output .= $this->_parse_var($_complete_var, true);
+ $_output .= $this->_parse_var($_complete_var);
// get the modifiers working (only the last var from math + modifier is left)
$var_expr = $_complete_var;
}
// prevent cutting of first digit in the number (we _definitly_ got a number if the first char is a digit)
- if(is_numeric($var_expr{0}))
+ if(is_numeric(substr($var_expr, 0, 1)))
$_var_ref = $var_expr;
else
$_var_ref = substr($var_expr, 1);
-
+
if(!$_has_math) {
+
// get [foo] and .foo and ->foo and (...) pieces
- preg_match_all('!(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\$?\w+|\.\$?\w+|\S+!', $_var_ref, $match);
-
+ preg_match_all('~(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\$?\w+|\.\$?\w+|\S+~', $_var_ref, $match);
+
$_indexes = $match[0];
$_var_name = array_shift($_indexes);
$_var_name = substr(array_shift($_indexes), 1);
$_output = "\$this->_smarty_vars['$_var_name']";
}
- } elseif(is_numeric($_var_name) && is_numeric($var_expr{0})) {
+ } elseif(is_numeric($_var_name) && is_numeric(substr($var_expr, 0, 1))) {
// because . is the operator for accessing arrays thru inidizes we need to put it together again for floating point numbers
if(count($_indexes) > 0)
{
}
foreach ($_indexes as $_index) {
- if ($_index{0} == '[') {
+ if (substr($_index, 0, 1) == '[') {
$_index = substr($_index, 1, -1);
if (is_numeric($_index)) {
$_output .= "[$_index]";
- } elseif ($_index{0} == '$') {
+ } elseif (substr($_index, 0, 1) == '$') {
if (strpos($_index, '.') !== false) {
$_output .= '[' . $this->_parse_var($_index) . ']';
} else {
$_var_section_prop = isset($_var_parts[1]) ? $_var_parts[1] : 'index';
$_output .= "[\$this->_sections['$_var_section']['$_var_section_prop']]";
}
- } else if ($_index{0} == '.') {
- if ($_index{1} == '$')
+ } else if (substr($_index, 0, 1) == '.') {
+ if (substr($_index, 1, 1) == '$')
$_output .= "[\$this->_tpl_vars['" . substr($_index, 2) . "']]";
else
$_output .= "['" . substr($_index, 1) . "']";
$this->_syntax_error('call to internal object members is not allowed', E_USER_ERROR, __FILE__, __LINE__);
} elseif($this->security && substr($_index, 2, 1) == '_') {
$this->_syntax_error('(secure) call to private object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
- } elseif ($_index{2} == '$') {
+ } elseif (substr($_index, 2, 1) == '$') {
if ($this->security) {
$this->_syntax_error('(secure) call to dynamic object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
} else {
} else {
$_output .= $_index;
}
- } elseif ($_index{0} == '(') {
+ } elseif (substr($_index, 0, 1) == '(') {
$_index = $this->_parse_parenth_args($_index);
$_output .= $_index;
} else {
*/
function _parse_parenth_args($parenth_args)
{
- preg_match_all('!' . $this->_param_regexp . '!',$parenth_args, $match);
- $match = $match[0];
- rsort($match);
- reset($match);
- $orig_vals = $match;
+ preg_match_all('~' . $this->_param_regexp . '~',$parenth_args, $match);
+ $orig_vals = $match = $match[0];
$this->_parse_vars_props($match);
- return str_replace($orig_vals, $match, $parenth_args);
+ $replace = array();
+ for ($i = 0, $count = count($match); $i < $count; $i++) {
+ $replace[$orig_vals[$i]] = $match[$i];
+ }
+ return strtr($parenth_args, $replace);
}
/**
*/
function _parse_modifiers(&$output, $modifier_string)
{
- preg_match_all('!\|(@?\w+)((?>:(?:'. $this->_qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $_match);
+ preg_match_all('~\|(@?\w+)((?>:(?:'. $this->_qstr_regexp . '|[^|]+))*)~', '|' . $modifier_string, $_match);
list(, $_modifiers, $modifier_arg_strings) = $_match;
for ($_i = 0, $_for_max = count($_modifiers); $_i < $_for_max; $_i++) {
continue;
}
- preg_match_all('!:(' . $this->_qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$_i], $_match);
+ preg_match_all('~:(' . $this->_qstr_regexp . '|[^:]+)~', $modifier_arg_strings[$_i], $_match);
$_modifier_args = $_match[1];
- if ($_modifier_name{0} == '@') {
+ if (substr($_modifier_name, 0, 1) == '@') {
$_map_array = false;
$_modifier_name = substr($_modifier_name, 1);
} else {
$_map_array = true;
}
- $this->_add_plugin('modifier', $_modifier_name);
if (empty($this->_plugins['modifier'][$_modifier_name])
&& !$this->_get_plugin_filepath('modifier', $_modifier_name)
&& function_exists($_modifier_name)) {
if ($this->security && !in_array($_modifier_name, $this->security_settings['MODIFIER_FUNCS'])) {
- $this->_trigger_fatal_error("[plugin] (secure mode) modifier '$_modifier_name' is not allowed" , $_tpl_file, $_tpl_line, __FILE__, __LINE__);
+ $this->_trigger_fatal_error("[plugin] (secure mode) modifier '$_modifier_name' is not allowed" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
} else {
$this->_plugins['modifier'][$_modifier_name] = array($_modifier_name, null, null, false);
}
}
+ $this->_add_plugin('modifier', $_modifier_name);
$this->_parse_vars_props($_modifier_args);
if($_modifier_name == 'default') {
// supress notifications of default modifier vars and args
- if($output{0} == '$') {
+ if(substr($output, 0, 1) == '$') {
$output = '@' . $output;
}
- if(isset($_modifier_args[0]) && $_modifier_args[0]{0} == '$') {
+ if(isset($_modifier_args[0]) && substr($_modifier_args[0], 0, 1) == '$') {
$_modifier_args[0] = '@' . $_modifier_args[0];
}
}
/* Extract the reference name. */
$_ref = substr($indexes[0], 1);
foreach($indexes as $_index_no=>$_index) {
- if ($_index{0} != '.' && $_index_no<2 || !preg_match('!^(\.|\[|->)!', $_index)) {
+ if (substr($_index, 0, 1) != '.' && $_index_no<2 || !preg_match('~^(\.|\[|->)~', $_index)) {
$this->_syntax_error('$smarty' . implode('', array_slice($indexes, 0, 2)) . ' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
}
}
break;
case 'foreach':
+ array_shift($indexes);
+ $_var = $this->_parse_var_props(substr($indexes[0], 1));
+ $_propname = substr($indexes[1], 1);
+ $_max_index = 1;
+ switch ($_propname) {
+ case 'index':
+ array_shift($indexes);
+ $compiled_ref = "(\$this->_foreach[$_var]['iteration']-1)";
+ break;
+
+ case 'first':
+ array_shift($indexes);
+ $compiled_ref = "(\$this->_foreach[$_var]['iteration'] <= 1)";
+ break;
+
+ case 'last':
+ array_shift($indexes);
+ $compiled_ref = "(\$this->_foreach[$_var]['iteration'] == \$this->_foreach[$_var]['total'])";
+ break;
+
+ case 'show':
+ array_shift($indexes);
+ $compiled_ref = "(\$this->_foreach[$_var]['total'] > 0)";
+ break;
+
+ default:
+ unset($_max_index);
+ $compiled_ref = "\$this->_foreach[$_var]";
+ }
+ break;
+
case 'section':
array_shift($indexes);
$_var = $this->_parse_var_props(substr($indexes[0], 1));
- if ($_ref == 'foreach')
- $compiled_ref = "\$this->_foreach[$_var]";
- else
- $compiled_ref = "\$this->_sections[$_var]";
+ $compiled_ref = "\$this->_sections[$_var]";
break;
case 'get':
break;
case 'const':
+ if ($this->security && !$this->security_settings['ALLOW_CONSTANTS']) {
+ $this->_syntax_error("(secure mode) constants not permitted",
+ E_USER_WARNING, __FILE__, __LINE__);
+ return;
+ }
array_shift($indexes);
- $_val = $this->_parse_var_props(substr($indexes[0],1));
- $compiled_ref = '@constant(' . $_val . ')';
+ if (preg_match('!^\.\w+$!', $indexes[0])) {
+ $compiled_ref = '@' . substr($indexes[0], 1);
+ } else {
+ $_val = $this->_parse_var_props(substr($indexes[0], 1));
+ $compiled_ref = '@constant(' . $_val . ')';
+ }
$_max_index = 1;
break;
case 'config':
$compiled_ref = "\$this->_config[0]['vars']";
- $_max_index = 2;
+ $_max_index = 3;
break;
+ case 'ldelim':
+ $compiled_ref = "'$this->left_delimiter'";
+ break;
+
+ case 'rdelim':
+ $compiled_ref = "'$this->right_delimiter'";
+ break;
+
default:
$this->_syntax_error('$smarty.' . $_ref . ' is an unknown reference', E_USER_ERROR, __FILE__, __LINE__);
break;
if ($prefilter === false) {
unset($this->_plugins['prefilter'][$filter_name]);
$_params = array('plugins' => array(array('prefilter', $filter_name, null, null, false)));
- require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php');
+ require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
smarty_core_load_plugins($_params, $this);
}
}
if ($postfilter === false) {
unset($this->_plugins['postfilter'][$filter_name]);
$_params = array('plugins' => array(array('postfilter', $filter_name, null, null, false)));
- require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php');
+ require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
smarty_core_load_plugins($_params, $this);
}
}
*/
function _quote_replace($string)
{
- return preg_replace('![\\$]\d!', '\\\\\\0', $string);
+ return strtr($string, array('\\' => '\\\\', '$' => '\\$'));
}
/**
*/
function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null)
{
- if(isset($file) && isset($line)) {
- $info = ' ('.basename($file).", line $line)";
- } else {
- $info = null;
- }
- trigger_error('Smarty: [in ' . $this->_current_file . ' line ' .
- $this->_current_line_no . "]: syntax error: $error_msg$info", $error_type);
+ $this->_trigger_fatal_error("syntax error: $error_msg", $this->_current_file, $this->_current_line_no, $file, $line, $error_type);
}
if ($_cacheable
|| 0<$this->_cacheable_state++) return '';
if (!isset($this->_cache_serial)) $this->_cache_serial = md5(uniqid('Smarty'));
- $_ret = 'if ($this->caching) { echo \'{nocache:'
+ $_ret = 'if ($this->caching && !$this->_cache_including): echo \'{nocache:'
. $this->_cache_serial . '#' . $this->_nocache_count
- . '}\';}';
+ . '}\'; endif;';
return $_ret;
}
$_cacheable = !isset($this->_plugins[$type][$name]) || $this->_plugins[$type][$name][4];
if ($_cacheable
|| --$this->_cacheable_state>0) return '';
- return 'if ($this->caching) { echo \'{/nocache:'
+ return 'if ($this->caching && !$this->_cache_including): echo \'{/nocache:'
. $this->_cache_serial . '#' . ($this->_nocache_count++)
- . '}\';}';
+ . '}\'; endif;';
+ }
+
+
+ /**
+ * push opening tag-name, file-name and line-number on the tag-stack
+ * @param string the opening tag's name
+ */
+ function _push_tag($open_tag)
+ {
+ array_push($this->_tag_stack, array($open_tag, $this->_current_line_no));
+ }
+
+ /**
+ * pop closing tag-name
+ * raise an error if this stack-top doesn't match with the closing tag
+ * @param string the closing tag's name
+ * @return string the opening tag's name
+ */
+ function _pop_tag($close_tag)
+ {
+ $message = '';
+ if (count($this->_tag_stack)>0) {
+ list($_open_tag, $_line_no) = array_pop($this->_tag_stack);
+ if ($close_tag == $_open_tag) {
+ return $_open_tag;
+ }
+ if ($close_tag == 'if' && ($_open_tag == 'else' || $_open_tag == 'elseif' )) {
+ return $this->_pop_tag($close_tag);
+ }
+ if ($close_tag == 'section' && $_open_tag == 'sectionelse') {
+ $this->_pop_tag($close_tag);
+ return $_open_tag;
+ }
+ if ($close_tag == 'foreach' && $_open_tag == 'foreachelse') {
+ $this->_pop_tag($close_tag);
+ return $_open_tag;
+ }
+ if ($_open_tag == 'else' || $_open_tag == 'elseif') {
+ $_open_tag = 'if';
+ } elseif ($_open_tag == 'sectionelse') {
+ $_open_tag = 'section';
+ } elseif ($_open_tag == 'foreachelse') {
+ $_open_tag = 'foreach';
+ }
+ $message = " expected {/$_open_tag} (opened line $_line_no).";
+ }
+ $this->_syntax_error("mismatched tag {/$close_tag}.$message",
+ E_USER_ERROR, __FILE__, __LINE__);
}
}