Skip to content

Instantly share code, notes, and snippets.

@davecarlson
Created December 23, 2014 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davecarlson/a44162a12850378e997d to your computer and use it in GitHub Desktop.
Save davecarlson/a44162a12850378e997d to your computer and use it in GitHub Desktop.
Media Combiner
The combiner takes a comma-separated list of files:
eg: /jsc/reset.css,main.css
To use it, add this to your Apache Config:
RewriteRule ^/jsc/(.*\.js) /path/to/combine-gzip.inc.php?type=javascript&encoding=none&files=$1
RewriteRule ^/cssc/(.*\.css) /path/to/combine-gzip.inc.php?type=css&encoding=none&files=$1
And save the included php file to a suitable location. Your cache directory will need to be writable by the web server.
<?php
/************************************************************************
* CSS and Javascript Combinator 0.7
*/
$cache = true;
$cachedir = $_SERVER['DOCUMENT_ROOT'] . '/media/cache';
$cssdir = $_SERVER['DOCUMENT_ROOT'] . '/media/css';
$jsdir = $_SERVER['DOCUMENT_ROOT'] . '/media/js';
$force = false;
if( strstr($_SERVER['REQUEST_URI'], "force=true") ):
$force = true;
endif;
// Determine the directory and type we should use
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']);
// Determine last modification date of the files
$lastmodified = 0;
foreach($elements as $key => $element) {
$element = $base . '/' . $element;
$path = realpath($element);
if($path == ""):
unset($elements[$key]);
continue;
endif;
if (($type == 'javascript' && substr($path, -3) != '.js') ||
($type == 'css' && substr($path, -4) != '.css' &&
(substr($path, 0, strlen($base)) != $base)
)) {
header ("HTTP/1.0 403 Forbidden");
exit;
}
if (!file_exists($path)) {
// header ("HTTP/1.0 404 Not Found");
//exit;
}
$lastmodified = max($lastmodified, filemtime($path));
}
// If not files matched, send 404
if (empty($elements)):
header ("HTTP/1.0 404 Not Found");
exit;
endif;
// Send Etag hash
$mhash = md5($_GET['files']);
$hash = $lastmodified . '-' . $mhash;
header ("Etag: \"" . $hash . "\"");
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
$force == true &&
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
{
// last modified is the max mtime for loaded files
header('Cache-Control: must-revalidate');
header('Last-modified: ' . gmdate('r', $lastmodified));
// Set expiry
if (!(isset($_GET['e']) && $_GET['e']==='no')) {
$time = time()+(isset($_GET['e']) ? $_GET['e'] : 365)*86400;
header('Expires: '.gmdate('r', $time));
}
// Send Content-Type
header("Content-Type: text/" . $type);
// First time visit or files were modified
if ($cache)
{
// Try the cache first to see if the combined files were already generated
$cachefile = 'cache-' . $hash . '.' . $type . '.combine-cache';
if (file_exists($cachedir . '/' . $cachefile) && $force == false) {
echo file_get_contents("$cachedir/$cachefile");
exit;
} else {
# Make the cache directory if it doesnt exist
if (!file_exists($cachedir)) {
if (@mkdir($cachedir)) {
@chmod($cachedir, 0775);
}
}
}
// Get contents of the files
$contents = '/* combined '.date("c").' */';
reset($elements);
foreach ($elements as $element) {
$element = $base . '/' . $element;
$path = realpath($element);
$contents .= "\n\n" . file_get_contents($path);
}
// Store cache
if ($cache) {
if ($fp = @fopen($cachedir . '/' . $cachefile, 'wb')) {
fwrite($fp, $contents);
fclose($fp);
}
foreach (glob($cachedir . "/" . "*" . $type .".combine-cache*") as $filename) {
if (strpos($filename,$mhash) !== false && strpos($filename,$cachefile) === false && strpos($filename,$cachefile) === false)
@unlink($filename);
}
}
echo $contents;
exit;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment