2012-02-12 23:13:06 -05:00
|
|
|
<?php
|
2015-09-29 13:17:31 -04:00
|
|
|
die('Disabled: use this only for testing');
|
2012-02-12 23:13:06 -05:00
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
$app = (require __DIR__ . '/../../bootstrap.php');
|
|
|
|
/* @var \Minify\App $app */
|
2014-10-12 16:27:43 +03:00
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
// use FirePHP if not already setup
|
|
|
|
if (!$app->config->errorLogger) {
|
|
|
|
$app->config->errorLogger = true;
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
$app->cache = new Minify_Cache_Null();
|
|
|
|
|
|
|
|
$env = $app->env;
|
|
|
|
|
2013-11-27 18:28:39 -05:00
|
|
|
function h($txt) {
|
|
|
|
return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
if ($env->post('textIn')) {
|
|
|
|
$textIn = str_replace("\r\n", "\n", $env->post('textIn'));
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
if ($env->post('method') === 'Minify and serve') {
|
2012-02-12 23:13:06 -05:00
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
$base = trim($env->post('base'));
|
2012-02-12 23:13:06 -05:00
|
|
|
if ($base) {
|
|
|
|
$textIn = preg_replace(
|
|
|
|
'@(<head\\b[^>]*>)@i'
|
2013-11-27 18:28:39 -05:00
|
|
|
,'$1<base href="' . h($base) . '" />'
|
2012-02-12 23:13:06 -05:00
|
|
|
,$textIn
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sourceSpec['content'] = $textIn;
|
|
|
|
$sourceSpec['id'] = 'foo';
|
|
|
|
if (isset($_POST['minJs'])) {
|
2015-09-28 15:36:52 -04:00
|
|
|
$sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin\\JSMin', 'minify');
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
|
|
|
if (isset($_POST['minCss'])) {
|
2015-09-28 20:30:20 -04:00
|
|
|
$sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSSmin', 'minify');
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
|
|
|
$source = new Minify_Source($sourceSpec);
|
2015-09-29 13:17:31 -04:00
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
$controller = new Minify_Controller_Files($env, $app->sourceFactory, $app->logger);
|
2012-02-12 23:13:06 -05:00
|
|
|
try {
|
2016-02-27 01:13:28 -05:00
|
|
|
$app->minify->serve($controller, array(
|
|
|
|
'files' => $source,
|
|
|
|
'contentType' => Minify::TYPE_HTML,
|
2012-02-12 23:13:06 -05:00
|
|
|
));
|
|
|
|
} catch (Exception $e) {
|
2013-11-27 18:28:39 -05:00
|
|
|
echo h($e->getMessage());
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
2014-09-20 10:47:59 +02:00
|
|
|
exit;
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
|
|
|
|
2013-11-27 18:28:39 -05:00
|
|
|
$tpl = array();
|
2015-09-29 13:17:31 -04:00
|
|
|
$tpl['classes'] = array('Minify_HTML', 'JSMin\\JSMin', 'Minify_CSS');
|
2012-02-12 23:13:06 -05:00
|
|
|
|
2016-02-27 01:13:28 -05:00
|
|
|
if (in_array($env->post('method'), $tpl['classes'])) {
|
2012-02-12 23:13:06 -05:00
|
|
|
|
|
|
|
$args = array($textIn);
|
2016-02-27 01:13:28 -05:00
|
|
|
if ($env->post('method') === 'Minify_HTML') {
|
2012-02-12 23:13:06 -05:00
|
|
|
$args[] = array(
|
2015-09-28 20:30:20 -04:00
|
|
|
'cssMinifier' => array('Minify_CSSmin', 'minify')
|
2015-09-28 15:36:52 -04:00
|
|
|
,'jsMinifier' => array('JSMin\\JSMin', 'minify')
|
2012-02-12 23:13:06 -05:00
|
|
|
);
|
|
|
|
}
|
2016-02-27 01:13:28 -05:00
|
|
|
$func = array($env->post('method'), 'minify');
|
2013-11-27 18:28:39 -05:00
|
|
|
$tpl['inBytes'] = strlen($textIn);
|
2012-11-25 16:34:55 -05:00
|
|
|
$startTime = microtime(true);
|
2012-02-12 23:13:06 -05:00
|
|
|
try {
|
2013-11-27 18:28:39 -05:00
|
|
|
$tpl['output'] = call_user_func_array($func, $args);
|
2012-02-12 23:13:06 -05:00
|
|
|
} catch (Exception $e) {
|
2013-11-27 18:28:39 -05:00
|
|
|
$tpl['exceptionMsg'] = getExceptionMsg($e, $textIn);
|
|
|
|
$tpl['output'] = $textIn;
|
|
|
|
sendPage($tpl);
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
2013-11-27 18:28:39 -05:00
|
|
|
$tpl['time'] = microtime(true) - $startTime;
|
|
|
|
$tpl['outBytes'] = strlen($tpl['output']);
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
|
|
|
|
2013-11-27 18:28:39 -05:00
|
|
|
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)) {
|
|
|
|
$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]]) : '␄';
|
|
|
|
if ($highlighted === "\n") {
|
|
|
|
$highlighted = "⏎\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');
|
2012-02-12 23:13:06 -05:00
|
|
|
?>
|
|
|
|
<!DOCTYPE html><head><title>minifyTextarea</title></head>
|
2015-09-29 13:17:31 -04:00
|
|
|
|
|
|
|
<p><strong>Warning! Please do not place this application on a public site.</strong> This should be used only for testing.</p>
|
|
|
|
|
2012-02-12 23:13:06 -05:00
|
|
|
<?php
|
2013-11-27 18:28:39 -05:00
|
|
|
if (isset($vars['exceptionMsg'])) {
|
|
|
|
echo $vars['exceptionMsg'];
|
|
|
|
}
|
|
|
|
if (isset($vars['time'])) {
|
2012-02-12 23:13:06 -05:00
|
|
|
echo "
|
|
|
|
<table>
|
2013-11-27 18:28:39 -05:00
|
|
|
<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>
|
2012-02-12 23:13:06 -05:00
|
|
|
</table>
|
|
|
|
";
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
<form action="?2" method="post">
|
|
|
|
<p><label>Content<br><textarea name="textIn" cols="80" rows="35" style="width:99%"><?php
|
2013-11-27 18:28:39 -05:00
|
|
|
if (isset($vars['output'])) {
|
|
|
|
echo h($vars['output']);
|
2012-02-12 23:13:06 -05:00
|
|
|
}
|
|
|
|
?></textarea></label></p>
|
|
|
|
<p>Minify with:
|
2013-11-27 18:28:39 -05:00
|
|
|
<?php foreach ($vars['classes'] as $minClass): ?>
|
2012-02-12 23:13:06 -05:00
|
|
|
<input type="submit" name="method" value="<?php echo $minClass; ?>">
|
|
|
|
<?php endForEach; ?>
|
|
|
|
</p>
|
|
|
|
<p>...or <input type="submit" name="method" value="Minify and serve"> this HTML to the browser. Also minify:
|
|
|
|
<label>CSS <input type="checkbox" name="minCss" checked></label> :
|
|
|
|
<label>JS <input type="checkbox" name="minJs" checked></label>.
|
|
|
|
<label>Insert BASE element w/ href: <input type="text" name="base" size="20"></label>
|
|
|
|
</p>
|
|
|
|
</form>
|
2013-11-27 18:28:39 -05:00
|
|
|
<?php if (isset($vars['selectByte'])) { ?>
|
|
|
|
<script>
|
|
|
|
function selectText(el, begin, end) {
|
|
|
|
var len = el.value.length;
|
|
|
|
end = end || len;
|
|
|
|
if (begin == null) {
|
|
|
|
el.select();
|
|
|
|
} else {
|
|
|
|
if (el.setSelectionRange) {
|
|
|
|
el.setSelectionRange(begin, end);
|
|
|
|
} else {
|
|
|
|
if (el.createTextRange) {
|
|
|
|
var tr = el.createTextRange()
|
|
|
|
,c = "character";
|
|
|
|
tr.moveStart(c, begin);
|
|
|
|
tr.moveEnd(c, end - len);
|
|
|
|
tr.select();
|
|
|
|
} else {
|
|
|
|
el.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
el.focus();
|
|
|
|
}
|
|
|
|
window.onload = function () {
|
|
|
|
var ta = document.querySelector('textarea[name="textIn"]');
|
|
|
|
selectText(ta, <?= $vars['selectByte'] ?>, <?= ($vars['selectByte'] + 1) ?>);
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<?php }
|
|
|
|
exit;
|
|
|
|
}
|