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();
39 var $files_inherit = array();
40 var $files_template = array();
41 var $inherit_root = '';
42 var $orig_tpl_storedb;
43 var $orig_tpl_inherits_id;
45 // this will hash handle names to the compiled/uncompiled code for that handle.
46 var $compiled_code = array();
49 * Set template location
52 function set_template()
54 global $phpbb_root_path, $user;
56 if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'))
58 $this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
59 $this->cachepath = $phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $user->theme['template_path']) . '_';
61 if ($this->orig_tpl_storedb === null)
63 $this->orig_tpl_storedb = $user->theme['template_storedb'];
66 if ($this->orig_tpl_inherits_id === null)
68 $this->orig_tpl_inherits_id = $user->theme['template_inherits_id'];
71 $user->theme['template_storedb'] = $this->orig_tpl_storedb;
72 $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
74 if ($user->theme['template_inherits_id'])
76 $this->inherit_root = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template';
81 trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
84 $this->_rootref = &$this->_tpldata['.'][0];
90 * Set custom template location (able to use directory outside of phpBB)
93 function set_custom_template($template_path, $template_name, $fallback_template_path = false)
95 global $phpbb_root_path, $user;
97 // Make sure $template_path has no ending slash
98 if (substr($template_path, -1) == '/')
100 $template_path = substr($template_path, 0, -1);
103 $this->root = $template_path;
104 $this->cachepath = $phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_';
106 if ($fallback_template_path !== false)
108 if (substr($fallback_template_path, -1) == '/')
110 $fallback_template_path = substr($fallback_template_path, 0, -1);
113 $this->inherit_root = $fallback_template_path;
114 $this->orig_tpl_inherits_id = true;
118 $this->orig_tpl_inherits_id = false;
121 // the database does not store the path or name of a custom template
122 // so there is no way we can properly store custom templates there
123 $this->orig_tpl_storedb = false;
125 $this->_rootref = &$this->_tpldata['.'][0];
131 * Sets the template filenames for handles. $filename_array
132 * should be a hash of handle => filename pairs.
135 function set_filenames($filename_array)
137 if (!is_array($filename_array))
141 foreach ($filename_array as $handle => $filename)
143 if (empty($filename))
145 trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
148 $this->filename[$handle] = $filename;
149 $this->files[$handle] = $this->root . '/' . $filename;
151 if ($this->inherit_root)
153 $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
161 * Destroy template data set
166 $this->_tpldata = array('.' => array(0 => array()));
167 $this->_rootref = &$this->_tpldata['.'][0];
171 * Reset/empty complete block
174 function destroy_block_vars($blockname)
176 if (strpos($blockname, '.') !== false)
179 $blocks = explode('.', $blockname);
180 $blockcount = sizeof($blocks) - 1;
182 $str = &$this->_tpldata;
183 for ($i = 0; $i < $blockcount; $i++)
185 $str = &$str[$blocks[$i]];
186 $str = &$str[sizeof($str) - 1];
189 unset($str[$blocks[$blockcount]]);
194 unset($this->_tpldata[$blockname]);
204 function display($handle, $include_once = true)
206 global $user, $phpbb_hook;
208 if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this))
210 if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
212 return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
216 if (defined('IN_ERROR_HANDLER'))
218 if ((E_NOTICE & error_reporting()) == E_NOTICE)
220 error_reporting(error_reporting() ^ E_NOTICE);
224 if ($filename = $this->_tpl_load($handle))
226 ($include_once) ? include_once($filename) : include($filename);
230 eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
237 * Display the handle and assign the output to a template variable or return the compiled result.
240 function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
243 $this->display($handle, $include_once);
244 $contents = ob_get_clean();
251 $this->assign_var($template_var, $contents);
257 * Load a compiled template if possible, if not, recompile it
260 function _tpl_load(&$handle)
262 global $user, $phpEx, $config;
264 if (!isset($this->filename[$handle]))
266 trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
269 // reload these settings to have the values they had when this object was initialised
270 // using set_template or set_custom_template, they might otherwise have been overwritten
271 // by other template class instances in between.
272 $user->theme['template_storedb'] = $this->orig_tpl_storedb;
273 $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
275 $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
276 $this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0;
279 if (!file_exists($filename) || @filesize($filename) === 0 || defined('DEBUG_EXTRA'))
283 else if ($config['load_tplcompile'])
285 // No way around it: we need to check inheritance here
286 if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
288 $this->files[$handle] = $this->files_inherit[$handle];
289 $this->files_template[$handle] = $user->theme['template_inherits_id'];
291 $recompile = (@filemtime($filename) < filemtime($this->files[$handle])) ? true : false;
294 // Recompile page if the original template is newer, otherwise load the compiled version
300 global $db, $phpbb_root_path;
302 if (!class_exists('template_compile'))
304 include($phpbb_root_path . 'includes/functions_template.' . $phpEx);
307 // Inheritance - we point to another template file for this one. Equality is also used for store_db
308 if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
310 $this->files[$handle] = $this->files_inherit[$handle];
311 $this->files_template[$handle] = $user->theme['template_inherits_id'];
314 $compile = new template_compile($this);
316 // If we don't have a file assigned to this handle, die.
317 if (!isset($this->files[$handle]))
319 trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
322 // Just compile if no user object is present (happens within the installer)
325 $compile->_tpl_load_file($handle);
329 if (isset($user->theme['template_storedb']) && $user->theme['template_storedb'])
334 if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
336 $ids[] = $user->theme['template_inherits_id'];
338 $ids[] = $user->theme['template_id'];
340 foreach ($ids as $id)
343 FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
344 WHERE template_id = ' . $id . "
345 AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "'
346 OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')';
348 $result = $db->sql_query($sql);
349 while ($row = $db->sql_fetchrow($result))
351 $rows[$row['template_filename']] = $row;
353 $db->sql_freeresult($result);
358 foreach ($rows as $row)
360 $file = $this->root . '/' . $row['template_filename'];
361 $force_reload = false;
362 if ($row['template_id'] != $user->theme['template_id'])
364 // make sure that we are not overlooking a file not in the db yet
365 if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
367 $file = $this->inherit_root . '/' . $row['template_filename'];
368 $this->files[$row['template_filename']] = $file;
369 $this->files_inherit[$row['template_filename']] = $file;
370 $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
372 else if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
374 // Ok, we have a situation. There is a file in the subtemplate, but nothing in the DB. We have to fix that.
375 $force_reload = true;
376 $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
381 $this->files_template[$row['template_filename']] = $user->theme['template_id'];
384 if ($force_reload || $row['template_mtime'] < filemtime($file))
386 if ($row['template_filename'] == $this->filename[$handle])
388 $compile->_tpl_load_file($handle, true);
392 $this->files[$row['template_filename']] = $file;
393 $this->filename[$row['template_filename']] = $row['template_filename'];
394 $compile->_tpl_load_file($row['template_filename'], true);
395 unset($this->compiled_code[$row['template_filename']]);
396 unset($this->files[$row['template_filename']]);
397 unset($this->filename[$row['template_filename']]);
401 if ($row['template_filename'] == $this->filename[$handle])
403 $this->compiled_code[$handle] = $compile->compile(trim($row['template_data']));
404 $compile->compile_write($handle, $this->compiled_code[$handle]);
408 // Only bother compiling if it doesn't already exist
409 if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx))
411 $this->filename[$row['template_filename']] = $row['template_filename'];
412 $compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));
413 unset($this->filename[$row['template_filename']]);
420 $file = $this->root . '/' . $row['template_filename'];
422 if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
424 $file = $this->inherit_root . '/' . $row['template_filename'];
425 $this->files[$row['template_filename']] = $file;
426 $this->files_inherit[$row['template_filename']] = $file;
427 $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
429 // Try to load from filesystem and instruct to insert into the styles table...
430 $compile->_tpl_load_file($handle, true);
437 $compile->_tpl_load_file($handle);
442 * Assign key variable pairs from an array
445 function assign_vars($vararray)
447 foreach ($vararray as $key => $val)
449 $this->_rootref[$key] = $val;
456 * Assign a single variable to a single key
459 function assign_var($varname, $varval)
461 $this->_rootref[$varname] = $varval;
467 * Assign key variable pairs from an array to a specified block
470 function assign_block_vars($blockname, $vararray)
472 if (strpos($blockname, '.') !== false)
475 $blocks = explode('.', $blockname);
476 $blockcount = sizeof($blocks) - 1;
478 $str = &$this->_tpldata;
479 for ($i = 0; $i < $blockcount; $i++)
481 $str = &$str[$blocks[$i]];
482 $str = &$str[sizeof($str) - 1];
485 $s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
486 $vararray['S_ROW_COUNT'] = $s_row_count;
488 // Assign S_FIRST_ROW
491 $vararray['S_FIRST_ROW'] = true;
494 // Now the tricky part, we always assign S_LAST_ROW and remove the entry before
495 // This is much more clever than going through the complete template data on display (phew)
496 $vararray['S_LAST_ROW'] = true;
497 if ($s_row_count > 0)
499 unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
502 // Now we add the block that we're actually assigning to.
503 // We're adding a new iteration to this block with the given
504 // variable assignments.
505 $str[$blocks[$blockcount]][] = $vararray;
510 $s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
511 $vararray['S_ROW_COUNT'] = $s_row_count;
513 // Assign S_FIRST_ROW
516 $vararray['S_FIRST_ROW'] = true;
519 // We always assign S_LAST_ROW and remove the entry before
520 $vararray['S_LAST_ROW'] = true;
521 if ($s_row_count > 0)
523 unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
526 // Add a new iteration to this block with the variable assignments we were given.
527 $this->_tpldata[$blockname][] = $vararray;
534 * Change already assigned key variable pair (one-dimensional - single loop entry)
536 * An example of how to use this function:
537 * {@example alter_block_array.php}
539 * @param string $blockname the blockname, for example 'loop'
540 * @param array $vararray the var array to insert/add or merge
541 * @param mixed $key Key to search for
543 * array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
545 * int: Position [the position to change or insert at directly given]
547 * If key is false the position is set to 0
548 * If key is true the position is set to the last entry
550 * @param string $mode Mode to execute (valid modes are 'insert' and 'change')
552 * If insert, the vararray is inserted at the given position (position counting from zero).
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).
555 * Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
556 * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
558 * @return bool false on error, true on success
561 function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
563 if (strpos($blockname, '.') !== false)
565 // Nested blocks are not supported
569 // Change key to zero (change first position) if false and to last position if true
570 if ($key === false || $key === true)
572 $key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
575 // Get correct position if array given
578 // Search array to get correct position
579 list($search_key, $search_value) = @each($key);
582 foreach ($this->_tpldata[$blockname] as $i => $val_ary)
584 if ($val_ary[$search_key] === $search_value)
591 // key/value pair not found
599 if ($mode == 'insert')
601 // Make sure we are not exceeding the last iteration
602 if ($key >= sizeof($this->_tpldata[$blockname]))
604 $key = sizeof($this->_tpldata[$blockname]);
605 unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
606 $vararray['S_LAST_ROW'] = true;
610 unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
611 $vararray['S_FIRST_ROW'] = true;
614 // Re-position template blocks
615 for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
617 $this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
618 $this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
621 // Insert vararray at given position
622 $vararray['S_ROW_COUNT'] = $key;
623 $this->_tpldata[$blockname][$key] = $vararray;
628 // Which block to change?
629 if ($mode == 'change')
631 if ($key == sizeof($this->_tpldata[$blockname]))
636 $this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
644 * Include a separate template
647 function _tpl_include($filename, $include = true)
650 $this->filename[$handle] = $filename;
651 $this->files[$handle] = $this->root . '/' . $filename;
652 if ($this->inherit_root)
654 $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
657 $filename = $this->_tpl_load($handle);
668 eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
676 function _php_include($filename)
678 global $phpbb_root_path;
680 $file = $phpbb_root_path . $filename;
682 if (!file_exists($file))
684 // trigger_error cannot be used here, as the output already started
685 echo 'template->_php_include(): File ' . htmlspecialchars($file) . ' does not exist or is empty';