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

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