Update to Smarty 2.6.22

This commit is contained in:
anubis
2009-01-04 17:34:22 -05:00
parent eedd737a97
commit 60b674c776
18 changed files with 473 additions and 216 deletions

View File

@@ -18,15 +18,15 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @link http://smarty.php.net/
* @link http://www.smarty.net/
* @author Monte Ohrt <monte at ohrt dot com>
* @author Andrei Zmievski <andrei@php.net>
* @version 2.6.12
* @version 2.6.22
* @copyright 2001-2005 New Digital Group, Inc.
* @package Smarty
*/
/* $Id: Smarty_Compiler.class.php,v 1.376 2006/01/15 19:29:45 messju Exp $ */
/* $Id: Smarty_Compiler.class.php 2966 2008-12-08 15:10:03Z monte.ohrt $ */
/**
* Template compiling class
@@ -73,6 +73,9 @@ class Smarty_Compiler extends Smarty {
var $_strip_depth = 0;
var $_additional_newline = "\n";
var $_phpversion = 0;
/**#@-*/
/**
@@ -80,6 +83,8 @@ class Smarty_Compiler extends Smarty {
*/
function Smarty_Compiler()
{
$this->_phpversion = substr(phpversion(),0,1);
// matches double quoted strings:
// "foobar"
// "foo\"bar"
@@ -152,16 +157,20 @@ class Smarty_Compiler extends Smarty {
// $foo->bar($foo->bar)
// $foo->bar($foo->bar())
// $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar))
// $foo->getBar()->getFoo()
// $foo->getBar()->foo
$this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_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->_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
$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->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_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->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';
// matches valid modifier syntax:
// |foo
@@ -278,7 +287,7 @@ class Smarty_Compiler extends Smarty {
/* 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');
@@ -304,7 +313,7 @@ class Smarty_Compiler extends Smarty {
}
}
}
/* Compile the template tags into PHP code. */
$compiled_tags = array();
for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
@@ -349,17 +358,30 @@ class Smarty_Compiler extends Smarty {
}
}
$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]);
}
$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]);
// 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 (strlen($compiled_content) && (substr($compiled_content, -1) == "\n") ) {
$compiled_content = substr($compiled_content, 0, -1);
@@ -369,9 +391,6 @@ class Smarty_Compiler extends Smarty {
$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) {
@@ -859,7 +878,7 @@ class Smarty_Compiler extends Smarty {
// traditional argument format
$args = implode(',', array_values($attrs));
if (empty($args)) {
$args = 'null';
$args = '';
}
}
@@ -880,9 +899,9 @@ class Smarty_Compiler extends Smarty {
$prefix .= "while (\$_block_repeat) { ob_start();";
$return = null;
$postfix = '';
} else {
$prefix = "\$_obj_block_content = ob_get_contents(); ob_end_clean(); ";
$return = "\$_block_repeat=false; \$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$_obj_block_content, \$this, \$_block_repeat)";
} 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 {
@@ -924,7 +943,11 @@ class Smarty_Compiler extends Smarty {
$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'])) {
@@ -1157,7 +1180,7 @@ class Smarty_Compiler extends Smarty {
}
$item = $this->_dequote($attrs['item']);
if (!preg_match('~^\w+$~', $item)) {
return $this->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
return $this->_syntax_error("foreach: 'item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
}
if (isset($attrs['key'])) {
@@ -1208,23 +1231,21 @@ class Smarty_Compiler extends Smarty {
$attrs = $this->_parse_attrs($tag_args);
if ($start) {
if (isset($attrs['name']))
$buffer = $attrs['name'];
else
$buffer = "'default'";
if (isset($attrs['assign']))
$assign = $attrs['assign'];
else
$assign = null;
$buffer = isset($attrs['name']) ? $attrs['name'] : "'default'";
$assign = isset($attrs['assign']) ? $attrs['assign'] : null;
$append = isset($attrs['append']) ? $attrs['append'] : null;
$output = "<?php ob_start(); ?>";
$this->_capture_stack[] = array($buffer, $assign);
$this->_capture_stack[] = array($buffer, $assign, $append);
} else {
list($buffer, $assign) = array_pop($this->_capture_stack);
list($buffer, $assign, $append) = array_pop($this->_capture_stack);
$output = "<?php \$this->_smarty_vars['capture'][$buffer] = ob_get_contents(); ";
if (isset($assign)) {
$output .= " \$this->assign($assign, ob_get_contents());";
}
if (isset($append)) {
$output .= " \$this->append($append, ob_get_contents());";
}
$output .= "ob_end_clean(); ?>";
}
@@ -1253,7 +1274,7 @@ class Smarty_Compiler extends Smarty {
$tokens = $match[0];
if(empty($tokens)) {
$_error_msg .= $elseif ? "'elseif'" : "'if'";
$_error_msg = $elseif ? "'elseif'" : "'if'";
$_error_msg .= ' statement requires arguments';
$this->_syntax_error($_error_msg, E_USER_ERROR, __FILE__, __LINE__);
}
@@ -1351,9 +1372,14 @@ class Smarty_Compiler extends Smarty {
/* If last token was a ')', we operate on the parenthesized
expression. The start of the expression is on the stack.
Otherwise, we operate on the last encountered token. */
if ($tokens[$i-1] == ')')
if ($tokens[$i-1] == ')') {
$is_arg_start = array_pop($is_arg_stack);
else
if ($is_arg_start != 0) {
if (preg_match('~^' . $this->_func_regexp . '$~', $tokens[$is_arg_start-1])) {
$is_arg_start--;
}
}
} else
$is_arg_start = $i-1;
/* Construct the argument for 'is' expression, so it knows
what to operate on. */
@@ -1668,17 +1694,19 @@ class Smarty_Compiler extends Smarty {
// if contains unescaped $, expand it
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)) . ')."';
}
$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);
// escape dollar sign if not printing a var
$_return = preg_replace('~\$(\W)~',"\\\\\$\\1",$_return);
return $_return;
}
@@ -1692,6 +1720,7 @@ class Smarty_Compiler extends Smarty {
function _parse_var($var_expr)
{
$_has_math = false;
$_has_php4_method_chaining = false;
$_math_vars = preg_split('~('.$this->_dvar_math_regexp.'|'.$this->_qstr_regexp.')~', $var_expr, -1, PREG_SPLIT_DELIM_CAPTURE);
if(count($_math_vars) > 1) {
@@ -1804,6 +1833,10 @@ class Smarty_Compiler extends Smarty {
$_output .= '->{(($_var=$this->_tpl_vars[\''.substr($_index,3).'\']) && substr($_var,0,2)!=\'__\') ? $_var : $this->trigger_error("cannot access property \\"$_var\\"")}';
}
} else {
if ($this->_phpversion < 5) {
$_has_php4_method_chaining = true;
$_output .= "; \$_foo = \$_foo";
}
$_output .= $_index;
}
} elseif (substr($_index, 0, 1) == '(') {
@@ -1815,7 +1848,12 @@ class Smarty_Compiler extends Smarty {
}
}
return $_output;
if ($_has_php4_method_chaining) {
$_tmp = str_replace("'","\'",'$_foo = '.$_output.'; return $_foo;');
return "eval('".$_tmp."')";
} else {
return $_output;
}
}
/**
@@ -2216,9 +2254,9 @@ class Smarty_Compiler extends Smarty {
if ($_cacheable
|| 0<$this->_cacheable_state++) return '';
if (!isset($this->_cache_serial)) $this->_cache_serial = md5(uniqid('Smarty'));
$_ret = 'if ($this->caching && !$this->_cache_including) { echo \'{nocache:'
$_ret = 'if ($this->caching && !$this->_cache_including): echo \'{nocache:'
. $this->_cache_serial . '#' . $this->_nocache_count
. '}\'; };';
. '}\'; endif;';
return $_ret;
}
@@ -2233,9 +2271,9 @@ class Smarty_Compiler extends Smarty {
$_cacheable = !isset($this->_plugins[$type][$name]) || $this->_plugins[$type][$name][4];
if ($_cacheable
|| --$this->_cacheable_state>0) return '';
return 'if ($this->caching && !$this->_cache_including) { echo \'{/nocache:'
return 'if ($this->caching && !$this->_cache_including): echo \'{/nocache:'
. $this->_cache_serial . '#' . ($this->_nocache_count++)
. '}\'; };';
. '}\'; endif;';
}