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

ef5584
ef5584
/**
ef5584
*
ef5584
* @package phpBB3
ef5584
* @version $Id: template.php 8943 2008-09-26 13:09:56Z acydburn $
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
* Base Template class.
ef5584
* @package phpBB3
ef5584
*/
ef5584
class template
ef5584
{
ef5584
	/** variable that holds all the data we'll be substituting into
ef5584
	* the compiled templates. Takes form:
ef5584
	* --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
ef5584
	* if it's a root-level variable, it'll be like this:
ef5584
	* --> $this->_tpldata[.][0][varname] == value
ef5584
	*/
ef5584
	var $_tpldata = array('.' => array(0 => array()));
ef5584
	var $_rootref;
ef5584
ef5584
	// Root dir and hash of filenames for each template handle.
ef5584
	var $root = '';
ef5584
	var $cachepath = '';
ef5584
	var $files = array();
ef5584
	var $filename = array();
ef5584
	var $files_inherit = array();
ef5584
	var $files_template = array();
ef5584
	var $inherit_root = '';
ef5584
ef5584
	// this will hash handle names to the compiled/uncompiled code for that handle.
ef5584
	var $compiled_code = array();
ef5584
ef5584
	/**
ef5584
	* Set template location
ef5584
	* @access public
ef5584
	*/
ef5584
	function set_template()
ef5584
	{
ef5584
		global $phpbb_root_path, $user;
ef5584
ef5584
		if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'))
ef5584
		{
ef5584
			$this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
ef5584
			$this->cachepath = $phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $user->theme['template_path']) . '_';
ef5584
			
ef5584
			if ($user->theme['template_inherits_id'])
ef5584
			{
ef5584
				$this->inherit_root = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template';
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
ef5584
		}
ef5584
ef5584
		$this->_rootref = &$this->_tpldata['.'][0];
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Set custom template location (able to use directory outside of phpBB)
ef5584
	* @access public
ef5584
	*/
ef5584
	function set_custom_template($template_path, $template_name)
ef5584
	{
ef5584
		global $phpbb_root_path;
ef5584
ef5584
		$this->root = $template_path;
ef5584
		$this->cachepath = $phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_';
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Sets the template filenames for handles. $filename_array
ef5584
	* should be a hash of handle => filename pairs.
ef5584
	* @access public
ef5584
	*/
ef5584
	function set_filenames($filename_array)
ef5584
	{
ef5584
		if (!is_array($filename_array))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
		foreach ($filename_array as $handle => $filename)
ef5584
		{
ef5584
			if (empty($filename))
ef5584
			{
ef5584
				trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
ef5584
			}
ef5584
ef5584
			$this->filename[$handle] = $filename;
ef5584
			$this->files[$handle] = $this->root . '/' . $filename;
ef5584
			
ef5584
			if ($this->inherit_root)
ef5584
			{
ef5584
				$this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
ef5584
			}
ef5584
		}
ef5584
		
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Destroy template data set
ef5584
	* @access public
ef5584
	*/
ef5584
	function destroy()
ef5584
	{
ef5584
		$this->_tpldata = array('.' => array(0 => array()));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Reset/empty complete block
ef5584
	* @access public
ef5584
	*/
ef5584
	function destroy_block_vars($blockname)
ef5584
	{
ef5584
		if (strpos($blockname, '.') !== false)
ef5584
		{
ef5584
			// Nested block.
ef5584
			$blocks = explode('.', $blockname);
ef5584
			$blockcount = sizeof($blocks) - 1;
ef5584
ef5584
			$str = &$this->_tpldata;
ef5584
			for ($i = 0; $i < $blockcount; $i++)
ef5584
			{
ef5584
				$str = &$str[$blocks[$i]];
ef5584
				$str = &$str[sizeof($str) - 1];
ef5584
			}
ef5584
ef5584
			unset($str[$blocks[$blockcount]]);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			// Top-level block.
ef5584
			unset($this->_tpldata[$blockname]);
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Display handle
ef5584
	* @access public
ef5584
	*/
ef5584
	function display($handle, $include_once = true)
ef5584
	{
ef5584
		global $user, $phpbb_hook;
ef5584
ef5584
		if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once))
ef5584
		{
ef5584
			if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
ef5584
			{
ef5584
				return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
ef5584
			}
ef5584
		}
ef5584
ef5584
		if (defined('IN_ERROR_HANDLER'))
ef5584
		{
ef5584
			if ((E_NOTICE & error_reporting()) == E_NOTICE)
ef5584
			{
ef5584
				error_reporting(error_reporting() ^ E_NOTICE);
ef5584
			}
ef5584
		}
ef5584
ef5584
		if ($filename = $this->_tpl_load($handle))
ef5584
		{
ef5584
			($include_once) ? include_once($filename) : include($filename);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			eval(' ?>' . $this->compiled_code[$handle] . '
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Display the handle and assign the output to a template variable or return the compiled result.
ef5584
	* @access public
ef5584
	*/
ef5584
	function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
ef5584
	{
ef5584
		ob_start();
ef5584
		$this->display($handle, $include_once);
ef5584
		$contents = ob_get_clean();
ef5584
ef5584
		if ($return_content)
ef5584
		{
ef5584
			return $contents;
ef5584
		}
ef5584
ef5584
		$this->assign_var($template_var, $contents);
ef5584
ef5584
		return true;
ef5584
	}
ef5584
	
ef5584
	/**
ef5584
	* Load a compiled template if possible, if not, recompile it
ef5584
	* @access private
ef5584
	*/
ef5584
	function _tpl_load(&$handle)
ef5584
	{
ef5584
		global $user, $phpEx, $config;
ef5584
ef5584
		$filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
ef5584
		$this->files_template[$handle] = $user->theme['template_id'];
ef5584
		
ef5584
		$recompile = false;
ef5584
		if (!file_exists($filename) || @filesize($filename) === 0)
ef5584
		{
ef5584
			$recompile = true;
ef5584
		}
ef5584
		else if ($config['load_tplcompile'])
ef5584
		{
ef5584
			// No way around it: we need to check inheritance here
ef5584
			if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
ef5584
			{
ef5584
				$this->files[$handle] = $this->files_inherit[$handle];
ef5584
				$this->files_template[$handle] = $user->theme['template_inherits_id'];
ef5584
			}
ef5584
			$recompile = (@filemtime($filename) < filemtime($this->files[$handle])) ? true : false;
ef5584
		}
ef5584
		
ef5584
		// Recompile page if the original template is newer, otherwise load the compiled version
ef5584
		if (!$recompile)
ef5584
		{
ef5584
			return $filename;
ef5584
		}
ef5584
ef5584
		global $db, $phpbb_root_path;
ef5584
ef5584
		if (!class_exists('template_compile'))
ef5584
		{
ef5584
			include($phpbb_root_path . 'includes/functions_template.' . $phpEx);
ef5584
		}
ef5584
		
ef5584
		// Inheritance - we point to another template file for this one. Equality is also used for store_db
ef5584
		if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
ef5584
		{
ef5584
			$this->files[$handle] = $this->files_inherit[$handle];
ef5584
			$this->files_template[$handle] = $user->theme['template_inherits_id'];
ef5584
		}
ef5584
		
ef5584
		$compile = new template_compile($this);
ef5584
ef5584
		// If we don't have a file assigned to this handle, die.
ef5584
		if (!isset($this->files[$handle]))
ef5584
		{
ef5584
			trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
ef5584
		}
ef5584
ef5584
		// Just compile if no user object is present (happens within the installer)
ef5584
		if (!$user)
ef5584
		{
ef5584
			$compile->_tpl_load_file($handle);
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		if (isset($user->theme['template_storedb']) && $user->theme['template_storedb'])
ef5584
		{
ef5584
			$rows = array();
ef5584
			$ids = array();
ef5584
			// Inheritance
ef5584
			if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
ef5584
			{
ef5584
				$ids[] = $user->theme['template_inherits_id'];
ef5584
			}
ef5584
			$ids[] = $user->theme['template_id'];
ef5584
			
ef5584
			foreach ($ids as $id)
ef5584
			{
ef5584
				$sql = 'SELECT *
ef5584
				FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
ef5584
				WHERE template_id = ' . $id . "
ef5584
					AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "'
ef5584
						OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')';
ef5584
			
ef5584
				$result = $db->sql_query($sql);
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$rows[$row['template_filename']] = $row;
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
			}
ef5584
					
ef5584
			if (sizeof($rows))
ef5584
			{
ef5584
				foreach ($rows as $row)
ef5584
				{
ef5584
					$file = $this->root . '/' . $row['template_filename'];
ef5584
					$force_reload = false;
ef5584
					if ($row['template_id'] != $user->theme['template_id'])
ef5584
					{
ef5584
						// make sure that we are not overlooking a file not in the db yet
ef5584
						if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
ef5584
						{
ef5584
							$file = $this->inherit_root . '/' . $row['template_filename'];
ef5584
							$this->files[$row['template_filename']] = $file;
ef5584
							$this->files_inherit[$row['template_filename']] = $file;
ef5584
							$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
ef5584
						}
ef5584
						else if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
ef5584
						{
ef5584
							// Ok, we have a situation. There is a file in the subtemplate, but nothing in the DB. We have to fix that.
ef5584
							$force_reload = true;
ef5584
							$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
ef5584
						}
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$this->files_template[$row['template_filename']] = $user->theme['template_id'];
ef5584
					}
ef5584
					
ef5584
					if ($force_reload || $row['template_mtime'] < filemtime($file))
ef5584
					{
ef5584
						if ($row['template_filename'] == $this->filename[$handle])
ef5584
						{
ef5584
							$compile->_tpl_load_file($handle, true);
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							$this->files[$row['template_filename']] = $file;
ef5584
							$this->filename[$row['template_filename']] = $row['template_filename'];
ef5584
							$compile->_tpl_load_file($row['template_filename'], true);
ef5584
							unset($this->compiled_code[$row['template_filename']]);
ef5584
							unset($this->files[$row['template_filename']]);
ef5584
							unset($this->filename[$row['template_filename']]);
ef5584
						}
ef5584
					}
ef5584
ef5584
					if ($row['template_filename'] == $this->filename[$handle])
ef5584
					{
ef5584
						$this->compiled_code[$handle] = $compile->compile(trim($row['template_data']));
ef5584
						$compile->compile_write($handle, $this->compiled_code[$handle]);
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						// Only bother compiling if it doesn't already exist
ef5584
						if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx))
ef5584
						{
ef5584
							$this->filename[$row['template_filename']] = $row['template_filename'];
ef5584
							$compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));
ef5584
							unset($this->filename[$row['template_filename']]);
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$file = $this->root . '/' . $row['template_filename'];
ef5584
ef5584
				if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
ef5584
				{
ef5584
					$file = $this->inherit_root . '/' . $row['template_filename'];
ef5584
					$this->files[$row['template_filename']] = $file;
ef5584
					$this->files_inherit[$row['template_filename']] = $file;
ef5584
					$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
ef5584
				}
ef5584
				// Try to load from filesystem and instruct to insert into the styles table...
ef5584
				$compile->_tpl_load_file($handle, true);
ef5584
				return false;
ef5584
			}
ef5584
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$compile->_tpl_load_file($handle);
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Assign key variable pairs from an array
ef5584
	* @access public
ef5584
	*/
ef5584
	function assign_vars($vararray)
ef5584
	{
ef5584
		foreach ($vararray as $key => $val)
ef5584
		{
ef5584
			$this->_rootref[$key] = $val;
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Assign a single variable to a single key
ef5584
	* @access public
ef5584
	*/
ef5584
	function assign_var($varname, $varval)
ef5584
	{
ef5584
		$this->_rootref[$varname] = $varval;
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Assign key variable pairs from an array to a specified block
ef5584
	* @access public
ef5584
	*/
ef5584
	function assign_block_vars($blockname, $vararray)
ef5584
	{
ef5584
		if (strpos($blockname, '.') !== false)
ef5584
		{
ef5584
			// Nested block.
ef5584
			$blocks = explode('.', $blockname);
ef5584
			$blockcount = sizeof($blocks) - 1;
ef5584
ef5584
			$str = &$this->_tpldata;
ef5584
			for ($i = 0; $i < $blockcount; $i++)
ef5584
			{
ef5584
				$str = &$str[$blocks[$i]];
ef5584
				$str = &$str[sizeof($str) - 1];
ef5584
			}
ef5584
ef5584
			$s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
ef5584
			$vararray['S_ROW_COUNT'] = $s_row_count;
ef5584
ef5584
			// Assign S_FIRST_ROW
ef5584
			if (!$s_row_count)
ef5584
			{
ef5584
				$vararray['S_FIRST_ROW'] = true;
ef5584
			}
ef5584
ef5584
			// Now the tricky part, we always assign S_LAST_ROW and remove the entry before
ef5584
			// This is much more clever than going through the complete template data on display (phew)
ef5584
			$vararray['S_LAST_ROW'] = true;
ef5584
			if ($s_row_count > 0)
ef5584
			{
ef5584
				unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
ef5584
			}
ef5584
ef5584
			// Now we add the block that we're actually assigning to.
ef5584
			// We're adding a new iteration to this block with the given
ef5584
			// variable assignments.
ef5584
			$str[$blocks[$blockcount]][] = $vararray;
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			// Top-level block.
ef5584
			$s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
ef5584
			$vararray['S_ROW_COUNT'] = $s_row_count;
ef5584
ef5584
			// Assign S_FIRST_ROW
ef5584
			if (!$s_row_count)
ef5584
			{
ef5584
				$vararray['S_FIRST_ROW'] = true;
ef5584
			}
ef5584
ef5584
			// We always assign S_LAST_ROW and remove the entry before
ef5584
			$vararray['S_LAST_ROW'] = true;
ef5584
			if ($s_row_count > 0)
ef5584
			{
ef5584
				unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
ef5584
			}
ef5584
			
ef5584
			// Add a new iteration to this block with the variable assignments we were given.
ef5584
			$this->_tpldata[$blockname][] = $vararray;
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Change already assigned key variable pair (one-dimensional - single loop entry)
ef5584
	*
ef5584
	* An example of how to use this function:
ef5584
	* {@example alter_block_array.php}
ef5584
	*
ef5584
	* @param	string	$blockname	the blockname, for example 'loop'
ef5584
	* @param	array	$vararray	the var array to insert/add or merge
ef5584
	* @param	mixed	$key		Key to search for
ef5584
	*
ef5584
	* array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
ef5584
	*
ef5584
	* int: Position [the position to change or insert at directly given]
ef5584
	*
ef5584
	* If key is false the position is set to 0
ef5584
	* If key is true the position is set to the last entry
ef5584
	*
ef5584
	* @param	string	$mode		Mode to execute (valid modes are 'insert' and 'change')
ef5584
	*
ef5584
	*	If insert, the vararray is inserted at the given position (position counting from zero).
ef5584
	*	If change, the current block gets merged with the vararray (resulting in new key/value pairs be added and existing keys be replaced by the new value).
ef5584
	*
ef5584
	* Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
ef5584
	* and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
ef5584
	*
ef5584
	* @return bool false on error, true on success
ef5584
	* @access public
ef5584
	*/
ef5584
	function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
ef5584
	{
ef5584
		if (strpos($blockname, '.') !== false)
ef5584
		{
ef5584
			// Nested blocks are not supported
ef5584
			return false;
ef5584
		}
ef5584
		
ef5584
		// Change key to zero (change first position) if false and to last position if true
ef5584
		if ($key === false || $key === true)
ef5584
		{
ef5584
			$key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
ef5584
		}
ef5584
ef5584
		// Get correct position if array given
ef5584
		if (is_array($key))
ef5584
		{
ef5584
			// Search array to get correct position
ef5584
			list($search_key, $search_value) = @each($key);
ef5584
ef5584
			$key = NULL;
ef5584
			foreach ($this->_tpldata[$blockname] as $i => $val_ary)
ef5584
			{
ef5584
				if ($val_ary[$search_key] === $search_value)
ef5584
				{
ef5584
					$key = $i;
ef5584
					break;
ef5584
				}
ef5584
			}
ef5584
ef5584
			// key/value pair not found
ef5584
			if ($key === NULL)
ef5584
			{
ef5584
				return false;
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Insert Block
ef5584
		if ($mode == 'insert')
ef5584
		{
ef5584
			// Make sure we are not exceeding the last iteration
ef5584
			if ($key >= sizeof($this->_tpldata[$blockname]))
ef5584
			{
ef5584
				$key = sizeof($this->_tpldata[$blockname]);
ef5584
				unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
ef5584
				$vararray['S_LAST_ROW'] = true;
ef5584
			}
ef5584
			else if ($key === 0)
ef5584
			{
ef5584
				unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
ef5584
				$vararray['S_FIRST_ROW'] = true;
ef5584
			}
ef5584
ef5584
			// Re-position template blocks
ef5584
			for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
ef5584
			{
ef5584
				$this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
ef5584
				$this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
ef5584
			}
ef5584
ef5584
			// Insert vararray at given position
ef5584
			$vararray['S_ROW_COUNT'] = $key;
ef5584
			$this->_tpldata[$blockname][$key] = $vararray;
ef5584
ef5584
			return true;
ef5584
		}
ef5584
ef5584
		// Which block to change?
ef5584
		if ($mode == 'change')
ef5584
		{
ef5584
			if ($key == sizeof($this->_tpldata[$blockname]))
ef5584
			{
ef5584
				$key--;
ef5584
			}
ef5584
ef5584
			$this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
ef5584
			return true;
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Include a separate template
ef5584
	* @access private
ef5584
	*/
ef5584
	function _tpl_include($filename, $include = true)
ef5584
	{
ef5584
		$handle = $filename;
ef5584
		$this->filename[$handle] = $filename;
ef5584
		$this->files[$handle] = $this->root . '/' . $filename;
ef5584
		if ($this->inherit_root)
ef5584
		{
ef5584
			$this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
ef5584
		}
ef5584
ef5584
		$filename = $this->_tpl_load($handle);
ef5584
ef5584
		if ($include)
ef5584
		{
ef5584
			global $user;
ef5584
ef5584
			if ($filename)
ef5584
			{
ef5584
				include($filename);
ef5584
				return;
ef5584
			}
ef5584
			eval(' ?>' . $this->compiled_code[$handle] . '
ef5584
		}
ef5584
	}
ef5584
}
ef5584
ef5584
?>