<?php
/*
Plugin Name: Lite Cache
Plugin URI: http://www.satollo.net/plugins/lite-cache
Description: Simple cache
Version: 1.1.1
Author: Satollo
Author URI: http://www.satollo.net
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
*/
$lite_cache = new LiteCache();
global $cache_stop;
$cache_stop = false;
class LiteCache {
var $post_id;
function __construct() {
add_action('admin_menu', array(&$this, 'hook_admin_menu'));
add_action('edit_post', array(&$this, 'hook_edit_post'), 1);
add_action('wp_update_comment_count', array(&$this, 'hook_wp_update_comment_count'), 1);
add_action('template_redirect', array(&$this, 'hook_template_redirect'), 0);
}
function hook_template_redirect() {
global $cache_stop;
if ($cache_stop) return;
if (is_user_logged_in()) return;
if (is_404()) return;
if (is_trackback()) return;
if (is_feed()) return;
if (is_robots()) return;
if (defined(SID) && SID != '') return;
$home_root = parse_url(get_option('home'), PHP_URL_PATH);
if (substr($_SERVER['REQUEST_URI'], 0, 4) == ($home_root . '/wp-')) return;
if ($_SERVER['REQUEST_METHOD'] == 'POST') return false;
if ($_SERVER['QUERY_STRING'] != '') return false;
foreach ($_COOKIE as $n=>$v ) {
if (substr($n, 0, 14) == 'comment_author') unset($_COOKIE[$n]);
}
ob_start('lc_callback');
}
function hook_admin_menu() {
add_options_page('Lite Cache', 'Lite Cache', 'manage_options', basename(dirname(__FILE__)) . '/options.php');
}
function hook_edit_post($post_id) {
if ($this->post_id == $post_id) return;
$this->post_id = $post_id;
$url = get_permalink($post_id);
$dir = ABSPATH . 'wp-content/cache/lite-cache' . substr($url, strlen(get_option('home'))) . '/';
$this->remove_dir($dir);
@unlink(ABSPATH . 'wp-content/cache/lite-cache/index.html');
@unlink(ABSPATH . 'wp-content/cache/lite-cache/index.html.gz');
$this->remove_dir(ABSPATH . 'wp-content/cache/lite-cache/page/');
$base = get_option('category_base');
if (empty($base)) $base = 'category';
$this->remove_dir(ABSPATH . 'wp-content/cache/lite-cache/' . $base . '/');
$base = get_option('tag_base');
if (empty($base)) $base = 'tag';
$this->remove_dir(ABSPATH . 'wp-content/cache/lite-cache/' . $base . '/');
$this->remove_dir(ABSPATH . 'wp-content/cache/lite-cache/type/');
$this->remove_dir(ABSPATH . 'wp-content/cache/lite-cache/' . date('Y') . '/');
}
function hook_wp_update_comment_count($post_id) {
if ($this->post_id == $post_id) return;
$this->post_id = $post_id;
$url = get_permalink($post_id);
$dir = ABSPATH . 'wp-content/cache/lite-cache' . substr($url, strlen(get_option('home'))) . '/';
$this->remove_dir($dir);
}
function remove_dir($dir) {
$files = glob($dir . '*', GLOB_MARK);
foreach ($files as &$file) {
if (substr($file, -1) == '/')
$this->remove_dir($file);
else
@unlink($file);
}
@rmdir($dir);
}
function remove_older_than($time) {
$this->_remove_older_than($time, ABSPATH . 'wp-content/cache/lite-cache/');
}
function _remove_older_than($time, $dir) {
$files = glob($dir . '*', GLOB_MARK);
foreach ($files as &$file) {
if (substr($file, -1) == '/')
$this->_clean_older_than($time, $file);
else if (@filemtime($file) < $time) @unlink($file);
}
}
}
function lc_callback($buffer) {
global $cache_stop, $lite_cache;
if ($cache_stop) return $buffer;
if (strlen($buffer) == 0) return '';
$lc_dir = ABSPATH . 'wp-content/cache/lite-cache' . $_SERVER['REQUEST_URI'];
$lc_file = $lc_dir . '/index.html';
if (!is_dir($lc_dir)) mkdir($lc_dir, 0777, true);
if (is_singular()) {
$script = '<script>';
$script .= 'function lc_get_cookie(name) {';
$script .= 'var c = document.cookie;';
$script .= 'if (c.indexOf(name) != -1) {';
$script .= 'var x = c.indexOf(name)+name.length+1;';
$script .= 'var y = c.indexOf(";",x);';
$script .= 'if (y < 0) y = c.length;';
$script .= 'return unescape(c.substring(x,y));';
$script .= '} else return "";}';
$script .= 'if ((d = document.getElementById("commentform")) != null) { e = d.elements;';
$script .= 'e["email"].value=lc_get_cookie("comment_author_email_'. COOKIEHASH . '");';
$script .= 'e["author"].value=lc_get_cookie("comment_author_'. COOKIEHASH . '");';
$script .= 'e["url"].value=lc_get_cookie("comment_author_url_'. COOKIEHASH . '");';
$script .= '}';
$script .= '</script>';
}
// if (class_exists('tidy')) {
// $tidy = new tidy();
// $tidy->parseString($buffer, array('wrap'=>200), 'utf8');
// $tidy->cleanRepair();
// $buffer = $tidy;
// }
$buffer = apply_filters('cache_buffer', $buffer);
file_put_contents($lc_file, $buffer . $script . '<!-- lite cache ' . date('Y-m-d h:i:s') . ' -->');
$gzf = gzopen($lc_file . '.gz', 'wb9');
gzwrite($gzf, $buffer . $script . '<!-- lite cache ' . date('Y-m-d h:i:s') . ' -->');
gzclose($gzf);
return $buffer . $script;
}