2004-06-09 21:42:23 +00:00
|
|
|
<?php
|
2004-04-14 20:55:37 +00:00
|
|
|
|
2004-07-22 23:42:50 +00:00
|
|
|
require_once("../project/cache_parameters.inc");
|
|
|
|
|
2004-04-14 20:55:37 +00:00
|
|
|
// mechanism for caching commonly-accessed pages
|
|
|
|
|
2004-07-22 23:42:50 +00:00
|
|
|
function make_cache_dirs() {
|
2004-08-31 23:08:28 +00:00
|
|
|
if (!@filemtime("../cache")) mkdir("../cache");
|
|
|
|
for ($i=0;$i<256;$i++) {
|
|
|
|
$j=sprintf("%02x",$i);
|
|
|
|
if (!@filemtime("../cache/$j")) {
|
|
|
|
mkdir("../cache/$j", 0700);
|
|
|
|
}
|
|
|
|
}
|
2004-07-22 23:42:50 +00:00
|
|
|
}
|
|
|
|
|
2004-06-10 19:43:54 +00:00
|
|
|
function get_path($params) {
|
2004-07-22 23:42:50 +00:00
|
|
|
if (!@filemtime("../cache/00")) make_cache_dirs();
|
2004-06-10 19:43:54 +00:00
|
|
|
$y = pathinfo($_SERVER["PHP_SELF"]);
|
|
|
|
$z = $y["basename"];
|
2004-08-31 23:08:28 +00:00
|
|
|
|
2004-07-22 23:42:50 +00:00
|
|
|
// add a layer of subdirectories for reducing file lookup time
|
|
|
|
$sz = substr(md5($z."_".urlencode($params)),1,2);
|
|
|
|
$path = "../cache/".$sz."/".$z;
|
2004-06-10 19:43:54 +00:00
|
|
|
if ($params) {
|
|
|
|
$path = $path."_".urlencode($params);
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2004-07-23 04:04:21 +00:00
|
|
|
function disk_usage($dir) {
|
2004-08-31 23:08:28 +00:00
|
|
|
$usage=0;
|
|
|
|
if ($handle=@opendir($dir)) {
|
|
|
|
while ($file=readdir($handle)) {
|
|
|
|
if (($file != ".") && ($file != "..")) {
|
|
|
|
if (@is_dir($dir."/".$file)) {
|
|
|
|
$usage+=disk_usage($dir."/".$file);
|
|
|
|
} else {
|
|
|
|
$usage+=@filesize($dir."/".$file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@closedir($handle);
|
|
|
|
}
|
|
|
|
return $usage;
|
2004-07-23 04:04:21 +00:00
|
|
|
}
|
|
|
|
|
2004-09-01 22:51:23 +00:00
|
|
|
function clean_cache($max_age, $dir) {
|
2004-09-01 23:17:33 +00:00
|
|
|
$start_dir = getcwd();
|
|
|
|
if (!chdir($dir)) {
|
|
|
|
return;
|
|
|
|
}
|
2004-09-01 22:51:23 +00:00
|
|
|
if ($handle=@opendir(".")) {
|
2004-08-31 23:08:28 +00:00
|
|
|
while ($file=readdir($handle)) {
|
2004-09-01 22:51:23 +00:00
|
|
|
if ($file == ".") continue;
|
|
|
|
if ($file == "..") continue;
|
|
|
|
|
|
|
|
// don't let hackers trick us into deleting other files!
|
2004-09-01 23:17:33 +00:00
|
|
|
if (strstr($file, "..")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (@is_dir($file)) {
|
|
|
|
clean_cache($max_age, $file);
|
2004-09-01 22:51:23 +00:00
|
|
|
} else {
|
|
|
|
if ((time()-@filemtime($file))>$max_age) {
|
2004-09-01 23:17:33 +00:00
|
|
|
//echo "unlinking ".getcwd()."/$file\n";
|
2004-09-01 22:51:23 +00:00
|
|
|
@unlink($file);
|
2004-08-31 23:08:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@closedir($handle);
|
|
|
|
}
|
2004-09-01 23:17:33 +00:00
|
|
|
chdir($start_dir);
|
2004-07-23 04:04:21 +00:00
|
|
|
}
|
|
|
|
|
2004-06-10 19:43:54 +00:00
|
|
|
function start_cache($max_age, $params=""){
|
2004-07-23 04:04:21 +00:00
|
|
|
$path = get_path($params);
|
2004-09-20 21:49:54 +00:00
|
|
|
|
2004-07-23 04:04:21 +00:00
|
|
|
// check free disk space every once in a while
|
2004-09-20 21:49:54 +00:00
|
|
|
//
|
2004-07-23 04:04:21 +00:00
|
|
|
if (!(rand() % CACHE_SIZE_CHECK_FREQ)) {
|
|
|
|
$too_old=86400;
|
|
|
|
while ((disk_free_space("../cache") < MIN_FREE_SPACE) ||
|
|
|
|
(disk_usage("../cache") > MAX_CACHE_USAGE)) {
|
2004-09-01 22:51:23 +00:00
|
|
|
clean_cache($too_old, "../cache");
|
2004-08-31 23:08:28 +00:00
|
|
|
$too_old/=2;
|
2004-07-23 04:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-20 21:49:54 +00:00
|
|
|
|
|
|
|
$regenerate = false;
|
2004-07-23 01:07:18 +00:00
|
|
|
if ($max_age) {
|
2004-08-31 23:08:28 +00:00
|
|
|
$request = getallheaders();
|
2004-09-20 21:49:54 +00:00
|
|
|
|
2004-11-01 23:42:29 +00:00
|
|
|
clearstatcache();
|
2004-08-31 23:08:28 +00:00
|
|
|
$lastmodified = @filemtime($path);
|
2004-09-20 21:49:54 +00:00
|
|
|
if ($lastmodified) {
|
2004-04-14 20:55:37 +00:00
|
|
|
|
2004-09-20 21:49:54 +00:00
|
|
|
// Check to see if this is a conditional fetch.
|
|
|
|
//
|
|
|
|
$if_modified_since = isset($request['If-Modified-Since']) ?
|
|
|
|
(explode(';',$request['If-Modified-Since'])) :
|
|
|
|
false;
|
2004-07-20 21:54:18 +00:00
|
|
|
|
2004-09-20 21:49:54 +00:00
|
|
|
if ($if_modified_since) {
|
|
|
|
$if_modified_since=strtotime($if_modified_since[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($if_modified_since && ($if_modified_since == $lastmodified)) {
|
|
|
|
Header("Last-Modified: " . gmdate("D, d M Y H:i:s",$lastmodified) . " GMT");
|
|
|
|
Header('HTTP/1.0 304 Not Modified');
|
|
|
|
exit;
|
|
|
|
}
|
2004-07-20 21:54:18 +00:00
|
|
|
|
2004-09-20 21:49:54 +00:00
|
|
|
// See if cached copy is too old.
|
|
|
|
// If so regenerate,
|
|
|
|
// and touch the cached copy so other processes
|
|
|
|
// don't regenerate at the same time
|
|
|
|
//
|
|
|
|
if ($lastmodified<time()-$max_age) {
|
|
|
|
$regenerate = true;
|
|
|
|
@touch($path);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$regenerate = true;
|
2004-08-31 23:08:28 +00:00
|
|
|
}
|
2004-07-20 21:54:18 +00:00
|
|
|
|
2004-09-20 21:49:54 +00:00
|
|
|
if ($regenerate){
|
2004-08-31 23:08:28 +00:00
|
|
|
// If cached version is too old (or non-existent)
|
|
|
|
// generate the page and write to cache
|
|
|
|
//
|
|
|
|
ob_start();
|
|
|
|
ob_implicit_flush(0);
|
|
|
|
Header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
|
|
|
|
Header("Expires: " . gmdate("D, d M Y H:i:s",time()+$max_age) . " GMT");
|
|
|
|
Header("Cache-Control: public, max-age=" . $max_age);
|
|
|
|
} else {
|
|
|
|
// Otherwise serve the cached version and exit
|
|
|
|
//
|
|
|
|
if (strstr($params, "format=xml")) {
|
|
|
|
header('Content-type: text/xml');
|
|
|
|
}
|
|
|
|
Header("Last-Modified: " . gmdate("D, d M Y H:i:s",$lastmodified) . " GMT");
|
|
|
|
Header("Expires: " . gmdate("D, d M Y H:i:s",$lastmodified+$max_age) . " GMT");
|
|
|
|
Header("Cache-Control: public, max-age=" . $max_age );
|
2004-11-01 23:42:29 +00:00
|
|
|
if (!@readfile($path)) {
|
|
|
|
echo "can't read $path; lastmod $lastmodified\n";
|
|
|
|
}
|
2004-08-31 23:08:28 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
2004-04-14 20:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write output buffer both to client and to cache
|
|
|
|
//
|
2004-07-23 01:07:18 +00:00
|
|
|
function end_cache($max_age,$params=""){
|
2004-08-31 23:08:28 +00:00
|
|
|
// for the benefit of hackers
|
|
|
|
if (strstr($params, "..")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($max_age) {
|
|
|
|
$path = get_path($params);
|
|
|
|
$fhandle=fopen($path, "w");
|
|
|
|
$page=ob_get_contents();
|
|
|
|
ob_end_flush();
|
|
|
|
fwrite($fhandle, $page);
|
|
|
|
fclose($fhandle);
|
|
|
|
}
|
2004-04-14 20:55:37 +00:00
|
|
|
}
|
|
|
|
?>
|