From bdcca24f8d189cb0f0454f61db9d00e5292b1154 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 1 Aug 2009 02:31:49 +0100 Subject: [PATCH] static file caching --- core/util.inc.php | 38 ++++++++++++++++++++++++++++++++++++++ index.php | 5 ++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/core/util.inc.php b/core/util.inc.php index bf12be7a..d4a3a560 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -718,4 +718,42 @@ function _get_user() { return $user; } +function _start_cache() { + $_do_cache = (CACHE && !isset($_COOKIE["shm_session"]) && !isset($_COOKIE["shm_nocache"])); + if($_do_cache) { + $hash = md5($_SERVER["QUERY_STRING"]); + $ab = substr($hash, 0, 2); + $cd = substr($hash, 2, 2); + $cachename = "data/$ab/$cd/$hash"; + if(!file_exists("data/$ab/$cd/")) { + mkdir("data/$ab/$cd/", 0750, true); + } + if(file_exists($cachename)) { + $gmdate_mod = gmdate('D, d M Y H:i:s', filemtime($cachename)) . ' GMT'; + + if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { + $if_modified_since = preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]); + + if($if_modified_since == $gmdate_mod) { + header("HTTP/1.0 304 Not Modified"); + header("Content-type: text/html"); + } + } + else { + header("Content-type: text/html"); + header("Last-Modified: $gmdate_mod"); + print file_get_contents($cachename); + } + exit; + } + ob_start(); + } +} + +function _end_cache() { + $_do_cache = (CACHE && !isset($_COOKIE["shm_session"]) && !isset($_COOKIE["shm_nocache"])); + if($_do_cache) { + file_put_contents($cachename, ob_get_contents()); + } +} ?> diff --git a/index.php b/index.php index dcb5105c..41c462b5 100644 --- a/index.php +++ b/index.php @@ -52,6 +52,7 @@ // set up and purify the environment define("DEBUG", true); +define("CACHE", true); define("SCORE_VERSION", 's2hack'); define("VERSION", 'trunk'); @@ -63,7 +64,7 @@ if(!file_exists("config.php")) { require_once "core/util.inc.php"; _version_check(); _sanitise_environment(); - +_start_cache(); try { // load base files @@ -125,6 +126,8 @@ try { if($database->engine->name != "sqlite") { $database->db->CommitTrans(true); } + + _end_cache(); } catch(Exception $e) { $version = VERSION;