Augmentation vers version 3.3.0

This commit is contained in:
Gauvain Boiché
2020-03-31 15:31:03 +02:00
parent d926806907
commit a1864c0414
2618 changed files with 406015 additions and 31377 deletions

View File

@@ -0,0 +1,210 @@
<?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;
class asset
{
protected $components = array();
/** @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, \phpbb\filesystem\filesystem $filesystem)
{
$this->path_helper = $path_helper;
$this->filesystem = $filesystem;
$this->set_url($url);
}
/**
* Set URL
*
* @param string $url URL
*/
public function set_url($url)
{
if (version_compare(PHP_VERSION, '5.4.7') < 0 && substr($url, 0, 2) === '//')
{
// Workaround for PHP 5.4.6 and older bug #62844 - add fake scheme and then remove it
$this->components = parse_url('http:' . $url);
$this->components['scheme'] = '';
return;
}
$this->components = parse_url($url);
}
/**
* Convert URL components into string
*
* @param array $components URL components
* @return string URL
*/
protected function join_url($components)
{
$path = '';
if (isset($components['scheme']))
{
$path = $components['scheme'] === '' ? '//' : $components['scheme'] . '://';
}
if (isset($components['user']) || isset($components['pass']))
{
if ($path === '' && !isset($components['port']))
{
$path = '//';
}
$path .= $components['user'];
if (isset($components['pass']))
{
$path .= ':' . $components['pass'];
}
$path .= '@';
}
if (isset($components['host']))
{
if ($path === '' && !isset($components['port']))
{
$path = '//';
}
$path .= $components['host'];
if (isset($components['port']))
{
$path .= ':' . $components['port'];
}
}
if (isset($components['path']))
{
$path .= $components['path'];
}
if (isset($components['query']))
{
$path .= '?' . $components['query'];
}
if (isset($components['fragment']))
{
$path .= '#' . $components['fragment'];
}
return $path;
}
/**
* Get URL
*
* @return string URL
*/
public function get_url()
{
return $this->path_helper->update_web_root_path($this->join_url($this->components));
}
/**
* Checks if URL is local and relative
*
* @return boolean True if URL is local and relative
*/
public function is_relative()
{
if (empty($this->components) || !isset($this->components['path']))
{
// Invalid URL
return false;
}
return !isset($this->components['scheme']) && !isset($this->components['host']) && substr($this->components['path'], 0, 1) !== '/';
}
/**
* Get path component of current URL
*
* @return string Path
*/
public function get_path()
{
return isset($this->components['path']) ? $this->components['path'] : '';
}
/**
* Set path component
*
* @param string $path Path component
* @param boolean $urlencode If true, parts of path should be encoded with rawurlencode()
*/
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);
foreach ($paths as &$dir)
{
$dir = rawurlencode($dir);
}
$path = implode('/', $paths);
}
$this->components['path'] = $path;
}
/**
* Add assets_version parameter to URL.
* Parameter will not be added if assets_version already exists in URL
*
* @param string $version Version
*/
public function add_assets_version($version)
{
if (!isset($this->components['query']))
{
$this->components['query'] = 'assets_version=' . $version;
return;
}
$query = $this->components['query'];
if (!preg_match('/(^|[&;])assets_version=/', $query))
{
$this->components['query'] = $query . '&amp;assets_version=' . $version;
}
}
}

View File

@@ -0,0 +1,631 @@
<?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;
/**
* Stores variables assigned to template.
*/
class context
{
/**
* variable that holds all the data we'll be substituting into
* the compiled templates. Takes form:
* --> $this->tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
* if it's a root-level variable, it'll be like this:
* --> $this->tpldata[.][0][varname] == value
*
* @var array
*/
private $tpldata = array('.' => array(0 => array()));
/**
* @var array Reference to template->tpldata['.'][0]
*/
private $rootref;
/**
* @var bool
*/
private $num_rows_is_set;
public function __construct()
{
$this->clear();
}
/**
* Clears template data set.
*/
public function clear()
{
$this->tpldata = array('.' => array(0 => array()));
$this->rootref = &$this->tpldata['.'][0];
$this->num_rows_is_set = false;
}
/**
* 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 true
*/
public function assign_var($varname, $varval)
{
$this->rootref[$varname] = $varval;
return true;
}
/**
* 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 true
*/
public function append_var($varname, $varval)
{
$this->rootref[$varname] = (isset($this->rootref[$varname]) ? $this->rootref[$varname] : '') . $varval;
return true;
}
/**
* Retreive a single scalar value from a single key.
*
* @param string $varname Variable name
* @return mixed Variable value, or null if not set
*/
public function retrieve_var($varname)
{
return isset($this->rootref[$varname]) ? $this->rootref[$varname] : null;
}
/**
* Returns a reference to template data array.
*
* This function is public so that template renderer may invoke it.
* Users should alter template variables via functions in \phpbb\template\template.
*
* Note: modifying returned array will affect data stored in the context.
*
* @return array template data
*/
public function &get_data_ref()
{
// 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 = count($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.
* Users should not need to invoke this function.
*
* Note: modifying returned array will affect data stored in the context.
*
* @return array template data
*/
public function &get_root_ref()
{
// rootref is already a reference
return $this->rootref;
}
/**
* 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 true
*/
public function assign_block_vars($blockname, array $vararray)
{
$this->num_rows_is_set = false;
// For nested block, $blockcount > 0, for top-level block, $blockcount == 0
$blocks = explode('.', $blockname);
$blockcount = count($blocks) - 1;
$block = &$this->tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
$pos = strpos($blocks[$i], '[');
$name = ($pos !== false) ? substr($blocks[$i], 0, $pos) : $blocks[$i];
$block = &$block[$name];
$block_count = empty($block) ? 0 : count($block) - 1;
$index = (!$pos || strpos($blocks[$i], '[]') === $pos) ? $block_count : (min((int) substr($blocks[$i], $pos + 1, -1), $block_count));
$block = &$block[$index];
}
// $block = &$block[$blocks[$i]]; // Do not traverse the last block as it might be empty
$name = $blocks[$i];
// Assign S_ROW_COUNT and S_ROW_NUM
$s_row_count = isset($block[$name]) ? count($block[$name]) : 0;
$vararray['S_ROW_COUNT'] = $vararray['S_ROW_NUM'] = $s_row_count;
// Assign S_FIRST_ROW
if (!$s_row_count)
{
$vararray['S_FIRST_ROW'] = true;
}
// Assign S_BLOCK_NAME
$vararray['S_BLOCK_NAME'] = $name;
// Now the tricky part, we always assign S_LAST_ROW and remove the entry before
// This is much more clever than going through the complete template data on display (phew)
$vararray['S_LAST_ROW'] = true;
if ($s_row_count > 0)
{
unset($block[$name][($s_row_count - 1)]['S_LAST_ROW']);
}
// Now we add the block that we're actually assigning to.
// We're adding a new iteration to this block with the given
// variable assignments.
$block[$name][] = $vararray;
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;
}
/**
* Retrieve key variable pairs from the specified block
*
* @param string $blockname Name of block to retrieve $vararray from
* @param array $vararray An array of variable names, empty array retrieves all vars
* @return array of hashes with variable name as key and retrieved value or null as value
*/
public function retrieve_block_vars($blockname, array $vararray)
{
// For nested block, $blockcount > 0, for top-level block, $blockcount == 0
$blocks = explode('.', $blockname);
$blockcount = count($blocks) - 1;
$block = $this->tpldata;
for ($i = 0; $i <= $blockcount; $i++)
{
if (($pos = strpos($blocks[$i], '[')) !== false)
{
$name = substr($blocks[$i], 0, $pos);
if (empty($block[$name]))
{
return array();
}
if (strpos($blocks[$i], '[]') === $pos)
{
$index = count($block[$name]) - 1;
}
else
{
$index = min((int) substr($blocks[$i], $pos + 1, -1), count($block[$name]) - 1);
}
}
else
{
$name = $blocks[$i];
if (empty($block[$name]))
{
return array();
}
$index = count($block[$name]) - 1;
}
$block = $block[$name];
$block = $block[$index];
}
$result = array();
if ($vararray === array())
{
// The calculated vars that depend on the block position are excluded from the complete block returned results
$excluded_vars = array('S_FIRST_ROW', 'S_LAST_ROW', 'S_BLOCK_NAME', 'S_NUM_ROWS', 'S_ROW_COUNT', 'S_ROW_NUM');
foreach ($block as $varname => $varvalue)
{
if ($varname === strtoupper($varname) && !is_array($varvalue) && !in_array($varname, $excluded_vars))
{
$result[$varname] = $varvalue;
}
}
}
else
{
foreach ($vararray as $varname)
{
$result[$varname] = isset($block[$varname]) ? $block[$varname] : null;
}
}
return $result;
}
/**
* Find the index for a specified key in the innermost specified block
*
* @param string $blockname the blockname, for example 'loop'
* @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 search for]
*
* If key is false the position is set to 0
* If key is true the position is set to the last entry
*
* @return mixed false if not found, index position otherwise; be sure to test with ===
*/
public function find_key_index($blockname, $key)
{
// For nested block, $blockcount > 0, for top-level block, $blockcount == 0
$blocks = explode('.', $blockname);
$blockcount = count($blocks) - 1;
$block = $this->tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
$pos = strpos($blocks[$i], '[');
$name = ($pos !== false) ? substr($blocks[$i], 0, $pos) : $blocks[$i];
if (!isset($block[$name]))
{
return false;
}
$index = (!$pos || strpos($blocks[$i], '[]') === $pos) ? (count($block[$name]) - 1) : (min((int) substr($blocks[$i], $pos + 1, -1), count($block[$name]) - 1));
if (!isset($block[$name][$index]))
{
return false;
}
$block = $block[$name][$index];
}
if (!isset($block[$blocks[$i]]))
{
return false;
}
$block = $block[$blocks[$i]]; // Traverse the last block
// Change key to zero (change first position) if false and to last position if true
if (is_bool($key))
{
return (!$key) ? 0 : count($block) - 1;
}
// Get correct position if array given
if (is_array($key))
{
// Search array to get correct position
list($search_key, $search_value) = @each($key);
foreach ($block as $i => $val_ary)
{
if ($val_ary[$search_key] === $search_value)
{
return $i;
}
}
}
return (is_int($key) && ((0 <= $key) && ($key < count($block)))) ? $key : false;
}
/**
* 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', 'change' and 'delete')
*
* 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).
* If delete, the vararray is ignored, and the block at the given position (counting from zero) is removed.
*
* 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')
{
$this->num_rows_is_set = false;
// For nested block, $blockcount > 0, for top-level block, $blockcount == 0
$blocks = explode('.', $blockname);
$blockcount = count($blocks) - 1;
$block = &$this->tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
if (($pos = strpos($blocks[$i], '[')) !== false)
{
$name = substr($blocks[$i], 0, $pos);
if (strpos($blocks[$i], '[]') === $pos)
{
$index = count($block[$name]) - 1;
}
else
{
$index = min((int) substr($blocks[$i], $pos + 1, -1), count($block[$name]) - 1);
}
}
else
{
$name = $blocks[$i];
$index = count($block[$name]) - 1;
}
$block = &$block[$name];
$block = &$block[$index];
}
$name = $blocks[$i];
// If last block does not exist and we are inserting, and not searching for key, we create it empty; otherwise, nothing to do
if (!isset($block[$name]))
{
if ($mode != 'insert' || is_array($key))
{
return false;
}
$block[$name] = array();
}
$block = &$block[$name]; // Now we can traverse the last block
// Change key to zero (change first position) if false and to last position if true
if ($key === false || $key === true)
{
$key = ($key === false) ? 0 : count($block);
}
// Get correct position if array given
if (is_array($key))
{
// Search array to get correct position
list($search_key, $search_value) = @each($key);
$key = null;
foreach ($block as $i => $val_ary)
{
if ($val_ary[$search_key] === $search_value)
{
$key = $i;
break;
}
}
// key/value pair not found
if ($key === null)
{
return false;
}
}
// Insert Block
if ($mode == 'insert')
{
// Make sure we are not exceeding the last iteration
if ($key >= count($block))
{
$key = count($block);
unset($block[($key - 1)]['S_LAST_ROW']);
$vararray['S_LAST_ROW'] = true;
}
if ($key <= 0)
{
$key = 0;
unset($block[0]['S_FIRST_ROW']);
$vararray['S_FIRST_ROW'] = true;
}
// Assign S_BLOCK_NAME
$vararray['S_BLOCK_NAME'] = $name;
// Re-position template blocks
for ($i = count($block); $i > $key; $i--)
{
$block[$i] = $block[$i-1];
$block[$i]['S_ROW_COUNT'] = $block[$i]['S_ROW_NUM'] = $i;
}
// Insert vararray at given position
$block[$key] = $vararray;
$block[$key]['S_ROW_COUNT'] = $block[$key]['S_ROW_NUM'] = $key;
return true;
}
// Which block to change?
if ($mode == 'change')
{
// If key is out of bounds, do not change anything
if ($key > count($block) || $key < 0)
{
return false;
}
if ($key == count($block))
{
$key--;
}
$block[$key] = array_merge($block[$key], $vararray);
return true;
}
// Delete Block
if ($mode == 'delete')
{
// If we are exceeding last iteration, do not delete anything
if ($key > count($block) || $key < 0)
{
return false;
}
// If we are positioned at the end, we remove the last element
if ($key == count($block))
{
$key--;
}
// We are deleting the last element in the block, so remove the block
if (count($block) === 1)
{
$block = null; // unset($block); does not work on references
return true;
}
// Re-position template blocks
for ($i = $key; $i < count($block)-1; $i++)
{
$block[$i] = $block[$i+1];
$block[$i]['S_ROW_COUNT'] = $block[$i]['S_ROW_NUM'] = $i;
}
// Remove the last element
unset($block[$i]);
// Set first and last elements again, in case they were removed
$block[0]['S_FIRST_ROW'] = true;
$block[count($block)-1]['S_LAST_ROW'] = true;
return true;
}
return false;
}
/**
* 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.
$blocks = explode('.', $blockname);
$blockcount = count($blocks) - 1;
$str = &$this->tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
$str = &$str[$blocks[$i]];
$str = &$str[count($str) - 1];
}
unset($str[$blocks[$blockcount]]);
}
else
{
// Top-level block.
unset($this->tpldata[$blockname]);
}
return true;
}
}

View File

@@ -0,0 +1,224 @@
<?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;
interface template
{
/**
* Clear the cache
*
* @return \phpbb\template\template
*/
public function clear_cache();
/**
* Sets the template filenames for handles.
*
* @param array $filename_array Should be a hash of handle => filename pairs.
* @return \phpbb\template\template $this
*/
public function set_filenames(array $filename_array);
/**
* Get the style tree of the style preferred by the current user
*
* @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($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.
*
* @return \phpbb\template\template $this
*/
public function destroy();
/**
* Reset/empty complete block
*
* @param string $blockname Name of block to destroy
* @return \phpbb\template\template $this
*/
public function destroy_block_vars($blockname);
/**
* Display a template for provided handle.
*
* The template will be loaded and compiled, if necessary, first.
*
* This function calls hooks.
*
* @param string $handle Handle to display
* @return \phpbb\template\template $this
*/
public function display($handle);
/**
* Display the handle and assign the output to a template variable
* or return the compiled result.
*
* @param string $handle Handle to operate on
* @param string $template_var Template variable to assign compiled handle to
* @param bool $return_content If true return compiled handle, otherwise assign to $template_var
* @return \phpbb\template\template|string if $return_content is true return string of the compiled handle, otherwise return $this
*/
public function assign_display($handle, $template_var = '', $return_content = true);
/**
* 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);
/**
* 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);
/**
* 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);
/**
* Retrieve multiple template values
*
* @param array $vararray An array with variable names
* @return array A hash of variable name => value pairs (value is null if not set)
*/
public function retrieve_vars(array $vararray);
/**
* Retreive a single scalar value from a single key.
*
* @param string $varname Variable name
* @return mixed Variable value, or null if not set
*/
public function retrieve_var($varname);
/**
* 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);
/**
* 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);
/**
* Retrieve variable values from an specified block
* @param string $blockname Name of block to retrieve $vararray from
* @param array $vararray An array with variable names, empty array gets all vars
* @return array A hash of variable name => value pairs (value is null if not set)
*/
public function retrieve_block_vars($blockname, array $vararray);
/**
* 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', 'change' and 'delete')
*
* 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).
* If delete, the vararray is ignored, and the block at the given position (counting from zero) is removed.
*
* 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');
/**
* Find the index for a specified key in the innermost specified block
*
* @param string $blockname the blockname, for example 'loop'
* @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 search for]
*
* If key is false the position is set to 0
* If key is true the position is set to the last entry
*
* @return mixed false if not found, index position otherwise; be sure to test with ===
*/
public function find_key_index($blockname, $key);
/**
* 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);
}

View File

@@ -0,0 +1,185 @@
<?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;
class extension extends \Twig_Extension
{
/** @var \phpbb\template\context */
protected $context;
/** @var \phpbb\language\language */
protected $language;
/**
* Constructor
*
* @param \phpbb\template\context $context
* @param \phpbb\language\language $language
* @return \phpbb\template\twig\extension
*/
public function __construct(\phpbb\template\context $context, $language)
{
$this->context = $context;
$this->language = $language;
}
/**
* Get the name of this extension
*
* @return string
*/
public function getName()
{
return 'phpbb';
}
/**
* Returns the token parser instance to add to the existing list.
*
* @return array An array of Twig_TokenParser instances
*/
public function getTokenParsers()
{
return array(
new \phpbb\template\twig\tokenparser\defineparser,
new \phpbb\template\twig\tokenparser\includeparser,
new \phpbb\template\twig\tokenparser\includejs,
new \phpbb\template\twig\tokenparser\includecss,
new \phpbb\template\twig\tokenparser\event,
new \phpbb\template\twig\tokenparser\includephp,
new \phpbb\template\twig\tokenparser\php,
);
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
public function getFilters()
{
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'),
);
}
/**
* Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('lang', array($this, 'lang')),
);
}
/**
* Returns a list of operators to add to the existing list.
*
* @return array An array of operators
*/
public function getOperators()
{
return array(
array(
'!' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'),
),
array(
// precedence settings are copied from similar operators in Twig core extension
'||' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'&&' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'eq' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'ne' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'neq' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'<>' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'===' => array('precedence' => 20, 'class' => '\phpbb\template\twig\node\expression\binary\equalequal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'!==' => array('precedence' => 20, 'class' => '\phpbb\template\twig\node\expression\binary\notequalequal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'gt' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Greater', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'gte' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'ge' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'lt' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Less', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'lte' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'le' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
'mod' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
),
);
}
/**
* Grabs a subset of a loop
*
* @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
* @param Boolean $preserveKeys Whether to preserve key or not (when the input is an array)
*
* @return mixed The sliced variable
*/
function loop_subset(\Twig_Environment $env, $item, $start, $end = null, $preserveKeys = false)
{
// We do almost the same thing as Twig's slice (array_slice), except when $end is positive
if ($end >= 1)
{
// When end is > 1, subset will end on the last item in an array with the specified $end
// This is different from slice in that it is the number we end on rather than the number
// of items to grab (length)
// Start must always be the actual starting number for this calculation (not negative)
$start = ($start < 0) ? count($item) + $start : $start;
$end = $end - $start;
}
// We always include the last element (this was the past design)
$end = ($end == -1 || $end === null) ? null : $end + 1;
return twig_slice($env, $item, $start, $end, $preserveKeys);
}
/**
* Get output for a language variable (L_FOO, LA_FOO)
*
* This function checks to see if the language var was outputted to $context
* (e.g. in the ACP, L_TITLE)
* If not, we return the result of $user->lang()
*
* @return string
*/
function lang()
{
$args = func_get_args();
$key = $args[0];
$context_vars = $this->context->get_root_ref();
if (is_string($key) && isset($context_vars['L_' . $key]))
{
return $context_vars['L_' . $key];
}
// LA_ is transformed into lang(\'$1\')|escape('js'), so we should not
// need to check for it
return call_user_func_array(array($this->language, 'lang'), $args);
}
}

View File

@@ -0,0 +1,368 @@
<?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;
class lexer extends \Twig_Lexer
{
public function set_environment(\Twig_Environment $env)
{
$this->env = $env;
}
public function tokenize($code, $filename = null)
{
// Handle \Twig_Source format input
if ($code instanceof \Twig_Source)
{
$source = $code;
$code = $source->getCode();
$filename = $source->getName();
}
// Our phpBB tags
// Commented out tokens are handled separately from the main replace
$phpbb_tags = array(
/*'BEGIN',
'BEGINELSE',
'END',
'IF',
'ELSE',
'ELSEIF',
'ENDIF',
'DEFINE',
'UNDEFINE',*/
'ENDDEFINE',
'INCLUDE',
'INCLUDEPHP',
'INCLUDEJS',
'INCLUDECSS',
'PHP',
'ENDPHP',
'EVENT',
);
// Twig tag masks
$twig_tags = array(
'autoescape',
'endautoescape',
'if',
'elseif',
'else',
'endif',
'block',
'endblock',
'use',
'extends',
'embed',
'filter',
'endfilter',
'flush',
'for',
'endfor',
'macro',
'endmacro',
'import',
'from',
'sandbox',
'endsandbox',
'set',
'endset',
'spaceless',
'endspaceless',
'verbatim',
'endverbatim',
);
// Fix tokens that may have inline variables (e.g. <!-- DEFINE $TEST = '{FOO}')
$code = $this->strip_surrounding_quotes(array(
'INCLUDE',
'INCLUDEPHP',
'INCLUDEJS',
'INCLUDECSS',
), $code);
$code = $this->fix_inline_variable_tokens(array(
'DEFINE \$[a-zA-Z0-9_]+ =',
'INCLUDE',
'INCLUDEPHP',
'INCLUDEJS',
'INCLUDECSS',
), $code);
$code = $this->add_surrounding_quotes(array(
'INCLUDE',
'INCLUDEPHP',
'INCLUDEJS',
'INCLUDECSS',
), $code);
// Fix our BEGIN statements
$code = $this->fix_begin_tokens($code);
// Fix our IF tokens
$code = $this->fix_if_tokens($code);
// Fix our DEFINE tokens
$code = $this->fix_define_tokens($code);
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
$code = preg_replace('#<!-- (' . implode('|', $phpbb_tags) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
// Replace all of our twig masks with Twig code (e.g. <!-- BLOCK .+ --> with {% block $1 %})
$code = $this->replace_twig_tag_masks($code, $twig_tags);
// Replace all of our language variables, {L_VARNAME}, with Twig style, {{ lang('NAME') }}
// 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')|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
$code = preg_replace('#{([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ $1$2 }}', $code);
// Tokenize \Twig_Source instance
return parent::tokenize(new \Twig_Source($code, $filename));
}
/**
* Strip surrounding quotes
*
* First step to fix tokens that may have inline variables
* E.g. <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE {TEST}.html
*
* @param array $tokens array of tokens to search for (imploded to a regular expression)
* @param string $code
* @return string
*/
protected function strip_surrounding_quotes($tokens, $code)
{
// Remove matching quotes at the beginning/end if a statement;
// E.g. 'asdf'"' -> asdf'"
// E.g. "asdf'"" -> asdf'"
// E.g. 'asdf'" -> 'asdf'"
return preg_replace('#<!-- (' . implode('|', $tokens) . ') (([\'"])?(.*?)\1) -->#', '<!-- $1 $2 -->', $code);
}
/**
* Fix tokens that may have inline variables
*
* Second step to fix tokens that may have inline variables
* E.g. <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE ' ~ {TEST} ~ '.html
*
* @param array $tokens array of tokens to search for (imploded to a regular expression)
* @param string $code
* @return string
*/
protected function fix_inline_variable_tokens($tokens, $code)
{
$callback = function($matches)
{
// Replace template variables with start/end to parse variables (' ~ TEST ~ '.html)
$matches[2] = preg_replace('#{([a-zA-Z0-9_\.$]+)}#', "'~ \$1 ~'", $matches[2]);
return "<!-- {$matches[1]} {$matches[2]} -->";
};
return preg_replace_callback('#<!-- (' . implode('|', $tokens) . ') (.+?) -->#', $callback, $code);
}
/**
* Add surrounding quotes
*
* Last step to fix tokens that may have inline variables
* E.g. <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE '' ~ {TEST} ~ '.html'
*
* @param array $tokens array of tokens to search for (imploded to a regular expression)
* @param string $code
* @return string
*/
protected function add_surrounding_quotes($tokens, $code)
{
return preg_replace('#<!-- (' . implode('|', $tokens) . ') (.+?) -->#', '<!-- $1 \'$2\' -->', $code);
}
/**
* Fix begin tokens (convert our BEGIN to Twig for)
*
* Not meant to be used outside of this context, public because the anonymous function calls this
*
* @param string $code
* @param array $parent_nodes (used in recursion)
* @return string
*/
public function fix_begin_tokens($code, $parent_nodes = array())
{
// PHP 5.3 cannot use $this in an anonymous function, so use this as a work-around
$parent_class = $this;
$callback = function ($matches) use ($parent_class, $parent_nodes)
{
$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);
// Is the designer wanting to call another loop in a loop?
// <!-- BEGIN loop -->
// <!-- BEGIN !loop2 -->
// <!-- END !loop2 -->
// <!-- END loop -->
// 'loop2' is actually on the same nesting level as 'loop' you assign
// variables to it with template->assign_block_vars('loop2', array(...))
if (strpos($name, '!') === 0)
{
// Count the number if ! occurrences
$count = substr_count($name, '!');
for ($i = 0; $i < $count; $i++)
{
array_pop($parent_nodes);
$name = substr($name, 1);
}
}
// Remove all parent nodes, e.g. foo, bar from foo.bar.foobar.VAR
foreach ($parent_nodes as $node)
{
$body = preg_replace('#([^a-zA-Z0-9_])' . $node . '\.([a-zA-Z0-9_]+)\.#', '$1$2.', $body);
}
// Add current node to list of parent nodes for child nodes
$parent_nodes[] = $name;
// Recursive...fix any child nodes
$body = $parent_class->fix_begin_tokens($body, $parent_nodes);
// Need the parent variable name
array_pop($parent_nodes);
$parent = (!empty($parent_nodes)) ? end($parent_nodes) . '.' : '';
if ($subset !== '')
{
$subset = '|subset(' . $subset . ')';
}
$parent = ($parent) ?: 'loops.';
// Turn into a Twig for loop
return "{% for {$name} in {$parent}{$name}{$subset} %}{$body}{% endfor %}";
};
return preg_replace_callback('#<!-- BEGIN ((?:[a-zA-Z0-9_]+\.)*)([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1\2 -->#s', $callback, $code);
}
/**
* Fix IF statements
*
* @param string $code
* @return string
*/
protected function fix_if_tokens($code)
{
// 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);
$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);
}
/**
* Fix DEFINE statements and {$VARNAME} variables
*
* @param string $code
* @return string
*/
protected function fix_define_tokens($code)
{
/**
* Changing $VARNAME to definition.varname because set is only local
* context (e.g. DEFINE $TEST will only make $TEST available in current
* template and any child templates, but not any parent templates).
*
* DEFINE handles setting it properly to definition in its node, but the
* variables reading FROM it need to be altered to definition.VARNAME
*
* Setting up definition as a class in the array passed to Twig
* ($context) makes set definition.TEST available in the global context
*/
// Replace <!-- DEFINE $NAME with {% DEFINE definition.NAME
$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);
// Replace all of our variables, {$VARNAME}, with Twig style, {{ definition.VARNAME }}
$code = preg_replace('#{\$([a-zA-Z0-9_\.]+)}#', '{{ definition.$1 }}', $code);
// Replace all of our variables, ~ $VARNAME ~, with Twig style, ~ definition.VARNAME ~
$code = preg_replace('#~ \$([a-zA-Z0-9_\.]+) ~#', '~ definition.$1 ~', $code);
return $code;
}
/**
* Replace Twig tag masks with Twig tag calls
*
* E.g. <!-- BLOCK foo --> with {% block foo %}
*
* @param string $code
* @param array $twig_tags All tags we want to create a mask for
* @return string
*/
protected function replace_twig_tag_masks($code, $twig_tags)
{
$callback = function ($matches)
{
$matches[1] = strtolower($matches[1]);
return "{% {$matches[1]}{$matches[2]}%}";
};
foreach ($twig_tags as &$tag)
{
$tag = strtoupper($tag);
}
// twig_tags is an array of the twig tags, which are all lowercase, but we use all uppercase tags
$code = preg_replace_callback('#<!-- (' . implode('|', $twig_tags) . ')(.*?)-->#',$callback, $code);
return $code;
}
}

View File

@@ -0,0 +1,176 @@
<?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;
/**
* Twig Template loader
*/
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, $this->filesystem->realpath(dirname(__FILE__)));
}
/**
* 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 = $this->filesystem->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;
}
/**
* Adds a realpath call to fix a BC break in Twig 1.26 (https://github.com/twigphp/Twig/issues/2145)
*
* {@inheritdoc}
*/
public function addPath($path, $namespace = self::MAIN_NAMESPACE)
{
return parent::addPath($this->filesystem->realpath($path), $namespace);
}
/**
* 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 = $this->filesystem->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

@@ -0,0 +1,57 @@
<?php
/**
*
* 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)
{
parent::__construct(array('name' => $name, 'value' => $value), array('capture' => $capture, 'safe' => false), $lineno, $tag);
}
/**
* 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'))
{
$compiler
->write("ob_start();\n")
->subcompile($this->getNode('value'))
;
$compiler->write("\$value = ('' === \$value = ob_get_clean()) ? '' : new \Twig_Markup(\$value, \$this->env->getCharset());\n");
}
else
{
$compiler
->write("\$value = ")
->subcompile($this->getNode('value'))
->raw(";\n")
;
}
$compiler
->write("\$context['definition']->set('")
->raw($this->getNode('name')->getAttribute('name'))
->raw("', \$value);\n")
;
}
}

View File

@@ -0,0 +1,71 @@
<?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\node;
abstract class includeasset extends \Twig_Node
{
/** @var \Twig_Environment */
protected $environment;
public function __construct(\Twig_Node_Expression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null)
{
$this->environment = $environment;
parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param \Twig_Compiler A Twig_Compiler instance
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);
$config = $this->environment->get_phpbb_config();
$compiler
->write("\$asset_file = ")
->subcompile($this->getNode('expr'))
->raw(";\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();")
->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()->findTemplate(\$asset_path);\n")
->write("\$asset->set_path(\$local_file, true);\n")
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->write("\n")
->write("if (\$asset->is_relative()) {\n")
->indent()
->write("\$asset->add_assets_version('{$config['assets_version']}');\n")
->outdent()
->write("}\n")
->write("\$this->getEnvironment()->get_assets_bag()->add_{$this->get_setters_name()}(\$asset);")
;
}
/**
* Get the name of the assets bag setter
*
* @return string (e.g. 'script')
*/
abstract public function get_setters_name();
}

View File

@@ -0,0 +1,91 @@
<?php
/**
*
* 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 */
protected $environment;
public function __construct(\Twig_Node_Expression $expr, \phpbb\template\twig\environment $environment, $lineno, $ignoreMissing = false, $tag = null)
{
$this->environment = $environment;
parent::__construct(array('expr' => $expr), array('ignore_missing' => (Boolean) $ignoreMissing), $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param \Twig_Compiler A Twig_Compiler instance
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);
$config = $this->environment->get_phpbb_config();
if (!$config['tpl_allow_php'])
{
$compiler
->write("// INCLUDEPHP Disabled\n")
;
return;
}
if ($this->getAttribute('ignore_missing'))
{
$compiler
->write("try {\n")
->indent()
;
}
$compiler
->write("\$location = ")
->subcompile($this->getNode('expr'))
->raw(";\n")
->write("if (phpbb_is_absolute(\$location)) {\n")
->indent()
// Absolute path specified
->write("require(\$location);\n")
->outdent()
->write("} else if (file_exists(\$this->getEnvironment()->get_phpbb_root_path() . \$location)) {\n")
->indent()
// PHP file relative to phpbb_root_path
->write("require(\$this->getEnvironment()->get_phpbb_root_path() . \$location);\n")
->outdent()
->write("} else {\n")
->indent()
// Local path (behaves like INCLUDE)
->write("require(\$this->getEnvironment()->getLoader()->getCacheKey(\$location));\n")
->outdent()
->write("}\n")
;
if ($this->getAttribute('ignore_missing'))
{
$compiler
->outdent()
->write("} catch (\Twig_Error_Loader \$e) {\n")
->indent()
->write("// ignore missing template\n")
->outdent()
->write("}\n\n")
;
}
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
*
* 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
* @throws \Twig_Error_Syntax
* @throws \phpbb\template\twig\node\definenode
*/
public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$name = $this->parser->getExpressionParser()->parseExpression();
$capture = false;
if ($stream->test(\Twig_Token::OPERATOR_TYPE, '='))
{
$stream->next();
$value = $this->parser->getExpressionParser()->parseExpression();
if ($value instanceof \Twig_Node_Expression_Name)
{
// This would happen if someone improperly formed their DEFINE syntax
// e.g. <!-- DEFINE $VAR = foo -->
throw new \Twig_Error_Syntax('Invalid DEFINE', $token->getLine(), $this->parser->getFilename());
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
}
else
{
$capture = true;
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$value = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
}
return new \phpbb\template\twig\node\definenode($capture, $name, $value, $lineno, $this->getTag());
}
public function decideBlockEnd(\Twig_Token $token)
{
return $token->test('ENDDEFINE');
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'DEFINE';
}
}

View File

@@ -0,0 +1,44 @@
<?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\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
*/
public function parse(\Twig_Token $token)
{
$expr = $this->parser->getExpressionParser()->parseExpression();
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\event($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'EVENT';
}
}

View File

@@ -0,0 +1,44 @@
<?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\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
*/
public function parse(\Twig_Token $token)
{
$expr = $this->parser->getExpressionParser()->parseExpression();
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\includecss($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'INCLUDECSS';
}
}

View File

@@ -0,0 +1,44 @@
<?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\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
*/
public function parse(\Twig_Token $token)
{
$expr = $this->parser->getExpressionParser()->parseExpression();
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\includejs($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'INCLUDEJS';
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
*
* 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
*/
public function parse(\Twig_Token $token)
{
$expr = $this->parser->getExpressionParser()->parseExpression();
list($variables, $only, $ignoreMissing) = $this->parseArguments();
return new \phpbb\template\twig\node\includenode($expr, $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'INCLUDE';
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
*
* 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
*/
public function parse(\Twig_Token $token)
{
$expr = $this->parser->getExpressionParser()->parseExpression();
$stream = $this->parser->getStream();
$ignoreMissing = false;
if ($stream->test(\Twig_Token::NAME_TYPE, 'ignore'))
{
$stream->next();
$stream->expect(\Twig_Token::NAME_TYPE, 'missing');
$ignoreMissing = true;
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\includephp($expr, $this->parser->getEnvironment(), $token->getLine(), $ignoreMissing, $this->getTag());
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'INCLUDEPHP';
}
}

View File

@@ -0,0 +1,52 @@
<?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\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
*/
public function parse(\Twig_Token $token)
{
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideEnd'), true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\php($body, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
}
public function decideEnd(\Twig_Token $token)
{
return $token->test('ENDPHP');
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'PHP';
}
}