Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
View | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
assign | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
draw | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
_scriptTag | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 |
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 | public function assign($name, $value) |
39 | { |
40 | $this->_variables[$name] = $value; |
41 | } |
42 | |
43 | /** |
44 | * render a template |
45 | * |
46 | * @access public |
47 | * @param string $template |
48 | * @throws Exception |
49 | */ |
50 | public function draw($template) |
51 | { |
52 | $file = substr($template, 0, 10) === 'bootstrap-' ? 'bootstrap' : $template; |
53 | $path = PATH . 'tpl' . DIRECTORY_SEPARATOR . $file . '.php'; |
54 | if (!file_exists($path)) { |
55 | throw new Exception('Template ' . $template . ' not found!', 80); |
56 | } |
57 | extract($this->_variables); |
58 | include $path; |
59 | } |
60 | |
61 | /** |
62 | * echo script tag incl. SRI hash for given script file |
63 | * |
64 | * @access private |
65 | * @param string $file |
66 | * @param string $attributes additional attributes to add into the script tag |
67 | */ |
68 | private function _scriptTag($file, $attributes = '') |
69 | { |
70 | $sri = array_key_exists($file, $this->_variables['SRI']) ? |
71 | ' integrity="' . $this->_variables['SRI'][$file] . '"' : ''; |
72 | // if the file isn't versioned (ends in a digit), add our own version |
73 | $cacheBuster = ctype_digit(substr($file, -4, 1)) ? |
74 | '' : '?' . rawurlencode($this->_variables['VERSION']); |
75 | echo '<script ', $attributes, |
76 | ' type="text/javascript" data-cfasync="false" src="', $file, |
77 | $cacheBuster, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL; |
78 | } |
79 | } |