config->errorLogger) {
$app->config->errorLogger = true;
}
$app->cache = new Minify_Cache_Null();
$env = $app->env;
function h($txt)
{
return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8');
}
if ($env->post('textIn')) {
$textIn = str_replace("\r\n", "\n", $env->post('textIn'));
}
if ($env->post('method') === 'Minify and serve') {
$base = trim($env->post('base'));
if ($base) {
$textIn = preg_replace(
'@(
]*>)@i',
'$1',
$textIn
);
}
$sourceSpec['content'] = $textIn;
$sourceSpec['id'] = 'foo';
if (isset($_POST['minJs'])) {
$sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin\\JSMin', 'minify');
}
if (isset($_POST['minCss'])) {
$sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSSmin', 'minify');
}
$source = new Minify_Source($sourceSpec);
$controller = new Minify_Controller_Files($env, $app->sourceFactory, $app->logger);
try {
$app->minify->serve($controller, array(
'files' => $source,
'contentType' => Minify::TYPE_HTML,
));
} catch (Exception $e) {
echo h($e->getMessage());
}
exit;
}
$tpl = array();
$tpl['classes'] = array('Minify_HTML', 'JSMin\\JSMin', 'Minify_CSS', 'Minify_Lines');
if (in_array($env->post('method'), $tpl['classes'])) {
$args = array($textIn);
if ($env->post('method') === 'Minify_HTML') {
$args[] = array(
'cssMinifier' => array('Minify_CSSmin', 'minify')
,'jsMinifier' => array('JSMin\\JSMin', 'minify')
);
}
$func = array($env->post('method'), 'minify');
$tpl['inBytes'] = strlen($textIn);
$startTime = microtime(true);
try {
$tpl['output'] = call_user_func_array($func, $args);
} 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 = "" . h($e->getMessage()) . "
";
if (0 === strpos(get_class($e), 'JSMin_Unterminated')
&& preg_match('~byte (\d+)~', $e->getMessage(), $m)) {
$msg .= "";
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]]) : '␄';
if ($highlighted === "\n") {
$highlighted = "⏎\n";
}
$msg .= "$highlighted";
$msg .= h(substr($input, $m[1] + 1, 200)) . "
";
}
return $msg;
}
/**
* Draw page
*
* @param array $vars
*/
function sendPage($vars)
{
header('Content-Type: text/html; charset=utf-8'); ?>
minifyTextarea
Warning! Please do not place this application on a public site. This should be used only for testing.
Bytes in | {$vars['inBytes']} (after line endings normalized to \\n ) |
Bytes out | {$vars['outBytes']} (reduced " . round(100 - (100 * $vars['outBytes'] / $vars['inBytes'])) . "%) |
Time (s) | " . round($vars['time'], 5) . " |
";
} ?>