Want more features on Pastebin? Sign Up, it's FREE!
Guest

update to wp-cache.php of WP Super Cache / capabilities

By: jrevillini on Apr 16th, 2012  |  syntax: PHP  |  size: 179.35 KB  |  views: 180  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: WP Super Cache
  4. Plugin URI: http://ocaoimh.ie/wp-super-cache/
  5. Description: Very fast caching plugin for WordPress.
  6. Version: 1.0
  7. Author: Donncha O Caoimh
  8. Author URI: http://ocaoimh.ie/
  9. */
  10.  
  11. /*  Copyright 2005-2006  Ricardo Galli Granada  (email : gallir@uib.es)
  12.     Copyright 2007-2011 Donncha O Caoimh (http://ocaoimh.ie/) and many others.
  13.  
  14.     This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License as published by
  16.     the Free Software Foundation; either version 2 of the License, or
  17.     (at your option) any later version.
  18.  
  19.     This program is distributed in the hope that it will be useful,
  20.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.     GNU General Public License for more details.
  23.  
  24.     You should have received a copy of the GNU General Public License
  25.     along with this program; if not, write to the Free Software
  26.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  27. */
  28.  
  29. // Pre-2.6 compatibility
  30. if( !defined('WP_CONTENT_URL') )
  31.         define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
  32. if( !defined('WP_CONTENT_DIR') )
  33.         define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  34.  
  35. $wp_cache_config_file = WP_CONTENT_DIR . '/wp-cache-config.php';
  36.  
  37. if( !@include($wp_cache_config_file) ) {
  38.         get_wpcachehome();
  39.         $wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
  40.         @include($wp_cache_config_file_sample);
  41. } else {
  42.         get_wpcachehome();
  43. }
  44.  
  45. $wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
  46. $wp_cache_link = WP_CONTENT_DIR . '/advanced-cache.php';
  47. $wp_cache_file = WPCACHEHOME . 'advanced-cache.php';
  48.  
  49. if( !defined( 'WP_CACHE' ) || ( defined( 'WP_CACHE' ) && constant( 'WP_CACHE' ) == false ) ) {
  50.         $wp_cache_check_wp_config = true;
  51. }
  52.  
  53. include(WPCACHEHOME . 'wp-cache-base.php');
  54.  
  55. function wp_super_cache_text_domain() {
  56.         load_plugin_textdomain( 'wp-super-cache', WPCACHEHOME . 'languages', basename( dirname( __FILE__ ) ) . '/languages' );
  57. }
  58. add_action( 'init', 'wp_super_cache_text_domain' );
  59.  
  60. function wp_cache_set_home() {
  61.         global $wp_cache_is_home;
  62.         $wp_cache_is_home = ( is_front_page() || is_home() );
  63.         if ( $wp_cache_is_home && is_paged() )
  64.                 $wp_cache_is_home = false;
  65. }
  66. add_action( 'template_redirect', 'wp_cache_set_home' );
  67.  
  68.  
  69. // OSSDL CDN plugin (http://wordpress.org/extend/plugins/ossdl-cdn-off-linker/)
  70. include_once( WPCACHEHOME . 'ossdl-cdn.php' );
  71.  
  72. // from legolas558 d0t users dot sf dot net at http://www.php.net/is_writable
  73. function is_writeable_ACLSafe($path) {
  74.  
  75.         // PHP's is_writable does not work with Win32 NTFS
  76.                  
  77.         if ($path{strlen($path)-1}=='/') // recursively return a temporary file path
  78.                 return is_writeable_ACLSafe($path.uniqid(mt_rand()).'.tmp');
  79.         else if (is_dir($path))
  80.                 return is_writeable_ACLSafe($path.'/'.uniqid(mt_rand()).'.tmp');
  81.         // check tmp file for read/write capabilities
  82.         $rm = file_exists($path);
  83.         $f = @fopen($path, 'a');
  84.         if ($f===false)
  85.                 return false;
  86.         fclose($f);
  87.         if (!$rm)
  88.                 unlink($path);
  89.         return true;
  90. }
  91.  
  92. function get_wpcachehome() {
  93.         if( defined( 'WPCACHEHOME' ) == false ) {
  94.                 if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
  95.                         define( 'WPCACHEHOME', trailingslashit( dirname(__FILE__) ) );
  96.                 } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
  97.                         define( 'WPCACHEHOME', dirname(__FILE__) . '/wp-super-cache/' );
  98.                 } else {
  99.                         die( sprintf( __( 'Please create %s /wp-cache-config.php from wp-super-cache/wp-cache-config-sample.php', 'wp-super-cache' ), WP_CONTENT_DIR ) );
  100.                 }
  101.         }
  102. }
  103.  
  104. function wpsupercache_add_caps() {
  105.         wpsupercache_mod_caps(true);
  106. }
  107.  
  108.  
  109. function wpsupercache_rem_caps() {
  110.         wpsupercache_mod_caps(false);
  111. }
  112.  
  113.  
  114. function wpsupercache_mod_caps($add=true) {
  115.         global $wp_roles;
  116.         $caps = array(
  117.                 // 'minimum_capability' => 'wp_supercache_capability'
  118.                 // OR
  119.                 // 'minimum_capability' => array('wp_supercache_capability1', 'wp_supercache_capability2', ...)
  120.                 'manage_options' => array('wp_supercache_manage_options', 'wp_supercache_delete_page_cache'),
  121.                 'edit_pages' => 'wp_supercache_delete_page_cache'
  122.         );
  123.        
  124.         /**
  125.          * Add the capability to the role objects
  126.          * Should be in your activation function and done before you inspect with your plugin
  127.          * adopted from http://wordpress.stackexchange.com/a/35199/6775
  128.          *
  129.          * @return void
  130.          */
  131.  
  132.         foreach ($caps as $min_cap => $wpsupercache_custom_caps) {
  133.                 if ( !is_array($wpsupercache_custom_caps) )
  134.                         $wpsupercache_custom_caps = array($wpsupercache_custom_caps);
  135.                
  136.                 foreach ($wpsupercache_custom_caps as $wpsupercache_custom_cap) {
  137.                         foreach ( $wp_roles->role_names as $role_key => $role_name ) {
  138.                                 $role = get_role( $role_key );
  139.                                
  140.                                 if ( $add ) {
  141.                                         if (
  142.                                                 ! $role->has_cap( $wpsupercache_custom_cap )
  143.                                                 AND $role->has_cap( $min_cap )
  144.                                         )
  145.                                                 $role->add_cap( $wpsupercache_custom_cap );
  146.                                 } else {
  147.                                         $role->remove_cap( $wpsupercache_custom_cap );
  148.                                 }
  149.                         }
  150.                 }
  151.         }
  152. }
  153.  
  154. function wpsupercache_deactivate() {
  155.         global $wp_cache_config_file, $wp_cache_link, $cache_path;
  156.         $files = array( $wp_cache_config_file, $wp_cache_link );
  157.         foreach( $files as $file ) {
  158.                 if( file_exists( $file ) )
  159.                         unlink( $file );
  160.         }
  161.         if( !function_exists( 'prune_super_cache' ) )
  162.                 include_once( 'wp-cache-phase2.php' );
  163.         prune_super_cache ($cache_path, true);
  164.         @unlink( $cache_path . '.htaccess' );
  165.         @unlink( $cache_path . 'meta' );
  166.         @unlink( $cache_path . 'supercache' );
  167.         wp_clear_scheduled_hook( 'wp_cache_check_site_hook' );
  168.         wpsupercache_rem_caps();
  169.         wp_cache_disable_plugin();
  170. }
  171. register_deactivation_hook( __FILE__, 'wpsupercache_deactivate' );
  172.  
  173. function wpsupercache_activate() {
  174.         wpsupercache_add_caps();
  175. }
  176. register_activation_hook( __FILE__, 'wpsupercache_activate' );
  177.  
  178. function wpsupercache_site_admin() {
  179.         if ( function_exists( 'is_super_admin' ) ) {
  180.                 return is_super_admin();
  181.         } elseif ( function_exists( 'is_site_admin' ) ) {
  182.                 return is_site_admin();
  183.         } else {
  184.                 return true;
  185.         }
  186. }
  187.  
  188. function wp_cache_add_pages() {
  189.         global $wpmu_version;
  190.         $min_cap = 'wp_supercache_manage_options';
  191.         if ( function_exists( 'is_multisite' ) && is_multisite() && current_user_can($min_cap) ) {
  192.                 add_submenu_page( 'ms-admin.php', 'WP Super Cache', 'WP Super Cache', $min_cap, 'wpsupercache', 'wp_cache_manager' );
  193.         } elseif ( isset( $wpmu_version ) && current_user_can($min_cap) ) {
  194.                 add_submenu_page( 'wpmu-admin.php', 'WP Super Cache', 'WP Super Cache', $min_cap, 'wpsupercache', 'wp_cache_manager' );
  195.         }
  196.  
  197.         if ( current_user_can($min_cap) ) { // in single or MS mode add this menu item too, but only for superadmins in MS mode.
  198.                 add_options_page( 'WP Super Cache', 'WP Super Cache', $min_cap, 'wpsupercache', 'wp_cache_manager');
  199.         }
  200. }
  201. add_action('admin_menu', 'wp_cache_add_pages');
  202.  
  203. function wp_cache_network_pages() {
  204.         add_submenu_page('settings.php', 'WP Super Cache', 'WP Super Cache', 'manage_options', 'wpsupercache', 'wp_cache_manager');
  205. }
  206. add_action( 'network_admin_menu', 'wp_cache_network_pages' );
  207.  
  208. function wp_cache_manager_error_checks() {
  209.         global $wpmu_version, $wp_cache_debug, $wp_cache_cron_check, $cache_enabled, $super_cache_enabled, $wp_cache_config_file, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes, $wp_cache_mobile_browsers, $wp_cache_mobile_enabled, $wp_cache_mod_rewrite, $cache_path;
  210.  
  211.         if ( !current_user_can('wp_supercache_manage_options') && !current_user_can('wp_supercache_delete_page_cache') )
  212.                 return false;
  213.        
  214.         if ( version_compare( PHP_VERSION, '5.3.0', '<' ) && ( 1 == ini_get( 'safe_mode' ) || "on" == strtolower( ini_get( 'safe_mode' ) ) ) ) {
  215.                 echo '<div id="message" class="updated fade"><h3>' . __( 'Warning! PHP Safe Mode Enabled!', 'wp-super-cache' ) . '</h3><p>' .
  216.                         __( 'You may experience problems running this plugin because SAFE MODE is enabled.', 'wp-super-cache' ) . ' ';
  217.                
  218.                
  219.                 if( !ini_get( 'safe_mode_gid' ) ) {
  220.                         _e( 'Your server is set up to check the owner of PHP scripts before allowing them to read and write files.', 'wp-super-cache' ) . " ";
  221.                         printf( __( '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 %s/cache/ directory must also be changed. See the <a href="http://php.net/features.safe-mode">safe mode manual page</a> for further details.', 'wp-super-cache' ), WP_CONTENT_DIR );
  222.                 } else {
  223.                         _e( '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.', 'wp-super-cache' );
  224.                 }
  225.                 echo '</p></div>';
  226.         }
  227.  
  228.         if ( '' == get_option( 'permalink_structure' ) ) {
  229.                 echo '<div id="message" class="updated fade"><h3>' . __( 'Permlink Structure Error', 'wp-super-cache' ) . '</h3>';
  230.                 echo "<p>" . __( 'A custom url or permalink structure is required for this plugin to work correctly. Please go to the <a href="options-permalink.php">Permalinks Options Page</a> to configure your permalinks.' ) . "</p>";
  231.                 echo '</div>';
  232.                 return false;
  233.         }
  234.  
  235.         if( $wp_cache_debug || !$wp_cache_cron_check ) {
  236.                 if( function_exists( "wp_remote_get" ) == false ) {
  237.                         $hostname = str_replace( 'http://', '', str_replace( 'https://', '', get_option( 'siteurl' ) ) );
  238.                         if( strpos( $hostname, '/' ) )
  239.                                 $hostname = substr( $hostname, 0, strpos( $hostname, '/' ) );
  240.                         $ip = gethostbyname( $hostname );
  241.                         if( substr( $ip, 0, 3 ) == '127' || substr( $ip, 0, 7 ) == '192.168' ) {
  242.                                 ?><div id="message" class="updated fade"><h3><?php printf( __( 'Warning! Your hostname "%s" resolves to %s', 'wp-super-cache' ), $hostname, $ip ); ?></h3>
  243.                                         <p><?php printf( __( 'Your server thinks your hostname resolves to %s. Some services such as garbage collection by this plugin, and WordPress scheduled posts may not operate correctly.', 'wp-super-cache' ), $ip ); ?></p>
  244.                                         <p><?php printf( __( 'Please see entry 16 in the <a href="%s">Troubleshooting section</a> of the readme.txt', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wp-super-cache/faq/' ); ?></p>
  245.                                         </div>
  246.                                         <?php
  247.                                         return false;
  248.                         } else {
  249.                                 wp_cache_replace_line('^ *\$wp_cache_cron_check', "\$wp_cache_cron_check = 1;", $wp_cache_config_file);
  250.                         }
  251.                 } else {
  252.                         $cron_url = get_option( 'siteurl' ) . '/wp-cron.php?check=' . wp_hash('187425');
  253.                         $cron = wp_remote_get($cron_url, array('timeout' => 0.01, 'blocking' => true));
  254.                         if( is_array( $cron ) ) {
  255.                                 if( $cron[ 'response' ][ 'code' ] == '404' ) {
  256.                                         ?><div id="message" class="updated fade"><h3>Warning! wp-cron.php not found!</h3>
  257.                                         <p><?php _e( 'Unfortunately WordPress cannot find the file wp-cron.php. This script is required for the the correct operation of garbage collection by this plugin, WordPress scheduled posts as well as other critical activities.', 'wp-super-cache' ); ?></p>
  258.                                         <p><?php printf( __( 'Please see entry 16 in the <a href="%s">Troubleshooting section</a> of the readme.txt', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wp-super-cache/faq/' ); ?></p>
  259.                                         </div>
  260.                                         <?php
  261.                                         return true;
  262.                                 } else {
  263.                                         wp_cache_replace_line('^ *\$wp_cache_cron_check', "\$wp_cache_cron_check = 1;", $wp_cache_config_file);
  264.                                 }
  265.                         }
  266.                 }
  267.         }
  268.  
  269.         if ( !wp_cache_check_link() ||
  270.                 !wp_cache_verify_config_file() ||
  271.                 !wp_cache_verify_cache_dir() ) {
  272.                 echo '<p>' . __( "Cannot continue... fix previous problems and retry.", 'wp-super-cache' ) . '</p>';
  273.                 return false;
  274.         }
  275.  
  276.         if (!wp_cache_check_global_config()) {
  277.                 return false;
  278.         }
  279.  
  280.         if ( 1 == ini_get( 'zlib.output_compression' ) || "on" == strtolower( ini_get( 'zlib.output_compression' ) ) ) {
  281.                 ?><div id="message" class="updated fade"><h3><?php _e( 'Zlib Output Compression Enabled!', 'wp-super-cache' ); ?></h3>
  282.                 <p><?php _e( 'PHP is compressing the data sent to the visitors of your site. Disabling this is recommended as the plugin caches the compressed output once instead of compressing the same page over and over again. Also see #21 in the Troubleshooting section. See <a href="http://php.net/manual/en/zlib.configuration.php">this page</a> for instructions on modifying your php.ini.', 'wp-super-cache' ); ?></p></div><?php
  283.         }
  284.  
  285.         if( $cache_enabled == true && $super_cache_enabled == true && $wp_cache_mod_rewrite && !got_mod_rewrite() ) {
  286.                 ?><div id="message" class="updated fade"><h3><?php _e( 'Mod rewrite may not be installed!', 'wp-super-cache' ); ?></h3>
  287.                 <p><?php _e( 'It appears that mod_rewrite is not installed. Sometimes this check isn&#8217;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 legacy or PHP modes.', 'wp-super-cache' ); ?></p></div><?php
  288.         }
  289.  
  290.         if( !is_writeable_ACLSafe( $wp_cache_config_file ) ) {
  291.                 if ( !defined( 'SUBMITDISABLED' ) )
  292.                         define( "SUBMITDISABLED", 'disabled style="color: #aaa" ' );
  293.                 ?><div id="message" class="updated fade"><h3><?php _e( 'Read Only Mode. Configuration cannot be changed.', 'wp-super-cache' ); ?></h3>
  294.                 <p><?php printf( __( 'The WP Super Cache configuration file is <code>%s/wp-cache-config.php</code> and cannot be modified. That file must be writeable by the webserver to make any changes.', 'wp-super-cache' ), WP_CONTENT_DIR ); ?>
  295.                 <?php _e( 'A simple way of doing that is by changing the permissions temporarily using the CHMOD command or through your ftp client. Make sure it&#8217;s globally writeable and it should be fine.', 'wp-super-cache' ); ?></p>
  296.                 <p><?php _e( '<a href="http://codex.wordpress.org/Changing_File_Permissions">This page</a> explains how to change file permissions.', 'wp-super-cache' ); ?></p>
  297.                 <?php _e( 'Writeable:', 'wp-super-cache' ); ?> <code>chmod 666 <?php echo WP_CONTENT_DIR; ?>/wp-cache-config.php</code>
  298.                 <?php _e( 'Readonly:', 'wp-super-cache' ); ?> <code>chmod 644 <?php echo WP_CONTENT_DIR; ?>/wp-cache-config.php</code></p>
  299.                 </div><?php
  300.         } elseif ( !defined( 'SUBMITDISABLED' ) ) {
  301.                 define( "SUBMITDISABLED", ' ' );
  302.         }
  303.  
  304.         // Server could be running as the owner of the wp-content directory.  Therefore, if it's
  305.         // writable, issue a warning only if the permissions aren't 755.
  306.         if( is_writeable_ACLSafe( WP_CONTENT_DIR . '/' ) ) {
  307.                 $wp_content_stat = stat(WP_CONTENT_DIR . '/');
  308.                 $wp_content_mode = decoct( $wp_content_stat[ 'mode' ] & 0777 );
  309.                 if( substr( $wp_content_mode, -2 ) == '77' ) {
  310.                         ?><div id="message" class="updated fade"><h3><?php printf( __( 'Warning! %s is writeable!', 'wp-super-cache' ), WP_CONTENT_DIR ); ?></h3>
  311.                         <p><?php printf( __( 'You should change the permissions on %s and make it more restrictive. Use your ftp client, or the following command to fix things:', 'wp-super-cache' ), WP_CONTENT_DIR ); ?> <code>chmod 755 <?php echo WP_CONTENT_DIR; ?>/</code></p>
  312.                         <p><?php _e( '<a href="http://codex.wordpress.org/Changing_File_Permissions">This page</a> explains how to change file permissions.', 'wp-super-cache' ); ?></p></div>
  313.                         <?php
  314.                 }
  315.         }
  316.  
  317.         if ( function_exists( "is_main_site" ) && true == is_main_site() || function_exists( 'is_main_blog' ) && true == is_main_blog() ) {
  318.         $home_path = trailingslashit( get_home_path() );
  319.         $scrules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WPSuperCache' ) );
  320.         if ( $cache_enabled && $wp_cache_mod_rewrite && !$wp_cache_mobile_enabled && strpos( $scrules, addcslashes( implode( '|', $wp_cache_mobile_browsers ), ' ' ) ) ) {
  321.                 echo '<div id="message" class="updated fade"><h3>' . __( 'Mobile rewrite rules detected', 'wp-super-cache' ) . "</h3>";
  322.                 echo "<p>" . __( 'For best performance you should enable "Mobile device support" or delete the mobile rewrite rules in your .htaccess. Look for the 2 lines with the text "2.0\ MMP|240x320" and delete those.', 'wp-super-cache' ) . "</p><p>" . __( 'This will have no affect on ordinary users but mobile users will see uncached pages.', 'wp-super-cache' ) . "</p></div>";
  323.         } elseif ( $wp_cache_mod_rewrite && $cache_enabled && $wp_cache_mobile_enabled && $scrules != '' && (
  324.                 ( false == empty( $wp_cache_mobile_prefixes ) && false === strpos( $scrules, addcslashes( implode( '|', $wp_cache_mobile_prefixes ), ' ' ) ) ) ||
  325.                 ( false == empty( $wp_cache_mobile_browsers ) && false === strpos( $scrules, addcslashes( implode( '|', $wp_cache_mobile_browsers ), ' ' ) ) ) )
  326.                 ) {
  327.                 ?>
  328.                         <div id="message" class="updated fade"><h3><?php _e( 'Rewrite rules must be updated', 'wp-super-cache' ); ?></h3>
  329.                         <p><?php _e( 'The rewrite rules required by this plugin have changed or are missing. ', 'wp-super-cache' ); ?>
  330.                         <?php _e( 'Mobile support requires extra rules in your .htaccess file, or you can set the plugin to legacy mode. Here are your options (in order of difficulty):', 'wp-super-cache' ); ?>
  331.                         <ol><li> <?php _e( 'Set the plugin to legacy mode and enable mobile support.', 'wp-super-cache' ); ?></li>
  332.                         <li> <?php _e( 'Scroll down the Advanced Settings page and click the <strong>Update Mod_Rewrite Rules</strong> button.', 'wp-super-cache' ); ?></li>
  333.                         <li> <?php printf( __( 'Delete the plugin mod_rewrite rules in %s.htaccess enclosed by <code># BEGIN WPSuperCache</code> and <code># END WPSuperCache</code> and let the plugin regenerate them by reloading this page.', 'wp-super-cache' ), $home_path ); ?></li>
  334.                         <li> <?php printf( __( 'Add the rules yourself. Edit %s.htaccess and find the block of code enclosed by the lines <code># BEGIN WPSuperCache</code> and <code># END WPSuperCache</code>. There are two sections that look very similar. Just below the line <code>%%{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$</code> add these lines: (do it twice, once for each section)', 'wp-super-cache' ), $home_path ); ?></p>
  335.                         <div style='padding: 2px; margin: 2px; border: 1px solid #333; width:400px; overflow: scroll'><pre><?php echo "RewriteCond %{HTTP_user_agent} !^.*(" . addcslashes( implode( '|', $wp_cache_mobile_browsers ), ' ' ) . ").*\nRewriteCond %{HTTP_user_agent} !^(" . addcslashes( implode( '|', $wp_cache_mobile_prefixes ), ' ' ) . ").*"; ?></pre></div></li></ol></div><?php
  336.         }
  337.  
  338.         if ( $cache_enabled && $super_cache_enabled && $wp_cache_mod_rewrite && $scrules == '' ) {
  339.                 ?><div id="message" class="updated fade"><h3><?php _e( 'Rewrite rules must be updated', 'wp-super-cache' ); ?></h3>
  340.                 <p><?php _e( 'The rewrite rules required by this plugin have changed or are missing. ', 'wp-super-cache' ); ?>
  341.                 <?php _e( 'Scroll down the Advanced Settings page and click the <strong>Update Mod_Rewrite Rules</strong> button.', 'wp-super-cache' ); ?></p></div><?php
  342.         }
  343.         }
  344.  
  345.         if ( $wp_cache_mod_rewrite && $super_cache_enabled && function_exists( 'apache_get_modules' ) ) {
  346.                 $mods = apache_get_modules();
  347.                 $required_modules = array( 'mod_mime' => __( 'Required to serve compressed supercache files properly.', 'wp-super-cache' ), 'mod_headers' => __( 'Required to set caching information on supercache pages. IE7 users will see old pages without this module.', 'wp-super-cache' ), 'mod_expires' => __( 'Set the expiry date on supercached pages. Visitors may not see new pages when they refresh or leave comments without this module.', 'wp-super-cache' ) );
  348.                 foreach( $required_modules as $req => $desc ) {
  349.                         if( !in_array( $req, $mods ) ) {
  350.                                 $missing_mods[ $req ] = $desc;
  351.                         }
  352.                 }
  353.                 if( isset( $missing_mods) && is_array( $missing_mods ) ) {
  354.                         ?><div id="message" class="updated fade"><h3><?php _e( 'Missing Apache Modules', 'wp-super-cache' ); ?></h3>
  355.                         <p><?php __( 'The following Apache modules are missing. The plugin will work in legacy mode without them. In full Supercache mode, your visitors may see corrupted pages or out of date content however.', 'wp-super-cache' ); ?></p><?php
  356.                         echo "<ul>";
  357.                         foreach( $missing_mods as $req => $desc ) {
  358.                                 echo "<li> $req - $desc</li>";
  359.                         }
  360.                         echo "</ul>";
  361.                         echo "</div>";
  362.                 }
  363.         }
  364.  
  365.         return true;
  366.  
  367. }
  368. add_filter( 'wp_super_cache_error_checking', 'wp_cache_manager_error_checks' );
  369.  
  370. function wp_cache_manager_updates() {
  371.         global $wp_cache_mobile_enabled, $wp_supercache_cache_list, $wp_cache_config_file, $wp_cache_hello_world, $wp_cache_clear_on_post_edit, $cache_rebuild_files, $wp_cache_mutex_disabled, $wp_cache_not_logged_in, $wp_cache_make_known_anon, $cache_path, $wp_cache_object_cache, $_wp_using_ext_object_cache, $wp_cache_refresh_single_only, $cache_compression, $wp_cache_mod_rewrite, $wp_supercache_304, $wp_super_cache_late_init, $wp_cache_front_page_checks, $cache_page_secret;
  372.  
  373.         if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'wpsupercache-delete-page-cache' && ( isset( $_GET[ '_wpnonce' ] ) ? wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'delete-cache' ) : false ) ) {
  374.                 if ( !current_user_can('wp_supercache_delete_page_cache') ) {
  375.                         return false;
  376.                 }
  377.         } else {
  378.                 if ( !current_user_can('wp_supercache_manage_options') ) {
  379.                         return false;
  380.                 }
  381.         }
  382.                        
  383.         if ( false == isset( $cache_page_secret ) ) {
  384.                 $cache_page_secret = md5( date( 'H:i:s' ) . mt_rand() );
  385.                 wp_cache_replace_line('^ *\$cache_page_secret', "\$cache_page_secret = '" . $cache_page_secret . "';", $wp_cache_config_file);
  386.         }
  387.  
  388.         // Delete cache for a specific page
  389.         if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'wpsupercache-delete-page-cache' && ( isset( $_GET[ '_wpnonce' ] ) ? wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'delete-cache' ) : false ) ) {
  390.                 $path = get_supercache_dir() . preg_replace( '/:.*$/', '', $_GET[ 'path' ] );
  391.                 $files = get_all_supercache_filenames( $path );
  392.                 foreach( $files as $cache_file )
  393.                         prune_super_cache( $path . $cache_file, true );
  394.  
  395.                 wp_redirect( preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', $_GET[ 'path' ] . '&page-cache-deleted=1') );
  396.                 die();
  397.         }
  398.  
  399.         $valid_nonce = isset($_REQUEST['_wpnonce']) ? wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache') : false;
  400.         if ( $valid_nonce == false )
  401.                 return false;
  402.  
  403.         if ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'easysetup' ) {
  404.                 $_POST[ 'action' ] = 'scupdates';
  405.                 if( isset( $_POST[ 'wp_cache_easy_on' ] ) && $_POST[ 'wp_cache_easy_on' ] == 1 ) {
  406.                         $_POST[ 'wp_cache_mobile_enabled' ] = 1;
  407.                         $_POST[ 'wp_cache_status' ] = 'all';
  408.                         $_POST[ 'super_cache_enabled' ] = 2; // PHP
  409.                         $_POST[ 'cache_rebuild_files' ] = 1;
  410.                         unset( $_POST[ 'cache_compression' ] );
  411.                 } else {
  412.                         unset( $_POST[ 'wp_cache_status' ] );
  413.                         $_POST[ 'super_cache_enabled' ] = 0;
  414.                 }
  415.         }
  416.  
  417.         if( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'scupdates' ) {
  418.  
  419.                 if( isset( $_POST[ 'wp_super_cache_late_init' ] ) ) {
  420.                         $wp_super_cache_late_init = 1;
  421.                 } else {
  422.                         $wp_super_cache_late_init = 0;
  423.                 }
  424.                 wp_cache_replace_line('^ *\$wp_super_cache_late_init', "\$wp_super_cache_late_init = " . $wp_super_cache_late_init . ";", $wp_cache_config_file);
  425.  
  426.                 if( isset( $_POST[ 'wp_cache_disable_utf8' ] ) ) {
  427.                         $wp_cache_disable_utf8 = 1;
  428.                 } else {
  429.                         $wp_cache_disable_utf8 = 0;
  430.                 }
  431.                 wp_cache_replace_line('^ *\$wp_cache_disable_utf8', "\$wp_cache_disable_utf8 = " . $wp_cache_disable_utf8 . ";", $wp_cache_config_file);
  432.  
  433.                 if( isset( $_POST[ 'wp_cache_no_cache_for_get' ] ) ) {
  434.                         $wp_cache_no_cache_for_get = 1;
  435.                 } else {
  436.                         $wp_cache_no_cache_for_get = 0;
  437.                 }
  438.                 wp_cache_replace_line('^ *\$wp_cache_no_cache_for_get', "\$wp_cache_no_cache_for_get = " . $wp_cache_no_cache_for_get . ";", $wp_cache_config_file);
  439.  
  440.                 if( isset( $_POST[ 'wp_supercache_304' ] ) ) {
  441.                         $wp_supercache_304 = 1;
  442.                 } else {
  443.                         $wp_supercache_304 = 0;
  444.                 }
  445.                 wp_cache_replace_line('^ *\$wp_supercache_304', "\$wp_supercache_304 = " . $wp_supercache_304 . ";", $wp_cache_config_file);
  446.  
  447.                 if( isset( $_POST[ 'wp_cache_mobile_enabled' ] ) ) {
  448.                         $wp_cache_mobile_enabled = 1;
  449.                 } else {
  450.                         $wp_cache_mobile_enabled = 0;
  451.                 }
  452.                 wp_cache_replace_line('^ *\$wp_cache_mobile_enabled', "\$wp_cache_mobile_enabled = " . $wp_cache_mobile_enabled . ";", $wp_cache_config_file);
  453.  
  454.                 if( isset( $_POST[ 'wp_cache_front_page_checks' ] ) ) {
  455.                         $wp_cache_front_page_checks = 1;
  456.                 } else {
  457.                         $wp_cache_front_page_checks = 0;
  458.                 }
  459.                 wp_cache_replace_line('^ *\$wp_cache_front_page_checks', "\$wp_cache_front_page_checks = " . $wp_cache_front_page_checks . ";", $wp_cache_config_file);
  460.  
  461.                 $wp_supercache_cache_list = $_POST[ 'wp_supercache_cache_list' ] == 1 ? 1 : 0;
  462.                 wp_cache_replace_line('^ *\$wp_supercache_cache_list', "\$wp_supercache_cache_list = " . $wp_supercache_cache_list . ";", $wp_cache_config_file);
  463.  
  464.                 if ( isset( $_POST[ 'wp_cache_status' ] ) ) {
  465.                         if ( $_POST[ 'wp_cache_status' ] == 'all' )
  466.                                         wp_cache_enable();
  467.  
  468.                         if ( isset( $_POST[ 'super_cache_enabled' ] ) ) {
  469.                                 if ( $_POST[ 'super_cache_enabled' ] == 0 ) {
  470.                                         wp_cache_enable();
  471.                                         wp_super_cache_disable();
  472.                                 }
  473.                                 if( $_POST[ 'super_cache_enabled' ] == 1 ) {
  474.                                         $wp_cache_mod_rewrite = 1; // we need this because supercached files can be served by PHP too.
  475.                                 } else {
  476.                                         $wp_cache_mod_rewrite = 0;
  477.                                 }
  478.                                 wp_cache_replace_line('^ *\$wp_cache_mod_rewrite', '$wp_cache_mod_rewrite = ' . $wp_cache_mod_rewrite . ";", $wp_cache_config_file);
  479.                         }
  480.                 } else {
  481.                         wp_cache_disable();
  482.                 }
  483.  
  484.                 if( isset( $_POST[ 'wp_cache_hello_world' ] ) ) {
  485.                         $wp_cache_hello_world = 1;
  486.                 } else {
  487.                         $wp_cache_hello_world = 0;
  488.                 }
  489.                 wp_cache_replace_line('^ *\$wp_cache_hello_world', '$wp_cache_hello_world = ' . $wp_cache_hello_world . ";", $wp_cache_config_file);
  490.  
  491.                 if( isset( $_POST[ 'wp_cache_clear_on_post_edit' ] ) ) {
  492.                         $wp_cache_clear_on_post_edit = 1;
  493.                 } else {
  494.                         $wp_cache_clear_on_post_edit = 0;
  495.                 }
  496.                 wp_cache_replace_line('^ *\$wp_cache_clear_on_post_edit', "\$wp_cache_clear_on_post_edit = " . $wp_cache_clear_on_post_edit . ";", $wp_cache_config_file);
  497.  
  498.                 if( isset( $_POST[ 'cache_rebuild_files' ] ) ) {
  499.                         $cache_rebuild_files = 1;
  500.                 } else {
  501.                         $cache_rebuild_files = 0;
  502.                 }
  503.                 wp_cache_replace_line('^ *\$cache_rebuild_files', "\$cache_rebuild_files = " . $cache_rebuild_files . ";", $wp_cache_config_file);
  504.  
  505.                 if( isset( $_POST[ 'wp_cache_mutex_disabled' ] ) ) {
  506.                         $wp_cache_mutex_disabled = 0;
  507.                 } else {
  508.                         $wp_cache_mutex_disabled = 1;
  509.                 }
  510.                 if( defined( 'WPSC_DISABLE_LOCKING' ) ) {
  511.                         $wp_cache_mutex_disabled = 1;
  512.                 }
  513.                 wp_cache_replace_line('^ *\$wp_cache_mutex_disabled', "\$wp_cache_mutex_disabled = " . $wp_cache_mutex_disabled . ";", $wp_cache_config_file);
  514.  
  515.                 if( isset( $_POST[ 'wp_cache_not_logged_in' ] ) ) {
  516.                         if( $wp_cache_not_logged_in == 0 && function_exists( 'prune_super_cache' ) )
  517.                                 prune_super_cache ($cache_path, true);
  518.                         $wp_cache_not_logged_in = 1;
  519.                 } else {
  520.                         $wp_cache_not_logged_in = 0;
  521.                 }
  522.                 wp_cache_replace_line('^ *\$wp_cache_not_logged_in', "\$wp_cache_not_logged_in = " . $wp_cache_not_logged_in . ";", $wp_cache_config_file);
  523.  
  524.                 if( isset( $_POST[ 'wp_cache_make_known_anon' ] ) ) {
  525.                         if( $wp_cache_make_known_anon == 0 && function_exists( 'prune_super_cache' ) )
  526.                                 prune_super_cache ($cache_path, true);
  527.                         $wp_cache_make_known_anon = 1;
  528.                 } else {
  529.                         $wp_cache_make_known_anon = 0;
  530.                 }
  531.                 wp_cache_replace_line('^ *\$wp_cache_make_known_anon', "\$wp_cache_make_known_anon = " . $wp_cache_make_known_anon . ";", $wp_cache_config_file);
  532.  
  533.                 if( $_wp_using_ext_object_cache && isset( $_POST[ 'wp_cache_object_cache' ] ) ) {
  534.                         if( $wp_cache_object_cache == 0 && function_exists( 'prune_super_cache' ) )
  535.                                 prune_super_cache( $cache_path, true );
  536.                         $wp_cache_object_cache = 1;
  537.                 } else {
  538.                         $wp_cache_object_cache = 0;
  539.                 }
  540.                 wp_cache_replace_line('^ *\$wp_cache_object_cache', "\$wp_cache_object_cache = " . $wp_cache_object_cache . ";", $wp_cache_config_file);
  541.  
  542.                 if( isset( $_POST[ 'wp_cache_refresh_single_only' ] ) ) {
  543.                         $wp_cache_refresh_single_only = 1;
  544.                 } else {
  545.                         $wp_cache_refresh_single_only = 0;
  546.                 }
  547.                 wp_cache_replace_line('^ *\$wp_cache_refresh_single_only', "\$wp_cache_refresh_single_only = '" . $wp_cache_refresh_single_only . "';", $wp_cache_config_file);
  548.        
  549.                 if ( defined( 'WPSC_DISABLE_COMPRESSION' ) ) {
  550.                         $cache_compression = 0;
  551.                         wp_cache_replace_line('^ *\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
  552.                 } else {
  553.                         if ( isset( $_POST[ 'cache_compression' ] ) ) {
  554.                                 $new_cache_compression = 1;
  555.                         } else {
  556.                                 $new_cache_compression = 0;
  557.                         }
  558.                         if ( 1 == ini_get( 'zlib.output_compression' ) || "on" == strtolower( ini_get( 'zlib.output_compression' ) ) ) {
  559.                                 echo '<div id="message" class="updated fade">' . __( "<strong>Warning!</strong> You attempted to enable compression but <code>zlib.output_compression</code> is enabled. See #21 in the Troubleshooting section of the readme file.", 'wp-super-cache' ) . '</div>';
  560.                         } else {
  561.                                 if ( $new_cache_compression != $cache_compression ) {
  562.                                         $cache_compression = $new_cache_compression;
  563.                                         wp_cache_replace_line('^ *\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
  564.                                         if ( function_exists( 'prune_super_cache' ) )
  565.                                                 prune_super_cache( $cache_path, true );
  566.                                         delete_option( 'super_cache_meta' );
  567.                                 }
  568.                         }
  569.                 }
  570.         }
  571. }
  572. if ( ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == 'wpsupercache' ) || ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'wpsupercache-delete-page-cache' ) ) {
  573.         add_action( 'admin_init', 'wp_cache_manager_updates' );
  574. }
  575.  
  576. function wpsupercache_page_cache_deleted_notice () {
  577.     echo '<div class="updated">
  578.       <p>Page cache deleted.</p>
  579.    </div>';
  580. }
  581. if ( isset($_GET['page-cache-deleted']) && $_GET['page-cache-deleted'] == '1' ) {
  582.         add_action('admin_notices', 'wpsupercache_page_cache_deleted_notice');
  583. }
  584.  
  585. function wp_cache_manager() {
  586.         global $wp_cache_config_file, $valid_nonce, $supercachedir, $cache_path, $cache_enabled, $cache_compression, $super_cache_enabled, $wp_cache_hello_world;
  587.         global $wp_cache_clear_on_post_edit, $cache_rebuild_files, $wp_cache_mutex_disabled, $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_no_cache_for_get;
  588.         global $wp_cache_cron_check, $wp_cache_debug, $wp_cache_not_logged_in, $wp_cache_make_known_anon, $wp_supercache_cache_list, $cache_page_secret, $cache_home_path;
  589.         global $wp_super_cache_front_page_check, $wp_cache_object_cache, $_wp_using_ext_object_cache, $wp_cache_refresh_single_only, $wp_cache_mobile_prefixes;
  590.         global $wpmu_version, $cache_max_time, $wp_cache_mod_rewrite, $wp_supercache_304, $wp_super_cache_late_init, $wp_cache_front_page_checks, $wp_cache_disable_utf8;
  591.  
  592.         if ( !current_user_can('wp_supercache_delete_page_cache') )
  593.                 return false;
  594.  
  595.         // used by mod_rewrite rules and config file
  596.         if ( function_exists( "cfmobi_default_browsers" ) ) {
  597.                 $wp_cache_mobile_browsers = cfmobi_default_browsers( "mobile" );
  598.                 $wp_cache_mobile_browsers = array_merge( $wp_cache_mobile_browsers, cfmobi_default_browsers( "touch" ) );
  599.         } elseif ( function_exists( 'lite_detection_ua_contains' ) ) {
  600.                 $wp_cache_mobile_browsers = explode( '|', lite_detection_ua_contains() );
  601.         } else {
  602.                 $wp_cache_mobile_browsers = array( '2.0 MMP', '240x320', '400X240', 'AvantGo', 'BlackBerry', 'Blazer', 'Cellphone', 'Danger', 'DoCoMo', 'Elaine/3.0', 'EudoraWeb', 'Googlebot-Mobile', 'hiptop', 'IEMobile', 'KYOCERA/WX310K', 'LG/U990', 'MIDP-2.', 'MMEF20', 'MOT-V', 'NetFront', 'Newt', 'Nintendo Wii', 'Nitro', 'Nokia', 'Opera Mini', 'Palm', 'PlayStation Portable', 'portalmmm', 'Proxinet', 'ProxiNet', 'SHARP-TQ-GX10', 'SHG-i900', 'Small', 'SonyEricsson', 'Symbian OS', 'SymbianOS', 'TS21i-10', 'UP.Browser', 'UP.Link', 'webOS', 'Windows CE', 'WinWAP', 'YahooSeeker/M1A1-R2D2', 'iPhone', 'iPod', 'Android', 'BlackBerry9530', 'LG-TU915 Obigo', 'LGE VX', 'webOS', 'Nokia5800' );
  603.         }
  604.         if ( function_exists( "lite_detection_ua_prefixes" ) ) {
  605.                 $wp_cache_mobile_prefixes = lite_detection_ua_prefixes();
  606.         } else {
  607.                 $wp_cache_mobile_prefixes = array( 'w3c ', 'w3c-', 'acs-', 'alav', 'alca', 'amoi', 'audi', 'avan', 'benq', 'bird', 'blac', 'blaz', 'brew', 'cell', 'cldc', 'cmd-', 'dang', 'doco', 'eric', 'hipt', 'htc_', 'inno', 'ipaq', 'ipod', 'jigs', 'kddi', 'keji', 'leno', 'lg-c', 'lg-d', 'lg-g', 'lge-', 'lg/u', 'maui', 'maxo', 'midp', 'mits', 'mmef', 'mobi', 'mot-', 'moto', 'mwbp', 'nec-', 'newt', 'noki', 'palm', 'pana', 'pant', 'phil', 'play', 'port', 'prox', 'qwap', 'sage', 'sams', 'sany', 'sch-', 'sec-', 'send', 'seri', 'sgh-', 'shar', 'sie-', 'siem', 'smal', 'smar', 'sony', 'sph-', 'symb', 't-mo', 'teli', 'tim-', 'tosh', 'tsm-', 'upg1', 'upsi', 'vk-v', 'voda', 'wap-', 'wapa', 'wapi', 'wapp', 'wapr', 'webc', 'winw', 'winw', 'xda ', 'xda-' ); // from http://svn.wp-plugins.org/wordpress-mobile-pack/trunk/plugins/wpmp_switcher/lite_detection.php
  608.         }
  609.         $wp_cache_mobile_browsers = apply_filters( 'cached_mobile_browsers', $wp_cache_mobile_browsers ); // Allow mobile plugins access to modify the mobile UA list
  610.         $wp_cache_mobile_prefixes = apply_filters( 'cached_mobile_prefixes', $wp_cache_mobile_prefixes ); // Allow mobile plugins access to modify the mobile UA prefix list
  611.         if ( function_exists( 'do_cacheaction' ) ) {
  612.                 $wp_cache_mobile_browsers = do_cacheaction( 'wp_super_cache_mobile_browsers', $wp_cache_mobile_browsers );
  613.                 $wp_cache_mobile_prefixes = do_cacheaction( 'wp_super_cache_mobile_prefixes', $wp_cache_mobile_prefixes );
  614.         }
  615.         $mobile_groups = apply_filters( 'cached_mobile_groups', array() ); // Group mobile user agents by capabilities. Lump them all together by default
  616.         // mobile_groups = array( 'apple' => array( 'ipod', 'iphone' ), 'nokia' => array( 'nokia5800', 'symbianos' ) );
  617.  
  618.         if ( false == apply_filters( 'wp_super_cache_error_checking', true ) )
  619.                 return false;
  620.  
  621.         $supercachedir = get_supercache_dir();
  622.         if( get_option( 'gzipcompression' ) == 1 )
  623.                 update_option( 'gzipcompression', 0 );
  624.         if( !isset( $cache_rebuild_files ) )
  625.                 $cache_rebuild_files = 0;
  626.  
  627.         $valid_nonce = isset($_REQUEST['_wpnonce']) ? wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache') : false;
  628.         /* http://www.netlobo.com/div_hiding.html */
  629.         ?>
  630. <script type='text/javascript'>
  631. <!--
  632. function toggleLayer( whichLayer ) {
  633.   var elem, vis;
  634.   if( document.getElementById ) // this is the way the standards work
  635.     elem = document.getElementById( whichLayer );
  636.   else if( document.all ) // this is the way old msie versions work
  637.       elem = document.all[whichLayer];
  638.   else if( document.layers ) // this is the way nn4 works
  639.     elem = document.layers[whichLayer];
  640.   vis = elem.style;
  641.   // if the style.display value is blank we try to figure it out here
  642.   if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
  643.     vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  644.   vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  645. }
  646. // -->
  647. //Clicking header opens fieldset options
  648. jQuery(document).ready(function(){
  649.         jQuery("fieldset h3").css("cursor","pointer").click(function(){
  650.                 jQuery(this).parent("fieldset").find("p,form,ul,blockquote").toggle("slow");
  651.         });
  652. });
  653. </script>
  654.  
  655. <style type='text/css'>
  656. #nav h2 {
  657.         border-bottom: 1px solid #ccc;
  658.         padding-bottom: 0;
  659. }
  660. </style>
  661. <?php
  662.         echo '<a name="top"></a>';
  663.         echo '<div class="wrap">';
  664.         echo '<h2>' . __( 'WP Super Cache Settings', 'wp-super-cache' ) . '</h2>';
  665.  
  666.         // set a default
  667.         if ( $cache_enabled == false && isset( $wp_cache_mod_rewrite ) == false ) {
  668.                 $wp_cache_mod_rewrite = 0;
  669.         } elseif ( !isset( $wp_cache_mod_rewrite ) && $cache_enabled && $super_cache_enabled ) {
  670.                 $wp_cache_mod_rewrite = 1;
  671.         }
  672.  
  673.         if ( !isset( $_GET[ 'tab' ] ) && $cache_enabled && ( $wp_cache_mod_rewrite || $super_cache_enabled == false ) ) {
  674.                 $_GET[ 'tab' ] = 'settings';
  675.                 echo '<div id="message" class="updated fade"><p>' .  __( 'Notice: <em>Mod_rewrite or Legacy caching enabled</em>. Showing Advanced Settings Page by default.', 'wp-super-cache' ) . '</p></div>';
  676.         }
  677.         wpsc_admin_tabs();
  678.  
  679.         if ( isset( $wp_super_cache_front_page_check ) && $wp_super_cache_front_page_check == 1 && !wp_next_scheduled( 'wp_cache_check_site_hook' ) ) {
  680.                 wp_schedule_single_event( time() + 360 , 'wp_cache_check_site_hook' );
  681.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'scheduled wp_cache_check_site_hook for 360 seconds time.', 2 );
  682.         }
  683.  
  684.         if(isset($_REQUEST['wp_restore_config']) && $valid_nonce) {
  685.                 unlink($wp_cache_config_file);
  686.                 echo '<strong>' . __( 'Configuration file changed, some values might be wrong. Load the page again from the "Settings" menu to reset them.', 'wp-super-cache' ) . '</strong>';
  687.         }
  688.  
  689.         if ( substr( get_option( 'permalink_structure' ), -1 ) == '/' ) {
  690.                 wp_cache_replace_line('^ *\$wp_cache_slash_check', "\$wp_cache_slash_check = 1;", $wp_cache_config_file);
  691.         } else {
  692.                 wp_cache_replace_line('^ *\$wp_cache_slash_check', "\$wp_cache_slash_check = 0;", $wp_cache_config_file);
  693.         }
  694.         $home_path = parse_url( site_url() );
  695.         $home_path = trailingslashit( array_key_exists( 'path', $home_path ) ? $home_path[ 'path' ] : '' );
  696.         if (! isset( $wp_cache_home_path ) )
  697.                 $wp_cache_home_path = '/';
  698.         if ( "$home_path" != "$wp_cache_home_path" )
  699.                 wp_cache_replace_line('^ *\$wp_cache_home_path', "\$wp_cache_home_path = '$home_path';", $wp_cache_config_file);
  700.        
  701.  
  702.         if( $wp_cache_mobile_enabled == 1 ) {
  703.                 update_cached_mobile_ua_list( $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes, $mobile_groups );
  704.         }
  705.  
  706.         ?> <table><td valign='top'><?php
  707.         switch( $_GET[ 'tab' ] ) {
  708.                 case "cdn":
  709.                 scossdl_off_options();
  710.                 break;
  711.                 case "tester":
  712.                 case "contents":
  713.                 echo '<a name="test"></a>';
  714.                 wp_cache_files();
  715.                 break;
  716.                 case "preload":
  717.                 if ( !$cache_enabled )
  718.                         wp_die( __( 'Caching must be enabled to use this feature', 'wp-super-cache' ) );
  719.                 echo '<a name="preload"></a>';
  720.                 if ( $super_cache_enabled == true && false == defined( 'DISABLESUPERCACHEPRELOADING' ) ) {
  721.                         global $wp_cache_preload_interval, $wp_cache_preload_on, $wp_cache_preload_taxonomies, $wp_cache_preload_email_me, $wp_cache_preload_email_volume, $wp_cache_preload_posts, $wpdb;
  722.                         $count = $wpdb->get_var( "SELECT count(*) FROM {$wpdb->posts} WHERE post_status = 'publish'" );
  723.                         if ( $count > 1000 ) {
  724.                                 $min_refresh_interval = 720;
  725.                         } else {
  726.                                 $min_refresh_interval = 30;
  727.                         }
  728.                         if ( array_key_exists('action', $_POST) && $_POST[ 'action' ] == 'preload' && $valid_nonce ) {
  729.                                 if ( $_POST[ 'posts_to_cache' ] == 'all' ) {
  730.                                         $wp_cache_preload_posts = 'all';
  731.                                 } else {
  732.                                         $wp_cache_preload_posts = (int)$_POST[ 'posts_to_cache' ];
  733.                                 }
  734.                                 wp_cache_replace_line('^ *\$wp_cache_preload_posts', "\$wp_cache_preload_posts = '$wp_cache_preload_posts';", $wp_cache_config_file);
  735.  
  736.                                 if ( isset( $_POST[ 'preload' ] ) && $_POST[ 'preload' ] == __( 'Cancel Cache Preload', 'wp-super-cache' ) ) {
  737.                                         $next_preload = wp_next_scheduled( 'wp_cache_preload_hook' );
  738.                                         if ( $next_preload ) {
  739.                                                 update_option( 'preload_cache_counter', array( 'c' => 0, 't' => time() ) );
  740.                                                 wp_unschedule_event( $next_preload, 'wp_cache_preload_hook' );
  741.                                         }
  742.                                         $fp = @fopen( $cache_path . "stop_preload.txt", 'w' );
  743.                                         @fclose( $fp );
  744.                                         echo "<p><strong>" . __( 'Scheduled preloading of cache almost cancelled. It may take up to a minute for it to cancel completely.', 'wp-super-cache' ) . "</strong></p>";
  745.                                 } elseif ( isset( $_POST[ 'custom_preload_interval' ] ) && ( $_POST[ 'custom_preload_interval' ] == 0 || $_POST[ 'custom_preload_interval' ] >= $min_refresh_interval ) ) {
  746.                                         // if preload interval changes than unschedule any preload jobs and schedule any new one.
  747.                                         $_POST[ 'custom_preload_interval' ] = (int)$_POST[ 'custom_preload_interval' ];
  748.                                         if ( $wp_cache_preload_interval != $_POST[ 'custom_preload_interval' ] ) {
  749.                                                 $next_preload = wp_next_scheduled( 'wp_cache_full_preload_hook' );
  750.                                                 if ( $next_preload ) {
  751.                                                         update_option( 'preload_cache_counter', array( 'c' => 0, 't' => time() ) );
  752.                                                         add_option( 'preload_cache_stop', 1 );
  753.                                                         wp_unschedule_event( $next_preload, 'wp_cache_full_preload_hook' );
  754.                                                         if ( $wp_cache_preload_interval == 0 ) {
  755.                                                                 echo "<p><strong>" . __( 'Scheduled preloading of cache cancelled.', 'wp-super-cache' ) . "</strong></p>";
  756.                                                         }
  757.                                                 }
  758.                                                 if ( $_POST[ 'custom_preload_interval' ] != 0 )
  759.                                                         wp_schedule_single_event( time() + ( $_POST[ 'custom_preload_interval' ] * 60 ), 'wp_cache_full_preload_hook' );
  760.                                         }
  761.                                         $wp_cache_preload_interval = (int)$_POST[ 'custom_preload_interval' ];
  762.                                         wp_cache_replace_line('^ *\$wp_cache_preload_interval', "\$wp_cache_preload_interval = $wp_cache_preload_interval;", $wp_cache_config_file);
  763.                                         if ( isset( $_POST[ 'preload_email_me' ] ) ) {
  764.                                                 $wp_cache_preload_email_me = 1;
  765.                                         } else {
  766.                                                 $wp_cache_preload_email_me = 0;
  767.                                         }
  768.                                         wp_cache_replace_line('^ *\$wp_cache_preload_email_me', "\$wp_cache_preload_email_me = $wp_cache_preload_email_me;", $wp_cache_config_file);
  769.                                         if ( isset( $_POST[ 'wp_cache_preload_email_volume' ] ) && in_array( $_POST[ 'wp_cache_preload_email_volume' ], array( 'less', 'medium', 'many' ) ) ) {
  770.                                                 $wp_cache_preload_email_volume = $_POST[ 'wp_cache_preload_email_volume' ];
  771.                                         } else {
  772.                                                 $wp_cache_preload_email_volume = 'medium';
  773.                                         }
  774.                                         wp_cache_replace_line('^ *\$wp_cache_preload_email_volume', "\$wp_cache_preload_email_volume = '$wp_cache_preload_email_volume';", $wp_cache_config_file);
  775.                                         if ( isset( $_POST[ 'preload_taxonomies' ] ) ) {
  776.                                                 $wp_cache_preload_taxonomies = 1;
  777.                                         } else {
  778.                                                 $wp_cache_preload_taxonomies = 0;
  779.                                         }
  780.                                         wp_cache_replace_line('^ *\$wp_cache_preload_taxonomies', "\$wp_cache_preload_taxonomies = $wp_cache_preload_taxonomies;", $wp_cache_config_file);
  781.                                         if ( isset( $_POST[ 'preload_on' ] ) ) {
  782.                                                 $wp_cache_preload_on = 1;
  783.                                         } else {
  784.                                                 $wp_cache_preload_on = 0;
  785.                                         }
  786.                                         wp_cache_replace_line('^ *\$wp_cache_preload_on', "\$wp_cache_preload_on = $wp_cache_preload_on;", $wp_cache_config_file);
  787.                                         if ( isset( $_POST[ 'preload' ] ) && $_POST[ 'preload' ] == __( 'Preload Cache Now', 'wp-super-cache' ) ) {
  788.                                                 @unlink( $cache_path . "preload_mutex.tmp" );
  789.                                                 update_option( 'preload_cache_counter', array( 'c' => 0, 't' => time() ) );
  790.                                                 wp_schedule_single_event( time() + 10, 'wp_cache_preload_hook' );
  791.                                                 echo "<p><strong>" . __( 'Scheduled preloading of cache in 10 seconds.' ) . "</strong></p>";
  792.                                         } elseif ( (int)$_POST[ 'custom_preload_interval' ] ) {
  793.                                                 @unlink( $cache_path . "preload_mutex.tmp" );
  794.                                                 update_option( 'preload_cache_counter', array( 'c' => 0, 't' => time() ) );
  795.                                                 wp_schedule_single_event( time() + ( (int)$_POST[ 'custom_preload_interval' ] * 60 ), 'wp_cache_full_preload_hook' );
  796.                                                 echo "<p><strong>" . sprintf( __( 'Scheduled preloading of cache in %d minutes', 'wp-super-cache' ), (int)$_POST[ 'custom_preload_interval' ] ) . "</strong></p>";
  797.                                         }
  798.                                 }
  799.                         }
  800.                         echo '<p>' . __( 'This will cache every published post and page on your site. It will create supercache static files so unknown visitors (including bots) will hit a cached page. This will probably help your Google ranking as they are using speed as a metric when judging websites now.', 'wp-super-cache' ) . '</p>';
  801.                         echo '<p>' . __( 'Preloading creates lots of files however. Caching is done from the newest post to the oldest so please consider only caching the newest if you have lots (10,000+) of posts. This is especially important on shared hosting.', 'wp-super-cache' ) . '</p>';
  802.                         echo '<p>' . __( 'In &#8217;Preload Mode&#8217; regular garbage collection will only clean out old legacy files for known users, not the preloaded supercache files. This is a recommended setting when the cache is preloaded.', 'wp-super-cache' ) . '</p>';
  803.                         echo '<form name="cache_filler" action="" method="POST">';
  804.                         echo '<input type="hidden" name="action" value="preload" />';
  805.                         echo '<input type="hidden" name="page" value="wpsupercache" />';
  806.                         echo '<p>' . sprintf( __( 'Refresh preloaded cache files every %s minutes. (0 to disable, minimum %d minutes.)', 'wp-super-cache' ), "<input type='text' size=4 name='custom_preload_interval' value='" . (int)$wp_cache_preload_interval . "' />", $min_refresh_interval ) . '</p>';
  807.                         if ( $count > 100 ) {
  808.                                 $step = (int)( $count / 10 );
  809.  
  810.                                 $select = "<select name='posts_to_cache' size=1>";
  811.                                 $select .= "<option value='all' ";
  812.                                 if ( !isset( $wp_cache_preload_posts ) || $wp_cache_preload_posts == 'all' ) {
  813.                                         $checked = 'selectect=1 ';
  814.                                         $best = 'all';
  815.                                 } else {
  816.                                         $checked = ' ';
  817.                                         $best = $wp_cache_preload_posts;
  818.                                 }
  819.                                 $select .= "{$checked}>" . __( 'all', 'wp-super-cache' ) . "</option>";
  820.  
  821.                                 for( $c = $step; $c < $count; $c += $step ) {
  822.                                         $checked = ' ';
  823.                                         if ( $best == $c )
  824.                                                 $checked = 'selected=1 ';
  825.                                         $select .= "<option value='$c'{$checked}>$c</option>";
  826.                                 }
  827.                                 $checked = ' ';
  828.                                 if ( $best == $count )
  829.                                         $checked = 'selected=1 ';
  830.                                 $select .= "<option value='$count'{$checked}>$count</option>";
  831.                                 $select .= "</select>";
  832.                                 echo '<p>' . sprintf( __( 'Preload %s posts.', 'wp-super-cache' ), $select ) . '</p>';
  833.                         } else {
  834.                                 echo '<input type="hidden" name="posts_to_cache" value="' . $count . '" />';
  835.                         }
  836.  
  837.                         echo '<input type="checkbox" name="preload_on" value="1" ';
  838.                         echo $wp_cache_preload_on == 1 ? 'checked=1' : '';
  839.                         echo ' /> ' . __( 'Preload mode (garbage collection only on legacy cache files. Recommended.)', 'wp-super-cache' ) . '<br />';
  840.                         echo '<input type="checkbox" name="preload_taxonomies" value="1" ';
  841.                         echo $wp_cache_preload_taxonomies == 1 ? 'checked=1' : '';
  842.                         echo ' /> ' . __( 'Preload tags, categories and other taxonomies.', 'wp-super-cache' ) . '<br />';
  843.                         echo '<input type="checkbox" name="preload_email_me" value="1" ';
  844.                         echo $wp_cache_preload_email_me == 1 ? 'checked=1' : '';
  845.                         echo ' /> ' . __( 'Send me status emails when files are refreshed.', 'wp-super-cache' ) . '<br />';
  846.                         if ( !isset( $wp_cache_preload_email_volume ) )
  847.                                 $wp_cache_preload_email_volume = 'many';
  848.                         echo '&nbsp;&nbsp;&nbsp;&nbsp;<input name="wp_cache_preload_email_volume" type="radio" value="many" class="tog" ';
  849.                         checked( 'many', $wp_cache_preload_email_volume );
  850.                         echo '/> ' . __( 'Many emails, 2 emails per 100 posts.', 'wp-super-cache' ) . '<br >';
  851.                         echo '&nbsp;&nbsp;&nbsp;&nbsp;<input name="wp_cache_preload_email_volume" type="radio" value="medium" class="tog" ';
  852.                         checked( 'medium', $wp_cache_preload_email_volume );
  853.                         echo '/> ' . __( 'Medium, 1 email per 100 posts.', 'wp-super-cache' ) . '<br >';
  854.                         echo '&nbsp;&nbsp;&nbsp;&nbsp;<input name="wp_cache_preload_email_volume" type="radio" value="less" class="tog" ';
  855.                         checked( 'less', $wp_cache_preload_email_volume );
  856.                         echo '/> ' . __( 'Less emails, 1 at the start and 1 at the end of preloading all posts.', 'wp-super-cache' ) . '<br >';
  857.  
  858.                         $currently_preloading = false;
  859.  
  860.                         next_preload_message( 'wp_cache_preload_hook', __( 'Refresh of cache in %d hours %d minutes and %d seconds.', 'wp-super-cache' ), 60 );
  861.                         next_preload_message( 'wp_cache_full_preload_hook', __( 'Full refresh of cache in %d hours %d minutes and %d seconds.', 'wp-super-cache' ) );
  862.  
  863.                         $preload_counter = get_option( 'preload_cache_counter' );
  864.                         if ( isset( $preload_counter[ 'first' ] ) ) // converted from int to array
  865.                                 update_option( 'preload_cache_counter', array( 'c' => $preload_counter[ 'c' ], 't' => time() ) );
  866.                         if ( is_array( $preload_counter ) && $preload_counter[ 'c' ] > 0 ) {
  867.                                 echo '<p><strong>' . sprintf( __( 'Currently caching from post %d to %d.', 'wp-super-cache' ), ( $preload_counter[ 'c' ] - 100 ), $preload_counter[ 'c' ] ) . '</strong></p>';
  868.                                 $currently_preloading = true;
  869.                                 if ( @file_exists( $cache_path . "preload_permalink.txt" ) ) {
  870.                                         $url = file_get_contents( $cache_path . "preload_permalink.txt" );
  871.                                         echo "<p>" . sprintf( __( "<strong>Page last cached:</strong> %s", 'wp-super-cache' ), $url ) . "</p>";
  872.                                 }
  873.                         }
  874.                         echo '<div class="submit"><input type="submit" name="preload" value="' . __( 'Update Settings', 'wp-super-cache' ) . '" />&nbsp;<input type="submit" name="preload" value="' . __( 'Preload Cache Now', 'wp-super-cache' ) . '" />';
  875.                         if ( $currently_preloading ) {
  876.                                 echo '&nbsp;<input type="submit" name="preload" value="' . __( 'Cancel Cache Preload', 'wp-super-cache' ) . '" />';
  877.                         }
  878.                         echo '</div>';
  879.                         wp_nonce_field('wp-cache');
  880.                         echo '</form>';
  881.                 } else {
  882.                         echo '<p>' . __( 'Preloading of cache disabled. Please disable legacy page caching or talk to your host administrator.', 'wp-super-cache' ) . '</p>';
  883.                 }
  884.                 break;
  885.                 case 'plugins':
  886.                 wpsc_plugins_tab();
  887.                 break;
  888.                 case 'debug':
  889.                 wp_cache_debug_settings();
  890.                 break;
  891.                 case 'settings':
  892.                 if ( isset( $wp_cache_front_page_checks ) == false )
  893.                         $wp_cache_front_page_checks = true;
  894.                 echo '<form name="wp_manager" action="' . add_query_arg( array( 'page' => 'wpsupercache', 'tab' => 'settings' ) ) . '" method="post">';
  895.                 wp_nonce_field('wp-cache');
  896.                 echo '<input type="hidden" name="action" value="scupdates" />';
  897.                 ?><table class="form-table">
  898.                 <tr valign="top">
  899.                         <th scope="row"><label for="wp_cache_status"><?php _e( 'Caching', 'wp-super-cache' ); ?></label></th>
  900.                         <td>
  901.                                 <fieldset>
  902.                                 <legend class="hidden">Caching</legend>
  903.                                 <label><input type='checkbox' name='wp_cache_status' value='all' <?php if ( $cache_enabled == true ) { echo 'checked=checked'; } ?>> <?php _e( 'Cache hits to this website for quick access.', 'wp-super-cache' ); echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?></label><br /><br />
  904.                                 <label><input type='radio' name='super_cache_enabled' <?php if( $super_cache_enabled ) echo "checked"; ?> value='1'> <?php printf( __( 'Use mod_rewrite to serve cache files.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wordpress-mobile-edition/' ); echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?></label><br />
  905.                                 <label><input type='radio' name='super_cache_enabled' <?php if( $wp_cache_mod_rewrite == 0 ) echo "checked"; ?> value='2'> <?php printf( __( 'Use PHP to serve cache files.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wordpress-mobile-edition/' ); ?></label><br />
  906.                                 <label><input type='radio' name='super_cache_enabled' <?php if( $super_cache_enabled == false ) echo "checked"; ?> value='0'> <?php _e( 'Legacy page caching.', 'wp-super-cache' ); ?></label><br />
  907.                                 <em><?php _e( 'Mod_rewrite is fastest, PHP is almost as fast and easier to get working, while legacy caching is slower again, but more flexible and also easy to get working. New users should use PHP caching.', 'wp-super-cache' ); ?></em><br />
  908.                                 </legend>
  909.                                 </fieldset>
  910.                         </td>
  911.                 </tr>
  912.                 <tr valign="top">
  913.                         <th scope="row"><label for="wp_cache_status"><?php _e( 'Miscellaneous', 'wp-super-cache' ); ?></label></th>
  914.                         <td>
  915.                                 <fieldset>
  916.                                 <legend class="hidden">Miscellaneous</legend>
  917.                                 <?php if ( false == defined( 'WPSC_DISABLE_COMPRESSION' ) ) { ?>
  918.                                         <?php if ( false == function_exists( 'gzencode' ) ) { ?>
  919.                                                 <em><?php _e( 'Warning! Compression is disabled as gzencode() function not found.', 'wp-super-cache' ); ?></em><br />
  920.                                         <?php } else { ?>
  921.                                                 <label><input type='checkbox' name='cache_compression' <?php if( $cache_compression ) echo "checked"; ?> value='1'> <?php _e( 'Compress pages so they&#8217;re served more quickly to visitors.', 'wp-super-cache' ); echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?></label><br />
  922.                                                 <em><?php _e( 'Compression is disabled by default because some hosts have problems with compressed files. Switching it on and off clears the cache.', 'wp-super-cache' ); ?></em><br />
  923.                                         <?php }
  924.                                 }
  925.                                 $disable_304 = true;
  926.                                 if ( 0 == $wp_cache_mod_rewrite )
  927.                                         $disable_304 = false;
  928.                                 if ( $disable_304 )
  929.                                         echo "<strike>";
  930.                                 ?><label><input <?php if ( $disable_304 ) { echo "disabled"; } ?> type='checkbox' name='wp_supercache_304' <?php if( $wp_supercache_304 ) echo "checked"; ?> value='1'> <?php _e( '304 Not Modified browser caching. Indicate when a page has not been modified since last requested.', 'wp-super-cache' ); echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?></label><br /><?php
  931.                                 if ( $disable_304 ) {
  932.                                         echo "</strike>";
  933.                                         echo "<p><strong>" . __( 'Warning! 304 browser caching is only supported when not using mod_rewrite caching.', 'wp-super-cache' ) . "</strong></p>";
  934.                                 } else {
  935.                                         ?><em><?php _e( '304 support is disabled by default because in the past GoDaddy had problems with some of the headers used.', 'wp-super-cache' ); ?></em><br /><?php
  936.                                 }
  937.                                 ?><label><input type='checkbox' name='wp_cache_not_logged_in' <?php if( $wp_cache_not_logged_in ) echo "checked"; ?> value='1'> <?php _e( 'Don&#8217;t cache pages for <acronym title="Logged in users and those that comment">known users</acronym>.', 'wp-super-cache' ); echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?></label><br />
  938.                                 <label><input type='checkbox' name='wp_cache_no_cache_for_get' <?php if( $wp_cache_no_cache_for_get ) echo "checked"; ?> value='1'> <?php _e( 'Don&#8217;t cache pages with GET parameters. (?x=y at the end of a url)', 'wp-super-cache' ); ?></label><br />
  939.                                 <label><input type='checkbox' name='wp_cache_make_known_anon' <?php if( $wp_cache_make_known_anon ) echo "checked"; ?> value='1'> <?php _e( 'Make known users anonymous so they&#8217;re served supercached static files.', 'wp-super-cache' ); ?></label><br />
  940.                                 <label><input type='checkbox' name='cache_rebuild_files' <?php if( $cache_rebuild_files ) echo "checked"; ?> value='1'> <?php _e( 'Cache rebuild. Serve a supercache file to anonymous users while a new file is being generated.', 'wp-super-cache' ); echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?></label><br />
  941.                                 <label><input type='checkbox' name='wp_cache_hello_world' <?php if( $wp_cache_hello_world ) echo "checked"; ?> value='1'> <?php printf( __( 'Proudly tell the world your server is <a href="%s">Stephen Fry proof</a>! (places a message in your blog&#8217;s footer)', 'wp-super-cache' ), 'https://twitter.com/#!/HibbsLupusTrust/statuses/136429993059291136' ); ?></label><br />
  942.                                 </legend>
  943.                                 </fieldset>
  944.                         </td>
  945.                 </tr>
  946.                 <tr valign="top">
  947.                         <th scope="row"><label for="wp_cache_status"><?php _e( 'Advanced', 'wp-super-cache' ); ?></label></th>
  948.                         <td>
  949.                                 <fieldset>
  950.                                 <legend class="hidden">Advanced</legend>
  951.                                 <label><input type='checkbox' name='wp_cache_mobile_enabled' <?php if( $wp_cache_mobile_enabled ) echo "checked"; ?> value='1'> <?php _e( 'Mobile device support. (External plugin or theme required. See the <a href="http://wordpress.org/extend/plugins/wp-super-cache/faq/">FAQ</a> for further details.)', 'wp-super-cache' ); ?></label><br />
  952.                                 <?php if ( $wp_cache_mobile_enabled ) {
  953.                                         echo '<blockquote><h4>' . __( 'Mobile Browsers', 'wp-super-cache' ) . '</h4>' . implode( ', ', $wp_cache_mobile_browsers ) . "<br /><h4>" . __( 'Mobile Prefixes', 'wp-super-cache' ) . "</h4>" . implode( ', ', $wp_cache_mobile_prefixes ) . "<br /></blockquote>";
  954.                                 } ?>
  955.                                 <label><input type='checkbox' name='wp_cache_disable_utf8' <?php if( $wp_cache_disable_utf8 ) echo "checked"; ?> value='1'> <?php _e( 'Remove UTF8/blog charset support from .htaccess file. Only necessary if you see odd characters or punctuation looks incorrect. Requires rewrite rules update.', 'wp-super-cache' ); ?></label><br />
  956.                                 <label><input type='checkbox' name='wp_cache_clear_on_post_edit' <?php if( $wp_cache_clear_on_post_edit ) echo "checked"; ?> value='1'> <?php _e( 'Clear all cache files when a post or page is published or updated.', 'wp-super-cache' ); ?></label><br />
  957.                                 <label><input type='checkbox' name='wp_cache_front_page_checks' <?php if( $wp_cache_front_page_checks ) echo "checked"; ?> value='1'> <?php _e( 'Extra homepage checks. (Very occasionally stops homepage caching)', 'wp-super-cache' ); ?></label><?php echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?><br />
  958.                                 <label><input type='checkbox' name='wp_cache_refresh_single_only' <?php if( $wp_cache_refresh_single_only ) echo "checked"; ?> value='1'> <?php _e( 'Only refresh current page when comments made.', 'wp-super-cache' ); ?></label><br />
  959.                                 <label><input type='checkbox' name='wp_supercache_cache_list' <?php if( $wp_supercache_cache_list ) echo "checked"; ?> value='1'> <?php _e( 'List the newest cached pages on this page.', 'wp-super-cache' ); ?></label><br />
  960.                         <?php if( false == defined( 'WPSC_DISABLE_LOCKING' ) ) { ?>
  961.                                 <label><input type='checkbox' name='wp_cache_mutex_disabled' <?php if( !$wp_cache_mutex_disabled ) echo "checked"; ?> value='0'> <?php _e( 'Coarse file locking. You probably don&#8217;t need this but it may help if your server is underpowered. Warning! <em>May cause your server to lock up in very rare cases!</em>', 'wp-super-cache' ); ?></label><br />
  962.                         <?php } ?>
  963.                                 <label><input type='checkbox' name='wp_super_cache_late_init' <?php if( $wp_super_cache_late_init ) echo "checked"; ?> value='1'> <?php _e( 'Late init. Display cached files after WordPress has loaded. Most useful in legacy mode.', 'wp-super-cache' ); ?></label><br />
  964.                         <?php if ( $_wp_using_ext_object_cache ) {
  965.                                 ?><label><input type='checkbox' name='wp_cache_object_cache' <?php if( $wp_cache_object_cache ) echo "checked"; ?> value='1'> <?php echo __( 'Use object cache to store cached files.', 'wp-super-cache' ) . ' ' . __( '(Experimental)', 'wp-super-cache' ); ?></label><?php
  966.                         }?>
  967.                         <?php printf( __( '<strong>DO NOT CACHE PAGE</strong> secret key: <a href="%s">%s</a>', 'wp-super-cache' ), trailingslashit( get_bloginfo( 'wpurl' ) ) . "?donotcachepage={$cache_page_secret}", $cache_page_secret ); ?>
  968.                                 </legend>
  969.                                 </fieldset>
  970.                         </td>
  971.                 </tr>
  972.                 </table>
  973.                 <h3><?php _e( 'Note:', 'wp-super-cache' ); ?></h3>
  974.                 <ol>
  975.                 <li><?php _e( 'Uninstall this plugin on the plugins page. It will automatically clean up after itself. If manual intervention is required then simple instructions are provided.', 'wp-super-cache' ); ?></li>
  976.                 <li><?php printf( __( 'If uninstalling this plugin, make sure the directory <em>%s</em> is writeable by the webserver so the files <em>advanced-cache.php</em> and <em>cache-config.php</em> can be deleted automatically. (Making sure those files are writeable too is probably a good idea!)', 'wp-super-cache' ), WP_CONTENT_DIR ); ?></li>
  977.                 <li><?php printf( __( 'Please see the <a href="%1$s/wp-super-cache/readme.txt">readme.txt</a> for instructions on uninstalling this script. Look for the heading, "How to uninstall WP Super Cache".', 'wp-super-cache' ), WP_PLUGIN_URL ); ?></li><?php
  978.                 echo "<li><em>" . sprintf( __( 'Need help? Check the <a href="%1$s">Super Cache readme file</a>. It includes installation documentation, a FAQ and Troubleshooting tips. The <a href="%2$s">support forum</a> is also available. Your question may already have been answered.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wp-super-cache/', 'http://wordpress.org/tags/wp-super-cache?forum_id=10' ) . "</em></li>";
  979.                 echo "</ol>";
  980.        
  981.                 echo "<div class='submit'><input class='button-primary' type='submit' " . SUBMITDISABLED . " value='" . __( 'Update Status', 'wp-super-cache' ) . " &raquo;' /></div>";
  982.                 wp_nonce_field('wp-cache');
  983.                 ?> </form> <?php
  984.                 wsc_mod_rewrite();
  985.  
  986.                 wp_cache_edit_max_time();
  987.  
  988.                 echo '<a name="files"></a><fieldset class="options"><h3>' . __( 'Accepted Filenames &amp; Rejected URIs', 'wp-super-cache' ) . '</h3>';
  989.                 wp_cache_edit_rejected_pages();
  990.                 echo "\n";
  991.                 wp_cache_edit_rejected();
  992.                 echo "\n";
  993.                 wp_cache_edit_accepted();
  994.                 echo '</fieldset>';
  995.  
  996.                 wp_cache_edit_rejected_ua();
  997.  
  998.                 wp_lock_down();
  999.  
  1000.                 wp_cache_restore();
  1001.  
  1002.                 break;
  1003.                 case "easy":
  1004.                 default:
  1005.                         echo '<form name="wp_manager" action="" method="post">';
  1006.                         echo '<input type="hidden" name="action" value="easysetup" />';
  1007.                         wp_nonce_field('wp-cache');
  1008.                         ?><table class="form-table">
  1009.                                 <tr valign="top">
  1010.                                 <th scope="row"><label for="wp_cache_status"><?php _e( 'Caching', 'wp-super-cache' ); ?></label></th>
  1011.                                 <td>
  1012.                                 <fieldset>
  1013.                                 <label><input type='radio' name='wp_cache_easy_on' value='1' <?php if ( $cache_enabled == true ) { echo 'checked=checked'; } ?>> <?php _e( 'Caching On', 'wp-super-cache' ); echo " <em>(" . __( "Recommended", "wp-super-cache" ) . ")</em>"; ?></label><br />
  1014.                                 <label><input type='radio' name='wp_cache_easy_on' value='0' <?php if ( $cache_enabled == false ) { echo 'checked=checked'; } ?>> <?php _e( 'Caching Off', 'wp-super-cache' ); ?></label><br />
  1015.                                 <em><?php _e( 'Note: enables PHP caching, cache rebuild, and mobile support', 'wp-super-cache' ); ?></em><br />
  1016.                                 </legend>
  1017.                                 </fieldset>
  1018.                                 </td>
  1019.                                 </tr>
  1020.                                 </table>
  1021.                         <?php
  1022.                         if ( $cache_enabled && !$wp_cache_mod_rewrite ) {
  1023.                                 $scrules = trim( implode( "\n", extract_from_markers( trailingslashit( get_home_path() ) . '.htaccess', 'WPSuperCache' ) ) );
  1024.                                 if ( $scrules != '' ) {
  1025.                                         echo "<p><strong>" . __( 'Notice: PHP caching enabled but Supercache mod_rewrite rules detected. Cached files will be served using those rules. If your site is working ok please ignore this message or you can edit the .htaccess file in the root of your install and remove the SuperCache rules.', 'wp-super-cache' ) . '</strong></p>';
  1026.                                 }
  1027.                         }
  1028.                         echo "<div class='submit'><input class='button-primary' type='submit' " . SUBMITDISABLED . " value='" . __( 'Update Status', 'wp-super-cache' ) . " &raquo;' /></div></form>";
  1029.                         if ( $cache_enabled ) {
  1030.                                 echo "<h3>" . __( 'Cache Tester', 'wp-super-cache' ) . "</h3>";
  1031.                                 echo '<p>' . __( 'Test your cached website by clicking the test button below.', 'wp-super-cache' ) . '</p>';
  1032.                                 if ( array_key_exists('action', $_POST) && $_POST[ 'action' ] == 'test' && $valid_nonce ) {
  1033.                                         $url = trailingslashit( get_bloginfo( 'url' ) );
  1034.                                         if ( isset( $_POST[ 'httponly' ] ) )
  1035.                                                 $url = str_replace( 'https://', 'http://', $url );
  1036.                                         // Prime the cache
  1037.                                         echo "<p>" . sprintf(  __( 'Fetching %s to prime cache: ', 'wp-super-cache' ), $url );
  1038.                                         $page = wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
  1039.                                         echo '<span style="color: #0a0; font-weight: bold;">' . __( 'OK', 'wp-super-cache' ) . '</strong></p>';
  1040.                                         sleep( 1 );
  1041.                                         // Get the first copy
  1042.                                         echo "<p>" . sprintf(  __( 'Fetching first copy of %s: ', 'wp-super-cache' ), $url );
  1043.                                         $page = wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
  1044.                                         if ( !is_wp_error( $page ) ) {
  1045.                                                 $fp = fopen( $cache_path . "1.html", "w" );
  1046.                                                 fwrite( $fp, $page[ 'body' ] );
  1047.                                                 fclose( $fp );
  1048.                                                 echo '<span style="color: #0a0; font-weight: bold;">' . __( 'OK', 'wp-super-cache' ) . "</span> (<a href='" . WP_CONTENT_URL . "/cache/1.html'>1.html</a>)</p>";
  1049.                                                 sleep( 1 );
  1050.                                         } else {
  1051.                                                 echo '<span style="color: #a00; font-weight: bold;">' . __( 'FAILED', 'wp-super-cache' ) . "</span></p>";
  1052.                                         }
  1053.                                         // Get the second copy
  1054.                                         echo "<p>" . sprintf(  __( 'Fetching second copy of %s: ', 'wp-super-cache' ), $url );
  1055.                                         $page2 = wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
  1056.                                         if ( !is_wp_error( $page2 ) ) {
  1057.                                                 $fp = fopen( $cache_path . "2.html", "w" );
  1058.                                                 fwrite( $fp, $page2[ 'body' ] );
  1059.                                                 fclose( $fp );
  1060.                                                 echo '<span style="color: #0a0; font-weight: bold;">' . __( 'OK', 'wp-super-cache' ) . "</span> (<a href='" . WP_CONTENT_URL . "/cache/2.html'>2.html</a>)</p>";
  1061.                                         } else {
  1062.                                                 echo '<span style="color: #a00; font-weight: bold;">' . __( 'FAILED', 'wp-super-cache' ) . "</span></p>";
  1063.                                         }
  1064.  
  1065.                                         if ( is_wp_error( $page ) || is_wp_error( $page2 ) || $page[ 'response' ][ 'code' ] != 200 || $page2[ 'response' ][ 'code' ] != 200 ) {
  1066.                                                 echo '<p><span style="color: #a00; font-weight: bold;">' . __( 'One or more page requests failed:', 'wp-super-cache' ) . '</span></p>';
  1067.                                                 $error = false;
  1068.                                                 if ( is_wp_error( $page ) ) {
  1069.                                                         $error = $page;
  1070.                                                 } elseif ( is_wp_error( $page2 ) ) {
  1071.                                                         $error = $page2;
  1072.                                                 }
  1073.                                                 if ( $error ) {
  1074.                                                         $errors = '';
  1075.                                                         $messages = '';
  1076.                                                         foreach ( $error->get_error_codes() as $code ) {
  1077.                                                                 $severity = $error->get_error_data($code);
  1078.                                                                 foreach ( $error->get_error_messages( $code ) as $err ) {
  1079.                                                                         $errors .= '    ' . $err . "<br />\n";
  1080.                                                                 }
  1081.                                                         }
  1082.                                                         if ( !empty($err) )
  1083.                                                                 echo '<div class="updated fade">' . $errors . "</div>\n";
  1084.                                                 } else {
  1085.                                                         echo '<ul><li>' . sprintf( __( 'Page %d: %d (%s)', 'wp-super-cache' ), 1, $page[ 'response' ][ 'code' ], $page[ 'response' ][ 'message' ] ) . '</li>';
  1086.                                                         echo '<li>' . sprintf( __( 'Page %d: %d (%s)', 'wp-super-cache' ), 2, $page2[ 'response' ][ 'code' ], $page2[ 'response' ][ 'message' ] ) . '</li></ul>';
  1087.                                                 }
  1088.                                         }
  1089.  
  1090.                                         if ( ( !is_wp_error( $page ) && !is_wp_error( $page2 ) ) && preg_match( '/(Cached page generated by WP-Super-Cache on) ([0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*)/', $page[ 'body' ], $matches1 ) &&
  1091.                                                         preg_match( '/(Cached page generated by WP-Super-Cache on) ([0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*)/', $page2[ 'body' ], $matches2 ) && $matches1[2] == $matches2[2] ) {
  1092.                                                 echo '<p>' . sprintf( __( 'Page 1: %s', 'wp-super-cache' ), $matches1[ 2 ] ) . '</p>';
  1093.                                                 echo '<p>' . sprintf( __( 'Page 2: %s', 'wp-super-cache' ), $matches2[ 2 ] ) . '</p>';
  1094.                                                 echo '<p><span style="color: #0a0; font-weight: bold;">' . __( 'The timestamps on both pages match!', 'wp-super-cache' ) . '</span></p>';
  1095.                                         } else {
  1096.                                                 echo '<p><strong>' . __( 'The pages do not match! Timestamps differ or were not found!', 'wp-super-cache' ) . '</strong></p>';
  1097.                                                 echo '<p>' . __( 'Things you can do:', 'wp-super-cache' ) . '</p>';
  1098.                                                 echo '<ol><li>' . __( 'Load your homepage in a logged out browser, check the timestamp at the end of the html source. Load the page again and compare the timestamp. Caching is working if the timestamps match.', 'wp-super-cache' ) . '</li>';
  1099.                                                 echo '<li>' . __( 'Enable logging on the Debug page here. That should help you track down the problem.', 'wp-super-cache' ) . '</li>';
  1100.                                                 echo '<li>' . __( 'You should check Page 1 and Page 2 above for errors. Your local server configuration may not allow your website to access itself.', 'wp-super-cache' ) . '</li>';
  1101.                                                 echo "</ol>";
  1102.  
  1103.                                         }
  1104.                                 }
  1105.                                 echo '<form name="cache_tester" action="" method="post">';
  1106.                                 echo '<input type="hidden" name="action" value="test" />';
  1107.                                 if ( 'on' == strtolower( $_SERVER['HTTPS' ] ) )
  1108.                                         echo "<input type='checkbox' name='httponly' checked='checked' value='1' /> " . __( 'Send non-secure (non https) request for homepage', 'wp-super-cache' );
  1109.                                 echo '<div class="submit"><input type="submit" name="test" value="' . __( 'Test Cache', 'wp-super-cache' ) . '" /></div>';
  1110.                                 wp_nonce_field('wp-cache');
  1111.                                 echo '</form>';
  1112.                         }
  1113.                         echo "<h3>" . __( "Delete Cached Pages", 'wp-super-cache' ) . "</h3>";
  1114.                         echo "<p>" . __( "Cached pages are stored on your server as html and PHP files. If you need to delete them use the button below.", 'wp-super-cache' ) . "</p>";
  1115.                         echo '<form name="wp_cache_content_delete" action="?page=wpsupercache&tab=contents" method="post">';
  1116.                         echo '<input type="hidden" name="wp_delete_cache" />';
  1117.                         echo '<div class="submit" style="float:left;margin-left:10px"><input id="deletepost" type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Delete Cache', 'wp-super-cache' ) . ' &raquo;" /></div>';
  1118.                         wp_nonce_field('wp-cache');
  1119.                         echo "</form>\n";
  1120.  
  1121.                         if ( ( defined( 'VHOST' ) || ( defined( 'WP_ALLOW_MULTISITE' ) && constant( 'WP_ALLOW_MULTISITE' ) == true ) ) && wpsupercache_site_admin() ) {
  1122.                                 echo '<form name="wp_cache_content_delete" action="#listfiles" method="post">';
  1123.                                 echo '<input type="hidden" name="wp_delete_all_cache" />';
  1124.                                 echo '<div class="submit" style="float:left;margin-left:10px"><input id="deleteallpost" type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Delete Cache On All Blogs', 'wp-super-cache' ) . ' &raquo;" />';
  1125.                                 wp_nonce_field('wp-cache');
  1126.                                 echo "</form><br />\n";
  1127.                         }
  1128.                         ?>
  1129.                         <h3><?php _e( 'Recommended Links and Plugins', 'wp-super-cache' ); ?></h3>
  1130.                         <p><?php _e( 'Caching is only one part of making a website faster. Here are some other plugins that will help:', 'wp-super-cache' ); ?></p>
  1131.                         <ol><li><?php printf( __( '<a href="%s">WP Minify</a> reduces the number of files served by your web server by joining Javascript and CSS files together. Alternatively you can use <a href="%s">WPSCMin</a>, a Supercache plugin that minifies cached pages. It does not however join JS/CSS files together.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wp-minify/', 'http://lyncd.com/wpscmin/' ); ?></li>
  1132.                         <li><?php printf( __( '<a href="%s">Yahoo! Yslow</a> is an extension for the Firefox add-on Firebug. It analyzes web pages and suggests ways to improve their performance based on a set of rules for high performance web pages. Also try the performance tools online at <a href="%s">GTMetrix</a>.', 'wp-super-cache' ), 'http://developer.yahoo.com/yslow/', 'http://gtmetrix.com/' ); ?></li>
  1133.                         <li><?php printf( __( '<a href="%s">Use Google Libraries</a> allows you to load some commonly used Javascript libraries from Google webservers. Ironically it may reduce your Yslow score.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/use-google-libraries/' ); ?></li>
  1134.                         <li><?php printf( __( 'The <a href="%1$s">CDN Sync Tool</a> plugin will help upload files to Amazon S3/Cloudfront if you would rather not depend on origin pull. See the <a href="%2$s">plugin support forum</a> if you have any queries about this plugin.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/cdn-sync-tool/', 'http://wordpress.org/tags/cdn-sync-tool?forum_id=10' ); ?></li>
  1135.                         <li><?php printf( __( '<strong>Advanced users only:</strong> <a href="%s">Speed up your site with Caching and cache-control</a> explains how to make your site more cacheable with .htaccess rules.', 'wp-super-cache' ), 'http://www.askapache.com/htaccess/speed-up-your-site-with-caching-and-cache-control.html' ); ?></li>
  1136.                         <li><?php printf( __( '<strong>Advanced users only:</strong> Install an object cache. Choose from <a href="%s">Memcached</a>, <a href="%s">XCache</a>, <a href="%s">eAcccelerator</a> and others.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/memcached/', 'http://neosmart.net/dl.php?id=12', 'http://neosmart.net/dl.php?id=13' ); ?></li>
  1137.                         <li><?php printf( __( '<a href="%s">Cron View</a> is a useful plugin to use when trying to debug garbage collection and preload problems.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/cron-view/' ); ?></li>
  1138.                         </ol>
  1139.                        
  1140.                         <?php
  1141.                 break;
  1142.         }
  1143.  
  1144.         ?>
  1145.         </fieldset>
  1146.         </td><td valign='top' style='width: 300px'>
  1147.         <div style='background: #ffc; border: 1px solid #333; margin: 2px; padding: 5px'>
  1148.         <h3 align='center'><?php _e( 'Make WordPress Faster', 'wp-super-cache' ); ?></h3>
  1149.         <p><?php printf( __( '%1$s is maintained and developed by %2$s with contributions from many others.', 'wp-super-cache' ), '<a href="http://ocaoimh.ie/wp-super-cache/?r=supercache">WP Super Cache</a>', '<a href="http://ocaoimh.ie/?r=supercache">Donncha O Caoimh</a>' ); ?></p>
  1150.         <p><?php printf( __( 'He blogs at %1$s and posts photos at %2$s.', 'wp-super-cache' ), '<a href="http://ocaoimh.ie/?r=supercache">Holy Shmoly</a>', '<a href="http://inphotos.org/?r=supercache">In Photos.org</a>' ); ?></p>
  1151.         <p><?php printf( __( 'Please say hi to him on %s too!', 'wp-super-cache' ), '<a href="http://twitter.com/donncha/">Twitter</a>' ); ?></p>
  1152.         <h3 align='center'><?php _e( 'Need Help?', 'wp-super-cache' ); ?></h3>
  1153.         <ol>
  1154.         <li><?php printf( __( '<a href="%1$s">Installation Help</a>', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wp-super-cache/installation/' ); ?></li>
  1155.         <li><?php printf( __( '<a href="%1$s">Frequently Asked Questions</a>', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wp-super-cache/faq/' ); ?></li>
  1156.         <li><?php printf( __( '<a href="%1$s">Support Forum</a>', 'wp-super-cache' ), 'http://wordpress.org/tags/wp-super-cache' ); ?></li>
  1157.         </ol>
  1158.         <h3 align='center'><?php _e( 'Rate This Plugin!', 'wp-super-cache' ); ?></h3>
  1159.         <p><?php printf( __( 'Please <a href="%s">rate</a> this plugin and tell me if it works for you or not. It really helps development.', 'wp-super-cache' ), 'http://wordpress.org/extend/plugins/wp-super-cache/' ); ?></p>
  1160.  
  1161.         <?php
  1162.         if ( isset( $wp_supercache_cache_list ) && $wp_supercache_cache_list ) {
  1163.                 $start_date = get_option( 'wpsupercache_start' );
  1164.                 if ( !$start_date ) {
  1165.                         $start_date = time();
  1166.                 }
  1167.                 ?>
  1168.                 <p><?php printf( __( 'Cached pages since %1$s : <strong>%2$s</strong>', 'wp-super-cache' ), date( 'M j, Y', $start_date ), number_format( get_option( 'wpsupercache_count' ) ) ); ?></p>
  1169.                 <p><?php _e( 'Newest Cached Pages:', 'wp-super-cache' ); ?><ol>
  1170.                 <?php
  1171.                 foreach( array_reverse( (array)get_option( 'supercache_last_cached' ) ) as $url ) {
  1172.                         $since = time() - strtotime( $url[ 'date' ] );
  1173.                         echo "<li><a title='" . sprintf( __( 'Cached %s seconds ago', 'wp-super-cache' ), $since ) . "' href='" . site_url( $url[ 'url' ] ) . "'>" . substr( $url[ 'url' ], 0, 20 ) . "</a></li>\n";
  1174.                 }
  1175.                 ?></ol>
  1176.                 <small><?php _e( '(may not always be accurate on busy sites)', 'wp-super-cache' ); ?></small>
  1177.                 </p><?php
  1178.         } else {
  1179.                 $start_date = get_option( 'wpsupercache_start' );
  1180.                 if ( $start_date ) {
  1181.                         update_option( 'wpsupercache_start', $start_date );
  1182.                         update_option( 'wpsupercache_count', 0 );
  1183.                 }
  1184.         }
  1185.         ?>
  1186.         </div>
  1187.         </td></table>
  1188.  
  1189.         <?php
  1190.  
  1191.         echo "</div>\n";
  1192. }
  1193.  
  1194. function wpsc_plugins_tab() {
  1195.         echo '<p>' . __( 'Cache plugins are PHP scripts that live in a plugins folder inside the wp-super-cache folder. They are loaded when Supercache loads, much sooner than regular WordPress plugins.', 'wp-super-cache' ) . '</p>';
  1196.         echo '<p>' . __( 'This is strictly an advanced feature only and knowledge of both PHP and WordPress actions is required to create them.', 'wp-super-cache' ) . '</p>';
  1197.         ob_start();
  1198.         if( defined( 'WP_CACHE' ) ) {
  1199.                 if( function_exists( 'do_cacheaction' ) ) {
  1200.                         do_cacheaction( 'cache_admin_page' );
  1201.                 }
  1202.         }
  1203.         $out = ob_get_contents();
  1204.         ob_end_clean();
  1205.         if( SUBMITDISABLED == ' ' && $out != '' ) {
  1206.                 echo '<h3>' . __( 'Available Plugins', 'wp-super-cache' ) . '</h3>';
  1207.                 echo "<ol>";
  1208.                 echo $out;
  1209.                 echo "</ol>";
  1210.         }
  1211.  
  1212. }
  1213.  
  1214. function wpsc_admin_tabs( $current = 0 ) {
  1215.         global $wp_db_version;
  1216.         if ( $current == 0 ) {
  1217.                 if ( isset( $_GET[ 'tab' ] ) ) {
  1218.                         $current = $_GET[ 'tab' ];
  1219.                 } else {
  1220.                         $current = 'easy';
  1221.                 }
  1222.         }
  1223.         $tabs = array( 'easy' => __( 'Easy', 'wp-super-cache' ), 'settings' => __( 'Advanced', 'wp-super-cache' ), 'cdn' => __( 'CDN', 'wp-super-cache' ), 'contents' => __( 'Contents', 'wp-super-cache' ), 'preload' => __( 'Preload', 'wp-super-cache' ), 'plugins' => __( 'Plugins', 'wp-super-cache' ), 'debug' => __( 'Debug', 'wp-super-cache' ) );
  1224.         $links = array();
  1225.         foreach( $tabs as $tab => $name ) {
  1226.                 if ( $current == $tab ) {
  1227.                         $links[] = "<a class='nav-tab nav-tab-active' href='?page=wpsupercache&tab=$tab'>$name</a>";
  1228.                 } else {
  1229.                         $links[] = "<a class='nav-tab' href='?page=wpsupercache&tab=$tab'>$name</a>";
  1230.                 }
  1231.         }
  1232.         if ( $wp_db_version >= 15477 ) {
  1233.                 echo '<div id="nav"><h2 class="themes-php">';
  1234.                 echo implode( "", $links );
  1235.                 echo '</h2></div>';
  1236.         } else {
  1237.                 echo implode( " | ", $links );
  1238.         }
  1239. }
  1240.  
  1241. function wsc_mod_rewrite() {
  1242.         global $cache_enabled, $super_cache_enabled, $valid_nonce, $cache_path, $wp_cache_mod_rewrite, $wpmu_version;
  1243.  
  1244.         if ( !$wp_cache_mod_rewrite )
  1245.                 return false;
  1246.  
  1247.         if ( isset( $wpmu_version ) || function_exists( 'is_multisite' ) && is_multisite() ) {
  1248.                 if ( false == current_user_can('wp_supercache_manage_options') )
  1249.                         return false;
  1250.                 if ( function_exists( "is_main_site" ) && false == is_main_site() || function_exists( 'is_main_blog' ) && false == is_main_blog() ) {
  1251.                         global $current_site;
  1252.                         $protocol = ( 'on' == strtolower( $_SERVER['HTTPS' ] ) ) ? 'https://' : 'http://';
  1253.                         if ( isset( $wpmu_version ) ) {
  1254.                                 $link_to_admin = admin_url( "wpmu-admin.php?page=wpsupercache" );
  1255.                         } else {
  1256.                                 $link_to_admin = admin_url( "ms-admin.php?page=wpsupercache" );
  1257.                         }
  1258.                         echo '<div id="message" class="updated fade"><p>' .  sprintf( __( 'Notice: WP Super Cache mod_rewrite rule checks disabled unless running on <a href="%s">the main site</a> of this network.', 'wp-super-cache' ), $link_to_admin ) . '</p></div>';
  1259.                         return false;
  1260.                 }
  1261.         }
  1262.  
  1263.         if ( function_exists( "is_main_site" ) && false == is_main_site() || function_exists( 'is_main_blog' ) && false == is_main_blog() )
  1264.                 return true;
  1265.         ?>
  1266.         <a name="modrewrite"></a><fieldset class="options">
  1267.         <h3><?php _e( 'Mod Rewrite Rules', 'wp-super-cache' ); ?></h3><?php
  1268.  
  1269.         extract( wpsc_get_htaccess_info() );
  1270.         $dohtaccess = true;
  1271.         global $wpmu_version;
  1272.         if( isset( $wpmu_version ) ) {
  1273.                 echo "<h4 style='color: #a00'>" . __( 'WordPress MU Detected', 'wp-super-cache' ) . "</h4><p>" . __( "Unfortunately the rewrite rules cannot be updated automatically when running WordPress MU. Please open your .htaccess and add the following mod_rewrite rules above any other rules in that file.", 'wp-super-cache' ) . "</p>";
  1274.         } elseif( !$wprules || $wprules == '' ) {
  1275.                 echo "<h4 style='color: #a00'>" . __( 'Mod Rewrite rules cannot be updated!', 'wp-super-cache' ) . "</h4>";
  1276.                 echo "<p>" . sprintf( __( "You must have <strong>BEGIN</strong> and <strong>END</strong> markers in %s.htaccess for the auto update to work. They look like this and surround the main WordPress mod_rewrite rules:", 'wp-super-cache' ), $home_path );
  1277.                 echo "<blockquote><pre><em># BEGIN WordPress</em>\n RewriteCond %{REQUEST_FILENAME} !-f\n RewriteCond %{REQUEST_FILENAME} !-d\n RewriteRule . /index.php [L]\n <em># END WordPress</em></pre></blockquote>";
  1278.                 _e( 'Refresh this page when you have updated your .htaccess file.', 'wp-super-cache' );
  1279.                 echo "</fieldset>";
  1280.                 $dohtaccess = false;
  1281.         } elseif( strpos( $wprules, 'wordpressuser' ) ) { // Need to clear out old mod_rewrite rules
  1282.                 echo "<p><strong>" . __( 'Thank you for upgrading.', 'wp-super-cache' ) . "</strong> " . sprintf( __( 'The mod_rewrite rules changed since you last installed this plugin. Unfortunately you must remove the old supercache rules before the new ones are updated. Refresh this page when you have edited your .htaccess file. If you wish to manually upgrade, change the following line: %1$s so it looks like this: %2$s The only changes are "HTTP_COOKIE" becomes "HTTP:Cookie" and "wordpressuser" becomes "wordpress". This is a WordPress 2.5 change but it&#8217;s backwards compatible with older versions if you&#8217;re brave enough to use them.', 'wp-super-cache' ), '<blockquote><code>RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$</code></blockquote>', '<blockquote><code>RewriteCond %{HTTP:Cookie} !^.*wordpress.*$</code></blockquote>' ) . "</p>";
  1283.                 echo "</fieldset></div>";
  1284.                 return;
  1285.         } elseif( $scrules != '' && strpos( $scrules, '%{REQUEST_URI} !^.*[^/]$' ) === false && substr( get_option( 'permalink_structure' ), -1 ) == '/' ) { // permalink structure has a trailing slash, need slash check in rules.
  1286.                 echo "<div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><h4>" . __( 'Trailing slash check required.', 'wp-super-cache' ) . "</h4><p>" . __( 'It looks like your blog has URLs that end with a "/". Unfortunately since you installed this plugin a duplicate content bug has been found where URLs not ending in a "/" end serve the same content as those with the "/" and do not redirect to the proper URL. To fix, you must edit your .htaccess file and add these two rules to the two groups of Super Cache rules:', 'wp-super-cache' ) . "</p>";
  1287.                 echo "<blockquote><code>RewriteCond %{REQUEST_URI} !^.*[^/]$RewriteCond %{REQUEST_URI} !^.*//.*$</code></blockquote>";
  1288.                 echo "<p>" . __( 'You can see where the rules go and examine the complete rules by clicking the "View mod_rewrite rules" link below.', 'wp-super-cache' ) . "</p></div>";
  1289.                 $dohtaccess = false;
  1290.         } elseif( strpos( $scrules, 'supercache' ) || strpos( $wprules, 'supercache' ) ) { // only write the rules once
  1291.                 $dohtaccess = false;
  1292.         }
  1293.         if( $dohtaccess && !$_POST[ 'updatehtaccess' ] ) {
  1294.                 if ( $scrules == '' ) {
  1295.                         wpsc_update_htaccess_form( 0 ); // don't hide the update htaccess form
  1296.                 } else {
  1297.                         wpsc_update_htaccess_form();
  1298.                 }
  1299.         } elseif( $valid_nonce && $_POST[ 'updatehtaccess' ] ) {
  1300.                 echo "<div style='padding:0 8px;color:#4f8a10;background-color:#dff2bf;border:1px solid #4f8a10;'>";
  1301.                 if( wpsc_update_htaccess() ) {
  1302.                         echo "<h4>" . __( 'Mod Rewrite rules updated!', 'wp-super-cache' ) . "</h4>";
  1303.                         echo "<p><strong>" . sprintf( __( '%s.htaccess has been updated with the necessary mod_rewrite rules. Please verify they are correct. They should look like this:', 'wp-super-cache' ), $home_path ) . "</strong></p>\n";
  1304.                 } else {
  1305.                         echo "<h4>" . __( 'Mod Rewrite rules must be updated!', 'wp-super-cache' ) . "</h4>";
  1306.                         echo "<p><strong>" . sprintf( __( 'Your %s.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:', 'wp-super-cache' ), $home_path ) . "</strong></p>\n";
  1307.                 }
  1308.                 echo "<p><pre>" . esc_html( $rules ) . "</pre></p>\n</div>";
  1309.         } else {
  1310.                 ?>
  1311.                 <p><?php printf( __( 'WP Super Cache mod rewrite rules were detected in your %s.htaccess file.<br /> Click the following link to see the lines added to that file. If you have upgraded the plugin make sure these rules match.', 'wp-super-cache' ), $home_path ); ?></p>
  1312.                 <?php
  1313.                 if ( $rules != $scrules ) {
  1314.                         ?><p style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><?php _e( 'A difference between the rules in your .htaccess file and the plugin rewrite rules has been found. This could be simple whitespace differences but you should compare the rules in the file with those below as soon as possible. Click the &#8217;Update Mod_Rewrite Rules&#8217; button to update the rules.', 'wp-super-cache' ); ?></p><?php
  1315.                 }
  1316.                 ?><a href="javascript:toggleLayer('rewriterules');" class="button"><?php _e( 'View Mod_Rewrite Rules', 'wp-super-cache' ); ?></a><?php
  1317.                 wpsc_update_htaccess_form();
  1318.                 echo "<div id='rewriterules' style='display: none;'>";
  1319.                 if ( $rules != $scrules )
  1320.                         echo '<div style="background: #fff; border: 1px solid #333; margin: 2px;">' . wp_text_diff( $scrules, $rules, array( 'title' => 'Rewrite Rules', 'title_left' => 'Current Rules', 'title_right' => 'New Rules' ) ) . "</div>";
  1321.                 echo "<p><pre># BEGIN WPSuperCache\n" . esc_html( $rules ) . "# END WPSuperCache</pre></p>\n";
  1322.                 echo "<p>" . sprintf( __( 'Rules must be added to %s too:', 'wp-super-cache' ), WP_CONTENT_DIR . "/cache/.htaccess" ) . "</p>";
  1323.                 echo "<pre># BEGIN supercache\n" . esc_html( $gziprules ) . "# END supercache</pre></p>";
  1324.                 echo '</div>';
  1325.         }
  1326.         // http://allmybrain.com/2007/11/08/making-wp-super-cache-gzip-compression-work/
  1327.         if( !is_file( $cache_path . '.htaccess' ) ) {
  1328.                 $gziprules = insert_with_markers( $cache_path . '.htaccess', 'supercache', explode( "\n", $gziprules ) );
  1329.                 echo "<h4>" . sprintf( __( 'Gzip encoding rules in %s.htaccess created.', 'wp-super-cache' ), $cache_path ) . "</h4>";
  1330.         }
  1331.  
  1332.         ?></fieldset><?php
  1333. }
  1334.  
  1335. function wp_cache_restore() {
  1336.         echo '<fieldset class="options"><h3>' . __( 'Fix Configuration', 'wp-super-cache' ) . '</h3>';
  1337.         echo '<form name="wp_restore" action="#top" method="post">';
  1338.         echo '<input type="hidden" name="wp_restore_config" />';
  1339.         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'id="deletepost" value="' . __( 'Restore Default Configuration', 'wp-super-cache' ) . ' &raquo;" /></div>';
  1340.         wp_nonce_field('wp-cache');
  1341.         echo "</form>\n";
  1342.         echo '</fieldset>';
  1343.  
  1344. }
  1345.  
  1346. function comment_form_lockdown_message() {
  1347.         ?><p><?php _e( "Comment moderation is enabled. Your comment may take some time to appear.", 'wp-super-cache' ); ?></p><?php
  1348. }
  1349. if( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) )
  1350.         add_action( 'comment_form', 'comment_form_lockdown_message' );
  1351.  
  1352. function wp_lock_down() {
  1353.         global $wpdb, $cache_path, $wp_cache_config_file, $valid_nonce, $cached_direct_pages, $cache_enabled, $super_cache_enabled;
  1354.         global $wp_super_cache_lock_down;
  1355.  
  1356.         if(isset($_POST['wp_lock_down']) && $valid_nonce) {
  1357.                 $wp_lock_down = $_POST['wp_lock_down'] == '1' ? '1' : '0';
  1358.                 wp_cache_replace_line('^.*WPLOCKDOWN', "define( 'WPLOCKDOWN', '$wp_lock_down' );", $wp_cache_config_file);
  1359.                 if( $wp_lock_down == '0' && function_exists( 'prune_super_cache' ) )
  1360.                         prune_super_cache( $cache_path, true ); // clear the cache after lockdown
  1361.  
  1362.         }
  1363.         if( !isset( $wp_lock_down ) ) {
  1364.                 if( defined( 'WPLOCKDOWN' ) ) {
  1365.                         $wp_lock_down = constant( 'WPLOCKDOWN' );
  1366.                 } else {
  1367.                         $wp_lock_down = '0';
  1368.                 }
  1369.         }
  1370.         ?><a name='lockdown'></a>
  1371.         <fieldset class="options">
  1372.         <h3><?php _e( 'Lock Down:', 'wp-super-cache' ); ?> <?php echo $wp_lock_down == '0' ? '<span style="color:red">' . __( 'Disabled', 'wp-super-cache' ) . '</span>' : '<span style="color:green">' . __( 'Enabled', 'wp-super-cache' ) . '</span>'; ?></h3>
  1373.         <p><?php _e( '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.', 'wp-super-cache' ); ?></p>
  1374.         <p><?php _e( 'Developers: Make your plugin lock down compatible by checking the "WPLOCKDOWN" constant. The following code will make sure your plugin respects the WPLOCKDOWN setting.', 'wp-super-cache' ); ?>
  1375.         <blockquote><code>if( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) ) {
  1376.                 &nbsp;&nbsp;&nbsp;&nbsp;echo "<?php _e( 'Sorry. My blog is locked down. Updates will appear shortly', 'wp-super-cache' ); ?>";
  1377.                 }</code></blockquote>
  1378.         <?php
  1379.         if( $wp_lock_down == '1' ) {
  1380.                 ?><p><?php _e( 'WordPress is locked down. Super Cache static files will not be deleted when new comments are made.', 'wp-super-cache' ); ?></p><?php
  1381.         } else {
  1382.                 ?><p><?php _e( 'WordPress is not locked down. New comments will refresh Super Cache static files as normal.', 'wp-super-cache' ); ?></p><?php
  1383.         }
  1384.         $new_lockdown =  $wp_lock_down == '1' ? '0' : '1';
  1385.         $new_lockdown_desc =  $wp_lock_down == '1' ? __( 'Disable', 'wp-super-cache' ) : __( 'Enable', 'wp-super-cache' );
  1386.         echo '<form name="wp_lock_down" action="#lockdown" method="post">';
  1387.         echo "<input type='hidden' name='wp_lock_down' value='{$new_lockdown}' />";
  1388.         echo "<div class='submit'><input type='submit' " . SUBMITDISABLED . " value='{$new_lockdown_desc} " . __( 'Lock Down', 'wp-super-cache' ) . " &raquo;' /></div>";
  1389.         wp_nonce_field('wp-cache');
  1390.         echo "</form>\n";
  1391.  
  1392.         ?></fieldset><?php
  1393.         if( $cache_enabled == true && $super_cache_enabled == true ) {
  1394.                 ?><a name='direct'></a>
  1395.         <fieldset class="options">
  1396.         <h3><?php _e( 'Directly Cached Files', 'wp-super-cache' ); ?></h3><?php
  1397.  
  1398.         $out = '';
  1399.         if( $valid_nonce && array_key_exists('direct_pages', $_POST) && is_array( $_POST[ 'direct_pages' ] ) && !empty( $_POST[ 'direct_pages' ] ) ) {
  1400.                 $expiredfiles = array_diff( $cached_direct_pages, $_POST[ 'direct_pages' ] );
  1401.                 unset( $cached_direct_pages );
  1402.                 foreach( $_POST[ 'direct_pages' ] as $page ) {
  1403.                         $page = $wpdb->escape( $page );
  1404.                         if( $page != '' ) {
  1405.                                 $cached_direct_pages[] = $page;
  1406.                                 $out .= "'$page', ";
  1407.                         }
  1408.                 }
  1409.                 if( $out == '' ) {
  1410.                         $out = "'', ";
  1411.                 }
  1412.         }
  1413.         if( $valid_nonce && array_key_exists('new_direct_page', $_POST) && $_POST[ 'new_direct_page' ] && '' != $_POST[ 'new_direct_page' ] ) {
  1414.                 $page = str_replace( get_option( 'siteurl' ), '', $_POST[ 'new_direct_page' ] );
  1415.                 if( substr( $page, 0, 1 ) != '/' )
  1416.                         $page = '/' . $page;
  1417.                 $page = $wpdb->escape( $page );
  1418.                 if( in_array( $page, $cached_direct_pages ) == false ) {
  1419.                         $cached_direct_pages[] = $page;
  1420.                         $out .= "'$page', ";
  1421.                 }
  1422.         }
  1423.  
  1424.         if( $out != '' ) {
  1425.                 $out = substr( $out, 0, -2 );
  1426.                 $out = '$cached_direct_pages = array( ' . $out . ' );';
  1427.                 wp_cache_replace_line('^ *\$cached_direct_pages', "$out", $wp_cache_config_file);
  1428.                 prune_super_cache( $cache_path, true );
  1429.         }
  1430.  
  1431.         if( !empty( $expiredfiles ) ) {
  1432.                 foreach( $expiredfiles as $file ) {
  1433.                         if( $file != '' ) {
  1434.                                 $firstfolder = explode( '/', $file );
  1435.                                 $firstfolder = ABSPATH . $firstfolder[1];
  1436.                                 $file = ABSPATH . $file;
  1437.                                 @unlink( trailingslashit( $file ) . 'index.html' );
  1438.                                 @unlink( trailingslashit( $file ) . 'index.html.gz' );
  1439.                                 RecursiveFolderDelete( trailingslashit( $firstfolder ) );
  1440.                         }
  1441.                 }
  1442.         }
  1443.  
  1444.         if( $valid_nonce && array_key_exists('deletepage', $_POST) && $_POST[ 'deletepage' ] ) {
  1445.                 $page = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '..', '', $_POST['deletepage']) );
  1446.                 $pagefile = ABSPATH . $page . 'index.html';
  1447.                 $firstfolder = explode( '/', $page );
  1448.                 $firstfolder = ABSPATH . $firstfolder[1];
  1449.                 $page = ABSPATH . $page;
  1450.                 if( is_file( $pagefile ) && is_writeable_ACLSafe( $pagefile ) && is_writeable_ACLSafe( $firstfolder ) ) {
  1451.                         @unlink( $pagefile );
  1452.                         @unlink( $pagefile . '.gz' );
  1453.                         RecursiveFolderDelete( $firstfolder );
  1454.                         echo "<strong>" . sprintf( __( '%s removed!', 'wp-super-cache' ), $pagefile ) . "</strong>";
  1455.                         prune_super_cache( $cache_path, true );
  1456.                 }
  1457.         }
  1458.  
  1459.         $readonly = '';
  1460.         if( !is_writeable_ACLSafe( ABSPATH ) ) {
  1461.                 $readonly = 'READONLY';
  1462.                 ?><p style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><strong><?php _e( 'Warning!', 'wp-super-cache' ); ?></strong> <?php printf( __( 'You must make %s writable to enable this feature. As this is a security risk please make it readonly after your page is generated.', 'wp-super-cache' ), ABSPATH ); ?></p><?php
  1463.         } else {
  1464.                 $abspath_stat = stat(ABSPATH . '/');
  1465.                 $abspath_mode = decoct( $abspath_stat[ 'mode' ] & 0777 );
  1466.                 if ( substr( $abspath_mode, -2 ) == '77' ) {
  1467.                         ?><p style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><strong><?php _e( 'Warning!', 'wp-super-cache' ); ?></strong> <?php printf( __( '%s is writable. Please make it readonly after your page is generated as this is a security risk.', 'wp-super-cache' ), ABSPATH ); ?></p><?php
  1468.                 }
  1469.         }
  1470.         echo '<form name="direct_page" action="#direct" method="post">';
  1471.         if( is_array( $cached_direct_pages ) ) {
  1472.                 $out = '';
  1473.                 foreach( $cached_direct_pages as $page ) {
  1474.                         if( $page == '' )
  1475.                                 continue;
  1476.                         $generated = '';
  1477.                         if( is_file( ABSPATH . $page . '/index.html' ) )
  1478.                                 $generated = '<input type="Submit" name="deletepage" value="' . $page . '">';
  1479.                         $out .= "<tr><td><input type='text' $readonly name='direct_pages[]' size='30' value='$page' /></td><td>$generated</td></tr>";
  1480.                 }
  1481.                 if( $out != '' ) {
  1482.                         ?><table><tr><th><?php _e( 'Existing direct page', 'wp-super-cache' ); ?></th><th><?php _e( 'Delete cached file', 'wp-super-cache' ); ?></th></tr><?php
  1483.                         echo "$out</table>";
  1484.                 }
  1485.         }
  1486.         if( $readonly != 'READONLY' )
  1487.                 echo __( "Add direct page:", 'wp-super-cache' ) . "<input type='text' $readonly name='new_direct_page' size='30' value='' />";
  1488.  
  1489.         echo "<p>" . sprintf( __( "Directly cached files are files created directly off %s 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.", 'wp-super-cache' ), ABSPATH ) . "</p>";
  1490.         if( $readonly != 'READONLY' ) {
  1491.                 echo "<p>" . sprintf( __( 'For example: to cache <em>%1$sabout/</em>, you would enter %1$sabout/ or /about/. The cached file will be generated the next time an anonymous user visits that page.', 'wp-super-cache' ), trailingslashit( get_option( 'siteurl' ) ) ) . "</p>";
  1492.                 echo "<p>" . __( 'Make the textbox blank to remove it from the list of direct pages and delete the cached file.', 'wp-super-cache' ) . "</p>";
  1493.         }
  1494.  
  1495.         wp_nonce_field('wp-cache');
  1496.         if( $readonly != 'READONLY' )
  1497.                 echo "<div class='submit'><input type='submit' ' . SUBMITDISABLED . 'value='" . __( 'Update Direct Pages', 'wp-super-cache' ) . " &raquo;' /></div>";
  1498.         echo "</form>\n";
  1499.         ?></fieldset><?php
  1500.         } // if $super_cache_enabled
  1501. }
  1502.  
  1503. function RecursiveFolderDelete ( $folderPath ) { // from http://www.php.net/manual/en/function.rmdir.php
  1504.         if( trailingslashit( constant( 'ABSPATH' ) ) == trailingslashit( $folderPath ) )
  1505.                 return false;
  1506.         if ( @is_dir ( $folderPath ) ) {
  1507.                 $dh  = @opendir($folderPath);
  1508.                 while (false !== ($value = @readdir($dh))) {
  1509.                         if ( $value != "." && $value != ".." ) {
  1510.                                 $value = $folderPath . "/" . $value;
  1511.                                 if ( @is_dir ( $value ) ) {
  1512.                                         RecursiveFolderDelete ( $value );
  1513.                                 }
  1514.                         }
  1515.                 }
  1516.                 return @rmdir ( $folderPath );
  1517.         } else {
  1518.                 return FALSE;
  1519.         }
  1520. }
  1521.  
  1522. function wp_cache_edit_max_time () {
  1523.         global $cache_max_time, $wp_cache_config_file, $valid_nonce, $cache_enabled, $super_cache_enabled, $cache_schedule_type, $cache_scheduled_time, $cache_schedule_interval, $cache_time_interval, $cache_gc_email_me, $wp_cache_preload_on;
  1524.  
  1525.         $timezone_format = _x('Y-m-d G:i:s', 'timezone date format');
  1526.  
  1527.         if( !isset( $cache_schedule_type ) ) {
  1528.                 $cache_schedule_type = 'interval';
  1529.                 wp_cache_replace_line('^ *\$cache_schedule_type', "\$cache_schedule_type = '$cache_schedule_type';", $wp_cache_config_file);
  1530.         }
  1531.  
  1532.         if( !isset( $cache_scheduled_time ) ) {
  1533.                 $cache_scheduled_time = '00:00';
  1534.                 wp_cache_replace_line('^ *\$cache_scheduled_time', "\$cache_scheduled_time = '$cache_scheduled_time';", $wp_cache_config_file);
  1535.         }
  1536.  
  1537.         if( !isset( $cache_max_time ) ) {
  1538.                 $cache_max_time = 3600;
  1539.                 wp_cache_replace_line('^ *\$cache_max_time', "\$cache_max_time = $cache_max_time;", $wp_cache_config_file);
  1540.         }
  1541.  
  1542.         if ( !isset( $cache_time_interval ) ) {
  1543.                 $cache_time_interval = $cache_max_time;
  1544.                 wp_cache_replace_line('^ *\$cache_time_interval', "\$cache_time_interval = '$cache_time_interval';", $wp_cache_config_file);
  1545.         }
  1546.  
  1547.         if ( isset( $_POST['wp_max_time'] ) && $valid_nonce ) {
  1548.                 $cache_max_time = (int)$_POST['wp_max_time'];
  1549.                 wp_cache_replace_line('^ *\$cache_max_time', "\$cache_max_time = $cache_max_time;", $wp_cache_config_file);
  1550.         }
  1551.  
  1552.         if ( isset( $_POST[ 'cache_gc_email_me' ] ) && $valid_nonce ) {
  1553.                 $cache_gc_email_me = 1;
  1554.                 wp_cache_replace_line('^ *\$cache_gc_email_me', "\$cache_gc_email_me = $cache_gc_email_me;", $wp_cache_config_file);
  1555.         } elseif ( $valid_nonce ) {
  1556.                 $cache_gc_email_me = 0;
  1557.                 wp_cache_replace_line('^ *\$cache_gc_email_me', "\$cache_gc_email_me = $cache_gc_email_me;", $wp_cache_config_file);
  1558.         }
  1559.         if ( $_POST[ 'cache_schedule_type' ] == 'interval' && isset( $_POST['cache_time_interval'] ) && $valid_nonce ) {
  1560.                 wp_clear_scheduled_hook( 'wp_cache_gc' );
  1561.                 $cache_schedule_type = 'interval';
  1562.                 if ( (int)$_POST[ 'cache_time_interval' ] == 0 )
  1563.                         $_POST[ 'cache_time_interval' ] = 600;
  1564.                 $cache_time_interval = (int)$_POST[ 'cache_time_interval' ];
  1565.                 wp_schedule_single_event( time() + $cache_time_interval, 'wp_cache_gc' );
  1566.                 wp_cache_replace_line('^ *\$cache_schedule_type', "\$cache_schedule_type = '$cache_schedule_type';", $wp_cache_config_file);
  1567.                 wp_cache_replace_line('^ *\$cache_time_interval', "\$cache_time_interval = '$cache_time_interval';", $wp_cache_config_file);
  1568.         } elseif ( $valid_nonce ) { // clock
  1569.                 wp_clear_scheduled_hook( 'wp_cache_gc' );
  1570.                 $cache_schedule_type = 'time';
  1571.                 if ( $_POST[ 'cache_scheduled_time' ] == '' )
  1572.                         $_POST[ 'cache_scheduled_time' ] = '00:00';
  1573.                 $cache_scheduled_time = $_POST[ 'cache_scheduled_time' ];
  1574.                 $schedules = wp_get_schedules();
  1575.                 if ( isset( $schedules[ $_POST[ 'cache_schedule_interval' ] ] ) )
  1576.                         $cache_schedule_interval = $_POST[ 'cache_schedule_interval' ];
  1577.                 wp_schedule_single_event( strtotime( $cache_scheduled_time ), 'wp_cache_gc' );
  1578.                 wp_cache_replace_line('^ *\$cache_schedule_type', "\$cache_schedule_type = '$cache_schedule_type';", $wp_cache_config_file);
  1579.                 wp_cache_replace_line('^ *\$cache_schedule_interval', "\$cache_schedule_interval = '{$cache_schedule_interval}';", $wp_cache_config_file);
  1580.                 wp_cache_replace_line('^ *\$cache_scheduled_time', "\$cache_scheduled_time = '$cache_scheduled_time';", $wp_cache_config_file);
  1581.                 wp_schedule_event( strtotime( $cache_scheduled_time ), $cache_schedule_interval, 'wp_cache_gc' );
  1582.         }
  1583.         ?><fieldset class="options">
  1584.         <a name='expirytime'></a>
  1585.         <h3><?php _e( 'Expiry Time &amp; Garbage Collection', 'wp-super-cache' ); ?></h3><?php
  1586.  
  1587.         ?><span id="utc-time"><?php printf(__('<abbr title="Coordinated Universal Time">UTC</abbr> time is <code>%s</code>'), date_i18n($timezone_format, false, 'gmt')); ?></span><?php
  1588.         $current_offset = get_option('gmt_offset');
  1589.         if ( get_option('timezone_string') || !empty($current_offset) ) {
  1590.                 ?><span id="local-time"><?php printf(__('Local time is <code>%1$s</code>'), date_i18n($timezone_format)); ?></span><?php
  1591.         }
  1592.         $next_gc = wp_next_scheduled( 'wp_cache_gc' );
  1593.         if ( $next_gc )
  1594.                 echo "<p>" . sprintf( __( 'Next scheduled garbage collection will be at <strong>%s UTC</strong>', 'wp-super-cache' ), date_i18n( $timezone_format, $next_gc, 'gmt' ) ) . "</p>";
  1595.  
  1596.  
  1597.         if ( $wp_cache_preload_on )
  1598.                 echo "<p>" . __( 'Warning! <strong>PRELOAD MODE</strong> activated. Supercache files will not be deleted regardless of age.' ) . "</p>";
  1599.  
  1600.         echo "<script type='text/javascript'>";
  1601.         echo "jQuery(function () {
  1602.                 jQuery('#cache_interval_time').click(function () {
  1603.                         jQuery('#schedule_interval').attr('checked', true);
  1604.                 });
  1605.                 jQuery('#cache_scheduled_time').click(function () {
  1606.                         jQuery('#schedule_time').attr('checked', true);
  1607.                 });
  1608.                 jQuery('#cache_scheduled_select').click(function () {
  1609.                         jQuery('#schedule_time').attr('checked', true);
  1610.                 });
  1611.                 });";
  1612.         echo "</script>";
  1613.         echo '<form name="wp_edit_max_time" action="#expirytime" method="post">';
  1614.         echo '<table class="form-table">';
  1615.         echo '<tr><td><label for="wp_max_time"><strong>' . __( 'Cache Timeout', 'wp-super-cache' ) . '</strong></label></td>';
  1616.         echo "<td><input type='text' id='wp_max_time' size=6 name='wp_max_time' value='$cache_max_time' /> " . __( "seconds", 'wp-super-cache' ) . "</td></tr>\n";
  1617.         echo "<tr><td></td><td>" . __( 'How long should cached pages remain fresh? Set to 0 to disable garbage collection. A good starting point is 3600 seconds.', 'wp-super-cache' ) . "</td></tr>\n";
  1618.         echo '<tr><td valign="top"><strong>' . __( 'Scheduler', 'wp-super-cache' ) . '</strong></td><td><table cellpadding=0 cellspacing=0><tr><td valign="top"><input type="radio" id="schedule_interval" name="cache_schedule_type" value="interval" ' . checked( 'interval', $cache_schedule_type, false ) . ' /></td><td valign="top"><label for="cache_interval_time">' . __( 'Timer:', 'wp-super-cache' ) . '</label></td>';
  1619.         echo "<td><input type='text' id='cache_interval_time' size=6 name='cache_time_interval' value='$cache_time_interval' /> " . __( "seconds", 'wp-super-cache' ) . '<br />' . __( 'Check for stale cached files every <em>interval</em> seconds.', 'wp-super-cache' ) . "</td></tr>";
  1620.         echo '<tr><td valign="top"><input type="radio" id="schedule_time" name="cache_schedule_type" value="time" ' . checked( 'time', $cache_schedule_type, false ) . ' /></td><td valign="top"><label for="schedule_time">' . __( 'Clock:', 'wp-super-cache' ) . '</label></td>';
  1621.         echo "<td><input type=\"text\" size=5 id='cache_scheduled_time' name='cache_scheduled_time' value=\"$cache_scheduled_time\" /> " . __( "HH:MM", 'wp-super-cache' ) . "<br />" . __( 'Check for stale cached files at this time <strong>(UTC)</strong> or starting at this time every <em>interval</em> below.', 'wp-super-cache' ) . "</td></tr>";
  1622.         $schedules = wp_get_schedules();
  1623.         echo "<tr><td><br /></td><td><label for='cache_scheduled_select'>" . __( 'Interval:', 'wp-super-cache' ) . "</label></td><td><select id='cache_scheduled_select' name='cache_schedule_interval' size=1>";
  1624.         foreach( $schedules as $desc => $details ) {
  1625.                 echo "<option value='$desc' " . selected( $desc, $cache_schedule_interval, false ) . " /> {$details[ 'display' ]}</option>";
  1626.         }
  1627.         echo "</select></td></tr>";
  1628.         echo '</table></td></tr>';
  1629.         echo '<tr><td><label for="cache_gc_email_me"><strong>' . __( 'Notification Emails', 'wp-super-cache' ) . '</strong></label></td>';
  1630.         echo "<td><input type='checkbox' id='cache_gc_email_me' name='cache_gc_email_me' " . checked( $cache_gc_email_me, 1, false ) . " /> " . __( 'Email me when the garbage collection runs.', 'wp-super-cache' ) . "</td></tr>\n";
  1631.         echo "</table>\n";
  1632.         echo "<h4>" . __( 'Garbage Collection', 'wp-super-cache' ) . "</h4>";
  1633.         echo "<ol><li>" . __( '<em>Garbage collection</em> is the simple act of throwing out your garbage. For this plugin that would be old or <em>stale</em> cached files that may be out of date. New cached files are described as <em>fresh</em>.', 'wp-super-cache' ) . "</li>\n";
  1634.         echo "<li>" . __( 'Cached files are fresh for a limited length of time. You can set that time in the <em>Cache Timeout</em> text box on this page.', 'wp-super-cache' ) . "</li>\n";
  1635.         echo "<li>" . __( 'Stale cached files are not removed as soon as they become stale. They have to be removed by the garbage collecter. That is why you have to tell the plugin when the garbage collector should run.', 'wp-super-cache' ) . "</li>\n";
  1636.         echo "<li>" . __( 'Use the <em>Timer</em> or <em>Clock</em> schedulers to define when the garbage collector should run.', 'wp-super-cache' ) . "</li>\n";
  1637.         echo "<li>" . __( 'The <em>Timer</em> scheduler tells the plugin to run the garbage collector at regular intervals. When one garbage collection is done, the next run is scheduled.', 'wp-super-cache' ) . "</li>\n";
  1638.         echo "<li>" . __( 'Or, the <em>Clock</em> scheduler allows the garbage collection to run at specific times. If set to run hourly or twicedaily the garbage collector will be first scheduled for the time you enter here. It will then run again at the indicated interval. If set to run daily it will run once a day at the time specified.', 'wp-super-cache' ) . "</li>\n";
  1639.         echo "</ol>";
  1640.         echo "<p>" . __( 'There are no best garbage collection settings but here are a few scenarios. Garbage collection is separate to other actions that clear our cached files like leaving a comment or publishing a post.', 'wp-super-cache' ) . "</p>\n";
  1641.         echo "<ol>";
  1642.         echo "<li>" . __( 'Sites that want to serve lots of newly generated data should set the <em>Cache Timeout</em> to 60 and use the <em>Timer</em> scheduler set to 90 seconds.', 'wp-super-cache' ) . "</li>\n";
  1643.         echo "<li>" . __( 'Sites with widgets and rss feeds in their sidebar should probably use a timeout of 3600 seconds and set the timer to 600 seconds. Stale files will be caught within 10 minutes of going stale.', 'wp-super-cache' ) . "</li>\n";
  1644.         echo "<li>" . __( 'Sites with lots of static content, no widgets or rss feeds in their sidebar can use a timeout of 86400 seconds or even more and set the timer to something equally long.', 'wp-super-cache' ) . "</li>\n";
  1645.         echo "<li>" . __( 'Sites where an external data source updates at a particular time every day should set the timeout to 86400 seconds and use the Clock scheduler set appropriately.', 'wp-super-cache' ) . "</li>\n";
  1646.         echo "</ol>";
  1647.         echo "<p>" . __( 'Checking for and deleting expired files is expensive, but it&#8217;s expensive leaving them there too. On a very busy site you should set the expiry time to <em>600 seconds</em>. Experiment with different values and visit this page to see how many expired files remain at different times during the day. If you are using legacy caching aim to have less than 500 cached files if possible. You can have many times more cached files when using mod_rewrite or PHP caching.', 'wp-super-cache' ) . "</p>";
  1648.         echo "<p>" . __( 'Set the expiry time to 0 seconds to disable garbage collection.', 'wp-super-cache' ) . "</p>";
  1649.         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Change Expiration', 'wp-super-cache' ) . ' &raquo;" /></div>';
  1650.         wp_nonce_field('wp-cache');
  1651.         echo "</form>\n";
  1652.         ?></fieldset><?php
  1653. }
  1654.  
  1655. function wp_cache_sanitize_value($text, & $array) {
  1656.         $text = esc_html(strip_tags($text));
  1657.         $array = preg_split("/[\s,]+/", chop($text));
  1658.         $text = var_export($array, true);
  1659.         $text = preg_replace('/[\s]+/', ' ', $text);
  1660.         return $text;
  1661. }
  1662.  
  1663. // from tehjosh at gamingg dot net http://uk2.php.net/manual/en/function.apache-request-headers.php#73964
  1664. // fixed bug in second substr()
  1665. if( !function_exists('apache_request_headers') ) {
  1666.         function apache_request_headers() {
  1667.                 $headers = array();
  1668.                 foreach(array_keys($_SERVER) as $skey) {
  1669.                         if(substr($skey, 0, 5) == "HTTP_") {
  1670.                                 $headername = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($skey, 5)))));
  1671.                                 $headers[$headername] = $_SERVER[$skey];
  1672.                         }
  1673.                 }
  1674.                 return $headers;
  1675.         }
  1676. }
  1677.  
  1678. function wp_cache_edit_rejected_ua() {
  1679.         global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;
  1680.  
  1681.         if ( !function_exists( 'apache_request_headers' ) ) return;
  1682.  
  1683.         if ( isset( $_POST[ 'wp_rejected_user_agent' ] ) && $valid_nonce ) {
  1684.                 $_POST[ 'wp_rejected_user_agent' ] = str_replace( ' ', '___', $_POST[ 'wp_rejected_user_agent' ] );
  1685.                 $text = str_replace( '___', ' ', wp_cache_sanitize_value( $_POST[ 'wp_rejected_user_agent' ], $cache_rejected_user_agent ) );
  1686.                 wp_cache_replace_line( '^ *\$cache_rejected_user_agent', "\$cache_rejected_user_agent = $text;", $wp_cache_config_file );
  1687.                 foreach( $cache_rejected_user_agent as $k => $ua ) {
  1688.                         $cache_rejected_user_agent[ $k ] = str_replace( '___', ' ', $ua );
  1689.                 }
  1690.                 reset( $cache_rejected_user_agent );
  1691.         }
  1692.  
  1693.         echo '<a name="useragents"></a><fieldset class="options"><h3>' . __( 'Rejected User Agents', 'wp-super-cache' ) . '</h3>';
  1694.         echo "<p>" . __( 'Strings in the HTTP &#8217;User Agent&#8217; header that prevent WP-Cache from caching bot, spiders, and crawlers&#8217; requests. Note that super cached files are still sent to these agents if they already exists.', 'wp-super-cache' ) . "</p>\n";
  1695.         echo '<form name="wp_edit_rejected_user_agent" action="#useragents" method="post">';
  1696.         echo '<textarea name="wp_rejected_user_agent" cols="40" rows="4" style="width: 50%; font-size: 12px;" class="code">';
  1697.         foreach( $cache_rejected_user_agent as $ua ) {
  1698.                 echo esc_html( $ua ) . "\n";
  1699.         }
  1700.         echo '</textarea> ';
  1701.         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save UA Strings', 'wp-super-cache' ) . ' &raquo;" /></div>';
  1702.         wp_nonce_field('wp-cache');
  1703.         echo '</form>';
  1704.         echo "</fieldset>\n";
  1705. }
  1706.  
  1707. function wp_cache_edit_rejected_pages() {
  1708.         global $wp_cache_config_file, $valid_nonce, $wp_cache_pages;
  1709.  
  1710.         if ( isset( $_POST[ 'wp_edit_rejected_pages' ] ) && $valid_nonce ) {
  1711.                 $pages = array( 'single', 'pages', 'archives', 'tag', 'frontpage', 'home', 'category', 'feed', 'author', 'search' );
  1712.                 foreach( $pages as $page ) {
  1713.                         if ( isset( $_POST[ 'wp_cache_pages' ][ $page ] ) ) {
  1714.                                 $value = 1;
  1715.                         } else {
  1716.                                 $value = 0;
  1717.                         }
  1718.                         wp_cache_replace_line('^ *\$wp_cache_pages\[ "' . $page . '" \]', "\$wp_cache_pages[ \"{$page}\" ] = $value;", $wp_cache_config_file);
  1719.                         $wp_cache_pages[ $page ] = $value;
  1720.                 }
  1721.         }
  1722.  
  1723.         echo '<a name="rejectpages"></a>';
  1724.         echo '<p>' . __( 'Do not cache the following page types. See the <a href="http://codex.wordpress.org/Conditional_Tags">Conditional Tags</a> documentation for a complete discussion on each type.', 'wp-super-cache' ) . '</p>';
  1725.         echo '<form name="wp_edit_rejected_pages" action="#rejectpages" method="post">';
  1726.         echo '<input type="hidden" name="wp_edit_rejected_pages" value="1" />';
  1727.         echo '<label><input type="checkbox" value="1" name="wp_cache_pages[single]" ' . checked( 1, $wp_cache_pages[ 'single' ], false ) . ' /> ' . __( 'Single Posts', 'wp-super-cache' ) . ' (is_single)</label><br />';
  1728.         echo '<label><input type="checkbox" value="1" name="wp_cache_pages[pages]" ' . checked( 1, $wp_cache_pages[ 'pages' ], false ) . ' /> ' . __( 'Pages', 'wp-super-cache' ) . ' (is_page)</label><br />';
  1729.         echo '<label><input type="checkbox" value="1" name="wp_cache_pages[frontpage]" ' . checked( 1, $wp_cache_pages[ 'frontpage' ], false ) . ' /> ' . __( 'Front Page', 'wp-super-cache' ) . ' (is_front_page)</label><br />';
  1730.         echo '&nbsp;&nbsp;<label><input type="checkbox" value="1" name="wp_cache_pages[home]" ' . checked( 1, $wp_cache_pages[ 'home' ], false ) . ' /> ' . __( 'Home', 'wp-super-cache' ) . ' (is_home)</label><br />';
  1731.         echo '<label><input type="checkbox" value="1" name="wp_cache_pages[archives]" ' . checked( 1, $wp_cache_pages[ 'archives' ], false ) . ' /> ' . __( 'Archives', 'wp-super-cache' ) . ' (is_archive)</label><br />';
  1732.         echo '&nbsp;&nbsp;<label><input type="checkbox" value="1" name="wp_cache_pages[tag]" ' . checked( 1, $wp_cache_pages[ 'tag' ], false ) . ' /> ' . __( 'Tags', 'wp-super-cache' ) . ' (is_tag)</label><br />';
  1733.         echo '&nbsp;&nbsp;<label><input type="checkbox" value="1" name="wp_cache_pages[category]" ' . checked( 1, $wp_cache_pages[ 'category' ], false ) . ' /> ' . __( 'Category', 'wp-super-cache' ) . ' (is_category)</label><br />';
  1734.         echo '<label><input type="checkbox" value="1" name="wp_cache_pages[feed]" ' . checked( 1, $wp_cache_pages[ 'feed' ], false ) . ' /> ' . __( 'Feeds', 'wp-super-cache' ) . ' (is_feed)</label><br />';
  1735.         echo '<label><input type="checkbox" value="1" name="wp_cache_pages[search]" ' . checked( 1, $wp_cache_pages[ 'search' ], false ) . ' /> ' . __( 'Search Pages', 'wp-super-cache' ) . ' (is_search)</label><br />';
  1736.         echo '<label><input type="checkbox" value="1" name="wp_cache_pages[author]" ' . checked( 1, $wp_cache_pages[ 'author' ], false ) . ' /> ' . __( 'Author Pages', 'wp-super-cache' ) . ' (is_author)</label><br />';
  1737.  
  1738.         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save' ) . ' &raquo;" /></div>';
  1739.         wp_nonce_field('wp-cache');
  1740.         echo "</form>\n";
  1741.  
  1742. }
  1743.  
  1744. function wp_cache_edit_rejected() {
  1745.         global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
  1746.  
  1747.         if(isset($_REQUEST['wp_rejected_uri']) && $valid_nonce) {
  1748.                 $text = wp_cache_sanitize_value( str_replace( '\\\\', '\\', $_REQUEST['wp_rejected_uri'] ), $cache_rejected_uri );
  1749.                 wp_cache_replace_line('^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file);
  1750.         }
  1751.  
  1752.  
  1753.         echo '<a name="rejecturi"></a>';
  1754.         echo '<form name="wp_edit_rejected" action="#rejecturi" method="post">';
  1755.         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&#8217;s enough to specify the year, i.e. &#8217;/2004/&#8217;. WP-Cache will search if that string is part of the URI and if so, it will not cache that page.', 'wp-super-cache' ) . "</p>\n";
  1756.         echo '<textarea name="wp_rejected_uri" cols="40" rows="4" style="width: 50%; font-size: 12px;" class="code">';
  1757.         foreach ($cache_rejected_uri as $file) {
  1758.                 echo esc_html( $file ) . "\n";
  1759.         }
  1760.         echo '</textarea> ';
  1761.         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save Strings', 'wp-super-cache' ) . ' &raquo;" /></div>';
  1762.         wp_nonce_field('wp-cache');
  1763.         echo "</form>\n";
  1764. }
  1765.  
  1766. function wp_cache_edit_accepted() {
  1767.         global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
  1768.  
  1769.         if(isset($_REQUEST['wp_accepted_files']) && $valid_nonce) {
  1770.                 $text = wp_cache_sanitize_value($_REQUEST['wp_accepted_files'], $cache_acceptable_files);
  1771.                 wp_cache_replace_line('^ *\$cache_acceptable_files', "\$cache_acceptable_files = $text;", $wp_cache_config_file);
  1772.         }
  1773.  
  1774.  
  1775.         echo '<a name="cancache"></a>';
  1776.         echo '<div style="clear:both"></div><form name="wp_edit_accepted" action="#cancache" method="post">';
  1777.         echo "<p>" . __( 'Add here those filenames that can be cached, even if they match one of the rejected substring specified above.', 'wp-super-cache' ) . "</p>\n";
  1778.         echo '<textarea name="wp_accepted_files" cols="40" rows="8" style="width: 50%; font-size: 12px;" class="code">';
  1779.         foreach ($cache_acceptable_files as $file) {
  1780.                 echo esc_html($file) . "\n";
  1781.         }
  1782.         echo '</textarea> ';
  1783.         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save Files', 'wp-super-cache' ) . ' &raquo;" /></div>';
  1784.         wp_nonce_field('wp-cache');
  1785.         echo "</form>\n";
  1786. }
  1787.  
  1788. function wp_cache_debug_settings() {
  1789.         global $wp_super_cache_debug, $wp_cache_debug_log, $wp_cache_debug_level, $wp_cache_debug_ip, $cache_path, $valid_nonce, $wp_cache_config_file;
  1790.         global $wp_super_cache_front_page_check, $wp_super_cache_front_page_clear, $wp_super_cache_front_page_text, $wp_super_cache_front_page_notification, $wp_super_cache_advanced_debug;
  1791.  
  1792.         if ( !isset( $wp_cache_debug_level ) )
  1793.                 $wp_cache_debug_level = 1;
  1794.         if ( isset( $_POST[ 'wp_cache_debug' ] ) && $valid_nonce ) {
  1795.                 $wp_super_cache_debug = intval( $_POST[ 'wp_super_cache_debug' ] );
  1796.                 wp_cache_replace_line('^ *\$wp_super_cache_debug', "\$wp_super_cache_debug = '$wp_super_cache_debug';", $wp_cache_config_file);
  1797.                 if ( $wp_super_cache_debug && ( ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log == '' ) || !isset( $wp_cache_debug_log ) ) ) {
  1798.                         $wp_cache_debug_log = md5( time() ) . ".txt";
  1799.                 } else {
  1800.                         $wp_cache_debug_log = "";
  1801.                 }
  1802.                 wp_cache_replace_line('^ *\$wp_cache_debug_log', "\$wp_cache_debug_log = '$wp_cache_debug_log';", $wp_cache_config_file);
  1803.                 $wp_cache_debug_ip = esc_html( $_POST[ 'wp_cache_debug_ip' ] );
  1804.                 wp_cache_replace_line('^ *\$wp_cache_debug_ip', "\$wp_cache_debug_ip = '$wp_cache_debug_ip';", $wp_cache_config_file);
  1805.                 $wp_cache_debug_level = (int)$_POST[ 'wp_cache_debug_level' ];
  1806.                 wp_cache_replace_line('^ *\$wp_cache_debug_level', "\$wp_cache_debug_level = '$wp_cache_debug_level';", $wp_cache_config_file);
  1807.                 $wp_super_cache_front_page_check = (int)$_POST[ 'wp_super_cache_front_page_check' ];
  1808.                 wp_cache_replace_line('^ *\$wp_super_cache_front_page_check', "\$wp_super_cache_front_page_check = '$wp_super_cache_front_page_check';", $wp_cache_config_file);
  1809.                 $wp_super_cache_front_page_clear = (int)$_POST[ 'wp_super_cache_front_page_clear' ];
  1810.                 wp_cache_replace_line('^ *\$wp_super_cache_front_page_clear', "\$wp_super_cache_front_page_clear = '$wp_super_cache_front_page_clear';", $wp_cache_config_file);
  1811.                 $wp_super_cache_front_page_text = esc_html( $_POST[ 'wp_super_cache_front_page_text' ] );
  1812.                 wp_cache_replace_line('^ *\$wp_super_cache_front_page_text', "\$wp_super_cache_front_page_text = '$wp_super_cache_front_page_text';", $wp_cache_config_file);
  1813.                 $wp_super_cache_front_page_notification = (int)$_POST[ 'wp_super_cache_front_page_notification' ];
  1814.                 wp_cache_replace_line('^ *\$wp_super_cache_front_page_notification', "\$wp_super_cache_front_page_notification = '$wp_super_cache_front_page_notification';", $wp_cache_config_file);
  1815.                 if ( isset( $wp_super_cache_front_page_check ) && $wp_super_cache_front_page_check == 1 && !wp_next_scheduled( 'wp_cache_check_site_hook' ) ) {
  1816.                         wp_schedule_single_event( time() + 360 , 'wp_cache_check_site_hook' );
  1817.                         if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'scheduled wp_cache_check_site_hook for 360 seconds time.', 2 );
  1818.                 }
  1819.         }
  1820.  
  1821.         echo '<a name="debug"></a>';
  1822.         echo '<fieldset class="options">';
  1823.         if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' )
  1824.                 echo "<p>" . sprintf( __( 'Currently logging to: %s', 'wp-super-cache' ), "<a href='" . site_url( str_replace( ABSPATH, '', "{$cache_path}{$wp_cache_debug_log}" ) ) . "'>$cache_path{$wp_cache_debug_log}</a>" ) . "</p>";
  1825.  
  1826.  
  1827.         echo '<p>' . __( 'Fix problems with the plugin by debugging it here. It can log them to a file in your cache directory.', 'wp-super-cache' ) . '</p>';
  1828.         echo '<div style="clear:both"></div><form name="wp_cache_debug" action="" method="post">';
  1829.         echo "<input type='hidden' name='wp_cache_debug' value='1' /><br />";
  1830.         echo "<table class='form-table'>";
  1831.         echo "<tr><td>" . __( 'Debugging', 'wp-super-cache' ) . "</td><td><input type='checkbox' name='wp_super_cache_debug' value='1' " . checked( 1, $wp_super_cache_debug, false ) . " /> " . __( 'enabled', 'wp-super-cache' ) . "</td></tr>";
  1832.         echo "<tr><td>" . __( 'IP Address', 'wp-super-cache' ) . "</td><td> <input type='text' size='20' name='wp_cache_debug_ip' value='{$wp_cache_debug_ip}' /> " . sprintf( __( '(only log requests from this IP address. Your IP is %s)', 'wp-super-cache' ), $_SERVER[ 'REMOTE_ADDR' ] ) . "</td></tr>";
  1833.         echo "<tr><td>" . __( 'Log level', 'wp-super-cache' ) . "</td><td> ";
  1834.         for( $t = 1; $t <= 5; $t++ ) {
  1835.                 echo "<input type='radio' name='wp_cache_debug_level' value='$t' ";
  1836.                 echo $wp_cache_debug_level == $t ? "checked='checked' " : '';
  1837.                 echo "/> $t ";
  1838.         }
  1839.         echo " " . __( '(1 = less, 5 = more, may cause severe server load.)', 'wp-super-cache' ) . "</td></tr>";
  1840.         echo "</table>\n";
  1841.         if ( isset( $wp_super_cache_advanced_debug ) ) {
  1842.         echo "<h4>" . __( 'Advanced', 'wp-super-cache' ) . "</h4><p>" . __( 'In very rare cases two problems may arise on some blogs:<ol><li> The front page may start downloading as a zip file.</li><li> The wrong page is occasionally cached as the front page if your blog uses a static front page and the permalink structure is <em>/%category%/%postname%/</em>.</li></ol>', 'wp-super-cache' ) . '</p>';
  1843.         echo "<p>" . __( 'I&#8217;m 99% certain that they aren&#8217;t bugs in WP Super Cache and they only happen in very rare cases but you can run a simple check once every 5 minutes to verify that your site is ok if you&#8217;re worried. You will be emailed if there is a problem.', 'wp-super-cache' ) . "</p>";
  1844.         echo "<table class='form-table'>";
  1845.         echo "<tr><td valign='top' colspan='2'><input type='checkbox' name='wp_super_cache_front_page_check' value='1' " . checked( 1, $wp_super_cache_front_page_check, false ) . " /> " . __( 'Check front page every 5 minutes.', 'wp-super-cache' ) . "</td></tr>";
  1846.         echo "<tr><td valign='top'>" . __( 'Front page text', 'wp-super-cache' ) . "</td><td> <input type='text' size='30' name='wp_super_cache_front_page_text' value='{$wp_super_cache_front_page_text}' /> (" . __( 'Text to search for on your front page. If this text is missing the cache will be cleared. Leave blank to disable.', 'wp-super-cache' ) . ")</td></tr>";
  1847.         echo "<tr><td valign='top' colspan='2'><input type='checkbox' name='wp_super_cache_front_page_clear' value='1' " . checked( 1, $wp_super_cache_front_page_clear, false ) . " /> " . __( 'Clear cache on error.', 'wp-super-cache' ) . "</td></tr>";
  1848.         echo "<tr><td valign='top' colspan='2'><input type='checkbox' name='wp_super_cache_front_page_notification' value='1' " . checked( 1, $wp_super_cache_front_page_notification, false ) . " /> " . __( 'Email the blog admin when checks are made. (useful for testing)', 'wp-super-cache' ) . "</td></tr>";
  1849.        
  1850.         echo "</table>\n";
  1851.         }
  1852.         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save', 'wp-super-cache' ) . ' &raquo;" /></div>';
  1853.         wp_nonce_field('wp-cache');
  1854.         echo "</form>\n";
  1855.         echo '</fieldset>';
  1856. }
  1857.  
  1858. function wp_cache_enable() {
  1859.         global $wp_cache_config_file, $cache_enabled, $supercachedir;
  1860.  
  1861.         if(get_option('gzipcompression')) {
  1862.                 echo "<strong>" . __( 'Error: GZIP compression is enabled, disable it if you want to enable wp-cache.', 'wp-super-cache' ) . "</strong>";
  1863.                 return false;
  1864.         }
  1865.         if( wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = true;', $wp_cache_config_file) ) {
  1866.                 $cache_enabled = true;
  1867.         }
  1868.         wp_super_cache_enable();
  1869. }
  1870.  
  1871. function wp_cache_disable() {
  1872.         global $wp_cache_config_file, $cache_enabled;
  1873.  
  1874.         if (wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = false;', $wp_cache_config_file)) {
  1875.                 $cache_enabled = false;
  1876.         }
  1877. }
  1878. function wp_super_cache_enable() {
  1879.         global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
  1880.  
  1881.         if( is_dir( $supercachedir . ".disabled" ) )
  1882.                 if( is_dir( $supercachedir ) ) {
  1883.                         prune_super_cache( $supercachedir . ".disabled", true );
  1884.                         @unlink( $supercachedir . ".disabled" );
  1885.                 } else {
  1886.                         @rename( $supercachedir . ".disabled", $supercachedir );
  1887.                 }
  1888.         wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = true;', $wp_cache_config_file);
  1889.         $super_cache_enabled = true;
  1890. }
  1891.  
  1892. function wp_super_cache_disable() {
  1893.         global $cache_path, $supercachedir, $wp_cache_config_file, $super_cache_enabled;
  1894.  
  1895.         wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file);
  1896.         if( is_dir( $supercachedir ) )
  1897.                 @rename( $supercachedir, $supercachedir . ".disabled" );
  1898.         $super_cache_enabled = false;
  1899.         sleep( 1 ); // allow existing processes to write to the supercachedir and then delete it
  1900.         if (function_exists ('prune_super_cache') && is_dir( $supercachedir ) ) {
  1901.                 prune_super_cache( $cache_path, true );
  1902.         }
  1903. }
  1904.  
  1905. function wp_cache_is_enabled() {
  1906.         global $wp_cache_config_file;
  1907.  
  1908.         if(get_option('gzipcompression')) {
  1909.                 echo "<strong>" . __( 'Warning', 'wp-super-cache' ) . "</strong>: " . __( "GZIP compression is enabled in WordPress, wp-cache will be bypassed until you disable gzip compression.", 'wp-super-cache' );
  1910.                 return false;
  1911.         }
  1912.         $lines = file($wp_cache_config_file);
  1913.         foreach($lines as $line) {
  1914.                 if (preg_match('/^ *\$cache_enabled *= *true *;/', $line))
  1915.                         return true;
  1916.         }
  1917.         return false;
  1918. }
  1919.  
  1920.  
  1921. function wp_cache_replace_line($old, $new, $my_file) {
  1922.         if ( @is_file( $my_file ) == false ) {
  1923.                 return false;
  1924.         }
  1925.         if (!is_writeable_ACLSafe($my_file)) {
  1926.                 echo "Error: file $my_file is not writable.\n";
  1927.                 return false;
  1928.         }
  1929.  
  1930.         $found = false;
  1931.         $lines = file($my_file);
  1932.         foreach( (array)$lines as $line ) {
  1933.                 if ( preg_match("/$old/", $line)) {
  1934.                         $found = true;
  1935.                         break;
  1936.                 }
  1937.         }
  1938.         if ($found) {
  1939.                 $fd = fopen($my_file, 'w');
  1940.                 foreach( (array)$lines as $line ) {
  1941.                         if ( !preg_match("/$old/", $line))
  1942.                                 fputs($fd, $line);
  1943.                         else {
  1944.                                 fputs($fd, "$new //Added by WP-Cache Manager\n");
  1945.                         }
  1946.                 }
  1947.                 fclose($fd);
  1948.                 return true;
  1949.         }
  1950.         $fd = fopen($my_file, 'w');
  1951.         $done = false;
  1952.         foreach( (array)$lines as $line ) {
  1953.                 if ( $done || !preg_match('/^(if\ \(\ \!\ )?define|\$|\?>/', $line) ) {
  1954.                         fputs($fd, $line);
  1955.                 } else {
  1956.                         fputs($fd, "$new //Added by WP-Cache Manager\n");
  1957.                         fputs($fd, $line);
  1958.                         $done = true;
  1959.                 }
  1960.         }
  1961.         fclose($fd);
  1962.         return true;
  1963. }
  1964.  
  1965. function wp_cache_verify_cache_dir() {
  1966.         global $cache_path, $blog_cache_dir, $blogcacheid;
  1967.  
  1968.         $dir = dirname($cache_path);
  1969.         if ( !file_exists($cache_path) ) {
  1970.                 if ( !is_writeable_ACLSafe( $dir ) || !($dir = mkdir( $cache_path ) ) ) {
  1971.                                 echo "<strong>" . __( 'Error', 'wp-super-cache' ) . ":</strong> " . sprintf( __( 'Your cache directory (<strong>%1$s</strong>) did not exist and couldn&#8217;t be created by the web server. Check %1$s permissions.', 'wp-super-cache' ), $dir );
  1972.                                 return false;
  1973.                 }
  1974.         }
  1975.         if ( !is_writeable_ACLSafe($cache_path)) {
  1976.                 echo "<strong>" . __( 'Error', 'wp-super-cache' ) . ":</strong> " . sprintf( __( 'Your cache directory (<strong>%1$s</strong>) or <strong>%2$s</strong> need to be writable for this plugin to work. Double-check it.', 'wp-super-cache' ), $cache_path, $dir );
  1977.                 return false;
  1978.         }
  1979.  
  1980.         if ( '/' != substr($cache_path, -1)) {
  1981.                 $cache_path .= '/';
  1982.         }
  1983.  
  1984.         if( false == is_dir( $blog_cache_dir ) ) {
  1985.                 @mkdir( $cache_path . "blogs" );
  1986.                 if( $blog_cache_dir != $cache_path . "blogs/" )
  1987.                         @mkdir( $blog_cache_dir );
  1988.         }
  1989.  
  1990.         if( false == is_dir( $blog_cache_dir . 'meta' ) )
  1991.                 @mkdir( $blog_cache_dir . 'meta' );
  1992.  
  1993.         return true;
  1994. }
  1995.  
  1996. function wp_cache_verify_config_file() {
  1997.         global $wp_cache_config_file, $wp_cache_config_file_sample, $sem_id, $cache_path;
  1998.  
  1999.         $new = false;
  2000.         $dir = dirname($wp_cache_config_file);
  2001.  
  2002.         if ( file_exists($wp_cache_config_file) ) {
  2003.                 $lines = join( ' ', file( $wp_cache_config_file ) );
  2004.                 if( strpos( $lines, 'WPCACHEHOME' ) === false ) {
  2005.                         if( is_writeable_ACLSafe( $wp_cache_config_file ) ) {
  2006.                                 @unlink( $wp_cache_config_file );
  2007.                         } else {
  2008.                                 echo "<strong>" . __( 'Error', 'wp-super-cache' ) . ":</strong> " . sprintf( __( 'Your WP-Cache config file (<strong>%s</strong>) is out of date and not writable by the Web server.Please delete it and refresh this page.', 'wp-super-cache' ), $wp_cache_config_file );
  2009.                                 return false;
  2010.                         }
  2011.                 }
  2012.         } elseif( !is_writeable_ACLSafe($dir)) {
  2013.                 echo "<strong>" . __( 'Error', 'wp-super-cache' ) . ":</strong> " . sprintf( __( 'Configuration file missing and %1$s  directory (<strong>%2$s</strong>) is not writable by the Web server.Check its permissions.', 'wp-super-cache' ), WP_CONTENT_DIR, $dir );
  2014.                 return false;
  2015.         }
  2016.  
  2017.         if ( !file_exists($wp_cache_config_file) ) {
  2018.                 if ( !file_exists($wp_cache_config_file_sample) ) {
  2019.                         echo "<strong>" . __( 'Error', 'wp-super-cache' ) . ":</strong> " . sprintf( __( 'Sample WP-Cache config file (<strong>%s</strong>) does not exist.Verify you installation.', 'wp-super-cache' ), $wp_cache_config_file_sample );
  2020.                         return false;
  2021.                 }
  2022.                 copy($wp_cache_config_file_sample, $wp_cache_config_file);
  2023.                 $dir = str_replace( str_replace( '\\', '/', WP_CONTENT_DIR ), '', str_replace( '\\', '/', dirname(__FILE__) ) );
  2024.                 if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
  2025.                         wp_cache_replace_line('define\(\ \'WPCACHEHOME', "\tdefine( 'WPCACHEHOME', WP_CONTENT_DIR . \"{$dir}/\" );", $wp_cache_config_file);
  2026.                 } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
  2027.                         wp_cache_replace_line('define\(\ \'WPCACHEHOME', "\tdefine( 'WPCACHEHOME', WP_CONTENT_DIR . \"{$dir}/wp-super-cache/\" );", $wp_cache_config_file);
  2028.                 }
  2029.                 $new = true;
  2030.         }
  2031.         if( $sem_id == 5419 && $cache_path != '' ) {
  2032.                 $sem_id = crc32( $_SERVER[ 'SERVER_NAME' ] . $cache_path ) & 0x7fffffff;
  2033.                 wp_cache_replace_line('sem_id', '$sem_id = ' . $sem_id . ';', $wp_cache_config_file);
  2034.         }
  2035.         require($wp_cache_config_file);
  2036.         return true;
  2037. }
  2038.  
  2039. function wp_cache_create_advanced_cache() {
  2040.         global $wp_cache_link, $wp_cache_file;
  2041.         $ret = true;
  2042.  
  2043.         $file = file_get_contents( $wp_cache_file );
  2044.         $file = str_replace( 'CACHEHOME', constant( 'WPCACHEHOME' ), $file );
  2045.         $fp = @fopen( $wp_cache_link, 'w' );
  2046.         if( $fp ) {
  2047.                 fputs( $fp, $file );
  2048.                 fclose( $fp );
  2049.         } else {
  2050.                 $ret = false;
  2051.         }
  2052.         return $ret;
  2053. }
  2054.  
  2055. function wp_cache_check_link() {
  2056.         global $wp_cache_link, $wp_cache_file;
  2057.  
  2058.         $ret = true;
  2059.         if( file_exists($wp_cache_link) ) {
  2060.                 $file = file_get_contents( $wp_cache_link );
  2061.                 if( strpos( $file, "WP SUPER CACHE 0.8.9.1" ) ) {
  2062.                         return true;
  2063.                 } else {
  2064.                         if( !@unlink($wp_cache_link) ) {
  2065.                                 $ret = false;
  2066.                         } else {
  2067.                                 $ret = wp_cache_create_advanced_cache();
  2068.                         }
  2069.                 }
  2070.         } else {
  2071.                 $ret = wp_cache_create_advanced_cache();
  2072.         }
  2073.  
  2074.         if( false == $ret ) {
  2075.                 echo '<div id="message" class="updated fade"><h3>' . __( 'Warning', 'wp-super-cache' ) . "! <em>" . sprintf( __( '%s/advanced-cache.php</em> does not exist or cannot be updated.', 'wp-super-cache' ), WP_CONTENT_DIR ) . "</h3>";
  2076.                 echo "<p><ul><li>" . __( '1. If it already exists please delete the file first.', 'wp-super-cache' ) . "</li>";
  2077.                 echo "<li>" . sprintf( __( '2. Make %1$s writable using the chmod command through your ftp or server software. (<em>chmod 777 %1$s</em>) and refresh this page. This is only a temporary measure and you&#8217;ll have to make it read only afterwards again. (Change 777 to 755 in the previous command)', 'wp-super-cache' ), WP_CONTENT_DIR ) . "</li>";
  2078.                 echo "<li>" . sprintf( __( '3. Refresh this page to update <em>%s/advanced-cache.php</em>', 'wp-super-cache' ), WP_CONTENT_DIR ) . "</li></ul>";
  2079.                 echo sprintf( __( 'If that doesn&#8217;t work, make sure the file <em>%s/advanced-cache.php</em> doesn&#8217;t exist:', 'wp-super-cache' ), WP_CONTENT_DIR ) . "<ol>";
  2080.                 printf( __( '<li>1. Open <em>%1$s$wp_cache_file</em> in a text editor.</li><li>2. Change the text <em>CACHEHOME</em> to <em>%2$s</em></li><li>3. Save the file and copy it to <em>%3$s</em> and refresh this page.</li>', 'wp-super-cache' ), $wp_cache_file, WPCACHEHOME, $wp_cache_link );
  2081.                 echo "</div>";
  2082.                 return false;
  2083.         }
  2084.         return true;
  2085. }
  2086.  
  2087. function wp_cache_check_global_config() {
  2088.         global $wp_cache_check_wp_config;
  2089.  
  2090.         if ( !isset( $wp_cache_check_wp_config ) )
  2091.                 return true;
  2092.  
  2093.  
  2094.         if ( file_exists( ABSPATH . 'wp-config.php') ) {
  2095.                 $global = ABSPATH . 'wp-config.php';
  2096.         } else {
  2097.                 $global = dirname(ABSPATH) . '/wp-config.php';
  2098.         }
  2099.  
  2100.         $line = 'define(\'WP_CACHE\', true);';
  2101.         if (!is_writeable_ACLSafe($global) || !wp_cache_replace_line('define *\( *\'WP_CACHE\'', $line, $global) ) {
  2102.                 if ( defined( 'WP_CACHE' ) && constant( 'WP_CACHE' ) == false ) {
  2103.                         echo '<div id="message" class="updated fade">' . __( "<h3>WP_CACHE constant set to false</h3><p>The WP_CACHE constant is used by WordPress to load the code that serves cached pages. Unfortunately it is set to false. Please edit your wp-config.php and add or edit the following line above the final require_once command:<br /><br /><code>define('WP_CACHE', true);</code></p>", 'wp-super-cache' ) . "</div>";
  2104.                 } else {
  2105.                         echo "<p>" . __( "<strong>Error: WP_CACHE is not enabled</strong> in your <code>wp-config.php</code> file and I couldn&#8217;t modify it.", 'wp-super-cache' ) . "</p>";;
  2106.                         echo "<p>" . sprintf( __( "Edit <code>%s</code> and add the following line:<br /> <code>define('WP_CACHE', true);</code><br />Otherwise, <strong>WP-Cache will not be executed</strong> by WordPress core. ", 'wp-super-cache' ), $global ) . "</p>";
  2107.                 }
  2108.                 return false;
  2109.         }  else {
  2110.                 echo "<div style='border: 1px solid #333; background: #ffffaa; padding: 2px;'>" . __( '<h3>WP_CACHE constant added to wp-config.php</h3><p>If you continue to see this warning message please see point 5 of the <a href="http://wordpress.org/extend/plugins/wp-super-cache/faq/">Troubleshooting Guide</a>. The WP_CACHE line must be moved up.', 'wp-super-cache' ) . "</p></div>";
  2111.         }
  2112.         return true;
  2113. }
  2114.  
  2115. function wp_cache_files() {
  2116.         global $cache_path, $file_prefix, $cache_max_time, $valid_nonce, $supercachedir, $cache_enabled, $super_cache_enabled, $blog_cache_dir, $cache_compression;
  2117.         global $wp_cache_object_cache, $wp_cache_preload_on;
  2118.  
  2119.         if ( '/' != substr($cache_path, -1)) {
  2120.                 $cache_path .= '/';
  2121.         }
  2122.  
  2123.         if ( $valid_nonce ) {
  2124.                 if(isset($_REQUEST['wp_delete_cache'])) {
  2125.                         wp_cache_clean_cache($file_prefix);
  2126.                         $_GET[ 'action' ] = 'regenerate_cache_stats';
  2127.                 }
  2128.                 if ( isset( $_REQUEST[ 'wp_delete_all_cache' ] ) ) {
  2129.                         wp_cache_clean_cache( $file_prefix, true );
  2130.                         $_GET[ 'action' ] = 'regenerate_cache_stats';
  2131.                 }
  2132.                 if(isset($_REQUEST['wp_delete_expired'])) {
  2133.                         wp_cache_clean_expired($file_prefix);
  2134.                         $_GET[ 'action' ] = 'regenerate_cache_stats';
  2135.                 }
  2136.         }
  2137.         echo "<a name='listfiles'></a>";
  2138.         echo '<fieldset class="options" id="show-this-fieldset"><h3>' . __( 'Cache Contents', 'wp-super-cache' ) . '</h3>';
  2139.  
  2140.         if ( $wp_cache_object_cache ) {
  2141.                 echo "<p>" . __( "Object cache in use. No cache listing available.", 'wp-super-cache' ) . "</p>";
  2142.                 wp_cache_delete_buttons();
  2143.                 echo "</fieldset>";
  2144.                 return false;
  2145.         }
  2146.  
  2147.         $cache_stats = get_option( 'supercache_stats' );
  2148.         if ( !is_array( $cache_stats ) || ( isset( $_GET[ 'listfiles' ] ) ) || ( $valid_nonce && array_key_exists('action', $_GET) && $_GET[ 'action' ] == 'regenerate_cache_stats' ) ) {
  2149.         $list_files = false; // it doesn't list supercached files, and removing single pages is buggy
  2150.         $count = 0;
  2151.         $expired = 0;
  2152.         $now = time();
  2153.         if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) {
  2154.                 $wp_cache_fsize = 0;
  2155.                 if ( $valid_nonce && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'deletewpcache' ) {
  2156.                         $deleteuri = preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', base64_decode( $_GET[ 'uri' ] ) ) ) ) );
  2157.                         $deleteuri = str_replace( '\\', '', $deleteuri );
  2158.                 } else {
  2159.                         $deleteuri = '';
  2160.                 }
  2161.  
  2162.                 if ( $valid_nonce && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'deletesupercache' ) {
  2163.                         $supercacheuri = preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', base64_decode( $_GET[ 'uri' ] ) ) ) ) );
  2164.                         $supercacheuri = trailingslashit( str_replace( '\\', '', $supercacheuri ) );
  2165.                         printf( __( "Deleting supercache file: <strong>%s</strong><br />", 'wp-super-cache' ), $supercacheuri );
  2166.                         @unlink( $cache_path . 'supercache/' . $supercacheuri . 'index.html' );
  2167.                         @unlink( $cache_path . 'supercache/' . $supercacheuri . 'index.html.gz' );
  2168.                         prune_super_cache( $cache_path . 'supercache/' . $supercacheuri . 'page', true );
  2169.                         @rmdir( $cache_path . 'supercache/' . $supercacheuri );
  2170.                 }
  2171.                 while( false !== ($file = readdir($handle))) {
  2172.                         if ( preg_match("/^$file_prefix.*\.meta/", $file) ) {
  2173.                                 $content_file = preg_replace("/meta$/", "html", $file);
  2174.                                 $mtime = filemtime( $blog_cache_dir . 'meta/' . $file );
  2175.                                 if ( ! ( $fsize = @filesize( $blog_cache_dir . $content_file ) ) )
  2176.                                         continue; // .meta does not exists
  2177.  
  2178.                                 $age = $now - $mtime;
  2179.                                 if ( $valid_nonce && $_GET[ 'listfiles' ] ) {
  2180.                                         $meta = unserialize( file_get_contents( $blog_cache_dir . 'meta/' . $file ) );
  2181.                                         if ( $deleteuri != '' && $meta[ 'uri' ] == $deleteuri ) {
  2182.                                                 printf( __( "Deleting wp-cache file: <strong>%s</strong><br />", 'wp-super-cache' ), $deleteuri );
  2183.                                                 @unlink( $blog_cache_dir . 'meta/' . $file );
  2184.                                                 @unlink( $blog_cache_dir . $content_file );
  2185.                                                 continue;
  2186.                                         }
  2187.                                         $meta[ 'age' ] = $age;
  2188.                                         if ( $cache_max_time > 0 && $age > $cache_max_time ) {
  2189.                                                 $expired_list[ $age ][] = $meta;
  2190.                                         } else {
  2191.                                                 $cached_list[ $age ][] = $meta;
  2192.                                         }
  2193.                                 }
  2194.  
  2195.                                 if ( $cache_max_time > 0 && $age > $cache_max_time ) {
  2196.                                         $expired++;
  2197.                                 } else {
  2198.                                         $count++;
  2199.                                 }
  2200.                                 $wp_cache_fsize += $fsize;
  2201.                                 $fsize = intval($fsize/1024);
  2202.                         }
  2203.                 }
  2204.                 closedir($handle);
  2205.         }
  2206.         if( $wp_cache_fsize != 0 ) {
  2207.                 $wp_cache_fsize = $wp_cache_fsize/1024;
  2208.         } else {
  2209.                 $wp_cache_fsize = 0;
  2210.         }
  2211.         if( $wp_cache_fsize > 1024 ) {
  2212.                 $wp_cache_fsize = number_format( $wp_cache_fsize / 1024, 2 ) . "MB";
  2213.         } elseif( $wp_cache_fsize != 0 ) {
  2214.                 $wp_cache_fsize = number_format( $wp_cache_fsize, 2 ) . "KB";
  2215.         } else {
  2216.                 $wp_cache_fsize = '0KB';
  2217.         }
  2218.  
  2219.         // Supercache files
  2220.         $now = time();
  2221.         $sizes = array( 'expired' => 0, 'expired_list' => array(), 'cached' => 0, 'cached_list' => array(), 'ts' => 0 );
  2222.  
  2223.         if (is_dir($supercachedir)) {
  2224.                 if( $dh = opendir( $supercachedir ) ) {
  2225.                         while( ( $entry = readdir( $dh ) ) !== false ) {
  2226.                                 if ($entry != '.' && $entry != '..') {
  2227.                                         $sizes = wpsc_dirsize( trailingslashit( $supercachedir ) . $entry, $sizes );
  2228.                                 }
  2229.                         }
  2230.                         closedir($dh);
  2231.                 }
  2232.         } else {
  2233.                 $filem = @filemtime( $supercachedir );
  2234.                 if ( false == $wp_cache_preload_on && is_file( $supercachedir ) && $cache_max_time > 0 && $filem + $cache_max_time <= $now ) {
  2235.                         $sizes[ 'expired' ] ++;
  2236.                         if ( $valid_nonce && $_GET[ 'listfiles' ] )
  2237.                                 $sizes[ 'expired_list' ][ str_replace( $cache_path . 'supercache/' , '', $supercachedir ) ] = $now - $filem;
  2238.                 } else {
  2239.                         if ( $valid_nonce && $_GET[ 'listfiles' ] && $filem )
  2240.                                 $sizes[ 'cached_list' ][ str_replace( $cache_path . 'supercache/' , '', $supercachedir ) ] = $now - $filem;
  2241.                 }
  2242.         }
  2243.         $sizes[ 'ts' ] = time();
  2244.         $cache_stats = array( 'generated' => time(), 'supercache' => $sizes, 'wpcache' => array( 'cached' => $count, 'expired' => $expired, 'fsize' => $wp_cache_fsize ) );
  2245.         update_option( 'supercache_stats', $cache_stats );
  2246.         } else {
  2247.                 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>";
  2248.                 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>";
  2249.                 if ( is_array( $cache_stats ) ) {
  2250.                         echo "<p>" . sprintf( __( 'Cache stats last generated: %s minutes ago.', 'wp-super-cache' ), number_format( ( time() - $cache_stats[ 'generated' ] ) / 60 ) ) . "</p>";
  2251.                 }
  2252.                 $cache_stats = get_option( 'supercache_stats' );
  2253.         }// regerate stats cache
  2254.  
  2255.         if ( is_array( $cache_stats ) ) {
  2256.                 echo "<p><strong>" . __( 'WP-Cache', 'wp-super-cache' ) . " ({$cache_stats[ 'wpcache' ][ 'fsize' ]})</strong></p>";
  2257.                 echo "<ul><li>" . sprintf( __( '%s Cached Pages', 'wp-super-cache' ), $cache_stats[ 'wpcache' ][ 'cached' ] ) . "</li>";
  2258.                 echo "<li>" . sprintf( __( '%s Expired Pages', 'wp-super-cache' ),    $cache_stats[ 'wpcache' ][ 'expired' ] ) . "</li></ul>";
  2259.                 $divisor = $cache_compression == 1 ? 2 : 1;
  2260.                 if ( array_key_exists('fsize', (array)$cache_stats[ 'supercache' ]) )
  2261.                         $fsize = $cache_stats[ 'supercache' ][ 'fsize' ] / 1024;
  2262.                 else
  2263.                         $fsize = 0;
  2264.                 if( $fsize > 1024 ) {
  2265.                         $fsize = number_format( $fsize / 1024, 2 ) . "MB";
  2266.                 } elseif( $fsize != 0 ) {
  2267.                         $fsize = number_format( $fsize, 2 ) . "KB";
  2268.                 } else {
  2269.                         $fsize = "0KB";
  2270.                 }
  2271.                 echo "<p><strong>" . __( 'WP-Super-Cache', 'wp-super-cache' ) . " ({$fsize})</strong></p>";
  2272.                 echo "<ul><li>" . sprintf( __( '%s Cached Pages', 'wp-super-cache' ), intval( $cache_stats[ 'supercache' ][ 'cached' ] / $divisor ) ) . "</li>";
  2273.                 if (isset($now) && isset($sizes))
  2274.                         $age = intval(($now - $sizes['ts'])/60);
  2275.                 else
  2276.                         $age = 0;
  2277.                 echo "<li>" . sprintf( __( '%s Expired Pages', 'wp-super-cache' ), intval( $cache_stats[ 'supercache' ][ 'expired' ] / $divisor ) ) . "</li></ul>";
  2278.                 if ( $valid_nonce && array_key_exists('listfiles', $_GET) && $_GET[ 'listfiles' ] ) {
  2279.                         echo "<div style='padding: 10px; border: 1px solid #333; height: 400px; width: 90%; overflow: auto'>";
  2280.                         if ( is_array( $cached_list ) && !empty( $cached_list ) ) {
  2281.                                 echo "<h4>" . __( 'Fresh WP-Cached Files', 'wp-super-cache' ) . "</h4>";
  2282.                                 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>";
  2283.                                 $c = 1;
  2284.                                 $flip = 1;
  2285.                                 ksort( $cached_list );
  2286.                                 foreach( $cached_list as $age => $d ) {
  2287.                                         foreach( $d as $details ) {
  2288.                                                 $bg = $flip ? 'style="background: #EAEAEA;"' : '';
  2289.                                                 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";
  2290.                                                 $flip = !$flip;
  2291.                                                 $c++;
  2292.                                         }
  2293.                                 }
  2294.                                 echo "</table>";
  2295.                         }
  2296.                         if ( is_array( $expired_list ) && !empty( $expired_list ) ) {
  2297.                                 echo "<h4>" . __( 'Stale WP-Cached Files', 'wp-super-cache' ) . "</h4>";
  2298.                                 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>";
  2299.                                 $c = 1;
  2300.                                 $flip = 1;
  2301.                                 ksort( $expired_list );
  2302.                                 foreach( $expired_list as $age => $d ) {
  2303.                                         foreach( $d as $details ) {
  2304.                                                 $bg = $flip ? 'style="background: #EAEAEA;"' : '';
  2305.                                                 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";
  2306.                                                 $flip = !$flip;
  2307.                                                 $c++;
  2308.                                         }
  2309.                                 }
  2310.                                 echo "</table>";
  2311.                         }
  2312.                         if ( is_array( $sizes[ 'cached_list' ] ) & !empty( $sizes[ 'cached_list' ] ) ) {
  2313.                                 echo "<h4>" . __( 'Fresh Super Cached Files', 'wp-super-cache' ) . "</h4>";
  2314.                                 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>";
  2315.                                 $c = 1;
  2316.                                 $flip = 1;
  2317.                                 ksort( $sizes[ 'cached_list' ] );
  2318.                                 foreach( $sizes[ 'cached_list' ] as $age => $d ) {
  2319.                                         foreach( $d as $uri => $n ) {
  2320.                                                 $bg = $flip ? 'style="background: #EAEAEA;"' : '';
  2321.                                                 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";
  2322.                                                 $flip = !$flip;
  2323.                                                 $c++;
  2324.                                         }
  2325.                                 }
  2326.                                 echo "</table>";
  2327.                         }
  2328.                         if ( is_array( $sizes[ 'expired_list' ] ) && !empty( $sizes[ 'expired_list' ] ) ) {
  2329.                                 echo "<h4>" . __( 'Stale Super Cached Files', 'wp-super-cache' ) . "</h4>";
  2330.                                 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>";
  2331.                                 $c = 1;
  2332.                                 $flip = 1;
  2333.                                 ksort( $sizes[ 'expired_list' ] );
  2334.                                 foreach( $sizes[ 'expired_list' ] as $age => $d ) {
  2335.                                         foreach( $d as $uri => $n ) {
  2336.                                                 $bg = $flip ? 'style="background: #EAEAEA;"' : '';
  2337.                                                 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";
  2338.                                                 $flip = !$flip;
  2339.                                                 $c++;
  2340.                                         }
  2341.                                 }
  2342.                                 echo "</table>";
  2343.                         }
  2344.                         echo "</div>";
  2345.                         echo "<p><a href='?page=wpsupercache&tab=contents#top'>" . __( 'Hide file list', 'wp-super-cache' ) . "</a></p>";
  2346.                 } elseif ( $cache_stats[ 'supercache' ][ 'cached' ] > 300 || $cache_stats[ 'supercache' ][ 'expired' ] > 300 || ( $cache_stats[ 'wpcache' ][ 'cached' ] / $divisor ) > 300 || ( $cache_stats[ 'wpcache' ][ 'expired' ] / $divisor) > 300 ) {
  2347.                         echo "<p><em>" . __( 'Too many cached files, no listing possible.', 'wp-super-cache' ) . "</em></p>";
  2348.                 } else {
  2349.                         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>";
  2350.                 }
  2351.                 $last_gc = get_option( "wpsupercache_gc_time" );
  2352.                 if ( $cache_max_time > 0 && $last_gc ) {
  2353.                         $next_gc = $cache_max_time < 1800 ? $cache_max_time : 600;
  2354.                         $next_gc_mins = ( time() - $last_gc );
  2355.                         echo "<p>" . sprintf( __( '<strong>Garbage Collection</strong><br />Last GC was <strong>%s</strong> minutes ago<br />', 'wp-super-cache' ), date( 'i:s', $next_gc_mins ) );
  2356.                         printf( __( "Next GC in <strong>%s</strong> minutes", 'wp-super-cache' ), date( 'i:s', $next_gc - $next_gc_mins ) ) . "</p>";
  2357.                 }
  2358.                 if ( $cache_max_time > 0 )
  2359.                         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>";
  2360.         } // cache_stats
  2361.         wp_cache_delete_buttons();
  2362.  
  2363.         echo '</fieldset>';
  2364. }
  2365.  
  2366. function wp_cache_delete_buttons() {
  2367.  
  2368.         echo '<form name="wp_cache_content_expired" action="#listfiles" method="post">';
  2369.         echo '<input type="hidden" name="wp_delete_expired" />';
  2370.         echo '<div class="submit" style="float:left"><input type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Delete Expired', 'wp-super-cache' ) . ' &raquo;" /></div>';
  2371.         wp_nonce_field('wp-cache');
  2372.         echo "</form>\n";
  2373.  
  2374.         echo '<form name="wp_cache_content_delete" action="#listfiles" method="post">';
  2375.         echo '<input type="hidden" name="wp_delete_cache" />';
  2376.         echo '<div class="submit" style="float:left;margin-left:10px"><input id="deletepost" type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Delete Cache', 'wp-super-cache' ) . ' &raquo;" /></div>';
  2377.         wp_nonce_field('wp-cache');
  2378.         echo "</form>\n";
  2379.         if ( ( defined( 'VHOST' ) || ( defined( 'WP_ALLOW_MULTISITE' ) && constant( 'WP_ALLOW_MULTISITE' ) == true ) ) && current_user_can('wp_supercache_manage_options') ) {
  2380.                 echo '<form name="wp_cache_content_delete" action="#listfiles" method="post">';
  2381.                 echo '<input type="hidden" name="wp_delete_all_cache" />';
  2382.                 echo '<div class="submit" style="float:left;margin-left:10px"><input id="deleteallpost" type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Delete Cache On All Blogs', 'wp-super-cache' ) . ' &raquo;" />';
  2383.                 wp_nonce_field('wp-cache');
  2384.                 echo "</form>\n";
  2385.         }
  2386. }
  2387.  
  2388. function delete_cache_dashboard() {
  2389.         if ( false == current_user_can('wp_supercache_manage_options') )
  2390.                 return false;
  2391.  
  2392.         if ( function_exists('current_user_can') && !current_user_can('manage_options') )
  2393.                 return false;
  2394.  
  2395.         echo "<li><a href='" . wp_nonce_url( 'options-general.php?page=wpsupercache&wp_delete_cache=1', 'wp-cache' ) . "' target='_blank' title='" . __( 'Delete Super Cache cached files (opens in new window)', 'wp-super-cache' ) . "'>" . __( 'Delete Cache', 'wp-super-cache' ) . "</a></li>";
  2396. }
  2397. add_action( 'dashmenu', 'delete_cache_dashboard' );
  2398.  
  2399. function wpsc_dirsize($directory, $sizes) {
  2400.         global $cache_max_time, $cache_path, $valid_nonce, $wp_cache_preload_on;
  2401.         $now = time();
  2402.  
  2403.         if (is_dir($directory)) {
  2404.                 if( $dh = opendir( $directory ) ) {
  2405.                         while( ( $entry = readdir( $dh ) ) !== false ) {
  2406.                                 if ($entry != '.' && $entry != '..') {
  2407.                                         $sizes = wpsc_dirsize( trailingslashit( $directory ) . $entry, $sizes );
  2408.                                 }
  2409.                         }
  2410.                         closedir($dh);
  2411.                 }
  2412.         } else {
  2413.                 if(is_file($directory) ) {
  2414.                         $filem = filemtime( $directory );
  2415.                         if ( $wp_cache_preload_on == false && $cache_max_time > 0 && $filem + $cache_max_time <= $now ) {
  2416.                                 $sizes[ 'expired' ]+=1;
  2417.                                 if ( $valid_nonce && $_GET[ 'listfiles' ] )
  2418.                                         $sizes[ 'expired_list' ][ $now - $filem ][ str_replace( $cache_path . 'supercache/' , '', str_replace( 'index.html', '', str_replace( 'index.html.gz', '', $directory ) ) ) ] = 1;
  2419.                         } else {
  2420.                                 $sizes[ 'cached' ]+=1;
  2421.                                 if ( $valid_nonce && array_key_exists('listfiles', $_GET) && $_GET[ 'listfiles' ] )
  2422.                                         $sizes[ 'cached_list' ][ $now - $filem ][ str_replace( $cache_path . 'supercache/' , '', str_replace( 'index.html', '', str_replace( 'index.html.gz', '', $directory ) ) ) ] = 1;
  2423.                         }
  2424.                         if ( ! isset( $sizes[ 'fsize' ] ) )
  2425.                                 $sizes[ 'fsize' ] = @filesize( $directory );
  2426.                         else
  2427.                                 $sizes[ 'fsize' ] += @filesize( $directory );
  2428.                 }
  2429.         }
  2430.         return $sizes;
  2431. }
  2432.  
  2433. function wp_cache_clean_cache( $file_prefix, $all = false ) {
  2434.         global $wpdb, $cache_path, $supercachedir, $blog_cache_dir, $wp_cache_object_cache;
  2435.  
  2436.         if ( $wp_cache_object_cache && function_exists( "reset_oc_version" ) )
  2437.                 reset_oc_version();
  2438.  
  2439.         if ( $all == true && current_user_can('wp_supercache_manage_options') && function_exists( 'prune_super_cache' ) ) {
  2440.                 prune_super_cache( $cache_path, true );
  2441.                 return true;
  2442.         }
  2443.  
  2444.         if (function_exists ('prune_super_cache')) {
  2445.                 if( is_dir( $supercachedir ) ) {
  2446.                         prune_super_cache( $supercachedir, true );
  2447.                 } elseif( is_dir( $supercachedir . '.disabled' ) ) {
  2448.                         prune_super_cache( $supercachedir . '.disabled', true );
  2449.                 }
  2450.                 $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
  2451.         } elseif ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Warning! prune_super_cache() not found in wp-cache.php', 1 );
  2452.  
  2453.         wp_cache_clean_legacy_files( $blog_cache_dir, $file_prefix );
  2454.         wp_cache_clean_legacy_files( $cache_path, $file_prefix );
  2455.  
  2456. }
  2457.  
  2458. function wp_cache_clean_legacy_files( $dir, $file_prefix ) {
  2459.         global $wpdb;
  2460.         if ( $handle = @opendir( $dir . 'meta/' ) ) {
  2461.                 while ( false !== ($file = readdir($handle))) {
  2462.                         if ( preg_match( "/^$file_prefix/", $file ) ) {
  2463.                                 $meta = unserialize( file_get_contents( $dir . 'meta/' . $file ) );
  2464.                                 if ( ( defined( 'VHOST' ) || ( defined( 'WP_ALLOW_MULTISITE' ) && constant( 'WP_ALLOW_MULTISITE' ) == true ) ) && $meta[ 'blog_id' ] != $wpdb->blogid )
  2465.                                         continue;
  2466.                                 @unlink( $dir . 'meta/' . $file);
  2467.                                 @unlink( $dir .  str_replace( '.meta', '.html', $file ) );
  2468.                         }
  2469.                 }
  2470.                 closedir($handle);
  2471.         }
  2472. }
  2473.  
  2474. function wp_cache_clean_expired($file_prefix) {
  2475.         global $cache_path, $cache_max_time, $blog_cache_dir, $wp_cache_preload_on;
  2476.  
  2477.         if ( $cache_max_time == 0 ) {
  2478.                 return false;
  2479.         }
  2480.  
  2481.         // If phase2 was compiled, use its function to avoid race-conditions
  2482.         if(function_exists('wp_cache_phase2_clean_expired')) {
  2483.                 if ( $wp_cache_preload_on != 1 && function_exists ('prune_super_cache')) {
  2484.                         $dir = get_supercache_dir();
  2485.                         if( is_dir( $dir ) ) {
  2486.                                 prune_super_cache( $dir );
  2487.                         } elseif( is_dir( $dir . '.disabled' ) ) {
  2488.                                 prune_super_cache( $dir . '.disabled' );
  2489.                         }
  2490.                         $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
  2491.                 }
  2492.                 return wp_cache_phase2_clean_expired($file_prefix);
  2493.         }
  2494.  
  2495.         $expr = "/^$file_prefix/";
  2496.         $now = time();
  2497.         if ( ($handle = @opendir( $blog_cache_dir )) ) {
  2498.                 while ( false !== ($file = readdir($handle))) {
  2499.                         if ( preg_match( $expr, $file )  &&
  2500.                                 ( filemtime( $blog_cache_dir . $file ) + $cache_max_time ) <= $now ) {
  2501.                                 @unlink( $blog_cache_dir . $file );
  2502.                                 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
  2503.                         }
  2504.                 }
  2505.                 closedir($handle);
  2506.         }
  2507. }
  2508.  
  2509. function wpsc_remove_marker( $filename, $marker ) {
  2510.         if (!file_exists( $filename ) || is_writeable_ACLSafe( $filename ) ) {
  2511.                 if (!file_exists( $filename ) ) {
  2512.                         return '';
  2513.                 } else {
  2514.                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
  2515.                 }
  2516.  
  2517.                 $f = fopen( $filename, 'w' );
  2518.                 $foundit = false;
  2519.                 if ( $markerdata ) {
  2520.                         $state = true;
  2521.                         foreach ( $markerdata as $n => $markerline ) {
  2522.                                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
  2523.                                         $state = false;
  2524.                                 if ( $state ) {
  2525.                                         if ( $n + 1 < count( $markerdata ) )
  2526.                                                 fwrite( $f, "{$markerline}\n" );
  2527.                                         else
  2528.                                                 fwrite( $f, "{$markerline}" );
  2529.                                 }
  2530.                                 if (strpos($markerline, '# END ' . $marker) !== false) {
  2531.                                         $state = true;
  2532.                                 }
  2533.                         }
  2534.                 }
  2535.                 return true;
  2536.         } else {
  2537.                 return false;
  2538.         }
  2539. }
  2540.  
  2541. function wp_super_cache_footer() {
  2542.         ?><p id='supercache'><?php printf( __( '%1$s is Stephen Fry proof thanks to caching by %2$s', 'wp-super-cache' ), bloginfo( 'name' ), '<a href="http://ocaoimh.ie/wp-super-cache/">WP Super Cache</a>' ); ?></p><?php
  2543. }
  2544. if( isset( $wp_cache_hello_world ) && $wp_cache_hello_world )
  2545.         add_action( 'wp_footer', 'wp_super_cache_footer' );
  2546.  
  2547. if( get_option( 'gzipcompression' ) )
  2548.         update_option( 'gzipcompression', 0 );
  2549.  
  2550. // Catch 404 requests. Themes that use query_posts() destroy $wp_query->is_404
  2551. function wp_cache_catch_404() {
  2552.         global $wp_cache_404;
  2553.         $wp_cache_404 = false;
  2554.         if( is_404() )
  2555.                 $wp_cache_404 = true;
  2556. }
  2557. add_action( 'template_redirect', 'wp_cache_catch_404' );
  2558.  
  2559. function wp_cache_favorite_action( $actions ) {
  2560.  
  2561.         $min_cap = 'wp_supercache_manage_options';
  2562.  
  2563.         if ( !current_user_can($min_cap) )
  2564.                 return $actions;
  2565.  
  2566.         $actions[ wp_nonce_url( 'options-general.php?page=wpsupercache&wp_delete_cache=1&tab=contents', 'wp-cache' ) ] = array( __( 'Delete Cache', 'wp-super-cache' ), $min_cap );
  2567.  
  2568.         return $actions;
  2569. }
  2570. add_filter( 'favorite_actions', 'wp_cache_favorite_action' );
  2571.  
  2572. function wp_cache_plugin_notice( $plugin ) {
  2573.         global $cache_enabled;
  2574.         if( $plugin == 'wp-super-cache/wp-cache.php' && !$cache_enabled && function_exists( "admin_url" ) )
  2575.                 echo '<td colspan="5" class="plugin-update">' . sprintf( __( 'WP Super Cache must be configured. Go to <a href="%s">the admin page</a> to enable and configure the plugin.' ), admin_url( 'options-general.php?page=wpsupercache' ) ) . '</td>';
  2576. }
  2577. add_action( 'after_plugin_row', 'wp_cache_plugin_notice' );
  2578.  
  2579. function wp_cache_plugin_actions( $links, $file ) {
  2580.         if( $file == 'wp-super-cache/wp-cache.php' && function_exists( "admin_url" ) ) {
  2581.                 $settings_link = '<a href="' . admin_url( 'options-general.php?page=wpsupercache' ) . '">' . __('Settings') . '</a>';
  2582.                 array_unshift( $links, $settings_link ); // before other links
  2583.         }
  2584.         return $links;
  2585. }
  2586. add_filter( 'plugin_action_links', 'wp_cache_plugin_actions', 10, 2 );
  2587.  
  2588. function wp_cache_admin_notice() {
  2589.         global $cache_enabled;
  2590.         if( substr( $_SERVER["PHP_SELF"], -11 ) == 'plugins.php' && !$cache_enabled && function_exists( "admin_url" ) )
  2591.                 echo '<div class="error"><p><strong>' . sprintf( __('WP Super Cache is disabled. Please go to the <a href="%s">plugin admin page</a> to enable caching.', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) . '</strong></p></div>';
  2592.  
  2593.         if ( defined( 'ADVANCEDCACHEPROBLEM' ) )
  2594.                 echo '<div class="error"><p><strong>' . sprintf( __( 'Warning! WP Super Cache caching broken! The script advanced-cache.php could not load wp-cache-phase1.php.<br /><br />Please edit %1$s/advanced-cache.php and make sure the path to %2$swp-cache-phase1.php is correct.', 'wp-super-cache' ), WP_CONTENT_DIR, WPCACHEHOME ) . '</strong></p></div>';
  2595. }
  2596. add_action( 'admin_notices', 'wp_cache_admin_notice' );
  2597.  
  2598. function wp_cache_check_site() {
  2599.         global $wp_super_cache_front_page_check, $wp_super_cache_front_page_clear, $wp_super_cache_front_page_text, $wp_super_cache_front_page_notification;
  2600.  
  2601.         if ( !isset( $wp_super_cache_front_page_check ) || ( isset( $wp_super_cache_front_page_check ) && $wp_super_cache_front_page_check == 0 ) ) {
  2602.                 return false;
  2603.         }
  2604.  
  2605.         if ( function_exists( "wp_remote_get" ) == false ) {
  2606.                 return false;
  2607.         }
  2608.         $front_page = wp_remote_get( site_url(), array('timeout' => 60, 'blocking' => true ) );
  2609.         if( is_array( $front_page ) ) {
  2610.                 // Check for gzipped front page
  2611.                 if ( $front_page[ 'headers' ][ 'content-type' ] == 'application/x-gzip' ) {
  2612.                         if ( !isset( $wp_super_cache_front_page_clear ) || ( isset( $wp_super_cache_front_page_clear ) && $wp_super_cache_front_page_clear == 0 ) ) {
  2613.                                 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is gzipped! Please clear cache!', 'wp-super-cache' ), site_url() ), sprintf( __( "Please visit %s to clear the cache as the front page of your site is now downloading!", 'wp-super-cache' ), trailingslashit( site_url() ) . "wp-admin/options-general.php?page=wpsupercache" ) );
  2614.                         } else {
  2615.                                 wp_cache_clear_cache();
  2616.                                 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is gzipped! Cache Cleared!', 'wp-super-cache' ), site_url() ), sprintf( __( "The cache on your blog has been cleared because the front page of your site is now downloading. Please visit %s to verify the cache has been cleared.", 'wp-super-cache' ), trailingslashit( site_url() ) . "wp-admin/options-general.php?page=wpsupercache" ) );
  2617.                         }
  2618.                 }
  2619.  
  2620.                 // Check for broken front page
  2621.                 if ( isset( $wp_super_cache_front_page_text ) && $wp_super_cache_front_page_text != '' && false === strpos( $front_page[ 'body' ], $wp_super_cache_front_page_text ) ) {
  2622.                         if ( !isset( $wp_super_cache_front_page_clear ) || ( isset( $wp_super_cache_front_page_clear ) && $wp_super_cache_front_page_clear == 0 ) ) {
  2623.                                 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is not correct! Please clear cache!', 'wp-super-cache' ), site_url() ), sprintf( __( 'Please visit %1$s to clear the cache as the front page of your site is not correct and missing the text, "%2$s"!', 'wp-super-cache' ), trailingslashit( site_url() ) . "wp-admin/options-general.php?page=wpsupercache", $wp_super_cache_front_page_text ) );
  2624.                         } else {
  2625.                                 wp_cache_clear_cache();
  2626.                                 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is not correct! Cache Cleared!', 'wp-super-cache' ), site_url() ), sprintf( __( 'The cache on your blog has been cleared because the front page of your site is missing the text "%2$s". Please visit %1$s to verify the cache has been cleared.', 'wp-super-cache' ), trailingslashit( site_url() ) . "wp-admin/options-general.php?page=wpsupercache", $wp_super_cache_front_page_text ) );
  2627.                         }
  2628.                 }
  2629.         }
  2630.         if ( isset( $wp_super_cache_front_page_notification ) && $wp_super_cache_front_page_notification == 1 ) {
  2631.                 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page check!', 'wp-super-cache' ), site_url() ), sprintf( __( "WP Super Cache has checked the front page of your blog. Please visit %s if you would like to disable this.", 'wp-super-cache' ) . "\n\n", trailingslashit( site_url() ) . "wp-admin/options-general.php?page=wpsupercache#debug" ) . print_r( $front_page, 1 ) );
  2632.         }
  2633.  
  2634.         if ( !wp_next_scheduled( 'wp_cache_check_site_hook' ) ) {
  2635.                 wp_schedule_single_event( time() + 360 , 'wp_cache_check_site_hook' );
  2636.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'scheduled wp_cache_check_site_hook for 360 seconds time.', 2 );
  2637.         }
  2638. }
  2639. add_action( 'wp_cache_check_site_hook', 'wp_cache_check_site' );
  2640.  
  2641. function update_cached_mobile_ua_list( $mobile_browsers, $mobile_prefixes = 0, $mobile_groups = 0 ) {
  2642.         global $wp_cache_config_file, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes, $wp_cache_mobile_groups;
  2643.         if ( is_array( $mobile_browsers ) ) {
  2644.                 $wp_cache_mobile_browsers = $mobile_browsers;
  2645.                 wp_cache_replace_line('^ *\$wp_cache_mobile_browsers', "\$wp_cache_mobile_browsers = '" . implode( ', ', $mobile_browsers ) . "';", $wp_cache_config_file);
  2646.         }
  2647.         if ( is_array( $mobile_prefixes ) ) {
  2648.                 $wp_cache_mobile_prefixes = $mobile_prefixes;
  2649.                 wp_cache_replace_line('^ *\$wp_cache_mobile_prefixes', "\$wp_cache_mobile_prefixes = '" . implode( ', ', $mobile_prefixes ) . "';", $wp_cache_config_file);
  2650.         }
  2651.         if ( is_array( $mobile_groups ) ) {
  2652.                 $wp_cache_mobile_groups = $mobile_groups;
  2653.                 wp_cache_replace_line('^ *\$wp_cache_mobile_groups', "\$wp_cache_mobile_groups = '" . implode( ', ', $mobile_groups ) . "';", $wp_cache_config_file);
  2654.         }
  2655.        
  2656.         return true;
  2657. }
  2658.  
  2659. function wpsc_update_htaccess() {
  2660.         extract( wpsc_get_htaccess_info() );
  2661.         wpsc_remove_marker( $home_path.'.htaccess', 'WordPress' ); // remove original WP rules so SuperCache rules go on top
  2662.         if( insert_with_markers( $home_path.'.htaccess', 'WPSuperCache', explode( "\n", $rules ) ) && insert_with_markers( $home_path.'.htaccess', 'WordPress', explode( "\n", $wprules ) ) ) {
  2663.                 return true;
  2664.         } else {
  2665.                 return false;
  2666.         }
  2667. }
  2668.  
  2669. function wpsc_update_htaccess_form( $short_form = true ) {
  2670.         global $wpmu_version;
  2671.  
  2672.         extract( wpsc_get_htaccess_info() );
  2673.         if( !is_writeable_ACLSafe( $home_path . ".htaccess" ) ) {
  2674.                 echo "<div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><h4>" . __( 'Cannot update .htaccess', 'wp-super-cache' ) . "</h4><p>" . sprintf( __( 'The file <code>%s.htaccess</code> cannot be modified by the web server. Please correct this using the chmod command or your ftp client.', 'wp-super-cache' ), $home_path ) . "</p><p>" . __( 'Refresh this page when the file permissions have been modified.' ) . "</p><p>" . sprintf( __( 'Alternatively, you can edit your <code>%s.htaccess</code> file manually and add the following code (before any WordPress rules):', 'wp-super-cache' ), $home_path ) . "</p>";
  2675.                 echo "<p><pre># BEGIN WPSuperCache\n" . esc_html( $rules ) . "# END WPSuperCache</pre></p></div>";
  2676.         } else {
  2677.                 if ( $short_form == false ) {
  2678.                         echo "<div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><p>" . sprintf( __( 'To serve static html files your server must have the correct mod_rewrite rules added to a file called <code>%s.htaccess</code>', 'wp-super-cache' ), $home_path ) . " ";
  2679.                         _e( "You can edit the file yourself add the following rules.", 'wp-super-cache' );
  2680.                         echo __( " Make sure they appear before any existing WordPress rules. ", 'wp-super-cache' ) . "</p>";
  2681.                         echo "<pre># BEGIN WPSuperCache\n" . esc_html( $rules ) . "# END WPSuperCache</pre></p>";
  2682.                         echo "<p>" . sprintf( __( 'Rules must be added to %s too:', 'wp-super-cache' ), WP_CONTENT_DIR . "/cache/.htaccess" ) . "</p>";
  2683.                         echo "<pre># BEGIN supercache\n" . esc_html( $gziprules ) . "# END supercache</pre></p>";
  2684.                 }
  2685.                 if ( !isset( $wpmu_version ) || $wpmu_version == '' ) {
  2686.                         echo '<form name="updatehtaccess" action="#modrewrite" method="post">';
  2687.                         echo '<input type="hidden" name="updatehtaccess" value="1" />';
  2688.                         echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'id="updatehtaccess" value="' . __( 'Update Mod_Rewrite Rules', 'wp-super-cache' ) . ' &raquo;" /></div>';
  2689.                         wp_nonce_field('wp-cache');
  2690.                         echo "</form></div>\n";
  2691.                 }
  2692.         }
  2693. }
  2694.  
  2695. function wpsc_get_htaccess_info() {
  2696.         global $wp_cache_mobile_enabled, $wp_cache_mobile_prefixes, $wp_cache_mobile_browsers, $wp_cache_disable_utf8;
  2697.         if ( isset( $_SERVER[ "PHP_DOCUMENT_ROOT" ] ) ) {
  2698.                 $document_root = $_SERVER[ "PHP_DOCUMENT_ROOT" ];
  2699.                 $apache_root = $_SERVER[ "PHP_DOCUMENT_ROOT" ];
  2700.         } else {
  2701.                 $document_root = $_SERVER[ "DOCUMENT_ROOT" ];
  2702.                 $apache_root = '%{DOCUMENT_ROOT}';
  2703.         }
  2704.         $content_dir_root = $document_root;
  2705.         if ( strpos( $document_root, '/kunden/' ) === 0 ) {
  2706.                 // http://wordpress.org/support/topic/plugin-wp-super-cache-how-to-get-mod_rewrite-working-on-1and1-shared-hosting?replies=1
  2707.                 // On 1and1, PHP's directory structure starts with '/homepages'. The
  2708.                 // Apache directory structure has an extra '/kunden' before it.
  2709.                 // Also 1and1 does not support the %{DOCUMENT_ROOT} variable in
  2710.                 // .htaccess files.
  2711.                 // This prevents the $inst_root from being calculated correctly and
  2712.                 // means that the $apache_root is wrong.
  2713.                 //
  2714.                 // e.g. This is an example of how Apache and PHP see the directory
  2715.                 // structure on 1and1:
  2716.                 // Apache: /kunden/homepages/xx/dxxxxxxxx/htdocs/site1/index.html
  2717.                 // PHP:           /homepages/xx/dxxxxxxxx/htdocs/site1/index.html
  2718.                 // Here we fix up the paths to make mode_rewrite work on 1and1 shared hosting.
  2719.                 $content_dir_root = substr( $content_dir_root, 7 );
  2720.                 $apache_root = $document_root;
  2721.         }
  2722.         $home_path = get_home_path();
  2723.         $home_root = parse_url(get_bloginfo('url'));
  2724.         $home_root = isset( $home_root['path'] ) ? trailingslashit( $home_root['path'] ) : '/';
  2725.         $home_root_lc = str_replace( '//', '/', strtolower( $home_root ) );
  2726.         $inst_root = str_replace( '//', '/', '/' . trailingslashit( str_replace( $content_dir_root, '', str_replace( '\\', '/', WP_CONTENT_DIR ) ) ) );
  2727.         $wprules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WordPress' ) );
  2728.         $wprules = str_replace( "RewriteEngine On\n", '', $wprules );
  2729.         $wprules = str_replace( "RewriteBase $home_root\n", '', $wprules );
  2730.         $scrules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WPSuperCache' ) );
  2731.  
  2732.         if( substr( get_option( 'permalink_structure' ), -1 ) == '/' ) {
  2733.                 $condition_rules[] = "RewriteCond %{REQUEST_URI} !^.*[^/]$";
  2734.                 $condition_rules[] = "RewriteCond %{REQUEST_URI} !^.*//.*$";
  2735.         }
  2736.         $condition_rules[] = "RewriteCond %{REQUEST_METHOD} !POST";
  2737.         $condition_rules[] = "RewriteCond %{QUERY_STRING} !.*=.*";
  2738.         $condition_rules[] = "RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$";
  2739.         $condition_rules[] = "RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\\\"]+ [NC]";
  2740.         $condition_rules[] = "RewriteCond %{HTTP:Profile} !^[a-z0-9\\\"]+ [NC]";
  2741.         if ( $wp_cache_mobile_enabled ) {
  2742.                 if ( false == empty( $wp_cache_mobile_browsers ) )
  2743.                         $condition_rules[] = "RewriteCond %{HTTP_USER_AGENT} !^.*(" . addcslashes( implode( '|', $wp_cache_mobile_browsers ), ' ' ) . ").* [NC]";
  2744.                 if ( false == empty( $wp_cache_mobile_prefixes ) )
  2745.                         $condition_rules[] = "RewriteCond %{HTTP_user_agent} !^(" . addcslashes( implode( '|', $wp_cache_mobile_prefixes ), ' ' ) . ").* [NC]";
  2746.         }
  2747.         $condition_rules = apply_filters( 'supercacherewriteconditions', $condition_rules );
  2748.  
  2749.         $rules = "<IfModule mod_rewrite.c>\n";
  2750.         $rules .= "RewriteEngine On\n";
  2751.         $rules .= "RewriteBase $home_root\n"; // props Chris Messina
  2752.         $rules .= "#If you serve pages from behind a proxy you may want to change 'RewriteCond %{HTTPS} on' to something more sensible\n";
  2753.         if ( isset( $wp_cache_disable_utf8 ) == false || $wp_cache_disable_utf8 = 0 ) {
  2754.                 $charset = get_option('blog_charset') == '' ? 'UTF-8' : get_option('blog_charset');
  2755.                 $rules .= "AddDefaultCharset {$charset}\n";
  2756.         }
  2757.  
  2758.         $rules .= "CONDITION_RULES";
  2759.         $rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
  2760.         $rules .= "RewriteCond %{HTTPS} on\n";
  2761.         $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html.gz -f\n";
  2762.         $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html.gz\" [L]\n\n";
  2763.  
  2764.         $rules .= "CONDITION_RULES";
  2765.         $rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
  2766.         $rules .= "RewriteCond %{HTTPS} !on\n";
  2767.         $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html.gz -f\n";
  2768.         $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html.gz\" [L]\n\n";
  2769.  
  2770.         $rules .= "CONDITION_RULES";
  2771.         $rules .= "RewriteCond %{HTTPS} on\n";
  2772.         $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html -f\n";
  2773.         $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html\" [L]\n\n";
  2774.  
  2775.         $rules .= "CONDITION_RULES";
  2776.         $rules .= "RewriteCond %{HTTPS} !on\n";
  2777.         $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html -f\n";
  2778.         $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html\" [L]\n";
  2779.         $rules .= "</IfModule>\n";
  2780.         $rules = apply_filters( 'supercacherewriterules', $rules );
  2781.  
  2782.         $rules = str_replace( "CONDITION_RULES", implode( "\n", $condition_rules ) . "\n", $rules );
  2783.  
  2784.         $gziprules =  "<IfModule mod_mime.c>\n  <FilesMatch \"\\.html\\.gz\$\">\n    ForceType text/html\n    FileETag None\n  </FilesMatch>\n  AddEncoding gzip .gz\n  AddType text/html .gz\n</IfModule>\n";
  2785.         $gziprules .= "<IfModule mod_deflate.c>\n  SetEnvIfNoCase Request_URI \.gz$ no-gzip\n</IfModule>\n";
  2786.         $gziprules .= "<IfModule mod_headers.c>\n  Header set Vary \"Accept-Encoding, Cookie\"\n  Header set Cache-Control 'max-age=3, must-revalidate'\n</IfModule>\n";
  2787.         $gziprules .= "<IfModule mod_expires.c>\n  ExpiresActive On\n  ExpiresByType text/html A3\n</IfModule>\n";
  2788.         return array( "document_root" => $document_root, "apache_root" => $apache_root, "home_path" => $home_path, "home_root" => $home_root, "home_root_lc" => $home_root_lc, "inst_root" => $inst_root, "wprules" => $wprules, "scrules" => $scrules, "condition_rules" => $condition_rules, "rules" => $rules, "gziprules" => $gziprules );
  2789. }
  2790.  
  2791. function clear_post_supercache( $post_id ) {
  2792.         $dir = get_current_url_supercache_dir( $post_id );
  2793.         if ( false == @is_dir( $dir ) )
  2794.                 return false;
  2795.  
  2796.         if ( !function_exists( 'prune_super_cache' ) )
  2797.                 include_once( 'wp-cache-phase2.php' );
  2798.  
  2799.         if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "clear_post_supercache: deleting $dir/index*.html files", 2 );
  2800.         $files_to_check = get_all_supercache_filenames( $dir );
  2801.         foreach( $files_to_check as $cache_file ) {
  2802.                 prune_super_cache( $dir . $cache_file, true );
  2803.         }
  2804. }
  2805.  
  2806. function wp_cron_preload_cache() {
  2807.         global $wpdb, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_email_me, $wp_cache_preload_email_volume, $cache_path, $wp_cache_preload_taxonomies;
  2808.  
  2809.         if ( get_option( 'preload_cache_stop' ) ) {
  2810.                 delete_option( 'preload_cache_stop' );
  2811.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: preload cancelled", 1 );
  2812.                 return true;
  2813.         }
  2814.         $mutex = $cache_path . "preload_mutex.tmp";
  2815.         sleep( 3 + mt_rand( 1, 5 ) );
  2816.         if ( @file_exists( $mutex ) ) {
  2817.                 if ( @filemtime( $mutex ) > ( time() - 600 ) ) {
  2818.                         if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: preload mutex found and less than 600 seconds old. Aborting preload.", 1 );
  2819.                         return true;
  2820.                 } else {
  2821.                         if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: old preload mutex found and deleted. Preload continues.", 1 );
  2822.                         @unlink( $mutex );
  2823.                 }
  2824.         }
  2825.         $fp = @fopen( $mutex, 'w' );
  2826.         @fclose( $fp );
  2827.  
  2828.         $counter = get_option( 'preload_cache_counter' );
  2829.         if ( is_array( $counter ) == false ) {
  2830.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: setting up preload for the first time!", 5 );
  2831.                 $counter = array( 'c' => 0, 't' => time() );
  2832.                 update_option( 'preload_cache_counter', $counter );
  2833.         }
  2834.         $c = $counter[ 'c' ];
  2835.  
  2836.         update_option( 'preload_cache_counter', array( 'c' => ( $c + 100 ), 't' => time() ) );
  2837.  
  2838.         if ( $wp_cache_preload_email_me && $c == 0 )
  2839.                 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Started', 'wp-super-cache' ), site_url(), '' ), ' ' );
  2840.  
  2841.         if ( $wp_cache_preload_posts == 'all' || $c < $wp_cache_preload_posts ) {
  2842.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: doing taxonomy preload.", 5 );
  2843.                 $permalink_counter_msg = $cache_path . "preload_permalink.txt";
  2844.                 if ( isset( $wp_cache_preload_taxonomies ) && $wp_cache_preload_taxonomies ) {
  2845.                         $taxonomies = apply_filters( 'wp_cache_preload_taxonomies', array( 'post_tag' => 'tag', 'category' => 'category' ) );
  2846.                         foreach( $taxonomies as $taxonomy => $path ) {
  2847.                                 $taxonomy_filename = $cache_path . "taxonomy_" . $taxonomy . ".txt";
  2848.                                 if ( $c == 0 )
  2849.                                         @unlink( $taxonomy_filename );
  2850.  
  2851.                                 if ( false == @file_exists( $taxonomy_filename ) ) {
  2852.                                         $out = '';
  2853.                                         $records = get_terms( $taxonomy );
  2854.                                         foreach( $records as $term ) {
  2855.                                                 $out .= site_url( $path . "/" . $term->slug . "/" ) . "\n";
  2856.                                         }
  2857.                                         $fp = fopen( $taxonomy_filename, 'w' );
  2858.                                         if ( $fp ) {
  2859.                                                 fwrite( $fp, $out );
  2860.                                                 fclose( $fp );
  2861.                                         }
  2862.                                         $details = explode( "\n", $out );
  2863.                                 } else {
  2864.                                         $details = explode( "\n", file_get_contents( $taxonomy_filename ) );
  2865.                                 }
  2866.                                 if ( count( $details ) != 1 && $details[ 0 ] != '' ) {
  2867.                                         $rows = array_splice( $details, 0, 50 );
  2868.                                         if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume == 'many' )
  2869.                                                 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing %2$s taxonomy from %3$d to %4$d', 'wp-super-cache' ), site_url(), $taxonomy, $c, ($c+100) ), 'Refreshing: ' . print_r( $rows, 1 ) );
  2870.                                         foreach( (array)$rows as $url ) {
  2871.                                                 set_time_limit( 60 );
  2872.                                                 if ( $url == '' )
  2873.                                                         continue;
  2874.                                                 $url_info = parse_url( $url );
  2875.                                                 $dir = get_supercache_dir() . $url_info[ 'path' ];
  2876.                                                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: delete $dir", 5 );
  2877.                                                 prune_super_cache( $dir );
  2878.                                                 $fp = @fopen( $permalink_counter_msg, 'w' );
  2879.                                                 if ( $fp ) {
  2880.                                                         @fwrite( $fp, "$taxonomy: $url" );
  2881.                                                         @fclose( $fp );
  2882.                                                 }
  2883.                                                 wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
  2884.                                                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: fetched $url", 5 );
  2885.                                                 sleep( 1 );
  2886.                                         }
  2887.                                         $fp = fopen( $taxonomy_filename, 'w' );
  2888.                                         if ( $fp ) {
  2889.                                                 fwrite( $fp, implode( "\n", $details ) );
  2890.                                                 fclose( $fp );
  2891.                                         }
  2892.                                 }
  2893.                         }
  2894.                 }
  2895.         }
  2896.  
  2897.         if ( $wp_cache_preload_posts == 'all' || $c < $wp_cache_preload_posts ) {
  2898.                 $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE ( post_type != 'revision' AND post_type != 'nav_menu_item' ) AND post_status = 'publish' ORDER BY ID ASC LIMIT $c, 100" );
  2899.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: got 100 posts from position $c.", 5 );
  2900.         } else {
  2901.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: no more posts to get. Limit ($wp_cache_preload_posts) reached.", 5 );
  2902.                 $posts = false;
  2903.         }
  2904.         if ( !isset( $wp_cache_preload_email_volume ) )
  2905.                 $wp_cache_preload_email_volume = 'medium';
  2906.  
  2907.         if ( $posts ) {
  2908.                 if ( get_option( 'show_on_front' ) == 'page' ) {
  2909.                         $page_on_front = get_option( 'page_on_front' );
  2910.                         $page_for_posts = get_option( 'page_for_posts' );
  2911.                 } else {
  2912.                         $page_on_front = $page_for_posts = 0;
  2913.                 }
  2914.                 if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume == 'many' )
  2915.                         wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing posts from %2$d to %3$d', 'wp-super-cache' ), site_url(), $c, ($c+100) ), ' ' );
  2916.                 $msg = '';
  2917.                 $count = $c + 1;
  2918.                 $permalink_counter_msg = $cache_path . "preload_permalink.txt";
  2919.                 foreach( $posts as $post_id ) {
  2920.                         set_time_limit( 60 );
  2921.                         if ( $page_on_front != 0 && ( $post_id == $page_on_front || $post_id == $page_for_posts ) )
  2922.                                 continue;
  2923.                         clear_post_supercache( $post_id );
  2924.                         $url = get_permalink( $post_id );
  2925.                         $fp = @fopen( $permalink_counter_msg, 'w' );
  2926.                         if ( $fp ) {
  2927.                                 @fwrite( $fp, $count . " " . $url );
  2928.                                 @fclose( $fp );
  2929.                         }
  2930.                         if ( @file_exists( $cache_path . "stop_preload.txt" ) ) {
  2931.                                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: cancelling preload. stop_preload.txt found.", 5 );
  2932.                                 @unlink( $mutex );
  2933.                                 @unlink( $cache_path . "stop_preload.txt" );
  2934.                                 update_option( 'preload_cache_counter', array( 'c' => 0, 't' => time() ) );
  2935.                                 if ( $wp_cache_preload_email_me )
  2936.                                         wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Stopped', 'wp-super-cache' ), site_url(), '' ), ' ' );
  2937.                                 return true;
  2938.                         }
  2939.                         $msg .= "$url\n";
  2940.                         wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
  2941.                         if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: fetched $url", 5 );
  2942.                         sleep( 1 );
  2943.                         $count++;
  2944.                 }
  2945.                 if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume != 'less' )
  2946.                         wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] %2$d posts refreshed', 'wp-super-cache' ), $_SERVER[ 'SERVER_NAME' ], ($c+100) ), __( "Refreshed the following posts:", 'wp-super-cache' ) . "\n$msg" );
  2947.                 if ( defined( 'DOING_CRON' ) ) {
  2948.                         if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: scheduling the next preload in 30 seconds.", 5 );
  2949.                         wp_schedule_single_event( time() + 30, 'wp_cache_preload_hook' );
  2950.                 }
  2951.         } else {
  2952.                 $msg = '';
  2953.                 update_option( 'preload_cache_counter', array( 'c' => 0, 't' => time() ) );
  2954.                 if ( (int)$wp_cache_preload_interval && defined( 'DOING_CRON' ) ) {
  2955.                         if ( $wp_cache_preload_email_me )
  2956.                                 $msg = sprintf( __( 'Scheduling next preload refresh in %d minutes.', 'wp-super-cache' ), (int)$wp_cache_preload_interval );
  2957.                         if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: no more posts. scheduling next preload in $wp_cache_preload_interval minutes.", 5 );
  2958.                         wp_schedule_single_event( time() + ( (int)$wp_cache_preload_interval * 60 ), 'wp_cache_full_preload_hook' );
  2959.                 }
  2960.                 global $file_prefix, $cache_max_time;
  2961.                 if ( $wp_cache_preload_interval > 0 ) {
  2962.                         $cache_max_time = (int)$wp_cache_preload_interval * 60; // fool the GC into expiring really old files
  2963.                 } else {
  2964.                         $cache_max_time = 86400; // fool the GC into expiring really old files
  2965.                 }
  2966.                 if ( $wp_cache_preload_email_me )
  2967.                         wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Cache Preload Completed', 'wp-super-cache' ), site_url() ), __( "Cleaning up old supercache files.", 'wp-super-cache' ) . "\n" . $msg );
  2968.                 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cron_preload_cache: clean expired cache files older than $cache_max_time seconds.", 5 );
  2969.                 wp_cache_phase2_clean_expired( $file_prefix, true ); // force cleanup of old files.
  2970.         }
  2971.         @unlink( $mutex );
  2972. }
  2973. add_action( 'wp_cache_preload_hook', 'wp_cron_preload_cache' );
  2974. add_action( 'wp_cache_full_preload_hook', 'wp_cron_preload_cache' );
  2975.  
  2976. function next_preload_message( $hook, $text, $limit = 0 ) {
  2977.         global $currently_preloading, $wp_cache_preload_interval;
  2978.         if ( $next_preload = wp_next_scheduled( $hook ) ) {
  2979.                 $next_time = $next_preload - time();
  2980.                 if ( $limit != 0 && $next_time > $limit )
  2981.                         return false;
  2982.                 $h = $m = $s = 0;
  2983.                 if ( $next_time > 0 ) {
  2984.                         // http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss
  2985.                         $m = (int)($next_time / 60);
  2986.