diff options
Diffstat (limited to 'phpBB/includes/style')
| -rw-r--r-- | phpBB/includes/style/extension_path_provider.php | 119 | ||||
| -rw-r--r-- | phpBB/includes/style/path_provider.php | 62 | ||||
| -rw-r--r-- | phpBB/includes/style/path_provider_interface.php | 42 | ||||
| -rw-r--r-- | phpBB/includes/style/resource_locator.php | 291 | ||||
| -rw-r--r-- | phpBB/includes/style/style.php | 186 | 
5 files changed, 700 insertions, 0 deletions
diff --git a/phpBB/includes/style/extension_path_provider.php b/phpBB/includes/style/extension_path_provider.php new file mode 100644 index 0000000000..4eac300424 --- /dev/null +++ b/phpBB/includes/style/extension_path_provider.php @@ -0,0 +1,119 @@ +<?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 style resource locator with core style paths and extension style paths +* +* Finds installed style paths and makes them available to the resource locator. +* +* @package phpBB3 +*/ +class phpbb_style_extension_path_provider extends phpbb_extension_provider implements phpbb_style_path_provider_interface +{ +	/** +	* Optional prefix for style paths searched within extensions. +	* +	* Empty by default. Relative to the extension directory. As an example, it +	* could be adm/ for admin style. +	* +	* @var string +	*/ +	protected $ext_dir_prefix = ''; + +	/** +	* A provider of paths to be searched for styles +	* @var phpbb_style_path_provider +	*/ +	protected $base_path_provider; + +	/** +	* Constructor stores extension manager +	* +	* @param phpbb_extension_manager $extension_manager phpBB extension manager +	* @param phpbb_style_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_style_path_provider $base_path_provider) +	{ +		parent::__construct($extension_manager); +		$this->base_path_provider = $base_path_provider; +	} + +	/** +	* Sets a prefix for style paths searched within extensions. +	* +	* The prefix is inserted between the extension's path e.g. ext/foo/ and +	* the looked up style path, e.g. styles/bar/. 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 style paths using the extension manager +	* +	* Locates a path (e.g. styles/prosilver/) in all active extensions. +	* Then appends the core style paths based in the current working +	* directory. +	* +	* @return array     List of style paths +	*/ +	public function find() +	{ +		$directories = array(); + +		$finder = $this->extension_manager->get_finder(); +		foreach ($this->base_path_provider as $key => $paths) +		{ +			if ($key == 'style') +			{ +				foreach ($paths as $path) +				{ +					$directories['style'][] = $path; +					if ($path && !phpbb_is_absolute($path)) +					{ +						$result = $finder->directory('/' . $this->ext_dir_prefix . $path) +							->get_directories(true, true); +						foreach ($result as $ext => $ext_path) +						{ +							$directories[$ext][] = $ext_path; +						} +					} +				} +			} +		} + +		return $directories; +	} + +	/** +	* Overwrites the current style paths +	* +	* @param array $styles An array of style paths. The first element is the main style. +	* @return null +	*/ +	public function set_styles(array $styles) +	{ +		$this->base_path_provider->set_styles($styles); +		$this->items = null; +	} +} diff --git a/phpBB/includes/style/path_provider.php b/phpBB/includes/style/path_provider.php new file mode 100644 index 0000000000..731d682e88 --- /dev/null +++ b/phpBB/includes/style/path_provider.php @@ -0,0 +1,62 @@ +<?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 style resource locator with paths +* +* Finds installed style paths and makes them available to the resource locator. +* +* @package phpBB3 +*/ +class phpbb_style_path_provider implements IteratorAggregate, phpbb_style_path_provider_interface +{ +	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 style paths +	* +	* The first element of the passed styles map, is considered the main +	* style. +	* +	* @param array $styles An array of style paths. The first element is the main style. +	* @return null +	*/ +	public function set_styles(array $styles) +	{ +		$this->paths = array('style' => $styles); +	} + +	/** +	* Retrieve an iterator over all style paths +	* +	* @return ArrayIterator An iterator for the array of style paths +	*/ +	public function getIterator() +	{ +		return new ArrayIterator($this->paths); +	} +} diff --git a/phpBB/includes/style/path_provider_interface.php b/phpBB/includes/style/path_provider_interface.php new file mode 100644 index 0000000000..1a6153a4d3 --- /dev/null +++ b/phpBB/includes/style/path_provider_interface.php @@ -0,0 +1,42 @@ +<?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 style resource locator with paths +* +* Finds installed style paths and makes them available to the resource locator. +* +* @package phpBB3 +*/ +interface phpbb_style_path_provider_interface extends Traversable +{ +	/** +	* Defines a prefix to use for style 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 style paths +	* +	* @param array $styles An array of style paths. The first element is the main style. +	* @return null +	*/ +	public function set_styles(array $styles); +} diff --git a/phpBB/includes/style/resource_locator.php b/phpBB/includes/style/resource_locator.php new file mode 100644 index 0000000000..fafa11c352 --- /dev/null +++ b/phpBB/includes/style/resource_locator.php @@ -0,0 +1,291 @@ +<?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; +} + + +/** +* Style resource locator.  +* Maintains mapping from template handles to source template file paths. +* Locates style files: resources (such as .js and .css files) and templates. +* +* Style 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_style_resource_locator implements phpbb_template_locator +{ +	/** +	* Paths to style directories. +	* @var array +	*/ +	private $roots = array(); + +	/** +	* Location of templates directory within style directories. +	* Must have trailing slash. Empty if templates are stored in root +	* style directory, such as admin control panel templates. +	* @var string +	*/ +	public $template_path = 'template/'; + +	/** +	* 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(); + +	/** +	* Sets the list of style paths +	* +	* These paths will be searched for style files in the provided order. +	* Paths may be outside of phpBB, but templates loaded from these paths +	* will still be cached. +	* +	* @param array $style_paths An array of paths to style directories +	* @return null +	*/ +	public function set_paths($style_paths) +	{ +		$this->roots = array(); +		$this->files = array(); +		$this->filenames = array(); + +		foreach ($style_paths as $key => $paths) +		{ +			foreach ($paths as $path) +			{ +				// Make sure $path has no ending slash +				if (substr($path, -1) === '/') +				{ +					$path = substr($path, 0, -1); +				} +				$this->roots[$key][] = $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("style resource locator: set_filenames: Empty filename specified for $handle", E_USER_ERROR); +			} + +			$this->filename[$handle] = $filename; + +			foreach ($this->roots as $root_key => $root_paths) +			{ +				foreach ($root_paths as $root_index => $root) +				{ +					$this->files[$root_key][$root_index][$handle] = $root . '/' . $this->template_path . $filename; +				} +			} +		} +	} + +	/** +	* Determines the filename for a template handle. +	* +	* The filename comes from array used in a set_filenames call, +	* which should have been performed prior to invoking this function. +	* Return value is a file basename (without path). +	* +	* @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("style resource locator: get_filename_for_handle: No file specified for handle $handle", E_USER_ERROR); +		} +		return $this->filename[$handle]; +	} + +	/** +	* Determines the source file path for a template handle without +	* regard for styles tree. +	* +	* 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 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 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['style'][0][$handle])) +		{ +			trigger_error("style resource locator: No file specified for handle $handle", E_USER_ERROR); +		} + +		$source_file = $this->files['style'][0][$handle]; +		return $source_file; +	} + +	/** +	* Determines the source file path for a template handle, accounting +	* 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 +	* the template handle was never associated with a template path +	* via set_filenames or if the template file does not exist on the +	* filesystem. +	* +	* Use get_virtual_source_file_for_handle to just resolve a template +	* 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, $find_all = false) +	{ +		// If we don't have a file assigned to this handle, die. +		if (!isset($this->files['style'][0][$handle])) +		{ +			trigger_error("style resource locator: No file specified for handle $handle", E_USER_ERROR); +		} + +		// locate a source file that exists +		$source_file = $this->files['style'][0][$handle]; +		$tried = $source_file; +		$found = false; +		$found_all = array(); +		foreach ($this->roots as $root_key => $root_paths) +		{ +			foreach ($root_paths as $root_index => $root) +			{ +				$source_file = $this->files[$root_key][$root_index][$handle]; +				$tried .= ', ' . $source_file; +				if (file_exists($source_file)) +				{ +					$found = true; +					break; +				} +			} +			if ($found) +			{ +				if ($find_all) +				{ +					$found_all[] = $source_file; +					$found = false; +				} +				else +				{ +					break; +				} +			} +		} + +		// search failed +		if (!$found && !$find_all) +		{ +			trigger_error("style resource locator: File for handle $handle does not exist. Could not find: $tried", E_USER_ERROR); +		} + +		return ($find_all) ? $found_all : $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) +	{ +		// set default value +		$default_result = false; + +		// check all available paths +		foreach ($this->roots as $root_paths) +		{ +			foreach ($root_paths as $path) +			{ +				// check all files +				foreach ($files as $filename) +				{ +					$source_file = $path . '/' . $filename; +					if (file_exists($source_file)) +					{ +						return ($return_full_path) ? $source_file : $filename; +					} + +					// assign first file as result if $return_default is true +					if ($return_default && $default_result === false) +					{ +						$default_result = $source_file; +					} +				} +			} +		} + +		// search failed +		return $default_result; +	} +} diff --git a/phpBB/includes/style/style.php b/phpBB/includes/style/style.php new file mode 100644 index 0000000000..6b7cd31cb3 --- /dev/null +++ b/phpBB/includes/style/style.php @@ -0,0 +1,186 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Base Style class. +* @package phpBB3 +*/ +class phpbb_style +{ +	/** +	* Template class. +	* Handles everything related to templates. +	* @var phpbb_template +	*/ +	private $template; + +	/** +	* phpBB root path +	* @var string +	*/ +	private $phpbb_root_path; + +	/** +	* PHP file extension +	* @var string +	*/ +	private $php_ext; + +	/** +	* phpBB config instance +	* @var phpbb_config +	*/ +	private $config; + +	/** +	* Current user +	* @var phpbb_user +	*/ +	private $user; + +	/** +	* Style resource locator +	* @var phpbb_style_resource_locator +	*/ +	private $locator; + +	/** +	* Style path provider +	* @var phpbb_style_path_provider +	*/ +	private $provider; + +	/** +	* Constructor. +	* +	* @param string $phpbb_root_path phpBB root path +	* @param user $user current user +	* @param phpbb_style_resource_locator $locator style resource locator +	* @param phpbb_style_path_provider $provider style path provider +	* @param phpbb_template $template template +	*/ +	public function __construct($phpbb_root_path, $php_ext, $config, $user, phpbb_style_resource_locator $locator, phpbb_style_path_provider_interface $provider, phpbb_template $template) +	{ +		$this->phpbb_root_path = $phpbb_root_path; +		$this->php_ext = $php_ext; +		$this->config = $config; +		$this->user = $user; +		$this->locator = $locator; +		$this->provider = $provider; +		$this->template = $template; +	} + +	/** +	* Set style location based on (current) user's chosen style. +	*/ +	public function set_style() +	{ +		$style_path = $this->user->style['style_path']; +		$style_dirs = ($this->user->style['style_parent_id']) ? array_reverse(explode('/', $this->user->style['style_parent_tree'])) : array(); +		$paths = array($this->get_style_path($style_path)); +		foreach ($style_dirs as $dir) +		{ +			$paths[] = $this->get_style_path($dir); +		} + +		// Add 'all' path, used as last fallback path by hooks and extensions +		$paths[] = $this->get_style_path('all'); + +		return $this->set_custom_style($style_path, $paths); +	} + +	/** +	* Set custom style location (able to use directory outside of phpBB). +	* +	* Note: Templates are still compiled to phpBB's cache directory. +	* +	* @param string $name Name of style, used for cache prefix. Examples: "admin", "prosilver" +	* @param array or string $paths Array of style paths, relative to current root directory +	* @param string $template_path Path to templates, relative to style directory. False if path should not be changed. +	*/ +	public function set_custom_style($name, $paths, $template_path = false) +	{ +		if (is_string($paths)) +		{ +			$paths = array($paths); +		} + +		$this->provider->set_styles($paths); +		$this->locator->set_paths($this->provider); + +		$this->template->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $name) . '_'; + +		$this->template->context = new phpbb_template_context(); + +		if ($template_path !== false) +		{ +			$this->template->template_path = $this->locator->template_path = $template_path; +		} + +		return true; +	} + +	/** +	* Get location of style directory for specific style_path +	* +	* @param string $path Style path, such as "prosilver" +	* @return string Path to style directory, relative to current path +	*/ +	public function get_style_path($path) +	{ +		return $this->phpbb_root_path . 'styles/' . $path; +	} + +	/** +	* Defines a prefix to use for style 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); +	} + +	/** +	* Locates source file path, accounting for styles tree and verifying that +	* the path exists. +	* +	* @param string or array $files List of files to locate. If there is only +	*				one file, $files can be a string to make code easier to read. +	* @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 locate($files, $return_default = false, $return_full_path = true) +	{ +		// convert string to array +		if (is_string($files)) +		{ +			$files = array($files); +		} + +		// use resource locator to find files +		return $this->locator->get_first_file_location($files, $return_default, $return_full_path); +	} +}  | 
