1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 05:50:42 +02:00

Merge remote-tracking branch 'github-phpbb/develop' into ticket/11700

* github-phpbb/develop: (586 commits)
  [ticket/11735] Display disabled checkbox in subsilver for read notifications
  [ticket/11735] Display disabled checkbox when notification is already read
  [ticket/11844] update acp/authentication language var
  [ticket/11795] Remove PM popup
  [ticket/11795] Remove outdated comment from forum_fn.js
  [ticket/11795] Move find user JS to forum_fn
  [ticket/11795] Replace TWIG with phpBB syntax in ACP
  [ticket/11795] Move MSN scripts to forum_fn.js
  [ticket/11795] Use phpBB template syntax instead of TWIG
  [ticket/11795] Move PM popup JS to forum_fn.js
  [ticket/11795] Get rid of pagination JS variables
  [ticket/11795] Get rid of onload_functions
  [ticket/11795] Use data-reset-on-edit attr to reset elements
  [ticket/11795] Redo form elements auto-focus
  [ticket/11811] Remove outline on :focus
  [ticket/11836] Fix subsilver fatal error
  [ticket/11837] Replace escaped single quote with utf-8 single quote
  [ticket/11836] Fix fatal error on unsupported provider for auth link
  [ticket/11837] Translate UCP_AUTH_LINK_NOT_SUPPORTED
  [ticket/11809] Ensure code.js is first script included after jQuery
  ...

Conflicts:
	phpBB/config/services.yml
	phpBB/develop/create_schema_files.php
	phpBB/develop/mysql_upgrader.php
	phpBB/download/file.php
	phpBB/includes/bbcode.php
	phpBB/includes/functions_container.php
	phpBB/install/database_update.php
	phpBB/install/index.php
	phpBB/phpbb/controller/helper.php
	phpBB/phpbb/controller/resolver.php
	phpBB/phpbb/request/request_interface.php
	phpBB/phpbb/session.php
	phpBB/phpbb/style/extension_path_provider.php
	phpBB/phpbb/style/path_provider.php
	phpBB/phpbb/style/path_provider_interface.php
	phpBB/phpbb/style/resource_locator.php
	phpBB/phpbb/style/style.php
	phpBB/phpbb/template/locator.php
	phpBB/phpbb/template/template.php
	phpBB/phpbb/template/twig/node/includeasset.php
	phpBB/phpbb/template/twig/node/includecss.php
	phpBB/phpbb/template/twig/node/includejs.php
	phpBB/phpbb/template/twig/twig.php
	tests/controller/helper_url_test.php
	tests/di/create_container_test.php
	tests/extension/style_path_provider_test.php
	tests/notification/notification_test.php
	tests/session/continue_test.php
	tests/session/creation_test.php
	tests/template/template_events_test.php
	tests/template/template_test_case.php
	tests/template/template_test_case_with_tree.php
	tests/test_framework/phpbb_functional_test_case.php
This commit is contained in:
Nils Adermann
2013-09-16 01:24:05 +02:00
330 changed files with 10287 additions and 6367 deletions

View File

@@ -0,0 +1,148 @@
<?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;
}
abstract class phpbb_template_base implements phpbb_template
{
/**
* Template context.
* Stores template data used during template rendering.
*
* @var phpbb_template_context
*/
protected $context;
/**
* Array of filenames assigned to set_filenames
*
* @var array
*/
protected $filenames = array();
/**
* {@inheritdoc}
*/
public function set_filenames(array $filename_array)
{
$this->filenames = array_merge($this->filenames, $filename_array);
return $this;
}
/**
* 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;
}
/**
* {@inheritdoc}
*/
public function destroy()
{
$this->context->clear();
return $this;
}
/**
* {@inheritdoc}
*/
public function destroy_block_vars($blockname)
{
$this->context->destroy_block_vars($blockname);
return $this;
}
/**
* {@inheritdoc}
*/
public function assign_vars(array $vararray)
{
foreach ($vararray as $key => $val)
{
$this->assign_var($key, $val);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function assign_var($varname, $varval)
{
$this->context->assign_var($varname, $varval);
return $this;
}
/**
* {@inheritdoc}
*/
public function append_var($varname, $varval)
{
$this->context->append_var($varname, $varval);
return $this;
}
/**
* {@inheritdoc}
*/
public function assign_block_vars($blockname, array $vararray)
{
$this->context->assign_block_vars($blockname, $vararray);
return $this;
}
/**
* {@inheritdoc}
*/
public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
{
return $this->context->alter_block_array($blockname, $vararray, $key, $mode);
}
/**
* 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;
}
}

View File

@@ -1,165 +0,0 @@
<?php
/**
*
* @package phpBB3
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\template;
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Resource locator interface.
*
* Objects implementing this interface maintain mapping from template handles
* to source template file paths and locate templates.
*
* Locates style files.
*
* Resource locator is aware of styles tree, and can return actual
* filesystem paths (i.e., the "child" style or the "parent" styles)
* depending on what files exist.
*
* Root paths stored in locator are paths to style directories. Templates are
* stored in subdirectory that $template_path points to.
*
* @package phpBB3
*/
interface locator
{
/**
* Sets the template filenames for handles. $filename_array
* should be a hash of handle => filename pairs.
*
* @param array $filename_array Should be a hash of handle => filename pairs.
*/
public function set_filenames(array $filename_array);
/**
* Determines the filename for a template handle.
*
* The filename comes from array used in a set_filenames call,
* which should have been performed prior to invoking this function.
* Return value is a file basename (without path).
*
* @param $handle string Template handle
* @return string Filename corresponding to the template handle
*/
public function get_filename_for_handle($handle);
/**
* Determines the source file path for a template handle without
* regard for styles tree.
*
* This function returns the path in "primary" style directory
* corresponding to the given template handle. That path may or
* may not actually exist on the filesystem. Because this function
* does not perform stat calls to determine whether the path it
* returns actually exists, it is faster than get_source_file_for_handle.
*
* Use get_source_file_for_handle to obtain the actual path that is
* guaranteed to exist (which might come from the parent style
* directory if primary style has parent styles).
*
* This function will trigger an error if the handle was never
* associated with a template file via set_filenames.
*
* @param $handle string Template handle
* @return string Path to source file path in primary style directory
*/
public function get_virtual_source_file_for_handle($handle);
/**
* Determines the source file path for a template handle, accounting
* for styles tree and verifying that the path exists.
*
* This function returns the actual path that may be compiled for
* the specified template handle. It will trigger an error if
* the template handle was never associated with a template path
* via set_filenames or if the template file does not exist on the
* filesystem.
*
* Use get_virtual_source_file_for_handle to just resolve a template
* handle to a path without any filesystem or styles tree checks.
*
* @param string $handle Template handle (i.e. "friendly" template name)
* @param bool $find_all If true, each root path will be checked and function
* will return array of files instead of string and will not
* trigger a error if template does not exist
* @return string Source file path
*/
public function get_source_file_for_handle($handle, $find_all = false);
/**
* Obtains a complete filesystem path for a file in a style.
*
* This function traverses the style tree (selected style and
* its parents in order, if inheritance is being used) and finds
* the first file on the filesystem matching specified relative path,
* or the first of the specified paths if more than one path is given.
*
* This function can be used to determine filesystem path of any
* file under any style, with the consequence being that complete
* relative to the style directory path must be provided as an argument.
*
* In particular, this function can be used to locate templates
* and javascript files.
*
* For locating templates get_first_template_location should be used
* as it prepends the configured template path to the template basename.
*
* Note: "selected style" is whatever style the style resource locator
* is configured for.
*
* The return value then will be a path, relative to the current
* directory or absolute, to the first existing file in the selected
* style or its closest parent.
*
* If the selected style does not have the file being searched,
* (and if inheritance is involved, none of the parents have it either),
* false will be returned.
*
* Multiple files can be specified, in which case the first file in
* the list that can be found on the filesystem is returned.
*
* If multiple files are specified and inheritance is involved,
* first each of the specified files is checked in the selected style,
* then each of the specified files is checked in the immediate parent,
* etc.
*
* Specifying true for $return_default will cause the function to
* return the first path which was checked for existence in the event
* that the template file was not found, instead of false.
* This is always a path in the selected style itself, not any of its
* parents.
*
* If $return_full_path is false, then instead of returning a usable
* path (when the file is found) the file's path relative to the style
* directory will be returned. This is the same path as was given to
* the function as a parameter. This can be used to check which of the
* files specified in $files exists. Naturally this requires passing
* more than one file in $files.
*
* @param array $files List of files to locate.
* @param bool $return_default Determines what to return if file does not
* exist. If true, function will return location where file is
* supposed to be. If false, function will return false.
* @param bool $return_full_path If true, function will return full path
* to file. If false, function will return file name. This
* parameter can be used to check which one of set of files
* is available.
* @return string or boolean Source file path if file exists or $return_default is
* true. False if file does not exist and $return_default is false
*/
public function get_first_file_location($files, $return_default = false, $return_full_path = true);
}

View File

@@ -36,14 +36,32 @@ interface template
public function set_filenames(array $filename_array);
/**
* Sets the style names/paths corresponding to style hierarchy being compiled
* and/or rendered.
* Get the style tree of the style preferred by the current user
*
* @param array $style_names List of style names in inheritance tree order
* @param array $style_paths List of style paths in inheritance tree order
* @return array Style tree, most specific first
*/
public function get_user_style();
/**
* Set style location based on (current) user's chosen style.
*
* @param array $style_directories The directories to add style paths for
* E.g. array('ext/foo/bar/styles', 'styles')
* Default: array('styles') (phpBB's style directory)
* @return \phpbb\template\template $this
*/
public function set_style_names(array $style_names, array $style_paths);
public function set_style($style_directories = array('styles'));
/**
* Set custom style location (able to use directory outside of phpBB).
*
* Note: Templates are still compiled to phpBB's cache directory.
*
* @param string|array $names Array of names or string of name of template(s) in inheritance tree order, used by extensions.
* @param string|array or string $paths Array of style paths, relative to current root directory
* @return \phpbb\template\template $this
*/
public function set_custom_style($names, $paths);
/**
* Clears all variables and blocks assigned to this template.

View File

@@ -139,4 +139,39 @@ class environment extends \Twig_Environment
return parent::loadTemplate($name, $index);
}
}
/**
* Finds a template by name.
*
* @param string $name The template name
* @return string
*/
public function findTemplate($name)
{
if (strpos($name, '@') === false)
{
foreach ($this->getNamespaceLookUpOrder() as $namespace)
{
try
{
if ($namespace === '__main__')
{
return parent::getLoader()->getCacheKey($name);
}
return parent::getLoader()->getCacheKey('@' . $namespace . '/' . $name);
}
catch (Twig_Error_Loader $e)
{
}
}
// We were unable to load any templates
throw $e;
}
else
{
return parent::getLoader()->getCacheKey($name);
}
}
}

View File

@@ -77,7 +77,7 @@ class lexer extends \Twig_Lexer
// Fix tokens that may have inline variables (e.g. <!-- DEFINE $TEST = '{FOO}')
$code = $this->fix_inline_variable_tokens(array(
'DEFINE.+=',
'DEFINE \$[a-zA-Z0-9_]+ =',
'INCLUDE',
'INCLUDEPHP',
'INCLUDEJS',
@@ -128,10 +128,14 @@ class lexer extends \Twig_Lexer
{
$callback = function($matches)
{
// Remove any quotes that may have been used in different implementations
// E.g. DEFINE $TEST = 'blah' vs INCLUDE foo
// Replace {} with start/end to parse variables (' ~ TEST ~ '.html)
$matches[2] = str_replace(array('"', "'", '{', '}'), array('', '', "' ~ ", " ~ '"), $matches[2]);
// Remove matching quotes at the beginning/end if a statement;
// E.g. 'asdf'"' -> asdf'"
// E.g. "asdf'"" -> asdf'"
// E.g. 'asdf'" -> 'asdf'"
$matches[2] = preg_replace('#^([\'"])?(.*?)\1$#', '$2', $matches[2]);
// Replace template variables with start/end to parse variables (' ~ TEST ~ '.html)
$matches[2] = preg_replace('#{([a-zA-Z0-9_\.$]+)}#', "'~ \$1 ~'", $matches[2]);
// Surround the matches in single quotes ('' ~ TEST ~ '.html')
return "<!-- {$matches[1]} '{$matches[2]}' -->";
@@ -159,6 +163,9 @@ class lexer extends \Twig_Lexer
$subset = trim(substr($matches[2], 1, -1)); // Remove parenthesis
$body = $matches[3];
// Replace <!-- BEGINELSE -->
$body = str_replace('<!-- BEGINELSE -->', '{% else %}', $body);
// Is the designer wanting to call another loop in a loop?
// <!-- BEGIN loop -->
// <!-- BEGIN !loop2 -->
@@ -189,25 +196,20 @@ class lexer extends \Twig_Lexer
// Recursive...fix any child nodes
$body = $parent_class->fix_begin_tokens($body, $parent_nodes);
// Rename loopname vars (to prevent collisions, loop children are named (loop name)_loop_element)
$body = str_replace($name . '.', $name . '_loop_element.', $body);
// Need the parent variable name
array_pop($parent_nodes);
$parent = (!empty($parent_nodes)) ? end($parent_nodes) . '_loop_element.' : '';
$parent = (!empty($parent_nodes)) ? end($parent_nodes) . '.' : '';
if ($subset !== '')
{
$subset = '|subset(' . $subset . ')';
}
// Turn into a Twig for loop, using (loop name)_loop_element for each child
return "{% for {$name}_loop_element in {$parent}{$name}{$subset} %}{$body}{% endfor %}";
$parent = ($parent) ?: 'loops.';
// Turn into a Twig for loop
return "{% for {$name} in {$parent}{$name}{$subset} %}{$body}{% endfor %}";
};
// Replace <!-- BEGINELSE --> correctly, only needs to be done once
$code = str_replace('<!-- BEGINELSE -->', '{% else %}', $code);
return preg_replace_callback('#<!-- BEGIN ([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1 -->#s', $callback, $code);
}
@@ -219,21 +221,28 @@ class lexer extends \Twig_Lexer
*/
protected function fix_if_tokens($code)
{
$callback = function($matches)
{
// Replace $TEST with definition.TEST
$matches[1] = preg_replace('#\s\$([a-zA-Z_0-9]+)#', ' definition.$1', $matches[1]);
// Replace .test with test|length
$matches[1] = preg_replace('#\s\.([a-zA-Z_0-9\.]+)#', ' $1|length', $matches[1]);
return '<!-- IF' . $matches[1] . '-->';
};
// Replace ELSE IF with ELSEIF
$code = preg_replace('#<!-- ELSE IF (.+?) -->#', '<!-- ELSEIF $1 -->', $code);
// Replace our "div by" with Twig's divisibleby (Twig does not like test names with spaces)
$code = preg_replace('# div by ([0-9]+)#', ' divisibleby($1)', $code);
return preg_replace_callback('#<!-- IF((.*)[\s][\$|\.|!]([^\s]+)(.*))-->#', $callback, $code);
$callback = function($matches)
{
$inner = $matches[2];
// Replace $TEST with definition.TEST
$inner = preg_replace('#(\s\(?!?)\$([a-zA-Z_0-9]+)#', '$1definition.$2', $inner);
// Replace .foo with loops.foo|length
$inner = preg_replace('#(\s\(?!?)\.([a-zA-Z_0-9]+)([^a-zA-Z_0-9\.])#', '$1loops.$2|length$3', $inner);
// Replace .foo.bar with foo.bar|length
$inner = preg_replace('#(\s\(?!?)\.([a-zA-Z_0-9\.]+)([^a-zA-Z_0-9\.])#', '$1$2|length$3', $inner);
return "<!-- {$matches[1]}IF{$inner}-->";
};
return preg_replace_callback('#<!-- (ELSE)?IF((.*?) \(?!?[\$|\.]([^\s]+)(.*?))-->#', $callback, $code);
}
/**
@@ -257,10 +266,10 @@ class lexer extends \Twig_Lexer
*/
// Replace <!-- DEFINE $NAME with {% DEFINE definition.NAME
$code = preg_replace('#<!-- DEFINE \$(.*)-->#', '{% DEFINE $1 %}', $code);
$code = preg_replace('#<!-- DEFINE \$(.*?) -->#', '{% DEFINE $1 %}', $code);
// Changing UNDEFINE NAME to DEFINE NAME = null to save from creating an extra token parser/node
$code = preg_replace('#<!-- UNDEFINE \$(.*)-->#', '{% DEFINE $1= null %}', $code);
$code = preg_replace('#<!-- UNDEFINE \$(.*?)-->#', '{% DEFINE $1= null %}', $code);
// Replace all of our variables, {$VARNAME}, with Twig style, {{ definition.VARNAME }}
$code = preg_replace('#{\$([a-zA-Z0-9_\.]+)}#', '{{ definition.$1 }}', $code);

View File

@@ -0,0 +1,150 @@
<?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 loader
* @package phpBB3
*/
class phpbb_template_twig_loader extends Twig_Loader_Filesystem
{
protected $safe_directories = array();
/**
* Set safe directories
*
* @param array $directories Array of directories that are safe (empty to clear)
* @return Twig_Loader_Filesystem
*/
public function setSafeDirectories($directories = array())
{
$this->safe_directories = array();
if (!empty($directories))
{
foreach ($directories as $directory)
{
$this->addSafeDirectory($directory);
}
}
return $this;
}
/**
* Add safe directory
*
* @param string $directory Directory that should be added
* @return Twig_Loader_Filesystem
*/
public function addSafeDirectory($directory)
{
$directory = phpbb_realpath($directory);
if ($directory !== false)
{
$this->safe_directories[] = $directory;
}
return $this;
}
/**
* Get current safe directories
*
* @return array
*/
public function getSafeDirectories()
{
return $this->safe_directories;
}
/**
* Override for parent::validateName()
*
* This is done because we added support for safe directories, and when Twig
* findTemplate() is called, validateName() is called first, which would
* always throw an exception if the file is outside of the configured
* template directories.
*/
protected function validateName($name)
{
return;
}
/**
* Find the template
*
* Override for Twig_Loader_Filesystem::findTemplate to add support
* for loading from safe directories.
*/
protected function findTemplate($name)
{
$name = (string) $name;
// normalize name
$name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
// If this is in the cache we can skip the entire process below
// as it should have already been validated
if (isset($this->cache[$name])) {
return $this->cache[$name];
}
// First, find the template name. The override above of validateName
// causes the validateName process to be skipped for this call
$file = parent::findTemplate($name);
try
{
// Try validating the name (which may throw an exception)
parent::validateName($name);
}
catch (Twig_Error_Loader $e)
{
if (strpos($e->getRawMessage(), 'Looks like you try to load a template outside configured directories') === 0)
{
// Ok, so outside of the configured template directories, we
// can now check if we're within a "safe" directory
// Find the real path of the directory the file is in
$directory = phpbb_realpath(dirname($file));
if ($directory === false)
{
// Some sort of error finding the actual path, must throw the exception
throw $e;
}
foreach ($this->safe_directories as $safe_directory)
{
if (strpos($directory, $safe_directory) === 0)
{
// The directory being loaded is below a directory
// that is "safe". We're good to load it!
return $file;
}
}
}
// Not within any safe directories
throw $e;
}
// No exception from validateName, safe to load.
return $file;
}
}

View File

@@ -20,6 +20,12 @@ if (!defined('IN_PHPBB'))
class event extends \Twig_Node
{
/**
* The subdirectory in which all template listener files must be placed
* @var string
*/
protected $listener_directory = 'event/';
/** @var Twig_Environment */
protected $environment;
@@ -39,7 +45,7 @@ class event extends \Twig_Node
{
$compiler->addDebugInfo($this);
$location = $this->getNode('expr')->getAttribute('name');
$location = $this->listener_directory . $this->getNode('expr')->getAttribute('name');
foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path)
{

View File

@@ -9,7 +9,7 @@
namespace phpbb\template\twig\node;
class includeasset extends \Twig_Node
abstract class includeasset extends \Twig_Node
{
/** @var Twig_Environment */
protected $environment;
@@ -42,10 +42,10 @@ class includeasset extends \Twig_Node
->write("\$local_file = \$this->getEnvironment()->get_phpbb_root_path() . \$asset_path;\n")
->write("if (!file_exists(\$local_file)) {\n")
->indent()
->write("\$local_file = \$this->getEnvironment()->getLoader()->getCacheKey(\$asset_path);\n")
->write("\$local_file = \$this->getEnvironment()->findTemplate(\$asset_path);\n")
->write("\$asset->set_path(\$local_file, true);\n")
->outdent()
->write("\$asset->add_assets_version({$config['assets_version']});\n")
->write("\$asset->add_assets_version('{$config['assets_version']}');\n")
->write("\$asset_file = \$asset->get_url();\n")
->write("}\n")
->outdent()
@@ -59,4 +59,19 @@ class includeasset extends \Twig_Node
->raw("\n');\n")
;
}
/**
* Get the definition name
*
* @return string (e.g. 'SCRIPTS')
*/
abstract public function get_definition_name();
/**
* Append the output code for the asset
*
* @param Twig_Compiler A Twig_Compiler instance
* @return null
*/
abstract protected function append_asset(Twig_Compiler $compiler);
}

View File

@@ -11,17 +11,18 @@ namespace phpbb\template\twig\node;
class includecss extends \phpbb\template\twig\node\includeasset
{
/**
* {@inheritdoc}
*/
public function get_definition_name()
{
return 'STYLESHEETS';
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function append_asset(\Twig_Compiler $compiler)
* {@inheritdoc}
*/
public function append_asset(Twig_Compiler $compiler)
{
$compiler
->raw("<link href=\"' . ")

View File

@@ -11,17 +11,18 @@ namespace phpbb\template\twig\node;
class includejs extends \phpbb\template\twig\node\includeasset
{
/**
* {@inheritdoc}
*/
public function get_definition_name()
{
return 'SCRIPTS';
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
protected function append_asset(\Twig_Compiler $compiler)
* {@inheritdoc}
*/
protected function append_asset(Twig_Compiler $compiler)
{
$config = $this->environment->get_phpbb_config();

View File

@@ -21,15 +21,8 @@ if (!defined('IN_PHPBB'))
* Twig Template class.
* @package phpBB3
*/
class twig implements \phpbb\template\template
class twig extends \phpbb\template\base
{
/**
* Template context.
* Stores template data used during template rendering.
* @var \phpbb\template\context
*/
protected $context;
/**
* Path of the cache directory for the template
*
@@ -45,12 +38,6 @@ class twig implements \phpbb\template\template
*/
protected $phpbb_root_path;
/**
* adm relative path
* @var string
*/
protected $adm_relative_path;
/**
* PHP file extension
* @var string
@@ -76,16 +63,6 @@ class twig implements \phpbb\template\template
*/
protected $extension_manager;
/**
* 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
*/
protected $style_names;
/**
* Twig Environment
*
@@ -93,13 +70,6 @@ class twig implements \phpbb\template\template
*/
protected $twig;
/**
* Array of filenames assigned to set_filenames
*
* @var array
*/
protected $filenames = array();
/**
* Constructor.
*
@@ -114,7 +84,6 @@ class twig implements \phpbb\template\template
public function __construct($phpbb_root_path, $php_ext, $config, $user, \phpbb\template\context $context, \phpbb\extension\manager $extension_manager = null, $adm_relative_path = null)
{
$this->phpbb_root_path = $phpbb_root_path;
$this->adm_relative_path = $adm_relative_path;
$this->php_ext = $php_ext;
$this->config = $config;
$this->user = $user;
@@ -124,7 +93,7 @@ class twig implements \phpbb\template\template
$this->cachepath = $phpbb_root_path . 'cache/twig/';
// Initiate the loader, __main__ namespace paths will be setup later in set_style_names()
$loader = new \Twig_Loader_Filesystem('');
$loader = new \phpbb\template\twig\loader('');
$this->twig = new \phpbb\template\twig\environment(
$this->config,
@@ -149,6 +118,12 @@ class twig implements \phpbb\template\template
$lexer = new \phpbb\template\twig\lexer($this->twig);
$this->twig->setLexer($lexer);
// Add admin namespace
if ($adm_relative_path !== null && is_dir($this->phpbb_root_path . $adm_relative_path . 'style/'))
{
$this->twig->getLoader()->setPaths($this->phpbb_root_path . $adm_relative_path . 'style/', 'admin');
}
}
/**
@@ -167,51 +142,94 @@ class twig implements \phpbb\template\template
}
/**
* Sets the template filenames for handles.
* Get the style tree of the style preferred by the current user
*
* @param array $filename_array Should be a hash of handle => filename pairs.
* @return array Style tree, most specific first
*/
public function get_user_style()
{
$style_list = array(
$this->user->style['style_path'],
);
if ($this->user->style['style_parent_id'])
{
$style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
}
return $style_list;
}
/**
* Set style location based on (current) user's chosen style.
*
* @param array $style_directories The directories to add style paths for
* E.g. array('ext/foo/bar/styles', 'styles')
* Default: array('styles') (phpBB's style directory)
* @return \phpbb\template\template $this
*/
public function set_filenames(array $filename_array)
public function set_style($style_directories = array('styles'))
{
$this->filenames = array_merge($this->filenames, $filename_array);
if ($style_directories !== array('styles') && $this->twig->getLoader()->getPaths('core') === array())
{
// We should set up the core styles path since not already setup
$this->set_style();
}
$names = $this->get_user_style();
$paths = array();
foreach ($style_directories as $directory)
{
foreach ($names as $name)
{
$path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
$template_path = $path . 'template/';
if (is_dir($template_path))
{
// Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($path);
$paths[] = $template_path;
}
}
}
// If we're setting up the main phpBB styles directory and the core
// namespace isn't setup yet, we will set it up now
if ($style_directories === array('styles') && $this->twig->getLoader()->getPaths('core') === array())
{
// Set up the core style paths namespace
$this->twig->getLoader()->setPaths($paths, 'core');
}
$this->set_custom_style($names, $paths);
return $this;
}
/**
* Sets the style names/paths corresponding to style hierarchy being compiled
* and/or rendered.
* Set custom style location (able to use directory outside of phpBB).
*
* @param array $style_names List of style names in inheritance tree order
* @param array $style_paths List of style paths in inheritance tree order
* @param bool $is_core True if the style names are the "core" styles for this page load
* Core means the main phpBB template files
* @return \phpbb\template\template $this
* Note: Templates are still compiled to phpBB's cache directory.
*
* @param string|array $names Array of names or string of name of template(s) in inheritance tree order, used by extensions.
* @param string|array or string $paths Array of style paths, relative to current root directory
* @return phpbb_template $this
*/
public function set_style_names(array $style_names, array $style_paths, $is_core = false)
public function set_custom_style($names, $paths)
{
$this->style_names = $style_names;
$paths = (is_string($paths)) ? array($paths) : $paths;
$names = (is_string($names)) ? array($names) : $names;
// Set as __main__ namespace
$this->twig->getLoader()->setPaths($style_paths);
// Core style namespace from \phpbb\style\style::set_style()
if ($is_core)
{
$this->twig->getLoader()->setPaths($style_paths, 'core');
}
// Add admin namespace
if (is_dir($this->phpbb_root_path . $this->adm_relative_path . 'style/'))
{
$this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->adm_relative_path . 'style/', 'admin');
}
$this->twig->getLoader()->setPaths($paths);
// Add all namespaces for all extensions
if ($this->extension_manager instanceof \phpbb\extension\manager)
{
$style_names[] = 'all';
$names[] = 'all';
foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
{
@@ -219,13 +237,17 @@ class twig implements \phpbb\template\template
$namespace = str_replace('/', '_', $ext_namespace);
$paths = array();
foreach ($style_names as $style_name)
foreach ($names as $style_name)
{
$ext_style_path = $ext_path . 'styles/' . $style_name . '/template';
$ext_style_path = $ext_path . 'styles/' . $style_name . '/';
$ext_style_template_path = $ext_style_path . 'template/';
if (is_dir($ext_style_path))
if (is_dir($ext_style_template_path))
{
$paths[] = $ext_style_path;
// Add the base style directory as a safe directory
$this->twig->getLoader()->addSafeDirectory($ext_style_path);
$paths[] = $ext_style_template_path;
}
}
@@ -236,31 +258,6 @@ class twig implements \phpbb\template\template
return $this;
}
/**
* Clears all variables and blocks assigned to this template.
*
* @return \phpbb\template\template $this
*/
public function destroy()
{
$this->context = array();
return $this;
}
/**
* Reset/empty complete block
*
* @param string $blockname Name of block to destroy
* @return \phpbb\template\template $this
*/
public function destroy_block_vars($blockname)
{
$this->context->destroy_block_vars($blockname);
return $this;
}
/**
* Display a template for provided handle.
*
@@ -284,28 +281,6 @@ class twig implements \phpbb\template\template
return $this;
}
/**
* 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;
}
/**
* Display the handle and assign the output to a template variable
* or return the compiled result.
@@ -327,134 +302,30 @@ class twig implements \phpbb\template\template
return $this;
}
/**
* Assign key variable pairs from an array
*
* @param array $vararray A hash of variable name => value pairs
* @return \phpbb\template\template $this
*/
public function assign_vars(array $vararray)
{
foreach ($vararray as $key => $val)
{
$this->assign_var($key, $val);
}
return $this;
}
/**
* 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
* @return \phpbb\template\template $this
*/
public function assign_var($varname, $varval)
{
$this->context->assign_var($varname, $varval);
return $this;
}
/**
* 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
* @return \phpbb\template\template $this
*/
public function append_var($varname, $varval)
{
$this->context->append_var($varname, $varval);
return $this;
}
/**
* 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
* @return \phpbb\template\template $this
*/
public function assign_block_vars($blockname, array $vararray)
{
$this->context->assign_block_vars($blockname, $vararray);
return $this;
}
/**
* 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')
{
return $this->context->alter_block_array($blockname, $vararray, $key, $mode);
}
/**
* Get template vars in a format Twig will use (from the context)
*
* @return array
*/
public function get_template_vars()
protected function get_template_vars()
{
$context_vars = $this->context->get_data_ref();
$vars = array_merge(
$context_vars['.'][0], // To get normal vars
$context_vars, // To get loops
array(
'definition' => new \phpbb\template\twig\definition(),
'user' => $this->user,
'loops' => $context_vars, // To get loops
)
);
// cleanup
unset($vars['.']);
unset($vars['loops']['.']);
return $vars;
}
/**
* 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;
}
/**
* Get path to template for handle (required for BBCode parser)
*