1<?php 2 /* 3 pCache - Faster renderding using data cache 4 Copyright (C) 2008 Jean-Damien POGOLOTTI 5 Version 1.1.2 last updated on 06/17/08 6 7 http://pchart.sourceforge.net 8 9 This program is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 1,2,3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. 21 22 Class initialisation : 23 pCache($CacheFolder="Cache/") 24 Cache management : 25 IsInCache($Data) 26 GetFromCache($ID,$Data) 27 WriteToCache($ID,$Data,$Picture) 28 DeleteFromCache($ID,$Data) 29 ClearCache() 30 Inner functions : 31 GetHash($ID,$Data) 32 */ 33 34 /* pCache class definition */ 35 class pCache 36 { 37 var $HashKey = ""; 38 var $CacheFolder = "cache/"; 39 40 /* Create the pCache object */ 41 function pCache($CacheFolder="cache/") 42 { 43 $this->CacheFolder = $CacheFolder; 44 } 45 46 /* This function is clearing the cache folder */ 47 function ClearCache() 48 { 49 if ($handle = opendir($this->CacheFolder)) 50 { 51 while (false !== ($file = readdir($handle))) 52 { 53 if ( $file != "." && $file != ".." ) 54 unlink($this->CacheFolder.$file); 55 } 56 closedir($handle); 57 } 58 } 59 60 /* This function is checking if we have an offline version of this chart */ 61 function IsInCache($ID,$Data,$Hash="") 62 { 63 if ( $Hash == "" ) 64 $Hash = $this->GetHash($ID,$Data); 65 66 if ( file_exists($this->CacheFolder.$Hash) ) 67 return(TRUE); 68 else 69 return(FALSE); 70 } 71 72 /* This function is making a copy of drawn chart in the cache folder */ 73 function WriteToCache($ID,$Data,$Picture) 74 { 75 $Hash = $this->GetHash($ID,$Data); 76 $FileName = $this->CacheFolder.$Hash; 77 78 imagepng($Picture->Picture,$FileName); 79 } 80 81 /* This function is removing any cached copy of this chart */ 82 function DeleteFromCache($ID,$Data) 83 { 84 $Hash = $this->GetHash($ID,$Data); 85 $FileName = $this->CacheFolder.$Hash; 86 87 if ( file_exists($FileName ) ) 88 unlink($FileName); 89 } 90 91 /* This function is retrieving the cached picture if applicable */ 92 function GetFromCache($ID,$Data) 93 { 94 $Hash = $this->GetHash($ID,$Data); 95 if ( $this->IsInCache("","",$Hash ) ) 96 { 97 $FileName = $this->CacheFolder.$Hash; 98 99 header('Content-type: image/png'); 100 @readfile($FileName); 101 exit(); 102 } 103 } 104 105 /* This function is building the graph unique hash key */ 106 function GetHash($ID,$Data) 107 { 108 $mKey = "$ID"; 109 foreach($Data as $key => $Values) 110 { 111 $tKey = ""; 112 foreach($Values as $Serie => $Value) 113 $tKey = $tKey.$Serie.$Value; 114 $mKey = $mKey.md5($tKey); 115 } 116 return(md5($mKey)); 117 } 118 } 119?>