Archived
1
0
This repository has been archived on 2020-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
old/combine.php

256 lines
9.6 KiB
PHP
Raw Normal View History

2018-10-16 16:28:42 +00:00
<?php
2018-11-01 10:19:02 +00:00
$cache = true;
$cachedir = dirname(__FILE__) . '/assets/cache';
$cssdir = dirname(__FILE__) . '/assets/css';
$jsdir = dirname(__FILE__) . '/assets/js';
2018-10-16 16:28:42 +00:00
// Determine the directory and type we should use
2018-11-01 10:19:02 +00:00
switch ($_GET['type']) {
case 'css':
$base = realpath($cssdir);
break;
case 'javascript':
$base = realpath($jsdir);
break;
default:
header("HTTP/1.0 503 Not Implemented");
exit;
};
$type = $_GET['type'];
$elements = explode(',', $_GET['files']);
2018-10-16 16:28:42 +00:00
// Determine last modification date of the files
2018-11-01 10:19:02 +00:00
$lastmodified = 0;
while (list(, $element) = each($elements)) {
$path = realpath($base . '/' . $element);
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
if (($type == 'javascript' && substr($path, -3) != '.js') ||
($type == 'css' && substr($path, -4) != '.css')) {
header("HTTP/1.0 403 Forbidden");
exit;
}
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
if (substr($path, 0, strlen($base)) != $base || !file_exists($path)) {
header("HTTP/1.0 404 Not Found");
exit;
}
$lastmodified = max($lastmodified, filemtime($path));
}
2018-10-16 16:28:42 +00:00
// Send Etag hash
2018-11-01 10:19:02 +00:00
$hash = $lastmodified . '-' . md5($_GET['files']);
header("Etag: \"" . $hash . "\"");
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') {
// Return visit and no modifications, so do not send anything
header("HTTP/1.0 304 Not Modified");
header('Content-Length: 0');
} else {
// First time visit or files were modified
if ($cache) {
// Determine supported compression method
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
// Determine used compression method
$encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
// Check for buggy versions of Internet Explorer
if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&
preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$version = floatval($matches[1]);
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
if ($version < 6)
$encoding = 'none';
if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))
$encoding = 'none';
}
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
// Try the cache first to see if the combined files were already generated
$cachefile = 'cache-' . $hash . '.' . $type . ($encoding != 'none' ? '.' . $encoding : '');
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
if (file_exists($cachedir . '/' . $cachefile)) {
if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) {
if ($encoding != 'none') {
header("Content-Encoding: " . $encoding);
}
header("Content-Type: text/" . $type);
header("Content-Length: " . filesize($cachedir . '/' . $cachefile));
fpassthru($fp);
fclose($fp);
exit;
}
2018-10-16 16:28:42 +00:00
}
}
2018-11-01 10:19:02 +00:00
// Get contents of the files
$contents = '';
reset($elements);
while (list(, $element) = each($elements)) {
$path = realpath($base . '/' . $element);
$contents .= "\n\n" . file_get_contents($path);
// $contents = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $contents);
// $contents = str_replace(': ', ':', $contents);
// $contents = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $contents);
2018-10-16 16:28:42 +00:00
// $contents = fn_minify_css($contents);
2018-11-01 10:19:02 +00:00
}
// Send Content-Type
header("Content-Type: text/" . $type);
if (isset($encoding) && $encoding != 'none') {
// Send compressed contents
$contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
header("Content-Encoding: " . $encoding);
header('Content-Length: ' . strlen($contents));
echo $contents;
} else {
// Send regular contents
header('Content-Length: ' . strlen($contents));
echo $contents;
}
echo strlen($contents);
// Store cache
if ($cache) {
if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) {
fwrite($fp, $contents);
fclose($fp);
}
}
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
define('MINIFY_STRING', '"(?:[^"\\\]|\\\.)*"|\'(?:[^\'\\\]|\\\.)*\'');
define('MINIFY_COMMENT_CSS', '/\*[\s\S]*?\*/');
define('MINIFY_COMMENT_HTML', '<!\-{2}[\s\S]*?\-{2}>');
define('X', "\x1A");
2018-10-16 16:28:42 +00:00
2018-11-01 10:19:02 +00:00
function n($s)
{
return str_replace(["\r\n", "\r"], "\n", $s);
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
function t($a, $b)
{
if ($a && strpos($a, $b) === 0 && substr($a, -strlen($b)) === $b) {
return substr(substr($a, strlen($b)), 0, -strlen($b));
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
return $a;
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
function fn_minify($pattern, $input)
{
return preg_split('#(' . implode('|', $pattern) . ')#', $input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
function fn_minify_css($input, $comment = 2, $quote = 2)
{
if (!is_string($input) || !$input = n(trim($input))) return $input;
$output = $prev = "";
foreach (fn_minify([MINIFY_COMMENT_CSS, MINIFY_STRING], $input) as $part) {
if (trim($part) === "") continue;
if ($comment !== 1 && strpos($part, '/*') === 0 && substr($part, -2) === '*/') {
if (
$comment === 2 && (
// Detect special comment(s) from the third character. It should be a `!` or `*` → `/*! keep */` or `/** keep */`
strpos('*!', $part[2]) !== false ||
// Detect license comment(s) from the content. It should contains character(s) like `@license`
stripos($part, '@licence') !== false || // noun
stripos($part, '@license') !== false || // verb
stripos($part, '@preserve') !== false
)
) {
$output .= $part;
}
continue;
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
if ($part[0] === '"' && substr($part, -1) === '"' || $part[0] === "'" && substr($part, -1) === "'") {
// Remove quote(s) where possible …
$q = $part[0];
if (
$quote !== 1 && (
// <https://www.w3.org/TR/CSS2/syndata.html#uri>
substr($prev, -4) === 'url(' && preg_match('#\burl\($#', $prev) ||
// <https://www.w3.org/TR/CSS2/syndata.html#characters>
substr($prev, -1) === '=' && preg_match('#^' . $q . '[a-zA-Z_][\w-]*?' . $q . '$#', $part)
)
) {
$part = t($part, $q); // trim quote(s)
}
$output .= $part;
} else {
$output .= fn_minify_css_union($part);
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
$prev = $part;
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
return trim($output);
2018-10-16 16:28:42 +00:00
}
2018-11-01 10:19:02 +00:00
function fn_minify_css_union($input)
{
if (stripos($input, 'calc(') !== false) {
// Keep important whitespace(s) in `calc()`
$input = preg_replace_callback('#\b(calc\()\s*(.*?)\s*\)#i', function ($m) {
return $m[1] . preg_replace('#\s+#', X, $m[2]) . ')';
}, $input);
}
$input = preg_replace([
// Fix case for `#foo<space>[bar="baz"]`, `#foo<space>*` and `#foo<space>:first-child` [^1]
'#(?<=[\w])\s+(\*|\[|:[\w-]+)#',
// Fix case for `[bar="baz"]<space>.foo`, `*<space>.foo`, `:nth-child(2)<space>.foo` and `@media<space>(foo: bar)<space>and<space>(baz: qux)` [^2]
'#([*\]\)])\s+(?=[\w\#.])#', '#\b\s+\(#', '#\)\s+\b#',
// Minify HEX color code … [^3]
'#\#([a-f\d])\1([a-f\d])\2([a-f\d])\3\b#i',
// Remove whitespace(s) around punctuation(s) [^4]
'#\s*([~!@*\(\)+=\{\}\[\]:;,>\/])\s*#',
// Replace zero unit(s) with `0` [^5]
'#\b(?<!\d\.)(?:0+\.)?0+(?:[a-z]+\b)#i',
// Replace `0.6` with `.6` [^6]
'#\b0+\.(\d+)#',
// Replace `:0 0`, `:0 0 0` and `:0 0 0 0` with `:0` [^7]
'#:(0\s+){0,3}0(?=[!,;\)\}]|$)#',
// Replace `background(?:-position)?:(0|none)` with `background$1:0 0` [^8]
'#\b(background(?:-position)?):(?:0|none)([;,\}])#i',
// Replace `(border(?:-radius)?|outline):none` with `$1:0` [^9]
'#\b(border(?:-radius)?|outline):none\b#i',
// Remove empty selector(s) [^10]
'#(^|[\{\}])(?:[^\{\}]+)\{\}#',
// Remove the last semicolon and replace multiple semicolon(s) with a semicolon [^11]
'#;+([;\}])#',
// Replace multiple whitespace(s) with a space [^12]
'#\s+#'
], [
// [^1]
X . '$1',
// [^2]
'$1' . X, X . '(', ')' . X,
// [^3]
'#$1$2$3',
// [^4]
'$1',
// [^5]
'0',
// [^6]
'.$1',
// [^7]
':0',
// [^8]
'$1:0 0$2',
// [^9]
'$1:0',
// [^10]
'$1',
// [^11]
'$1',
// [^12]
' '
], $input);
return trim(str_replace(X, ' ', $input));
}