extranet/includes/cache.php

103 lines
2.6 KiB
PHP
Raw Normal View History

2009-03-04 16:46:29 +00:00
<?php
/**
* Cache - G<EFBFBD>re le cache de la page et des diff<EFBFBD>rents fichiers
* @package Cache
* @author Michael RICOIS
* @copyright Scores&Decisions
*/
class Cache {
public $content = '';
public $length = '';
2009-03-05 16:19:38 +00:00
public $forceStart = FALSE;
2009-03-04 16:46:29 +00:00
public $fileName = '';
public $path = '';
public $disable = FALSE;
2009-03-04 16:46:29 +00:00
2009-03-05 16:19:38 +00:00
private $maxTime = 6; //heures max en cache
private $CacheRefreshHours = 16; //Raffraichir les pages apr<70>s xx heures
2009-03-04 16:46:29 +00:00
function __construct(){
$this->path = realpath(dirname(__FILE__).'/../cache/');
}
function startCapture($fileName){
2009-03-04 16:46:29 +00:00
global $firephp;
2009-03-05 16:19:38 +00:00
$fileName = str_replace('.php','',$fileName);
$this->fileName = $this->path .'/'. $fileName;
if(!$this->isInCache() || $this->forceStart){
2009-03-04 16:46:29 +00:00
$firephp->log('Pas en CACHE','CACHE');
ob_start();
return TRUE;
}else{
$firephp->log('En CACHE','CACHE');
return FALSE;
}
2009-03-04 16:46:29 +00:00
}
function stopCapture(){
if (!$this->disable){
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();
}
}else{
//On supprime les fichiers pr<70>c<EFBFBD>dement cr<63>e pour qu'il soit g<>n<EFBFBD>r<EFBFBD> la prochaine fois
$this->delete();
2009-03-04 16:46:29 +00:00
}
}
function isInCache(){
2009-03-06 16:29:16 +00:00
global $firephp;
if(file_exists($this->fileName) && !$this->timeOver())
return TRUE;
2009-03-04 16:46:29 +00:00
else return FALSE;
}
function timeOver(){
2009-03-05 11:24:33 +00:00
global $firephp;
2009-03-04 16:46:29 +00:00
$dateFile = filemtime($this->fileName);
2009-03-06 16:29:16 +00:00
$now= mktime(date('G'), date('i'), date('s'), date("m") , date("d"), date("Y"));
$maxTime = mktime(date('G',$dateFile)+$this->maxTime, date('i',$dateFile), date('s',$dateFile), date("m",$dateFile), date("d",$dateFile), date("Y",$dateFile));
$refreshHour = mktime($this->CacheRefreshHours, 0, 0, date("m"), date("d"), date("Y"));
$firephp->log(date('G:i:s - d/m/Y',$dateFile), 'CACHE : dateFile ');
$firephp->log(date('G:i:s - d/m/Y',$now), 'CACHE : now ');
$firephp->log(date('G:i:s - d/m/Y',$maxTime), 'CACHE : MaxTime');
$firephp->log(date('G:i:s - d/m/Y',$refreshHour), 'CACHE : RefreshHour ');
2009-03-05 11:24:33 +00:00
2009-03-06 16:29:16 +00:00
if( ($now>$maxTime) ||
($dateFile<$refreshHour && $now>$refreshHour) )
2009-03-05 11:24:33 +00:00
return TRUE;
2009-03-04 16:46:29 +00:00
else return FALSE;
}
function delete(){
if(file_exists($this->fileName)){
unlink($this->fileName);
}
}
2009-03-04 16:46:29 +00:00
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);
2009-03-05 08:31:21 +00:00
if( !file_put_contents($this->fileName, $this->content))
{
//TODO : Gestion des erreurs
}
2009-03-04 16:46:29 +00:00
}
}
?>