/**
* Javascript Porter ver 1.0
* This program gathers and sends multiple Javascript files on demand
* which benefits webpage loading speed in the way of avoiding
* unnecessary requests and reduces the amount of Script tags on pages
*
* Instruction:
* This program search under the preset folder (JSFILEFOLDER) for requested files and send them in order
* This program accepts 3 arguments: l(link list), duplicate and compress
* Arguments:
* link list
* A list of names of Javascript files needed to be gathered
* Do not include file extension (.js), separate with commas
* duplicate
* Allow duplication or not (i.e. when the request is "A,B,A", if duplication is not allowed, the last duplicated A will be ignored)
* Acceptable Value: true | yes | false | no
* Default: no
* compress
* Level of compression
* Add 1 to remove comment blocks
* Add 2 to remove comment lines
* Add 4 to remove indents
* Add 8 to remove line breakers
* i.e. if need to remove all comments, use value 3 (Add 1 and 2)
* Default: 0 (no compression)
* Request Sample: js.php?l=jquery-latest,yui-latest&duplicate=true&compress=15
* jquery-latest.js and yui-latest.js are requested, allowing duplication, at compression level of 15
*/
define('APPNAME', 'Javascript Porter ver 1.0');
define('JSFILEFOLDER', './js');
// Enable output buffer
ob_start();
// Configure output header (Type: Javascript, Filename: container.js, disable cache)
header('Content-Type: application/javascript');
header('Content-Disposition: inline; filename="container.js"');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
// Print program name
printf("/* %s */n", APPNAME);
// Fetch raw request
// If link list is not found, halt the program
if (!array_key_exists('l', $_GET))
die('// no request');
$rawRequestString = $_GET['l'];
$allowDuplicate = array_key_exists('duplicate', $_GET)
? (($_GET['duplicate'] == 'yes' || $_GET['duplicate'] == 'true')
? true
: false)
: false;
$compression = array_key_exists('compress', $_GET)
? intval($_GET['compress'])
: 0;
// change to program directory
chdir(dirname(__FILE__));
// check if Javascript file directory exists
if (!file_exists(JSFILEFOLDER) || !is_dir(JSFILEFOLDER))
die('// js directory not found');
// change to Javascript file directory
chdir(JSFILEFOLDER);
// Compression Function
function jsCompress($jsContent, $compressLevel)
{
/**
* 1 Remove comment blocks
* 2 Remove comment lines
* 3 Remove all comments
* 4 Remove indents
* 5 Remove comment blocks + indents
* 6 Remove comment lines + indents
* 7 Remove all comments + indents
* 8 Remove line breakers *
* 9 Remove comment blocks + line breakers *
* 10 Remove comment lines + line breakers
* 11 Remove all comments + line breakers
* 12 Remove indents + line breakers *
* 13 Remove comment blocks + indents + line breakers *
* 14 Remove comment lines + indents + line breakers
* 15 Remove all comments + indents + line breakers
* * Not recommended before removing comment lines
*/
// return raw data if compression level is invalid
if ($compressLevel > 15)
return $jsContent;
// remove comment blocks
if ($compressLevel % 2 >= 1)
$jsContent = preg_replace('//*.**//uUs', '', $jsContent);
// remove comment lines
if ($compressLevel % 4 >= 2)
$jsContent = preg_replace('///.*$/uUm', '', $jsContent);
// remove indents
if ($compressLevel % 8 >= 4)
$jsContent = preg_replace('/^s+/um', '', $jsContent);
// remove line breakers
if ($compressLevel % 16 >= 8)
$jsContent = preg_replace('/(n|r)/um', '', $jsContent);
return $jsContent;
}
// Print organized request info
printf("// raw request: %sn", $rawRequestString);
printf("// allow duplicates: %sn", $allowDuplicate ? 'true' : 'false');
// Turn request list into an array
$requestList = explode(',', $rawRequestString);
// Traverse the requested items and generate a list of files to be loaded
// jsFiles this array has keys as Javascript filenames (without extension) and contents as Javascript file contents
$jsFiles = Array();
// printList this array contains printing order
$printList = Array();
print("// requested js files: n");
foreach ($requestList as $value) {
$requestItem = trim(basename($value));
if ($requestItem == '') continue;
printf("// %s,n", $requestItem);
if ($allowDuplicate || !array_key_exists($requestItem, $jsFiles))
$printList[] = $requestItem;
$jsFiles[$requestItem] = false;
}
print("// loading requested files:n");
foreach ($jsFiles as $key => $value) {
$filename = "{$key}.js";
printf("// ./%s ", $filename);
if (file_exists($filename) && is_file($filename)) {
$jsFiles[$key] = jsCompress(file_get_contents($filename), $compression);
printf("(md5: %s)n", md5($jsFiles[$key]));
} else {
printf("%sn", 'not found');
}
}
// Print Javascript contents
for ($i = 0; $i < count($printList); ++$i) {
$key = $printList[$i];
$content = $jsFiles[$key];
printf("n// %sn%sn", $key, $content);
}
// Program ends
die();
Categories