wp-super-cache
Last commit date
plugins
17 years ago
Changelog.txt
16 years ago
advanced-cache.php
17 years ago
readme.txt
16 years ago
uninstall.php
16 years ago
wp-cache-base.php
18 years ago
wp-cache-config-sample.php
16 years ago
wp-cache-phase1.php
16 years ago
wp-cache-phase2.php
16 years ago
wp-cache.php
16 years ago
wp-cache-phase2.php
727 lines
| 1 | <?php |
| 2 | |
| 3 | function wp_cache_phase2() { |
| 4 | global $cache_filename, $cache_acceptable_files, $wp_cache_gzip_encoding, $super_cache_enabled, $cache_rebuild_files, $wp_cache_gmt_offset, $wp_cache_blog_charset, $wp_cache_last_gc; |
| 5 | global $cache_max_time, $wp_cache_not_logged_in, $wp_cache_request_uri; |
| 6 | |
| 7 | $wp_cache_gmt_offset = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349 |
| 8 | $wp_cache_blog_charset = get_option( 'blog_charset' ); |
| 9 | |
| 10 | wp_cache_mutex_init(); |
| 11 | if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) { |
| 12 | // Post ID is received |
| 13 | add_action('publish_post', 'wp_cache_post_edit', 0); |
| 14 | add_action('edit_post', 'wp_cache_post_change', 0); // leaving a comment called edit_post |
| 15 | add_action('delete_post', 'wp_cache_post_edit', 0); |
| 16 | add_action('publish_phone', 'wp_cache_post_edit', 0); |
| 17 | // Coment ID is received |
| 18 | add_action('trackback_post', 'wp_cache_get_postid_from_comment', 99); |
| 19 | add_action('pingback_post', 'wp_cache_get_postid_from_comment', 99); |
| 20 | add_action('comment_post', 'wp_cache_get_postid_from_comment', 99); |
| 21 | add_action('edit_comment', 'wp_cache_get_postid_from_comment', 99); |
| 22 | add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 99); |
| 23 | // No post_id is available |
| 24 | add_action('delete_comment', 'wp_cache_no_postid', 99); |
| 25 | add_action('switch_theme', 'wp_cache_no_postid', 99); |
| 26 | add_action('edit_user_profile_update', 'wp_cache_no_postid', 99); |
| 27 | |
| 28 | add_action('wp_cache_gc','wp_cache_gc_cron'); |
| 29 | |
| 30 | do_cacheaction( 'add_cacheaction' ); |
| 31 | } |
| 32 | |
| 33 | if( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option('gzipcompression')) |
| 34 | return false; |
| 35 | |
| 36 | if ( $wp_cache_not_logged_in && is_user_logged_in() && !is_feed() && !is_admin() ) { |
| 37 | register_shutdown_function( 'wpcache_logged_in_message' ); |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | $script = basename($_SERVER['PHP_SELF']); |
| 42 | if (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri)) |
| 43 | return false; |
| 44 | if (wp_cache_user_agent_is_rejected()) return; |
| 45 | if($wp_cache_gzip_encoding) |
| 46 | header('Vary: Accept-Encoding, Cookie'); |
| 47 | else |
| 48 | header('Vary: Cookie'); |
| 49 | ob_start( 'wp_cache_ob_callback' ); |
| 50 | |
| 51 | // restore old supercache file temporarily |
| 52 | if( $super_cache_enabled && $cache_rebuild_files ) { |
| 53 | $user_info = wp_cache_get_cookies_values(); |
| 54 | $do_cache = apply_filters( 'do_createsupercache', $user_info ); |
| 55 | if( $user_info == '' || $do_cache === true ) { |
| 56 | $dir = get_current_url_supercache_dir(); |
| 57 | $files_to_check = array( $dir . 'index.html', $dir . 'index.html.gz' ); |
| 58 | foreach( $files_to_check as $cache_file ) { |
| 59 | if( !@file_exists( $cache_file . '.needs-rebuild' ) ) |
| 60 | continue; |
| 61 | $mtime = @filemtime($cache_file . '.needs-rebuild'); |
| 62 | if( $mtime && (time() - $mtime) < 30 ) { |
| 63 | @rename( $cache_file . '.needs-rebuild', $cache_file ); |
| 64 | } |
| 65 | // cleanup old files or if rename fails |
| 66 | if( @file_exists( $cache_file . '.needs-rebuild' ) ) { |
| 67 | @unlink( $cache_file . '.needs-rebuild' ); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if( !isset( $cache_max_time ) ) |
| 74 | $cache_max_time = 600; |
| 75 | $last_gc = get_option( "wpsupercache_gc_time" ); |
| 76 | |
| 77 | if( !$last_gc ) { |
| 78 | update_option( 'wpsupercache_gc_time', time() ); |
| 79 | } |
| 80 | $next_gc = $cache_max_time < 1800 ? $cache_max_time : 600; |
| 81 | if( $last_gc < ( time() - $next_gc ) ) { |
| 82 | update_option( 'wpsupercache_gc_time', time() ); |
| 83 | |
| 84 | global $wp_cache_shutdown_gc; |
| 85 | if( !isset( $wp_cache_shutdown_gc ) || $wp_cache_shutdown_gc == 0 ) { |
| 86 | if(!wp_next_scheduled('wp_cache_gc')) wp_schedule_single_event(time() + 10 , 'wp_cache_gc'); |
| 87 | } else { |
| 88 | global $time_to_gc_cache; |
| 89 | $time_to_gc_cache = 1; // tell the "shutdown gc" to run! |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | function wpcache_logged_in_message() { |
| 95 | 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. -->'; |
| 96 | } |
| 97 | |
| 98 | if ( !function_exists( 'wp_cache_user_agent_is_rejected' ) ) { |
| 99 | function wp_cache_user_agent_is_rejected() { |
| 100 | global $cache_rejected_user_agent; |
| 101 | |
| 102 | if (!function_exists('apache_request_headers')) return false; |
| 103 | $headers = apache_request_headers(); |
| 104 | if (!isset($headers["User-Agent"])) return false; |
| 105 | foreach ($cache_rejected_user_agent as $expr) { |
| 106 | if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr)) |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function wp_cache_get_response_headers() { |
| 114 | if(function_exists('apache_response_headers')) { |
| 115 | flush(); |
| 116 | $headers = apache_response_headers(); |
| 117 | } else if(function_exists('headers_list')) { |
| 118 | $headers = array(); |
| 119 | foreach(headers_list() as $hdr) { |
| 120 | list($header_name, $header_value) = explode(': ', $hdr, 2); |
| 121 | $headers[$header_name] = $header_value; |
| 122 | } |
| 123 | } else |
| 124 | $headers = null; |
| 125 | |
| 126 | return $headers; |
| 127 | } |
| 128 | |
| 129 | function wp_cache_is_rejected($uri) { |
| 130 | global $cache_rejected_uri; |
| 131 | |
| 132 | $auto_rejected = array( '/wp-admin/', 'xmlrpc.php', 'wp-app.php' ); |
| 133 | foreach( $auto_rejected as $u ) { |
| 134 | if( strstr( $uri, $u ) ) |
| 135 | return true; // we don't allow caching of wp-admin for security reasons |
| 136 | } |
| 137 | foreach ($cache_rejected_uri as $expr) { |
| 138 | if( $expr != '' && preg_match( "~$expr~", $uri ) ) |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | function wp_cache_mutex_init() { |
| 145 | global $use_flock, $mutex, $cache_path, $mutex_filename, $sem_id, $blog_cache_dir; |
| 146 | |
| 147 | if(!is_bool($use_flock)) { |
| 148 | if(function_exists('sem_get')) |
| 149 | $use_flock = false; |
| 150 | else |
| 151 | $use_flock = true; |
| 152 | } |
| 153 | |
| 154 | $mutex = false; |
| 155 | if ($use_flock) |
| 156 | $mutex = @fopen($blog_cache_dir . $mutex_filename, 'w'); |
| 157 | else |
| 158 | $mutex = @sem_get($sem_id, 1, 0644 | IPC_CREAT, 1); |
| 159 | } |
| 160 | |
| 161 | function wp_cache_writers_entry() { |
| 162 | global $use_flock, $mutex, $cache_path, $mutex_filename, $wp_cache_mutex_disabled; |
| 163 | |
| 164 | if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled ) |
| 165 | return true; |
| 166 | |
| 167 | if( !$mutex ) |
| 168 | return false; |
| 169 | |
| 170 | if ($use_flock) |
| 171 | flock($mutex, LOCK_EX); |
| 172 | else |
| 173 | sem_acquire($mutex); |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | function wp_cache_writers_exit() { |
| 179 | global $use_flock, $mutex, $cache_path, $mutex_filename, $wp_cache_mutex_disabled; |
| 180 | |
| 181 | if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled ) |
| 182 | return true; |
| 183 | |
| 184 | if( !$mutex ) |
| 185 | return false; |
| 186 | |
| 187 | if ($use_flock) |
| 188 | flock($mutex, LOCK_UN); |
| 189 | else |
| 190 | sem_release($mutex); |
| 191 | } |
| 192 | |
| 193 | function get_current_url_supercache_dir() { |
| 194 | global $cached_direct_pages, $cache_path, $wp_cache_request_uri; |
| 195 | $uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $wp_cache_request_uri ) ) ) ); |
| 196 | $uri = str_replace( '\\', '', $uri ); |
| 197 | $dir = strtolower(preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacks |
| 198 | $dir = apply_filters( 'supercache_dir', $dir ); |
| 199 | $dir = trailingslashit( $cache_path . 'supercache/' . $dir ); |
| 200 | if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) { |
| 201 | $dir = trailingslashit( ABSPATH . $uri ); |
| 202 | } |
| 203 | $dir = str_replace( '//', '/', $dir ); |
| 204 | return $dir; |
| 205 | } |
| 206 | |
| 207 | function wp_cache_ob_callback( $buffer ) { |
| 208 | global $wp_cache_pages; |
| 209 | if( defined( 'DONOTCACHEPAGE' ) ) |
| 210 | return $buffer; |
| 211 | |
| 212 | if ( isset( $wp_cache_pages[ 'single' ] ) && $wp_cache_pages[ 'single' ] == 1 && is_single() ) { |
| 213 | return $buffer; |
| 214 | } elseif ( isset( $wp_cache_pages[ 'pages' ] ) && $wp_cache_pages[ 'pages' ] == 1 && is_page() ) { |
| 215 | return $buffer; |
| 216 | } elseif ( isset( $wp_cache_pages[ 'archives' ] ) && $wp_cache_pages[ 'archives' ] == 1 && is_archive() ) { |
| 217 | return $buffer; |
| 218 | } elseif ( isset( $wp_cache_pages[ 'tag' ] ) && $wp_cache_pages[ 'tag' ] == 1 && is_tag() ) { |
| 219 | return $buffer; |
| 220 | } elseif ( isset( $wp_cache_pages[ 'category' ] ) && $wp_cache_pages[ 'category' ] == 1 && is_category() ) { |
| 221 | return $buffer; |
| 222 | } elseif ( isset( $wp_cache_pages[ 'frontpage' ] ) && $wp_cache_pages[ 'frontpage' ] == 1 && is_front_page() ) { |
| 223 | return $buffer; |
| 224 | } elseif ( isset( $wp_cache_pages[ 'home' ] ) && $wp_cache_pages[ 'home' ] == 1 && is_home() ) { |
| 225 | return $buffer; |
| 226 | } |
| 227 | $buffer = &wp_cache_get_ob( $buffer ); |
| 228 | wp_cache_shutdown_callback(); |
| 229 | return $buffer; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | function wp_cache_get_ob(&$buffer) { |
| 234 | global $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir; |
| 235 | global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression; |
| 236 | global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages; |
| 237 | global $wp_cache_404, $gzsize, $supercacheonly, $wp_cache_gzip_first, $wp_cache_gmt_offset; |
| 238 | global $blog_cache_dir, $wp_cache_request_uri; |
| 239 | |
| 240 | $new_cache = true; |
| 241 | $wp_cache_meta = ''; |
| 242 | |
| 243 | /* Mode paranoic, check for closing tags |
| 244 | * we avoid caching incomplete files */ |
| 245 | if( $wp_cache_404 ) { |
| 246 | $new_cache = false; |
| 247 | $buffer .= "\n<!-- Page not cached by WP Super Cache. 404. -->\n"; |
| 248 | } |
| 249 | |
| 250 | if (!preg_match('/(<\/html>|<\/rss>|<\/feed>)/i',$buffer) ) { |
| 251 | $new_cache = false; |
| 252 | if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) ) |
| 253 | $buffer .= "\n<!-- Page not cached by WP Super Cache. No closing HTML tag. Check your theme. -->\n"; |
| 254 | } |
| 255 | |
| 256 | if( !$new_cache ) |
| 257 | return $buffer; |
| 258 | |
| 259 | $duration = wp_cache_microtime_diff($wp_start_time, microtime()); |
| 260 | $duration = sprintf("%0.3f", $duration); |
| 261 | $buffer .= "\n<!-- Dynamic page generated in $duration seconds. -->\n"; |
| 262 | |
| 263 | if( !wp_cache_writers_entry() ) { |
| 264 | $buffer .= "\n<!-- Page not cached by WP Super Cache. Could not get mutex lock. -->\n"; |
| 265 | return $buffer; |
| 266 | } |
| 267 | |
| 268 | $dir = get_current_url_supercache_dir(); |
| 269 | $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]); |
| 270 | if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) |
| 271 | $super_cache_enabled = false; |
| 272 | |
| 273 | $tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp'; |
| 274 | |
| 275 | // Don't create wp-cache files for anon users |
| 276 | $supercacheonly = false; |
| 277 | if( $super_cache_enabled && wp_cache_get_cookies_values() == '' ) |
| 278 | $supercacheonly = true; |
| 279 | |
| 280 | if( !$supercacheonly ) { |
| 281 | if ( !@file_exists( $blog_cache_dir . $cache_filename ) || ( @file_exists( $blog_cache_dir . $cache_filename ) && ( time() - @filemtime( $blog_cache_dir . $cache_filename ) ) > 5 ) ) { |
| 282 | $fr = @fopen($tmp_wpcache_filename, 'w'); |
| 283 | if (!$fr) { |
| 284 | $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename . " -->\n"; |
| 285 | return $buffer; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | if( $super_cache_enabled ) { |
| 290 | $user_info = wp_cache_get_cookies_values(); |
| 291 | $do_cache = apply_filters( 'do_createsupercache', $user_info ); |
| 292 | if( $user_info == '' || $do_cache === true ) { |
| 293 | |
| 294 | if( @is_dir( $dir ) == false ) |
| 295 | @wp_mkdir_p( $dir ); |
| 296 | |
| 297 | $cache_fname = "{$dir}index.html"; |
| 298 | $tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp'; |
| 299 | if ( !@file_exists( $cache_fname ) || ( @file_exists( $cache_fname ) && ( time() - @filemtime( $cache_fname ) ) > 5 ) ) { |
| 300 | $fr2 = @fopen( $tmp_cache_filename, 'w' ); |
| 301 | if (!$fr2) { |
| 302 | $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . " -->\n"; |
| 303 | @fclose( $fr ); |
| 304 | @unlink( $tmp_wpcache_filename ); |
| 305 | return $buffer; |
| 306 | } elseif( $cache_compression ) { |
| 307 | $gz = @fopen( $tmp_cache_filename . ".gz", 'w'); |
| 308 | if (!$gz) { |
| 309 | $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz -->\n"; |
| 310 | @fclose( $fr ); |
| 311 | @unlink( $tmp_wpcache_filename ); |
| 312 | @fclose( $fr2 ); |
| 313 | @unlink( $tmp_cache_filename ); |
| 314 | return $buffer; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content |
| 322 | $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is', |
| 323 | "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer); |
| 324 | $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is', |
| 325 | "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store); |
| 326 | $store = apply_filters( 'wpsupercache_buffer', $store ); |
| 327 | $wp_cache_meta[ 'dynamic' ] = true; |
| 328 | /* Clean function calls in tag */ |
| 329 | $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer); |
| 330 | $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer); |
| 331 | if( $fr ) |
| 332 | fputs($fr, $store); |
| 333 | if( $fr2 ) |
| 334 | fputs($fr2, $store . '<!-- super cache -->' ); |
| 335 | if( $gz ) |
| 336 | fputs($gz, gzencode( $store . '<!-- super cache gz -->', 1, FORCE_GZIP ) ); |
| 337 | } else { |
| 338 | $buffer = apply_filters( 'wpsupercache_buffer', $buffer ); |
| 339 | $buffer .= "<!-- Cached page generated by WP-Super-Cache on " . gmdate('Y-m-d H:i:s', (time() + ( $wp_cache_gmt_offset * 3600))) . " -->\n"; |
| 340 | |
| 341 | if( $gz || $wp_cache_gzip_encoding ) { |
| 342 | $gzdata = gzencode( $buffer . "<!-- Compression = gzip -->", 3, FORCE_GZIP ); |
| 343 | $gzsize = strlen($gzdata); |
| 344 | } |
| 345 | if ($wp_cache_gzip_encoding) { |
| 346 | $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding; |
| 347 | $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie'; |
| 348 | // Return uncompressed data & store compressed for later use |
| 349 | if( $fr ) |
| 350 | fputs($fr, $gzdata); |
| 351 | } else { // no compression |
| 352 | $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie'; |
| 353 | if( $fr ) |
| 354 | fputs($fr, $buffer); |
| 355 | } |
| 356 | if( $fr2 ) |
| 357 | fputs($fr2, $buffer . '<!-- super cache -->' ); |
| 358 | if( $gz ) |
| 359 | fwrite($gz, $gzdata ); |
| 360 | $buffer .= $log; |
| 361 | } |
| 362 | $new_cache = true; |
| 363 | if( $fr ) { |
| 364 | $supercacheonly = false; |
| 365 | fclose($fr); |
| 366 | if( !rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ) ) { |
| 367 | unlink( $blog_cache_dir . $cache_filename ); |
| 368 | rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ); |
| 369 | } |
| 370 | } |
| 371 | if( $fr2 ) { |
| 372 | fclose($fr2); |
| 373 | if( !@rename( $tmp_cache_filename, $cache_fname ) ) { |
| 374 | @unlink( $cache_fname ); |
| 375 | @rename( $tmp_cache_filename, $cache_fname ); |
| 376 | } |
| 377 | } |
| 378 | if( $gz ) { |
| 379 | fclose($gz); |
| 380 | if( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) { |
| 381 | @unlink( $cache_fname . '.gz' ); |
| 382 | @rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ); |
| 383 | } |
| 384 | } |
| 385 | wp_cache_writers_exit(); |
| 386 | if ( !headers_sent() && isset( $wp_cache_gzip_first ) && 1 == $wp_cache_gzip_first && $wp_cache_gzip_encoding && $gzdata) { |
| 387 | header( 'Content-Encoding: ' . $wp_cache_gzip_encoding ); |
| 388 | header( 'Vary: Accept-Encoding, Cookie' ); |
| 389 | header( 'Content-Length: ' . $gzsize ); |
| 390 | return $gzdata; |
| 391 | } else { |
| 392 | return $buffer; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | function wp_cache_phase2_clean_cache($file_prefix) { |
| 397 | global $cache_path, $blog_cache_dir; |
| 398 | |
| 399 | if( !wp_cache_writers_entry() ) |
| 400 | return false; |
| 401 | if ( ( $handle = @opendir( $blog_cache_dir ) ) ) { |
| 402 | while ( false !== ($file = @readdir($handle))) { |
| 403 | if ( preg_match("/^$file_prefix/", $file) ) |
| 404 | @unlink( $blog_cache_dir . $file ); |
| 405 | } |
| 406 | closedir($handle); |
| 407 | } |
| 408 | wp_cache_writers_exit(); |
| 409 | } |
| 410 | |
| 411 | function prune_super_cache( $directory, $force = false, $rename = false ) { |
| 412 | global $cache_max_time, $cache_path, $super_cache_enabled, $cache_rebuild_files, $blog_cache_dir; |
| 413 | |
| 414 | if( !is_admin() && $super_cache_enabled == 0 ) |
| 415 | return false; |
| 416 | |
| 417 | if( !isset( $cache_max_time ) ) |
| 418 | $cache_max_time = 3600; |
| 419 | |
| 420 | $now = time(); |
| 421 | |
| 422 | $protected_directories = array( $cache_path . '.htaccess', $cache_path . $blog_cache_dir . 'meta', $cache_path . 'supercache' ); |
| 423 | |
| 424 | $oktodelete = false; |
| 425 | if (is_dir($directory)) { |
| 426 | if( $dh = @opendir( $directory ) ) { |
| 427 | $directory = trailingslashit( $directory ); |
| 428 | while( ( $entry = @readdir( $dh ) ) !== false ) { |
| 429 | if ($entry == '.' || $entry == '..') |
| 430 | continue; |
| 431 | $entry = $directory . $entry; |
| 432 | prune_super_cache( $entry, $force, $rename ); |
| 433 | // 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, |
| 434 | if( is_dir( $entry ) && !in_array( $entry, $protected_directories ) && ( $force || @filemtime( $entry ) + $cache_max_time <= $now ) ) { |
| 435 | // if the directory isn't empty can't delete it |
| 436 | if( $handle = @opendir( $entry ) ) { |
| 437 | $donotdelete = false; |
| 438 | while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) { |
| 439 | if ($file == '.' || $file == '..') |
| 440 | continue; |
| 441 | $donotdelete = true; |
| 442 | } |
| 443 | closedir($handle); |
| 444 | } |
| 445 | if( $donotdelete ) |
| 446 | continue; |
| 447 | if( !$rename ) |
| 448 | @rmdir( $entry ); |
| 449 | } |
| 450 | } |
| 451 | closedir($dh); |
| 452 | } |
| 453 | } elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) { |
| 454 | $oktodelete = true; |
| 455 | if( in_array( $directory, $protected_directories ) ) |
| 456 | $oktodelete = false; |
| 457 | if( $oktodelete && !$rename ) { |
| 458 | @unlink( $directory ); |
| 459 | } elseif( $oktodelete && $rename ) { |
| 460 | wp_cache_rebuild_or_delete( $directory ); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | function wp_cache_rebuild_or_delete( $file ) { |
| 466 | global $cache_rebuild_files; |
| 467 | if( strpos( $file, '?' ) !== false ) |
| 468 | $file = substr( $file, 0, strpos( $file, '?' ) ); |
| 469 | if( $cache_rebuild_files && substr( $file, -14 ) != '.needs-rebuild' ) { |
| 470 | if( @rename($file, $file . '.needs-rebuild') ) { |
| 471 | @touch( $file . '.needs-rebuild' ); |
| 472 | } else { |
| 473 | @unlink( $file ); |
| 474 | } |
| 475 | } else { |
| 476 | @unlink( $file ); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | function wp_cache_phase2_clean_expired($file_prefix) { |
| 481 | global $cache_path, $cache_max_time, $blog_cache_dir; |
| 482 | |
| 483 | clearstatcache(); |
| 484 | if( !wp_cache_writers_entry() ) |
| 485 | return false; |
| 486 | $now = time(); |
| 487 | if ( ( $handle = @opendir( $blog_cache_dir ) ) ) { |
| 488 | while ( false !== ($file = readdir($handle))) { |
| 489 | if ( preg_match("/^$file_prefix/", $file) && |
| 490 | (@filemtime( $blog_cache_dir . $file) + $cache_max_time) <= $now ) { |
| 491 | @unlink( $blog_cache_dir . $file ); |
| 492 | @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) ); |
| 493 | continue; |
| 494 | } |
| 495 | if($file != '.' && $file != '..') { |
| 496 | if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now ) { |
| 497 | if( substr( $file, -9 ) != '.htaccess' ) |
| 498 | @unlink($blog_cache_dir . $file); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | closedir($handle); |
| 503 | prune_super_cache( $cache_path . 'supercache' ); |
| 504 | } |
| 505 | |
| 506 | wp_cache_writers_exit(); |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | function wp_cache_shutdown_callback() { |
| 511 | 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; |
| 512 | global $wp_cache_blog_charset, $wp_cache_request_uri; |
| 513 | |
| 514 | $wp_cache_meta[ 'uri' ] = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks |
| 515 | $wp_cache_meta[ 'blog_id' ] = $blog_id; |
| 516 | $wp_cache_meta[ 'post' ] = wp_cache_post_id(); |
| 517 | |
| 518 | $response = wp_cache_get_response_headers(); |
| 519 | foreach ($known_headers as $key) { |
| 520 | if(isset($response[$key])) { |
| 521 | $wp_cache_meta[ 'headers' ][ $key ] = "$key: " . $response[$key]; |
| 522 | } |
| 523 | } |
| 524 | if (!isset( $response['Last-Modified'] )) { |
| 525 | $value = gmdate('D, d M Y H:i:s') . ' GMT'; |
| 526 | /* Dont send this the first time */ |
| 527 | /* @header('Last-Modified: ' . $value); */ |
| 528 | $wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value"; |
| 529 | } |
| 530 | if (!$response['Content-Type'] && !$response['Content-type']) { |
| 531 | // On some systems, headers set by PHP can't be fetched from |
| 532 | // the output buffer. This is a last ditch effort to set the |
| 533 | // correct Content-Type header for feeds, if we didn't see |
| 534 | // it in the response headers already. -- dougal |
| 535 | if (is_feed()) { |
| 536 | $type = get_query_var('feed'); |
| 537 | $type = str_replace('/','',$type); |
| 538 | switch ($type) { |
| 539 | case 'atom': |
| 540 | $value = "application/atom+xml"; |
| 541 | break; |
| 542 | case 'rdf': |
| 543 | $value = "application/rdf+xml"; |
| 544 | break; |
| 545 | case 'rss': |
| 546 | case 'rss2': |
| 547 | default: |
| 548 | $value = "application/rss+xml"; |
| 549 | } |
| 550 | } else { // not a feed |
| 551 | $value = get_option( 'html_type' ); |
| 552 | if( $value == '' ) |
| 553 | $value = 'text/html'; |
| 554 | } |
| 555 | $value .= "; charset=\"" . $wp_cache_blog_charset . "\""; |
| 556 | |
| 557 | @header("Content-Type: $value"); |
| 558 | $wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value"; |
| 559 | } |
| 560 | |
| 561 | if ( ! $supercacheonly && $new_cache ) { |
| 562 | if( $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) { |
| 563 | $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding; |
| 564 | $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie'; |
| 565 | } |
| 566 | |
| 567 | $serial = serialize($wp_cache_meta); |
| 568 | if( wp_cache_writers_entry() ) { |
| 569 | $tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid( mt_rand(), true ) . '.tmp'; |
| 570 | $fr = @fopen( $tmp_meta_filename, 'w'); |
| 571 | if( !$fr ) |
| 572 | @mkdir( $blog_cache_dir . 'meta' ); |
| 573 | $fr = fopen( $tmp_meta_filename, 'w'); |
| 574 | fputs($fr, $serial); |
| 575 | fclose($fr); |
| 576 | @chmod( $tmp_meta_filename, 0666 & ~umask()); |
| 577 | if( !@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ) ) { |
| 578 | unlink( $blog_cache_dir . 'meta/' . $meta_file ); |
| 579 | rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ); |
| 580 | } |
| 581 | wp_cache_writers_exit(); |
| 582 | } |
| 583 | } |
| 584 | global $time_to_gc_cache; |
| 585 | if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) |
| 586 | do_action( 'wp_cache_gc' ); |
| 587 | } |
| 588 | |
| 589 | function wp_cache_no_postid($id) { |
| 590 | return wp_cache_post_change(wp_cache_post_id()); |
| 591 | } |
| 592 | |
| 593 | function wp_cache_get_postid_from_comment($comment_id) { |
| 594 | global $super_cache_enabled, $wp_cache_request_uri; |
| 595 | $comment = get_comment($comment_id, ARRAY_A); |
| 596 | $postid = $comment['comment_post_ID']; |
| 597 | // Do nothing if comment is not moderated |
| 598 | // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world |
| 599 | if( !preg_match('/wp-admin\//', $wp_cache_request_uri) ) |
| 600 | if( $comment['comment_approved'] == 'spam' ) { // changed from 1 to "spam" |
| 601 | return $postid; |
| 602 | } elseif( $comment['comment_approved'] == '0' ) { |
| 603 | $super_cache_enabled = 0; // don't remove the super cache static file until comment is approved |
| 604 | } |
| 605 | // We must check it up again due to WP bugs calling two different actions |
| 606 | // for delete, for example both wp_set_comment_status and delete_comment |
| 607 | // are called when deleting a comment |
| 608 | if ($postid > 0) |
| 609 | return wp_cache_post_change($postid); |
| 610 | else |
| 611 | return wp_cache_post_change(wp_cache_post_id()); |
| 612 | } |
| 613 | |
| 614 | function wp_cache_post_edit($post_id) { |
| 615 | global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir; |
| 616 | if( $wp_cache_clear_on_post_edit ) { |
| 617 | prune_super_cache( $blog_cache_dir, true ); |
| 618 | prune_super_cache( $cache_path . 'supercache/', true ); |
| 619 | } else { |
| 620 | wp_cache_post_change( $post_id ); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | function wp_cache_post_id_gc( $siteurl, $post_id ) { |
| 625 | global $cache_path; |
| 626 | |
| 627 | $post_id = intval( $post_id ); |
| 628 | if( $post_id == 0 ) |
| 629 | return; |
| 630 | |
| 631 | $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) ); |
| 632 | $dir = $cache_path . 'supercache/' . $siteurl; |
| 633 | prune_super_cache( $dir . $permalink, true, true ); |
| 634 | @rmdir( $dir . $permalink ); |
| 635 | prune_super_cache( $dir . 'page/', true ); |
| 636 | } |
| 637 | |
| 638 | function wp_cache_post_change($post_id) { |
| 639 | global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid; |
| 640 | static $last_processed = -1; |
| 641 | |
| 642 | if ($post_id == $last_processed) return $post_id; |
| 643 | $last_processed = $post_id; |
| 644 | if( !wp_cache_writers_entry() ) |
| 645 | return $post_id; |
| 646 | |
| 647 | $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) ); |
| 648 | if( $super_cache_enabled ) { |
| 649 | $siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) ); |
| 650 | // make sure the front page has a rebuild file |
| 651 | prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html', true, true ); |
| 652 | prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.gz', true, true ); |
| 653 | wp_cache_post_id_gc( $siteurl, $post_id ); |
| 654 | if( get_option( 'show_on_front' ) == 'page' ) { |
| 655 | wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ) ); |
| 656 | wp_cache_post_id_gc( $siteurl, get_option( 'page_for_posts' ) ); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | $matches = array(); |
| 661 | if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) { |
| 662 | while ( false !== ($file = readdir($handle))) { |
| 663 | if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) { |
| 664 | $meta_pathname = $blog_cache_dir . 'meta/' . $file; |
| 665 | $content_pathname = $blog_cache_dir . $matches[1] . ".html"; |
| 666 | $meta = unserialize(@file_get_contents($meta_pathname)); |
| 667 | if( false == is_array( $meta ) ) { |
| 668 | @unlink($meta_pathname); |
| 669 | @unlink($content_pathname); |
| 670 | continue; |
| 671 | } |
| 672 | if ($post_id > 0 && $meta) { |
| 673 | if ($meta[ 'blog_id' ] == $blog_id && (!$meta[ 'post' ] || $meta[ 'post' ] == $post_id) ) { |
| 674 | @unlink($meta_pathname); |
| 675 | @unlink($content_pathname); |
| 676 | @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html'); |
| 677 | @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz'); |
| 678 | } |
| 679 | } elseif ($meta[ 'blog_id' ] == $blog_id) { |
| 680 | @unlink($meta_pathname); |
| 681 | @unlink($content_pathname); |
| 682 | @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html'); |
| 683 | @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz'); |
| 684 | } |
| 685 | |
| 686 | } |
| 687 | } |
| 688 | closedir($handle); |
| 689 | } |
| 690 | wp_cache_writers_exit(); |
| 691 | return $post_id; |
| 692 | } |
| 693 | |
| 694 | function wp_cache_microtime_diff($a, $b) { |
| 695 | list($a_dec, $a_sec) = explode(' ', $a); |
| 696 | list($b_dec, $b_sec) = explode(' ', $b); |
| 697 | return $b_sec - $a_sec + $b_dec - $a_dec; |
| 698 | } |
| 699 | |
| 700 | function wp_cache_post_id() { |
| 701 | global $posts, $comment_post_ID, $post_ID; |
| 702 | // We try hard all options. More frequent first. |
| 703 | if ($post_ID > 0 ) return $post_ID; |
| 704 | if ($comment_post_ID > 0 ) return $comment_post_ID; |
| 705 | if (is_single() || is_page()) return $posts[0]->ID; |
| 706 | if (isset( $_GET[ 'p' ] ) && $_GET['p'] > 0) return $_GET['p']; |
| 707 | if (isset( $_POST[ 'p' ] ) && $_POST['p'] > 0) return $_POST['p']; |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | function wp_cache_gc_cron() { |
| 712 | global $file_prefix, $cache_max_time; |
| 713 | |
| 714 | if( !isset( $cache_max_time ) ) |
| 715 | $cache_max_time = 600; |
| 716 | |
| 717 | $start = time(); |
| 718 | if( !wp_cache_phase2_clean_expired($file_prefix ) ) { |
| 719 | wp_cache_debug( 'Cache Expiry cron job failed. Probably mutex locked.' ); |
| 720 | update_option( 'wpsupercache_gc_time', time() - ( $cache_max_time - 10 ) ); // if GC failed then run it again in one minute |
| 721 | } |
| 722 | if( time() - $start > 30 ) |
| 723 | wp_cache_debug( "Cache Expiry cron job took more than 30 seconds to execute.\nYou should reduce the Expiry Time in the WP Super Cache admin page\nas you probably have more cache files than your server can handle efficiently." ); |
| 724 | } |
| 725 | |
| 726 | ?> |
| 727 |