1
+ − 1
<?php
+ − 2
+ − 3
/*
+ − 4
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
536
+ − 5
* Version 1.1.4 (Caoineag alpha 4)
+ − 6
* Copyright (C) 2006-2008 Dan Fuhry
1
+ − 7
* jsres.php - the Enano client-side runtime, a.k.a. AJAX on steroids
+ − 8
*
+ − 9
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ − 10
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ − 11
*
+ − 12
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ − 13
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ − 14
*/
+ − 15
430
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 16
// Disable for IE, it causes problems.
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 17
if ( strstr(@$_SERVER['HTTP_USER_AGENT'], 'MSIE') )
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 18
{
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 19
header('HTTP/1.1 302 Redirect');
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 20
header('Location: static/enano-lib-basic.js');
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 21
exit();
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 22
}
ec90736b9cb9
Started (but disabled) work on the new theme manager, 1.1.2 is being released with this thing halfway done.
Dan
diff
changeset
+ − 23
420
+ − 24
// Setup Enano
1
+ − 25
420
+ − 26
//
+ − 27
// Determine the location of Enano as an absolute path.
+ − 28
//
1
+ − 29
420
+ − 30
// We need to see if this is a specially marked Enano development server. You can create an Enano
+ − 31
// development server by cloning the Mercurial repository into a directory named repo, and then
+ − 32
// using symlinks to reference the original files so as to segregate unique files from non-unique
+ − 33
// and distribution-standard ones. Enano will pivot its root directory accordingly if the file
+ − 34
// .enanodev is found in the Enano root (not /repo/).
+ − 35
if ( strpos(__FILE__, '/repo/') && ( file_exists('../../.enanodev') || file_exists('../../../.enanodev') ) )
+ − 36
{
+ − 37
// We have a development directory. Remove /repo/ from the picture.
+ − 38
$filename = str_replace('/repo/', '/', __FILE__);
+ − 39
}
+ − 40
else
+ − 41
{
+ − 42
// Standard Enano installation
+ − 43
$filename = __FILE__;
+ − 44
}
1
+ − 45
420
+ − 46
// ENANO_ROOT is sometimes defined by plugins like AjIM that need the constant before the Enano API is initialized
+ − 47
if ( !defined('ENANO_ROOT') )
+ − 48
define('ENANO_ROOT', dirname(dirname(dirname($filename))));
+ − 49
+ − 50
chdir(ENANO_ROOT);
+ − 51
+ − 52
// CONFIG
1
+ − 53
420
+ − 54
// Files safe to run full (aggressive) compression on
421
+ − 55
$full_compress_safe = array(
+ − 56
// Sorted by file size, descending (du -b *.js | sort -n)
436
+ − 57
'libbigint.js',
421
+ − 58
'ajax.js',
+ − 59
'editor.js',
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 60
'login.js',
421
+ − 61
'acl.js',
+ − 62
'misc.js',
+ − 63
'comments.js',
+ − 64
'rijndael.js',
+ − 65
'autofill.js',
+ − 66
'dropdown.js',
+ − 67
'paginate.js',
+ − 68
'autocomplete.js',
+ − 69
'md5.js',
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 70
'enano-lib-basic.js',
437
+ − 71
'pwstrength.js',
436
+ − 72
'sha256.js',
421
+ − 73
'flyin.js',
559
+ − 74
'rank-manager.js',
421
+ − 75
'template-compiler.js',
436
+ − 76
'toolbar.js',
+ − 77
'diffiehellman.js',
+ − 78
'enanomath.js'
421
+ − 79
);
420
+ − 80
+ − 81
// Files that should NOT be compressed due to already being compressed, licensing, or invalid produced code
421
+ − 82
$compress_unsafe = array('SpryEffects.js', 'json.js', 'fat.js', 'admin-menu.js');
420
+ − 83
+ − 84
require('includes/functions.php');
+ − 85
require('includes/json2.php');
+ − 86
require('includes/js-compressor.php');
+ − 87
542
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 88
// try to gzip the output
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 89
$do_gzip = false;
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 90
if ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) )
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 91
{
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 92
$acceptenc = str_replace(' ', '', strtolower($_SERVER['HTTP_ACCEPT_ENCODING']));
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 93
$acceptenc = explode(',', $acceptenc);
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 94
if ( in_array('gzip', $acceptenc) )
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 95
{
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 96
$do_gzip = true;
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 97
ob_start();
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 98
}
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 99
}
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
+ − 100
420
+ − 101
// Output format will always be JS
+ − 102
header('Content-type: text/javascript');
+ − 103
$everything = '';
1
+ − 104
420
+ − 105
// Load and parse enano_lib_basic
+ − 106
$file = @file_get_contents('includes/clientside/static/enano-lib-basic.js');
+ − 107
+ − 108
$pos_start_includes = strpos($file, '/*!START_INCLUDER*/');
+ − 109
$pos_end_includes = strpos($file, '/*!END_INCLUDER*/');
1
+ − 110
420
+ − 111
if ( !$pos_start_includes || !$pos_end_includes )
+ − 112
{
+ − 113
die('// Error: enano-lib-basic does not have required metacomments');
+ − 114
}
1
+ − 115
420
+ − 116
$pos_end_includes += strlen('/*!END_INCLUDER*/');
+ − 117
+ − 118
preg_match('/var thefiles = (\[([^\]]+?)\]);/', $file, $match);
+ − 119
+ − 120
if ( empty($match) )
+ − 121
die('// Error: could not retrieve file list from enano-lib-basic');
1
+ − 122
420
+ − 123
// Decode file list
+ − 124
try
+ − 125
{
+ − 126
$file_list = enano_json_decode($match[1]);
+ − 127
}
+ − 128
catch ( Exception $e )
1
+ − 129
{
420
+ − 130
die("// Exception caught during file list parsing");
+ − 131
}
+ − 132
+ − 133
$apex = filemtime('includes/clientside/static/enano-lib-basic.js');
+ − 134
+ − 135
$before_includes = substr($file, 0, $pos_start_includes);
+ − 136
$after_includes = substr($file, $pos_end_includes);
+ − 137
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 138
// compress enano-lib-basic
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 139
$libbasic = "$before_includes\n$after_includes";
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 140
$libbasic = jsres_cache_check('enano-lib-basic.js', $libbasic);
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 141
$everything .= $libbasic;
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 142
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 143
// $everything .= $before_includes;
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 144
// $everything .= $after_includes;
1
+ − 145
420
+ − 146
foreach ( $file_list as $js_file )
+ − 147
{
+ − 148
$file_contents = file_get_contents("includes/clientside/static/$js_file");
+ − 149
$time = filemtime("includes/clientside/static/$js_file");
+ − 150
if ( $time > $apex )
+ − 151
$apex = $time;
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 152
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 153
$file_contents = jsres_cache_check($js_file, $file_contents);
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 154
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 155
$everything .= "\n\n// $js_file\n";
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 156
$everything .= "\n" . $file_contents;
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 157
}
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 158
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 159
// generate ETag
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 160
$etag = base64_encode(hexdecode(sha1($everything)));
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 161
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 162
if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) )
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 163
{
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 164
if ( "\"$etag\"" == $_SERVER['HTTP_IF_NONE_MATCH'] )
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 165
{
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 166
header('HTTP/1.1 304 Not Modified');
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 167
exit();
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 168
}
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 169
}
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 170
559
+ − 171
// generate expires header
+ − 172
$expires = date('r', mktime(-1, -1, -1, -1, -1, intval(date('y'))+1));
+ − 173
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 174
$everything = str_replace('/* JavaScriptCompressor 0.8 [www.devpro.it], thanks to Dean Edwards for idea [dean.edwards.name] */' . "\r\n", '', $everything);
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 175
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 176
$date = date('r', $apex);
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 177
header("Date: $date");
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 178
header("Last-Modified: $date");
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 179
header("ETag: \"$etag\"");
559
+ − 180
header("Expires: $expires");
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 181
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 182
echo $everything;
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 183
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 184
if ( $do_gzip )
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 185
{
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 186
gzip_output();
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 187
}
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 188
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 189
/**
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 190
* Check the cache for the given JS file and return the best-compressed version.
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 191
* @param string Javascript file (acl.js)
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 192
* @param string Default/current contents
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 193
* @return string
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 194
*/
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 195
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 196
function jsres_cache_check($js_file, $file_contents)
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 197
{
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 198
global $full_compress_safe, $compress_unsafe;
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 199
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 200
$file_md5 = md5($file_contents);
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 201
420
+ − 202
// Is this file cached?
+ − 203
$cache_path = ENANO_ROOT . "/cache/jsres_$js_file.json";
+ − 204
$loaded_cache = false;
1
+ − 205
420
+ − 206
if ( file_exists($cache_path) )
1
+ − 207
{
420
+ − 208
// Load the cache file and parse it.
+ − 209
$cache_file = file_get_contents($cache_path);
+ − 210
try
+ − 211
{
+ − 212
$cache_file = enano_json_decode($cache_file);
+ − 213
}
+ − 214
catch ( Exception $e )
+ − 215
{
+ − 216
// Don't do anything - let our fallbacks come into place
+ − 217
}
+ − 218
if ( is_array($cache_file) && isset($cache_file['md5']) && isset($cache_file['src']) )
1
+ − 219
{
420
+ − 220
if ( $cache_file['md5'] === $file_md5 )
+ − 221
{
+ − 222
$loaded_cache = true;
+ − 223
$file_contents = $cache_file['src'];
+ − 224
}
+ − 225
}
+ − 226
}
+ − 227
if ( !$loaded_cache )
+ − 228
{
+ − 229
// Try to open the cache file and write to it. If we can't do that, just don't compress the code.
+ − 230
$handle = @fopen($cache_path, 'w');
+ − 231
if ( $handle )
+ − 232
{
+ − 233
$aggressive = in_array($js_file, $full_compress_safe);
+ − 234
if ( !in_array($js_file, $compress_unsafe) )
+ − 235
$file_contents = perform_js_compress($file_contents, $aggressive);
+ − 236
+ − 237
$payload = enano_json_encode(array(
+ − 238
'md5' => $file_md5,
+ − 239
'src' => $file_contents
+ − 240
));
+ − 241
fwrite($handle, $payload);
+ − 242
fclose($handle);
1
+ − 243
}
+ − 244
}
+ − 245
555
ac4c6a7f01d8
Added user preference for disabling visual effects in Javascript applets; added re-import button to installed plugins
Dan
diff
changeset
+ − 246
return $file_contents;
1
+ − 247
}
420
+ − 248