|
1 <?php |
|
2 // Pre-2.6 compatibility |
|
3 if( !defined('WP_CONTENT_DIR') ) |
|
4 define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); |
|
5 |
|
6 if( !include( WP_CONTENT_DIR . '/wp-cache-config.php' ) ) |
|
7 return true; |
|
8 if( !defined( 'WPCACHEHOME' ) ) |
|
9 define('WPCACHEHOME', dirname(__FILE__).'/'); |
|
10 |
|
11 include( WPCACHEHOME . 'wp-cache-base.php'); |
|
12 |
|
13 if( $blogcacheid != '' ) { |
|
14 $blog_cache_dir = str_replace( '//', '/', $cache_path . "blogs/" . $blogcacheid . '/' ); |
|
15 } else { |
|
16 $blog_cache_dir = $cache_path; |
|
17 } |
|
18 |
|
19 $mutex_filename = 'wp_cache_mutex.lock'; |
|
20 $new_cache = false; |
|
21 |
|
22 // Don't change variables behind this point |
|
23 |
|
24 if( !isset( $wp_cache_plugins_dir ) ) |
|
25 $wp_cache_plugins_dir = WPCACHEHOME . 'plugins'; |
|
26 |
|
27 require_once( WPCACHEHOME . 'wp-cache-phase2.php'); |
|
28 |
|
29 $plugins = glob( $wp_cache_plugins_dir . '/*.php' ); |
|
30 if( is_array( $plugins ) ) { |
|
31 foreach ( $plugins as $plugin ) { |
|
32 if( is_file( $plugin ) ) |
|
33 require_once( $plugin ); |
|
34 } |
|
35 } |
|
36 |
|
37 if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST') |
|
38 return true; |
|
39 |
|
40 $file_expired = false; |
|
41 $cache_filename = ''; |
|
42 $meta_file = ''; |
|
43 $wp_cache_gzip_encoding = ''; |
|
44 |
|
45 $gzipped = 0; |
|
46 $gzsize = 0; |
|
47 |
|
48 function gzip_accepted(){ |
|
49 if( ini_get( 'zlib.output_compression' ) ) // don't compress WP-Cache data files when PHP is already doing it |
|
50 return false; |
|
51 |
|
52 if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) return false; |
|
53 return 'gzip'; |
|
54 } |
|
55 |
|
56 if ($cache_compression) { |
|
57 $wp_cache_gzip_encoding = gzip_accepted(); |
|
58 } |
|
59 |
|
60 add_cacheaction( 'wp_cache_get_cookies_values', 'wp_cache_check_mobile' ); |
|
61 |
|
62 $wp_cache_request_uri = $_SERVER[ 'REQUEST_URI' ]; // Cache this in case any plugin modifies it. |
|
63 |
|
64 if( false == @is_dir( $blog_cache_dir ) ) { |
|
65 @mkdir( $cache_path . "blogs" ); |
|
66 @mkdir( $blog_cache_dir ); |
|
67 } |
|
68 |
|
69 if( false == @is_dir( $blog_cache_dir . 'meta' ) ) |
|
70 @mkdir( $blog_cache_dir . 'meta' ); |
|
71 |
|
72 |
|
73 $wp_start_time = microtime(); |
|
74 |
|
75 function wp_super_cache_init() { |
|
76 global $wp_cache_key, $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta; |
|
77 |
|
78 $wp_cache_key = do_cacheaction( 'wp_cache_key', $_SERVER['HTTP_HOST'].preg_replace('/#.*$/', '', str_replace( '/index.php', '/', $wp_cache_request_uri ) ).$wp_cache_gzip_encoding.wp_cache_get_cookies_values() ); |
|
79 $key = $blogcacheid . md5( $wp_cache_key ); |
|
80 $wp_cache_key = $blogcacheid . $wp_cache_key; |
|
81 |
|
82 $cache_filename = $file_prefix . $key . '.html'; |
|
83 $meta_file = $file_prefix . $key . '.meta'; |
|
84 $cache_file = realpath( $blog_cache_dir . $cache_filename ); |
|
85 $meta_pathname = realpath( $blog_cache_dir . 'meta/' . $meta_file ); |
|
86 return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' ); |
|
87 } |
|
88 |
|
89 function wp_cache_serve_cache_file() { |
|
90 global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta; |
|
91 |
|
92 extract( wp_super_cache_init() ); |
|
93 |
|
94 if ( wp_cache_user_agent_is_rejected() ) { |
|
95 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file served as user agent rejected.", 5 ); |
|
96 return false; |
|
97 } |
|
98 |
|
99 if ( file_exists( $cache_file ) ) { |
|
100 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "wp-cache file exists", 5 ); |
|
101 if ( !( $meta = unserialize( @file_get_contents( $meta_pathname) ) ) ) { |
|
102 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "couldn't load wp-cache meta file", 5 ); |
|
103 return true; |
|
104 } |
|
105 if ( is_array( $meta ) == false ) { |
|
106 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 ); |
|
107 @unlink( $meta_pathname ); |
|
108 @unlink( $cache_file ); |
|
109 return true; |
|
110 } |
|
111 $cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file ); |
|
112 // Sometimes the gzip headers are lost. If this is a gzip capable client, send those headers. |
|
113 if( $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) { |
|
114 $meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding; |
|
115 $meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie'; |
|
116 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "added gzip headers before serving.", 1 ); |
|
117 } |
|
118 foreach ($meta[ 'headers' ] as $t => $header) { |
|
119 // godaddy fix, via http://blog.gneu.org/2008/05/wp-supercache-on-godaddy/ and http://www.littleredrails.com/blog/2007/09/08/using-wp-cache-on-godaddy-500-error/ |
|
120 if( strpos( $header, 'Last-Modified:' ) === false ) |
|
121 header($header); |
|
122 } |
|
123 header( 'WP-Super-Cache: WP-Cache' ); |
|
124 if ( $meta[ 'dynamic' ] ) { |
|
125 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file", 5 ); |
|
126 include($cache_file); |
|
127 } else { |
|
128 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file", 5 ); |
|
129 readfile( $cache_file ); |
|
130 } |
|
131 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 ); |
|
132 die(); |
|
133 } else { |
|
134 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file exists. Must generate a new one.", 5 ); |
|
135 } |
|
136 } |
|
137 |
|
138 if(defined('DOING_CRON')) { |
|
139 extract( wp_super_cache_init() ); |
|
140 return true; |
|
141 } |
|
142 |
|
143 if ( !isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && false == $wp_super_cache_late_init ) ) { |
|
144 wp_cache_serve_cache_file(); |
|
145 } |
|
146 |
|
147 function wp_cache_postload() { |
|
148 global $cache_enabled, $wp_super_cache_late_init, $wp_super_cache_debug; |
|
149 |
|
150 if ( !$cache_enabled || isset( $_GET[ 'preview' ] ) ) |
|
151 return true; |
|
152 |
|
153 if ( isset( $wp_super_cache_late_init ) && true == $wp_super_cache_late_init ) { |
|
154 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Init: add wp_cache_serve_cache_file to init", 3 ); |
|
155 add_action( 'init', 'wp_cache_late_loader', 9999 ); |
|
156 } else { |
|
157 wp_super_cache_init(); |
|
158 wp_cache_phase2(); |
|
159 } |
|
160 } |
|
161 |
|
162 function wp_cache_late_loader() { |
|
163 global $wp_super_cache_debug; |
|
164 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Loader running on init", 3 ); |
|
165 wp_cache_serve_cache_file(); |
|
166 wp_cache_phase2(); |
|
167 } |
|
168 |
|
169 function wp_cache_get_cookies_values() { |
|
170 $string = ''; |
|
171 while ($key = key($_COOKIE)) { |
|
172 if ( preg_match( "/^wp-postpass|^wordpress|^comment_author_/", $key ) ) { |
|
173 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cookie detected: $key", 5 ); |
|
174 $string .= $_COOKIE[ $key ] . ","; |
|
175 } |
|
176 next($_COOKIE); |
|
177 } |
|
178 reset($_COOKIE); |
|
179 |
|
180 // If you use this hook, make sure you update your .htaccess rules with the same conditions |
|
181 $string = do_cacheaction( 'wp_cache_get_cookies_values', $string ); |
|
182 return $string; |
|
183 } |
|
184 |
|
185 function add_cacheaction( $action, $func ) { |
|
186 global $wp_supercache_actions; |
|
187 $wp_supercache_actions[ $action ][] = $func; |
|
188 } |
|
189 |
|
190 function do_cacheaction( $action, $value = '' ) { |
|
191 global $wp_supercache_actions; |
|
192 if( is_array( $wp_supercache_actions[ $action ] ) ) { |
|
193 $actions = $wp_supercache_actions[ $action ]; |
|
194 foreach( $actions as $func ) { |
|
195 $value = $func( $value ); |
|
196 } |
|
197 } |
|
198 |
|
199 return $value; |
|
200 } |
|
201 |
|
202 // From http://wordpress.org/extend/plugins/wordpress-mobile-edition/ by Alex King |
|
203 function wp_cache_check_mobile( $cache_key ) { |
|
204 global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_whitelist; |
|
205 if( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled ) |
|
206 return $cache_key; |
|
207 |
|
208 if (!isset($_SERVER["HTTP_USER_AGENT"])) { |
|
209 return $cache_key; |
|
210 } |
|
211 $whitelist = explode( ',', $wp_cache_mobile_whitelist ); |
|
212 foreach ($whitelist as $browser) { |
|
213 if (strstr($_SERVER["HTTP_USER_AGENT"], trim($browser))) { |
|
214 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "whitelst mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 ); |
|
215 return $cache_key; |
|
216 } |
|
217 } |
|
218 |
|
219 $browsers = explode( ',', $wp_cache_mobile_browsers ); |
|
220 foreach ($browsers as $browser) { |
|
221 if (strstr($_SERVER["HTTP_USER_AGENT"], trim( $browser ))) { |
|
222 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 ); |
|
223 return $cache_key . $browser; |
|
224 } |
|
225 } |
|
226 return $cache_key; |
|
227 } |
|
228 |
|
229 function wp_cache_debug( $message, $level = 1 ) { |
|
230 global $wp_cache_debug_level, $wp_cache_debug_log, $wp_cache_debug_email, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug; |
|
231 |
|
232 if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug == false ) |
|
233 return false; |
|
234 |
|
235 if ( isset( $wp_cache_debug_email ) == false && isset( $wp_cache_debug_log ) == false ) |
|
236 return false; |
|
237 |
|
238 if ( isset( $wp_cache_debug_level ) == false ) |
|
239 $wp_cache_debug_level = 1; |
|
240 if ( $wp_cache_debug_level < $level ) |
|
241 return false; |
|
242 |
|
243 if ( isset( $wp_cache_debug_ip ) && $wp_cache_debug_ip != '' && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] ) |
|
244 return false; |
|
245 |
|
246 if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' ) { |
|
247 error_log( date( 'H:i:s' ) . " " . $_SERVER[ 'REQUEST_URI' ] . " " . $message . "\n", 3, $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) ) ); |
|
248 } |
|
249 |
|
250 if ( isset( $wp_cache_debug_email ) && $wp_cache_debug_email != '' ) { |
|
251 $message .= "\n\nDisable these emails by commenting out or deleting the line containing\n\$wp_cache_debug_email in wp-content/wp-cache-config.php on your server.\n"; |
|
252 mail( $wp_cache_debug_email, '[' . addslashes( $_SERVER[ 'HTTP_HOST' ] ) . "] WP Super Cache Debug", $message ); |
|
253 } |
|
254 } |
|
255 |
|
256 function wp_cache_user_agent_is_rejected() { |
|
257 global $cache_rejected_user_agent; |
|
258 |
|
259 if (!function_exists('apache_request_headers')) return false; |
|
260 $headers = apache_request_headers(); |
|
261 if (!isset($headers["User-Agent"])) return false; |
|
262 foreach ($cache_rejected_user_agent as $expr) { |
|
263 if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr)) |
|
264 return true; |
|
265 } |
|
266 return false; |
|
267 } |
|
268 |
|
269 ?> |