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 | use Exception; |
||
15 | |||
16 | /** |
||
17 | * View |
||
18 | * |
||
19 | * Displays the templates |
||
20 | */ |
||
21 | class View |
||
22 | { |
||
23 | /** |
||
24 | * variables available in the template |
||
25 | * |
||
26 | * @access private |
||
27 | * @var array |
||
28 | */ |
||
29 | private $_variables = array(); |
||
30 | |||
31 | /** |
||
32 | * assign variables to be used inside of the template |
||
33 | * |
||
34 | * @access public |
||
35 | * @param string $name |
||
36 | * @param mixed $value |
||
37 | */ |
||
38 | 41 | public function assign($name, $value) |
|
39 | { |
||
40 | 41 | $this->_variables[$name] = $value; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * render a template |
||
45 | * |
||
46 | * @access public |
||
47 | * @param string $template |
||
48 | * @throws Exception |
||
49 | */ |
||
50 | 41 | public function draw($template) |
|
51 | { |
||
52 | 41 | $path = self::getTemplateFilePath($template); |
|
53 | 41 | if (!file_exists($path)) { |
|
54 | 1 | throw new Exception('Template ' . $template . ' not found!', 80); |
|
55 | } |
||
56 | 41 | extract($this->_variables); |
|
57 | 41 | include $path; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get template file path |
||
62 | * |
||
63 | * @access public |
||
64 | * @param string $template |
||
65 | * @return string |
||
66 | */ |
||
67 | 42 | public static function getTemplateFilePath(string $template): string |
|
68 | { |
||
69 | 42 | $file = self::isBootstrapTemplate($template) ? 'bootstrap' : $template; |
|
70 | 42 | return PATH . 'tpl' . DIRECTORY_SEPARATOR . $file . '.php'; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Is the template a variation of the bootstrap template |
||
75 | * |
||
76 | * @access public |
||
77 | * @param string $template |
||
78 | * @return bool |
||
79 | */ |
||
80 | 44 | public static function isBootstrapTemplate(string $template): bool |
|
81 | { |
||
82 | 44 | return substr($template, 0, 10) === 'bootstrap-'; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * echo script tag incl. SRI hash for given script file |
||
87 | * |
||
88 | * @access private |
||
89 | * @param string $file |
||
90 | * @param string $attributes additional attributes to add into the script tag |
||
91 | */ |
||
92 | 37 | private function _scriptTag($file, $attributes = '') |
|
0 ignored issues
–
show
|
|||
93 | { |
||
94 | 37 | $sri = array_key_exists($file, $this->_variables['SRI']) ? |
|
95 | 37 | ' integrity="' . $this->_variables['SRI'][$file] . '"' : ''; |
|
96 | // if the file isn't versioned (ends in a digit), add our own version |
||
97 | 37 | $cacheBuster = (bool) preg_match('#[0-9]\.js$#', (string) $file) ? |
|
98 | 37 | '' : '?' . rawurlencode($this->_variables['VERSION']); |
|
99 | 37 | echo '<script ', $attributes, |
|
100 | 37 | ' type="text/javascript" data-cfasync="false" src="', $file, |
|
101 | 37 | $cacheBuster, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL; |
|
102 | } |
||
103 | } |
||
104 |
This check looks for private methods that have been defined, but are not used inside the class.