2013-06-10 12:59:47 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
* @copyright (c) 2013 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Twig Template class.
|
|
|
|
* @package phpBB3
|
|
|
|
*/
|
|
|
|
class phpbb_template_twig implements phpbb_template
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Template context.
|
|
|
|
* Stores template data used during template rendering.
|
2013-06-11 09:41:15 -05:00
|
|
|
* @var phpbb_template_context
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $context;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path of the cache directory for the template
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $cachepath = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phpBB root path
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $phpbb_root_path;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
2013-07-01 13:39:51 -05:00
|
|
|
/**
|
|
|
|
* adm relative path
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $adm_relative_path;
|
|
|
|
|
2013-06-10 12:59:47 -05:00
|
|
|
/**
|
|
|
|
* PHP file extension
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $php_ext;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* phpBB config instance
|
|
|
|
* @var phpbb_config
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $config;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current user
|
|
|
|
* @var phpbb_user
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $user;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension manager.
|
|
|
|
*
|
|
|
|
* @var phpbb_extension_manager
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $extension_manager;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the style that the template being compiled and/or rendered
|
|
|
|
* belongs to, and its parents, in inheritance tree order.
|
|
|
|
*
|
|
|
|
* Used to invoke style-specific template events.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-11 09:41:15 -05:00
|
|
|
protected $style_names;
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Twig Environment
|
|
|
|
*
|
|
|
|
* @var Twig_Environment
|
|
|
|
*/
|
|
|
|
protected $twig;
|
|
|
|
|
2013-06-25 19:23:42 -05:00
|
|
|
/**
|
|
|
|
* Array of filenames assigned to set_filenames
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $filenames = array();
|
|
|
|
|
2013-06-10 12:59:47 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $phpbb_root_path phpBB root path
|
|
|
|
* @param user $user current user
|
|
|
|
* @param phpbb_template_context $context template context
|
|
|
|
* @param phpbb_extension_manager $extension_manager extension manager, if null then template events will not be invoked
|
2013-07-01 13:39:51 -05:00
|
|
|
* @param string $adm_relative_path relative path to adm directory
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-07-01 13:39:51 -05:00
|
|
|
public function __construct($phpbb_root_path, $php_ext, $config, $user, phpbb_template_context $context, phpbb_extension_manager $extension_manager = null, $adm_relative_path = null)
|
2013-06-10 12:59:47 -05:00
|
|
|
{
|
|
|
|
$this->phpbb_root_path = $phpbb_root_path;
|
2013-07-01 13:39:51 -05:00
|
|
|
$this->adm_relative_path = $adm_relative_path;
|
2013-06-10 12:59:47 -05:00
|
|
|
$this->php_ext = $php_ext;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->user = $user;
|
2013-06-11 09:41:15 -05:00
|
|
|
$this->context = $context;
|
2013-06-10 12:59:47 -05:00
|
|
|
$this->extension_manager = $extension_manager;
|
|
|
|
|
2013-06-14 01:00:38 -05:00
|
|
|
$this->cachepath = $phpbb_root_path . 'cache/twig/';
|
|
|
|
|
2013-06-18 10:37:25 -05:00
|
|
|
// Initiate the loader, __main__ namespace paths will be setup later in set_style_names()
|
|
|
|
$loader = new Twig_Loader_Filesystem('');
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-06-25 14:58:55 -05:00
|
|
|
$this->twig = new phpbb_template_twig_environment(
|
|
|
|
$this->config,
|
|
|
|
($this->extension_manager) ? $this->extension_manager->all_enabled() : array(),
|
|
|
|
$this->phpbb_root_path,
|
|
|
|
$loader,
|
|
|
|
array(
|
|
|
|
'cache' => $this->cachepath,
|
2013-07-01 13:32:43 -05:00
|
|
|
'debug' => defined('DEBUG'),
|
|
|
|
'auto_reload' => (bool) $this->config['load_tplcompile'],
|
2013-06-25 14:58:55 -05:00
|
|
|
'autoescape' => false,
|
|
|
|
)
|
|
|
|
);
|
2013-06-24 22:37:58 -05:00
|
|
|
|
2013-07-01 09:32:21 -05:00
|
|
|
$this->twig->addExtension(
|
|
|
|
new phpbb_template_twig_extension(
|
|
|
|
$this->user
|
|
|
|
)
|
|
|
|
);
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
$lexer = new phpbb_template_twig_lexer($this->twig);
|
|
|
|
|
|
|
|
$this->twig->setLexer($lexer);
|
|
|
|
}
|
|
|
|
|
2013-06-24 15:28:54 -05:00
|
|
|
/**
|
|
|
|
* Clear the cache
|
|
|
|
*
|
|
|
|
* @return phpbb_template
|
|
|
|
*/
|
|
|
|
public function clear_cache()
|
|
|
|
{
|
|
|
|
if (is_dir($this->cachepath))
|
|
|
|
{
|
|
|
|
$this->twig->clearCacheFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-06-10 12:59:47 -05:00
|
|
|
/**
|
|
|
|
* Sets the template filenames for handles.
|
|
|
|
*
|
|
|
|
* @param array $filename_array Should be a hash of handle => filename pairs.
|
2013-06-14 01:00:38 -05:00
|
|
|
* @return phpbb_template $this
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
|
|
|
public function set_filenames(array $filename_array)
|
|
|
|
{
|
2013-06-25 19:23:42 -05:00
|
|
|
$this->filenames = array_merge($filename_array, $this->filenames);
|
2013-06-10 12:59:47 -05:00
|
|
|
|
2013-06-14 01:00:38 -05:00
|
|
|
return $this;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the style names corresponding to style hierarchy being compiled
|
|
|
|
* and/or rendered.
|
|
|
|
*
|
|
|
|
* @param array $style_names List of style names in inheritance tree order
|
2013-06-14 01:00:38 -05:00
|
|
|
* @return phpbb_template $this
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
2013-06-14 01:00:38 -05:00
|
|
|
public function set_style_names(array $style_names, $style_paths = array())
|
2013-06-10 12:59:47 -05:00
|
|
|
{
|
|
|
|
$this->style_names = $style_names;
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-06-18 10:37:25 -05:00
|
|
|
// Set as __main__ namespace
|
2013-06-14 01:00:38 -05:00
|
|
|
$this->twig->getLoader()->setPaths($style_paths);
|
|
|
|
|
2013-06-18 10:37:25 -05:00
|
|
|
// Core style namespace from phpbb_style::set_style()
|
2013-06-25 19:23:42 -05:00
|
|
|
if ($this->user && ($style_names === array($this->user->style['style_path']) || $style_names[0] == $this->user->style['style_path']))
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
|
|
|
$this->twig->getLoader()->setPaths($style_paths, 'core');
|
2013-06-25 19:23:42 -05:00
|
|
|
}
|
2013-06-18 10:37:25 -05:00
|
|
|
|
2013-06-25 19:23:42 -05:00
|
|
|
// Add admin namespace
|
2013-07-01 13:39:51 -05:00
|
|
|
if (is_dir($this->phpbb_root_path . $this->adm_relative_path . 'style/'))
|
2013-06-25 19:23:42 -05:00
|
|
|
{
|
2013-07-01 13:39:51 -05:00
|
|
|
$this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->adm_relative_path . 'style/', 'admin');
|
2013-06-25 19:23:42 -05:00
|
|
|
}
|
2013-06-24 15:28:54 -05:00
|
|
|
|
2013-06-25 19:23:42 -05:00
|
|
|
// Add all namespaces for all extensions
|
|
|
|
if ($this->extension_manager instanceof phpbb_extension_manager)
|
|
|
|
{
|
|
|
|
$style_names[] = 'all';
|
|
|
|
|
|
|
|
foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
2013-06-25 19:23:42 -05:00
|
|
|
// namespaces cannot contain /
|
|
|
|
$namespace = str_replace('/', '_', $ext_namespace);
|
|
|
|
$paths = array();
|
2013-06-18 10:37:25 -05:00
|
|
|
|
2013-06-25 19:23:42 -05:00
|
|
|
foreach ($style_names as $style_name)
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
2013-06-25 19:23:42 -05:00
|
|
|
$ext_style_path = $ext_path . 'styles/' . $style_name . '/template';
|
|
|
|
|
|
|
|
if (is_dir($ext_style_path))
|
2013-06-18 10:37:25 -05:00
|
|
|
{
|
2013-06-25 19:23:42 -05:00
|
|
|
$paths[] = $ext_style_path;
|
2013-06-18 10:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 19:23:42 -05:00
|
|
|
|
|
|
|
$this->twig->getLoader()->setPaths($paths, $namespace);
|
2013-06-18 10:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 01:00:38 -05:00
|
|
|
return $this;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all variables and blocks assigned to this template.
|
2013-06-14 01:00:38 -05:00
|
|
|
*
|
|
|
|
* @return phpbb_template $this
|
2013-06-10 12:59:47 -05:00
|
|
|
*/
|
|
|
|
public function destroy()
|
|
|
|
{
|
|
|
|
$this->context = array();
|
2013-06-14 01:00:38 -05:00
|
|
|
|
|
|
|
return $this;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset/empty complete block
|
|
|
|
*
|
|
|
|
* @param string $blockname Name of block to destroy
|
|
|
|
*/
|
|
|
|
public function destroy_block_vars($blockname)
|
|
|
|
{
|
2013-06-11 09:41:15 -05:00
|
|
|
return $this->context->destroy_block_vars($blockname);
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a template for provided handle.
|
|
|
|
*
|
|
|
|
* The template will be loaded and compiled, if necessary, first.
|
|
|
|
*
|
|
|
|
* This function calls hooks.
|
|
|
|
*
|
|
|
|
* @param string $handle Handle to display
|
|
|
|
* @return bool True on success, false on failure
|
|
|
|
*/
|
|
|
|
public function display($handle)
|
|
|
|
{
|
|
|
|
$result = $this->call_hook($handle, __FUNCTION__);
|
|
|
|
if ($result !== false)
|
|
|
|
{
|
|
|
|
return $result[0];
|
|
|
|
}
|
|
|
|
|
2013-06-29 11:07:10 -05:00
|
|
|
$context = &$this->get_template_vars();
|
2013-07-01 11:52:03 -05:00
|
|
|
$this->twig->display($this->get_filename_from_handle($handle), $context);
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls hook if any is defined.
|
|
|
|
*
|
|
|
|
* @param string $handle Template handle being displayed.
|
|
|
|
* @param string $method Method name of the caller.
|
|
|
|
*/
|
|
|
|
protected function call_hook($handle, $method)
|
|
|
|
{
|
|
|
|
global $phpbb_hook;
|
|
|
|
|
|
|
|
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, $method), $handle, $this))
|
|
|
|
{
|
|
|
|
if ($phpbb_hook->hook_return(array(__CLASS__, $method)))
|
|
|
|
{
|
|
|
|
$result = $phpbb_hook->hook_return_result(array(__CLASS__, $method));
|
|
|
|
return array($result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains language array.
|
|
|
|
* This is either lang property of $user property, or if
|
|
|
|
* it is not set an empty array.
|
|
|
|
* @return array language entries
|
|
|
|
*/
|
|
|
|
public function get_lang()
|
|
|
|
{
|
|
|
|
if (isset($this->user->lang))
|
|
|
|
{
|
|
|
|
$lang = $this->user->lang;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$lang = array();
|
|
|
|
}
|
2013-06-14 01:00:38 -05:00
|
|
|
|
2013-06-10 12:59:47 -05:00
|
|
|
return $lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the handle and assign the output to a template variable
|
|
|
|
* or return the compiled result.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @return bool|string false on failure, otherwise if $return_content is true return string of the compiled handle, otherwise return true
|
|
|
|
*/
|
|
|
|
public function assign_display($handle, $template_var = '', $return_content = true)
|
|
|
|
{
|
|
|
|
if ($return_content)
|
|
|
|
{
|
2013-07-01 11:52:03 -05:00
|
|
|
return $this->twig->render($this->get_filename_from_handle($handle));
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-01 11:52:03 -05:00
|
|
|
$this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle)));
|
2013-06-10 12:59:47 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign key variable pairs from an array
|
|
|
|
*
|
|
|
|
* @param array $vararray A hash of variable name => value pairs
|
|
|
|
*/
|
|
|
|
public function assign_vars(array $vararray)
|
|
|
|
{
|
|
|
|
foreach ($vararray as $key => $val)
|
|
|
|
{
|
|
|
|
$this->assign_var($key, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign a single scalar value to a single key.
|
|
|
|
*
|
|
|
|
* Value can be a string, an integer or a boolean.
|
|
|
|
*
|
|
|
|
* @param string $varname Variable name
|
|
|
|
* @param string $varval Value to assign to variable
|
|
|
|
*/
|
|
|
|
public function assign_var($varname, $varval)
|
|
|
|
{
|
2013-06-11 09:41:15 -05:00
|
|
|
return $this->context->assign_var($varname, $varval);
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append text to the string value stored in a key.
|
|
|
|
*
|
|
|
|
* Text is appended using the string concatenation operator (.).
|
|
|
|
*
|
|
|
|
* @param string $varname Variable name
|
|
|
|
* @param string $varval Value to append to variable
|
|
|
|
*/
|
|
|
|
public function append_var($varname, $varval)
|
|
|
|
{
|
2013-06-11 09:41:15 -05:00
|
|
|
return $this->context->append_var($varname, $varval);
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Docstring is copied from phpbb_template_context method with the same name.
|
|
|
|
/**
|
|
|
|
* Assign key variable pairs from an array to a specified block
|
|
|
|
* @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)
|
|
|
|
{
|
2013-06-11 09:41:15 -05:00
|
|
|
return $this->context->assign_block_vars($blockname, $vararray);
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Docstring is copied from phpbb_template_context method with the same name.
|
|
|
|
/**
|
|
|
|
* Change already assigned key variable pair (one-dimensional - single loop entry)
|
|
|
|
*
|
|
|
|
* An example of how to use this function:
|
|
|
|
* {@example alter_block_array.php}
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
|
|
|
|
{
|
2013-06-11 09:41:15 -05:00
|
|
|
return $this->context->alter_block_array($blockname, $vararray, $key, $mode);
|
|
|
|
}
|
|
|
|
|
2013-06-11 10:57:00 -05:00
|
|
|
/**
|
|
|
|
* Get template vars in a format Twig will use (from the context)
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-06-26 12:30:59 -05:00
|
|
|
public function get_template_vars()
|
2013-06-11 09:41:15 -05:00
|
|
|
{
|
2013-07-01 09:36:03 -05:00
|
|
|
$context_vars = $this->context->get_data_ref();
|
2013-06-11 09:41:15 -05:00
|
|
|
|
2013-06-24 22:37:58 -05:00
|
|
|
$vars = array_merge(
|
2013-07-01 09:36:03 -05:00
|
|
|
$context_vars['.'][0], // To get normal vars
|
|
|
|
$context_vars, // To get loops
|
2013-06-29 11:07:10 -05:00
|
|
|
array(
|
|
|
|
'definition' => new phpbb_template_twig_definition(),
|
2013-07-01 09:32:21 -05:00
|
|
|
'user' => $this->user,
|
2013-06-29 11:07:10 -05:00
|
|
|
)
|
2013-06-24 22:37:58 -05:00
|
|
|
);
|
|
|
|
|
2013-06-11 09:41:15 -05:00
|
|
|
// cleanup
|
2013-06-26 12:30:59 -05:00
|
|
|
unset($vars['.']);
|
2013-06-11 09:41:15 -05:00
|
|
|
|
|
|
|
return $vars;
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|
2013-07-01 11:52:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a filename from the handle
|
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_filename_from_handle($handle)
|
|
|
|
{
|
|
|
|
return (isset($this->filenames[$handle])) ? $this->filenames[$handle] : $handle;
|
|
|
|
}
|
2013-06-10 12:59:47 -05:00
|
|
|
}
|