function wp_cache_files()
{
    global $cache_path, $file_prefix, $cache_max_time, $valid_nonce, $supercachedir, $cache_enabled, $super_cache_enabled, $blog_cache_dir, $cache_compression;
    global $wp_cache_object_cache, $wp_cache_preload_on;
    if ('/' != substr($cache_path, -1)) {
        $cache_path .= '/';
    }
    if ($valid_nonce) {
        if (isset($_REQUEST['wp_delete_cache'])) {
            wp_cache_clean_cache($file_prefix);
            $_GET['action'] = 'regenerate_cache_stats';
        }
        if (isset($_REQUEST['wp_delete_all_cache'])) {
            wp_cache_clean_cache($file_prefix, true);
            $_GET['action'] = 'regenerate_cache_stats';
        }
        if (isset($_REQUEST['wp_delete_expired'])) {
            wp_cache_clean_expired($file_prefix);
            $_GET['action'] = 'regenerate_cache_stats';
        }
    }
    echo "<a name='listfiles'></a>";
    echo '<fieldset class="options" id="show-this-fieldset"><h3>' . __('Cache Contents', 'wp-super-cache') . '</h3>';
    if ($wp_cache_object_cache) {
        echo "<p>" . __("Object cache in use. No cache listing available.", 'wp-super-cache') . "</p>";
        wp_cache_delete_buttons();
        echo "</fieldset>";
        return false;
    }
    $cache_stats = get_option('supercache_stats');
    if (!is_array($cache_stats) || isset($_GET['listfiles']) || $valid_nonce && array_key_exists('action', $_GET) && $_GET['action'] == 'regenerate_cache_stats') {
        $list_files = false;
        // it doesn't list supercached files, and removing single pages is buggy
        $count = 0;
        $expired = 0;
        $now = time();
        if ($handle = @opendir($blog_cache_dir)) {
            $wp_cache_fsize = 0;
            if ($valid_nonce && isset($_GET['action']) && $_GET['action'] == 'deletewpcache') {
                $deleteuri = wpsc_deep_replace(array('..', '\\', 'index.php'), preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', base64_decode($_GET['uri'])));
            } else {
                $deleteuri = '';
            }
            if ($valid_nonce && isset($_GET['action']) && $_GET['action'] == 'deletesupercache') {
                $supercacheuri = wpsc_deep_replace(array('..', '\\', 'index.php'), preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', preg_replace("/(\\?.*)?\$/", '', base64_decode($_GET['uri']))));
                $supercacheuri = trailingslashit(realpath($cache_path . 'supercache/' . $supercacheuri));
                if (wp_cache_confirm_delete($supercacheuri)) {
                    printf(__("Deleting supercache file: <strong>%s</strong><br />", 'wp-super-cache'), $supercacheuri);
                    @unlink($supercacheuri . 'index.html');
                    @unlink($supercacheuri . 'index.html.gz');
                    prune_super_cache($supercacheuri . 'page', true);
                    @rmdir($supercacheuri);
                }
            }
            while (false !== ($file = readdir($handle))) {
                if (strpos($file, $file_prefix) !== false && substr($file, -4) == '.php') {
                    if (false == file_exists($blog_cache_dir . 'meta/' . $file)) {
                        @unlink($blog_cache_dir . $file);
                        continue;
                        // meta does not exist
                    }
                    $mtime = filemtime($blog_cache_dir . 'meta/' . $file);
                    $fsize = @filesize($blog_cache_dir . $file);
                    if ($fsize > 0) {
                        $fsize = $fsize - 15;
                    }
                    // die() command takes 15 bytes at the start of the file
                    $age = $now - $mtime;
                    if ($valid_nonce && isset($_GET['listfiles'])) {
                        $meta = json_decode(wp_cache_get_legacy_cache($blog_cache_dir . 'meta/' . $file), true);
                        if ($deleteuri != '' && $meta['uri'] == $deleteuri) {
                            printf(__("Deleting wp-cache file: <strong>%s</strong><br />", 'wp-super-cache'), esc_html($deleteuri));
                            @unlink($blog_cache_dir . 'meta/' . $file);
                            @unlink($blog_cache_dir . $file);
                            continue;
                        }
                        $meta['age'] = $age;
                        foreach ($meta as $key => $val) {
                            $meta[$key] = esc_html($val);
                        }
                        if ($cache_max_time > 0 && $age > $cache_max_time) {
                            $expired_list[$age][] = $meta;
                        } else {
                            $cached_list[$age][] = $meta;
                        }
                    }
                    if ($cache_max_time > 0 && $age > $cache_max_time) {
                        $expired++;
                    } else {
                        $count++;
                    }
                    $wp_cache_fsize += $fsize;
                    $fsize = intval($fsize / 1024);
                }
            }
            closedir($handle);
        }
        if ($wp_cache_fsize != 0) {
            $wp_cache_fsize = $wp_cache_fsize / 1024;
        } else {
            $wp_cache_fsize = 0;
        }
        if ($wp_cache_fsize > 1024) {
            $wp_cache_fsize = number_format($wp_cache_fsize / 1024, 2) . "MB";
        } elseif ($wp_cache_fsize != 0) {
            $wp_cache_fsize = number_format($wp_cache_fsize, 2) . "KB";
        } else {
            $wp_cache_fsize = '0KB';
        }
        // Supercache files
        $now = time();
        $sizes = array('expired' => 0, 'expired_list' => array(), 'cached' => 0, 'cached_list' => array(), 'ts' => 0);
        if (is_dir($supercachedir)) {
            if ($dh = opendir($supercachedir)) {
                while (($entry = readdir($dh)) !== false) {
                    if ($entry != '.' && $entry != '..') {
                        $sizes = wpsc_dirsize(trailingslashit($supercachedir) . $entry, $sizes);
                    }
                }
                closedir($dh);
            }
        } else {
            $filem = @filemtime($supercachedir);
            if (false == $wp_cache_preload_on && is_file($supercachedir) && $cache_max_time > 0 && $filem + $cache_max_time <= $now) {
                $sizes['expired']++;
                if ($valid_nonce && isset($_GET['listfiles'])) {
                    $sizes['expired_list'][str_replace($cache_path . 'supercache/', '', $supercachedir)] = $now - $filem;
                }
            } else {
                if ($valid_nonce && isset($_GET['listfiles']) && $filem) {
                    $sizes['cached_list'][str_replace($cache_path . 'supercache/', '', $supercachedir)] = $now - $filem;
                }
            }
        }
        $sizes['ts'] = time();
        $cache_stats = array('generated' => time(), 'supercache' => $sizes, 'wpcache' => array('cached' => $count, 'expired' => $expired, 'fsize' => $wp_cache_fsize));
        update_option('supercache_stats', $cache_stats);
    } else {
        echo "<p>" . __('Cache stats are not automatically generated. You must click the link below to regenerate the stats on this page.', 'wp-super-cache') . "</p>";
        echo "<a href='" . wp_nonce_url(add_query_arg(array('page' => 'wpsupercache', 'tab' => 'contents', 'action' => 'regenerate_cache_stats')), 'wp-cache') . "'>" . __('Regenerate cache stats', 'wp-super-cache') . "</a>";
        if (is_array($cache_stats)) {
            echo "<p>" . sprintf(__('Cache stats last generated: %s minutes ago.', 'wp-super-cache'), number_format((time() - $cache_stats['generated']) / 60)) . "</p>";
        }
        $cache_stats = get_option('supercache_stats');
    }
    // regerate stats cache
    if (is_array($cache_stats)) {
        echo "<p><strong>" . __('WP-Cache', 'wp-super-cache') . " ({$cache_stats['wpcache']['fsize']})</strong></p>";
        echo "<ul><li>" . sprintf(__('%s Cached Pages', 'wp-super-cache'), $cache_stats['wpcache']['cached']) . "</li>";
        echo "<li>" . sprintf(__('%s Expired Pages', 'wp-super-cache'), $cache_stats['wpcache']['expired']) . "</li></ul>";
        $divisor = $cache_compression == 1 ? 2 : 1;
        if (array_key_exists('fsize', (array) $cache_stats['supercache'])) {
            $fsize = $cache_stats['supercache']['fsize'] / 1024;
        } else {
            $fsize = 0;
        }
        if ($fsize > 1024) {
            $fsize = number_format($fsize / 1024, 2) . "MB";
        } elseif ($fsize != 0) {
            $fsize = number_format($fsize, 2) . "KB";
        } else {
            $fsize = "0KB";
        }
        echo "<p><strong>" . __('WP-Super-Cache', 'wp-super-cache') . " ({$fsize})</strong></p>";
        echo "<ul><li>" . sprintf(__('%s Cached Pages', 'wp-super-cache'), intval($cache_stats['supercache']['cached'] / $divisor)) . "</li>";
        if (isset($now) && isset($sizes)) {
            $age = intval(($now - $sizes['ts']) / 60);
        } else {
            $age = 0;
        }
        echo "<li>" . sprintf(__('%s Expired Pages', 'wp-super-cache'), intval($cache_stats['supercache']['expired'] / $divisor)) . "</li></ul>";
        if ($valid_nonce && array_key_exists('listfiles', $_GET) && $_GET['listfiles']) {
            echo "<div style='padding: 10px; border: 1px solid #333; height: 400px; width: 90%; overflow: auto'>";
            if (isset($cached_list) && is_array($cached_list) && !empty($cached_list)) {
                echo "<h4>" . __('Fresh WP-Cached Files', 'wp-super-cache') . "</h4>";
                echo "<table class='widefat'><tr><th>#</th><th>" . __('URI', 'wp-super-cache') . "</th><th>" . __('Key', 'wp-super-cache') . "</th><th>" . __('Age', 'wp-super-cache') . "</th><th>" . __('Delete', 'wp-super-cache') . "</th></tr>";
                $c = 1;
                $flip = 1;
                ksort($cached_list);
                foreach ($cached_list as $age => $d) {
                    foreach ($d as $details) {
                        $bg = $flip ? 'style="background: #EAEAEA;"' : '';
                        echo "<tr {$bg}><td>{$c}</td><td> <a href='http://{$details['uri']}'>" . $details['uri'] . "</a></td><td> " . str_replace($details['uri'], '', $details['key']) . "</td><td> {$age}</td><td><a href='" . wp_nonce_url(add_query_arg(array('page' => 'wpsupercache', 'action' => 'deletewpcache', 'uri' => base64_encode($details['uri']))), 'wp-cache') . "#listfiles'>X</a></td></tr>\n";
                        $flip = !$flip;
                        $c++;
                    }
                }
                echo "</table>";
            }
            if (isset($expired_list) && is_array($expired_list) && !empty($expired_list)) {
                echo "<h4>" . __('Stale WP-Cached Files', 'wp-super-cache') . "</h4>";
                echo "<table class='widefat'><tr><th>#</th><th>" . __('URI', 'wp-super-cache') . "</th><th>" . __('Key', 'wp-super-cache') . "</th><th>" . __('Age', 'wp-super-cache') . "</th><th>" . __('Delete', 'wp-super-cache') . "</th></tr>";
                $c = 1;
                $flip = 1;
                ksort($expired_list);
                foreach ($expired_list as $age => $d) {
                    foreach ($d as $details) {
                        $bg = $flip ? 'style="background: #EAEAEA;"' : '';
                        echo "<tr {$bg}><td>{$c}</td><td> <a href='http://{$details['uri']}'>" . $details['uri'] . "</a></td><td> " . str_replace($details['uri'], '', $details['key']) . "</td><td> {$age}</td><td><a href='" . wp_nonce_url(add_query_arg(array('page' => 'wpsupercache', 'action' => 'deletewpcache', 'uri' => base64_encode($details['uri']))), 'wp-cache') . "#listfiles'>X</a></td></tr>\n";
                        $flip = !$flip;
                        $c++;
                    }
                }
                echo "</table>";
            }
            if (is_array($sizes['cached_list']) & !empty($sizes['cached_list'])) {
                echo "<h4>" . __('Fresh Super Cached Files', 'wp-super-cache') . "</h4>";
                echo "<table class='widefat'><tr><th>#</th><th>" . __('URI', 'wp-super-cache') . "</th><th>" . __('Age', 'wp-super-cache') . "</th><th>" . __('Delete', 'wp-super-cache') . "</th></tr>";
                $c = 1;
                $flip = 1;
                ksort($sizes['cached_list']);
                foreach ($sizes['cached_list'] as $age => $d) {
                    foreach ($d as $uri => $n) {
                        $bg = $flip ? 'style="background: #EAEAEA;"' : '';
                        echo "<tr {$bg}><td>{$c}</td><td> <a href='http://{$uri}'>" . $uri . "</a></td><td>{$age}</td><td><a href='" . wp_nonce_url(add_query_arg(array('page' => 'wpsupercache', 'action' => 'deletesupercache', 'uri' => base64_encode($uri))), 'wp-cache') . "#listfiles'>X</a></td></tr>\n";
                        $flip = !$flip;
                        $c++;
                    }
                }
                echo "</table>";
            }
            if (is_array($sizes['expired_list']) && !empty($sizes['expired_list'])) {
                echo "<h4>" . __('Stale Super Cached Files', 'wp-super-cache') . "</h4>";
                echo "<table class='widefat'><tr><th>#</th><th>" . __('URI', 'wp-super-cache') . "</th><th>" . __('Age', 'wp-super-cache') . "</th><th>" . __('Delete', 'wp-super-cache') . "</th></tr>";
                $c = 1;
                $flip = 1;
                ksort($sizes['expired_list']);
                foreach ($sizes['expired_list'] as $age => $d) {
                    foreach ($d as $uri => $n) {
                        $bg = $flip ? 'style="background: #EAEAEA;"' : '';
                        echo "<tr {$bg}><td>{$c}</td><td> <a href='http://{$uri}'>" . $uri . "</a></td><td>{$age}</td><td><a href='" . wp_nonce_url(add_query_arg(array('page' => 'wpsupercache', 'action' => 'deletesupercache', 'uri' => base64_encode($uri))), 'wp-cache') . "#listfiles'>X</a></td></tr>\n";
                        $flip = !$flip;
                        $c++;
                    }
                }
                echo "</table>";
            }
            echo "</div>";
            echo "<p><a href='?page=wpsupercache&tab=contents#top'>" . __('Hide file list', 'wp-super-cache') . "</a></p>";
        } elseif ($cache_stats['supercache']['cached'] > 500 || $cache_stats['supercache']['expired'] > 500 || $cache_stats['wpcache']['cached'] / $divisor > 500 || $cache_stats['wpcache']['expired'] / $divisor > 500) {
            echo "<p><em>" . __('Too many cached files, no listing possible.', 'wp-super-cache') . "</em></p>";
        } else {
            echo "<p><a href='" . wp_nonce_url(add_query_arg(array('page' => 'wpsupercache', 'listfiles' => '1')), 'wp-cache') . "#listfiles'>" . __('List all cached files', 'wp-super-cache') . "</a></p>";
        }
        if ($cache_max_time > 0) {
            echo "<p>" . sprintf(__('Expired files are files older than %s seconds. They are still used by the plugin and are deleted periodically.', 'wp-super-cache'), $cache_max_time) . "</p>";
        }
    }
    // cache_stats
    wp_cache_delete_buttons();
    echo '</fieldset>';
}
예제 #2
0
function wp_cache_ob_callback($buffer)
{
    global $wp_cache_pages, $wp_query, $wp_super_cache_query, $cache_acceptable_files, $wp_cache_no_cache_for_get, $wp_cache_object_cache, $wp_cache_request_uri, $do_rebuild_list;
    $buffer = apply_filters('wp_cache_ob_callback_filter', $buffer);
    $script = basename($_SERVER['PHP_SELF']);
    // All the things that can stop a page being cached
    $cache_this_page = true;
    if (defined('DONOTCACHEPAGE')) {
        wp_cache_debug('DONOTCACHEPAGE defined. Caching disabled.', 2);
        $cache_this_page = false;
    } elseif ($wp_cache_no_cache_for_get && false == empty($_GET) && false == defined('DOING_CRON')) {
        wp_cache_debug("Non empty GET request. Caching disabled on settings page. " . serialize($_GET), 1);
        $cache_this_page = false;
    } elseif ($_SERVER["REQUEST_METHOD"] == 'POST' || !empty($_POST) || get_option('gzipcompression')) {
        wp_cache_debug('Not caching POST request.', 5);
        $cache_this_page = false;
    } elseif ($wp_cache_object_cache && !empty($_GET)) {
        wp_cache_debug('Not caching GET request while object cache storage enabled.', 5);
        $cache_this_page = false;
    } elseif (isset($_GET['preview'])) {
        wp_cache_debug('Not caching preview post.', 2);
        $cache_this_page = false;
    } elseif (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri)) {
        wp_cache_debug('URI rejected. Not Caching', 2);
        $cache_this_page = false;
    } elseif (wp_cache_user_agent_is_rejected()) {
        wp_cache_debug("USER AGENT ({$_SERVER['HTTP_USER_AGENT']}) rejected. Not Caching", 4);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['single']) && $wp_cache_pages['single'] == 1 && isset($wp_super_cache_query['is_single'])) {
        wp_cache_debug('Not caching single post.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['pages']) && $wp_cache_pages['pages'] == 1 && isset($wp_super_cache_query['is_page'])) {
        wp_cache_debug('Not caching single page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['archives']) && $wp_cache_pages['archives'] == 1 && isset($wp_super_cache_query['is_archive'])) {
        wp_cache_debug('Not caching archive page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['tag']) && $wp_cache_pages['tag'] == 1 && isset($wp_super_cache_query['is_tag'])) {
        wp_cache_debug('Not caching tag page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['category']) && $wp_cache_pages['category'] == 1 && isset($wp_super_cache_query['is_category'])) {
        wp_cache_debug('Not caching category page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['frontpage']) && $wp_cache_pages['frontpage'] == 1 && isset($wp_super_cache_query['is_front_page'])) {
        wp_cache_debug('Not caching front page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['home']) && $wp_cache_pages['home'] == 1 && isset($wp_super_cache_query['is_home'])) {
        wp_cache_debug('Not caching home page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['search']) && $wp_cache_pages['search'] == 1 && isset($wp_super_cache_query['is_search'])) {
        wp_cache_debug('Not caching search page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['author']) && $wp_cache_pages['author'] == 1 && isset($wp_super_cache_query['is_author'])) {
        wp_cache_debug('Not caching author page.', 2);
        $cache_this_page = false;
    } elseif (isset($wp_cache_pages['feed']) && $wp_cache_pages['feed'] == 1 && isset($wp_super_cache_query['is_feed'])) {
        wp_cache_debug('Not caching feed.', 2);
        $cache_this_page = false;
    }
    if (!isset($wp_query)) {
        wp_cache_debug('wp_cache_ob_callback: WARNING! $query not defined but the plugin has worked around that problem.', 4);
    }
    if ($cache_this_page) {
        wp_cache_debug('Output buffer callback', 4);
        $buffer = wp_cache_get_ob($buffer);
        wp_cache_shutdown_callback();
        return $buffer;
    } else {
        if (is_array($do_rebuild_list) && false == empty($do_rebuild_list)) {
            foreach ($do_rebuild_list as $dir => $n) {
                if (wp_cache_confirm_delete($dir)) {
                    wp_cache_debug('wp_cache_ob_callback clearing rebuilt files in ' . $dir);
                    $files_to_check = get_all_supercache_filenames($dir);
                    foreach ($files_to_check as $cache_file) {
                        $cache_file = $dir . $cache_file;
                        @unlink($cache_file);
                    }
                }
            }
        }
        return wp_cache_maybe_dynamic($buffer);
    }
}