PageRenderTime 189ms CodeModel.GetById 125ms app.highlight 52ms RepoModel.GetById 2ms 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

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

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

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