2012-01-13 11:58:06 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MiniMVC
|
|
|
|
*
|
|
|
|
* Convention-based micro-framework for PHP
|
|
|
|
*
|
2012-04-27 16:28:25 -04:00
|
|
|
* @package miniMVC
|
2012-01-13 11:58:06 -05:00
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2011 - 2012
|
2012-05-23 12:01:13 -04:00
|
|
|
* @link https://github.com/aviat4ion/miniMVC
|
2012-01-13 11:58:06 -05:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CSS Minifier and Cacher
|
2012-04-27 16:28:25 -04:00
|
|
|
*
|
|
|
|
* @package miniMVC
|
|
|
|
* @subpackage Assets
|
2012-01-13 11:58:06 -05:00
|
|
|
*/
|
2012-04-27 16:28:25 -04:00
|
|
|
|
2012-01-13 11:58:06 -05:00
|
|
|
//Get config files
|
|
|
|
require('./config/config.php');
|
|
|
|
|
|
|
|
//Include the css groups
|
|
|
|
$groups = require("./config/css_groups.php");
|
|
|
|
|
|
|
|
//The name of this file
|
2012-05-03 11:39:09 -04:00
|
|
|
$this_file = __FILE__;
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-04-27 16:28:25 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-04-27 16:28:25 -04:00
|
|
|
/**
|
|
|
|
* CSS Minifier
|
|
|
|
*
|
|
|
|
* @param string $buffer
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-01-13 11:58:06 -05:00
|
|
|
function compress($buffer) {
|
|
|
|
|
|
|
|
//Remove CSS comments
|
|
|
|
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
|
|
|
|
|
|
|
|
//Remove tabs, spaces, newlines, etc.
|
|
|
|
$buffer = preg_replace('`\s+`', ' ', $buffer);
|
2012-05-22 11:11:36 -04:00
|
|
|
$replace = [
|
2012-01-13 11:58:06 -05:00
|
|
|
' )' => ')',
|
|
|
|
') ' => ')',
|
|
|
|
' }' => '}',
|
|
|
|
'} ' => '}',
|
|
|
|
' {' => '{',
|
|
|
|
'{ ' => '{',
|
|
|
|
', ' => ',',
|
|
|
|
': ' => ':',
|
|
|
|
'; ' => ';',
|
2012-05-22 11:11:36 -04:00
|
|
|
];
|
2012-01-13 11:58:06 -05:00
|
|
|
|
|
|
|
//Eradicate every last space!
|
|
|
|
$buffer = trim(strtr($buffer, $replace));
|
|
|
|
$buffer = str_replace('{ ', '{', $buffer);
|
|
|
|
$buffer = str_replace('} ', '}', $buffer);
|
|
|
|
|
|
|
|
return $buffer;
|
|
|
|
}
|
|
|
|
|
2012-04-27 16:28:25 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-01-13 11:58:06 -05:00
|
|
|
//Creative rewriting
|
|
|
|
$pi = $_SERVER['PATH_INFO'];
|
|
|
|
$pia = explode('/', $pi);
|
|
|
|
|
|
|
|
$pia_len = count($pia);
|
|
|
|
$i = 1;
|
|
|
|
|
|
|
|
while($i < $pia_len)
|
|
|
|
{
|
|
|
|
$j = $i+1;
|
|
|
|
$j = (isset($pia[$j])) ? $j : $i;
|
|
|
|
|
|
|
|
$_GET[$pia[$i]] = $pia[$j];
|
|
|
|
|
|
|
|
$i = $j + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$css = '';
|
2012-05-14 14:35:57 -04:00
|
|
|
$modified = [];
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
if (isset($groups[$_GET['g']]))
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-05-18 13:52:12 -04:00
|
|
|
foreach ($groups[$_GET['g']] as &$file)
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-04-27 16:28:25 -04:00
|
|
|
$new_file = $css_root.$file;
|
2012-01-13 11:58:06 -05:00
|
|
|
$css .= file_get_contents($new_file);
|
|
|
|
$modified[] = filemtime($new_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add this page too
|
|
|
|
$modified[] = filemtime($this_file);
|
|
|
|
|
|
|
|
//Get the latest modified date
|
|
|
|
rsort($modified);
|
|
|
|
$last_modified = $modified[0];
|
|
|
|
|
2012-05-18 13:52:12 -04:00
|
|
|
$requested_time= (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
2012-01-13 11:58:06 -05:00
|
|
|
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
|
|
|
: time();
|
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
if ($last_modified === $requested_time)
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2012-05-03 13:26:09 -04:00
|
|
|
if (!isset($_GET['debug']))
|
2012-04-27 16:28:25 -04:00
|
|
|
{
|
|
|
|
$css = compress($css);
|
|
|
|
}
|
|
|
|
|
|
|
|
$size = strlen($css) * 8;
|
|
|
|
|
2012-01-13 11:58:06 -05:00
|
|
|
//This GZIPs the CSS for transmission to the user
|
|
|
|
//making file size smaller and transfer rate quicker
|
|
|
|
ob_start("ob_gzhandler");
|
|
|
|
|
|
|
|
header("Content-Type: text/css; charset=utf8");
|
|
|
|
header("Cache-control: public, max-age=691200, must-revalidate");
|
|
|
|
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
|
|
|
|
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
|
|
|
|
|
|
|
|
echo $css;
|
|
|
|
|
|
|
|
ob_end_flush();
|
2012-04-27 16:28:25 -04:00
|
|
|
//End of css.php
|