index.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | require __DIR__ . '/../vendor/autoload.php'; |
| 4 | |
| 5 | function h($txt) { |
| 6 | return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8'); |
| 7 | } |
| 8 | |
| 9 | $tpl = array(); |
| 10 | |
| 11 | if (isset($_POST['textIn'])) { |
| 12 | $textIn = str_replace("\r\n", "\n", $_POST['textIn']); |
| 13 | |
| 14 | $tpl['inBytes'] = strlen($textIn); |
| 15 | $startTime = microtime(true); |
| 16 | try { |
| 17 | $tpl['output'] = \JSMin\JSMin::minify($textIn); |
| 18 | } catch (Exception $e) { |
| 19 | $tpl['exceptionMsg'] = getExceptionMsg($e, $textIn); |
| 20 | $tpl['output'] = $textIn; |
| 21 | sendPage($tpl); |
| 22 | } |
| 23 | $tpl['time'] = microtime(true) - $startTime; |
| 24 | $tpl['outBytes'] = strlen($tpl['output']); |
| 25 | } |
| 26 | |
| 27 | sendPage($tpl); |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * @param Exception $e |
| 32 | * @param string $input |
| 33 | * @return string HTML |
| 34 | */ |
| 35 | function getExceptionMsg(Exception $e, $input) { |
| 36 | $msg = "<p>" . h($e->getMessage()) . "</p>"; |
| 37 | |
| 38 | if (0 !== strpos(get_class($e), 'JSMin\\Unterminated') |
| 39 | || !preg_match('~byte (\d+)~', $e->getMessage(), $m)) { |
| 40 | return $msg; |
| 41 | } |
| 42 | |
| 43 | $msg .= "<pre>"; |
| 44 | if ($m[1] > 200) { |
| 45 | $msg .= h(substr($input, ($m[1] - 200), 200)); |
| 46 | } else { |
| 47 | $msg .= h(substr($input, 0, $m[1])); |
| 48 | } |
| 49 | $highlighted = isset($input[$m[1]]) ? h($input[$m[1]]) : '␄'; |
| 50 | if ($highlighted === "\n") { |
| 51 | $highlighted = "⏎\n"; |
| 52 | } |
| 53 | $msg .= "<span style='background:#c00;color:#fff'>$highlighted</span>"; |
| 54 | $msg .= h(substr($input, $m[1] + 1, 200)) . "</span></pre>"; |
| 55 | |
| 56 | return $msg; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Draw page |
| 61 | * |
| 62 | * @param array $vars |
| 63 | */ |
| 64 | function sendPage($vars) { |
| 65 | header('Content-Type: text/html; charset=utf-8'); |
| 66 | |
| 67 | ?> |
| 68 | <!DOCTYPE html><head><title>JSMin</title></head> |
| 69 | <?php |
| 70 | if (isset($vars['exceptionMsg'])) { |
| 71 | echo $vars['exceptionMsg']; |
| 72 | } |
| 73 | if (isset($vars['time'])) { |
| 74 | echo " |
| 75 | <table> |
| 76 | <tr><th>Bytes in</th><td>{$vars['inBytes']} (after line endings normalized to <code>\\n</code>)</td></tr> |
| 77 | <tr><th>Bytes out</th><td>{$vars['outBytes']} (reduced " . round(100 - (100 * $vars['outBytes'] / $vars['inBytes'])) . "%)</td></tr> |
| 78 | <tr><th>Time (s)</th><td>" . round($vars['time'], 5) . "</td></tr> |
| 79 | </table> |
| 80 | "; |
| 81 | } |
| 82 | ?> |
| 83 | <form action="?2" method="post"> |
| 84 | <p><label>Content<br><textarea name="textIn" cols="80" rows="35" style="width:99%"><?php |
| 85 | if (isset($vars['output'])) { |
| 86 | echo h($vars['output']); |
| 87 | } |
| 88 | ?></textarea></label></p> |
| 89 | <p><input type="submit" name="method" value="JSMin::minify()"></p> |
| 90 | </form><?php |
| 91 | exit; |
| 92 | } |
| 93 |