Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
TemplateSwitcher | |
94.44% |
17 / 18 |
|
83.33% |
5 / 6 |
11.02 | |
0.00% |
0 / 1 |
setAvailableTemplates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setTemplateFallback | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
getTemplate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getAvailableTemplates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isTemplateAvailable | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getSelectedByUserTemplate | |
100.00% |
5 / 5 |
|
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 = 'bootstrap'; |
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 | |
63 | if (!in_array($template, self::getAvailableTemplates())) { |
64 | // Add custom template to the available templates list |
65 | self::$_availableTemplates[] = $template; |
66 | } |
67 | } |
68 | } |
69 | |
70 | /** |
71 | * get currently loaded template |
72 | * |
73 | * @access public |
74 | * @static |
75 | * @return string |
76 | */ |
77 | public static function getTemplate(): string |
78 | { |
79 | $selectedTemplate = self::getSelectedByUserTemplate(); |
80 | return $selectedTemplate ?? self::$_templateFallback; |
81 | } |
82 | |
83 | /** |
84 | * get list of available templates |
85 | * |
86 | * @access public |
87 | * @static |
88 | * @return array |
89 | */ |
90 | public static function getAvailableTemplates(): array |
91 | { |
92 | return self::$_availableTemplates; |
93 | } |
94 | |
95 | /** |
96 | * check if the provided template is available |
97 | * |
98 | * @access public |
99 | * @static |
100 | * @return bool |
101 | */ |
102 | public static function isTemplateAvailable(string $template): bool |
103 | { |
104 | $available = in_array($template, self::getAvailableTemplates()); |
105 | |
106 | if (!$available && !View::isBootstrapTemplate($template)) { |
107 | $path = View::getTemplateFilePath($template); |
108 | $available = file_exists($path); |
109 | } |
110 | |
111 | return $available; |
112 | } |
113 | |
114 | /** |
115 | * get the template selected by user |
116 | * |
117 | * @access private |
118 | * @static |
119 | * @return string|null |
120 | */ |
121 | private static function getSelectedByUserTemplate(): ?string |
122 | { |
123 | $selectedTemplate = null; |
124 | $templateCookieValue = $_COOKIE['template'] ?? ''; |
125 | |
126 | if (self::isTemplateAvailable($templateCookieValue)) { |
127 | $selectedTemplate = $templateCookieValue; |
128 | } |
129 | |
130 | return $selectedTemplate; |
131 | } |
132 | } |