| 1 |
<?php |
| 2 |
die('Disabled: use this only for testing'); |
| 3 |
|
| 4 |
$app = (require __DIR__ . '/../../bootstrap.php'); |
| 5 |
/* @var \Minify\App $app */ |
| 6 |
|
| 7 |
// use FirePHP if not already setup |
| 8 |
if (!$app->config->errorLogger) { |
| 9 |
$app->config->errorLogger = true; |
| 10 |
} |
| 11 |
|
| 12 |
$app->cache = new Minify_Cache_Null(); |
| 13 |
|
| 14 |
$env = $app->env; |
| 15 |
|
| 16 |
function h($txt) |
| 17 |
{ |
| 18 |
return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8'); |
| 19 |
} |
| 20 |
|
| 21 |
if ($env->post('textIn')) { |
| 22 |
$textIn = str_replace("\r\n", "\n", $env->post('textIn')); |
| 23 |
} |
| 24 |
|
| 25 |
if ($env->post('method') === 'Minify and serve') { |
| 26 |
$base = trim($env->post('base')); |
| 27 |
if ($base) { |
| 28 |
$textIn = preg_replace( |
| 29 |
'@(<head\\b[^>]*>)@i', |
| 30 |
'$1<base href="' . h($base) . '" />', |
| 31 |
$textIn |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
$sourceSpec['content'] = $textIn; |
| 36 |
$sourceSpec['id'] = 'foo'; |
| 37 |
if (isset($_POST['minJs'])) { |
| 38 |
$sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin\\JSMin', 'minify'); |
| 39 |
} |
| 40 |
if (isset($_POST['minCss'])) { |
| 41 |
$sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSSmin', 'minify'); |
| 42 |
} |
| 43 |
$source = new Minify_Source($sourceSpec); |
| 44 |
|
| 45 |
$controller = new Minify_Controller_Files($env, $app->sourceFactory, $app->logger); |
| 46 |
try { |
| 47 |
$app->minify->serve($controller, array( |
| 48 |
'files' => $source, |
| 49 |
'contentType' => Minify::TYPE_HTML, |
| 50 |
)); |
| 51 |
} catch (Exception $e) { |
| 52 |
echo h($e->getMessage()); |
| 53 |
} |
| 54 |
exit; |
| 55 |
} |
| 56 |
|
| 57 |
$tpl = array(); |
| 58 |
$tpl['classes'] = array('Minify_HTML', 'JSMin\\JSMin', 'Minify_CSS', 'Minify_Lines'); |
| 59 |
|
| 60 |
if (in_array($env->post('method'), $tpl['classes'])) { |
| 61 |
$args = array($textIn); |
| 62 |
if ($env->post('method') === 'Minify_HTML') { |
| 63 |
$args[] = array( |
| 64 |
'cssMinifier' => array('Minify_CSSmin', 'minify') |
| 65 |
,'jsMinifier' => array('JSMin\\JSMin', 'minify') |
| 66 |
); |
| 67 |
} |
| 68 |
$func = array($env->post('method'), 'minify'); |
| 69 |
$tpl['inBytes'] = strlen($textIn); |
| 70 |
$startTime = microtime(true); |
| 71 |
try { |
| 72 |
$tpl['output'] = call_user_func_array($func, $args); |
| 73 |
} catch (Exception $e) { |
| 74 |
$tpl['exceptionMsg'] = getExceptionMsg($e, $textIn); |
| 75 |
$tpl['output'] = $textIn; |
| 76 |
sendPage($tpl); |
| 77 |
} |
| 78 |
$tpl['time'] = microtime(true) - $startTime; |
| 79 |
$tpl['outBytes'] = strlen($tpl['output']); |
| 80 |
} |
| 81 |
|
| 82 |
sendPage($tpl); |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* @param Exception $e |
| 87 |
* @param string $input |
| 88 |
* @return string HTML |
| 89 |
*/ |
| 90 |
function getExceptionMsg(Exception $e, $input) |
| 91 |
{ |
| 92 |
$msg = "<p>" . h($e->getMessage()) . "</p>"; |
| 93 |
if (0 === strpos(get_class($e), 'JSMin_Unterminated') |
| 94 |
&& preg_match('~byte (\d+)~', $e->getMessage(), $m)) { |
| 95 |
$msg .= "<pre>"; |
| 96 |
if ($m[1] > 200) { |
| 97 |
$msg .= h(substr($input, ($m[1] - 200), 200)); |
| 98 |
} else { |
| 99 |
$msg .= h(substr($input, 0, $m[1])); |
| 100 |
} |
| 101 |
$highlighted = isset($input[$m[1]]) ? h($input[$m[1]]) : '␄'; |
| 102 |
if ($highlighted === "\n") { |
| 103 |
$highlighted = "⏎\n"; |
| 104 |
} |
| 105 |
$msg .= "<span style='background:#c00;color:#fff'>$highlighted</span>"; |
| 106 |
$msg .= h(substr($input, $m[1] + 1, 200)) . "</span></pre>"; |
| 107 |
} |
| 108 |
return $msg; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Draw page |
| 113 |
* |
| 114 |
* @param array $vars |
| 115 |
*/ |
| 116 |
function sendPage($vars) |
| 117 |
{ |
| 118 |
header('Content-Type: text/html; charset=utf-8'); ?> |
| 119 |
<!DOCTYPE html><head><title>minifyTextarea</title></head> |
| 120 |
|
| 121 |
<p><strong>Warning! Please do not place this application on a public site.</strong> This should be used only for testing.</p> |
| 122 |
|
| 123 |
<?php |
| 124 |
if (isset($vars['exceptionMsg'])) { |
| 125 |
echo $vars['exceptionMsg']; |
| 126 |
} |
| 127 |
if (isset($vars['time'])) { |
| 128 |
echo " |
| 129 |
<table> |
| 130 |
<tr><th>Bytes in</th><td>{$vars['inBytes']} (after line endings normalized to <code>\\n</code>)</td></tr> |
| 131 |
<tr><th>Bytes out</th><td>{$vars['outBytes']} (reduced " . round(100 - (100 * $vars['outBytes'] / $vars['inBytes'])) . "%)</td></tr> |
| 132 |
<tr><th>Time (s)</th><td>" . round($vars['time'], 5) . "</td></tr> |
| 133 |
</table> |
| 134 |
"; |
| 135 |
} ?> |
| 136 |
<form action="?2" method="post"> |
| 137 |
<p><label>Content<br><textarea name="textIn" cols="80" rows="35" style="width:99%"><?php |
| 138 |
if (isset($vars['output'])) { |
| 139 |
echo h($vars['output']); |
| 140 |
} ?></textarea></label></p> |
| 141 |
<p>Minify with: |
| 142 |
<?php foreach ($vars['classes'] as $minClass): ?> |
| 143 |
<input type="submit" name="method" value="<?php echo $minClass; ?>"> |
| 144 |
<?php endforeach; ?> |
| 145 |
</p> |
| 146 |
<p>...or <input type="submit" name="method" value="Minify and serve"> this HTML to the browser. Also minify: |
| 147 |
<label>CSS <input type="checkbox" name="minCss" checked></label> : |
| 148 |
<label>JS <input type="checkbox" name="minJs" checked></label>. |
| 149 |
<label>Insert BASE element w/ href: <input type="text" name="base" size="20"></label> |
| 150 |
</p> |
| 151 |
</form> |
| 152 |
<?php if (isset($vars['selectByte'])) { ?> |
| 153 |
<script> |
| 154 |
function selectText(el, begin, end) { |
| 155 |
var len = el.value.length; |
| 156 |
end = end || len; |
| 157 |
if (begin == null) { |
| 158 |
el.select(); |
| 159 |
} else { |
| 160 |
if (el.setSelectionRange) { |
| 161 |
el.setSelectionRange(begin, end); |
| 162 |
} else { |
| 163 |
if (el.createTextRange) { |
| 164 |
var tr = el.createTextRange() |
| 165 |
,c = "character"; |
| 166 |
tr.moveStart(c, begin); |
| 167 |
tr.moveEnd(c, end - len); |
| 168 |
tr.select(); |
| 169 |
} else { |
| 170 |
el.select(); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
el.focus(); |
| 175 |
} |
| 176 |
window.onload = function () { |
| 177 |
var ta = document.querySelector('textarea[name="textIn"]'); |
| 178 |
selectText(ta, <?= $vars['selectByte'] ?>, <?= ($vars['selectByte'] + 1) ?>); |
| 179 |
}; |
| 180 |
</script> |
| 181 |
<?php } |
| 182 |
exit; |
| 183 |
} |
| 184 |
|