extranet/includes/cache.php
2009-03-05 16:19:38 +00:00

92 lines
2.3 KiB
PHP

<?php
/**
* Cache - Gère le cache de la page et des différents fichiers
* @package Cache
* @author Michael RICOIS
* @copyright Scores&Decisions
*/
class Cache {
public $content = '';
public $length = '';
public $forceStart = FALSE;
public $fileName = '';
public $path = '';
private $maxTime = 6; //heures max en cache
private $CacheRefreshHours = 16; //Raffraichir les pages après xx heures
function __construct(){
$this->path = realpath(dirname(__FILE__).'/../cache/');
}
function startCapture($fileName, $forceStart = FALSE){
global $firephp;
$fileName = str_replace('.php','',$fileName);
$this->fileName = $this->path .'/'. $fileName;
$this->forceStart = $forceStart;
if(!$this->isInCache() || $this->forceStart){
$firephp->log('Pas en CACHE','CACHE');
ob_start();
return TRUE;
}else{
$firephp->log('En CACHE','CACHE');
return FALSE;
}
}
function stopCapture(){
if(!$this->isInCache() || $this->forceStart){
$this->content = ob_get_contents();
$this->length = ob_get_length();
ob_end_flush();
$this->content.='<!-- Page fourni par le cache -->';
$this->create();
}
}
function isInCache(){
if(file_exists($this->fileName) && !$this->timeOver()) return TRUE;
else return FALSE;
}
function timeOver(){
global $firephp;
$today = mktime(date('G'), date('i'), date('s'), date("m") , date("d"), date("Y"));
$todayMaxTime = mktime(date('G')+$this->maxTime, 0, 0, date("m"), date("d"), date("Y"));
$today14 = mktime($this->CacheRefreshHours, 0, 0, date("m"), date("d"), date("Y"));
$dateFile = filemtime($this->fileName);
$firephp->log($dateFile, 'CACHE : dateFile ');
$firephp->log($today, 'CACHE : today ');
$firephp->log($todayMaxTime,'CACHE : todayMaxTime');
$firephp->log($today14, 'CACHE : today14 ');
if( ($dateFile>$todayMaxTime) ||
($dateFile<$today14 && $today>$today14) )
return TRUE;
else return FALSE;
}
function delete(){}
function displayCache(){
if(file_exists($this->fileName)){
return file_get_contents($this->fileName);
}else{
return 'Erreur';
}
}
function create(){
//Retrait <script>...</script>
preg_replace('@<script[^>]*?>.*?</script>@si', '', $this->content);
if( !file_put_contents($this->fileName, $this->content))
{
//TODO : Gestion des erreurs
}
}
}
?>