Blame Identity/Models/Html/phpBB/3.0.4/includes/functions_template.php

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