-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine-jsmin.php
130 lines (107 loc) · 4.66 KB
/
combine-jsmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
// Written by Ed Eliot (www.ejeliot.com) - provided as-is, use at your own risk
/****************** start of config ******************/
define('FILE_TYPE', 'text/javascript'); // type of code we're outputting
define('CACHE_LENGTH', 31356000); // length of time to cache output file, default approx 1 year
define('CREATE_ARCHIVE', true); // set to false to suppress writing of code archive, files will be merged on each request
define('ARCHIVE_FOLDER', 'javascript/archive'); // location to store archive, don't add starting or trailing slashes
define('JSMIN_COMPRESS', true); // switch JSMin compression on or off
define('JSMIN_COMMENTS', ''); // any comments to append to the top of the compressed output
define('JSMIN_AS_LIB', true); // prevents auto-run on include
require_once("jsmin-1.1.0.php"); // get the JSMin library in
// files to merge
$aFiles = array(
'javascript/calendar.js',
'javascript/overlibmws.js',
'javascript/overlibmws_print.js',
'javascript/overlibmws_draggable.js',
'javascript/overlibmws_filter.js',
'javascript/overlibmws_bubble.js',
'javascript/collapse.js',
'javascript/inscription.js',
'javascript/forms_effect.js',
'javascript/util.js',
'javascript/ajax/autocomplete-3-2.js',
'javascript/autocomplete/prototype.js',
'javascript/autocomplete/effects.js',
'javascript/autocomplete/controls.js',
'javascript/autocomplete/voyage.js',
);
/****************** end of config ********************/
function Write($sFilename, $sCode) {
$oFile = fopen($sFilename, 'w');
if (flock($oFile, LOCK_EX)) {
fwrite($oFile, $sCode);
flock($oFile, LOCK_UN);
}
fclose($oFile);
}
/*
if etag parameter is present then the script is being called directly, otherwise we're including it in
another script with require or include. If calling directly we return code othewise we return the etag
representing the latest files
*/
if (isset($_GET['version'])) {
$iETag = (int)$_GET['version'];
$sLastModified = gmdate('D, d M Y H:i:s', $iETag).' GMT';
// see if the user has an updated copy in browser cache
if (
(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $sLastModified) ||
(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $iETag)
) {
header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified");
exit;
}
// create a directory for storing current and archive versions
if (CREATE_ARCHIVE && !is_dir(ARCHIVE_FOLDER)) {
mkdir(ARCHIVE_FOLDER);
}
$sMergedFilename = ARCHIVE_FOLDER."/$iETag.cache";
// get code from archive folder if it exists, otherwise grab latest files, merge and save in archive folder
if (CREATE_ARCHIVE && file_exists($sMergedFilename)) {
$sCode = file_get_contents($sMergedFilename);
} else {
// get and merge code
$sCode = '';
$aLastModifieds = array();
foreach ($aFiles as $sFile) {
$aLastModifieds[] = filemtime($sFile);
$sCode .= file_get_contents($sFile);
}
// sort dates, newest first
rsort($aLastModifieds);
if (CREATE_ARCHIVE) {
if ($iETag == $aLastModifieds[0]) { // check for valid etag, we don't want invalid requests to fill up archive folder
Write($sMergedFilename, $sCode);
if (JSMIN_COMPRESS) {
$sCode = JSMin::minify(file_get_contents($sMergedFilename));
Write($sMergedFilename, $sCode);
}
} else {
// archive file no longer exists or invalid etag specified
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
exit;
}
}
}
// send HTTP headers to ensure aggressive caching
header('Expires: '.gmdate('D, d M Y H:i:s', time() + CACHE_LENGTH).' GMT'); // 1 year from now
header('Content-Type: '.FILE_TYPE);
header('Content-Length: '.strlen($sCode));
header("Last-Modified: $sLastModified");
header("ETag: $iETag");
header('Cache-Control: max-age='.CACHE_LENGTH);
// output merged code
echo $sCode;
} else {
// get file last modified dates
$aLastModifieds = array();
foreach ($aFiles as $sFile) {
$aLastModifieds[] = filemtime($sFile);
}
// sort dates, newest first
rsort($aLastModifieds);
// output latest timestamp
echo $aLastModifieds[0];
}
?>