/wp-content/advanced-cache.php)
/*
Plugin Name: 1 Blog Cacher
Plugin URI: http://1blogcacher.com/
Description: This file (advanced-cache.php) shouldn't be here. Move it to the /wp-content/ directory so 1 Blog Cacher can work properly.
Version: 2.0.2
Author: Javier García
Date: 2007-09-18
Author URI: http://1blogr.com/
License: Attribution - Non Commercial - Share Alike 2.5 - http://creativecommons.org/licenses/by-nc-sa/2.5/
*/
// EDIT THESE VALUES:
define("OBC_EXPIRATION",3600);
define("OBC_CACHE_USERS",2);
define("OBC_CACHE_COMMENTERS",2);
define("OBC_CACHE_ERROR_PAGES",false);
define("OBC_CACHE_REDIRECTIONS",false);
define("OBC_AVOID_TRAILING_SLASH_DUPLICATION",false);
define("OBC_ENABLE_BROWSER_CACHE",false);
define("OBC_LOOK_FOR_DYNAMIC_CODE",true);
define("OBC_USE_CACHE_DIRECTORIES",true);
define("OBC_REJECTED_STRINGS","wp-");
define("OBC_ACCEPTED_STRINGS","wp-atom.php, wp-comments-popup.php, wp-commentsrss2.php, wp-links-opml.php, wp-locations.php, wp-rdf.php, wp-rss.php, wp-rss2.php");
define("OBC_REJECTED_USER_AGENTS","bot, ia_archive, slurp, crawl, spider");
/*
- OBC_EXPIRATION: Cache expiration: number of seconds (3600 by default = 1 hour)
- OBC_CACHE_USERS: Plugin behaviour with registered (and logged-in) users:
* 0: Use no cache
* 1: Use a single global cache
* 2: Use an individual cache for each user (2 by default)
- OBC_CACHE_COMMENTERS: Plugin behaviour with commenters: same values as with OBC_CACHE_USERS (2 by default).
- OBC_CACHE_ERROR_PAGES: Option to cache error pages (status 404): true or false (false by default).
- OBC_CACHE_REDIRECTIONS: Option to cache redirections (status 301 and 302): true or false (false by default).
- OBC_AVOID_TRAILING_SLASH_DUPLICATION: Option to omit url trailing slash ("/") in order to avoid caching the same content twice: true or false (false by default).
* IMPORTANT: Don't set this parameter to true in WordPress 2.3+ or if you are using a plugin that redirects urls with trailing slash.
- OBC_ENABLE_BROWSER_CACHE: Option to allow the visitor browser to cache contents: true or false (false by default).
- OBC_LOOK_FOR_DYNAMIC_CODE: Option to look for dynamic code (mfunc and mclude comments): true or false (true by default).
* IMPORTANT: If you have the Gzip compression enabled and you are not using dynamic code, setting OBC_LOOK_FOR_DYNAMIC_CODE to false will allow the plugin to save from having to read the code cached in plain text and so return directly the compressed content.
- OBC_USE_CACHE_DIRECTORIES: Option to save the cached files in a directory strcuture that mimics the blog urls: true or false (true by default).
- OBC_REJECTED_STRINGS: If the url contains one of those comma-separated strings, that url won't be cached (by default: "wp-").
- OBC_ACCEPTED_STRINGS: Exceptions to the previous rule (by default: "wp-atom.php, wp-comments-popup.php, wp-commentsrss2.php, wp-links-opml.php, wp-locations.php, wp-rdf.php, wp-rss.php, wp-rss2.php").
- OBC_REJECTED_USER_AGENTS: If the user-agent (name of the cient's browser, bot, etc.) contains one of those comma-separated strings, its requests won't be cached, though cached pages will be displayed if they exist and haven't expired (by default: "bot, ia_archive, slurp, crawl, spider").
// STOP EDITING BELOW THIS LINE!
*/
define("OBC_ADVANCED_CACHE",true);
IF (isset($_SERVER["HTTPS"]) && strtolower($_SERVER['HTTPS']) == "on"){
define("OBC_SCHEME","https://");
define("OBC_PORT",443);
define("OBC_SECURE_SCHEME","ssl://");
}ELSE{
define("OBC_SCHEME","http://");
define("OBC_PORT",80);
define("OBC_SECURE_SCHEME","");
};
IF (!file_exists(ABSPATH."wp-content/plugins/1blogcacher2.0.php")) RETURN;
IF (!function_exists("obc_cache_init")){
FUNCTION obc_cache_init(){
global $obc_configuration;
$obc_create_cache = true;
define("OBC_PATH",obc_clean_directory(ABSPATH)."/wp-cache");
$obc_configuration = obc_load_file(OBC_PATH."/obc_configuration");
$obc_configuration = unserialize($obc_configuration);
IF (!is_array($obc_configuration)) RETURN false;
IF (!$obc_configuration["plugin_active"] || !$obc_configuration["home"]) RETURN false;
define("OBC_SITE_URL",obc_clean_url($obc_configuration["home"]));
define("OBC_GZIP",$obc_configuration["gzip_enabled"]);
define("OBC_COOKIEHASH",md5($obc_configuration["home"]));
define("OBC_USER",$_COOKIE["wordpressuser_".OBC_COOKIEHASH]);
define("OBC_COMMENTER",$_COOKIE["comment_author_email_".OBC_COOKIEHASH]);
define("OBC_CACHE_DIRECTORIES",(OBC_USE_CACHE_DIRECTORIES && !ini_get("safe_mode")));
IF (OBC_GZIP && function_exists("gzencode")){
$accept_encoding = str_replace(" ","",strtolower($_SERVER["HTTP_ACCEPT_ENCODING"]));
$accept_encoding = explode(",",$accept_encoding);
define("OBC_ACCEPT_GZIP",in_array("gzip",$accept_encoding));
}ELSE define("OBC_ACCEPT_GZIP",false);
IF (strpos($_SERVER["HTTP_USER_AGENT"],"1 Blog Cacher") !== false) RETURN false;
IF (strtoupper($_SERVER["REQUEST_METHOD"]) != "GET") RETURN false;
IF (strlen(OBC_USER) && OBC_CACHE_USERS == 0) RETURN false;
IF (strlen(OBC_COMMENTER) && OBC_CACHE_COMMENTERS == 0) RETURN false;
IF (strlen($_COOKIE["wp-postpass_".OBC_COOKIEHASH])) RETURN false;
$user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);
$obc_rejected_user_agents = explode(",",OBC_REJECTED_USER_AGENTS);
$obc_rejected_user_agents = array_filter($obc_rejected_user_agents);
IF (count($obc_rejected_user_agents)){
FOREACH ($obc_rejected_user_agents as $x=>$rejected_user_agent){
$rejected_user_agent = trim(strtolower($rejected_user_agent));
IF (strpos($user_agent,$rejected_user_agent) !== false){
$obc_create_cache = false;
break;
};
};
};
define("OBC_CREATE_CACHE",$obc_create_cache);
obc_cache(OBC_SCHEME.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
};
};
IF (!function_exists("obc_cache")){
FUNCTION obc_cache($url){
global $obc_request_headers;
$url = obc_clean_url($url);
IF (strpos($url,"*") !== false || strpos($url,"/.") !== false || strpos($url,":") !== false) RETURN false;
$file = obc_url_to_file($url);
IF ($file === false) RETURN false;
IF (strlen(OBC_USER) && OBC_CACHE_USERS == 2) define("OBC_USER_STRING",md5(OBC_USER));
ELSEIF (!strlen(OBC_USER) && strlen(OBC_COMMENTER) && OBC_CACHE_COMMENTERS == 2) define("OBC_USER_STRING",md5(OBC_COMMENTER));
IF (!defined("OBC_USER_STRING")) define("OBC_USER_STRING",false);
IF (OBC_USER_STRING) $file .= ((OBC_CACHE_DIRECTORIES)? "/" : "-").OBC_USER_STRING;
ELSE $file .= ((OBC_CACHE_DIRECTORIES)? "/" : "-")."d41d8cd98f00b204e9800998ecf8427e";
$file_txt = OBC_PATH."/{$file}.txt";
$file_html = OBC_PATH."/{$file}.html";
$file_gzip = "{$file_html}.gz";
$return_gzip = (OBC_GZIP && function_exists("gzencode") && OBC_ACCEPT_GZIP && !headers_sent());
$cache_date = 0;
$obc_request_headers = obc_request_headers();
IF ($obc_request_headers["Cache-Control"] != "no-cache") $cache_date = @filemtime($file_txt);
IF ($cache_date > 0 && $cache_date > time()-OBC_EXPIRATION){
$response["headers"] = obc_load_file($file_txt);
IF ($response["headers"] === false) RETURN false;
$response["headers"] = explode("\r\n",$response["headers"]);
$empty_content = (!file_exists($file_html) || $cache_date - @filemtime($file_html) > 10);
IF (!$empty_content){
FOREACH ($response["headers"] as $x=>$header){
IF (strtolower(substr($header,0,6)) == "date: "){
$date = substr(strtolower($header),6);
break;
};
};
IF (!OBC_LOOK_FOR_DYNAMIC_CODE){
global $obc_request_headers;
IF ($date && $obc_request_headers["If-Modified-Since"] == $date) obc_status_304();
};
IF ($return_gzip){
$response["gzip"] = obc_load_file($file_gzip);
IF ($response["gzip"] === false) RETURN false;
};
IF (!$return_gzip || OBC_LOOK_FOR_DYNAMIC_CODE){
$response["content"] = obc_load_file($file_html);
IF ($response["content"] === false) RETURN false;
IF ($return_gzip) $response["content"] = preg_replace("'\n$'","",$response["content"]);
$response["content"] .= "\n";
};
IF (OBC_LOOK_FOR_DYNAMIC_CODE) IF ($dynamic_code = obc_dynamic_code($response["content"])){
$response["content"] = obc_replace_dynamic_code($response["content"]);
IF ($return_gzip) $response["gzip"] = gzencode($response["content"]);
};
IF (!$dynamic_code && OBC_LOOK_FOR_DYNAMIC_CODE){
global $obc_request_headers;
IF ($date && $obc_request_headers["If-Modified-Since"] == $date) obc_status_304();
};
$etag = ($return_gzip)? md5($response["gzip"]) : md5(preg_replace("'(