<?php
/** Custom-made funky-caching for HB!: save it as wp-content/advanced-cache.php */
/** Where all the static pages will be stored, each site in a sub-folder */
define('HBCACHE_STORAGE', __DIR__ . '/hb-cache');
/** The magic happens here */
class HB_Cache {
/**
* Start output buffering
*/
Public Static Function process() {
ob_start(array( __CLASS__, 'ob_callback'));
}
/**
* Output buffering callback
*
* @param string $buffer
* @return string
*/
Public Static Function ob_callback(&$buffer) {
if ($buffer != '' ) {
$file = HBCACHE_STORAGE . '/'
. $_SERVER['HTTP_HOST']
. $_SERVER['REQUEST_URI']
. 'hb.html';
mkdir(dirname($file), 0755, true);
file_put_contents($file, $buffer
. '<!-- HB! Cache ' . date('r') . ' -->');
}
return $buffer;
}
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
/**
* Disk cache cleanup
*
* @param string $path
* @param boolean $remove
*/
Public Static Function cleanup($path = HBCACHE_STORAGE, $remove = false) {
if ($dir = @opendir($path)) {
while (($entry = @readdir($dir)) !== false) {
if ($entry != '.' && $entry != '..') {
$full_path = $path . '/' . $entry;
if (@is_dir($full_path)) {
self::cleanup($full_path, true);
} else
if (!self::is_valid($full_path)) {
echo $full_path, "\r\n";
@unlink($full_path);
}
}
}
@closedir($dir);
if ($remove) {
echo $path, "\r\n";
@rmdir($path);
}
}
}
/**
* Check if a file is due to be garbage-collected
*
* @param string $file
* @return boolean
*/
Public Static Function is_valid($file) {
if (file_exists($file)) {
$ftime = @filemtime($file);
/* cache the pages for 24hrs */
if ($ftime && $ftime > (time() - 86400)) {
return true;
}
}
return false;
}
////-end-of-class--
}
/////////////////////////////////////////////////////////////////////////////
/**
* Skip caching for some pages
*/
switch (true) {
case defined('DOING_AJAX'):
case defined('DOING_CRON'):
case defined('APP_REQUEST'):
case defined('XMLRPC_REQUEST'):
case defined('WP_ADMIN'):
case (defined('SHORTINIT') && SHORTINIT):
return;
}
/**
* If included from WordPress will process the buffered output, otherwise
* if called from console\cron will execute the garbage-collection
*/
defined('WP_CACHE')
? HB_Cache::process()
: HB_Cache::cleanup();
/////////////////////////////////////////////////////////////////////////////
/* Include this in your nginx conf:
location / {
# file exist, push it
if (-f $request_filename) {
break;
}
set $hb_file '';
# compose the path to the static file form the cache
if ($request_uri ~ ^.+$) {
set $hb_file /wp-content/hb-cache/$http_host/$request_uri/hb.html;
}
if ($request_method = POST) {
set $hb_file '';
}
# query string found, do not push then
if ($query_string) {
set $hb_file '';
}
# admin, comment troll or whetever -- do not push either
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
set $hb_file '';
}
# only rewrite to the static cache file if it actually exists
if (-f $document_root$hb_file) {
rewrite ^(.*)$ $hb_file break;
}
# all other requests go to WordPress
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
*/