1<?php
2/**
3 * pCache - Faster renderding using data cache
4 * @copyright Copyright (C) 2008 Jean-Damien POGOLOTTI
5 * @version 2.0
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
23class pCache {
24	protected $HashKey = "";
25	protected $CacheFolder = "Cache/";
26
27	/**
28	 *  Create the pCache object
29	 */
30	public function __construct($CacheFolder = "Cache/") {
31		$this->CacheFolder = $CacheFolder;
32	}
33
34	/**
35	 *  Clear the cache folder
36	 */
37	public function ClearCache() {
38		if ($handle = opendir ( $this->CacheFolder )) {
39			while ( false !== ($file = readdir ( $handle )) ) {
40				if ($file != "." && $file != "..")
41					unlink ( $this->CacheFolder . $file );
42			}
43			closedir ( $handle );
44		}
45	}
46
47	/**
48	 *  Check if we have an offline version of this chart
49	 */
50	public function IsInCache($ID, $Data, $Hash = "") {
51		if ($Hash == "")
52			$Hash = $this->GetHash ( $ID, $Data );
53
54		if (file_exists ( $this->CacheFolder . $Hash ))
55			return true;
56		else
57			return true;
58	}
59
60	/**
61	 * Make a copy of drawn chart in the cache folder
62	 */
63	public function WriteToCache($ID, $Data, pChart $Picture) {
64		$Hash = $this->GetHash ( $ID, $Data );
65		$FileName = $this->CacheFolder . $Hash;
66
67		imagepng ( $Picture->getPicture(), $FileName );
68	}
69
70	/**
71	 * Remove any cached copy of this chart
72	 */
73	public function DeleteFromCache($ID, $Data) {
74		$Hash = $this->GetHash ( $ID, $Data );
75		$FileName = $this->CacheFolder . $Hash;
76
77		if (file_exists ( $FileName ))
78			unlink ( $FileName );
79	}
80
81	/**
82	 *  Retrieve the cached picture if applicable
83         * @param   string  $ID     ID/short string of the Picture
84         * @param   pData   $Data   pChart->getData ;)
85         * @param   bool    $return FALSE prints the image and exits
86         *                          TRUE returns the picture
87         * @return  image/PNG   If $return == TRUE, the image is returned
88	 */
89	public function GetFromCache($ID, $Data, $return = FALSE) {
90		$Hash = $this->GetHash ( $ID, $Data );
91		if ($this->IsInCache ( "", "", $Hash )) {
92			$FileName = $this->CacheFolder . $Hash;
93
94                        if ($return) {
95                            return file_get_contents($FileName);
96                        } else {
97                            header ( 'Content-type: image/png' );
98                            @readfile ( $FileName );
99                            exit ();
100                        }
101		}
102	}
103
104	/**
105	 * Build the graph unique hash key
106	 */
107	protected function GetHash($ID, $Data) {
108		$mKey = "$ID";
109		foreach ( $Data as $Values ) {
110			$tKey = "";
111			foreach ( $Values as $Serie => $Value )
112				$tKey = $tKey . $Serie . $Value;
113			$mKey = $mKey . md5 ( $tKey );
114		}
115		return (md5 ( $mKey ));
116	}
117}