diff --git a/min_extras/tools/minifyUrl.php b/min_extras/tools/minifyUrl.php new file mode 100644 index 0000000..dd6cd7b --- /dev/null +++ b/min_extras/tools/minifyUrl.php @@ -0,0 +1,166 @@ + $type + ,'sent' => $sentType + ,'charset' => $charset + ); +} + +if (isset($_POST['url'])) { + + require '../config.php'; + + $url = trim(getPost('url')); + $ua = trim(getPost('ua')); + $cook = trim(getPost('cook')); + + if (! preg_match('@^https?://@', $url)) { + die('HTTP(s) only.'); + } + + $httpOpts = array( + 'max_redirects' => 0 + ,'timeout' => 3 + ); + if ($ua !== '') { + $httpOpts['user_agent'] = $ua; + } + if ($cook !== '') { + $httpOpts['header'] = "Cookie: {$cook}\r\n"; + } + $ctx = stream_context_create(array( + 'http' => $httpOpts + )); + + // fetch + if (! ($fp = @fopen($url, 'r', false, $ctx))) { + die('Couldn\'t open URL.'); + } + $meta = stream_get_meta_data($fp); + $content = stream_get_contents($fp); + fclose($fp); + + // get type info + $type = sniffType($meta['wrapper_data']); + if (! $type['minify']) { + die('Unrecognized Content-Type: ' . $type['sent']); + } + + require 'Minify.php'; + require 'Minify/HTML.php'; + require 'Minify/CSS.php'; + require 'JSMin.php'; + + if ($type['minify'] === 'text/html' + && isset($_POST['addBase']) + && ! preg_match('@]*>)@i' + ,'$1' + ,$content + ); + } + + $sourceSpec['content'] = $content; + $sourceSpec['id'] = 'foo'; + + if ($type['minify'] === 'text/html') { + if (isset($_POST['minJs'])) { + $sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin', 'minify'); + require 'JSMin.php'; + } + if (isset($_POST['minCss'])) { + $sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSS', 'minify'); + require 'Minify/CSS.php'; + } + } + + $source = new Minify_Source($sourceSpec); + + $sendType = 'text/plain'; + if ($type['minify'] === 'text/html' && ! isset($_POST['asText'])) { + $sendType = $type['sent']; + } + if ($type['charset']) { + $sendType .= ';charset=' . $type['charset']; + } + header('Content-Type: ' . $sendType); + // using combine instead of serve because it allows us to specify a + // Content-Type like application/xhtml+xml IF we need to + echo Minify::combine(array($source), array( + 'contentType' => $type['minify'] + )); + exit(); +} + +header('Content-Type: text/html; charset=utf-8'); + +$ua = get_magic_quotes_gpc() + ? stripslashes($_SERVER['HTTP_USER_AGENT']) + : $_SERVER['HTTP_USER_AGENT']; + +?> +Minify URL + +

Warning! Please do not place this application on a public site. This should be used +only for testing.

+ +

Fetch and Minify a URL

+

This tool will retrieve the contents of a URL and minify it. +The fetched resource Content-Type will determine the minifier used.

+ +
+

+

+ +
HTML options +

If the resource above is sent with an (x)HTML Content-Type, the following options will apply:

+
    +
  • +
  • +
  • +
  • +
+
+ +
Retreival options +
    +
  • +
  • +
+
+ +