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-11-13 11:32:12 -05:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-11-13 11:32:12 -05:00
|
|
|
// Include guzzle
|
|
|
|
require_once('../vendor/autoload.php');
|
2016-02-04 21:57:14 -05:00
|
|
|
require_once('./min.php');
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
/**
|
2016-02-03 14:57:00 -05:00
|
|
|
* Simple Javascript minfier, using google closure compiler
|
2015-06-24 16:01:35 -04:00
|
|
|
*/
|
2016-02-04 21:57:14 -05:00
|
|
|
class JSMin extends BaseMin {
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
protected $js_root;
|
|
|
|
protected $js_group;
|
|
|
|
protected $js_groups_file;
|
|
|
|
protected $cache_file;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
protected $last_modified;
|
|
|
|
protected $requested_time;
|
|
|
|
protected $cache_modified;
|
|
|
|
|
|
|
|
public function __construct(array $config, array $groups)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-03 14:57:00 -05:00
|
|
|
$group = $_GET['g'];
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
$this->js_root = $config['js_root'];
|
|
|
|
$this->js_group = $groups[$group];
|
|
|
|
$this->js_groups_file = $config['js_groups_file'];
|
|
|
|
$this->cache_file = "{$this->js_root}cache/{$group}";
|
|
|
|
$this->last_modified = $this->get_last_modified();
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
$this->cache_modified = (is_file($this->cache_file))
|
|
|
|
? filemtime($this->cache_file)
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
// Output some JS!
|
|
|
|
$this->send();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function send()
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2016-02-08 10:57:44 -05:00
|
|
|
// Override caching if debug key is set
|
|
|
|
if($this->is_debug_call())
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2016-02-08 10:57:44 -05:00
|
|
|
return $this->output($this->get_files());
|
|
|
|
}
|
2016-02-03 14:57:00 -05:00
|
|
|
|
2016-02-08 10:57:44 -05:00
|
|
|
// If the browser's cached version is up to date,
|
|
|
|
// don't resend the file
|
2016-02-08 11:32:39 -05:00
|
|
|
if($this->last_modified == $this->get_if_modified() && $this->is_not_debug())
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2016-02-08 10:57:44 -05:00
|
|
|
throw new FileNotChangedException();
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
2016-02-08 10:57:44 -05:00
|
|
|
|
|
|
|
if($this->cache_modified < $this->last_modified)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
|
|
|
$js = $this->minify($this->get_files());
|
|
|
|
|
|
|
|
//Make sure cache file gets created/updated
|
2016-02-08 10:57:44 -05:00
|
|
|
if (file_put_contents($this->cache_file, $js) === FALSE)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2016-02-08 10:57:44 -05:00
|
|
|
echo 'Cache file was not created. Make sure you have the correct folder permissions.';
|
|
|
|
return;
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->output($js);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-04 21:57:14 -05:00
|
|
|
return $this->output(file_get_contents($this->cache_file));
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Makes a call to google closure compiler service
|
|
|
|
*
|
|
|
|
* @param array $options - Form parameters
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
protected function closure_call(array $options)
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$response = $client->post('http://closure-compiler.appspot.com/compile', [
|
|
|
|
'headers' => [
|
|
|
|
'Accept-Encoding' => 'gzip',
|
|
|
|
'Content-type' => 'application/x-www-form-urlencoded'
|
|
|
|
],
|
|
|
|
'form_params' => $options
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Do a call to the closure compiler to check for compilation errors
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function check_minify_errors($options)
|
|
|
|
{
|
|
|
|
$error_res = $this->closure_call($options);
|
2017-01-05 13:41:32 -05:00
|
|
|
$error_json = (string)$error_res->getBody();
|
2016-02-03 14:57:00 -05:00
|
|
|
$error_obj = json_decode($error_json) ?: (object)[];
|
|
|
|
|
|
|
|
// Show error if exists
|
2016-02-08 13:37:44 -05:00
|
|
|
if ( ! empty($error_obj->errors) || ! empty($error_obj->serverErrors))
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
|
|
|
$error_json = json_encode($error_obj, JSON_PRETTY_PRINT);
|
2016-02-08 13:37:44 -05:00
|
|
|
header('Content-type: application/javascript');
|
2016-02-03 14:57:00 -05:00
|
|
|
echo "console.error(${error_json});";
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Get Files
|
|
|
|
*
|
|
|
|
* Concatenates the javascript files for the current
|
|
|
|
* group as a string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_files()
|
|
|
|
{
|
|
|
|
$js = '';
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
foreach($this->js_group as $file)
|
|
|
|
{
|
|
|
|
$new_file = realpath("{$this->js_root}{$file}");
|
|
|
|
$js .= file_get_contents($new_file) . "\n\n";
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
return $js;
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Get the most recent modified 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
|
|
|
foreach($this->js_group as $file)
|
|
|
|
{
|
|
|
|
$new_file = realpath("{$this->js_root}{$file}");
|
|
|
|
$modified[] = filemtime($new_file);
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
//Add this page too, as well as the groups file
|
|
|
|
$modified[] = filemtime(__FILE__);
|
|
|
|
$modified[] = filemtime($this->js_groups_file);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
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
|
|
|
/**
|
|
|
|
* Minifies javascript using google's closure compiler
|
|
|
|
*
|
|
|
|
* @param string $js
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-08 10:57:44 -05:00
|
|
|
protected function minify($js)
|
2015-06-26 12:03:42 -04:00
|
|
|
{
|
2016-02-03 14:57:00 -05:00
|
|
|
$options = [
|
|
|
|
'output_info' => 'errors',
|
|
|
|
'output_format' => 'json',
|
|
|
|
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
|
|
|
|
//'compilation_level' => 'ADVANCED_OPTIMIZATIONS',
|
|
|
|
'js_code' => $js,
|
|
|
|
'language' => 'ECMASCRIPT6_STRICT',
|
|
|
|
'language_out' => 'ECMASCRIPT5_STRICT'
|
|
|
|
];
|
|
|
|
|
|
|
|
// Check for errors
|
|
|
|
$this->check_minify_errors($options);
|
|
|
|
|
|
|
|
// Now actually retrieve the compiled code
|
|
|
|
$options['output_info'] = 'compiled_code';
|
|
|
|
$res = $this->closure_call($options);
|
2017-01-05 13:41:32 -05:00
|
|
|
$json = (string)$res->getBody();
|
2016-02-03 14:57:00 -05:00
|
|
|
$obj = json_decode($json);
|
|
|
|
|
|
|
|
return $obj->compiledCode;
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
/**
|
|
|
|
* Output the minified javascript
|
|
|
|
*
|
|
|
|
* @param string $js
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function output($js)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-08 11:32:39 -05:00
|
|
|
$this->send_final_output($js, 'application/javascript', $this->last_modified);
|
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
|
|
|
$config = require_once('../app/config/minify_config.php');
|
|
|
|
$groups = require_once($config['js_groups_file']);
|
|
|
|
$cache_dir = "{$config['js_root']}cache";
|
|
|
|
|
|
|
|
if ( ! is_dir($cache_dir))
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2016-02-03 14:57:00 -05:00
|
|
|
mkdir($cache_dir);
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
if ( ! array_key_exists($_GET['g'], $groups))
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-08 10:57:44 -05:00
|
|
|
throw new InvalidArgumentException('You must specify a js group that exists');
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2016-02-08 10:57:44 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
new JSMin($config, $groups);
|
|
|
|
}
|
|
|
|
catch (FileNotChangedException $e)
|
|
|
|
{
|
|
|
|
BaseMin::send304();
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
//end of js.php
|