includes/template.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:47:37 +0400
changeset 16 d6012dd9347f
parent 0 ceef6d64c21d
permissions -rw-r--r--
Removing old cubometry-style style
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Base Template class.
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class template
indvdum@0
    24
{
indvdum@0
    25
	/** variable that holds all the data we'll be substituting into
indvdum@0
    26
	* the compiled templates. Takes form:
indvdum@0
    27
	* --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
indvdum@0
    28
	* if it's a root-level variable, it'll be like this:
indvdum@0
    29
	* --> $this->_tpldata[.][0][varname] == value
indvdum@0
    30
	*/
indvdum@0
    31
	var $_tpldata = array('.' => array(0 => array()));
indvdum@0
    32
	var $_rootref;
indvdum@0
    33
indvdum@0
    34
	// Root dir and hash of filenames for each template handle.
indvdum@0
    35
	var $root = '';
indvdum@0
    36
	var $cachepath = '';
indvdum@0
    37
	var $files = array();
indvdum@0
    38
	var $filename = array();
indvdum@0
    39
	var $files_inherit = array();
indvdum@0
    40
	var $files_template = array();
indvdum@0
    41
	var $inherit_root = '';
indvdum@0
    42
	var $orig_tpl_storedb;
indvdum@0
    43
	var $orig_tpl_inherits_id;
indvdum@0
    44
indvdum@0
    45
	// this will hash handle names to the compiled/uncompiled code for that handle.
indvdum@0
    46
	var $compiled_code = array();
indvdum@0
    47
indvdum@0
    48
	/**
indvdum@0
    49
	* Set template location
indvdum@0
    50
	* @access public
indvdum@0
    51
	*/
indvdum@0
    52
	function set_template()
indvdum@0
    53
	{
indvdum@0
    54
		global $phpbb_root_path, $user;
indvdum@0
    55
indvdum@0
    56
		if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'))
indvdum@0
    57
		{
indvdum@0
    58
			$this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
indvdum@0
    59
			$this->cachepath = $phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $user->theme['template_path']) . '_';
indvdum@0
    60
indvdum@0
    61
			if ($this->orig_tpl_storedb === null)
indvdum@0
    62
			{
indvdum@0
    63
				$this->orig_tpl_storedb = $user->theme['template_storedb'];
indvdum@0
    64
			}
indvdum@0
    65
indvdum@0
    66
			if ($this->orig_tpl_inherits_id === null)
indvdum@0
    67
			{
indvdum@0
    68
				$this->orig_tpl_inherits_id = $user->theme['template_inherits_id'];
indvdum@0
    69
			}
indvdum@0
    70
indvdum@0
    71
			$user->theme['template_storedb'] = $this->orig_tpl_storedb;
indvdum@0
    72
			$user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
indvdum@0
    73
indvdum@0
    74
			if ($user->theme['template_inherits_id'])
indvdum@0
    75
			{
indvdum@0
    76
				$this->inherit_root = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template';
indvdum@0
    77
			}
indvdum@0
    78
		}
indvdum@0
    79
		else
indvdum@0
    80
		{
indvdum@0
    81
			trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
indvdum@0
    82
		}
indvdum@0
    83
indvdum@0
    84
		$this->_rootref = &$this->_tpldata['.'][0];
indvdum@0
    85
indvdum@0
    86
		return true;
indvdum@0
    87
	}
indvdum@0
    88
indvdum@0
    89
	/**
indvdum@0
    90
	* Set custom template location (able to use directory outside of phpBB)
indvdum@0
    91
	* @access public
indvdum@0
    92
	*/
indvdum@0
    93
	function set_custom_template($template_path, $template_name, $fallback_template_path = false)
indvdum@0
    94
	{
indvdum@0
    95
		global $phpbb_root_path, $user;
indvdum@0
    96
indvdum@0
    97
		// Make sure $template_path has no ending slash
indvdum@0
    98
		if (substr($template_path, -1) == '/')
indvdum@0
    99
		{
indvdum@0
   100
			$template_path = substr($template_path, 0, -1);
indvdum@0
   101
		}
indvdum@0
   102
indvdum@0
   103
		$this->root = $template_path;
indvdum@0
   104
		$this->cachepath = $phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_';
indvdum@0
   105
indvdum@0
   106
		if ($fallback_template_path !== false)
indvdum@0
   107
		{
indvdum@0
   108
			if (substr($fallback_template_path, -1) == '/')
indvdum@0
   109
			{
indvdum@0
   110
				$fallback_template_path = substr($fallback_template_path, 0, -1);
indvdum@0
   111
			}
indvdum@0
   112
indvdum@0
   113
			$this->inherit_root = $fallback_template_path;
indvdum@0
   114
			$this->orig_tpl_inherits_id = true;
indvdum@0
   115
		}
indvdum@0
   116
		else
indvdum@0
   117
		{
indvdum@0
   118
			$this->orig_tpl_inherits_id = false;
indvdum@0
   119
		}
indvdum@0
   120
indvdum@0
   121
		// the database does not store the path or name of a custom template
indvdum@0
   122
		// so there is no way we can properly store custom templates there
indvdum@0
   123
		$this->orig_tpl_storedb = false;
indvdum@0
   124
indvdum@0
   125
		$this->_rootref = &$this->_tpldata['.'][0];
indvdum@0
   126
indvdum@0
   127
		return true;
indvdum@0
   128
	}
indvdum@0
   129
indvdum@0
   130
	/**
indvdum@0
   131
	* Sets the template filenames for handles. $filename_array
indvdum@0
   132
	* should be a hash of handle => filename pairs.
indvdum@0
   133
	* @access public
indvdum@0
   134
	*/
indvdum@0
   135
	function set_filenames($filename_array)
indvdum@0
   136
	{
indvdum@0
   137
		if (!is_array($filename_array))
indvdum@0
   138
		{
indvdum@0
   139
			return false;
indvdum@0
   140
		}
indvdum@0
   141
		foreach ($filename_array as $handle => $filename)
indvdum@0
   142
		{
indvdum@0
   143
			if (empty($filename))
indvdum@0
   144
			{
indvdum@0
   145
				trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
indvdum@0
   146
			}
indvdum@0
   147
indvdum@0
   148
			$this->filename[$handle] = $filename;
indvdum@0
   149
			$this->files[$handle] = $this->root . '/' . $filename;
indvdum@0
   150
indvdum@0
   151
			if ($this->inherit_root)
indvdum@0
   152
			{
indvdum@0
   153
				$this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
indvdum@0
   154
			}
indvdum@0
   155
		}
indvdum@0
   156
indvdum@0
   157
		return true;
indvdum@0
   158
	}
indvdum@0
   159
indvdum@0
   160
	/**
indvdum@0
   161
	* Destroy template data set
indvdum@0
   162
	* @access public
indvdum@0
   163
	*/
indvdum@0
   164
	function destroy()
indvdum@0
   165
	{
indvdum@0
   166
		$this->_tpldata = array('.' => array(0 => array()));
indvdum@0
   167
		$this->_rootref = &$this->_tpldata['.'][0];
indvdum@0
   168
	}
indvdum@0
   169
indvdum@0
   170
	/**
indvdum@0
   171
	* Reset/empty complete block
indvdum@0
   172
	* @access public
indvdum@0
   173
	*/
indvdum@0
   174
	function destroy_block_vars($blockname)
indvdum@0
   175
	{
indvdum@0
   176
		if (strpos($blockname, '.') !== false)
indvdum@0
   177
		{
indvdum@0
   178
			// Nested block.
indvdum@0
   179
			$blocks = explode('.', $blockname);
indvdum@0
   180
			$blockcount = sizeof($blocks) - 1;
indvdum@0
   181
indvdum@0
   182
			$str = &$this->_tpldata;
indvdum@0
   183
			for ($i = 0; $i < $blockcount; $i++)
indvdum@0
   184
			{
indvdum@0
   185
				$str = &$str[$blocks[$i]];
indvdum@0
   186
				$str = &$str[sizeof($str) - 1];
indvdum@0
   187
			}
indvdum@0
   188
indvdum@0
   189
			unset($str[$blocks[$blockcount]]);
indvdum@0
   190
		}
indvdum@0
   191
		else
indvdum@0
   192
		{
indvdum@0
   193
			// Top-level block.
indvdum@0
   194
			unset($this->_tpldata[$blockname]);
indvdum@0
   195
		}
indvdum@0
   196
indvdum@0
   197
		return true;
indvdum@0
   198
	}
indvdum@0
   199
indvdum@0
   200
	/**
indvdum@0
   201
	* Display handle
indvdum@0
   202
	* @access public
indvdum@0
   203
	*/
indvdum@0
   204
	function display($handle, $include_once = true)
indvdum@0
   205
	{
indvdum@0
   206
		global $user, $phpbb_hook;
indvdum@0
   207
indvdum@0
   208
		if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this))
indvdum@0
   209
		{
indvdum@0
   210
			if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
indvdum@0
   211
			{
indvdum@0
   212
				return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
indvdum@0
   213
			}
indvdum@0
   214
		}
indvdum@0
   215
indvdum@0
   216
		if (defined('IN_ERROR_HANDLER'))
indvdum@0
   217
		{
indvdum@0
   218
			if ((E_NOTICE & error_reporting()) == E_NOTICE)
indvdum@0
   219
			{
indvdum@0
   220
				error_reporting(error_reporting() ^ E_NOTICE);
indvdum@0
   221
			}
indvdum@0
   222
		}
indvdum@0
   223
indvdum@0
   224
		if ($filename = $this->_tpl_load($handle))
indvdum@0
   225
		{
indvdum@0
   226
			($include_once) ? include_once($filename) : include($filename);
indvdum@0
   227
		}
indvdum@0
   228
		else
indvdum@0
   229
		{
indvdum@0
   230
			eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
indvdum@0
   231
		}
indvdum@0
   232
indvdum@0
   233
		return true;
indvdum@0
   234
	}
indvdum@0
   235
indvdum@0
   236
	/**
indvdum@0
   237
	* Display the handle and assign the output to a template variable or return the compiled result.
indvdum@0
   238
	* @access public
indvdum@0
   239
	*/
indvdum@0
   240
	function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
indvdum@0
   241
	{
indvdum@0
   242
		ob_start();
indvdum@0
   243
		$this->display($handle, $include_once);
indvdum@0
   244
		$contents = ob_get_clean();
indvdum@0
   245
indvdum@0
   246
		if ($return_content)
indvdum@0
   247
		{
indvdum@0
   248
			return $contents;
indvdum@0
   249
		}
indvdum@0
   250
indvdum@0
   251
		$this->assign_var($template_var, $contents);
indvdum@0
   252
indvdum@0
   253
		return true;
indvdum@0
   254
	}
indvdum@0
   255
indvdum@0
   256
	/**
indvdum@0
   257
	* Load a compiled template if possible, if not, recompile it
indvdum@0
   258
	* @access private
indvdum@0
   259
	*/
indvdum@0
   260
	function _tpl_load(&$handle)
indvdum@0
   261
	{
indvdum@0
   262
		global $user, $phpEx, $config;
indvdum@0
   263
indvdum@0
   264
		if (!isset($this->filename[$handle]))
indvdum@0
   265
		{
indvdum@0
   266
			trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
indvdum@0
   267
		}
indvdum@0
   268
indvdum@0
   269
		// reload these settings to have the values they had when this object was initialised
indvdum@0
   270
		// using set_template or set_custom_template, they might otherwise have been overwritten
indvdum@0
   271
		// by other template class instances in between.
indvdum@0
   272
		$user->theme['template_storedb'] = $this->orig_tpl_storedb;
indvdum@0
   273
		$user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
indvdum@0
   274
indvdum@0
   275
		$filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
indvdum@0
   276
		$this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0;
indvdum@0
   277
indvdum@0
   278
		$recompile = false;
indvdum@0
   279
		if (!file_exists($filename) || @filesize($filename) === 0 || defined('DEBUG_EXTRA'))
indvdum@0
   280
		{
indvdum@0
   281
			$recompile = true;
indvdum@0
   282
		}
indvdum@0
   283
		else if ($config['load_tplcompile'])
indvdum@0
   284
		{
indvdum@0
   285
			// No way around it: we need to check inheritance here
indvdum@0
   286
			if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
indvdum@0
   287
			{
indvdum@0
   288
				$this->files[$handle] = $this->files_inherit[$handle];
indvdum@0
   289
				$this->files_template[$handle] = $user->theme['template_inherits_id'];
indvdum@0
   290
			}
indvdum@0
   291
			$recompile = (@filemtime($filename) < filemtime($this->files[$handle])) ? true : false;
indvdum@0
   292
		}
indvdum@0
   293
indvdum@0
   294
		// Recompile page if the original template is newer, otherwise load the compiled version
indvdum@0
   295
		if (!$recompile)
indvdum@0
   296
		{
indvdum@0
   297
			return $filename;
indvdum@0
   298
		}
indvdum@0
   299
indvdum@0
   300
		global $db, $phpbb_root_path;
indvdum@0
   301
indvdum@0
   302
		if (!class_exists('template_compile'))
indvdum@0
   303
		{
indvdum@0
   304
			include($phpbb_root_path . 'includes/functions_template.' . $phpEx);
indvdum@0
   305
		}
indvdum@0
   306
indvdum@0
   307
		// Inheritance - we point to another template file for this one. Equality is also used for store_db
indvdum@0
   308
		if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
indvdum@0
   309
		{
indvdum@0
   310
			$this->files[$handle] = $this->files_inherit[$handle];
indvdum@0
   311
			$this->files_template[$handle] = $user->theme['template_inherits_id'];
indvdum@0
   312
		}
indvdum@0
   313
indvdum@0
   314
		$compile = new template_compile($this);
indvdum@0
   315
indvdum@0
   316
		// If we don't have a file assigned to this handle, die.
indvdum@0
   317
		if (!isset($this->files[$handle]))
indvdum@0
   318
		{
indvdum@0
   319
			trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
indvdum@0
   320
		}
indvdum@0
   321
indvdum@0
   322
		// Just compile if no user object is present (happens within the installer)
indvdum@0
   323
		if (!$user)
indvdum@0
   324
		{
indvdum@0
   325
			$compile->_tpl_load_file($handle);
indvdum@0
   326
			return false;
indvdum@0
   327
		}
indvdum@0
   328
indvdum@0
   329
		if (isset($user->theme['template_storedb']) && $user->theme['template_storedb'])
indvdum@0
   330
		{
indvdum@0
   331
			$rows = array();
indvdum@0
   332
			$ids = array();
indvdum@0
   333
			// Inheritance
indvdum@0
   334
			if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
indvdum@0
   335
			{
indvdum@0
   336
				$ids[] = $user->theme['template_inherits_id'];
indvdum@0
   337
			}
indvdum@0
   338
			$ids[] = $user->theme['template_id'];
indvdum@0
   339
indvdum@0
   340
			foreach ($ids as $id)
indvdum@0
   341
			{
indvdum@0
   342
				$sql = 'SELECT *
indvdum@0
   343
				FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
indvdum@0
   344
				WHERE template_id = ' . $id . "
indvdum@0
   345
					AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "'
indvdum@0
   346
						OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')';
indvdum@0
   347
indvdum@0
   348
				$result = $db->sql_query($sql);
indvdum@0
   349
				while ($row = $db->sql_fetchrow($result))
indvdum@0
   350
				{
indvdum@0
   351
					$rows[$row['template_filename']] = $row;
indvdum@0
   352
				}
indvdum@0
   353
				$db->sql_freeresult($result);
indvdum@0
   354
			}
indvdum@0
   355
indvdum@0
   356
			if (sizeof($rows))
indvdum@0
   357
			{
indvdum@0
   358
				foreach ($rows as $row)
indvdum@0
   359
				{
indvdum@0
   360
					$file = $this->root . '/' . $row['template_filename'];
indvdum@0
   361
					$force_reload = false;
indvdum@0
   362
					if ($row['template_id'] != $user->theme['template_id'])
indvdum@0
   363
					{
indvdum@0
   364
						// make sure that we are not overlooking a file not in the db yet
indvdum@0
   365
						if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
indvdum@0
   366
						{
indvdum@0
   367
							$file = $this->inherit_root . '/' . $row['template_filename'];
indvdum@0
   368
							$this->files[$row['template_filename']] = $file;
indvdum@0
   369
							$this->files_inherit[$row['template_filename']] = $file;
indvdum@0
   370
							$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
indvdum@0
   371
						}
indvdum@0
   372
						else if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
indvdum@0
   373
						{
indvdum@0
   374
							// Ok, we have a situation. There is a file in the subtemplate, but nothing in the DB. We have to fix that.
indvdum@0
   375
							$force_reload = true;
indvdum@0
   376
							$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
indvdum@0
   377
						}
indvdum@0
   378
					}
indvdum@0
   379
					else
indvdum@0
   380
					{
indvdum@0
   381
						$this->files_template[$row['template_filename']] = $user->theme['template_id'];
indvdum@0
   382
					}
indvdum@0
   383
indvdum@0
   384
					if ($force_reload || $row['template_mtime'] < filemtime($file))
indvdum@0
   385
					{
indvdum@0
   386
						if ($row['template_filename'] == $this->filename[$handle])
indvdum@0
   387
						{
indvdum@0
   388
							$compile->_tpl_load_file($handle, true);
indvdum@0
   389
						}
indvdum@0
   390
						else
indvdum@0
   391
						{
indvdum@0
   392
							$this->files[$row['template_filename']] = $file;
indvdum@0
   393
							$this->filename[$row['template_filename']] = $row['template_filename'];
indvdum@0
   394
							$compile->_tpl_load_file($row['template_filename'], true);
indvdum@0
   395
							unset($this->compiled_code[$row['template_filename']]);
indvdum@0
   396
							unset($this->files[$row['template_filename']]);
indvdum@0
   397
							unset($this->filename[$row['template_filename']]);
indvdum@0
   398
						}
indvdum@0
   399
					}
indvdum@0
   400
indvdum@0
   401
					if ($row['template_filename'] == $this->filename[$handle])
indvdum@0
   402
					{
indvdum@0
   403
						$this->compiled_code[$handle] = $compile->compile(trim($row['template_data']));
indvdum@0
   404
						$compile->compile_write($handle, $this->compiled_code[$handle]);
indvdum@0
   405
					}
indvdum@0
   406
					else
indvdum@0
   407
					{
indvdum@0
   408
						// Only bother compiling if it doesn't already exist
indvdum@0
   409
						if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx))
indvdum@0
   410
						{
indvdum@0
   411
							$this->filename[$row['template_filename']] = $row['template_filename'];
indvdum@0
   412
							$compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));
indvdum@0
   413
							unset($this->filename[$row['template_filename']]);
indvdum@0
   414
						}
indvdum@0
   415
					}
indvdum@0
   416
				}
indvdum@0
   417
			}
indvdum@0
   418
			else
indvdum@0
   419
			{
indvdum@0
   420
				$file = $this->root . '/' . $row['template_filename'];
indvdum@0
   421
indvdum@0
   422
				if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
indvdum@0
   423
				{
indvdum@0
   424
					$file = $this->inherit_root . '/' . $row['template_filename'];
indvdum@0
   425
					$this->files[$row['template_filename']] = $file;
indvdum@0
   426
					$this->files_inherit[$row['template_filename']] = $file;
indvdum@0
   427
					$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
indvdum@0
   428
				}
indvdum@0
   429
				// Try to load from filesystem and instruct to insert into the styles table...
indvdum@0
   430
				$compile->_tpl_load_file($handle, true);
indvdum@0
   431
				return false;
indvdum@0
   432
			}
indvdum@0
   433
indvdum@0
   434
			return false;
indvdum@0
   435
		}
indvdum@0
   436
indvdum@0
   437
		$compile->_tpl_load_file($handle);
indvdum@0
   438
		return false;
indvdum@0
   439
	}
indvdum@0
   440
indvdum@0
   441
	/**
indvdum@0
   442
	* Assign key variable pairs from an array
indvdum@0
   443
	* @access public
indvdum@0
   444
	*/
indvdum@0
   445
	function assign_vars($vararray)
indvdum@0
   446
	{
indvdum@0
   447
		foreach ($vararray as $key => $val)
indvdum@0
   448
		{
indvdum@0
   449
			$this->_rootref[$key] = $val;
indvdum@0
   450
		}
indvdum@0
   451
indvdum@0
   452
		return true;
indvdum@0
   453
	}
indvdum@0
   454
indvdum@0
   455
	/**
indvdum@0
   456
	* Assign a single variable to a single key
indvdum@0
   457
	* @access public
indvdum@0
   458
	*/
indvdum@0
   459
	function assign_var($varname, $varval)
indvdum@0
   460
	{
indvdum@0
   461
		$this->_rootref[$varname] = $varval;
indvdum@0
   462
indvdum@0
   463
		return true;
indvdum@0
   464
	}
indvdum@0
   465
indvdum@0
   466
	/**
indvdum@0
   467
	* Assign key variable pairs from an array to a specified block
indvdum@0
   468
	* @access public
indvdum@0
   469
	*/
indvdum@0
   470
	function assign_block_vars($blockname, $vararray)
indvdum@0
   471
	{
indvdum@0
   472
		if (strpos($blockname, '.') !== false)
indvdum@0
   473
		{
indvdum@0
   474
			// Nested block.
indvdum@0
   475
			$blocks = explode('.', $blockname);
indvdum@0
   476
			$blockcount = sizeof($blocks) - 1;
indvdum@0
   477
indvdum@0
   478
			$str = &$this->_tpldata;
indvdum@0
   479
			for ($i = 0; $i < $blockcount; $i++)
indvdum@0
   480
			{
indvdum@0
   481
				$str = &$str[$blocks[$i]];
indvdum@0
   482
				$str = &$str[sizeof($str) - 1];
indvdum@0
   483
			}
indvdum@0
   484
indvdum@0
   485
			$s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
indvdum@0
   486
			$vararray['S_ROW_COUNT'] = $s_row_count;
indvdum@0
   487
indvdum@0
   488
			// Assign S_FIRST_ROW
indvdum@0
   489
			if (!$s_row_count)
indvdum@0
   490
			{
indvdum@0
   491
				$vararray['S_FIRST_ROW'] = true;
indvdum@0
   492
			}
indvdum@0
   493
indvdum@0
   494
			// Now the tricky part, we always assign S_LAST_ROW and remove the entry before
indvdum@0
   495
			// This is much more clever than going through the complete template data on display (phew)
indvdum@0
   496
			$vararray['S_LAST_ROW'] = true;
indvdum@0
   497
			if ($s_row_count > 0)
indvdum@0
   498
			{
indvdum@0
   499
				unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
indvdum@0
   500
			}
indvdum@0
   501
indvdum@0
   502
			// Now we add the block that we're actually assigning to.
indvdum@0
   503
			// We're adding a new iteration to this block with the given
indvdum@0
   504
			// variable assignments.
indvdum@0
   505
			$str[$blocks[$blockcount]][] = $vararray;
indvdum@0
   506
		}
indvdum@0
   507
		else
indvdum@0
   508
		{
indvdum@0
   509
			// Top-level block.
indvdum@0
   510
			$s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
indvdum@0
   511
			$vararray['S_ROW_COUNT'] = $s_row_count;
indvdum@0
   512
indvdum@0
   513
			// Assign S_FIRST_ROW
indvdum@0
   514
			if (!$s_row_count)
indvdum@0
   515
			{
indvdum@0
   516
				$vararray['S_FIRST_ROW'] = true;
indvdum@0
   517
			}
indvdum@0
   518
indvdum@0
   519
			// We always assign S_LAST_ROW and remove the entry before
indvdum@0
   520
			$vararray['S_LAST_ROW'] = true;
indvdum@0
   521
			if ($s_row_count > 0)
indvdum@0
   522
			{
indvdum@0
   523
				unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
indvdum@0
   524
			}
indvdum@0
   525
indvdum@0
   526
			// Add a new iteration to this block with the variable assignments we were given.
indvdum@0
   527
			$this->_tpldata[$blockname][] = $vararray;
indvdum@0
   528
		}
indvdum@0
   529
indvdum@0
   530
		return true;
indvdum@0
   531
	}
indvdum@0
   532
indvdum@0
   533
	/**
indvdum@0
   534
	* Change already assigned key variable pair (one-dimensional - single loop entry)
indvdum@0
   535
	*
indvdum@0
   536
	* An example of how to use this function:
indvdum@0
   537
	* {@example alter_block_array.php}
indvdum@0
   538
	*
indvdum@0
   539
	* @param	string	$blockname	the blockname, for example 'loop'
indvdum@0
   540
	* @param	array	$vararray	the var array to insert/add or merge
indvdum@0
   541
	* @param	mixed	$key		Key to search for
indvdum@0
   542
	*
indvdum@0
   543
	* array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
indvdum@0
   544
	*
indvdum@0
   545
	* int: Position [the position to change or insert at directly given]
indvdum@0
   546
	*
indvdum@0
   547
	* If key is false the position is set to 0
indvdum@0
   548
	* If key is true the position is set to the last entry
indvdum@0
   549
	*
indvdum@0
   550
	* @param	string	$mode		Mode to execute (valid modes are 'insert' and 'change')
indvdum@0
   551
	*
indvdum@0
   552
	*	If insert, the vararray is inserted at the given position (position counting from zero).
indvdum@0
   553
	*	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).
indvdum@0
   554
	*
indvdum@0
   555
	* Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
indvdum@0
   556
	* and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
indvdum@0
   557
	*
indvdum@0
   558
	* @return bool false on error, true on success
indvdum@0
   559
	* @access public
indvdum@0
   560
	*/
indvdum@0
   561
	function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
indvdum@0
   562
	{
indvdum@0
   563
		if (strpos($blockname, '.') !== false)
indvdum@0
   564
		{
indvdum@0
   565
			// Nested blocks are not supported
indvdum@0
   566
			return false;
indvdum@0
   567
		}
indvdum@0
   568
indvdum@0
   569
		// Change key to zero (change first position) if false and to last position if true
indvdum@0
   570
		if ($key === false || $key === true)
indvdum@0
   571
		{
indvdum@0
   572
			$key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
indvdum@0
   573
		}
indvdum@0
   574
indvdum@0
   575
		// Get correct position if array given
indvdum@0
   576
		if (is_array($key))
indvdum@0
   577
		{
indvdum@0
   578
			// Search array to get correct position
indvdum@0
   579
			list($search_key, $search_value) = @each($key);
indvdum@0
   580
indvdum@0
   581
			$key = NULL;
indvdum@0
   582
			foreach ($this->_tpldata[$blockname] as $i => $val_ary)
indvdum@0
   583
			{
indvdum@0
   584
				if ($val_ary[$search_key] === $search_value)
indvdum@0
   585
				{
indvdum@0
   586
					$key = $i;
indvdum@0
   587
					break;
indvdum@0
   588
				}
indvdum@0
   589
			}
indvdum@0
   590
indvdum@0
   591
			// key/value pair not found
indvdum@0
   592
			if ($key === NULL)
indvdum@0
   593
			{
indvdum@0
   594
				return false;
indvdum@0
   595
			}
indvdum@0
   596
		}
indvdum@0
   597
indvdum@0
   598
		// Insert Block
indvdum@0
   599
		if ($mode == 'insert')
indvdum@0
   600
		{
indvdum@0
   601
			// Make sure we are not exceeding the last iteration
indvdum@0
   602
			if ($key >= sizeof($this->_tpldata[$blockname]))
indvdum@0
   603
			{
indvdum@0
   604
				$key = sizeof($this->_tpldata[$blockname]);
indvdum@0
   605
				unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
indvdum@0
   606
				$vararray['S_LAST_ROW'] = true;
indvdum@0
   607
			}
indvdum@0
   608
			else if ($key === 0)
indvdum@0
   609
			{
indvdum@0
   610
				unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
indvdum@0
   611
				$vararray['S_FIRST_ROW'] = true;
indvdum@0
   612
			}
indvdum@0
   613
indvdum@0
   614
			// Re-position template blocks
indvdum@0
   615
			for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
indvdum@0
   616
			{
indvdum@0
   617
				$this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
indvdum@0
   618
				$this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
indvdum@0
   619
			}
indvdum@0
   620
indvdum@0
   621
			// Insert vararray at given position
indvdum@0
   622
			$vararray['S_ROW_COUNT'] = $key;
indvdum@0
   623
			$this->_tpldata[$blockname][$key] = $vararray;
indvdum@0
   624
indvdum@0
   625
			return true;
indvdum@0
   626
		}
indvdum@0
   627
indvdum@0
   628
		// Which block to change?
indvdum@0
   629
		if ($mode == 'change')
indvdum@0
   630
		{
indvdum@0
   631
			if ($key == sizeof($this->_tpldata[$blockname]))
indvdum@0
   632
			{
indvdum@0
   633
				$key--;
indvdum@0
   634
			}
indvdum@0
   635
indvdum@0
   636
			$this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
indvdum@0
   637
			return true;
indvdum@0
   638
		}
indvdum@0
   639
indvdum@0
   640
		return false;
indvdum@0
   641
	}
indvdum@0
   642
indvdum@0
   643
	/**
indvdum@0
   644
	* Include a separate template
indvdum@0
   645
	* @access private
indvdum@0
   646
	*/
indvdum@0
   647
	function _tpl_include($filename, $include = true)
indvdum@0
   648
	{
indvdum@0
   649
		$handle = $filename;
indvdum@0
   650
		$this->filename[$handle] = $filename;
indvdum@0
   651
		$this->files[$handle] = $this->root . '/' . $filename;
indvdum@0
   652
		if ($this->inherit_root)
indvdum@0
   653
		{
indvdum@0
   654
			$this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
indvdum@0
   655
		}
indvdum@0
   656
indvdum@0
   657
		$filename = $this->_tpl_load($handle);
indvdum@0
   658
indvdum@0
   659
		if ($include)
indvdum@0
   660
		{
indvdum@0
   661
			global $user;
indvdum@0
   662
indvdum@0
   663
			if ($filename)
indvdum@0
   664
			{
indvdum@0
   665
				include($filename);
indvdum@0
   666
				return;
indvdum@0
   667
			}
indvdum@0
   668
			eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
indvdum@0
   669
		}
indvdum@0
   670
	}
indvdum@0
   671
indvdum@0
   672
	/**
indvdum@0
   673
	* Include a php-file
indvdum@0
   674
	* @access private
indvdum@0
   675
	*/
indvdum@0
   676
	function _php_include($filename)
indvdum@0
   677
	{
indvdum@0
   678
		global $phpbb_root_path;
indvdum@0
   679
indvdum@0
   680
		$file = $phpbb_root_path . $filename;
indvdum@0
   681
indvdum@0
   682
		if (!file_exists($file))
indvdum@0
   683
		{
indvdum@0
   684
			// trigger_error cannot be used here, as the output already started
indvdum@0
   685
			echo 'template->_php_include(): File ' . htmlspecialchars($file) . ' does not exist or is empty';
indvdum@0
   686
			return;
indvdum@0
   687
		}
indvdum@0
   688
		include($file);
indvdum@0
   689
	}
indvdum@0
   690
}
indvdum@0
   691
indvdum@0
   692
?>