PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-content/plugins/wp-super-cache/wp-cache-phase2.php

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 1097 lines | 968 code | 104 blank | 25 comment | 557 complexity | d0df4e09d81b9631b480cc408d57079d MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. function wp_cache_phase2() {
  3. global $wpsc_settings;
  4. global $cache_filename, $cache_acceptable_files, $wp_cache_gzip_encoding, $super_cache_enabled, $cache_rebuild_files, $wp_cache_last_gc;
  5. global $cache_max_time, $wp_cache_request_uri, $super_cache_enabled, $wp_cache_object_cache;
  6. global $cache_enabled, $wp_cache_gmt_offset, $wp_cache_blog_charset;
  7. if ( $cache_enabled == false )
  8. return false;
  9. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'In WP Cache Phase 2', 5 );
  10. $wp_cache_gmt_offset = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349
  11. $wp_cache_blog_charset = get_option( 'blog_charset' );
  12. wp_cache_mutex_init();
  13. if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) {
  14. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Setting up WordPress actions', 5 );
  15. // Post ID is received
  16. add_action('publish_post', 'wp_cache_post_edit', 0);
  17. add_action('edit_post', 'wp_cache_post_change', 0); // leaving a comment called edit_post
  18. add_action('delete_post', 'wp_cache_post_edit', 0);
  19. add_action('publish_phone', 'wp_cache_post_edit', 0);
  20. // Coment ID is received
  21. add_action('trackback_post', 'wp_cache_get_postid_from_comment', 99);
  22. add_action('pingback_post', 'wp_cache_get_postid_from_comment', 99);
  23. add_action('comment_post', 'wp_cache_get_postid_from_comment', 99);
  24. add_action('edit_comment', 'wp_cache_get_postid_from_comment', 99);
  25. add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 99, 2);
  26. // No post_id is available
  27. add_action('switch_theme', 'wp_cache_no_postid', 99);
  28. add_action('edit_user_profile_update', 'wp_cache_no_postid', 99);
  29. add_action( 'wp_update_nav_menu', 'wp_cache_clear_cache' );
  30. add_action('wp_cache_gc','wp_cache_gc_cron');
  31. do_cacheaction( 'add_cacheaction' );
  32. }
  33. if ( is_admin() ) {
  34. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching wp-admin requests.', 5 );
  35. return false;
  36. }
  37. if ( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option( 'gzipcompression' ) ) {
  38. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching POST request.', 5 );
  39. return false;
  40. }
  41. if ( $wp_cache_object_cache && !empty( $_GET ) ) {
  42. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching GET request while object cache storage enabled.', 5 );
  43. return false;
  44. }
  45. if ( isset( $_GET[ 'preview' ] ) ) {
  46. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching preview post.', 2 );
  47. return false;
  48. }
  49. if ( !empty( $_GET ) ) {
  50. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Supercache caching disabled. Only using wp-cache. Non empty GET request.', 5 );
  51. $super_cache_enabled = false;
  52. }
  53. $script = basename($_SERVER['PHP_SELF']);
  54. if (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri)) {
  55. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'URI rejected. Not Caching', 2 );
  56. return false;
  57. }
  58. if (wp_cache_user_agent_is_rejected()) {
  59. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "USER AGENT ({$_SERVER[ 'HTTP_USER_AGENT' ]}) rejected. Not Caching", 4 );
  60. return;
  61. }
  62. if($wp_cache_gzip_encoding)
  63. header('Vary: Accept-Encoding, Cookie');
  64. else
  65. header('Vary: Cookie');
  66. ob_start( 'wp_cache_ob_callback' );
  67. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Created output buffer', 4 );
  68. // restore old supercache file temporarily
  69. if( $super_cache_enabled && $cache_rebuild_files ) {
  70. $user_info = wp_cache_get_cookies_values();
  71. $do_cache = apply_filters( 'do_createsupercache', $user_info );
  72. if( $user_info == '' || $do_cache === true ) {
  73. $dir = get_current_url_supercache_dir();
  74. $files_to_check = array( $dir . 'index.html', $dir . 'index.html.php', $dir . 'index.html.gz' );
  75. foreach( $files_to_check as $cache_file ) {
  76. if( !@file_exists( $cache_file . '.needs-rebuild' ) )
  77. continue;
  78. $mtime = @filemtime($cache_file . '.needs-rebuild');
  79. if( $mtime && (time() - $mtime) < 30 ) {
  80. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file renamed to cache file temporarily", 3 );
  81. @rename( $cache_file . '.needs-rebuild', $cache_file );
  82. }
  83. // cleanup old files or if rename fails
  84. if( @file_exists( $cache_file . '.needs-rebuild' ) ) {
  85. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file deleted", 3 );
  86. @unlink( $cache_file . '.needs-rebuild' );
  87. }
  88. }
  89. }
  90. }
  91. if ( $cache_max_time == 0 )
  92. return false;
  93. if( !isset( $cache_max_time ) )
  94. $cache_max_time = 600;
  95. $last_gc = get_option( "wpsupercache_gc_time" );
  96. if( !$last_gc ) {
  97. update_option( 'wpsupercache_gc_time', time() );
  98. }
  99. $next_gc = $cache_max_time < 1800 ? $cache_max_time : 600;
  100. if( $last_gc < ( time() - $next_gc ) ) {
  101. update_option( 'wpsupercache_gc_time', time() );
  102. global $wp_cache_shutdown_gc;
  103. if( !isset( $wp_cache_shutdown_gc ) || $wp_cache_shutdown_gc == 0 ) {
  104. if(!wp_next_scheduled('wp_cache_gc')) {
  105. wp_schedule_single_event(time() + 10 , 'wp_cache_gc');
  106. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'scheduled wp_cache_gc for 10 seconds time.', 5 );
  107. }
  108. } else {
  109. global $time_to_gc_cache;
  110. $time_to_gc_cache = 1; // tell the "shutdown gc" to run!
  111. }
  112. }
  113. }
  114. function wpcache_logged_in_message() {
  115. echo '<!-- WP Super Cache did not cache this page because you are logged in and "Don\'t cache pages for logged in users" is enabled. -->';
  116. }
  117. if ( !function_exists( 'wp_cache_user_agent_is_rejected' ) ) {
  118. function wp_cache_user_agent_is_rejected() {
  119. global $cache_rejected_user_agent;
  120. if (!function_exists('apache_request_headers')) return false;
  121. $headers = apache_request_headers();
  122. if (!isset($headers["User-Agent"])) return false;
  123. foreach ($cache_rejected_user_agent as $expr) {
  124. if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
  125. return true;
  126. }
  127. return false;
  128. }
  129. }
  130. function wp_cache_get_response_headers() {
  131. if(function_exists('apache_response_headers')) {
  132. flush();
  133. $headers = apache_response_headers();
  134. } else if(function_exists('headers_list')) {
  135. $headers = array();
  136. foreach(headers_list() as $hdr) {
  137. list($header_name, $header_value) = explode(': ', $hdr, 2);
  138. $headers[$header_name] = $header_value;
  139. }
  140. } else
  141. $headers = null;
  142. return $headers;
  143. }
  144. function wp_cache_is_rejected($uri) {
  145. global $cache_rejected_uri;
  146. $auto_rejected = array( '/wp-admin/', 'xmlrpc.php', 'wp-app.php' );
  147. foreach( $auto_rejected as $u ) {
  148. if( strstr( $uri, $u ) )
  149. return true; // we don't allow caching of wp-admin for security reasons
  150. }
  151. foreach ( $cache_rejected_uri as $expr ) {
  152. if( $expr != '' && preg_match( "~$expr~", $uri ) )
  153. return true;
  154. }
  155. return false;
  156. }
  157. function wp_cache_mutex_init() {
  158. global $mutex, $wp_cache_mutex_disabled, $use_flock, $blog_cache_dir, $mutex_filename, $sem_id;
  159. if( isset( $wp_cache_mutex_disabled) && $wp_cache_mutex_disabled )
  160. return true;
  161. if( !is_bool( $use_flock ) ) {
  162. if(function_exists('sem_get'))
  163. $use_flock = false;
  164. else
  165. $use_flock = true;
  166. }
  167. $mutex = false;
  168. if ($use_flock ) {
  169. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on filename: {$blog_cache_dir}{$mutex_filename}", 5 );
  170. $mutex = @fopen( $blog_cache_dir . $mutex_filename, 'w' );
  171. } else {
  172. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on semaphore: {$sem_id}", 5 );
  173. $mutex = @sem_get( $sem_id, 1, 0644 | IPC_CREAT, 1 );
  174. }
  175. }
  176. function wp_cache_writers_entry() {
  177. global $mutex, $wp_cache_mutex_disabled, $use_flock;
  178. if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
  179. return true;
  180. if( !$mutex ) {
  181. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers entry) mutex lock not created. not caching.", 2 );
  182. return false;
  183. }
  184. if ( $use_flock ) {
  185. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using flock()", 5 );
  186. flock($mutex, LOCK_EX);
  187. } else {
  188. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using sem_acquire()", 5 );
  189. sem_acquire($mutex);
  190. }
  191. return true;
  192. }
  193. function wp_cache_writers_exit() {
  194. global $mutex, $wp_cache_mutex_disabled, $use_flock;
  195. if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
  196. return true;
  197. if( !$mutex ) {
  198. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers exit) mutex lock not created. not caching.", 2 );
  199. return false;
  200. }
  201. if ( $use_flock ) {
  202. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using flock()", 5 );
  203. flock($mutex, LOCK_UN);
  204. } else {
  205. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using sem_release()", 5 );
  206. sem_release($mutex);
  207. }
  208. }
  209. function wp_cache_ob_callback( $buffer ) {
  210. global $wp_cache_pages;
  211. $buffer = apply_filters( 'wp_cache_ob_callback_filter', $buffer );
  212. if( defined( 'DONOTCACHEPAGE' ) ) {
  213. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'DONOTCACHEPAGE defined. Caching disabled.', 2 );
  214. return $buffer;
  215. }
  216. if ( isset( $wp_cache_pages[ 'single' ] ) && $wp_cache_pages[ 'single' ] == 1 && is_single() ) {
  217. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single post.', 2 );
  218. return $buffer;
  219. } elseif ( isset( $wp_cache_pages[ 'pages' ] ) && $wp_cache_pages[ 'pages' ] == 1 && is_page() ) {
  220. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single page.', 2 );
  221. return $buffer;
  222. } elseif ( isset( $wp_cache_pages[ 'archives' ] ) && $wp_cache_pages[ 'archives' ] == 1 && is_archive() ) {
  223. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching archive page.', 2 );
  224. return $buffer;
  225. } elseif ( isset( $wp_cache_pages[ 'tag' ] ) && $wp_cache_pages[ 'tag' ] == 1 && is_tag() ) {
  226. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching tag page.', 2 );
  227. return $buffer;
  228. } elseif ( isset( $wp_cache_pages[ 'category' ] ) && $wp_cache_pages[ 'category' ] == 1 && is_category() ) {
  229. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching category page.', 2 );
  230. return $buffer;
  231. } elseif ( isset( $wp_cache_pages[ 'frontpage' ] ) && $wp_cache_pages[ 'frontpage' ] == 1 && is_front_page() ) {
  232. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching front page.', 2 );
  233. return $buffer;
  234. } elseif ( isset( $wp_cache_pages[ 'home' ] ) && $wp_cache_pages[ 'home' ] == 1 && is_home() ) {
  235. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching home page.', 2 );
  236. return $buffer;
  237. } elseif ( isset( $wp_cache_pages[ 'search' ] ) && $wp_cache_pages[ 'search' ] == 1 && is_search() ) {
  238. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching search page.', 2 );
  239. return $buffer;
  240. } elseif ( isset( $wp_cache_pages[ 'feed' ] ) && $wp_cache_pages[ 'feed' ] == 1 && is_feed() ) {
  241. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching feed.', 2 );
  242. return $buffer;
  243. }
  244. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Output buffer callback', 4 );
  245. $buffer = &wp_cache_get_ob( $buffer );
  246. wp_cache_shutdown_callback();
  247. return $buffer;
  248. }
  249. function wp_cache_append_tag( &$buffer ) {
  250. global $wp_cache_gmt_offset;
  251. global $cache_enabled, $super_cache_enabled;
  252. $timestamp = gmdate('Y-m-d H:i:s', (time() + ( $wp_cache_gmt_offset * 3600)));
  253. if ( $cache_enabled || $super_cache_enabled ) {
  254. $buffer .= "<!-- Cached page generated by WP-Super-Cache on $timestamp -->\n";
  255. } else {
  256. $buffer .= "<!-- Live page served on $timestamp -->\n";
  257. }
  258. }
  259. function wp_cache_get_ob(&$buffer) {
  260. global $wpsc_settings;
  261. global $cache_enabled, $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
  262. global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
  263. global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
  264. global $wp_cache_404, $gzsize, $supercacheonly;
  265. global $blog_cache_dir, $wp_cache_request_uri, $wp_supercache_cache_list;
  266. global $wp_cache_not_logged_in, $wp_cache_object_cache, $cache_max_time;
  267. global $wp_cache_is_home, $wp_cache_front_page_checks;
  268. $new_cache = true;
  269. $wp_cache_meta = '';
  270. /* Mode paranoic, check for closing tags
  271. * we avoid caching incomplete files */
  272. if ( $buffer == '' ) {
  273. $new_cache = false;
  274. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
  275. wp_cache_debug( "Buffer is blank. Output buffer may have been corrupted by another plugin or this is a redirected URL. Look for text 'ob_start' in the files of your plugins directory.", 2 );
  276. $buffer .= "\n<!-- Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins. -->\n";
  277. }
  278. }
  279. if ( $wp_cache_404 && false == apply_filters( 'wpsupercache_404', false ) ) {
  280. $new_cache = false;
  281. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
  282. wp_cache_debug( "404 file not found not cached", 2 );
  283. $buffer .= "\n<!-- Page not cached by WP Super Cache. 404. -->\n";
  284. }
  285. }
  286. if (!preg_match('/(<\/html>|<\/rss>|<\/feed>|<\/urlset)/i',$buffer) ) {
  287. $new_cache = false;
  288. if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) ) {
  289. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
  290. wp_cache_debug( "No closing html tag. Not caching.", 2 );
  291. $buffer .= "\n<!-- Page not cached by WP Super Cache. No closing HTML tag. Check your theme. -->\n";
  292. }
  293. } else {
  294. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "robots.txt detected. Not caching.", 2 );
  295. }
  296. }
  297. if( !$new_cache )
  298. return $buffer;
  299. $duration = wp_cache_microtime_diff($wp_start_time, microtime());
  300. $duration = sprintf("%0.3f", $duration);
  301. $buffer .= "\n<!-- Dynamic page generated in $duration seconds. -->\n";
  302. if( !wp_cache_writers_entry() ) {
  303. $buffer .= "\n<!-- Page not cached by WP Super Cache. Could not get mutex lock. -->\n";
  304. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Could not get mutex lock. Not caching.", 1 );
  305. return $buffer;
  306. }
  307. if ( $wp_cache_not_logged_in && is_feed() ) {
  308. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Feed detected. Writing legacy cache files.", 5 );
  309. $wp_cache_not_logged_in = false;
  310. }
  311. $home_url = parse_url( trailingslashit( get_bloginfo( 'url' ) ) );
  312. $dir = get_current_url_supercache_dir();
  313. $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $home_url[ 'host' ]);
  314. if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
  315. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Supercache disabled: GET or feed detected or disabled by config.", 2 );
  316. $super_cache_enabled = false;
  317. }
  318. $tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp';
  319. $supercacheonly = false;
  320. if( $super_cache_enabled ) {
  321. if ( wp_cache_get_cookies_values() == '' ) {
  322. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Anonymous user detected. Only creating Supercache file.", 3 );
  323. $supercacheonly = true;
  324. }
  325. }
  326. if ( $wp_cache_not_logged_in && wp_cache_get_cookies_values() != '' ) {
  327. $super_cache_enabled = false;
  328. $cache_enabled = false;
  329. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching for known user.', 5 );
  330. }
  331. if ( $wp_cache_object_cache ) { // half on mode when using the object cache
  332. if ( wp_cache_get_cookies_values() != '' ) {
  333. $cache_enabled = false;
  334. }
  335. $super_cache_enabled = false;
  336. $supercacheonly = false;
  337. wp_cache_init(); // PHP5 destroys objects during shutdown
  338. }
  339. $fr = $fr2 = $gz = false;
  340. if ( $cache_enabled ) {
  341. // Open wp-cache cache file
  342. if ( !$supercacheonly && ( !@file_exists( $blog_cache_dir . $cache_filename ) || ( @file_exists( $blog_cache_dir . $cache_filename ) && ( time() - @filemtime( $blog_cache_dir . $cache_filename ) ) > 5 ) ) ) {
  343. if ( false == $wp_cache_object_cache ) {
  344. $fr = @fopen($tmp_wpcache_filename, 'w');
  345. if (!$fr) {
  346. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename, 1 );
  347. $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename . " -->\n";
  348. wp_cache_writers_exit();
  349. return $buffer;
  350. }
  351. }
  352. } else {
  353. $user_info = wp_cache_get_cookies_values();
  354. $do_cache = apply_filters( 'do_createsupercache', $user_info );
  355. if ( $super_cache_enabled && ( $user_info == '' || $do_cache === true ) ) {
  356. if( @is_dir( $dir ) == false )
  357. @wp_mkdir_p( $dir );
  358. $cache_fname = "{$dir}index.html";
  359. $tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
  360. if ( !@file_exists( $cache_fname ) || ( @file_exists( $cache_fname ) && ( time() - @filemtime( $cache_fname ) ) > 5 ) ) {
  361. $fr2 = @fopen( $tmp_cache_filename, 'w' );
  362. if (!$fr2) {
  363. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ), 1 );
  364. $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . " -->\n";
  365. @fclose( $fr );
  366. @unlink( $tmp_wpcache_filename );
  367. wp_cache_writers_exit();
  368. return $buffer;
  369. } elseif ( $cache_compression ) {
  370. $gz = @fopen( $tmp_cache_filename . ".gz", 'w');
  371. if (!$gz) {
  372. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz", 1 );
  373. $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz -->\n";
  374. @fclose( $fr );
  375. @unlink( $tmp_wpcache_filename );
  376. @fclose( $fr2 );
  377. @unlink( $tmp_cache_filename );
  378. wp_cache_writers_exit();
  379. return $buffer;
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. $added_cache = 0;
  387. $oc_key = get_oc_key();
  388. if ( preg_match( '/<!--mclude|<!--mfunc|<!--dynamic-cached-content-->/', $buffer ) ) { //Dynamic content
  389. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
  390. $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
  391. "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
  392. $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
  393. "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
  394. $store = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is',
  395. "<!--dynamic-cached-content-->\n<?php$2?>\n<!--/dynamic-cached-content-->", $store);
  396. $wp_cache_meta[ 'dynamic' ] = true;
  397. /* Clean function calls in tag */
  398. $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
  399. $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
  400. $buffer = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is',
  401. "<!--dynamic-cached-content-->$1<!--/dynamic-cached-content-->", $buffer);
  402. $store = apply_filters( 'wpsupercache_buffer', $store );
  403. // Append WP Super Cache or Live page comment tag
  404. wp_cache_append_tag($buffer);
  405. wp_cache_append_tag($store);
  406. global $wp_super_cache_late_init;
  407. if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
  408. $buffer .= '<!-- Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details. -->';
  409. if ( false == $wp_cache_object_cache ) {
  410. if( $fr ) { // legacy caching
  411. fputs($fr, $store);
  412. } elseif ( isset( $fr2 ) ) { // supercache active
  413. $php_fname = "{$dir}index.html.php";
  414. $tmp_php_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
  415. $php_fd = @fopen( $tmp_php_filename, 'w' );
  416. if ( !$php_fd ) {
  417. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_php_filename ), 1 );
  418. $buffer .= "<!-- File not cached! Super Cache couldn't write to: " . str_replace( ABSPATH, '', $tmp_php_filename ) . " -->\n";
  419. @fclose( $php_fd );
  420. @unlink( $tmp_php_filename );
  421. wp_cache_writers_exit();
  422. return $buffer;
  423. }
  424. fputs( $php_fd, $store );
  425. }
  426. } else {
  427. wp_cache_set( $oc_key, $store, 'supercache', $cache_max_time );
  428. }
  429. if ( $cache_compression && $wp_cache_gzip_encoding ) {
  430. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Gzipping dynamic buffer.", 5 );
  431. $gzdata = gzencode( $buffer . "<!-- Compression = gzip -->", 6, FORCE_GZIP );
  432. $gzsize = strlen($gzdata);
  433. }
  434. } else {
  435. $buffer = apply_filters( 'wpsupercache_buffer', $buffer );
  436. // Append WP Super Cache or Live page comment tag
  437. wp_cache_append_tag($buffer);
  438. if( $gz || $wp_cache_gzip_encoding ) {
  439. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Gzipping buffer.", 5 );
  440. $gzdata = gzencode( $buffer . "<!-- Compression = gzip -->", 6, FORCE_GZIP );
  441. $gzsize = strlen($gzdata);
  442. }
  443. if ($wp_cache_gzip_encoding) {
  444. $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
  445. $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
  446. // Return uncompressed data & store compressed for later use
  447. if ( false == $wp_cache_object_cache ) {
  448. if( $fr ) {
  449. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to wp-cache cache file.", 5 );
  450. fputs($fr, $gzdata);
  451. }
  452. } elseif ( $cache_enabled ) {
  453. wp_cache_set( $oc_key . ".gz", $gzdata, 'supercache', $cache_max_time );
  454. $added_cache = 1;
  455. }
  456. } else { // no compression
  457. $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie';
  458. if ( false == $wp_cache_object_cache ) {
  459. if( $fr ) {
  460. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to wp-cache cache file.", 5 );
  461. fputs($fr, $buffer);
  462. }
  463. } elseif ( $cache_enabled ) {
  464. wp_cache_set( $oc_key, $buffer, 'supercache', $cache_max_time );
  465. $added_cache = 1;
  466. }
  467. }
  468. if ( false == $wp_cache_object_cache ) {
  469. if( $fr2 ) {
  470. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to supercache file.", 5 );
  471. fputs($fr2, $buffer . '<!-- super cache -->' );
  472. }
  473. if( $gz ) {
  474. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to supercache file.", 5 );
  475. fwrite($gz, $gzdata );
  476. }
  477. }
  478. }
  479. $new_cache = true;
  480. if ( false == $wp_cache_object_cache ) {
  481. if( $fr ) {
  482. $supercacheonly = false;
  483. fclose($fr);
  484. if ( filesize( $tmp_wpcache_filename ) == 0 ) {
  485. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_wpcache_filename was empty. Did not rename to {$blog_cache_dir}{$cache_filename}", 5 );
  486. @unlink( $tmp_wpcache_filename );
  487. } else {
  488. if ( !rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ) ) {
  489. unlink( $blog_cache_dir . $cache_filename );
  490. rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename );
  491. }
  492. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp wp-cache file to {$blog_cache_dir}$cache_filename", 5 );
  493. $added_cache = 1;
  494. }
  495. }
  496. if( $fr2 ) {
  497. fclose($fr2);
  498. if ( $wp_cache_front_page_checks && $cache_fname == $supercachedir . $home_url[ 'path' ] . 'index.html' && !( $wp_cache_is_home ) ) {
  499. wp_cache_writers_exit();
  500. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! Not writing another page to front page cache.", 1 );
  501. return $buffer;
  502. } elseif ( filesize( $tmp_cache_filename ) == 0 ) {
  503. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_cache_filename was empty. Did not rename to {$cache_fname}", 5 );
  504. @unlink( $tmp_cache_filename );
  505. } else {
  506. if ( !@rename( $tmp_cache_filename, $cache_fname ) ) {
  507. @unlink( $cache_fname );
  508. @rename( $tmp_cache_filename, $cache_fname );
  509. }
  510. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache file to $cache_fname", 5 );
  511. $added_cache = 1;
  512. }
  513. }
  514. if( $php_fd ) {
  515. fclose( $php_fd );
  516. if ( $php_fname == $supercachedir . $home_url[ 'path' ] . 'index.html.php' && !( $wp_cache_is_home ) ) {
  517. wp_cache_writers_exit();
  518. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! Not writing another page to front page cache.", 1 );
  519. return $buffer;
  520. } elseif ( filesize( $tmp_php_filename ) == 0 ) {
  521. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_php_filename was empty. Did not rename to {$php_fname}", 5 );
  522. @unlink( $tmp_php_filename );
  523. } else {
  524. if ( !@rename( $tmp_php_filename, $php_fname ) ) {
  525. @unlink( $php_fname );
  526. @rename( $tmp_php_filename, $php_fname );
  527. }
  528. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache file to $php_fname", 5 );
  529. $added_cache = 1;
  530. }
  531. }
  532. if( $gz ) {
  533. fclose($gz);
  534. if ( filesize( $tmp_cache_filename . '.gz' ) == 0 ) {
  535. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file {$tmp_cache_filename}.gz was empty. Did not rename to {$cache_fname}.gz", 5 );
  536. @unlink( $tmp_cache_filename . '.gz' );
  537. } else {
  538. if ( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) {
  539. @unlink( $cache_fname . '.gz' );
  540. @rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' );
  541. }
  542. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache gz file to {$cache_fname}.gz", 5 );
  543. $added_cache = 1;
  544. }
  545. }
  546. }
  547. if ( $added_cache && isset( $wp_supercache_cache_list ) && $wp_supercache_cache_list ) {
  548. update_option( 'wpsupercache_count', ( get_option( 'wpsupercache_count' ) + 1 ) );
  549. $last_urls = (array)get_option( 'supercache_last_cached' );
  550. if ( count( $last_urls ) >= 10 )
  551. $last_urls = array_slice( $last_urls, 1, 9 );
  552. $last_urls[] = array( 'url' => $_SERVER[ 'REQUEST_URI' ], 'date' => date( 'Y-m-d H:i:s' ) );
  553. update_option( 'supercache_last_cached', $last_urls );
  554. }
  555. wp_cache_writers_exit();
  556. if ( !headers_sent() && $wp_cache_gzip_encoding && $gzdata) {
  557. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzip content headers. Sending buffer to browser", 5 );
  558. header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
  559. header( 'Vary: Accept-Encoding, Cookie' );
  560. header( 'Content-Length: ' . $gzsize );
  561. return $gzdata;
  562. } else {
  563. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending buffer to browser", 5 );
  564. return $buffer;
  565. }
  566. }
  567. function wp_cache_phase2_clean_cache($file_prefix) {
  568. global $cache_path, $blog_cache_dir;
  569. if( !wp_cache_writers_entry() )
  570. return false;
  571. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning cache in $blog_cache_dir", 3 );
  572. if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
  573. while ( false !== ($file = @readdir($handle))) {
  574. if ( preg_match("/^$file_prefix/", $file) )
  575. @unlink( $blog_cache_dir . $file );
  576. }
  577. closedir($handle);
  578. }
  579. wp_cache_writers_exit();
  580. }
  581. function prune_super_cache( $directory, $force = false, $rename = false ) {
  582. global $cache_max_time, $cache_path, $super_cache_enabled, $cache_rebuild_files, $blog_cache_dir;
  583. if( !is_admin() && $super_cache_enabled == 0 )
  584. return false;
  585. if( !isset( $cache_max_time ) )
  586. $cache_max_time = 3600;
  587. $now = time();
  588. $protected_directories = array( $cache_path . '.htaccess', $cache_path . $blog_cache_dir . 'meta', $cache_path . 'supercache' );
  589. $oktodelete = false;
  590. if (is_dir($directory)) {
  591. if( $dh = @opendir( $directory ) ) {
  592. $directory = trailingslashit( $directory );
  593. while( ( $entry = @readdir( $dh ) ) !== false ) {
  594. if ($entry == '.' || $entry == '..')
  595. continue;
  596. $entry = $directory . $entry;
  597. prune_super_cache( $entry, $force, $rename );
  598. // If entry is a directory, AND it's not a protected one, AND we're either forcing the delete, OR the file is out of date,
  599. if( is_dir( $entry ) && !in_array( $entry, $protected_directories ) && ( $force || @filemtime( $entry ) + $cache_max_time <= $now ) ) {
  600. // if the directory isn't empty can't delete it
  601. if( $handle = @opendir( $entry ) ) {
  602. $donotdelete = false;
  603. while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) {
  604. if ($file == '.' || $file == '..')
  605. continue;
  606. $donotdelete = true;
  607. }
  608. closedir($handle);
  609. }
  610. if( $donotdelete )
  611. continue;
  612. if( !$rename ) {
  613. @rmdir( $entry );
  614. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "gc: deleted $entry", 2 );
  615. }
  616. }
  617. }
  618. closedir($dh);
  619. }
  620. } elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) {
  621. $oktodelete = true;
  622. if( in_array( $directory, $protected_directories ) )
  623. $oktodelete = false;
  624. if( $oktodelete && !$rename ) {
  625. @unlink( $directory );
  626. } elseif( $oktodelete && $rename ) {
  627. wp_cache_rebuild_or_delete( $directory );
  628. }
  629. }
  630. }
  631. function wp_cache_rebuild_or_delete( $file ) {
  632. global $cache_rebuild_files;
  633. if( strpos( $file, '?' ) !== false )
  634. $file = substr( $file, 0, strpos( $file, '?' ) );
  635. if( $cache_rebuild_files && substr( $file, -14 ) != '.needs-rebuild' ) {
  636. if( @rename($file, $file . '.needs-rebuild') ) {
  637. @touch( $file . '.needs-rebuild' );
  638. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: rename to {$file}.needs-rebuild", 2 );
  639. } else {
  640. @unlink( $file );
  641. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
  642. }
  643. } else {
  644. @unlink( $file );
  645. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
  646. }
  647. }
  648. function wp_cache_phase2_clean_expired( $file_prefix, $force = false ) {
  649. global $cache_path, $cache_max_time, $blog_cache_dir, $wp_cache_preload_on;
  650. clearstatcache();
  651. if( !wp_cache_writers_entry() )
  652. return false;
  653. $now = time();
  654. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning expired cache files in $blog_cache_dir", 2 );
  655. if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
  656. while ( false !== ($file = readdir($handle))) {
  657. if ( preg_match("/^$file_prefix/", $file) &&
  658. (@filemtime( $blog_cache_dir . $file) + $cache_max_time) <= $now ) {
  659. @unlink( $blog_cache_dir . $file );
  660. @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
  661. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file} (plus meta)", 5 );
  662. continue;
  663. }
  664. if($file != '.' && $file != '..') {
  665. if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now ) {
  666. if( substr( $file, -9 ) != '.htaccess' ) {
  667. @unlink($blog_cache_dir . $file);
  668. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file}", 5 );
  669. }
  670. }
  671. }
  672. }
  673. closedir($handle);
  674. if ( false == $wp_cache_preload_on || true == $force ) {
  675. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Doing GC on supercache dir: {$cache_path}supercache", 2 );
  676. prune_super_cache( $cache_path . 'supercache' );
  677. }
  678. }
  679. wp_cache_writers_exit();
  680. return true;
  681. }
  682. function wp_cache_shutdown_callback() {
  683. global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $gzsize, $cache_filename, $supercacheonly, $blog_cache_dir;
  684. global $wp_cache_request_uri, $wp_cache_key, $wp_cache_object_cache, $cache_enabled, $wp_cache_blog_charset, $wp_cache_not_logged_in;
  685. $wp_cache_meta[ 'uri' ] = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks
  686. $wp_cache_meta[ 'blog_id' ] = $blog_id;
  687. $wp_cache_meta[ 'post' ] = wp_cache_post_id();
  688. $wp_cache_meta[ 'key' ] = $wp_cache_key;
  689. $wp_cache_meta = apply_filters( 'wp_cache_meta', $wp_cache_meta );
  690. $response = wp_cache_get_response_headers();
  691. foreach ($known_headers as $key) {
  692. if(isset($response[$key])) {
  693. $wp_cache_meta[ 'headers' ][ $key ] = "$key: " . $response[$key];
  694. }
  695. }
  696. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_shutdown_callback: collecting meta data.", 2 );
  697. if (!isset( $response['Last-Modified'] )) {
  698. $value = gmdate('D, d M Y H:i:s') . ' GMT';
  699. /* Dont send this the first time */
  700. /* @header('Last-Modified: ' . $value); */
  701. $wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value";
  702. }
  703. if ( !isset( $response[ 'Content-Type' ] ) && !isset( $response[ 'Content-type' ] ) ) {
  704. // On some systems, headers set by PHP can't be fetched from
  705. // the output buffer. This is a last ditch effort to set the
  706. // correct Content-Type header for feeds, if we didn't see
  707. // it in the response headers already. -- dougal
  708. if (is_feed()) {
  709. $type = get_query_var('feed');
  710. $type = str_replace('/','',$type);
  711. switch ($type) {
  712. case 'atom':
  713. $value = "application/atom+xml";
  714. break;
  715. case 'rdf':
  716. $value = "application/rdf+xml";
  717. break;
  718. case 'rss':
  719. case 'rss2':
  720. default:
  721. $value = "application/rss+xml";
  722. }
  723. } else { // not a feed
  724. $value = get_option( 'html_type' );
  725. if( $value == '' )
  726. $value = 'text/html';
  727. }
  728. $value .= "; charset=\"" . $wp_cache_blog_charset . "\"";
  729. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending 'Content-Type: $value' header.", 2 );
  730. @header("Content-Type: $value");
  731. $wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value";
  732. }
  733. if ( !$supercacheonly && !$wp_cache_not_logged_in && $new_cache ) {
  734. if( !isset( $wp_cache_meta[ 'dynamic' ] ) && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) {
  735. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending gzip headers.", 2 );
  736. $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
  737. $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
  738. }
  739. $serial = serialize($wp_cache_meta);
  740. if( wp_cache_writers_entry() ) {
  741. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2 );
  742. if ( false == $wp_cache_object_cache ) {
  743. $tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid( mt_rand(), true ) . '.tmp';
  744. $fr = @fopen( $tmp_meta_filename, 'w');
  745. if( !$fr )
  746. @mkdir( $blog_cache_dir . 'meta' );
  747. $fr = @fopen( $tmp_meta_filename, 'w');
  748. if ( $fr ) {
  749. fputs($fr, $serial);
  750. fclose($fr);
  751. @chmod( $tmp_meta_filename, 0666 & ~umask());
  752. if( !@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ) ) {
  753. @unlink( $blog_cache_dir . 'meta/' . $meta_file );
  754. @rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file );
  755. }
  756. } else {
  757. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Problem writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2 );
  758. }
  759. } elseif ( $cache_enabled ) {
  760. $oc_key = get_oc_key() . ".meta";
  761. if ( gzip_accepted() )
  762. $oc_key .= ".gz";
  763. wp_cache_set( $oc_key, $serial, 'supercache', $cache_max_time );
  764. }
  765. wp_cache_writers_exit();
  766. }
  767. }
  768. global $time_to_gc_cache;
  769. if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) {
  770. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Executing wp_cache_gc action.", 3 );
  771. do_action( 'wp_cache_gc' );
  772. }
  773. }
  774. function wp_cache_no_postid($id) {
  775. return wp_cache_post_change(wp_cache_post_id());
  776. }
  777. function wp_cache_get_postid_from_comment( $comment_id, $status = 'NA' ) {
  778. global $super_cache_enabled, $wp_cache_request_uri;
  779. $comment = get_comment($comment_id, ARRAY_A);
  780. if ( $status != 'NA' ) {
  781. $comment[ 'old_comment_approved' ] = $comment[ 'comment_approved' ];
  782. $comment[ 'comment_approved' ] = $status;
  783. }
  784. if ( ( $status == 'trash' || $status == 'spam' ) && $comment[ 'comment_approved' ] != 1 ) {
  785. // don't modify cache if moderated comments are trashed or spammed
  786. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment deleted or spammed. Don't delete any cache files.", 4 );
  787. return wp_cache_post_id();
  788. }
  789. $postid = $comment['comment_post_ID'];
  790. // Do nothing if comment is not moderated
  791. // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
  792. if ( !preg_match('/wp-admin\//', $wp_cache_request_uri) ) {
  793. if ( $comment['comment_approved'] == 'delete' && ( isset( $comment[ 'old_comment_approved' ] ) && $comment[ 'old_comment_approved' ] == 0 ) ) { // do nothing if moderated comments are deleted
  794. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment deleted. Don't delete any cache files.", 4 );
  795. return $postid;
  796. } elseif ( $comment['comment_approved'] == 'spam' ) {
  797. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Spam comment. Don't delete any cache files.", 4 );
  798. return $postid;
  799. } elseif( $comment['comment_approved'] == '0' ) {
  800. if ( $comment[ 'content_type' ] == '' ) {
  801. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment. Don't delete supercache file until comment approved.", 4 );
  802. $super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
  803. } else {
  804. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated ping or trackback. Not deleting cache files..", 4 );
  805. return $postid;
  806. }
  807. }
  808. }
  809. // We must check it up again due to WP bugs calling two different actions
  810. // for delete, for example both wp_set_comment_status and delete_comment
  811. // are called when deleting a comment
  812. if ($postid > 0) {
  813. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post $postid changed. Update cache.", 4 );
  814. return wp_cache_post_change( $postid );
  815. } elseif ( $_GET[ 'delete_all' ] != 'Empty Trash' && $_GET[ 'delete_all2' ] != 'Empty Spam' ) {
  816. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Unknown post changed. Update cache.", 4 );
  817. return wp_cache_post_change( wp_cache_post_id() );
  818. }
  819. }
  820. /* Clear out the cache directory. */
  821. function wp_cache_clear_cache() {
  822. global $cache_path, $wp_cache_object_cache;
  823. if ( $wp_cache_object_cache ) {
  824. reset_oc_version();
  825. } else {
  826. prune_super_cache( $cache_path . 'supercache/', true );
  827. prune_super_cache( $cache_path, true );
  828. }
  829. }
  830. function wp_cache_post_edit($post_id) {
  831. global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
  832. if( $wp_cache_clear_on_post_edit ) {
  833. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing cache $blog_cache_dir and {$cache_path}supercache/ on post edit per config.", 2 );
  834. if ( $wp_cache_object_cache ) {
  835. reset_oc_version();
  836. } else {
  837. prune_super_cache( $blog_cache_dir, true );
  838. prune_super_cache( $cache_path . 'supercache/', true );
  839. }
  840. } else {
  841. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing cache for post $post_id on post edit.", 2 );
  842. wp_cache_post_change( $post_id );
  843. }
  844. }
  845. function wp_cache_post_id_gc( $siteurl, $post_id, $all = 'all' ) {
  846. global $cache_path, $wp_cache_object_cache;
  847. if ( $wp_cache_object_cache )
  848. reset_oc_version();
  849. $post_id = intval( $post_id );
  850. if( $post_id == 0 )
  851. return;
  852. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc post_id: $post_id " . post_permalink( $post_id ) . " clearing cache in $dir{$permalink}.", 4 );
  853. $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
  854. $dir = $cache_path . 'supercache/' . $siteurl;
  855. if ( $all == 'all' ) {
  856. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in $dir{$permalink}.", 4 );
  857. prune_super_cache( $dir . $permalink, true, true );
  858. do_action( 'gc_cache', 'prune', $permalink );
  859. @rmdir( $dir . $permalink );
  860. } else {
  861. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cached index.html(.gz) in $dir{$permalink}.", 4 );
  862. prune_super_cache( $dir . $permalink . 'index.html', true, true );
  863. prune_super_cache( $dir . $permalink . 'index.html.php', true, true );
  864. prune_super_cache( $dir . $permalink . 'index.html.gz', true, true );
  865. do_action( 'gc_cache', 'prune', $permalink );
  866. }
  867. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in {$dir}page/.", 4 );
  868. prune_super_cache( $dir . 'page/', true );
  869. do_action( 'gc_cache', 'prune', '/page/' );
  870. }
  871. function wp_cache_post_change( $post_id ) {
  872. global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid, $wp_cache_refresh_single_only;
  873. static $last_processed = -1;
  874. if ($post_id == $last_processed) return $post_id;
  875. $last_processed = $post_id;
  876. $post = get_post( $post_id );
  877. if( $post->post_status == 'draft' ) {
  878. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: draft post, not deleting any cache files.", 4 );
  879. return $post_id;
  880. }
  881. if( !wp_cache_writers_entry() )
  882. return $post_id;
  883. if ( isset( $wp_cache_refresh_single_only ) && $wp_cache_refresh_single_only && strpos( $_SERVER[ 'REQUEST_URI' ], 'wp-comments-post.php' ) ) {
  884. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: comment detected. only deleting post page.", 4 );
  885. $all = false;
  886. } else {
  887. $all = true;
  888. }
  889. if ( $wp_cache_object_cache )
  890. reset_oc_version();
  891. $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
  892. if( $super_cache_enabled ) {
  893. $siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) );
  894. // make sure the front page has a rebuild file
  895. if ( $all == true ) {
  896. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting cache files in " . $cache_path . 'supercache/' . $siteurl, 4 );
  897. prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html', true, true );
  898. prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.php', true, true );
  899. prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.gz', true, true );
  900. do_action( 'gc_cache', 'prune', 'homepage' );
  901. }
  902. wp_cache_post_id_gc( $siteurl, $post_id );
  903. if( $all == true && get_option( 'show_on_front' ) == 'page' ) {
  904. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting page_on_front and_page_for_posts pages.", 4 );
  905. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: page_on_front " . get_option( 'page_on_front' ), 4 );
  906. wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ), 'single' );
  907. $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( get_option( 'page_for_posts' ) ) ) );
  908. prune_super_cache( $cache_path . 'supercache/' . $siteurl . $permalink . 'index.html', true, true );
  909. prune_super_cache( $cache_path . 'supercache/' . $siteurl . $permalink . 'index.html.php', true, true );
  910. prune_super_cache( $cache_path . 'supercache/' . $siteurl . $permalink . 'index.html.gz', true, true );
  911. do_action( 'gc_cache', 'prune', $permalink );
  912. }
  913. }
  914. $matches = array();
  915. if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) {
  916. while ( false !== ($file = readdir($handle))) {
  917. if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
  918. $meta_pathname = $blog_cache_dir . 'meta/' . $file;
  919. $content_pathname = $blog_cache_dir . $matches[1] . ".html";
  920. $meta = unserialize(@file_get_contents($meta_pathname));
  921. if( false == is_array( $meta ) ) {
  922. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change cleaning up stray file: $content_pathname", 4 );
  923. @unlink($meta_pathname);
  924. @unlink($content_pathname);
  925. continue;
  926. }
  927. if ($post_id > 0 && $meta) {
  928. if ( $meta[ 'blog_id' ] == $blog_id && ( ( $all == true && !$meta[ 'post' ] ) || $meta[ 'post' ] == $post_id) ) {
  929. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting post cache files for {$meta[ 'uri' ]}: $content_pathname", 4 );
  930. @unlink($meta_pathname);
  931. @unlink($content_pathname);
  932. if ( $super_cache_enabled == true ) {
  933. @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
  934. @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.php');
  935. @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
  936. do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
  937. }
  938. }
  939. } elseif ($meta[ 'blog_id' ] == $blog_id) {
  940. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting cache files for {$meta[ 'uri' ]}: $content_pathname", 4 );
  941. @unlink($meta_pathname);
  942. @unlink($content_pathname);
  943. if ( $super_cache_enabled == true ) {
  944. @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailing

Large files files are truncated, but you can click here to view the full file