diff options
Diffstat (limited to 'phpBB/includes/template')
-rw-r--r-- | phpBB/includes/template/compile.php | 18 | ||||
-rw-r--r-- | phpBB/includes/template/extension_path_provider.php | 130 | ||||
-rw-r--r-- | phpBB/includes/template/filter.php | 61 | ||||
-rw-r--r-- | phpBB/includes/template/locator.php | 185 | ||||
-rw-r--r-- | phpBB/includes/template/path_provider.php | 102 | ||||
-rw-r--r-- | phpBB/includes/template/path_provider_interface.php | 54 | ||||
-rw-r--r-- | phpBB/includes/template/template.php | 143 |
7 files changed, 194 insertions, 499 deletions
diff --git a/phpBB/includes/template/compile.php b/phpBB/includes/template/compile.php index b0009421c5..82b301c1a2 100644 --- a/phpBB/includes/template/compile.php +++ b/phpBB/includes/template/compile.php @@ -26,20 +26,26 @@ stream_filter_register('phpbb_template', 'phpbb_template_filter'); class phpbb_template_compile { /** - * Whether <!-- PHP --> tags are allowed + * Array of parameters to forward to template filter * - * @var bool + * @var array */ - private $allow_php; + private $filter_params; /** * 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->allow_php = $allow_php; + $this->filter_params = array( + 'allow_php' => $allow_php, + 'locator' => $locator, + 'phpbb_root_path' => $phpbb_root_path + ); } /** @@ -116,7 +122,7 @@ class phpbb_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)); + stream_filter_append($source_stream, 'phpbb_template', null, $this->filter_params); stream_copy_to_stream($source_stream, $dest_stream); } } diff --git a/phpBB/includes/template/extension_path_provider.php b/phpBB/includes/template/extension_path_provider.php deleted file mode 100644 index 2897f60ac1..0000000000 --- a/phpBB/includes/template/extension_path_provider.php +++ /dev/null @@ -1,130 +0,0 @@ -<?php -/** -* -* @package phpBB3 -* @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - -/** -* Provides a template locator with core template paths and extension template paths -* -* Finds installed template paths and makes them available to the locator. -* -* @package phpBB3 -*/ -class phpbb_template_extension_path_provider extends phpbb_extension_provider implements phpbb_template_path_provider_interface -{ - /** - * Optional prefix for template paths searched within extensions. - * - * Empty by default. Relative to the extension directory. As an example, it - * could be adm/ for admin templates. - * - * @var string - */ - protected $ext_dir_prefix = ''; - - /** - * A provider of paths to be searched for templates - * @var phpbb_template_path_provider - */ - protected $base_path_provider; - - /** - * Constructor stores extension manager - * - * @param phpbb_extension_manager $extension_manager phpBB extension manager - * @param phpbb_template_path_provider $base_path_provider A simple path provider - * to provide paths to be located in extensions - */ - public function __construct(phpbb_extension_manager $extension_manager, phpbb_template_path_provider $base_path_provider) - { - parent::__construct($extension_manager); - $this->base_path_provider = $base_path_provider; - } - - /** - * Sets a prefix for template paths searched within extensions. - * - * The prefix is inserted between the extension's path e.g. ext/foo/ and - * the looked up template path, e.g. styles/bar/template/some.html. So it - * should not have a leading slash, but should have a trailing slash. - * - * @param string $ext_dir_prefix The prefix including trailing slash - * @return null - */ - public function set_ext_dir_prefix($ext_dir_prefix) - { - $this->ext_dir_prefix = $ext_dir_prefix; - } - - /** - * Finds template paths using the extension manager - * - * Locates a path (e.g. styles/prosilver/template/) in all active extensions. - * Then appends the core template paths based in the current working - * directory. - * - * @return array List of template paths - */ - public function find() - { - $directories = array(); - - $finder = $this->extension_manager->get_finder(); - foreach ($this->base_path_provider as $path) - { - if ($path && !phpbb_is_absolute($path)) - { - $directories = array_merge($directories, $finder - ->directory('/' . $this->ext_dir_prefix . $path) - ->get_directories() - ); - } - } - - foreach ($this->base_path_provider as $path) - { - $directories[] = $path; - } - - return $directories; - } - - /** - * Overwrites the current template names and paths - * - * @param array $templates An associative map from template names to paths. - * The first element is the main template. - * If the path is false, it will be generated from - * the supplied name. - * @param string $style_root_path The root directory for styles identified - * by name only. - * @return null - */ - public function set_templates(array $templates, $style_root_path) - { - $this->base_path_provider->set_templates($templates, $style_root_path); - $this->items = null; - } - - /** - * Retrieves the path to the main template passed into set_templates() - * - * @return string Main template path - */ - public function get_main_template_path() - { - return $this->base_path_provider->get_main_template_path(); - } -} diff --git a/phpBB/includes/template/filter.php b/phpBB/includes/template/filter.php index f55c8d7e9c..4a2593b757 100644 --- a/phpBB/includes/template/filter.php +++ b/phpBB/includes/template/filter.php @@ -76,6 +76,18 @@ class phpbb_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_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_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_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'] . ' diff --git a/phpBB/includes/template/locator.php b/phpBB/includes/template/locator.php index d4502d7984..01c79eec4e 100644 --- a/phpBB/includes/template/locator.php +++ b/phpBB/includes/template/locator.php @@ -17,107 +17,31 @@ if (!defined('IN_PHPBB')) /** -* Template locator. Maintains mapping from template handles to source paths. +* Resource locator interface. * -* Template locator is aware of template inheritance, and can return actual -* filesystem paths (i.e., the "primary" template or the "parent" template) +* Objects implementing this interface maintain mapping from template handles +* to source template file paths and locate templates. +* +* Locates style files. +* +* Resource locator is aware of styles tree, and can return actual +* filesystem paths (i.e., the "child" style or the "parent" styles) * depending on what files exist. * +* Root paths stored in locator are paths to style directories. Templates are +* stored in subdirectory that $template_path points to. +* * @package phpBB3 */ -class phpbb_template_locator +interface phpbb_template_locator { /** - * Paths to directories that templates are stored in. - * @var array - */ - private $roots = array(); - - /** - * Index of the main template in the roots array - * @var int - */ - private $main_root_id = 0; - - /** - * Map from root index to handles to source template file paths. - * Normally it only contains paths for handles that are used - * (or are likely to be used) by the page being rendered and not - * all templates that exist on the filesystem. - * @var array - */ - private $files = array(); - - /** - * Map from handles to source template file names. - * Covers the same data as $files property but maps to basenames - * instead of paths. - * @var array - */ - private $filenames = array(); - - /** - * Set main template location (must have been added through set_paths first). - * - * @param string $template_path Path to template directory - * @return null - */ - public function set_main_template($template) - { - $this->main_root_id = array_search($template, $this->roots, true); - } - - /** - * Sets the list of template paths - * - * These paths will be searched for template files in the provided order. - * Paths may be outside of phpBB, but templates loaded from these paths - * will still be cached. - * - * @param array $template_paths An array of paths to template directories - * @return null - */ - public function set_paths($template_paths) - { - $this->roots = array(); - $this->files = array(); - $this->filenames = array(); - $this->main_root_id = 0; - - foreach ($template_paths as $path) - { - // Make sure $path has no ending slash - if (substr($path, -1) === '/') - { - $path = substr($path, 0, -1); - } - $this->roots[] = $path; - } - } - - /** * Sets the template filenames for handles. $filename_array * should be a hash of handle => filename pairs. * * @param array $filname_array Should be a hash of handle => filename pairs. */ - public function set_filenames(array $filename_array) - { - foreach ($filename_array as $handle => $filename) - { - if (empty($filename)) - { - trigger_error("template locator: set_filenames: Empty filename specified for $handle", E_USER_ERROR); - } - - $this->filename[$handle] = $filename; - - foreach ($this->roots as $root_index => $root) - { - $this->files[$root_index][$handle] = $root . '/' . $filename; - } - } - } + public function set_filenames(array $filename_array); /** * Determines the filename for a template handle. @@ -129,50 +53,33 @@ class phpbb_template_locator * @param $handle string Template handle * @return string Filename corresponding to the template handle */ - public function get_filename_for_handle($handle) - { - if (!isset($this->filename[$handle])) - { - trigger_error("template locator: get_filename_for_handle: No file specified for handle $handle", E_USER_ERROR); - } - return $this->filename[$handle]; - } + public function get_filename_for_handle($handle); /** * Determines the source file path for a template handle without - * regard for template inheritance. + * regard for styles tree. * - * This function returns the path in "primary" template directory + * This function returns the path in "primary" style directory * corresponding to the given template handle. That path may or * may not actually exist on the filesystem. Because this function * does not perform stat calls to determine whether the path it * returns actually exists, it is faster than get_source_file_for_handle. * * Use get_source_file_for_handle to obtain the actual path that is - * guaranteed to exist (which might come from the parent/fallback - * template directory if template inheritance is used). + * guaranteed to exist (which might come from the parent style + * directory if primary style has parent styles). * * This function will trigger an error if the handle was never * associated with a template file via set_filenames. * * @param $handle string Template handle - * @return string Path to source file path in primary template directory + * @return string Path to source file path in primary style directory */ - public function get_virtual_source_file_for_handle($handle) - { - // If we don't have a file assigned to this handle, die. - if (!isset($this->files[$this->main_root_id][$handle])) - { - trigger_error("template locator: No file specified for handle $handle", E_USER_ERROR); - } - - $source_file = $this->files[$this->main_root_id][$handle]; - return $source_file; - } + public function get_virtual_source_file_for_handle($handle); /** * Determines the source file path for a template handle, accounting - * for template inheritance and verifying that the path exists. + * for styles tree and verifying that the path exists. * * This function returns the actual path that may be compiled for * the specified template handle. It will trigger an error if @@ -181,34 +88,34 @@ class phpbb_template_locator * filesystem. * * Use get_virtual_source_file_for_handle to just resolve a template - * handle to a path without any filesystem or inheritance checks. + * handle to a path without any filesystem or styles tree checks. * * @param string $handle Template handle (i.e. "friendly" template name) + * @param bool $find_all If true, each root path will be checked and function + * will return array of files instead of string and will not + * trigger a error if template does not exist * @return string Source file path */ - public function get_source_file_for_handle($handle) - { - // If we don't have a file assigned to this handle, die. - if (!isset($this->files[$this->main_root_id][$handle])) - { - trigger_error("template locator: No file specified for handle $handle", E_USER_ERROR); - } - - // locate a source file that exists - $source_file = $this->files[0][$handle]; - $tried = $source_file; - for ($i = 1, $n = count($this->roots); $i < $n && !file_exists($source_file); $i++) - { - $source_file = $this->files[$i][$handle]; - $tried .= ', ' . $source_file; - } - - // search failed - if (!file_exists($source_file)) - { - trigger_error("template locator: File for handle $handle does not exist. Could not find: $tried", E_USER_ERROR); - } + public function get_source_file_for_handle($handle, $find_all = false); - return $source_file; - } + /** + * Locates source file path, accounting for styles tree and verifying that + * the path exists. + * + * Unlike previous functions, this function works without template handle + * and it can search for more than one file. If more than one file name is + * specified, it will return location of file that it finds first. + * + * @param array $files List of files to locate. + * @param bool $return_default Determines what to return if file does not + * exist. If true, function will return location where file is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to file. If false, function will return file name. This + * parameter can be used to check which one of set of files + * is available. + * @return string or boolean Source file path if file exists or $return_default is + * true. False if file does not exist and $return_default is false + */ + public function get_first_file_location($files, $return_default = false, $return_full_path = true); } diff --git a/phpBB/includes/template/path_provider.php b/phpBB/includes/template/path_provider.php deleted file mode 100644 index b0b545973d..0000000000 --- a/phpBB/includes/template/path_provider.php +++ /dev/null @@ -1,102 +0,0 @@ -<?php -/** -* -* @package phpBB3 -* @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - -/** -* Provides a template locator with paths -* -* Finds installed template paths and makes them available to the locator. -* -* @package phpBB3 -*/ -class phpbb_template_path_provider implements IteratorAggregate, phpbb_template_path_provider_interface -{ - protected $main_template_name = ''; - protected $paths = array(); - - /** - * Ignores the extension dir prefix - * - * @param string $ext_dir_prefix The prefix including trailing slash - * @return null - */ - public function set_ext_dir_prefix($ext_dir_prefix) - { - } - - /** - * Overwrites the current template names and paths - * - * The first element of the passed templates map, is considered the main - * template and can be retrieved through get_main_template_path(). - * - * @param array $templates An associative map from template names to paths. - * The first element is the main template. - * If the path is false, it will be generated from - * the supplied name. - * @param string $style_root_path The root directory for styles identified - * by name only. - * @return null - */ - public function set_templates(array $templates, $style_root_path) - { - $this->paths = array(); - - foreach ($templates as $name => $path) - { - if (!$path) - { - $path = $style_root_path . $this->template_root_for_style($name); - } - - $this->paths[] = $path; - } - - $this->main_template_path = $this->paths[0]; - } - - /** - * Retrieves the path to the main template passed into set_templates() - * - * @return string Main template path - */ - public function get_main_template_path() - { - return $this->main_template_path; - } - - /** - * Converts a style name to relative (to board root or extension) path to - * the style's template files. - * - * @param $style_name string Style name - * @return string Path to style template files - */ - private function template_root_for_style($style_name) - { - return 'styles/' . $style_name . '/template'; - } - - /** - * Retrieve an iterator over all template paths - * - * @return ArrayIterator An iterator for the array of template paths - */ - public function getIterator() - { - return new ArrayIterator($this->paths); - } -} diff --git a/phpBB/includes/template/path_provider_interface.php b/phpBB/includes/template/path_provider_interface.php deleted file mode 100644 index 8b3a406d2a..0000000000 --- a/phpBB/includes/template/path_provider_interface.php +++ /dev/null @@ -1,54 +0,0 @@ -<?php -/** -* -* @package phpBB3 -* @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - -/** -* Provides a template locator with paths -* -* Finds installed template paths and makes them available to the locator. -* -* @package phpBB3 -*/ -interface phpbb_template_path_provider_interface extends Traversable -{ - /** - * Defines a prefix to use for template paths in extensions - * - * @param string $ext_dir_prefix The prefix including trailing slash - * @return null - */ - public function set_ext_dir_prefix($ext_dir_prefix); - - /** - * Overwrites the current template names and paths - * - * @param array $templates An associative map from template names to paths. - * The first element is the main template. - * If the path is false, it will be generated from - * the supplied name. - * @param string $style_root_path The root directory for styles identified - * by name only. - * @return null - */ - public function set_templates(array $templates, $style_root_path); - - /** - * Retrieves the path to the main template passed into set_templates() - * - * @return string Main template path - */ - public function get_main_template_path(); -} diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index bac5445511..e6512c8417 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -32,33 +32,39 @@ if (!defined('IN_PHPBB')) class phpbb_template { /** - * @var phpbb_template_context Template context. + * Template context. * Stores template data used during template rendering. + * @var phpbb_template_context */ - private $context; + public $context; /** - * @var string Path of the cache directory for the template + * Path of the cache directory for the template + * @var string */ public $cachepath = ''; /** - * @var string phpBB root path + * phpBB root path + * @var string */ private $phpbb_root_path; /** - * @var phpEx PHP file extension + * PHP file extension + * @var string */ private $phpEx; /** - * @var phpbb_config phpBB config instance + * phpBB config instance + * @var phpbb_config */ private $config; /** - * @var user current user + * Current user + * @var phpbb_user */ private $user; @@ -69,10 +75,10 @@ class phpbb_template private $locator; /** - * Template path provider - * @var phpbb_template_path_provider + * Location of templates directory within style directories + * @var string */ - private $provider; + public $template_path = 'template/'; /** * Constructor. @@ -80,68 +86,15 @@ class phpbb_template * @param string $phpbb_root_path phpBB root path * @param user $user current user * @param phpbb_template_locator $locator template locator - * @param phpbb_template_path_provider $provider template path provider */ - public function __construct($phpbb_root_path, $phpEx, $config, $user, phpbb_template_locator $locator, phpbb_template_path_provider_interface $provider) + public function __construct($phpbb_root_path, $phpEx, $config, $user, phpbb_template_locator $locator) { $this->phpbb_root_path = $phpbb_root_path; $this->phpEx = $phpEx; $this->config = $config; $this->user = $user; $this->locator = $locator; - $this->provider = $provider; - } - - /** - * Set template location based on (current) user's chosen style. - */ - public function set_template() - { - $template_name = $this->user->theme['template_path']; - $fallback_name = ($this->user->theme['template_inherits_id']) ? $this->user->theme['template_inherit_path'] : false; - - return $this->set_custom_template(false, $template_name, false, $fallback_name); - } - - /** - * Defines a prefix to use for template paths in extensions - * - * @param string $ext_dir_prefix The prefix including trailing slash - * @return null - */ - public function set_ext_dir_prefix($ext_dir_prefix) - { - $this->provider->set_ext_dir_prefix($ext_dir_prefix); - } - - /** - * Set custom template location (able to use directory outside of phpBB). - * - * Note: Templates are still compiled to phpBB's cache directory. - * - * @param string $template_path Path to template directory - * @param string $template_name Name of template - * @param string $fallback_template_path Path to fallback template - * @param string $fallback_template_name Name of fallback template - */ - public function set_custom_template($template_path, $template_name, $fallback_template_path = false, $fallback_template_name = false) - { - $templates = array($template_name => $template_path); - - if ($fallback_template_name !== false) - { - $templates[$fallback_template_name] = $fallback_template_path; - } - - $this->provider->set_templates($templates, $this->phpbb_root_path); - $this->locator->set_paths($this->provider); - $this->locator->set_main_template($this->provider->get_main_template_path()); - - $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $template_name) . '_'; - - $this->context = new phpbb_template_context(); - - return true; + $this->template_path = $this->locator->template_path; } /** @@ -333,7 +286,7 @@ class phpbb_template return new phpbb_template_renderer_include($output_file, $this); } - $compile = new phpbb_template_compile($this->config['tpl_allow_php']); + $compile = new phpbb_template_compile($this->config['tpl_allow_php'], $this->locator, $this->phpbb_root_path); if ($compile->compile_file_to_file($source_file, $output_file) !== false) { @@ -501,4 +454,62 @@ class phpbb_template } include($file); } + + /** + * Locates source template path, accounting for styles tree and verifying that + * the path exists. + * + * @param string or array $files List of templates to locate. If there is only + * one template, $files can be a string to make code easier to read. + * @param bool $return_default Determines what to return if template does not + * exist. If true, function will return location where template is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to template. If false, function will return template file name. + * This parameter can be used to check which one of set of template + * files is available. + * @return string or boolean Source template path if template exists or $return_default is + * true. False if template does not exist and $return_default is false + */ + public function locate($files, $return_default = false, $return_full_path = true) + { + // add tempalte path prefix + $templates = array(); + if (is_string($files)) + { + $templates[] = $this->template_path . $files; + } + else + { + foreach ($files as $file) + { + $templates[] = $this->template_path . $file; + } + } + + // 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 + */ + public function _js_include($file, $locate = false) + { + // Locate file + if ($locate) + { + $file = $this->locator->get_first_file_location(array($file), true, true); + } + + $file .= (strpos($file, '?') === false) ? '?' : '&'; + $file .= 'assets_version=' . $this->config['assets_version']; + + // Add HTML code + $code = '<script src="' . htmlspecialchars($file) . '"></script>'; + $this->context->append_var('SCRIPTS', $code); + } } |