summaryrefslogtreecommitdiffstats
path: root/admin/survey/pChart/classes/class.pCache.php
blob: 419dc5adcb86da8dd546a80d377de218da6c3700 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
 /*
     pCache - Faster renderding using data cache
     Copyright (C) 2008 Jean-Damien POGOLOTTI
     Version  1.1.2 last updated on 06/17/08

     http://pchart.sourceforge.net

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation, either version 1,2,3 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     Class initialisation :
      pCache($CacheFolder="Cache/")
     Cache management :
      IsInCache($Data)
      GetFromCache($ID,$Data)
      WriteToCache($ID,$Data,$Picture)
      DeleteFromCache($ID,$Data)
      ClearCache()
     Inner functions :
      GetHash($ID,$Data)
 */

 /* pCache class definition */
 class pCache
  {
   var $HashKey     = "";
   var $CacheFolder = "Cache/";

   /* Create the pCache object */
   function __construct($CacheFolder="Cache/")
    {
     $this->CacheFolder = $CacheFolder;
    }

   /* This function is clearing the cache folder */
   function ClearCache()
    {
     if ($handle = opendir($this->CacheFolder))
      {
       while (false !== ($file = readdir($handle)))
        {
         if ( $file != "." && $file != ".." )
          unlink($this->CacheFolder.$file);
        }
       closedir($handle);
      }
    }

   /* This function is checking if we have an offline version of this chart */
   function IsInCache($ID,$Data,$Hash="")
    {
     if ( $Hash == "" )
      $Hash = $this->GetHash($ID,$Data);

     if ( file_exists($this->CacheFolder.$Hash) )
      return(TRUE);
     else
      return(FALSE);
    }

   /* This function is making a copy of drawn chart in the cache folder */
   function WriteToCache($ID,$Data,$Picture)
    {
     $Hash     = $this->GetHash($ID,$Data);
     $FileName = $this->CacheFolder.$Hash;

     imagepng($Picture->Picture,$FileName);
    }

   /* This function is removing any cached copy of this chart */
   function DeleteFromCache($ID,$Data)
    {
     $Hash     = $this->GetHash($ID,$Data);
     $FileName = $this->CacheFolder.$Hash;

     if ( file_exists($FileName ) )
      unlink($FileName);
    }

   /* This function is retrieving the cached picture if applicable */
   function GetFromCache($ID,$Data)
    {
     $Hash     = $this->GetHash($ID,$Data);
     if ( $this->IsInCache("","",$Hash ) )
      {
       $FileName = $this->CacheFolder.$Hash;

       header('Content-type: image/png');
       @readfile($FileName);
       exit();
      }
    }

   /* This function is building the graph unique hash key */
   function GetHash($ID,$Data)
    {
     $mKey = "$ID";
     foreach($Data as $key => $Values)
      {
       $tKey = "";
       foreach($Values as $Serie => $Value)
        $tKey = $tKey.$Serie.$Value;
       $mKey = $mKey.md5($tKey);
      }
     return(md5($mKey));
    }
  }
?>