extranet/includes/cache.php

105 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 $idEntreprise = '';
public $siren = '';
public $page = '';
public $content = '';
public $length = '';
/*
* Construction du nom du fichier
* [page]-[idEntreprise].html
* [page]-[siren].html
*/
public $fileName = '';
public $path = '';
2009-03-05 11:24:33 +00:00
private $maxTime = 4; //4h max en cache
2009-03-04 16:46:29 +00:00
private $CacheRefreshHours = 14; //Raffraichir les pages apr<70>s 14heures
function __construct(){
$this->path = realpath(dirname(__FILE__).'/../cache/');
}
function startCapture($page, $siren, $idEntreprise = 0){
global $firephp;
2009-03-05 08:31:21 +00:00
$this->page = str_replace('.php','',$page);
2009-03-04 16:46:29 +00:00
$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();
2009-03-05 08:31:21 +00:00
$this->content.='<!-- Page fourni par le cache -->';
2009-03-04 16:46:29 +00:00
$this->create();
}
}
function isInCache(){
2009-03-05 11:24:33 +00:00
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
$today = mktime(date('G'), date('i'), date('s'), date("m") , date("d"), date("Y"));
2009-03-05 11:24:33 +00:00
$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"));
2009-03-04 16:46:29 +00:00
$dateFile = filemtime($this->fileName);
2009-03-05 11:24:33 +00:00
$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;
2009-03-04 16:46:29 +00:00
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);
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
}
}
?>