2011-07-06 19:31:40 -04:00
|
|
|
<?php
|
2012-04-26 10:11:15 -04:00
|
|
|
/**
|
|
|
|
* Easy Min
|
|
|
|
*
|
|
|
|
* Simple minification for better website performance
|
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
2016-02-08 14:49:30 -05:00
|
|
|
* @copyright Copyright (c) 2012-2016
|
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Easy-Min
|
2012-04-26 10:11:15 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2016-02-08 14:49:30 -05:00
|
|
|
namespace Aviat\EasyMin;
|
2012-04-26 10:11:15 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
require_once('./min.php');
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
/**
|
|
|
|
* Simple CSS Minifier
|
|
|
|
*/
|
|
|
|
class CSSMin extends BaseMin {
|
2012-04-26 10:11:15 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
protected $css_root;
|
|
|
|
protected $path_from;
|
|
|
|
protected $path_to;
|
|
|
|
protected $group;
|
|
|
|
protected $last_modified;
|
|
|
|
protected $requested_time;
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
public function __construct(array $config, array $groups)
|
|
|
|
{
|
|
|
|
$group = $_GET['g'];
|
|
|
|
$this->css_root = $config['css_root'];
|
|
|
|
$this->path_from = $config['path_from'];
|
|
|
|
$this->path_to = $config['path_to'];
|
|
|
|
$this->group = $groups[$group];
|
|
|
|
$this->last_modified = $this->get_last_modified();
|
|
|
|
|
|
|
|
$this->send();
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
/**
|
|
|
|
* Send the CSS
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function send()
|
|
|
|
{
|
|
|
|
if($this->last_modified >= $this->get_if_modified() && $this->is_not_debug())
|
|
|
|
{
|
|
|
|
throw new FileNotChangedException();
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
$css = ( ! array_key_exists('debug', $_GET))
|
|
|
|
? $this->compress($this->get_css())
|
|
|
|
: $this->get_css();
|
2012-04-26 10:11:15 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
$this->output($css);
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
/**
|
|
|
|
* Function for compressing the CSS as tightly as possible
|
|
|
|
*
|
|
|
|
* @param string $buffer
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function compress($buffer)
|
2011-07-06 19:31:40 -04:00
|
|
|
{
|
2016-02-08 14:49:30 -05:00
|
|
|
//Remove CSS comments
|
|
|
|
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
|
|
|
|
|
|
|
|
//Remove tabs, spaces, newlines, etc.
|
|
|
|
$buffer = preg_replace('`\s+`', ' ', $buffer);
|
|
|
|
$replace = array(
|
|
|
|
' )' => ')',
|
|
|
|
') ' => ')',
|
|
|
|
' }' => '}',
|
|
|
|
'} ' => '}',
|
|
|
|
' {' => '{',
|
|
|
|
'{ ' => '{',
|
|
|
|
', ' => ',',
|
|
|
|
': ' => ':',
|
|
|
|
'; ' => ';',
|
|
|
|
);
|
|
|
|
|
|
|
|
//Eradicate every last space!
|
|
|
|
$buffer = trim(strtr($buffer, $replace));
|
|
|
|
$buffer = str_replace('{ ', '{', $buffer);
|
|
|
|
$buffer = str_replace('} ', '}', $buffer);
|
|
|
|
|
|
|
|
return $buffer;
|
2011-07-06 19:31:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
/**
|
|
|
|
* Get the most recent file modification date
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function get_last_modified()
|
|
|
|
{
|
|
|
|
$modified = array();
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
// Get all the css files, and concatenate them together
|
|
|
|
if(isset($this->group))
|
|
|
|
{
|
|
|
|
foreach($this->group as $file)
|
|
|
|
{
|
|
|
|
$new_file = realpath("{$this->css_root}{$file}");
|
|
|
|
$modified[] = filemtime($new_file);
|
|
|
|
}
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
//Add this page for last modified check
|
|
|
|
$modified[] = filemtime(__FILE__);
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
//Get the latest modified date
|
|
|
|
rsort($modified);
|
2011-09-09 17:23:38 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
return array_shift($modified);
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
/**
|
|
|
|
* Get the css to display
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_css()
|
|
|
|
{
|
|
|
|
$css = '';
|
|
|
|
|
|
|
|
foreach($this->group as $file)
|
|
|
|
{
|
|
|
|
$new_file = realpath("{$this->css_root}{$file}");
|
|
|
|
$css .= file_get_contents($new_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correct paths that have changed due to concatenation
|
|
|
|
// based on rules in the config file
|
|
|
|
$css = str_replace($this->path_from, $this->path_to, $css);
|
|
|
|
|
|
|
|
return $css;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the CSS
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function output($css)
|
|
|
|
{
|
|
|
|
$this->send_final_output($css, 'text/css', $this->last_modified);
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Start Minifying
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//Get config files
|
|
|
|
$config = require('config/config.php');
|
|
|
|
$groups = require($config['css_groups_file']);
|
2011-09-08 14:18:20 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
if ( ! array_key_exists($_GET['g'], $groups))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('You must specify a css group that exists');
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2016-02-08 14:49:30 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
new CSSMin($config, $groups);
|
|
|
|
}
|
|
|
|
catch (FileNotChangedException $e)
|
|
|
|
{
|
|
|
|
BaseMin::send304();
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2012-04-26 10:11:15 -04:00
|
|
|
//End of css.php
|