includes/template.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/template.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,692 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Base Template class.
    1.24 +* @package phpBB3
    1.25 +*/
    1.26 +class template
    1.27 +{
    1.28 +	/** variable that holds all the data we'll be substituting into
    1.29 +	* the compiled templates. Takes form:
    1.30 +	* --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
    1.31 +	* if it's a root-level variable, it'll be like this:
    1.32 +	* --> $this->_tpldata[.][0][varname] == value
    1.33 +	*/
    1.34 +	var $_tpldata = array('.' => array(0 => array()));
    1.35 +	var $_rootref;
    1.36 +
    1.37 +	// Root dir and hash of filenames for each template handle.
    1.38 +	var $root = '';
    1.39 +	var $cachepath = '';
    1.40 +	var $files = array();
    1.41 +	var $filename = array();
    1.42 +	var $files_inherit = array();
    1.43 +	var $files_template = array();
    1.44 +	var $inherit_root = '';
    1.45 +	var $orig_tpl_storedb;
    1.46 +	var $orig_tpl_inherits_id;
    1.47 +
    1.48 +	// this will hash handle names to the compiled/uncompiled code for that handle.
    1.49 +	var $compiled_code = array();
    1.50 +
    1.51 +	/**
    1.52 +	* Set template location
    1.53 +	* @access public
    1.54 +	*/
    1.55 +	function set_template()
    1.56 +	{
    1.57 +		global $phpbb_root_path, $user;
    1.58 +
    1.59 +		if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'))
    1.60 +		{
    1.61 +			$this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
    1.62 +			$this->cachepath = $phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $user->theme['template_path']) . '_';
    1.63 +
    1.64 +			if ($this->orig_tpl_storedb === null)
    1.65 +			{
    1.66 +				$this->orig_tpl_storedb = $user->theme['template_storedb'];
    1.67 +			}
    1.68 +
    1.69 +			if ($this->orig_tpl_inherits_id === null)
    1.70 +			{
    1.71 +				$this->orig_tpl_inherits_id = $user->theme['template_inherits_id'];
    1.72 +			}
    1.73 +
    1.74 +			$user->theme['template_storedb'] = $this->orig_tpl_storedb;
    1.75 +			$user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
    1.76 +
    1.77 +			if ($user->theme['template_inherits_id'])
    1.78 +			{
    1.79 +				$this->inherit_root = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template';
    1.80 +			}
    1.81 +		}
    1.82 +		else
    1.83 +		{
    1.84 +			trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
    1.85 +		}
    1.86 +
    1.87 +		$this->_rootref = &$this->_tpldata['.'][0];
    1.88 +
    1.89 +		return true;
    1.90 +	}
    1.91 +
    1.92 +	/**
    1.93 +	* Set custom template location (able to use directory outside of phpBB)
    1.94 +	* @access public
    1.95 +	*/
    1.96 +	function set_custom_template($template_path, $template_name, $fallback_template_path = false)
    1.97 +	{
    1.98 +		global $phpbb_root_path, $user;
    1.99 +
   1.100 +		// Make sure $template_path has no ending slash
   1.101 +		if (substr($template_path, -1) == '/')
   1.102 +		{
   1.103 +			$template_path = substr($template_path, 0, -1);
   1.104 +		}
   1.105 +
   1.106 +		$this->root = $template_path;
   1.107 +		$this->cachepath = $phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_';
   1.108 +
   1.109 +		if ($fallback_template_path !== false)
   1.110 +		{
   1.111 +			if (substr($fallback_template_path, -1) == '/')
   1.112 +			{
   1.113 +				$fallback_template_path = substr($fallback_template_path, 0, -1);
   1.114 +			}
   1.115 +
   1.116 +			$this->inherit_root = $fallback_template_path;
   1.117 +			$this->orig_tpl_inherits_id = true;
   1.118 +		}
   1.119 +		else
   1.120 +		{
   1.121 +			$this->orig_tpl_inherits_id = false;
   1.122 +		}
   1.123 +
   1.124 +		// the database does not store the path or name of a custom template
   1.125 +		// so there is no way we can properly store custom templates there
   1.126 +		$this->orig_tpl_storedb = false;
   1.127 +
   1.128 +		$this->_rootref = &$this->_tpldata['.'][0];
   1.129 +
   1.130 +		return true;
   1.131 +	}
   1.132 +
   1.133 +	/**
   1.134 +	* Sets the template filenames for handles. $filename_array
   1.135 +	* should be a hash of handle => filename pairs.
   1.136 +	* @access public
   1.137 +	*/
   1.138 +	function set_filenames($filename_array)
   1.139 +	{
   1.140 +		if (!is_array($filename_array))
   1.141 +		{
   1.142 +			return false;
   1.143 +		}
   1.144 +		foreach ($filename_array as $handle => $filename)
   1.145 +		{
   1.146 +			if (empty($filename))
   1.147 +			{
   1.148 +				trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
   1.149 +			}
   1.150 +
   1.151 +			$this->filename[$handle] = $filename;
   1.152 +			$this->files[$handle] = $this->root . '/' . $filename;
   1.153 +
   1.154 +			if ($this->inherit_root)
   1.155 +			{
   1.156 +				$this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
   1.157 +			}
   1.158 +		}
   1.159 +
   1.160 +		return true;
   1.161 +	}
   1.162 +
   1.163 +	/**
   1.164 +	* Destroy template data set
   1.165 +	* @access public
   1.166 +	*/
   1.167 +	function destroy()
   1.168 +	{
   1.169 +		$this->_tpldata = array('.' => array(0 => array()));
   1.170 +		$this->_rootref = &$this->_tpldata['.'][0];
   1.171 +	}
   1.172 +
   1.173 +	/**
   1.174 +	* Reset/empty complete block
   1.175 +	* @access public
   1.176 +	*/
   1.177 +	function destroy_block_vars($blockname)
   1.178 +	{
   1.179 +		if (strpos($blockname, '.') !== false)
   1.180 +		{
   1.181 +			// Nested block.
   1.182 +			$blocks = explode('.', $blockname);
   1.183 +			$blockcount = sizeof($blocks) - 1;
   1.184 +
   1.185 +			$str = &$this->_tpldata;
   1.186 +			for ($i = 0; $i < $blockcount; $i++)
   1.187 +			{
   1.188 +				$str = &$str[$blocks[$i]];
   1.189 +				$str = &$str[sizeof($str) - 1];
   1.190 +			}
   1.191 +
   1.192 +			unset($str[$blocks[$blockcount]]);
   1.193 +		}
   1.194 +		else
   1.195 +		{
   1.196 +			// Top-level block.
   1.197 +			unset($this->_tpldata[$blockname]);
   1.198 +		}
   1.199 +
   1.200 +		return true;
   1.201 +	}
   1.202 +
   1.203 +	/**
   1.204 +	* Display handle
   1.205 +	* @access public
   1.206 +	*/
   1.207 +	function display($handle, $include_once = true)
   1.208 +	{
   1.209 +		global $user, $phpbb_hook;
   1.210 +
   1.211 +		if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this))
   1.212 +		{
   1.213 +			if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
   1.214 +			{
   1.215 +				return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
   1.216 +			}
   1.217 +		}
   1.218 +
   1.219 +		if (defined('IN_ERROR_HANDLER'))
   1.220 +		{
   1.221 +			if ((E_NOTICE & error_reporting()) == E_NOTICE)
   1.222 +			{
   1.223 +				error_reporting(error_reporting() ^ E_NOTICE);
   1.224 +			}
   1.225 +		}
   1.226 +
   1.227 +		if ($filename = $this->_tpl_load($handle))
   1.228 +		{
   1.229 +			($include_once) ? include_once($filename) : include($filename);
   1.230 +		}
   1.231 +		else
   1.232 +		{
   1.233 +			eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
   1.234 +		}
   1.235 +
   1.236 +		return true;
   1.237 +	}
   1.238 +
   1.239 +	/**
   1.240 +	* Display the handle and assign the output to a template variable or return the compiled result.
   1.241 +	* @access public
   1.242 +	*/
   1.243 +	function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
   1.244 +	{
   1.245 +		ob_start();
   1.246 +		$this->display($handle, $include_once);
   1.247 +		$contents = ob_get_clean();
   1.248 +
   1.249 +		if ($return_content)
   1.250 +		{
   1.251 +			return $contents;
   1.252 +		}
   1.253 +
   1.254 +		$this->assign_var($template_var, $contents);
   1.255 +
   1.256 +		return true;
   1.257 +	}
   1.258 +
   1.259 +	/**
   1.260 +	* Load a compiled template if possible, if not, recompile it
   1.261 +	* @access private
   1.262 +	*/
   1.263 +	function _tpl_load(&$handle)
   1.264 +	{
   1.265 +		global $user, $phpEx, $config;
   1.266 +
   1.267 +		if (!isset($this->filename[$handle]))
   1.268 +		{
   1.269 +			trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
   1.270 +		}
   1.271 +
   1.272 +		// reload these settings to have the values they had when this object was initialised
   1.273 +		// using set_template or set_custom_template, they might otherwise have been overwritten
   1.274 +		// by other template class instances in between.
   1.275 +		$user->theme['template_storedb'] = $this->orig_tpl_storedb;
   1.276 +		$user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
   1.277 +
   1.278 +		$filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
   1.279 +		$this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0;
   1.280 +
   1.281 +		$recompile = false;
   1.282 +		if (!file_exists($filename) || @filesize($filename) === 0 || defined('DEBUG_EXTRA'))
   1.283 +		{
   1.284 +			$recompile = true;
   1.285 +		}
   1.286 +		else if ($config['load_tplcompile'])
   1.287 +		{
   1.288 +			// No way around it: we need to check inheritance here
   1.289 +			if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
   1.290 +			{
   1.291 +				$this->files[$handle] = $this->files_inherit[$handle];
   1.292 +				$this->files_template[$handle] = $user->theme['template_inherits_id'];
   1.293 +			}
   1.294 +			$recompile = (@filemtime($filename) < filemtime($this->files[$handle])) ? true : false;
   1.295 +		}
   1.296 +
   1.297 +		// Recompile page if the original template is newer, otherwise load the compiled version
   1.298 +		if (!$recompile)
   1.299 +		{
   1.300 +			return $filename;
   1.301 +		}
   1.302 +
   1.303 +		global $db, $phpbb_root_path;
   1.304 +
   1.305 +		if (!class_exists('template_compile'))
   1.306 +		{
   1.307 +			include($phpbb_root_path . 'includes/functions_template.' . $phpEx);
   1.308 +		}
   1.309 +
   1.310 +		// Inheritance - we point to another template file for this one. Equality is also used for store_db
   1.311 +		if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
   1.312 +		{
   1.313 +			$this->files[$handle] = $this->files_inherit[$handle];
   1.314 +			$this->files_template[$handle] = $user->theme['template_inherits_id'];
   1.315 +		}
   1.316 +
   1.317 +		$compile = new template_compile($this);
   1.318 +
   1.319 +		// If we don't have a file assigned to this handle, die.
   1.320 +		if (!isset($this->files[$handle]))
   1.321 +		{
   1.322 +			trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
   1.323 +		}
   1.324 +
   1.325 +		// Just compile if no user object is present (happens within the installer)
   1.326 +		if (!$user)
   1.327 +		{
   1.328 +			$compile->_tpl_load_file($handle);
   1.329 +			return false;
   1.330 +		}
   1.331 +
   1.332 +		if (isset($user->theme['template_storedb']) && $user->theme['template_storedb'])
   1.333 +		{
   1.334 +			$rows = array();
   1.335 +			$ids = array();
   1.336 +			// Inheritance
   1.337 +			if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
   1.338 +			{
   1.339 +				$ids[] = $user->theme['template_inherits_id'];
   1.340 +			}
   1.341 +			$ids[] = $user->theme['template_id'];
   1.342 +
   1.343 +			foreach ($ids as $id)
   1.344 +			{
   1.345 +				$sql = 'SELECT *
   1.346 +				FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
   1.347 +				WHERE template_id = ' . $id . "
   1.348 +					AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "'
   1.349 +						OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')';
   1.350 +
   1.351 +				$result = $db->sql_query($sql);
   1.352 +				while ($row = $db->sql_fetchrow($result))
   1.353 +				{
   1.354 +					$rows[$row['template_filename']] = $row;
   1.355 +				}
   1.356 +				$db->sql_freeresult($result);
   1.357 +			}
   1.358 +
   1.359 +			if (sizeof($rows))
   1.360 +			{
   1.361 +				foreach ($rows as $row)
   1.362 +				{
   1.363 +					$file = $this->root . '/' . $row['template_filename'];
   1.364 +					$force_reload = false;
   1.365 +					if ($row['template_id'] != $user->theme['template_id'])
   1.366 +					{
   1.367 +						// make sure that we are not overlooking a file not in the db yet
   1.368 +						if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
   1.369 +						{
   1.370 +							$file = $this->inherit_root . '/' . $row['template_filename'];
   1.371 +							$this->files[$row['template_filename']] = $file;
   1.372 +							$this->files_inherit[$row['template_filename']] = $file;
   1.373 +							$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
   1.374 +						}
   1.375 +						else if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
   1.376 +						{
   1.377 +							// Ok, we have a situation. There is a file in the subtemplate, but nothing in the DB. We have to fix that.
   1.378 +							$force_reload = true;
   1.379 +							$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
   1.380 +						}
   1.381 +					}
   1.382 +					else
   1.383 +					{
   1.384 +						$this->files_template[$row['template_filename']] = $user->theme['template_id'];
   1.385 +					}
   1.386 +
   1.387 +					if ($force_reload || $row['template_mtime'] < filemtime($file))
   1.388 +					{
   1.389 +						if ($row['template_filename'] == $this->filename[$handle])
   1.390 +						{
   1.391 +							$compile->_tpl_load_file($handle, true);
   1.392 +						}
   1.393 +						else
   1.394 +						{
   1.395 +							$this->files[$row['template_filename']] = $file;
   1.396 +							$this->filename[$row['template_filename']] = $row['template_filename'];
   1.397 +							$compile->_tpl_load_file($row['template_filename'], true);
   1.398 +							unset($this->compiled_code[$row['template_filename']]);
   1.399 +							unset($this->files[$row['template_filename']]);
   1.400 +							unset($this->filename[$row['template_filename']]);
   1.401 +						}
   1.402 +					}
   1.403 +
   1.404 +					if ($row['template_filename'] == $this->filename[$handle])
   1.405 +					{
   1.406 +						$this->compiled_code[$handle] = $compile->compile(trim($row['template_data']));
   1.407 +						$compile->compile_write($handle, $this->compiled_code[$handle]);
   1.408 +					}
   1.409 +					else
   1.410 +					{
   1.411 +						// Only bother compiling if it doesn't already exist
   1.412 +						if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx))
   1.413 +						{
   1.414 +							$this->filename[$row['template_filename']] = $row['template_filename'];
   1.415 +							$compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));
   1.416 +							unset($this->filename[$row['template_filename']]);
   1.417 +						}
   1.418 +					}
   1.419 +				}
   1.420 +			}
   1.421 +			else
   1.422 +			{
   1.423 +				$file = $this->root . '/' . $row['template_filename'];
   1.424 +
   1.425 +				if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
   1.426 +				{
   1.427 +					$file = $this->inherit_root . '/' . $row['template_filename'];
   1.428 +					$this->files[$row['template_filename']] = $file;
   1.429 +					$this->files_inherit[$row['template_filename']] = $file;
   1.430 +					$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
   1.431 +				}
   1.432 +				// Try to load from filesystem and instruct to insert into the styles table...
   1.433 +				$compile->_tpl_load_file($handle, true);
   1.434 +				return false;
   1.435 +			}
   1.436 +
   1.437 +			return false;
   1.438 +		}
   1.439 +
   1.440 +		$compile->_tpl_load_file($handle);
   1.441 +		return false;
   1.442 +	}
   1.443 +
   1.444 +	/**
   1.445 +	* Assign key variable pairs from an array
   1.446 +	* @access public
   1.447 +	*/
   1.448 +	function assign_vars($vararray)
   1.449 +	{
   1.450 +		foreach ($vararray as $key => $val)
   1.451 +		{
   1.452 +			$this->_rootref[$key] = $val;
   1.453 +		}
   1.454 +
   1.455 +		return true;
   1.456 +	}
   1.457 +
   1.458 +	/**
   1.459 +	* Assign a single variable to a single key
   1.460 +	* @access public
   1.461 +	*/
   1.462 +	function assign_var($varname, $varval)
   1.463 +	{
   1.464 +		$this->_rootref[$varname] = $varval;
   1.465 +
   1.466 +		return true;
   1.467 +	}
   1.468 +
   1.469 +	/**
   1.470 +	* Assign key variable pairs from an array to a specified block
   1.471 +	* @access public
   1.472 +	*/
   1.473 +	function assign_block_vars($blockname, $vararray)
   1.474 +	{
   1.475 +		if (strpos($blockname, '.') !== false)
   1.476 +		{
   1.477 +			// Nested block.
   1.478 +			$blocks = explode('.', $blockname);
   1.479 +			$blockcount = sizeof($blocks) - 1;
   1.480 +
   1.481 +			$str = &$this->_tpldata;
   1.482 +			for ($i = 0; $i < $blockcount; $i++)
   1.483 +			{
   1.484 +				$str = &$str[$blocks[$i]];
   1.485 +				$str = &$str[sizeof($str) - 1];
   1.486 +			}
   1.487 +
   1.488 +			$s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
   1.489 +			$vararray['S_ROW_COUNT'] = $s_row_count;
   1.490 +
   1.491 +			// Assign S_FIRST_ROW
   1.492 +			if (!$s_row_count)
   1.493 +			{
   1.494 +				$vararray['S_FIRST_ROW'] = true;
   1.495 +			}
   1.496 +
   1.497 +			// Now the tricky part, we always assign S_LAST_ROW and remove the entry before
   1.498 +			// This is much more clever than going through the complete template data on display (phew)
   1.499 +			$vararray['S_LAST_ROW'] = true;
   1.500 +			if ($s_row_count > 0)
   1.501 +			{
   1.502 +				unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
   1.503 +			}
   1.504 +
   1.505 +			// Now we add the block that we're actually assigning to.
   1.506 +			// We're adding a new iteration to this block with the given
   1.507 +			// variable assignments.
   1.508 +			$str[$blocks[$blockcount]][] = $vararray;
   1.509 +		}
   1.510 +		else
   1.511 +		{
   1.512 +			// Top-level block.
   1.513 +			$s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
   1.514 +			$vararray['S_ROW_COUNT'] = $s_row_count;
   1.515 +
   1.516 +			// Assign S_FIRST_ROW
   1.517 +			if (!$s_row_count)
   1.518 +			{
   1.519 +				$vararray['S_FIRST_ROW'] = true;
   1.520 +			}
   1.521 +
   1.522 +			// We always assign S_LAST_ROW and remove the entry before
   1.523 +			$vararray['S_LAST_ROW'] = true;
   1.524 +			if ($s_row_count > 0)
   1.525 +			{
   1.526 +				unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
   1.527 +			}
   1.528 +
   1.529 +			// Add a new iteration to this block with the variable assignments we were given.
   1.530 +			$this->_tpldata[$blockname][] = $vararray;
   1.531 +		}
   1.532 +
   1.533 +		return true;
   1.534 +	}
   1.535 +
   1.536 +	/**
   1.537 +	* Change already assigned key variable pair (one-dimensional - single loop entry)
   1.538 +	*
   1.539 +	* An example of how to use this function:
   1.540 +	* {@example alter_block_array.php}
   1.541 +	*
   1.542 +	* @param	string	$blockname	the blockname, for example 'loop'
   1.543 +	* @param	array	$vararray	the var array to insert/add or merge
   1.544 +	* @param	mixed	$key		Key to search for
   1.545 +	*
   1.546 +	* array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
   1.547 +	*
   1.548 +	* int: Position [the position to change or insert at directly given]
   1.549 +	*
   1.550 +	* If key is false the position is set to 0
   1.551 +	* If key is true the position is set to the last entry
   1.552 +	*
   1.553 +	* @param	string	$mode		Mode to execute (valid modes are 'insert' and 'change')
   1.554 +	*
   1.555 +	*	If insert, the vararray is inserted at the given position (position counting from zero).
   1.556 +	*	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).
   1.557 +	*
   1.558 +	* Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
   1.559 +	* and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
   1.560 +	*
   1.561 +	* @return bool false on error, true on success
   1.562 +	* @access public
   1.563 +	*/
   1.564 +	function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
   1.565 +	{
   1.566 +		if (strpos($blockname, '.') !== false)
   1.567 +		{
   1.568 +			// Nested blocks are not supported
   1.569 +			return false;
   1.570 +		}
   1.571 +
   1.572 +		// Change key to zero (change first position) if false and to last position if true
   1.573 +		if ($key === false || $key === true)
   1.574 +		{
   1.575 +			$key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
   1.576 +		}
   1.577 +
   1.578 +		// Get correct position if array given
   1.579 +		if (is_array($key))
   1.580 +		{
   1.581 +			// Search array to get correct position
   1.582 +			list($search_key, $search_value) = @each($key);
   1.583 +
   1.584 +			$key = NULL;
   1.585 +			foreach ($this->_tpldata[$blockname] as $i => $val_ary)
   1.586 +			{
   1.587 +				if ($val_ary[$search_key] === $search_value)
   1.588 +				{
   1.589 +					$key = $i;
   1.590 +					break;
   1.591 +				}
   1.592 +			}
   1.593 +
   1.594 +			// key/value pair not found
   1.595 +			if ($key === NULL)
   1.596 +			{
   1.597 +				return false;
   1.598 +			}
   1.599 +		}
   1.600 +
   1.601 +		// Insert Block
   1.602 +		if ($mode == 'insert')
   1.603 +		{
   1.604 +			// Make sure we are not exceeding the last iteration
   1.605 +			if ($key >= sizeof($this->_tpldata[$blockname]))
   1.606 +			{
   1.607 +				$key = sizeof($this->_tpldata[$blockname]);
   1.608 +				unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
   1.609 +				$vararray['S_LAST_ROW'] = true;
   1.610 +			}
   1.611 +			else if ($key === 0)
   1.612 +			{
   1.613 +				unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
   1.614 +				$vararray['S_FIRST_ROW'] = true;
   1.615 +			}
   1.616 +
   1.617 +			// Re-position template blocks
   1.618 +			for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
   1.619 +			{
   1.620 +				$this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
   1.621 +				$this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
   1.622 +			}
   1.623 +
   1.624 +			// Insert vararray at given position
   1.625 +			$vararray['S_ROW_COUNT'] = $key;
   1.626 +			$this->_tpldata[$blockname][$key] = $vararray;
   1.627 +
   1.628 +			return true;
   1.629 +		}
   1.630 +
   1.631 +		// Which block to change?
   1.632 +		if ($mode == 'change')
   1.633 +		{
   1.634 +			if ($key == sizeof($this->_tpldata[$blockname]))
   1.635 +			{
   1.636 +				$key--;
   1.637 +			}
   1.638 +
   1.639 +			$this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
   1.640 +			return true;
   1.641 +		}
   1.642 +
   1.643 +		return false;
   1.644 +	}
   1.645 +
   1.646 +	/**
   1.647 +	* Include a separate template
   1.648 +	* @access private
   1.649 +	*/
   1.650 +	function _tpl_include($filename, $include = true)
   1.651 +	{
   1.652 +		$handle = $filename;
   1.653 +		$this->filename[$handle] = $filename;
   1.654 +		$this->files[$handle] = $this->root . '/' . $filename;
   1.655 +		if ($this->inherit_root)
   1.656 +		{
   1.657 +			$this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
   1.658 +		}
   1.659 +
   1.660 +		$filename = $this->_tpl_load($handle);
   1.661 +
   1.662 +		if ($include)
   1.663 +		{
   1.664 +			global $user;
   1.665 +
   1.666 +			if ($filename)
   1.667 +			{
   1.668 +				include($filename);
   1.669 +				return;
   1.670 +			}
   1.671 +			eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
   1.672 +		}
   1.673 +	}
   1.674 +
   1.675 +	/**
   1.676 +	* Include a php-file
   1.677 +	* @access private
   1.678 +	*/
   1.679 +	function _php_include($filename)
   1.680 +	{
   1.681 +		global $phpbb_root_path;
   1.682 +
   1.683 +		$file = $phpbb_root_path . $filename;
   1.684 +
   1.685 +		if (!file_exists($file))
   1.686 +		{
   1.687 +			// trigger_error cannot be used here, as the output already started
   1.688 +			echo 'template->_php_include(): File ' . htmlspecialchars($file) . ' does not exist or is empty';
   1.689 +			return;
   1.690 +		}
   1.691 +		include($file);
   1.692 +	}
   1.693 +}
   1.694 +
   1.695 +?>
   1.696 \ No newline at end of file