105 lines
2.6 KiB
PHP
105 lines
2.6 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 $idEntreprise = '';
|
|
public $siren = '';
|
|
public $page = '';
|
|
public $content = '';
|
|
public $length = '';
|
|
|
|
/*
|
|
* Construction du nom du fichier
|
|
* [page]-[idEntreprise].html
|
|
* [page]-[siren].html
|
|
*/
|
|
public $fileName = '';
|
|
public $path = '';
|
|
|
|
private $maxTime = 4; //4h max en cache
|
|
private $CacheRefreshHours = 14; //Raffraichir les pages après 14heures
|
|
|
|
function __construct(){
|
|
$this->path = realpath(dirname(__FILE__).'/../cache/');
|
|
}
|
|
|
|
function startCapture($page, $siren, $idEntreprise = 0){
|
|
global $firephp;
|
|
$this->page = str_replace('.php','',$page);
|
|
$this->idEntreprise = $idEntreprise;
|
|
$this->siren = $siren;
|
|
if ($this->siren==0){
|
|
$this->fileName = $this->path.'/'.$this->page.'-'.$this->idEntreprise.'.html';
|
|
}else{
|
|
$this->fileName = $this->path.'/'.$this->page.'-'.$this->siren.'.html';
|
|
}
|
|
if(!$this->isInCache()){
|
|
$firephp->log('Pas en CACHE','CACHE');
|
|
ob_start();
|
|
return TRUE;
|
|
}else{
|
|
$firephp->log('En CACHE','CACHE');
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
function stopCapture(){
|
|
if(!$this->isInCache()){
|
|
$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
|
|
}
|
|
}
|
|
|
|
}
|
|
?>
|