<?php
define('HBCACHE_STORAGE', __DIR__ . '/hb-cache');
class HB_Cache {
Public Static Function process() {
ob_start(array( __CLASS__, 'ob_callback'));
}
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;
}
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);
}
}
}
Public Static Function is_valid($file) {
if (file_exists($file)) {
$ftime = @filemtime($file);
if ($ftime && $ftime > (time() - 86400)) {
return true;
}
}
return false;
}
}
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;
}
defined('WP_CACHE')
? HB_Cache::process()
: HB_Cache::cleanup();