PluginProbe ʕ •ᴥ•ʔ
WP Super Cache / 0.5.2
WP Super Cache v0.5.2
3.1.1 trunk 0.1 0.2 0.3 0.3.1 0.4 0.5 0.5.1 0.5.2 0.5.3 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.7 0.7.1 0.8 0.8.1 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.8.8 0.8.9 0.9 0.9.1 0.9.2 0.9.3 0.9.3.1 0.9.4 0.9.4.1 0.9.4.2 0.9.4.3 0.9.5 0.9.6 0.9.6.1 0.9.7 0.9.8 0.9.9 0.9.9.1 0.9.9.2 0.9.9.3 0.9.9.4 0.9.9.5 0.9.9.6 0.9.9.7 0.9.9.8 0.9.9.9 1.0 1.0.1 1.1 1.1.1 1.10.0 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.12.4 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.7.1 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8 1.9 1.9.1 1.9.2 1.9.3 1.9.4 2.0.0 2.0.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0
wp-super-cache / wp-cache-phase2.php
wp-super-cache Last commit date
plugins 18 years ago Changelog.txt 18 years ago readme.txt 18 years ago wp-cache-base.php 18 years ago wp-cache-config-sample.php 18 years ago wp-cache-phase1.php 18 years ago wp-cache-phase2.php 18 years ago wp-cache.php 18 years ago
wp-cache-phase2.php
480 lines
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 global $wp_cache_gzip_encoding;
13
14 wp_cache_mutex_init();
15 if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) {
16 // Post ID is received
17 add_action('publish_post', 'wp_cache_post_change', 0);
18 add_action('edit_post', 'wp_cache_post_change', 0);
19 add_action('delete_post', 'wp_cache_post_change', 0);
20 add_action('publish_phone', 'wp_cache_post_change', 0);
21 // Coment ID is received
22 add_action('trackback_post', 'wp_cache_get_postid_from_comment', 0);
23 add_action('pingback_post', 'wp_cache_get_postid_from_comment', 0);
24 add_action('comment_post', 'wp_cache_get_postid_from_comment', 0);
25 add_action('edit_comment', 'wp_cache_get_postid_from_comment', 0);
26 add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 0);
27 // No post_id is available
28 add_action('delete_comment', 'wp_cache_no_postid', 0);
29 add_action('switch_theme', 'wp_cache_no_postid', 0);
30
31 do_cacheaction( 'add_cacheaction' );
32 }
33 if( $_SERVER["REQUEST_METHOD"] == 'POST' || get_option('gzipcompression'))
34 return;
35 $script = basename($_SERVER['PHP_SELF']);
36 if (!in_array($script, $cache_acceptable_files) &&
37 wp_cache_is_rejected($_SERVER["REQUEST_URI"]))
38 return;
39 if (wp_cache_user_agent_is_rejected()) return;
40 $wp_cache_meta_object = new CacheMeta;
41 ob_start('wp_cache_ob_callback');
42 register_shutdown_function('wp_cache_shutdown_callback');
43 }
44
45 function wp_cache_get_response_headers() {
46 if(function_exists('apache_response_headers')) {
47 flush();
48 $headers = apache_response_headers();
49 } else if(function_exists('headers_list')) {
50 $headers = array();
51 foreach(headers_list() as $hdr) {
52 list($header_name, $header_value) = explode(': ', $hdr, 2);
53 $headers[$header_name] = $header_value;
54 }
55 } else
56 $headers = null;
57
58 return $headers;
59 }
60
61 function wp_cache_is_rejected($uri) {
62 global $cache_rejected_uri;
63
64 if (strstr($uri, '/wp-admin/'))
65 return true; // we don't allow caching of wp-admin for security reasons
66 foreach ($cache_rejected_uri as $expr) {
67 if( preg_match( "~$expr~", $uri ) )
68 return true;
69 }
70 return false;
71 }
72
73 function wp_cache_user_agent_is_rejected() {
74 global $cache_rejected_user_agent;
75
76 if (!function_exists('apache_request_headers')) return false;
77 $headers = apache_request_headers();
78 if (!isset($headers["User-Agent"])) return false;
79 foreach ($cache_rejected_user_agent as $expr) {
80 if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
81 return true;
82 }
83 return false;
84 }
85
86
87 function wp_cache_mutex_init() {
88 global $use_flock, $mutex, $cache_path, $mutex_filename, $sem_id;
89
90 if(!is_bool($use_flock)) {
91 if(function_exists('sem_get'))
92 $use_flock = false;
93 else
94 $use_flock = true;
95 }
96
97 $mutex = false;
98 if ($use_flock)
99 $mutex = @fopen($cache_path . $mutex_filename, 'w');
100 else
101 $mutex = @sem_get($sem_id, 1, 0644 | IPC_CREAT, 1);
102 }
103
104 function wp_cache_writers_entry() {
105 global $use_flock, $mutex, $cache_path, $mutex_filename;
106
107 if( !$mutex )
108 return false;
109
110 if ($use_flock)
111 flock($mutex, LOCK_EX);
112 else
113 sem_acquire($mutex);
114
115 return true;
116 }
117
118 function wp_cache_writers_exit() {
119 global $use_flock, $mutex, $cache_path, $mutex_filename;
120
121 if( !$mutex )
122 return false;
123
124 if ($use_flock)
125 flock($mutex, LOCK_UN);
126 else
127 sem_release($mutex);
128 }
129
130 function wp_cache_ob_callback($buffer) {
131 global $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
132 global $new_cache, $wp_cache_meta_object, $file_expired, $blog_id, $cache_compression;
133 global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
134
135 /* Mode paranoic, check for closing tags
136 * we avoid caching incomplete files */
137 if (is_404() || !preg_match('/(<\/html>|<\/rss>|<\/feed>)/i',$buffer) ) {
138 $new_cache = false;
139 return $buffer;
140 }
141
142 $duration = wp_cache_microtime_diff($wp_start_time, microtime());
143 $duration = sprintf("%0.3f", $duration);
144 $buffer .= "\n<!-- Dynamic Page Served (once) in $duration seconds -->\n";
145
146 if( !wp_cache_writers_entry() )
147 return false;
148 $mtime = @filemtime($cache_path . $cache_filename);
149 /* Return if:
150 the file didn't exist before but it does exist now (another connection created)
151 OR
152 the file was expired and its mtime is less than 5 seconds
153 */
154 if( !((!$file_expired && $mtime) || ($mtime && $file_expired && (time() - $mtime) < 5)) ) {
155 $uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '..', '', $_SERVER['REQUEST_URI']) );
156 $dir = strtolower(preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacs
157 $dir = trailingslashit( $cache_path . 'supercache/' . $dir );
158 if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) {
159 $dir = trailingslashit( ABSPATH . $uri );
160 }
161 $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
162 if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) )
163 $super_cache_enabled = false;
164
165 $fr = @fopen($cache_path . $cache_filename, 'w');
166 if (!$fr)
167 $buffer = "Couldn't write to: " . $cache_path . $cache_filename . "\n";
168 if( $super_cache_enabled ) {
169 if( @is_dir( $dir ) == false )
170 @wp_mkdir_p( $dir );
171
172 $user_info = wp_cache_get_cookies_values();
173 $do_cache = apply_filters( 'do_createsupercache', $user_info );
174 if( $user_info == '' || $do_cache === true ) {
175 $fr2 = @fopen("{$dir}index.html", 'w');
176 if( $cache_compression )
177 $gz = @gzopen("{$dir}index.html.gz", 'w3');
178 }
179 }
180
181 if (preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content
182 $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
183 "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
184 $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
185 "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
186 $wp_cache_meta_object->dynamic = true;
187 /* Clean function calls in tag */
188 $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
189 $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
190 fputs($fr, $store);
191 if( $fr2 )
192 fputs($fr2, $store . '<!-- super cache -->' );
193 if( $gz )
194 gzwrite($gz, $store . '<!-- super cache gz -->' );
195 } else {
196 $log = "<!-- Cached page served by WP-Cache -->\n";
197
198 if ($wp_cache_gzip_encoding) {
199 $log .= "<!-- Compression = " . $wp_cache_gzip_encoding ." -->";
200 $gzdata = gzencode($buffer . $log, 3, FORCE_GZIP);
201 $gzsize = strlen($gzdata);
202
203 array_push($wp_cache_meta_object->headers, 'Content-Encoding: ' . $wp_cache_gzip_encoding);
204 array_push($wp_cache_meta_object->headers, 'Vary: Accept-Encoding');
205 array_push($wp_cache_meta_object->headers, 'Content-Length: ' . strlen($gzdata));
206 // Return uncompressed data & store compressed for later use
207 fputs($fr, $gzdata);
208 }else{ // no compression
209 fputs($fr, $buffer.$log);
210 }
211 if( $fr2 )
212 fputs($fr2, $buffer . '<!-- super cache -->' );
213 if( $gz )
214 gzwrite($gz, $buffer . '<!-- super cache gz -->' );
215 }
216 $new_cache = true;
217 fclose($fr);
218 if( $fr2 )
219 fclose($fr2);
220 if( $gz )
221 fclose($gz);
222 }
223 wp_cache_writers_exit();
224 return $buffer;
225 }
226
227 function wp_cache_phase2_clean_cache($file_prefix) {
228 global $cache_path;
229
230 if( !wp_cache_writers_entry() )
231 return false;
232 if ( ($handle = opendir( $cache_path )) ) {
233 while ( false !== ($file = readdir($handle))) {
234 if ( preg_match("/^$file_prefix/", $file) ) {
235 @unlink($cache_path . $file);
236 }
237 }
238 closedir($handle);
239 }
240 wp_cache_writers_exit();
241 }
242
243 function prune_super_cache($directory, $force = false) {
244 global $super_cache_max_time, $cache_path;
245
246 if( !isset( $super_cache_max_time ) )
247 $super_cache_max_time = 21600;
248
249 $now = time();
250
251 $protected_directories = array( $cache_path . '.htaccess', $cache_path . 'meta', $cache_path . 'supercache' );
252
253 $oktodelete = false;
254 if (is_dir($directory)) {
255 $directory = trailingslashit( $directory );
256 $entries = glob($directory. '*');
257 if( is_array( $entries ) && !empty( $entries ) ) foreach ($entries as $entry) {
258 if ($entry != '.' && $entry != '..') {
259 prune_super_cache($entry, $force);
260 if( is_dir( $entry ) && ( $force || @filemtime( $entry ) + $super_cache_max_time <= $now ) ) {
261 $oktodelete = true;
262 if( in_array( $entry, $protected_directories ) )
263 $oktodelete = false;
264 if( $oktodelete )
265 @rmdir( addslashes( $entry ) );
266 }
267 }
268 }
269 } else {
270 if( is_file($directory) && ($force || filemtime( $directory ) + $super_cache_max_time <= $now ) ) {
271 $oktodelete = true;
272 if( in_array( $directory, $protected_directories ) )
273 $oktodelete = false;
274 if( $oktodelete )
275 @unlink( addslashes( $directory ) );
276 }
277 }
278 }
279
280 function wp_cache_phase2_clean_expired($file_prefix) {
281 global $cache_path, $cache_max_time;
282
283 clearstatcache();
284 if( !wp_cache_writers_entry() )
285 return false;
286 $now = time();
287 if ( ($handle = opendir( $cache_path )) ) {
288 while ( false !== ($file = readdir($handle))) {
289 if ( preg_match("/^$file_prefix/", $file) &&
290 (filemtime($cache_path . $file) + $cache_max_time) <= $now ) {
291 @unlink($cache_path . $file);
292 @unlink($cache_path . 'meta/' . str_replace( '.html', '.meta', $file ) );
293 continue;
294 }
295 if($file != '.' && $file != '..') {
296 if( is_dir( $cache_path . $file ) == false && (filemtime($cache_path . $file) + $cache_max_time) <= $now ) {
297 if( substr( $file, -9 ) != '.htaccess' )
298 @unlink($cache_path . $file);
299 } elseif( is_dir( $cache_path . $file ) ) {
300 prune_super_cache( $cache_path . $file );
301 }
302 }
303 }
304 closedir($handle);
305 }
306
307 wp_cache_writers_exit();
308 }
309
310 function wp_cache_shutdown_callback() {
311 global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache;
312 global $wp_cache_meta_object, $known_headers, $blog_id;
313
314 $wp_cache_meta_object->uri = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER['REQUEST_URI']); // To avoid XSS attacs
315 $wp_cache_meta_object->blog_id=$blog_id;
316 $wp_cache_meta_object->post = wp_cache_post_id();
317
318 $response = wp_cache_get_response_headers();
319 foreach ($known_headers as $key) {
320 if(isset($response{$key})) {
321 array_push($wp_cache_meta_object->headers, "$key: " . $response{$key});
322 }
323 }
324 /* Not used because it gives problems with some
325 * PHP installations
326 if (!$response{'Content-Length'}) {
327 // WP does not set content size
328 $content_size = ob_get_length();
329 @header("Content-Length: $content_size");
330 array_push($wp_cache_meta_object->headers, "Content-Length: $content_size");
331 }
332 */
333 if (!$response{'Last-Modified'}) {
334 $value = gmdate('D, d M Y H:i:s') . ' GMT';
335 /* Dont send this the first time */
336 /* @header('Last-Modified: ' . $value); */
337 array_push($wp_cache_meta_object->headers, "Last-Modified: $value");
338 }
339 if (!$response{'Content-Type'} && !$response{'Content-type'}) {
340 // On some systems, headers set by PHP can't be fetched from
341 // the output buffer. This is a last ditch effort to set the
342 // correct Content-Type header for feeds, if we didn't see
343 // it in the response headers already. -- dougal
344 if (is_feed()) {
345 $type = get_query_var('feed');
346 $type = str_replace('/','',$type);
347 switch ($type) {
348 case 'atom':
349 $value = "application/atom+xml";
350 break;
351 case 'rdf':
352 $value = "application/rdf+xml";
353 break;
354 case 'rss':
355 case 'rss2':
356 default:
357 $value = "application/rss+xml";
358 }
359 } else { // not a feed
360 $value = 'text/html';
361 }
362 $value .= "; charset=\"" . get_option('blog_charset') . "\"";
363
364 @header("Content-Type: $value");
365 array_push($wp_cache_meta_object->headers, "Content-Type: $value");
366 }
367
368 ob_end_flush();
369 if ($new_cache) {
370 $serial = serialize($wp_cache_meta_object);
371 if( !wp_cache_writers_entry() )
372 return false;
373 $fr = @fopen($cache_path . 'meta/' . $meta_file, 'w');
374 if( !$fr )
375 @mkdir( $cache_path . 'meta' );
376 $fr = fopen($cache_path . 'meta/' . $meta_file, 'w');
377 fputs($fr, $serial);
378 fclose($fr);
379 wp_cache_writers_exit();
380 }
381
382 if ($file_expired == false) {
383 return;
384 }
385
386 // we delete expired files
387 flush(); //Ensure we send data to the client
388 wp_cache_phase2_clean_expired($file_prefix);
389 }
390
391 function wp_cache_no_postid($id) {
392 return wp_cache_post_change(wp_cache_post_id());
393 }
394
395 function wp_cache_get_postid_from_comment($comment_id) {
396 global $super_cache_enabled;
397 $comment = get_commentdata($comment_id, 1, true);
398 $postid = $comment['comment_post_ID'];
399 // Do nothing if comment is not moderated
400 // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
401 if( !preg_match('/wp-admin\//', $_SERVER['REQUEST_URI']) )
402 if( $comment['comment_approved'] == 'spam' ) { // changed from 1 to "spam"
403 return $post_id;
404 } elseif( $comment['comment_approved'] == '0' ) {
405 $super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
406 }
407 // We must check it up again due to WP bugs calling two different actions
408 // for delete, for example both wp_set_comment_status and delete_comment
409 // are called when deleting a comment
410 if ($postid > 0)
411 return wp_cache_post_change($postid);
412 else
413 return wp_cache_post_change(wp_cache_post_id());
414 }
415
416 function wp_cache_post_change($post_id) {
417 global $file_prefix, $cache_path, $blog_id, $blogcacheid, $super_cache_enabled;
418 static $last_processed = -1;
419
420 // Avoid cleaning twice the same pages
421 if ($post_id == $last_processed) return $post_id;
422 $last_processed = $post_id;
423 $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
424 if( $super_cache_enabled ) {
425 $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
426 $siteurl = str_replace( 'http://', '', get_option( 'siteurl' ) );
427 $dir = $cache_path . 'supercache/' . strtolower(preg_replace('/:.*$/', '', $siteurl ) );
428 $files_to_delete = array( $dir . '/index.html', $dir . '/feed/index.html', $dir . $permalink . 'index.html', $dir . $permalink . 'feed/index.html' );
429 foreach( $files_to_delete as $cache_file ) {
430 @unlink( $cache_file );
431 @unlink( $cache_file . '.gz' );
432 }
433 }
434
435 $meta = new CacheMeta;
436 $matches = array();
437 if( !wp_cache_writers_entry() )
438 return $post_id;
439 if ( ($handle = opendir( $cache_path . 'meta/' )) ) {
440 while ( false !== ($file = readdir($handle))) {
441 if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
442 $meta_pathname = $cache_path . 'meta/' . $file;
443 $content_pathname = $cache_path . $matches[1] . ".html";
444 $meta = unserialize(@file_get_contents($meta_pathname));
445 if ($post_id > 0 && $meta) {
446 if ($meta->blog_id == $blog_id && (!$meta->post || $meta->post == $post_id) ) {
447 @unlink($meta_pathname);
448 @unlink($content_pathname);
449 }
450 } elseif ($meta->blog_id == $blog_id) {
451 @unlink($meta_pathname);
452 @unlink($content_pathname);
453 }
454
455 }
456 }
457 closedir($handle);
458 }
459 wp_cache_writers_exit();
460 return $post_id;
461 }
462
463 function wp_cache_microtime_diff($a, $b) {
464 list($a_dec, $a_sec) = explode(' ', $a);
465 list($b_dec, $b_sec) = explode(' ', $b);
466 return $b_sec - $a_sec + $b_dec - $a_dec;
467 }
468
469 function wp_cache_post_id() {
470 global $posts, $comment_post_ID, $post_ID;
471 // We try hard all options. More frequent first.
472 if ($post_ID > 0 ) return $post_ID;
473 if ($comment_post_ID > 0 ) return $comment_post_ID;
474 if (is_single() || is_page()) return $posts[0]->ID;
475 if ($_GET['p'] > 0) return $_GET['p'];
476 if ($_POST['p'] > 0) return $_POST['p'];
477 return 0;
478 }
479 ?>
480