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

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