mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-01 03:54:56 +02:00
[feature/template-engine] Refactor $config dependency out of the filter
The template stream filter no longer depends on the $config global. Instead it uses a 'allow_php' param that is passed via stream_bucket_append's last argument. Tests also adjusted. PHPBB3-9726
This commit is contained in:
parent
ee0bba3ab6
commit
ae53623230
@ -25,6 +25,18 @@ stream_filter_register('phpbb_template', 'phpbb_template_filter');
|
|||||||
*/
|
*/
|
||||||
class phpbb_template_compile
|
class phpbb_template_compile
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Whether <!-- PHP --> tags are allowed
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $allow_php;
|
||||||
|
|
||||||
|
public function __construct($allow_php)
|
||||||
|
{
|
||||||
|
$this->allow_php = $allow_php;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compiles template in $source_file and writes compiled template to
|
* Compiles template in $source_file and writes compiled template to
|
||||||
* cache directory
|
* cache directory
|
||||||
@ -96,7 +108,7 @@ class phpbb_template_compile
|
|||||||
*/
|
*/
|
||||||
private function compile_stream_to_stream($source_stream, $dest_stream)
|
private function compile_stream_to_stream($source_stream, $dest_stream)
|
||||||
{
|
{
|
||||||
stream_filter_append($source_stream, 'phpbb_template');
|
stream_filter_append($source_stream, 'phpbb_template', null, array('allow_php' => $this->allow_php));
|
||||||
stream_copy_to_stream($source_stream, $dest_stream);
|
stream_copy_to_stream($source_stream, $dest_stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,18 @@ class phpbb_template_filter extends php_user_filter
|
|||||||
*/
|
*/
|
||||||
private $in_php;
|
private $in_php;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether <!-- PHP --> tags are allowed
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $allow_php;
|
||||||
|
|
||||||
|
public function __construct($allow_php)
|
||||||
|
{
|
||||||
|
$this->allow_php = $allow_php;
|
||||||
|
}
|
||||||
|
|
||||||
public function filter($in, $out, &$consumed, $closing)
|
public function filter($in, $out, &$consumed, $closing)
|
||||||
{
|
{
|
||||||
$written = false;
|
$written = false;
|
||||||
@ -111,6 +123,7 @@ class phpbb_template_filter extends php_user_filter
|
|||||||
{
|
{
|
||||||
$this->chunk = '';
|
$this->chunk = '';
|
||||||
$this->in_php = false;
|
$this->in_php = false;
|
||||||
|
$this->allow_php = $this->params['allow_php'];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,10 +134,8 @@ class phpbb_template_filter extends php_user_filter
|
|||||||
$data = preg_replace('#<(?:[\\?%]|script)#s', '<?php echo\'\\0\';?>', $data);
|
$data = preg_replace('#<(?:[\\?%]|script)#s', '<?php echo\'\\0\';?>', $data);
|
||||||
$data = preg_replace_callback(self::REGEX_TOKENS, array($this, 'replace'), $data);
|
$data = preg_replace_callback(self::REGEX_TOKENS, array($this, 'replace'), $data);
|
||||||
|
|
||||||
global $config;
|
|
||||||
|
|
||||||
// Remove php
|
// Remove php
|
||||||
if (!$config['tpl_allow_php'])
|
if (!$this->allow_php)
|
||||||
{
|
{
|
||||||
if ($block_start_in_php
|
if ($block_start_in_php
|
||||||
&& $this->in_php
|
&& $this->in_php
|
||||||
@ -195,8 +206,6 @@ class phpbb_template_filter extends php_user_filter
|
|||||||
return $this->compile_var_tags($matches[0]);
|
return $this->compile_var_tags($matches[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
global $config;
|
|
||||||
|
|
||||||
switch ($matches[1])
|
switch ($matches[1])
|
||||||
{
|
{
|
||||||
case 'BEGIN':
|
case 'BEGIN':
|
||||||
@ -243,11 +252,11 @@ class phpbb_template_filter extends php_user_filter
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'INCLUDEPHP':
|
case 'INCLUDEPHP':
|
||||||
return ($config['tpl_allow_php']) ? '<?php ' . $this->compile_tag_include_php($matches[2]) . ' ?>' : '';
|
return ($this->allow_php) ? '<?php ' . $this->compile_tag_include_php($matches[2]) . ' ?>' : '';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'PHP':
|
case 'PHP':
|
||||||
if ($config['tpl_allow_php'])
|
if ($this->allow_php)
|
||||||
{
|
{
|
||||||
$this->in_php = true;
|
$this->in_php = true;
|
||||||
return '<?php ';
|
return '<?php ';
|
||||||
@ -256,7 +265,7 @@ class phpbb_template_filter extends php_user_filter
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'ENDPHP':
|
case 'ENDPHP':
|
||||||
if ($config['tpl_allow_php'])
|
if ($this->allow_php)
|
||||||
{
|
{
|
||||||
$this->in_php = false;
|
$this->in_php = false;
|
||||||
return ' ?>';
|
return ' ?>';
|
||||||
|
@ -376,7 +376,7 @@ class phpbb_template
|
|||||||
|
|
||||||
$source_file = $this->_source_file_for_handle($handle);
|
$source_file = $this->_source_file_for_handle($handle);
|
||||||
|
|
||||||
$compile = new phpbb_template_compile();
|
$compile = new phpbb_template_compile($this->config['tpl_allow_php']);
|
||||||
|
|
||||||
$output_file = $this->_compiled_file_for_handle($handle);
|
$output_file = $this->_compiled_file_for_handle($handle);
|
||||||
if ($compile->compile_file_to_file($source_file, $output_file) !== false)
|
if ($compile->compile_file_to_file($source_file, $output_file) !== false)
|
||||||
|
@ -13,7 +13,7 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case
|
|||||||
{
|
{
|
||||||
public function test_includephp_relative()
|
public function test_includephp_relative()
|
||||||
{
|
{
|
||||||
$GLOBALS['config']['tpl_allow_php'] = true;
|
$this->setup_engine(array('tpl_allow_php' => true));
|
||||||
|
|
||||||
$cache_file = $this->template->cachepath . 'includephp_relative.html.php';
|
$cache_file = $this->template->cachepath . 'includephp_relative.html.php';
|
||||||
|
|
||||||
@ -21,8 +21,6 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case
|
|||||||
|
|
||||||
$this->template->set_filenames(array('test' => 'includephp_relative.html'));
|
$this->template->set_filenames(array('test' => 'includephp_relative.html'));
|
||||||
$this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
$this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
||||||
|
|
||||||
$GLOBALS['config']['tpl_allow_php'] = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_includephp_absolute()
|
public function test_includephp_absolute()
|
||||||
@ -36,7 +34,7 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case
|
|||||||
fputs($fp, $template_text);
|
fputs($fp, $template_text);
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
|
|
||||||
$GLOBALS['config']['tpl_allow_php'] = true;
|
$this->setup_engine(array('tpl_allow_php' => true));
|
||||||
|
|
||||||
$this->template->set_custom_template($cache_dir, 'tests');
|
$this->template->set_custom_template($cache_dir, 'tests');
|
||||||
$cache_file = $this->template->cachepath . 'includephp_absolute.html.php';
|
$cache_file = $this->template->cachepath . 'includephp_absolute.html.php';
|
||||||
@ -45,7 +43,5 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case
|
|||||||
|
|
||||||
$this->template->set_filenames(array('test' => 'includephp_absolute.html'));
|
$this->template->set_filenames(array('test' => 'includephp_absolute.html'));
|
||||||
$this->assertEquals("Path is absolute.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
$this->assertEquals("Path is absolute.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
||||||
|
|
||||||
$GLOBALS['config']['tpl_allow_php'] = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ class phpbb_template_subdir_includephp_from_subdir_test extends phpbb_template_t
|
|||||||
// board root.
|
// board root.
|
||||||
public function test_includephp_relative()
|
public function test_includephp_relative()
|
||||||
{
|
{
|
||||||
$GLOBALS['config']['tpl_allow_php'] = true;
|
$this->setup_engine(array('tpl_allow_php' => true));
|
||||||
|
|
||||||
$cache_file = $this->template->cachepath . 'includephp_relative.html.php';
|
$cache_file = $this->template->cachepath . 'includephp_relative.html.php';
|
||||||
|
|
||||||
@ -25,7 +25,5 @@ class phpbb_template_subdir_includephp_from_subdir_test extends phpbb_template_t
|
|||||||
|
|
||||||
$this->template->set_filenames(array('test' => 'includephp_relative.html'));
|
$this->template->set_filenames(array('test' => 'includephp_relative.html'));
|
||||||
$this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
$this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP");
|
||||||
|
|
||||||
$GLOBALS['config']['tpl_allow_php'] = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ class phpbb_template_template_compile_test extends phpbb_test_case
|
|||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
$this->template_compile = new phpbb_template_compile();
|
$this->template_compile = new phpbb_template_compile(false);
|
||||||
$this->template_path = dirname(__FILE__) . '/templates';
|
$this->template_path = dirname(__FILE__) . '/templates';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,15 +328,13 @@ class phpbb_template_template_test extends phpbb_template_template_test_case
|
|||||||
|
|
||||||
public function test_php()
|
public function test_php()
|
||||||
{
|
{
|
||||||
$GLOBALS['config']['tpl_allow_php'] = true;
|
$this->setup_engine(array('tpl_allow_php' => true));
|
||||||
|
|
||||||
$cache_file = $this->template->cachepath . 'php.html.php';
|
$cache_file = $this->template->cachepath . 'php.html.php';
|
||||||
|
|
||||||
$this->assertFileNotExists($cache_file);
|
$this->assertFileNotExists($cache_file);
|
||||||
|
|
||||||
$this->run_template('php.html', array(), array(), array(), 'test', $cache_file);
|
$this->run_template('php.html', array(), array(), array(), 'test', $cache_file);
|
||||||
|
|
||||||
$GLOBALS['config']['tpl_allow_php'] = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function alter_block_array_data()
|
public static function alter_block_array_data()
|
||||||
|
@ -29,9 +29,16 @@ class phpbb_template_template_test_case extends phpbb_test_case
|
|||||||
return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result)))));
|
return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setup_engine()
|
protected function setup_engine(array $new_config = array())
|
||||||
{
|
{
|
||||||
global $phpbb_root_path, $phpEx, $config, $user;
|
global $phpbb_root_path, $phpEx, $user;
|
||||||
|
|
||||||
|
$defaults = array(
|
||||||
|
'load_tplcompile' => true,
|
||||||
|
'tpl_allow_php' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
$config = new phpbb_config(array_merge($defaults, $new_config));
|
||||||
|
|
||||||
$this->template_path = dirname(__FILE__) . '/templates';
|
$this->template_path = dirname(__FILE__) . '/templates';
|
||||||
$this->template = new phpbb_template($phpbb_root_path, $phpEx, $config, $user);
|
$this->template = new phpbb_template($phpbb_root_path, $phpEx, $config, $user);
|
||||||
@ -53,10 +60,7 @@ class phpbb_template_template_test_case extends phpbb_test_case
|
|||||||
unlink($file);
|
unlink($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
$GLOBALS['config'] = array(
|
$this->setup_engine();
|
||||||
'load_tplcompile' => true,
|
|
||||||
'tpl_allow_php' => false,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown()
|
protected function tearDown()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user