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>'; }
function get_current_url_supercache_dir($post_id = 0) { global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST, $wp_cache_home_path; static $saved_supercache_dir = array(); if (isset($saved_supercache_dir[$post_id])) { return $saved_supercache_dir[$post_id]; } $DONOTREMEMBER = 0; if ($post_id != 0) { $site_url = site_url(); $permalink = get_permalink($post_id); if (false === strpos($permalink, $site_url)) { /* * Sometimes site_url doesn't return the siteurl. See http://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made */ $DONOTREMEMBER = 1; wp_cache_debug("get_current_url_supercache_dir: warning! site_url ({$site_url}) not found in permalink ({$permalink}).", 1); if (false === strpos($permalink, $WPSC_HTTP_HOST)) { wp_cache_debug("get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ({$permalink}). ", 1); $p = parse_url($permalink); if (is_array($p)) { $uri = $p['path']; wp_cache_debug("get_current_url_supercache_dir: WARNING! Using {$uri} as permalink. Used parse_url.", 1); } else { wp_cache_debug("get_current_url_supercache_dir: WARNING! Permalink ({$permalink}) could not be understood by parse_url. Using front page.", 1); $uri = ''; } } else { wp_cache_debug("get_current_url_supercache_dir: Removing SERVER_NAME ({$WPSC_HTTP_HOST}) and {$protocol} from permalink ({$permalink}). Is the url right?", 1); $uri = str_replace($WPSC_HTTP_HOST, '', $permalink); $uri = str_replace('http://', '', $uri); $uri = str_replace('https://', '', $uri); } } else { $uri = str_replace($site_url, '', $permalink); if (strpos($uri, $wp_cache_home_path) !== 0) { $uri = rtrim($wp_cache_home_path, '/') . $uri; } } } else { $uri = strtolower($wp_cache_request_uri); } $uri = wpsc_deep_replace(array('..', '\\', 'index.php'), preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', preg_replace("/(\\?.*)?\$/", '', $uri))); $dir = preg_replace('/:.*$/', '', $WPSC_HTTP_HOST) . $uri; // To avoid XSS attacks if (function_exists("apply_filters")) { $dir = apply_filters('supercache_dir', $dir); } else { $dir = do_cacheaction('supercache_dir', $dir); } $dir = $cache_path . 'supercache/' . $dir . '/'; if (is_array($cached_direct_pages) && in_array($_SERVER['REQUEST_URI'], $cached_direct_pages)) { $dir = ABSPATH . $uri . '/'; } $dir = str_replace('..', '', str_replace('//', '/', $dir)); wp_cache_debug("supercache dir: {$dir}", 5); if ($DONOTREMEMBER == 0) { $saved_supercache_dir[$post_id] = $dir; } return $dir; }