PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/cos-html-cache/cos-html-cache.php

http://suziwen.googlecode.com/
PHP | 332 lines | 229 code | 40 blank | 63 comment | 54 complexity | 9fcfb34fb22586482c58ef77553562be MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: cos-html-cache
  4. Plugin URI: http://www.storyday.com/html/y2007/1213_cos-html-cache-2cos-html-cache-2.html
  5. 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.
  6. cos-html-cache. Current version, cos-html-cache2.6, is a huge improvement over previous versions of cos-html-cache.
  7. Version: 2.7.3
  8. Author: jiangdong
  9. date:2007-07-19
  10. Author URI:http://www.storyday.com
  11. */
  12. /*
  13. Change log:
  14. 2007-06-02: added custom cookie to fix Chinese charactor problems
  15. 2007-06-03: added page cache function
  16. 2007-06-24: fixed js bugs of chinese display
  17. 2007-07-25: changedd the cache merchanism
  18. 2007-08-14: changed the comment js
  19. 2008-02-21: fixed database crush error
  20. 2008-04-06: Compatible for wordpress2.5
  21. 2008-07-18: Compatible for wordpress2.6 solved the cookie problems
  22. 2008-12-20: fixed admin cookie httponly problems
  23. 2009-03-04: fixed cookie '+' problems
  24. 2009-03-15: remove cache for password protected posts & fixed some js problems
  25. 2009-03-24: remove comment user cache data
  26. */
  27. /* config */
  28. define('IS_INDEX',true);// false = do not create home page cache
  29. /*end of config*/
  30. define('COSVERSION','2.7.3');
  31. require_once(ABSPATH . 'wp-admin/includes/file.php');
  32. /* end of config */
  33. $sm_locale = get_locale();
  34. $sm_mofile = dirname(__FILE__) . "/cosbeta-$sm_locale.mo";
  35. load_textdomain('cosbeta', $sm_mofile);
  36. $cossithome = get_option('home');
  37. $script_uri = rtrim( "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"] ,"/");
  38. $home_path = get_home_path();
  39. define('SCRIPT_URI',$script_uri);
  40. define('CosSiteHome',$cossithome);
  41. define('CosBlogPath', $home_path);
  42. define("COSMETA","<!--this is a real static html file created at ".date("Y-m-d H:i:s")." by cos-html-cache ".COSVERSION." -->");
  43. function CreateHtmlFile($FilePath,$Content){
  44. $FilePath = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $FilePath);
  45. // if there is http:// $FilePath will return its bas path
  46. $dir_array = explode("/",$FilePath);
  47. //split the FilePath
  48. $max_index = count($dir_array) ;
  49. $i = 0;
  50. $path = $_SERVER['DOCUMENT_ROOT']."/";
  51. while( $i < $max_index ){
  52. $path .= "/".$dir_array[$i];
  53. $path = str_replace("//","",$path);
  54. if( $dir_array[$i] == "" ){
  55. $i ++ ;
  56. continue;
  57. }
  58. if( substr_count($path, '&') ) return true;
  59. if( substr_count($path, '?') ) return true;
  60. if( !substr_count($path, '.htm') ){
  61. //if is a directory
  62. if( !file_exists( $path ) ){
  63. @mkdir( $path, 0777);
  64. @chmod( $path, 0777 );
  65. }
  66. }
  67. $i ++;
  68. }
  69. if( is_dir( $path ) ){
  70. $path = $path."/index.html";
  71. }
  72. if ( !strstr( strtolower($Content), '</html>' ) ) return;
  73. //if sql error ignore...
  74. $fp = @fopen( $path , "w+" );
  75. if( $fp ){
  76. @chmod($path, 0666 ) ;
  77. @flock($fp ,LOCK_EX );
  78. // write the file?
  79. fwrite( $fp , $Content );
  80. @flock($fp, LOCK_UN);
  81. fclose($fp);
  82. }
  83. }
  84. /* read the content from output buffer */
  85. $is_buffer = false;
  86. if( substr_count($_SERVER['REQUEST_URI'], '.htm') || ( SCRIPT_URI == CosSiteHome) ){
  87. if( strlen( $_COOKIE['wordpress_logged_in_'.COOKIEHASH] ) < 4 ){
  88. $is_buffer = true;
  89. }
  90. if( substr_count($_SERVER['REQUEST_URI'], '?')) $is_buffer = false;
  91. if( substr_count($_SERVER['REQUEST_URI'], '../')) $is_buffer = false;
  92. }
  93. if( $is_buffer ){
  94. ob_start('cos_cache_ob_callback');
  95. register_shutdown_function('cos_cache_shutdown_callback');
  96. }
  97. function cos_cache_ob_callback($buffer){
  98. $buffer = preg_replace('/(<\s*input[^>]+?(name=["\']author[\'"])[^>]+?value=(["\']))([^"\']+?)\3/i', '\1\3', $buffer);
  99. $buffer = preg_replace('/(<\s*input[^>]+?value=)([\'"])[^\'"]+\2([^>]+?name=[\'"]author[\'"])/i', '\1""\3', $buffer);
  100. $buffer = preg_replace('/(<\s*input[^>]+?(name=["\']url[\'"])[^>]+?value=(["\']))([^"\']+?)\3/i', '\1\3', $buffer);
  101. $buffer = preg_replace('/(<\s*input[^>]+?value=)([\'"])[^\'"]+\2([^>]+?name=[\'"]url[\'"])/i', '\1""\3', $buffer);
  102. $buffer = preg_replace('/(<\s*input[^>]+?(name=["\']email[\'"])[^>]+?value=(["\']))([^"\']+?)\3/i', '\1\3', $buffer);
  103. $buffer = preg_replace('/(<\s*input[^>]+?value=)([\'"])[^\'"]+\2([^>]+?name=[\'"]email[\'"])/i', '\1""\3', $buffer);
  104. if( !substr_count($buffer, '<!--cos-html-cache-safe-tag-->') ) return $buffer;
  105. if( substr_count($buffer, 'post_password') > 0 ) return $buffer;//to check if post password protected
  106. $wppasscookie = "wp-postpass_".COOKIEHASH;
  107. if( strlen( $_COOKIE[$wppasscookie] ) > 0 ) return $buffer;//to check if post password protected
  108. /*
  109. $comment_author_url='';
  110. $comment_author_email='';
  111. $comment_author='';*/
  112. elseif( SCRIPT_URI == CosSiteHome) {// creat homepage
  113. $fp = @fopen( CosBlogPath."index.bak" , "w+" );
  114. if( $fp ){
  115. @flock($fp ,LOCK_EX );
  116. // write the file?
  117. fwrite( $fp , $buffer.COSMETA );
  118. @flock($fp, LOCK_UN);
  119. fclose($fp);
  120. }
  121. if(IS_INDEX)
  122. @rename(CosBlogPath."index.bak",CosBlogPath."index.html");
  123. }
  124. else
  125. CreateHtmlFile($_SERVER['REQUEST_URI'],$buffer.COSMETA );
  126. return $buffer;
  127. }
  128. function cos_cache_shutdown_callback(){
  129. ob_end_flush();
  130. flush();
  131. }
  132. if( !function_exists('DelCacheByUrl') ){
  133. function DelCacheByUrl($url) {
  134. $url = CosBlogPath.str_replace( CosSiteHome,"",$url );
  135. $url = str_replace("//","/", $url );
  136. if( file_exists( $url )){
  137. if( is_dir( $url )) {@unlink( $url."/index.html" );@rmdir($url);}
  138. else @unlink( $url );
  139. }
  140. }
  141. }
  142. if( !function_exists('htmlCacheDel') ){
  143. // create single html
  144. function htmlCacheDel($post_ID) {
  145. if( $post_ID == "" ) return true;
  146. $uri = get_permalink($post_ID);
  147. DelCacheByUrl($uri );
  148. }
  149. }
  150. if( !function_exists('htmlCacheDelNb') ){
  151. // delete nabour posts
  152. function htmlCacheDelNb($post_ID) {
  153. if( $post_ID == "" ) return true;
  154. $uri = get_permalink($post_ID);
  155. DelCacheByUrl($uri );
  156. global $wpdb;
  157. $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;");
  158. $uri1 = get_permalink($postRes[0]->ID);
  159. DelCacheByUrl($uri1 );
  160. $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;");
  161. if( $postRes[0]->ID != '' ){
  162. $uri2 = get_permalink($postRes[0]->ID);
  163. DelCacheByUrl($uri2 );
  164. }
  165. }
  166. }
  167. //create index.html
  168. if( !function_exists('createIndexHTML') ){
  169. function createIndexHTML($post_ID){
  170. if( $post_ID == "" ) return true;
  171. //[menghao]@rename(ABSPATH."index.html",ABSPATH."index.bak");
  172. @rename(CosBlogPath."index.html",CosBlogPath."index.bak");//[menghao]
  173. }
  174. }
  175. if(!function_exists("htmlCacheDel_reg_admin")) {
  176. /**
  177. * Add the options page in the admin menu
  178. */
  179. function htmlCacheDel_reg_admin() {
  180. if (function_exists('add_options_page')) {
  181. add_options_page('html-cache-creator', 'CosHtmlCache',8, basename(__FILE__), 'cosHtmlOption');
  182. //add_options_page($page_title, $menu_title, $access_level, $file).
  183. }
  184. }
  185. }
  186. add_action('admin_menu', 'htmlCacheDel_reg_admin');
  187. if(!function_exists("cosHtmlOption")) {
  188. function cosHtmlOption(){
  189. do_cos_html_cache_action();
  190. ?>
  191. <div class="wrap" style="padding:10px 0 0 10px;text-align:left">
  192. <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
  193. <p>
  194. <?php _e("Click the button bellow to delete all the html cache files","cosbeta");?></p>
  195. <p><?php _e("Note:this will Not delete data from your databases","cosbeta");?></p>
  196. <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>
  197. <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>
  198. <p><input type="submit" value="<?php _e("Delete Html Cache files","cosbeta");?>" id="htmlCacheDelbt" name="htmlCacheDelbt" onClick="return checkcacheinput(); " />
  199. </form>
  200. </div>
  201. <SCRIPT LANGUAGE="JavaScript">
  202. <!--
  203. function checkcacheinput(){
  204. document.getElementById('htmlCacheDelbt').value = 'Please Wait...';
  205. return true;
  206. }
  207. //-->
  208. </SCRIPT>
  209. <?php
  210. }
  211. }
  212. /*
  213. end of get url
  214. */
  215. // deal with rebuild or delete
  216. function do_cos_html_cache_action(){
  217. if( !empty($_POST['htmlCacheDelbt']) ){
  218. @rename(CosBlogPath."index.html",CosBlogPath."index.bak");
  219. @chmod( CosBlogPath."index.bak", 0666 );
  220. global $wpdb;
  221. if( $_POST['cache_id'] * 1 > 0 ){
  222. //delete cache by id
  223. DelCacheByUrl(get_permalink($_POST['cache_id']));
  224. $msg = __('the post cache was deleted successfully: ID=','cosbeta').$_POST['cache_id'];
  225. }
  226. else if( strlen($_POST['cache_id']) > 2 ){
  227. $postRes=$wpdb->get_results("SELECT `ID` FROM `" . $wpdb->posts . "` WHERE post_title like '%".$_POST['cache_id']."%' LIMIT 0,1 ");
  228. DelCacheByUrl( get_permalink( $postRes[0]->ID ) );
  229. $msg = __('the post cache was deleted successfully: Title=','cosbeta').$_POST['cache_id'];
  230. }
  231. else{
  232. $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 ");
  233. foreach($postRes as $post) {
  234. DelCacheByUrl(get_permalink($post->ID));
  235. }
  236. $msg = __('HTML Caches were deleted successfully','cosbeta');
  237. }
  238. }
  239. if($msg)
  240. echo '<div class="updated"><strong><p>'.$msg.'</p></strong></div>';
  241. }
  242. $is_add_comment_is = true;
  243. /*
  244. * with ajax comments
  245. */
  246. if ( !function_exists("cos_comments_js") ){
  247. function cos_comments_js($postID){
  248. global $is_add_comment_is;
  249. if( $is_add_comment_is ){
  250. $is_add_comment_is = false;
  251. ?>
  252. <script language="JavaScript" type="text/javascript" src="<?php echo CosSiteHome;?>/wp-content/plugins/cos-html-cache/common.js.php?hash=<?php echo COOKIEHASH;?>"></script>
  253. <script language="JavaScript" type="text/javascript">
  254. //<![CDATA[
  255. var hash = "<?php echo COOKIEHASH;?>";
  256. var author_cookie = "comment_author_" + hash;
  257. var email_cookie = "comment_author_email_" + hash;
  258. var url_cookie = "comment_author_url_" + hash;
  259. var adminmail = "<?php echo str_replace('@','{_}',get_option('admin_email'));?>";
  260. var adminurl = "<?php echo get_option('siteurl') ;?>";
  261. setCommForm();
  262. //]]>
  263. </script>
  264. <?php
  265. }
  266. }
  267. }
  268. function CosSafeTag(){
  269. if ( is_single() || (is_home() && IS_INDEX) ) {
  270. echo "<!--cos-html-cache-safe-tag-->";
  271. }
  272. }
  273. function clearCommentHistory(){
  274. global $comment_author_url,$comment_author_email,$comment_author;
  275. $comment_author_url='';
  276. $comment_author_email='';
  277. $comment_author='';
  278. }
  279. //add_action('comments_array','clearCommentHistory');
  280. add_action('get_footer', 'CosSafeTag');
  281. add_action('comment_form', 'cos_comments_js');
  282. /* end of ajaxcomments*/
  283. if(IS_INDEX) add_action('publish_post', 'createIndexHTML');
  284. add_action('publish_post', 'htmlCacheDelNb');
  285. if(IS_INDEX) add_action('delete_post', 'createIndexHTML');
  286. add_action('delete_post', 'htmlCacheDelNb');
  287. //if comments add
  288. add_action('edit_post', 'htmlCacheDel');
  289. if(IS_INDEX) add_action('edit_post', 'createIndexHTML');
  290. ?>