PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / szymach / c-pchart / src / Cache.php
matomo / app / vendor / szymach / c-pchart / src Last commit date
Barcode 1 year ago Chart 1 year ago BaseDraw.php 1 year ago Cache.php 1 year ago Data.php 1 year ago Draw.php 1 year ago Image.php 1 year ago
Cache.php
319 lines
1 <?php
2
3 namespace CpChart;
4
5 /**
6 * Cache - speed up the rendering by caching up the pictures
7 *
8 * Version : 2.1.4
9 * Made by : Jean-Damien POGOLOTTI
10 * Last Update : 19/01/2014
11 *
12 * This file can be distributed under the license you can find at :
13 *
14 * http://www.pchart.net/license
15 *
16 * You can find the whole class documentation on the pChart web site.
17 */
18 class Cache
19 {
20 /**
21 * @var string
22 */
23 public $CacheFolder;
24 /**
25 * @var string
26 */
27 public $CacheIndex;
28 /**
29 * @var string
30 */
31 public $CacheDB;
32 /**
33 * @param array $Settings
34 */
35 public function __construct(array $Settings = [])
36 {
37 $CacheFolder = isset($Settings["CacheFolder"]) ? $Settings["CacheFolder"] : __DIR__ . "/../cache";
38 $CacheIndex = isset($Settings["CacheIndex"]) ? $Settings["CacheIndex"] : "index.db";
39 $CacheDB = isset($Settings["CacheDB"]) ? $Settings["CacheDB"] : "cache.db";
40 $this->CacheFolder = $CacheFolder;
41 $this->CacheIndex = $CacheIndex;
42 $this->CacheDB = $CacheDB;
43 if (!file_exists($this->CacheFolder . "/" . $this->CacheIndex)) {
44 touch($this->CacheFolder . "/" . $this->CacheIndex);
45 }
46 if (!file_exists($this->CacheFolder . "/" . $this->CacheDB)) {
47 touch($this->CacheFolder . "/" . $this->CacheDB);
48 }
49 }
50 /**
51 * Flush the cache contents
52 */
53 public function flush()
54 {
55 if (file_exists($this->CacheFolder . "/" . $this->CacheIndex)) {
56 unlink($this->CacheFolder . "/" . $this->CacheIndex);
57 touch($this->CacheFolder . "/" . $this->CacheIndex);
58 }
59 if (file_exists($this->CacheFolder . "/" . $this->CacheDB)) {
60 unlink($this->CacheFolder . "/" . $this->CacheDB);
61 touch($this->CacheFolder . "/" . $this->CacheDB);
62 }
63 }
64 /**
65 * Return the MD5 of the data array to clearly identify the chart
66 *
67 * @param Data $Data
68 * @param string $Marker
69 * @return string
70 */
71 public function getHash(\CpChart\Data $Data, $Marker = "")
72 {
73 return md5($Marker . serialize($Data->Data));
74 }
75 /**
76 * Write the generated picture to the cache
77 *
78 * @param string $ID
79 * @param Image $pChartObject
80 */
81 public function writeToCache($ID, \CpChart\Image $pChartObject)
82 {
83 /* Compute the paths */
84 $TemporaryFile = tempnam($this->CacheFolder, "tmp_");
85 $Database = $this->CacheFolder . "/" . $this->CacheDB;
86 $Index = $this->CacheFolder . "/" . $this->CacheIndex;
87 /* Flush the picture to a temporary file */
88 imagepng($pChartObject->Picture, $TemporaryFile);
89 /* Retrieve the files size */
90 $PictureSize = filesize($TemporaryFile);
91 $DBSize = filesize($Database);
92 /* Save the index */
93 $Handle = fopen($Index, "a");
94 fwrite($Handle, $ID . "," . $DBSize . "," . $PictureSize . "," . time() . ",0\r\n");
95 fclose($Handle);
96 /* Get the picture raw contents */
97 $Handle = fopen($TemporaryFile, "r");
98 $Raw = fread($Handle, $PictureSize);
99 fclose($Handle);
100 /* Save the picture in the solid database file */
101 $Handle = fopen($Database, "a");
102 fwrite($Handle, $Raw);
103 fclose($Handle);
104 /* Remove temporary file */
105 unlink($TemporaryFile);
106 }
107 /**
108 * Remove object older than the specified TS
109 * @param int $Expiry
110 */
111 public function removeOlderThan($Expiry)
112 {
113 $this->dbRemoval(["Expiry" => $Expiry]);
114 }
115 /**
116 * Remove an object from the cache
117 * @param string $ID
118 */
119 public function remove($ID)
120 {
121 $this->dbRemoval(["Name" => $ID]);
122 }
123 /**
124 * Remove with specified criterias.
125 *
126 * @param array $Settings
127 * @return int
128 */
129 public function dbRemoval(array $Settings)
130 {
131 $ID = isset($Settings["Name"]) ? $Settings["Name"] : null;
132 $Expiry = isset($Settings["Expiry"]) ? $Settings["Expiry"] : -(24 * 60 * 60);
133 $TS = time() - $Expiry;
134 /* Compute the paths */
135 $Database = $this->CacheFolder . "/" . $this->CacheDB;
136 $Index = $this->CacheFolder . "/" . $this->CacheIndex;
137 $DatabaseTemp = $this->CacheFolder . "/" . $this->CacheDB . ".tmp";
138 $IndexTemp = $this->CacheFolder . "/" . $this->CacheIndex . ".tmp";
139 /* Single file removal */
140 if ($ID != null) {
141 /* Retrieve object informations */
142 $Object = $this->isInCache($ID, \true);
143 /* If it's not in the cache DB, go away */
144 if (!$Object) {
145 return 0;
146 }
147 }
148 /* Create the temporary files */
149 if (!file_exists($DatabaseTemp)) {
150 touch($DatabaseTemp);
151 }
152 if (!file_exists($IndexTemp)) {
153 touch($IndexTemp);
154 }
155 /* Open the file handles */
156 $IndexHandle = @fopen($Index, "r");
157 $IndexTempHandle = @fopen($IndexTemp, "w");
158 $DBHandle = @fopen($Database, "r");
159 $DBTempHandle = @fopen($DatabaseTemp, "w");
160 /* Remove the selected ID from the database */
161 while (!feof($IndexHandle)) {
162 $Entry = fgets($IndexHandle, 4096);
163 $Entry = str_replace("\r", "", $Entry);
164 $Entry = str_replace("\n", "", $Entry);
165 $Settings = preg_split("/,/", $Entry);
166 if ($Entry != "") {
167 $PicID = $Settings[0];
168 $DBPos = $Settings[1];
169 $PicSize = $Settings[2];
170 $GeneratedTS = $Settings[3];
171 $Hits = $Settings[4];
172 if ($Settings[0] != $ID && $GeneratedTS > $TS) {
173 $CurrentPos = ftell($DBTempHandle);
174 fwrite($IndexTempHandle, sprintf("%s,%s,%s,%s,%s\r\n", $PicID, $CurrentPos, $PicSize, $GeneratedTS, $Hits));
175 fseek($DBHandle, $DBPos);
176 $Picture = fread($DBHandle, $PicSize);
177 fwrite($DBTempHandle, $Picture);
178 }
179 }
180 }
181 /* Close the handles */
182 fclose($IndexHandle);
183 fclose($IndexTempHandle);
184 fclose($DBHandle);
185 fclose($DBTempHandle);
186 /* Remove the prod files */
187 unlink($Database);
188 unlink($Index);
189 /* Swap the temp & prod DB */
190 rename($DatabaseTemp, $Database);
191 rename($IndexTemp, $Index);
192 }
193 /**
194 * Is the file in cache?
195 *
196 * @param string $ID
197 * @param boolean $Verbose
198 * @param boolean $UpdateHitsCount
199 * @return boolean
200 */
201 public function isInCache($ID, $Verbose = \false, $UpdateHitsCount = \false)
202 {
203 /* Compute the paths */
204 $Index = $this->CacheFolder . "/" . $this->CacheIndex;
205 /* Search the picture in the index file */
206 $Handle = @fopen($Index, "r");
207 while (!feof($Handle)) {
208 $IndexPos = ftell($Handle);
209 $Entry = fgets($Handle, 4096);
210 if ($Entry != "") {
211 $Settings = preg_split("/,/", $Entry);
212 $PicID = $Settings[0];
213 if ($PicID == $ID) {
214 fclose($Handle);
215 $DBPos = $Settings[1];
216 $PicSize = $Settings[2];
217 $GeneratedTS = $Settings[3];
218 $Hits = intval($Settings[4]);
219 if ($UpdateHitsCount) {
220 $Hits++;
221 if (strlen($Hits) < 7) {
222 $Hits = $Hits . str_repeat(" ", 7 - strlen($Hits));
223 }
224 $Handle = @fopen($Index, "r+");
225 fseek($Handle, $IndexPos);
226 fwrite($Handle, sprintf("%s,%s,%s,%s,%s\r\n", $PicID, $DBPos, $PicSize, $GeneratedTS, $Hits));
227 fclose($Handle);
228 }
229 if ($Verbose) {
230 return ["DBPos" => $DBPos, "PicSize" => $PicSize, "GeneratedTS" => $GeneratedTS, "Hits" => $Hits];
231 } else {
232 return \true;
233 }
234 }
235 }
236 }
237 fclose($Handle);
238 /* Picture isn't in the cache */
239 return \false;
240 }
241 /**
242 * Automatic output method based on the calling interface
243 * @param string $ID
244 * @param string $Destination
245 */
246 public function autoOutput($ID, $Destination = "output.png")
247 {
248 if (php_sapi_name() == "cli") {
249 $this->saveFromCache($ID, $Destination);
250 } else {
251 $this->strokeFromCache($ID);
252 }
253 }
254 /**
255 * Show image from cache
256 * @param string $ID
257 * @return boolean
258 */
259 public function strokeFromCache($ID)
260 {
261 /* Get the raw picture from the cache */
262 $Picture = $this->getFromCache($ID);
263 /* Do we have a hit? */
264 if ($Picture == null) {
265 return \false;
266 }
267 header('Content-type: image/png');
268 echo $Picture;
269 return \true;
270 }
271 /**
272 * Save file from cache.
273 * @param string $ID
274 * @param string $Destination
275 * @return boolean
276 */
277 public function saveFromCache($ID, $Destination)
278 {
279 /* Get the raw picture from the cache */
280 $Picture = $this->getFromCache($ID);
281 /* Do we have a hit? */
282 if ($Picture == null) {
283 return \false;
284 }
285 /* Flush the picture to a file */
286 $Handle = fopen($Destination, "w");
287 fwrite($Handle, $Picture);
288 fclose($Handle);
289 /* All went fine */
290 return \true;
291 }
292 /**
293 * Get file from cache
294 * @param string $ID
295 * @return string|null
296 */
297 public function getFromCache($ID)
298 {
299 /* Compute the path */
300 $Database = $this->CacheFolder . "/" . $this->CacheDB;
301 /* Lookup for the picture in the cache */
302 $CacheInfo = $this->isInCache($ID, \true, \true);
303 /* Not in the cache */
304 if (!$CacheInfo) {
305 return null;
306 }
307 /* Get the database extended information */
308 $DBPos = $CacheInfo["DBPos"];
309 $PicSize = $CacheInfo["PicSize"];
310 /* Extract the picture from the solid cache file */
311 $Handle = @fopen($Database, "r");
312 fseek($Handle, $DBPos);
313 $Picture = fread($Handle, $PicSize);
314 fclose($Handle);
315 /* Return back the raw picture data */
316 return $Picture;
317 }
318 }
319