home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Plugins / wp-super-cache / wp-cache.php < prev   
PHP Script  |  2008-02-18  |  44KB  |  1,036 lines

  1. <?php
  2. /*
  3. Plugin Name: WP Super Cache
  4. Plugin URI: http://ocaoimh.ie/wp-super-cache/
  5. Description: Very fast caching module for WordPress. Once enabled, you must <a href="options-general.php?page=wp-super-cache/wp-cache.php">enable the cache</a>. Based on WP-Cache by <a href="http://mnm.uib.es/gallir/">Ricardo Galli Granada</a>.
  6. Version: 0.5.4
  7. Author: Donncha O Caoimh
  8. Author URI: http://ocaoimh.ie/
  9. */
  10. /*  Copyright 2005-2006  Ricardo Galli Granada  (email : gallir@uib.es)
  11.     Some code copyright 2008 Donncha O Caoimh (http://ocaoimh.ie/)
  12.  
  13.     This program is free software; you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation; either version 2 of the License, or
  16.     (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  26. */
  27.  
  28. $wp_cache_config_file = ABSPATH . 'wp-content/wp-cache-config.php';
  29.  
  30. if( !@include($wp_cache_config_file) ) {
  31.     get_wpcachehome();
  32.     $wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
  33.     @include($wp_cache_config_file_sample);
  34. } else {
  35.     get_wpcachehome();
  36. }
  37.  
  38. $wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
  39. $wp_cache_link = ABSPATH . 'wp-content/advanced-cache.php';
  40. $wp_cache_file = WPCACHEHOME . 'wp-cache-phase1.php';
  41.  
  42. include(WPCACHEHOME . 'wp-cache-base.php');
  43.  
  44. function get_wpcachehome() {
  45.     if( defined( 'WPCACHEHOME' ) == false ) {
  46.         if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
  47.             define( 'WPCACHEHOME', trailingslashit( dirname(__FILE__) ) );
  48.         } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
  49.             define( 'WPCACHEHOME', dirname(__FILE__) . '/wp-super-cache/' );
  50.         } else {
  51.             die( 'Please create wp-content/wp-cache-config.php from wp-super-cache/wp-cache-config-sample.php' );
  52.         }
  53.     }
  54. }
  55.  
  56. function wp_cache_add_pages() {
  57.     if( function_exists( 'is_site_admin' ) ) {
  58.         if( is_site_admin() ) {
  59.             add_submenu_page('wpmu-admin.php', __('WP Super Cache'), __('WP Super Cache'), 'manage_options', __FILE__, 'wp_cache_manager');
  60.             add_options_page('WP Super Cache', 'WP Super Cache', 'manage_options', __FILE__, 'wp_cache_manager');
  61.         }
  62.     } else {
  63.         add_options_page('WP Super Cache', 'WP Super Cache', 'manage_options', __FILE__, 'wp_cache_manager');
  64.     }
  65. }
  66.  
  67. function wp_cache_manager() {
  68.     global $wp_cache_config_file, $valid_nonce, $supercachedir, $cache_path, $cache_enabled, $cache_compression, $super_cache_enabled, $wp_cache_hello_world;
  69.  
  70.     if( function_exists( 'is_site_admin' ) )
  71.         if( !is_site_admin() )
  72.             return;
  73.  
  74.     $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '',  $_SERVER["HTTP_HOST"]);
  75.     if( get_option( 'gzipcompression' ) == 1 )
  76.         update_option( 'gzipcompression', 0 );
  77.     $valid_nonce = wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache');
  78.     
  79.      echo '<div class="wrap">';
  80.     echo "<h2>WP Super Cache Manager</h2>\n";
  81.     if( ini_get( 'safe_mode' ) ) {
  82.         ?><h3>Warning! PHP safe mode enabled!</h3>
  83.         <p>You may experience problems running this plugin because SAFE MODE is enabled. <?php
  84.         if( !ini_get( 'safe_mode_gid' ) ) {
  85.             ?>Your server is set up to check the owner of PHP scripts before allowing them to read and write files.</p><p>You or an administrator may be able to make it work by changing the group owner of the plugin scripts to match that of the web server user. The group owner of the wp-content/cache/ directory must also be changed. See the <a href='http://php.net/features.safe-mode'>safe mode manual page</a> for further details.</p><?php
  86.         } else {
  87.             ?>You or an administrator must disable this. See the <a href='http://php.net/features.safe-mode'>safe mode manual page</a> for further details. This cannot be disabled in a .htaccess file unfortunately. It must be done in the php.ini config file.</p><?php
  88.         }
  89.     }
  90.     if(isset($_REQUEST['wp_restore_config']) && $valid_nonce) {
  91.         unlink($wp_cache_config_file);
  92.         echo '<strong>Configuration file changed, some values might be wrong. Load the page again from the "Options" menu to reset them.</strong>';
  93.     }
  94.  
  95.     if ( !wp_cache_check_link() ||
  96.         !wp_cache_verify_config_file() ||
  97.         !wp_cache_verify_cache_dir() ) {
  98.         echo "<br>Cannot continue... fix previous problems and retry.<br />";
  99.         echo "</div>\n";
  100.         return;
  101.     }
  102.  
  103.     if (!wp_cache_check_global_config()) {
  104.         echo "</div>\n";
  105.         return;
  106.     }
  107.  
  108.     if( !got_mod_rewrite() ) {
  109.         ?><h4 style='color: #a00'>Mod rewrite may not be installed!</h4>
  110.         <p>It appears that mod_rewrite is not installed. Sometimes this check isn't 100% reliable, especially if you are not using Apache. Please verify that the mod_rewrite module is loaded. It is required for serving Super Cache static files. You will still be able to use WP-Cache.</p><?php
  111.     }
  112.  
  113.     if( !is_writeable( ABSPATH . 'wp-content/' ) || !is_writable($wp_cache_config_file) ) {
  114.         define( "SUBMITDISABLED", 'disabled style="color: #aaa" ' );
  115.         ?><h4 style='color: #a00'>Read Only Mode. Configuration cannot be changed. <a href="javascript:toggleLayer('readonlywarning');" title="Why your configuration may not be changed">Why</a></h4>
  116.         <div id='readonlywarning' style='border: 1px solid #aaa; margin: 2px; padding: 2px; display: none;'>
  117.         <p>The WP Super Cache configuration file is <code><?php echo ABSPATH ?>wp-content/wp-cache-config.php</code> and cannot be modified. The wp-content directory and wp-cache-config.php file must be writeable by the webserver to make any changes.<br />
  118.         A simple way of doing that is by changing the permissions temporarily using the CHMOD command or through your ftp client. Make sure it's globally writeable and it should be fine.<br />
  119.         Writeable: <code>chmod 777 wp-content; chmod 666 wp-content/wp-cache-config.php</code><br />
  120.         Readonly: <code>chmod 755 wp-content; chmod 644 wp-content/wp-cache-config.php</code></p>
  121.         </div><?php
  122.     } else {
  123.         define( "SUBMITDISABLED", ' ' );
  124.     }
  125.  
  126.     if ( $valid_nonce ) {
  127.         if( isset( $_POST[ 'wp_cache_status' ] ) ) {
  128.             switch( $_POST[ 'wp_cache_status' ] ) {
  129.                 case 'all':
  130.                     wp_cache_enable();
  131.                     break;
  132.                 case 'none':
  133.                     wp_cache_disable();
  134.                     break;
  135.                 case 'wpcache':
  136.                     wp_cache_enable();
  137.                     wp_super_cache_disable();
  138.                     break;
  139.             }
  140.             if( isset( $_POST[ 'wp_cache_hello_world' ] ) ) {
  141.                 $wp_cache_hello_world = (int)$_POST[ 'wp_cache_hello_world' ];
  142.             } else {
  143.                 $wp_cache_hello_world = 0;
  144.             }
  145.             wp_cache_replace_line('^ *\$wp_cache_hello_world', '$wp_cache_hello_world = ' . (int)$wp_cache_hello_world . ";", $wp_cache_config_file);
  146.         }
  147.         if( isset( $_POST[ 'cache_compression' ] ) && $_POST[ 'cache_compression' ] != $cache_compression ) {
  148.             $cache_compression_changed = true;
  149.             $cache_compression = intval( $_POST[ 'cache_compression' ] );
  150.             wp_cache_replace_line('^ *\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
  151.             if( function_exists( 'prune_super_cache' ) )
  152.                 prune_super_cache ($cache_path, true);
  153.             delete_option( 'super_cache_meta' );
  154.         }
  155.     }
  156.  
  157.     ?><fieldset style='border: 1px solid #aaa' class="options"> 
  158.     <legend>WP Super Cache Status</legend><?php
  159.     echo '<form name="wp_manager" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  160.     ?>
  161.     <label><input type='radio' name='wp_cache_status' value='all' <?php if( $cache_enabled == true && $super_cache_enabled == true ) { echo 'checked=checked'; } ?>> WP Cache and Super Cache enabled</label><br />
  162.     <label><input type='radio' name='wp_cache_status' value='none' <?php if( $cache_enabled == false ) { echo 'checked=checked'; } ?>> WP Cache and Super Cache disabled</label><br />
  163.     <label><input type='radio' name='wp_cache_status' value='wpcache' <?php if( $cache_enabled == true && $super_cache_enabled == false ) { echo 'checked=checked'; } ?>> Super Cache Disabled</label><br />
  164.     <p><label><input type='checkbox' name='wp_cache_hello_world' <?php if( $wp_cache_hello_world ) echo "checked"; ?> value='1'> Proudly tell the world your server is Digg proof! (places a message in your blog's footer)</label></p>
  165.     <?php
  166.     echo "<div class='submit'><input " . SUBMITDISABLED . "type='submit' value='Update Status »' /></div>";
  167.     wp_nonce_field('wp-cache');
  168.     ?>
  169.     </form>
  170.     </fieldset><br />
  171.  
  172.     <fieldset style='border: 1px solid #aaa' class="options"> 
  173.     <legend>Super Cache Compression</legend>
  174.     <form name="wp_manager" action="<?php echo $_SERVER["REQUEST_URI"]; ?>" method="post">
  175.     <label><input type="radio" name="cache_compression" value="1" <?php if( $cache_compression ) { echo "checked=checked"; } ?>> Enabled</label>
  176.     <label><input type="radio" name="cache_compression" value="0" <?php if( !$cache_compression ) { echo "checked=checked"; } ?>> Disabled</label>
  177.     <p>Compression is disabled by default because some hosts have problems with compressed files. Switching this on and off clears the cache.</p>
  178.     <?php
  179.     if( isset( $cache_compression_changed ) && isset( $_POST[ 'cache_compression' ] ) && !$cache_compression ) {
  180.         ?><p><strong>Super Cache compression is now disabled.</strong></p> <?php
  181.     } elseif( isset( $cache_compression_changed ) && isset( $_POST[ 'cache_compression' ] ) && $cache_compression ) {
  182.         ?><p><strong>Super Cache compression is now enabled.</strong></p><?php
  183.     }
  184.     echo '<div class="submit"><input ' . SUBMITDISABLED . 'type="submit" value="Update Compression »" /></div>';
  185.     wp_nonce_field('wp-cache');
  186.     echo "</form>\n";
  187.     ?></fieldset><br />
  188.  
  189.     <fieldset style='border: 1px solid #aaa' class="options"> 
  190.     <legend>Mod Rewrite Rules</legend><?php
  191.     $home_path = get_home_path();
  192.     $home_root = parse_url(get_option('home'));
  193.     $home_root = trailingslashit($home_root['path']);
  194.     $wprules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WordPress' ) );
  195.     $wprules = str_replace( "RewriteEngine On\n", '', $wprules );
  196.     $wprules = str_replace( "RewriteBase $home_root\n", '', $wprules );
  197.  
  198.     $dohtaccess = false;
  199.     if( !$wprules || $wprules == '' ) {
  200.         echo "<h4 style='color: #a00'>Mod Rewrite rules not updated!</h4>";
  201.         echo "<p>You must have <strong>BEGIN</strong> and <strong>END</strong> markers in {$home_path}.htaccess for the auto update to work. They look like this and surround the main WordPress mod_rewrite rules:
  202.         <blockquote><code><em># BEGIN WordPress</em><br /> RewriteCond %{REQUEST_FILENAME} !-f<br /> RewriteCond %{REQUEST_FILENAME} !-d<br /> RewriteRule . /index.php [L]<br /> <em># END WordPress</em></code></blockquote>
  203.         Refresh this page when you have updated your .htaccess file to add the Super Cache rules.";
  204.     } elseif( strpos( $wprules, 'supercache' ) == false ) { // only write the rules once
  205.         $dohtaccess = true;
  206.     }
  207.     $rules = "<IfModule mod_rewrite.c>\n";
  208.     $rules .= "RewriteEngine On\n";
  209.     $rules .= "RewriteBase $home_root\n"; // props Chris Messina
  210.     $rules .= "RewriteCond %{QUERY_STRING} !.*s=.*\n";
  211.     $rules .= "RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$\n";
  212.     $rules .= "RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$\n";
  213.     $rules .= "RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$\n";
  214.     $rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
  215.     $rules .= "RewriteCond %{DOCUMENT_ROOT}{$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1index.html.gz -f\n";
  216.     $rules .= "RewriteRule ^(.*) {$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1index.html.gz [L]\n\n";
  217.  
  218.     $rules .= "RewriteCond %{QUERY_STRING} !.*s=.*\n";
  219.     $rules .= "RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$\n";
  220.     $rules .= "RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$\n";
  221.     $rules .= "RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$\n";
  222.     $rules .= "RewriteCond %{DOCUMENT_ROOT}{$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1index.html -f\n";
  223.     $rules .= "RewriteRule ^(.*) {$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1index.html [L]\n";
  224.     $rules .= "WPRULES\n";
  225.     $rules .= "</IfModule>";
  226.     if( $dohtaccess ) {
  227.         $rules = str_replace( 'WPRULES', $wprules, $rules );
  228.         if( insert_with_markers( $home_path.'.htaccess', 'WordPress', explode( "\n", $rules ) ) ) {
  229.             echo "<h4>Mod Rewrite rules updated!</h4>";
  230.             echo "<p><strong>" . ABSPATH . ".htaccess has been updated with the necessary mod_rewrite rules. Please verify they are correct. The file should look like this:</strong></p>\n";
  231.         } else {
  232.             echo "<h4>Mod Rewrite rules must be updated!</h4>";
  233.             echo "<p><strong> Your " . ABSPATH . ".htaccess is not writable by the webserver and must be updated with the necessary mod_rewrite rules. The new rules go above the regular WordPress rules as shown in the code below:</strong></p>\n";
  234.         }
  235.         echo "<p><pre># BEGIN WordPress\n{$rules}# END WordPress</pre></p>\n";
  236.     } else {
  237.         $rules = str_replace( 'WPRULES', '', $rules );
  238.         /* http://www.netlobo.com/div_hiding.html */
  239.         ?>
  240. <script type='text/javascript'>
  241. <!--
  242. function toggleLayer( whichLayer ) {
  243.   var elem, vis;
  244.   if( document.getElementById ) // this is the way the standards work
  245.     elem = document.getElementById( whichLayer );
  246.   else if( document.all ) // this is the way old msie versions work
  247.       elem = document.all[whichLayer];
  248.   else if( document.layers ) // this is the way nn4 works
  249.     elem = document.layers[whichLayer];
  250.   vis = elem.style;
  251.   // if the style.display value is blank we try to figure it out here
  252.   if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
  253.     vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  254.   vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  255. }
  256. // -->
  257. </script>
  258.         <p>WP Super Cache has modified your <?php echo ABSPATH ?>.htaccess file. Click the following link to see the lines added. If you have upgraded the plugin make sure these rules match. <a href="javascript:toggleLayer('rewriterules');" title="See your mod_rewrite rules">View mod_rewrite rules</a>
  259.         <div id='rewriterules' style='display: none;'>
  260.         <?php echo "<p><pre># BEGIN WordPress\n{$rules}# END WordPress</pre></p>\n"; ?>
  261.         </div>
  262.         <?php
  263.     }
  264.     // http://allmybrain.com/2007/11/08/making-wp-super-cache-gzip-compression-work/
  265.     if( !is_file( $cache_path . '.htaccess' ) ) {
  266.         $gziprules = "AddEncoding x-gzip .gz\n";
  267.         $gziprules .= "AddType text/html .gz";
  268.         $gziprules = insert_with_markers( $cache_path . '.htaccess', 'supercache', explode( "\n", $gziprules ) );
  269.         echo "<h4>Gzip encoding rules in {$cache_path}.htaccess created.</h4>";
  270.     }
  271.  
  272.     ?></fieldset><?php
  273.  
  274.     wp_cache_edit_max_time();
  275.  
  276.     echo '<br /><a name="files"></a><fieldset style="border: 1px solid #aaa" class="options"><legend>Accepted filenames, rejected URIs</legend>';
  277.     wp_cache_edit_rejected();
  278.     echo "<br />\n";
  279.     wp_cache_edit_accepted();
  280.     echo '</fieldset>';
  281.  
  282.     wp_cache_edit_rejected_ua();
  283.  
  284.     wp_lock_down();
  285.  
  286.     wp_cache_files();
  287.  
  288.     wp_cache_restore();
  289.  
  290.     ob_start();
  291.     if( defined( 'WP_CACHE' ) ) {
  292.         if( function_exists( 'do_cacheaction' ) ) {
  293.             do_cacheaction( 'cache_admin_page' );
  294.         }
  295.     }
  296.     $out = ob_get_contents();
  297.     ob_end_clean();
  298.     if( SUBMITDISABLED == ' ' && $out != '' ) {
  299.         echo '<fieldset class="options"><legend>Cache Plugins</legend>';
  300.         echo $out;
  301.         echo '</fieldset>';
  302.     }
  303.  
  304.     echo "</div>\n";
  305.  
  306. }
  307.  
  308. function wp_cache_restore() {
  309.     echo '<br /><fieldset style="border: 1px solid #aaa" class="options"><legend>Configuration messed up?</legend>';
  310.     echo '<form name="wp_restore" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  311.     echo '<input type="hidden" name="wp_restore_config" />';
  312.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'id="deletepost" value="Restore default configuration »" /></div>';
  313.     wp_nonce_field('wp-cache');
  314.     echo "</form>\n";
  315.     echo '</fieldset>';
  316.  
  317. }
  318.  
  319. function comment_form_lockdown_message() {
  320.     ?><p><?php _e( "Comment moderation is enabled. Your comment may take some time to appear." ); ?></p><?php
  321. }
  322. if( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) )
  323.     add_action( 'comment_form', 'comment_form_lockdown_message' );
  324.  
  325. function wp_lock_down() {
  326.     global $wpdb, $cache_path, $wp_cache_config_file, $valid_nonce, $cached_direct_pages;
  327.  
  328.     if(isset($_POST['wp_lock_down']) && $valid_nonce) {
  329.         $wp_lock_down = $_POST['wp_lock_down'] == '1' ? '1' : '0';
  330.         wp_cache_replace_line('^.*WPLOCKDOWN', "define( 'WPLOCKDOWN', '$wp_lock_down' );", $wp_cache_config_file);
  331.         if( $wp_lock_down == '0' && function_exists( 'prune_super_cache' ) )
  332.             prune_super_cache( $cache_path, true ); // clear the cache after lockdown
  333.  
  334.     }
  335.     if( !isset( $wp_lock_down ) ) {
  336.         if( defined( 'WPLOCKDOWN' ) ) {
  337.             $wp_lock_down = constant( 'WPLOCKDOWN' );
  338.         } else {
  339.             $wp_lock_down = '0';
  340.         }
  341.     }
  342.     ?><br /><fieldset style='border: 1px solid #aaa' class="options"> 
  343.     <legend>Lock Down: <span style='color: #f00'><?php echo $wp_lock_down == '0' ? 'disabled' : 'enabled'; ?></span></legend>
  344.     <p>Prepare your server for an expected spike in traffic by enabling the lock down. When this is enabled, new comments on a post will not refresh the cached static files.</p>
  345.     <p>Developers: Make your plugin lock down compatible by checking the 'WPLOCKDOWN' constant. The following code will make sure your plugin respects the WPLOCKDOWN setting.
  346.     <blockquote><code>if( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) ) { <br />
  347.             echo "Sorry. My blog is locked down. Updates will appear shortly";<br />
  348.         }</code></blockquote>
  349.     <?php
  350.     if( $wp_lock_down == '1' ) {
  351.         ?><strong>WordPress is locked down. Super Cache static files will not be deleted when new comments are made.</strong><?php
  352.     } else {
  353.         ?><strong>WordPress is not locked down. New comments will refresh Super Cache static files as normal.</strong><?php
  354.     }
  355.     $new_lockdown =  $wp_lock_down == '1' ? '0' : '1';
  356.     $new_lockdown_desc =  $wp_lock_down == '1' ? 'Disable' : 'Enable';
  357.     echo '<form name="wp_lock_down" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  358.     echo "<input type='hidden' name='wp_lock_down' value='{$new_lockdown}' />";
  359.     echo "<div class='submit'><input type='submit' " . SUBMITDISABLED . "value='{$new_lockdown_desc} Lock Down »' /></div>";
  360.     wp_nonce_field('wp-cache');
  361.     echo "</form>\n";
  362.  
  363.     ?></fieldset><br /><?php
  364.     ?><fieldset style='border: 1px solid #aaa' class="options"> 
  365.     <legend>Directly Cached Files (advanced use only)</legend><?php
  366.  
  367.     $out = '';
  368.     if( $valid_nonce && is_array( $_POST[ 'direct_pages' ] ) && !empty( $_POST[ 'direct_pages' ] ) ) {
  369.         $expiredfiles = array_diff( $cached_direct_pages, $_POST[ 'direct_pages' ] );
  370.         unset( $cached_direct_pages );
  371.         foreach( $_POST[ 'direct_pages' ] as $page ) {
  372.             $page = $wpdb->escape( $page );
  373.             if( $page != '' ) {
  374.                 $cached_direct_pages[] = $page;
  375.                 $out .= "'$page', ";
  376.             }
  377.         }
  378.         if( $out == '' ) {
  379.             $out = "'', ";
  380.         }
  381.     }
  382.     if( $valid_nonce && $_POST[ 'new_direct_page' ] && '' != $_POST[ 'new_direct_page' ] ) {
  383.         $page = str_replace( get_option( 'siteurl' ), '', $_POST[ 'new_direct_page' ] );
  384.         if( substr( $page, 0, 1 ) != '/' )
  385.             $page = '/' . $page;
  386.         $page = $wpdb->escape( $page );
  387.         if( in_array( $page, $cached_direct_pages ) == false ) {
  388.             $cached_direct_pages[] = $page;
  389.             $out .= "'$page', ";
  390.         }
  391.     }
  392.  
  393.     if( $out != '' ) {
  394.         $out = substr( $out, 0, -2 );
  395.         $out = '$cached_direct_pages = array( ' . $out . ' );';
  396.         wp_cache_replace_line('^ *\$cached_direct_pages', "$out", $wp_cache_config_file);
  397.         prune_super_cache( $cache_path, true );
  398.     }
  399.  
  400.     if( !empty( $expiredfiles ) ) {
  401.         foreach( $expiredfiles as $file ) {
  402.             if( $file != '' ) {
  403.                 $firstfolder = explode( '/', $file );
  404.                 $firstfolder = ABSPATH . $firstfolder[1];
  405.                 $file = ABSPATH . $file;
  406.                 @unlink( trailingslashit( $file ) . 'index.html' );
  407.                 @unlink( trailingslashit( $file ) . 'index.html.gz' );
  408.                 RecursiveFolderDelete( trailingslashit( $firstfolder ) );
  409.             }
  410.         }
  411.     }
  412.  
  413.     if( $valid_nonce && $_POST[ 'deletepage' ] ) {
  414.         $page = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '..', '', $_POST['deletepage']) );
  415.         $pagefile = ABSPATH . $page . 'index.html';
  416.         $firstfolder = explode( '/', $page );
  417.         $firstfolder = ABSPATH . $firstfolder[1];
  418.         $page = ABSPATH . $page;
  419.         if( is_file( $pagefile ) && is_writable( $pagefile ) && is_writable( $firstfolder ) ) {
  420.             @unlink( $pagefile );
  421.             @unlink( $pagefile . '.gz' );
  422.             RecursiveFolderDelete( $firstfolder );
  423.             echo "<strong>$pagefile removed!</strong><br />";
  424.             prune_super_cache( $cache_path, true );
  425.         }
  426.     }
  427.  
  428.     $readonly = '';
  429.     if( !is_writeable( ABSPATH ) ) {
  430.         $readonly = 'READONLY';
  431.         ?><p><strong style='color: #a00'>WARNING! You must make <?php echo ABSPATH ?> writable to enable this feature. As this is a security risk please make it readonly after your page is generated.</strong></p><?php
  432.     } else {
  433.         ?><p><strong style='color: #a00'>WARNING! <?php echo ABSPATH ?> is writable. Please make it readonly after your page is generated as this is a security risk.</strong></p><?php
  434.     }
  435.     echo '<form name="direct_page" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  436.     if( is_array( $cached_direct_pages ) ) {
  437.         $out = '';
  438.         foreach( $cached_direct_pages as $page ) {
  439.             if( $page == '' )
  440.                 continue;
  441.             $generated = '';
  442.             if( is_file( ABSPATH . $page . '/index.html' ) )
  443.                 $generated = '<input type="Submit" name="deletepage" value="' . $page . '">';
  444.             $out .= "<tr><td><input type='text' $readonly name='direct_pages[]' size='30' value='$page' /></td><td>$generated</td></tr>";
  445.         }
  446.         if( $out != '' ) {
  447.             ?><table><tr><th>Existing direct page</th><th>Delete cached file</th></tr><?php
  448.             echo "$out</table>";
  449.         }
  450.     }
  451.     if( $readonly != 'READONLY' )
  452.         echo "Add direct page: <input type='text' $readonly name='new_direct_page' size='30' value='' /><br />";
  453.  
  454.     echo "<p>Directly cached files are files created directly off " . ABSPATH . " where your blog lives. This feature is only useful if you are expecting a major Digg or Slashdot level of traffic to one post or page.</p>";
  455.     if( $readonly != 'READONLY' ) {
  456.         echo "<p>For example: to cache <em>'" . trailingslashit( get_option( 'siteurl' ) ) . "about/'</em>, you would enter '" . trailingslashit( get_option( 'siteurl' ) ) . "about/' or '/about/'. The cached file will be generated the next time an anonymous user visits that page.</p>";
  457.         echo "<p>Make the textbox blank to remove it from the list of direct pages and delete the cached file.</p>";
  458.     }
  459.  
  460.     wp_nonce_field('wp-cache');
  461.     if( $readonly != 'READONLY' )
  462.         echo "<div class='submit'><input type='submit' ' . SUBMITDISABLED . 'value='Update direct pages »' /></div>";
  463.     echo "</form>\n";
  464.     ?></fieldset><?php
  465. }
  466.  
  467. function RecursiveFolderDelete ( $folderPath ) { // from http://www.php.net/manual/en/function.rmdir.php
  468.     if( trailingslashit( constant( 'ABSPATH' ) ) == trailingslashit( $folderPath ) )
  469.         return false;
  470.     if ( @is_dir ( $folderPath ) ) {
  471.         $dh  = @opendir($folderPath);
  472.         while (false !== ($value = @readdir($dh))) {
  473.             if ( $value != "." && $value != ".." ) {
  474.                 $value = $folderPath . "/" . $value; 
  475.                 if ( @is_dir ( $value ) ) {
  476.                     RecursiveFolderDelete ( $value );
  477.                 }
  478.             }
  479.         }
  480.         return @rmdir ( $folderPath );
  481.     } else {
  482.         return FALSE;
  483.     }
  484. }
  485.  
  486. function wp_cache_edit_max_time () {
  487.     global $super_cache_max_time, $cache_max_time, $wp_cache_config_file, $valid_nonce;
  488.  
  489.     if( !isset( $super_cache_max_time ) )
  490.         $super_cache_max_time = 21600;
  491.  
  492.     if(isset($_POST['wp_max_time']) && $valid_nonce) {
  493.         $max_time = (int)$_POST['wp_max_time'];
  494.         if ($max_time > 0) {
  495.             $cache_max_time = $max_time;
  496.             wp_cache_replace_line('^ *\$cache_max_time', "\$cache_max_time = $cache_max_time;", $wp_cache_config_file);
  497.         }
  498.     }
  499.     if(isset($_POST['super_cache_max_time']) && $valid_nonce) {
  500.         $max_time = (int)$_POST['super_cache_max_time'];
  501.         if ($max_time > 0) {
  502.             $super_cache_max_time = $max_time;
  503.             wp_cache_replace_line('^ *\$super_cache_max_time', "\$super_cache_max_time = $super_cache_max_time;", $wp_cache_config_file);
  504.         }
  505.     }
  506.     ?><br /><fieldset style='border: 1px solid #aaa' class="options"> 
  507.     <legend>Expiry Time</legend><?php
  508.     echo '<form name="wp_edit_max_time" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  509.     echo '<label for="wp_max_time">Expire time:</label> ';
  510.     echo "<input type=\"text\" size=6 name=\"wp_max_time\" value=\"$cache_max_time\" /> seconds<br />";
  511.     echo '<label for="super_cache_max_time">Super Cache Expire time:</label> ';
  512.     echo "<input type=\"text\" size=6 name=\"super_cache_max_time\" value=\"$super_cache_max_time\" /> seconds";
  513.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Change expiration »" /></div>';
  514.     wp_nonce_field('wp-cache');
  515.     echo "</form>\n";
  516.     ?></fieldset><?php
  517. }
  518.  
  519. function wp_cache_sanitize_value($text, & $array) {
  520.     $text = wp_specialchars(strip_tags($text));
  521.     $array = preg_split("/[\s,]+/", chop($text));
  522.     $text = var_export($array, true);
  523.     $text = preg_replace('/[\s]+/', ' ', $text);
  524.     return $text;
  525. }
  526.  
  527. function wp_cache_edit_rejected_ua() {
  528.     global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;
  529.  
  530.     if (!function_exists('apache_request_headers')) return;
  531.  
  532.     if(isset($_REQUEST['wp_rejected_user_agent']) && $valid_nonce) {
  533.         $text = wp_cache_sanitize_value($_REQUEST['wp_rejected_user_agent'], $cache_rejected_user_agent);
  534.         wp_cache_replace_line('^ *\$cache_rejected_user_agent', "\$cache_rejected_user_agent = $text;", $wp_cache_config_file);
  535.     }
  536.  
  537.  
  538.     echo '<br /><a name="user-agents"></a><fieldset style="border: 1px solid #aaa" class="options"><legend>Rejected User Agents</legend>';
  539.     echo "<p>Strings in the HTTP 'User Agent' header that prevent WP-Cache from 
  540.         caching bot, spiders, and crawlers' requests.
  541.         Note that cached files are still sent to these request if they already exists.</p>\n";
  542.     echo '<form name="wp_edit_rejected_user_agent" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  543.     echo '<label for="wp_rejected_user_agent">Rejected UA strings</label>';
  544.     echo '<textarea name="wp_rejected_user_agent" cols="40" rows="4" style="width: 70%; font-size: 12px;" class="code">';
  545.     foreach ($cache_rejected_user_agent as $ua) {
  546.         echo wp_specialchars($ua) . "\n";
  547.     }
  548.     echo '</textarea> ';
  549.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Save UA strings »" /></div>';
  550.     wp_nonce_field('wp-cache');
  551.     echo '</form>';
  552.     echo "</fieldset>\n";
  553. }
  554.  
  555.  
  556. function wp_cache_edit_rejected() {
  557.     global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
  558.  
  559.     if(isset($_REQUEST['wp_rejected_uri']) && $valid_nonce) {
  560.         $text = wp_cache_sanitize_value($_REQUEST['wp_rejected_uri'], $cache_rejected_uri);
  561.         wp_cache_replace_line('^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file);
  562.     }
  563.  
  564.  
  565.     echo "<p>Add here strings (not a filename) that forces a page not to be cached. For example, if your URLs include year and you dont want to cache last year posts, it's enough to specify the year, i.e. '/2004/'. WP-Cache will search if that string is part of the URI and if so, it will no cache that page.</p>\n";
  566.     echo '<form name="wp_edit_rejected" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  567.     echo '<label for="wp_rejected_uri">Rejected URIs</label>';
  568.     echo '<textarea name="wp_rejected_uri" cols="40" rows="4" style="width: 70%; font-size: 12px;" class="code">';
  569.     foreach ($cache_rejected_uri as $file) {
  570.         echo wp_specialchars($file) . "\n";
  571.     }
  572.     echo '</textarea> ';
  573.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Save strings »" /></div>';
  574.     wp_nonce_field('wp-cache');
  575.     echo "</form>\n";
  576. }
  577.  
  578. function wp_cache_edit_accepted() {
  579.     global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
  580.  
  581.     if(isset($_REQUEST['wp_accepted_files']) && $valid_nonce) {
  582.         $text = wp_cache_sanitize_value($_REQUEST['wp_accepted_files'], $cache_acceptable_files);
  583.         wp_cache_replace_line('^ *\$cache_acceptable_files', "\$cache_acceptable_files = $text;", $wp_cache_config_file);
  584.     }
  585.  
  586.  
  587.     echo "<p>Add here those filenames that can be cached, even if they match one of the rejected substring specified above.</p>\n";
  588.     echo '<form name="wp_edit_accepted" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  589.     echo '<label for="wp_accepted_files">Accepted files</label>';
  590.     echo '<textarea name="wp_accepted_files" cols="40" rows="8" style="width: 70%; font-size: 12px;" class="code">';
  591.     foreach ($cache_acceptable_files as $file) {
  592.         echo wp_specialchars($file) . "\n";
  593.     }
  594.     echo '</textarea> ';
  595.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Save files »" /></div>';
  596.     wp_nonce_field('wp-cache');
  597.     echo "</form>\n";
  598. }
  599.  
  600. function wp_cache_enable() {
  601.     global $wp_cache_config_file, $cache_enabled, $supercachedir;
  602.  
  603.     if(get_option('gzipcompression')) {
  604.         echo "<b>Error: GZIP compression is enabled, disable it if you want to enable wp-cache.</b><br /><br />";
  605.         return false;
  606.     }
  607.     if( wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = true;', $wp_cache_config_file) ) {
  608.         $cache_enabled = true;
  609.     }
  610.     wp_super_cache_enable();
  611. }
  612.  
  613. function wp_cache_disable() {
  614.     global $wp_cache_config_file, $cache_enabled, $supercachedir, $cache_path;
  615.  
  616.     if (wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = false;', $wp_cache_config_file)) {
  617.         $cache_enabled = false;
  618.     }
  619.     wp_super_cache_disable();
  620. }
  621. function wp_super_cache_enable() {
  622.     global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
  623.  
  624.     if( is_dir( $supercachedir . ".disabled" ) )
  625.         rename( $supercachedir . ".disabled", $supercachedir );
  626.     wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = true;', $wp_cache_config_file);
  627.     $super_cache_enabled = true;
  628. }
  629.  
  630. function wp_super_cache_disable() {
  631.     global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
  632.  
  633.     wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file);
  634.     if( is_dir( $supercachedir ) )
  635.         rename( $supercachedir, $supercachedir . ".disabled" );
  636.     $super_cache_enabled = false;
  637.     sleep( 1 ); // allow existing processes to write to the supercachedir and then delete it
  638.     if (function_exists ('prune_super_cache') && is_dir( $supercachedir ) ) {
  639.         prune_super_cache( $supercachedir, true );
  640.     }
  641. }
  642.  
  643. function wp_cache_is_enabled() {
  644.     global $wp_cache_config_file;
  645.  
  646.     if(get_option('gzipcompression')) {
  647.         echo "<b>Warning</b>: GZIP compression is enabled in Wordpress, wp-cache will be bypassed until you disable gzip compression.<br />";
  648.         return false;
  649.     }
  650.     $lines = file($wp_cache_config_file);
  651.     foreach($lines as $line) {
  652.          if (preg_match('/^ *\$cache_enabled *= *true *;/', $line))
  653.             return true;
  654.     }
  655.     return false;
  656. }
  657.  
  658.  
  659. function wp_cache_replace_line($old, $new, $my_file) {
  660.     if (!is_writable($my_file)) {
  661.         echo "Error: file $my_file is not writable.<br />\n";
  662.         return false;
  663.     }
  664.     $found = false;
  665.     $lines = file($my_file);
  666.     foreach($lines as $line) {
  667.          if ( preg_match("/$old/", $line)) {
  668.             $found = true;
  669.             break;
  670.         }
  671.     }
  672.     if ($found) {
  673.         $fd = fopen($my_file, 'w');
  674.         foreach($lines as $line) {
  675.             if ( !preg_match("/$old/", $line))
  676.                 fputs($fd, $line);
  677.             else {
  678.                 fputs($fd, "$new //Added by WP-Cache Manager\n");
  679.             }
  680.         }
  681.         fclose($fd);
  682.         return true;
  683.     }
  684.     $fd = fopen($my_file, 'w');
  685.     $done = false;
  686.     foreach($lines as $line) {
  687.         if ( $done || !preg_match('/^define|\$|\?>/', $line))
  688.             fputs($fd, $line);
  689.         else {
  690.             fputs($fd, "$new //Added by WP-Cache Manager\n");
  691.             fputs($fd, $line);
  692.             $done = true;
  693.         }
  694.     }
  695.     fclose($fd);
  696.     return true;
  697. /*
  698.     copy($my_file, $my_file . "-prev");
  699.     rename($my_file . '-new', $my_file);
  700. */
  701. }
  702.  
  703. function wp_cache_verify_cache_dir() {
  704.     global $cache_path;
  705.  
  706.     $dir = dirname($cache_path);
  707.     if ( !file_exists($cache_path) ) {
  708.         if ( !is_writable( $dir ) || !($dir = mkdir( $cache_path ) ) ) {
  709.                 echo "<b>Error:</b> Your cache directory (<b>$cache_path</b>) did not exist and couldn't be created by the web server. <br /> Check  $dir permissions.";
  710.                 return false;
  711.         }
  712.     }
  713.     if ( !is_writable($cache_path)) {
  714.         echo "<b>Error:</b> Your cache directory (<b>$cache_path</b>) or <b>$dir</b> need to be writable for this plugin to work. <br /> Double-check it.";
  715.         return false;
  716.     }
  717.  
  718.     if ( '/' != substr($cache_path, -1)) {
  719.         $cache_path .= '/';
  720.     }
  721.  
  722.     @mkdir( $cache_path . 'meta' );
  723.  
  724.     return true;
  725. }
  726.  
  727. function wp_cache_verify_config_file() {
  728.     global $wp_cache_config_file, $wp_cache_config_file_sample;
  729.  
  730.     $new = false;
  731.     $dir = dirname($wp_cache_config_file);
  732.  
  733.     if ( file_exists($wp_cache_config_file) ) {
  734.         $lines = join( ' ', file( $wp_cache_config_file ) );
  735.         if( strpos( $lines, 'WPCACHEHOME' ) === false ) {
  736.             if( is_writable( $wp_cache_config_file ) ) {
  737.                 @unlink( $wp_cache_config_file );
  738.             } else {
  739.                 echo "<b>Error:</b> Your WP-Cache config file (<b>$wp_cache_config_file</b>) is out of date and not writable by the Web server.<br />Please delete it and refresh this page.";
  740.                 return false;
  741.             }
  742.         }
  743.     } elseif( !is_writable($dir)) {
  744.         echo "<b>Error:</b> Configuration file missing and wp-content directory (<b>$dir</b>) is not writable by the Web server.<br />Check its permissions.";
  745.         return false;
  746.     }
  747.  
  748.     if ( !file_exists($wp_cache_config_file) ) {
  749.         if ( !file_exists($wp_cache_config_file_sample) ) {
  750.             echo "<b>Error:</b> Sample WP-Cache config file (<b>$wp_cache_config_file_sample</b>) does not exist.<br />Verify you installation.";
  751.             return false;
  752.         }
  753.         copy($wp_cache_config_file_sample, $wp_cache_config_file);
  754.         if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
  755.             wp_cache_replace_line('WPCACHEHOME', "define( 'WPCACHEHOME', ABSPATH . " . str_replace( '\\', '/', str_replace( ABSPATH, ' "', dirname(__FILE__) ) ) . "/\" );", $wp_cache_config_file);
  756.         } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
  757.             wp_cache_replace_line('WPCACHEHOME', "define( 'WPCACHEHOME', ABSPATH . " . str_replace( '\\', '/', str_replace( ABSPATH, ' "', dirname(__FILE__) ) ) . "/wp-super-cache/\" );", $wp_cache_config_file);
  758.         }
  759.         $new = true;
  760.     }
  761.     require($wp_cache_config_file);
  762.     return true;
  763. }
  764.  
  765. function wp_cache_check_link() {
  766.     global $wp_cache_link, $wp_cache_file;
  767.  
  768.     if( file_exists($wp_cache_link) ) {
  769.         if( strpos( join( "\n", file( $wp_cache_link ) ), 'WPCACHEHOME' ) ) {
  770.             // read the file and verify it's a super-cache file and not the wp-cache one.
  771.             return true;
  772.         } else {
  773.             // remove the old version
  774.             @unlink($wp_cache_link);
  775.         }
  776.     }
  777.  
  778.     $ret = true;
  779.     if ( basename(@readlink($wp_cache_link)) != basename($wp_cache_file)) {
  780.         @unlink($wp_cache_link);
  781.         if( function_exists( 'symlink' ) ) {
  782.             if( !@symlink ($wp_cache_file, $wp_cache_link) ) {
  783.                 $ret = false;
  784.             }
  785.         } elseif( !@copy( $wp_cache_file, $wp_cache_link ) ) {
  786.             $ret = false;
  787.         }
  788.         if( !$ret ) {
  789.             echo "<code>advanced-cache.php</code> does not exist<br />";
  790.             echo "Create it by executing: <br /><code>ln -s $wp_cache_file $wp_cache_link</code><br /> in your server<br />";
  791.             echo "Or by copying $wp_cache_file to $wp_cache_link.<br />";
  792.             return false;
  793.         }
  794.     }
  795.     return true;
  796. }
  797.  
  798. function wp_cache_check_global_config() {
  799.  
  800.     $global = ABSPATH . 'wp-config.php';
  801.  
  802.     $lines = file($global);
  803.     foreach($lines as $line) {
  804.          if (preg_match('/^ *define *\( *\'WP_CACHE\' *, *true *\) *;/', $line)) {
  805.             return true;
  806.         }
  807.     }
  808.     $line = 'define(\'WP_CACHE\', true);';
  809.     if (!is_writable($global) || !wp_cache_replace_line('define *\( *\'WP_CACHE\'', $line, $global) ) {
  810.             echo "<b>Error: WP_CACHE is not enabled</b> in your <code>wp-config.php</code> file and I couldn't modified it.<br />";
  811.             echo "Edit <code>$global</code> and add the following line: <br /><code>define('WP_CACHE', true);</code><br />It <em>must</em> appear before the <code>require_once(ABSPATH.'wp-settings.php');</code> line. Otherwise, <b>WP-Cache will not be executed</b> by Wordpress core. <br />";
  812.             return false;
  813.     } 
  814.     return true;
  815. }
  816.  
  817. function wp_cache_files() {
  818.     global $cache_path, $file_prefix, $cache_max_time, $super_cache_max_time, $valid_nonce, $supercachedir;
  819.  
  820.     if ( '/' != substr($cache_path, -1)) {
  821.         $cache_path .= '/';
  822.     }
  823.  
  824.     if ( $valid_nonce ) {
  825.         if(isset($_REQUEST['wp_delete_cache'])) {
  826.             wp_cache_clean_cache($file_prefix);
  827.         }
  828.         if(isset($_REQUEST['wp_delete_cache_file'])) {
  829.             wp_cache_clean_cache($_REQUEST['wp_delete_cache_file']);
  830.         }
  831.         if(isset($_REQUEST['wp_delete_expired'])) {
  832.             wp_cache_clean_expired($file_prefix);
  833.         }
  834.     }
  835.     if(isset($_REQUEST['wp_list_cache'])) {
  836.         $list_files = true;
  837.         $list_mess = "Update list";
  838.     } else 
  839.         $list_mess = "List files";
  840.  
  841.     echo '<br /><a name="list"></a><fieldset style="border: 1px solid #aaa" class="options"><legend>Cache contents</legend>';
  842.     echo '<form name="wp_cache_content_list" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
  843.     echo '<input type="hidden" name="wp_list_cache" />';
  844.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="'.$list_mess.' »" /></div>';
  845.     echo "</form>\n";
  846.  
  847.     $count = 0;
  848.     $expired = 0;
  849.     $now = time();
  850.     if ( ($handle = @opendir( $cache_path . 'meta/' )) ) { 
  851.         if ($list_files) echo "<table cellspacing=\"0\" cellpadding=\"5\">";
  852.         while ( false !== ($file = readdir($handle))) {
  853.             if ( preg_match("/^$file_prefix.*\.meta/", $file) ) {
  854.                 $this_expired = false;
  855.                 $content_file = preg_replace("/meta$/", "html", $file);
  856.                 $mtime = filemtime($cache_path . 'meta/' . $file);
  857.                 if ( ! ($fsize = @filesize($cache_path.$content_file)) ) 
  858.                     continue; // .meta does not exists
  859.                 $fsize = intval($fsize/1024);
  860.                 $age = $now - $mtime;
  861.                 if ( $age > $cache_max_time) {
  862.                     $expired++;
  863.                     $this_expired = true;
  864.                 }
  865.                 $count++;
  866.                 if ($list_files) {
  867.                     $meta = new CacheMeta;
  868.                     $meta = unserialize(file_get_contents($cache_path . 'meta/' . $file));
  869.                     echo $flip ? '<tr style="background: #EAEAEA;">' : '<tr>';
  870.                     $flip = !$flip;
  871.                     echo '<td><a href="http://' . $meta->uri . '" target="_blank" >';
  872.                     echo $meta->uri . "</a></td>";
  873.                     if ($this_expired) echo "<td><span style='color:red'>$age secs</span></td>";
  874.                     else echo "<td>$age secs</td>";
  875.                     echo "<td>$fsize KB</td>";
  876.                     echo '<td><form name="wp_delete_cache_file" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
  877.                     echo '<input type="hidden" name="wp_list_cache" />';
  878.                     echo '<input type="hidden" name="wp_delete_cache_file" value="'.preg_replace("/^(.*)\.meta$/", "$1", $file).'" />';
  879.                     echo '<div class="submit"><input id="deletepost" ' . SUBMITDISABLED . 'type="submit" value="Remove" /></div>';
  880.                     wp_nonce_field('wp-cache');
  881.                     echo "</form></td></tr>\n";
  882.                 }
  883.             }
  884.         }
  885.         closedir($handle);
  886.         if ($list_files) echo "</table>";
  887.     }
  888.     $sizes = get_option( 'super_cache_meta' );
  889.     if( !$sizes )
  890.         $sizes = array( 'expired' => 0, 'cached' => 0, 'ts' => 0 );
  891.  
  892.     $now = time();
  893.     if( $_POST[ 'super_cache_stats' ] == 1 || $sizes[ 'cached' ] == 0 || $sizes[ 'ts' ] + 3600 <= $now ) {
  894.         $sizes = array( 'expired' => 0, 'cached' => 0, 'ts' => 0 );
  895.  
  896.         if (is_dir($supercachedir)) {
  897.             $entries = glob($supercachedir. '/*');
  898.             foreach ($entries as $entry) {
  899.                 if ($entry != '.' && $entry != '..') {
  900.                     $sizes = wpsc_dirsize( $entry, $sizes );
  901.                 }
  902.             }
  903.         } else {
  904.             if(is_file($supercachedir) && filemtime( $supercachedir ) + $super_cache_max_time <= $now )
  905.                 $sizes[ 'expired' ] ++;
  906.         }
  907.         $sizes[ 'ts' ] = time();
  908.         update_option( 'super_cache_meta', $sizes );
  909.     }
  910.     echo "<p><strong>WP-Cache</strong></p>";
  911.     echo "<ul><li>$count cached pages</li>";
  912.     echo "<li>$expired expired pages</li></ul>";
  913.     echo "<p><strong>WP-Super-Cache</strong></p>";
  914.     echo "<ul><li>" . intval($sizes['cached']/2) . " cached pages</li>";
  915.     $age = intval(($now - $sizes['ts'])/60);
  916.     echo "<li>" . intval($sizes['expired']/2) . " expired pages. (Generated $age minutes ago. Refresh in " . (60 - $age) . " minutes. )</li></ul>";
  917.  
  918.     echo '<form name="wp_cache_content_expired" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
  919.     echo '<input type="hidden" name="wp_delete_expired" />';
  920.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Delete expired »" /></div>';
  921.     wp_nonce_field('wp-cache');
  922.     echo "</form>\n";
  923.  
  924.  
  925.     echo '<form name="wp_cache_content_delete" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
  926.     echo '<input type="hidden" name="wp_delete_cache" />';
  927.     echo '<div class="submit"><input id="deletepost" type="submit" ' . SUBMITDISABLED . 'value="Delete cache »" /></div>';
  928.  
  929.     wp_nonce_field('wp-cache');
  930.     echo "</form>\n";
  931.  
  932.     echo '<form name="wp_super_cache_stats" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
  933.     echo '<input type="hidden" name="super_cache_stats" value="1" />';
  934.     echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Regenerate Super Cache Stats »" /></div>';
  935.     wp_nonce_field('wp-cache');
  936.     echo "</form>\n";
  937.     echo '</fieldset>';
  938. }
  939.  
  940. function wpsc_dirsize($directory, $sizes) {
  941.     global $super_cache_max_time;
  942.     $now = time();
  943.  
  944.     if (is_dir($directory)) {
  945.         $entries = glob($directory. '/*');
  946.         if( is_array( $entries ) && !empty( $entries ) ) foreach ($entries as $entry) {
  947.             if ($entry != '.' && $entry != '..') {
  948.                 $sizes = wpsc_dirsize($entry, $sizes);
  949.             }
  950.         }
  951.     } else {
  952.         if(is_file($directory) ) {
  953.             if( filemtime( $directory ) + $super_cache_max_time <= $now ) {
  954.                 $sizes[ 'expired' ]+=1;
  955.             } else {
  956.                 $sizes[ 'cached' ]+=1;
  957.             }
  958.         }
  959.     }
  960.     return $sizes;
  961. }
  962.  
  963.  
  964. function wp_cache_clean_cache($file_prefix) {
  965.     global $cache_path, $supercachedir;
  966.  
  967.     // If phase2 was compiled, use its function to avoid race-conditions
  968.     if(function_exists('wp_cache_phase2_clean_cache')) {
  969.         if (function_exists ('prune_super_cache')) {
  970.             if( is_dir( $supercachedir ) ) {
  971.                 prune_super_cache( $supercachedir, true );
  972.             } elseif( is_dir( $supercachedir . '.disabled' ) ) {
  973.                 prune_super_cache( $supercachedir . '.disabled', true );
  974.             }
  975.             prune_super_cache( $cache_path, true );
  976.             $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
  977.         }
  978.         return wp_cache_phase2_clean_cache($file_prefix);
  979.     }
  980.  
  981.     $expr = "/^$file_prefix/";
  982.     if ( ($handle = opendir( $cache_path )) ) { 
  983.         while ( false !== ($file = readdir($handle))) {
  984.             if ( preg_match($expr, $file) ) {
  985.                 unlink($cache_path . $file);
  986.                 unlink($cache_path . 'meta/' . str_replace( '.html', '.term', $file ) );
  987.             }
  988.         }
  989.         closedir($handle);
  990.     }
  991. }
  992.  
  993. function wp_cache_clean_expired($file_prefix) {
  994.     global $cache_path, $cache_max_time;
  995.  
  996.     // If phase2 was compiled, use its function to avoid race-conditions
  997.     if(function_exists('wp_cache_phase2_clean_expired')) {
  998.         if (function_exists ('prune_super_cache')) {
  999.             $dir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '',  $_SERVER["HTTP_HOST"]);
  1000.             if( is_dir( $dir ) ) {
  1001.                 prune_super_cache( $dir );
  1002.             } elseif( is_dir( $dir . '.disabled' ) ) {
  1003.                 prune_super_cache( $dir . '.disabled' );
  1004.             }
  1005.             $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
  1006.         }
  1007.         return wp_cache_phase2_clean_expired($file_prefix);
  1008.     }
  1009.  
  1010.     $expr = "/^$file_prefix/";
  1011.     $now = time();
  1012.     if ( ($handle = opendir( $cache_path )) ) { 
  1013.         while ( false !== ($file = readdir($handle))) {
  1014.             if ( preg_match($expr, $file)  &&
  1015.                 (filemtime($cache_path . $file) + $cache_max_time) <= $now) {
  1016.                 unlink($cache_path . $file);
  1017.                 unlink($cache_path . 'meta/' . str_replace( '.html', '.term', $file ) );
  1018.             }
  1019.         }
  1020.         closedir($handle);
  1021.     }
  1022. }
  1023.  
  1024. add_action('admin_menu', 'wp_cache_add_pages');
  1025.  
  1026. function wp_super_cache_footer() {
  1027.     ?><p><?php bloginfo('name'); ?> is Digg proof thanks to caching by <a href="http://ocaoimh.ie/wp-super-cache/">WP Super Cache</a>!</p><?php
  1028. }
  1029. if( isset( $wp_cache_hello_world ) && $wp_cache_hello_world )
  1030.     add_action( 'wp_footer', 'wp_super_cache_footer' );
  1031.  
  1032. if( get_option( 'gzipcompression' ) )
  1033.     update_option( 'gzipcompression', 0 );
  1034.  
  1035. ?>
  1036.