2015-06-24 16:01:35 -04:00
|
|
|
<?php
|
|
|
|
/**
|
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-02-03 14:57:00 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren
|
2016-01-04 16:58:33 -05:00
|
|
|
* @copyright Copyright (c) 2015 - 2016
|
2016-02-03 14:57:00 -05:00
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
* @license MIT
|
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
|
|
|
|
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-03 14:57:00 -05:00
|
|
|
class CSSMin {
|
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;
|
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-03 14:57:00 -05:00
|
|
|
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
|
|
|
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
// Send 304 when not modified for faster response
|
|
|
|
if($this->last_modified === $requested_time)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-03 14:57:00 -05:00
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit();
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
public function compress($buffer)
|
|
|
|
{
|
2015-06-24 16:01:35 -04:00
|
|
|
|
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);
|
|
|
|
$replace = array(
|
|
|
|
' )' => ')',
|
|
|
|
') ' => ')',
|
|
|
|
' }' => '}',
|
|
|
|
'} ' => '}',
|
|
|
|
' {' => '{',
|
|
|
|
'{ ' => '{',
|
|
|
|
', ' => ',',
|
|
|
|
': ' => ':',
|
|
|
|
'; ' => ';',
|
|
|
|
);
|
|
|
|
|
|
|
|
//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()
|
|
|
|
{
|
|
|
|
$modified = array();
|
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);
|
|
|
|
$last_modified = $modified[0];
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
return $last_modified;
|
|
|
|
}
|
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 = '';
|
|
|
|
|
|
|
|
if(isset($this->group))
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-03 14:57:00 -05:00
|
|
|
foreach($this->group as $file)
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function output($css)
|
|
|
|
{
|
|
|
|
//This GZIPs the CSS for transmission to the user
|
|
|
|
//making file size smaller and transfer rate quicker
|
|
|
|
ob_start("ob_gzhandler");
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
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', $this->last_modified)." GMT");
|
|
|
|
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime(basename(__FILE__)) + 691200))." GMT");
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
echo $css;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
ob_end_flush();
|
|
|
|
}
|
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
|
|
|
|
$config = require('../app/config/minify_config.php');
|
|
|
|
$groups = require($config['css_groups_file']);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
new CSSMin($config, $groups);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
//End of css.php
|