|
2 | 2 | require(__DIR__.'/vendor/autoload.php');
|
3 | 3 |
|
4 | 4 | $error = null;
|
5 |
| -set_error_handler(function (int $type, string $msg) use (&$error) { |
6 |
| - if (in_array($type, [E_USER_WARNING, E_USER_NOTICE])) { |
| 5 | +\set_error_handler(function (int $type, string $msg) use (&$error) { |
| 6 | + if (\in_array($type, [E_USER_WARNING, E_USER_NOTICE])) { |
7 | 7 | $error = $msg;
|
8 | 8 | } else {
|
9 | 9 | return false;
|
|
15 | 15 | $output = '';
|
16 | 16 | $minify = Array();
|
17 | 17 | $timing = Array(
|
18 |
| - 'start' => microtime(true) |
| 18 | + 'start' => \microtime(true) |
19 | 19 | );
|
20 | 20 | $mem = Array(
|
21 |
| - 'start' => memory_get_peak_usage() |
| 21 | + 'start' => \memory_get_peak_usage() |
22 | 22 | );
|
23 | 23 |
|
24 | 24 | // create object and retrieve config
|
|
33 | 33 |
|
34 | 34 | // parse the URL
|
35 | 35 | if (($url = parse_url($_POST['url'])) === false) {
|
36 |
| - trigger_error('Could not parse URL: The URL is not valid', E_USER_WARNING); |
| 36 | + \trigger_error('Could not parse URL: The URL is not valid', E_USER_WARNING); |
37 | 37 |
|
38 | 38 | // check the host name
|
39 | 39 | } elseif (!isset($url['host'])) {
|
40 |
| - trigger_error('Could not parse URL: No host was supplied', E_USER_WARNING); |
| 40 | + \trigger_error('Could not parse URL: No host was supplied', E_USER_WARNING); |
41 | 41 |
|
42 | 42 | // open the document manually so we can time it
|
43 |
| - } elseif (($input = file_get_contents($_POST['url'])) === false) { |
44 |
| - trigger_error('Could not load HTML: The file could not be accessed'.$error, E_USER_WARNING); |
| 43 | + } elseif (($input = \file_get_contents($_POST['url'])) === false) { |
| 44 | + \trigger_error('Could not load HTML: The file could not be accessed'.$error, E_USER_WARNING); |
45 | 45 |
|
46 | 46 | // save base URL
|
47 | 47 | } else {
|
|
50 | 50 |
|
51 | 51 | // handle directly entered source code
|
52 | 52 | } elseif (empty($_POST['source'])) {
|
53 |
| - trigger_error('No URL or HTML source was posted', E_USER_WARNING); |
| 53 | + \trigger_error('No URL or HTML source was posted', E_USER_WARNING); |
54 | 54 |
|
55 | 55 | // record the HTML
|
56 | 56 | } else {
|
57 | 57 | $input = $_POST['source'];
|
58 | 58 | }
|
59 |
| - $timing['fetch'] = microtime(true); |
60 |
| - $mem['fetch'] = memory_get_peak_usage(); |
| 59 | + $timing['fetch'] = \microtime(true); |
| 60 | + $mem['fetch'] = \memory_get_peak_usage(); |
61 | 61 |
|
62 | 62 | // load the source code
|
63 | 63 | if ($input) {
|
64 | 64 | if (!$doc->load($input, null, $error)) {
|
65 |
| - trigger_error('Could not parse HTML: '.$error, E_USER_WARNING); |
| 65 | + \trigger_error('Could not parse HTML: '.$error, E_USER_WARNING); |
66 | 66 |
|
67 | 67 | // minify the output
|
68 | 68 | } else {
|
69 |
| - $timing['parse'] = microtime(true); |
70 |
| - $mem['parse'] = memory_get_peak_usage(); |
| 69 | + $timing['parse'] = \microtime(true); |
| 70 | + $mem['parse'] = \memory_get_peak_usage(); |
71 | 71 |
|
72 | 72 | // retrieve the user posted options
|
73 |
| - $isset = isset($_POST['minify']) && is_array($_POST['minify']); |
| 73 | + $isset = isset($_POST['minify']) && \is_array($_POST['minify']); |
74 | 74 | foreach ($options AS $key => $item) {
|
75 | 75 | if ($key != 'elements') {
|
76 |
| - $minify[$key] = $isset && in_array($key, $_POST['minify']) ? (is_array($item) ? [] : (is_bool($options[$key]) ? true : $options[$key])) : false; |
77 |
| - if (is_array($item)) { |
| 76 | + $minify[$key] = $isset && \in_array($key, $_POST['minify']) ? (\is_array($item) ? [] : (\is_bool($options[$key]) ? true : $options[$key])) : false; |
| 77 | + if (\is_array($item)) { |
78 | 78 | foreach ($item AS $sub => $value) {
|
79 |
| - if ($minify[$key] !== false && isset($_POST['minify'][$key]) && is_array($_POST['minify'][$key]) && in_array($sub, $_POST['minify'][$key])) { |
| 79 | + if ($minify[$key] !== false && isset($_POST['minify'][$key]) && \is_array($_POST['minify'][$key]) && \in_array($sub, $_POST['minify'][$key])) { |
80 | 80 | $minify[$key][$sub] = true;
|
81 | 81 | } elseif ($minify[$key]) {
|
82 | 82 | $minify[$key][$sub] = false;
|
|
94 | 94 | }
|
95 | 95 |
|
96 | 96 | // record timings
|
97 |
| - $timing['minify'] = microtime(true); |
98 |
| - $mem['minify'] = memory_get_peak_usage(); |
| 97 | + $timing['minify'] = \microtime(true); |
| 98 | + $mem['minify'] = \memory_get_peak_usage(); |
99 | 99 | $output = $doc->save();
|
100 |
| - $timing['output'] = microtime(true); |
101 |
| - $mem['output'] = memory_get_peak_usage(); |
| 100 | + $timing['output'] = \microtime(true); |
| 101 | + $mem['output'] = \memory_get_peak_usage(); |
102 | 102 | }
|
103 | 103 | }
|
104 | 104 | } else {
|
|
187 | 187 | </style>
|
188 | 188 | </head>
|
189 | 189 | <body>
|
190 |
| - <form action="<?= htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" accept-charset="<?= mb_internal_encoding(); ?>" class="minify__form"> |
| 190 | + <form action="<?= \htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" accept-charset="<?= \htmlspecialchars(\mb_internal_encoding()); ?>" class="minify__form"> |
191 | 191 | <div class="minify__form-wrap">
|
192 | 192 | <h1 class="minify__form-heading">HTML Minifier</h1>
|
193 | 193 | <?php if ($error) { ?>
|
194 |
| - <div class="minify__form-error"><?= htmlspecialchars($error); ?></div> |
| 194 | + <div class="minify__form-error"><?= \htmlspecialchars($error); ?></div> |
195 | 195 | <?php } ?>
|
196 | 196 | <div class="minify__form-input">
|
197 | 197 | <label for="source">Paste HTML:</label>
|
198 |
| - <textarea name="source" id="source" class="minify__form-input-box"><?= htmlspecialchars($input); ?></textarea> |
| 198 | + <textarea name="source" id="source" class="minify__form-input-box"><?= \htmlspecialchars($input); ?></textarea> |
199 | 199 | </div>
|
200 | 200 | <div class="minify__form-url">
|
201 | 201 | <label for="url">or External URL:</label>
|
|
205 | 205 | <?php if ($output) { ?>
|
206 | 206 | <div class="minify__form-input">
|
207 | 207 | <label for="output">Output HTML:</label>
|
208 |
| - <textarea id="output" class="minify__form-input-box"><?= htmlspecialchars($output); ?></textarea> |
| 208 | + <textarea id="output" class="minify__form-input-box"><?= \htmlspecialchars($output); ?></textarea> |
209 | 209 | </div>
|
210 | 210 | <table class="minify__table">
|
211 | 211 | <thead>
|
|
225 | 225 | </thead>
|
226 | 226 | <tbody>
|
227 | 227 | <?php
|
228 |
| - $ilen = strlen($input); |
229 |
| - $olen = strlen($output); |
230 |
| - $gilen = strlen(gzencode($input)); |
231 |
| - $golen = strlen(gzencode($output)); |
| 228 | + $ilen = \strlen($input); |
| 229 | + $olen = \strlen($output); |
| 230 | + $gilen = \strlen(\gzencode($input)); |
| 231 | + $golen = \strlen(\gzencode($output)); |
232 | 232 | ?>
|
233 | 233 | <tr>
|
234 | 234 | <td>Uncompressed</td>
|
235 |
| - <td><?= htmlspecialchars(number_format($ilen)); ?></td> |
236 |
| - <td><?= htmlspecialchars(number_format($olen)); ?></td> |
237 |
| - <td><?= htmlspecialchars(number_format($ilen - $olen)); ?></td> |
238 |
| - <td><?= htmlspecialchars(number_format((100 / $ilen) * $olen)); ?>%</td> |
| 235 | + <td><?= \htmlspecialchars(\number_format($ilen)); ?></td> |
| 236 | + <td><?= \htmlspecialchars(\number_format($olen)); ?></td> |
| 237 | + <td><?= \htmlspecialchars(\number_format($ilen - $olen)); ?></td> |
| 238 | + <td><?= \htmlspecialchars(\number_format((100 / $ilen) * $olen)); ?>%</td> |
239 | 239 | <td style="font-weight:bold;">Time (sec)</td>
|
240 |
| - <td><?= htmlspecialchars(number_format($timing['fetch'] - $timing['start'], 4)); ?>s</td> |
241 |
| - <td><?= htmlspecialchars(number_format($timing['parse'] - $timing['fetch'], 4)); ?>s</td> |
242 |
| - <td><?= htmlspecialchars(number_format($timing['minify'] - $timing['parse'], 4)); ?>s</td> |
243 |
| - <td><?= htmlspecialchars(number_format($timing['output'] - $timing['minify'], 4)); ?>s</td> |
244 |
| - <td><?= htmlspecialchars(number_format($timing['output'] - $timing['fetch'], 4)); ?>s</td> |
| 240 | + <td><?= \htmlspecialchars(\number_format($timing['fetch'] - $timing['start'], 4)); ?>s</td> |
| 241 | + <td><?= \htmlspecialchars(\number_format($timing['parse'] - $timing['fetch'], 4)); ?>s</td> |
| 242 | + <td><?= \htmlspecialchars(\number_format($timing['minify'] - $timing['parse'], 4)); ?>s</td> |
| 243 | + <td><?= \htmlspecialchars(\number_format($timing['output'] - $timing['minify'], 4)); ?>s</td> |
| 244 | + <td><?= \htmlspecialchars(\number_format($timing['output'] - $timing['fetch'], 4)); ?>s</td> |
245 | 245 | </tr>
|
246 | 246 | <tr>
|
247 | 247 | <td>Compressed</td>
|
248 |
| - <td><?= htmlspecialchars(number_format($gilen)); ?></td> |
249 |
| - <td><?= htmlspecialchars(number_format($golen)); ?></td> |
250 |
| - <td><?= htmlspecialchars(number_format($gilen - $golen)); ?></td> |
251 |
| - <td><?= htmlspecialchars(number_format((100 / $gilen) * $golen)); ?>%</td> |
| 248 | + <td><?= \htmlspecialchars(\number_format($gilen)); ?></td> |
| 249 | + <td><?= \htmlspecialchars(\number_format($golen)); ?></td> |
| 250 | + <td><?= \htmlspecialchars(\number_format($gilen - $golen)); ?></td> |
| 251 | + <td><?= \htmlspecialchars(\number_format((100 / $gilen) * $golen)); ?>%</td> |
252 | 252 | <td style="font-weight:bold;">Peak (kb)</td>
|
253 |
| - <td><?= htmlspecialchars(number_format($mem['fetch'] / 1024, 0)); ?>kb</td> |
254 |
| - <td><?= htmlspecialchars(number_format($mem['parse'] / 1024, 0)); ?>kb</td> |
255 |
| - <td><?= htmlspecialchars(number_format($mem['minify'] / 1024, 0)); ?>kb</td> |
256 |
| - <td><?= htmlspecialchars(number_format($mem['output'] / 1024, 0)); ?>kb</td> |
257 |
| - <td><?= htmlspecialchars(number_format(memory_get_peak_usage() / 1024, 0)); ?>kb</td> |
| 253 | + <td><?= \htmlspecialchars(\number_format($mem['fetch'] / 1024, 0)); ?>kb</td> |
| 254 | + <td><?= \htmlspecialchars(\number_format($mem['parse'] / 1024, 0)); ?>kb</td> |
| 255 | + <td><?= \htmlspecialchars(\number_format($mem['minify'] / 1024, 0)); ?>kb</td> |
| 256 | + <td><?= \htmlspecialchars(\number_format($mem['output'] / 1024, 0)); ?>kb</td> |
| 257 | + <td><?= \htmlspecialchars(\number_format(\memory_get_peak_usage() / 1024, 0)); ?>kb</td> |
258 | 258 | </tr>
|
259 | 259 | </tbody>
|
260 | 260 | </table>
|
261 | 261 | <?php } ?>
|
262 | 262 | </div>
|
263 | 263 | <?php if ($output) { ?>
|
264 |
| - <input type="hidden" name="base" value="<?= htmlspecialchars($base); ?>" /> |
265 |
| - <iframe class="minify__preview" srcdoc="<?= htmlspecialchars(preg_replace('/<head[^>]*>/i', '$0<base href="'.htmlspecialchars($base).'">', $output)); ?>"></iframe> |
| 264 | + <input type="hidden" name="base" value="<?= \htmlspecialchars($base); ?>" /> |
| 265 | + <iframe class="minify__preview" srcdoc="<?= \htmlspecialchars(\preg_replace('/<head[^>]*>/i', '$0<base href="'.\htmlspecialchars($base).'">', $output)); ?>"></iframe> |
266 | 266 | <?php } ?>
|
267 | 267 | <div class="minify__options">
|
268 | 268 | <h3>Options</h3>
|
269 | 269 | <ul class="minify__options-list">
|
270 |
| - <?php foreach ($options AS $key => $item) { ?> |
271 |
| - <li> |
272 |
| - <label> |
273 |
| - <input type="checkbox" name="minify[]" value="<?= $key; ?>"<?= !isset($minify[$key]) || $minify[$key] === false ? '' : ' checked="checked"'; ?> /><?= htmlspecialchars(ucfirst($key)); ?> |
274 |
| - </label> |
275 |
| - <?php if (is_array($item)) { ?> |
276 |
| - <ul class="minify__options-list"> |
277 |
| - <?php foreach ($item AS $sub => $value) { ?> |
278 |
| - <li> |
279 |
| - <label> |
280 |
| - <input type="checkbox" name="minify[<?= $key; ?>][]" value="<?= $sub; ?>"<?= !isset($minify[$key][$sub]) || $minify[$key][$sub] === false ? '' : ' checked="checked"'; ?> /><?= htmlspecialchars(ucfirst($sub)); ?> |
281 |
| - </label> |
282 |
| - </li> |
283 |
| - <?php } ?> |
284 |
| - </ul> |
285 |
| - <?php } ?> |
286 |
| - </li> |
287 |
| - <?php } ?> |
| 270 | + <?php foreach ($options AS $key => $item) { |
| 271 | + if (\is_bool($item) || \is_array($item)) { ?> |
| 272 | + <li> |
| 273 | + <label> |
| 274 | + <input type="checkbox" name="minify[]" value="<?= $key; ?>"<?= !isset($minify[$key]) || $minify[$key] === false ? '' : ' checked="checked"'; ?> /><?= \htmlspecialchars(ucfirst($key)); ?> |
| 275 | + </label> |
| 276 | + <?php if (\is_array($item)) { ?> |
| 277 | + <ul class="minify__options-list"> |
| 278 | + <?php foreach ($item AS $sub => $value) { ?> |
| 279 | + <li> |
| 280 | + <label> |
| 281 | + <input type="checkbox" name="minify[<?= $key; ?>][]" value="<?= $sub; ?>"<?= !isset($minify[$key][$sub]) || $minify[$key][$sub] === false ? '' : ' checked="checked"'; ?> /><?= \htmlspecialchars(\ucfirst($sub)); ?> |
| 282 | + </label> |
| 283 | + </li> |
| 284 | + <?php } ?> |
| 285 | + </ul> |
| 286 | + <?php } ?> |
| 287 | + </li> |
| 288 | + <?php } |
| 289 | + } ?> |
288 | 290 | </ul>
|
289 | 291 | </div>
|
290 | 292 | </form>
|
|
0 commit comments