//##########################################################################
//#################### FUNZIONI CACHE ###################################
//##########################################################################
function cache_id($INRI) {
$varurl = serialize($_GET).serialize($_POST);
$id_univoco = abs(crc32(md5($_SERVER['PHP_SELF'].$INRI.$varurl)));
return $id_univoco;
}
function cache_out($timeout) {
global $CONF,$INRI;
$cachedir = $CONF[cache_dir];
$path = $cachedir."/".cache_id($INRI);
$timeout = "3";
if(!file_exists($CONF[cache_dir])) mkdir($CONF[cache_dir]);
//CONTROLLO 304
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
$mtime = filemtime($file);
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
if ($if_modified_since == $gmdate_mod) {
header("HTTP/1.0 304 Not Modified");
exit;
}
header("Last-Modified: $gmdate_mod");
//controllo se il file di cache c'e' e non e' scaduto
if(file_exists($path) and filemtime($path) + $timeout > time()) {
$size = filesize($path);
//########OUTPUT
header("Content-Length: $size");
print "
CACHEEEEEEEEEE
";
$result = readfile($path);
//scrivo_statistiche();
exit(); //E FINISCE QUI
}
}
function cache_out2($timeout) {
global $CONF,$INRI;
$cachedir = $CONF[cache_dir];
$path = $cachedir."/".cache_id($INRI);
$timeout = "3";
if(!file_exists($CONF[cache_dir])) mkdir($CONF[cache_dir]);
if(!file_exists($path)) {
//print "creo file";
//se non c'e' il file di cache proseguo
return 0;
}
//CONTROLLO 304
//print "a".$_SERVER['HTTP_IF_MODIFIED_SINCE'];
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
$mtime = filemtime($path);
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
//controllo se il file di cache c'e' e non e' scaduto
if($if_modified_since == $gmdate_mod) {
//$size = filesize($path);
//########OUTPUT
//header("Content-Length: $size");
//print "aaa";
header("HTTP/1.0 304 Not Modified");
flush();
//exit;
//print "CACHEEEEEEEEEE
";
//$result = readfile($path);
//scrivo_statistiche();
//exit(); //E FINISCE QUI
} else {
header("Last-Modified: $gmdate_mod");
readfile($path);
flush();
}
}
//scrivo il file in cache sempre
function cache_in($output) {
global $CONF,$DATI,$INRI;
if(!file_exists($CONF[cache_dir])) mkdir($CONF[cache_dir]);
$file = $DATI[cache_dir]."/".cache_id($INRI);
//controllo se l' md5 che ho in memoria e' lo stesso del file che devo sovrascrivere
//se chiaramente c'e' un file con lo stesso nome e md5
//controolo esistenza file
if(is_file($file)) {
$md5_new = md5(file_get_contents($file));
$md5_old = md5($output);
if($md5_new == $md5_old) {
//print "UGUALE";
return 0;
}
}
//scrivo il file
$fp = fopen($file, "w");
fwrite($fp, $output, strlen($output));
fclose($fp);
//leggo la data
$mtime = filemtime($file);
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
header("Last-Modified: $gmdate_mod");
//print "impostato last-mod $gmdate_mod";
//mando in out quello che ho
//print "SCRIVO";
print $output;
flush();
}
function cache_eietto($buffer) {
$size=strlen($buffer);
//calcolo la data di ultima modifica
setlocale(LC_TIME, "C");
clearstatcache();
// $ft = filemtime ('referencefile');
//metto tempo arbitrartio
$ft = mktime(0, 0, 0, 12, 32, 2004);
$localt = mktime ();
$gmtt = gmmktime ();
$ft = $ft - $gmtt + $localt;
$modified = strftime ("%a, %d %b %Y %T GMT", $ft);
//mando gli header giusti al posto del server web
//header("Last-modified: Wed, 25 Apr 2001 17:17:08 GMT");
//header("HTTP/1.1 304 Not Modified");
//exit();
//header("Content-Length: $size");
//header("Last-Modified: $modified");
//d(apache_request_headers());
print $buffer;
}
?>