--- /dev/null
+<?php\r
+ /*\r
+ pCache - Faster renderding using data cache\r
+ Copyright (C) 2008 Jean-Damien POGOLOTTI\r
+ Version 1.1.2 last updated on 06/17/08\r
+\r
+ http://pchart.sourceforge.net\r
+\r
+ This program is free software: you can redistribute it and/or modify\r
+ it under the terms of the GNU General Public License as published by\r
+ the Free Software Foundation, either version 1,2,3 of the License, or\r
+ (at your option) any later version.\r
+\r
+ This program is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ GNU General Public License for more details.\r
+\r
+ You should have received a copy of the GNU General Public License\r
+ along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+\r
+ Class initialisation :\r
+ pCache($CacheFolder="Cache/")\r
+ Cache management :\r
+ IsInCache($Data)\r
+ GetFromCache($ID,$Data)\r
+ WriteToCache($ID,$Data,$Picture)\r
+ DeleteFromCache($ID,$Data)\r
+ ClearCache()\r
+ Inner functions :\r
+ GetHash($ID,$Data)\r
+ */\r
+\r
+ /* pCache class definition */\r
+ class pCache\r
+ {\r
+ var $HashKey = "";\r
+ var $CacheFolder = "Cache/";\r
+\r
+ /* Create the pCache object */\r
+ function pCache($CacheFolder="Cache/")\r
+ {\r
+ $this->CacheFolder = $CacheFolder;\r
+ }\r
+\r
+ /* This function is clearing the cache folder */\r
+ function ClearCache()\r
+ {\r
+ if ($handle = opendir($this->CacheFolder))\r
+ {\r
+ while (false !== ($file = readdir($handle)))\r
+ {\r
+ if ( $file != "." && $file != ".." )\r
+ unlink($this->CacheFolder.$file);\r
+ }\r
+ closedir($handle);\r
+ }\r
+ }\r
+\r
+ /* This function is checking if we have an offline version of this chart */\r
+ function IsInCache($ID,$Data,$Hash="")\r
+ {\r
+ if ( $Hash == "" )\r
+ $Hash = $this->GetHash($ID,$Data);\r
+\r
+ if ( file_exists($this->CacheFolder.$Hash) )\r
+ return(TRUE);\r
+ else\r
+ return(FALSE);\r
+ }\r
+\r
+ /* This function is making a copy of drawn chart in the cache folder */\r
+ function WriteToCache($ID,$Data,$Picture)\r
+ {\r
+ $Hash = $this->GetHash($ID,$Data);\r
+ $FileName = $this->CacheFolder.$Hash;\r
+\r
+ imagepng($Picture->Picture,$FileName);\r
+ }\r
+\r
+ /* This function is removing any cached copy of this chart */\r
+ function DeleteFromCache($ID,$Data)\r
+ {\r
+ $Hash = $this->GetHash($ID,$Data);\r
+ $FileName = $this->CacheFolder.$Hash;\r
+\r
+ if ( file_exists($FileName ) )\r
+ unlink($FileName);\r
+ }\r
+\r
+ /* This function is retrieving the cached picture if applicable */\r
+ function GetFromCache($ID,$Data)\r
+ {\r
+ $Hash = $this->GetHash($ID,$Data);\r
+ if ( $this->IsInCache("","",$Hash ) )\r
+ {\r
+ $FileName = $this->CacheFolder.$Hash;\r
+\r
+ header('Content-type: image/png');\r
+ @readfile($FileName);\r
+ exit();\r
+ }\r
+ }\r
+\r
+ /* This function is building the graph unique hash key */\r
+ function GetHash($ID,$Data)\r
+ {\r
+ $mKey = "$ID";\r
+ foreach($Data as $key => $Values)\r
+ {\r
+ $tKey = "";\r
+ foreach($Values as $Serie => $Value)\r
+ $tKey = $tKey.$Serie.$Value;\r
+ $mKey = $mKey.md5($tKey);\r
+ }\r
+ return(md5($mKey));\r
+ }\r
+ }\r
+?>
\ No newline at end of file