Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
View
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
8
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
 _scriptTag
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
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
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     * echo script tag incl. SRI hash for given script file
67     *
68     * @access private
69     * @param  string $file
70     * @param  string $attributes additional attributes to add into the script tag
71     */
72    private function _scriptTag($file, $attributes = '')
73    {
74        $sri = array_key_exists($file, $this->_variables['SRI']) ?
75            ' integrity="' . $this->_variables['SRI'][$file] . '"' : '';
76        // if the file isn't versioned (ends in a digit), add our own version
77        $cacheBuster = (bool) preg_match('#[0-9]\.js$#', (string) $file) ?
78            '' : '?' . rawurlencode($this->_variables['VERSION']);
79        echo '<script ', $attributes,
80        ' type="text/javascript" data-cfasync="false" src="', $file,
81        $cacheBuster, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL;
82    }
83}