From a7d0ef90ea921b2ed876bb933bfa022863771a6e Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 10:58:24 +0300 Subject: [PATCH 1/9] [ticket/10665] INCLUDEJS template tag Implementing INLCUDEJS template tag in style classes PHPBB3-10665 --- phpBB/includes/style/template.php | 21 +++++++- phpBB/includes/style/template_compile.php | 32 +++++++++++- phpBB/includes/style/template_filter.php | 61 ++++++++++++++++++++++- 3 files changed, 109 insertions(+), 5 deletions(-) diff --git a/phpBB/includes/style/template.php b/phpBB/includes/style/template.php index aebf1da603..4586e8dbf9 100644 --- a/phpBB/includes/style/template.php +++ b/phpBB/includes/style/template.php @@ -288,7 +288,7 @@ class phpbb_style_template return new phpbb_style_template_renderer_include($output_file, $this); } - $compile = new phpbb_style_template_compile($this->config['tpl_allow_php']); + $compile = new phpbb_style_template_compile($this->config['tpl_allow_php'], $this->locator, $this->phpbb_root_path); if ($compile->compile_file_to_file($source_file, $output_file) !== false) { @@ -492,4 +492,23 @@ class phpbb_style_template // use resource locator to find files return $this->locator->get_first_file_location($templates, $return_default, $return_full_path); } + + /** + * Include JS file + * + * @param string $file file name + * @param bool $locate True if file needs to be located + */ + function _js_include($file, $locate = false) + { + // Locate file + if ($locate) + { + $file = $this->locator->get_first_file_location(array($file), true, true); + } + + // Add HTML code + $code = ''; + $this->context->append_var('SCRIPTS', $code); + } } diff --git a/phpBB/includes/style/template_compile.php b/phpBB/includes/style/template_compile.php index 3ef3fffc00..bd9e18e102 100644 --- a/phpBB/includes/style/template_compile.php +++ b/phpBB/includes/style/template_compile.php @@ -25,6 +25,13 @@ stream_filter_register('phpbb_template', 'phpbb_style_template_filter'); */ class phpbb_style_template_compile { + /** + * Array of parameters to forward to template filter + * + * @var array + */ + private $filter_params; + /** * Whether tags are allowed * @@ -32,14 +39,31 @@ class phpbb_style_template_compile */ private $allow_php; + /** + * Style resource locator + * + * @var phpbb_style_resource_locator + */ + private $locator; + + /** + * @var string phpBB root path + */ + private $phpbb_root_path; + /** * Constructor. * * @param bool @allow_php Whether PHP code will be allowed in templates (inline PHP code, PHP tag and INCLUDEPHP tag) + * @param phpbb_style_resource_locator $locator Resource locator + * @param string $phpbb_root_path Path to phpBB root directory */ - public function __construct($allow_php) + public function __construct($allow_php, $locator, $phpbb_root_path) { + $this->filter_params = array(); $this->allow_php = $allow_php; + $this->locator = $locator; + $this->phpbb_root_path = $phpbb_root_path; } /** @@ -116,7 +140,11 @@ class phpbb_style_template_compile */ private function compile_stream_to_stream($source_stream, $dest_stream) { - stream_filter_append($source_stream, 'phpbb_template', null, array('allow_php' => $this->allow_php)); + $params = $this->filter_params; + $params['allow_php'] = $this->allow_php; + $params['locator'] = $this->locator; + $params['phpbb_root_path'] = $this->phpbb_root_path; + stream_filter_append($source_stream, 'phpbb_template', null, $params); stream_copy_to_stream($source_stream, $dest_stream); } } diff --git a/phpBB/includes/style/template_filter.php b/phpBB/includes/style/template_filter.php index 04fe85e07f..d62ad0ba1e 100644 --- a/phpBB/includes/style/template_filter.php +++ b/phpBB/includes/style/template_filter.php @@ -75,6 +75,18 @@ class phpbb_style_template_filter extends php_user_filter */ private $allow_php; + /** + * Resource locator. + * + * @var phpbb_template_locator + */ + private $locator; + + /** + * @var string phpBB root path + */ + private $phpbb_root_path; + /** * Stream filter * @@ -126,14 +138,16 @@ class phpbb_style_template_filter extends php_user_filter /** * Initializer, called on creation. * - * Get the allow_php option from params, which is passed - * to stream_filter_append. + * Get the allow_php option, root directory and locator from params, + * which are passed to stream_filter_append. */ public function onCreate() { $this->chunk = ''; $this->in_php = false; $this->allow_php = $this->params['allow_php']; + $this->locator = $this->params['locator']; + $this->phpbb_root_path = $this->params['phpbb_root_path']; return true; } @@ -281,6 +295,10 @@ class phpbb_style_template_filter extends php_user_filter return ($this->allow_php) ? 'compile_tag_include_php($matches[2]) . ' ?>' : ''; break; + case 'INCLUDEJS': + return 'compile_tag_include_js($matches[2]) . ' ?>'; + break; + case 'PHP': if ($this->allow_php) { @@ -856,6 +874,45 @@ class phpbb_style_template_filter extends php_user_filter return $tokens; } + /** + * Compile INCLUDEJS tag + * + * @param string $tag_args Expression given with INCLUDEJS in source template + * @return string compiled template code + */ + private function compile_tag_include_js($tag_args) + { + // Process dynamic includes + if ($tag_args[0] == '{') + { + $var = $this->get_varref($tag_args, $is_expr); + if (!$is_expr) + { + return " \$_template->_js_include($var, true);"; + } + return ''; + } + + // Locate file + $filename = $this->locator->get_first_file_location(array($tag_args), false, true); + + if ($filename === false) + { + // File does not exist, find it during run time + return ' $_template->_js_include(\'' . addslashes($tag_args) . '\', true); '; + } + + if (substr($filename, 0, strlen($this->phpbb_root_path)) != $this->phpbb_root_path) + { + // Absolute path, include as is + return ' $_template->_js_include(\'' . addslashes($filename) . '\', false); '; + } + + // Relative path, remove root path from it + $filename = substr($filename, strlen($this->phpbb_root_path)); + return ' global $phpbb_root_path; $_template->_js_include($phpbb_root_path . \'' . addslashes($filename) . '\', false); '; + } + /** * Generates a reference to the given variable inside the given (possibly nested) * block namespace. This is a string of the form: From 2225efc5609add8baf0dc0e7867d9f9f0fae4245 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 10:58:59 +0300 Subject: [PATCH 2/9] [ticket/10665] INCLUDEJS template changes Implementing INLCUDEJS template tag in styles PHPBB3-10665 --- phpBB/styles/prosilver/template/overall_footer.html | 3 ++- phpBB/styles/prosilver/template/simple_footer.html | 1 + phpBB/styles/subsilver2/template/overall_footer.html | 1 + phpBB/styles/subsilver2/template/simple_footer.html | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/phpBB/styles/prosilver/template/overall_footer.html b/phpBB/styles/prosilver/template/overall_footer.html index 1561bae26a..47071ba4df 100644 --- a/phpBB/styles/prosilver/template/overall_footer.html +++ b/phpBB/styles/prosilver/template/overall_footer.html @@ -52,7 +52,8 @@ - + +{SCRIPTS} diff --git a/phpBB/styles/prosilver/template/simple_footer.html b/phpBB/styles/prosilver/template/simple_footer.html index 3458d02495..549c31cfb6 100644 --- a/phpBB/styles/prosilver/template/simple_footer.html +++ b/phpBB/styles/prosilver/template/simple_footer.html @@ -8,6 +8,7 @@ +{SCRIPTS} diff --git a/phpBB/styles/subsilver2/template/overall_footer.html b/phpBB/styles/subsilver2/template/overall_footer.html index 9b0b95372e..77b11034b9 100644 --- a/phpBB/styles/subsilver2/template/overall_footer.html +++ b/phpBB/styles/subsilver2/template/overall_footer.html @@ -10,6 +10,7 @@ +{SCRIPTS} diff --git a/phpBB/styles/subsilver2/template/simple_footer.html b/phpBB/styles/subsilver2/template/simple_footer.html index b51be3ac4c..4a65bafd1c 100644 --- a/phpBB/styles/subsilver2/template/simple_footer.html +++ b/phpBB/styles/subsilver2/template/simple_footer.html @@ -7,6 +7,7 @@ +{SCRIPTS} From 38c988fb5997f947976ca4c12e3e0d57d1e82b60 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 11:16:57 +0300 Subject: [PATCH 3/9] [ticket/10665] Changing template compiler test Adding new constructor parameters to template compiler test PHPBB3-10665 --- tests/template/template_compile_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/template/template_compile_test.php b/tests/template/template_compile_test.php index fb158cabd5..e2264fb1b7 100644 --- a/tests/template/template_compile_test.php +++ b/tests/template/template_compile_test.php @@ -16,7 +16,7 @@ class phpbb_template_template_compile_test extends phpbb_test_case protected function setUp() { - $this->template_compile = new phpbb_style_template_compile(false); + $this->template_compile = new phpbb_style_template_compile(false, null, ''); $this->template_path = dirname(__FILE__) . '/templates'; } From 4b2ede433e9336cf115fdd41e5c88738b03aa448 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 11:17:13 +0300 Subject: [PATCH 4/9] [ticket/10665] INCLUDEJS unit test Adding INLCUDEJS test PHPBB3-10665 --- tests/template/template_includejs_test.php | 48 ++++++++++++++++++++++ tests/template/templates/includejs.html | 5 +++ 2 files changed, 53 insertions(+) create mode 100644 tests/template/template_includejs_test.php create mode 100644 tests/template/templates/includejs.html diff --git a/tests/template/template_includejs_test.php b/tests/template/template_includejs_test.php new file mode 100644 index 0000000000..e0229e978b --- /dev/null +++ b/tests/template/template_includejs_test.php @@ -0,0 +1,48 @@ +setup_engine(); + + // Prepare correct result + $dir = dirname(__FILE__); + $scripts = array( + '', + '', + '' + ); + + // Run test + $cache_file = $this->template->cachepath . 'includejs.html.php'; + $this->run_template('includejs.html', array('PARENT' => 'parent_only.html'), array(), array(), implode('', $scripts), $cache_file); + } + + protected function setup_engine(array $new_config = array()) + { + global $phpbb_root_path, $phpEx, $user; + + $defaults = $this->config_defaults(); + $config = new phpbb_config(array_merge($defaults, $new_config)); + + $this->template_path = dirname(__FILE__) . '/templates'; + $this->parent_template_path = dirname(__FILE__) . '/parent_templates'; + $this->style_resource_locator = new phpbb_style_resource_locator(); + $this->style_provider = new phpbb_style_path_provider(); + $this->template = new phpbb_style_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider); + $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); + $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), ''); + $this->template->set_filenames(array('body' => 'includejs.html')); + } +} diff --git a/tests/template/templates/includejs.html b/tests/template/templates/includejs.html new file mode 100644 index 0000000000..186fc30b43 --- /dev/null +++ b/tests/template/templates/includejs.html @@ -0,0 +1,5 @@ + + + + +{SCRIPTS} \ No newline at end of file From fb0df8d2e398a634457b317a721734e53596f002 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 20:19:07 +0300 Subject: [PATCH 5/9] [ticket/10665] Moving filter parameters to one array Moving filter parameters to one array in template compiler class PHPBB3-10665 --- phpBB/includes/style/template_compile.php | 34 ++++------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/phpBB/includes/style/template_compile.php b/phpBB/includes/style/template_compile.php index bd9e18e102..fa0928f424 100644 --- a/phpBB/includes/style/template_compile.php +++ b/phpBB/includes/style/template_compile.php @@ -32,25 +32,6 @@ class phpbb_style_template_compile */ private $filter_params; - /** - * Whether tags are allowed - * - * @var bool - */ - private $allow_php; - - /** - * Style resource locator - * - * @var phpbb_style_resource_locator - */ - private $locator; - - /** - * @var string phpBB root path - */ - private $phpbb_root_path; - /** * Constructor. * @@ -60,10 +41,11 @@ class phpbb_style_template_compile */ public function __construct($allow_php, $locator, $phpbb_root_path) { - $this->filter_params = array(); - $this->allow_php = $allow_php; - $this->locator = $locator; - $this->phpbb_root_path = $phpbb_root_path; + $this->filter_params = array( + 'allow_php' => $allow_php, + 'locator' => $locator, + 'phpbb_root_path' => $phpbb_root_path + ); } /** @@ -140,11 +122,7 @@ class phpbb_style_template_compile */ private function compile_stream_to_stream($source_stream, $dest_stream) { - $params = $this->filter_params; - $params['allow_php'] = $this->allow_php; - $params['locator'] = $this->locator; - $params['phpbb_root_path'] = $this->phpbb_root_path; - stream_filter_append($source_stream, 'phpbb_template', null, $params); + stream_filter_append($source_stream, 'phpbb_template', null, $this->filter_params); stream_copy_to_stream($source_stream, $dest_stream); } } From 1ffc7c1fab85b1eb732b31bfd8a0f48c7351e68a Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 20:29:03 +0300 Subject: [PATCH 6/9] [ticket/10665] Changing template->_js_include to public Changing template->_js_include to public function PHPBB3-10665 --- phpBB/includes/style/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/style/template.php b/phpBB/includes/style/template.php index 4586e8dbf9..3f15355f7a 100644 --- a/phpBB/includes/style/template.php +++ b/phpBB/includes/style/template.php @@ -499,7 +499,7 @@ class phpbb_style_template * @param string $file file name * @param bool $locate True if file needs to be located */ - function _js_include($file, $locate = false) + public function _js_include($file, $locate = false) { // Locate file if ($locate) From 2d9d8d367389c6bc0cef7852849e3d43ac682789 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 20:37:45 +0300 Subject: [PATCH 7/9] [ticket/10665] New test class for templates with tree New parent template test class for tests that use styles tree PHPBB3-10665 --- tests/template/template_includejs_test.php | 21 ++------------ tests/template/template_inheritance_test.php | 20 ++----------- tests/template/template_locate_test.php | 20 ++----------- .../template/template_test_case_with_tree.php | 29 +++++++++++++++++++ 4 files changed, 35 insertions(+), 55 deletions(-) create mode 100644 tests/template/template_test_case_with_tree.php diff --git a/tests/template/template_includejs_test.php b/tests/template/template_includejs_test.php index e0229e978b..fa23837553 100644 --- a/tests/template/template_includejs_test.php +++ b/tests/template/template_includejs_test.php @@ -7,9 +7,9 @@ * */ -require_once dirname(__FILE__) . '/template_test_case.php'; +require_once dirname(__FILE__) . '/template_test_case_with_tree.php'; -class phpbb_template_template_includejs_test extends phpbb_template_template_test_case +class phpbb_template_template_includejs_test extends phpbb_template_template_test_case_with_tree { public function test_includejs_compilation() { @@ -28,21 +28,4 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes $cache_file = $this->template->cachepath . 'includejs.html.php'; $this->run_template('includejs.html', array('PARENT' => 'parent_only.html'), array(), array(), implode('', $scripts), $cache_file); } - - protected function setup_engine(array $new_config = array()) - { - global $phpbb_root_path, $phpEx, $user; - - $defaults = $this->config_defaults(); - $config = new phpbb_config(array_merge($defaults, $new_config)); - - $this->template_path = dirname(__FILE__) . '/templates'; - $this->parent_template_path = dirname(__FILE__) . '/parent_templates'; - $this->style_resource_locator = new phpbb_style_resource_locator(); - $this->style_provider = new phpbb_style_path_provider(); - $this->template = new phpbb_style_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider); - $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); - $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), ''); - $this->template->set_filenames(array('body' => 'includejs.html')); - } } diff --git a/tests/template/template_inheritance_test.php b/tests/template/template_inheritance_test.php index a76658701a..febfed9ef0 100644 --- a/tests/template/template_inheritance_test.php +++ b/tests/template/template_inheritance_test.php @@ -7,9 +7,9 @@ * */ -require_once dirname(__FILE__) . '/template_test_case.php'; +require_once dirname(__FILE__) . '/template_test_case_with_tree.php'; -class phpbb_template_template_inheritance_test extends phpbb_template_template_test_case +class phpbb_template_template_inheritance_test extends phpbb_template_template_test_case_with_tree { /** * @todo put test data into templates/xyz.test @@ -61,20 +61,4 @@ class phpbb_template_template_inheritance_test extends phpbb_template_template_t $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); } - - protected function setup_engine(array $new_config = array()) - { - global $phpbb_root_path, $phpEx, $user; - - $defaults = $this->config_defaults(); - $config = new phpbb_config(array_merge($defaults, $new_config)); - - $this->template_path = dirname(__FILE__) . '/templates'; - $this->parent_template_path = dirname(__FILE__) . '/parent_templates'; - $this->style_resource_locator = new phpbb_style_resource_locator(); - $this->style_provider = new phpbb_style_path_provider(); - $this->template = new phpbb_style_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider); - $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); - $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), ''); - } } diff --git a/tests/template/template_locate_test.php b/tests/template/template_locate_test.php index 89a4ae6fb1..d6e2e82a47 100644 --- a/tests/template/template_locate_test.php +++ b/tests/template/template_locate_test.php @@ -7,9 +7,9 @@ * */ -require_once dirname(__FILE__) . '/template_test_case.php'; +require_once dirname(__FILE__) . '/template_test_case_with_tree.php'; -class phpbb_template_template_locate_test extends phpbb_template_template_test_case +class phpbb_template_template_locate_test extends phpbb_template_template_test_case_with_tree { public function template_data() { @@ -65,20 +65,4 @@ class phpbb_template_template_locate_test extends phpbb_template_template_test_c $result = $this->template->locate($files, $return_default, $return_full_path); $this->assertSame($expected, $result); } - - protected function setup_engine(array $new_config = array()) - { - global $phpbb_root_path, $phpEx, $user; - - $defaults = $this->config_defaults(); - $config = new phpbb_config(array_merge($defaults, $new_config)); - - $this->template_path = dirname(__FILE__) . '/templates'; - $this->parent_template_path = dirname(__FILE__) . '/parent_templates'; - $this->style_resource_locator = new phpbb_style_resource_locator(); - $this->style_provider = new phpbb_style_path_provider(); - $this->template = new phpbb_style_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider); - $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); - $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), ''); - } } diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php new file mode 100644 index 0000000000..e76d9436cf --- /dev/null +++ b/tests/template/template_test_case_with_tree.php @@ -0,0 +1,29 @@ +config_defaults(); + $config = new phpbb_config(array_merge($defaults, $new_config)); + + $this->template_path = dirname(__FILE__) . '/templates'; + $this->parent_template_path = dirname(__FILE__) . '/parent_templates'; + $this->style_resource_locator = new phpbb_style_resource_locator(); + $this->style_provider = new phpbb_style_path_provider(); + $this->template = new phpbb_style_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider); + $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); + $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), ''); + } +} From c89ea703bda1006b382f8cc0da292f6e2a3701f5 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 20:46:10 +0300 Subject: [PATCH 8/9] [ticket/10665] Solution for T_SUPER_TEMPLATE_PATH Temporary solution for T_SUPER_TEMPLATE_PATH pointing to wrong directory, variable will be completely removed later because it will be obsolete PHPBB3-10665 --- phpBB/includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 74db8cb8fd..a44ebb04e8 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4770,7 +4770,7 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 'T_ASSETS_PATH' => "{$web_path}assets", 'T_THEME_PATH' => "{$web_path}styles/" . rawurlencode($user->theme['style_path']) . '/theme', 'T_TEMPLATE_PATH' => "{$web_path}styles/" . rawurlencode($user->theme['style_path']) . '/template', - 'T_SUPER_TEMPLATE_PATH' => ($user->theme['style_parent_id']) ? "{$web_path}styles/" . rawurlencode($user->theme['style_parent_tree']) . '/template' : "{$web_path}styles/" . rawurlencode($user->theme['style_path']) . '/template', + 'T_SUPER_TEMPLATE_PATH' => "{$web_path}styles/" . rawurlencode($user->theme['style_path']) . '/template', 'T_IMAGES_PATH' => "{$web_path}images/", 'T_SMILIES_PATH' => "{$web_path}{$config['smilies_path']}/", 'T_AVATAR_PATH' => "{$web_path}{$config['avatar_path']}/", From 37480e5594145bbead07c8f70644d52983f92913 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 20:50:59 +0300 Subject: [PATCH 9/9] [ticket/10665] Adding includejs to acp templates Adding includejs to acp overall_footer.html PHPBB3-10665 --- phpBB/adm/style/overall_footer.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/adm/style/overall_footer.html b/phpBB/adm/style/overall_footer.html index 0337080f3d..a486a69514 100644 --- a/phpBB/adm/style/overall_footer.html +++ b/phpBB/adm/style/overall_footer.html @@ -39,7 +39,8 @@ - + +{SCRIPTS}