function wp_cache_get_ob(&$buffer)
{
    global $wpsc_settings;
    global $cache_enabled, $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
    global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
    global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
    global $wp_cache_404, $gzsize, $supercacheonly;
    global $blog_cache_dir, $wp_cache_request_uri, $wp_supercache_cache_list;
    global $wp_cache_not_logged_in, $wp_cache_object_cache, $cache_max_time;
    global $wp_cache_is_home, $wp_cache_front_page_checks;
    $new_cache = true;
    $wp_cache_meta = '';
    /* Mode paranoic, check for closing tags 
     * we avoid caching incomplete files */
    if ($buffer == '') {
        $new_cache = false;
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Buffer is blank. Output buffer may have been corrupted by another plugin or this is a redirected URL. Look for text 'ob_start' in the files of your plugins directory.", 2);
            $buffer .= "\n<!-- Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins. -->\n";
        }
    }
    if ($wp_cache_404 && false == apply_filters('wpsupercache_404', false)) {
        $new_cache = false;
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("404 file not found not cached", 2);
            $buffer .= "\n<!-- Page not cached by WP Super Cache. 404. -->\n";
        }
    }
    if (!preg_match('/(<\\/html>|<\\/rss>|<\\/feed>|<\\/urlset)/i', $buffer)) {
        $new_cache = false;
        if (false === strpos($_SERVER['REQUEST_URI'], 'robots.txt')) {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("No closing html tag. Not caching.", 2);
                $buffer .= "\n<!-- Page not cached by WP Super Cache. No closing HTML tag. Check your theme. -->\n";
            }
        } else {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("robots.txt detected. Not caching.", 2);
            }
        }
    }
    if (!$new_cache) {
        return $buffer;
    }
    $duration = wp_cache_microtime_diff($wp_start_time, microtime());
    $duration = sprintf("%0.3f", $duration);
    $buffer .= "\n<!-- Dynamic page generated in {$duration} seconds. -->\n";
    if (!wp_cache_writers_entry()) {
        $buffer .= "\n<!-- Page not cached by WP Super Cache. Could not get mutex lock. -->\n";
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Could not get mutex lock. Not caching.", 1);
        }
        return $buffer;
    }
    if ($wp_cache_not_logged_in && is_feed()) {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Feed detected. Writing legacy cache files.", 5);
        }
        $wp_cache_not_logged_in = false;
    }
    $home_url = parse_url(trailingslashit(get_bloginfo('url')));
    $dir = get_current_url_supercache_dir();
    $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $home_url['host']);
    if (!empty($_GET) || is_feed() || $super_cache_enabled == true && is_dir(substr($supercachedir, 0, -1) . '.disabled')) {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Supercache disabled: GET or feed detected or disabled by config.", 2);
        }
        $super_cache_enabled = false;
    }
    $tmp_wpcache_filename = $cache_path . uniqid(mt_rand(), true) . '.tmp';
    $supercacheonly = false;
    if ($super_cache_enabled) {
        if (wp_cache_get_cookies_values() == '') {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("Anonymous user detected. Only creating Supercache file.", 3);
            }
            $supercacheonly = true;
        }
    }
    $cache_error = '';
    if ($wp_cache_not_logged_in && wp_cache_get_cookies_values() != '') {
        $super_cache_enabled = false;
        $cache_enabled = false;
        $cache_error = 'Not caching requests by known users. (See Advanced Settings page)';
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug('Not caching for known user.', 5);
        }
    }
    if ($wp_cache_object_cache) {
        // half on mode when using the object cache
        if (wp_cache_get_cookies_values() != '') {
            $cache_enabled = false;
            $cache_error = 'Known User and using object. Only anonymous users cached.';
        }
        $super_cache_enabled = false;
        $supercacheonly = false;
        wp_cache_init();
        // PHP5 destroys objects during shutdown
    }
    if (!$cache_enabled) {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug('', 5);
        }
        wp_cache_writers_exit();
        return $buffer . "\n<!-- Page not cached by WP Super Cache. Check your settings page. {$cache_error} -->";
    }
    if (@is_dir($dir) == false) {
        @wp_mkdir_p($dir);
    }
    $fr = $fr2 = $gz = false;
    // Open wp-cache cache file
    if (!$supercacheonly) {
        if (false == $wp_cache_object_cache) {
            $fr = @fopen($tmp_wpcache_filename, 'w');
            if (!$fr) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Error. Supercache could not write to " . str_replace(ABSPATH, '', $cache_path) . $cache_filename, 1);
                }
                $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace(ABSPATH, '', $cache_path) . $cache_filename . " -->\n";
                wp_cache_writers_exit();
                return $buffer;
            }
        }
    } else {
        $user_info = wp_cache_get_cookies_values();
        $do_cache = apply_filters('do_createsupercache', $user_info);
        if ($super_cache_enabled && ($user_info == '' || $do_cache === true)) {
            $cache_fname = $dir . supercache_filename();
            $tmp_cache_filename = $dir . uniqid(mt_rand(), true) . '.tmp';
            $fr2 = @fopen($tmp_cache_filename, 'w');
            if (!$fr2) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Error. Supercache could not write to " . str_replace(ABSPATH, '', $tmp_cache_filename), 1);
                }
                $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace(ABSPATH, '', $tmp_cache_filename) . " -->\n";
                @fclose($fr);
                @unlink($tmp_wpcache_filename);
                wp_cache_writers_exit();
                return $buffer;
            } elseif ($cache_compression) {
                $gz = @fopen($tmp_cache_filename . ".gz", 'w');
                if (!$gz) {
                    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                        wp_cache_debug("Error. Supercache could not write to " . str_replace(ABSPATH, '', $tmp_cache_filename) . ".gz", 1);
                    }
                    $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace(ABSPATH, '', $tmp_cache_filename) . ".gz -->\n";
                    @fclose($fr);
                    @unlink($tmp_wpcache_filename);
                    @fclose($fr2);
                    @unlink($tmp_cache_filename);
                    wp_cache_writers_exit();
                    return $buffer;
                }
            }
        }
    }
    $added_cache = 0;
    $oc_key = get_oc_key();
    if (preg_match('/<!--mclude|<!--mfunc|<!--dynamic-cached-content-->/', $buffer)) {
        //Dynamic content
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Dynamic content found in buffer.", 4);
        }
        $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is', "<!--mclude-->\n<?php include_once('" . ABSPATH . "\$1'); ?>\n<!--/mclude-->", $buffer);
        $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is', "<!--mfunc-->\n<?php \$1 ;?>\n<!--/mfunc-->", $store);
        $store = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is', "<!--dynamic-cached-content-->\n<?php\$2?>\n<!--/dynamic-cached-content-->", $store);
        $wp_cache_meta['dynamic'] = true;
        /* Clean function calls in tag */
        $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
        $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
        $buffer = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is', "<!--dynamic-cached-content-->\$1<!--/dynamic-cached-content-->", $buffer);
        $store = apply_filters('wpsupercache_buffer', $store);
        // Append WP Super Cache or Live page comment tag
        wp_cache_append_tag($buffer);
        wp_cache_append_tag($store);
        global $wp_super_cache_late_init;
        if (false == isset($wp_super_cache_late_init) || isset($wp_super_cache_late_init) && $wp_super_cache_late_init == 0) {
            $buffer .= '<!-- Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details. -->';
        }
        if (false == $wp_cache_object_cache) {
            if ($fr) {
                // legacy caching
                fputs($fr, $store);
            } elseif (isset($fr2)) {
                // supercache active
                $php_fname = "{$dir}" . supercache_filename() . ".php";
                $tmp_php_filename = $dir . uniqid(mt_rand(), true) . '.tmp';
                $php_fd = @fopen($tmp_php_filename, 'w');
                if (!$php_fd) {
                    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                        wp_cache_debug("Error. Supercache could not write to " . str_replace(ABSPATH, '', $tmp_php_filename), 1);
                    }
                    $buffer .= "<!-- File not cached! Super Cache couldn't write to: " . str_replace(ABSPATH, '', $tmp_php_filename) . " -->\n";
                    @fclose($php_fd);
                    @unlink($tmp_php_filename);
                    wp_cache_writers_exit();
                    return $buffer;
                }
                fputs($php_fd, $store);
            }
        } else {
            wp_cache_set($oc_key, $store, 'supercache', $cache_max_time);
        }
        if ($cache_compression && $wp_cache_gzip_encoding) {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("Gzipping dynamic buffer.", 5);
            }
            $gzdata = gzencode($buffer . "<!-- Compression = gzip -->", 6, FORCE_GZIP);
            $gzsize = function_exists('mb_strlen') ? mb_strlen($gzdata, '8bit') : strlen($gzdata);
        }
    } else {
        $buffer = apply_filters('wpsupercache_buffer', $buffer);
        // Append WP Super Cache or Live page comment tag
        wp_cache_append_tag($buffer);
        if ($gz || $wp_cache_gzip_encoding) {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("Gzipping buffer.", 5);
            }
            $gzdata = gzencode($buffer . "<!-- Compression = gzip -->", 6, FORCE_GZIP);
            $gzsize = function_exists('mb_strlen') ? mb_strlen($gzdata, '8bit') : strlen($gzdata);
        }
        if ($wp_cache_gzip_encoding) {
            $wp_cache_meta['headers']['Content-Encoding'] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
            $wp_cache_meta['headers']['Vary'] = 'Vary: Accept-Encoding, Cookie';
            // Return uncompressed data & store compressed for later use
            if (false == $wp_cache_object_cache) {
                if ($fr) {
                    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                        wp_cache_debug("Writing gzipped buffer to wp-cache cache file.", 5);
                    }
                    fputs($fr, $gzdata);
                }
            } elseif ($cache_enabled) {
                wp_cache_set($oc_key . ".gz", $gzdata, 'supercache', $cache_max_time);
                $added_cache = 1;
            }
        } else {
            // no compression
            $wp_cache_meta['headers']['Vary'] = 'Vary: Cookie';
            if (false == $wp_cache_object_cache) {
                if ($fr) {
                    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                        wp_cache_debug("Writing non-gzipped buffer to wp-cache cache file.", 5);
                    }
                    fputs($fr, $buffer);
                }
            } elseif ($cache_enabled) {
                wp_cache_set($oc_key, $buffer, 'supercache', $cache_max_time);
                $added_cache = 1;
            }
        }
        if (false == $wp_cache_object_cache) {
            if ($fr2) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Writing non-gzipped buffer to supercache file.", 5);
                }
                fputs($fr2, $buffer . '<!-- super cache -->');
            }
            if ($gz) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Writing gzipped buffer to supercache file.", 5);
                }
                fwrite($gz, $gzdata);
            }
        }
    }
    $new_cache = true;
    if (false == $wp_cache_object_cache) {
        if ($fr) {
            $supercacheonly = false;
            fclose($fr);
            if (filesize($tmp_wpcache_filename) == 0) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Warning! The file {$tmp_wpcache_filename} was empty. Did not rename to {$blog_cache_dir}{$cache_filename}", 5);
                }
                @unlink($tmp_wpcache_filename);
            } else {
                if (!rename($tmp_wpcache_filename, $blog_cache_dir . $cache_filename)) {
                    unlink($blog_cache_dir . $cache_filename);
                    rename($tmp_wpcache_filename, $blog_cache_dir . $cache_filename);
                }
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Renamed temp wp-cache file to {$blog_cache_dir}{$cache_filename}", 5);
                }
                $added_cache = 1;
            }
        }
        if ($fr2) {
            fclose($fr2);
            if ($wp_cache_front_page_checks && $cache_fname == $supercachedir . $home_url['path'] . supercache_filename() && !$wp_cache_is_home) {
                wp_cache_writers_exit();
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Warning! Not writing another page to front page cache.", 1);
                }
                return $buffer;
            } elseif (filesize($tmp_cache_filename) == 0) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Warning! The file {$tmp_cache_filename} was empty. Did not rename to {$cache_fname}", 5);
                }
                @unlink($tmp_cache_filename);
            } else {
                if (!@rename($tmp_cache_filename, $cache_fname)) {
                    @unlink($cache_fname);
                    @rename($tmp_cache_filename, $cache_fname);
                }
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Renamed temp supercache file to {$cache_fname}", 5);
                }
                $added_cache = 1;
            }
        }
        if ($php_fd) {
            fclose($php_fd);
            if ($php_fname == $supercachedir . $home_url['path'] . supercache_filename() . '.php' && !$wp_cache_is_home) {
                wp_cache_writers_exit();
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Warning! Not writing another page to front page cache.", 1);
                }
                return $buffer;
            } elseif (filesize($tmp_php_filename) == 0) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Warning! The file {$tmp_php_filename} was empty. Did not rename to {$php_fname}", 5);
                }
                @unlink($tmp_php_filename);
            } else {
                if (!@rename($tmp_php_filename, $php_fname)) {
                    @unlink($php_fname);
                    @rename($tmp_php_filename, $php_fname);
                }
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Renamed temp supercache file to {$php_fname}", 5);
                }
                $added_cache = 1;
            }
        }
        if ($gz) {
            fclose($gz);
            if (filesize($tmp_cache_filename . '.gz') == 0) {
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Warning! The file {$tmp_cache_filename}.gz was empty. Did not rename to {$cache_fname}.gz", 5);
                }
                @unlink($tmp_cache_filename . '.gz');
            } else {
                if (!@rename($tmp_cache_filename . '.gz', $cache_fname . '.gz')) {
                    @unlink($cache_fname . '.gz');
                    @rename($tmp_cache_filename . '.gz', $cache_fname . '.gz');
                }
                if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                    wp_cache_debug("Renamed temp supercache gz file to {$cache_fname}.gz", 5);
                }
                $added_cache = 1;
            }
        }
    }
    if ($added_cache && isset($wp_supercache_cache_list) && $wp_supercache_cache_list) {
        update_option('wpsupercache_count', get_option('wpsupercache_count') + 1);
        $last_urls = (array) get_option('supercache_last_cached');
        if (count($last_urls) >= 10) {
            $last_urls = array_slice($last_urls, 1, 9);
        }
        $last_urls[] = array('url' => $_SERVER['REQUEST_URI'], 'date' => date('Y-m-d H:i:s'));
        update_option('supercache_last_cached', $last_urls);
    }
    wp_cache_writers_exit();
    if (!headers_sent() && $wp_cache_gzip_encoding && $gzdata) {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Writing gzip content headers. Sending buffer to browser", 5);
        }
        header('Content-Encoding: ' . $wp_cache_gzip_encoding);
        header('Vary: Accept-Encoding, Cookie');
        header('Content-Length: ' . $gzsize);
        return $gzdata;
    } else {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Sending buffer to browser", 5);
        }
        return $buffer;
    }
}
Example #2
0
function wp_cache_get_ob(&$buffer)
{
    global $cache_enabled, $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
    global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
    global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
    global $wp_cache_404, $gzsize, $supercacheonly;
    global $blog_cache_dir, $wp_cache_request_uri, $wp_supercache_cache_list;
    global $wp_cache_not_logged_in, $wp_cache_object_cache, $cache_max_time;
    global $wp_cache_is_home, $wp_cache_front_page_checks, $wp_cache_mfunc_enabled;
    if (isset($wp_cache_mfunc_enabled) == false) {
        $wp_cache_mfunc_enabled = 0;
    }
    $new_cache = true;
    $wp_cache_meta = '';
    /* Mode paranoic, check for closing tags 
     * we avoid caching incomplete files */
    if ($buffer == '') {
        $new_cache = false;
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("Buffer is blank. Output buffer may have been corrupted by another plugin or this is a redirected URL. Look for text 'ob_start' in the files of your plugins directory.", 2);
            wp_cache_add_to_buffer($buffer, "Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins.");
        }
    }
    if ($wp_cache_404 && false == apply_filters('wpsupercache_404', false)) {
        $new_cache = false;
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("404 file not found not cached", 2);
            wp_cache_add_to_buffer($buffer, "Page not cached by WP Super Cache. 404.");
        }
    }
    if (!preg_match(apply_filters('wp_cache_eof_tags', '/(<\\/html>|<\\/rss>|<\\/feed>|<\\/urlset|<\\?xml)/i'), $buffer)) {
        $new_cache = false;
        if (false === strpos($_SERVER['REQUEST_URI'], 'robots.txt')) {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("No closing html tag. Not caching.", 2);
                wp_cache_add_to_buffer($buffer, "Page not cached by WP Super Cache. No closing HTML tag. Check your theme.");
            }
        } else {
            wp_cache_debug("robots.txt detected. Not caching.", 2);
        }
    }
    if (!$new_cache) {
        return wp_cache_maybe_dynamic($buffer);
    }
    $duration = wp_cache_microtime_diff($wp_start_time, microtime());
    $duration = sprintf("%0.3f", $duration);
    wp_cache_add_to_buffer($buffer, "Dynamic page generated in {$duration} seconds.");
    if (!wp_cache_writers_entry()) {
        wp_cache_add_to_buffer($buffer, "Page not cached by WP Super Cache. Could not get mutex lock.");
        wp_cache_debug("Could not get mutex lock. Not caching.", 1);
        return wp_cache_maybe_dynamic($buffer);
    }
    if ($wp_cache_not_logged_in && is_feed()) {
        wp_cache_debug("Feed detected. Writing legacy cache files.", 5);
        $wp_cache_not_logged_in = false;
    }
    $home_url = parse_url(trailingslashit(get_bloginfo('url')));
    $dir = get_current_url_supercache_dir();
    $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $home_url['host']);
    if (!empty($_GET) || is_feed() || $super_cache_enabled == true && is_dir(substr($supercachedir, 0, -1) . '.disabled')) {
        wp_cache_debug("Supercache disabled: GET or feed detected or disabled by config.", 2);
        $super_cache_enabled = false;
    }
    $tmp_wpcache_filename = $cache_path . uniqid(mt_rand(), true) . '.tmp';
    $supercacheonly = false;
    if ($super_cache_enabled) {
        if (wp_cache_get_cookies_values() == '' && empty($_GET)) {
            wp_cache_debug("Anonymous user detected. Only creating Supercache file.", 3);
            $supercacheonly = true;
        }
    }
    $cache_error = '';
    if ($wp_cache_not_logged_in && wp_cache_get_cookies_values() != '') {
        $super_cache_enabled = false;
        $cache_enabled = false;
        $cache_error = 'Not caching requests by known users. (See Advanced Settings page)';
        wp_cache_debug('Not caching for known user.', 5);
    }
    if ($wp_cache_object_cache) {
        // half on mode when using the object cache
        if (wp_cache_get_cookies_values() != '') {
            $cache_enabled = false;
            $cache_error = 'Known User and using object. Only anonymous users cached.';
        }
        $super_cache_enabled = false;
        $supercacheonly = false;
        wp_cache_init();
        // PHP5 destroys objects during shutdown
    }
    if (!$cache_enabled) {
        wp_cache_debug('Cache is not enabled. Sending buffer to browser.', 5);
        wp_cache_writers_exit();
        wp_cache_add_to_buffer($buffer, "Page not cached by WP Super Cache. Check your settings page. {$cache_error}");
        if ($wp_cache_mfunc_enabled == 1) {
            global $wp_super_cache_late_init;
            if (false == isset($wp_super_cache_late_init) || isset($wp_super_cache_late_init) && $wp_super_cache_late_init == 0) {
                wp_cache_add_to_buffer($buffer, 'Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details.');
            }
        }
        return wp_cache_maybe_dynamic($buffer);
    }
    if (@is_dir($dir) == false) {
        @wp_mkdir_p($dir);
    }
    $fr = $fr2 = $gz = false;
    // Open wp-cache cache file
    if (false == $wp_cache_object_cache) {
        if (!$supercacheonly) {
            $fr = @fopen($tmp_wpcache_filename, 'w');
            if (!$fr) {
                wp_cache_debug("Error. Supercache could not write to " . str_replace(ABSPATH, '', $cache_path) . $cache_filename, 1);
                wp_cache_add_to_buffer($buffer, "File not cached! Super Cache Couldn't write to: " . str_replace(ABSPATH, '', $cache_path) . $cache_filename);
                wp_cache_writers_exit();
                return wp_cache_maybe_dynamic($buffer);
            }
        } else {
            $user_info = wp_cache_get_cookies_values();
            $do_cache = apply_filters('do_createsupercache', $user_info);
            if ($super_cache_enabled && ($user_info == '' || $do_cache === true)) {
                $cache_fname = $dir . supercache_filename();
                $tmp_cache_filename = $dir . uniqid(mt_rand(), true) . '.tmp';
                $fr2 = @fopen($tmp_cache_filename, 'w');
                if (!$fr2) {
                    wp_cache_debug("Error. Supercache could not write to " . str_replace(ABSPATH, '', $tmp_cache_filename), 1);
                    wp_cache_add_to_buffer($buffer, "File not cached! Super Cache Couldn't write to: " . str_replace(ABSPATH, '', $tmp_cache_filename));
                    @fclose($fr);
                    @unlink($tmp_wpcache_filename);
                    wp_cache_writers_exit();
                    return wp_cache_maybe_dynamic($buffer);
                } elseif ((!isset($wp_cache_mfunc_enabled) || $wp_cache_mfunc_enabled == 0) && $cache_compression) {
                    // don't want to store compressed files if using dynamic content
                    $gz = @fopen($tmp_cache_filename . ".gz", 'w');
                    if (!$gz) {
                        wp_cache_debug("Error. Supercache could not write to " . str_replace(ABSPATH, '', $tmp_cache_filename) . ".gz", 1);
                        wp_cache_add_to_buffer($buffer, "File not cached! Super Cache Couldn't write to: " . str_replace(ABSPATH, '', $tmp_cache_filename) . ".gz");
                        @fclose($fr);
                        @unlink($tmp_wpcache_filename);
                        @fclose($fr2);
                        @unlink($tmp_cache_filename);
                        wp_cache_writers_exit();
                        return wp_cache_maybe_dynamic($buffer);
                    }
                }
            }
        }
    }
    $added_cache = 0;
    $oc_key = get_oc_key();
    $buffer = apply_filters('wpsupercache_buffer', $buffer);
    wp_cache_append_tag($buffer);
    /*
     * Dynamic content enabled: write the buffer to a file and then process any templates found using
     * the wpsc_cachedata filter. Buffer is then returned to the visitor.
     */
    if ($wp_cache_mfunc_enabled == 1) {
        if (preg_match('/<!--mclude|<!--mfunc|<!--dynamic-cached-content-->/', $buffer)) {
            //Dynamic content
            wp_cache_debug("mfunc/mclude/dynamic-cached-content tags have been retired. Please update your theme. See docs for updates.");
            wp_cache_add_to_buffer($buffer, "Warning! Obsolete mfunc/mclude/dynamic-cached-content tags found. Please update your theme. See http://ocaoimh.ie/y/5b for more information.");
        }
        global $wp_super_cache_late_init;
        if (false == isset($wp_super_cache_late_init) || isset($wp_super_cache_late_init) && $wp_super_cache_late_init == 0) {
            wp_cache_add_to_buffer($buffer, 'Super Cache dynamic page detected but late init not set. See the readme.txt for further details.');
        }
        if ($fr) {
            // legacy caching
            wp_cache_debug("Writing dynamic buffer to legacy file.");
            wp_cache_add_to_buffer($buffer, "Dynamic Legacy Super Cache");
            fputs($fr, $buffer);
        } elseif (isset($fr2)) {
            // supercache active
            wp_cache_debug("Writing dynamic buffer to supercache file.");
            wp_cache_add_to_buffer($buffer, "Dynamic Super Cache");
            fputs($fr2, $buffer);
        } elseif (true == $wp_cache_object_cache) {
            wp_cache_set($oc_key, $buffer, 'supercache', $cache_max_time);
        }
        $wp_cache_meta['dynamic'] = true;
        if (do_cacheaction('wpsc_cachedata_safety', 0) === 1) {
            $buffer = do_cacheaction('wpsc_cachedata', $buffer);
        }
        // dynamic content for display
        if ($cache_compression && $wp_cache_gzip_encoding) {
            wp_cache_debug("Gzipping dynamic buffer for display.", 5);
            wp_cache_add_to_buffer($buffer, "Compression = gzip");
            $gzdata = gzencode($buffer, 6, FORCE_GZIP);
            $gzsize = function_exists('mb_strlen') ? mb_strlen($gzdata, '8bit') : strlen($gzdata);
        }
    } else {
        if ($gz || $wp_cache_gzip_encoding) {
            wp_cache_debug("Gzipping buffer.", 5);
            wp_cache_add_to_buffer($buffer, "Compression = gzip");
            $gzdata = gzencode($buffer, 6, FORCE_GZIP);
            $gzsize = function_exists('mb_strlen') ? mb_strlen($gzdata, '8bit') : strlen($gzdata);
            $wp_cache_meta['headers']['Content-Encoding'] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
            $wp_cache_meta['headers']['Vary'] = 'Vary: Accept-Encoding, Cookie';
            // Return uncompressed data & store compressed for later use
            if ($fr) {
                wp_cache_debug("Writing gzipped buffer to wp-cache cache file.", 5);
                fputs($fr, $gzdata);
            } elseif ($cache_enabled && $wp_cache_object_cache) {
                wp_cache_set($oc_key . ".gz", $gzdata, 'supercache', $cache_max_time);
                $added_cache = 1;
            }
        } else {
            // no compression
            $wp_cache_meta['headers']['Vary'] = 'Vary: Cookie';
            if ($cache_enabled && $wp_cache_object_cache) {
                wp_cache_set($oc_key, $buffer, 'supercache', $cache_max_time);
                $added_cache = 1;
            } elseif ($fr) {
                wp_cache_debug("Writing non-gzipped buffer to wp-cache cache file.");
                fputs($fr, $buffer);
            }
        }
        if ($fr2) {
            wp_cache_debug("Writing non-gzipped buffer to supercache file.");
            wp_cache_add_to_buffer($buffer, "super cache");
            fputs($fr2, $buffer);
        }
        if (isset($gzdata) && $gz) {
            wp_cache_debug("Writing gzipped buffer to supercache file.");
            fwrite($gz, $gzdata);
        }
    }
    $new_cache = true;
    if (false == $wp_cache_object_cache) {
        if ($fr) {
            $supercacheonly = false;
            fclose($fr);
            if (filesize($tmp_wpcache_filename) == 0) {
                wp_cache_debug("Warning! The file {$tmp_wpcache_filename} was empty. Did not rename to {$blog_cache_dir}{$cache_filename}", 5);
                @unlink($tmp_wpcache_filename);
            } else {
                if (!@rename($tmp_wpcache_filename, $blog_cache_dir . $cache_filename)) {
                    if (false == is_dir($blog_cache_dir)) {
                        @wp_mkdir_p($blog_cache_dir);
                    }
                    @unlink($blog_cache_dir . $cache_filename);
                    @rename($tmp_wpcache_filename, $blog_cache_dir . $cache_filename);
                }
                wp_cache_debug("Renamed temp wp-cache file to {$blog_cache_dir}{$cache_filename}", 5);
                $added_cache = 1;
            }
        }
        if ($fr2) {
            fclose($fr2);
            if ($wp_cache_front_page_checks && $cache_fname == $supercachedir . $home_url['path'] . supercache_filename() && !$wp_cache_is_home) {
                wp_cache_writers_exit();
                wp_cache_debug("Warning! Not writing another page to front page cache.", 1);
                return $buffer;
            } elseif (filesize($tmp_cache_filename) == 0) {
                wp_cache_debug("Warning! The file {$tmp_cache_filename} was empty. Did not rename to {$cache_fname}", 5);
                @unlink($tmp_cache_filename);
            } else {
                if (!@rename($tmp_cache_filename, $cache_fname)) {
                    @unlink($cache_fname);
                    @rename($tmp_cache_filename, $cache_fname);
                }
                wp_cache_debug("Renamed temp supercache file to {$cache_fname}", 5);
                $added_cache = 1;
            }
        }
        if ($gz) {
            fclose($gz);
            if (filesize($tmp_cache_filename . '.gz') == 0) {
                wp_cache_debug("Warning! The file {$tmp_cache_filename}.gz was empty. Did not rename to {$cache_fname}.gz", 5);
                @unlink($tmp_cache_filename . '.gz');
            } else {
                if (!@rename($tmp_cache_filename . '.gz', $cache_fname . '.gz')) {
                    @unlink($cache_fname . '.gz');
                    @rename($tmp_cache_filename . '.gz', $cache_fname . '.gz');
                }
                wp_cache_debug("Renamed temp supercache gz file to {$cache_fname}.gz", 5);
                $added_cache = 1;
            }
        }
    }
    if ($added_cache && isset($wp_supercache_cache_list) && $wp_supercache_cache_list) {
        update_option('wpsupercache_count', get_option('wpsupercache_count') + 1);
        $last_urls = (array) get_option('supercache_last_cached');
        if (count($last_urls) >= 10) {
            $last_urls = array_slice($last_urls, 1, 9);
        }
        $last_urls[] = array('url' => preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', $_SERVER['REQUEST_URI']), 'date' => date('Y-m-d H:i:s'));
        update_option('supercache_last_cached', $last_urls);
    }
    wp_cache_writers_exit();
    if (!headers_sent() && $wp_cache_gzip_encoding && $gzdata) {
        wp_cache_debug("Writing gzip content headers. Sending buffer to browser", 5);
        header('Content-Encoding: ' . $wp_cache_gzip_encoding);
        header('Vary: Accept-Encoding, Cookie');
        header('Content-Length: ' . $gzsize);
        return $gzdata;
    } else {
        wp_cache_debug("Sending buffer to browser", 5);
        return $buffer;
    }
}