Passed
Push — 2.x ( daf3a4...83ed90 )
by Terry
02:01
created

Asset::loadJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Panel;
24
25
use Psr\Http\Message\ResponseInterface;
26
use Shieldon\Firewall\HttpFactory;
27
use function Shieldon\Firewall\get_response;
28
29
/**
30
 * The static asset files such as CSS, JavaScript.
31
 */
32
class Asset extends BaseController
33
{
34
    /**
35
     * The directory in where the static assets of the firewall panel are placed.
36
     */
37
    const PANEL_ASSET_DIR = __DIR__ . '/../../../assets';
38
39
    /**
40
     * Constructor.
41
     */
42
    public function __construct() 
43
    {
44
        parent::__construct();
45
    }
46
47
    /**
48
     * The content output to be a CSS file.
49
     *
50
     * @return ResponseInterface
51
     */
52
    public function css(): ResponseInterface
53
    {
54
        $response = get_response();
55
        $response = $response->withHeader('Content-Type', 'text/css; charset=UTF-8');
56
        $stream = HttpFactory::createStream();
57
        $stream->write($this->loadCss());
58
        $stream->rewind();
59
        $response = $response->withBody($stream);
60
61
        return $this->withCacheHeader($response);
62
    }
63
64
    /**
65
     * The content output to be a JavaScript file.
66
     *
67
     * @return ResponseInterface
68
     */
69
    public function js(): ResponseInterface
70
    {
71
        $response = get_response();
72
        $response = $response->withHeader('Content-Type', 'text/javascript; charset=UTF-8');
73
        $stream = HttpFactory::createStream();
74
        $stream->write($this->loadJs());
75
        $stream->rewind();
76
        $response = $response->withBody($stream);
77
78
        return $this->withCacheHeader($response);
79
    }
80
81
    /**
82
     * The content output to be a favicon.
83
     *
84
     * @return ResponseInterface
85
     */
86
    public function favicon(): ResponseInterface
87
    {
88
        $response = get_response();
89
        $response = $response->withHeader('Content-Type', 'image/x-icon');
90
        $stream = HttpFactory::createStream();
91
        $stream->write($this->loadFavicon());
92
        $stream->rewind();
93
        $response = $response->withBody($stream);
94
95
        return $this->withCacheHeader($response);
96
    }
97
98
    /**
99
     * The content output to be a logo image.
100
     *
101
     * @return ResponseInterface
102
     */
103
    public function logo(): ResponseInterface
104
    {
105
        $response = get_response();
106
        $response = $response->withHeader('Content-Type', 'image/png');
107
        $stream = HttpFactory::createStream();
108
        $stream->write($this->loadLogo());
109
        $stream->rewind();
110
        $response = $response->withBody($stream);
111
112
        return $this->withCacheHeader($response);
113
    }
114
115
    /**
116
     * Return the header with cache parameters.
117
     *
118
     * @param ResponseInterface $response The PSR-7 server response.
119
     *
120
     * @return ResponseInterface
121
     */
122
    private function withCacheHeader(ResponseInterface $response): ResponseInterface
123
    {
124
        $seconds = 86400; // 24 hours
125
        $response = $response->withHeader('Expires', gmdate('D, d M Y H:i:s', time() + $seconds) . ' GMT');
126
        $response = $response->withHeader('Pragma', 'cache');
127
        $response = $response->withHeader('Cache-Control', 'max-age=' . $seconds);
128
129
        return $response;
130
    }
131
132
    /**
133
     * Load CSS content.
134
     *
135
     * @return string
136
     */
137
    private function loadJs(): string
138
    {
139
        ob_start();
140
        echo file_get_contents(self::PANEL_ASSET_DIR . '/dist/app-packed.js');
141
        $output = ob_get_contents();
142
        ob_end_clean();
143
    
144
        return $this->filterString($output);
0 ignored issues
show
Bug introduced by
$output of type string is incompatible with the type Shieldon\Firewall\Panel\sring expected by parameter $string of Shieldon\Firewall\Panel\Asset::filterString(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
        return $this->filterString(/** @scrutinizer ignore-type */ $output);
Loading history...
145
    }
146
147
    /**
148
     * Load CSS content.
149
     *
150
     * @return string
151
     */
152
    private function loadCss(): string
153
    {
154
        ob_start();
155
        echo file_get_contents(self::PANEL_ASSET_DIR . '/dist/app-packed.css');
156
        $output = ob_get_contents();
157
        ob_end_clean();
158
    
159
        return $this->filterString($output);
0 ignored issues
show
Bug introduced by
$output of type string is incompatible with the type Shieldon\Firewall\Panel\sring expected by parameter $string of Shieldon\Firewall\Panel\Asset::filterString(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        return $this->filterString(/** @scrutinizer ignore-type */ $output);
Loading history...
160
    }
161
162
    /**
163
     * Load Shieldon's favicon.
164
     *
165
     * @return string
166
     */
167
    private function loadFavicon(): string
168
    {
169
        ob_start();
170
        echo file_get_contents(self::PANEL_ASSET_DIR . '/src/images/favicon.ico');
171
        $output = ob_get_contents();
172
        ob_end_clean();
173
174
        return $output;
175
    }
176
177
    /**
178
     * Load Shieldon's logo.
179
     *
180
     * @return string
181
     */
182
    private function loadLogo(): string
183
    {
184
        ob_start();
185
        echo file_get_contents(self::PANEL_ASSET_DIR . '/src/images/logo.png');
186
        $output = ob_get_contents();
187
        ob_end_clean();
188
189
        return $output;
190
    }
191
192
    /**
193
     * Remove the PHP syntax, prevent the possible security issues.
194
     *
195
     * @param sring $string
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\Panel\sring was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
196
     *
197
     * @return string
198
     */
199
    private function filterString($string): string
200
    {
201
        return str_replace(['<?php', '<?', '?>'], '', $string);
202
    }
203
}
204