| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php declare(strict_types=1);
- /**
- * PrivateBin
- *
- * a zero-knowledge paste bin
- *
- * @link https://github.com/PrivateBin/PrivateBin
- * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
- * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
- */
- namespace PrivateBin;
- /**
- * TemplateSwitcher
- *
- * Provides tool to change application template
- */
- class TemplateSwitcher
- {
- /**
- * template fallback
- *
- * @access protected
- * @static
- * @var string
- */
- protected static $_templateFallback = 'bootstrap';
- /**
- * available templates
- *
- * @access protected
- * @static
- * @var array
- */
- protected static $_availableTemplates = array();
- /**
- * set available templates
- *
- * @access public
- * @static
- * @param array $templates
- */
- public static function setAvailableTemplates(array $templates)
- {
- self::$_availableTemplates = $templates;
- }
- /**
- * set the default template
- *
- * @access public
- * @static
- * @param string $template
- */
- public static function setTemplateFallback(string $template)
- {
- if (self::isTemplateAvailable($template)) {
- self::$_templateFallback = $template;
- if (!in_array($template, self::getAvailableTemplates())) {
- // Add custom template to the available templates list
- self::$_availableTemplates[] = $template;
- }
- }
- }
- /**
- * get currently loaded template
- *
- * @access public
- * @static
- * @return string
- */
- public static function getTemplate(): string
- {
- $selectedTemplate = self::getSelectedByUserTemplate();
- return $selectedTemplate ?? self::$_templateFallback;
- }
- /**
- * get list of available templates
- *
- * @access public
- * @static
- * @return array
- */
- public static function getAvailableTemplates(): array
- {
- return self::$_availableTemplates;
- }
- /**
- * check if the provided template is available
- *
- * @access public
- * @static
- * @return bool
- */
- public static function isTemplateAvailable(string $template): bool
- {
- $available = in_array($template, self::getAvailableTemplates());
- if (!$available && !View::isBootstrapTemplate($template)) {
- $path = View::getTemplateFilePath($template);
- $available = file_exists($path);
- }
- return $available;
- }
- /**
- * get the template selected by user
- *
- * @access private
- * @static
- * @return string|null
- */
- private static function getSelectedByUserTemplate(): ?string
- {
- $selectedTemplate = null;
- $templateCookieValue = $_COOKIE['template'] ?? '';
- if (self::isTemplateAvailable($templateCookieValue)) {
- $selectedTemplate = $templateCookieValue;
- }
- return $selectedTemplate;
- }
- }
|