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',
|
|
60 |
'acl.js',
|
|
61 |
'misc.js',
|
|
62 |
'comments.js',
|
|
63 |
'rijndael.js',
|
|
64 |
'autofill.js',
|
|
65 |
'dropdown.js',
|
|
66 |
'paginate.js',
|
|
67 |
'autocomplete.js',
|
|
68 |
'md5.js',
|
437
|
69 |
'pwstrength.js',
|
436
|
70 |
'sha256.js',
|
421
|
71 |
'flyin.js',
|
|
72 |
'template-compiler.js',
|
436
|
73 |
'toolbar.js',
|
|
74 |
'diffiehellman.js',
|
|
75 |
'enanomath.js'
|
421
|
76 |
);
|
420
|
77 |
|
|
78 |
// Files that should NOT be compressed due to already being compressed, licensing, or invalid produced code
|
421
|
79 |
$compress_unsafe = array('SpryEffects.js', 'json.js', 'fat.js', 'admin-menu.js');
|
420
|
80 |
|
|
81 |
require('includes/functions.php');
|
|
82 |
require('includes/json2.php');
|
|
83 |
require('includes/js-compressor.php');
|
|
84 |
|
542
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
85 |
// 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
|
86 |
$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
|
87 |
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
|
88 |
{
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
89 |
$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
|
90 |
$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
|
91 |
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
|
92 |
{
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
93 |
$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
|
94 |
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
|
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 |
}
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
97 |
|
420
|
98 |
// Output format will always be JS
|
|
99 |
header('Content-type: text/javascript');
|
|
100 |
$everything = '';
|
1
|
101 |
|
420
|
102 |
// Load and parse enano_lib_basic
|
|
103 |
$file = @file_get_contents('includes/clientside/static/enano-lib-basic.js');
|
|
104 |
|
|
105 |
$pos_start_includes = strpos($file, '/*!START_INCLUDER*/');
|
|
106 |
$pos_end_includes = strpos($file, '/*!END_INCLUDER*/');
|
1
|
107 |
|
420
|
108 |
if ( !$pos_start_includes || !$pos_end_includes )
|
|
109 |
{
|
|
110 |
die('// Error: enano-lib-basic does not have required metacomments');
|
|
111 |
}
|
1
|
112 |
|
420
|
113 |
$pos_end_includes += strlen('/*!END_INCLUDER*/');
|
|
114 |
|
|
115 |
preg_match('/var thefiles = (\[([^\]]+?)\]);/', $file, $match);
|
|
116 |
|
|
117 |
if ( empty($match) )
|
|
118 |
die('// Error: could not retrieve file list from enano-lib-basic');
|
1
|
119 |
|
420
|
120 |
// Decode file list
|
|
121 |
try
|
|
122 |
{
|
|
123 |
$file_list = enano_json_decode($match[1]);
|
|
124 |
}
|
|
125 |
catch ( Exception $e )
|
1
|
126 |
{
|
420
|
127 |
die("// Exception caught during file list parsing");
|
|
128 |
}
|
|
129 |
|
|
130 |
$apex = filemtime('includes/clientside/static/enano-lib-basic.js');
|
|
131 |
|
|
132 |
$before_includes = substr($file, 0, $pos_start_includes);
|
|
133 |
$after_includes = substr($file, $pos_end_includes);
|
|
134 |
|
|
135 |
$everything .= $before_includes;
|
|
136 |
$everything .= $after_includes;
|
1
|
137 |
|
420
|
138 |
foreach ( $file_list as $js_file )
|
|
139 |
{
|
|
140 |
$file_contents = file_get_contents("includes/clientside/static/$js_file");
|
|
141 |
$file_md5 = md5($file_contents);
|
|
142 |
$time = filemtime("includes/clientside/static/$js_file");
|
|
143 |
if ( $time > $apex )
|
|
144 |
$apex = $time;
|
|
145 |
// Is this file cached?
|
|
146 |
$cache_path = ENANO_ROOT . "/cache/jsres_$js_file.json";
|
|
147 |
$loaded_cache = false;
|
1
|
148 |
|
420
|
149 |
if ( file_exists($cache_path) )
|
1
|
150 |
{
|
420
|
151 |
// Load the cache file and parse it.
|
|
152 |
$cache_file = file_get_contents($cache_path);
|
|
153 |
try
|
|
154 |
{
|
|
155 |
$cache_file = enano_json_decode($cache_file);
|
|
156 |
}
|
|
157 |
catch ( Exception $e )
|
|
158 |
{
|
|
159 |
// Don't do anything - let our fallbacks come into place
|
|
160 |
}
|
|
161 |
if ( is_array($cache_file) && isset($cache_file['md5']) && isset($cache_file['src']) )
|
1
|
162 |
{
|
420
|
163 |
if ( $cache_file['md5'] === $file_md5 )
|
|
164 |
{
|
|
165 |
$loaded_cache = true;
|
|
166 |
$file_contents = $cache_file['src'];
|
|
167 |
}
|
|
168 |
}
|
|
169 |
}
|
|
170 |
if ( !$loaded_cache )
|
|
171 |
{
|
|
172 |
// Try to open the cache file and write to it. If we can't do that, just don't compress the code.
|
|
173 |
$handle = @fopen($cache_path, 'w');
|
|
174 |
if ( $handle )
|
|
175 |
{
|
|
176 |
$aggressive = in_array($js_file, $full_compress_safe);
|
|
177 |
if ( !in_array($js_file, $compress_unsafe) )
|
|
178 |
$file_contents = perform_js_compress($file_contents, $aggressive);
|
|
179 |
|
|
180 |
$payload = enano_json_encode(array(
|
|
181 |
'md5' => $file_md5,
|
|
182 |
'src' => $file_contents
|
|
183 |
));
|
|
184 |
fwrite($handle, $payload);
|
|
185 |
fclose($handle);
|
1
|
186 |
}
|
|
187 |
}
|
|
188 |
|
420
|
189 |
$everything .= "\n // $js_file\n";
|
|
190 |
$everything .= "\n" . $file_contents;
|
1
|
191 |
}
|
420
|
192 |
|
542
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
193 |
// generate ETag
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
194 |
$etag = base64_encode(hexdecode(sha1($everything)));
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
195 |
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
196 |
if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) )
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
197 |
{
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
198 |
if ( "\"$etag\"" == $_SERVER['HTTP_IF_NONE_MATCH'] )
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
199 |
{
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
200 |
header('HTTP/1.1 304 Not Modified');
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
201 |
exit();
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
202 |
}
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
203 |
}
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
204 |
|
420
|
205 |
$date = date('r', $apex);
|
|
206 |
header("Date: $date");
|
|
207 |
header("Last-Modified: $date");
|
542
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
208 |
header("ETag: \"$etag\"");
|
420
|
209 |
|
|
210 |
echo $everything;
|
|
211 |
|
542
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
212 |
if ( $do_gzip )
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
213 |
{
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
214 |
gzip_output();
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
215 |
}
|
5841df0ab575
Added ETag support and increased caching settings to try and speed the system up. Result of a YSlow audit.
Dan
diff
changeset
|
216 |
|