2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-06-24 16:01:35 -04:00
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2015-06-24 16:01:35 -04:00
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
2015-06-24 16:01:35 -04:00
|
|
|
*
|
2016-10-20 22:09:36 -04:00
|
|
|
* PHP version 7
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-02-15 16:13:32 -05:00
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2017-02-15 16:13:32 -05:00
|
|
|
* @version 4.0
|
2016-08-30 10:01:18 -04:00
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
2015-06-24 16:01:35 -04:00
|
|
|
*/
|
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
namespace Aviat\EasyMin;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-04 21:57:14 -05:00
|
|
|
require_once('./min.php');
|
|
|
|
|
2015-06-29 10:26:50 -04:00
|
|
|
/**
|
2016-02-03 14:57:00 -05:00
|
|
|
* Simple CSS Minifier
|
2015-06-29 10:26:50 -04:00
|
|
|
*/
|
2016-02-04 21:57:14 -05:00
|
|
|
class CSSMin extends BaseMin {
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
protected $cssRoot;
|
|
|
|
protected $pathFrom;
|
|
|
|
protected $pathTo;
|
2016-02-03 14:57:00 -05:00
|
|
|
protected $group;
|
2017-02-15 14:07:22 -05:00
|
|
|
protected $lastModified;
|
|
|
|
protected $requestedTime;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
public function __construct(array $config, array $groups)
|
|
|
|
{
|
|
|
|
$group = $_GET['g'];
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->cssRoot = $config['css_root'];
|
|
|
|
$this->pathFrom = $config['path_from'];
|
|
|
|
$this->pathTo = $config['path_to'];
|
2016-02-03 14:57:00 -05:00
|
|
|
$this->group = $groups[$group];
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->lastModified = $this->getLastModified();
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
$this->send();
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Send the CSS
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function send()
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
if($this->lastModified >= $this->getIfModified() && $this->isNotDebug())
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-08 10:57:44 -05:00
|
|
|
throw new FileNotChangedException();
|
|
|
|
}
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
$css = ( ! array_key_exists('debug', $_GET))
|
2017-02-15 14:07:22 -05:00
|
|
|
? $this->compress($this->getCss())
|
|
|
|
: $this->getCss();
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
$this->output($css);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Function for compressing the CSS as tightly as possible
|
|
|
|
*
|
|
|
|
* @param string $buffer
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-08 10:57:44 -05:00
|
|
|
protected function compress($buffer)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
|
|
|
//Remove CSS comments
|
|
|
|
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
|
|
|
|
|
|
|
|
//Remove tabs, spaces, newlines, etc.
|
|
|
|
$buffer = preg_replace('`\s+`', ' ', $buffer);
|
2017-02-15 11:57:29 -05:00
|
|
|
$replace = [
|
2016-02-03 14:57:00 -05:00
|
|
|
' )' => ')',
|
|
|
|
') ' => ')',
|
|
|
|
' }' => '}',
|
|
|
|
'} ' => '}',
|
|
|
|
' {' => '{',
|
|
|
|
'{ ' => '{',
|
|
|
|
', ' => ',',
|
|
|
|
': ' => ':',
|
|
|
|
'; ' => ';',
|
2017-02-15 11:57:29 -05:00
|
|
|
];
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
//Eradicate every last space!
|
|
|
|
$buffer = trim(strtr($buffer, $replace));
|
|
|
|
$buffer = str_replace('{ ', '{', $buffer);
|
|
|
|
$buffer = str_replace('} ', '}', $buffer);
|
|
|
|
|
|
|
|
return $buffer;
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Get the most recent file modification date
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-02-15 14:07:22 -05:00
|
|
|
protected function getLastModified()
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 11:57:29 -05:00
|
|
|
$modified = [];
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
// Get all the css files, and concatenate them together
|
|
|
|
if(isset($this->group))
|
|
|
|
{
|
|
|
|
foreach($this->group as $file)
|
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$newFile = realpath("{$this->cssRoot}{$file}");
|
|
|
|
$modified[] = filemtime($newFile);
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add this page for last modified check
|
|
|
|
$modified[] = filemtime(__FILE__);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
//Get the latest modified date
|
|
|
|
rsort($modified);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-04 21:57:14 -05:00
|
|
|
return array_shift($modified);
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Get the css to display
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-02-15 14:07:22 -05:00
|
|
|
protected function getCss()
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-03 14:57:00 -05:00
|
|
|
$css = '';
|
|
|
|
|
2016-02-04 21:57:14 -05:00
|
|
|
foreach($this->group as $file)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$newFile = realpath("{$this->cssRoot}{$file}");
|
|
|
|
$css .= file_get_contents($newFile);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
// Correct paths that have changed due to concatenation
|
|
|
|
// based on rules in the config file
|
2017-02-15 14:07:22 -05:00
|
|
|
$css = str_replace($this->pathFrom, $this->pathTo, $css);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
return $css;
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Output the CSS
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-02-08 10:57:44 -05:00
|
|
|
protected function output($css)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->sendFinalOutput($css, 'text/css', $this->lastModified);
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Start Minifying
|
|
|
|
// --------------------------------------------------------------------------
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
//Get config files
|
2017-02-10 15:50:07 -05:00
|
|
|
$config = require('../app/appConf/minify_config.php');
|
2016-02-03 14:57:00 -05:00
|
|
|
$groups = require($config['css_groups_file']);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-08 10:57:44 -05:00
|
|
|
if ( ! array_key_exists($_GET['g'], $groups))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('You must specify a css group that exists');
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
new CSSMin($config, $groups);
|
|
|
|
}
|
|
|
|
catch (FileNotChangedException $e)
|
|
|
|
{
|
|
|
|
BaseMin::send304();
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
//End of css.php
|