$cache_path) needs to be writable for this plugin to work. Double-check it."); endif; if ( '/' != substr($cache_path, -1)) { $cache_path .= '/'; } $cache_filename = ''; $mutex_filename = 'wp-cache-mutex.lock'; $meta_file = ''; $StaticizeStart = microtime(); class CacheMeta { var $dynamic = false; var $headers = array(); } wp_cache_init(); function wp_cache_init() { global $cache_path, $cache_filename, $meta_file, $StaticizeStart, $acceptableFiles; global $cache_max_time, $file_prefix, $file_expired; $key = md5($_SERVER['REQUEST_URI'] . join($_COOKIE, ',')); $cache_filename = $file_prefix . $key . '.html'; $meta_file = $file_prefix . $key . '.meta'; $cache_file = $cache_path . $cache_filename; if( is_file($cache_file) ) { if (filesize($cache_file) == 0) return; $cache_rnd = $cache_max_time/8; $mtime = filemtime($cache_file); $cache_time_rnd = $cache_rnd/2 - rand(0, $cache_rnd); // To avoid thrashing when loaded if ($mtime + $cache_max_time > time() + $cache_time_rnd) { $meta = new CacheMeta; $meta = unserialize(file_get_contents($cache_path . $meta_file)); foreach ($meta->headers as $header) { header($header); } $duration = microtime_diff( $StaticizeStart, microtime() ); $duration = sprintf("%0.3f", $duration); $log = "\n\n"; if ($meta->dynamic) include ($cache_file); else { $content_size = filesize($cache_file) + strlen($log); header("Content-Size: $content_size"); readfile ($cache_file); } echo $log; die; } $file_expired = true; // To signal this file was expired // It should delete a "couple" (4 for now) of files later } } function wp_cache_postload() { global $cache_filename, $acceptableFiles; // We are going to need the mutex mutex_init(); if(function_exists('add_action')) { add_action('publish_post', 'postChange', 0); add_action('edit_post', 'postChange', 0); add_action('delete_post', 'postChange', 0); add_action('comment_post', 'postChange', 0); add_action('trackback_post', 'postChange', 0); add_action('pingback_post', 'postChange', 0); add_action('edit_comment', 'postChange', 0); add_action('delete_comment', 'postChange', 0); add_action('template_save', 'postChange', 0); } // Only if it's cacheable //$script = basename($_SERVER['SCRIPT_NAME']); $script = basename($_SERVER['PHP_SELF']); if( $_SERVER["REQUEST_METHOD"] == 'POST' || (!in_array($script, $acceptableFiles) && strstr($_SERVER['PHP_SELF'], 'wp-')) ) return; ob_start('StaticizeCallback'); register_shutdown_function('StaticizeEnd'); } function microtime_diff($a, $b) { list($a_dec, $a_sec) = explode(' ', $a); list($b_dec, $b_sec) = explode(' ', $b); return $b_sec - $a_sec + $b_dec - $a_dec; } function mutex_init() { global $use_flock, $mutex, $cache_path, $mutex_filename, $sem_id; if(!is_bool($use_flock)) { if(function_exists('sem_get')) $use_flock = false; else $use_flock = true; } if ($use_flock) $mutex = fopen($cache_path . $mutex_filename, 'w'); else $mutex = sem_get($sem_id, 1, 0644 | IPC_CREAT, 1); } function writers_entry() { global $use_flock, $mutex, $cache_path, $mutex_filename; if ($use_flock) flock($mutex, LOCK_EX); else sem_acquire($mutex); } function writers_exit() { global $use_flock, $mutex, $cache_path, $mutex_filename; if ($use_flock) flock($mutex, LOCK_UN); else sem_release($mutex); } function StaticizeCallback($buffer) { global $cache_path, $cache_filename, $meta_file, $StaticizeStart; global $new_cache, $meta_object; if (strlen($buffer) == 0 ) { return $buffer; } $meta_object = new CacheMeta; $duration = microtime_diff($StaticizeStart, microtime()); $duration = sprintf("%0.3f", $duration); $buffer .= "\n"; writers_entry(); $fr = fopen($cache_path . $cache_filename, 'w'); if (!$fr) $buffer = "Couldn't write to: " . $cache_path . $cache_filename . "\n"; if (ereg("(.*?)|s', '', $buffer); $meta_object->dynamic = true; $buffer = $store; } fputs($fr, $buffer); $new_cache = true; fclose($fr); writers_exit(); return $buffer; } function StaticizeClean() { global $cache_path, $file_prefix; writers_entry(); if ( ($handle = opendir( $cache_path )) ) { while ( false !== ($file = readdir($handle))) { if ( preg_match("/^$file_prefix/", $file) ) { unlink($cache_path . $file); } } closedir($handle); } writers_exit(); } function StaticizeEnd() { global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache, $known_headers; global $meta_object; ob_end_clean(); if ($new_cache) { /* Preparing... with PHP5 is straightforward, use headers_list() */ if(function_exists('apache_response_headers')) { $response = apache_response_headers(); foreach ($known_headers as $key) { if(isset($response{$key})) { array_push($meta_object->headers, "$key: " . $response{$key}); } } } $serial = serialize($meta_object); writers_entry(); $fr = fopen($cache_path . $meta_file, 'w'); fputs($fr, $serial); fclose($fr); writers_exit(); } if ($file_expired == false) { return; } // we delete some files because our was expired // Just to keep clean the cache flush(); //Assure we send data to the client $deleted = 0; writers_entry(); if ( ($handle = opendir( $cache_path )) ) { while ( $deleted < 4 && false !== ($file = readdir($handle))) { if ( preg_match("/^$file_prefix/", $file) && (filemtime($cache_path . $file) + $cache_max_time) < time() ) { //syslog(LOG_NOTICE, "Deleting $file"); unlink($cache_path . $file); $deleted++; } } closedir($handle); } writers_exit(); } function postChange($id) { StaticizeClean(); //Delete all return $id; } ?>