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
|
|
|
*
|
2015-11-16 11:40:01 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren
|
2016-01-04 16:58:33 -05:00
|
|
|
* @copyright Copyright (c) 2015 - 2016
|
2015-11-16 11:40:01 -05:00
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
* @license MIT
|
2015-06-24 16:01:35 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2015-11-13 11:32:12 -05:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
//Get config files
|
2015-11-13 11:32:12 -05:00
|
|
|
require_once('../app/config/minify_config.php');
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
//Include the js groups
|
2015-06-26 12:03:42 -04:00
|
|
|
$groups_file = "../app/config/minify_js_groups.php";
|
2015-11-13 11:32:12 -05:00
|
|
|
$groups = require_once($groups_file);
|
|
|
|
|
|
|
|
// Include guzzle
|
|
|
|
require_once('../vendor/autoload.php');
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
//The name of this file
|
2015-06-26 12:03:42 -04:00
|
|
|
$this_file = __FILE__;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Files
|
|
|
|
*
|
|
|
|
* Concatenates the javascript files for the current
|
|
|
|
* group as a string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_files()
|
|
|
|
{
|
|
|
|
global $groups, $js_root;
|
|
|
|
|
|
|
|
$js = '';
|
|
|
|
|
|
|
|
foreach($groups[$_GET['g']] as $file)
|
|
|
|
{
|
|
|
|
$new_file = realpath($js_root.$file);
|
2015-07-02 14:04:04 -04:00
|
|
|
$js .= file_get_contents($new_file) . "\n\n";
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $js;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Google Min
|
|
|
|
*
|
|
|
|
* Minifies javascript using google's closure compiler
|
|
|
|
* @param string $new_file
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function google_min($new_file)
|
|
|
|
{
|
2015-07-02 14:04:04 -04:00
|
|
|
$options = [
|
2015-11-13 11:32:12 -05:00
|
|
|
'output_info' => 'errors',
|
2015-07-02 14:04:04 -04:00
|
|
|
'output_format' => 'json',
|
|
|
|
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
|
2015-11-13 11:32:12 -05:00
|
|
|
'js_code' => $new_file,
|
|
|
|
'language' => 'ECMASCRIPT5',
|
|
|
|
'language_out' => 'ECMASCRIPT5_STRICT'
|
2015-07-02 14:04:04 -04:00
|
|
|
];
|
|
|
|
|
2015-11-13 11:32:12 -05:00
|
|
|
// First check for errors
|
|
|
|
$error_client = new Client();
|
|
|
|
$error_res = $error_client->post('http://closure-compiler.appspot.com/compile', [
|
|
|
|
'headers' => [
|
|
|
|
'Accept-Encoding' => 'gzip',
|
|
|
|
"Content-type" => "application/x-www-form-urlencoded"
|
|
|
|
],
|
|
|
|
'form_params' => $options
|
|
|
|
]);
|
|
|
|
|
|
|
|
$error_json = $error_res->getBody();
|
|
|
|
$error_obj = json_decode($error_json);
|
|
|
|
|
|
|
|
if ( ! empty($error_obj->errors))
|
2015-07-02 14:04:04 -04:00
|
|
|
{
|
2015-11-13 11:32:12 -05:00
|
|
|
?><pre><?= json_encode($err_obj, JSON_PRETTY_PRINT) ?></pre><?php
|
|
|
|
die();
|
2015-07-02 14:04:04 -04:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:32:12 -05:00
|
|
|
// Now actually retrieve the compiled code
|
|
|
|
$options['output_info'] = 'compiled_code';
|
|
|
|
$client = new Client();
|
|
|
|
$res = $client->post('http://closure-compiler.appspot.com/compile', [
|
|
|
|
'headers' => [
|
|
|
|
'Accept-Encoding' => 'gzip',
|
|
|
|
"Content-type" => "application/x-www-form-urlencoded"
|
|
|
|
],
|
|
|
|
'form_params' => $options
|
|
|
|
]);
|
|
|
|
|
|
|
|
$json = $res->getBody();
|
2015-07-02 14:04:04 -04:00
|
|
|
$obj = json_decode($json);
|
2015-11-13 11:32:12 -05:00
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
return $obj->compiledCode;
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//Creative rewriting of /g/groupname to ?g=groupname
|
|
|
|
$pi = $_SERVER['PATH_INFO'];
|
|
|
|
$pia = explode('/', $pi);
|
|
|
|
|
|
|
|
$pia_len = count($pia);
|
|
|
|
$i = 1;
|
|
|
|
|
|
|
|
while($i < $pia_len)
|
|
|
|
{
|
|
|
|
$j = $i+1;
|
|
|
|
$j = (isset($pia[$j])) ? $j : $i;
|
|
|
|
|
|
|
|
$_GET[$pia[$i]] = $pia[$j];
|
|
|
|
|
|
|
|
$i = $j + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
$js = '';
|
|
|
|
$modified = array();
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//Aggregate the last modified times of the files
|
|
|
|
if(isset($groups[$_GET['g']]))
|
|
|
|
{
|
2015-06-26 12:03:42 -04:00
|
|
|
if ( ! is_dir($js_root . 'cache'))
|
|
|
|
{
|
|
|
|
mkdir($js_root . 'cache');
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
$cache_file = $js_root.'cache/'.$_GET['g'];
|
|
|
|
|
|
|
|
foreach($groups[$_GET['g']] as $file)
|
|
|
|
{
|
|
|
|
$new_file = realpath($js_root.$file);
|
|
|
|
$modified[] = filemtime($new_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add this page too, as well as the groups file
|
|
|
|
$modified[] = filemtime($this_file);
|
|
|
|
$modified[] = filemtime($groups_file);
|
|
|
|
|
|
|
|
$cache_modified = 0;
|
|
|
|
|
|
|
|
//Add the cache file
|
|
|
|
if(is_file($cache_file))
|
|
|
|
{
|
|
|
|
$cache_modified = filemtime($cache_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //Nothing to display? Just exit
|
|
|
|
{
|
|
|
|
die("You must specify a group that exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//Get the latest modified date
|
|
|
|
rsort($modified);
|
|
|
|
$last_modified = $modified[0];
|
|
|
|
|
|
|
|
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
|
|
|
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
|
|
|
: time();
|
|
|
|
|
|
|
|
// If the browser's cached version is up to date,
|
|
|
|
// don't resend the file
|
|
|
|
if($last_modified === $requested_time)
|
|
|
|
{
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//Determine what to do: rebuild cache, send files as is, or send cache.
|
2015-07-02 14:04:04 -04:00
|
|
|
// If debug is set, just concatenate
|
|
|
|
if(isset($_GET['debug']))
|
|
|
|
{
|
|
|
|
$js = get_files();
|
|
|
|
}
|
|
|
|
else if($cache_modified < $last_modified)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
|
|
|
$js = google_min(get_files());
|
|
|
|
|
|
|
|
//Make sure cache file gets created/updated
|
2015-06-26 12:03:42 -04:00
|
|
|
if(file_put_contents($cache_file, $js) === FALSE)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
|
|
|
die("Cache file was not created. Make sure you have the correct folder permissions.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise, send the cached file
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$js = file_get_contents($cache_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//This GZIPs the js for transmission to the user
|
|
|
|
//making file size smaller and transfer rate quicker
|
|
|
|
ob_start("ob_gzhandler");
|
|
|
|
|
|
|
|
// Set important caching headers
|
|
|
|
header("Content-Type: application/javascript; charset=utf8");
|
|
|
|
header("Cache-control: public, max-age=691200, must-revalidate");
|
|
|
|
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
|
|
|
|
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
|
|
|
|
|
|
|
|
echo $js;
|
|
|
|
|
|
|
|
ob_end_flush();
|
2015-06-26 12:03:42 -04:00
|
|
|
//end of js.php
|