diff options
Diffstat (limited to 'phpBB/phpbb/template')
29 files changed, 721 insertions, 325 deletions
| diff --git a/phpBB/phpbb/template/asset.php b/phpBB/phpbb/template/asset.php index 24e0d6698d..cb00f16549 100644 --- a/phpBB/phpbb/template/asset.php +++ b/phpBB/phpbb/template/asset.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -16,14 +20,20 @@ class asset  	/** @var \phpbb\path_helper **/  	protected $path_helper; +	/** @var \phpbb\filesystem\filesystem */ +	protected $filesystem; +  	/**  	* Constructor  	*  	* @param string $url URL +	* @param \phpbb\path_helper $path_helper Path helper object +	* @param \phpbb\filesystem\filesystem $filesystem  	*/ -	public function __construct($url, \phpbb\path_helper $path_helper) +	public function __construct($url, \phpbb\path_helper $path_helper, \phpbb\filesystem\filesystem $filesystem)  	{  		$this->path_helper = $path_helper; +		$this->filesystem = $filesystem;  		$this->set_url($url);  	} @@ -147,6 +157,24 @@ class asset  	*/  	public function set_path($path, $urlencode = false)  	{ +		// Since 1.7.0 Twig returns the real path of the file. We need it to be relative. +		$real_root_path = $this->filesystem->realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR; + +		// If the asset is under the phpBB root path we need to remove its path and then prepend $phpbb_root_path +		if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path) +		{ +			$path = $this->path_helper->get_phpbb_root_path() . str_replace('\\', '/', substr($path, strlen($real_root_path))); +		} +		else +		{ +			// Else we make the path relative to the current working directory +			$real_root_path = $this->filesystem->realpath('.') . DIRECTORY_SEPARATOR; +			if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path) +			{ +				$path = str_replace('\\', '/', substr($path, strlen($real_root_path))); +			} +		} +  		if ($urlencode)  		{  			$paths = explode('/', $path); @@ -156,6 +184,7 @@ class asset  			}  			$path = implode('/', $paths);  		} +  		$this->components['path'] = $path;  	} diff --git a/phpBB/phpbb/template/base.php b/phpBB/phpbb/template/base.php index 6044effa1f..9a40702ba8 100644 --- a/phpBB/phpbb/template/base.php +++ b/phpBB/phpbb/template/base.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -113,6 +117,16 @@ abstract class base implements template  	/**  	* {@inheritdoc}  	*/ +	public function assign_block_vars_array($blockname, array $block_vars_array) +	{ +		$this->context->assign_block_vars_array($blockname, $block_vars_array); + +		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); @@ -128,11 +142,11 @@ abstract class base implements template  	{  		global $phpbb_hook; -		if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, $method), $handle, $this)) +		if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array('template', $method), $handle, $this))  		{ -			if ($phpbb_hook->hook_return(array(__CLASS__, $method))) +			if ($phpbb_hook->hook_return(array('template', $method)))  			{ -				$result = $phpbb_hook->hook_return_result(array(__CLASS__, $method)); +				$result = $phpbb_hook->hook_return_result(array('template', $method));  				return array($result);  			}  		} diff --git a/phpBB/phpbb/template/context.php b/phpBB/phpbb/template/context.php index 65c7d094a0..4ee48205c8 100644 --- a/phpBB/phpbb/template/context.php +++ b/phpBB/phpbb/template/context.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -11,8 +15,6 @@ namespace phpbb\template;  /**  * Stores variables assigned to template. -* -* @package phpBB3  */  class context  { @@ -32,6 +34,11 @@ class context  	*/  	private $rootref; +	/** +	* @var bool +	*/ +	private $num_rows_is_set; +  	public function __construct()  	{  		$this->clear(); @@ -44,6 +51,7 @@ class context  	{  		$this->tpldata = array('.' => array(0 => array()));  		$this->rootref = &$this->tpldata['.'][0]; +		$this->num_rows_is_set = false;  	}  	/** @@ -53,6 +61,7 @@ class context  	*  	* @param string $varname Variable name  	* @param string $varval Value to assign to variable +	* @return true  	*/  	public function assign_var($varname, $varval)  	{ @@ -68,6 +77,7 @@ class context  	*  	* @param string $varname Variable name  	* @param string $varval Value to append to variable +	* @return true  	*/  	public function append_var($varname, $varval)  	{ @@ -91,10 +101,59 @@ class context  		// returning a reference directly is not  		// something php is capable of doing  		$ref = &$this->tpldata; + +		if (!$this->num_rows_is_set) +		{ +			/* +			* We do not set S_NUM_ROWS while adding a row, to reduce the complexity +			* If we would set it on adding, each subsequent adding would cause +			* n modifications, resulting in a O(n!) complexity, rather then O(n) +			*/ +			foreach ($ref as $loop_name => &$loop_data) +			{ +				if ($loop_name === '.') +				{ +					continue; +				} + +				$this->set_num_rows($loop_data); +			} +			$this->num_rows_is_set = true; +		} +  		return $ref;  	}  	/** +	* Set S_NUM_ROWS for each row in this template block +	* +	* @param array $loop_data +	*/ +	protected function set_num_rows(&$loop_data) +	{ +		$s_num_rows = sizeof($loop_data); +		foreach ($loop_data as &$mod_block) +		{ +			foreach ($mod_block as $sub_block_name => &$sub_block) +			{ +				// If the key name is lowercase and the data is an array, +				// it could be a template loop. So we set the S_NUM_ROWS there +				// aswell. +				if ($sub_block_name === strtolower($sub_block_name) && is_array($sub_block)) +				{ +					$this->set_num_rows($sub_block); +				} +			} + +			// Check whether we are inside a block before setting the variable +			if (isset($mod_block['S_BLOCK_NAME'])) +			{ +				$mod_block['S_NUM_ROWS'] = $s_num_rows; +			} +		} +	} + +	/**  	* Returns a reference to template root scope.  	*  	* This function is public so that template renderer may invoke it. @@ -115,9 +174,11 @@ class context  	*  	* @param string $blockname Name of block to assign $vararray to  	* @param array $vararray A hash of variable name => value pairs +	* @return true  	*/  	public function assign_block_vars($blockname, array $vararray)  	{ +		$this->num_rows_is_set = false;  		if (strpos($blockname, '.') !== false)  		{  			// Nested block. @@ -155,12 +216,6 @@ class context  			// We're adding a new iteration to this block with the given  			// variable assignments.  			$str[$blocks[$blockcount]][] = $vararray; - -			// Set S_NUM_ROWS -			foreach ($str[$blocks[$blockcount]] as &$mod_block) -			{ -				$mod_block['S_NUM_ROWS'] = sizeof($str[$blocks[$blockcount]]); -			}  		}  		else  		{ @@ -186,12 +241,23 @@ class context  			// Add a new iteration to this block with the variable assignments we were given.  			$this->tpldata[$blockname][] = $vararray; +		} -			// Set S_NUM_ROWS -			foreach ($this->tpldata[$blockname] as &$mod_block) -			{ -				$mod_block['S_NUM_ROWS'] = sizeof($this->tpldata[$blockname]); -			} +		return true; +	} + +	/** +	* Assign key variable pairs from an array to a whole specified block loop +	* +	* @param string $blockname Name of block to assign $block_vars_array to +	* @param array $block_vars_array An array of hashes of variable name => value pairs +	* @return true +	*/ +	public function assign_block_vars_array($blockname, array $block_vars_array) +	{ +		foreach ($block_vars_array as $vararray) +		{ +			$this->assign_block_vars($blockname, $vararray);  		}  		return true; @@ -226,6 +292,7 @@ class context  	*/  	public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')  	{ +		$this->num_rows_is_set = false;  		if (strpos($blockname, '.') !== false)  		{  			// Nested block. @@ -325,12 +392,6 @@ class context  			$block[$key] = $vararray;  			$block[$key]['S_ROW_COUNT'] = $block[$key]['S_ROW_NUM'] = $key; -			// Set S_NUM_ROWS -			foreach ($this->tpldata[$blockname] as &$mod_block) -			{ -				$mod_block['S_NUM_ROWS'] = sizeof($this->tpldata[$blockname]); -			} -  			return true;  		} @@ -354,9 +415,11 @@ class context  	* Reset/empty complete block  	*  	* @param string $blockname Name of block to destroy +	* @return true  	*/  	public function destroy_block_vars($blockname)  	{ +		$this->num_rows_is_set = false;  		if (strpos($blockname, '.') !== false)  		{  			// Nested block. diff --git a/phpBB/phpbb/template/exception/user_object_not_available.php b/phpBB/phpbb/template/exception/user_object_not_available.php new file mode 100644 index 0000000000..62fd2743c1 --- /dev/null +++ b/phpBB/phpbb/template/exception/user_object_not_available.php @@ -0,0 +1,22 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\exception; + +/** + * This exception is thrown when the user object was not set but it is required by the called method + */ +class user_object_not_available extends \phpbb\exception\runtime_exception +{ + +} diff --git a/phpBB/phpbb/template/template.php b/phpBB/phpbb/template/template.php index d95b0a822c..041ecb12e4 100644 --- a/phpBB/phpbb/template/template.php +++ b/phpBB/phpbb/template/template.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -132,6 +136,14 @@ interface template  	public function assign_block_vars($blockname, array $vararray);  	/** +	* Assign key variable pairs from an array to a whole specified block loop +	* @param string $blockname Name of block to assign $block_vars_array to +	* @param array $block_vars_array An array of hashes of variable name => value pairs +	* @return \phpbb\template\template $this +	*/ +	public function assign_block_vars_array($blockname, array $block_vars_array); + +	/**  	* Change already assigned key variable pair (one-dimensional - single loop entry)  	*  	* An example of how to use this function: @@ -163,6 +175,7 @@ interface template  	/**  	* Get path to template for handle (required for BBCode parser)  	* +	* @param string $handle Handle to retrieve the source file  	* @return string  	*/  	public function get_source_file_for_handle($handle); diff --git a/phpBB/phpbb/template/twig/definition.php b/phpBB/phpbb/template/twig/definition.php index 945c46675e..cb3c953692 100644 --- a/phpBB/phpbb/template/twig/definition.php +++ b/phpBB/phpbb/template/twig/definition.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -21,6 +25,8 @@ class definition  	* Get a DEFINE'd variable  	*  	* @param string $name +	* @param array $arguments +	*  	* @return mixed Null if not found  	*/  	public function __call($name, $arguments) diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index aa55f1e011..6e75403159 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -14,9 +18,15 @@ class environment extends \Twig_Environment  	/** @var \phpbb\config\config */  	protected $phpbb_config; +	/** @var \phpbb\filesystem\filesystem */ +	protected $filesystem; +  	/** @var \phpbb\path_helper */  	protected $phpbb_path_helper; +	/** @var \Symfony\Component\DependencyInjection\ContainerInterface */ +	protected $container; +  	/** @var \phpbb\extension\manager */  	protected $extension_manager; @@ -32,27 +42,53 @@ class environment extends \Twig_Environment  	/**  	* Constructor  	* -	* @param \phpbb\config\config $phpbb_config -	* @param \phpbb\path_helper -	* @param \phpbb\extension\manager -	* @param string $phpbb_root_path -	* @param Twig_LoaderInterface $loader +	* @param \phpbb\config\config $phpbb_config The phpBB configuration +	* @param \phpbb\filesystem\filesystem $filesystem +	* @param \phpbb\path_helper $path_helper phpBB path helper +	* @param \Symfony\Component\DependencyInjection\ContainerInterface $container The dependency injection container +	* @param string $cache_path The path to the cache directory +	* @param \phpbb\extension\manager $extension_manager phpBB extension manager +	* @param \Twig_LoaderInterface $loader Twig loader interface  	* @param array $options Array of options to pass to Twig  	*/ -	public function __construct($phpbb_config, \phpbb\path_helper $path_helper, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array()) +	public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, \Symfony\Component\DependencyInjection\ContainerInterface $container, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array())  	{  		$this->phpbb_config = $phpbb_config; +		$this->filesystem = $filesystem;  		$this->phpbb_path_helper = $path_helper;  		$this->extension_manager = $extension_manager; +		$this->container = $container;  		$this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path();  		$this->web_root_path = $this->phpbb_path_helper->get_web_root_path(); +		$options = array_merge(array( +			'cache'			=> (defined('IN_INSTALL')) ? false : $cache_path, +			'debug'			=> false, +			'auto_reload'	=> (bool) $this->phpbb_config['load_tplcompile'], +			'autoescape'	=> false, +		), $options); +  		return parent::__construct($loader, $options);  	}  	/** +	* {@inheritdoc} +	*/ +	public function getLexer() +	{ +		if (null === $this->lexer) +		{ +			$this->lexer = $this->container->get('template.twig.lexer'); +			$this->lexer->set_environment($this); +		} + +		return $this->lexer; +	} + + +	/**  	* Get the list of enabled phpBB extensions  	*  	* Used in EVENT node @@ -75,16 +111,26 @@ class environment extends \Twig_Environment  	}  	/** -	* Get the phpBB root path -	* -	* @return string -	*/ +	 * Get the phpBB root path +	 * +	 * @return string +	 */  	public function get_phpbb_root_path()  	{  		return $this->phpbb_root_path;  	}  	/** +	* Get the filesystem object +	* +	* @return \phpbb\filesystem\filesystem +	*/ +	public function get_filesystem() +	{ +		return $this->filesystem; +	} + +	/**  	* Get the web root path  	*  	* @return string @@ -118,7 +164,7 @@ class environment extends \Twig_Environment  	* Set the namespace look up order to load templates from  	*  	* @param array $namespace -	* @return Twig_Environment +	* @return \Twig_Environment  	*/  	public function setNamespaceLookUpOrder($namespace)  	{ @@ -128,12 +174,13 @@ class environment extends \Twig_Environment  	}  	/** -	 * Loads a template by name. -	 * -	 * @param string  $name  The template name -	 * @param integer $index The index if it is an embedded template -	 * @return Twig_TemplateInterface A template instance representing the given template name -	 */ +	* Loads a template by name. +	* +	* @param string  $name  The template name +	* @param integer $index The index if it is an embedded template +	* @return \Twig_TemplateInterface A template instance representing the given template name +	* @throws \Twig_Error_Loader +	*/  	public function loadTemplate($name, $index = null)  	{  		if (strpos($name, '@') === false) @@ -164,11 +211,12 @@ class environment extends \Twig_Environment  	}  	/** -	 * Finds a template by name. -	 * -	 * @param string  $name  The template name -	 * @return string -	 */ +	* Finds a template by name. +	* +	* @param string  $name  The template name +	* @return string +	* @throws \Twig_Error_Loader +	*/  	public function findTemplate($name)  	{  		if (strpos($name, '@') === false) @@ -184,7 +232,7 @@ class environment extends \Twig_Environment  					return parent::getLoader()->getCacheKey('@' . $namespace . '/' . $name);  				} -				catch (Twig_Error_Loader $e) +				catch (\Twig_Error_Loader $e)  				{  				}  			} diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 6847dbd9f8..92f87a0331 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -14,20 +18,20 @@ class extension extends \Twig_Extension  	/** @var \phpbb\template\context */  	protected $context; -	/** @var \phpbb\user */ -	protected $user; +	/** @var \phpbb\language\language */ +	protected $language;  	/**  	* Constructor  	*  	* @param \phpbb\template\context $context -	* @param \phpbb\user $user +	* @param \phpbb\language\language $language  	* @return \phpbb\template\twig\extension  	*/ -	public function __construct(\phpbb\template\context $context, $user) +	public function __construct(\phpbb\template\context $context, $language)  	{  		$this->context = $context; -		$this->user = $user; +		$this->language = $language;  	}  	/** @@ -67,6 +71,7 @@ class extension extends \Twig_Extension  	{  		return array(  			new \Twig_SimpleFilter('subset', array($this, 'loop_subset'), array('needs_environment' => true)), +			// @deprecated 3.2.0 Uses twig's JS escape method instead of addslashes  			new \Twig_SimpleFilter('addslashes', 'addslashes'),  		);  	} @@ -123,7 +128,7 @@ class extension extends \Twig_Extension  	/**  	* Grabs a subset of a loop  	* -	* @param Twig_Environment $env          A Twig_Environment instance +	* @param \Twig_Environment $env          A Twig_Environment instance  	* @param mixed            $item         A variable  	* @param integer          $start        Start of the subset  	* @param integer          $end   	     End of the subset @@ -158,7 +163,6 @@ class extension extends \Twig_Extension  	* (e.g. in the ACP, L_TITLE)  	* If not, we return the result of $user->lang()  	* -	* @param string $lang name  	* @return string  	*/  	function lang() @@ -174,9 +178,9 @@ class extension extends \Twig_Extension  			return $context_vars['L_' . $key];  		} -		// LA_ is transformed into lang(\'$1\')|addslashes, so we should not +		// LA_ is transformed into lang(\'$1\')|escape('js'), so we should not  		// need to check for it -		return call_user_func_array(array($this->user, 'lang'), $args); +		return call_user_func_array(array($this->language, 'lang'), $args);  	}  } diff --git a/phpBB/phpbb/template/twig/extension/routing.php b/phpBB/phpbb/template/twig/extension/routing.php new file mode 100644 index 0000000000..829ce738eb --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/routing.php @@ -0,0 +1,43 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\template\twig\extension; + +use Symfony\Bridge\Twig\Extension\RoutingExtension; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + +class routing extends RoutingExtension +{ +	/** @var \phpbb\controller\helper */ +	protected $helper; + +	/** +	* Constructor +	* +	* @param \phpbb\routing\helper $helper +	*/ +	public function __construct(\phpbb\routing\helper $helper) +	{ +		$this->helper = $helper; +	} + +	public function getPath($name, $parameters = array(), $relative = false) +	{ +		return $this->helper->route($name, $parameters, true, false, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH); +	} + +	public function getUrl($name, $parameters = array(), $schemeRelative = false) +	{ +		return $this->helper->route($name, $parameters, true, false, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL); +	} +} diff --git a/phpBB/phpbb/template/twig/lexer.php b/phpBB/phpbb/template/twig/lexer.php index f4efc58540..f1542109a4 100644 --- a/phpBB/phpbb/template/twig/lexer.php +++ b/phpBB/phpbb/template/twig/lexer.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -11,6 +15,11 @@ namespace phpbb\template\twig;  class lexer extends \Twig_Lexer  { +	public function set_environment(\Twig_Environment $env) +	{ +		$this->env = $env; +	} +  	public function tokenize($code, $filename = null)  	{  		// Our phpBB tags @@ -108,9 +117,9 @@ class lexer extends \Twig_Lexer  		// Appends any filters after lang()  		$code = preg_replace('#{L_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2 }}', $code); -		// Replace all of our escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|addslashes }} -		// Appends any filters after lang(), but before addslashes -		$code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2|addslashes }}', $code); +		// Replace all of our escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|escape('js') }} +		// Appends any filters after lang(), but before escape('js') +		$code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2|escape(\'js\') }}', $code);  		// Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }}  		// Appends any filters @@ -191,9 +200,16 @@ class lexer extends \Twig_Lexer  		$parent_class = $this;  		$callback = function ($matches) use ($parent_class, $parent_nodes)  		{ -			$name = $matches[1]; -			$subset = trim(substr($matches[2], 1, -1)); // Remove parenthesis -			$body = $matches[3]; +			$hard_parents = explode('.', $matches[1]); +			array_pop($hard_parents); // ends with . +			if ($hard_parents) +			{ +				$parent_nodes = array_merge($hard_parents, $parent_nodes); +			} + +			$name = $matches[2]; +			$subset = trim(substr($matches[3], 1, -1)); // Remove parenthesis +			$body = $matches[4];  			// Replace <!-- BEGINELSE -->  			$body = str_replace('<!-- BEGINELSE -->', '{% else %}', $body); @@ -242,7 +258,7 @@ class lexer extends \Twig_Lexer  			return "{% for {$name} in {$parent}{$name}{$subset} %}{$body}{% endfor %}";  		}; -		return preg_replace_callback('#<!-- BEGIN ([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1 -->#s', $callback, $code); +		return preg_replace_callback('#<!-- BEGIN ((?:[a-zA-Z0-9_]+\.)*)([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1\2 -->#s', $callback, $code);  	}  	/** @@ -274,7 +290,7 @@ class lexer extends \Twig_Lexer  			return "<!-- {$matches[1]}IF{$inner}-->";  		}; -		return preg_replace_callback('#<!-- (ELSE)?IF((.*?) \(*!?[\$|\.]([^\s]+)(.*?))-->#', $callback, $code); +		return preg_replace_callback('#<!-- (ELSE)?IF((.*?) (?:\(*!?[\$|\.]([^\s]+)(.*?))?)-->#', $callback, $code);  	}  	/** diff --git a/phpBB/phpbb/template/twig/loader.php b/phpBB/phpbb/template/twig/loader.php index e01e9de467..8b12188a77 100644 --- a/phpBB/phpbb/template/twig/loader.php +++ b/phpBB/phpbb/template/twig/loader.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -11,17 +15,34 @@ namespace phpbb\template\twig;  /**  * Twig Template loader -* @package phpBB3  */  class loader extends \Twig_Loader_Filesystem  {  	protected $safe_directories = array();  	/** +	 * @var \phpbb\filesystem\filesystem_interface +	 */ +	protected $filesystem; + +	/** +	 * Constructor +	 * +	 * @param \phpbb\filesystem\filesystem_interface $filesystem +	 * @param string|array	$paths +	 */ +	public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, $paths = array()) +	{ +		$this->filesystem = $filesystem; + +		parent::__construct($paths); +	} + +	/**  	* Set safe directories  	*  	* @param array $directories Array of directories that are safe (empty to clear) -	* @return Twig_Loader_Filesystem +	* @return \Twig_Loader_Filesystem  	*/  	public function setSafeDirectories($directories = array())  	{ @@ -42,11 +63,11 @@ class loader extends \Twig_Loader_Filesystem  	* Add safe directory  	*  	* @param string $directory Directory that should be added -	* @return Twig_Loader_Filesystem +	* @return \Twig_Loader_Filesystem  	*/  	public function addSafeDirectory($directory)  	{ -		$directory = phpbb_realpath($directory); +		$directory = $this->filesystem->realpath($directory);  		if ($directory !== false)  		{ @@ -94,7 +115,8 @@ class loader extends \Twig_Loader_Filesystem  		// 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])) { +		if (isset($this->cache[$name])) +		{  			return $this->cache[$name];  		} @@ -107,7 +129,7 @@ class loader extends \Twig_Loader_Filesystem  			// Try validating the name (which may throw an exception)  			parent::validateName($name);  		} -		catch (Twig_Error_Loader $e) +		catch (\Twig_Error_Loader $e)  		{  			if (strpos($e->getRawMessage(), 'Looks like you try to load a template outside configured directories') === 0)  			{ @@ -115,7 +137,7 @@ class loader extends \Twig_Loader_Filesystem  				//	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)); +				$directory = $this->filesystem->realpath(dirname($file));  				if ($directory === false)  				{ diff --git a/phpBB/phpbb/template/twig/node/definenode.php b/phpBB/phpbb/template/twig/node/definenode.php index 6a9969f8c6..ddbd151d20 100644 --- a/phpBB/phpbb/template/twig/node/definenode.php +++ b/phpBB/phpbb/template/twig/node/definenode.php @@ -1,15 +1,19 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group, sections (c) 2009 Fabien Potencier, Armin Ronacher -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @copyright Portions (c) 2009 Fabien Potencier, Armin Ronacher +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\node; -  class definenode extends \Twig_Node  {  	public function __construct($capture, \Twig_NodeInterface $name, \Twig_NodeInterface $value, $lineno, $tag = null) @@ -18,15 +22,16 @@ class definenode extends \Twig_Node  	}  	/** -	 * Compiles the node to PHP. -	 * -	 * @param Twig_Compiler A Twig_Compiler instance -	 */ +	* Compiles the node to PHP. +	* +	* @param \Twig_Compiler A Twig_Compiler instance +	*/  	public function compile(\Twig_Compiler $compiler)  	{  		$compiler->addDebugInfo($this); -		if ($this->getAttribute('capture')) { +		if ($this->getAttribute('capture')) +		{  			$compiler  				->write("ob_start();\n")  				->subcompile($this->getNode('value')) diff --git a/phpBB/phpbb/template/twig/node/event.php b/phpBB/phpbb/template/twig/node/event.php index 7a1181a866..11fdb75247 100644 --- a/phpBB/phpbb/template/twig/node/event.php +++ b/phpBB/phpbb/template/twig/node/event.php @@ -1,24 +1,27 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\node; -  class event extends \Twig_Node  {  	/** -	 * The subdirectory in which all template listener files must be placed -	 * @var string -	 */ +	* The subdirectory in which all template listener files must be placed +	* @var string +	*/  	protected $listener_directory = 'event/'; -	/** @var Twig_Environment */ +	/** @var \Twig_Environment */  	protected $environment;  	public function __construct(\Twig_Node_Expression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null) @@ -29,10 +32,10 @@ class event extends \Twig_Node  	}  	/** -	 * Compiles the node to PHP. -	 * -	 * @param Twig_Compiler A Twig_Compiler instance -	 */ +	* Compiles the node to PHP. +	* +	* @param \Twig_Compiler A Twig_Compiler instance +	*/  	public function compile(\Twig_Compiler $compiler)  	{  		$compiler->addDebugInfo($this); @@ -43,7 +46,7 @@ class event extends \Twig_Node  		{  			$ext_namespace = str_replace('/', '_', $ext_namespace); -			if (defined('DEBUG')) +			if ($this->environment->isDebug())  			{  				// If debug mode is enabled, lets check for new/removed EVENT  				//  templates on page load rather than at compile. This is @@ -55,7 +58,7 @@ class event extends \Twig_Node  				;  			} -			if (defined('DEBUG') || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) +			if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html'))  			{  				$compiler  					->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n") @@ -67,7 +70,7 @@ class event extends \Twig_Node  				;  			} -			if (defined('DEBUG')) +			if ($this->environment->isDebug())  			{  				$compiler  					->outdent() diff --git a/phpBB/phpbb/template/twig/node/expression/binary/equalequal.php b/phpBB/phpbb/template/twig/node/expression/binary/equalequal.php index f3bbfa6691..2cd15d59da 100644 --- a/phpBB/phpbb/template/twig/node/expression/binary/equalequal.php +++ b/phpBB/phpbb/template/twig/node/expression/binary/equalequal.php @@ -1,15 +1,18 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\node\expression\binary; -  class equalequal extends \Twig_Node_Expression_Binary  {  	public function operator(\Twig_Compiler $compiler) diff --git a/phpBB/phpbb/template/twig/node/expression/binary/notequalequal.php b/phpBB/phpbb/template/twig/node/expression/binary/notequalequal.php index c9c2687e08..5f2908fb9b 100644 --- a/phpBB/phpbb/template/twig/node/expression/binary/notequalequal.php +++ b/phpBB/phpbb/template/twig/node/expression/binary/notequalequal.php @@ -1,15 +1,18 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\node\expression\binary; -  class notequalequal extends \Twig_Node_Expression_Binary  {  	public function operator(\Twig_Compiler $compiler) diff --git a/phpBB/phpbb/template/twig/node/includeasset.php b/phpBB/phpbb/template/twig/node/includeasset.php index f6c9dc9c58..324823b8d7 100644 --- a/phpBB/phpbb/template/twig/node/includeasset.php +++ b/phpBB/phpbb/template/twig/node/includeasset.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -11,7 +15,7 @@ namespace phpbb\template\twig\node;  abstract class includeasset extends \Twig_Node  { -	/** @var Twig_Environment */ +	/** @var \Twig_Environment */  	protected $environment;  	public function __construct(\Twig_Node_Expression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null) @@ -21,10 +25,10 @@ abstract class includeasset extends \Twig_Node  		parent::__construct(array('expr' => $expr), array(), $lineno, $tag);  	}  	/** -	 * Compiles the node to PHP. -	 * -	 * @param Twig_Compiler A Twig_Compiler instance -	 */ +	* Compiles the node to PHP. +	* +	* @param \Twig_Compiler A Twig_Compiler instance +	*/  	public function compile(\Twig_Compiler $compiler)  	{  		$compiler->addDebugInfo($this); @@ -35,7 +39,7 @@ abstract class includeasset extends \Twig_Node  			->write("\$asset_file = ")  			->subcompile($this->getNode('expr'))  			->raw(";\n") -			->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->getEnvironment()->get_path_helper());\n") +			->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->getEnvironment()->get_path_helper(), \$this->getEnvironment()->get_filesystem());\n")  			->write("if (substr(\$asset_file, 0, 2) !== './' && \$asset->is_relative()) {\n")  			->indent()  				->write("\$asset_path = \$asset->get_path();") @@ -70,7 +74,7 @@ abstract class includeasset extends \Twig_Node  	/**  	* Append the output code for the asset  	* -	* @param Twig_Compiler A Twig_Compiler instance +	* @param \Twig_Compiler A Twig_Compiler instance  	* @return null  	*/  	abstract protected function append_asset(\Twig_Compiler $compiler); diff --git a/phpBB/phpbb/template/twig/node/includecss.php b/phpBB/phpbb/template/twig/node/includecss.php index deb279fa4a..2dac154036 100644 --- a/phpBB/phpbb/template/twig/node/includecss.php +++ b/phpBB/phpbb/template/twig/node/includecss.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -27,7 +31,7 @@ class includecss extends \phpbb\template\twig\node\includeasset  		$compiler  			->raw("<link href=\"' . ")  			->raw("\$asset_file . '\"") -			->raw(' rel="stylesheet" type="text/css" media="screen, projection" />') +			->raw(' rel="stylesheet" type="text/css" media="screen" />')  		;  	}  } diff --git a/phpBB/phpbb/template/twig/node/includejs.php b/phpBB/phpbb/template/twig/node/includejs.php index 696b640eac..e77f2afeed 100644 --- a/phpBB/phpbb/template/twig/node/includejs.php +++ b/phpBB/phpbb/template/twig/node/includejs.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -24,8 +28,6 @@ class includejs extends \phpbb\template\twig\node\includeasset  	*/  	protected function append_asset(\Twig_Compiler $compiler)  	{ -		$config = $this->environment->get_phpbb_config(); -  		$compiler  			->raw("<script type=\"text/javascript\" src=\"' . ")  			->raw("\$asset_file") diff --git a/phpBB/phpbb/template/twig/node/includenode.php b/phpBB/phpbb/template/twig/node/includenode.php index d9b45d6407..c36ac3c324 100644 --- a/phpBB/phpbb/template/twig/node/includenode.php +++ b/phpBB/phpbb/template/twig/node/includenode.php @@ -1,22 +1,25 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\node; -  class includenode extends \Twig_Node_Include  {  	/** -	 * Compiles the node to PHP. -	 * -	 * @param Twig_Compiler A Twig_Compiler instance -	 */ +	* Compiles the node to PHP. +	* +	* @param \Twig_Compiler A Twig_Compiler instance +	*/  	public function compile(\Twig_Compiler $compiler)  	{  		$compiler->addDebugInfo($this); diff --git a/phpBB/phpbb/template/twig/node/includephp.php b/phpBB/phpbb/template/twig/node/includephp.php index 3f4621c0a9..76182c2f84 100644 --- a/phpBB/phpbb/template/twig/node/includephp.php +++ b/phpBB/phpbb/template/twig/node/includephp.php @@ -1,18 +1,22 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group, sections (c) 2009 Fabien Potencier, Armin Ronacher -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* Sections (c) 2009 Fabien Potencier, Armin Ronacher +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\node; -  class includephp extends \Twig_Node  { -	/** @var Twig_Environment */ +	/** @var \Twig_Environment */  	protected $environment;  	public function __construct(\Twig_Node_Expression $expr, \phpbb\template\twig\environment $environment, $lineno, $ignoreMissing = false, $tag = null) @@ -23,10 +27,10 @@ class includephp extends \Twig_Node  	}  	/** -	 * Compiles the node to PHP. -	 * -	 * @param Twig_Compiler A Twig_Compiler instance -	 */ +	* Compiles the node to PHP. +	* +	* @param \Twig_Compiler A Twig_Compiler instance +	*/  	public function compile(\Twig_Compiler $compiler)  	{  		$compiler->addDebugInfo($this); @@ -42,7 +46,8 @@ class includephp extends \Twig_Node  			return;  		} -		if ($this->getAttribute('ignore_missing')) { +		if ($this->getAttribute('ignore_missing')) +		{  			$compiler  				->write("try {\n")  				->indent() @@ -71,7 +76,8 @@ class includephp extends \Twig_Node  			->write("}\n")  		; -		if ($this->getAttribute('ignore_missing')) { +		if ($this->getAttribute('ignore_missing')) +		{  			$compiler  				->outdent()  				->write("} catch (\Twig_Error_Loader \$e) {\n") diff --git a/phpBB/phpbb/template/twig/node/php.php b/phpBB/phpbb/template/twig/node/php.php index 2b18551266..4ee415e446 100644 --- a/phpBB/phpbb/template/twig/node/php.php +++ b/phpBB/phpbb/template/twig/node/php.php @@ -1,18 +1,21 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\node; -  class php extends \Twig_Node  { -	/** @var Twig_Environment */ +	/** @var \Twig_Environment */  	protected $environment;  	public function __construct(\Twig_Node_Text $text, \phpbb\template\twig\environment $environment, $lineno, $tag = null) @@ -23,10 +26,10 @@ class php extends \Twig_Node  	}  	/** -	 * Compiles the node to PHP. -	 * -	 * @param Twig_Compiler A Twig_Compiler instance -	 */ +	* Compiles the node to PHP. +	* +	* @param \Twig_Compiler A Twig_Compiler instance +	*/  	public function compile(\Twig_Compiler $compiler)  	{  		$compiler->addDebugInfo($this); diff --git a/phpBB/phpbb/template/twig/tokenparser/defineparser.php b/phpBB/phpbb/template/twig/tokenparser/defineparser.php index 8484f2e81a..b755836ccd 100644 --- a/phpBB/phpbb/template/twig/tokenparser/defineparser.php +++ b/phpBB/phpbb/template/twig/tokenparser/defineparser.php @@ -1,24 +1,30 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group, sections (c) 2009 Fabien Potencier -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @copyright Portions (c) 2009 Fabien Potencier, Armin Ronacher +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\tokenparser; -  class defineparser extends \Twig_TokenParser  {  	/** -	 * Parses a token and returns a node. -	 * -	 * @param Twig_Token $token A Twig_Token instance -	 * -	 * @return Twig_NodeInterface A Twig_NodeInterface instance -	 */ +	* Parses a token and returns a node. +	* +	* @param \Twig_Token $token A Twig_Token instance +	* +	* @return \Twig_NodeInterface A Twig_NodeInterface instance +	* @throws \Twig_Error_Syntax +	* @throws \phpbb\template\twig\node\definenode +	*/  	public function parse(\Twig_Token $token)  	{  		$lineno = $token->getLine(); @@ -26,7 +32,8 @@ class defineparser extends \Twig_TokenParser  		$name = $this->parser->getExpressionParser()->parseExpression();  		$capture = false; -		if ($stream->test(\Twig_Token::OPERATOR_TYPE, '=')) { +		if ($stream->test(\Twig_Token::OPERATOR_TYPE, '=')) +		{  			$stream->next();  			$value = $this->parser->getExpressionParser()->parseExpression(); @@ -38,7 +45,9 @@ class defineparser extends \Twig_TokenParser  			}  			$stream->expect(\Twig_Token::BLOCK_END_TYPE); -		} else { +		} +		else +		{  			$capture = true;  			$stream->expect(\Twig_Token::BLOCK_END_TYPE); @@ -56,10 +65,10 @@ class defineparser extends \Twig_TokenParser  	}  	/** -	 * Gets the tag name associated with this token parser. -	 * -	 * @return string The tag name -	 */ +	* Gets the tag name associated with this token parser. +	* +	* @return string The tag name +	*/  	public function getTag()  	{  		return 'DEFINE'; diff --git a/phpBB/phpbb/template/twig/tokenparser/event.php b/phpBB/phpbb/template/twig/tokenparser/event.php index 8864e879f8..f73ef4ae25 100644 --- a/phpBB/phpbb/template/twig/tokenparser/event.php +++ b/phpBB/phpbb/template/twig/tokenparser/event.php @@ -1,24 +1,27 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\tokenparser; -  class event extends \Twig_TokenParser  {  	/** -	 * Parses a token and returns a node. -	 * -	 * @param Twig_Token $token A Twig_Token instance -	 * -	 * @return Twig_NodeInterface A Twig_NodeInterface instance -	 */ +	* Parses a token and returns a node. +	* +	* @param \Twig_Token $token A Twig_Token instance +	* +	* @return \Twig_NodeInterface A Twig_NodeInterface instance +	*/  	public function parse(\Twig_Token $token)  	{  		$expr = $this->parser->getExpressionParser()->parseExpression(); @@ -30,10 +33,10 @@ class event extends \Twig_TokenParser  	}  	/** -	 * Gets the tag name associated with this token parser. -	 * -	 * @return string The tag name -	 */ +	* Gets the tag name associated with this token parser. +	* +	* @return string The tag name +	*/  	public function getTag()  	{  		return 'EVENT'; diff --git a/phpBB/phpbb/template/twig/tokenparser/includecss.php b/phpBB/phpbb/template/twig/tokenparser/includecss.php index 7bf4c610b1..1f30811754 100644 --- a/phpBB/phpbb/template/twig/tokenparser/includecss.php +++ b/phpBB/phpbb/template/twig/tokenparser/includecss.php @@ -1,9 +1,13 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -12,12 +16,12 @@ namespace phpbb\template\twig\tokenparser;  class includecss extends \Twig_TokenParser  {  	/** -	 * Parses a token and returns a node. -	 * -	 * @param Twig_Token $token A Twig_Token instance -	 * -	 * @return Twig_NodeInterface A Twig_NodeInterface instance -	 */ +	* Parses a token and returns a node. +	* +	* @param \Twig_Token $token A Twig_Token instance +	* +	* @return \Twig_NodeInterface A Twig_NodeInterface instance +	*/  	public function parse(\Twig_Token $token)  	{  		$expr = $this->parser->getExpressionParser()->parseExpression(); @@ -29,10 +33,10 @@ class includecss extends \Twig_TokenParser  	}  	/** -	 * Gets the tag name associated with this token parser. -	 * -	 * @return string The tag name -	 */ +	* Gets the tag name associated with this token parser. +	* +	* @return string The tag name +	*/  	public function getTag()  	{  		return 'INCLUDECSS'; diff --git a/phpBB/phpbb/template/twig/tokenparser/includejs.php b/phpBB/phpbb/template/twig/tokenparser/includejs.php index 0e46915b86..4b67d2c468 100644 --- a/phpBB/phpbb/template/twig/tokenparser/includejs.php +++ b/phpBB/phpbb/template/twig/tokenparser/includejs.php @@ -1,24 +1,27 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\tokenparser; -  class includejs extends \Twig_TokenParser  {  	/** -	 * Parses a token and returns a node. -	 * -	 * @param Twig_Token $token A Twig_Token instance -	 * -	 * @return Twig_NodeInterface A Twig_NodeInterface instance -	 */ +	* Parses a token and returns a node. +	* +	* @param \Twig_Token $token A Twig_Token instance +	* +	* @return \Twig_NodeInterface A Twig_NodeInterface instance +	*/  	public function parse(\Twig_Token $token)  	{  		$expr = $this->parser->getExpressionParser()->parseExpression(); @@ -30,10 +33,10 @@ class includejs extends \Twig_TokenParser  	}  	/** -	 * Gets the tag name associated with this token parser. -	 * -	 * @return string The tag name -	 */ +	* Gets the tag name associated with this token parser. +	* +	* @return string The tag name +	*/  	public function getTag()  	{  		return 'INCLUDEJS'; diff --git a/phpBB/phpbb/template/twig/tokenparser/includeparser.php b/phpBB/phpbb/template/twig/tokenparser/includeparser.php index d351f1b4cd..aa7236aaa6 100644 --- a/phpBB/phpbb/template/twig/tokenparser/includeparser.php +++ b/phpBB/phpbb/template/twig/tokenparser/includeparser.php @@ -1,24 +1,28 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group, sections (c) 2009 Fabien Potencier -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @copyright Portions (c) 2009 Fabien Potencier, Armin Ronacher +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\tokenparser; -  class includeparser extends \Twig_TokenParser_Include  {  	/** -	 * Parses a token and returns a node. -	 * -	 * @param Twig_Token $token A Twig_Token instance -	 * -	 * @return Twig_NodeInterface A Twig_NodeInterface instance -	 */ +	* Parses a token and returns a node. +	* +	* @param \Twig_Token $token A Twig_Token instance +	* +	* @return \Twig_NodeInterface A Twig_NodeInterface instance +	*/  	public function parse(\Twig_Token $token)  	{  		$expr = $this->parser->getExpressionParser()->parseExpression(); @@ -29,10 +33,10 @@ class includeparser extends \Twig_TokenParser_Include  	}  	/** -	 * Gets the tag name associated with this token parser. -	 * -	 * @return string The tag name -	 */ +	* Gets the tag name associated with this token parser. +	* +	* @return string The tag name +	*/  	public function getTag()  	{  		return 'INCLUDE'; diff --git a/phpBB/phpbb/template/twig/tokenparser/includephp.php b/phpBB/phpbb/template/twig/tokenparser/includephp.php index 1b3d1742e3..3992636f8c 100644 --- a/phpBB/phpbb/template/twig/tokenparser/includephp.php +++ b/phpBB/phpbb/template/twig/tokenparser/includephp.php @@ -1,24 +1,28 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group, sections (c) 2009 Fabien Potencier, Armin Ronacher -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @copyright Portions (c) 2009 Fabien Potencier, Armin Ronacher +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\tokenparser; -  class includephp extends \Twig_TokenParser  {  	/** -	 * Parses a token and returns a node. -	 * -	 * @param Twig_Token $token A Twig_Token instance -	 * -	 * @return Twig_NodeInterface A Twig_NodeInterface instance -	 */ +	* Parses a token and returns a node. +	* +	* @param \Twig_Token $token A Twig_Token instance +	* +	* @return \Twig_NodeInterface A Twig_NodeInterface instance +	*/  	public function parse(\Twig_Token $token)  	{  		$expr = $this->parser->getExpressionParser()->parseExpression(); @@ -26,7 +30,8 @@ class includephp extends \Twig_TokenParser  		$stream = $this->parser->getStream();  		$ignoreMissing = false; -		if ($stream->test(\Twig_Token::NAME_TYPE, 'ignore')) { +		if ($stream->test(\Twig_Token::NAME_TYPE, 'ignore')) +		{  			$stream->next();  			$stream->expect(\Twig_Token::NAME_TYPE, 'missing'); @@ -39,10 +44,10 @@ class includephp extends \Twig_TokenParser  	}  	/** -	 * Gets the tag name associated with this token parser. -	 * -	 * @return string The tag name -	 */ +	* Gets the tag name associated with this token parser. +	* +	* @return string The tag name +	*/  	public function getTag()  	{  		return 'INCLUDEPHP'; diff --git a/phpBB/phpbb/template/twig/tokenparser/php.php b/phpBB/phpbb/template/twig/tokenparser/php.php index b427969e2d..f11ce35896 100644 --- a/phpBB/phpbb/template/twig/tokenparser/php.php +++ b/phpBB/phpbb/template/twig/tokenparser/php.php @@ -1,24 +1,27 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig\tokenparser; -  class php extends \Twig_TokenParser  {  	/** -	 * Parses a token and returns a node. -	 * -	 * @param Twig_Token $token A Twig_Token instance -	 * -	 * @return Twig_NodeInterface A Twig_NodeInterface instance -	 */ +	* Parses a token and returns a node. +	* +	* @param \Twig_Token $token A Twig_Token instance +	* +	* @return \Twig_NodeInterface A Twig_NodeInterface instance +	*/  	public function parse(\Twig_Token $token)  	{  		$stream = $this->parser->getStream(); @@ -38,10 +41,10 @@ class php extends \Twig_TokenParser  	}  	/** -	 * Gets the tag name associated with this token parser. -	 * -	 * @return string The tag name -	 */ +	* Gets the tag name associated with this token parser. +	* +	* @return string The tag name +	*/  	public function getTag()  	{  		return 'PHP'; diff --git a/phpBB/phpbb/template/twig/twig.php b/phpBB/phpbb/template/twig/twig.php index 83630f5992..6b3cf32bc8 100644 --- a/phpBB/phpbb/template/twig/twig.php +++ b/phpBB/phpbb/template/twig/twig.php @@ -1,17 +1,22 @@  <?php  /**  * -* @package phpBB3 -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */  namespace phpbb\template\twig; +use phpbb\template\exception\user_object_not_available; +  /**  * Twig Template class. -* @package phpBB3  */  class twig extends \phpbb\template\base  { @@ -64,7 +69,7 @@ class twig extends \phpbb\template\base  	/**  	* Twig Environment  	* -	* @var Twig_Environment +	* @var \Twig_Environment  	*/  	protected $twig; @@ -73,11 +78,14 @@ class twig extends \phpbb\template\base  	*  	* @param \phpbb\path_helper $path_helper  	* @param \phpbb\config\config $config -	* @param \phpbb\user $user  	* @param \phpbb\template\context $context template context +	* @param \phpbb\template\twig\environment $twig_environment +	* @param string $cache_path +	* @param \phpbb\user|null $user +	* @param array|\ArrayAccess $extensions  	* @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked  	*/ -	public function __construct(\phpbb\path_helper $path_helper, $config, $user, \phpbb\template\context $context, \phpbb\extension\manager $extension_manager = null) +	public function __construct(\phpbb\path_helper $path_helper, $config, \phpbb\template\context $context, \phpbb\template\twig\environment $twig_environment, $cache_path, \phpbb\user $user = null, $extensions = array(), \phpbb\extension\manager $extension_manager = null)  	{  		$this->path_helper = $path_helper;  		$this->phpbb_root_path = $path_helper->get_phpbb_root_path(); @@ -86,35 +94,13 @@ class twig extends \phpbb\template\base  		$this->user = $user;  		$this->context = $context;  		$this->extension_manager = $extension_manager; +		$this->cachepath = $cache_path; +		$this->twig = $twig_environment; -		$this->cachepath = $this->phpbb_root_path . 'cache/twig/'; - -		// Initiate the loader, __main__ namespace paths will be setup later in set_style_names() -		$loader = new \phpbb\template\twig\loader(''); - -		$this->twig = new \phpbb\template\twig\environment( -			$this->config, -			$this->path_helper, -			$this->extension_manager, -			$loader, -			array( -				'cache'			=> (defined('IN_INSTALL')) ? false : $this->cachepath, -				'debug'			=> defined('DEBUG'), -				'auto_reload'	=> (bool) $this->config['load_tplcompile'], -				'autoescape'	=> false, -			) -		); - -		$this->twig->addExtension( -			new \phpbb\template\twig\extension( -				$this->context, -				$this->user -			) -		); - -		$lexer = new \phpbb\template\twig\lexer($this->twig); - -		$this->twig->setLexer($lexer); +		foreach ($extensions as $extension) +		{ +			$this->twig->addExtension($extension); +		}  		// Add admin namespace  		if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/')) @@ -142,9 +128,16 @@ class twig extends \phpbb\template\base  	* Get the style tree of the style preferred by the current user  	*  	* @return array Style tree, most specific first +	* +	* @throws \phpbb\template\exception\user_object_not_available	When user service was not set  	*/  	public function get_user_style()  	{ +		if ($this->user === null) +		{ +			throw new user_object_not_available(); +		} +  		$style_list = array(  			$this->user->style['style_path'],  		); @@ -174,6 +167,10 @@ class twig extends \phpbb\template\base  		}  		$names = $this->get_user_style(); +		// Add 'all' folder to $names array +		//	It allows extensions to load a template file from 'all' folder, +		//	if a style doesn't include it. +		$names[] = 'all';  		$paths = array();  		foreach ($style_directories as $directory) @@ -182,13 +179,24 @@ class twig extends \phpbb\template\base  			{  				$path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";  				$template_path = $path . 'template/'; +				$theme_path = $path . 'theme/'; +				$is_valid_dir = false;  				if (is_dir($template_path))  				{ +					$is_valid_dir = true; +					$paths[] = $template_path; +				} +				if (is_dir($theme_path)) +				{ +					$is_valid_dir = true; +					$paths[] = $theme_path; +				} + +				if ($is_valid_dir) +				{  					// Add the base style directory as a safe directory  					$this->twig->getLoader()->addSafeDirectory($path); - -					$paths[] = $template_path;  				}  			}  		} @@ -211,9 +219,13 @@ class twig extends \phpbb\template\base  	*  	* 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 +	* @param string|array $names Array of names (or detailed names) or string of name of template(s) in inheritance tree order, used by extensions. +	*	E.g. array( +	*			'name' 		=> 'adm', +	*			'ext_path' 	=> 'adm/style/', +	*		) +	* @param string|array of string $paths Array of style paths, relative to current root directory +	* @return \phpbb\template\template $this  	*/  	public function set_custom_style($names, $paths)  	{ @@ -234,17 +246,46 @@ class twig extends \phpbb\template\base  				$namespace = str_replace('/', '_', $ext_namespace);  				$paths = array(); -				foreach ($names as $style_name) +				foreach ($names as $template_dir)  				{ -					$ext_style_path = $ext_path . 'styles/' . $style_name . '/'; -					$ext_style_template_path = $ext_style_path . 'template/'; +					if (is_array($template_dir)) +					{ +						if (isset($template_dir['ext_path'])) +						{ +							$ext_style_template_path = $ext_path . $template_dir['ext_path']; +							$ext_style_path = dirname($ext_style_template_path); +							$ext_style_theme_path = $ext_style_path . 'theme/'; +						} +						else +						{ +							$ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/'; +							$ext_style_template_path = $ext_style_path . 'template/'; +							$ext_style_theme_path = $ext_style_path . 'theme/'; +						} +					} +					else +					{ +						$ext_style_path = $ext_path . 'styles/' . $template_dir . '/'; +						$ext_style_template_path = $ext_style_path . 'template/'; +						$ext_style_theme_path = $ext_style_path . 'theme/'; +					} +					$is_valid_dir = false;  					if (is_dir($ext_style_template_path))  					{ +						$is_valid_dir = true; +						$paths[] = $ext_style_template_path; +					} +					if (is_dir($ext_style_theme_path)) +					{ +						$is_valid_dir = true; +						$paths[] = $ext_style_theme_path; +					} + +					if ($is_valid_dir) +					{  						// Add the base style directory as a safe directory  						$this->twig->getLoader()->addSafeDirectory($ext_style_path); - -						$paths[] = $ext_style_template_path;  					}  				} @@ -312,21 +353,29 @@ class twig extends \phpbb\template\base  			$context_vars['.'][0], // To get normal vars  			array(  				'definition'	=> new \phpbb\template\twig\definition(), -				'user'			=> $this->user,  				'loops'			=> $context_vars, // To get loops  			)  		); +		if ($this->user instanceof \phpbb\user) +		{ +			$vars['user'] = $this->user; +		} +  		// cleanup  		unset($vars['loops']['.']); +		// Inject in the main context the value added by assign_block_vars() to be able to use directly the Twig loops. +		foreach ($vars['loops'] as $key => &$value) +		{ +			$vars[$key] = $value; +		} +  		return $vars;  	}  	/** -	* Get path to template for handle (required for BBCode parser) -	* -	* @return string +	* {@inheritdoc}  	*/  	public function get_source_file_for_handle($handle)  	{ | 
