Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
View
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
6 / 6
11
100.00% covered (success)
100.00%
1 / 1
 assign
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 draw
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 _getCacheBuster
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 _getSri
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 _linkTag
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 _scriptTag
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
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
12namespace PrivateBin;
13
14use Exception;
15
16/**
17 * View
18 *
19 * Displays the templates
20 */
21class 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        $dir  = PATH . 'tpl' . DIRECTORY_SEPARATOR;
53        $file = substr($template, 0, 10) === 'bootstrap-' ? 'bootstrap' : $template;
54        $path = $dir . $file . '.php';
55        if (!is_file($path)) {
56            throw new Exception('Template ' . $template . ' not found in file ' . $path . '!', 80);
57        }
58        if (!in_array($path, glob($dir . '*.php', GLOB_NOSORT | GLOB_ERR), true)) {
59            throw new Exception('Template ' . $file . '.php not found in ' . $dir . '!', 81);
60        }
61        extract($this->_variables);
62        include $path;
63    }
64
65    /**
66     * get cache buster query string
67     *
68     * if the file isn't versioned (ends in a digit), adds our own version as a query string
69     *
70     * @access private
71     * @param  string $file
72     */
73    private function _getCacheBuster($file)
74    {
75        if ((bool) preg_match('#[0-9]\.m?js$#', (string) $file)) {
76            return '';
77        }
78        return '?' . rawurlencode($this->_variables['VERSION']);
79    }
80
81    /**
82     * get SRI hash for given file
83     *
84     * @access private
85     * @param  string $file
86     */
87    private function _getSri($file)
88    {
89        if (array_key_exists($file, $this->_variables['SRI'])) {
90            return ' integrity="' . $this->_variables['SRI'][$file] . '"';
91        }
92        return '';
93    }
94
95    /**
96     * echo module preload link tag incl. SRI hash for given script file
97     *
98     * @access private
99     * @param  string $file
100     */
101    private function _linkTag($file)
102    {
103        echo '<link rel="modulepreload" href="', $file,
104        $this->_getCacheBuster($file), '"', $this->_getSri($file), ' />', PHP_EOL;
105    }
106
107    /**
108     * echo script tag incl. SRI hash for given script file
109     *
110     * @access private
111     * @param  string $file
112     * @param  string $attributes additional attributes to add into the script tag
113     */
114    private function _scriptTag($file, $attributes = '')
115    {
116        echo '<script ', $attributes,
117        ' type="text/javascript" data-cfasync="false" src="', $file,
118        $this->_getCacheBuster($file), '"', $this->_getSri($file),
119        ' crossorigin="anonymous"></script>', PHP_EOL;
120    }
121}