Blame Extras/phpBB/3.0.4/includes/functions_template.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
* @version $Id: functions_template.php 8813 2008-09-04 11:52:01Z aptx $
4c79b5
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Extension of template class - Functions needed for compiling templates only.
4c79b5
*
4c79b5
* psoTFX, phpBB Development Team - Completion of file caching, decompilation
4c79b5
* routines and implementation of conditionals/keywords and associated changes
4c79b5
*
4c79b5
* The interface was inspired by PHPLib templates,  and the template file (formats are
4c79b5
* quite similar)
4c79b5
*
4c79b5
* The keyword/conditional implementation is currently based on sections of code from
4c79b5
* the Smarty templating engine (c) 2001 ispi of Lincoln, Inc. which is released
4c79b5
* (on its own and in whole) under the LGPL. Section 3 of the LGPL states that any code
4c79b5
* derived from an LGPL application may be relicenced under the GPL, this applies
4c79b5
* to this source
4c79b5
*
4c79b5
* DEFINE directive inspired by a request by Cyberalien
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
*/
4c79b5
class template_compile
4c79b5
{
4c79b5
	var $template;
4c79b5
4c79b5
	// Various storage arrays
4c79b5
	var $block_names = array();
4c79b5
	var $block_else_level = array();
4c79b5
4c79b5
	/**
4c79b5
	* constuctor
4c79b5
	*/
4c79b5
	function template_compile(&$template)
4c79b5
	{
4c79b5
		$this->template = &$template;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Load template source from file
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function _tpl_load_file($handle, $store_in_db = false)
4c79b5
	{
4c79b5
		// Try and open template for read
4c79b5
		if (!file_exists($this->template->files[$handle]))
4c79b5
		{
4c79b5
			trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
4c79b5
		}
4c79b5
4c79b5
		$this->template->compiled_code[$handle] = $this->compile(trim(@file_get_contents($this->template->files[$handle])));
4c79b5
4c79b5
		// Actually compile the code now.
4c79b5
		$this->compile_write($handle, $this->template->compiled_code[$handle]);
4c79b5
4c79b5
		// Store in database if required...
4c79b5
		if ($store_in_db)
4c79b5
		{
4c79b5
			global $db, $user;
4c79b5
4c79b5
			$sql_ary = array(
4c79b5
				'template_id'			=> $this->template->files_template[$handle],
4c79b5
				'template_filename'		=> $this->template->filename[$handle],
4c79b5
				'template_included'		=> '',
4c79b5
				'template_mtime'		=> time(),
4c79b5
				'template_data'			=> trim(@file_get_contents($this->template->files[$handle])),
4c79b5
			);
4c79b5
4c79b5
			$sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Remove any PHP tags that do not belong, these regular expressions are derived from
4c79b5
	* the ones that exist in zend_language_scanner.l
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function remove_php_tags(&$code)
4c79b5
	{
4c79b5
		// This matches the information gathered from the internal PHP lexer
4c79b5
		$match = array(
4c79b5
			'#<([\?%])=?.*?\1>#s',
4c79b5
			'#<script\s+language\s*=\s*(["\']?)php\1\s*>.*?</script\s*>#s',
4c79b5
			'#<\?php(?:\r\n?|[ \n\t]).*?\?>#s'
4c79b5
		);
4c79b5
4c79b5
		$code = preg_replace($match, '', $code);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* The all seeing all doing compile method. Parts are inspired by or directly from Smarty
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile($code, $no_echo = false, $echo_var = '')
4c79b5
	{
4c79b5
		global $config;
4c79b5
4c79b5
		if ($echo_var)
4c79b5
		{
4c79b5
			global $$echo_var;
4c79b5
		}
4c79b5
4c79b5
		// Remove any "loose" php ... we want to give admins the ability
4c79b5
		// to switch on/off PHP for a given template. Allowing unchecked
4c79b5
		// php is a no-no. There is a potential issue here in that non-php
4c79b5
		// content may be removed ... however designers should use entities
4c79b5
		// if they wish to display < and >
4c79b5
		$this->remove_php_tags($code);
4c79b5
4c79b5
		// Pull out all block/statement level elements and separate plain text
4c79b5
		preg_match_all('#(.*?)#s', $code, $matches);
4c79b5
		$php_blocks = $matches[1];
4c79b5
		$code = preg_replace('#.*?#s', '', $code);
4c79b5
4c79b5
		preg_match_all('##', $code, $matches);
4c79b5
		$include_blocks = $matches[1];
4c79b5
		$code = preg_replace('##', '', $code);
4c79b5
4c79b5
		preg_match_all('##', $code, $matches);
4c79b5
		$includephp_blocks = $matches[1];
4c79b5
		$code = preg_replace('##', '', $code);
4c79b5
4c79b5
		preg_match_all('##', $code, $blocks, PREG_SET_ORDER);
4c79b5
4c79b5
		$text_blocks = preg_split('##', $code);
4c79b5
4c79b5
		for ($i = 0, $j = sizeof($text_blocks); $i < $j; $i++)
4c79b5
		{
4c79b5
			$this->compile_var_tags($text_blocks[$i]);
4c79b5
		}
4c79b5
		$compile_blocks = array();
4c79b5
4c79b5
		for ($curr_tb = 0, $tb_size = sizeof($blocks); $curr_tb < $tb_size; $curr_tb++)
4c79b5
		{
4c79b5
			$block_val = &$blocks[$curr_tb];
4c79b5
4c79b5
			switch ($block_val[1])
4c79b5
			{
4c79b5
				case 'BEGIN':
4c79b5
					$this->block_else_level[] = false;
4c79b5
					$compile_blocks[] = 'compile_tag_block($block_val[2]) . ' ?>';
4c79b5
				break;
4c79b5
4c79b5
				case 'BEGINELSE':
4c79b5
					$this->block_else_level[sizeof($this->block_else_level) - 1] = true;
4c79b5
					$compile_blocks[] = '';
4c79b5
				break;
4c79b5
4c79b5
				case 'END':
4c79b5
					array_pop($this->block_names);
4c79b5
					$compile_blocks[] = 'block_else_level)) ? '}' : '}}') . ' ?>';
4c79b5
				break;
4c79b5
4c79b5
				case 'IF':
4c79b5
					$compile_blocks[] = 'compile_tag_if($block_val[2], false) . ' ?>';
4c79b5
				break;
4c79b5
4c79b5
				case 'ELSE':
4c79b5
					$compile_blocks[] = '';
4c79b5
				break;
4c79b5
4c79b5
				case 'ELSEIF':
4c79b5
					$compile_blocks[] = 'compile_tag_if($block_val[2], true) . ' ?>';
4c79b5
				break;
4c79b5
4c79b5
				case 'ENDIF':
4c79b5
					$compile_blocks[] = '';
4c79b5
				break;
4c79b5
4c79b5
				case 'DEFINE':
4c79b5
					$compile_blocks[] = 'compile_tag_define($block_val[2], true) . ' ?>';
4c79b5
				break;
4c79b5
4c79b5
				case 'UNDEFINE':
4c79b5
					$compile_blocks[] = 'compile_tag_define($block_val[2], false) . ' ?>';
4c79b5
				break;
4c79b5
4c79b5
				case 'INCLUDE':
4c79b5
					$temp = array_shift($include_blocks);
4c79b5
					$compile_blocks[] = 'compile_tag_include($temp) . ' ?>';
4c79b5
					$this->template->_tpl_include($temp, false);
4c79b5
				break;
4c79b5
4c79b5
				case 'INCLUDEPHP':
4c79b5
					$compile_blocks[] = ($config['tpl_allow_php']) ? 'compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>' : '';
4c79b5
				break;
4c79b5
4c79b5
				case 'PHP':
4c79b5
					$compile_blocks[] = ($config['tpl_allow_php']) ? '' : '';
4c79b5
				break;
4c79b5
4c79b5
				default:
4c79b5
					$this->compile_var_tags($block_val[0]);
4c79b5
					$trim_check = trim($block_val[0]);
4c79b5
					$compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $block_val[0] : '') : ((!empty($trim_check)) ? $block_val[0] : '');
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$template_php = '';
4c79b5
		for ($i = 0, $size = sizeof($text_blocks); $i < $size; $i++)
4c79b5
		{
4c79b5
			$trim_check_text = trim($text_blocks[$i]);
4c79b5
			$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] : '');
4c79b5
		}
4c79b5
4c79b5
		// There will be a number of occasions where we switch into and out of
4c79b5
		// PHP mode instantaneously. Rather than "burden" the parser with this
4c79b5
		// we'll strip out such occurences, minimising such switching
4c79b5
		$template_php = str_replace(' ?>
4c79b5
4c79b5
		return (!$no_echo) ? $template_php : "\$$echo_var .= '" . $template_php . "'";
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Compile variables
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile_var_tags(&$text_blocks)
4c79b5
	{
4c79b5
		// change template varrefs into PHP varrefs
4c79b5
		$varrefs = array();
4c79b5
4c79b5
		// This one will handle varrefs WITH namespaces
4c79b5
		preg_match_all('#\{((?:[a-z0-9\-_]+\.)+)(\$)?([A-Z0-9\-_]+)\}#', $text_blocks, $varrefs, PREG_SET_ORDER);
4c79b5
4c79b5
		foreach ($varrefs as $var_val)
4c79b5
		{
4c79b5
			$namespace = $var_val[1];
4c79b5
			$varname = $var_val[3];
4c79b5
			$new = $this->generate_block_varref($namespace, $varname, true, $var_val[2]);
4c79b5
4c79b5
			$text_blocks = str_replace($var_val[0], $new, $text_blocks);
4c79b5
		}
4c79b5
4c79b5
		// This will handle the remaining root-level varrefs
4c79b5
		// transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array
4c79b5
		if (strpos($text_blocks, '{L_') !== false)
4c79b5
		{
4c79b5
			$text_blocks = preg_replace('#\{L_([a-z0-9\-_]*)\}#is', "_rootref['L_\\1'])) ? \$this->_rootref['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ \\1 }')); ?>", $text_blocks);
4c79b5
		}
4c79b5
4c79b5
		// Handle addslashed language variables prefixed with LA_
4c79b5
		// If a template variable already exist, it will be used in favor of it...
4c79b5
		if (strpos($text_blocks, '{LA_') !== false)
4c79b5
		{
4c79b5
			$text_blocks = preg_replace('#\{LA_([a-z0-9\-_]*)\}#is', "_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);
4c79b5
		}
4c79b5
4c79b5
		// Handle remaining varrefs
4c79b5
		$text_blocks = preg_replace('#\{([a-z0-9\-_]+)\}#is', "_rootref['\\1'])) ? \$this->_rootref['\\1'] : ''; ?>", $text_blocks);
4c79b5
		$text_blocks = preg_replace('#\{\$([a-z0-9\-_]+)\}#is', "_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks);
4c79b5
4c79b5
		return;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Compile blocks
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile_tag_block($tag_args)
4c79b5
	{
4c79b5
		$no_nesting = false;
4c79b5
4c79b5
		// Is the designer wanting to call another loop in a loop?
4c79b5
		if (strpos($tag_args, '!') === 0)
4c79b5
		{
4c79b5
			// Count the number if ! occurrences (not allowed in vars)
4c79b5
			$no_nesting = substr_count($tag_args, '!');
4c79b5
			$tag_args = substr($tag_args, $no_nesting);
4c79b5
		}
4c79b5
4c79b5
		// Allow for control of looping (indexes start from zero):
4c79b5
		// foo(2)    : Will start the loop on the 3rd entry
4c79b5
		// foo(-2)   : Will start the loop two entries from the end
4c79b5
		// foo(3,4)  : Will start the loop on the fourth entry and end it on the fifth
4c79b5
		// foo(3,-4) : Will start the loop on the fourth entry and end it four from last
4c79b5
		if (preg_match('#^([^()]*)\(([\-\d]+)(?:,([\-\d]+))?\)$#', $tag_args, $match))
4c79b5
		{
4c79b5
			$tag_args = $match[1];
4c79b5
4c79b5
			if ($match[2] < 0)
4c79b5
			{
4c79b5
				$loop_start = '($_' . $tag_args . '_count ' . $match[2] . ' < 0 ? 0 : $_' . $tag_args . '_count ' . $match[2] . ')';
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$loop_start = '($_' . $tag_args . '_count < ' . $match[2] . ' ? $_' . $tag_args . '_count : ' . $match[2] . ')';
4c79b5
			}
4c79b5
4c79b5
			if (strlen($match[3]) < 1 || $match[3] == -1)
4c79b5
			{
4c79b5
				$loop_end = '$_' . $tag_args . '_count';
4c79b5
			}
4c79b5
			else if ($match[3] >= 0)
4c79b5
			{
4c79b5
				$loop_end = '(' . ($match[3] + 1) . ' > $_' . $tag_args . '_count ? $_' . $tag_args . '_count : ' . ($match[3] + 1) . ')';
4c79b5
			}
4c79b5
			else //if ($match[3] < -1)
4c79b5
			{
4c79b5
				$loop_end = '$_' . $tag_args . '_count' . ($match[3] + 1);
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$loop_start = 0;
4c79b5
			$loop_end = '$_' . $tag_args . '_count';
4c79b5
		}
4c79b5
4c79b5
		$tag_template_php = '';
4c79b5
		array_push($this->block_names, $tag_args);
4c79b5
4c79b5
		if ($no_nesting !== false)
4c79b5
		{
4c79b5
			// We need to implode $no_nesting times from the end...
4c79b5
			$block = array_slice($this->block_names, -$no_nesting);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$block = $this->block_names;
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($block) < 2)
4c79b5
		{
4c79b5
			// Block is not nested.
4c79b5
			$tag_template_php = '$_' . $tag_args . "_count = (isset(\$this->_tpldata['$tag_args'])) ? sizeof(\$this->_tpldata['$tag_args']) : 0;";
4c79b5
			$varref = "\$this->_tpldata['$tag_args']";
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			// This block is nested.
4c79b5
			// Generate a namespace string for this block.
4c79b5
			$namespace = implode('.', $block);
4c79b5
4c79b5
			// Get a reference to the data array for this block that depends on the
4c79b5
			// current indices of all parent blocks.
4c79b5
			$varref = $this->generate_block_data_ref($namespace, false);
4c79b5
4c79b5
			// Create the for loop code to iterate over this block.
4c79b5
			$tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;';
4c79b5
		}
4c79b5
4c79b5
		$tag_template_php .= 'if ($_' . $tag_args . '_count) {';
4c79b5
4c79b5
		/**
4c79b5
		* 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
4c79b5
		* 
4c79b5
		*	if (!$offset)
4c79b5
		*	{
4c79b5
		*		$tag_template_php .= 'foreach (' . $varref . ' as $_' . $tag_args . '_i => $_' . $tag_args . '_val){';
4c79b5
		*	}
4c79b5
		* 
4c79b5
		*/
4c79b5
4c79b5
		$tag_template_php .= 'for ($_' . $tag_args . '_i = ' . $loop_start . '; $_' . $tag_args . '_i < ' . $loop_end . '; ++$_' . $tag_args . '_i){';
4c79b5
		$tag_template_php .= '$_'. $tag_args . '_val = &' . $varref . '[$_'. $tag_args. '_i];';
4c79b5
4c79b5
		return $tag_template_php;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Compile IF tags - much of this is from Smarty with
4c79b5
	* some adaptions for our block level methods
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile_tag_if($tag_args, $elseif)
4c79b5
	{
4c79b5
		// Tokenize args for 'if' tag.
4c79b5
		preg_match_all('/(?:
4c79b5
			"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"         |
4c79b5
			\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     |
4c79b5
			[(),]                                  |
4c79b5
			[^\s(),]+)/x', $tag_args, $match);
4c79b5
4c79b5
		$tokens = $match[0];
4c79b5
		$is_arg_stack = array();
4c79b5
4c79b5
		for ($i = 0, $size = sizeof($tokens); $i < $size; $i++)
4c79b5
		{
4c79b5
			$token = &$tokens[$i];
4c79b5
4c79b5
			switch ($token)
4c79b5
			{
4c79b5
				case '!==':
4c79b5
				case '===':
4c79b5
				case '<<':
4c79b5
				case '>>':
4c79b5
				case '|':
4c79b5
				case '^':
4c79b5
				case '&':
4c79b5
				case '~':
4c79b5
				case ')':
4c79b5
				case ',':
4c79b5
				case '+':
4c79b5
				case '-':
4c79b5
				case '*':
4c79b5
				case '/':
4c79b5
				case '@':
4c79b5
				break;
4c79b5
4c79b5
				case '==':
4c79b5
				case 'eq':
4c79b5
					$token = '==';
4c79b5
				break;
4c79b5
4c79b5
				case '!=':
4c79b5
				case '<>':
4c79b5
				case 'ne':
4c79b5
				case 'neq':
4c79b5
					$token = '!=';
4c79b5
				break;
4c79b5
4c79b5
				case '<':
4c79b5
				case 'lt':
4c79b5
					$token = '<';
4c79b5
				break;
4c79b5
4c79b5
				case '<=':
4c79b5
				case 'le':
4c79b5
				case 'lte':
4c79b5
					$token = '<=';
4c79b5
				break;
4c79b5
4c79b5
				case '>':
4c79b5
				case 'gt':
4c79b5
					$token = '>';
4c79b5
				break;
4c79b5
4c79b5
				case '>=':
4c79b5
				case 'ge':
4c79b5
				case 'gte':
4c79b5
					$token = '>=';
4c79b5
				break;
4c79b5
4c79b5
				case '&&':
4c79b5
				case 'and':
4c79b5
					$token = '&&';
4c79b5
				break;
4c79b5
4c79b5
				case '||':
4c79b5
				case 'or':
4c79b5
					$token = '||';
4c79b5
				break;
4c79b5
4c79b5
				case '!':
4c79b5
				case 'not':
4c79b5
					$token = '!';
4c79b5
				break;
4c79b5
4c79b5
				case '%':
4c79b5
				case 'mod':
4c79b5
					$token = '%';
4c79b5
				break;
4c79b5
4c79b5
				case '(':
4c79b5
					array_push($is_arg_stack, $i);
4c79b5
				break;
4c79b5
4c79b5
				case 'is':
4c79b5
					$is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1;
4c79b5
					$is_arg	= implode('	', array_slice($tokens,	$is_arg_start, $i -	$is_arg_start));
4c79b5
4c79b5
					$new_tokens	= $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
4c79b5
4c79b5
					array_splice($tokens, $is_arg_start, sizeof($tokens), $new_tokens);
4c79b5
4c79b5
					$i = $is_arg_start;
4c79b5
4c79b5
				// no break
4c79b5
4c79b5
				default:
4c79b5
					if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs))
4c79b5
					{
4c79b5
						$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] . '\']');
4c79b5
					}
4c79b5
					else if (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs))
4c79b5
					{
4c79b5
						// Allow checking if loops are set with .loopname
4c79b5
						// It is also possible to check the loop count by doing  for example
4c79b5
						$blocks = explode('.', $varrefs[1]);
4c79b5
4c79b5
						// If the block is nested, we have a reference that we can grab.
4c79b5
						// If the block is not nested, we just go and grab the block from _tpldata
4c79b5
						if (sizeof($blocks) > 1)
4c79b5
						{
4c79b5
							$block = array_pop($blocks);
4c79b5
							$namespace = implode('.', $blocks);
4c79b5
							$varref = $this->generate_block_data_ref($namespace, true);
4c79b5
4c79b5
							// Add the block reference for the last child.
4c79b5
							$varref .= "['" . $block . "']";
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							$varref = '$this->_tpldata';
4c79b5
4c79b5
							// Add the block reference for the last child.
4c79b5
							$varref .= "['" . $blocks[0] . "']";
4c79b5
						}
4c79b5
						$token = "sizeof($varref)";
4c79b5
					}
4c79b5
					else if (!empty($token))
4c79b5
					{
4c79b5
						$token = '(' . $token . ')';
4c79b5
					}
4c79b5
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// If there are no valid tokens left or only control/compare characters left, we do skip this statement
4c79b5
		if (!sizeof($tokens) || str_replace(array(' ', '=', '!', '<', '>', '&', '|', '%', '(', ')'), '', implode('', $tokens)) == '')
4c79b5
		{
4c79b5
			$tokens = array('false');
4c79b5
		}
4c79b5
		return (($elseif) ? '} else if (' : 'if (') . (implode(' ', $tokens) . ') { ');
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Compile DEFINE tags
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile_tag_define($tag_args, $op)
4c79b5
	{
4c79b5
		preg_match('#^((?:[a-z0-9\-_]+\.)+)?\$(?=[A-Z])([A-Z0-9_\-]*)(?: = (\'?)([^\']*)(\'?))?$#', $tag_args, $match);
4c79b5
4c79b5
		if (empty($match[2]) || (!isset($match[4]) && $op))
4c79b5
		{
4c79b5
			return '';
4c79b5
		}
4c79b5
4c79b5
		if (!$op)
4c79b5
		{
4c79b5
			return 'unset(' . (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ');';
4c79b5
		}
4c79b5
4c79b5
		// Are we a string?
4c79b5
		if ($match[3] && $match[5])
4c79b5
		{
4c79b5
			$match[4] = str_replace(array('\\\'', '\\\\', '\''), array('\'', '\\', '\\\''), $match[4]);
4c79b5
4c79b5
			// Compile reference, we allow template variables in defines...
4c79b5
			$match[4] = $this->compile($match[4]);
4c79b5
4c79b5
			// Now replace the php code
4c79b5
			$match[4] = "'" . str_replace(array(''), array("' . ", " . '"), $match[4]) . "'";
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			preg_match('#true|false|\.#i', $match[4], $type);
4c79b5
4c79b5
			switch (strtolower($type[0]))
4c79b5
			{
4c79b5
				case 'true':
4c79b5
				case 'false':
4c79b5
					$match[4] = strtoupper($match[4]);
4c79b5
				break;
4c79b5
4c79b5
				case '.':
4c79b5
					$match[4] = doubleval($match[4]);
4c79b5
				break;
4c79b5
4c79b5
				default:
4c79b5
					$match[4] = intval($match[4]);
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ' = ' . $match[4] . ';';
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Compile INCLUDE tag
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile_tag_include($tag_args)
4c79b5
	{
4c79b5
		return "\$this->_tpl_include('$tag_args');";
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Compile INCLUDE_PHP tag
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile_tag_include_php($tag_args)
4c79b5
	{
4c79b5
		return "include('" . $tag_args . "');";
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* parse expression
4c79b5
	* This is from Smarty
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function _parse_is_expr($is_arg, $tokens)
4c79b5
	{
4c79b5
		$expr_end = 0;
4c79b5
		$negate_expr = false;
4c79b5
4c79b5
		if (($first_token = array_shift($tokens)) == 'not')
4c79b5
		{
4c79b5
			$negate_expr = true;
4c79b5
			$expr_type = array_shift($tokens);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$expr_type = $first_token;
4c79b5
		}
4c79b5
4c79b5
		switch ($expr_type)
4c79b5
		{
4c79b5
			case 'even':
4c79b5
				if (@$tokens[$expr_end] == 'by')
4c79b5
				{
4c79b5
					$expr_end++;
4c79b5
					$expr_arg = $tokens[$expr_end++];
4c79b5
					$expr = "!(($is_arg / $expr_arg) % $expr_arg)";
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$expr = "!($is_arg & 1)";
4c79b5
				}
4c79b5
			break;
4c79b5
4c79b5
			case 'odd':
4c79b5
				if (@$tokens[$expr_end] == 'by')
4c79b5
				{
4c79b5
					$expr_end++;
4c79b5
					$expr_arg = $tokens[$expr_end++];
4c79b5
					$expr = "(($is_arg / $expr_arg) % $expr_arg)";
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$expr = "($is_arg & 1)";
4c79b5
				}
4c79b5
			break;
4c79b5
4c79b5
			case 'div':
4c79b5
				if (@$tokens[$expr_end] == 'by')
4c79b5
				{
4c79b5
					$expr_end++;
4c79b5
					$expr_arg = $tokens[$expr_end++];
4c79b5
					$expr = "!($is_arg % $expr_arg)";
4c79b5
				}
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		if ($negate_expr)
4c79b5
		{
4c79b5
			$expr = "!($expr)";
4c79b5
		}
4c79b5
4c79b5
		array_splice($tokens, 0, $expr_end, $expr);
4c79b5
4c79b5
		return $tokens;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Generates a reference to the given variable inside the given (possibly nested)
4c79b5
	* block namespace. This is a string of the form:
4c79b5
	* ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '
4c79b5
	* It's ready to be inserted into an "echo" line in one of the templates.
4c79b5
	* NOTE: expects a trailing "." on the namespace.
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function generate_block_varref($namespace, $varname, $echo = true, $defop = false)
4c79b5
	{
4c79b5
		// Strip the trailing period.
4c79b5
		$namespace = substr($namespace, 0, -1);
4c79b5
4c79b5
		// Get a reference to the data block for this namespace.
4c79b5
		$varref = $this->generate_block_data_ref($namespace, true, $defop);
4c79b5
		// Prepend the necessary code to stick this in an echo line.
4c79b5
4c79b5
		// Append the variable reference.
4c79b5
		$varref .= "['$varname']";
4c79b5
		$varref = ($echo) ? "" : ((isset($varref)) ? $varref : '');
4c79b5
4c79b5
		return $varref;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Generates a reference to the array of data values for the given
4c79b5
	* (possibly nested) block namespace. This is a string of the form:
4c79b5
	* $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN']
4c79b5
	*
4c79b5
	* If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above.
4c79b5
	* NOTE: does not expect a trailing "." on the blockname.
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function generate_block_data_ref($blockname, $include_last_iterator, $defop = false)
4c79b5
	{
4c79b5
		// Get an array of the blocks involved.
4c79b5
		$blocks = explode('.', $blockname);
4c79b5
		$blockcount = sizeof($blocks) - 1;
4c79b5
4c79b5
		// DEFINE is not an element of any referenced variable, we must use _tpldata to access it
4c79b5
		if ($defop)
4c79b5
		{
4c79b5
			$varref = '$this->_tpldata[\'DEFINE\']';
4c79b5
			// Build up the string with everything but the last child.
4c79b5
			for ($i = 0; $i < $blockcount; $i++)
4c79b5
			{
4c79b5
				$varref .= "['" . $blocks[$i] . "'][\$_" . $blocks[$i] . '_i]';
4c79b5
			}
4c79b5
			// Add the block reference for the last child.
4c79b5
			$varref .= "['" . $blocks[$blockcount] . "']";
4c79b5
			// Add the iterator for the last child if requried.
4c79b5
			if ($include_last_iterator)
4c79b5
			{
4c79b5
				$varref .= '[$_' . $blocks[$blockcount] . '_i]';
4c79b5
			}
4c79b5
			return $varref;
4c79b5
		}
4c79b5
		else if ($include_last_iterator)
4c79b5
		{
4c79b5
			return '$_'. $blocks[$blockcount] . '_val';
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			return '$_'. $blocks[$blockcount - 1] . '_val[\''. $blocks[$blockcount]. '\']';
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Write compiled file to cache directory
4c79b5
	* @access private
4c79b5
	*/
4c79b5
	function compile_write($handle, $data)
4c79b5
	{
4c79b5
		global $phpEx;
4c79b5
4c79b5
		$filename = $this->template->cachepath . str_replace('/', '.', $this->template->filename[$handle]) . '.' . $phpEx;
4c79b5
4c79b5
		if ($fp = @fopen($filename, 'wb'))
4c79b5
		{
4c79b5
			@flock($fp, LOCK_EX);
4c79b5
			@fwrite ($fp, $data);
4c79b5
			@flock($fp, LOCK_UN);
4c79b5
			@fclose($fp);
4c79b5
4c79b5
			phpbb_chmod($filename, CHMOD_WRITE);
4c79b5
		}
4c79b5
4c79b5
		return;
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>