Esempio n. 1
0
 function super_cache_invalidate()
 {
     //Now, we are actually using the referrer and not the request, with some precautions
     $GLOBALS['wp_cache_request_uri'] = substr($_SERVER['HTTP_REFERER'], stripos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) + strlen($_SERVER[''] . $_SERVER['HTTP_HOST']));
     $GLOBALS['wp_cache_request_uri'] = preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', str_replace('/index.php', '/', str_replace('..', '', preg_replace("/(\\?.*)?\$/", '', $GLOBALS['wp_cache_request_uri']))));
     // get some supercache variables
     extract(wp_super_cache_init());
     tp_logger(wp_super_cache_init());
     // this is hackery for logged in users, a cookie is added to the request somehow and gzip is not correctly set, so we forcefully fix this
     if (!$cache_file) {
         $GLOBALS['wp_cache_gzip_encoding'] = gzip_accepted();
         unset($_COOKIE[key($_COOKIE)]);
         extract(wp_super_cache_init());
         tp_logger(wp_super_cache_init());
     }
     $dir = get_current_url_supercache_dir();
     // delete possible files that we can figure out, not deleting files for other cookies for example, but will do the trick in most cases
     $cache_fname = "{$dir}index.html";
     tp_logger("attempting delete of supercache: {$cache_fname}");
     @unlink($cache_fname);
     $cache_fname = "{$dir}index.html.gz";
     tp_logger("attempting delete of supercache: {$cache_fname}");
     @unlink($cache_fname);
     tp_logger("attempting delete of wp_cache: {$cache_file}");
     @unlink($cache_file);
     tp_logger("attempting delete of wp_cache_meta: {$meta_pathname}");
     @unlink($meta_pathname);
     // go at edit pages too
     $GLOBALS['wp_cache_request_uri'] .= "?edit=1";
     extract(wp_super_cache_init());
     tp_logger(wp_super_cache_init());
     tp_logger("attempting delete of edit_wp_cache: {$cache_file}");
     @unlink($cache_file);
     tp_logger("attempting delete of edit_wp_cache_meta: {$meta_pathname}");
     @unlink($meta_pathname);
 }
function wp_cache_serve_cache_file()
{
    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;
    global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path, $wp_cache_no_cache_for_get;
    global $wp_cache_disable_utf8, $wp_cache_mfunc_enabled;
    extract(wp_super_cache_init());
    if (wp_cache_user_agent_is_rejected()) {
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("No wp-cache file served as user agent rejected.", 5);
        }
        return false;
    }
    if ($wp_cache_no_cache_for_get && false == empty($_GET)) {
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("Non empty GET request. Caching disabled on settings page. " . print_r($_GET, 1), 1);
        }
        return false;
    }
    if ($wp_cache_object_cache && wp_cache_get_cookies_values() == '') {
        if (!empty($_GET)) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Non empty GET request. Not serving request from object cache. " . print_r($_GET, 1), 1);
            }
            return false;
        }
        $oc_key = get_oc_key();
        $meta_filename = $oc_key . ".meta";
        if (gzip_accepted()) {
            $oc_key .= ".gz";
            $meta_filename .= ".gz";
        }
        $cache = wp_cache_get($oc_key, 'supercache');
        $meta = unserialize(wp_cache_get($meta_filename, 'supercache'));
        if (is_array($meta) == false) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Meta array from object cache corrupt. Ignoring cache.", 1);
            }
            return true;
        }
    } elseif (file_exists($cache_file)) {
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("wp-cache file exists: {$cache_file}", 5);
        }
        if (!($meta = unserialize(@file_get_contents($meta_pathname)))) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("couldn't load wp-cache meta file", 5);
            }
            return true;
        }
        if (is_array($meta) == false) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("meta array corrupt, deleting {$meta_pathname} and {$cache_file}", 1);
            }
            @unlink($meta_pathname);
            @unlink($cache_file);
            return true;
        }
    } else {
        // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
        $filename = supercache_filename();
        $file = get_current_url_supercache_dir() . $filename;
        if (false == file_exists($file)) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("No Super Cache file found for current URL: {$file}");
            }
            return false;
        } elseif (false == empty($_GET)) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("GET array not empty. Cannot serve a supercache file. " . print_r($_GET, 1));
            }
            return false;
        } elseif (wp_cache_get_cookies_values() != '') {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Cookies found. Cannot serve a supercache file. " . wp_cache_get_cookies_values());
            }
            return false;
        }
        if (isset($wp_cache_mfunc_enabled) == false) {
            $wp_cache_mfunc_enabled = 0;
        }
        if (false == isset($wp_cache_home_path)) {
            $wp_cache_home_path = '/';
        }
        // make sure ending slashes are ok
        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) != '/') {
            if ($wp_cache_mfunc_enabled == 0) {
                // get data from file
                if ($wp_cache_gzip_encoding) {
                    if (file_exists($file . '.gz')) {
                        $cachefiledata = file_get_contents($file . '.gz');
                        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                            wp_cache_debug("Fetched gzip static page data from supercache file using PHP. File: {$file}.gz");
                        }
                    } else {
                        $cachefiledata = gzencode(file_get_contents($file), 6, FORCE_GZIP);
                        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                            wp_cache_debug("Fetched static page data from supercache file using PHP and gzipped it. File: {$file}");
                        }
                    }
                } else {
                    $cachefiledata = file_get_contents($file);
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Fetched static page data from supercache file using PHP. File: {$file}");
                    }
                }
            } else {
                // get dynamic data from filtered file
                $cachefiledata = do_cacheaction('wpsc_cachedata', file_get_contents($file));
                if ($wp_cache_gzip_encoding) {
                    $cachefiledata = gzencode($cachefiledata, 6, FORCE_GZIP);
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Fetched dynamic page data from supercache file using PHP and gzipped it. File: {$file}");
                    }
                } else {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Fetched dynamic page data from supercache file using PHP. File: {$file}");
                    }
                }
            }
            if (isset($wp_cache_disable_utf8) == false || $wp_cache_disable_utf8 == 0) {
                header("Content-type: text/html; charset=UTF-8");
            }
            header("Vary: Accept-Encoding, Cookie");
            header("Cache-Control: max-age=3, must-revalidate");
            header("WP-Super-Cache: Served supercache file from PHP");
            $size = function_exists('mb_strlen') ? mb_strlen($cachefiledata, '8bit') : strlen($cachefiledata);
            if ($wp_cache_gzip_encoding) {
                header('Content-Encoding: ' . $wp_cache_gzip_encoding);
                header('Content-Length: ' . $size);
            } elseif ($wp_supercache_304) {
                header('Content-Length: ' . $size);
            }
            // don't try to match modified dates if using dynamic code.
            if ($wp_cache_mfunc_enabled == 0 && $wp_supercache_304) {
                if (function_exists('apache_request_headers')) {
                    $request = apache_request_headers();
                    $remote_mod_time = $request['If-Modified-Since'];
                } else {
                    if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
                        $remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
                    } else {
                        $remote_mod_time = 0;
                    }
                }
                $local_mod_time = gmdate("D, d M Y H:i:s", filemtime($file)) . ' GMT';
                if ($remote_mod_time != 0 && $remote_mod_time == $local_mod_time) {
                    header("HTTP/1.0 304 Not Modified");
                    exit;
                }
                header('Last-Modified: ' . $local_mod_time);
            }
            echo $cachefiledata;
            exit;
        } else {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("No wp-cache file exists. Must generate a new one.");
            }
            return false;
        }
    }
    $cache_file = do_cacheaction('wp_cache_served_cache_file', $cache_file);
    // Sometimes the gzip headers are lost. Make sure html returned isn't compressed!
    if ($cache_compression && $wp_cache_gzip_encoding && !in_array('Content-Encoding: ' . $wp_cache_gzip_encoding, $meta['headers'])) {
        $ungzip = true;
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("GZIP headers not found. Force uncompressed output.", 1);
        }
    } else {
        $ungzip = false;
    }
    foreach ($meta['headers'] as $t => $header) {
        // 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/
        if (strpos($header, 'Last-Modified:') === false) {
            header($header);
        }
    }
    header('WP-Super-Cache: Served legacy cache file');
    if ($wp_cache_object_cache) {
        if ($cache) {
            if ($ungzip) {
                // attempt to uncompress the cached file just in case it's gzipped
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from object cache", 1);
                    }
                    $cache = $uncompressed;
                    unset($uncompressed);
                }
            }
            if (isset($meta['dynamic']) && $meta['dynamic']) {
                if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                    wp_cache_debug("Serving wp-cache dynamic file from object cache", 5);
                }
                echo do_cacheaction('wpsc_cachedata', $cache);
            } else {
                if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                    wp_cache_debug("Serving wp-cache static file from object cache", 5);
                }
                echo $cache;
            }
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("exit request", 5);
            }
            die;
        }
    } else {
        if (isset($meta['dynamic'])) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Serving wp-cache dynamic file", 5);
            }
            if ($ungzip) {
                // attempt to uncompress the cached file just in case it's gzipped
                $cache = file_get_contents($cache_file);
                $uncompressed = @gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1);
                    }
                    unset($cache);
                    echo do_cacheaction('wpsc_cachedata', $uncompressed);
                } else {
                    echo do_cacheaction('wpsc_cachedata', $cache);
                }
            } else {
                echo do_cacheaction('wpsc_cachedata', file_get_contents($cache_file));
            }
        } else {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Serving wp-cache static file", 5);
            }
            if ($ungzip) {
                $cache = file_get_contents($cache_file);
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1);
                    }
                    echo $uncompressed;
                } else {
                    echo $cache;
                }
            } else {
                readfile($cache_file);
            }
        }
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("exit request", 5);
        }
        die;
    }
}
function wp_cache_shutdown_callback()
{
    global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $gzsize, $cache_filename, $supercacheonly, $blog_cache_dir;
    global $wp_cache_request_uri, $wp_cache_key, $wp_cache_object_cache, $cache_enabled, $wp_cache_blog_charset, $wp_cache_not_logged_in;
    $wp_cache_meta['uri'] = $_SERVER["SERVER_NAME"] . preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', $wp_cache_request_uri);
    // To avoid XSS attacks
    $wp_cache_meta['blog_id'] = $blog_id;
    $wp_cache_meta['post'] = wp_cache_post_id();
    $wp_cache_meta['key'] = $wp_cache_key;
    $wp_cache_meta = apply_filters('wp_cache_meta', $wp_cache_meta);
    $response = wp_cache_get_response_headers();
    foreach ($known_headers as $key) {
        if (isset($response[$key])) {
            $wp_cache_meta['headers'][$key] = "{$key}: " . $response[$key];
        }
    }
    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
        wp_cache_debug("wp_cache_shutdown_callback: collecting meta data.", 2);
    }
    if (!isset($response['Last-Modified'])) {
        $value = gmdate('D, d M Y H:i:s') . ' GMT';
        /* Dont send this the first time */
        /* @header('Last-Modified: ' . $value); */
        $wp_cache_meta['headers']['Last-Modified'] = "Last-Modified: {$value}";
    }
    if (!isset($response['Content-Type']) && !isset($response['Content-type'])) {
        // On some systems, headers set by PHP can't be fetched from
        // the output buffer. This is a last ditch effort to set the
        // correct Content-Type header for feeds, if we didn't see
        // it in the response headers already. -- dougal
        if (is_feed()) {
            $type = get_query_var('feed');
            $type = str_replace('/', '', $type);
            switch ($type) {
                case 'atom':
                    $value = "application/atom+xml";
                    break;
                case 'rdf':
                    $value = "application/rdf+xml";
                    break;
                case 'rss':
                case 'rss2':
                default:
                    $value = "application/rss+xml";
            }
        } else {
            // not a feed
            $value = get_option('html_type');
            if ($value == '') {
                $value = 'text/html';
            }
        }
        $value .= "; charset=\"" . $wp_cache_blog_charset . "\"";
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Sending 'Content-Type: {$value}' header.", 2);
        }
        @header("Content-Type: {$value}");
        $wp_cache_meta['headers']['Content-Type'] = "Content-Type: {$value}";
    }
    if (!$supercacheonly && !$wp_cache_not_logged_in && $new_cache) {
        if (!isset($wp_cache_meta['dynamic']) && $wp_cache_gzip_encoding && !in_array('Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta['headers'])) {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("Sending gzip headers.", 2);
            }
            $wp_cache_meta['headers']['Content-Encoding'] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
            $wp_cache_meta['headers']['Vary'] = 'Vary: Accept-Encoding, Cookie';
        }
        $serial = serialize($wp_cache_meta);
        if (wp_cache_writers_entry()) {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("Writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2);
            }
            if (false == $wp_cache_object_cache) {
                $tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid(mt_rand(), true) . '.tmp';
                $fr = @fopen($tmp_meta_filename, 'w');
                if (!$fr) {
                    @mkdir($blog_cache_dir . 'meta');
                }
                $fr = @fopen($tmp_meta_filename, 'w');
                if ($fr) {
                    fputs($fr, $serial);
                    fclose($fr);
                    @chmod($tmp_meta_filename, 0666 & ~umask());
                    if (!@rename($tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file)) {
                        @unlink($blog_cache_dir . 'meta/' . $meta_file);
                        @rename($tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file);
                    }
                } else {
                    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                        wp_cache_debug("Problem writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2);
                    }
                }
            } elseif ($cache_enabled) {
                $oc_key = get_oc_key() . ".meta";
                if (gzip_accepted()) {
                    $oc_key .= ".gz";
                }
                wp_cache_set($oc_key, $serial, 'supercache', $cache_max_time);
            }
            wp_cache_writers_exit();
        }
    }
    global $time_to_gc_cache;
    if (isset($time_to_gc_cache) && $time_to_gc_cache == 1) {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Executing wp_cache_gc action.", 3);
        }
        do_action('wp_cache_gc');
    }
}
function wp_cache_serve_cache_file()
{
    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;
    global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check, $wp_supercache_304;
    extract(wp_super_cache_init());
    if (wp_cache_user_agent_is_rejected()) {
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("No wp-cache file served as user agent rejected.", 5);
        }
        return false;
    }
    if ($wp_cache_object_cache && wp_cache_get_cookies_values() == '') {
        if (!empty($_GET)) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Non empty GET request. Not serving request from object cache", 1);
            }
            return false;
        }
        $oc_key = get_oc_key();
        $meta_filename = $oc_key . ".meta";
        if (gzip_accepted()) {
            $oc_key .= ".gz";
            $meta_filename .= ".gz";
        }
        $cache = wp_cache_get($oc_key, 'supercache');
        $meta = unserialize(wp_cache_get($meta_filename, 'supercache'));
        if (is_array($meta) == false) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Meta array from object cache corrupt. Ignoring cache.", 1);
            }
            return true;
        }
    } elseif (file_exists($cache_file)) {
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("wp-cache file exists: {$cache_file}", 5);
        }
        if (!($meta = unserialize(@file_get_contents($meta_pathname)))) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("couldn't load wp-cache meta file", 5);
            }
            return true;
        }
        if (is_array($meta) == false) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("meta array corrupt, deleting {$meta_pathname} and {$cache_file}", 1);
            }
            @unlink($meta_pathname);
            @unlink($cache_file);
            return true;
        }
    } else {
        // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
        $file = get_current_url_supercache_dir() . "index.html";
        $phpfile = get_current_url_supercache_dir() . "index.html.php";
        $serving_supercache = false;
        if (file_exists($file)) {
            $serving_supercache = 'html';
        } elseif (file_exists($phpfile)) {
            $serving_supercache = 'php';
        }
        if (($wp_cache_request_uri == '/' || $wp_cache_slash_check && substr($wp_cache_request_uri, -1) == '/' || $wp_cache_slash_check == 0 && substr($wp_cache_request_uri, -1) != '/') && (wp_cache_get_cookies_values() == '' && empty($_GET) && $serving_supercache)) {
            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
            header("Vary: Accept-Encoding, Cookie");
            header("Cache-Control: max-age=300, must-revalidate");
            header("WP-Super-Cache: Served supercache file from PHP");
            if (file_exists($file . '.gz') && $wp_cache_gzip_encoding) {
                $file = $file . '.gz';
                header('Content-Encoding: ' . $wp_cache_gzip_encoding);
                header('Content-Length: ' . filesize($file));
            } elseif ($serving_supercache == 'html' && $wp_supercache_304) {
                header('Content-Length: ' . filesize($file));
            }
            if ($serving_supercache == 'html' && $wp_supercache_304) {
                if (function_exists('apache_request_headers')) {
                    $request = apache_request_headers();
                    $remote_mod_time = $request['If-Modified-Since'];
                } else {
                    $remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
                }
                $local_mod_time = gmdate("D, d M Y H:i:s", filemtime($file)) . ' GMT';
                if ($remote_mod_time == $local_mod_time) {
                    header("HTTP/1.0 304 Not Modified");
                    exit;
                }
                header('Last-Modified: ' . $local_mod_time);
            }
            if ($serving_supercache == 'html') {
                readfile($file);
                if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                    wp_cache_debug("Served page from supercache file using PHP.", 5);
                }
                exit;
            } elseif ($serving_supercache == 'php') {
                $cachefiledata = file_get_contents($phpfile);
                if ($cache_compression and $wp_cache_gzip_encoding) {
                    ob_start("ob_gzhandler");
                    eval('?>' . $cachefiledata . '<?php ');
                    echo "\n<!-- Compression = gzip -->\n";
                    ob_end_flush();
                    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);
                    }
                } else {
                    eval('?>' . $cachefiledata . '<?php ');
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Served dynamic page from supercache file using PHP. File: {$file}", 5);
                    }
                }
                exit;
            }
        } else {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("No wp-cache file exists. Must generate a new one.", 5);
            }
            return false;
        }
    }
    $cache_file = do_cacheaction('wp_cache_served_cache_file', $cache_file);
    // Sometimes the gzip headers are lost. Make sure html returned isn't compressed!
    if ($cache_compression && $wp_cache_gzip_encoding && !in_array('Content-Encoding: ' . $wp_cache_gzip_encoding, $meta['headers'])) {
        $ungzip = true;
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("GZIP headers not found. Force uncompressed output.", 1);
        }
    } else {
        $ungzip = false;
    }
    foreach ($meta['headers'] as $t => $header) {
        // 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/
        if (strpos($header, 'Last-Modified:') === false) {
            header($header);
        }
    }
    header('WP-Super-Cache: Served legacy cache file');
    if ($wp_cache_object_cache) {
        if ($cache) {
            if ($ungzip) {
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from object cache", 1);
                    }
                    $cache = $uncompressed;
                    unset($uncompressed);
                }
            }
            if (isset($meta['dynamic']) && $meta['dynamic']) {
                if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                    wp_cache_debug("Serving wp-cache dynamic file from object cache", 5);
                }
                eval('?>' . $cache . '<?php ');
            } else {
                if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                    wp_cache_debug("Serving wp-cache static file from object cache", 5);
                }
                echo $cache;
            }
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("exit request", 5);
            }
            die;
        }
    } else {
        if ($meta['dynamic']) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Serving wp-cache dynamic file", 5);
            }
            if ($ungzip) {
                $cache = file_get_contents($cache_file);
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1);
                    }
                    unset($cache);
                    eval('?>' . $uncompressed . '<?php ');
                } else {
                    eval('?>' . $cache . '<?php ');
                }
            } else {
                include $cache_file;
            }
        } else {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Serving wp-cache static file", 5);
            }
            if ($wp_supercache_304) {
                if (function_exists('apache_request_headers')) {
                    $request = apache_request_headers();
                    $remote_mod_time = $request['If-Modified-Since'];
                } else {
                    $remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
                }
                $local_mod_time = gmdate("D, d M Y H:i:s", filemtime($cache_file)) . ' GMT';
                if ($remote_mod_time == $local_mod_time) {
                    header("HTTP/1.0 304 Not Modified");
                    exit;
                }
                header('Last-Modified: ' . $local_mod_time);
            }
            if ($ungzip) {
                $cache = file_get_contents($cache_file);
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1);
                    }
                    echo $uncompressed;
                } else {
                    echo $cache;
                }
            } else {
                readfile($cache_file);
            }
        }
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("exit request", 5);
        }
        die;
    }
}
Esempio n. 5
0
$wp_cache_gzip_encoding = '';
$gzipped = 0;
$gzsize = 0;
function gzip_accepted()
{
    if (ini_get('zlib.output_compression')) {
        // don't compress WP-Cache data files when PHP is already doing it
        return false;
    }
    if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) {
        return false;
    }
    return 'gzip';
}
if ($cache_compression) {
    $wp_cache_gzip_encoding = gzip_accepted();
}
add_cacheaction('wp_cache_key', 'wp_cache_check_mobile');
$key = $blogcacheid . md5(do_cacheaction('wp_cache_key', $_SERVER['HTTP_HOST'] . preg_replace('/#.*$/', '', str_replace('/index.php', '/', $_SERVER['REQUEST_URI'])) . $wp_cache_gzip_encoding . wp_cache_get_cookies_values()));
if (false == @is_dir($blog_cache_dir)) {
    @mkdir($cache_path . "blogs");
    @mkdir($blog_cache_dir);
}
if (false == @is_dir($blog_cache_dir . 'meta')) {
    @mkdir($blog_cache_dir . 'meta');
}
$cache_filename = $file_prefix . $key . '.html';
$meta_file = $file_prefix . $key . '.meta';
$cache_file = realpath($blog_cache_dir . $cache_filename);
$meta_pathname = realpath($blog_cache_dir . 'meta/' . $meta_file);
$wp_start_time = microtime();
Esempio n. 6
0
function wp_cache_serve_cache_file()
{
    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;
    global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check;
    extract(wp_super_cache_init());
    if (wp_cache_user_agent_is_rejected()) {
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("No wp-cache file served as user agent rejected.", 5);
        }
        return false;
    }
    if ($wp_cache_object_cache && wp_cache_get_cookies_values() == '') {
        if (!empty($_GET)) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Non empty GET request. Not serving request from object cache", 1);
            }
            return false;
        }
        $oc_key = get_oc_key();
        $meta_filename = $oc_key . ".meta";
        if (gzip_accepted()) {
            $oc_key .= ".gz";
            $meta_filename .= ".gz";
        }
        $cache = wp_cache_get($oc_key, 'supercache');
        $meta = unserialize(wp_cache_get($meta_filename, 'supercache'));
        if (is_array($meta) == false) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Meta array from object cache corrupt. Ignoring cache.", 1);
            }
            return true;
        }
    } elseif (file_exists($cache_file)) {
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("wp-cache file exists: {$cache_file}", 5);
        }
        if (!($meta = unserialize(@file_get_contents($meta_pathname)))) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("couldn't load wp-cache meta file", 5);
            }
            return true;
        }
        if (is_array($meta) == false) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("meta array corrupt, deleting {$meta_pathname} and {$cache_file}", 1);
            }
            @unlink($meta_pathname);
            @unlink($cache_file);
            return true;
        }
    } else {
        // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
        $file = get_current_url_supercache_dir() . "index.html";
        if (($wp_cache_request_uri == '/' || $wp_cache_slash_check && substr($wp_cache_request_uri, -1) == '/' || $wp_cache_slash_check == 0 && substr($wp_cache_request_uri, -1) != '/') && (wp_cache_get_cookies_values() == '' && empty($_GET) && file_exists($file))) {
            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
            header("Vary: Accept-Encoding, Cookie");
            header("Cache-Control: max-age=300, must-revalidate");
            header("WP-Cache: Served supercache file from PHP");
            readfile($file);
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Served page from supercache file. Mod rewrite rules may be broken or missing.", 5);
            }
            die;
        } else {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("No wp-cache file exists. Must generate a new one.", 5);
            }
            return false;
        }
    }
    $cache_file = do_cacheaction('wp_cache_served_cache_file', $cache_file);
    // Sometimes the gzip headers are lost. Make sure html returned isn't compressed!
    if ($cache_compression && $wp_cache_gzip_encoding && !in_array('Content-Encoding: ' . $wp_cache_gzip_encoding, $meta['headers'])) {
        $ungzip = true;
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("GZIP headers not found. Force uncompressed output.", 1);
        }
    } else {
        $ungzip = false;
    }
    foreach ($meta['headers'] as $t => $header) {
        // 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/
        if (strpos($header, 'Last-Modified:') === false) {
            header($header);
        }
    }
    header('WP-Super-Cache: WP-Cache');
    if ($wp_cache_object_cache) {
        if ($cache) {
            if ($ungzip) {
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from object cache", 1);
                    }
                    $cache = $uncompressed;
                    unset($uncompressed);
                }
            }
            if ($meta['dynamic']) {
                if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                    wp_cache_debug("Serving wp-cache dynamic file from object cache", 5);
                }
                eval('?>' . $cache . '<?php ');
            } else {
                if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                    wp_cache_debug("Serving wp-cache static file from object cache", 5);
                }
                echo $cache;
            }
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("exit request", 5);
            }
            die;
        }
    } else {
        if ($meta['dynamic']) {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Serving wp-cache dynamic file", 5);
            }
            if ($ungzip) {
                $cache = file_get_contents($cache_file);
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1);
                    }
                    unset($cache);
                    eval('?>' . $uncompressed . '<?php ');
                } else {
                    eval('?>' . $cache . '<?php ');
                }
            } else {
                include $cache_file;
            }
        } else {
            if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                wp_cache_debug("Serving wp-cache static file", 5);
            }
            if ($ungzip) {
                $cache = file_get_contents($cache_file);
                $uncompressed = gzuncompress($cache);
                if ($uncompressed) {
                    if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
                        wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1);
                    }
                    echo $uncompressed;
                } else {
                    echo $cache;
                }
            } else {
                readfile($cache_file);
            }
        }
        if (isset($wp_super_cache_debug) && $wp_super_cache_debug) {
            wp_cache_debug("exit request", 5);
        }
        die;
    }
}