Blame Identity/Webenv/App/phpBB/3.0.4/includes/functions_template.php

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