forked from Schepp/CSS-JS-Booster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp_booster.php
executable file
·403 lines (355 loc) · 17.8 KB
/
wp_booster.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/*
Plugin Name: CSS-JS-Booster
Plugin URI: http://github.com/Schepp/CSS-JS-Booster
Description: automates performance optimizing steps related to CSS, Media and Javascript linking/embedding.
Version: 0.3.7
Author: Christian "Schepp" Schaefer
Author URI: http://twitter.com/derSchepp
*/
/* Copyright 2010 Christian Schepp Schaefer (email : schaepp@gmx.de)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
include('booster_inc.php');
// Pre-2.6 compatibility
if(!defined('WP_CONTENT_URL')) define('WP_CONTENT_URL',get_option('siteurl').'/wp-content');
if(!defined('WP_CONTENT_DIR')) define('WP_CONTENT_DIR',ABSPATH.'wp-content');
if(!defined('WP_PLUGIN_URL')) define('WP_PLUGIN_URL',WP_CONTENT_URL.'/plugins');
if(!defined('WP_PLUGIN_DIR')) define('WP_PLUGIN_DIR',WP_CONTENT_DIR.'/plugins');
// Set Booster Cache Folder
define('BOOSTER_CACHE_DIR',str_replace('\\','/',dirname(__FILE__)).'/../../booster_cache');
function booster_htaccess() {
$wp_htacessfile = get_home_path().'.htaccess';
$booster_htacessfile = rtrim(str_replace('\\','/',realpath(dirname(__FILE__))),'/').'/htaccess/.htaccess';
if(file_exists($booster_htacessfile))
{
if(file_exists($wp_htacessfile) && is_writable($wp_htacessfile))
{
$wp_htacessfile_contents = file_get_contents($wp_htacessfile);
$wp_htacessfile_contents = preg_replace('/#CSS-JS-Booster Start#################################################.*#CSS-JS-Booster End#################################################/ims','',$wp_htacessfile_contents);
$wp_htacessfile_contents = $wp_htacessfile_contents.file_get_contents($booster_htacessfile);
}
else $wp_htacessfile_contents = file_get_contents($booster_htacessfile);
@file_put_contents($wp_htacessfile,$wp_htacessfile_contents);
}
@mkdir(BOOSTER_CACHE_DIR,0777);
@chmod(BOOSTER_CACHE_DIR,0777);
}
register_activation_hook(__FILE__,'booster_htaccess');
function booster_cleanup() {
// Remove entries from .htaccess
$wp_htacessfile = get_home_path().'.htaccess';
if(file_exists($wp_htacessfile) && is_writable($wp_htacessfile))
{
$wp_htacessfile_contents = file_get_contents($wp_htacessfile);
$wp_htacessfile_contents = preg_replace('/#CSS-JS-Booster Start#################################################.*#CSS-JS-Booster End#################################################/ims','',$wp_htacessfile_contents);
@file_put_contents($wp_htacessfile,$wp_htacessfile_contents);
}
// Remove all cache files
$handle=opendir(BOOSTER_CACHE_DIR);
while(false !== ($file = readdir($handle)))
{
if($file[0] != '.' && is_file(BOOSTER_CACHE_DIR.'/'.$file)) unlink(BOOSTER_CACHE_DIR.'/'.$file);
}
closedir($handle);
}
register_deactivation_hook(__FILE__,'booster_cleanup');
function booster_wp() {
// Dump output buffer
if($out = ob_get_contents())
{
// Check for right PHP version
if(strnatcmp(phpversion(),'5.0.0') >= 0)
{
$booster_cache_dir = BOOSTER_CACHE_DIR;
$js_plain = '';
$booster_out = '';
$booster_folder = explode('/',rtrim(str_replace('\\','/',realpath(dirname(__FILE__))),'/'));
$booster_folder = $booster_folder[count($booster_folder) - 1];
$booster = new Booster();
if(!is_dir($booster_cache_dir))
{
@mkdir($booster_cache_dir,0777);
@chmod($booster_cache_dir,0777);
}
if(is_dir($booster_cache_dir) && is_writable($booster_cache_dir) && substr(decoct(fileperms($booster_cache_dir)),1) == "0777")
{
$booster_cache_reldir = $booster->getpath(str_replace('\\','/',realpath($booster_cache_dir)),str_replace('\\','/',dirname(__FILE__)));
}
else
{
$booster_cache_dir = rtrim(str_replace('\\','/',dirname(__FILE__)),'/').'/../../booster_cache';
$booster_cache_reldir = '../../booster_cache';
}
$booster->booster_cachedir = $booster_cache_reldir;
$booster->js_minify = FALSE;
// Calculate relative path from root to Booster directory
$root_to_booster_path = $booster->getpath(str_replace('\\','/',dirname(__FILE__)),str_replace('\\','/',dirname(realpath(ABSPATH))));
if(preg_match_all('/<head.*<\/head>/ims',$out,$headtreffer,PREG_PATTERN_ORDER) > 0)
{
// Prevent processing of conditional comments
$headtreffer[0][0] = preg_replace('/<!--\[if.+?endif\]-->/ims','',$headtreffer[0][0]);
// CSS part
$css_rel_files = array();
// Start width inline-files
preg_match_all('/<style[^>]*>(.*?)<\/style>/ims',$headtreffer[0][0],$treffer,PREG_PATTERN_ORDER);
for($i=0;$i<count($treffer[0]);$i++)
{
// Get media-type
if(preg_match('/media=[\'"]*([^\'"]+)[\'"]*/ims',$treffer[0][$i],$mediatreffer))
{
$media = preg_replace('/[^a-z]+/i','',$mediatreffer[1]);
if(trim($media) == '') $media = 'all';
}
else $media = 'all';
// Save plain CSS to file to keep everything in line
$filename = $booster_cache_dir.'/'.md5($treffer[1][$i]).'_plain.css';
if(!file_exists($filename)) file_put_contents($filename,$treffer[1][$i]);
if(file_exists($filename))
{
@chmod($filename,0777);
// Calculate relative path from Booster to file
$booster_to_file_path = $booster->getpath($booster_cache_dir.'/',str_replace('\\','/',dirname(realpath(__FILE__))).'/');
$linkhref = get_option('siteurl').'/wp-content/plugins/'.$booster_folder.'/'.$booster_to_file_path.'/'.basename($filename);
$booster_cache_dir = $booster_cache_dir;
$linkcode = '<!-- Moved to file by Booster '.$treffer[0][$i].' --><link rel="stylesheet" media="'.$media.'" href="'.$linkhref.'" />';
$headtreffer[0][0] = str_replace($treffer[0][$i],$linkcode,$headtreffer[0][0]);
$out = str_replace($treffer[0][$i],$linkcode,$out);
}
else
{
$linkcode = '<!-- Failed to move inline-style to file '.$filename.' by Booster -->'.$treffer[0][$i];
$headtreffer[0][0] = str_replace($treffer[0][$i],$linkcode,$headtreffer[0][0]);
$out = str_replace($treffer[0][$i],$linkcode,$out);
}
}
// Continue with external files
preg_match_all('/<link[^>]*?href=[\'"]*?([^\'"]+?\.css)[\'"]*?[^>]*?>/ims',$headtreffer[0][0],$treffer,PREG_PATTERN_ORDER);
for($i=0;$i<count($treffer[0]);$i++)
{
// Get media-type
if(preg_match('/media=[\'"]*([^\'"]+)[\'"]*/ims',$treffer[0][$i],$mediatreffer))
{
$media = preg_replace('/[^a-z]+/i','',$mediatreffer[1]);
if(trim($media) == '') $media = 'all';
}
else $media = 'all';
// Get relation
if(preg_match('/rel=[\'"]*([^\'"]+)[\'"]*/ims',$treffer[0][$i],$reltreffer)) $rel = $reltreffer[1];
else $rel = 'stylesheet';
// Convert file's URI into an absolute local path
$filename = preg_replace('/^http:\/\/[^\/]+/',rtrim($_SERVER['DOCUMENT_ROOT'],'/'),$treffer[1][$i]);
// Remove any parameters from file's URI
$filename = preg_replace('/\?.*$/','',$filename);
// If file exists
if(file_exists($filename))
{
// If its a normal CSS-file
if(substr($filename,strlen($filename) - 4,4) == '.css' && file_exists($filename))
{
// Put file-reference inside a comment
$out = str_replace($treffer[0][$i],'<!-- Processed by Booster '.$treffer[0][$i].' -->',$out);
// Calculate relative path from Booster to file
$booster_to_file_path = $booster->getpath(str_replace('\\','/',dirname($filename)),str_replace('\\','/',dirname(__FILE__)));
$filename = $booster_to_file_path.'/'.basename($filename);
// Create sub-arrays if not yet there
if(!isset($css_rel_files[$media])) $css_rel_files[$media] = array();
if(!isset($css_abs_files[$media])) $css_abs_files[$media] = array();
if(!isset($css_rel_files[$media][$rel])) $css_rel_files[$media][$rel] = array();
if(!isset($css_abs_files[$media][$rel])) $css_abs_files[$media][$rel] = array();
// Enqueue file to respective array
array_push($css_rel_files[$media][$rel],$filename);
array_push($css_abs_files[$media][$rel],rtrim(str_replace('\\','/',dirname(realpath(ABSPATH))),'/').'/'.$root_to_booster_path.'/'.$filename);
}
else $out = str_replace($treffer[0][$i],$treffer[0][$i].'<!-- Booster skipped '.$filename.' -->',$out);
}
// Leave untouched but put calculated local file name into a comment for debugging
else $out = str_replace($treffer[0][$i],$treffer[0][$i].'<!-- Booster had a problems finding '.$filename.' -->',$out);
}
// Creating Booster markup for each media and relation seperately
reset($css_rel_files);
for($i=0;$i<count($css_rel_files);$i++)
{
$media_rel = $css_rel_files[key($css_rel_files)];
$media_abs = $css_abs_files[key($css_rel_files)];
reset($media_rel);
for($j=0;$j<count($media_rel);$j++)
{
$booster->getfilestime($media_rel[key($media_rel)],'css');
$media_rel[key($media_rel)] = implode(',',$media_rel[key($media_rel)]);
$media_abs[key($media_rel)] = implode(',',$media_abs[key($media_rel)]);
$link = '<link type="text/css" rel="'.key($media_rel).
'" media="'.key($css_rel_files).
'" href="'.get_option('siteurl').'/wp-content/plugins/'.$booster_folder.'/booster_css.php/dir='.htmlentities(str_replace('..','%3E',$media_rel[key($media_rel)])).
'&cachedir='.htmlentities(str_replace('..','%3E',$booster_cache_reldir),ENT_QUOTES).
($booster->debug ? '&debug=1' : '').
($booster->librarydebug ? '&librarydebug=1' : '').
'&nocache='.$booster->filestime.'" />';
if(key($css_rel_files) != 'print')
{
$booster_out .= $link."\r\n";
}
else
{
$booster_out .= '<noscript>'.$link.'</noscript>'."\r\n";
$js_plain .= 'jQuery(document).ready(function () {
jQuery("head").append("'.addslashes($link).'");
});
';
}
$booster_out .= "\r\n";
#$booster_out .= "\r\n<!-- ".$media_abs[key($media_rel)]." -->\r\n";
next($media_rel);
}
next($css_rel_files);
}
// Injecting the result
$out = str_replace('</title>',"</title>\r\n<meta name=\"booster_cache_dir\" content=\"".BOOSTER_CACHE_DIR."\" />\r\n".$booster_out,$out);
$booster_out = '';
// JS-part
$js_rel_files = array();
$js_abs_files = array();
preg_match_all('/<script[^>]*>(.*?)<\/script>/ims',$headtreffer[0][0],$treffer,PREG_PATTERN_ORDER);
for($i=0;$i<count($treffer[0]);$i++)
{
if(preg_match('/<script.*?src=[\'"]*([^\'"]+\.js)[\'"]*.*?<\/script>/ims',$treffer[0][$i],$srctreffer))
{
// Get Domainname
if(isset($_SERVER['SCRIPT_URI']))
{
$host = parse_url($_SERVER['SCRIPT_URI'],PHP_URL_HOST);
}
else
{
$host = $_SERVER['HTTP_HOST'];
}
// Convert siteurl into a regex-safe expression
$host = str_replace(array('/','.'),array('\/','\.'),$host);
// Convert file's URI into an absolute local path
$filename = preg_replace('/^http:\/\/'.$host.'[^\/]*/',rtrim($_SERVER['DOCUMENT_ROOT'],'/'),$srctreffer[1]);
// If file is external
if(substr($filename,0,7) == 'http://')
{
$buffered_filename = $booster_cache_dir.'/'.md5($filename).'_buffered.js';
$parsed_url = parse_url($filename);
if(!file_exists($buffered_filename) || filemtime($buffered_filename) < (time() - (1 * 24 * 60 * 60)))
{
$host = $parsed_url['host'];
$service_uri = $parsed_url['path'];
$vars = $parsed_url['query'];
// Compose HTTP request header
$header = "Host: $host\r\n";
$header .= "User-Agent: CSS-JS-Booster\r\n";
$header .= "Connection: close\r\n\r\n";
$fp = fsockopen($host, 80, $errno, $errstr);
if($fp)
{
$body = '';
fputs($fp,"GET $service_uri?$vars HTTP/1.0\r\n");
fputs($fp,$header.$vars);
while (!feof($fp)) {
$body .= fgets($fp,65000);
}
fclose($fp);
$body = preg_replace('/HTTP.+[\r\n]{1}[\r\n]{1}/ims','',$body);
@file_put_contents($buffered_filename,$body);
@chmod($filename,0777);
// Put file-reference inside a comment
$out = str_replace($srctreffer[0],'<!-- Processed by Booster '.$srctreffer[0].' -->',$out);
// Enqueue file to array
$booster_to_file_path = $booster->getpath(str_replace('\\','/',dirname($buffered_filename)),str_replace('\\','/',dirname(__FILE__)));
array_push($js_rel_files,$booster_cache_reldir.'/'.md5($filename).'_buffered.js');
array_push($js_abs_files,rtrim(str_replace('\\','/',dirname(realpath(ABSPATH))),'/').'/'.$buffered_filename);
}
// Leave untouched but put calculated local file name into a comment for debugging
else $out = str_replace($srctreffer[0],$srctreffer[0].'<!-- Booster had a problems retrieving '.$filename.' -->',$out);
}
else
{
// Put file-reference inside a comment
$out = str_replace($srctreffer[0],'<!-- Processed by Booster '.$srctreffer[0].' -->',$out);
// Enqueue file to array
$booster_to_file_path = $booster->getpath(str_replace('\\','/',dirname($buffered_filename)),str_replace('\\','/',dirname(__FILE__)));
array_push($js_rel_files,$booster_cache_reldir.'/'.md5($filename).'_buffered.js');
array_push($js_abs_files,rtrim(str_replace('\\','/',dirname(realpath(ABSPATH))),'/').'/'.$buffered_filename);
}
}
// If file is internal and does exist
elseif(file_exists($filename))
{
// If its a normal JavaScript-file
if(substr($filename,strlen($filename) - 3,3) == '.js')
{
// Remove any parameters from file's URI
$filename = preg_replace('/\?.*$/','',$filename);
// Put file-reference inside a comment
$out = str_replace($srctreffer[0],'<!-- Processed by Booster '.$srctreffer[0].' -->',$out);
// Calculate relative path from Booster to file
$booster_to_file_path = $booster->getpath(str_replace('\\','/',dirname($filename)),str_replace('\\','/',dirname(__FILE__)));
$filename = $booster_to_file_path.'/'.basename($filename);
// Enqueue file to array
array_push($js_rel_files,$filename);
array_push($js_abs_files,rtrim(str_replace('\\','/',dirname(realpath(ABSPATH))),'/').'/'.$root_to_booster_path.'/'.$filename);
}
else $out = str_replace($srctreffer[0],$srctreffer[0].'<!-- Booster skipped '.$filename.' -->',$out);
}
// Leave untouched but put calculated local file name into a comment for debugging
else $out = str_replace($srctreffer[0],$srctreffer[0].'<!-- Booster had a problems finding '.$filename.' -->',$out);
}
else
{
// Save plain JS to file to keep everything in line
$filename = $booster_cache_dir.'/'.md5($treffer[1][$i]).'_plain.js';
if(!file_exists($filename)) @file_put_contents($filename,$treffer[1][$i]);
@chmod($filename,0777);
// Enqueue file to array
$booster_to_file_path = $booster->getpath(str_replace('\\','/',dirname($filename)),str_replace('\\','/',dirname(__FILE__)));
array_push($js_rel_files,$booster_cache_reldir.'/'.md5($treffer[1][$i]).'_plain.js');
#array_push($js_rel_files,$booster_cache_reldir.'/'.md5($treffer[1][$i]).'_plain.js');
array_push($js_abs_files,rtrim(str_replace('\\','/',dirname(realpath(ABSPATH))),'/').'/'.$filename);
//$js_plain .= "try{".$treffer[1][$i];
$out = str_replace($treffer[0][$i],'<!-- '.$treffer[0][$i].' -->',$out);
}
}
// Creating Booster markup
$js_rel_files = implode(',',$js_rel_files);
$js_abs_files = implode(',',$js_abs_files);
$js_plain = preg_replace('/\/\*.*?\*\//ims','',$js_plain);
$js_plain .= 'try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {}
';
$booster_out .= '<script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/'.$booster_folder.'/booster_js.php/dir='.htmlentities(str_replace('..','%3E',$js_rel_files)).'&cachedir='.htmlentities(str_replace('..','%3E',$booster_cache_reldir),ENT_QUOTES).(($booster->debug) ? '&debug=1' : '').((!$booster->js_minify) ? '&js_minify=0' : '').'&nocache='.$booster->getfilestime($js_abs_files,'js').'"></script>
<script type="text/javascript">'.$js_plain.'</script>';
$booster_out .= "\r\n";
#$booster_out .= "\r\n<!-- ".$js_abs_files." -->\r\n";
// Injecting the result
$out = str_replace('</head>',$booster_out.'</head>',$out);
}
}
else $out = str_replace('<body','<div style="display: block; padding: 1em; background-color: #FFF9D0; color: #912C2C; border: 1px solid #912C2C; font-family: Calibri, \'Lucida Grande\', Arial, Verdana, sans-serif; white-space: pre;">You need to upgrade to PHP 5 or higher to have CSS-JS-Booster work. You currently are running on PHP '.phpversion().'</div><body',$out);
// Recreate output buffer
ob_end_clean();
if (
isset($_SERVER['HTTP_ACCEPT_ENCODING'])
&& substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')
&& function_exists('ob_gzhandler')
&& (!ini_get('zlib.output_compression') || intval(ini_get('zlib.output_compression')) <= 0)
&& !function_exists('wp_cache_ob_callback')
) @ob_start('ob_gzhandler');
elseif(function_exists('wp_cache_ob_callback')) @ob_start('wp_cache_ob_callback');
else @ob_start();
// Output page
echo $out;
}
}
add_action('wp_footer','booster_wp');
?>