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

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 24.4 KB
Line 
1<?php
2
3function wp_cache_phase2() {
4 global $cache_filename, $cache_acceptable_files, $wp_cache_gzip_encoding, $super_cache_enabled, $cache_rebuild_files, $wp_cache_gmt_offset, $wp_cache_blog_charset, $wp_cache_last_gc;
5 global $cache_max_time, $wp_cache_not_logged_in, $wp_cache_request_uri;
6
7 $wp_cache_gmt_offset = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349
8 $wp_cache_blog_charset = get_option( 'blog_charset' );
9
10 wp_cache_mutex_init();
11 if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) {
12 // Post ID is received
13 add_action('publish_post', 'wp_cache_post_edit', 0);
14 add_action('edit_post', 'wp_cache_post_change', 0); // leaving a comment called edit_post
15 add_action('delete_post', 'wp_cache_post_edit', 0);
16 add_action('publish_phone', 'wp_cache_post_edit', 0);
17 // Coment ID is received
18 add_action('trackback_post', 'wp_cache_get_postid_from_comment', 99);
19 add_action('pingback_post', 'wp_cache_get_postid_from_comment', 99);
20 add_action('comment_post', 'wp_cache_get_postid_from_comment', 99);
21 add_action('edit_comment', 'wp_cache_get_postid_from_comment', 99);
22 add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 99);
23 // No post_id is available
24 add_action('delete_comment', 'wp_cache_no_postid', 99);
25 add_action('switch_theme', 'wp_cache_no_postid', 99);
26 add_action('edit_user_profile_update', 'wp_cache_no_postid', 99);
27
28 add_action('wp_cache_gc','wp_cache_gc_cron');
29
30 do_cacheaction( 'add_cacheaction' );
31 }
32
33 if( $wp_cache_not_logged_in && is_user_logged_in() )
34 return false;
35
36 if( $_SERVER["REQUEST_METHOD"] == 'POST' || get_option('gzipcompression'))
37 return false;
38 $script = basename($_SERVER['PHP_SELF']);
39 if (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri))
40 return false;
41 if (wp_cache_user_agent_is_rejected()) return;
42 if($wp_cache_gzip_encoding)
43 header('Vary: Accept-Encoding, Cookie');
44 else
45 header('Vary: Cookie');
46 ob_start( 'wp_cache_ob_callback' );
47
48 // restore old supercache file temporarily
49 if( $super_cache_enabled && $cache_rebuild_files ) {
50 $user_info = wp_cache_get_cookies_values();
51 $do_cache = apply_filters( 'do_createsupercache', $user_info );
52 if( $user_info == '' || $do_cache === true ) {
53 $dir = get_current_url_supercache_dir();
54 $files_to_check = array( $dir . 'index.html', $dir . 'index.html.gz' );
55 foreach( $files_to_check as $cache_file ) {
56 if( !@file_exists( $cache_file . '.needs-rebuild' ) )
57 continue;
58 $mtime = @filemtime($cache_file . '.needs-rebuild');
59 if( $mtime && (time() - $mtime) < 30 ) {
60 @rename( $cache_file . '.needs-rebuild', $cache_file );
61 }
62 // cleanup old files or if rename fails
63 if( @file_exists( $cache_file . '.needs-rebuild' ) ) {
64 @unlink( $cache_file . '.needs-rebuild' );
65 }
66 }
67 }
68 }
69
70 if( !isset( $cache_max_time ) )
71 $cache_max_time = 600;
72 $last_gc = get_option( "wpsupercache_gc_time" );
73
74 if( !$last_gc ) {
75 update_option( 'wpsupercache_gc_time', time() );
76 }
77 $next_gc = $cache_max_time < 1800 ? $cache_max_time : 600;
78 if( $last_gc < ( time() - $next_gc ) ) {
79 update_option( 'wpsupercache_gc_time', time() );
80
81 global $wp_cache_shutdown_gc;
82 if( !isset( $wp_cache_shutdown_gc ) || $wp_cache_shutdown_gc == 0 ) {
83 if(!wp_next_scheduled('wp_cache_gc')) wp_schedule_single_event(time() + 10 , 'wp_cache_gc');
84 } else {
85 global $time_to_gc_cache;
86 $time_to_gc_cache = 1; // tell the "shutdown gc" to run!
87 }
88 }
89}
90
91function wp_cache_get_response_headers() {
92 if(function_exists('apache_response_headers')) {
93 flush();
94 $headers = apache_response_headers();
95 } else if(function_exists('headers_list')) {
96 $headers = array();
97 foreach(headers_list() as $hdr) {
98 list($header_name, $header_value) = explode(': ', $hdr, 2);
99 $headers[$header_name] = $header_value;
100 }
101 } else
102 $headers = null;
103
104 return $headers;
105}
106
107function wp_cache_is_rejected($uri) {
108 global $cache_rejected_uri;
109
110 $auto_rejected = array( '/wp-admin/', 'xmlrpc.php', 'wp-app.php' );
111 foreach( $auto_rejected as $u ) {
112 if( strstr( $uri, $u ) )
113 return true; // we don't allow caching of wp-admin for security reasons
114 }
115 foreach ($cache_rejected_uri as $expr) {
116 if( $expr != '' && preg_match( "~$expr~", $uri ) )
117 return true;
118 }
119 return false;
120}
121
122function wp_cache_mutex_init() {
123 global $use_flock, $mutex, $cache_path, $mutex_filename, $sem_id, $blog_cache_dir;
124
125 if(!is_bool($use_flock)) {
126 if(function_exists('sem_get'))
127 $use_flock = false;
128 else
129 $use_flock = true;
130 }
131
132 $mutex = false;
133 if ($use_flock)
134 $mutex = @fopen($blog_cache_dir . $mutex_filename, 'w');
135 else
136 $mutex = @sem_get($sem_id, 1, 0644 | IPC_CREAT, 1);
137}
138
139function wp_cache_writers_entry() {
140 global $use_flock, $mutex, $cache_path, $mutex_filename, $wp_cache_mutex_disabled;
141
142 if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
143 return true;
144
145 if( !$mutex )
146 return false;
147
148 if ($use_flock)
149 flock($mutex, LOCK_EX);
150 else
151 sem_acquire($mutex);
152
153 return true;
154}
155
156function wp_cache_writers_exit() {
157 global $use_flock, $mutex, $cache_path, $mutex_filename, $wp_cache_mutex_disabled;
158
159 if( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled )
160 return true;
161
162 if( !$mutex )
163 return false;
164
165 if ($use_flock)
166 flock($mutex, LOCK_UN);
167 else
168 sem_release($mutex);
169}
170
171function get_current_url_supercache_dir() {
172 global $cached_direct_pages, $cache_path, $wp_cache_request_uri;
173 $uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $wp_cache_request_uri ) ) ) );
174 $uri = str_replace( '\\', '', $uri );
175 $dir = strtolower(preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacks
176 $dir = apply_filters( 'supercache_dir', $dir );
177 $dir = trailingslashit( $cache_path . 'supercache/' . $dir );
178 if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) {
179 $dir = trailingslashit( ABSPATH . $uri );
180 }
181 $dir = str_replace( '//', '/', $dir );
182 return $dir;
183}
184
185function wp_cache_ob_callback( $buffer ) {
186 if( defined( 'DONOTCACHEPAGE' ) )
187 return $buffer;
188 $buffer = &wp_cache_get_ob( $buffer );
189 wp_cache_shutdown_callback();
190 return $buffer;
191}
192
193
194function wp_cache_get_ob(&$buffer) {
195 global $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
196 global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
197 global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
198 global $wp_cache_404, $gzsize, $supercacheonly, $wp_cache_gzip_first, $wp_cache_gmt_offset;
199 global $blog_cache_dir, $wp_cache_request_uri;
200
201 $new_cache = true;
202 $wp_cache_meta = '';
203
204 /* Mode paranoic, check for closing tags
205 * we avoid caching incomplete files */
206 if( $wp_cache_404 ) {
207 $new_cache = false;
208 $buffer .= "\n<!-- Page not cached by WP Super Cache. 404. -->\n";
209 }
210
211 if (!preg_match('/(<\/html>|<\/rss>|<\/feed>)/i',$buffer) ) {
212 $new_cache = false;
213 if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) )
214 $buffer .= "\n<!-- Page not cached by WP Super Cache. No closing HTML tag. Check your theme. -->\n";
215 }
216
217 if( !$new_cache )
218 return $buffer;
219
220 $duration = wp_cache_microtime_diff($wp_start_time, microtime());
221 $duration = sprintf("%0.3f", $duration);
222 $buffer .= "\n<!-- Dynamic page generated in $duration seconds. -->\n";
223
224 if( !wp_cache_writers_entry() ) {
225 $buffer .= "\n<!-- Page not cached by WP Super Cache. Could not get mutex lock. -->\n";
226 return $buffer;
227 }
228
229 //if( !((!$file_expired && $mtime) || ($mtime && $file_expired && (time() - $mtime) < 5)) ) {
230 $dir = get_current_url_supercache_dir();
231 $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
232 if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) )
233 $super_cache_enabled = false;
234
235 $tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp';
236
237 // Don't create wp-cache files for anon users
238 $supercacheonly = false;
239 if( $super_cache_enabled && wp_cache_get_cookies_values() == '' )
240 $supercacheonly = true;
241
242 if( !$supercacheonly ) {
243 if ( !@file_exists( $blog_cache_dir . $cache_filename ) || ( @file_exists( $blog_cache_dir . $cache_filename ) && ( time() - @filemtime( $blog_cache_dir . $cache_filename ) ) > 5 ) ) {
244 $fr = @fopen($tmp_wpcache_filename, 'w');
245 if (!$fr) {
246 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename . " -->\n";
247 return $buffer;
248 }
249 }
250 }
251 if( $super_cache_enabled ) {
252 $user_info = wp_cache_get_cookies_values();
253 $do_cache = apply_filters( 'do_createsupercache', $user_info );
254 if( $user_info == '' || $do_cache === true ) {
255
256 if( @is_dir( $dir ) == false )
257 @wp_mkdir_p( $dir );
258
259 $cache_fname = "{$dir}index.html";
260 $tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
261 if ( !@file_exists( $cache_fname ) || ( @file_exists( $cache_fname ) && ( time() - @filemtime( $blog_cache_dir . $cache_filename ) ) > 5 ) ) {
262 $fr2 = @fopen( $tmp_cache_filename, 'w' );
263 if (!$fr2) {
264 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . " -->\n";
265 @fclose( $fr );
266 @unlink( $tmp_wpcache_filename );
267 return $buffer;
268 } elseif( $cache_compression ) {
269 $gz = @fopen( $tmp_cache_filename . ".gz", 'w');
270 if (!$gz) {
271 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz -->\n";
272 @fclose( $fr );
273 @unlink( $tmp_wpcache_filename );
274 @fclose( $fr2 );
275 @unlink( $tmp_cache_filename );
276 return $buffer;
277 }
278 }
279 }
280 }
281 }
282
283 if (preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content
284 $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
285 "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
286 $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
287 "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
288 $store = apply_filters( 'wpsupercache_buffer', $store );
289 $wp_cache_meta[ 'dynamic' ] = true;
290 /* Clean function calls in tag */
291 $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
292 $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
293 if( $fr )
294 fputs($fr, $store);
295 if( $fr2 )
296 fputs($fr2, $store . '<!-- super cache -->' );
297 if( $gz )
298 fputs($gz, gzencode( $store . '<!-- super cache gz -->', 1, FORCE_GZIP ) );
299 } else {
300 $buffer = apply_filters( 'wpsupercache_buffer', $buffer );
301 $buffer .= "<!-- Cached page generated by WP-Super-Cache on " . gmdate('Y-m-d H:i:s', (time() + ( $wp_cache_gmt_offset * 3600))) . " -->\n";
302
303 if( $gz || $wp_cache_gzip_encoding ) {
304 $gzdata = gzencode( $buffer . "<!-- Compression = gzip -->", 3, FORCE_GZIP );
305 $gzsize = strlen($gzdata);
306 }
307 if ($wp_cache_gzip_encoding) {
308 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
309 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
310 // Return uncompressed data & store compressed for later use
311 if( $fr )
312 fputs($fr, $gzdata);
313 } else { // no compression
314 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie';
315 if( $fr )
316 fputs($fr, $buffer);
317 }
318 if( $fr2 )
319 fputs($fr2, $buffer . '<!-- super cache -->' );
320 if( $gz )
321 fwrite($gz, $gzdata );
322 $buffer .= $log;
323 }
324 $new_cache = true;
325 if( $fr ) {
326 $supercacheonly = false;
327 fclose($fr);
328 if( !rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ) ) {
329 unlink( $blog_cache_dir . $cache_filename );
330 rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename );
331 }
332 }
333 if( $fr2 ) {
334 fclose($fr2);
335 if( !@rename( $tmp_cache_filename, $cache_fname ) ) {
336 @unlink( $cache_fname );
337 @rename( $tmp_cache_filename, $cache_fname );
338 }
339 }
340 if( $gz ) {
341 fclose($gz);
342 if( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) {
343 @unlink( $cache_fname . '.gz' );
344 @rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' );
345 }
346 }
347 //}
348 wp_cache_writers_exit();
349 if ( !headers_sent() && isset( $wp_cache_gzip_first ) && 1 == $wp_cache_gzip_first && $wp_cache_gzip_encoding && $gzdata) {
350 header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
351 header( 'Vary: Accept-Encoding, Cookie' );
352 header( 'Content-Length: ' . $gzsize );
353 return $gzdata;
354 } else {
355 return $buffer;
356 }
357}
358
359function wp_cache_phase2_clean_cache($file_prefix) {
360 global $cache_path, $blog_cache_dir;
361
362 if( !wp_cache_writers_entry() )
363 return false;
364 if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
365 while ( false !== ($file = @readdir($handle))) {
366 if ( preg_match("/^$file_prefix/", $file) )
367 @unlink( $blog_cache_dir . $file );
368 }
369 closedir($handle);
370 }
371 wp_cache_writers_exit();
372}
373
374function prune_super_cache( $directory, $force = false, $rename = false ) {
375 global $cache_max_time, $cache_path, $super_cache_enabled, $cache_rebuild_files, $blog_cache_dir;
376
377 if( !is_admin() && $super_cache_enabled == 0 )
378 return false;
379
380 if( !isset( $cache_max_time ) )
381 $cache_max_time = 3600;
382
383 $now = time();
384
385 $protected_directories = array( $cache_path . '.htaccess', $cache_path . $blog_cache_dir . 'meta', $cache_path . 'supercache' );
386
387 $oktodelete = false;
388 if (is_dir($directory)) {
389 if( $dh = @opendir( $directory ) ) {
390 $directory = trailingslashit( $directory );
391 while( ( $entry = @readdir( $dh ) ) !== false ) {
392 if ($entry == '.' || $entry == '..')
393 continue;
394 $entry = $directory . $entry;
395 prune_super_cache( $entry, $force, $rename );
396 // If entry is a directory, AND it's not a protected one, AND we're either forcing the delete, OR the file is out of date,
397 if( is_dir( $entry ) && !in_array( $entry, $protected_directories ) && ( $force || @filemtime( $entry ) + $cache_max_time <= $now ) ) {
398 // if the directory isn't empty can't delete it
399 if( $handle = @opendir( $entry ) ) {
400 $donotdelete = false;
401 while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) {
402 if ($file == '.' || $file == '..')
403 continue;
404 $donotdelete = true;
405 }
406 closedir($handle);
407 }
408 if( $donotdelete )
409 continue;
410 if( !$rename )
411 @rmdir( $entry );
412 }
413 }
414 closedir($dh);
415 }
416 } elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) {
417 $oktodelete = true;
418 if( in_array( $directory, $protected_directories ) )
419 $oktodelete = false;
420 if( $oktodelete && !$rename ) {
421 @unlink( $directory );
422 } elseif( $oktodelete && $rename ) {
423 wp_cache_rebuild_or_delete( $directory );
424 }
425 }
426}
427
428function wp_cache_rebuild_or_delete( $file ) {
429 global $cache_rebuild_files;
430 if( strpos( $file, '?' ) !== false )
431 $file = substr( $file, 0, strpos( $file, '?' ) );
432 if( $cache_rebuild_files && substr( $file, -14 ) != '.needs-rebuild' ) {
433 if( @rename($file, $file . '.needs-rebuild') ) {
434 @touch( $file . '.needs-rebuild' );
435 } else {
436 @unlink( $file );
437 }
438 } else {
439 @unlink( $file );
440 }
441}
442
443function wp_cache_phase2_clean_expired($file_prefix) {
444 global $cache_path, $cache_max_time, $blog_cache_dir;
445
446 clearstatcache();
447 if( !wp_cache_writers_entry() )
448 return false;
449 $now = time();
450 if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
451 while ( false !== ($file = readdir($handle))) {
452 if ( preg_match("/^$file_prefix/", $file) &&
453 (@filemtime( $blog_cache_dir . $file) + $cache_max_time) <= $now ) {
454 @unlink( $blog_cache_dir . $file );
455 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
456 continue;
457 }
458 if($file != '.' && $file != '..') {
459 if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now ) {
460 if( substr( $file, -9 ) != '.htaccess' )
461 @unlink($blog_cache_dir . $file);
462 }
463 }
464 }
465 closedir($handle);
466 prune_super_cache( $cache_path . 'supercache' );
467 }
468
469 wp_cache_writers_exit();
470 return true;
471}
472
473function wp_cache_shutdown_callback() {
474 global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $gzsize, $cache_filename, $supercacheonly, $blog_cache_dir;
475 global $wp_cache_blog_charset, $wp_cache_request_uri;
476
477 $wp_cache_meta[ 'uri' ] = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks
478 $wp_cache_meta[ 'blog_id' ] = $blog_id;
479 $wp_cache_meta[ 'post' ] = wp_cache_post_id();
480
481 $response = wp_cache_get_response_headers();
482 foreach ($known_headers as $key) {
483 if(isset($response[$key])) {
484 $wp_cache_meta[ 'headers' ][ $key ] = "$key: " . $response[$key];
485 }
486 }
487 if (!isset( $response['Last-Modified'] )) {
488 $value = gmdate('D, d M Y H:i:s') . ' GMT';
489 /* Dont send this the first time */
490 /* @header('Last-Modified: ' . $value); */
491 $wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value";
492 }
493 if (!$response['Content-Type'] && !$response['Content-type']) {
494 // On some systems, headers set by PHP can't be fetched from
495 // the output buffer. This is a last ditch effort to set the
496 // correct Content-Type header for feeds, if we didn't see
497 // it in the response headers already. -- dougal
498 if (is_feed()) {
499 $type = get_query_var('feed');
500 $type = str_replace('/','',$type);
501 switch ($type) {
502 case 'atom':
503 $value = "application/atom+xml";
504 break;
505 case 'rdf':
506 $value = "application/rdf+xml";
507 break;
508 case 'rss':
509 case 'rss2':
510 default:
511 $value = "application/rss+xml";
512 }
513 } else { // not a feed
514 $value = get_option( 'html_type' );
515 if( $value == '' )
516 $value = 'text/html';
517 }
518 $value .= "; charset=\"" . $wp_cache_blog_charset . "\"";
519
520 @header("Content-Type: $value");
521 $wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value";
522 }
523
524 if ( ! $supercacheonly && $new_cache ) {
525 if( $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) {
526 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
527 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
528 }
529
530 $serial = serialize($wp_cache_meta);
531 if( wp_cache_writers_entry() ) {
532 $tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid( mt_rand(), true ) . '.tmp';
533 $fr = @fopen( $tmp_meta_filename, 'w');
534 if( !$fr )
535 @mkdir( $blog_cache_dir . 'meta' );
536 $fr = fopen( $tmp_meta_filename, 'w');
537 fputs($fr, $serial);
538 fclose($fr);
539 @chmod( $tmp_meta_filename, 0666 & ~umask());
540 if( !@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ) ) {
541 unlink( $blog_cache_dir . 'meta/' . $meta_file );
542 rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file );
543 }
544 wp_cache_writers_exit();
545 }
546 }
547 global $time_to_gc_cache;
548 if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 )
549 do_action( 'wp_cache_gc' );
550}
551
552function wp_cache_no_postid($id) {
553 return wp_cache_post_change(wp_cache_post_id());
554}
555
556function wp_cache_get_postid_from_comment($comment_id) {
557 global $super_cache_enabled, $wp_cache_request_uri;
558 $comment = get_comment($comment_id, ARRAY_A);
559 $postid = $comment['comment_post_ID'];
560 // Do nothing if comment is not moderated
561 // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
562 if( !preg_match('/wp-admin\//', $wp_cache_request_uri) )
563 if( $comment['comment_approved'] == 'spam' ) { // changed from 1 to "spam"
564 return $postid;
565 } elseif( $comment['comment_approved'] == '0' ) {
566 $super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
567 }
568 // We must check it up again due to WP bugs calling two different actions
569 // for delete, for example both wp_set_comment_status and delete_comment
570 // are called when deleting a comment
571 if ($postid > 0)
572 return wp_cache_post_change($postid);
573 else
574 return wp_cache_post_change(wp_cache_post_id());
575}
576
577function wp_cache_post_edit($post_id) {
578 global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
579 if( $wp_cache_clear_on_post_edit ) {
580 prune_super_cache( $blog_cache_dir, true );
581 prune_super_cache( $cache_path . 'supercache/', true );
582 } else {
583 wp_cache_post_change( $post_id );
584 }
585}
586
587function wp_cache_post_id_gc( $siteurl, $post_id ) {
588 global $cache_path;
589
590 $post_id = intval( $post_id );
591 if( $post_id == 0 )
592 return;
593
594 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
595 $dir = $cache_path . 'supercache/' . $siteurl;
596 prune_super_cache( $dir . $permalink, true, true );
597 @rmdir( $dir . $permalink );
598 prune_super_cache( $dir . 'page/', true );
599}
600
601function wp_cache_post_change($post_id) {
602 global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid;
603 static $last_processed = -1;
604
605 if ($post_id == $last_processed) return $post_id;
606 $last_processed = $post_id;
607 if( !wp_cache_writers_entry() )
608 return $post_id;
609
610 $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
611 if( $super_cache_enabled ) {
612 $siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) );
613 // make sure the front page has a rebuild file
614 prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html', true, true );
615 prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.gz', true, true );
616 wp_cache_post_id_gc( $siteurl, $post_id );
617 if( get_option( 'show_on_front' ) == 'page' ) {
618 wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ) );
619 wp_cache_post_id_gc( $siteurl, get_option( 'page_for_posts' ) );
620 }
621 }
622
623 $matches = array();
624 if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) {
625 while ( false !== ($file = readdir($handle))) {
626 if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
627 $meta_pathname = $blog_cache_dir . 'meta/' . $file;
628 $content_pathname = $blog_cache_dir . $matches[1] . ".html";
629 $meta = unserialize(@file_get_contents($meta_pathname));
630 if( false == is_array( $meta ) ) {
631 @unlink($meta_pathname);
632 @unlink($content_pathname);
633 continue;
634 }
635 if ($post_id > 0 && $meta) {
636 if ($meta[ 'blog_id' ] == $blog_id && (!$meta[ 'post' ] || $meta[ 'post' ] == $post_id) ) {
637 @unlink($meta_pathname);
638 @unlink($content_pathname);
639 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
640 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
641 }
642 } elseif ($meta[ 'blog_id' ] == $blog_id) {
643 @unlink($meta_pathname);
644 @unlink($content_pathname);
645 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
646 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
647 }
648
649 }
650 }
651 closedir($handle);
652 }
653 wp_cache_writers_exit();
654 return $post_id;
655}
656
657function wp_cache_microtime_diff($a, $b) {
658 list($a_dec, $a_sec) = explode(' ', $a);
659 list($b_dec, $b_sec) = explode(' ', $b);
660 return $b_sec - $a_sec + $b_dec - $a_dec;
661}
662
663function wp_cache_post_id() {
664 global $posts, $comment_post_ID, $post_ID;
665 // We try hard all options. More frequent first.
666 if ($post_ID > 0 ) return $post_ID;
667 if ($comment_post_ID > 0 ) return $comment_post_ID;
668 if (is_single() || is_page()) return $posts[0]->ID;
669 if (isset( $_GET[ 'p' ] ) && $_GET['p'] > 0) return $_GET['p'];
670 if (isset( $_POST[ 'p' ] ) && $_POST['p'] > 0) return $_POST['p'];
671 return 0;
672}
673
674function wp_cache_gc_cron() {
675 global $file_prefix, $cache_max_time;
676
677 if( !isset( $cache_max_time ) )
678 $cache_max_time = 600;
679
680 $start = time();
681 if( !wp_cache_phase2_clean_expired($file_prefix ) ) {
682 wp_cache_debug( 'Cache Expiry cron job failed. Probably mutex locked.' );
683 update_option( 'wpsupercache_gc_time', time() - ( $cache_max_time - 10 ) ); // if GC failed then run it again in one minute
684 }
685 if( time() - $start > 30 )
686 wp_cache_debug( "Cache Expiry cron job took more than 30 seconds to execute.\nYou should reduce the Expiry Time in the WP Super Cache admin page\nas you probably have more cache files than your server can handle efficiently." );
687}
688
689?>
Note: See TracBrowser for help on using the repository browser.