wp-super-cache
Last commit date
languages
14 years ago
plugins
15 years ago
Changelog.txt
14 years ago
advanced-cache.php
15 years ago
ossdl-cdn.php
14 years ago
readme.txt
11 years ago
wp-cache-base.php
14 years ago
wp-cache-config-sample.php
14 years ago
wp-cache-phase1.php
14 years ago
wp-cache-phase2.php
14 years ago
wp-cache.php
11 years ago
wp-super-cache.pot
14 years ago
wp-cache-phase1.php
585 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 | include( WPCACHEHOME . 'wp-cache-base.php'); |
| 14 | |
| 15 | if( $blogcacheid != '' ) { |
| 16 | $blog_cache_dir = str_replace( '//', '/', $cache_path . "blogs/" . $blogcacheid . '/' ); |
| 17 | } else { |
| 18 | $blog_cache_dir = $cache_path; |
| 19 | } |
| 20 | |
| 21 | $mutex_filename = 'wp_cache_mutex.lock'; |
| 22 | $new_cache = false; |
| 23 | |
| 24 | if( !isset( $wp_cache_plugins_dir ) ) |
| 25 | $wp_cache_plugins_dir = WPCACHEHOME . 'plugins'; |
| 26 | |
| 27 | require_once( WPCACHEHOME . 'wp-cache-phase2.php'); |
| 28 | |
| 29 | if ( isset( $_GET[ 'donotcachepage' ] ) && isset( $cache_page_secret ) && $_GET[ 'donotcachepage' ] == $cache_page_secret ) { |
| 30 | $cache_enabled = false; |
| 31 | define( 'DONOTCACHEPAGE', 1 ); |
| 32 | } |
| 33 | if ( isset( $wp_cache_make_known_anon ) && $wp_cache_make_known_anon ) |
| 34 | wp_supercache_cache_for_admins(); |
| 35 | |
| 36 | $plugins = glob( $wp_cache_plugins_dir . '/*.php' ); |
| 37 | if( is_array( $plugins ) ) { |
| 38 | foreach ( $plugins as $plugin ) { |
| 39 | if( is_file( $plugin ) ) |
| 40 | require_once( $plugin ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | do_cacheaction( 'cache_init' ); |
| 45 | |
| 46 | if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST') |
| 47 | return true; |
| 48 | |
| 49 | $file_expired = false; |
| 50 | $cache_filename = ''; |
| 51 | $meta_file = ''; |
| 52 | $wp_cache_gzip_encoding = ''; |
| 53 | |
| 54 | $gzipped = 0; |
| 55 | $gzsize = 0; |
| 56 | |
| 57 | function gzip_accepted(){ |
| 58 | 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 |
| 59 | return false; |
| 60 | |
| 61 | if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) return false; |
| 62 | return 'gzip'; |
| 63 | } |
| 64 | |
| 65 | if ($cache_compression) { |
| 66 | $wp_cache_gzip_encoding = gzip_accepted(); |
| 67 | } |
| 68 | |
| 69 | add_cacheaction( 'supercache_filename_str', 'wp_cache_check_mobile' ); |
| 70 | |
| 71 | $wp_cache_request_uri = $_SERVER[ 'REQUEST_URI' ]; // Cache this in case any plugin modifies it. |
| 72 | |
| 73 | if ( $wp_cache_object_cache ) { |
| 74 | if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) ) |
| 75 | return; |
| 76 | |
| 77 | wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here. |
| 78 | |
| 79 | if ( ! is_object( $wp_object_cache ) ) |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if( false == @is_dir( $blog_cache_dir ) ) { |
| 84 | @mkdir( $cache_path . "blogs" ); |
| 85 | @mkdir( $blog_cache_dir ); |
| 86 | } |
| 87 | |
| 88 | if( false == @is_dir( $blog_cache_dir . 'meta' ) ) |
| 89 | @mkdir( $blog_cache_dir . 'meta' ); |
| 90 | |
| 91 | |
| 92 | $wp_start_time = microtime(); |
| 93 | |
| 94 | function get_wp_cache_key( $url = false ) { |
| 95 | global $wp_cache_request_uri, $wp_cache_gzip_encoding; |
| 96 | if ( !$url ) |
| 97 | $url = $wp_cache_request_uri; |
| 98 | return do_cacheaction( 'wp_cache_key', $_SERVER['HTTP_HOST'] . intval( $_SERVER[ 'SERVER_PORT' ] ) . preg_replace('/#.*$/', '', str_replace( '/index.php', '/', $url ) ) . $wp_cache_gzip_encoding . wp_cache_get_cookies_values() ); |
| 99 | } |
| 100 | |
| 101 | function wp_super_cache_init() { |
| 102 | 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; |
| 103 | |
| 104 | $wp_cache_key = get_wp_cache_key(); |
| 105 | $key = $blogcacheid . md5( $wp_cache_key ); |
| 106 | $wp_cache_key = $blogcacheid . $wp_cache_key; |
| 107 | |
| 108 | $cache_filename = $file_prefix . $key . '.html'; |
| 109 | $meta_file = $file_prefix . $key . '.meta'; |
| 110 | $cache_file = realpath( $blog_cache_dir . $cache_filename ); |
| 111 | $meta_pathname = realpath( $blog_cache_dir . 'meta/' . $meta_file ); |
| 112 | return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' ); |
| 113 | } |
| 114 | |
| 115 | function wp_cache_serve_cache_file() { |
| 116 | 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; |
| 117 | global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path; |
| 118 | |
| 119 | extract( wp_super_cache_init() ); |
| 120 | |
| 121 | if ( wp_cache_user_agent_is_rejected() ) { |
| 122 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file served as user agent rejected.", 5 ); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if ( $wp_cache_object_cache && wp_cache_get_cookies_values() == '' ) { |
| 127 | if ( !empty( $_GET ) ) { |
| 128 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Non empty GET request. Not serving request from object cache", 1 ); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | $oc_key = get_oc_key(); |
| 133 | $meta_filename = $oc_key . ".meta"; |
| 134 | if ( gzip_accepted() ) { |
| 135 | $oc_key .= ".gz"; |
| 136 | $meta_filename .= ".gz"; |
| 137 | } |
| 138 | $cache = wp_cache_get( $oc_key, 'supercache' ); |
| 139 | $meta = unserialize( wp_cache_get( $meta_filename, 'supercache' ) ); |
| 140 | if ( is_array( $meta ) == false ) { |
| 141 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Meta array from object cache corrupt. Ignoring cache.", 1 ); |
| 142 | return true; |
| 143 | } |
| 144 | } elseif ( file_exists( $cache_file ) ) { |
| 145 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "wp-cache file exists: $cache_file", 5 ); |
| 146 | if ( !( $meta = unserialize( @file_get_contents( $meta_pathname) ) ) ) { |
| 147 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "couldn't load wp-cache meta file", 5 ); |
| 148 | return true; |
| 149 | } |
| 150 | if ( is_array( $meta ) == false ) { |
| 151 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 ); |
| 152 | @unlink( $meta_pathname ); |
| 153 | @unlink( $cache_file ); |
| 154 | return true; |
| 155 | } |
| 156 | } else { |
| 157 | // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host |
| 158 | $filename = supercache_filename(); |
| 159 | $file = get_current_url_supercache_dir() . $filename; |
| 160 | $phpfile = get_current_url_supercache_dir() . $filename . ".php"; |
| 161 | $serving_supercache = false; |
| 162 | if ( file_exists( $file ) ) { |
| 163 | $serving_supercache = 'html'; |
| 164 | } elseif ( file_exists( $phpfile ) ) { |
| 165 | $serving_supercache = 'php'; |
| 166 | } |
| 167 | if ( false == isset( $wp_cache_home_path ) ) |
| 168 | $wp_cache_home_path = '/'; |
| 169 | if ( |
| 170 | ( |
| 171 | $wp_cache_request_uri == $wp_cache_home_path || |
| 172 | ( $wp_cache_slash_check && substr( $wp_cache_request_uri, -1 ) == '/' ) || |
| 173 | ( $wp_cache_slash_check == 0 && substr( $wp_cache_request_uri, -1 ) != '/' ) |
| 174 | ) && |
| 175 | ( wp_cache_get_cookies_values() == '' && empty( $_GET ) && $serving_supercache ) ) |
| 176 | { |
| 177 | header( "Content-type: text/html; charset=UTF-8" ); // UTF-8 hard coded is bad but we don't know what it is this early in the process |
| 178 | header( "Vary: Accept-Encoding, Cookie" ); |
| 179 | header( "Cache-Control: max-age=3, must-revalidate" ); |
| 180 | header( "WP-Super-Cache: Served supercache file from PHP" ); |
| 181 | if ( file_exists( $file . '.gz' ) && $wp_cache_gzip_encoding ) { |
| 182 | $file = $file . '.gz'; |
| 183 | header( 'Content-Encoding: ' . $wp_cache_gzip_encoding ); |
| 184 | header( 'Content-Length: ' . filesize( $file ) ); |
| 185 | } elseif ( $serving_supercache == 'html' && $wp_supercache_304 ) { |
| 186 | header( 'Content-Length: ' . filesize( $file ) ); |
| 187 | } |
| 188 | |
| 189 | if ( $serving_supercache == 'html' && $wp_supercache_304 ) { |
| 190 | if ( function_exists( 'apache_request_headers' ) ) { |
| 191 | $request = apache_request_headers(); |
| 192 | $remote_mod_time = $request[ 'If-Modified-Since' ]; |
| 193 | } else { |
| 194 | $remote_mod_time = $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ]; |
| 195 | } |
| 196 | $local_mod_time = gmdate("D, d M Y H:i:s",filemtime( $file )).' GMT'; |
| 197 | if ( $remote_mod_time == $local_mod_time ) { |
| 198 | header("HTTP/1.0 304 Not Modified"); |
| 199 | exit(); |
| 200 | } |
| 201 | header( 'Last-Modified: ' . $local_mod_time ); |
| 202 | } |
| 203 | if ( $serving_supercache == 'html' ) { |
| 204 | readfile( $file ); |
| 205 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Served page from supercache file using PHP.", 5 ); |
| 206 | exit(); |
| 207 | } elseif ( $serving_supercache == 'php' ) { |
| 208 | $cachefiledata = file_get_contents($phpfile); |
| 209 | if ( $cache_compression and $wp_cache_gzip_encoding ) { |
| 210 | ob_start("ob_gzhandler"); |
| 211 | eval( '?>' . $cachefiledata . '<?php ' ); |
| 212 | echo "\n<!-- Compression = gzip -->\n"; |
| 213 | ob_end_flush(); |
| 214 | 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 ); |
| 215 | } else { |
| 216 | eval( '?>' . $cachefiledata . '<?php ' ); |
| 217 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Served dynamic page from supercache file using PHP. File: $file", 5 ); |
| 218 | } |
| 219 | exit(); |
| 220 | } |
| 221 | } else { |
| 222 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file exists. Must generate a new one.", 5 ); |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 | $cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file ); |
| 227 | // Sometimes the gzip headers are lost. Make sure html returned isn't compressed! |
| 228 | if ( $cache_compression && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) { |
| 229 | $ungzip = true; |
| 230 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "GZIP headers not found. Force uncompressed output.", 1 ); |
| 231 | } else { |
| 232 | $ungzip = false; |
| 233 | } |
| 234 | foreach ($meta[ 'headers' ] as $t => $header) { |
| 235 | // 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/ |
| 236 | if( strpos( $header, 'Last-Modified:' ) === false ) |
| 237 | header($header); |
| 238 | } |
| 239 | header( 'WP-Super-Cache: Served legacy cache file' ); |
| 240 | if ( $wp_cache_object_cache ) { |
| 241 | if ( $cache ) { |
| 242 | if ( $ungzip ) { |
| 243 | $uncompressed = gzuncompress( $cache ); |
| 244 | if ( $uncompressed ) { |
| 245 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from object cache", 1 ); |
| 246 | $cache = $uncompressed; |
| 247 | unset( $uncompressed ); |
| 248 | } |
| 249 | } |
| 250 | if ( isset( $meta[ 'dynamic' ] ) && $meta[ 'dynamic' ] ) { |
| 251 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file from object cache", 5 ); |
| 252 | eval( '?>' . $cache . '<?php ' ); |
| 253 | } else { |
| 254 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file from object cache", 5 ); |
| 255 | echo $cache; |
| 256 | } |
| 257 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 ); |
| 258 | die(); |
| 259 | } |
| 260 | } else { |
| 261 | if ( $meta[ 'dynamic' ] ) { |
| 262 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file", 5 ); |
| 263 | if ( $ungzip ) { |
| 264 | $cache = file_get_contents( $cache_file ); |
| 265 | $uncompressed = @gzuncompress( $cache ); |
| 266 | if ( $uncompressed ) { |
| 267 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 ); |
| 268 | unset( $cache ); |
| 269 | eval( '?>' . $uncompressed . '<?php ' ); |
| 270 | } else { |
| 271 | eval( '?>' . $cache . '<?php ' ); |
| 272 | } |
| 273 | } else { |
| 274 | include( $cache_file ); |
| 275 | } |
| 276 | } else { |
| 277 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file", 5 ); |
| 278 | if ( $ungzip ) { |
| 279 | $cache = file_get_contents( $cache_file ); |
| 280 | $uncompressed = gzuncompress( $cache ); |
| 281 | if ( $uncompressed ) { |
| 282 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 ); |
| 283 | echo $uncompressed; |
| 284 | } else { |
| 285 | echo $cache; |
| 286 | } |
| 287 | } else { |
| 288 | readfile( $cache_file ); |
| 289 | } |
| 290 | } |
| 291 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 ); |
| 292 | die(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if(defined('DOING_CRON')) { |
| 297 | extract( wp_super_cache_init() ); |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | if ( !isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && false == $wp_super_cache_late_init ) ) { |
| 302 | wp_cache_serve_cache_file(); |
| 303 | } |
| 304 | |
| 305 | function wp_cache_postload() { |
| 306 | global $cache_enabled, $wp_super_cache_late_init, $wp_super_cache_debug; |
| 307 | |
| 308 | if ( !$cache_enabled || isset( $_GET[ 'preview' ] ) ) |
| 309 | return true; |
| 310 | |
| 311 | if ( isset( $wp_super_cache_late_init ) && true == $wp_super_cache_late_init ) { |
| 312 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Init: add wp_cache_serve_cache_file to init", 3 ); |
| 313 | add_action( 'init', 'wp_cache_late_loader', 9999 ); |
| 314 | } else { |
| 315 | wp_super_cache_init(); |
| 316 | wp_cache_phase2(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | function wp_cache_late_loader() { |
| 321 | global $wp_super_cache_debug; |
| 322 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Loader running on init", 3 ); |
| 323 | wp_cache_serve_cache_file(); |
| 324 | wp_cache_phase2(); |
| 325 | } |
| 326 | |
| 327 | function wp_cache_get_cookies_values() { |
| 328 | $string = ''; |
| 329 | while ($key = key($_COOKIE)) { |
| 330 | if ( preg_match( "/^wp-postpass|^wordpress_logged_in|^comment_author_/", $key ) ) { |
| 331 | if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cookie detected: $key", 5 ); |
| 332 | $string .= $_COOKIE[ $key ] . ","; |
| 333 | } |
| 334 | next($_COOKIE); |
| 335 | } |
| 336 | reset($_COOKIE); |
| 337 | |
| 338 | // If you use this hook, make sure you update your .htaccess rules with the same conditions |
| 339 | $string = do_cacheaction( 'wp_cache_get_cookies_values', $string ); |
| 340 | return $string; |
| 341 | } |
| 342 | |
| 343 | function add_cacheaction( $action, $func ) { |
| 344 | global $wp_supercache_actions; |
| 345 | $wp_supercache_actions[ $action ][] = $func; |
| 346 | } |
| 347 | |
| 348 | function do_cacheaction( $action, $value = '' ) { |
| 349 | global $wp_supercache_actions; |
| 350 | |
| 351 | if ( !isset( $wp_supercache_actions ) || !is_array( $wp_supercache_actions ) ) |
| 352 | return $value; |
| 353 | |
| 354 | if( array_key_exists($action, $wp_supercache_actions) && is_array( $wp_supercache_actions[ $action ] ) ) { |
| 355 | $actions = $wp_supercache_actions[ $action ]; |
| 356 | foreach( $actions as $func ) { |
| 357 | if ( is_array( $func ) ) { |
| 358 | $value = $func[0]->{$func[1]}( $value ); |
| 359 | } else { |
| 360 | $value = $func( $value ); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return $value; |
| 366 | } |
| 367 | |
| 368 | function wp_cache_mobile_group( $user_agent ) { |
| 369 | global $wp_cache_mobile_groups; |
| 370 | foreach( (array)$wp_cache_mobile_groups as $name => $group ) { |
| 371 | foreach( (array)$group as $browser ) { |
| 372 | $browser = trim( strtolower( $browser ) ); |
| 373 | if ( $browser != '' && strstr( $user_agent, $browser ) ) { |
| 374 | return $browser; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | return "mobile"; |
| 379 | } |
| 380 | |
| 381 | // From http://wordpress.org/extend/plugins/wordpress-mobile-edition/ by Alex King |
| 382 | function wp_cache_check_mobile( $cache_key ) { |
| 383 | global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes; |
| 384 | if ( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled ) |
| 385 | return $cache_key; |
| 386 | |
| 387 | if ( !isset( $_SERVER[ "HTTP_USER_AGENT" ] ) ) { |
| 388 | return $cache_key; |
| 389 | } |
| 390 | |
| 391 | $browsers = explode( ',', $wp_cache_mobile_browsers ); |
| 392 | $user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] ); |
| 393 | foreach ($browsers as $browser) { |
| 394 | if ( strstr( $user_agent, trim( strtolower( $browser ) ) ) ) { |
| 395 | if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 ); |
| 396 | return $cache_key . '-' . wp_cache_mobile_group( $user_agent ); |
| 397 | } |
| 398 | } |
| 399 | if (isset($_SERVER['HTTP_X_WAP_PROFILE']) ) |
| 400 | return $cache_key . '-' . $_SERVER['HTTP_X_WAP_PROFILE']; |
| 401 | if (isset($_SERVER['HTTP_PROFILE']) ) |
| 402 | return $cache_key . '-' . $_SERVER['HTTP_PROFILE']; |
| 403 | |
| 404 | if ( isset( $wp_cache_mobile_prefixes ) ) { |
| 405 | $browsers = explode( ',', $wp_cache_mobile_prefixes ); |
| 406 | foreach ($browsers as $browser_prefix) { |
| 407 | if ( substr($user_agent, 0, 4) == $browser_prefix ) { |
| 408 | if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser (prefix) detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 ); |
| 409 | return $cache_key . '-' . $browser_prefix; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | $accept = strtolower($_SERVER['HTTP_ACCEPT']); |
| 414 | if (strpos($accept, 'wap') !== false) { |
| 415 | return $cache_key . '-' . 'wap'; |
| 416 | } |
| 417 | |
| 418 | if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) { |
| 419 | return $cache_key . '-' . 'operamini'; |
| 420 | } |
| 421 | return $cache_key; |
| 422 | } |
| 423 | |
| 424 | function wp_cache_debug( $message, $level = 1 ) { |
| 425 | global $wp_cache_debug_level, $wp_cache_debug_log, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug; |
| 426 | |
| 427 | if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug == false ) |
| 428 | return false; |
| 429 | |
| 430 | if ( isset( $wp_cache_debug_log ) == false ) |
| 431 | return false; |
| 432 | |
| 433 | if ( isset( $wp_cache_debug_level ) == false ) |
| 434 | $wp_cache_debug_level = 1; |
| 435 | if ( $wp_cache_debug_level < $level ) |
| 436 | return false; |
| 437 | |
| 438 | if ( isset( $wp_cache_debug_ip ) && $wp_cache_debug_ip != '' && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] ) |
| 439 | return false; |
| 440 | |
| 441 | if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' ) { |
| 442 | error_log( date( 'H:i:s' ) . " " . $_SERVER[ 'REQUEST_URI' ] . " " . $message . "\n", 3, $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) ) ); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | function wp_cache_user_agent_is_rejected() { |
| 447 | global $cache_rejected_user_agent; |
| 448 | |
| 449 | if (!function_exists('apache_request_headers')) return false; |
| 450 | $headers = apache_request_headers(); |
| 451 | if (!isset($headers["User-Agent"])) return false; |
| 452 | foreach ($cache_rejected_user_agent as $expr) { |
| 453 | if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr)) |
| 454 | return true; |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | function get_supercache_dir() { |
| 460 | global $cache_path; |
| 461 | return $cache_path . 'supercache/' . trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) ); |
| 462 | } |
| 463 | function get_current_url_supercache_dir( $post_id = 0 ) { |
| 464 | global $cached_direct_pages, $cache_path, $wp_cache_request_uri; |
| 465 | static $saved_supercache_dir = array(); |
| 466 | |
| 467 | if ( isset( $saved_supercache_dir[ $post_id ] ) ) { |
| 468 | return $saved_supercache_dir[ $post_id ]; |
| 469 | } |
| 470 | |
| 471 | if ( $post_id != 0 ) { |
| 472 | $uri = str_replace( site_url(), '', get_permalink( $post_id ) ); |
| 473 | } else { |
| 474 | $uri = strtolower( $wp_cache_request_uri ); |
| 475 | } |
| 476 | $uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $uri ) ) ) ); |
| 477 | $uri = str_replace( '\\', '', $uri ); |
| 478 | $dir = preg_replace( '/:.*$/', '', $_SERVER["HTTP_HOST"] ) . $uri; // To avoid XSS attacks |
| 479 | if ( function_exists( "apply_filters" ) ) { |
| 480 | $dir = apply_filters( 'supercache_dir', $dir ); |
| 481 | } else { |
| 482 | $dir = do_cacheaction( 'supercache_dir', $dir ); |
| 483 | } |
| 484 | $dir = $cache_path . 'supercache/' . $dir . '/'; |
| 485 | if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) { |
| 486 | $dir = ABSPATH . $uri . '/'; |
| 487 | } |
| 488 | $dir = str_replace( '//', '/', $dir ); |
| 489 | if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "supercache dir: $dir", 5 ); |
| 490 | $saved_supercache_dir[ $post_id ] = $dir; |
| 491 | return $dir; |
| 492 | } |
| 493 | |
| 494 | function get_all_supercache_filenames( $dir = '' ) { |
| 495 | global $wp_cache_mobile_enabled; |
| 496 | |
| 497 | $filenames = array( 'index.html', 'index-https.html', 'index.html.php' ); |
| 498 | |
| 499 | if ( $dir != '' && isset( $wp_cache_mobile_enabled ) && $wp_cache_mobile_enabled ) { |
| 500 | // open directory and look for index-*.html files |
| 501 | if ( is_dir( $dir ) && $dh = opendir( $dir ) ) { |
| 502 | while ( ( $file = readdir( $dh ) ) !== false ) { |
| 503 | if ( substr( $file, 0, 6 ) == 'index-' && strpos( $file, '.html' ) ) |
| 504 | $filenames[] = $file; |
| 505 | } |
| 506 | closedir( $dh ); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if ( function_exists( "apply_filters" ) ) { |
| 511 | $filenames = apply_filters( 'all_supercache_filenames', $filenames ); |
| 512 | } else { |
| 513 | $filenames = do_cacheaction( 'all_supercache_filenames', $filenames ); |
| 514 | } |
| 515 | |
| 516 | foreach( $filenames as $file ) { |
| 517 | $out[] = $file; |
| 518 | $out[] = $file . '.gz'; |
| 519 | } |
| 520 | |
| 521 | return $out; |
| 522 | } |
| 523 | |
| 524 | function supercache_filename() { |
| 525 | //Add support for https and http caching |
| 526 | $is_https = ('on' == strtolower($_SERVER['HTTPS']) || 'https' == strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])); //Also supports https requests coming from an nginx reverse proxy |
| 527 | $extra_str = $is_https ? '-https' : ''; |
| 528 | |
| 529 | if ( function_exists( "apply_filters" ) ) { |
| 530 | $extra_str = apply_filters( 'supercache_filename_str', $extra_str ); |
| 531 | } else { |
| 532 | $extra_str = do_cacheaction( 'supercache_filename_str', $extra_str ); |
| 533 | } |
| 534 | $filename = 'index' . $extra_str . '.html'; |
| 535 | |
| 536 | return $filename; |
| 537 | } |
| 538 | |
| 539 | function get_oc_version() { |
| 540 | $wp_cache_oc_key = wp_cache_get( "wp_cache_oc_key" ); |
| 541 | if ( ! $wp_cache_oc_key ) { |
| 542 | $wp_cache_oc_key[ 'key' ] = reset_oc_version(); |
| 543 | } elseif ( $wp_cache_oc_key[ 'ts' ] < time() - 600 ) |
| 544 | wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $wp_cache_oc_key[ 'key' ] ) ); |
| 545 | return $wp_cache_oc_key[ 'key' ]; |
| 546 | } |
| 547 | |
| 548 | function reset_oc_version( $version = 1 ) { |
| 549 | if ( $version == 1 ) |
| 550 | $version = mt_rand(); |
| 551 | wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $version ) ); |
| 552 | |
| 553 | return $version; |
| 554 | } |
| 555 | |
| 556 | function get_oc_key( $url = false ) { |
| 557 | global $wp_cache_gzip_encoding; |
| 558 | |
| 559 | if ( $url ) { |
| 560 | $key = intval( $_SERVER[ 'SERVER_PORT' ] ) . strtolower( preg_replace( '/:.*$/', '', $_SERVER[ "HTTP_HOST" ] ) ) . $url; |
| 561 | } else { |
| 562 | $key = get_current_url_supercache_dir(); |
| 563 | } |
| 564 | return $key . $wp_cache_gzip_encoding . get_oc_version(); |
| 565 | } |
| 566 | |
| 567 | function wp_supercache_cache_for_admins() { |
| 568 | if ( isset( $_GET[ 'preview' ] ) || function_exists( "is_admin" ) && is_admin() ) |
| 569 | return $cookies; |
| 570 | |
| 571 | $cookie_keys = array( 'wordpress_logged_in', 'comment_author_' ); |
| 572 | reset( $_COOKIE ); |
| 573 | foreach( $_COOKIE as $cookie => $val ) { |
| 574 | reset( $cookie_keys ); |
| 575 | foreach( $cookie_keys as $key ) { |
| 576 | if ( strpos( $cookie, $key ) !== FALSE ) { |
| 577 | 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 ); |
| 578 | unset( $_COOKIE[ $cookie ] ); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | ?> |
| 585 |