summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/vendor/mrclay/jsmin-php/web/index.php
blob: 5b50068e59b0719f012986d145dd4b378a5bebd4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

require __DIR__ . '/../vendor/autoload.php';

function h($txt) {
    return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8');
}

$tpl = array();

if (isset($_POST['textIn'])) {
    $textIn = str_replace("\r\n", "\n", $_POST['textIn']);

    $tpl['inBytes'] = strlen($textIn);
    $startTime = microtime(true);
    try {
        $tpl['output'] = \JSMin\JSMin::minify($textIn);
    } catch (Exception $e) {
        $tpl['exceptionMsg'] = getExceptionMsg($e, $textIn);
        $tpl['output'] = $textIn;
        sendPage($tpl);
    }
    $tpl['time'] = microtime(true) - $startTime;
    $tpl['outBytes'] = strlen($tpl['output']);
}

sendPage($tpl);


/**
 * @param Exception $e
 * @param string $input
 * @return string HTML
 */
function getExceptionMsg(Exception $e, $input) {
    $msg = "<p>" . h($e->getMessage()) . "</p>";

    if (0 !== strpos(get_class($e), 'JSMin\\Unterminated')
        || !preg_match('~byte (\d+)~', $e->getMessage(), $m)) {
        return $msg;
    }

    $msg .= "<pre>";
    if ($m[1] > 200) {
        $msg .= h(substr($input, ($m[1] - 200), 200));
    } else {
        $msg .= h(substr($input, 0, $m[1]));
    }
    $highlighted = isset($input[$m[1]]) ? h($input[$m[1]]) : '&#9220;';
    if ($highlighted === "\n") {
        $highlighted = "&#9166;\n";
    }
    $msg .= "<span style='background:#c00;color:#fff'>$highlighted</span>";
    $msg .= h(substr($input, $m[1] + 1, 200)) . "</span></pre>";

    return $msg;
}

/**
 * Draw page
 *
 * @param array $vars
 */
function sendPage($vars) {
    header('Content-Type: text/html; charset=utf-8');

    ?>
    <!DOCTYPE html><head><title>JSMin</title></head>
    <?php
    if (isset($vars['exceptionMsg'])) {
        echo $vars['exceptionMsg'];
    }
    if (isset($vars['time'])) {
        echo "
<table>
    <tr><th>Bytes in</th><td>{$vars['inBytes']} (after line endings normalized to <code>\\n</code>)</td></tr>
    <tr><th>Bytes out</th><td>{$vars['outBytes']} (reduced " . round(100 - (100 * $vars['outBytes'] / $vars['inBytes'])) . "%)</td></tr>
    <tr><th>Time (s)</th><td>" . round($vars['time'], 5) . "</td></tr>
</table>
    ";
    }
    ?>
    <form action="?2" method="post">
    <p><label>Content<br><textarea name="textIn" cols="80" rows="35" style="width:99%"><?php
                if (isset($vars['output'])) {
                    echo h($vars['output']);
                }
                ?></textarea></label></p>
    <p><input type="submit" name="method" value="JSMin::minify()"></p>
    </form><?php
    exit;
}