includes/functions_template.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_template.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,814 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Extension of template class - Functions needed for compiling templates only.
    1.24 +*
    1.25 +* psoTFX, phpBB Development Team - Completion of file caching, decompilation
    1.26 +* routines and implementation of conditionals/keywords and associated changes
    1.27 +*
    1.28 +* The interface was inspired by PHPLib templates,  and the template file (formats are
    1.29 +* quite similar)
    1.30 +*
    1.31 +* The keyword/conditional implementation is currently based on sections of code from
    1.32 +* the Smarty templating engine (c) 2001 ispi of Lincoln, Inc. which is released
    1.33 +* (on its own and in whole) under the LGPL. Section 3 of the LGPL states that any code
    1.34 +* derived from an LGPL application may be relicenced under the GPL, this applies
    1.35 +* to this source
    1.36 +*
    1.37 +* DEFINE directive inspired by a request by Cyberalien
    1.38 +*
    1.39 +* @package phpBB3
    1.40 +*/
    1.41 +class template_compile
    1.42 +{
    1.43 +	var $template;
    1.44 +
    1.45 +	// Various storage arrays
    1.46 +	var $block_names = array();
    1.47 +	var $block_else_level = array();
    1.48 +
    1.49 +	/**
    1.50 +	* constuctor
    1.51 +	*/
    1.52 +	function template_compile(&$template)
    1.53 +	{
    1.54 +		$this->template = &$template;
    1.55 +	}
    1.56 +
    1.57 +	/**
    1.58 +	* Load template source from file
    1.59 +	* @access private
    1.60 +	*/
    1.61 +	function _tpl_load_file($handle, $store_in_db = false)
    1.62 +	{
    1.63 +		// Try and open template for read
    1.64 +		if (!file_exists($this->template->files[$handle]))
    1.65 +		{
    1.66 +			trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
    1.67 +		}
    1.68 +
    1.69 +		$this->template->compiled_code[$handle] = $this->compile(trim(@file_get_contents($this->template->files[$handle])));
    1.70 +
    1.71 +		// Actually compile the code now.
    1.72 +		$this->compile_write($handle, $this->template->compiled_code[$handle]);
    1.73 +
    1.74 +		// Store in database if required...
    1.75 +		if ($store_in_db)
    1.76 +		{
    1.77 +			global $db, $user;
    1.78 +
    1.79 +			$sql_ary = array(
    1.80 +				'template_id'			=> $this->template->files_template[$handle],
    1.81 +				'template_filename'		=> $this->template->filename[$handle],
    1.82 +				'template_included'		=> '',
    1.83 +				'template_mtime'		=> time(),
    1.84 +				'template_data'			=> trim(@file_get_contents($this->template->files[$handle])),
    1.85 +			);
    1.86 +
    1.87 +			$sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
    1.88 +			$db->sql_query($sql);
    1.89 +		}
    1.90 +	}
    1.91 +
    1.92 +	/**
    1.93 +	* Remove any PHP tags that do not belong, these regular expressions are derived from
    1.94 +	* the ones that exist in zend_language_scanner.l
    1.95 +	* @access private
    1.96 +	*/
    1.97 +	function remove_php_tags(&$code)
    1.98 +	{
    1.99 +		// This matches the information gathered from the internal PHP lexer
   1.100 +		$match = array(
   1.101 +			'#<([\?%])=?.*?\1>#s',
   1.102 +			'#<script\s+language\s*=\s*(["\']?)php\1\s*>.*?</script\s*>#s',
   1.103 +			'#<\?php(?:\r\n?|[ \n\t]).*?\?>#s'
   1.104 +		);
   1.105 +
   1.106 +		$code = preg_replace($match, '', $code);
   1.107 +	}
   1.108 +
   1.109 +	/**
   1.110 +	* The all seeing all doing compile method. Parts are inspired by or directly from Smarty
   1.111 +	* @access private
   1.112 +	*/
   1.113 +	function compile($code, $no_echo = false, $echo_var = '')
   1.114 +	{
   1.115 +		global $config;
   1.116 +
   1.117 +		if ($echo_var)
   1.118 +		{
   1.119 +			global $$echo_var;
   1.120 +		}
   1.121 +
   1.122 +		// Remove any "loose" php ... we want to give admins the ability
   1.123 +		// to switch on/off PHP for a given template. Allowing unchecked
   1.124 +		// php is a no-no. There is a potential issue here in that non-php
   1.125 +		// content may be removed ... however designers should use entities
   1.126 +		// if they wish to display < and >
   1.127 +		$this->remove_php_tags($code);
   1.128 +
   1.129 +		// Pull out all block/statement level elements and separate plain text
   1.130 +		preg_match_all('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', $code, $matches);
   1.131 +		$php_blocks = $matches[1];
   1.132 +		$code = preg_replace('#<!-- PHP -->.*?<!-- ENDPHP -->#s', '<!-- PHP -->', $code);
   1.133 +
   1.134 +		preg_match_all('#<!-- INCLUDE (\{\$?[A-Z0-9\-_]+\}|[a-zA-Z0-9\_\-\+\./]+) -->#', $code, $matches);
   1.135 +		$include_blocks = $matches[1];
   1.136 +		$code = preg_replace('#<!-- INCLUDE (?:\{\$?[A-Z0-9\-_]+\}|[a-zA-Z0-9\_\-\+\./]+) -->#', '<!-- INCLUDE -->', $code);
   1.137 +
   1.138 +		preg_match_all('#<!-- INCLUDEPHP ([a-zA-Z0-9\_\-\+\./]+) -->#', $code, $matches);
   1.139 +		$includephp_blocks = $matches[1];
   1.140 +		$code = preg_replace('#<!-- INCLUDEPHP [a-zA-Z0-9\_\-\+\./]+ -->#', '<!-- INCLUDEPHP -->', $code);
   1.141 +
   1.142 +		preg_match_all('#<!-- ([^<].*?) (.*?)? ?-->#', $code, $blocks, PREG_SET_ORDER);
   1.143 +
   1.144 +		$text_blocks = preg_split('#<!-- [^<].*? (?:.*?)? ?-->#', $code);
   1.145 +
   1.146 +		for ($i = 0, $j = sizeof($text_blocks); $i < $j; $i++)
   1.147 +		{
   1.148 +			$this->compile_var_tags($text_blocks[$i]);
   1.149 +		}
   1.150 +		$compile_blocks = array();
   1.151 +
   1.152 +		for ($curr_tb = 0, $tb_size = sizeof($blocks); $curr_tb < $tb_size; $curr_tb++)
   1.153 +		{
   1.154 +			$block_val = &$blocks[$curr_tb];
   1.155 +
   1.156 +			switch ($block_val[1])
   1.157 +			{
   1.158 +				case 'BEGIN':
   1.159 +					$this->block_else_level[] = false;
   1.160 +					$compile_blocks[] = '<?php ' . $this->compile_tag_block($block_val[2]) . ' ?>';
   1.161 +				break;
   1.162 +
   1.163 +				case 'BEGINELSE':
   1.164 +					$this->block_else_level[sizeof($this->block_else_level) - 1] = true;
   1.165 +					$compile_blocks[] = '<?php }} else { ?>';
   1.166 +				break;
   1.167 +
   1.168 +				case 'END':
   1.169 +					array_pop($this->block_names);
   1.170 +					$compile_blocks[] = '<?php ' . ((array_pop($this->block_else_level)) ? '}' : '}}') . ' ?>';
   1.171 +				break;
   1.172 +
   1.173 +				case 'IF':
   1.174 +					$compile_blocks[] = '<?php ' . $this->compile_tag_if($block_val[2], false) . ' ?>';
   1.175 +				break;
   1.176 +
   1.177 +				case 'ELSE':
   1.178 +					$compile_blocks[] = '<?php } else { ?>';
   1.179 +				break;
   1.180 +
   1.181 +				case 'ELSEIF':
   1.182 +					$compile_blocks[] = '<?php ' . $this->compile_tag_if($block_val[2], true) . ' ?>';
   1.183 +				break;
   1.184 +
   1.185 +				case 'ENDIF':
   1.186 +					$compile_blocks[] = '<?php } ?>';
   1.187 +				break;
   1.188 +
   1.189 +				case 'DEFINE':
   1.190 +					$compile_blocks[] = '<?php ' . $this->compile_tag_define($block_val[2], true) . ' ?>';
   1.191 +				break;
   1.192 +
   1.193 +				case 'UNDEFINE':
   1.194 +					$compile_blocks[] = '<?php ' . $this->compile_tag_define($block_val[2], false) . ' ?>';
   1.195 +				break;
   1.196 +
   1.197 +				case 'INCLUDE':
   1.198 +					$temp = array_shift($include_blocks);
   1.199 +
   1.200 +					// Dynamic includes
   1.201 +					// Cheap match rather than a full blown regexp, we already know
   1.202 +					// the format of the input so just use string manipulation.
   1.203 +					if ($temp[0] == '{')
   1.204 +					{
   1.205 +						$file = false;
   1.206 +
   1.207 +						if ($temp[1] == '$')
   1.208 +						{
   1.209 +							$var = substr($temp, 2, -1);
   1.210 +							//$file = $this->template->_tpldata['DEFINE']['.'][$var];
   1.211 +							$temp = "\$this->_tpldata['DEFINE']['.']['$var']";
   1.212 +						}
   1.213 +						else
   1.214 +						{
   1.215 +							$var = substr($temp, 1, -1);
   1.216 +							//$file = $this->template->_rootref[$var];
   1.217 +							$temp = "\$this->_rootref['$var']";
   1.218 +						}
   1.219 +					}
   1.220 +					else
   1.221 +					{
   1.222 +						$file = $temp;
   1.223 +					}
   1.224 +
   1.225 +					$compile_blocks[] = '<?php ' . $this->compile_tag_include($temp) . ' ?>';
   1.226 +
   1.227 +					// No point in checking variable includes
   1.228 +					if ($file)
   1.229 +					{
   1.230 +						$this->template->_tpl_include($file, false);
   1.231 +					}
   1.232 +				break;
   1.233 +
   1.234 +				case 'INCLUDEPHP':
   1.235 +					$compile_blocks[] = ($config['tpl_allow_php']) ? '<?php ' . $this->compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>' : '';
   1.236 +				break;
   1.237 +
   1.238 +				case 'PHP':
   1.239 +					$compile_blocks[] = ($config['tpl_allow_php']) ? '<?php ' . array_shift($php_blocks) . ' ?>' : '';
   1.240 +				break;
   1.241 +
   1.242 +				default:
   1.243 +					$this->compile_var_tags($block_val[0]);
   1.244 +					$trim_check = trim($block_val[0]);
   1.245 +					$compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $block_val[0] : '') : ((!empty($trim_check)) ? $block_val[0] : '');
   1.246 +				break;
   1.247 +			}
   1.248 +		}
   1.249 +
   1.250 +		$template_php = '';
   1.251 +		for ($i = 0, $size = sizeof($text_blocks); $i < $size; $i++)
   1.252 +		{
   1.253 +			$trim_check_text = trim($text_blocks[$i]);
   1.254 +			$template_php .= (!$no_echo) ? (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '') : (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '');
   1.255 +		}
   1.256 +
   1.257 +		// Remove unused opening/closing tags
   1.258 +		$template_php = str_replace(' ?><?php ', ' ', $template_php);
   1.259 +
   1.260 +		// Now add a newline after each php closing tag which already has a newline
   1.261 +		// PHP itself strips a newline if a closing tag is used (this is documented behaviour) and it is mostly not intended by style authors to remove newlines
   1.262 +		$template_php = preg_replace('#\?\>([\r\n])#', '?>\1\1', $template_php);
   1.263 +
   1.264 +		// There will be a number of occasions where we switch into and out of
   1.265 +		// PHP mode instantaneously. Rather than "burden" the parser with this
   1.266 +		// we'll strip out such occurences, minimising such switching
   1.267 +		if ($no_echo)
   1.268 +		{
   1.269 +			return "\$$echo_var .= '" . $template_php . "'";
   1.270 +		}
   1.271 +
   1.272 +		return $template_php;
   1.273 +	}
   1.274 +
   1.275 +	/**
   1.276 +	* Compile variables
   1.277 +	* @access private
   1.278 +	*/
   1.279 +	function compile_var_tags(&$text_blocks)
   1.280 +	{
   1.281 +		// change template varrefs into PHP varrefs
   1.282 +		$varrefs = array();
   1.283 +
   1.284 +		// This one will handle varrefs WITH namespaces
   1.285 +		preg_match_all('#\{((?:[a-z0-9\-_]+\.)+)(\$)?([A-Z0-9\-_]+)\}#', $text_blocks, $varrefs, PREG_SET_ORDER);
   1.286 +
   1.287 +		foreach ($varrefs as $var_val)
   1.288 +		{
   1.289 +			$namespace = $var_val[1];
   1.290 +			$varname = $var_val[3];
   1.291 +			$new = $this->generate_block_varref($namespace, $varname, true, $var_val[2]);
   1.292 +
   1.293 +			$text_blocks = str_replace($var_val[0], $new, $text_blocks);
   1.294 +		}
   1.295 +
   1.296 +		// This will handle the remaining root-level varrefs
   1.297 +		// transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array
   1.298 +		if (strpos($text_blocks, '{L_') !== false)
   1.299 +		{
   1.300 +			$text_blocks = preg_replace('#\{L_([A-Z0-9\-_]+)\}#', "<?php echo ((isset(\$this->_rootref['L_\\1'])) ? \$this->_rootref['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ \\1 }')); ?>", $text_blocks);
   1.301 +		}
   1.302 +
   1.303 +		// Handle addslashed language variables prefixed with LA_
   1.304 +		// If a template variable already exist, it will be used in favor of it...
   1.305 +		if (strpos($text_blocks, '{LA_') !== false)
   1.306 +		{
   1.307 +			$text_blocks = preg_replace('#\{LA_([A-Z0-9\-_]+)\}#', "<?php echo ((isset(\$this->_rootref['LA_\\1'])) ? \$this->_rootref['LA_\\1'] : ((isset(\$this->_rootref['L_\\1'])) ? addslashes(\$this->_rootref['L_\\1']) : ((isset(\$user->lang['\\1'])) ? addslashes(\$user->lang['\\1']) : '{ \\1 }'))); ?>", $text_blocks);
   1.308 +		}
   1.309 +
   1.310 +		// Handle remaining varrefs
   1.311 +		$text_blocks = preg_replace('#\{([A-Z0-9\-_]+)\}#', "<?php echo (isset(\$this->_rootref['\\1'])) ? \$this->_rootref['\\1'] : ''; ?>", $text_blocks);
   1.312 +		$text_blocks = preg_replace('#\{\$([A-Z0-9\-_]+)\}#', "<?php echo (isset(\$this->_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks);
   1.313 +
   1.314 +		return;
   1.315 +	}
   1.316 +
   1.317 +	/**
   1.318 +	* Compile blocks
   1.319 +	* @access private
   1.320 +	*/
   1.321 +	function compile_tag_block($tag_args)
   1.322 +	{
   1.323 +		$no_nesting = false;
   1.324 +
   1.325 +		// Is the designer wanting to call another loop in a loop?
   1.326 +		if (strpos($tag_args, '!') === 0)
   1.327 +		{
   1.328 +			// Count the number of ! occurrences (not allowed in vars)
   1.329 +			$no_nesting = substr_count($tag_args, '!');
   1.330 +			$tag_args = substr($tag_args, $no_nesting);
   1.331 +		}
   1.332 +
   1.333 +		// Allow for control of looping (indexes start from zero):
   1.334 +		// foo(2)    : Will start the loop on the 3rd entry
   1.335 +		// foo(-2)   : Will start the loop two entries from the end
   1.336 +		// foo(3,4)  : Will start the loop on the fourth entry and end it on the fifth
   1.337 +		// foo(3,-4) : Will start the loop on the fourth entry and end it four from last
   1.338 +		if (preg_match('#^([^()]*)\(([\-\d]+)(?:,([\-\d]+))?\)$#', $tag_args, $match))
   1.339 +		{
   1.340 +			$tag_args = $match[1];
   1.341 +
   1.342 +			if ($match[2] < 0)
   1.343 +			{
   1.344 +				$loop_start = '($_' . $tag_args . '_count ' . $match[2] . ' < 0 ? 0 : $_' . $tag_args . '_count ' . $match[2] . ')';
   1.345 +			}
   1.346 +			else
   1.347 +			{
   1.348 +				$loop_start = '($_' . $tag_args . '_count < ' . $match[2] . ' ? $_' . $tag_args . '_count : ' . $match[2] . ')';
   1.349 +			}
   1.350 +
   1.351 +			if (strlen($match[3]) < 1 || $match[3] == -1)
   1.352 +			{
   1.353 +				$loop_end = '$_' . $tag_args . '_count';
   1.354 +			}
   1.355 +			else if ($match[3] >= 0)
   1.356 +			{
   1.357 +				$loop_end = '(' . ($match[3] + 1) . ' > $_' . $tag_args . '_count ? $_' . $tag_args . '_count : ' . ($match[3] + 1) . ')';
   1.358 +			}
   1.359 +			else //if ($match[3] < -1)
   1.360 +			{
   1.361 +				$loop_end = '$_' . $tag_args . '_count' . ($match[3] + 1);
   1.362 +			}
   1.363 +		}
   1.364 +		else
   1.365 +		{
   1.366 +			$loop_start = 0;
   1.367 +			$loop_end = '$_' . $tag_args . '_count';
   1.368 +		}
   1.369 +
   1.370 +		$tag_template_php = '';
   1.371 +		array_push($this->block_names, $tag_args);
   1.372 +
   1.373 +		if ($no_nesting !== false)
   1.374 +		{
   1.375 +			// We need to implode $no_nesting times from the end...
   1.376 +			$block = array_slice($this->block_names, -$no_nesting);
   1.377 +		}
   1.378 +		else
   1.379 +		{
   1.380 +			$block = $this->block_names;
   1.381 +		}
   1.382 +
   1.383 +		if (sizeof($block) < 2)
   1.384 +		{
   1.385 +			// Block is not nested.
   1.386 +			$tag_template_php = '$_' . $tag_args . "_count = (isset(\$this->_tpldata['$tag_args'])) ? sizeof(\$this->_tpldata['$tag_args']) : 0;";
   1.387 +			$varref = "\$this->_tpldata['$tag_args']";
   1.388 +		}
   1.389 +		else
   1.390 +		{
   1.391 +			// This block is nested.
   1.392 +			// Generate a namespace string for this block.
   1.393 +			$namespace = implode('.', $block);
   1.394 +
   1.395 +			// Get a reference to the data array for this block that depends on the
   1.396 +			// current indices of all parent blocks.
   1.397 +			$varref = $this->generate_block_data_ref($namespace, false);
   1.398 +
   1.399 +			// Create the for loop code to iterate over this block.
   1.400 +			$tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;';
   1.401 +		}
   1.402 +
   1.403 +		$tag_template_php .= 'if ($_' . $tag_args . '_count) {';
   1.404 +
   1.405 +		/**
   1.406 +		* The following uses foreach for iteration instead of a for loop, foreach is faster but requires PHP to make a copy of the contents of the array which uses more memory
   1.407 +		* <code>
   1.408 +		*	if (!$offset)
   1.409 +		*	{
   1.410 +		*		$tag_template_php .= 'foreach (' . $varref . ' as $_' . $tag_args . '_i => $_' . $tag_args . '_val){';
   1.411 +		*	}
   1.412 +		* </code>
   1.413 +		*/
   1.414 +
   1.415 +		$tag_template_php .= 'for ($_' . $tag_args . '_i = ' . $loop_start . '; $_' . $tag_args . '_i < ' . $loop_end . '; ++$_' . $tag_args . '_i){';
   1.416 +		$tag_template_php .= '$_'. $tag_args . '_val = &' . $varref . '[$_'. $tag_args. '_i];';
   1.417 +
   1.418 +		return $tag_template_php;
   1.419 +	}
   1.420 +
   1.421 +	/**
   1.422 +	* Compile IF tags - much of this is from Smarty with
   1.423 +	* some adaptions for our block level methods
   1.424 +	* @access private
   1.425 +	*/
   1.426 +	function compile_tag_if($tag_args, $elseif)
   1.427 +	{
   1.428 +		// Tokenize args for 'if' tag.
   1.429 +		preg_match_all('/(?:
   1.430 +			"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"         |
   1.431 +			\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     |
   1.432 +			[(),]                                  |
   1.433 +			[^\s(),]+)/x', $tag_args, $match);
   1.434 +
   1.435 +		$tokens = $match[0];
   1.436 +		$is_arg_stack = array();
   1.437 +
   1.438 +		for ($i = 0, $size = sizeof($tokens); $i < $size; $i++)
   1.439 +		{
   1.440 +			$token = &$tokens[$i];
   1.441 +
   1.442 +			switch ($token)
   1.443 +			{
   1.444 +				case '!==':
   1.445 +				case '===':
   1.446 +				case '<<':
   1.447 +				case '>>':
   1.448 +				case '|':
   1.449 +				case '^':
   1.450 +				case '&':
   1.451 +				case '~':
   1.452 +				case ')':
   1.453 +				case ',':
   1.454 +				case '+':
   1.455 +				case '-':
   1.456 +				case '*':
   1.457 +				case '/':
   1.458 +				case '@':
   1.459 +				break;
   1.460 +
   1.461 +				case '==':
   1.462 +				case 'eq':
   1.463 +					$token = '==';
   1.464 +				break;
   1.465 +
   1.466 +				case '!=':
   1.467 +				case '<>':
   1.468 +				case 'ne':
   1.469 +				case 'neq':
   1.470 +					$token = '!=';
   1.471 +				break;
   1.472 +
   1.473 +				case '<':
   1.474 +				case 'lt':
   1.475 +					$token = '<';
   1.476 +				break;
   1.477 +
   1.478 +				case '<=':
   1.479 +				case 'le':
   1.480 +				case 'lte':
   1.481 +					$token = '<=';
   1.482 +				break;
   1.483 +
   1.484 +				case '>':
   1.485 +				case 'gt':
   1.486 +					$token = '>';
   1.487 +				break;
   1.488 +
   1.489 +				case '>=':
   1.490 +				case 'ge':
   1.491 +				case 'gte':
   1.492 +					$token = '>=';
   1.493 +				break;
   1.494 +
   1.495 +				case '&&':
   1.496 +				case 'and':
   1.497 +					$token = '&&';
   1.498 +				break;
   1.499 +
   1.500 +				case '||':
   1.501 +				case 'or':
   1.502 +					$token = '||';
   1.503 +				break;
   1.504 +
   1.505 +				case '!':
   1.506 +				case 'not':
   1.507 +					$token = '!';
   1.508 +				break;
   1.509 +
   1.510 +				case '%':
   1.511 +				case 'mod':
   1.512 +					$token = '%';
   1.513 +				break;
   1.514 +
   1.515 +				case '(':
   1.516 +					array_push($is_arg_stack, $i);
   1.517 +				break;
   1.518 +
   1.519 +				case 'is':
   1.520 +					$is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1;
   1.521 +					$is_arg	= implode('	', array_slice($tokens,	$is_arg_start, $i -	$is_arg_start));
   1.522 +
   1.523 +					$new_tokens	= $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
   1.524 +
   1.525 +					array_splice($tokens, $is_arg_start, sizeof($tokens), $new_tokens);
   1.526 +
   1.527 +					$i = $is_arg_start;
   1.528 +
   1.529 +				// no break
   1.530 +
   1.531 +				default:
   1.532 +					if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs))
   1.533 +					{
   1.534 +						$token = (!empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']' : (($varrefs[2]) ? '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$this->_rootref[\'' . $varrefs[3] . '\']');
   1.535 +					}
   1.536 +					else if (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs))
   1.537 +					{
   1.538 +						// Allow checking if loops are set with .loopname
   1.539 +						// It is also possible to check the loop count by doing <!-- IF .loopname > 1 --> for example
   1.540 +						$blocks = explode('.', $varrefs[1]);
   1.541 +
   1.542 +						// If the block is nested, we have a reference that we can grab.
   1.543 +						// If the block is not nested, we just go and grab the block from _tpldata
   1.544 +						if (sizeof($blocks) > 1)
   1.545 +						{
   1.546 +							$block = array_pop($blocks);
   1.547 +							$namespace = implode('.', $blocks);
   1.548 +							$varref = $this->generate_block_data_ref($namespace, true);
   1.549 +
   1.550 +							// Add the block reference for the last child.
   1.551 +							$varref .= "['" . $block . "']";
   1.552 +						}
   1.553 +						else
   1.554 +						{
   1.555 +							$varref = '$this->_tpldata';
   1.556 +
   1.557 +							// Add the block reference for the last child.
   1.558 +							$varref .= "['" . $blocks[0] . "']";
   1.559 +						}
   1.560 +						$token = "sizeof($varref)";
   1.561 +					}
   1.562 +					else if (!empty($token))
   1.563 +					{
   1.564 +						$token = '(' . $token . ')';
   1.565 +					}
   1.566 +
   1.567 +				break;
   1.568 +			}
   1.569 +		}
   1.570 +
   1.571 +		// If there are no valid tokens left or only control/compare characters left, we do skip this statement
   1.572 +		if (!sizeof($tokens) || str_replace(array(' ', '=', '!', '<', '>', '&', '|', '%', '(', ')'), '', implode('', $tokens)) == '')
   1.573 +		{
   1.574 +			$tokens = array('false');
   1.575 +		}
   1.576 +		return (($elseif) ? '} else if (' : 'if (') . (implode(' ', $tokens) . ') { ');
   1.577 +	}
   1.578 +
   1.579 +	/**
   1.580 +	* Compile DEFINE tags
   1.581 +	* @access private
   1.582 +	*/
   1.583 +	function compile_tag_define($tag_args, $op)
   1.584 +	{
   1.585 +		preg_match('#^((?:[a-z0-9\-_]+\.)+)?\$(?=[A-Z])([A-Z0-9_\-]*)(?: = (\'?)([^\']*)(\'?))?$#', $tag_args, $match);
   1.586 +
   1.587 +		if (empty($match[2]) || (!isset($match[4]) && $op))
   1.588 +		{
   1.589 +			return '';
   1.590 +		}
   1.591 +
   1.592 +		if (!$op)
   1.593 +		{
   1.594 +			return 'unset(' . (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ');';
   1.595 +		}
   1.596 +
   1.597 +		// Are we a string?
   1.598 +		if ($match[3] && $match[5])
   1.599 +		{
   1.600 +			$match[4] = str_replace(array('\\\'', '\\\\', '\''), array('\'', '\\', '\\\''), $match[4]);
   1.601 +
   1.602 +			// Compile reference, we allow template variables in defines...
   1.603 +			$match[4] = $this->compile($match[4]);
   1.604 +
   1.605 +			// Now replace the php code
   1.606 +			$match[4] = "'" . str_replace(array('<?php echo ', '; ?>'), array("' . ", " . '"), $match[4]) . "'";
   1.607 +		}
   1.608 +		else
   1.609 +		{
   1.610 +			preg_match('#true|false|\.#i', $match[4], $type);
   1.611 +
   1.612 +			switch (strtolower($type[0]))
   1.613 +			{
   1.614 +				case 'true':
   1.615 +				case 'false':
   1.616 +					$match[4] = strtoupper($match[4]);
   1.617 +				break;
   1.618 +
   1.619 +				case '.':
   1.620 +					$match[4] = doubleval($match[4]);
   1.621 +				break;
   1.622 +
   1.623 +				default:
   1.624 +					$match[4] = intval($match[4]);
   1.625 +				break;
   1.626 +			}
   1.627 +		}
   1.628 +
   1.629 +		return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ' = ' . $match[4] . ';';
   1.630 +	}
   1.631 +
   1.632 +	/**
   1.633 +	* Compile INCLUDE tag
   1.634 +	* @access private
   1.635 +	*/
   1.636 +	function compile_tag_include($tag_args)
   1.637 +	{
   1.638 +		// Process dynamic includes
   1.639 +		if ($tag_args[0] == '$')
   1.640 +		{
   1.641 +			return "if (isset($tag_args)) { \$this->_tpl_include($tag_args); }";
   1.642 +		}
   1.643 +
   1.644 +		return "\$this->_tpl_include('$tag_args');";
   1.645 +	}
   1.646 +
   1.647 +	/**
   1.648 +	* Compile INCLUDE_PHP tag
   1.649 +	* @access private
   1.650 +	*/
   1.651 +	function compile_tag_include_php($tag_args)
   1.652 +	{
   1.653 +		return "\$this->_php_include('$tag_args');";
   1.654 +	}
   1.655 +
   1.656 +	/**
   1.657 +	* parse expression
   1.658 +	* This is from Smarty
   1.659 +	* @access private
   1.660 +	*/
   1.661 +	function _parse_is_expr($is_arg, $tokens)
   1.662 +	{
   1.663 +		$expr_end = 0;
   1.664 +		$negate_expr = false;
   1.665 +
   1.666 +		if (($first_token = array_shift($tokens)) == 'not')
   1.667 +		{
   1.668 +			$negate_expr = true;
   1.669 +			$expr_type = array_shift($tokens);
   1.670 +		}
   1.671 +		else
   1.672 +		{
   1.673 +			$expr_type = $first_token;
   1.674 +		}
   1.675 +
   1.676 +		switch ($expr_type)
   1.677 +		{
   1.678 +			case 'even':
   1.679 +				if (@$tokens[$expr_end] == 'by')
   1.680 +				{
   1.681 +					$expr_end++;
   1.682 +					$expr_arg = $tokens[$expr_end++];
   1.683 +					$expr = "!(($is_arg / $expr_arg) % $expr_arg)";
   1.684 +				}
   1.685 +				else
   1.686 +				{
   1.687 +					$expr = "!($is_arg & 1)";
   1.688 +				}
   1.689 +			break;
   1.690 +
   1.691 +			case 'odd':
   1.692 +				if (@$tokens[$expr_end] == 'by')
   1.693 +				{
   1.694 +					$expr_end++;
   1.695 +					$expr_arg = $tokens[$expr_end++];
   1.696 +					$expr = "(($is_arg / $expr_arg) % $expr_arg)";
   1.697 +				}
   1.698 +				else
   1.699 +				{
   1.700 +					$expr = "($is_arg & 1)";
   1.701 +				}
   1.702 +			break;
   1.703 +
   1.704 +			case 'div':
   1.705 +				if (@$tokens[$expr_end] == 'by')
   1.706 +				{
   1.707 +					$expr_end++;
   1.708 +					$expr_arg = $tokens[$expr_end++];
   1.709 +					$expr = "!($is_arg % $expr_arg)";
   1.710 +				}
   1.711 +			break;
   1.712 +		}
   1.713 +
   1.714 +		if ($negate_expr)
   1.715 +		{
   1.716 +			$expr = "!($expr)";
   1.717 +		}
   1.718 +
   1.719 +		array_splice($tokens, 0, $expr_end, $expr);
   1.720 +
   1.721 +		return $tokens;
   1.722 +	}
   1.723 +
   1.724 +	/**
   1.725 +	* Generates a reference to the given variable inside the given (possibly nested)
   1.726 +	* block namespace. This is a string of the form:
   1.727 +	* ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '
   1.728 +	* It's ready to be inserted into an "echo" line in one of the templates.
   1.729 +	* NOTE: expects a trailing "." on the namespace.
   1.730 +	* @access private
   1.731 +	*/
   1.732 +	function generate_block_varref($namespace, $varname, $echo = true, $defop = false)
   1.733 +	{
   1.734 +		// Strip the trailing period.
   1.735 +		$namespace = substr($namespace, 0, -1);
   1.736 +
   1.737 +		// Get a reference to the data block for this namespace.
   1.738 +		$varref = $this->generate_block_data_ref($namespace, true, $defop);
   1.739 +		// Prepend the necessary code to stick this in an echo line.
   1.740 +
   1.741 +		// Append the variable reference.
   1.742 +		$varref .= "['$varname']";
   1.743 +		$varref = ($echo) ? "<?php echo $varref; ?>" : ((isset($varref)) ? $varref : '');
   1.744 +
   1.745 +		return $varref;
   1.746 +	}
   1.747 +
   1.748 +	/**
   1.749 +	* Generates a reference to the array of data values for the given
   1.750 +	* (possibly nested) block namespace. This is a string of the form:
   1.751 +	* $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN']
   1.752 +	*
   1.753 +	* If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above.
   1.754 +	* NOTE: does not expect a trailing "." on the blockname.
   1.755 +	* @access private
   1.756 +	*/
   1.757 +	function generate_block_data_ref($blockname, $include_last_iterator, $defop = false)
   1.758 +	{
   1.759 +		// Get an array of the blocks involved.
   1.760 +		$blocks = explode('.', $blockname);
   1.761 +		$blockcount = sizeof($blocks) - 1;
   1.762 +
   1.763 +		// DEFINE is not an element of any referenced variable, we must use _tpldata to access it
   1.764 +		if ($defop)
   1.765 +		{
   1.766 +			$varref = '$this->_tpldata[\'DEFINE\']';
   1.767 +			// Build up the string with everything but the last child.
   1.768 +			for ($i = 0; $i < $blockcount; $i++)
   1.769 +			{
   1.770 +				$varref .= "['" . $blocks[$i] . "'][\$_" . $blocks[$i] . '_i]';
   1.771 +			}
   1.772 +			// Add the block reference for the last child.
   1.773 +			$varref .= "['" . $blocks[$blockcount] . "']";
   1.774 +			// Add the iterator for the last child if requried.
   1.775 +			if ($include_last_iterator)
   1.776 +			{
   1.777 +				$varref .= '[$_' . $blocks[$blockcount] . '_i]';
   1.778 +			}
   1.779 +			return $varref;
   1.780 +		}
   1.781 +		else if ($include_last_iterator)
   1.782 +		{
   1.783 +			return '$_'. $blocks[$blockcount] . '_val';
   1.784 +		}
   1.785 +		else
   1.786 +		{
   1.787 +			return '$_'. $blocks[$blockcount - 1] . '_val[\''. $blocks[$blockcount]. '\']';
   1.788 +		}
   1.789 +	}
   1.790 +
   1.791 +	/**
   1.792 +	* Write compiled file to cache directory
   1.793 +	* @access private
   1.794 +	*/
   1.795 +	function compile_write($handle, $data)
   1.796 +	{
   1.797 +		global $phpEx;
   1.798 +
   1.799 +		$filename = $this->template->cachepath . str_replace('/', '.', $this->template->filename[$handle]) . '.' . $phpEx;
   1.800 +
   1.801 +		$data = "<?php if (!defined('IN_PHPBB')) exit;" . ((strpos($data, '<?php') === 0) ? substr($data, 5) : ' ?>' . $data);
   1.802 +
   1.803 +		if ($fp = @fopen($filename, 'wb'))
   1.804 +		{
   1.805 +			@flock($fp, LOCK_EX);
   1.806 +			@fwrite ($fp, $data);
   1.807 +			@flock($fp, LOCK_UN);
   1.808 +			@fclose($fp);
   1.809 +
   1.810 +			phpbb_chmod($filename, CHMOD_READ | CHMOD_WRITE);
   1.811 +		}
   1.812 +
   1.813 +		return;
   1.814 +	}
   1.815 +}
   1.816 +
   1.817 +?>
   1.818 \ No newline at end of file