aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes
diff options
context:
space:
mode:
authorVjacheslav Trushkin <arty@phpbb.com>2012-04-01 10:58:24 +0300
committerVjacheslav Trushkin <arty@phpbb.com>2012-04-01 10:58:24 +0300
commita7d0ef90ea921b2ed876bb933bfa022863771a6e (patch)
tree4e4f46caf99fdce436a37feed45d4ee2175589ae /phpBB/includes
parent2509853ca530b3b5e0d5b2e10080eeba5a29d937 (diff)
downloadforums-a7d0ef90ea921b2ed876bb933bfa022863771a6e.tar
forums-a7d0ef90ea921b2ed876bb933bfa022863771a6e.tar.gz
forums-a7d0ef90ea921b2ed876bb933bfa022863771a6e.tar.bz2
forums-a7d0ef90ea921b2ed876bb933bfa022863771a6e.tar.xz
forums-a7d0ef90ea921b2ed876bb933bfa022863771a6e.zip
[ticket/10665] INCLUDEJS template tag
Implementing INLCUDEJS template tag in style classes PHPBB3-10665
Diffstat (limited to 'phpBB/includes')
-rw-r--r--phpBB/includes/style/template.php21
-rw-r--r--phpBB/includes/style/template_compile.php32
-rw-r--r--phpBB/includes/style/template_filter.php61
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 = '<script src="' . htmlspecialchars($file) . '"></script>';
+ $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
@@ -26,6 +26,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 <!-- PHP --> tags are allowed
*
* @var bool
@@ -33,13 +40,30 @@ 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
@@ -76,6 +76,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
*
* Is invoked for evey chunk of the stream, allowing us
@@ -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) ? '<?php ' . $this->compile_tag_include_php($matches[2]) . ' ?>' : '';
break;
+ case 'INCLUDEJS':
+ return '<?php ' . $this->compile_tag_include_js($matches[2]) . ' ?>';
+ break;
+
case 'PHP':
if ($this->allow_php)
{
@@ -857,6 +875,45 @@ class phpbb_style_template_filter extends php_user_filter
}
/**
+ * 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:
* ' . $_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '