source: trunk/www.guidonia.net/wp/wp-content/plugins/wp-super-cache/wp-cache.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 70.1 KB
Line 
1<?php
2/*
3Plugin Name: WP Super Cache
4Plugin URI: http://ocaoimh.ie/wp-super-cache/
5Description: Very fast caching plugin for WordPress.
6Version: 0.9.5
7Author: Donncha O Caoimh
8Author URI: http://ocaoimh.ie/
9*/
10
11/* Copyright 2005-2006 Ricardo Galli Granada (email : gallir@uib.es)
12 Some code copyright 2007-2008 Donncha O Caoimh (http://ocaoimh.ie/)
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
30if( !defined('WP_CONTENT_URL') )
31 define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
32if( !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
37if( !@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
49include(WPCACHEHOME . 'wp-cache-base.php');
50
51
52// from legolas558 d0t users dot sf dot net at http://www.php.net/is_writable
53function is_writeable_ACLSafe($path) {
54
55 // PHP's is_writable does not work with Win32 NTFS
56
57 if ($path{strlen($path)-1}=='/') // recursively return a temporary file path
58 return is_writeable_ACLSafe($path.uniqid(mt_rand()).'.tmp');
59 else if (is_dir($path))
60 return is_writeable_ACLSafe($path.'/'.uniqid(mt_rand()).'.tmp');
61 // check tmp file for read/write capabilities
62 $rm = file_exists($path);
63 $f = @fopen($path, 'a');
64 if ($f===false)
65 return false;
66 fclose($f);
67 if (!$rm)
68 unlink($path);
69 return true;
70}
71
72function get_wpcachehome() {
73 if( defined( 'WPCACHEHOME' ) == false ) {
74 if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
75 define( 'WPCACHEHOME', trailingslashit( dirname(__FILE__) ) );
76 } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
77 define( 'WPCACHEHOME', dirname(__FILE__) . '/wp-super-cache/' );
78 } else {
79 die( 'Please create ' . WP_CONTENT_DIR . '/wp-cache-config.php from wp-super-cache/wp-cache-config-sample.php' );
80 }
81 }
82}
83
84function wpsupercache_deactivate() {
85 global $wp_cache_config_file, $wp_cache_link, $cache_path;
86 $files = array( $wp_cache_config_file, $wp_cache_link );
87 foreach( $files as $file ) {
88 if( file_exists( $file ) )
89 unlink( $file );
90 }
91 if( !function_exists( 'prune_super_cache' ) )
92 include_once( 'wp-cache-phase2.php' );
93 prune_super_cache ($cache_path, true);
94 @unlink( $cache_path . '.htaccess' );
95 @unlink( $cache_path . 'meta' );
96 @unlink( $cache_path . 'supercache' );
97}
98register_deactivation_hook( __FILE__, 'wpsupercache_deactivate' );
99
100function wp_cache_add_pages() {
101 if( function_exists( 'is_site_admin' ) ) {
102 if( is_site_admin() ) {
103 add_submenu_page('wpmu-admin.php', __('WP Super Cache'), __('WP Super Cache'), 'manage_options', 'wpsupercache', 'wp_cache_manager');
104 add_options_page('WP Super Cache', 'WP Super Cache', 'manage_options', 'wpsupercache', 'wp_cache_manager');
105 }
106 } else {
107 add_options_page('WP Super Cache', 'WP Super Cache', 'manage_options', 'wpsupercache', 'wp_cache_manager');
108 }
109}
110add_action('admin_menu', 'wp_cache_add_pages');
111
112function wp_cache_manager() {
113 global $wp_cache_config_file, $valid_nonce, $supercachedir, $cache_path, $cache_enabled, $cache_compression, $super_cache_enabled, $wp_cache_hello_world;
114 global $wp_cache_clear_on_post_edit, $cache_rebuild_files, $wp_cache_mutex_disabled, $wp_cache_mobile_enabled, $wp_cache_mobile_whitelist, $wp_cache_mobile_browsers;
115 global $wp_cache_cron_check, $wp_cache_debug, $wp_cache_hide_donation, $wp_cache_not_logged_in;
116
117 if( function_exists( 'is_site_admin' ) )
118 if( !is_site_admin() )
119 return;
120
121 $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
122 if( get_option( 'gzipcompression' ) == 1 )
123 update_option( 'gzipcompression', 0 );
124 if( !isset( $cache_rebuild_files ) )
125 $cache_rebuild_files = 0;
126
127 $valid_nonce = isset($_REQUEST['_wpnonce']) ? wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache') : false;
128 /* http://www.netlobo.com/div_hiding.html */
129 ?>
130<script type='text/javascript'>
131<!--
132function toggleLayer( whichLayer ) {
133 var elem, vis;
134 if( document.getElementById ) // this is the way the standards work
135 elem = document.getElementById( whichLayer );
136 else if( document.all ) // this is the way old msie versions work
137 elem = document.all[whichLayer];
138 else if( document.layers ) // this is the way nn4 works
139 elem = document.layers[whichLayer];
140 vis = elem.style;
141 // if the style.display value is blank we try to figure it out here
142 if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
143 vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
144 vis.display = (vis.display==''||vis.display=='block')?'none':'block';
145}
146// -->
147//Clicking header opens fieldset options
148jQuery(document).ready(function(){
149 jQuery("fieldset h3").css("cursor","pointer").click(function(){
150 jQuery(this).parent("fieldset").find("p,form,ul,blockquote").toggle("slow");
151 });
152});
153</script>
154<?php
155 echo '<div class="wrap">';
156 echo "<h2>WP Super Cache Manager</h2>\n";
157 if( ini_get( 'safe_mode' ) ) {
158 ?><h3>Warning! PHP Safe Mode Enabled!</h3>
159 <p>You may experience problems running this plugin because SAFE MODE is enabled. <?php
160 if( !ini_get( 'safe_mode_gid' ) ) {
161 ?>Your server is set up to check the owner of PHP scripts before allowing them to read and write files.</p><p>You or an administrator may be able to make it work by changing the group owner of the plugin scripts to match that of the web server user. The group owner of the <?php echo WP_CONTENT_DIR; ?>/cache/ directory must also be changed. See the <a href='http://php.net/features.safe-mode'>safe mode manual page</a> for further details.</p><?php
162 } else {
163 ?>You or an administrator must disable this. See the <a href='http://php.net/features.safe-mode'>safe mode manual page</a> for further details. This cannot be disabled in a .htaccess file unfortunately. It must be done in the php.ini config file.</p><?php
164 }
165 }
166 if(isset($_REQUEST['wp_restore_config']) && $valid_nonce) {
167 unlink($wp_cache_config_file);
168 echo '<strong>Configuration file changed, some values might be wrong. Load the page again from the "Settings" menu to reset them.</strong>';
169 }
170
171 if ( !wp_cache_check_link() ||
172 !wp_cache_verify_config_file() ||
173 !wp_cache_verify_cache_dir() ) {
174 echo "Cannot continue... fix previous problems and retry.";
175 echo "</div>\n";
176 return;
177 }
178
179 if (!wp_cache_check_global_config()) {
180 echo "</div>\n";
181 return;
182 }
183 if( $wp_cache_debug || !$wp_cache_cron_check ) {
184 if( function_exists( "wp_remote_get" ) == false ) {
185 $hostname = str_replace( 'http://', '', str_replace( 'https://', '', get_option( 'siteurl' ) ) );
186 if( strpos( $hostname, '/' ) )
187 $hostname = substr( $hostname, 0, strpos( $hostname, '/' ) );
188 $ip = gethostbyname( $hostname );
189 if( substr( $ip, 0, 3 ) == '127' || substr( $ip, 0, 7 ) == '192.168' ) {
190 ?><h3>Warning! Your hostname "<?php echo $hostname; ?>" resolves to <?php echo $ip; ?></h3>
191 <div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'>
192 <p>Your server thinks your hostname resolves to <?php echo $ip; ?>. Some services such as garbage collection by this plugin, and WordPress scheduled posts may not operate correctly.</p>
193 <p>Please see entry 16 in the <a href="http://wordpress.org/extend/plugins/wp-super-cache/faq/">Troubleshooting section</a> of the readme.txt</p>
194 </div>
195 <?php
196 } else {
197 wp_cache_replace_line('^ *\$wp_cache_cron_check', "\$wp_cache_cron_check = 1;", $wp_cache_config_file);
198 }
199 } else {
200 $cron_url = get_option( 'siteurl' ) . '/wp-cron.php?check=' . wp_hash('187425');
201 $cron = wp_remote_get($cron_url, array('timeout' => 0.01, 'blocking' => true));
202 if( is_array( $cron ) ) {
203 if( $cron[ 'response' ][ 'code' ] == '404' ) {
204 ?><h3>Warning! wp-cron.php not found!</h3>
205 <div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'>
206 <p>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.</p>
207 <p>Please see entry 16 in the <a href="http://wordpress.org/extend/plugins/wp-super-cache/faq/">Troubleshooting section</a> of the readme.txt</p>
208 </div>
209 <?php
210 } else {
211 wp_cache_replace_line('^ *\$wp_cache_cron_check', "\$wp_cache_cron_check = 1;", $wp_cache_config_file);
212 }
213 }
214 }
215 }
216
217 if( $cache_enabled == true && $super_cache_enabled == true && !got_mod_rewrite() ) {
218 ?><h4 style='color: #a00'>Mod rewrite may not be installed!</h4>
219 <p>It appears that mod_rewrite is not installed. Sometimes this check isn't 100% reliable, especially if you are not using Apache. Please verify that the mod_rewrite module is loaded. It is required for serving Super Cache static files. You will still be able to use WP-Cache.</p><?php
220 }
221
222 if( !is_writeable_ACLSafe($wp_cache_config_file) ) {
223 define( "SUBMITDISABLED", 'disabled style="color: #aaa" ' );
224 ?><h4 style='text-align:center; color: #a00'>Read Only Mode. Configuration cannot be changed. <a href="javascript:toggleLayer('readonlywarning');" title="Why your configuration may not be changed">Why</a></h4>
225 <div id='readonlywarning' style='border: 1px solid #aaa; margin: 2px; padding: 2px; display: none;'>
226 <p>The WP Super Cache configuration file is <code><?php echo WP_CONTENT_DIR; ?>/wp-cache-config.php</code> and cannot be modified. The file <?php echo WP_CONTENT_DIR; ?>/wp-cache-config.php must be writeable by the webserver to make any changes.
227 A simple way of doing that is by changing the permissions temporarily using the CHMOD command or through your ftp client. Make sure it's globally writeable and it should be fine.
228 Writeable: <code>chmod 666 <?php echo WP_CONTENT_DIR; ?>/wp-cache-config.php</code>
229 Readonly: <code>chmod 644 <?php echo WP_CONTENT_DIR; ?>/wp-cache-config.php</code></p>
230 </div><?php
231 } else {
232 define( "SUBMITDISABLED", ' ' );
233 }
234
235 // Server could be running as the owner of the wp-content directory. Therefore, if it's
236 // writable, issue a warning only if the permissions aren't 755.
237 if( is_writeable_ACLSafe( WP_CONTENT_DIR . '/' ) ) {
238 $wp_content_stat = stat(WP_CONTENT_DIR . '/');
239 $wp_content_mode = ($wp_content_stat['mode'] & 0777);
240 if( $wp_content_mode != 0755 ) {
241 ?><h4 style='text-align:center; color: #a00'>Warning! <?php echo WP_CONTENT_DIR; ?> is writeable!</h4>
242 <p>You should change the permissions on <?php echo WP_CONTENT_DIR; ?> and make it more restrictive. Use your ftp client, or the following command to fix things:<code>chmod 755 <?php echo WP_CONTENT_DIR; ?>/</code></p><?php
243 }
244 }
245
246 if ( $valid_nonce ) {
247 if( isset( $_POST[ 'wp_cache_status' ] ) ) {
248 if( isset( $_POST[ 'wp_cache_mobile_enabled' ] ) ) {
249 $wp_cache_mobile_enabled = 1;
250 } else {
251 $wp_cache_mobile_enabled = 0;
252 }
253 if( $wp_cache_mobile_enabled == 1 ) {
254 if( !isset( $wp_cache_mobile_whitelist ) )
255 wp_cache_replace_line('^ *\$wp_cache_mobile_whitelist', "\$wp_cache_mobile_whitelist = 'Stand Alone/QNws';", $wp_cache_config_file);
256 if( false == isset( $wp_cache_mobile_browsers ) )
257 wp_cache_replace_line('^ *\$wp_cache_mobile_browsers', "\$wp_cache_mobile_browsers = 'Android, 2.0 MMP, 240x320, AvantGo, BlackBerry, Blazer, Cellphone, Danger, DoCoMo, Elaine/3.0, EudoraWeb, hiptop, IEMobile, iPhone, iPod, KYOCERA/WX310K, LG/U990, MIDP-2.0, MMEF20, MOT-V, NetFront, Newt, Nintendo Wii, Nitro, Nokia, Opera Mini, Palm, Playstation Portable, portalmmm, Proxinet, ProxiNet, SHARP-TQ-GX10, Small, SonyEricsson, Symbian OS, SymbianOS, TS21i-10, UP.Browser, UP.Link, Windows CE, WinWAP';", $wp_cache_config_file);
258 }
259 wp_cache_replace_line('^ *\$wp_cache_mobile_enabled', "\$wp_cache_mobile_enabled = " . $wp_cache_mobile_enabled . ";", $wp_cache_config_file);
260 switch( $_POST[ 'wp_cache_status' ] ) {
261 case 'all':
262 wp_cache_enable();
263 break;
264 case 'none':
265 wp_cache_disable();
266 break;
267 case 'wpcache':
268 wp_cache_enable();
269 wp_super_cache_disable();
270 break;
271 }
272 if( isset( $_POST[ 'wp_cache_hello_world' ] ) ) {
273 $wp_cache_hello_world = 1;
274 } else {
275 $wp_cache_hello_world = 0;
276 }
277 wp_cache_replace_line('^ *\$wp_cache_hello_world', '$wp_cache_hello_world = ' . (int)$wp_cache_hello_world . ";", $wp_cache_config_file);
278 if( isset( $_POST[ 'wp_cache_clear_on_post_edit' ] ) ) {
279 $wp_cache_clear_on_post_edit = 1;
280 } else {
281 $wp_cache_clear_on_post_edit = 0;
282 }
283 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);
284 if( isset( $_POST[ 'cache_rebuild_files' ] ) ) {
285 $cache_rebuild_files = 1;
286 } else {
287 $cache_rebuild_files = 0;
288 }
289 wp_cache_replace_line('^ *\$cache_rebuild_files', "\$cache_rebuild_files = " . $cache_rebuild_files . ";", $wp_cache_config_file);
290 if( isset( $_POST[ 'wp_cache_mutex_disabled' ] ) ) {
291 $wp_cache_mutex_disabled = 0;
292 } else {
293 $wp_cache_mutex_disabled = 1;
294 }
295 if( defined( 'WPSC_DISABLE_LOCKING' ) ) {
296 $wp_cache_mutex_disabled = 1;
297 }
298 wp_cache_replace_line('^ *\$wp_cache_mutex_disabled', "\$wp_cache_mutex_disabled = " . $wp_cache_mutex_disabled . ";", $wp_cache_config_file);
299 if( isset( $_POST[ 'wp_cache_not_logged_in' ] ) ) {
300 if( $wp_cache_not_logged_in == 0 && function_exists( 'prune_super_cache' ) )
301 prune_super_cache ($cache_path, true);
302 $wp_cache_not_logged_in = 1;
303 } else {
304 $wp_cache_not_logged_in = 0;
305 }
306 wp_cache_replace_line('^ *\$wp_cache_not_logged_in', "\$wp_cache_not_logged_in = " . $wp_cache_not_logged_in . ";", $wp_cache_config_file);
307 }
308 if( defined( 'WPSC_DISABLE_COMPRESSION' ) ) {
309 $cache_compression_changed = false;
310 $cache_compression = 0;
311 wp_cache_replace_line('^ *\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
312 } elseif( isset( $_POST[ 'cache_compression' ] ) && $_POST[ 'cache_compression' ] != $cache_compression ) {
313 $cache_compression_changed = true;
314 $cache_compression = intval( $_POST[ 'cache_compression' ] );
315 wp_cache_replace_line('^ *\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
316 if( function_exists( 'prune_super_cache' ) )
317 prune_super_cache ($cache_path, true);
318 delete_option( 'super_cache_meta' );
319 }
320 if( isset( $_POST[ 'wp_cache_hide_donation' ] ) && $_POST[ 'wp_cache_hide_donation' ] != $wp_cache_hide_donation ) {
321 $wp_cache_hide_donation = intval( $_POST[ 'wp_cache_hide_donation' ] );
322 wp_cache_replace_line('^ *\$wp_cache_hide_donation', "\$wp_cache_hide_donation = " . $wp_cache_hide_donation . ";", $wp_cache_config_file);
323 }
324 }
325
326 ?>
327 <table><td><fieldset class="options" id="show-this-fieldset">
328 <h3>WP Super Cache Status</h3><?php
329 echo '<form name="wp_manager" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
330 ?>
331 <label><input type='radio' name='wp_cache_status' value='all' <?php if( $cache_enabled == true && $super_cache_enabled == true ) { echo 'checked=checked'; } ?>> <strong>ON</strong> <span class="setting-description">WP Cache and Super Cache enabled</span></label><br />
332 <label><input type='radio' name='wp_cache_status' value='wpcache' <?php if( $cache_enabled == true && $super_cache_enabled == false ) { echo 'checked=checked'; } ?>> <strong>HALF ON</strong> <span class="setting-description">Super Cache Disabled, only legacy WP-Cache caching.</span></label><br />
333 <label><input type='radio' name='wp_cache_status' value='none' <?php if( $cache_enabled == false ) { echo 'checked=checked'; } ?>> <strong>OFF</strong> <span class="setting-description">WP Cache and Super Cache disabled</span></label><br />
334 <p><label><input type='checkbox' name='wp_cache_not_logged_in' <?php if( $wp_cache_not_logged_in ) echo "checked"; ?> value='1'> Don't cache pages for logged in users.</label></p>
335 <p><label><input type='checkbox' name='wp_cache_hello_world' <?php if( $wp_cache_hello_world ) echo "checked"; ?> value='1'> Proudly tell the world your server is Digg proof! (places a message in your blog's footer)</label></p>
336 <p><label><input type='checkbox' name='wp_cache_clear_on_post_edit' <?php if( $wp_cache_clear_on_post_edit ) echo "checked"; ?> value='1'> Clear all cache files when a post or page is published. (This may significantly slow down saving of posts.)</label></p>
337 <p><label><input type='checkbox' name='cache_rebuild_files' <?php if( $cache_rebuild_files ) echo "checked"; ?> value='1'> Cache rebuild. Serve a supercache file to anonymous users while a new file is being generated. Recommended for <em>very</em> busy websites with lots of comments. Makes "directly cached pages" and "Lockdown mode" obsolete.</label></p>
338 <?php if( false == defined( 'WPSC_DISABLE_LOCKING' ) ) { ?>
339 <p><label><input type='checkbox' name='wp_cache_mutex_disabled' <?php if( !$wp_cache_mutex_disabled ) echo "checked"; ?> value='0'> Coarse file locking. You probably don'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></label></p>
340 <?php } ?>
341 <p><label><input type='checkbox' name='wp_cache_mobile_enabled' <?php if( $wp_cache_mobile_enabled ) echo "checked"; ?> value='1'> Mobile device support.</label>
342 <?php
343 $home_path = trailingslashit( get_home_path() );
344 if ( false === strpos( implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WPSuperCache' ) ), 'SHARP-TQ-GX10' ) ) { // we don't have the rewrite rules
345 ?>
346 <blockquote><p>Mobile support requires extra rules in your .htaccess file, or you can set the plugin to half-on mode. Here's your options (in order of difficulty):
347 <ol><li> 1. Set the plugin to half on mode and enable mobile support.</li>
348 <li> 2. Delete the plugin mod_rewrite rules in <?php echo $home_path; ?>.htaccess enclosed by <code># BEGIN WPSuperCache</code> and <code># END WPSuperCache</code> and let the plugin regenerate them by reloading this page.</li>
349 <li> 3. Add the rules yourself. Edit <?php echo $home_path; ?>.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|wp-postpass_).*$</code> add this line: (do it twice, once for each section)</p>
350 <div style='border: 1px solid #333; width:400px; overflow: scroll'><pre>RewriteCond %{HTTP_user_agent} !^.*(Android|2.0\ MMP|240x320|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|hiptop|IEMobile|iPhone|iPod|KYOCERA/WX310K|LG/U990|MIDP-2.0|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|Playstation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|Windows\ CE|WinWAP).*</pre></div></li></ol></blockquote>
351 <?php } ?>
352 <p><strong>Note:</strong> If uninstalling this plugin, make sure the directory <em><?php echo WP_CONTENT_DIR; ?></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!)</p>
353 <?php
354 echo "<div class='submit'><input type='submit' " . SUBMITDISABLED . " value='Update Status &raquo;' /></div>";
355 wp_nonce_field('wp-cache');
356 ?>
357 </form>
358 <?php
359 if( $super_cache_enabled && function_exists( 'apache_get_modules' ) ) {
360 $mods = apache_get_modules();
361 $required_modules = array( 'mod_mime' => 'Required to serve compressed supercache files properly.', 'mod_headers' => 'Required to set caching information on supercache pages. IE7 users will see old pages without this module.', 'mod_expires' => 'Set the expiry date on supercached pages. Visitors may not see new pages when they refresh or leave comments without this module.' );
362 foreach( $required_modules as $req => $desc ) {
363 if( !in_array( $req, $mods ) ) {
364 $missing_mods[ $req ] = $desc;
365 }
366 }
367 if( isset( $missing_mods) && is_array( $missing_mods ) ) {
368 echo "<h3>Missing Apache Modules</h3>";
369 echo "<p>The following Apache modules are missing. The plugin will work in half-on mode without them. In full Supercache mode, your visitors may see corrupted pages or out of date content however.</p>";
370 echo "<ul>";
371 foreach( $missing_mods as $req => $desc ) {
372 echo "<li> $req - $desc</li>";
373 }
374 echo "</ul>";
375 }
376 }
377 ?>
378 </fieldset>
379 </td><td valign='top'>
380 <div style='background: #ffc; border: 1px solid #333; margin: 2px; padding: 5px'>
381 <h3 align='center'>Make WordPress Faster</h3>
382 <?php if( $wp_cache_hide_donation != 1 ) { ?>
383 <p><a href="http://ocaoimh.ie/wp-super-cache/?r=wpsc">WP Super Cache</a> really makes your blog go faster. Make it go faster<sup>*</sup> by buying me an <a href="https://www.amazon.co.uk/gp/gc/order-email/ref=g_gc-dp_lp_email">Amazon gift card</a>! Make it out to "donncha@ocaoimh.ie". A £10 card would be nice but it's up to you how much you think this plugin is worth to you.</p>
384 <p>If Amazon isn't your thing, there's also PayPal. Click the "Donate" button below or take a quick peek at my <a href="http://ocaoimh.ie/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy5hbWF6b24uY28udWsvZ3AvcmVnaXN0cnkvMzVaQ0dJVTJBSDROQg==">wishlist</a>.</p>
385 <p>Thanks in advance!<br />Donncha<br />
386 <small>* Ok, it won't go any faster but you'll make this plugin author very happy!</small></p>
387 <div align='center'>
388 <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
389 <input type="hidden" name="cmd" value="_s-xclick"/>
390 <input type="hidden" name="hosted_button_id" value="3244504"/>
391 <input type="image" src="https://www.paypal.com/en_GB/i/btn/btn_donate_SM.gif" border="0" name="submit" alt=""/>
392 <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"/><br />
393 </form>
394 <p>Don't show me this again. <form action="<?php echo $_SERVER["REQUEST_URI"]; ?>" method="post"><input type='hidden' name='wp_cache_hide_donation' value='1' /><input type='submit' value='Hide' /><?php wp_nonce_field('wp-cache'); ?></form></p>
395 </div>
396 <?php } else { ?>
397 <p><a href="http://ocaoimh.ie/wp-super-cache/?r=supercache">WP Super Cache</a> is maintained and developed by <a href="http://ocaoimh.ie/?r=supercache">Donncha O Caoimh</a> with contributions from many others.</p>
398 <p>He blogs at <a href="http://ocaoimh.ie/?r=supercache">Holy Shmoly</a>, posts photos at <a href="http://inphotos.org/?r=supercache">In Photos.org</a> and <a href="http://ocaoimh.ie/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy5hbWF6b24uY28udWsvZ3AvcmVnaXN0cnkvMzVaQ0dJVTJBSDROQg==">wishes</a> he had more time to read and relax.</p><p>Please say hi to him on <a href="http://twitter.com/donncha/">Twitter</a> too!</p>
399 <?php } ?>
400 </div>
401
402 </td></table>
403 <?php
404
405 wp_cache_files();
406
407 wsc_mod_rewrite();
408
409 wp_cache_edit_max_time();
410
411 echo '<a name="files"></a><fieldset class="options"><h3>Accepted Filenames &amp; Rejected URIs</h3>';
412 wp_cache_edit_rejected();
413 echo "\n";
414 wp_cache_edit_accepted();
415 echo '</fieldset>';
416
417 wp_cache_edit_rejected_ua();
418
419
420 wp_lock_down();
421
422 wp_cache_restore();
423
424 ob_start();
425 if( defined( 'WP_CACHE' ) ) {
426 if( function_exists( 'do_cacheaction' ) ) {
427 do_cacheaction( 'cache_admin_page' );
428 }
429 }
430 $out = ob_get_contents();
431 ob_end_clean();
432 if( SUBMITDISABLED == ' ' && $out != '' ) {
433 echo '<fieldset class="options"><h3>Cache Plugins</h3>';
434 echo $out;
435 echo '</fieldset>';
436 }
437
438 echo "</div>\n";
439}
440
441function wsc_mod_rewrite() {
442 global $super_cache_enabled, $cache_compression, $cache_compression_changed, $valid_nonce, $cache_path;
443 if( $super_cache_enabled == false )
444 return;
445 if( false == defined( 'WPSC_DISABLE_COMPRESSION' ) ) {
446 ?>
447 <fieldset class="options">
448 <h3>Super Cache Compression</h3>
449 <form name="wp_manager" action="<?php echo $_SERVER["REQUEST_URI"]; ?>" method="post">
450 <label><input type="radio" name="cache_compression" value="1" <?php if( $cache_compression ) { echo "checked=checked"; } ?>> Enabled</label>
451 <label><input type="radio" name="cache_compression" value="0" <?php if( !$cache_compression ) { echo "checked=checked"; } ?>> Disabled</label>
452 <p>Compression is disabled by default because some hosts have problems with compressed files. Switching this on and off clears the cache.</p>
453 <?php
454 if( isset( $cache_compression_changed ) && isset( $_POST[ 'cache_compression' ] ) && !$cache_compression ) {
455 ?><p><strong>Super Cache compression is now disabled.</strong></p> <?php
456 } elseif( isset( $cache_compression_changed ) && isset( $_POST[ 'cache_compression' ] ) && $cache_compression ) {
457 ?><p><strong>Super Cache compression is now enabled.</strong></p><?php
458 }
459 echo '<div class="submit"><input ' . SUBMITDISABLED . 'type="submit" value="Update Compression &raquo;" /></div>';
460 wp_nonce_field('wp-cache');
461 echo "</form>\n";
462 ?></fieldset>
463 <?php } ?>
464
465 <a name="modrewrite"></a><fieldset class="options">
466 <h3>Mod Rewrite Rules</h3><?php
467 if ( isset( $_SERVER[ "PHP_DOCUMENT_ROOT" ] ) ) {
468 $document_root = $_SERVER[ "PHP_DOCUMENT_ROOT" ];
469 $apache_root = $_SERVER[ "PHP_DOCUMENT_ROOT" ];
470 } else {
471 $document_root = $_SERVER[ "DOCUMENT_ROOT" ];
472 $apache_root = '%{DOCUMENT_ROOT}';
473 }
474 $home_path = get_home_path();
475 $home_root = parse_url(get_bloginfo('url'));
476 $home_root = trailingslashit($home_root['path']);
477 $home_root = isset( $home_root['path'] ) ? trailingslashit( $home_root['path'] ) : '/';
478 $inst_root = str_replace( '//', '/', '/' . trailingslashit( str_replace( $document_root, '', str_replace( '\\', '/', WP_CONTENT_DIR ) ) ) );
479 $wprules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WordPress' ) );
480 $wprules = str_replace( "RewriteEngine On\n", '', $wprules );
481 $wprules = str_replace( "RewriteBase $home_root\n", '', $wprules );
482 $scrules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WPSuperCache' ) );
483
484 if( substr( get_option( 'permalink_structure' ), -1 ) == '/' ) {
485 $condition_rules[] = "RewriteCond %{REQUEST_URI} !^.*[^/]$";
486 $condition_rules[] = "RewriteCond %{REQUEST_URI} !^.*//.*$";
487 }
488 $condition_rules[] = "RewriteCond %{REQUEST_METHOD} !=POST";
489 $condition_rules[] = "RewriteCond %{QUERY_STRING} !.*=.*";
490 $condition_rules[] = "RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$";
491 $condition_rules[] = "RewriteCond %{HTTP_user_agent} !^.*(Android|2.0\\ MMP|240x320|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|hiptop|IEMobile|iPhone|iPod|KYOCERA/WX310K|LG/U990|MIDP-2.0|MMEF20|MOT-V|NetFront|Newt|Nintendo\\ Wii|Nitro|Nokia|Opera\\ Mini|Palm|Playstation\\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|Small|SonyEricsson|Symbian\\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|Windows\\ CE|WinWAP).*";
492 $condition_rules = apply_filters( 'supercacherewriteconditions', $condition_rules );
493
494 $rules = "<IfModule mod_rewrite.c>\n";
495 $rules .= "RewriteEngine On\n";
496 $rules .= "RewriteBase $home_root\n"; // props Chris Messina
497 $charset = get_option('blog_charset') == '' ? 'UTF-8' : get_option('blog_charset');
498 $rules .= "AddDefaultCharset {$charset}\n";
499 $rules .= "CONDITION_RULES";
500 $rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
501 $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz -f\n";
502 $rules .= "RewriteRule ^(.*) {$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz [L]\n\n";
503
504 $rules .= "CONDITION_RULES";
505 $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html -f\n";
506 $rules .= "RewriteRule ^(.*) {$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html [L]\n";
507 $rules .= "</IfModule>\n";
508 $rules = apply_filters( 'supercacherewriterules', $rules );
509
510 $rules = str_replace( "CONDITION_RULES", implode( "\n", $condition_rules ) . "\n", $rules );
511
512 $dohtaccess = true;
513 if( function_exists( 'is_site_admin' ) ) {
514 echo "<h4 style='color: #a00'>WordPress MU Detected</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.</p>";
515 } elseif( !$wprules || $wprules == '' ) {
516 echo "<h4 style='color: #a00'>Mod Rewrite rules cannot be updated!</h4>";
517 echo "<p>You must have <strong>BEGIN</strong> and <strong>END</strong> markers in {$home_path}.htaccess for the auto update to work. They look like this and surround the main WordPress mod_rewrite rules:
518 <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>
519 Refresh this page when you have updated your .htaccess file.";
520 echo "</fieldset></div>";
521 return;
522 } elseif( strpos( $wprules, 'wordpressuser' ) ) { // Need to clear out old mod_rewrite rules
523 echo "<p><strong>Thank you for upgrading.</strong> 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: <blockquote><code>RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*\$</code></blockquote> so it looks like this: <blockquote><code>RewriteCond %{HTTP:Cookie} !^.*wordpress.*\$</code></blockquote> The only changes are 'HTTP_COOKIE' becomes 'HTTP:Cookie' and 'wordpressuser' becomes 'wordpress'. This is a WordPress 2.5 change but it's backwards compatible with older versions if you're brave enough to use them.</p>";
524 echo "</fieldset></div>";
525 return;
526 } elseif( $scrules != '' && strpos( $scrules, '%{REQUEST_URI} !^.*[^/]$' ) === false && substr( get_option( 'permalink_structure' ), -1 ) == '/' ) { // permalink structure has a trailing slash, need slash check in rules.
527 echo "<div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><h4>Trailing slash check required.</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.";
528 echo "To fix, you must edit your .htaccess file and add these two rules to the two groups of Super Cache rules:</p>";
529 echo "<blockquote><code>RewriteCond %{REQUEST_URI} !^.*[^/]$RewriteCond %{REQUEST_URI} !^.*//.*$</code></blockquote>";
530 echo "<p>You can see where the rules go and examine the complete rules by clicking the 'View mod_rewrite rules' link below.</p></div>";
531 $dohtaccess = false;
532 } elseif( strpos( $scrules, 'supercache' ) || strpos( $wprules, 'supercache' ) ) { // only write the rules once
533 $dohtaccess = false;
534 }
535 // cache/.htaccess rules
536 $gziprules = "<IfModule mod_mime.c>\n AddEncoding gzip .gz\n AddType text/html .gz\n</IfModule>\n";
537 $gziprules .= "<IfModule mod_deflate.c>\n SetEnvIfNoCase Request_URI \.gz$ no-gzip\n</IfModule>\n";
538 $gziprules .= "<IfModule mod_headers.c>\n Header set Cache-Control 'max-age=300, must-revalidate'\n</IfModule>\n";
539 $gziprules .= "<IfModule mod_expires.c>\n ExpiresActive On\n ExpiresByType text/html A300\n</IfModule>\n";
540 if( $dohtaccess && !$_POST[ 'updatehtaccess' ] ) {
541 if( !is_writeable_ACLSafe( $home_path . ".htaccess" ) ) {
542 echo "<div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><h4>Cannot update .htaccess</h4><p>The file <code>{$home_path}.htaccess</code> cannot be modified by the web server. Please correct this using the chmod command or your ftp client.</p><p>Refresh this page when the file permissions have been modified.</p><p>Alternatively, you can edit your <code>{$home_path}.htaccess</code> file manually and add the following code (before any WordPress rules):</p>";
543 echo "<p><pre># BEGIN WPSuperCache\n" . wp_specialchars( $rules ) . "# END WPSuperCache</pre></p></div>";
544 } else {
545 echo "<div style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><p>To serve static html files your server must have the correct mod_rewrite rules added to a file called <code>{$home_path}.htaccess</code> ";
546 if( !function_exists( 'is_site_admin' ) ) {
547 echo "You must edit the file yourself add the following rules.";
548 } else {
549 echo "You can edit the file yourself add the following rules.";
550 }
551 echo " Make sure they appear before any existing WordPress rules.</p>";
552 echo "<pre># BEGIN WPSuperCache\n" . wp_specialchars( $rules ) . "# END WPSuperCache</pre></p>";
553 echo "<p>Rules must be added to " . WP_CONTENT_DIR . "/cache/.htaccess too:</p>";
554 echo "<pre># BEGIN supercache\n" . wp_specialchars( $gziprules ) . "# END supercache</pre></p>";
555 if( !function_exists( 'is_site_admin' ) ) {
556 echo '<form name="updatehtaccess" action="'. $_SERVER["REQUEST_URI"] . '#modrewrite" method="post">';
557 echo '<input type="hidden" name="updatehtaccess" value="1" />';
558 echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'id="updatehtaccess" value="Update Mod_Rewrite Rules &raquo;" /></div>';
559 wp_nonce_field('wp-cache');
560 echo "</form></div>\n";
561 }
562 }
563 } elseif( $dohtaccess && $valid_nonce && $_POST[ 'updatehtaccess' ] ) {
564 wpsc_remove_marker( $home_path.'.htaccess', 'WordPress' ); // remove original WP rules so SuperCache rules go on top
565 echo "<div style='padding:0 8px;color:#4f8a10;background-color:#dff2bf;border:1px solid #4f8a10;'>";
566 if( insert_with_markers( $home_path.'.htaccess', 'WPSuperCache', explode( "\n", $rules ) ) && insert_with_markers( $home_path.'.htaccess', 'WordPress', explode( "\n", $wprules ) ) ) {
567 echo "<h4>Mod Rewrite rules updated!</h4>";
568 echo "<p><strong>{$home_path}.htaccess has been updated with the necessary mod_rewrite rules. Please verify they are correct. They should look like this:</strong></p>\n";
569 } else {
570 echo "<h4>Mod Rewrite rules must be updated!</h4>";
571 echo "<p><strong> Your {$home_path}.htaccess is not writable by the webserver and must be updated with the necessary mod_rewrite rules. The new rules go above the regular WordPress rules as shown in the code below:</strong></p>\n";
572 }
573 echo "<p><pre>" . wp_specialchars( $rules ) . "</pre></p>\n</div>";
574 } else {
575 ?>
576 <p>WP Super Cache mod rewrite rules were detected in your <?php echo $home_path ?>.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.<br /><br />
577 <a href="javascript:toggleLayer('rewriterules');" class="button">View Mod_Rewrite Rules</a>
578 <div id='rewriterules' style='display: none;'>
579 <?php echo "<p><pre># BEGIN WPSuperCache\n" . wp_specialchars( $rules ) . "# END WPSuperCache</pre></p>\n";
580 echo "<p>Rules must be added to " . WP_CONTENT_DIR ."/cache/.htaccess too:</p>";
581 echo "<pre># BEGIN supercache\n" . wp_specialchars( $gziprules ) . "# END supercache</pre></p>"; ?>
582 </div>
583 <?php
584 }
585 // http://allmybrain.com/2007/11/08/making-wp-super-cache-gzip-compression-work/
586 if( !is_file( $cache_path . '.htaccess' ) ) {
587 $gziprules = insert_with_markers( $cache_path . '.htaccess', 'supercache', explode( "\n", $gziprules ) );
588 echo "<h4>Gzip encoding rules in {$cache_path}.htaccess created.</h4>";
589 }
590
591 ?></fieldset><?php
592}
593
594function wp_cache_restore() {
595 echo '<fieldset class="options"><h3>Fix Configuration</h3>';
596 echo '<form name="wp_restore" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
597 echo '<input type="hidden" name="wp_restore_config" />';
598 echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'id="deletepost" value="Restore Default Configuration &raquo;" /></div>';
599 wp_nonce_field('wp-cache');
600 echo "</form>\n";
601 echo '</fieldset>';
602
603}
604
605function comment_form_lockdown_message() {
606 ?><p><?php _e( "Comment moderation is enabled. Your comment may take some time to appear." ); ?></p><?php
607}
608if( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) )
609 add_action( 'comment_form', 'comment_form_lockdown_message' );
610
611function wp_lock_down() {
612 global $wpdb, $cache_path, $wp_cache_config_file, $valid_nonce, $cached_direct_pages, $cache_enabled, $super_cache_enabled;
613
614 if(isset($_POST['wp_lock_down']) && $valid_nonce) {
615 $wp_lock_down = $_POST['wp_lock_down'] == '1' ? '1' : '0';
616 wp_cache_replace_line('^.*WPLOCKDOWN', "define( 'WPLOCKDOWN', '$wp_lock_down' );", $wp_cache_config_file);
617 if( $wp_lock_down == '0' && function_exists( 'prune_super_cache' ) )
618 prune_super_cache( $cache_path, true ); // clear the cache after lockdown
619
620 }
621 if( !isset( $wp_lock_down ) ) {
622 if( defined( 'WPLOCKDOWN' ) ) {
623 $wp_lock_down = constant( 'WPLOCKDOWN' );
624 } else {
625 $wp_lock_down = '0';
626 }
627 }
628 ?><fieldset class="options">
629 <h3>Lock Down: <?php echo $wp_lock_down == '0' ? '<span style="color:red">Disabled</span>' : '<span style="color:green">Enabled</span>'; ?></h3>
630 <p>Prepare your server for an expected spike in traffic by enabling the lock down. When this is enabled, new comments on a post will not refresh the cached static files.</p>
631 <p>Developers: Make your plugin lock down compatible by checking the 'WPLOCKDOWN' constant. The following code will make sure your plugin respects the WPLOCKDOWN setting.
632 <blockquote><code>if( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) ) {
633 &nbsp;&nbsp;&nbsp;&nbsp;echo "Sorry. My blog is locked down. Updates will appear shortly";
634 }</code></blockquote>
635 <?php
636 if( $wp_lock_down == '1' ) {
637 ?><p>WordPress is locked down. Super Cache static files will not be deleted when new comments are made.</p><?php
638 } else {
639 ?><p>WordPress is not locked down. New comments will refresh Super Cache static files as normal.</p><?php
640 }
641 $new_lockdown = $wp_lock_down == '1' ? '0' : '1';
642 $new_lockdown_desc = $wp_lock_down == '1' ? 'Disable' : 'Enable';
643 echo '<form name="wp_lock_down" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
644 echo "<input type='hidden' name='wp_lock_down' value='{$new_lockdown}' />";
645 echo "<div class='submit'><input type='submit' " . SUBMITDISABLED . " value='{$new_lockdown_desc} Lock Down &raquo;' /></div>";
646 wp_nonce_field('wp-cache');
647 echo "</form>\n";
648
649 ?></fieldset><?php
650 if( $cache_enabled == true && $super_cache_enabled == true ) {
651 ?><fieldset class="options">
652 <h3>Directly Cached Files</h3><?php
653
654 $out = '';
655 if( $valid_nonce && is_array( $_POST[ 'direct_pages' ] ) && !empty( $_POST[ 'direct_pages' ] ) ) {
656 $expiredfiles = array_diff( $cached_direct_pages, $_POST[ 'direct_pages' ] );
657 unset( $cached_direct_pages );
658 foreach( $_POST[ 'direct_pages' ] as $page ) {
659 $page = $wpdb->escape( $page );
660 if( $page != '' ) {
661 $cached_direct_pages[] = $page;
662 $out .= "'$page', ";
663 }
664 }
665 if( $out == '' ) {
666 $out = "'', ";
667 }
668 }
669 if( $valid_nonce && $_POST[ 'new_direct_page' ] && '' != $_POST[ 'new_direct_page' ] ) {
670 $page = str_replace( get_option( 'siteurl' ), '', $_POST[ 'new_direct_page' ] );
671 if( substr( $page, 0, 1 ) != '/' )
672 $page = '/' . $page;
673 $page = $wpdb->escape( $page );
674 if( in_array( $page, $cached_direct_pages ) == false ) {
675 $cached_direct_pages[] = $page;
676 $out .= "'$page', ";
677 }
678 }
679
680 if( $out != '' ) {
681 $out = substr( $out, 0, -2 );
682 $out = '$cached_direct_pages = array( ' . $out . ' );';
683 wp_cache_replace_line('^ *\$cached_direct_pages', "$out", $wp_cache_config_file);
684 prune_super_cache( $cache_path, true );
685 }
686
687 if( !empty( $expiredfiles ) ) {
688 foreach( $expiredfiles as $file ) {
689 if( $file != '' ) {
690 $firstfolder = explode( '/', $file );
691 $firstfolder = ABSPATH . $firstfolder[1];
692 $file = ABSPATH . $file;
693 @unlink( trailingslashit( $file ) . 'index.html' );
694 @unlink( trailingslashit( $file ) . 'index.html.gz' );
695 RecursiveFolderDelete( trailingslashit( $firstfolder ) );
696 }
697 }
698 }
699
700 if( $valid_nonce && $_POST[ 'deletepage' ] ) {
701 $page = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '..', '', $_POST['deletepage']) );
702 $pagefile = ABSPATH . $page . 'index.html';
703 $firstfolder = explode( '/', $page );
704 $firstfolder = ABSPATH . $firstfolder[1];
705 $page = ABSPATH . $page;
706 if( is_file( $pagefile ) && is_writeable_ACLSafe( $pagefile ) && is_writeable_ACLSafe( $firstfolder ) ) {
707 @unlink( $pagefile );
708 @unlink( $pagefile . '.gz' );
709 RecursiveFolderDelete( $firstfolder );
710 echo "<strong>$pagefile removed!</strong>";
711 prune_super_cache( $cache_path, true );
712 }
713 }
714
715 $readonly = '';
716 if( !is_writeable_ACLSafe( ABSPATH ) ) {
717 $readonly = 'READONLY';
718 ?><p style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><strong>Warning!</strong> You must make <?php echo ABSPATH ?> writable to enable this feature. As this is a security risk please make it readonly after your page is generated.</p><?php
719 } else {
720 ?><p style='padding:0 8px;color:#9f6000;background-color:#feefb3;border:1px solid #9f6000;'><strong>Warning!</strong> <?php echo ABSPATH ?> is writable. Please make it readonly after your page is generated as this is a security risk.</p><?php
721 }
722 echo '<form name="direct_page" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
723 if( is_array( $cached_direct_pages ) ) {
724 $out = '';
725 foreach( $cached_direct_pages as $page ) {
726 if( $page == '' )
727 continue;
728 $generated = '';
729 if( is_file( ABSPATH . $page . '/index.html' ) )
730 $generated = '<input type="Submit" name="deletepage" value="' . $page . '">';
731 $out .= "<tr><td><input type='text' $readonly name='direct_pages[]' size='30' value='$page' /></td><td>$generated</td></tr>";
732 }
733 if( $out != '' ) {
734 ?><table><tr><th>Existing direct page</th><th>Delete cached file</th></tr><?php
735 echo "$out</table>";
736 }
737 }
738 if( $readonly != 'READONLY' )
739 echo "Add direct page: <input type='text' $readonly name='new_direct_page' size='30' value='' />";
740
741 echo "<p>Directly cached files are files created directly off " . ABSPATH . " where your blog lives. This feature is only useful if you are expecting a major Digg or Slashdot level of traffic to one post or page.</p>";
742 if( $readonly != 'READONLY' ) {
743 echo "<p>For example: to cache <em>'" . trailingslashit( get_option( 'siteurl' ) ) . "about/'</em>, you would enter '" . trailingslashit( get_option( 'siteurl' ) ) . "about/' or '/about/'. The cached file will be generated the next time an anonymous user visits that page.</p>";
744 echo "<p>Make the textbox blank to remove it from the list of direct pages and delete the cached file.</p>";
745 }
746
747 wp_nonce_field('wp-cache');
748 if( $readonly != 'READONLY' )
749 echo "<div class='submit'><input type='submit' ' . SUBMITDISABLED . 'value='Update Direct Pages &raquo;' /></div>";
750 echo "</form>\n";
751 ?></fieldset><?php
752 } // if $super_cache_enabled
753}
754
755function RecursiveFolderDelete ( $folderPath ) { // from http://www.php.net/manual/en/function.rmdir.php
756 if( trailingslashit( constant( 'ABSPATH' ) ) == trailingslashit( $folderPath ) )
757 return false;
758 if ( @is_dir ( $folderPath ) ) {
759 $dh = @opendir($folderPath);
760 while (false !== ($value = @readdir($dh))) {
761 if ( $value != "." && $value != ".." ) {
762 $value = $folderPath . "/" . $value;
763 if ( @is_dir ( $value ) ) {
764 RecursiveFolderDelete ( $value );
765 }
766 }
767 }
768 return @rmdir ( $folderPath );
769 } else {
770 return FALSE;
771 }
772}
773
774function wp_cache_edit_max_time () {
775 global $cache_max_time, $wp_cache_config_file, $valid_nonce, $cache_enabled, $super_cache_enabled;
776
777 if( !isset( $cache_max_time ) )
778 $cache_max_time = 3600;
779
780 if(isset($_POST['wp_max_time']) && $valid_nonce) {
781 $max_time = (int)$_POST['wp_max_time'];
782 if ($max_time > 0) {
783 $cache_max_time = $max_time;
784 wp_cache_replace_line('^ *\$cache_max_time', "\$cache_max_time = $cache_max_time;", $wp_cache_config_file);
785 }
786 }
787 ?><fieldset class="options">
788 <h3>Expiry Time &amp; Garbage Collection</h3><?php
789 echo '<form name="wp_edit_max_time" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
790 echo '<label for="wp_max_time">Expire time:</label> ';
791 echo "<input type=\"text\" size=6 name=\"wp_max_time\" value=\"$cache_max_time\" /> seconds";
792 echo "<h4>Garbage Collection</h4><p>If expiry time is more than 1800 seconds (half an hour), garbage collection will be done every 10 minutes, otherwise it will happen 10 seconds after the expiry time above.</p>";
793 echo "<p>Checking for and deleting expired files is expensive, but it's expensive leaving them there too. On a very busy site you should set the expiry time to <em>300 seconds</em>. Experiment with different values and visit this page to see how many expired files remain at different times during the day. Aim to have less than 500 cached files if possible.</p>";
794 echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Change Expiration &raquo;" /></div>';
795 wp_nonce_field('wp-cache');
796 echo "</form>\n";
797 ?></fieldset><?php
798}
799
800function wp_cache_sanitize_value($text, & $array) {
801 $text = wp_specialchars(strip_tags($text));
802 $array = preg_split("/[\s,]+/", chop($text));
803 $text = var_export($array, true);
804 $text = preg_replace('/[\s]+/', ' ', $text);
805 return $text;
806}
807
808// from tehjosh at gamingg dot net http://uk2.php.net/manual/en/function.apache-request-headers.php#73964
809// fixed bug in second substr()
810if( !function_exists('apache_request_headers') ) {
811 function apache_request_headers() {
812 $headers = array();
813 foreach(array_keys($_SERVER) as $skey) {
814 if(substr($skey, 0, 5) == "HTTP_") {
815 $headername = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($skey, 5)))));
816 $headers[$headername] = $_SERVER[$skey];
817 }
818 }
819 return $headers;
820 }
821}
822
823function wp_cache_edit_rejected_ua() {
824 global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;
825
826 if (!function_exists('apache_request_headers')) return;
827
828 if(isset($_REQUEST['wp_rejected_user_agent']) && $valid_nonce) {
829 $text = wp_cache_sanitize_value($_REQUEST['wp_rejected_user_agent'], $cache_rejected_user_agent);
830 wp_cache_replace_line('^ *\$cache_rejected_user_agent', "\$cache_rejected_user_agent = $text;", $wp_cache_config_file);
831 }
832
833 echo '<a name="user-agents"></a><fieldset class="options"><h3>Rejected User Agents</h3>';
834 echo "<p>Strings in the HTTP 'User Agent' header that prevent WP-Cache from
835 caching bot, spiders, and crawlers' requests.
836 Note that super cached files are still sent to these agents if they already exists.</p>\n";
837 echo '<form name="wp_edit_rejected_user_agent" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
838 echo '<textarea name="wp_rejected_user_agent" cols="40" rows="4" style="width: 50%; font-size: 12px;" class="code">';
839 foreach ($cache_rejected_user_agent as $ua) {
840 echo wp_specialchars($ua) . "\n";
841 }
842 echo '</textarea> ';
843 echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Save UA Strings &raquo;" /></div>';
844 wp_nonce_field('wp-cache');
845 echo '</form>';
846 echo "</fieldset>\n";
847}
848
849
850function wp_cache_edit_rejected() {
851 global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
852
853 if(isset($_REQUEST['wp_rejected_uri']) && $valid_nonce) {
854 $text = wp_cache_sanitize_value( str_replace( '\\\\', '\\', $_REQUEST['wp_rejected_uri'] ), $cache_rejected_uri );
855 wp_cache_replace_line('^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file);
856 }
857
858
859 echo '<form name="wp_edit_rejected" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
860 echo "<p>Add here strings (not a filename) that forces a page not to be cached. For example, if your URLs include year and you dont want to cache last year posts, it's enough to specify the year, i.e. '/2004/'. WP-Cache will search if that string is part of the URI and if so, it will not cache that page.</p>\n";
861 echo '<textarea name="wp_rejected_uri" cols="40" rows="4" style="width: 50%; font-size: 12px;" class="code">';
862 foreach ($cache_rejected_uri as $file) {
863 echo wp_specialchars( $file ) . "\n";
864 }
865 echo '</textarea> ';
866 echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Save Strings &raquo;" /></div>';
867 wp_nonce_field('wp-cache');
868 echo "</form>\n";
869}
870
871function wp_cache_edit_accepted() {
872 global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
873
874 if(isset($_REQUEST['wp_accepted_files']) && $valid_nonce) {
875 $text = wp_cache_sanitize_value($_REQUEST['wp_accepted_files'], $cache_acceptable_files);
876 wp_cache_replace_line('^ *\$cache_acceptable_files', "\$cache_acceptable_files = $text;", $wp_cache_config_file);
877 }
878
879
880 echo '<div style="clear:both"></div><form name="wp_edit_accepted" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
881 echo "<p>Add here those filenames that can be cached, even if they match one of the rejected substring specified above.</p>\n";
882 echo '<textarea name="wp_accepted_files" cols="40" rows="8" style="width: 50%; font-size: 12px;" class="code">';
883 foreach ($cache_acceptable_files as $file) {
884 echo wp_specialchars($file) . "\n";
885 }
886 echo '</textarea> ';
887 echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="Save Files &raquo;" /></div>';
888 wp_nonce_field('wp-cache');
889 echo "</form>\n";
890}
891
892function wp_cache_enable() {
893 global $wp_cache_config_file, $cache_enabled, $supercachedir;
894
895 if(get_option('gzipcompression')) {
896 echo "<strong>Error: GZIP compression is enabled, disable it if you want to enable wp-cache.</strong>";
897 return false;
898 }
899 if( wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = true;', $wp_cache_config_file) ) {
900 $cache_enabled = true;
901 }
902 wp_super_cache_enable();
903}
904
905function wp_cache_disable() {
906 global $wp_cache_config_file, $cache_enabled;
907
908 if (wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = false;', $wp_cache_config_file)) {
909 $cache_enabled = false;
910 }
911 wp_super_cache_disable();
912}
913function wp_super_cache_enable() {
914 global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
915
916 if( is_dir( $supercachedir . ".disabled" ) )
917 if( is_dir( $supercachedir ) ) {
918 prune_super_cache( $supercachedir . ".disabled", true );
919 @unlink( $supercachedir . ".disabled" );
920 } else {
921 @rename( $supercachedir . ".disabled", $supercachedir );
922 }
923 wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = true;', $wp_cache_config_file);
924 $super_cache_enabled = true;
925}
926
927function wp_super_cache_disable() {
928 global $cache_path, $supercachedir, $wp_cache_config_file, $super_cache_enabled;
929
930 wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file);
931 if( is_dir( $supercachedir ) )
932 @rename( $supercachedir, $supercachedir . ".disabled" );
933 $super_cache_enabled = false;
934 sleep( 1 ); // allow existing processes to write to the supercachedir and then delete it
935 if (function_exists ('prune_super_cache') && is_dir( $supercachedir ) ) {
936 prune_super_cache( $cache_path, true );
937 }
938}
939
940function wp_cache_is_enabled() {
941 global $wp_cache_config_file;
942
943 if(get_option('gzipcompression')) {
944 echo "<strong>Warning</strong>: GZIP compression is enabled in Wordpress, wp-cache will be bypassed until you disable gzip compression.";
945 return false;
946 }
947 $lines = file($wp_cache_config_file);
948 foreach($lines as $line) {
949 if (preg_match('/^ *\$cache_enabled *= *true *;/', $line))
950 return true;
951 }
952 return false;
953}
954
955
956function wp_cache_replace_line($old, $new, $my_file) {
957 if (!is_writeable_ACLSafe($my_file)) {
958 echo "Error: file $my_file is not writable.\n";
959 return false;
960 }
961 $found = false;
962 $lines = file($my_file);
963 foreach($lines as $line) {
964 if ( preg_match("/$old/", $line)) {
965 $found = true;
966 break;
967 }
968 }
969 if ($found) {
970 $fd = fopen($my_file, 'w');
971 foreach($lines as $line) {
972 if ( !preg_match("/$old/", $line))
973 fputs($fd, $line);
974 else {
975 fputs($fd, "$new //Added by WP-Cache Manager\n");
976 }
977 }
978 fclose($fd);
979 return true;
980 }
981 $fd = fopen($my_file, 'w');
982 $done = false;
983 foreach($lines as $line) {
984 if ( $done || !preg_match('/^(if\ \(\ \!\ )?define|\$|\?>/', $line) ) {
985 fputs($fd, $line);
986 } else {
987 fputs($fd, "$new //Added by WP-Cache Manager\n");
988 fputs($fd, $line);
989 $done = true;
990 }
991 }
992 fclose($fd);
993 return true;
994}
995
996function wp_cache_verify_cache_dir() {
997 global $cache_path, $blog_cache_dir, $blogcacheid;
998
999 $dir = dirname($cache_path);
1000 if ( !file_exists($cache_path) ) {
1001 if ( !is_writeable_ACLSafe( $dir ) || !($dir = mkdir( $cache_path ) ) ) {
1002 echo "<strong>Error:</strong> Your cache directory (<strong>$cache_path</strong>) did not exist and couldn't be created by the web server. Check $dir permissions.";
1003 return false;
1004 }
1005 }
1006 if ( !is_writeable_ACLSafe($cache_path)) {
1007 echo "<strong>Error:</strong> Your cache directory (<strong>$cache_path</strong>) or <strong>$dir</strong> need to be writable for this plugin to work. Double-check it.";
1008 return false;
1009 }
1010
1011 if ( '/' != substr($cache_path, -1)) {
1012 $cache_path .= '/';
1013 }
1014
1015 if( false == is_dir( $blog_cache_dir ) ) {
1016 @mkdir( $cache_path . "blogs" );
1017 if( $blog_cache_dir != $cache_path . "blogs/" )
1018 @mkdir( $blog_cache_dir );
1019 }
1020
1021 if( false == is_dir( $blog_cache_dir . 'meta' ) )
1022 @mkdir( $blog_cache_dir . 'meta' );
1023
1024 return true;
1025}
1026
1027function wp_cache_verify_config_file() {
1028 global $wp_cache_config_file, $wp_cache_config_file_sample, $sem_id, $cache_path;
1029
1030 $new = false;
1031 $dir = dirname($wp_cache_config_file);
1032
1033 if ( file_exists($wp_cache_config_file) ) {
1034 $lines = join( ' ', file( $wp_cache_config_file ) );
1035 if( strpos( $lines, 'WPCACHEHOME' ) === false ) {
1036 if( is_writeable_ACLSafe( $wp_cache_config_file ) ) {
1037 @unlink( $wp_cache_config_file );
1038 } else {
1039 echo "<strong>Error:</strong> Your WP-Cache config file (<strong>$wp_cache_config_file</strong>) is out of date and not writable by the Web server.Please delete it and refresh this page.";
1040 return false;
1041 }
1042 }
1043 } elseif( !is_writeable_ACLSafe($dir)) {
1044 echo "<strong>Error:</strong> Configuration file missing and " . WP_CONTENT_DIR . " directory (<strong>$dir</strong>) is not writable by the Web server.Check its permissions.";
1045 return false;
1046 }
1047
1048 if ( !file_exists($wp_cache_config_file) ) {
1049 if ( !file_exists($wp_cache_config_file_sample) ) {
1050 echo "<strong>Error:</strong> Sample WP-Cache config file (<strong>$wp_cache_config_file_sample</strong>) does not exist.Verify you installation.";
1051 return false;
1052 }
1053 copy($wp_cache_config_file_sample, $wp_cache_config_file);
1054 $dir = str_replace( str_replace( '\\', '/', WP_CONTENT_DIR ), '', str_replace( '\\', '/', dirname(__FILE__) ) );
1055 if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
1056 wp_cache_replace_line('define\(\ \'WPCACHEHOME', "\tdefine( 'WPCACHEHOME', WP_CONTENT_DIR . \"{$dir}/\" );", $wp_cache_config_file);
1057 } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
1058 wp_cache_replace_line('define\(\ \'WPCACHEHOME', "\tdefine( 'WPCACHEHOME', WP_CONTENT_DIR . \"{$dir}/wp-super-cache/\" );", $wp_cache_config_file);
1059 }
1060 $new = true;
1061 }
1062 if( $sem_id == 5419 && $cache_path != '' ) {
1063 $sem_id = crc32( $_SERVER[ 'HTTP_HOST' ] . $cache_path ) & 0x7fffffff;
1064 wp_cache_replace_line('sem_id', '$sem_id = ' . $sem_id . ';', $wp_cache_config_file);
1065 }
1066 require($wp_cache_config_file);
1067 return true;
1068}
1069
1070function wp_cache_create_advanced_cache() {
1071 global $wp_cache_link, $wp_cache_file;
1072 $ret = true;
1073
1074 $file = file_get_contents( $wp_cache_file );
1075 $file = str_replace( 'CACHEHOME', constant( 'WPCACHEHOME' ), $file );
1076 $fp = @fopen( $wp_cache_link, 'w' );
1077 if( $fp ) {
1078 fputs( $fp, $file );
1079 fclose( $fp );
1080 } else {
1081 $ret = false;
1082 }
1083 return $ret;
1084}
1085
1086function wp_cache_check_link() {
1087 global $wp_cache_link, $wp_cache_file;
1088
1089 $ret = true;
1090 if( file_exists($wp_cache_link) ) {
1091 $file = file_get_contents( $wp_cache_link );
1092 if( strpos( $file, "WP SUPER CACHE 0.8.9.1" ) ) {
1093 return true;
1094 } else {
1095 if( !@unlink($wp_cache_link) ) {
1096 $ret = false;
1097 } else {
1098 $ret = wp_cache_create_advanced_cache();
1099 }
1100 }
1101 } else {
1102 $ret = wp_cache_create_advanced_cache();
1103 }
1104
1105 if( false == $ret ) {
1106 echo "<h3>Warning! <em>" . constant( 'WP_CONTENT_DIR' ) . "/advanced-cache.php</em> does not exist or cannot be updated.</h3>";
1107 echo "<p><ul><li>1. If it already exists please delete the file first.</li>";
1108 echo "<li>2. Make " . constant( 'WP_CONTENT_DIR' ) . " writable using the chmod command through your ftp or server software. (<em>chmod 777 " . constant( 'WP_CONTENT_DIR' ) . "</em>) and refresh this page. This is only a temporary measure and you'll have to make it read only afterwards again. (Change 777 to 755 in the previous command)</li>";
1109 echo "<li>3. Refresh this page to update <em>" . constant( 'WP_CONTENT_DIR' ) . "/advanced-cache.php</em></li></ul>";
1110 echo "If that doesn't work, make sure the file <em>" . constant( 'WP_CONTENT_DIR' ) . "/advanced-cache.php</em> doesn't exist:<ol>";
1111 echo "<li>1. Open <em>$wp_cache_file</em> in a text editor.</li><li>2. Change the text <em>CACHEHOME</em> to <em>" . constant( 'WPCACHEHOME' ) . "</em></li><li>3. Save the file and copy it to <em>$wp_cache_link</em> and refresh this page.</li>";
1112 return false;
1113 }
1114 return true;
1115}
1116
1117function wp_cache_check_global_config() {
1118 if( defined( 'WP_CACHE' ) )
1119 return true;
1120
1121 if ( file_exists( ABSPATH . 'wp-config.php') ) {
1122 $global = ABSPATH . 'wp-config.php';
1123 } else {
1124 $global = dirname(ABSPATH) . '/wp-config.php';
1125 }
1126
1127 $howtoenable = "Edit <code>$global</code> and add the following line: <code>define('WP_CACHE', true);</code>Otherwise, <strong>WP-Cache will not be executed</strong> by Wordpress core. ";
1128 $lines = file($global);
1129 foreach($lines as $line) {
1130 if (preg_match('/^\s*define\s*\(\s*\'WP_CACHE\'\s*,\s*(?i:TRUE|1)\s*\)\s*;/', $line)) {
1131 echo $howtoenable;
1132 return false;
1133 }
1134 }
1135 $line = 'define(\'WP_CACHE\', true);';
1136 if (!is_writeable_ACLSafe($global) || !wp_cache_replace_line('define *\( *\'WP_CACHE\'', $line, $global) ) {
1137 echo "<strong>Error: WP_CACHE is not enabled</strong> in your <code>wp-config.php</code> file and I couldn't modify it.";
1138 echo $howtoenable;
1139 return false;
1140 }
1141 return true;
1142}
1143
1144function wp_cache_files() {
1145 global $cache_path, $file_prefix, $cache_max_time, $valid_nonce, $supercachedir, $cache_enabled, $super_cache_enabled, $blog_cache_dir;
1146
1147 if ( '/' != substr($cache_path, -1)) {
1148 $cache_path .= '/';
1149 }
1150
1151 if ( $valid_nonce ) {
1152 if(isset($_REQUEST['wp_delete_cache'])) {
1153 wp_cache_clean_cache($file_prefix);
1154 }
1155 if(isset($_REQUEST['wp_delete_cache_file'])) {
1156 wp_cache_clean_cache($_REQUEST['wp_delete_cache_file']);
1157 }
1158 if(isset($_REQUEST['wp_delete_expired'])) {
1159 wp_cache_clean_expired($file_prefix);
1160 }
1161 }
1162 if(isset($_REQUEST['wp_list_cache'])) {
1163 $list_files = true;
1164 $list_mess = "Update list";
1165 } else
1166 $list_mess = "List files";
1167
1168 echo '<fieldset class="options" id="show-this-fieldset"><h3>Cache Contents</h3>';
1169 /*
1170 echo '<form name="wp_cache_content_list" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
1171 echo '<input type="hidden" name="wp_list_cache" />';
1172 echo '<div class="submit"><input type="submit" ' . SUBMITDISABLED . 'value="'.$list_mess.' &raquo;" /></div>';
1173 echo "</form>\n";
1174 */
1175
1176 $list_files = false; // it doesn't list supercached files, and removing single pages is buggy
1177 $count = 0;
1178 $expired = 0;
1179 $now = time();
1180 if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) {
1181 if ($list_files) echo "<table cellspacing=\"0\" cellpadding=\"5\">";
1182 $wp_cache_fsize = 0;
1183 while ( false !== ($file = readdir($handle))) {
1184 if ( preg_match("/^$file_prefix.*\.meta/", $file) ) {
1185 $this_expired = false;
1186 $content_file = preg_replace("/meta$/", "html", $file);
1187 $mtime = filemtime( $blog_cache_dir . 'meta/' . $file );
1188 if ( ! ( $fsize = @filesize( $blog_cache_dir . $content_file ) ) )
1189 continue; // .meta does not exists
1190 $wp_cache_fsize += $fsize;
1191 $fsize = intval($fsize/1024);
1192 $age = $now - $mtime;
1193 if ( $age > $cache_max_time ) {
1194 $expired++;
1195 $this_expired = true;
1196 }
1197 $count++;
1198 /*
1199 if ($list_files) {
1200 $meta = new CacheMeta;
1201 $meta = unserialize(file_get_contents($cache_path . 'meta/' . $file));
1202 echo $flip ? '<tr style="background: #EAEAEA;">' : '<tr>';
1203 $flip = !$flip;
1204 echo '<td><a href="http://' . $meta->uri . '" target="_blank" >';
1205 echo $meta->uri . "</a></td>";
1206 if ($this_expired) echo "<td><span style='color:red'>$age secs</span></td>";
1207 else echo "<td>$age secs</td>";
1208 echo "<td>$fsize KB</td>";
1209 echo '<td><form name="wp_delete_cache_file" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
1210 echo '<input type="hidden" name="wp_list_cache" />';
1211 echo '<input type="hidden" name="wp_delete_cache_file" value="'.preg_replace("/^(.*)\.meta$/", "$1", $file).'" />';
1212 echo '<div class="submit"><input id="deletepost" ' . SUBMITDISABLED . 'type="submit" value="Remove" /></div>';
1213 wp_nonce_field('wp-cache');
1214 echo "</form></td></tr>\n";
1215 }
1216 */
1217 }
1218 }
1219 closedir($handle);
1220 if ($list_files) echo "</table>";
1221 }
1222 if( $wp_cache_fsize != 0 ) {
1223 $wp_cache_fsize = $wp_cache_fsize/1024;
1224 } else {
1225 $wp_cache_fsize = 0;
1226 }
1227 if( $wp_cache_fsize > 1024 ) {
1228 $wp_cache_fsize = number_format( $wp_cache_fsize / 1024, 2 ) . "MB";
1229 } elseif( $wp_cache_fsize != 0 ) {
1230 $wp_cache_fsize = number_format( $wp_cache_fsize, 2 ) . "KB";
1231 } else {
1232 $wp_cache_fsize = '0KB';
1233 }
1234 if( $cache_enabled == true && $super_cache_enabled == true ) {
1235 $now = time();
1236 $sizes = array( 'expired' => 0, 'cached' => 0, 'ts' => 0 );
1237
1238 if (is_dir($supercachedir)) {
1239 if( $dh = opendir( $supercachedir ) ) {
1240 while( ( $entry = readdir( $dh ) ) !== false ) {
1241 if ($entry != '.' && $entry != '..') {
1242 $sizes = wpsc_dirsize( trailingslashit( $supercachedir ) . $entry, $sizes );
1243 }
1244 }
1245 closedir($dh);
1246 }
1247 } else {
1248 if(is_file($supercachedir) && filemtime( $supercachedir ) + $cache_max_time <= $now )
1249 $sizes[ 'expired' ] ++;
1250 }
1251 $sizes[ 'ts' ] = time();
1252 }
1253
1254 echo "<p><strong>WP-Cache ({$wp_cache_fsize})</strong></p>";
1255 echo "<ul><li>$count Cached Pages</li>";
1256 echo "<li>$expired Expired Pages</li></ul>";
1257 if( $cache_enabled == true && $super_cache_enabled == true ) {
1258 $fsize = $sizes[ 'fsize' ] / 1024;
1259 if( $fsize > 1024 ) {
1260 $fsize = number_format( $fsize / 1024, 2 ) . "MB";
1261 } elseif( $fsize != 0 ) {
1262 $fsize = number_format( $fsize, 2 ) . "KB";
1263 } else {
1264 $fsize = "0KB";
1265 }
1266 echo "<p><strong>WP-Super-Cache ({$fsize})</strong></p>";
1267 echo "<ul><li>" . intval($sizes['cached']/2) . " Cached Pages</li>";
1268 $age = intval(($now - $sizes['ts'])/60);
1269 echo "<li>" . intval($sizes['expired']/2) . " Expired Pages</li></ul>";
1270 }
1271 $last_gc = get_option( "wpsupercache_gc_time" );
1272 if( $last_gc ) {
1273 $next_gc = $cache_max_time < 1800 ? $cache_max_time : 600;
1274 $next_gc_mins = ( time() - $last_gc );
1275 echo "<p><strong>Garbage Collection</strong><br />Last GC was <strong>" . date( 'i:s', $next_gc_mins ) . "</strong> minutes ago<br />";
1276 echo "Next GC in <strong>" . date( 'i:s', $next_gc - $next_gc_mins ) . "</strong> minutes</p>";
1277 }
1278
1279 echo "<p>Expired files are files older than $cache_max_time seconds. They are still used by the plugin and are deleted periodically.</p>";
1280 echo '<form name="wp_cache_content_expired" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
1281 echo '<input type="hidden" name="wp_delete_expired" />';
1282 echo '<div class="submit" style="float:left"><input type="submit" ' . SUBMITDISABLED . 'value="Delete Expired &raquo;" /></div>';
1283 wp_nonce_field('wp-cache');
1284 echo "</form>\n";
1285
1286 echo '<form name="wp_cache_content_delete" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
1287 echo '<input type="hidden" name="wp_delete_cache" />';
1288 echo '<div class="submit" style="float:left;margin-left:10px"><input id="deletepost" type="submit" ' . SUBMITDISABLED . 'value="Delete Cache &raquo;" /></div>';
1289 wp_nonce_field('wp-cache');
1290 echo "</form>\n";
1291
1292 echo '</fieldset>';
1293}
1294
1295function delete_cache_dashboard() {
1296 if( function_exists( 'is_site_admin' ) && !is_site_admin() )
1297 return false;
1298
1299 if( function_exists('current_user_can') && !current_user_can('manage_options') )
1300 return false;
1301
1302 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)'>Delete Cache</a></li>";
1303}
1304add_action( 'dashmenu', 'delete_cache_dashboard' );
1305
1306function wpsc_dirsize($directory, $sizes) {
1307 global $cache_max_time;
1308 $now = time();
1309
1310 if (is_dir($directory)) {
1311 if( $dh = opendir( $directory ) ) {
1312 while( ( $entry = readdir( $dh ) ) !== false ) {
1313 if ($entry != '.' && $entry != '..') {
1314 $sizes = wpsc_dirsize( trailingslashit( $directory ) . $entry, $sizes );
1315 }
1316 }
1317 closedir($dh);
1318 }
1319 } else {
1320 if(is_file($directory) ) {
1321 if( filemtime( $directory ) + $cache_max_time <= $now ) {
1322 $sizes[ 'expired' ]+=1;
1323 } else {
1324 $sizes[ 'cached' ]+=1;
1325 }
1326 if ( ! isset( $sizes[ 'fsize' ] ) )
1327 $sizes[ 'fsize' ] = @filesize( $directory );
1328 else
1329 $sizes[ 'fsize' ] += @filesize( $directory );
1330 }
1331 }
1332 return $sizes;
1333}
1334
1335
1336function wp_cache_clean_cache($file_prefix) {
1337 global $cache_path, $supercachedir, $blog_cache_dir;
1338
1339 // If phase2 was compiled, use its function to avoid race-conditions
1340 if(function_exists('wp_cache_phase2_clean_cache')) {
1341 if (function_exists ('prune_super_cache')) {
1342 if( is_dir( $supercachedir ) ) {
1343 prune_super_cache( $supercachedir, true );
1344 } elseif( is_dir( $supercachedir . '.disabled' ) ) {
1345 prune_super_cache( $supercachedir . '.disabled', true );
1346 }
1347 prune_super_cache( $cache_path, true );
1348 $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
1349 }
1350 return wp_cache_phase2_clean_cache($file_prefix);
1351 }
1352
1353 $expr = "/^$file_prefix/";
1354 if ( ($handle = @opendir( $blog_cache_dir )) ) {
1355 while ( false !== ($file = readdir($handle))) {
1356 if ( preg_match($expr, $file) ) {
1357 @unlink( $blog_cache_dir . $file);
1358 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
1359 }
1360 }
1361 closedir($handle);
1362 }
1363}
1364
1365function wp_cache_clean_expired($file_prefix) {
1366 global $cache_path, $cache_max_time, $blog_cache_dir;
1367
1368 // If phase2 was compiled, use its function to avoid race-conditions
1369 if(function_exists('wp_cache_phase2_clean_expired')) {
1370 if (function_exists ('prune_super_cache')) {
1371 $dir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
1372 if( is_dir( $dir ) ) {
1373 prune_super_cache( $dir );
1374 } elseif( is_dir( $dir . '.disabled' ) ) {
1375 prune_super_cache( $dir . '.disabled' );
1376 }
1377 $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
1378 }
1379 return wp_cache_phase2_clean_expired($file_prefix);
1380 }
1381
1382 $expr = "/^$file_prefix/";
1383 $now = time();
1384 if ( ($handle = @opendir( $blog_cache_dir )) ) {
1385 while ( false !== ($file = readdir($handle))) {
1386 if ( preg_match( $expr, $file ) &&
1387 ( filemtime( $blog_cache_dir . $file ) + $cache_max_time ) <= $now ) {
1388 @unlink( $blog_cache_dir . $file );
1389 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
1390 }
1391 }
1392 closedir($handle);
1393 }
1394}
1395
1396function wpsc_remove_marker( $filename, $marker ) {
1397 if (!file_exists( $filename ) || is_writeable_ACLSafe( $filename ) ) {
1398 if (!file_exists( $filename ) ) {
1399 return '';
1400 } else {
1401 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
1402 }
1403
1404 $f = fopen( $filename, 'w' );
1405 $foundit = false;
1406 if ( $markerdata ) {
1407 $state = true;
1408 foreach ( $markerdata as $n => $markerline ) {
1409 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
1410 $state = false;
1411 if ( $state ) {
1412 if ( $n + 1 < count( $markerdata ) )
1413 fwrite( $f, "{$markerline}\n" );
1414 else
1415 fwrite( $f, "{$markerline}" );
1416 }
1417 if (strpos($markerline, '# END ' . $marker) !== false) {
1418 $state = true;
1419 }
1420 }
1421 }
1422 return true;
1423 } else {
1424 return false;
1425 }
1426}
1427
1428function wp_super_cache_footer() {
1429 ?><p id='supercache'><?php bloginfo('name'); ?> is Digg proof thanks to caching by <a href="http://ocaoimh.ie/wp-super-cache/">WP Super Cache</a>!</p><?php
1430}
1431if( isset( $wp_cache_hello_world ) && $wp_cache_hello_world )
1432 add_action( 'wp_footer', 'wp_super_cache_footer' );
1433
1434if( get_option( 'gzipcompression' ) )
1435 update_option( 'gzipcompression', 0 );
1436
1437// Catch 404 requests. Themes that use query_posts() destroy $wp_query->is_404
1438function wp_cache_catch_404() {
1439 global $wp_cache_404;
1440 $wp_cache_404 = false;
1441 if( is_404() )
1442 $wp_cache_404 = true;
1443}
1444add_action( 'template_redirect', 'wp_cache_catch_404' );
1445
1446function wp_cache_favorite_action( $actions ) {
1447 if( function_exists( 'is_site_admin' ) && !is_site_admin() )
1448 return $actions;
1449
1450 if( function_exists('current_user_can') && !current_user_can('manage_options') )
1451 return $actions;
1452
1453 $actions[ wp_nonce_url( 'options-general.php?page=wpsupercache&wp_delete_cache=1', 'wp-cache' ) ] = array( __( 'Delete Cache' ), 'manage_options' );
1454
1455 return $actions;
1456}
1457add_filter( 'favorite_actions', 'wp_cache_favorite_action' );
1458
1459function wp_cache_plugin_notice( $plugin ) {
1460 global $cache_enabled;
1461 if( $plugin == 'wp-super-cache/wp-cache.php' && !$cache_enabled && function_exists( "admin_url" ) )
1462 echo '<td colspan="5" class="plugin-update">WP Super Cache must be configured. Go to <a href="' . admin_url( 'options-general.php?page=wpsupercache' ) . '">the admin page</a> to enable and configure the plugin.</td>';
1463}
1464add_action( 'after_plugin_row', 'wp_cache_plugin_notice' );
1465
1466function wp_cache_plugin_actions( $links, $file ) {
1467 if( $file == 'wp-super-cache/wp-cache.php' && function_exists( "admin_url" ) ) {
1468 $settings_link = '<a href="' . admin_url( 'options-general.php?page=wpsupercache' ) . '">' . __('Settings') . '</a>';
1469 array_unshift( $links, $settings_link ); // before other links
1470 }
1471 return $links;
1472}
1473add_filter( 'plugin_action_links', 'wp_cache_plugin_actions', 10, 2 );
1474
1475function wpsc_author_url( $url ) {
1476 return clean_url( $url );
1477}
1478add_filter( 'get_comment_author_url' , 'wpsc_author_url' );
1479
1480function wp_cache_admin_notice() {
1481 global $cache_enabled;
1482 if( substr( $_SERVER["PHP_SELF"], -11 ) == 'plugins.php' && !$cache_enabled && function_exists( "admin_url" ) )
1483 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.' ), admin_url( 'options-general.php?page=wpsupercache' ) ) . '</strong></p></div>';
1484}
1485add_action( 'admin_notices', 'wp_cache_admin_notice' );
1486?>
Note: See TracBrowser for help on using the repository browser.