introduce LA_ for addslashing language variables for use in js vars (idea by naderman)

git-svn-id: file:///svn/phpbb/trunk@5865 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2006-04-29 17:19:24 +00:00
parent f1de99ff9a
commit 076a235bb0
2 changed files with 61 additions and 78 deletions

View file

@ -71,8 +71,7 @@ class template_compile
} }
/** /**
* The all seeing all doing compile method. Parts are inspired by or directly * The all seeing all doing compile method. Parts are inspired by or directly from Smarty
* from Smarty
* @private * @private
*/ */
function compile($code, $no_echo = false, $echo_var = '') function compile($code, $no_echo = false, $echo_var = '')
@ -169,32 +168,18 @@ class template_compile
break; break;
case 'INCLUDEPHP': case 'INCLUDEPHP':
if ($config['tpl_php']) $compile_blocks[] = ($config['tpl_php']) ? '<?php ' . $this->compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>' : '';
{ break;
$compile_blocks[] = '<?php ' . $this->compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>';
}
else
{
$compile_blocks[] = '';
}
break;
case 'PHP': case 'PHP':
if ($config['tpl_php']) $compile_blocks[] = ($config['tpl_php']) ? '<?php ' . array_shift($php_blocks) . ' ?>' : '';
{ break;
$compile_blocks[] = '<?php ' . array_shift($php_blocks) . ' ?>';
}
else
{
$compile_blocks[] = '';
}
break;
default: default:
$this->compile_var_tags($blocks[0][$curr_tb]); $this->compile_var_tags($blocks[0][$curr_tb]);
$trim_check = trim($blocks[0][$curr_tb]); $trim_check = trim($blocks[0][$curr_tb]);
$compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $blocks[0][$curr_tb] : '') : ((!empty($trim_check)) ? $blocks[0][$curr_tb] : ''); $compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $blocks[0][$curr_tb] : '') : ((!empty($trim_check)) ? $blocks[0][$curr_tb] : '');
break; break;
} }
} }
@ -235,17 +220,15 @@ class template_compile
} }
// This will handle the remaining root-level varrefs // This will handle the remaining root-level varrefs
if (!$this->template->static_lang)
{
$text_blocks = preg_replace('#\{L_([a-z0-9\-_]*)\}#is', "<?php echo ((isset(\$this->_tpldata['.'][0]['L_\\1'])) ? \$this->_tpldata['.'][0]['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ ' . ucfirst(strtolower(str_replace('_', ' ', '\\1'))) . ' }')); ?>", $text_blocks);
}
else
{
global $user;
$text_blocks = preg_replace('#\{L_([A-Z0-9\-_]*)\}#e', "'<?php echo ((isset(\$this->_tpldata[\'.\'][0][\'L_\\1\'])) ? \$this->_tpldata[\'.\'][0][\'L_\\1\'] : \'' . ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '') . '\'); ?>'" , $text_blocks); // transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array
} $text_blocks = preg_replace('#\{L_([a-z0-9\-_]*)\}#is', "<?php echo ((isset(\$this->_tpldata['.'][0]['L_\\1'])) ? \$this->_tpldata['.'][0]['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ ' . ucfirst(strtolower(str_replace('_', ' ', '\\1'))) . ' }')); ?>", $text_blocks);
// Handle addslashed language variables prefixed with LA_
// If a template variable already exist, it will be used in favor of it...
$text_blocks = preg_replace('#\{LA_([a-z0-9\-_]*)\}#is', "<?php echo ((isset(\$this->_tpldata['.'][0]['LA_\\1'])) ? \$this->_tpldata['.'][0]['LA_\\1'] : ((isset(\$this->_tpldata['.'][0]['L_\\1'])) ? addslashes(\$this->_tpldata['.'][0]['L_\\1']) : ((isset(\$user->lang['\\1'])) ? addslashes(\$user->lang['\\1']) : '{ ' . ucfirst(strtolower(str_replace('_', ' ', '\\1'))) . ' }'))); ?>", $text_blocks);
// Handle remaining varrefs
$text_blocks = preg_replace('#\{([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$this->_tpldata['.'][0]['\\1'])) ? \$this->_tpldata['.'][0]['\\1'] : ''; ?>", $text_blocks); $text_blocks = preg_replace('#\{([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$this->_tpldata['.'][0]['\\1'])) ? \$this->_tpldata['.'][0]['\\1'] : ''; ?>", $text_blocks);
$text_blocks = preg_replace('#\{\$([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$this->_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks); $text_blocks = preg_replace('#\{\$([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$this->_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks);
@ -260,6 +243,7 @@ class template_compile
{ {
$no_nesting = false; $no_nesting = false;
// Is the designer wanting to call another loop in a loop?
if (strpos($tag_args, '!') === 0) if (strpos($tag_args, '!') === 0)
{ {
// Count the number if ! occurrences (not allowed in vars) // Count the number if ! occurrences (not allowed in vars)
@ -364,21 +348,10 @@ class template_compile
switch ($token) switch ($token)
{ {
case '!':
case '%':
case '!==': case '!==':
case '==':
case '===': case '===':
case '>':
case '<':
case '!=':
case '<>':
case '<<': case '<<':
case '>>': case '>>':
case '<=':
case '>=':
case '&&':
case '||':
case '|': case '|':
case '^': case '^':
case '&': case '&':
@ -390,54 +363,65 @@ class template_compile
case '*': case '*':
case '/': case '/':
case '@': case '@':
break; break;
case '==':
case 'eq': case 'eq':
$token = '=='; $token = '==';
break; break;
case '!=':
case '<>':
case 'ne': case 'ne':
case 'neq': case 'neq':
$token = '!='; $token = '!=';
break; break;
case '<':
case 'lt': case 'lt':
$token = '<'; $token = '<';
break; break;
case '<=':
case 'le': case 'le':
case 'lte': case 'lte':
$token = '<='; $token = '<=';
break; break;
case '>':
case 'gt': case 'gt':
$token = '>'; $token = '>';
break; break;
case '>=':
case 'ge': case 'ge':
case 'gte': case 'gte':
$token = '>='; $token = '>=';
break; break;
case '&&':
case 'and': case 'and':
$token = '&&'; $token = '&&';
break; break;
case '||':
case 'or': case 'or':
$token = '||'; $token = '||';
break; break;
case '!':
case 'not': case 'not':
$token = '!'; $token = '!';
break; break;
case '%':
case 'mod': case 'mod':
$token = '%'; $token = '%';
break; break;
case '(': case '(':
array_push($is_arg_stack, $i); array_push($is_arg_stack, $i);
break; break;
case 'is': case 'is':
$is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1; $is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1;
@ -449,6 +433,8 @@ class template_compile
$i = $is_arg_start; $i = $is_arg_start;
// no break
default: default:
if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs)) if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs))
{ {
@ -460,7 +446,7 @@ class template_compile
$token = "(isset($_tok) && sizeof($_tok))"; $token = "(isset($_tok) && sizeof($_tok))";
} }
break; break;
} }
} }
@ -505,13 +491,15 @@ class template_compile
case 'true': case 'true':
case 'false': case 'false':
$match[4] = strtoupper($match[4]); $match[4] = strtoupper($match[4]);
break; break;
case '.'; case '.';
$match[4] = doubleval($match[4]); $match[4] = doubleval($match[4]);
break; break;
default: default:
$match[4] = intval($match[4]); $match[4] = intval($match[4]);
break; break;
} }
} }
@ -569,7 +557,7 @@ class template_compile
{ {
$expr = "!($is_arg % 2)"; $expr = "!($is_arg % 2)";
} }
break; break;
case 'odd': case 'odd':
if (@$tokens[$expr_end] == 'by') if (@$tokens[$expr_end] == 'by')
@ -582,7 +570,7 @@ class template_compile
{ {
$expr = "($is_arg % 2)"; $expr = "($is_arg % 2)";
} }
break; break;
case 'div': case 'div':
if (@$tokens[$expr_end] == 'by') if (@$tokens[$expr_end] == 'by')
@ -591,10 +579,10 @@ class template_compile
$expr_arg = $tokens[$expr_end++]; $expr_arg = $tokens[$expr_end++];
$expr = "!($is_arg % $expr_arg)"; $expr = "!($is_arg % $expr_arg)";
} }
break; break;
default: default:
break; break;
} }
if ($negate_expr) if ($negate_expr)
@ -673,7 +661,7 @@ class template_compile
{ {
global $phpEx, $user; global $phpEx, $user;
$filename = $this->template->cachepath . $this->template->filename[$handle] . '.' . (($this->template->static_lang) ? $user->data['user_lang'] . '.' : '') . $phpEx; $filename = $this->template->cachepath . $this->template->filename[$handle] . '.' . $phpEx;
if ($fp = @fopen($filename, 'wb')) if ($fp = @fopen($filename, 'wb'))
{ {

View file

@ -23,11 +23,12 @@ if (!defined('IN_PHPBB'))
class template class template
{ {
// variable that holds all the data we'll be substituting into /** variable that holds all the data we'll be substituting into
// the compiled templates. Takes form: * the compiled templates. Takes form:
// --> $this->_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value * --> $this->_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value
// if it's a root-level variable, it'll be like this: * if it's a root-level variable, it'll be like this:
// --> $this->_tpldata[.][0][varname] == value * --> $this->_tpldata[.][0][varname] == value
*/
var $_tpldata = array(); var $_tpldata = array();
// Root dir and hash of filenames for each template handle. // Root dir and hash of filenames for each template handle.
@ -38,13 +39,11 @@ class template
// this will hash handle names to the compiled/uncompiled code for that handle. // this will hash handle names to the compiled/uncompiled code for that handle.
var $compiled_code = array(); var $compiled_code = array();
var $static_lang;
/** /**
* Set template location * Set template location
* @public * @public
*/ */
function set_template($static_lang = false) function set_template()
{ {
global $phpbb_root_path, $config, $user; global $phpbb_root_path, $config, $user;
@ -54,8 +53,6 @@ class template
$this->cachepath = $phpbb_root_path . 'cache/tpl_' . $user->theme['template_path'] . '_'; $this->cachepath = $phpbb_root_path . 'cache/tpl_' . $user->theme['template_path'] . '_';
} }
$this->static_lang = $static_lang;
return true; return true;
} }
@ -63,15 +60,13 @@ class template
* Set custom template location (able to use directory outside of phpBB) * Set custom template location (able to use directory outside of phpBB)
* @public * @public
*/ */
function set_custom_template($template_path, $template_name, $static_lang = false) function set_custom_template($template_path, $template_name)
{ {
global $phpbb_root_path; global $phpbb_root_path;
$this->root = $template_path; $this->root = $template_path;
$this->cachepath = $phpbb_root_path . 'cache/ctpl_' . $template_name . '_'; $this->cachepath = $phpbb_root_path . 'cache/ctpl_' . $template_name . '_';
$this->static_lang = $static_lang;
return true; return true;
} }
@ -91,7 +86,7 @@ class template
{ {
if (empty($filename)) if (empty($filename))
{ {
trigger_error("template error - Empty filename specified for $handle", E_USER_ERROR); trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
} }
$this->filename[$handle] = $filename; $this->filename[$handle] = $filename;
@ -158,7 +153,7 @@ class template
{ {
global $user, $phpEx, $config; global $user, $phpEx, $config;
$filename = $this->cachepath . $this->filename[$handle] . '.' . (($this->static_lang) ? $user->data['user_lang'] . '.' : '') . $phpEx; $filename = $this->cachepath . $this->filename[$handle] . '.' . $phpEx;
$recompile = (($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle])) || !file_exists($filename)) ? true : false; $recompile = (($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle])) || !file_exists($filename)) ? true : false;
@ -227,7 +222,7 @@ class template
else else
{ {
// Only bother compiling if it doesn't already exist // Only bother compiling if it doesn't already exist
if (!file_exists($this->cachepath . $row['template_filename'] . '.' . (($this->static_lang) ? $user->data['user_lang'] . '.' : '') . $phpEx)) if (!file_exists($this->cachepath . $row['template_filename'] . '.' . $phpEx))
{ {
$this->filename[$row['template_filename']] = $row['template_filename']; $this->filename[$row['template_filename']] = $row['template_filename'];
$compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data']))); $compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));