Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| TemplateSwitcher | |
100.00% |
14 / 14 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| setAvailableTemplates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTemplateFallback | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getTemplate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getAvailableTemplates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isTemplateAvailable | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /** |
| 3 | * PrivateBin |
| 4 | * |
| 5 | * a zero-knowledge paste bin |
| 6 | * |
| 7 | * @link https://github.com/PrivateBin/PrivateBin |
| 8 | * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net) |
| 9 | * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License |
| 10 | */ |
| 11 | |
| 12 | namespace PrivateBin; |
| 13 | |
| 14 | /** |
| 15 | * TemplateSwitcher |
| 16 | * |
| 17 | * Provides tool to change application template |
| 18 | */ |
| 19 | class TemplateSwitcher |
| 20 | { |
| 21 | /** |
| 22 | * template fallback |
| 23 | * |
| 24 | * @access protected |
| 25 | * @static |
| 26 | * @var string |
| 27 | */ |
| 28 | protected static $_templateFallback = 'bootstrap5'; |
| 29 | |
| 30 | /** |
| 31 | * available templates |
| 32 | * |
| 33 | * @access protected |
| 34 | * @static |
| 35 | * @var array |
| 36 | */ |
| 37 | protected static $_availableTemplates = array(); |
| 38 | |
| 39 | /** |
| 40 | * set available templates |
| 41 | * |
| 42 | * @access public |
| 43 | * @static |
| 44 | * @param array $templates |
| 45 | */ |
| 46 | public static function setAvailableTemplates(array $templates) |
| 47 | { |
| 48 | self::$_availableTemplates = $templates; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * set the default template |
| 53 | * |
| 54 | * @access public |
| 55 | * @static |
| 56 | * @param string $template |
| 57 | */ |
| 58 | public static function setTemplateFallback(string $template) |
| 59 | { |
| 60 | if (self::isTemplateAvailable($template)) { |
| 61 | self::$_templateFallback = $template; |
| 62 | } else { |
| 63 | error_log('failed to set "' . $template . '" as a fallback, it needs to be added to the list of `availabletemplates` in the configuration file'); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * get user selected template or fallback |
| 69 | * |
| 70 | * @access public |
| 71 | * @static |
| 72 | * @return string |
| 73 | */ |
| 74 | public static function getTemplate(): string |
| 75 | { |
| 76 | if (array_key_exists('template', $_COOKIE)) { |
| 77 | $template = basename($_COOKIE['template']); |
| 78 | if (self::isTemplateAvailable($template)) { |
| 79 | return $template; |
| 80 | } |
| 81 | } |
| 82 | return self::$_templateFallback; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * get list of available templates |
| 87 | * |
| 88 | * @access public |
| 89 | * @static |
| 90 | * @return array |
| 91 | */ |
| 92 | public static function getAvailableTemplates(): array |
| 93 | { |
| 94 | return self::$_availableTemplates; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * check if the provided template is available |
| 99 | * |
| 100 | * @access public |
| 101 | * @static |
| 102 | * @return bool |
| 103 | */ |
| 104 | public static function isTemplateAvailable(string $template): bool |
| 105 | { |
| 106 | if (in_array($template, self::getAvailableTemplates(), true)) { |
| 107 | return true; |
| 108 | } |
| 109 | error_log('template "' . $template . '" is not in the list of `availabletemplates` in the configuration file'); |
| 110 | return false; |
| 111 | } |
| 112 | } |