WP Super Cache - Version 0.1

Version Description

Download this release

Release Info

Developer donncha
Plugin Icon 128x128 WP Super Cache
Version 0.1
Comparing to
See all releases

Version 0.1

plugins/searchengine.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ function wp_supercache_searchengine( $string ) {
3
+ global $passingthrough, $nevershowads, $cache_no_adverts_for_friends;
4
+
5
+ if( $cache_no_adverts_for_friends != 'yes' )
6
+ return $string;
7
+
8
+ if( $_COOKIE[ '7a1254cba80da02d5478d91cfd0a873a' ] == 1 ) {
9
+ $string = 'searchengine';
10
+ } elseif( $_SERVER[ 'HTTP_REFERER' ] != '' ) {
11
+ if( is_array( $passingthrough ) == false )
12
+ return $string;
13
+
14
+ foreach( $passingthrough as $url ) {
15
+ if( strpos( $_SERVER[ 'HTTP_REFERER' ], $url ) ) {
16
+ reset( $nevershowads );
17
+ $se = false;
18
+ foreach( $nevershowads as $whitesite ) {
19
+ if( false == strpos( $_SERVER[ 'HTTP_REFERER' ], $whitesite ) ) {
20
+ $se = true;
21
+ }
22
+ }
23
+ if( $se ) {
24
+ $string = 'searchengine';
25
+ setcookie( '7a1254cba80da02d5478d91cfd0a873a', 1, time()+3600, '/' );
26
+ }
27
+ }
28
+ }
29
+ }
30
+
31
+ return $string;
32
+ }
33
+ add_cacheaction( 'wp_cache_get_cookies_values', 'wp_supercache_searchengine' );
34
+
35
+ function searchenginesupercache( $user_info ) {
36
+ if( $user_info == 'searchengine' && is_single() && is_old_post() ) {
37
+ return true;
38
+ } else {
39
+ return false;
40
+ }
41
+ return $string;
42
+ }
43
+
44
+ function searchengine_phase2_actions() {
45
+ global $cache_no_adverts_for_friends;
46
+ if( $cache_no_adverts_for_friends == 'yes' ) {
47
+ add_filter( 'do_createsupercache', 'searchenginesupercache' );
48
+ }
49
+ }
50
+ add_cacheaction( 'add_cacheaction', 'searchengine_phase2_actions' );
51
+
52
+ function wp_supercache_searchengine_admin() {
53
+ global $cache_no_adverts_for_friends, $wp_cache_config_file, $valid_nonce;
54
+
55
+ $cache_no_adverts_for_friends = $cache_no_adverts_for_friends == '' ? 'no' : $cache_no_adverts_for_friends;
56
+
57
+ if(isset($_POST['cache_no_adverts_for_friends']) && $valid_nonce) {
58
+ $cache_no_adverts_for_friends = $_POST['cache_no_adverts_for_friends'] == 'Disable' ? 'no' : 'yes';
59
+ wp_cache_replace_line('^ *\$cache_no_adverts_for_friends', "\$cache_no_adverts_for_friends = '$cache_no_adverts_for_friends';", $wp_cache_config_file);
60
+ }
61
+ echo '<form name="wp_supercache_searchengine_admin" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
62
+ wp_nonce_field('wp-cache');
63
+ echo '<strong><a href="http://ocaoimh.ie/no-adverts-for-friends/">No Adverts for Friends</a> plugin is ';
64
+ if( $cache_no_adverts_for_friends == 'no' ) {
65
+ echo 'disabled';
66
+ } else {
67
+ echo 'enabled';
68
+ }
69
+ echo '.</strong> (requires <a href="http://ocaoimh.ie/no-adverts-for-friends/">friendsadverts.php</a> too)<div class="submit">';
70
+ if( $cache_no_adverts_for_friends == 'no' ) {
71
+ echo '<input type="submit" name="cache_no_adverts_for_friends" value="Enable" />';
72
+ } else {
73
+ echo '<input type="submit" name="cache_no_adverts_for_friends" value="Disable" />';
74
+ }
75
+ echo "</div></form>\n";
76
+
77
+ }
78
+ add_cacheaction( 'cache_admin_page', 'wp_supercache_searchengine_admin' );
79
+
80
+ ?>
readme.txt ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === WP Super Cache ===
2
+ Contributors: donncha
3
+ Tags: performance,caching,wp-cache
4
+ Tested up to: 2.3.1
5
+
6
+ A modification of WP-Cache that produces static html files.
7
+
8
+ == Description ==
9
+ A modification of WP-Cache that produces static html files. After a html file
10
+ is generated your webserver will serve that file instead of processing the
11
+ comparatively heavier and more expensive WordPress PHP scripts.
12
+ It will only cache and serve files to users who are not logged in, who
13
+ have not left a comment on your blog, or who have viewed a password protected
14
+ post. That still probably leave 90% of your visitors who will benefit. The users
15
+ mentioned above will also benefit because your server won't be as busy as before.
16
+ This script should help your server cope with a front page appearance on digg.com
17
+ or other social networking site.
18
+
19
+ This plugin is a modified version of the WP-Cache 2 plugin by Ricardo Galli Granada.
20
+ His plugin is still available. We're standing on the shoulders of giants and benefiting
21
+ from the power of the GPL here. Thanks Ricardo for creating such a great plugin!
22
+ See the following URLs for more info on WP-Cache 2
23
+
24
+ 1. http://mnm.uib.es/gallir/wp-cache-2/
25
+ 2. http://wordpress.org/extend/plugins/wp-cache/
26
+
27
+ A classic method of preparing an underpowered site for a Digg frontpage appearance
28
+ or a Slashdotting has been to manually save copies of dynamically generated pages,
29
+ and place them in directories that match the permalinks structure.
30
+
31
+ This method of performance enhancement does help servers handle a higher load
32
+ without crashing, but is only effective when an oncoming rush of traffic can be
33
+ anticipated.
34
+
35
+ WP-Cache alone, while helpful, is not adequate in many cases, so this modification
36
+ was created to effectively mimic the manual page caching method, but to handle it
37
+ in an automated fashion.
38
+
39
+ Original WP-Cache by Ricardo Galli Granada, http://mnm.uib.es/gallir/
40
+ WP Super Cache by Donncha O Caoimh, http://ocaoimh.ie/
41
+
42
+ == Installation ==
43
+ 1. You must have fancy permalinks enabled for this to work.
44
+ 2. If you have WP-Cache installed already, please disable it. Edit wp-config.php
45
+ and make sure the WP_CACHE define is deleted, and remove the file
46
+ wp-content/advanced-cache.php.
47
+ 3. Upload this directory to your plugins directory. It will create a
48
+ 'wp-content/plugins/wp-super-cache/' directory.
49
+ 4. If you are using WordPress MU you will need to install this in
50
+ 'wp-content/mu-plugins/wp-super-cache/' and the file wp-cache.php
51
+ must be copied into the mu-plugins directory.
52
+ 5. WordPress users should go to their Plugins page and activate "WP Super Cache".
53
+ 6. Now go to Options->WP Super Cache and enable caching. If you see an error message
54
+ or a blank screen you may need to fix it. See the "FAQ" section later in this
55
+ readme for instructions.
56
+ 7. Edit the .htaccess file in your root directory and add the following code:
57
+
58
+ RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
59
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
60
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
61
+ RewriteCond %{HTTP:Accept-Encoding} gzip
62
+ RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz -f
63
+ RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz [L]
64
+
65
+ RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
66
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
67
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
68
+ RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html -f
69
+ RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html [L]
70
+
71
+ That code must be inserted above the standard WordPress rewrite rules.
72
+ If your blog isn't located at the root of your server, you must add that directory
73
+ to the rules. For example, if your blog is in the directory "/blog/":
74
+
75
+ RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
76
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
77
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
78
+ RewriteCond %{HTTP:Accept-Encoding} gzip
79
+ RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz -f
80
+ RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html.gz [L]
81
+
82
+ RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
83
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
84
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
85
+ RewriteCond %{DOCUMENT_ROOT}/blog/wp-content/cache/supercache/%{HTTP_HOST}/blog/$1index.html -f
86
+ RewriteRule ^(.*) /blog/wp-content/cache/supercache/%{HTTP_HOST}/$1/blog/index.html [L]
87
+
88
+ Your .htaccess should look similar to this:
89
+
90
+ -----------------.htaccess-----------------
91
+ RewriteEngine On
92
+ RewriteBase /
93
+
94
+ RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
95
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
96
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
97
+ RewriteCond %{HTTP:Accept-Encoding} gzip
98
+ RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz -f
99
+ RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz [L]
100
+
101
+ RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$
102
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$
103
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$
104
+ RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html -f
105
+ RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html [L]
106
+
107
+ RewriteCond %{REQUEST_FILENAME} !-f
108
+ RewriteCond %{REQUEST_FILENAME} !-d
109
+ RewriteRule . /index.php [L]
110
+ -----------------.htaccess-----------------
111
+
112
+ == Frequently Asked Questions ==
113
+ If things don't work when you installed the plugin here are a few things to check:
114
+ 1. Is wp-content writable by the web server?
115
+ 2. Is there a wp-content/wp-cache-config.php ? If not, copy the file
116
+ wp-super-cache/wp-cache-config-sample.php to wp-content/wp-cache-config.php
117
+ and make sure WPCACHEHOME points at the right place. "plugins" should be
118
+ "mu-plugins" if you're using WordPress MU.
119
+ 3. Is there a wp-content/advanced-cache.php ? If not, then you must symlink
120
+ wp-super-cache/wp-cache-phase1.php to it with the command while in the
121
+ wp-content folder:
122
+ ln -s plugins/wp-super-cache/wp-cache-phase1.php advanced-cache.php
123
+ If you can't do that, then copy the file. That will work too.
124
+ 4. Make sure the following line is in wp-config.php
125
+ define( 'WP_CACHE', true );
126
+ 5. Try the Options->WP Super Cache page again and enable cache.
127
+ 6. Look in wp-content/cache/supercache/. Are there directories and files there?
128
+ 7. Anything in your php error_log?
129
+ 8. If your browser keeps asking you to save the file after the super cache is installed
130
+ you must disable Super Cache compression. Go to the Options->WP Super Cache page and disable
131
+ it there.
132
+
133
+ == Custom Caching ==
134
+ It is now possible to hook into the caching process using the add_cacheacton() function.
135
+ Three hooks are available:
136
+
137
+ 1. 'wp_cache_get_cookies_values' - modify the key used by WP Cache.
138
+ 2. 'add_cacheaction' - runs in phase2. Allows a plugin to add WordPress hooks.
139
+ 3. 'cache_admin_page' - runs in the admin page. Use it to add modify that page.
140
+
141
+ There is one regular WordPress filter too. Use the "do_createsupercache" filter
142
+ to customize the checks made before caching. The filter accepts one parameter.
143
+ The output of WP-Cache's wp_cache_get_cookies_values() function.
144
+
145
+ See plugins/searchengine.php as an example I use for my [No Adverts for Friends](plugin at http://ocaoimh.ie/no-adverts-for-friends/)
146
+
147
+ == Updates ==
148
+ Updates to the plugin will be posted here, to http://ocaoimh.ie/ and the page
149
+ http://ocaoimh.ie/wp-super-cache/ will always link to the newest version.
150
+
151
+ == Thanks ==
152
+ I would sincerely like to thank [John Pozadzides](http://onemansblog.com/) for
153
+ giving me the idea for this, for writing the "How it works" section and for
154
+ testing the plugin through 2 front page appearances on digg.com
155
+ Thanks to James Farmer and Andrew Billits of [Edu Blogs](http://edublogs.org/) fame who helped me
156
+ make this more WordPress MU friendly.
wp-cache-base.php ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $known_headers = array("Last-Modified", "Expires", "Content-Type", "Content-type", "X-Pingback", "ETag", "Cache-Control", "Pragma");
3
+
4
+ if (!class_exists('CacheMeta')) {
5
+ class CacheMeta {
6
+ var $dynamic = false;
7
+ var $headers = array();
8
+ var $uri = '';
9
+ var $post = 0;
10
+ }
11
+ }
12
+
13
+ ?>
wp-cache-config-sample.php ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ WP-Cache Config Sample File
4
+
5
+ See wp-cache.php for author details.
6
+ */
7
+
8
+ define( 'WPCACHEHOME', ABSPATH . 'wp-content/plugins/wp-super-cache/' );
9
+
10
+ $cache_compression = 0; // Super cache compression
11
+ $cache_enabled = false;
12
+ $super_cache_enabled = false;
13
+ $cache_max_time = 3600; //in seconds
14
+ //$use_flock = true; // Set it true or false if you know what to use
15
+ $super_cache_max_time = 21600; // in seconds
16
+ $cache_path = ABSPATH . 'wp-content/cache/';
17
+ $file_prefix = 'wp-cache-';
18
+
19
+ // We want to be able to identify each blog in a WordPress MU install
20
+ $blogcacheid = '';
21
+ if( defined( 'VHOST' ) ) {
22
+ $blogcacheid = 'blog'; // main blog
23
+ if( constant( 'VHOST' ) == 'yes' ) {
24
+ $blogcacheid = $_SERVER['HTTP_HOST'];
25
+ } else {
26
+ $request_uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '..', '', $_SERVER['REQUEST_URI'] ) );
27
+ if( strpos( $request_uri, '/', 1 ) ) {
28
+ if( $base == '/' ) {
29
+ $blogcacheid = substr( $request_uri, 1, strpos( $request_uri, '/', 1 ) - 1 );
30
+ } else {
31
+ $blogcacheid = str_replace( $base, '', $request_uri );
32
+ $blogcacheid = substr( $blogcacheid, 0, strpos( $blogcacheid, '/', 1 ) );
33
+ }
34
+ if ( '/' == substr($blogcacheid, -1))
35
+ $blogcacheid = substr($blogcacheid, 0, -1);
36
+ }
37
+ }
38
+ }
39
+
40
+ // Array of files that have 'wp-' but should still be cached
41
+ $cache_acceptable_files = array( 'wp-atom.php', 'wp-comments-popup.php', 'wp-commentsrss2.php', 'wp-links-opml.php', 'wp-locations.php', 'wp-rdf.php', 'wp-rss.php', 'wp-rss2.php');
42
+
43
+ $cache_rejected_uri = array('wp-');
44
+ $cache_rejected_user_agent = array ( 0 => 'bot', 1 => 'ia_archive', 2 => 'slurp', 3 => 'crawl', 4 => 'spider');
45
+
46
+ // Just modify it if you have conflicts with semaphores
47
+ $sem_id = 5419;
48
+
49
+ if ( '/' != substr($cache_path, -1)) {
50
+ $cache_path .= '/';
51
+ }
52
+
53
+ ?>
wp-cache-phase1.php ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if( !@include(ABSPATH . 'wp-content/wp-cache-config.php') ) {
3
+ return;
4
+ }
5
+ if( !defined( 'WPCACHEHOME' ) )
6
+ define('WPCACHEHOME', dirname(__FILE__).'/');
7
+
8
+ include( WPCACHEHOME . 'wp-cache-base.php');
9
+
10
+ $mutex_filename = 'wp_cache_mutex.lock';
11
+ $new_cache = false;
12
+
13
+
14
+ // Don't change variables behind this point
15
+
16
+ $plugins = glob( WPCACHEHOME . 'plugins/*.php' );
17
+ if( is_array( $plugins ) ) {
18
+ foreach ( $plugins as $plugin ) {
19
+ if( is_file( $plugin ) )
20
+ require_once( $plugin );
21
+ }
22
+ }
23
+
24
+ if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST')
25
+ return;
26
+
27
+ $file_expired = false;
28
+
29
+
30
+ $cache_filename = '';
31
+ $meta_file = '';
32
+
33
+ $key = $blogcacheid . md5($_SERVER['HTTP_HOST'].preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']).wp_cache_get_cookies_values());
34
+ $cache_filename = $file_prefix . $key . '.html';
35
+ $meta_file = $file_prefix . $key . '.meta';
36
+ $cache_file = $cache_path . $cache_filename;
37
+ $meta_pathname = $cache_path . 'meta/' . $meta_file;
38
+
39
+ $wp_start_time = microtime();
40
+ if( ($mtime = @filemtime($meta_pathname)) ) {
41
+ if ($mtime + $cache_max_time > time() ) {
42
+ $meta = new CacheMeta;
43
+ if (! ($meta = unserialize(@file_get_contents($meta_pathname))) )
44
+ return;
45
+ foreach ($meta->headers as $header) {
46
+ header($header);
47
+ }
48
+ $log = "<!-- Cached page served by WP-Cache -->\n";
49
+ if ( !($content_size = @filesize($cache_file)) > 0 || $mtime < @filemtime($cache_file))
50
+ return;
51
+ if ($meta->dynamic) {
52
+ include($cache_file);
53
+ } else {
54
+ /* No used to avoid problems with some PHP installations
55
+ $content_size += strlen($log);
56
+ header("Content-Length: $content_size");
57
+ */
58
+ if(!@readfile ($cache_file))
59
+ return;
60
+ }
61
+ echo $log;
62
+ die;
63
+ }
64
+ $file_expired = true; // To signal this file was expired
65
+ }
66
+
67
+ function wp_cache_postload() {
68
+ global $cache_enabled;
69
+
70
+ if (!$cache_enabled)
71
+ return;
72
+ require( WPCACHEHOME . 'wp-cache-phase2.php');
73
+ wp_cache_phase2();
74
+ }
75
+
76
+ function wp_cache_get_cookies_values() {
77
+ global $wp_supercache_actions, $passingthrough, $nevershowads;
78
+ $string = '';
79
+ while ($key = key($_COOKIE)) {
80
+ if (preg_match("/^wp-postpass|^wordpress|^comment_author_email_/", $key)) {
81
+ $string .= $_COOKIE[$key] . ",";
82
+ }
83
+ next($_COOKIE);
84
+ }
85
+ reset($_COOKIE);
86
+ if( $string != '' )
87
+ return $string;
88
+
89
+ $string = do_cacheaction( 'wp_cache_get_cookies_values', $string );
90
+ return $string;
91
+ }
92
+
93
+ function add_cacheaction( $action, $func ) {
94
+ global $wp_supercache_actions;
95
+ $wp_supercache_actions[ $action ][] = $func;
96
+ }
97
+
98
+ function do_cacheaction( $action, $value = '' ) {
99
+ global $wp_supercache_actions;
100
+ if( is_array( $wp_supercache_actions[ $action ] ) ) {
101
+ $actions = $wp_supercache_actions[ $action ];
102
+ foreach( $actions as $func ) {
103
+ $value = $func( $value );
104
+ }
105
+ }
106
+
107
+ return $value;
108
+ }
109
+
110
+ ?>
wp-cache-phase2.php ADDED
@@ -0,0 +1,410 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /** Diable here because PHP4.3 does not make the global
4
+ Serious bug?
5
+
6
+ $mutex_filename = 'wp_cache_mutex.lock';
7
+ $new_cache = false;
8
+ */
9
+
10
+ function wp_cache_phase2() {
11
+ global $cache_filename, $cache_acceptable_files, $wp_cache_meta_object;
12
+
13
+ wp_cache_mutex_init();
14
+ if(function_exists('add_action')) {
15
+ // Post ID is received
16
+ add_action('publish_post', 'wp_cache_post_change', 0);
17
+ add_action('edit_post', 'wp_cache_post_change', 0);
18
+ add_action('delete_post', 'wp_cache_post_change', 0);
19
+ add_action('publish_phone', 'wp_cache_post_change', 0);
20
+ // Coment ID is received
21
+ add_action('trackback_post', 'wp_cache_get_postid_from_comment', 0);
22
+ add_action('pingback_post', 'wp_cache_get_postid_from_comment', 0);
23
+ add_action('comment_post', 'wp_cache_get_postid_from_comment', 0);
24
+ add_action('edit_comment', 'wp_cache_get_postid_from_comment', 0);
25
+ add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 0);
26
+ // No post_id is available
27
+ add_action('delete_comment', 'wp_cache_no_postid', 0);
28
+ add_action('switch_theme', 'wp_cache_no_postid', 0);
29
+
30
+ do_cacheaction( 'add_cacheaction' );
31
+ }
32
+ if( $_SERVER["REQUEST_METHOD"] == 'POST' || get_option('gzipcompression'))
33
+ return;
34
+ $script = basename($_SERVER['PHP_SELF']);
35
+ if (!in_array($script, $cache_acceptable_files) &&
36
+ wp_cache_is_rejected($_SERVER["REQUEST_URI"]))
37
+ return;
38
+ if (wp_cache_user_agent_is_rejected()) return;
39
+ $wp_cache_meta_object = new CacheMeta;
40
+ ob_start('wp_cache_ob_callback');
41
+ register_shutdown_function('wp_cache_shutdown_callback');
42
+ }
43
+
44
+ function wp_cache_get_response_headers() {
45
+ if(function_exists('apache_response_headers')) {
46
+ $headers = apache_response_headers();
47
+ } else if(function_exists('headers_list')) {
48
+ $headers = array();
49
+ foreach(headers_list() as $hdr) {
50
+ list($header_name, $header_value) = explode(': ', $hdr, 2);
51
+ $headers[$header_name] = $header_value;
52
+ }
53
+ } else
54
+ $headers = null;
55
+
56
+ return $headers;
57
+ }
58
+
59
+ function wp_cache_is_rejected($uri) {
60
+ global $cache_rejected_uri;
61
+
62
+ if (strstr($uri, '/wp-admin/'))
63
+ return true; //we don't allow cacheing wp-admin for security
64
+ foreach ($cache_rejected_uri as $expr) {
65
+ if (strlen($expr) > 0 && substr( $uri, 1, strlen($expr) ) == $expr && strpos( $uri, '.php' ) )
66
+ return true;
67
+ }
68
+ return false;
69
+ }
70
+
71
+ function wp_cache_user_agent_is_rejected() {
72
+ global $cache_rejected_user_agent;
73
+
74
+ if (!function_exists('apache_request_headers')) return false;
75
+ $headers = apache_request_headers();
76
+ if (!isset($headers["User-Agent"])) return false;
77
+ foreach ($cache_rejected_user_agent as $expr) {
78
+ if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
79
+ return true;
80
+ }
81
+ return false;
82
+ }
83
+
84
+
85
+ function wp_cache_mutex_init() {
86
+ global $use_flock, $mutex, $cache_path, $mutex_filename, $sem_id;
87
+
88
+ if(!is_bool($use_flock)) {
89
+ if(function_exists('sem_get'))
90
+ $use_flock = false;
91
+ else
92
+ $use_flock = true;
93
+ }
94
+
95
+ if ($use_flock)
96
+ $mutex = fopen($cache_path . $mutex_filename, 'w');
97
+ else
98
+ $mutex = sem_get($sem_id, 1, 0644 | IPC_CREAT, 1);
99
+ }
100
+
101
+ function wp_cache_writers_entry() {
102
+ global $use_flock, $mutex, $cache_path, $mutex_filename;
103
+
104
+ if ($use_flock)
105
+ flock($mutex, LOCK_EX);
106
+ else
107
+ sem_acquire($mutex);
108
+ }
109
+
110
+ function wp_cache_writers_exit() {
111
+ global $use_flock, $mutex, $cache_path, $mutex_filename;
112
+
113
+ if ($use_flock)
114
+ flock($mutex, LOCK_UN);
115
+ else
116
+ sem_release($mutex);
117
+ }
118
+
119
+ function wp_cache_ob_callback($buffer) {
120
+ global $cache_path, $cache_filename, $meta_file, $wp_start_time;
121
+ global $new_cache, $wp_cache_meta_object, $file_expired, $blog_id, $cache_compression;
122
+
123
+
124
+ /* Mode paranoic, check for closing tags
125
+ * we avoid caching incomplete files */
126
+ if (is_404() || !preg_match('/(<\/html>|<\/rss>|<\/feed>)/i',$buffer) ) {
127
+ $new_cache = false;
128
+ return $buffer;
129
+ }
130
+
131
+ $duration = wp_cache_microtime_diff($wp_start_time, microtime());
132
+ $duration = sprintf("%0.3f", $duration);
133
+ $buffer .= "\n<!-- Dynamic Page Served (once) in $duration seconds -->\n";
134
+
135
+ wp_cache_writers_entry();
136
+ $mtime = @filemtime($cache_path . $cache_filename);
137
+ /* Return if:
138
+ the file didn't exist before but it does exist now (another connection created)
139
+ OR
140
+ the file was expired and its mtime is less than 5 seconds
141
+ */
142
+ if( !((!$file_expired && $mtime) || ($mtime && $file_expired && (time() - $mtime) < 5)) ) {
143
+ $dir = strtolower(preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"])).preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '..', '', $_SERVER['REQUEST_URI']) ); // To avoid XSS attacs
144
+ $dir = trailingslashit( $cache_path . 'supercache/' . $dir );
145
+ if( is_dir( substr( $dir, 0, -1 ) . '.disabled' ) ) {
146
+ return $buffer;
147
+ }
148
+ if( is_dir( $dir ) == false && is_dir( substr( $dir, 0, -1 ) . '.disabled' ) == false ) {
149
+ @mkpath( $dir );
150
+ }
151
+
152
+ $fr = @fopen($cache_path . $cache_filename, 'w');
153
+ if (!$fr)
154
+ $buffer = "Couldn't write to: " . $cache_path . $cache_filename . "\n";
155
+ $user_info = wp_cache_get_cookies_values();
156
+ $do_cache = apply_filters( 'do_createsupercache', $user_info );
157
+ if( $user_info == '' || $do_cache === true ) {
158
+ $fr2 = @fopen("{$dir}index.html", 'w');
159
+ if( $cache_compression )
160
+ $gz = @gzopen("{$dir}index.html.gz", 'w3');
161
+ }
162
+
163
+ if (preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content
164
+ $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
165
+ "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
166
+ $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
167
+ "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
168
+ $wp_cache_meta_object->dynamic = true;
169
+ /* Clean function calls in tag */
170
+ $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
171
+ $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
172
+ fputs($fr, $store);
173
+ if( $fr2 )
174
+ fputs($fr2, $store . '<!-- super cache -->' );
175
+ if( $gz )
176
+ gzwrite($gz, $store . '<!-- super cache gz -->' );
177
+ } else {
178
+ fputs($fr, $buffer);
179
+ if( $fr2 )
180
+ fputs($fr2, $buffer . '<!-- super cache -->' );
181
+ if( $gz )
182
+ gzwrite($gz, $buffer . '<!-- super cache gz -->' );
183
+ }
184
+ $new_cache = true;
185
+ fclose($fr);
186
+ if( $fr2 )
187
+ fclose($fr2);
188
+ if( $gz )
189
+ fclose($gz);
190
+ }
191
+ wp_cache_writers_exit();
192
+ return $buffer;
193
+ }
194
+
195
+ function wp_cache_phase2_clean_cache($file_prefix) {
196
+ global $cache_path;
197
+
198
+ wp_cache_writers_entry();
199
+ if ( ($handle = opendir( $cache_path )) ) {
200
+ while ( false !== ($file = readdir($handle))) {
201
+ if ( preg_match("/^$file_prefix/", $file) ) {
202
+ unlink($cache_path . $file);
203
+ }
204
+ }
205
+ closedir($handle);
206
+ }
207
+ wp_cache_writers_exit();
208
+ }
209
+
210
+ function prune_super_cache($directory, $force = false) {
211
+ global $super_cache_max_time, $cache_path;
212
+
213
+ if( !isset( $super_cache_max_time ) )
214
+ $super_cache_max_time = 21600;
215
+
216
+ $now = time();
217
+
218
+ if (is_dir($directory)) {
219
+ $entries = glob($directory. '/*');
220
+ foreach ($entries as $entry) {
221
+ if ($entry != '.' && $entry != '..') {
222
+ prune_super_cache($entry, $force);
223
+ if( $force || is_dir( $entry ) && @filemtime( $entry ) + $super_cache_max_time <= $now ) {
224
+ if( $entry != $cache_path . '/meta' )
225
+ @rmdir( $entry );
226
+ }
227
+ }
228
+ }
229
+ } else {
230
+ if( $force || is_file($directory) && filemtime( $directory ) + $super_cache_max_time <= $now )
231
+ unlink($directory);
232
+ }
233
+ }
234
+
235
+ function wp_cache_phase2_clean_expired($file_prefix) {
236
+ global $cache_path, $cache_max_time;
237
+
238
+ clearstatcache();
239
+ wp_cache_writers_entry();
240
+ $now = time();
241
+ if ( ($handle = opendir( $cache_path )) ) {
242
+ while ( false !== ($file = readdir($handle))) {
243
+ if ( preg_match("/^$file_prefix/", $file) &&
244
+ (filemtime($cache_path . $file) + $cache_max_time) <= $now ) {
245
+ @unlink($cache_path . $file);
246
+ @unlink($cache_path . 'meta/' . str_replace( '.html', '.meta', $file ) );
247
+ continue;
248
+ }
249
+ if($file != '.' && $file != '..') {
250
+ if ( is_dir( $cache_path . $file ) == false && (filemtime($cache_path . $file) + $cache_max_time) <= $now ) {
251
+ @unlink($cache_path . $file);
252
+ } elseif( is_dir( $cache_path . $file ) ) {
253
+ prune_super_cache( $cache_path . $file );
254
+ }
255
+ }
256
+ }
257
+ closedir($handle);
258
+ }
259
+
260
+ wp_cache_writers_exit();
261
+ }
262
+
263
+ function wp_cache_shutdown_callback() {
264
+ global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache;
265
+ global $wp_cache_meta_object, $known_headers, $blog_id;
266
+
267
+ $wp_cache_meta_object->uri = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER['REQUEST_URI']); // To avoid XSS attacs
268
+ $wp_cache_meta_object->blog_id=$blog_id;
269
+ $wp_cache_meta_object->post = wp_cache_post_id();
270
+
271
+ $response = wp_cache_get_response_headers();
272
+ $wp_cache_meta_object->headers = array();
273
+ foreach ($known_headers as $key) {
274
+ if(isset($response{$key})) {
275
+ array_push($wp_cache_meta_object->headers, "$key: " . $response{$key});
276
+ }
277
+ }
278
+ /* Not used because it gives problems with some
279
+ * PHP installations
280
+ if (!$response{'Content-Length'}) {
281
+ // WP does not set content size
282
+ $content_size = ob_get_length();
283
+ @header("Content-Length: $content_size");
284
+ array_push($wp_cache_meta_object->headers, "Content-Length: $content_size");
285
+ }
286
+ */
287
+ if (!$response{'Last-Modified'}) {
288
+ $value = gmdate('D, d M Y H:i:s') . ' GMT';
289
+ /* Dont send this the first time */
290
+ /* @header('Last-Modified: ' . $value); */
291
+ array_push($wp_cache_meta_object->headers, "Last-Modified: $value");
292
+ }
293
+ if (!$response{'Content-Type'} && !$response{'Content-type'}) {
294
+ $value = "text/html; charset=" . get_settings('blog_charset');
295
+ @header("Content-Type: $value");
296
+ array_push($wp_cache_meta_object->headers, "Content-Type: $value");
297
+ }
298
+
299
+ ob_end_flush();
300
+ if ($new_cache) {
301
+ $serial = serialize($wp_cache_meta_object);
302
+ wp_cache_writers_entry();
303
+ $fr = fopen($cache_path . 'meta/' . $meta_file, 'w');
304
+ if( !$fr )
305
+ @mkdir( $cache_path . 'meta/' );
306
+ $fr = fopen($cache_path . 'meta/' . $meta_file, 'w');
307
+ fputs($fr, $serial);
308
+ fclose($fr);
309
+ wp_cache_writers_exit();
310
+ }
311
+
312
+ if ($file_expired == false) {
313
+ return;
314
+ }
315
+
316
+ // we delete expired files
317
+ flush(); //Ensure we send data to the client
318
+ wp_cache_phase2_clean_expired($file_prefix);
319
+ }
320
+
321
+ function wp_cache_no_postid($id) {
322
+ return wp_cache_post_change(wp_cache_post_id());
323
+ }
324
+
325
+ function wp_cache_get_postid_from_comment($comment_id) {
326
+ $comment = get_commentdata($comment_id, 1, true);
327
+ $postid = $comment['comment_post_ID'];
328
+ // Do nothing if comment is not moderated
329
+ // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
330
+ if( !preg_match('/wp-admin\//', $_SERVER['REQUEST_URI']) && $comment['comment_approved'] == 'spam' ) // changed from 1 to "spam"
331
+ return $post_id;
332
+ // We must check it up again due to WP bugs calling two different actions
333
+ // for delete, for example both wp_set_comment_status and delete_comment
334
+ // are called when deleting a comment
335
+ if ($postid > 0)
336
+ return wp_cache_post_change($postid);
337
+ else
338
+ return wp_cache_post_change(wp_cache_post_id());
339
+ }
340
+
341
+ function wp_cache_post_change($post_id) {
342
+ global $file_prefix;
343
+ global $cache_path;
344
+ global $blog_id;
345
+ global $blogcacheid;
346
+ static $last_processed = -1;
347
+
348
+ // Avoid cleaning twice the same pages
349
+ if ($post_id == $last_processed) return $post_id;
350
+ $last_processed = $post_id;
351
+ $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
352
+ $siteurl = str_replace( 'http://', '', get_option( 'siteurl' ) );
353
+ $dir = $cache_path . 'supercache/' . strtolower(preg_replace('/:.*$/', '', $siteurl ) );
354
+ $files_to_delete = array( $dir . '/index.html', $dir . '/feed/index.html', $dir . $permalink . 'index.html', $dir . $permalink . 'feed/index.html' );
355
+ foreach( $files_to_delete as $cache_file ) {
356
+ @unlink( $cache_file );
357
+ @unlink( $cache_file . '.gz' );
358
+ }
359
+
360
+ $meta = new CacheMeta;
361
+ $matches = array();
362
+ wp_cache_writers_entry();
363
+ if ( ($handle = opendir( $cache_path . 'meta/' )) ) {
364
+ while ( false !== ($file = readdir($handle))) {
365
+ if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
366
+ $meta_pathname = $cache_path . 'meta/' . $file;
367
+ $content_pathname = $cache_path . $matches[1] . ".html";
368
+ $meta = unserialize(@file_get_contents($meta_pathname));
369
+ if ($post_id > 0 && $meta) {
370
+ if ($meta->blog_id == $blog_id && (!$meta->post || $meta->post == $post_id) ) {
371
+ unlink($meta_pathname);
372
+ unlink($content_pathname);
373
+ }
374
+ } elseif ($meta->blog_id == $blog_id) {
375
+ unlink($meta_pathname);
376
+ unlink($content_pathname);
377
+ }
378
+
379
+ }
380
+ }
381
+ closedir($handle);
382
+ }
383
+ wp_cache_writers_exit();
384
+ return $post_id;
385
+ }
386
+
387
+ function wp_cache_microtime_diff($a, $b) {
388
+ list($a_dec, $a_sec) = explode(' ', $a);
389
+ list($b_dec, $b_sec) = explode(' ', $b);
390
+ return $b_sec - $a_sec + $b_dec - $a_dec;
391
+ }
392
+
393
+ function wp_cache_post_id() {
394
+ global $posts, $comment_post_ID, $post_ID;
395
+ // We try hard all options. More frequent first.
396
+ if ($post_ID > 0 ) return $post_ID;
397
+ if ($comment_post_ID > 0 ) return $comment_post_ID;
398
+ if (is_single() || is_page()) return $posts[0]->ID;
399
+ if ($_GET['p'] > 0) return $_GET['p'];
400
+ if ($_POST['p'] > 0) return $_POST['p'];
401
+ return 0;
402
+ }
403
+
404
+ if( !function_exists( 'mkpath' ) ) {
405
+ function mkpath($path) {
406
+ if(@mkdir($path, 0777) or file_exists($path)) return true;
407
+ return (mkpath(dirname($path)) and mkdir($path, 0777));
408
+ }
409
+ }
410
+ ?>
wp-cache.php ADDED
@@ -0,0 +1,704 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: WP Super Cache
4
+ Plugin URI: http://ocaoimh.ie/wp-super-cache/
5
+ Description: Very fast cache module. It's composed of several modules, this plugin can configure and manage the whole system. Once enabled, you must <a href="options-general.php?page=wp-super-cache/wp-cache.php">enable the cache</a>. Based on WP-Cache by <a href="http://mnm.uib.es/gallir/">Ricardo Galli Granada</a>.
6
+ Version: 0.1
7
+ Author: Donncha O Caoimh
8
+ Author URI: http://ocaoimh.ie/
9
+ */
10
+ /* Copyright 2005-2006 Ricardo Galli Granada (email : gallir@uib.es)
11
+ Some code copyright 2007 Donncha O Caoimh (http://ocaoimh.ie/)
12
+
13
+ This program is free software; you can redistribute it and/or modify
14
+ it under the terms of the GNU General Public License as published by
15
+ the Free Software Foundation; either version 2 of the License, or
16
+ (at your option) any later version.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
22
+
23
+ You should have received a copy of the GNU General Public License
24
+ along with this program; if not, write to the Free Software
25
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
+ */
27
+
28
+ $wp_cache_config_file = ABSPATH . 'wp-content/wp-cache-config.php';
29
+
30
+ if( !@include($wp_cache_config_file) ) {
31
+ get_wpcachehome();
32
+ $wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
33
+ @include($wp_cache_config_file_sample);
34
+ } else {
35
+ get_wpcachehome();
36
+ }
37
+
38
+ $wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
39
+ $wp_cache_link = ABSPATH . 'wp-content/advanced-cache.php';
40
+ $wp_cache_file = WPCACHEHOME . 'wp-cache-phase1.php';
41
+
42
+ include(WPCACHEHOME . 'wp-cache-base.php');
43
+
44
+ function get_wpcachehome() {
45
+ if( defined( 'WPCACHEHOME' ) == false ) {
46
+ if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
47
+ define( 'WPCACHEHOME', trailingslashit( dirname(__FILE__) ) );
48
+ } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
49
+ define( 'WPCACHEHOME', dirname(__FILE__) . '/wp-super-cache/' );
50
+ } else {
51
+ die( 'Please create wp-content/wp-cache-config.php from wp-super-cache/wp-cache-config-sample.php' );
52
+ }
53
+ }
54
+ }
55
+
56
+ function wp_cache_add_pages() {
57
+ if( function_exists( 'is_site_admin' ) )
58
+ if( !is_site_admin() )
59
+ return;
60
+
61
+ add_options_page('WP Super Cache Manager', 'WP Super Cache', 'administrator', __FILE__, 'wp_cache_manager');
62
+ }
63
+
64
+ function wp_cache_manager() {
65
+ global $wp_cache_config_file, $valid_nonce, $supercachedir, $cache_path, $cache_enabled, $cache_compression, $super_cache_enabled;
66
+
67
+ if( function_exists( 'is_site_admin' ) )
68
+ if( !is_site_admin() )
69
+ return;
70
+
71
+ $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
72
+ if( get_option( 'gzipcompression' ) == 1 )
73
+ update_option( 'gzipcompression', 0 );
74
+ $valid_nonce = wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache');
75
+
76
+ echo '<div class="wrap">';
77
+ echo "<h2>WP Super Cache Manager</h2>\n";
78
+ if(isset($_REQUEST['wp_restore_config']) && $valid_nonce) {
79
+ unlink($wp_cache_config_file);
80
+ echo '<strong>Configuration file changed, some values might be wrong. Load the page again from the "Options" menu to reset them.</strong>';
81
+ }
82
+
83
+ echo '<a name="main"></a><fieldset class="options"><legend>Main options</legend>';
84
+ if ( !wp_cache_check_link() ||
85
+ !wp_cache_verify_config_file() ||
86
+ !wp_cache_verify_cache_dir() ) {
87
+ echo "<br>Cannot continue... fix previous problems and retry.<br />";
88
+ echo "</fieldset></div>\n";
89
+ return;
90
+ }
91
+
92
+ if (!wp_cache_check_global_config()) {
93
+ echo "</fieldset></div>\n";
94
+ return;
95
+ }
96
+
97
+ echo "<h4>WP Super Cache is:</h4>";
98
+ if ( $valid_nonce ) {
99
+ if( isset( $_POST[ 'wp_cache_status' ] ) ) {
100
+ switch( $_POST[ 'wp_cache_status' ] ) {
101
+ case 'all':
102
+ wp_cache_enable();
103
+ break;
104
+ case 'none':
105
+ wp_cache_disable();
106
+ break;
107
+ case 'wpcache':
108
+ wp_cache_enable();
109
+ wp_super_cache_disable();
110
+ break;
111
+ }
112
+ }
113
+ if( isset( $_POST[ 'cache_compression' ] ) && $_POST[ 'cache_compression' ] != $cache_compression ) {
114
+ $cache_compression_changed = true;
115
+ $cache_compression = intval( $_POST[ 'cache_compression' ] );
116
+ wp_cache_replace_line('^ *\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
117
+ prune_super_cache( $cache_path, true );
118
+ delete_option( 'super_cache_meta' );
119
+ }
120
+ }
121
+
122
+ echo '<form name="wp_manager" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
123
+ ?>
124
+ <label><input type='radio' name='wp_cache_status' value='all' <?php if( $cache_enabled == true && $super_cache_enabled == true ) { echo 'checked=checked'; } ?>> Enabled</label><br />
125
+ <label><input type='radio' name='wp_cache_status' value='none' <?php if( $cache_enabled == false ) { echo 'checked=checked'; } ?>> Disabled</label><br />
126
+ <label><input type='radio' name='wp_cache_status' value='wpcache' <?php if( $cache_enabled == true && $super_cache_enabled == false ) { echo 'checked=checked'; } ?>> Super Cache Disabled</label><br />
127
+ <p><strong>Super Cache compression:</strong>
128
+ <label><input type="radio" name="cache_compression" value="1" <?php if( $cache_compression ) { echo "checked=checked"; } ?>> Enabled</label>
129
+ <label><input type="radio" name="cache_compression" value="0" <?php if( !$cache_compression ) { echo "checked=checked"; } ?>> Disabled</label>
130
+ <p>Compression is disabled by default because some hosts have problems with compressed files. Switching this on and off clears the cache.</p>
131
+ <?php
132
+ if( isset( $cache_compression_changed ) && isset( $_POST[ 'cache_compression' ] ) && !$cache_compression ) {
133
+ ?><p><strong>Super Cache compression is now disabled. You should remove or comment out the following rules in your .htaccess file:</strong></p>
134
+ <blockquote style='background-color: #ff6'><code>RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$<br />
135
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$<br />
136
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$<br />
137
+ RewriteCond %{HTTP:Accept-Encoding} gzip<br />
138
+ RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz -f<br />
139
+ RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz [L]</code>
140
+ </blockquote><?php
141
+ } elseif( isset( $cache_compression_changed ) && isset( $_POST[ 'cache_compression' ] ) && $cache_compression ) {
142
+ ?><p><strong>Super Cache compression is now enabled. You must add or uncomment the following rules in your .htaccess file:</strong></p>
143
+ <blockquote style='background-color: #ff6'><code>RewriteCond %{HTTP_COOKIE} !^.*comment_author_.*$<br />
144
+ RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*$<br />
145
+ RewriteCond %{HTTP_COOKIE} !^.*wp-postpass_.*$<br />
146
+ RewriteCond %{HTTP:Accept-Encoding} gzip<br />
147
+ RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz -f<br />
148
+ RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1index.html.gz [L]</code>
149
+ </blockquote><?php
150
+ }
151
+ echo '<div class="submit"><input type="submit"value="Update" /></div>';
152
+ wp_nonce_field('wp-cache');
153
+ echo "</form>\n";
154
+
155
+ wp_cache_edit_max_time();
156
+ echo '</fieldset>';
157
+
158
+ echo '<a name="files"></a><fieldset class="options"><legend>Accepted filenames, rejected URIs</legend>';
159
+ wp_cache_edit_rejected();
160
+ echo "<br />\n";
161
+ wp_cache_edit_accepted();
162
+ echo '</fieldset>';
163
+
164
+ wp_cache_edit_rejected_ua();
165
+
166
+ wp_cache_files();
167
+
168
+ wp_cache_restore();
169
+
170
+ ob_start();
171
+ do_cacheaction( 'cache_admin_page' );
172
+ $out = ob_get_contents();
173
+ ob_end_clean();
174
+ if( $out != '' ) {
175
+ echo '<fieldset class="options"><legend>Cache Plugins</legend>';
176
+ echo $out;
177
+ echo '</fieldset>';
178
+ }
179
+
180
+ echo "</div>\n";
181
+
182
+ }
183
+
184
+ function wp_cache_restore() {
185
+ echo '<fieldset class="options"><legend>Configuration messed up?</legend>';
186
+ echo '<form name="wp_restore" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
187
+ echo '<input type="hidden" name="wp_restore_config" />';
188
+ echo '<div class="submit"><input type="submit" id="deletepost" value="Restore default configuration" /></div>';
189
+ wp_nonce_field('wp-cache');
190
+ echo "</form>\n";
191
+ echo '</fieldset>';
192
+
193
+ }
194
+
195
+ function wp_cache_edit_max_time () {
196
+ global $super_cache_max_time, $cache_max_time, $wp_cache_config_file, $valid_nonce;
197
+
198
+ if( !isset( $super_cache_max_time ) )
199
+ $super_cache_max_time = 21600;
200
+
201
+ if(isset($_POST['wp_max_time']) && $valid_nonce) {
202
+ $max_time = (int)$_POST['wp_max_time'];
203
+ if ($max_time > 0) {
204
+ $cache_max_time = $max_time;
205
+ wp_cache_replace_line('^ *\$cache_max_time', "\$cache_max_time = $cache_max_time;", $wp_cache_config_file);
206
+ }
207
+ }
208
+ if(isset($_POST['super_cache_max_time']) && $valid_nonce) {
209
+ $max_time = (int)$_POST['super_cache_max_time'];
210
+ if ($max_time > 0) {
211
+ $super_cache_max_time = $max_time;
212
+ wp_cache_replace_line('^ *\$super_cache_max_time', "\$super_cache_max_time = $super_cache_max_time;", $wp_cache_config_file);
213
+ }
214
+ }
215
+ echo '<form name="wp_edit_max_time" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
216
+ echo '<label for="wp_max_time">Expire time (in seconds)</label>';
217
+ echo "<input type=\"text\" name=\"wp_max_time\" value=\"$cache_max_time\" /><br />";
218
+ echo '<label for="super_cache_max_time">Super Cache Expire time (in seconds)</label>';
219
+ echo "<input type=\"text\" name=\"super_cache_max_time\" value=\"$super_cache_max_time\" />";
220
+ echo '<div class="submit"><input type="submit" value="Change expiration" /></div>';
221
+ wp_nonce_field('wp-cache');
222
+ echo "</form>\n";
223
+
224
+
225
+ }
226
+
227
+ function wp_cache_sanitize_value($text, & $array) {
228
+ $text = wp_specialchars(strip_tags($text));
229
+ $array = preg_split("/[\s,]+/", chop($text));
230
+ $text = var_export($array, true);
231
+ $text = preg_replace('/[\s]+/', ' ', $text);
232
+ return $text;
233
+ }
234
+
235
+ function wp_cache_edit_rejected_ua() {
236
+ global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;
237
+
238
+ if (!function_exists('apache_request_headers')) return;
239
+
240
+ if(isset($_REQUEST['wp_rejected_user_agent']) && $valid_nonce) {
241
+ $text = wp_cache_sanitize_value($_REQUEST['wp_rejected_user_agent'], $cache_rejected_user_agent);
242
+ wp_cache_replace_line('^ *\$cache_rejected_user_agent', "\$cache_rejected_user_agent = $text;", $wp_cache_config_file);
243
+ }
244
+
245
+
246
+ echo '<a name="user-agents"></a><fieldset class="options"><legend>Rejected User Agents</legend>';
247
+ echo "<p>Strings in the HTTP 'User Agent' header that prevent WP-Cache from
248
+ caching bot, spiders, and crawlers' requests.
249
+ Note that cached files are still sent to these request if they already exists.</p>\n";
250
+ echo '<form name="wp_edit_rejected_user_agent" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
251
+ echo '<label for="wp_rejected_user_agent">Rejected UA strings</label>';
252
+ echo '<textarea name="wp_rejected_user_agent" cols="40" rows="4" style="width: 70%; font-size: 12px;" class="code">';
253
+ foreach ($cache_rejected_user_agent as $ua) {
254
+ echo wp_specialchars($ua) . "\n";
255
+ }
256
+ echo '</textarea> ';
257
+ echo '<div class="submit"><input type="submit" value="Save UA strings" /></div>';
258
+ wp_nonce_field('wp-cache');
259
+ echo '</form>';
260
+ echo "</fieldset>\n";
261
+ }
262
+
263
+
264
+ function wp_cache_edit_rejected() {
265
+ global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
266
+
267
+ if(isset($_REQUEST['wp_rejected_uri']) && $valid_nonce) {
268
+ $text = wp_cache_sanitize_value($_REQUEST['wp_rejected_uri'], $cache_rejected_uri);
269
+ wp_cache_replace_line('^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file);
270
+ }
271
+
272
+
273
+ echo "<p>Add here strings (not a filename) that forces a page not to be cached. For example, if your URLs include year and you dont want to cache last year posts, it's enough to specify the year, i.e. '/2004/'. WP-Cache will search if that string is part of the URI and if so, it will no cache that page.</p>\n";
274
+ echo '<form name="wp_edit_rejected" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
275
+ echo '<label for="wp_rejected_uri">Rejected URIs</label>';
276
+ echo '<textarea name="wp_rejected_uri" cols="40" rows="4" style="width: 70%; font-size: 12px;" class="code">';
277
+ foreach ($cache_rejected_uri as $file) {
278
+ echo wp_specialchars($file) . "\n";
279
+ }
280
+ echo '</textarea> ';
281
+ echo '<div class="submit"><input type="submit" value="Save strings" /></div>';
282
+ wp_nonce_field('wp-cache');
283
+ echo "</form>\n";
284
+ }
285
+
286
+ function wp_cache_edit_accepted() {
287
+ global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
288
+
289
+ if(isset($_REQUEST['wp_accepted_files']) && $valid_nonce) {
290
+ $text = wp_cache_sanitize_value($_REQUEST['wp_accepted_files'], $cache_acceptable_files);
291
+ wp_cache_replace_line('^ *\$cache_acceptable_files', "\$cache_acceptable_files = $text;", $wp_cache_config_file);
292
+ }
293
+
294
+
295
+ echo "<p>Add here those filenames that can be cached, even if they match one of the rejected substring specified above.</p>\n";
296
+ echo '<form name="wp_edit_accepted" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
297
+ echo '<label for="wp_accepted_files">Accepted files</label>';
298
+ echo '<textarea name="wp_accepted_files" cols="40" rows="8" style="width: 70%; font-size: 12px;" class="code">';
299
+ foreach ($cache_acceptable_files as $file) {
300
+ echo wp_specialchars($file) . "\n";
301
+ }
302
+ echo '</textarea> ';
303
+ echo '<div class="submit"><input type="submit" value="Save files" /></div>';
304
+ wp_nonce_field('wp-cache');
305
+ echo "</form>\n";
306
+ }
307
+
308
+ function wp_cache_enable() {
309
+ global $wp_cache_config_file, $cache_enabled, $supercachedir;
310
+
311
+ if(get_settings('gzipcompression')) {
312
+ echo "<b>Error: GZIP compression is enabled, disable it if you want to enable wp-cache.</b><br /><br />";
313
+ return false;
314
+ }
315
+ if( wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = true;', $wp_cache_config_file) ) {
316
+ $cache_enabled = true;
317
+ }
318
+ wp_super_cache_enable();
319
+ }
320
+
321
+ function wp_cache_disable() {
322
+ global $wp_cache_config_file, $cache_enabled, $supercachedir, $cache_path;
323
+
324
+ if (wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = false;', $wp_cache_config_file)) {
325
+ $cache_enabled = false;
326
+ }
327
+ wp_super_cache_disable();
328
+ }
329
+ function wp_super_cache_enable() {
330
+ global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
331
+
332
+ if( is_dir( $supercachedir . ".disabled" ) )
333
+ rename( $supercachedir . ".disabled", $supercachedir );
334
+ wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = true;', $wp_cache_config_file);
335
+ $super_cache_enabled = true;
336
+ }
337
+
338
+ function wp_super_cache_disable() {
339
+ global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
340
+
341
+ if( is_dir( $supercachedir ) )
342
+ rename( $supercachedir, $supercachedir . ".disabled" );
343
+ wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file);
344
+ $super_cache_enabled = false;
345
+ }
346
+
347
+ function wp_cache_is_enabled() {
348
+ global $wp_cache_config_file;
349
+
350
+ if(get_settings('gzipcompression')) {
351
+ echo "<b>Warning</b>: GZIP compression is enabled in Wordpress, wp-cache will be bypassed until you disable gzip compression.<br />";
352
+ return false;
353
+ }
354
+ $lines = file($wp_cache_config_file);
355
+ foreach($lines as $line) {
356
+ if (preg_match('/^ *\$cache_enabled *= *true *;/', $line))
357
+ return true;
358
+ }
359
+ return false;
360
+ }
361
+
362
+
363
+ function wp_cache_replace_line($old, $new, $my_file) {
364
+ if (!is_writable($my_file)) {
365
+ echo "Error: file $my_file is not writable.<br />\n";
366
+ return false;
367
+ }
368
+ $found = false;
369
+ $lines = file($my_file);
370
+ foreach($lines as $line) {
371
+ if ( preg_match("/$old/", $line)) {
372
+ $found = true;
373
+ break;
374
+ }
375
+ }
376
+ if ($found) {
377
+ $fd = fopen($my_file, 'w');
378
+ foreach($lines as $line) {
379
+ if ( !preg_match("/$old/", $line))
380
+ fputs($fd, $line);
381
+ else {
382
+ fputs($fd, "$new //Added by WP-Cache Manager\n");
383
+ }
384
+ }
385
+ fclose($fd);
386
+ return true;
387
+ }
388
+ $fd = fopen($my_file, 'w');
389
+ $done = false;
390
+ foreach($lines as $line) {
391
+ if ( $done || !preg_match('/^define|\$|\?>/', $line))
392
+ fputs($fd, $line);
393
+ else {
394
+ fputs($fd, "$new //Added by WP-Cache Manager\n");
395
+ fputs($fd, $line);
396
+ $done = true;
397
+ }
398
+ }
399
+ fclose($fd);
400
+ return true;
401
+ /*
402
+ copy($my_file, $my_file . "-prev");
403
+ rename($my_file . '-new', $my_file);
404
+ */
405
+ }
406
+
407
+ function wp_cache_verify_cache_dir() {
408
+ global $cache_path;
409
+
410
+ $dir = dirname($cache_path);
411
+ if ( !file_exists($cache_path) ) {
412
+ if ( !is_writable( $dir ) || !($dir = mkdir( $cache_path ) ) ) {
413
+ echo "<b>Error:</b> Your cache directory (<b>$cache_path</b>) did not exist and couldn't be created by the web server. <br /> Check $dir permissions.";
414
+ return false;
415
+ }
416
+ }
417
+ if ( !is_writable($cache_path)) {
418
+ echo "<b>Error:</b> Your cache directory (<b>$cache_path</b>) or <b>$dir</b> need to be writable for this plugin to work. <br /> Double-check it.";
419
+ return false;
420
+ }
421
+
422
+ if ( '/' != substr($cache_path, -1)) {
423
+ $cache_path .= '/';
424
+ }
425
+
426
+ @mkdir( $cache_path . 'meta/' );
427
+
428
+ return true;
429
+ }
430
+
431
+ function wp_cache_verify_config_file() {
432
+ global $wp_cache_config_file, $wp_cache_config_file_sample;
433
+
434
+ $new = false;
435
+ $dir = dirname($wp_cache_config_file);
436
+
437
+ if ( !is_writable($dir)) {
438
+ echo "<b>Error:</b> wp-content directory (<b>$dir</b>) is not writable by the Web server.<br />Check its permissions.";
439
+ return false;
440
+ }
441
+ if ( !file_exists($wp_cache_config_file) ) {
442
+ if ( !file_exists($wp_cache_config_file_sample) ) {
443
+ echo "<b>Error:</b> Sample WP-Cache config file (<b>$wp_cache_config_file_sample</b>) does not exist.<br />Verify you installation.";
444
+ return false;
445
+ }
446
+ copy($wp_cache_config_file_sample, $wp_cache_config_file);
447
+ if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
448
+ wp_cache_replace_line('WPCACHEHOME', "define( 'WPCACHEHOME', " . str_replace( ABSPATH, 'ABSPATH . "', dirname(__FILE__) ) . "/\" );", $wp_cache_config_file);
449
+ } elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
450
+ wp_cache_replace_line('WPCACHEHOME', "define( 'WPCACHEHOME', " . str_replace( ABSPATH, 'ABSPATH . "', dirname(__FILE__) ) . "/wp-super-cache/\" );", $wp_cache_config_file);
451
+ }
452
+ $new = true;
453
+ }
454
+ if ( !is_writable($wp_cache_config_file)) {
455
+ echo "<b>Error:</b> Your WP-Cache config file (<b>$wp_cache_config_file</b>) is not writable by the Web server.<br />Check its permissions.";
456
+ return false;
457
+ }
458
+ require($wp_cache_config_file);
459
+ return true;
460
+ }
461
+
462
+ function wp_cache_check_link() {
463
+ global $wp_cache_link, $wp_cache_file;
464
+
465
+ if ( basename(@readlink($wp_cache_link)) != basename($wp_cache_file)) {
466
+ @unlink($wp_cache_link);
467
+ if (!@symlink ($wp_cache_file, $wp_cache_link)) {
468
+ echo "<code>advanced-cache.php</code> link does not exist<br />";
469
+ echo "Create it by executing: <br /><code>ln -s $wp_cache_file $wp_cache_link</code><br /> in your server<br />";
470
+ return false;
471
+ }
472
+ }
473
+ return true;
474
+ }
475
+
476
+ function wp_cache_check_global_config() {
477
+
478
+ $global = ABSPATH . 'wp-config.php';
479
+
480
+ $lines = file($global);
481
+ foreach($lines as $line) {
482
+ if (preg_match('/^ *define *\( *\'WP_CACHE\' *, *true *\) *;/', $line)) {
483
+ return true;
484
+ }
485
+ }
486
+ $line = 'define(\'WP_CACHE\', true);';
487
+ if (!is_writable($global) || !wp_cache_replace_line('define *\( *\'WP_CACHE\'', $line, $global) ) {
488
+ echo "<b>Error: WP_CACHE is not enabled</b> in your <code>wp-config.php</code> file and I couldn't modified it.<br />";
489
+ echo "Edit <code>$global</code> and add the following line: <br /><code>define('WP_CACHE', true);</code><br />Otherwise, <b>WP-Cache will not be executed</b> by Wordpress core. <br />";
490
+ return false;
491
+ }
492
+ return true;
493
+ }
494
+
495
+ function wp_cache_files() {
496
+ global $cache_path, $file_prefix, $cache_max_time, $super_cache_max_time, $valid_nonce, $supercachedir;
497
+
498
+ if ( '/' != substr($cache_path, -1)) {
499
+ $cache_path .= '/';
500
+ }
501
+
502
+ if ( $valid_nonce ) {
503
+ if(isset($_REQUEST['wp_delete_cache'])) {
504
+ wp_cache_clean_cache($file_prefix);
505
+ }
506
+ if(isset($_REQUEST['wp_delete_cache_file'])) {
507
+ wp_cache_clean_cache($_REQUEST['wp_delete_cache_file']);
508
+ }
509
+ if(isset($_REQUEST['wp_delete_expired'])) {
510
+ wp_cache_clean_expired($file_prefix);
511
+ }
512
+ }
513
+ if(isset($_REQUEST['wp_list_cache'])) {
514
+ $list_files = true;
515
+ $list_mess = "Update list";
516
+ } else
517
+ $list_mess = "List files";
518
+
519
+ echo '<a name="list"></a><fieldset class="options"><legend>Cache contents</legend>';
520
+ echo '<form name="wp_cache_content_list" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
521
+ echo '<input type="hidden" name="wp_list_cache" />';
522
+ echo '<div class="submit"><input type="submit" value="'.$list_mess.'" /></div>';
523
+ echo "</form>\n";
524
+
525
+ $count = 0;
526
+ $expired = 0;
527
+ $now = time();
528
+ if ( ($handle = opendir( $cache_path . 'meta/' )) ) {
529
+ if ($list_files) echo "<table cellspacing=\"0\" cellpadding=\"5\">";
530
+ while ( false !== ($file = readdir($handle))) {
531
+ if ( preg_match("/^$file_prefix.*\.meta/", $file) ) {
532
+ $this_expired = false;
533
+ $content_file = preg_replace("/meta$/", "html", $file);
534
+ $mtime = filemtime($cache_path . 'meta/' . $file);
535
+ if ( ! ($fsize = @filesize($cache_path.$content_file)) )
536
+ continue; // .meta does not exists
537
+ $fsize = intval($fsize/1024);
538
+ $age = $now - $mtime;
539
+ if ( $age > $cache_max_time) {
540
+ $expired++;
541
+ $this_expired = true;
542
+ }
543
+ $count++;
544
+ if ($list_files) {
545
+ $meta = new CacheMeta;
546
+ $meta = unserialize(file_get_contents($cache_path . 'meta/' . $file));
547
+ echo $flip ? '<tr style="background: #EAEAEA;">' : '<tr>';
548
+ $flip = !$flip;
549
+ echo '<td><a href="http://' . $meta->uri . '" target="_blank" >';
550
+ echo $meta->uri . "</a></td>";
551
+ if ($this_expired) echo "<td><span style='color:red'>$age secs</span></td>";
552
+ else echo "<td>$age secs</td>";
553
+ echo "<td>$fsize KB</td>";
554
+ echo '<td><form name="wp_delete_cache_file" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
555
+ echo '<input type="hidden" name="wp_list_cache" />';
556
+ echo '<input type="hidden" name="wp_delete_cache_file" value="'.preg_replace("/^(.*)\.meta$/", "$1", $file).'" />';
557
+ echo '<div class="submit"><input id="deletepost" type="submit" value="Remove" /></div>';
558
+ wp_nonce_field('wp-cache');
559
+ echo "</form></td></tr>\n";
560
+ }
561
+ }
562
+ }
563
+ closedir($handle);
564
+ if ($list_files) echo "</table>";
565
+ }
566
+ $sizes = get_option( 'super_cache_meta' );
567
+ if( !$sizes )
568
+ $sizes = array( 'expired' => 0, 'cached' => 0, 'ts' => 0 );
569
+
570
+ $now = time();
571
+ if( $_POST[ 'super_cache_stats' ] == 1 || $sizes[ 'cached' ] == 0 || $sizes[ 'ts' ] + 3600 <= $now ) {
572
+ $sizes = array( 'expired' => 0, 'cached' => 0, 'ts' => 0 );
573
+
574
+ if (is_dir($supercachedir)) {
575
+ $entries = glob($supercachedir. '/*');
576
+ foreach ($entries as $entry) {
577
+ if ($entry != '.' && $entry != '..') {
578
+ $sizes = wpsc_dirsize( $entry, $sizes );
579
+ }
580
+ }
581
+ } else {
582
+ if(is_file($supercachedir) && filemtime( $supercachedir ) + $super_cache_max_time <= $now )
583
+ $sizes[ 'expired' ] ++;
584
+ }
585
+ $sizes[ 'ts' ] = time();
586
+ update_option( 'super_cache_meta', $sizes );
587
+ }
588
+ echo "<p><strong>WP-Cache</strong></p>";
589
+ echo "<ul><li>$count cached pages</li>";
590
+ echo "<li>$expired expired pages</li></ul>";
591
+ echo "<p><strong>WP-Super-Cache</strong></p>";
592
+ echo "<ul><li>" . intval($sizes['cached']/2) . " cached pages</li>";
593
+ $age = intval(($now - $sizes['ts'])/60);
594
+ echo "<li>" . intval($sizes['expired']/2) . " expired pages. (Generated $age minutes ago. Refresh in " . (60 - $age) . " minutes. )</li></ul>";
595
+
596
+ echo '<form name="wp_cache_content_expired" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
597
+ echo '<input type="hidden" name="wp_delete_expired" />';
598
+ echo '<div class="submit"><input type="submit" value="Delete expired" /></div>';
599
+ wp_nonce_field('wp-cache');
600
+ echo "</form>\n";
601
+
602
+
603
+ echo '<form name="wp_cache_content_delete" action="'. $_SERVER["REQUEST_URI"] . '#list" method="post">';
604
+ echo '<input type="hidden" name="wp_delete_cache" />';
605
+ echo '<div class="submit"><input id="deletepost" type="submit" value="Delete cache" /></div>';
606
+
607
+ wp_nonce_field('wp-cache');
608
+ echo "</form>\n";
609
+
610
+ echo '<form name="wp_super_cache_stats" action="'. $_SERVER["REQUEST_URI"] . '" method="post">';
611
+ echo '<input type="hidden" name="super_cache_stats" value="1" />';
612
+ echo '<div class="submit"><input type="submit" value="Regenerate Super Cache Stats" /></div>';
613
+ wp_nonce_field('wp-cache');
614
+ echo "</form>\n";
615
+
616
+ echo '</fieldset>';
617
+ }
618
+
619
+ function wpsc_dirsize($directory, $sizes) {
620
+ global $super_cache_max_time;
621
+ $now = time();
622
+
623
+ if (is_dir($directory)) {
624
+ $entries = glob($directory. '/*');
625
+ foreach ($entries as $entry) {
626
+ if ($entry != '.' && $entry != '..') {
627
+ $sizes = wpsc_dirsize($entry, $sizes);
628
+ }
629
+ }
630
+ } else {
631
+ if(is_file($directory) ) {
632
+ if( filemtime( $directory ) + $super_cache_max_time <= $now ) {
633
+ $sizes[ 'expired' ]+=1;
634
+ } else {
635
+ $sizes[ 'cached' ]+=1;
636
+ }
637
+ }
638
+ }
639
+ return $sizes;
640
+ }
641
+
642
+
643
+ function wp_cache_clean_cache($file_prefix) {
644
+ global $cache_path, $supercachedir;
645
+
646
+ // If phase2 was compiled, use its function to avoid race-conditions
647
+ if(function_exists('wp_cache_phase2_clean_cache')) {
648
+ if( is_dir( $supercachedir ) ) {
649
+ prune_super_cache( $supercachedir, true );
650
+ } elseif( is_dir( $supercachedir . '.disabled' ) ) {
651
+ prune_super_cache( $supercachedir . '.disabled', true );
652
+ }
653
+ prune_super_cache( $cache_path, true );
654
+ $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
655
+ return wp_cache_phase2_clean_cache($file_prefix);
656
+ }
657
+
658
+ $expr = "/^$file_prefix/";
659
+ if ( ($handle = opendir( $cache_path )) ) {
660
+ while ( false !== ($file = readdir($handle))) {
661
+ if ( preg_match($expr, $file) ) {
662
+ unlink($cache_path . $file);
663
+ unlink($cache_path . 'meta/' . str_replace( '.html', '.term', $file ) );
664
+ }
665
+ }
666
+ closedir($handle);
667
+ }
668
+ }
669
+
670
+ function wp_cache_clean_expired($file_prefix) {
671
+ global $cache_path, $cache_max_time;
672
+
673
+ // If phase2 was compiled, use its function to avoid race-conditions
674
+ if(function_exists('wp_cache_phase2_clean_expired')) {
675
+ $dir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
676
+ if( is_dir( $dir ) ) {
677
+ prune_super_cache( $dir );
678
+ } elseif( is_dir( $dir . '.disabled' ) ) {
679
+ prune_super_cache( $dir . '.disabled' );
680
+ }
681
+ $_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
682
+ return wp_cache_phase2_clean_expired($file_prefix);
683
+ }
684
+
685
+ $expr = "/^$file_prefix/";
686
+ $now = time();
687
+ if ( ($handle = opendir( $cache_path )) ) {
688
+ while ( false !== ($file = readdir($handle))) {
689
+ if ( preg_match($expr, $file) &&
690
+ (filemtime($cache_path . $file) + $cache_max_time) <= $now) {
691
+ unlink($cache_path . $file);
692
+ unlink($cache_path . 'meta/' . str_replace( '.html', '.term', $file ) );
693
+ }
694
+ }
695
+ closedir($handle);
696
+ }
697
+ }
698
+
699
+ add_action('admin_menu', 'wp_cache_add_pages');
700
+
701
+ if( get_option( 'gzipcompression' ) )
702
+ update_option( 'gzipcompression', 0 );
703
+
704
+ ?>