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;
|
|
|
|
|
2017-02-09 13:44:56 -05:00
|
|
|
use function Amp\wait;
|
|
|
|
use Amp\Artax\{Client, FormBody, Request};
|
|
|
|
use Aviat\Ion\Json;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-11-13 11:32:12 -05:00
|
|
|
// Include guzzle
|
|
|
|
require_once('../vendor/autoload.php');
|
2017-04-06 11:45:25 -04:00
|
|
|
|
|
|
|
//Creative rewriting of /g/groupname to ?g=groupname
|
|
|
|
$pi = $_SERVER['PATH_INFO'];
|
|
|
|
$pia = explode('/', $pi);
|
|
|
|
|
|
|
|
$piaLen = count($pia);
|
|
|
|
$i = 1;
|
|
|
|
|
|
|
|
while($i < $piaLen)
|
|
|
|
{
|
|
|
|
$j = $i+1;
|
|
|
|
$j = (isset($pia[$j])) ? $j : $i;
|
|
|
|
|
|
|
|
$_GET[$pia[$i]] = $pia[$j];
|
|
|
|
|
|
|
|
$i = $j + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileNotChangedException extends \Exception {}
|
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
|
|
|
*/
|
2017-04-06 11:45:25 -04:00
|
|
|
class JSMin {
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
protected $jsRoot;
|
|
|
|
protected $jsGroup;
|
2017-04-06 11:45:25 -04:00
|
|
|
protected $configFile;
|
2017-02-15 14:07:22 -05:00
|
|
|
protected $cacheFile;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
protected $lastModified;
|
|
|
|
protected $requestedTime;
|
|
|
|
protected $cacheModified;
|
2016-02-03 14:57:00 -05:00
|
|
|
|
2017-04-06 11:45:25 -04:00
|
|
|
public function __construct(array $config, string $configFile)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-02-03 14:57:00 -05:00
|
|
|
$group = $_GET['g'];
|
2017-04-06 11:45:25 -04:00
|
|
|
$groups = $config['groups'];
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->jsRoot = $config['js_root'];
|
|
|
|
$this->jsGroup = $groups[$group];
|
2017-04-06 11:45:25 -04:00
|
|
|
$this->configFile = $configFile;
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->cacheFile = "{$this->jsRoot}cache/{$group}";
|
|
|
|
$this->lastModified = $this->getLastModified();
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->cacheModified = (is_file($this->cacheFile))
|
|
|
|
? filemtime($this->cacheFile)
|
2016-02-03 14:57:00 -05:00
|
|
|
: 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
|
2017-02-15 14:07:22 -05:00
|
|
|
if($this->isDebugCall())
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
return $this->output($this->getFiles());
|
2016-02-08 10:57:44 -05:00
|
|
|
}
|
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
|
2017-02-15 14:07:22 -05:00
|
|
|
if($this->lastModified == $this->getIfModified() && $this->isNotDebug())
|
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
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
if($this->cacheModified < $this->lastModified)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$js = $this->minify($this->getFiles());
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
//Make sure cache file gets created/updated
|
2017-02-15 14:07:22 -05:00
|
|
|
if (file_put_contents($this->cacheFile, $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
|
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
return $this->output(file_get_contents($this->cacheFile));
|
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
|
|
|
|
*/
|
2017-02-15 14:07:22 -05:00
|
|
|
protected function closureCall(array $options)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-09 13:44:56 -05:00
|
|
|
$formFields = http_build_query($options);
|
2017-04-06 11:45:25 -04:00
|
|
|
|
2017-02-09 13:44:56 -05:00
|
|
|
$request = (new Request)
|
|
|
|
->setMethod('POST')
|
|
|
|
->setUri('http://closure-compiler.appspot.com/compile')
|
|
|
|
->setAllHeaders([
|
|
|
|
'Accept' => 'application/json',
|
2016-02-03 14:57:00 -05:00
|
|
|
'Accept-Encoding' => 'gzip',
|
|
|
|
'Content-type' => 'application/x-www-form-urlencoded'
|
2017-02-09 13:44:56 -05:00
|
|
|
])
|
|
|
|
->setBody($formFields);
|
2017-04-06 11:45:25 -04:00
|
|
|
|
2017-02-09 13:44:56 -05:00
|
|
|
$response = wait((new Client)->request($request, [
|
|
|
|
Client::OP_AUTO_ENCODING => false
|
|
|
|
]));
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2017-02-15 14:07:22 -05:00
|
|
|
protected function checkMinifyErrors($options)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$errorRes = $this->closureCall($options);
|
|
|
|
$errorJson = $errorRes->getBody();
|
|
|
|
$errorObj = Json::decode($errorJson) ?: (object)[];
|
2017-04-06 11:45:25 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
// Show error if exists
|
2017-02-15 14:07:22 -05:00
|
|
|
if ( ! empty($errorObj->errors) || ! empty($errorObj->serverErrors))
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$errorJson = Json::encode($errorObj, JSON_PRETTY_PRINT);
|
2016-02-08 13:37:44 -05:00
|
|
|
header('Content-type: application/javascript');
|
2017-02-15 14:07:22 -05:00
|
|
|
echo "console.error(${errorJson});";
|
2016-02-03 14:57:00 -05:00
|
|
|
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
|
|
|
|
*/
|
2017-02-15 14:07:22 -05:00
|
|
|
protected function getFiles()
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
|
|
|
$js = '';
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
foreach($this->jsGroup as $file)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$newFile = realpath("{$this->jsRoot}{$file}");
|
|
|
|
$js .= file_get_contents($newFile) . "\n\n";
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
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
|
|
|
|
*/
|
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
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
foreach($this->jsGroup as $file)
|
2016-02-03 14:57:00 -05:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$newFile = realpath("{$this->jsRoot}{$file}");
|
|
|
|
$modified[] = filemtime($newFile);
|
2016-02-03 14:57:00 -05:00
|
|
|
}
|
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__);
|
2017-04-06 11:45:25 -04:00
|
|
|
$modified[] = filemtime($this->configFile);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-02-03 14:57:00 -05:00
|
|
|
rsort($modified);
|
2017-02-15 14:07:22 -05:00
|
|
|
$lastModified = $modified[0];
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
return $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
|
|
|
/**
|
|
|
|
* 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
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->checkMinifyErrors($options);
|
2016-02-03 14:57:00 -05:00
|
|
|
|
|
|
|
// Now actually retrieve the compiled code
|
|
|
|
$options['output_info'] = 'compiled_code';
|
2017-02-15 14:07:22 -05:00
|
|
|
$res = $this->closureCall($options);
|
2017-02-09 13:44:56 -05:00
|
|
|
$json = $res->getBody();
|
|
|
|
$obj = Json::decode($json);
|
2016-02-03 14:57:00 -05:00
|
|
|
|
2017-02-09 13:44:56 -05:00
|
|
|
//return $obj;
|
|
|
|
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
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
$this->sendFinalOutput($js, 'application/javascript', $this->lastModified);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
2017-04-06 11:45:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of the if-modified-since header
|
|
|
|
*
|
|
|
|
* @return int - timestamp to compare for cache control
|
|
|
|
*/
|
|
|
|
protected function getIfModified()
|
|
|
|
{
|
|
|
|
return (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER))
|
|
|
|
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
|
|
|
: time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of etag to compare to hash of output
|
|
|
|
*
|
|
|
|
* @return string - the etag to compare
|
|
|
|
*/
|
|
|
|
protected function getIfNoneMatch()
|
|
|
|
{
|
|
|
|
return (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER))
|
|
|
|
? $_SERVER['HTTP_IF_NONE_MATCH']
|
|
|
|
: '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether or not to send debug version
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function isNotDebug()
|
|
|
|
{
|
|
|
|
return ! $this->isDebugCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether or not to send debug version
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function isDebugCall()
|
|
|
|
{
|
|
|
|
return array_key_exists('debug', $_GET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send actual output to browser
|
|
|
|
*
|
|
|
|
* @param string $content - the body of the response
|
|
|
|
* @param string $mimeType - the content type
|
|
|
|
* @param int $lastModified - the last modified date
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function sendFinalOutput($content, $mimeType, $lastModified)
|
|
|
|
{
|
|
|
|
//This GZIPs the CSS for transmission to the user
|
|
|
|
//making file size smaller and transfer rate quicker
|
|
|
|
ob_start("ob_gzhandler");
|
|
|
|
|
|
|
|
$expires = $lastModified + 691200;
|
|
|
|
$lastModifiedDate = gmdate('D, d M Y H:i:s', $lastModified);
|
|
|
|
$expiresDate = gmdate('D, d M Y H:i:s', $expires);
|
|
|
|
|
|
|
|
header("Content-Type: {$mimeType}; charset=utf8");
|
|
|
|
header("Cache-control: public, max-age=691200, must-revalidate");
|
|
|
|
header("Last-Modified: {$lastModifiedDate} GMT");
|
|
|
|
header("Expires: {$expiresDate} GMT");
|
|
|
|
|
|
|
|
echo $content;
|
|
|
|
|
|
|
|
ob_end_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a 304 Not Modified header
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function send304()
|
|
|
|
{
|
|
|
|
header("status: 304 Not Modified", true, 304);
|
|
|
|
}
|
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
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2017-04-06 11:45:25 -04:00
|
|
|
$configFile = realpath(__DIR__ . '/../app/appConf/minify_config.php');
|
|
|
|
$config = require_once($configFile);
|
|
|
|
$groups = $config['groups'];
|
2017-02-15 14:07:22 -05:00
|
|
|
$cacheDir = "{$config['js_root']}cache";
|
2016-02-03 14:57:00 -05:00
|
|
|
|
2017-02-15 14:07:22 -05:00
|
|
|
if ( ! is_dir($cacheDir))
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2017-02-15 14:07:22 -05:00
|
|
|
mkdir($cacheDir);
|
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
|
|
|
|
{
|
2017-04-06 11:45:25 -04:00
|
|
|
new JSMin($config, $configFile);
|
2016-02-08 10:57:44 -05:00
|
|
|
}
|
|
|
|
catch (FileNotChangedException $e)
|
|
|
|
{
|
2017-04-06 11:45:25 -04:00
|
|
|
JSMin::send304();
|
2016-02-08 10:57:44 -05:00
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
//end of js.php
|