web/wp-content/plugins/wp-super-cache/wp-cache-phase1.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 <?php
     1 <?php
       
     2 //error_reporting(E_ERROR | E_PARSE); // uncomment to debug this file!
     2 // Pre-2.6 compatibility
     3 // Pre-2.6 compatibility
     3 if( !defined('WP_CONTENT_DIR') )
     4 if( !defined('WP_CONTENT_DIR') )
     4 	define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
     5 	define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
     5 
     6 
     6 if( !include( WP_CONTENT_DIR . '/wp-cache-config.php' ) )
     7 if( !include( WP_CONTENT_DIR . '/wp-cache-config.php' ) )
     7 	return true;
     8 	return false;
       
     9 
     8 if( !defined( 'WPCACHEHOME' ) )
    10 if( !defined( 'WPCACHEHOME' ) )
     9 	define('WPCACHEHOME', dirname(__FILE__).'/');
    11 	define('WPCACHEHOME', dirname(__FILE__).'/');
       
    12 
    10 
    13 
    11 include( WPCACHEHOME . 'wp-cache-base.php');
    14 include( WPCACHEHOME . 'wp-cache-base.php');
    12 
    15 
    13 if( $blogcacheid != '' ) {
    16 if( $blogcacheid != '' ) {
    14 	$blog_cache_dir = str_replace( '//', '/', $cache_path . "blogs/" . $blogcacheid . '/' );
    17 	$blog_cache_dir = str_replace( '//', '/', $cache_path . "blogs/" . $blogcacheid . '/' );
    15 } else {
    18 } else {
    16 	$blog_cache_dir = $cache_path;
    19 	$blog_cache_dir = $cache_path;
    17 }
    20 }
    18 
    21 
    19 $mutex_filename = 'wp_cache_mutex.lock';
    22 $mutex_filename  = 'wp_cache_mutex.lock';
    20 $new_cache = false;
    23 $new_cache = false;
    21 
       
    22 // Don't change variables behind this point
       
    23 
    24 
    24 if( !isset( $wp_cache_plugins_dir ) )
    25 if( !isset( $wp_cache_plugins_dir ) )
    25 	$wp_cache_plugins_dir = WPCACHEHOME . 'plugins';
    26 	$wp_cache_plugins_dir = WPCACHEHOME . 'plugins';
    26 
    27 
    27 require_once( WPCACHEHOME . 'wp-cache-phase2.php');
    28 require_once( WPCACHEHOME . 'wp-cache-phase2.php');
       
    29 
       
    30 if ( isset( $_GET[ 'donotcachepage' ] ) && isset( $cache_page_secret ) && $_GET[ 'donotcachepage' ] == $cache_page_secret ) {
       
    31 	$cache_enabled = false;
       
    32 	define( 'DONOTCACHEPAGE', 1 );
       
    33 }
    28 
    34 
    29 $plugins = glob( $wp_cache_plugins_dir . '/*.php' );
    35 $plugins = glob( $wp_cache_plugins_dir . '/*.php' );
    30 if( is_array( $plugins ) ) {
    36 if( is_array( $plugins ) ) {
    31 	foreach ( $plugins as $plugin ) {
    37 	foreach ( $plugins as $plugin ) {
    32 	if( is_file( $plugin ) )
    38 	if( is_file( $plugin ) )
    33 		require_once( $plugin );
    39 		require_once( $plugin );
    34 	}
    40 	}
    35 }
    41 }
    36 
    42 
       
    43 if ( isset( $wp_cache_make_known_anon ) && $wp_cache_make_known_anon )
       
    44 	wp_supercache_cache_for_admins();
       
    45 
       
    46 do_cacheaction( 'cache_init' );
       
    47 
    37 if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST') 
    48 if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST') 
    38 	return true;
    49 	return true;
    39 
    50 
    40 $file_expired = false;
    51 $file_expired = false;
    41 $cache_filename = '';
    52 $cache_filename = '';
    44 
    55 
    45 $gzipped = 0;
    56 $gzipped = 0;
    46 $gzsize = 0;
    57 $gzsize = 0;
    47 
    58 
    48 function gzip_accepted(){
    59 function gzip_accepted(){
    49 	if( ini_get( 'zlib.output_compression' ) ) // don't compress WP-Cache data files when PHP is already doing it
    60 	if ( 1 == ini_get( 'zlib.output_compression' ) || "on" == strtolower( ini_get( 'zlib.output_compression' ) ) ) // don't compress WP-Cache data files when PHP is already doing it
    50 		return false;
    61 		return false;
    51 
    62 
    52 	if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) return false;
    63 	if ( !isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) || ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) && strpos( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ], 'gzip' ) === false ) ) return false;
    53 	return 'gzip';
    64 	return 'gzip';
    54 }
    65 }
    55 
    66 
    56 if ($cache_compression) {
    67 if ($cache_compression) {
    57 	$wp_cache_gzip_encoding = gzip_accepted();
    68 	$wp_cache_gzip_encoding = gzip_accepted();
    58 }
    69 }
    59 
    70 
    60 add_cacheaction( 'wp_cache_get_cookies_values', 'wp_cache_check_mobile' );
    71 add_cacheaction( 'supercache_filename_str', 'wp_cache_check_mobile' );
    61 
    72 
    62 $wp_cache_request_uri = $_SERVER[ 'REQUEST_URI' ]; // Cache this in case any plugin modifies it.
    73 $wp_cache_request_uri = $_SERVER[ 'REQUEST_URI' ]; // Cache this in case any plugin modifies it.
    63 
    74 
    64 if( false == @is_dir( $blog_cache_dir ) ) {
    75 if ( $wp_cache_object_cache ) {
    65 	@mkdir( $cache_path . "blogs" );
    76 	if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) )
    66 	@mkdir( $blog_cache_dir );
    77 		return;
    67 }
    78 	
    68 
    79 	wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here.
    69 if( false == @is_dir( $blog_cache_dir . 'meta' ) )
    80 	
    70 	@mkdir( $blog_cache_dir . 'meta' );
    81 	if ( ! is_object( $wp_object_cache ) )
    71 
    82 		return;
       
    83 }
       
    84 
       
    85 function setup_blog_cache_dir() {
       
    86 	global $blog_cache_dir, $cache_path;
       
    87 	if( false == @is_dir( $blog_cache_dir ) ) {
       
    88 		@mkdir( $cache_path . "blogs" );
       
    89 		@mkdir( $blog_cache_dir );
       
    90 	}
       
    91 
       
    92 	if( false == @is_dir( $blog_cache_dir . 'meta' ) )
       
    93 		@mkdir( $blog_cache_dir . 'meta' );
       
    94 }
    72 
    95 
    73 $wp_start_time = microtime();
    96 $wp_start_time = microtime();
       
    97 
       
    98 function get_wp_cache_key( $url = false ) {
       
    99 	global $wp_cache_request_uri, $wp_cache_gzip_encoding, $WPSC_HTTP_HOST;
       
   100 	if ( !$url )
       
   101 		$url = $wp_cache_request_uri;
       
   102 	return do_cacheaction( 'wp_cache_key', $WPSC_HTTP_HOST . intval( $_SERVER[ 'SERVER_PORT' ] ) . preg_replace('/#.*$/', '', str_replace( '/index.php', '/', $url ) ) . $wp_cache_gzip_encoding . wp_cache_get_cookies_values() );
       
   103 }
    74 
   104 
    75 function wp_super_cache_init() {
   105 function wp_super_cache_init() {
    76 	global $wp_cache_key, $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta;
   106 	global $wp_cache_key, $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta;
    77 
   107 
    78 	$wp_cache_key = do_cacheaction( 'wp_cache_key', $_SERVER['HTTP_HOST'].preg_replace('/#.*$/', '', str_replace( '/index.php', '/', $wp_cache_request_uri ) ).$wp_cache_gzip_encoding.wp_cache_get_cookies_values() );
   108 	$wp_cache_key = get_wp_cache_key();
    79 	$key = $blogcacheid . md5( $wp_cache_key );
   109 	$key = $blogcacheid . md5( $wp_cache_key );
    80 	$wp_cache_key = $blogcacheid . $wp_cache_key;
   110 	$wp_cache_key = $blogcacheid . $wp_cache_key;
    81 
   111 
    82 	$cache_filename = $file_prefix . $key . '.html';
   112 	$cache_filename = $file_prefix . $key . '.html';
    83 	$meta_file = $file_prefix . $key . '.meta';
   113 	$meta_file = $file_prefix . $key . '.meta';
    86 	return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' );
   116 	return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' );
    87 }
   117 }
    88 
   118 
    89 function wp_cache_serve_cache_file() {
   119 function wp_cache_serve_cache_file() {
    90 	global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta;
   120 	global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta;
       
   121 	global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path, $wp_cache_no_cache_for_get;
       
   122 	global $wp_cache_disable_utf8;
    91 
   123 
    92 	extract( wp_super_cache_init() );
   124 	extract( wp_super_cache_init() );
    93 
   125 
    94 	if ( wp_cache_user_agent_is_rejected() ) {
   126 	if ( wp_cache_user_agent_is_rejected() ) {
    95 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file served as user agent rejected.", 5 );
   127 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file served as user agent rejected.", 5 );
    96 		return false;
   128 		return false;
    97 	}
   129 	}
    98 
   130 
    99 	if ( file_exists( $cache_file ) ) {
   131 	if ( $wp_cache_no_cache_for_get && false == empty( $_GET ) ) {
   100 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "wp-cache file exists", 5 );
   132 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Non empty GET request. Caching disabled on settings page.", 1 );
       
   133 		return false;
       
   134 	}
       
   135 
       
   136 	if ( $wp_cache_object_cache && wp_cache_get_cookies_values() == '' ) { 
       
   137 		if ( !empty( $_GET ) ) {
       
   138 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Non empty GET request. Not serving request from object cache", 1 );
       
   139 			return false;
       
   140 		}
       
   141 
       
   142 		$oc_key = get_oc_key();
       
   143 		$meta_filename = $oc_key . ".meta";
       
   144 		if ( gzip_accepted() ) {
       
   145 			$oc_key .= ".gz";
       
   146 			$meta_filename .= ".gz";
       
   147 		}
       
   148 		$cache = wp_cache_get( $oc_key, 'supercache' );
       
   149 		$meta = unserialize( wp_cache_get( $meta_filename, 'supercache' ) );
       
   150 		if ( is_array( $meta ) == false ) {
       
   151 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Meta array from object cache corrupt. Ignoring cache.", 1 );
       
   152 			return true;
       
   153 		}
       
   154 	} elseif ( file_exists( $cache_file ) ) {
       
   155 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "wp-cache file exists: $cache_file", 5 );
   101 		if ( !( $meta = unserialize( @file_get_contents( $meta_pathname) ) ) )  {
   156 		if ( !( $meta = unserialize( @file_get_contents( $meta_pathname) ) ) )  {
   102 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "couldn't load wp-cache meta file", 5 );
   157 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "couldn't load wp-cache meta file", 5 );
   103 			return true;
   158 			return true;
   104 		}
   159 		}
   105 		if ( is_array( $meta ) == false ) {
   160 		if ( is_array( $meta ) == false ) {
   106 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 );
   161 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 );
   107 			@unlink( $meta_pathname );
   162 			@unlink( $meta_pathname );
   108 			@unlink( $cache_file );
   163 			@unlink( $cache_file );
   109 			return true;
   164 			return true;
   110 		}
   165 		}
   111 		$cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file );
   166 	} else {
   112 		// Sometimes the gzip headers are lost. If this is a gzip capable client, send those headers.
   167 		// last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
   113 		if( $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) {
   168 		$filename = supercache_filename();
   114 			$meta[ 'headers' ][ 'Content-Encoding' ] =  'Content-Encoding: ' . $wp_cache_gzip_encoding;
   169 		$file = get_current_url_supercache_dir() . $filename;
   115 			$meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
   170 		$phpfile = get_current_url_supercache_dir() . $filename . ".php";
   116 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "added gzip headers before serving.", 1 );
   171 		$serving_supercache = false;
   117 		}
   172 		if ( file_exists( $file ) ) {
   118 		foreach ($meta[ 'headers' ] as $t => $header) {
   173 			$serving_supercache = 'html';
   119 			// godaddy fix, via http://blog.gneu.org/2008/05/wp-supercache-on-godaddy/ and http://www.littleredrails.com/blog/2007/09/08/using-wp-cache-on-godaddy-500-error/
   174 		} elseif ( file_exists( $phpfile ) ) {
   120 			if( strpos( $header, 'Last-Modified:' ) === false ) 
   175 			$serving_supercache = 'php';
   121 				header($header);
   176 		}
   122 		}
   177 		if ( false == isset( $wp_cache_home_path ) )
   123 		header( 'WP-Super-Cache: WP-Cache' );
   178 			$wp_cache_home_path = '/';
   124 		if ( $meta[ 'dynamic' ] ) {
   179 		if ( 
       
   180 			( 
       
   181 				$wp_cache_request_uri == $wp_cache_home_path || 
       
   182 				( $wp_cache_slash_check && substr( $wp_cache_request_uri, -1 ) == '/' ) || 
       
   183 				( $wp_cache_slash_check == 0 && substr( $wp_cache_request_uri, -1 ) != '/' ) 
       
   184 			) && 
       
   185 			( wp_cache_get_cookies_values() == '' && empty( $_GET ) && $serving_supercache ) )
       
   186 		{
       
   187 			if ( isset( $wp_cache_disable_utf8 ) == false || $wp_cache_disable_utf8 == 0 )
       
   188 				header( "Content-type: text/html; charset=UTF-8" ); 
       
   189 			header( "Vary: Accept-Encoding, Cookie" );
       
   190 			header( "Cache-Control: max-age=3, must-revalidate" );
       
   191 			header( "WP-Super-Cache: Served supercache file from PHP" );
       
   192 			if ( file_exists( $file . '.gz' ) && $wp_cache_gzip_encoding ) {
       
   193 				$file = $file . '.gz';
       
   194 				header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
       
   195 				header( 'Content-Length: ' . filesize( $file ) );
       
   196 			} elseif ( $serving_supercache == 'html' && $wp_supercache_304 ) {
       
   197 				header( 'Content-Length: ' . filesize( $file ) );
       
   198 			}
       
   199 
       
   200 			if ( $serving_supercache == 'html' && $wp_supercache_304 ) {
       
   201 				if ( function_exists( 'apache_request_headers' ) ) {
       
   202 					$request = apache_request_headers();
       
   203 					$remote_mod_time = $request[ 'If-Modified-Since' ];
       
   204 				} else {
       
   205 					$remote_mod_time = $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ];
       
   206 				}
       
   207 				$local_mod_time = gmdate("D, d M Y H:i:s",filemtime( $file )).' GMT';
       
   208 				if ( $remote_mod_time == $local_mod_time ) {
       
   209 					header("HTTP/1.0 304 Not Modified");
       
   210 					exit();
       
   211 				}
       
   212 				header( 'Last-Modified: ' . $local_mod_time );
       
   213 			}
       
   214 			if ( $serving_supercache == 'html' ) {
       
   215 				readfile( $file );
       
   216 				if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Served page from supercache file using PHP.", 5 );
       
   217 				exit();
       
   218 			} elseif ( $serving_supercache == 'php' ) {
       
   219 				$cachefiledata = file_get_contents($phpfile); 
       
   220 				if ( $cache_compression and $wp_cache_gzip_encoding ) {
       
   221 					ob_start("ob_gzhandler");
       
   222 					eval( '?>' . $cachefiledata . '<?php ' ); 
       
   223 					echo "\n<!-- Compression = gzip -->\n";
       
   224 					ob_end_flush();
       
   225 					if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Served compressed dynamic page from supercache file using PHP. File: $file", 5 ); 
       
   226 				} else {
       
   227 					eval( '?>' . $cachefiledata . '<?php ' ); 
       
   228 					if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Served dynamic page from supercache file using PHP. File: $file", 5 ); 
       
   229 				}
       
   230 				exit();
       
   231 			}
       
   232 		} else {
       
   233 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file exists. Must generate a new one.", 5 );
       
   234 			return false;
       
   235 		}
       
   236 	}
       
   237 	$cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file );
       
   238 	// Sometimes the gzip headers are lost. Make sure html returned isn't compressed!
       
   239 	if ( $cache_compression && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) {
       
   240 		$ungzip = true;
       
   241 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "GZIP headers not found. Force uncompressed output.", 1 );
       
   242 	} else {
       
   243 		$ungzip = false;
       
   244 	}
       
   245 	foreach ($meta[ 'headers' ] as $t => $header) {
       
   246 		// godaddy fix, via http://blog.gneu.org/2008/05/wp-supercache-on-godaddy/ and http://www.littleredrails.com/blog/2007/09/08/using-wp-cache-on-godaddy-500-error/
       
   247 		if( strpos( $header, 'Last-Modified:' ) === false ) 
       
   248 			header($header);
       
   249 	}
       
   250 	header( 'WP-Super-Cache: Served legacy cache file' );
       
   251 	if ( $wp_cache_object_cache ) { 
       
   252 		if ( $cache ) {
       
   253 			if ( $ungzip ) {
       
   254 				$uncompressed = gzuncompress( $cache );
       
   255 				if ( $uncompressed ) {
       
   256 					if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from object cache", 1 );
       
   257 					$cache = $uncompressed;
       
   258 					unset( $uncompressed );
       
   259 				}
       
   260 			}
       
   261 			if ( isset( $meta[ 'dynamic' ] ) && $meta[ 'dynamic' ] ) {
       
   262 				if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file from object cache", 5 );
       
   263 				eval( '?>' . $cache . '<?php ' );
       
   264 			} else {
       
   265 				if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file from object cache", 5 );
       
   266 				echo $cache;
       
   267 			}
       
   268 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
       
   269 			die();
       
   270 		}
       
   271 	} else {
       
   272 		if ( isset( $meta[ 'dynamic' ] ) ) {
   125 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file", 5 );
   273 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file", 5 );
   126 			include($cache_file);
   274 			if ( $ungzip ) {
       
   275 				$cache = file_get_contents( $cache_file );
       
   276 				$uncompressed = @gzuncompress( $cache );
       
   277 				if ( $uncompressed ) {
       
   278 					if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 );
       
   279 					unset( $cache );
       
   280 					eval( '?>' . $uncompressed . '<?php ' );
       
   281 				} else {
       
   282 					eval( '?>' . $cache . '<?php ' );
       
   283 				}
       
   284 			} else {
       
   285 				include( $cache_file );
       
   286 			}
   127 		} else {
   287 		} else {
   128 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file", 5 );
   288 			if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file", 5 );
   129 			readfile( $cache_file );
   289 			if ( $ungzip ) {
       
   290 				$cache = file_get_contents( $cache_file );
       
   291 				$uncompressed = gzuncompress( $cache );
       
   292 				if ( $uncompressed ) {
       
   293 					if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 );
       
   294 					echo $uncompressed;
       
   295 				} else {
       
   296 					echo $cache;
       
   297 				}
       
   298 			} else {
       
   299 				readfile( $cache_file );
       
   300 			}
   130 		}
   301 		}
   131 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
   302 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
   132 		die();
   303 		die();
   133 	} else {
       
   134 		if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file exists. Must generate a new one.", 5 );
       
   135 	}
   304 	}
   136 }
   305 }
   137 
   306 
   138 if(defined('DOING_CRON')) {
   307 if(defined('DOING_CRON')) {
   139 	extract( wp_super_cache_init() );
   308 	extract( wp_super_cache_init() );
   167 }
   336 }
   168 
   337 
   169 function wp_cache_get_cookies_values() {
   338 function wp_cache_get_cookies_values() {
   170 	$string = '';
   339 	$string = '';
   171 	while ($key = key($_COOKIE)) {
   340 	while ($key = key($_COOKIE)) {
   172 		if ( preg_match( "/^wp-postpass|^wordpress|^comment_author_/", $key ) ) {
   341 		if ( preg_match( "/^wp-postpass|^wordpress_logged_in|^comment_author_/", $key ) ) {
   173 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cookie detected: $key", 5 );
   342 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cookie detected: $key", 5 );
   174 			$string .= $_COOKIE[ $key ] . ",";
   343 			$string .= $_COOKIE[ $key ] . ",";
   175 		}
   344 		}
   176 		next($_COOKIE);
   345 		next($_COOKIE);
   177 	}
   346 	}
   187 	$wp_supercache_actions[ $action ][] = $func;
   356 	$wp_supercache_actions[ $action ][] = $func;
   188 }
   357 }
   189 
   358 
   190 function do_cacheaction( $action, $value = '' ) {
   359 function do_cacheaction( $action, $value = '' ) {
   191 	global $wp_supercache_actions;
   360 	global $wp_supercache_actions;
   192 	if( is_array( $wp_supercache_actions[ $action ] ) ) {
   361 
       
   362 	if ( !isset( $wp_supercache_actions ) || !is_array( $wp_supercache_actions ) )
       
   363 		return $value;
       
   364 
       
   365 	if( array_key_exists($action, $wp_supercache_actions) && is_array( $wp_supercache_actions[ $action ] ) ) {
   193 		$actions = $wp_supercache_actions[ $action ];
   366 		$actions = $wp_supercache_actions[ $action ];
   194 		foreach( $actions as $func ) {
   367 		foreach( $actions as $func ) {
   195 			$value = $func( $value );
   368 			if ( is_array( $func ) ) {
       
   369 				$value = $func[0]->{$func[1]}( $value );
       
   370 			} else {
       
   371 				$value = $func( $value ); 
       
   372 			}
   196 		}
   373 		}
   197 	}
   374 	}
   198 
   375 
   199 	return $value;
   376 	return $value;
       
   377 }
       
   378 
       
   379 function wp_cache_mobile_group( $user_agent ) {
       
   380 	global $wp_cache_mobile_groups;
       
   381 	foreach( (array)$wp_cache_mobile_groups as $name => $group ) {
       
   382 		foreach( (array)$group as $browser ) {
       
   383 			$browser = trim( strtolower( $browser ) );
       
   384 			if ( $browser != '' && strstr( $user_agent, $browser ) ) {
       
   385 				return $browser;
       
   386 			}
       
   387 		}
       
   388 	}
       
   389 	return "mobile";
   200 }
   390 }
   201 
   391 
   202 // From http://wordpress.org/extend/plugins/wordpress-mobile-edition/ by Alex King
   392 // From http://wordpress.org/extend/plugins/wordpress-mobile-edition/ by Alex King
   203 function wp_cache_check_mobile( $cache_key ) {
   393 function wp_cache_check_mobile( $cache_key ) {
   204 	global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_whitelist;
   394 	global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes;
   205 	if( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled )
   395 	if ( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled )
   206 		return $cache_key;
   396 		return $cache_key;
   207 
   397 
   208 	if (!isset($_SERVER["HTTP_USER_AGENT"])) {
   398 	// allow plugins to short circuit mobile check. Cookie, extra UA checks?
       
   399 	switch( do_cacheaction( 'wp_cache_check_mobile', $cache_key ) ) {
       
   400 	case "normal":
   209 		return $cache_key;
   401 		return $cache_key;
   210 	}
   402 		break;
   211 	$whitelist = explode( ',', $wp_cache_mobile_whitelist );
   403 	case "mobile":
   212 	foreach ($whitelist as $browser) {
   404 		return $cache_key . "-mobile";
   213 		if (strstr($_SERVER["HTTP_USER_AGENT"], trim($browser))) {
   405 		break;
   214 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "whitelst mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
   406 	}
   215 			return $cache_key;
   407 
   216 		}
   408 	if ( !isset( $_SERVER[ "HTTP_USER_AGENT" ] ) ) {
   217 	}
   409 		return $cache_key;
       
   410 	}
       
   411 
       
   412 	if ( do_cacheaction( 'disable_mobile_check', false ) )
       
   413 		return $cache_key;
   218 
   414 
   219 	$browsers = explode( ',', $wp_cache_mobile_browsers );
   415 	$browsers = explode( ',', $wp_cache_mobile_browsers );
       
   416 	$user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
   220 	foreach ($browsers as $browser) {
   417 	foreach ($browsers as $browser) {
   221 		if (strstr($_SERVER["HTTP_USER_AGENT"], trim( $browser ))) {
   418 		if ( strstr( $user_agent, trim( strtolower( $browser ) ) ) ) {
   222 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
   419 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
   223 			return $cache_key . $browser;
   420 			return $cache_key . '-' . wp_cache_mobile_group( $user_agent );
   224 		}
   421 		}
   225 	}
   422 	}
       
   423 	if (isset($_SERVER['HTTP_X_WAP_PROFILE']) ) 
       
   424 		return $cache_key . '-' . $_SERVER['HTTP_X_WAP_PROFILE'];
       
   425 	if (isset($_SERVER['HTTP_PROFILE']) )
       
   426 		return $cache_key . '-' . $_SERVER['HTTP_PROFILE'];
       
   427 
       
   428 	if ( isset( $wp_cache_mobile_prefixes ) ) {
       
   429 		$browsers = explode( ',', $wp_cache_mobile_prefixes );
       
   430 		foreach ($browsers as $browser_prefix) {
       
   431 			if ( substr($user_agent, 0, 4) == $browser_prefix ) {
       
   432 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser (prefix) detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
       
   433 				return $cache_key . '-' . $browser_prefix;
       
   434 			}
       
   435 		}
       
   436 	}
       
   437 	$accept = isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ? strtolower( $_SERVER[ 'HTTP_ACCEPT' ] ) : '';
       
   438 	if (strpos($accept, 'wap') !== false) {
       
   439 		return $cache_key . '-' . 'wap';
       
   440 	}
       
   441 
       
   442 	if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) {
       
   443 		return $cache_key . '-' . 'operamini';
       
   444 	}
       
   445 
   226 	return $cache_key;
   446 	return $cache_key;
   227 }
   447 }
   228 
   448 
   229 function wp_cache_debug( $message, $level = 1 ) {
   449 function wp_cache_debug( $message, $level = 1 ) {
   230 	global $wp_cache_debug_level, $wp_cache_debug_log, $wp_cache_debug_email, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug;
   450 	global $wp_cache_debug_log, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug;
   231 
   451 
   232 	if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug == false )
   452 	if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug == false )
   233 		return false;
   453 		return false;
   234 
   454 
   235 	if ( isset( $wp_cache_debug_email ) == false && isset( $wp_cache_debug_log ) == false )
   455 	if ( isset( $wp_cache_debug_log ) == false )
   236 		return false;
       
   237 
       
   238 	if ( isset( $wp_cache_debug_level ) == false )
       
   239 		$wp_cache_debug_level = 1;
       
   240 	if ( $wp_cache_debug_level < $level )
       
   241 		return false;
   456 		return false;
   242 
   457 
   243 	if ( isset( $wp_cache_debug_ip ) && $wp_cache_debug_ip != '' && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] )
   458 	if ( isset( $wp_cache_debug_ip ) && $wp_cache_debug_ip != '' && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] )
   244 		return false;
   459 		return false;
   245 
   460 
   246 	if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' ) {
   461 	if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' ) {
   247 		error_log( date( 'H:i:s' ) . " " . $_SERVER[ 'REQUEST_URI' ] . " " . $message . "\n", 3, $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) ) );
   462 		error_log( date( 'H:i:s' ) . " " . $_SERVER[ 'REQUEST_URI' ] . " " . $message . "\n", 3, $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) ) );
   248 	}
       
   249 
       
   250 	if ( isset( $wp_cache_debug_email ) && $wp_cache_debug_email != ''  ) {
       
   251 		$message .= "\n\nDisable these emails by commenting out or deleting the line containing\n\$wp_cache_debug_email in wp-content/wp-cache-config.php on your server.\n";
       
   252 		mail( $wp_cache_debug_email, '[' . addslashes( $_SERVER[ 'HTTP_HOST' ] ) . "] WP Super Cache Debug", $message );
       
   253 	}
   463 	}
   254 }
   464 }
   255 
   465 
   256 function wp_cache_user_agent_is_rejected() {
   466 function wp_cache_user_agent_is_rejected() {
   257 	global $cache_rejected_user_agent;
   467 	global $cache_rejected_user_agent;
   258 
   468 
   259 	if (!function_exists('apache_request_headers')) return false;
   469 	if (!function_exists('apache_request_headers')) return false;
   260 	$headers = apache_request_headers();
   470 	$headers = apache_request_headers();
   261 	if (!isset($headers["User-Agent"])) return false;
   471 	if (!isset($headers["User-Agent"])) return false;
       
   472 	if ( false == is_array( $cache_rejected_user_agent ) )
       
   473 		return false;
   262 	foreach ($cache_rejected_user_agent as $expr) {
   474 	foreach ($cache_rejected_user_agent as $expr) {
   263 		if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
   475 		if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
   264 			return true;
   476 			return true;
   265 	}
   477 	}
   266 	return false;
   478 	return false;
   267 }
   479 }
   268 
   480 
       
   481 function get_supercache_dir( $blog_id = 0 ) {
       
   482 	global $cache_path;
       
   483 	if ( $blog_id == 0 ) {
       
   484 		$home = get_option( 'home' );
       
   485 	} else {
       
   486 		$home = get_blog_option( $blog_id, 'home' );
       
   487 	}
       
   488 	return apply_filters( 'wp_super_cache_supercachedir', $cache_path . 'supercache/' . trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', str_replace( 'https://', '', $home ) ) ) ) ) );
       
   489 }
       
   490 function get_current_url_supercache_dir( $post_id = 0 ) {
       
   491 	global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST, $wp_cache_home_path;
       
   492 	static $saved_supercache_dir = array();
       
   493 
       
   494 	if ( isset( $saved_supercache_dir[ $post_id ] ) ) {
       
   495 		return $saved_supercache_dir[ $post_id ];
       
   496 	}
       
   497 
       
   498 	$DONOTREMEMBER = 0;
       
   499 	if ( $post_id != 0 ) {
       
   500 		$site_url = site_url();
       
   501 		$permalink = get_permalink( $post_id );
       
   502 		if ( false === strpos( $permalink, $site_url ) ) {
       
   503 			/*
       
   504 			 * Sometimes site_url doesn't return the siteurl. See http://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made
       
   505 			*/
       
   506 			$DONOTREMEMBER = 1;
       
   507 			if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "get_current_url_supercache_dir: warning! site_url ($site_url) not found in permalink ($permalink).", 1 );
       
   508 			if ( false === strpos( $permalink, $WPSC_HTTP_HOST ) ) {
       
   509 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ($permalink). ", 1 );
       
   510 				$p = parse_url( $permalink );
       
   511 				if ( is_array( $p ) ) {
       
   512 					$uri = $p[ 'path' ];
       
   513 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "get_current_url_supercache_dir: WARNING! Using $uri as permalink. Used parse_url.", 1 );
       
   514 				} else {
       
   515 					if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) could not be understood by parse_url. Using front page.", 1 );
       
   516 					$uri = '';
       
   517 				}
       
   518 			} else {
       
   519 				if ( isset( $_SERVER[ 'HTTPS' ] ) )
       
   520 					$protocol = ( 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) ? 'https://' : 'http://';
       
   521 				else
       
   522 					$protocol = 'http://';
       
   523 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$WPSC_HTTP_HOST}) and $protocol from permalink ($permalink). Is the url right?", 1 );
       
   524 				$uri = str_replace( $WPSC_HTTP_HOST, '', $permalink );
       
   525 				$uri = str_replace( $protocol, '', $uri );
       
   526 			}
       
   527 		} else {
       
   528 			$uri = str_replace( $site_url, '', $permalink );
       
   529 			if ( strpos( $uri, $wp_cache_home_path ) !== 0 )
       
   530 				$uri = rtrim( $wp_cache_home_path, '/' ) . $uri;
       
   531 		}
       
   532 	} else {
       
   533 		$uri = strtolower( $wp_cache_request_uri );
       
   534 	}
       
   535 	$uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $uri ) ) ) );
       
   536 	$uri = str_replace( '\\', '', $uri );
       
   537 	$dir = preg_replace( '/:.*$/', '',  $WPSC_HTTP_HOST ) . $uri; // To avoid XSS attacks
       
   538 	if ( function_exists( "apply_filters" ) ) {
       
   539 		$dir = apply_filters( 'supercache_dir', $dir );
       
   540 	} else {
       
   541 		$dir = do_cacheaction( 'supercache_dir', $dir );
       
   542 	}
       
   543 	$dir = $cache_path . 'supercache/' . $dir . '/';
       
   544 	if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) {
       
   545 		$dir = ABSPATH . $uri . '/';
       
   546 	}
       
   547 	$dir = str_replace( '//', '/', $dir );
       
   548 	if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "supercache dir: $dir", 5 );
       
   549 	if ( $DONOTREMEMBER == 0 )
       
   550 		$saved_supercache_dir[ $post_id ] = $dir;
       
   551 	return $dir;
       
   552 }
       
   553 
       
   554 function get_all_supercache_filenames( $dir = '' ) {
       
   555 	global $wp_cache_mobile_enabled;
       
   556 
       
   557 	$filenames = array( 'index.html', 'index-https.html', 'index.html.php' );
       
   558 
       
   559 	if ( $dir != '' && isset( $wp_cache_mobile_enabled ) && $wp_cache_mobile_enabled ) {
       
   560 		// open directory and look for index-*.html files
       
   561 		if ( is_dir( $dir ) && $dh = opendir( $dir ) ) {
       
   562 			while ( ( $file = readdir( $dh ) ) !== false ) {
       
   563 				if ( substr( $file, 0, 6 ) == 'index-' && strpos( $file, '.html' ) )
       
   564 					$filenames[] = $file;
       
   565 			}
       
   566 			closedir( $dh );
       
   567 		}
       
   568 	}
       
   569 
       
   570 	if ( function_exists( "apply_filters" ) ) {
       
   571 		$filenames = apply_filters( 'all_supercache_filenames', $filenames );
       
   572 	} else {
       
   573 		$filenames = do_cacheaction( 'all_supercache_filenames', $filenames );
       
   574 	}
       
   575 
       
   576 	foreach( $filenames as $file ) {
       
   577 		$out[] = $file;
       
   578 		$out[] = $file . '.gz';
       
   579 	}
       
   580 
       
   581 	return $out;
       
   582 }
       
   583 
       
   584 function supercache_filename() {
       
   585 	//Add support for https and http caching
       
   586 	$is_https = ( ( isset( $_SERVER[ 'HTTPS' ] ) && 'on' ==  strtolower( $_SERVER[ 'HTTPS' ] ) ) || ( isset( $_SERVER[ 'HTTP_X_FORWARDED_PROTO' ] ) && 'https' == strtolower( $_SERVER[ 'HTTP_X_FORWARDED_PROTO' ] ) ) ); //Also supports https requests coming from an nginx reverse proxy
       
   587 	$extra_str = $is_https ? '-https' : '';
       
   588 
       
   589 	if ( function_exists( "apply_filters" ) ) {
       
   590 		$extra_str = apply_filters( 'supercache_filename_str', $extra_str );
       
   591 	} else {
       
   592 		$extra_str = do_cacheaction( 'supercache_filename_str', $extra_str );
       
   593 	}
       
   594 	$filename = 'index' . $extra_str . '.html';
       
   595 
       
   596 	return $filename;
       
   597 }
       
   598 
       
   599 function get_oc_version() {
       
   600 	$wp_cache_oc_key = wp_cache_get( "wp_cache_oc_key" );
       
   601 	if ( ! $wp_cache_oc_key ) {
       
   602 		$wp_cache_oc_key[ 'key' ] = reset_oc_version();
       
   603 	} elseif ( $wp_cache_oc_key[ 'ts' ] < time() - 600 )
       
   604 		wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $wp_cache_oc_key[ 'key' ] ) );
       
   605 	return $wp_cache_oc_key[ 'key' ];
       
   606 }
       
   607 
       
   608 function reset_oc_version( $version = 1 ) {
       
   609 	if ( $version == 1 )
       
   610 		$version = mt_rand();
       
   611 	wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $version ) );
       
   612 
       
   613 	return $version;
       
   614 }
       
   615 
       
   616 function get_oc_key( $url = false ) {
       
   617 	global $wp_cache_gzip_encoding, $WPSC_HTTP_HOST;
       
   618 
       
   619 	if ( $url ) {
       
   620 		$key = intval( $_SERVER[ 'SERVER_PORT' ] ) . strtolower( preg_replace( '/:.*$/', '',  $WPSC_HTTP_HOST ) ) . $url;
       
   621 	} else {
       
   622 		$key = get_current_url_supercache_dir();
       
   623 	}
       
   624 	return $key . $wp_cache_gzip_encoding . get_oc_version();
       
   625 }
       
   626 
       
   627 function wp_supercache_cache_for_admins() {
       
   628 	if ( isset( $_GET[ 'preview' ] ) || function_exists( "is_admin" ) && is_admin() )
       
   629 		return $cookies;
       
   630 
       
   631 	if ( false == do_cacheaction( 'wp_supercache_remove_cookies', true ) )
       
   632 		return true;
       
   633 
       
   634 	$cookie_keys = array( 'wordpress_logged_in', 'comment_author_' );
       
   635 	if ( defined( 'LOGGED_IN_COOKIE' ) )
       
   636 		$cookie_keys[] = constant( 'LOGGED_IN_COOKIE' );
       
   637 	reset( $_COOKIE );
       
   638 	foreach( $_COOKIE as $cookie => $val ) {
       
   639 		reset( $cookie_keys );
       
   640 		foreach( $cookie_keys as $key ) {
       
   641 			if ( strpos( $cookie, $key ) !== FALSE ) {
       
   642 				if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Removing auth from $_COOKIE to allow caching for logged user (' . $cookie . ')', 5 );
       
   643 				unset( $_COOKIE[ $cookie ] );
       
   644 			}
       
   645 		}
       
   646 	}
       
   647 }
       
   648 
   269 ?>
   649 ?>