2004-06-09 21:42:23 +00:00
|
|
|
<?php
|
2008-08-05 22:43:14 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2008 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2004-04-14 20:55:37 +00:00
|
|
|
|
2004-07-22 23:42:50 +00:00
|
|
|
require_once("../project/cache_parameters.inc");
|
|
|
|
|
2008-04-24 16:16:36 +00:00
|
|
|
// If we can't see request headers, don't do caching
|
|
|
|
//
|
|
|
|
$no_cache = false;
|
|
|
|
if (!function_exists("apache_request_headers")) {
|
2005-08-30 15:01:28 +00:00
|
|
|
$no_cache = true;
|
2007-06-15 23:54:25 +00:00
|
|
|
}
|
2005-08-30 15:01:28 +00:00
|
|
|
|
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() {
|
2007-12-21 21:01:13 +00:00
|
|
|
if (!@filemtime("../cache")) {
|
|
|
|
mkdir("../cache", 0770);
|
|
|
|
chmod("../cache", 0770);
|
|
|
|
}
|
2004-08-31 23:08:28 +00:00
|
|
|
for ($i=0;$i<256;$i++) {
|
|
|
|
$j=sprintf("%02x",$i);
|
|
|
|
if (!@filemtime("../cache/$j")) {
|
2007-05-01 21:21:13 +00:00
|
|
|
mkdir("../cache/$j", 0770);
|
2007-12-21 21:01:13 +00:00
|
|
|
chmod("../cache/$j", 0770);
|
2004-08-31 23:08:28 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-22 23:42:50 +00:00
|
|
|
}
|
|
|
|
|
2009-12-25 04:23:48 +00:00
|
|
|
function get_path($params, $phpfile=null) {
|
2004-07-22 23:42:50 +00:00
|
|
|
if (!@filemtime("../cache/00")) make_cache_dirs();
|
2009-12-25 04:23:48 +00:00
|
|
|
if ($phpfile) {
|
|
|
|
$z = $phpfile;
|
|
|
|
} else {
|
|
|
|
$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-09-20 21:49:54 +00:00
|
|
|
|
2005-06-23 20:37:43 +00:00
|
|
|
// check free disk space every once in a while
|
|
|
|
function cache_check_diskspace(){
|
2004-07-23 04:04:21 +00:00
|
|
|
if (!(rand() % CACHE_SIZE_CHECK_FREQ)) {
|
2007-01-11 17:39:23 +00:00
|
|
|
set_time_limit(0); // this may take a while
|
|
|
|
$max_age = 86400;
|
|
|
|
while ((disk_free_space("../cache") < MIN_FREE_SPACE) ||
|
|
|
|
(disk_usage("../cache") > MAX_CACHE_USAGE)
|
2006-12-29 03:29:13 +00:00
|
|
|
) {
|
2007-01-11 17:39:23 +00:00
|
|
|
clean_cache($max_age, "../cache");
|
|
|
|
$max_age/=2;
|
|
|
|
}
|
2004-07-23 04:04:21 +00:00
|
|
|
}
|
2005-06-23 20:37:43 +00:00
|
|
|
}
|
2004-09-20 21:49:54 +00:00
|
|
|
|
2005-06-23 20:37:43 +00:00
|
|
|
function cache_need_to_regenerate($path, $max_age){
|
2004-09-20 21:49:54 +00:00
|
|
|
$regenerate = false;
|
2006-12-29 03:29:13 +00:00
|
|
|
$request = apache_request_headers();
|
2004-09-20 21:49:54 +00:00
|
|
|
|
2006-12-29 03:29:13 +00:00
|
|
|
clearstatcache();
|
|
|
|
$lastmodified = @filemtime($path);
|
|
|
|
if ($lastmodified) {
|
2004-04-14 20:55:37 +00:00
|
|
|
|
2006-12-29 03:29:13 +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
|
|
|
|
2006-12-29 03:29:13 +00:00
|
|
|
if ($if_modified_since) {
|
|
|
|
$if_modified_since=strtotime($if_modified_since[0]);
|
|
|
|
}
|
2004-09-20 21:49:54 +00:00
|
|
|
|
2006-12-29 03:29:13 +00:00
|
|
|
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
|
|
|
|
2006-12-29 03:29:13 +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) {
|
2004-09-20 21:49:54 +00:00
|
|
|
$regenerate = true;
|
2006-12-29 03:29:13 +00:00
|
|
|
@touch($path);
|
2004-08-31 23:08:28 +00:00
|
|
|
}
|
2006-12-29 03:29:13 +00:00
|
|
|
} else {
|
|
|
|
$regenerate = true;
|
|
|
|
}
|
2005-06-23 20:37:43 +00:00
|
|
|
return $regenerate;
|
|
|
|
}
|
2004-07-20 21:54:18 +00:00
|
|
|
|
2005-06-23 20:37:43 +00:00
|
|
|
// Returns cached data or false if nothing was found
|
2006-12-29 03:29:13 +00:00
|
|
|
function get_cached_data($max_age, $params=""){
|
2011-02-01 04:43:19 +00:00
|
|
|
global $no_cache;
|
|
|
|
|
|
|
|
if ($no_cache) return;
|
|
|
|
|
2005-06-23 20:37:43 +00:00
|
|
|
$path = get_path($params);
|
|
|
|
if ($max_age) {
|
2011-03-12 15:36:58 +00:00
|
|
|
if (defined('MEMCACHE_SERVERS')) {
|
|
|
|
$cache = BoincMemcache::get()->get($path);
|
2011-03-13 20:05:25 +00:00
|
|
|
if ($cache['content']) {
|
|
|
|
return $cache['content'];
|
|
|
|
} else {
|
|
|
|
return $cache;
|
2011-03-12 15:36:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cache_check_diskspace();
|
|
|
|
$regenerate=cache_need_to_regenerate($path, $max_age);
|
|
|
|
if (!$regenerate) {
|
|
|
|
return file_get_contents($path);
|
|
|
|
}
|
2006-12-29 03:29:13 +00:00
|
|
|
}
|
2005-06-23 20:37:43 +00:00
|
|
|
}
|
|
|
|
return false; //No data was cached, just return
|
|
|
|
}
|
|
|
|
|
2011-03-13 20:05:25 +00:00
|
|
|
// DEPRECATED
|
2005-06-23 20:37:43 +00:00
|
|
|
function start_cache($max_age, $params=""){
|
2011-03-12 15:36:58 +00:00
|
|
|
global $no_cache, $caching, $memcache;
|
2008-04-24 16:16:36 +00:00
|
|
|
|
2005-08-30 15:01:28 +00:00
|
|
|
if ($no_cache) return;
|
2008-04-24 16:16:36 +00:00
|
|
|
$caching = true;
|
2005-08-30 15:01:28 +00:00
|
|
|
|
2005-06-23 20:37:43 +00:00
|
|
|
if ($max_age) {
|
2011-03-12 15:36:58 +00:00
|
|
|
$path = get_path($params);
|
|
|
|
if (defined('MEMCACHE_SERVERS')) {
|
|
|
|
$cache = BoincMemcache::get()->get($path);
|
|
|
|
if ($cache) {
|
|
|
|
$regenerate = false;
|
|
|
|
$lastmodified = abs($cache->timestamp);
|
|
|
|
} else {
|
|
|
|
$regenerate = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$lastmodified = @filemtime($path);
|
|
|
|
cache_check_diskspace(); //Check free disk space once in a while
|
|
|
|
$regenerate = cache_need_to_regenerate($path, $max_age);
|
|
|
|
}
|
2007-06-15 23:54:25 +00:00
|
|
|
//Is the stored version too old, do we need to regenerate it?
|
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);
|
2004-12-28 05:25:44 +00:00
|
|
|
|
|
|
|
// allow the calling page to see cache period
|
|
|
|
//
|
|
|
|
global $cached_max_age;
|
|
|
|
$cached_max_age = $max_age;
|
2004-08-31 23:08:28 +00:00
|
|
|
} 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 );
|
2011-03-12 15:36:58 +00:00
|
|
|
if ($cache->content) {
|
|
|
|
echo $cache->content;
|
|
|
|
exit;
|
|
|
|
}
|
2004-11-01 23:42:29 +00:00
|
|
|
if (!@readfile($path)) {
|
2006-06-16 23:53:56 +00:00
|
|
|
//echo "can't read $path; lastmod $lastmodified\n";
|
2006-12-29 03:29:13 +00:00
|
|
|
@unlink($path);
|
|
|
|
//Proceed to regenerate content
|
2006-06-16 23:53:56 +00:00
|
|
|
} else {
|
2006-12-29 03:29:13 +00:00
|
|
|
exit;
|
|
|
|
}
|
2004-08-31 23:08:28 +00:00
|
|
|
}
|
|
|
|
}
|
2004-04-14 20:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write output buffer both to client and to cache
|
2011-03-13 20:05:25 +00:00
|
|
|
// DEPRECATED
|
2004-07-23 01:07:18 +00:00
|
|
|
function end_cache($max_age,$params=""){
|
2005-09-26 19:52:01 +00:00
|
|
|
global $no_cache;
|
2005-08-30 15:01:28 +00:00
|
|
|
if ($no_cache) return;
|
|
|
|
|
2004-08-31 23:08:28 +00:00
|
|
|
// for the benefit of hackers
|
|
|
|
if (strstr($params, "..")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($max_age) {
|
|
|
|
$path = get_path($params);
|
2011-03-12 15:36:58 +00:00
|
|
|
|
|
|
|
if (defined('MEMCACHE_SERVERS')) {
|
|
|
|
$cache = array('content' => ob_get_contents(), 'timestamp' => time());
|
|
|
|
ob_end_flush();
|
|
|
|
$cache = BoincMemcache::get()->set($path, $cache, $max_age);
|
|
|
|
} else {
|
|
|
|
$fhandle=fopen($path, "w");
|
|
|
|
$page=ob_get_contents();
|
|
|
|
ob_end_flush();
|
|
|
|
fwrite($fhandle, $page);
|
|
|
|
fclose($fhandle);
|
|
|
|
}
|
2004-08-31 23:08:28 +00:00
|
|
|
}
|
2004-04-14 20:55:37 +00:00
|
|
|
}
|
2005-06-23 20:37:43 +00:00
|
|
|
|
2011-03-12 15:36:58 +00:00
|
|
|
function set_cached_data($max_age, $data, $params=""){
|
2005-06-23 20:37:43 +00:00
|
|
|
// for the benefit of hackers
|
|
|
|
if (strstr($params, "..")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$path = get_path($params);
|
2011-03-12 15:36:58 +00:00
|
|
|
if (defined('MEMCACHE_SERVERS')) {
|
|
|
|
$cache = array('content' => $data, 'timestamp' => time());
|
2011-03-13 20:05:25 +00:00
|
|
|
BoincMemcache::get()->set($path, $cache, $max_age);
|
2011-03-12 15:36:58 +00:00
|
|
|
} else {
|
|
|
|
$fhandle = fopen($path, "w");
|
|
|
|
fwrite($fhandle, $data);
|
|
|
|
fclose($fhandle);
|
|
|
|
}
|
2009-12-25 04:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clear_cache_entry($phpfile, $params) {
|
|
|
|
if (strstr($phpfile, "..")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (strstr($params, "..")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$path = get_path($params, $phpfile);
|
2011-09-09 16:05:46 +00:00
|
|
|
@unlink($path);
|
2009-12-25 04:23:48 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 15:36:58 +00:00
|
|
|
// Memcached class
|
|
|
|
class BoincMemcache {
|
|
|
|
static $instance;
|
|
|
|
|
|
|
|
static function get() {
|
|
|
|
self::$instance = new Memcached;
|
|
|
|
if (defined('MEMCACHE_PREFIX')) {
|
|
|
|
self::$instance->setOption(Memcached::OPT_PREFIX_KEY, MEMCACHE_PREFIX);
|
|
|
|
}
|
|
|
|
$servers = explode('|', MEMCACHE_SERVERS);
|
|
|
|
foreach($servers as &$server) {
|
|
|
|
list($ip, $port) = explode(':', $server);
|
|
|
|
if (!$port) { $port = 11211; }
|
|
|
|
$server = array($ip, $port);
|
|
|
|
}
|
|
|
|
self::$instance->addServers($servers);
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-25 04:23:48 +00:00
|
|
|
?>
|