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