wp-super-cache
Last commit date
languages
13 years ago
plugins
10 years ago
Changelog.txt
10 years ago
advanced-cache.php
11 years ago
ossdl-cdn.php
11 years ago
readme.txt
10 years ago
wp-cache-base.php
10 years ago
wp-cache-config-sample.php
11 years ago
wp-cache-phase1.php
10 years ago
wp-cache-phase2.php
10 years ago
wp-cache.php
10 years ago
wp-super-cache.pot
12 years ago
wp-cache-phase1.php
743 lines
| 1 | <?php |
| 2 | //error_reporting(E_ERROR | E_PARSE); // uncomment to debug this file! |
| 3 | // Pre-2.6 compatibility |
| 4 | if( !defined('WP_CONTENT_DIR') ) |
| 5 | define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); |
| 6 | |
| 7 | if( !include( WP_CONTENT_DIR . '/wp-cache-config.php' ) ) |
| 8 | return false; |
| 9 | |
| 10 | if( !defined( 'WPCACHEHOME' ) ) |
| 11 | define('WPCACHEHOME', dirname(__FILE__).'/'); |
| 12 | |
| 13 | |
| 14 | include( WPCACHEHOME . 'wp-cache-base.php'); |
| 15 | |
| 16 | if( $blogcacheid != '' ) { |
| 17 | $blog_cache_dir = str_replace( '//', '/', $cache_path . "blogs/" . $blogcacheid . '/' ); |
| 18 | } else { |
| 19 | $blog_cache_dir = $cache_path; |
| 20 | } |
| 21 | |
| 22 | $wp_cache_phase1_loaded = true; |
| 23 | |
| 24 | $mutex_filename = 'wp_cache_mutex.lock'; |
| 25 | $new_cache = false; |
| 26 | |
| 27 | if( !isset( $wp_cache_plugins_dir ) ) |
| 28 | $wp_cache_plugins_dir = WPCACHEHOME . 'plugins'; |
| 29 | |
| 30 | require_once( WPCACHEHOME . 'wp-cache-phase2.php'); |
| 31 | |
| 32 | if ( isset( $_GET[ 'donotcachepage' ] ) && isset( $cache_page_secret ) && $_GET[ 'donotcachepage' ] == $cache_page_secret ) { |
| 33 | $cache_enabled = false; |
| 34 | define( 'DONOTCACHEPAGE', 1 ); |
| 35 | } |
| 36 | |
| 37 | $plugins = glob( $wp_cache_plugins_dir . '/*.php' ); |
| 38 | if( is_array( $plugins ) ) { |
| 39 | foreach ( $plugins as $plugin ) { |
| 40 | if( is_file( $plugin ) ) |
| 41 | require_once( $plugin ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if ( isset( $wp_cache_make_known_anon ) && $wp_cache_make_known_anon ) |
| 46 | wp_supercache_cache_for_admins(); |
| 47 | |
| 48 | do_cacheaction( 'cache_init' ); |
| 49 | |
| 50 | if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST') |
| 51 | return true; |
| 52 | |
| 53 | $file_expired = false; |
| 54 | $cache_filename = ''; |
| 55 | $meta_file = ''; |
| 56 | $wp_cache_gzip_encoding = ''; |
| 57 | |
| 58 | $gzipped = 0; |
| 59 | $gzsize = 0; |
| 60 | |
| 61 | function gzip_accepted(){ |
| 62 | 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 |
| 63 | return false; |
| 64 | |
| 65 | if ( !isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) || ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) && strpos( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ], 'gzip' ) === false ) ) return false; |
| 66 | return 'gzip'; |
| 67 | } |
| 68 | |
| 69 | if ($cache_compression) { |
| 70 | $wp_cache_gzip_encoding = gzip_accepted(); |
| 71 | } |
| 72 | |
| 73 | add_cacheaction( 'supercache_filename_str', 'wp_cache_check_mobile' ); |
| 74 | |
| 75 | $wp_cache_request_uri = $_SERVER[ 'REQUEST_URI' ]; // Cache this in case any plugin modifies it. |
| 76 | |
| 77 | if ( $wp_cache_object_cache ) { |
| 78 | if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) ) |
| 79 | return; |
| 80 | |
| 81 | wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here. |
| 82 | |
| 83 | if ( ! is_object( $wp_object_cache ) ) |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | function setup_blog_cache_dir() { |
| 88 | global $blog_cache_dir, $cache_path; |
| 89 | if( false == @is_dir( $blog_cache_dir ) ) { |
| 90 | @mkdir( $cache_path . "blogs" ); |
| 91 | @mkdir( $blog_cache_dir ); |
| 92 | } |
| 93 | |
| 94 | if( false == @is_dir( $blog_cache_dir . 'meta' ) ) |
| 95 | @mkdir( $blog_cache_dir . 'meta' ); |
| 96 | } |
| 97 | |
| 98 | $wp_start_time = microtime(); |
| 99 | |
| 100 | function get_wp_cache_key( $url = false ) { |
| 101 | global $wp_cache_request_uri, $wp_cache_gzip_encoding, $WPSC_HTTP_HOST; |
| 102 | if ( !$url ) |
| 103 | $url = $wp_cache_request_uri; |
| 104 | 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() ); |
| 105 | } |
| 106 | |
| 107 | function wp_super_cache_init() { |
| 108 | global $wp_cache_key, $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $meta_pathname, $wp_cache_gzip_encoding, $meta; |
| 109 | |
| 110 | $wp_cache_key = get_wp_cache_key(); |
| 111 | $key = $blogcacheid . md5( $wp_cache_key ); |
| 112 | $wp_cache_key = $blogcacheid . $wp_cache_key; |
| 113 | |
| 114 | $cache_filename = $file_prefix . $key . '.php'; |
| 115 | $meta_file = $file_prefix . $key . '.php'; |
| 116 | $cache_file = realpath( $blog_cache_dir . $cache_filename ); |
| 117 | $meta_pathname = realpath( $blog_cache_dir . 'meta/' . $meta_file ); |
| 118 | return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' ); |
| 119 | } |
| 120 | |
| 121 | function wp_cache_serve_cache_file() { |
| 122 | global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $meta_pathname, $wp_cache_gzip_encoding, $meta; |
| 123 | global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path, $wp_cache_no_cache_for_get; |
| 124 | global $wp_cache_disable_utf8, $wp_cache_mfunc_enabled; |
| 125 | |
| 126 | extract( wp_super_cache_init() ); |
| 127 | |
| 128 | if ( wp_cache_user_agent_is_rejected() ) { |
| 129 | wp_cache_debug( "No wp-cache file served as user agent rejected.", 5 ); |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | if ( $wp_cache_no_cache_for_get && false == empty( $_GET ) ) { |
| 134 | wp_cache_debug( "Non empty GET request. Caching disabled on settings page. " . json_encode( $_GET ), 1 ); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | if ( $wp_cache_object_cache && wp_cache_get_cookies_values() == '' ) { |
| 139 | if ( !empty( $_GET ) ) { |
| 140 | wp_cache_debug( "Non empty GET request. Not serving request from object cache. " . json_encode( $_GET ), 1 ); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | $oc_key = get_oc_key(); |
| 145 | $meta_filename = $oc_key . ".meta"; |
| 146 | if ( gzip_accepted() ) { |
| 147 | $oc_key .= ".gz"; |
| 148 | $meta_filename .= ".gz"; |
| 149 | } |
| 150 | $cache = wp_cache_get( $oc_key, 'supercache' ); |
| 151 | $meta = json_decode( wp_cache_get( $meta_filename, 'supercache' ), true ); |
| 152 | if ( is_array( $meta ) == false ) { |
| 153 | wp_cache_debug( "Meta array from object cache corrupt. Ignoring cache.", 1 ); |
| 154 | return true; |
| 155 | } |
| 156 | } elseif ( file_exists( $cache_file ) ) { |
| 157 | wp_cache_debug( "wp-cache file exists: $cache_file", 5 ); |
| 158 | if ( !( $meta = json_decode( wp_cache_get_legacy_cache( $meta_pathname ), true ) ) ) { |
| 159 | wp_cache_debug( "couldn't load wp-cache meta file", 5 ); |
| 160 | return true; |
| 161 | } |
| 162 | if ( is_array( $meta ) == false ) { |
| 163 | wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 ); |
| 164 | @unlink( $meta_pathname ); |
| 165 | @unlink( $cache_file ); |
| 166 | return true; |
| 167 | } |
| 168 | } else { |
| 169 | // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host |
| 170 | $filename = supercache_filename(); |
| 171 | $file = get_current_url_supercache_dir() . $filename; |
| 172 | if ( false == file_exists( $file ) ) { |
| 173 | wp_cache_debug( "No Super Cache file found for current URL: $file" ); |
| 174 | return false; |
| 175 | } elseif ( false == empty( $_GET ) ) { |
| 176 | wp_cache_debug( "GET array not empty. Cannot serve a supercache file. " . json_encode( $_GET ) ); |
| 177 | return false; |
| 178 | } elseif ( wp_cache_get_cookies_values() != '' ) { |
| 179 | wp_cache_debug( "Cookies found. Cannot serve a supercache file. " . wp_cache_get_cookies_values() ); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | if ( isset( $wp_cache_mfunc_enabled ) == false ) |
| 184 | $wp_cache_mfunc_enabled = 0; |
| 185 | |
| 186 | if ( false == isset( $wp_cache_home_path ) ) |
| 187 | $wp_cache_home_path = '/'; |
| 188 | |
| 189 | // make sure ending slashes are ok |
| 190 | if ( $wp_cache_request_uri == $wp_cache_home_path || ( $wp_cache_slash_check && substr( $wp_cache_request_uri, -1 ) == '/' ) || ( $wp_cache_slash_check == 0 && substr( $wp_cache_request_uri, -1 ) != '/' ) ) { |
| 191 | |
| 192 | if ( $wp_cache_mfunc_enabled == 0 ) { |
| 193 | // get data from file |
| 194 | if ( $wp_cache_gzip_encoding ) { |
| 195 | if ( file_exists( $file . '.gz' ) ) { |
| 196 | $cachefiledata = file_get_contents( $file . '.gz' ); |
| 197 | wp_cache_debug( "Fetched gzip static page data from supercache file using PHP. File: $file.gz" ); |
| 198 | } else { |
| 199 | $cachefiledata = gzencode( file_get_contents( $file ), 6, FORCE_GZIP ); |
| 200 | wp_cache_debug( "Fetched static page data from supercache file using PHP and gzipped it. File: $file" ); |
| 201 | } |
| 202 | } else { |
| 203 | $cachefiledata = file_get_contents( $file ); |
| 204 | wp_cache_debug( "Fetched static page data from supercache file using PHP. File: $file" ); |
| 205 | } |
| 206 | } else { |
| 207 | // get dynamic data from filtered file |
| 208 | $cachefiledata = do_cacheaction( 'wpsc_cachedata', file_get_contents( $file ) ); |
| 209 | if ( $wp_cache_gzip_encoding ) { |
| 210 | $cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP ); |
| 211 | wp_cache_debug( "Fetched dynamic page data from supercache file using PHP and gzipped it. File: $file" ); |
| 212 | } else { |
| 213 | wp_cache_debug( "Fetched dynamic page data from supercache file using PHP. File: $file" ); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if ( isset( $wp_cache_disable_utf8 ) == false || $wp_cache_disable_utf8 == 0 ) |
| 218 | header( "Content-type: text/html; charset=UTF-8" ); |
| 219 | |
| 220 | header( "Vary: Accept-Encoding, Cookie" ); |
| 221 | header( "Cache-Control: max-age=3, must-revalidate" ); |
| 222 | header( "WP-Super-Cache: Served supercache file from PHP" ); |
| 223 | $size = function_exists( 'mb_strlen' ) ? mb_strlen( $cachefiledata, '8bit' ) : strlen( $cachefiledata ); |
| 224 | if ( $wp_cache_gzip_encoding ) { |
| 225 | header( 'Content-Encoding: ' . $wp_cache_gzip_encoding ); |
| 226 | header( 'Content-Length: ' . $size ); |
| 227 | } elseif ( $wp_supercache_304 ) { |
| 228 | header( 'Content-Length: ' . $size ); |
| 229 | } |
| 230 | |
| 231 | // don't try to match modified dates if using dynamic code. |
| 232 | if ( $wp_cache_mfunc_enabled == 0 && $wp_supercache_304 ) { |
| 233 | if ( function_exists( 'apache_request_headers' ) ) { |
| 234 | $request = apache_request_headers(); |
| 235 | $remote_mod_time = ( isset ( $request[ 'If-Modified-Since' ] ) ) ? $request[ 'If-Modified-Since' ] : 0; |
| 236 | } else { |
| 237 | if ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) |
| 238 | $remote_mod_time = $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ]; |
| 239 | else |
| 240 | $remote_mod_time = 0; |
| 241 | } |
| 242 | $local_mod_time = gmdate("D, d M Y H:i:s",filemtime( $file )).' GMT'; |
| 243 | if ( $remote_mod_time != 0 && $remote_mod_time == $local_mod_time ) { |
| 244 | header("HTTP/1.0 304 Not Modified"); |
| 245 | exit(); |
| 246 | } |
| 247 | header( 'Last-Modified: ' . $local_mod_time ); |
| 248 | } |
| 249 | echo $cachefiledata; |
| 250 | exit(); |
| 251 | } else { |
| 252 | wp_cache_debug( "No wp-cache file exists. Must generate a new one." ); |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | $cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file ); |
| 258 | // Sometimes the gzip headers are lost. Make sure html returned isn't compressed! |
| 259 | if ( $cache_compression && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) { |
| 260 | $ungzip = true; |
| 261 | wp_cache_debug( "GZIP headers not found. Force uncompressed output.", 1 ); |
| 262 | } else { |
| 263 | $ungzip = false; |
| 264 | } |
| 265 | foreach ($meta[ 'headers' ] as $t => $header) { |
| 266 | // 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/ |
| 267 | if( strpos( $header, 'Last-Modified:' ) === false ) |
| 268 | header($header); |
| 269 | } |
| 270 | header( 'WP-Super-Cache: Served legacy cache file' ); |
| 271 | if ( $wp_cache_object_cache ) { |
| 272 | if ( $cache ) { |
| 273 | if ( $ungzip ) { |
| 274 | // attempt to uncompress the cached file just in case it's gzipped |
| 275 | $uncompressed = gzuncompress( $cache ); |
| 276 | if ( $uncompressed ) { |
| 277 | wp_cache_debug( "Uncompressed gzipped cache file from object cache", 1 ); |
| 278 | $cache = $uncompressed; |
| 279 | unset( $uncompressed ); |
| 280 | } |
| 281 | } |
| 282 | if ( isset( $meta[ 'dynamic' ] ) && $meta[ 'dynamic' ] ) { |
| 283 | wp_cache_debug( "Serving wp-cache dynamic file from object cache", 5 ); |
| 284 | echo do_cacheaction( 'wpsc_cachedata', $cache ); |
| 285 | } else { |
| 286 | wp_cache_debug( "Serving wp-cache static file from object cache", 5 ); |
| 287 | echo $cache; |
| 288 | } |
| 289 | wp_cache_debug( "exit request", 5 ); |
| 290 | die(); |
| 291 | } |
| 292 | } else { |
| 293 | if ( isset( $meta[ 'dynamic' ] ) ) { |
| 294 | wp_cache_debug( "Serving wp-cache dynamic file", 5 ); |
| 295 | if ( $ungzip ) { |
| 296 | // attempt to uncompress the cached file just in case it's gzipped |
| 297 | $cache = wp_cache_get_legacy_cache( $cache_file ); |
| 298 | $uncompressed = @gzuncompress( $cache ); |
| 299 | if ( $uncompressed ) { |
| 300 | wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 ); |
| 301 | unset( $cache ); |
| 302 | echo do_cacheaction( 'wpsc_cachedata', $uncompressed ); |
| 303 | } else { |
| 304 | echo do_cacheaction( 'wpsc_cachedata', $cache ); |
| 305 | } |
| 306 | } else { |
| 307 | echo do_cacheaction( 'wpsc_cachedata', wp_cache_get_legacy_cache( $cache_file ) ); |
| 308 | } |
| 309 | } else { |
| 310 | wp_cache_debug( "Serving wp-cache static file", 5 ); |
| 311 | if ( $ungzip ) { |
| 312 | $cache = wp_cache_get_legacy_cache( $cache_file ); |
| 313 | $uncompressed = gzuncompress( $cache ); |
| 314 | if ( $uncompressed ) { |
| 315 | wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 ); |
| 316 | echo $uncompressed; |
| 317 | } else { |
| 318 | echo $cache; |
| 319 | } |
| 320 | } else { |
| 321 | echo( wp_cache_get_legacy_cache( $cache_file ) ); |
| 322 | } |
| 323 | } |
| 324 | wp_cache_debug( "exit request", 5 ); |
| 325 | die(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | function wp_cache_get_legacy_cache( $cache_file ) { |
| 330 | return substr( @file_get_contents( $cache_file ), 15 ); |
| 331 | } |
| 332 | |
| 333 | if(defined('DOING_CRON')) { |
| 334 | extract( wp_super_cache_init() ); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | if ( !isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && false == $wp_super_cache_late_init ) ) { |
| 339 | wp_cache_serve_cache_file(); |
| 340 | } |
| 341 | |
| 342 | function wp_cache_postload() { |
| 343 | global $cache_enabled, $wp_super_cache_late_init; |
| 344 | |
| 345 | if ( !$cache_enabled ) |
| 346 | return true; |
| 347 | |
| 348 | if ( isset( $wp_super_cache_late_init ) && true == $wp_super_cache_late_init ) { |
| 349 | wp_cache_debug( "Supercache Late Init: add wp_cache_serve_cache_file to init", 3 ); |
| 350 | add_action( 'init', 'wp_cache_late_loader', 9999 ); |
| 351 | } else { |
| 352 | wp_super_cache_init(); |
| 353 | wp_cache_phase2(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | function wp_cache_late_loader() { |
| 358 | wp_cache_debug( "Supercache Late Loader running on init", 3 ); |
| 359 | wp_cache_serve_cache_file(); |
| 360 | wp_cache_phase2(); |
| 361 | } |
| 362 | |
| 363 | function wp_cache_get_cookies_values() { |
| 364 | static $string = ''; |
| 365 | |
| 366 | if ( $string != '' ) { |
| 367 | wp_cache_debug( "wp_cache_get_cookies_values: cached: $string" ); |
| 368 | return $string; |
| 369 | } |
| 370 | |
| 371 | $regex = "/^wp-postpass|^comment_author_"; |
| 372 | if ( defined( 'LOGGED_IN_COOKIE' ) ) |
| 373 | $regex .= "|^" . preg_quote( constant( 'LOGGED_IN_COOKIE' ) ); |
| 374 | else |
| 375 | $regex .= "|^wordpress_logged_in_"; |
| 376 | $regex .= "/"; |
| 377 | while ($key = key($_COOKIE)) { |
| 378 | if ( preg_match( $regex, $key ) ) { |
| 379 | wp_cache_debug( "wp_cache_get_cookies_values: $regex Cookie detected: $key", 5 ); |
| 380 | $string .= $_COOKIE[ $key ] . ","; |
| 381 | } |
| 382 | next($_COOKIE); |
| 383 | } |
| 384 | reset($_COOKIE); |
| 385 | |
| 386 | // If you use this hook, make sure you update your .htaccess rules with the same conditions |
| 387 | $string = do_cacheaction( 'wp_cache_get_cookies_values', $string ); |
| 388 | if ( $string != '' ) |
| 389 | $string = md5( $string ); |
| 390 | |
| 391 | wp_cache_debug( "wp_cache_get_cookies_values: return: $string", 5 ); |
| 392 | return $string; |
| 393 | } |
| 394 | |
| 395 | function add_cacheaction( $action, $func ) { |
| 396 | global $wp_supercache_actions; |
| 397 | $wp_supercache_actions[ $action ][] = $func; |
| 398 | } |
| 399 | |
| 400 | function do_cacheaction( $action, $value = '' ) { |
| 401 | global $wp_supercache_actions; |
| 402 | |
| 403 | if ( !isset( $wp_supercache_actions ) || !is_array( $wp_supercache_actions ) ) |
| 404 | return $value; |
| 405 | |
| 406 | if( array_key_exists($action, $wp_supercache_actions) && is_array( $wp_supercache_actions[ $action ] ) ) { |
| 407 | $actions = $wp_supercache_actions[ $action ]; |
| 408 | foreach( $actions as $func ) { |
| 409 | if ( is_array( $func ) ) { |
| 410 | $value = $func[0]->{$func[1]}( $value ); |
| 411 | } else { |
| 412 | $value = $func( $value ); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return $value; |
| 418 | } |
| 419 | |
| 420 | function wp_cache_mobile_group( $user_agent ) { |
| 421 | global $wp_cache_mobile_groups; |
| 422 | foreach( (array)$wp_cache_mobile_groups as $name => $group ) { |
| 423 | foreach( (array)$group as $browser ) { |
| 424 | $browser = trim( strtolower( $browser ) ); |
| 425 | if ( $browser != '' && strstr( $user_agent, $browser ) ) { |
| 426 | return $browser; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | return "mobile"; |
| 431 | } |
| 432 | |
| 433 | // From http://wordpress.org/plugins/wordpress-mobile-edition/ by Alex King |
| 434 | function wp_cache_check_mobile( $cache_key ) { |
| 435 | global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes; |
| 436 | if ( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled ) |
| 437 | return $cache_key; |
| 438 | |
| 439 | wp_cache_debug( "wp_cache_check_mobile: $cache_key" ); |
| 440 | |
| 441 | // allow plugins to short circuit mobile check. Cookie, extra UA checks? |
| 442 | switch( do_cacheaction( 'wp_cache_check_mobile', $cache_key ) ) { |
| 443 | case "normal": |
| 444 | wp_cache_debug( "wp_cache_check_mobile: desktop user agent detected by wp_cache_check_mobile action" ); |
| 445 | return $cache_key; |
| 446 | break; |
| 447 | case "mobile": |
| 448 | wp_cache_debug( "wp_cache_check_mobile: mobile user agent detected by wp_cache_check_mobile action" ); |
| 449 | return $cache_key . "-mobile"; |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | if ( !isset( $_SERVER[ "HTTP_USER_AGENT" ] ) ) { |
| 454 | return $cache_key; |
| 455 | } |
| 456 | |
| 457 | if ( do_cacheaction( 'disable_mobile_check', false ) ) { |
| 458 | wp_cache_debug( "wp_cache_check_mobile: disable_mobile_check disabled mobile check" ); |
| 459 | return $cache_key; |
| 460 | } |
| 461 | |
| 462 | $browsers = explode( ',', $wp_cache_mobile_browsers ); |
| 463 | $user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] ); |
| 464 | foreach ($browsers as $browser) { |
| 465 | if ( strstr( $user_agent, trim( strtolower( $browser ) ) ) ) { |
| 466 | wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 ); |
| 467 | return $cache_key . '-' . wp_cache_mobile_group( $user_agent ); |
| 468 | } |
| 469 | } |
| 470 | if (isset($_SERVER['HTTP_X_WAP_PROFILE']) ) |
| 471 | return $cache_key . '-' . $_SERVER['HTTP_X_WAP_PROFILE']; |
| 472 | if (isset($_SERVER['HTTP_PROFILE']) ) |
| 473 | return $cache_key . '-' . $_SERVER['HTTP_PROFILE']; |
| 474 | |
| 475 | if ( isset( $wp_cache_mobile_prefixes ) ) { |
| 476 | $browsers = explode( ',', $wp_cache_mobile_prefixes ); |
| 477 | foreach ($browsers as $browser_prefix) { |
| 478 | if ( substr($user_agent, 0, 4) == $browser_prefix ) { |
| 479 | wp_cache_debug( "mobile browser (prefix) detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 ); |
| 480 | return $cache_key . '-' . $browser_prefix; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | $accept = isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ? strtolower( $_SERVER[ 'HTTP_ACCEPT' ] ) : ''; |
| 485 | if (strpos($accept, 'wap') !== false) { |
| 486 | return $cache_key . '-' . 'wap'; |
| 487 | } |
| 488 | |
| 489 | if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) { |
| 490 | return $cache_key . '-' . 'operamini'; |
| 491 | } |
| 492 | |
| 493 | return $cache_key; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Add a log message to the file, if debugging is turned on |
| 498 | * |
| 499 | * @param $message string The message that should be added to the log |
| 500 | * @param $level int |
| 501 | */ |
| 502 | function wp_cache_debug( $message, $level = 1 ) { |
| 503 | global $wp_cache_debug_log, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug; |
| 504 | |
| 505 | // If either of the debug or log globals aren't set, then we can stop |
| 506 | if ( !isset($wp_super_cache_debug) |
| 507 | || !isset($wp_cache_debug_log) ) |
| 508 | return false; |
| 509 | |
| 510 | // If either the debug or log globals are false or empty, we can stop |
| 511 | if ( $wp_super_cache_debug == false |
| 512 | || $wp_cache_debug_log == '' ) |
| 513 | return false; |
| 514 | |
| 515 | // If the debug_ip has been set, but it doesn't match the ip of the requester |
| 516 | // then we can stop. |
| 517 | if ( isset($wp_cache_debug_ip) |
| 518 | && $wp_cache_debug_ip != '' |
| 519 | && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] ) |
| 520 | return false; |
| 521 | |
| 522 | // Log message: Date URI Message |
| 523 | $log_message = date('H:i:s') . " " . getmypid() . " {$_SERVER['REQUEST_URI']} {$message}\n\r"; |
| 524 | // path to the log file in the cache folder |
| 525 | $log_file = $cache_path . str_replace('/', '', str_replace('..', '', $wp_cache_debug_log)); |
| 526 | |
| 527 | error_log( $log_message, 3, $log_file ); |
| 528 | } |
| 529 | |
| 530 | function wp_cache_user_agent_is_rejected() { |
| 531 | global $cache_rejected_user_agent; |
| 532 | |
| 533 | if (!function_exists('apache_request_headers')) return false; |
| 534 | $headers = apache_request_headers(); |
| 535 | if (!isset($headers["User-Agent"])) return false; |
| 536 | if ( false == is_array( $cache_rejected_user_agent ) ) |
| 537 | return false; |
| 538 | foreach ($cache_rejected_user_agent as $expr) { |
| 539 | if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr)) |
| 540 | return true; |
| 541 | } |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | function get_supercache_dir( $blog_id = 0 ) { |
| 546 | global $cache_path; |
| 547 | if ( $blog_id == 0 ) { |
| 548 | $home = get_option( 'home' ); |
| 549 | } else { |
| 550 | $home = get_blog_option( $blog_id, 'home' ); |
| 551 | } |
| 552 | return trailingslashit( apply_filters( 'wp_super_cache_supercachedir', $cache_path . 'supercache/' . trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', str_replace( 'https://', '', $home ) ) ) ) ) ) ); |
| 553 | } |
| 554 | function get_current_url_supercache_dir( $post_id = 0 ) { |
| 555 | global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST, $wp_cache_home_path; |
| 556 | static $saved_supercache_dir = array(); |
| 557 | |
| 558 | if ( isset( $saved_supercache_dir[ $post_id ] ) ) { |
| 559 | return $saved_supercache_dir[ $post_id ]; |
| 560 | } |
| 561 | |
| 562 | $DONOTREMEMBER = 0; |
| 563 | if ( $post_id != 0 ) { |
| 564 | $site_url = site_url(); |
| 565 | $permalink = get_permalink( $post_id ); |
| 566 | if ( false === strpos( $permalink, $site_url ) ) { |
| 567 | /* |
| 568 | * Sometimes site_url doesn't return the siteurl. See http://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made |
| 569 | */ |
| 570 | $DONOTREMEMBER = 1; |
| 571 | wp_cache_debug( "get_current_url_supercache_dir: warning! site_url ($site_url) not found in permalink ($permalink).", 1 ); |
| 572 | if ( false === strpos( $permalink, $WPSC_HTTP_HOST ) ) { |
| 573 | wp_cache_debug( "get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ($permalink). ", 1 ); |
| 574 | $p = parse_url( $permalink ); |
| 575 | if ( is_array( $p ) ) { |
| 576 | $uri = $p[ 'path' ]; |
| 577 | wp_cache_debug( "get_current_url_supercache_dir: WARNING! Using $uri as permalink. Used parse_url.", 1 ); |
| 578 | } else { |
| 579 | wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) could not be understood by parse_url. Using front page.", 1 ); |
| 580 | $uri = ''; |
| 581 | } |
| 582 | } else { |
| 583 | wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$WPSC_HTTP_HOST}) and $protocol from permalink ($permalink). Is the url right?", 1 ); |
| 584 | $uri = str_replace( $WPSC_HTTP_HOST, '', $permalink ); |
| 585 | $uri = str_replace( 'http://', '', $uri ); |
| 586 | $uri = str_replace( 'https://', '', $uri ); |
| 587 | } |
| 588 | } else { |
| 589 | $uri = str_replace( $site_url, '', $permalink ); |
| 590 | if ( strpos( $uri, $wp_cache_home_path ) !== 0 ) |
| 591 | $uri = rtrim( $wp_cache_home_path, '/' ) . $uri; |
| 592 | } |
| 593 | } else { |
| 594 | $uri = strtolower( $wp_cache_request_uri ); |
| 595 | } |
| 596 | $uri = wpsc_deep_replace( array( '..', '\\', 'index.php', ), preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', preg_replace( "/(\?.*)?$/", '', $uri ) ) ); |
| 597 | $dir = preg_replace( '/:.*$/', '', $WPSC_HTTP_HOST ) . $uri; // To avoid XSS attacks |
| 598 | if ( function_exists( "apply_filters" ) ) { |
| 599 | $dir = apply_filters( 'supercache_dir', $dir ); |
| 600 | } else { |
| 601 | $dir = do_cacheaction( 'supercache_dir', $dir ); |
| 602 | } |
| 603 | $dir = $cache_path . 'supercache/' . $dir . '/'; |
| 604 | if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) { |
| 605 | $dir = ABSPATH . $uri . '/'; |
| 606 | } |
| 607 | $dir = str_replace( '..', '', str_replace( '//', '/', $dir ) ); |
| 608 | wp_cache_debug( "supercache dir: $dir", 5 ); |
| 609 | if ( $DONOTREMEMBER == 0 ) |
| 610 | $saved_supercache_dir[ $post_id ] = $dir; |
| 611 | return $dir; |
| 612 | } |
| 613 | |
| 614 | function get_all_supercache_filenames( $dir = '' ) { |
| 615 | global $wp_cache_mobile_enabled, $cache_path; |
| 616 | |
| 617 | $dir = realpath( $dir ); |
| 618 | if ( substr( $dir, 0, strlen( $cache_path ) ) != $cache_path ) |
| 619 | return array(); |
| 620 | |
| 621 | $filenames = array( 'index.html', 'index-https.html', 'index.html.php' ); |
| 622 | |
| 623 | if ( $dir != '' && isset( $wp_cache_mobile_enabled ) && $wp_cache_mobile_enabled ) { |
| 624 | // open directory and look for index-*.html files |
| 625 | if ( is_dir( $dir ) && $dh = @opendir( $dir ) ) { |
| 626 | while ( ( $file = readdir( $dh ) ) !== false ) { |
| 627 | if ( substr( $file, 0, 6 ) == 'index-' && strpos( $file, '.html' ) ) |
| 628 | $filenames[] = $file; |
| 629 | } |
| 630 | closedir( $dh ); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if ( function_exists( "apply_filters" ) ) { |
| 635 | $filenames = apply_filters( 'all_supercache_filenames', $filenames ); |
| 636 | } else { |
| 637 | $filenames = do_cacheaction( 'all_supercache_filenames', $filenames ); |
| 638 | } |
| 639 | |
| 640 | foreach( $filenames as $file ) { |
| 641 | $out[] = $file; |
| 642 | $out[] = $file . '.gz'; |
| 643 | } |
| 644 | |
| 645 | return $out; |
| 646 | } |
| 647 | |
| 648 | function supercache_filename() { |
| 649 | //Add support for https and http caching |
| 650 | $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 |
| 651 | $extra_str = $is_https ? '-https' : ''; |
| 652 | |
| 653 | if ( function_exists( "apply_filters" ) ) { |
| 654 | $extra_str = apply_filters( 'supercache_filename_str', $extra_str ); |
| 655 | } else { |
| 656 | $extra_str = do_cacheaction( 'supercache_filename_str', $extra_str ); |
| 657 | } |
| 658 | $filename = 'index' . $extra_str . '.html'; |
| 659 | |
| 660 | return $filename; |
| 661 | } |
| 662 | |
| 663 | function get_oc_version() { |
| 664 | $wp_cache_oc_key = wp_cache_get( "wp_cache_oc_key" ); |
| 665 | if ( ! $wp_cache_oc_key ) { |
| 666 | $wp_cache_oc_key[ 'key' ] = reset_oc_version(); |
| 667 | } elseif ( $wp_cache_oc_key[ 'ts' ] < time() - 600 ) |
| 668 | wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $wp_cache_oc_key[ 'key' ] ) ); |
| 669 | return $wp_cache_oc_key[ 'key' ]; |
| 670 | } |
| 671 | |
| 672 | function reset_oc_version( $version = 1 ) { |
| 673 | if ( $version == 1 ) |
| 674 | $version = mt_rand(); |
| 675 | wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $version ) ); |
| 676 | |
| 677 | return $version; |
| 678 | } |
| 679 | |
| 680 | function get_oc_key( $url = false ) { |
| 681 | global $wp_cache_gzip_encoding, $WPSC_HTTP_HOST; |
| 682 | |
| 683 | if ( $url ) { |
| 684 | $key = intval( $_SERVER[ 'SERVER_PORT' ] ) . strtolower( preg_replace( '/:.*$/', '', $WPSC_HTTP_HOST ) ) . $url; |
| 685 | } else { |
| 686 | $key = get_current_url_supercache_dir(); |
| 687 | } |
| 688 | return $key . $wp_cache_gzip_encoding . get_oc_version(); |
| 689 | } |
| 690 | |
| 691 | function wp_supercache_cache_for_admins() { |
| 692 | if ( isset( $_GET[ 'preview' ] ) || function_exists( "is_admin" ) && is_admin() ) |
| 693 | return true; |
| 694 | |
| 695 | if ( false == do_cacheaction( 'wp_supercache_remove_cookies', true ) ) |
| 696 | return true; |
| 697 | |
| 698 | $cookie_keys = array( 'wordpress_logged_in', 'comment_author_' ); |
| 699 | if ( defined( 'LOGGED_IN_COOKIE' ) ) |
| 700 | $cookie_keys[] = constant( 'LOGGED_IN_COOKIE' ); |
| 701 | reset( $_COOKIE ); |
| 702 | foreach( $_COOKIE as $cookie => $val ) { |
| 703 | reset( $cookie_keys ); |
| 704 | foreach( $cookie_keys as $key ) { |
| 705 | if ( strpos( $cookie, $key ) !== FALSE ) { |
| 706 | wp_cache_debug( 'Removing auth from $_COOKIE to allow caching for logged in user (' . $cookie . ')', 5 ); |
| 707 | unset( $_COOKIE[ $cookie ] ); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /* returns true/false depending on location of $dir. */ |
| 714 | function wp_cache_confirm_delete( $dir ) { |
| 715 | global $cache_path, $blog_cache_dir; |
| 716 | // don't allow cache_path, blog cache dir, blog meta dir, supercache. |
| 717 | $dir = realpath( $dir ); |
| 718 | if ( |
| 719 | $dir == $cache_path || |
| 720 | $dir == $blog_cache_dir || |
| 721 | $dir == $blog_cache_dir . "meta/" || |
| 722 | $dir == $cache_path . "supercache" |
| 723 | ) { |
| 724 | return false; |
| 725 | } else { |
| 726 | return true; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // copy of _deep_replace() to be used before WordPress loads |
| 731 | function wpsc_deep_replace( $search, $subject ) { |
| 732 | $subject = (string) $subject; |
| 733 | |
| 734 | $count = 1; |
| 735 | while ( $count ) { |
| 736 | $subject = str_replace( $search, '', $subject, $count ); |
| 737 | } |
| 738 | |
| 739 | return $subject; |
| 740 | } |
| 741 | |
| 742 | ?> |
| 743 |