1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-05 16:27:38 +02:00

relocate some files

git-svn-id: file:///svn/phpbb/trunk@9225 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen
2008-12-24 14:15:41 +00:00
parent 4e32840ab7
commit ac37f87105
6 changed files with 0 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,422 @@
<?php
/**
*
* @package acm
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Class for grabbing/handling cached entries, extends acm_file or acm_db depending on the setup
* @package acm
*/
class cache
{
/**
* Get config values
*/
public static function obtain_config()
{
global $db, $cache;
if (($config = $cache->get('config')) !== false)
{
$sql = 'SELECT config_name, config_value
FROM ' . CONFIG_TABLE . '
WHERE is_dynamic = 1';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
}
else
{
$config = $cached_config = array();
$sql = 'SELECT config_name, config_value, is_dynamic
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
if (!$row['is_dynamic'])
{
$cached_config[$row['config_name']] = $row['config_value'];
}
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
$cache->put('config', $cached_config);
}
return $config;
}
/**
* Obtain list of naughty words and build preg style replacement arrays for use by the
* calling script
*/
public static function obtain_word_list()
{
global $cache;
if (($censors = $cache->get('_word_censors')) === false)
{
global $db;
$sql = 'SELECT word, replacement
FROM ' . WORDS_TABLE;
$result = $db->sql_query($sql);
$censors = array();
while ($row = $db->sql_fetchrow($result))
{
$censors['match'][] = '#(?<!\w)(' . str_replace('\*', '\w*?', preg_quote($row['word'], '#')) . ')(?!\w)#i';
$censors['replace'][] = $row['replacement'];
}
$db->sql_freeresult($result);
$cache->put('_word_censors', $censors);
}
return $censors;
}
/**
* Obtain currently listed icons
*/
public static function obtain_icons()
{
global $cache;
if (($icons = $cache->get('_icons')) === false)
{
global $db;
// Topic icons
$sql = 'SELECT *
FROM ' . ICONS_TABLE . '
ORDER BY icons_order';
$result = $db->sql_query($sql);
$icons = array();
while ($row = $db->sql_fetchrow($result))
{
$icons[$row['icons_id']]['img'] = $row['icons_url'];
$icons[$row['icons_id']]['width'] = (int) $row['icons_width'];
$icons[$row['icons_id']]['height'] = (int) $row['icons_height'];
$icons[$row['icons_id']]['display'] = (bool) $row['display_on_posting'];
}
$db->sql_freeresult($result);
$cache->put('_icons', $icons);
}
return $icons;
}
/**
* Obtain ranks
*/
public static function obtain_ranks()
{
global $cache;
if (($ranks = $cache->get('_ranks')) === false)
{
global $db;
$sql = 'SELECT *
FROM ' . RANKS_TABLE . '
ORDER BY rank_min DESC';
$result = $db->sql_query($sql);
$ranks = array();
while ($row = $db->sql_fetchrow($result))
{
if ($row['rank_special'])
{
$ranks['special'][$row['rank_id']] = array(
'rank_title' => $row['rank_title'],
'rank_image' => $row['rank_image']
);
}
else
{
$ranks['normal'][] = array(
'rank_title' => $row['rank_title'],
'rank_min' => $row['rank_min'],
'rank_image' => $row['rank_image']
);
}
}
$db->sql_freeresult($result);
$cache->put('_ranks', $ranks);
}
return $ranks;
}
/**
* Obtain allowed extensions
*
* @param mixed $forum_id If false then check for private messaging, if int then check for forum id. If true, then only return extension informations.
*
* @return array allowed extensions array.
*/
public static function obtain_attach_extensions($forum_id)
{
global $cache;
if (($extensions = $cache->get('_extensions')) === false)
{
global $db;
$extensions = array(
'_allowed_post' => array(),
'_allowed_pm' => array(),
);
// The rule is to only allow those extensions defined. ;)
$sql = 'SELECT e.extension, g.*
FROM ' . EXTENSIONS_TABLE . ' e, ' . EXTENSION_GROUPS_TABLE . ' g
WHERE e.group_id = g.group_id
AND (g.allow_group = 1 OR g.allow_in_pm = 1)';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$extension = strtolower(trim($row['extension']));
$extensions[$extension] = array(
'display_cat' => (int) $row['cat_id'],
'download_mode' => (int) $row['download_mode'],
'upload_icon' => trim($row['upload_icon']),
'max_filesize' => (int) $row['max_filesize'],
'allow_group' => $row['allow_group'],
'allow_in_pm' => $row['allow_in_pm'],
);
$allowed_forums = ($row['allowed_forums']) ? unserialize(trim($row['allowed_forums'])) : array();
// Store allowed extensions forum wise
if ($row['allow_group'])
{
$extensions['_allowed_post'][$extension] = (!sizeof($allowed_forums)) ? 0 : $allowed_forums;
}
if ($row['allow_in_pm'])
{
$extensions['_allowed_pm'][$extension] = 0;
}
}
$db->sql_freeresult($result);
$cache->put('_extensions', $extensions);
}
// Forum post
if ($forum_id === false)
{
// We are checking for private messages, therefore we only need to get the pm extensions...
$return = array('_allowed_' => array());
foreach ($extensions['_allowed_pm'] as $extension => $check)
{
$return['_allowed_'][$extension] = 0;
$return[$extension] = $extensions[$extension];
}
$extensions = $return;
}
else if ($forum_id === true)
{
return $extensions;
}
else
{
$forum_id = (int) $forum_id;
$return = array('_allowed_' => array());
foreach ($extensions['_allowed_post'] as $extension => $check)
{
// Check for allowed forums
if (is_array($check))
{
$allowed = (!in_array($forum_id, $check)) ? false : true;
}
else
{
$allowed = true;
}
if ($allowed)
{
$return['_allowed_'][$extension] = 0;
$return[$extension] = $extensions[$extension];
}
}
$extensions = $return;
}
if (!isset($extensions['_allowed_']))
{
$extensions['_allowed_'] = array();
}
return $extensions;
}
/**
* Obtain active bots
*/
public static function obtain_bots()
{
global $cache;
if (($bots = $cache->get('_bots')) === false)
{
global $db;
$sql = 'SELECT user_id, bot_agent, bot_ip
FROM ' . BOTS_TABLE . '
WHERE bot_active = 1
ORDER BY ' . $db->sql_function('length_varchar', 'bot_agent') . 'DESC';
$result = $db->sql_query($sql);
$bots = array();
while ($row = $db->sql_fetchrow($result))
{
$bots[] = $row;
}
$db->sql_freeresult($result);
$cache->put('_bots', $bots);
}
return $bots;
}
/**
* Obtain cfg file data
*
* @param array $theme An array containing the path to the item
*
* @param string $item The specific item to get: 'theme', 'template', or 'imageset'
*
*/
public static function obtain_cfg_item($theme, $item = 'theme')
{
global $config, $cache;
$parsed_array = $cache->get('_cfg_' . $item . '_' . $theme[$item . '_path']);
if ($parsed_array === false)
{
$parsed_array = array();
}
$reparse = false;
$filename = PHPBB_ROOT_PATH . 'styles/' . $theme[$item . '_path'] . '/' . $item . '/' . $item . '.cfg';
if (!file_exists($filename))
{
return $parsed_array;
}
if (!isset($parsed_array['filetime']) || (($config['load_tplcompile'] && @filemtime($filename) > $parsed_array['filetime'])))
{
$reparse = true;
}
// Re-parse cfg file
if ($reparse)
{
$parsed_array = parse_cfg_file($filename);
$parsed_array['filetime'] = @filemtime($filename);
$cache->put('_cfg_' . $item . '_' . $theme[$item . '_path'], $parsed_array);
}
return $parsed_array;
}
/**
* Obtain disallowed usernames
*/
public static function obtain_disallowed_usernames()
{
global $cache;
if (($usernames = $cache->get('_disallowed_usernames')) === false)
{
global $db;
$sql = 'SELECT disallow_username
FROM ' . DISALLOW_TABLE;
$result = $db->sql_query($sql);
$usernames = array();
while ($row = $db->sql_fetchrow($result))
{
$usernames[] = str_replace('%', '.*?', preg_quote(utf8_clean_string($row['disallow_username']), '#'));
}
$db->sql_freeresult($result);
$cache->put('_disallowed_usernames', $usernames);
}
return $usernames;
}
/**
* Obtain hooks...
*/
public static function obtain_hooks()
{
global $cache;
if (($hook_files = $cache->get('_hooks')) === false)
{
$hook_files = array();
// Now search for hooks...
$dh = @opendir(PHPBB_ROOT_PATH . 'includes/hooks/');
if ($dh)
{
while (($file = readdir($dh)) !== false)
{
if (strpos($file, 'hook_') === 0 && substr($file, -(strlen(PHP_EXT) + 1)) === '.' . PHP_EXT)
{
$hook_files[] = substr($file, 0, -(strlen(PHP_EXT) + 1));
}
}
closedir($dh);
}
$cache->put('_hooks', $hook_files);
}
return $hook_files;
}
}
?>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,536 @@
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Base Template class.
* @package phpBB3
*/
class template
{
/**
* variable that holds all the data we'll be substituting into
* the compiled templates. Takes form:
* --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
* if it's a root-level variable, it'll be like this:
* --> $this->_tpldata[.][0][varname] == value
* @var array
*/
private $_tpldata = array('.' => array(0 => array()));
/**
* @var array Reference to template->_tpldata['.'][0]
*/
private $_rootref;
/**
* @var string Root dir for template.
*/
private $root = '';
/**
* @var string Path of the cache directory for the template
*/
public $cachepath = '';
/**
* @var array Hash of handle => file path pairs
*/
public $files = array();
/**
* @var array Hash of handle => filename pairs
*/
public $filename = array();
/**
* Set template location
* @access public
*/
public function set_template()
{
global $user;
if (file_exists(PHPBB_ROOT_PATH . 'styles/' . $user->theme['template_path'] . '/template'))
{
$this->root = PHPBB_ROOT_PATH . 'styles/' . $user->theme['template_path'] . '/template';
$this->cachepath = PHPBB_ROOT_PATH . 'cache/tpl_' . $user->theme['template_path'] . '_';
}
else
{
trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
}
$this->_rootref = &$this->_tpldata['.'][0];
}
/**
* Set custom template location (able to use directory outside of phpBB)
* @access public
* @param string $template_path Path to template directory
* @param string $template_name Name of template
*/
public function set_custom_template($template_path, $template_name)
{
$this->root = $template_path;
$this->cachepath = PHPBB_ROOT_PATH . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_';
}
/**
* Sets the template filenames for handles. $filename_array
* @access public
* @param array $filname_array Should be a hash of handle => filename pairs.
*/
public function set_filenames(array $filename_array)
{
foreach ($filename_array as $handle => $filename)
{
if (empty($filename))
{
trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
}
$this->filename[$handle] = $filename;
$this->files[$handle] = $this->root . '/' . $filename;
}
return true;
}
/**
* Destroy template data set
* @access public
*/
public function __destruct()
{
$this->_tpldata = array('.' => array(0 => array()));
}
/**
* Reset/empty complete block
* @access public
* @param string $blockname Name of block to destroy
*/
public function destroy_block_vars($blockname)
{
if (strpos($blockname, '.') !== false)
{
// Nested block.
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$str = &$this->_tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
$str = &$str[$blocks[$i]];
$str = &$str[sizeof($str) - 1];
}
unset($str[$blocks[$blockcount]]);
}
else
{
// Top-level block.
unset($this->_tpldata[$blockname]);
}
}
/**
* Display handle
* @access public
* @param string $handle Handle to display
* @param bool $include_once Allow multiple inclusions
* @return bool True on success, false on failure
*/
public function display($handle, $include_once = true)
{
global $user, $phpbb_hook;
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once))
{
if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
{
return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
}
}
/* if (defined('IN_ERROR_HANDLER'))
{
if ((E_NOTICE & error_reporting()) == E_NOTICE)
{
//error_reporting(error_reporting() ^ E_NOTICE);
}
}*/
$_tpldata = &$this->_tpldata;
$_rootref = &$this->_rootref;
$_lang = &$user->lang;
// These _are_ used the included files.
$_tpldata; $_rootref; $_lang;
if (($filename = $this->_tpl_load($handle)) !== false)
{
($include_once) ? include_once($filename) : include($filename);
}
else if (($code = $this->_tpl_eval($handle)) !== false)
{
$code = ' ?> ' . $code . ' <?php ';
eval($code);
}
else
{
// if we could not eval AND the file exists, something horrific has occured
return false;
}
return true;
}
/**
* Display the handle and assign the output to a template variable or return the compiled result.
* @access public
* @param string $handle Handle to operate on
* @param string $template_var Template variable to assign compiled handle to
* @param bool $return_content If true return compiled handle, otherwise assign to $template_var
* @param bool $include_once Allow multiple inclusions of the file
* @return bool|string If $return_content is true return string of the compiled handle, otherwise return true
*/
public function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
{
ob_start();
$this->display($handle, $include_once);
$contents = ob_get_clean();
if ($return_content)
{
return $contents;
}
$this->assign_var($template_var, $contents);
return true;
}
/**
* Load a compiled template if possible, if not, recompile it
* @access private
* @param string $handle Handle of the template to load
* @return string|bool Return filename on success otherwise false
* @uses template_compile is used to compile uncached templates
*/
private function _tpl_load($handle)
{
global $config;
$filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . PHP_EXT;
$recompile = (!file_exists($filename) || @filesize($filename) === 0 || ($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle]))) ? true : false;
// Recompile page if the original template is newer, otherwise load the compiled version
if ($recompile)
{
if (!class_exists('template_compile'))
{
include(PHPBB_ROOT_PATH . 'includes/functions_template.' . PHP_EXT);
}
$compile = new template_compile($this);
// If we don't have a file assigned to this handle, die.
if (!isset($this->files[$handle]))
{
trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
}
if ($compile->_tpl_load_file($handle) === false)
{
return false;
}
}
return $filename;
}
/**
* This code should only run when some high level error prevents us from writing to the cache.
* @access private
* @param string $handle Template handle to compile
* @return string|bool Return compiled code on success otherwise false
* @uses template_compile is used to compile template
*/
private function _tpl_eval($handle)
{
if (!class_exists('template_compile'))
{
include(PHPBB_ROOT_PATH . 'includes/functions_template.' . PHP_EXT);
}
$compile = new template_compile($this);
// If we don't have a file assigned to this handle, die.
if (!isset($this->files[$handle]))
{
trigger_error("template->_tpl_eval(): No file specified for handle $handle", E_USER_ERROR);
}
if (($code = $compile->_tpl_gen_src($handle)) === false)
{
return false;
}
return $code;
}
/**
* Assign key variable pairs from an array
* @access public
* @param array $vararray A hash of variable name => value pairs
*/
public function assign_vars(array $vararray)
{
foreach ($vararray as $key => $val)
{
$this->_rootref[$key] = $val;
}
}
/**
* Assign a single variable to a single key
* @access public
* @param string $varname Variable name
* @param string $varval Value to assign to variable
*/
public function assign_var($varname, $varval)
{
$this->_rootref[$varname] = $varval;
}
/**
* Assign key variable pairs from an array to a specified block
* @access public
* @param string $blockname Name of block to assign $vararray to
* @param array $vararray A hash of variable name => value pairs
*/
public function assign_block_vars($blockname, array $vararray)
{
if (strpos($blockname, '.') !== false)
{
// Nested block.
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$str = &$this->_tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
$str = &$str[$blocks[$i]];
$str = &$str[sizeof($str) - 1];
}
// Now we add the block that we're actually assigning to.
// We're adding a new iteration to this block with the given
// variable assignments.
$str[$blocks[$blockcount]][] = $vararray;
}
else
{
// Top-level block.
// Add a new iteration to this block with the variable assignments we were given.
$this->_tpldata[$blockname][] = $vararray;
}
}
/**
* Change already assigned key variable pair (one-dimensional - single loop entry)
*
* An example of how to use this function:
*
* @param string $blockname the blockname, for example 'loop'
* @param array $vararray the var array to insert/add or merge
* @param mixed $key Key to search for
*
* array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
*
* int: Position [the position to change or insert at directly given]
*
* If key is false the position is set to 0
* If key is true the position is set to the last entry
*
* @param string $mode Mode to execute (valid modes are 'insert' and 'change')
*
* If insert, the vararray is inserted at the given position (position counting from zero).
* 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).
*
* Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
* and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
*
* @return bool false on error, true on success
* @access public
*/
public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
{
if (strpos($blockname, '.') !== false)
{
// Nested block.
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$block = &$this->_tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
if (($pos = strpos($blocks[$i], '[')) !== false)
{
$name = substr($blocks[$i], 0, $pos);
if (strpos($blocks[$i], '[]') === $pos)
{
$index = sizeof($block[$name]) - 1;
}
else
{
$index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1);
}
}
else
{
$name = $blocks[$i];
$index = sizeof($block[$name]) - 1;
}
$block = &$block[$name];
$block = &$block[$index];
}
$block = &$block[$blocks[$i]]; // Traverse the last block
}
else
{
// Top-level block.
$block = &$this->_tpldata[$blockname];
}
// Change key to zero (change first position) if false and to last position if true
if ($key === false || $key === true)
{
$key = ($key === false) ? 0 : sizeof($block);
}
// Get correct position if array given
if (is_array($key))
{
// Search array to get correct position
list($search_key, $search_value) = @each($key);
$key = NULL;
foreach ($block as $i => $val_ary)
{
if ($val_ary[$search_key] === $search_value)
{
$key = $i;
break;
}
}
// key/value pair not found
if ($key === NULL)
{
return false;
}
}
// Insert Block
if ($mode == 'insert')
{
// Re-position template blocks
for ($i = sizeof($block); $i > $key; $i--)
{
$block[$i] = $block[$i-1];
}
// Insert vararray at given position
$block[$key] = $vararray;
return true;
}
// Which block to change?
if ($mode == 'change')
{
if ($key == sizeof($block))
{
$key--;
}
$block[$key] = array_merge($block[$key], $vararray);
return true;
}
return false;
}
/**
* Include a separate template
* @access private
* @param string $filename Template filename to include
* @param bool $include True to include the file, false to just load it
* @uses template_compile is used to compile uncached templates
*/
private function _tpl_include($filename, $include = true)
{
$handle = $filename;
$this->filename[$handle] = $filename;
$this->files[$handle] = $this->root . '/' . $filename;
$filename = $this->_tpl_load($handle);
if ($include)
{
global $user;
$_tpldata = &$this->_tpldata;
$_rootref = &$this->_rootref;
$_lang = &$user->lang;
// These _are_ used the included files.
$_tpldata; $_rootref; $_lang;
if ($filename)
{
include($filename);
return;
}
else
{
if (!class_exists('template_compile'))
{
include(PHPBB_ROOT_PATH . 'includes/functions_template.' . PHP_EXT);
}
$compile = new template_compile($this);
if (($code = $compile->_tpl_gen_src($handle)) !== false)
{
$code = ' ?> ' . $code . ' <?php ';
eval($code);
}
}
}
}
}
?>

View File

@@ -0,0 +1,869 @@
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* The template filter that does the actual compilation
* @see template_compile
* @package phpBB3
*
*/
class template_filter extends php_user_filter
{
/**
* @var string Replaceable tokens regex
*/
private $regex = '~<!-- ([A-Z][A-Z_0-9]+)(?: (.*?) ?)?-->|{((?:[a-z][a-z_0-9]+\.)*\\$?[A-Z][A-Z_0-9]+)}~';
/**
* @var array
*/
private $block_names = array();
/**
* @var array
*/
private $block_else_level = array();
/**
* @var string
*/
private $chunk;
public function filter($in, $out, &$consumed, $closing)
{
$written = false;
while ($bucket = stream_bucket_make_writeable($in))
{
$consumed += $bucket->datalen;
$data = $this->chunk . $bucket->data;
$last_nl = strrpos($data, "\n");
$this->chunk = substr($data, $last_nl);
$data = substr($data, 0, $last_nl);
if (!strlen($data))
{
continue;
}
$written = true;
$bucket->data = $this->compile($data);
$bucket->datalen = strlen($bucket->data);
stream_bucket_append($out, $bucket);
}
if ($closing && strlen($this->chunk))
{
$written = true;
$bucket = stream_bucket_new($this->stream, $this->compile($this->chunk));
stream_bucket_append($out, $bucket);
}
return $written ? PSFS_PASS_ON : PSFS_FEED_ME;
}
public function onCreate()
{
$this->chunk = '';
return true;
}
private function compile($data)
{
$data = preg_replace('#<(?:[\\?%]|script)#s', '<?php echo\'\\0\';?>', $data);
return str_replace('?><?php', '', preg_replace_callback($this->regex, array($this, 'replace'), $data));
}
private function replace($matches)
{
global $config;
if (isset($matches[3]))
{
return $this->compile_var_tags($matches[0]);
}
switch ($matches[1])
{
case 'BEGIN':
$this->block_else_level[] = false;
return '<?php ' . $this->compile_tag_block($matches[2]) . ' ?>';
break;
case 'BEGINELSE':
$this->block_else_level[sizeof($this->block_else_level) - 1] = true;
return '<?php }} else { ?>';
break;
case 'END':
array_pop($this->block_names);
return '<?php ' . ((array_pop($this->block_else_level)) ? '}' : '}}') . ' ?>';
break;
case 'IF':
return '<?php ' . $this->compile_tag_if($matches[2], false) . ' ?>';
break;
case 'ELSE':
return '<?php } else { ?>';
break;
case 'ELSEIF':
return '<?php ' . $this->compile_tag_if($matches[2], true) . ' ?>';
break;
case 'ENDIF':
return '<?php } ?>';
break;
case 'DEFINE':
return '<?php ' . $this->compile_tag_define($matches[2], true) . ' ?>';
break;
case 'UNDEFINE':
return '<?php ' . $this->compile_tag_define($matches[2], false) . ' ?>';
break;
case 'INCLUDE':
return '<?php ' . $this->compile_tag_include($matches[2]) . ' ?>';
break;
case 'INCLUDEPHP':
return ($config['tpl_allow_php']) ? '<?php ' . $this->compile_tag_include_php($matches[2]) . ' ?>' : '';
break;
case 'PHP':
return ($config['tpl_allow_php']) ? '<?php ' : '<!-- ';
break;
case 'ENDPHP':
return ($config['tpl_allow_php']) ? ' ?>' : ' -->';
break;
default:
return $matches[0];
break;
}
return '';
}
/**
* Compile variables
* @access private
*/
private function compile_var_tags(&$text_blocks)
{
// change template varrefs into PHP varrefs
$varrefs = array();
// This one will handle varrefs WITH namespaces
preg_match_all('#\{((?:[a-z0-9\-_]+\.)+)(\$)?([A-Z0-9\-_]+)\}#', $text_blocks, $varrefs, PREG_SET_ORDER);
foreach ($varrefs as $var_val)
{
$namespace = $var_val[1];
$varname = $var_val[3];
$new = $this->generate_block_varref($namespace, $varname, true, $var_val[2]);
$text_blocks = str_replace($var_val[0], $new, $text_blocks);
}
// This will handle the remaining root-level varrefs
// transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array
if (strpos($text_blocks, '{L_') !== false)
{
$text_blocks = preg_replace('#\{L_([a-z0-9\-_]*)\}#is', "<?php echo ((isset(\$_rootref['L_\\1'])) ? \$_rootref['L_\\1'] : ((isset(\$_lang['\\1'])) ? \$_lang['\\1'] : '{ \\1 }')); ?>", $text_blocks);
}
// Handle addslashed language variables prefixed with LA_
// If a template variable already exist, it will be used in favor of it...
if (strpos($text_blocks, '{LA_') !== false)
{
$text_blocks = preg_replace('#\{LA_([a-z0-9\-_]*)\}#is', "<?php echo ((isset(\$_rootref['LA_\\1'])) ? \$_rootref['LA_\\1'] : ((isset(\$_rootref['L_\\1'])) ? addslashes(\$_rootref['L_\\1']) : ((isset(\$_lang['\\1'])) ? addslashes(\$_lang['\\1']) : '{ \\1 }'))); ?>", $text_blocks);
}
// Handle remaining varrefs
$text_blocks = preg_replace('#\{([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$_rootref['\\1'])) ? \$_rootref['\\1'] : ''; ?>", $text_blocks);
$text_blocks = preg_replace('#\{\$([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$_tpldata['DEFINE']['.']['\\1'])) ? \$_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks);
return $text_blocks;
}
/**
* Compile blocks
* @access private
*/
private function compile_tag_block($tag_args)
{
$no_nesting = false;
// Is the designer wanting to call another loop in a loop?
// <!-- BEGIN loop -->
// <!-- BEGIN !loop2 -->
// <!-- END !loop2 -->
// <!-- END loop -->
// 'loop2' is actually on the same nesting level as 'loop' you assign
// variables to it with template->assign_block_vars('loop2', array(...))
if (strpos($tag_args, '!') === 0)
{
// Count the number if ! occurrences (not allowed in vars)
$no_nesting = substr_count($tag_args, '!');
$tag_args = substr($tag_args, $no_nesting);
}
// Allow for control of looping (indexes start from zero):
// foo(2) : Will start the loop on the 3rd entry
// foo(-2) : Will start the loop two entries from the end
// foo(3,4) : Will start the loop on the fourth entry and end it on the fifth
// foo(3,-4) : Will start the loop on the fourth entry and end it four from last
$match = array();
if (preg_match('#^([^()]*)\(([\-\d]+)(?:,([\-\d]+))?\)$#', $tag_args, $match))
{
$tag_args = $match[1];
if ($match[2] < 0)
{
$loop_start = '($_' . $tag_args . '_count ' . $match[2] . ' < 0 ? 0 : $_' . $tag_args . '_count ' . $match[2] . ')';
}
else
{
$loop_start = '($_' . $tag_args . '_count < ' . $match[2] . ' ? $_' . $tag_args . '_count : ' . $match[2] . ')';
}
if (strlen($match[3]) < 1 || $match[3] == -1)
{
$loop_end = '$_' . $tag_args . '_count';
}
else if ($match[3] >= 0)
{
$loop_end = '(' . ($match[3] + 1) . ' > $_' . $tag_args . '_count ? $_' . $tag_args . '_count : ' . ($match[3] + 1) . ')';
}
else //if ($match[3] < -1)
{
$loop_end = '$_' . $tag_args . '_count' . ($match[3] + 1);
}
}
else
{
$loop_start = 0;
$loop_end = '$_' . $tag_args . '_count';
}
$tag_template_php = '';
array_push($this->block_names, $tag_args);
if ($no_nesting !== false)
{
// We need to implode $no_nesting times from the end...
$block = array_slice($this->block_names, -$no_nesting);
}
else
{
$block = $this->block_names;
}
if (sizeof($block) < 2)
{
// Block is not nested.
$tag_template_php = '$_' . $tag_args . "_count = (isset(\$_tpldata['$tag_args'])) ? sizeof(\$_tpldata['$tag_args']) : 0;";
$varref = "\$_tpldata['$tag_args']";
}
else
{
// This block is nested.
// Generate a namespace string for this block.
$namespace = implode('.', $block);
// Get a reference to the data array for this block that depends on the
// current indices of all parent blocks.
$varref = $this->generate_block_data_ref($namespace, false);
// Create the for loop code to iterate over this block.
$tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;';
}
$tag_template_php .= 'if ($_' . $tag_args . '_count) {';
/**
* The following uses foreach for iteration instead of a for loop, foreach is faster but requires PHP to make a copy of the contents of the array which uses more memory
* <code>
* if (!$offset)
* {
* $tag_template_php .= 'foreach (' . $varref . ' as $_' . $tag_args . '_i => $_' . $tag_args . '_val){';
* }
* </code>
*/
$tag_template_php .= 'for ($_' . $tag_args . '_i = ' . $loop_start . '; $_' . $tag_args . '_i < ' . $loop_end . '; ++$_' . $tag_args . '_i){';
$tag_template_php .= '$_'. $tag_args . '_val = &' . $varref . '[$_'. $tag_args. '_i];';
return $tag_template_php;
}
/**
* Compile a general expression - much of this is from Smarty with
* some adaptions for our block level methods
* @access private
*/
private function compile_expression($tag_args)
{
$match = array();
preg_match_all('/(?:
"[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' |
[(),] |
[^\s(),]+)/x', $tag_args, $match);
$tokens = $match[0];
$is_arg_stack = array();
for ($i = 0, $size = sizeof($tokens); $i < $size; $i++)
{
$token = &$tokens[$i];
switch ($token)
{
case '!==':
case '===':
case '<<':
case '>>':
case '|':
case '^':
case '&':
case '~':
case ')':
case ',':
case '+':
case '-':
case '*':
case '/':
case '@':
break;
case '==':
case 'eq':
$token = '==';
break;
case '!=':
case '<>':
case 'ne':
case 'neq':
$token = '!=';
break;
case '<':
case 'lt':
$token = '<';
break;
case '<=':
case 'le':
case 'lte':
$token = '<=';
break;
case '>':
case 'gt':
$token = '>';
break;
case '>=':
case 'ge':
case 'gte':
$token = '>=';
break;
case '&&':
case 'and':
$token = '&&';
break;
case '||':
case 'or':
$token = '||';
break;
case '!':
case 'not':
$token = '!';
break;
case '%':
case 'mod':
$token = '%';
break;
case '(':
array_push($is_arg_stack, $i);
break;
case 'is':
$is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1;
$is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start));
$new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
array_splice($tokens, $is_arg_start, sizeof($tokens), $new_tokens);
$i = $is_arg_start;
// no break
default:
$varrefs = array();
if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs))
{
if (!empty($varrefs[1]))
{
$namespace = substr($varrefs[1], 0, -1);
$namespace = (strpos($namespace, '.') === false) ? $namespace : strrchr($namespace, '.');
// S_ROW_COUNT is deceptive, it returns the current row number not the number of rows
// hence S_ROW_COUNT is deprecated in favour of S_ROW_NUM
switch ($varrefs[3])
{
case 'S_ROW_NUM':
case 'S_ROW_COUNT':
$token = "\$_${namespace}_i";
break;
case 'S_NUM_ROWS':
$token = "\$_${namespace}_count";
break;
case 'S_FIRST_ROW':
$token = "(\$_${namespace}_i == 0)";
break;
case 'S_LAST_ROW':
$token = "(\$_${namespace}_i == \$_${namespace}_count - 1)";
break;
case 'S_BLOCK_NAME':
$token = "'$namespace'";
break;
default:
$token = $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']';
break;
}
}
else
{
$token = ($varrefs[2]) ? '$_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$_rootref[\'' . $varrefs[3] . '\']';
}
}
else if (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs))
{
// Allow checking if loops are set with .loopname
// It is also possible to check the loop count by doing <!-- IF .loopname > 1 --> for example
$blocks = explode('.', $varrefs[1]);
// If the block is nested, we have a reference that we can grab.
// If the block is not nested, we just go and grab the block from _tpldata
if (sizeof($blocks) > 1)
{
$block = array_pop($blocks);
$namespace = implode('.', $blocks);
$varref = $this->generate_block_data_ref($namespace, true);
// Add the block reference for the last child.
$varref .= "['" . $block . "']";
}
else
{
$varref = '$_tpldata';
// Add the block reference for the last child.
$varref .= "['" . $blocks[0] . "']";
}
$token = "isset($varref) && sizeof($varref)";
}
break;
}
}
return $tokens;
}
private function compile_tag_if($tag_args, $elseif)
{
$tokens = $this->compile_expression($tag_args);
return (($elseif) ? '} else if (' : 'if (') . (implode(' ', $tokens) . ') { ');
}
/**
* Compile DEFINE tags
* @access private
*/
private function compile_tag_define($tag_args, $op)
{
$match = array();
preg_match('#^((?:[a-z0-9\-_]+\.)+)?\$(?=[A-Z])([A-Z0-9_\-]*)(?: = (.*?))?$#', $tag_args, $match);
if (empty($match[2]) || (!isset($match[3]) && $op))
{
return '';
}
if (!$op)
{
return 'unset(' . (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ');';
}
$parsed_statement = implode(' ', $this->compile_expression($match[3]));
return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ' = ' . $parsed_statement . ';';
}
/**
* Compile INCLUDE tag
* @access private
*/
private function compile_tag_include($tag_args)
{
return "\$this->_tpl_include('$tag_args');";
}
/**
* Compile INCLUDE_PHP tag
* @access private
*/
private function compile_tag_include_php($tag_args)
{
return "include('" . $tag_args . "');";
}
/**
* parse expression
* This is from Smarty
* @access private
*/
private function _parse_is_expr($is_arg, $tokens)
{
$expr_end = 0;
$negate_expr = false;
if (($first_token = array_shift($tokens)) == 'not')
{
$negate_expr = true;
$expr_type = array_shift($tokens);
}
else
{
$expr_type = $first_token;
}
switch ($expr_type)
{
case 'even':
if (@$tokens[$expr_end] == 'by')
{
$expr_end++;
$expr_arg = $tokens[$expr_end++];
$expr = "!(($is_arg / $expr_arg) & 1)";
}
else
{
$expr = "!($is_arg & 1)";
}
break;
case 'odd':
if (@$tokens[$expr_end] == 'by')
{
$expr_end++;
$expr_arg = $tokens[$expr_end++];
$expr = "(($is_arg / $expr_arg) & 1)";
}
else
{
$expr = "($is_arg & 1)";
}
break;
case 'div':
if (@$tokens[$expr_end] == 'by')
{
$expr_end++;
$expr_arg = $tokens[$expr_end++];
$expr = "!($is_arg % $expr_arg)";
}
break;
}
if ($negate_expr)
{
$expr = "!($expr)";
}
array_splice($tokens, 0, $expr_end, $expr);
return $tokens;
}
/**
* Generates a reference to the given variable inside the given (possibly nested)
* block namespace. This is a string of the form:
* ' . $_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '
* It's ready to be inserted into an "echo" line in one of the templates.
*
* @access private
* @param string $namespace Namespace to access (expects a trailing "." on the namespace)
* @param string $varname Variable name to use
* @param bool $echo If true return an echo statement, otherwise a reference to the internal variable
* @param bool $defop If true this is a variable created with the DEFINE construct, otherwise template variable
* @return string Code to access variable or echo it if $echo is true
*/
private function generate_block_varref($namespace, $varname, $echo = true, $defop = false)
{
// Strip the trailing period.
$namespace = substr($namespace, 0, -1);
$expr = true;
// S_ROW_COUNT is deceptive, it returns the current row number not the number of rows
// hence S_ROW_COUNT is deprecated in favour of S_ROW_NUM
switch ($varname)
{
case 'S_ROW_NUM':
case 'S_ROW_COUNT':
$varref = "\$_${namespace}_i";
break;
case 'S_NUM_ROWS':
$varref = "\$_${namespace}_count";
break;
case 'S_FIRST_ROW':
$varref = "(\$_${namespace}_i == 0)";
break;
case 'S_LAST_ROW':
$varref = "(\$_${namespace}_i == \$_${namespace}_count - 1)";
break;
case 'S_BLOCK_NAME':
$varref = "'$namespace'";
break;
default:
// Get a reference to the data block for this namespace.
$varref = $this->generate_block_data_ref($namespace, true, $defop);
// Prepend the necessary code to stick this in an echo line.
// Append the variable reference.
$varref .= "['$varname']";
$expr = false;
break;
}
// @todo Test the !$expr more
$varref = ($echo) ? "<?php echo $varref; ?>" : (($expr || isset($varref)) ? $varref : '');
return $varref;
}
/**
* Generates a reference to the array of data values for the given
* (possibly nested) block namespace. This is a string of the form:
* $_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN']
*
* @access private
* @param string $blockname Block to access (does not expect a trailing "." on the blockname)
* @param bool $include_last_iterator If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above.
* @param bool $defop If true this is a variable created with the DEFINE construct, otherwise template variable
* @return string Code to access variable
*/
private function generate_block_data_ref($blockname, $include_last_iterator, $defop = false)
{
// Get an array of the blocks involved.
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
// DEFINE is not an element of any referenced variable, we must use _tpldata to access it
if ($defop)
{
$varref = '$_tpldata[\'DEFINE\']';
// Build up the string with everything but the last child.
for ($i = 0; $i < $blockcount; $i++)
{
$varref .= "['" . $blocks[$i] . "'][\$_" . $blocks[$i] . '_i]';
}
// Add the block reference for the last child.
$varref .= "['" . $blocks[$blockcount] . "']";
// Add the iterator for the last child if requried.
if ($include_last_iterator)
{
$varref .= '[$_' . $blocks[$blockcount] . '_i]';
}
return $varref;
}
else if ($include_last_iterator)
{
return '$_'. $blocks[$blockcount] . '_val';
}
else
{
return '$_'. $blocks[$blockcount - 1] . '_val[\''. $blocks[$blockcount]. '\']';
}
}
}
stream_filter_register('template', 'template_filter');
/**
* Extension of template class - Functions needed for compiling templates only.
*
* psoTFX, phpBB Development Team - Completion of file caching, decompilation
* routines and implementation of conditionals/keywords and associated changes
*
* The interface was inspired by PHPLib templates, and the template file (formats are
* quite similar)
*
* The keyword/conditional implementation is currently based on sections of code from
* the Smarty templating engine (c) 2001 ispi of Lincoln, Inc. which is released
* (on its own and in whole) under the LGPL. Section 3 of the LGPL states that any code
* derived from an LGPL application may be relicenced under the GPL, this applies
* to this source
*
* DEFINE directive inspired by a request by Cyberalien
*
* @package phpBB3
* @uses template_filter As a PHP stream filter to perform compilation of templates
*/
class template_compile
{
/**
* @var template Reference to the {@link template template} object performing compilation
*/
private $template;
/**
* Constructor
* @param template $template {@link template Template} object performing compilation
*/
function __construct(template $template)
{
$this->template = $template;
}
/**
* Load template source from file
* @access public
* @param string $handle Template handle we wish to load
* @return bool Return true on success otherwise false
*/
public function _tpl_load_file($handle)
{
// Try and open template for read
if (!file_exists($this->template->files[$handle]))
{
trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
}
// Actually compile the code now.
return $this->compile_write($handle, $this->template->files[$handle]);
}
/**
* Load template source from file
* @access public
* @param string $handle Template handle we wish to compile
* @return string|bool Return compiled code on successful compilation otherwise false
*/
public function _tpl_gen_src($handle)
{
// Try and open template for read
if (!file_exists($this->template->files[$handle]))
{
trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
}
// Actually compile the code now.
return $this->compile_gen($this->template->files[$handle]);
}
/**
* Write compiled file to cache directory
* @access private
* @param string $handle Template handle to compile
* @param string $source_file Source template file
* @return bool Return true on success otherwise false
*/
private function compile_write($handle, $source_file)
{
$filename = $this->template->cachepath . str_replace('/', '.', $this->template->filename[$handle]) . '.' . PHP_EXT;
$source_handle = @fopen($source_file, 'rb');
$destination_handle = @fopen($filename, 'wb');
if (!$source_handle || !$destination_handle)
{
return false;
}
@flock($destination_handle, LOCK_EX);
stream_filter_append($source_handle, 'template');
stream_copy_to_stream($source_handle, $destination_handle);
@fclose($source_handle);
@flock($destination_handle, LOCK_UN);
@fclose($destination_handle);
phpbb_chmod($filename, CHMOD_WRITE);
clearstatcache();
return true;
}
/**
* Generate source for eval()
* @access private
* @param string $source_file Source template file
* @return string|bool Return compiled code on successful compilation otherwise false
*/
private function compile_gen($source_file)
{
$source_handle = @fopen($source_file, 'rb');
$destination_handle = @fopen('php://temp' ,'r+b');
if (!$source_handle || !$destination_handle)
{
return false;
}
stream_filter_append($source_handle, 'template');
stream_copy_to_stream($source_handle, $destination_handle);
@fclose($source_handle);
rewind($destination_handle);
return stream_get_contents($destination_handle);
}
}
?>