108 lines
2.8 KiB
PHP
108 lines
2.8 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 = '';
|
|
public $disable = FALSE;
|
|
|
|
private $maxTime = 2; //heures max en cache
|
|
private $CacheRefreshHours = 14; //Raffraichir les pages après xx heures
|
|
|
|
function __construct(){
|
|
$this->path = realpath(dirname(__FILE__).'/../cache/');
|
|
}
|
|
|
|
function startCapture($fileName){
|
|
global $firephp;
|
|
$fileName = str_replace('.php','',$fileName);
|
|
$this->fileName = $this->path .'/'. $fileName;
|
|
if (!$this->disable){
|
|
if(!$this->isInCache() || $this->forceStart){
|
|
$firephp->info('Le fichier n\'est pas en cache');
|
|
ob_start();
|
|
return TRUE;
|
|
}else{
|
|
$firephp->info('Le fichier est en cache');
|
|
return FALSE;
|
|
}
|
|
}else{
|
|
$this->delete(); //On supprime le fichier en cache
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
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écédement crée pour qu'il soit généré la prochaine fois
|
|
$this->delete();
|
|
}
|
|
}
|
|
|
|
function isInCache(){
|
|
global $firephp;
|
|
if(file_exists($this->fileName) && !$this->timeOver())
|
|
return TRUE;
|
|
else return FALSE;
|
|
}
|
|
|
|
function timeOver(){
|
|
global $firephp;
|
|
$dateFile = filemtime($this->fileName);
|
|
$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 ');
|
|
|
|
if( ($now>$maxTime) ||
|
|
($dateFile<$refreshHour && $now>$refreshHour) )
|
|
return TRUE;
|
|
else return FALSE;
|
|
}
|
|
|
|
function delete(){
|
|
if(file_exists($this->fileName)){
|
|
unlink($this->fileName);
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
}
|
|
?>
|