2011-07-06 19:31:40 -04:00
|
|
|
<?php
|
2011-09-08 14:18:20 -04:00
|
|
|
//Get config files
|
|
|
|
require('./config/config.php');
|
2011-09-08 14:44:38 -04:00
|
|
|
require('./config/JSMinPlus.php');
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2011-09-08 14:18:20 -04:00
|
|
|
//Include the js groups
|
|
|
|
$groups = require("./config/js_groups.php");
|
|
|
|
|
|
|
|
//The name of this file
|
|
|
|
$this_file = $js_root.'js.php';
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Files
|
|
|
|
*
|
|
|
|
* Concatonates 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);
|
|
|
|
$js .= file_get_contents($new_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $js;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2011-09-08 14:18:20 -04:00
|
|
|
//Creative rewriting of /g/groupname to ?g=groupname
|
2011-07-06 19:31:40 -04:00
|
|
|
$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();
|
|
|
|
|
2011-09-08 14:18:20 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//Aggregate the last modified times of the files
|
2011-07-06 19:31:40 -04:00
|
|
|
if(isset($groups[$_GET['g']]))
|
|
|
|
{
|
2011-09-08 14:18:20 -04:00
|
|
|
$cache_file = $js_root.'/cache/'.$_GET['g'];
|
2011-09-07 19:24:17 -04:00
|
|
|
|
2011-07-06 19:31:40 -04:00
|
|
|
foreach($groups[$_GET['g']] as $file)
|
|
|
|
{
|
2011-09-08 14:18:20 -04:00
|
|
|
$new_file = realpath($js_root.$file);
|
2011-07-06 19:31:40 -04:00
|
|
|
$modified[] = filemtime($new_file);
|
|
|
|
}
|
2011-09-08 14:18:20 -04:00
|
|
|
|
|
|
|
//Add this page too
|
|
|
|
$modified[] = filemtime($this_file);
|
|
|
|
|
|
|
|
$cache_modified = 0;
|
|
|
|
|
|
|
|
//Add the cache file
|
|
|
|
if(is_file($cache_file))
|
|
|
|
{
|
|
|
|
$cache_modified = filemtime($cache_file);
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
}
|
2011-09-07 19:24:17 -04:00
|
|
|
else //Nothing to display? Just exit
|
|
|
|
{
|
|
|
|
die("You must specify a group that exists");
|
|
|
|
}
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2011-09-08 14:18:20 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2011-07-06 19:31:40 -04:00
|
|
|
|
2011-09-07 17:08:57 -04:00
|
|
|
//Get the latest modified date
|
|
|
|
rsort($modified);
|
|
|
|
$last_modified = $modified[0];
|
|
|
|
|
2011-07-06 19:31:40 -04:00
|
|
|
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
|
|
|
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
|
|
|
: time();
|
|
|
|
|
2011-09-08 14:18:20 -04:00
|
|
|
// If the browser's cached version is up to date,
|
|
|
|
// don't resend the file
|
2011-07-06 19:31:40 -04:00
|
|
|
if($last_modified === $requested_time)
|
|
|
|
{
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:18:20 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//Determine what to do: rebuild cache, send files as is, or send cache.
|
|
|
|
if($cache_modified < $last_modified)
|
|
|
|
{
|
2011-09-08 14:44:38 -04:00
|
|
|
$js = JSMinPlus::minify(get_files());
|
2011-09-08 14:18:20 -04:00
|
|
|
$cs = file_put_contents($cache_file, $js);
|
|
|
|
|
|
|
|
//Make sure cache file gets created/updated
|
|
|
|
if($cs === FALSE)
|
|
|
|
{
|
|
|
|
die("Cache file was not created. Make sure you have the correct folder permissions.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(isset($_GET['debug']))
|
2011-09-07 17:08:57 -04:00
|
|
|
{
|
2011-09-08 14:18:20 -04:00
|
|
|
$js = get_files();
|
2011-09-07 17:08:57 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$js = file_get_contents($cache_file);
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:18:20 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//This GZIPs the js for transmission to the user
|
|
|
|
//making file size smaller and transfer rate quicker
|
|
|
|
ob_start("ob_gzhandler");
|
|
|
|
|
2011-07-06 19:31:40 -04:00
|
|
|
header("Content-Type: application/x-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");
|
2011-09-08 14:18:20 -04:00
|
|
|
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
|
2011-07-06 19:31:40 -04:00
|
|
|
|
|
|
|
echo $js;
|
|
|
|
|
|
|
|
ob_end_flush();
|
2011-07-06 21:31:14 -04:00
|
|
|
//end of js.php
|