-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcos-html-cache.php
378 lines (338 loc) · 12.5 KB
/
cos-html-cache.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
<?php
/*
Plugin Name: cos-html-cache
Plugin URI: http://www.storyday.com/tag/cos-html-cache
Description: cos-html-cache is an extremely efficient WordPress page caching plugin designed to make your WordPress site much faster and more responsive. Based on URL rewriting, the plugin will automatically generate real html files for posts when they are loaded for the first time, and automatically renew the html files if their associated posts are modified.
cos-html-cache. Current version, cos-html-cache2.6, is a huge improvement over previous versions of cos-html-cache.
Version: 2.7.4
Author: jiangdong
date:2007-07-19
Author URI:http://www.storyday.com
*/
/*
Change log:
2007-06-02: added custom cookie to fix Chinese charactor problems
2007-06-03: added page cache function
2007-06-24: fixed js bugs of chinese display
2007-07-25: changedd the cache merchanism
2007-08-14: changed the comment js
2008-02-21: fixed database crush error
2008-04-06: Compatible for wordpress2.5
2008-07-18: Compatible for wordpress2.6 solved the cookie problems
2008-12-20: fixed admin cookie httponly problems
2009-03-04: fixed cookie '+' problems
2009-03-15: remove cache for password protected posts & fixed some js problems
2009-03-24: remove comment user cache data
2012-09-19: cache remove bug fixed
*/
/* config */
define('IS_INDEX',true);// false = do not create home page cache
/*end of config*/
define('COSVERSION','2.7.4');
require_once(ABSPATH . 'wp-admin/includes/file.php');
/* end of config */
$sm_locale = get_locale();
$sm_mofile = dirname(__FILE__) . "/cosbeta-$sm_locale.mo";
load_textdomain('cosbeta', $sm_mofile);
$cossithome = get_option('home');
$script_uri = rtrim( "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"] ,"/");
$home_path = get_home_path();
date_default_timezone_set('PRC');
define('SCRIPT_URI',$script_uri);
define('CosSiteHome',$cossithome);
define('CosBlogPath', $home_path);
define("COSMETA","<!--this is a real static html file created at ".date("Y-m-d H:i:s")." by cos-html-cache ".COSVERSION." -->");
function CreateHtmlFile($FilePath,$Content){
$FilePath = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $FilePath);
// if there is http:// $FilePath will return its bas path
$dir_array = explode("/",$FilePath);
//split the FilePath
$max_index = count($dir_array) ;
$i = 0;
//$path = $_SERVER['DOCUMENT_ROOT']."/";
$path = $_SERVER['HTTP_HOST']."/";
while( $i < $max_index ){
$path .= "/".$dir_array[$i];
$path = str_replace("//","/",$path);
if( $dir_array[$i] == "" ){
$i ++ ;
continue;
}
if( substr_count($path, '&') ) return true;
if( substr_count($path, '?') ) return true;
// if( !substr_count($path, '.htm') ){
// //if is a directory
// if( !file_exists( $path ) ){
// @mkdir( $path, 0777);
// @chmod( $path, 0777 );
// }
// }
$i ++;
}
$path = 'http://'.$path;
if( is_dir( $path ) ){
$path = $path."/index.html";
}
if ( !strstr( strtolower($Content), '</html>' ) ) return;
$kv=new SaeKV();
$kv->init();
$kv->set($path,$Content);
//if sql error ignore...
// $fp = @fopen( $path , "w+" );
// if( $fp ){
// @chmod($path, 0666 ) ;
// @flock($fp ,LOCK_EX );
// // write the file。
// fwrite( $fp , $Content );
// @flock($fp, LOCK_UN);
// fclose($fp);
// }
}
/* read the content from output buffer */
$is_buffer = false;
if( substr_count($_SERVER['REQUEST_URI'], '.htm') || ( SCRIPT_URI == CosSiteHome) ){
if( strlen( $_COOKIE['wordpress_logged_in_'.COOKIEHASH] ) < 4 ){
$is_buffer = true;
}
if( substr_count($_SERVER['REQUEST_URI'], '?')) $is_buffer = false;
if( substr_count($_SERVER['REQUEST_URI'], '../')) $is_buffer = false;
}
if( $is_buffer ){
ob_start('cos_cache_ob_callback');
register_shutdown_function('cos_cache_shutdown_callback');
}
function cos_cache_ob_callback($buffer){
$buffer = preg_replace('/(<\s*input[^>]+?(name=["\']author[\'"])[^>]+?value=(["\']))([^"\']+?)\3/i', '\1\3', $buffer);
$buffer = preg_replace('/(<\s*input[^>]+?value=)([\'"])[^\'"]+\2([^>]+?name=[\'"]author[\'"])/i', '\1""\3', $buffer);
$buffer = preg_replace('/(<\s*input[^>]+?(name=["\']url[\'"])[^>]+?value=(["\']))([^"\']+?)\3/i', '\1\3', $buffer);
$buffer = preg_replace('/(<\s*input[^>]+?value=)([\'"])[^\'"]+\2([^>]+?name=[\'"]url[\'"])/i', '\1""\3', $buffer);
$buffer = preg_replace('/(<\s*input[^>]+?(name=["\']email[\'"])[^>]+?value=(["\']))([^"\']+?)\3/i', '\1\3', $buffer);
$buffer = preg_replace('/(<\s*input[^>]+?value=)([\'"])[^\'"]+\2([^>]+?name=[\'"]email[\'"])/i', '\1""\3', $buffer);
if( !substr_count($buffer, '<!--cos-html-cache-safe-tag-->') ) return $buffer;
if( substr_count($buffer, 'post_password') > 0 ) return $buffer;//to check if post password protected
$wppasscookie = "wp-postpass_".COOKIEHASH;
if( strlen( $_COOKIE[$wppasscookie] ) > 0 ) return $buffer;//to check if post password protected
/*
$comment_author_url='';
$comment_author_email='';
$comment_author='';*/
elseif( SCRIPT_URI == CosSiteHome) {// creat homepage
$kv=new SaeKV();
$kv->init();
//$kv->set(CosBlogPath.'index.bak',$buffer.COSMETA);
// $fp = @fopen( CosBlogPath."index.bak" , "w+" );
// if( $fp ){
// @flock($fp ,LOCK_EX );
// // write the file。
// fwrite( $fp , $buffer.COSMETA );
// @flock($fp, LOCK_UN);
// fclose($fp);
// }
if(IS_INDEX)
$kv->set(CosSiteHome.'/index.html',$buffer.COSMETA);
//@rename(CosBlogPath."index.bak",CosBlogPath."index.html");
}
else
CreateHtmlFile($_SERVER['REQUEST_URI'],$buffer.COSMETA );
return $buffer;
}
function cos_cache_shutdown_callback(){
ob_end_flush();
flush();
}
if( !function_exists('DelCacheByUrl') ){
function DelCacheByUrl($url) {
//$url = CosBlogPath.str_replace( CosSiteHome,"",$url );
//$url = str_replace("//","/", $url );
$kv=new SaeKV();
$kv->init();
if ($kv->get($url)) {
$kv->delete($url);
}
// if( file_exists( $url )){
// if( is_dir( $url )) {@unlink( $url."/index.html" );@rmdir($url);}
// else @unlink( $url );
// }
}
}
if( !function_exists('htmlCacheDel') ){
// create single html
function htmlCacheDel($post_ID) {
if( $post_ID == "" ) return true;
$uri = get_permalink($post_ID);
DelCacheByUrl($uri );
}
}
if( !function_exists('htmlCacheDelNb') ){
// delete nabour posts
function htmlCacheDelNb($post_ID) {
if( $post_ID == "" ) return true;
$uri = get_permalink($post_ID);
DelCacheByUrl($uri );
global $wpdb;
$postRes=$wpdb->get_results("SELECT `ID` FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type='post' AND ID < ".$post_ID." ORDER BY ID DESC LIMIT 0,1;");
$uri1 = get_permalink($postRes[0]->ID);
DelCacheByUrl($uri1 );
$postRes=$wpdb->get_results("SELECT `ID` FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type='post' AND ID > ".$post_ID." ORDER BY ID ASC LIMIT 0,1;");
if( $postRes[0]->ID != '' ){
$uri2 = get_permalink($postRes[0]->ID);
DelCacheByUrl($uri2 );
}
}
}
//create index.html
if( !function_exists('createIndexHTML') ){
function createIndexHTML($post_ID){
if( $post_ID == "" ) return true;
//[menghao]@rename(ABSPATH."index.html",ABSPATH."index.bak");
$kv=new SaeKV();
$kv->init();
if ($kv->get(CosSiteHome.'/index.html')) {
//$kv->set(CosBlogPath.'index.bak',$ret);
$kv->delete(CosSiteHome.'/index.html');
}
//@rename(CosBlogPath."index.html",CosBlogPath."index.bak");//[menghao]
}
}
if(!function_exists("htmlCacheDel_reg_admin")) {
/**
* Add the options page in the admin menu
*/
function htmlCacheDel_reg_admin() {
if (function_exists('add_options_page')) {
add_options_page('html-cache-creator', 'CosHtmlCache',8, basename(__FILE__), 'cosHtmlOption');
//add_options_page($page_title, $menu_title, $access_level, $file).
}
}
}
add_action('admin_menu', 'htmlCacheDel_reg_admin');
if(!function_exists("cosHtmlOption")) {
function cosHtmlOption(){
do_cos_html_cache_action();
?>
<div class="wrap" style="padding:10px 0 0 10px;text-align:left">
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<p>
<?php _e("Click the button bellow to delete all the html cache files","cosbeta");?></p>
<p><?php _e("Note:this will Not delete data from your databases","cosbeta");?></p>
<p><?php _e("If you want to rebuild all cache files, you should delete them first,and then the cache files will be built when post or page first visited","cosbeta");?></p>
<p><b><?php _e("specify a post ID or Title to to delete the related cache file","cosbeta");?></b> <input type="text" id="cache_id" name="cache_id" value="" /> <?php _e("Leave blank if you want to delete all caches","cosbeta");?></p>
<p><input type="submit" value="<?php _e("Delete Html Cache files","cosbeta");?>" id="htmlCacheDelbt" name="htmlCacheDelbt" onClick="return checkcacheinput(); " />
<p><input type="submit" value="删除首页" id="indexHtmlCacheDelbt" name="indexHtmlCacheDelbt" onClick="return checkcacheinput1(); " />
</form>
</div>
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkcacheinput(){
document.getElementById('htmlCacheDelbt').value = 'Please Wait...';
return true;
} function checkcacheinput1(){
document.getElementById('indexHtmlCacheDelbt').value = 'Please Wait...';
return true;
}
//-->
</SCRIPT>
<?php
}
}
/*
end of get url
*/
// deal with rebuild or delete
function do_cos_html_cache_action(){
if ($_POST['indexHtmlCacheDelbt']) {
$kv=new SaeKV();
$kv->init();
$ret=$kv->get(CosSiteHome.'/index.html');
if ($ret) {
if($kv->delete(CosSiteHome.'/index.html')){
$msg = __('Index Caches were deleted successfully','cosbeta');
}else{
$msg = '存在首页缓存,但是删除失败';
}
}else{
$msg = '不存在首页缓存';
}
}
if( !empty($_POST['htmlCacheDelbt']) ){
// @rename(CosBlogPath."index.html",CosBlogPath."index.bak");
// @chmod( CosBlogPath."index.bak", 0666 );
$kv=new SaeKV();
$kv->init();
$ret=$kv->get(CosSiteHome.'/index.html');
if ($ret) {
//$kv->set(CosBlogPath.'index.bak',$ret);
$kv->delete(CosSiteHome.'/index.html');
}
global $wpdb;
if( $_POST['cache_id'] * 1 > 0 ){
//delete cache by id
DelCacheByUrl(get_permalink($_POST['cache_id']));
$msg = __('the post cache was deleted successfully: ID=','cosbeta').$_POST['cache_id'];
}
else if( strlen($_POST['cache_id']) > 2 ){
$postRes=$wpdb->get_results("SELECT `ID` FROM `" . $wpdb->posts . "` WHERE post_title like '%".$_POST['cache_id']."%' LIMIT 0,1 ");
DelCacheByUrl( get_permalink( $postRes[0]->ID ) );
$msg = __('the post cache was deleted successfully: Title=','cosbeta').$_POST['cache_id'];
}
else{
$postRes=$wpdb->get_results("SELECT `ID` FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND ( post_type='post' OR post_type='page' ) ORDER BY post_modified DESC ");
foreach($postRes as $post) {
DelCacheByUrl(get_permalink($post->ID));
}
$msg = __('HTML Caches were deleted successfully','cosbeta');
}
}
if($msg)
echo '<div class="updated"><strong><p>'.$msg.'</p></strong></div>';
}
$is_add_comment_is = true;
/*
* with ajax comments
*/
if ( !function_exists("cos_comments_js") ){
function cos_comments_js($postID){
global $is_add_comment_is;
if( $is_add_comment_is ){
$is_add_comment_is = false;
?>
<script language="JavaScript" type="text/javascript" src="<?php echo CosSiteHome;?>/wp-content/plugins/cos-html-cache/common.js.php?hash=<?php echo COOKIEHASH;?>"></script>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
var hash = "<?php echo COOKIEHASH;?>";
var author_cookie = "comment_author_" + hash;
var email_cookie = "comment_author_email_" + hash;
var url_cookie = "comment_author_url_" + hash;
var adminmail = "<?php echo str_replace('@','{_}',get_option('admin_email'));?>";
var adminurl = "<?php echo get_option('siteurl') ;?>";
setCommForm();
//]]>
</script>
<?php
}
}
}
function CosSafeTag(){
if ( is_single() || (is_home() && IS_INDEX) ) {
echo "<!--cos-html-cache-safe-tag-->";
}
}
function clearCommentHistory(){
global $comment_author_url,$comment_author_email,$comment_author;
$comment_author_url='';
$comment_author_email='';
$comment_author='';
}
//add_action('comments_array','clearCommentHistory');
add_action('get_footer', 'CosSafeTag');
add_action('comment_form', 'cos_comments_js');
/* end of ajaxcomments*/
add_action('publish_post', 'htmlCacheDelNb');
add_action('delete_post', 'htmlCacheDelNb');
add_action('edit_post', 'htmlCacheDel');
if(IS_INDEX){
add_action('edit_post', 'createIndexHTML');
add_action('delete_post', 'createIndexHTML');
add_action('publish_post', 'createIndexHTML');
}
?>