miniMVC/assets/js.php

188 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2012-01-13 11:58:06 -05:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
2012-04-27 16:28:25 -04:00
* @package miniMVC
2012-01-13 11:58:06 -05:00
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
2012-05-23 12:01:13 -04:00
* @link https://github.com/aviat4ion/miniMVC
2012-07-12 10:23:55 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
2012-01-13 11:58:06 -05:00
*/
// --------------------------------------------------------------------------
/**
* JS Minifier and Cacher
2012-04-27 16:28:25 -04:00
*
* @package miniMVC
* @subpackage Assets
2012-01-13 11:58:06 -05:00
*/
//Get config files
2012-07-12 10:23:55 -04:00
require './config/config.php';
2012-01-13 11:58:06 -05:00
//Include the js groups
$groups_file = "./config/js_groups.php";
$groups = require($groups_file);
//The name of this file
$this_file = __FILE__;
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
/**
* Get Files
2012-07-12 10:23:55 -04:00
*
2012-01-13 11:58:06 -05:00
* Concatenates the javascript files for the current
* group as a string
* @return string
*/
function get_files()
{
global $groups, $js_root;
$js = '';
2012-07-12 10:23:55 -04:00
2012-05-18 13:52:12 -04:00
foreach ($groups[$_GET['g']] as &$file)
2012-01-13 11:58:06 -05:00
{
$new_file = realpath($js_root.$file);
$js .= file_get_contents($new_file);
}
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
return $js;
}
// --------------------------------------------------------------------------
2012-04-27 16:28:25 -04:00
/**
* Google Min
*
* Minifies javascript using google's closure compiler
* @param string $new_file
* @return string
*/
function google_min($new_file)
{
//Get a much-minified version from Google's closure compiler
$ch = curl_init('http://closure-compiler.appspot.com/compile');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($new_file));
$output = curl_exec($ch);
2012-07-12 10:23:55 -04:00
curl_close($ch);
2012-04-27 16:28:25 -04:00
return $output;
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
//Creative rewriting of /g/groupname to ?g=groupname
$pi = $_SERVER['PATH_INFO'];
$pia = explode('/', $pi);
$pia_len = count($pia);
$i = 1;
2012-05-18 13:52:12 -04:00
while ($i < $pia_len)
2012-01-13 11:58:06 -05:00
{
$j = $i+1;
$j = (isset($pia[$j])) ? $j : $i;
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
$_GET[$pia[$i]] = $pia[$j];
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
$i = $j + 1;
};
$js = '';
2012-05-14 14:35:57 -04:00
$modified = [];
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
//Aggregate the last modified times of the files
2012-05-03 13:26:09 -04:00
if (isset($groups[$_GET['g']]))
2012-01-13 11:58:06 -05:00
{
2012-04-27 16:28:25 -04:00
$cache_file = $js_root.'cache/'.$_GET['g'];
2012-07-12 10:23:55 -04:00
2012-05-18 13:52:12 -04:00
foreach ($groups[$_GET['g']] as &$file)
2012-01-13 11:58:06 -05:00
{
$new_file = realpath($js_root.$file);
$modified[] = filemtime($new_file);
}
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
//Add this page too, as well as the groups file
$modified[] = filemtime($this_file);
$modified[] = filemtime($groups_file);
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
$cache_modified = 0;
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
//Add the cache file
2012-05-03 13:26:09 -04:00
if (is_file($cache_file))
2012-01-13 11:58:06 -05:00
{
$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];
2012-07-12 10:23:55 -04:00
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
2012-01-13 11:58:06 -05:00
: time();
2012-07-12 10:23:55 -04:00
// If the browser's cached version is up to date,
2012-01-13 11:58:06 -05:00
// don't resend the file
2012-05-03 13:26:09 -04:00
if ($last_modified === $requested_time)
2012-01-13 11:58:06 -05:00
{
header("HTTP/1.1 304 Not Modified");
exit();
}
// --------------------------------------------------------------------------
//Determine what to do: rebuild cache, send files as is, or send cache.
2012-05-03 13:26:09 -04:00
if ($cache_modified < $last_modified)
2012-01-13 11:58:06 -05:00
{
2012-04-27 16:28:25 -04:00
$js = google_min(get_files());
2012-01-13 11:58:06 -05:00
$cs = file_put_contents($cache_file, $js);
2012-07-12 10:23:55 -04:00
2012-01-13 11:58:06 -05:00
//Make sure cache file gets created/updated
2012-05-03 13:26:09 -04:00
if ($cs === FALSE)
2012-01-13 11:58:06 -05:00
{
die("Cache file was not created. Make sure you have the correct folder permissions.");
}
}
2012-05-18 13:52:12 -04:00
elseif (isset($_GET['debug']))
2012-01-13 11:58:06 -05:00
{
$js = get_files();
}
else
2012-07-12 10:23:55 -04:00
{
2012-04-27 16:28:25 -04:00
$len = filesize($cache_file);
header("Content-Length: {$len}");
2012-01-13 11:58:06 -05:00
$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");
2012-04-27 16:28:25 -04:00
header("Content-Type: text/javascript; charset=utf8");
2012-01-13 11:58:06 -05:00
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();
2012-05-18 13:52:12 -04:00
2012-04-27 16:28:25 -04:00
//end of js.php