2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-06-24 16:01:35 -04:00
|
|
|
/**
|
2015-11-16 11:40:01 -05:00
|
|
|
* Hummingbird Anime Client
|
2015-06-24 16:01:35 -04:00
|
|
|
*
|
2015-11-16 11:40:01 -05:00
|
|
|
* An API client for Hummingbird 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>
|
|
|
|
* @copyright 2015 - 2016 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 3.1
|
|
|
|
* @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
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
protected $css_root;
|
|
|
|
protected $path_from;
|
|
|
|
protected $path_to;
|
|
|
|
protected $group;
|
|
|
|
protected $last_modified;
|
2016-02-04 21:57:14 -05:00
|
|
|
protected $requested_time;
|
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'];
|
|
|
|
$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();
|
|
|
|
}
|
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
|
|
|
{
|
2016-02-08 10:57:44 -05:00
|
|
|
if($this->last_modified >= $this->get_if_modified() && $this->is_not_debug())
|
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))
|
|
|
|
? $this->compress($this->get_css())
|
|
|
|
: $this->get_css();
|
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
protected function get_last_modified()
|
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
$new_file = realpath("{$this->css_root}{$file}");
|
|
|
|
$modified[] = filemtime($new_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
|
|
|
*/
|
|
|
|
protected function get_css()
|
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
|
|
|
{
|
2016-02-04 21:57:14 -05:00
|
|
|
$new_file = realpath("{$this->css_root}{$file}");
|
|
|
|
$css .= file_get_contents($new_file);
|
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
|
2016-02-08 10:57:44 -05:00
|
|
|
$css = str_replace($this->path_from, $this->path_to, $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
|
|
|
{
|
2016-02-08 11:32:39 -05:00
|
|
|
$this->send_final_output($css, 'text/css', $this->last_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
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! 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
|