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