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