5
* @version $Id: template.php,v 1.116 2007/10/04 12:02:03 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
20
* Base Template class.
25
/** variable that holds all the data we'll be substituting into
26
* the compiled templates. Takes form:
27
* --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
28
* if it's a root-level variable, it'll be like this:
29
* --> $this->_tpldata[.][0][varname] == value
31
var $_tpldata = array('.' => array(0 => array()));
34
// Root dir and hash of filenames for each template handle.
38
var $filename = array();
40
// this will hash handle names to the compiled/uncompiled code for that handle.
41
var $compiled_code = array();
44
* Set template location
47
function set_template()
49
global $phpbb_root_path, $user;
51
if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'))
53
$this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
54
$this->cachepath = $phpbb_root_path . 'cache/tpl_' . $user->theme['template_path'] . '_';
58
trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
61
$this->_rootref = &$this->_tpldata['.'][0];
67
* Set custom template location (able to use directory outside of phpBB)
70
function set_custom_template($template_path, $template_name)
72
global $phpbb_root_path;
74
$this->root = $template_path;
75
$this->cachepath = $phpbb_root_path . 'cache/ctpl_' . $template_name . '_';
81
* Sets the template filenames for handles. $filename_array
82
* should be a hash of handle => filename pairs.
85
function set_filenames($filename_array)
87
if (!is_array($filename_array))
92
foreach ($filename_array as $handle => $filename)
96
trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
99
$this->filename[$handle] = $filename;
100
$this->files[$handle] = $this->root . '/' . $filename;
107
* Destroy template data set
112
$this->_tpldata = array('.' => array(0 => array()));
116
* Reset/empty complete block
119
function destroy_block_vars($blockname)
121
if (strpos($blockname, '.') !== false)
124
$blocks = explode('.', $blockname);
125
$blockcount = sizeof($blocks) - 1;
127
$str = &$this->_tpldata;
128
for ($i = 0; $i < $blockcount; $i++)
130
$str = &$str[$blocks[$i]];
131
$str = &$str[sizeof($str) - 1];
134
unset($str[$blocks[$blockcount]]);
139
unset($this->_tpldata[$blockname]);
149
function display($handle, $include_once = true)
151
global $user, $phpbb_hook;
153
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once))
155
if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
157
return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
161
if (defined('IN_ERROR_HANDLER'))
163
if ((E_NOTICE & error_reporting()) == E_NOTICE)
165
error_reporting(error_reporting() ^ E_NOTICE);
169
if ($filename = $this->_tpl_load($handle))
171
($include_once) ? include_once($filename) : include($filename);
175
eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
182
* Display the handle and assign the output to a template variable or return the compiled result.
185
function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
188
$this->display($handle, $include_once);
189
$contents = ob_get_clean();
196
$this->assign_var($template_var, $contents);
202
* Load a compiled template if possible, if not, recompile it
205
function _tpl_load(&$handle)
207
global $user, $phpEx, $config;
209
$filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
211
$recompile = (($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle])) || !file_exists($filename) || @filesize($filename) === 0) ? true : false;
213
// Recompile page if the original template is newer, otherwise load the compiled version
219
global $db, $phpbb_root_path;
221
if (!class_exists('template_compile'))
223
include($phpbb_root_path . 'includes/functions_template.' . $phpEx);
226
$compile = new template_compile($this);
228
// If we don't have a file assigned to this handle, die.
229
if (!isset($this->files[$handle]))
231
trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
234
// Just compile if no user object is present (happens within the installer)
237
$compile->_tpl_load_file($handle);
241
if (isset($user->theme['template_storedb']) && $user->theme['template_storedb'])
244
FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
245
WHERE template_id = ' . $user->theme['template_id'] . "
246
AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "'
247
OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')';
248
$result = $db->sql_query($sql);
249
$row = $db->sql_fetchrow($result);
255
if ($row['template_mtime'] < filemtime($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/' . $row['template_filename']))
257
if ($row['template_filename'] == $this->filename[$handle])
259
$compile->_tpl_load_file($handle);
263
$this->files[$row['template_filename']] = $this->root . '/' . $row['template_filename'];
264
$compile->_tpl_load_file($row['template_filename']);
265
unset($this->compiled_code[$row['template_filename']]);
266
unset($this->files[$row['template_filename']]);
267
unset($this->filename[$row['template_filename']]);
271
if ($row['template_filename'] == $this->filename[$handle])
273
$this->compiled_code[$handle] = $compile->compile(trim($row['template_data']));
274
$compile->compile_write($handle, $this->compiled_code[$handle]);
278
// Only bother compiling if it doesn't already exist
279
if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx))
281
$this->filename[$row['template_filename']] = $row['template_filename'];
282
$compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));
283
unset($this->filename[$row['template_filename']]);
287
while ($row = $db->sql_fetchrow($result));
291
// Try to load from filesystem and instruct to insert into the styles table...
292
$compile->_tpl_load_file($handle, true);
295
$db->sql_freeresult($result);
300
$compile->_tpl_load_file($handle);
305
* Assign key variable pairs from an array
308
function assign_vars($vararray)
310
foreach ($vararray as $key => $val)
312
$this->_rootref[$key] = $val;
319
* Assign a single variable to a single key
322
function assign_var($varname, $varval)
324
$this->_rootref[$varname] = $varval;
330
* Assign key variable pairs from an array to a specified block
333
function assign_block_vars($blockname, $vararray)
335
if (strpos($blockname, '.') !== false)
338
$blocks = explode('.', $blockname);
339
$blockcount = sizeof($blocks) - 1;
341
$str = &$this->_tpldata;
342
for ($i = 0; $i < $blockcount; $i++)
344
$str = &$str[$blocks[$i]];
345
$str = &$str[sizeof($str) - 1];
348
$s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
349
$vararray['S_ROW_COUNT'] = $s_row_count;
351
// Assign S_FIRST_ROW
354
$vararray['S_FIRST_ROW'] = true;
357
// Now the tricky part, we always assign S_LAST_ROW and remove the entry before
358
// This is much more clever than going through the complete template data on display (phew)
359
$vararray['S_LAST_ROW'] = true;
360
if ($s_row_count > 0)
362
unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
365
// Now we add the block that we're actually assigning to.
366
// We're adding a new iteration to this block with the given
367
// variable assignments.
368
$str[$blocks[$blockcount]][] = $vararray;
373
$s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
374
$vararray['S_ROW_COUNT'] = $s_row_count;
376
// Assign S_FIRST_ROW
379
$vararray['S_FIRST_ROW'] = true;
382
// We always assign S_LAST_ROW and remove the entry before
383
$vararray['S_LAST_ROW'] = true;
384
if ($s_row_count > 0)
386
unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
389
// Add a new iteration to this block with the variable assignments we were given.
390
$this->_tpldata[$blockname][] = $vararray;
397
* Change already assigned key variable pair (one-dimensional - single loop entry)
399
* An example of how to use this function:
400
* {@example alter_block_array.php}
402
* @param string $blockname the blockname, for example 'loop'
403
* @param array $vararray the var array to insert/add or merge
404
* @param mixed $key Key to search for
406
* array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
408
* int: Position [the position to change or insert at directly given]
410
* If key is false the position is set to 0
411
* If key is true the position is set to the last entry
413
* @param string $mode Mode to execute (valid modes are 'insert' and 'change')
415
* If insert, the vararray is inserted at the given position (position counting from zero).
416
* 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).
418
* Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
419
* and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
421
* @return bool false on error, true on success
424
function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
426
if (strpos($blockname, '.') !== false)
428
// Nested blocks are not supported
432
// Change key to zero (change first position) if false and to last position if true
433
if ($key === false || $key === true)
435
$key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
438
// Get correct position if array given
441
// Search array to get correct position
442
list($search_key, $search_value) = @each($key);
445
foreach ($this->_tpldata[$blockname] as $i => $val_ary)
447
if ($val_ary[$search_key] === $search_value)
454
// key/value pair not found
462
if ($mode == 'insert')
464
// Make sure we are not exceeding the last iteration
465
if ($key >= sizeof($this->_tpldata[$blockname]))
467
$key = sizeof($this->_tpldata[$blockname]);
468
unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
469
$vararray['S_LAST_ROW'] = true;
473
unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
474
$vararray['S_FIRST_ROW'] = true;
477
// Re-position template blocks
478
for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
480
$this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
481
$this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
484
// Insert vararray at given position
485
$vararray['S_ROW_COUNT'] = $key;
486
$this->_tpldata[$blockname][$key] = $vararray;
491
// Which block to change?
492
if ($mode == 'change')
494
if ($key == sizeof($this->_tpldata[$blockname]))
499
$this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
507
* Include a separate template
510
function _tpl_include($filename, $include = true)
513
$this->filename[$handle] = $filename;
514
$this->files[$handle] = $this->root . '/' . $filename;
516
$filename = $this->_tpl_load($handle);
527
eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
b'\\ No newline at end of file'