Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
View
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
9
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%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getTemplateFilePath
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isBootstrapTemplate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 _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        $path = self::getTemplateFilePath($template);
53        if (!file_exists($path)) {
54            throw new Exception('Template ' . $template . ' not found!', 80);
55        }
56        extract($this->_variables);
57        include $path;
58    }
59
60    /**
61     * Get template file path
62     *
63     * @access public
64     * @param  string $template
65     * @return string
66     */
67    public static function getTemplateFilePath(string $template): string
68    {
69        $file = self::isBootstrapTemplate($template) ? 'bootstrap' : $template;
70        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    public static function isBootstrapTemplate(string $template): bool
81    {
82        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    private function _scriptTag($file, $attributes = '')
93    {
94        $sri = array_key_exists($file, $this->_variables['SRI']) ?
95            ' integrity="' . $this->_variables['SRI'][$file] . '"' : '';
96        // if the file isn't versioned (ends in a digit), add our own version
97        $cacheBuster = (bool) preg_match('#[0-9]\.js$#', (string) $file) ?
98            '' : '?' . rawurlencode($this->_variables['VERSION']);
99        echo '<script ', $attributes,
100        ' type="text/javascript" data-cfasync="false" src="', $file,
101        $cacheBuster, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL;
102    }
103}