]> git.mxchange.org Git - mailer.git/blob - inc/libs/cache_functions.php
121cfc096a79f08c32c6f8b53d09b3ce1b426281
[mailer.git] / inc / libs / cache_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/11/2003 *
4  * ===============                              Last change: 10/11/2003 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admins_functions.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for the admins extension               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die admins-Erweiterung           *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Caching class
41 class CacheSystem {
42         // Define variables
43         var $ret = "init";
44         var $path = "";
45         var $inc = "";
46         var $pointer = false;
47         var $data = "";
48         var $version = "";
49         var $name = "";
50         var $rebuilt = array();
51
52         // Constructor
53         function CacheSystem ($interval, $path, $tested) {
54                 // Failed is the default
55                 $this->ret = "failed";
56
57                 // Remeber path
58                 $this->path = $path;
59
60                 // Check if path exists
61                 if ((is_dir($path)) && (!$tested)) {
62                         // Check if we can create a file inside the path
63                         touch($path."dummy.tmp", 'w');
64                         if (FILE_READABLE($path."dummy.tmp")) {
65                                 // Yes, we can do. So let's remove it
66                                 unlink($path."dummy.tmp");
67
68                                 // Is there a .htaccess file?
69                                 if (FILE_READABLE($path.".htaccess")) {
70                                         // Update database that we have tested it
71                                         UPDATE_CONFIG("cache_tested", 1);
72
73                                         // All done!
74                                         $this->ret = "done";
75                                 } else {
76                                         // Stop! Set a .htaccess file first
77                                         $this->ret = "htaccess";
78                                 }
79                         }
80                 } elseif ($tested) {
81                         // System already tested
82                         $this->ret = "done";
83                 }
84         }
85
86         // Checks validity of cache file and if content is given
87         function loadCacheFile ($cacheName, $forceContent = false) {
88                 // Remember cache file
89                 $this->name = $cacheName;
90
91                 // Construct FQFN (full qualified file name)
92                 $this->inc = $this->path . $cacheName . ".cache";
93
94                 // Check if file exists and if version matches
95                 $status = (FILE_READABLE($this->inc) && (is_writeable($this->inc)) && ($this->extensionVersionMatches("cache")));
96
97                 // Return status
98                 return $status;
99         }
100
101         function init () {
102                 // This will destory an existing cache file!
103                 if ($this->ret == "done") {
104                         // Create file
105                         if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
106                         $this->pointer = fopen($this->inc, 'w') or mxchange_die("Cannot write to cache ".$this->inc." !");
107
108                         // Add open PHP tag
109                         fwrite($this->pointer, "<?php\n");
110
111                         // Add default depency
112                         $this->storeExtensionVersion("cache");
113                 } else {
114                         // Cannot create file
115                         addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
116                 }
117         }
118
119         function addRow ($data) {
120                 global $cacheArray;
121
122                 if (is_resource($this->pointer)) {
123                         // Write every array element to cache file
124                         foreach ($data as $k => $v) {
125                                 // Write global cache array for immediate access
126                                 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
127                                         if ($k != "ext_name") {
128                                                 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
129                                         } else {
130                                                 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
131                                         }
132                                         if (($k == "ext_keep") && ($v == "Y")) {
133                                                 $cacheArray['active_extensions'][$data['ext_name']] = $v;
134                                         } // END - if
135                                 } elseif (is_array($v)) {
136                                         // Serialize and BASE64-encode the array
137                                         $v = base64_encode(serialize($v));
138                                 }
139
140                                 // Write cache line to file
141                                 fwrite($this->pointer, $this->rewriteEntry($k, $v));
142                         }
143                 } else {
144                         // Cannot create file
145                         addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
146                 }
147         }
148
149         function finalize () {
150                 // Quit function when no pointer is set
151                 if (is_resource($this->pointer)) {
152                         // Write footer
153                         fwrite($this->pointer, "?>\n");
154
155                         // Close file add destroy handler
156                         fclose($this->pointer);
157
158                         // Set rights
159                         if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
160
161                         // Remove pointer
162                         $this->pointer = false;
163                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />\n";
164                 } // END - if
165         }
166
167         function getArrayFromCache () {
168                 // Is the cache already loaded?
169                 if (isset($this->data[$this->name])) {
170                         // Return it's content!
171                         return $this->data[$this->name];
172                 } // END - if
173
174                 // Is the cache file there?
175                 if (FILE_READABLE($this->inc)) {
176                         // Prepare temporary array
177                         $data = array();
178                         $cache_version = null;
179
180                         // Load cache file
181                         LOAD_INC_ONCE($this->inc);
182
183                         // Is there an array?
184                         if (is_array($data)) {
185                                 // Cache data
186                                 $this->data[$this->name] = $data;
187
188                                 // Cache version found?
189                                 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
190                                         // Remember it as well...
191                                         $this->version[$this->name] = $cache_version;
192                                 } else {
193                                         // Invalid cache so destroy it
194                                         $this->destroyCacheFile();
195
196                                         // Clear cached data
197                                         $this->data[$this->name] = array();
198                                 }
199
200                                 // Return cache
201                                 return $this->data[$this->name];
202                         } else {
203                                 // Cache problem detected!
204                                 $this->destroyCacheFile();
205                         }
206                 } else {
207                         // Cache file not found or not readable
208                         addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->inc.CACHE_CANNOT_LOAD_2);
209                 }
210         }
211
212         // Destroy an existing cache file
213         function destroyCacheFile () {
214                 // Is the cache file there?
215                 if ((!isset($this->rebuilt[$this->name])) && (FILE_READABLE($this->inc))) {
216                         // Close cache
217                         $this->finalize();
218
219                         // Remove cache file from system
220                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - DESTROYED!<br />\n";
221                         unlink($this->inc);
222
223                         // Is the file there?
224                         if (!FILE_READABLE($this->inc)) {
225                                 // The cache does no longer exist so kill the content
226                                 unset($this->data[$this->name]);
227                                 unset($this->version[$this->name]);
228                                 $this->rebuilt[$this->name] = true;
229                         } else {
230                                 // Not removed!
231                                 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
232                         }
233                 } // END - if
234         }
235
236         // Unused method:
237         function removeEntry ($search, $data, $array) {
238                 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
239                         // Load cache into dummy array
240                         $dummy = $this->getArrayFromCache();
241
242                         // Search for key in array
243                         $key = array_search($data, $dummy[$search]);
244                         if (!empty($key)) {
245                                 // Key (hopefully) found?
246                                 foreach ($array as $a) {
247                                         // So we can remove all elements as requested
248                                         unset($dummy[$a][$key]);
249                                 } // END - foreach
250
251                                 // Flush array to cache file
252                                 $this->init();
253
254                                 // Write array out
255                                 $this->writeArray($dummy);
256
257                                 // Close cache file
258                                 $this->finalize();
259                         }
260                 } else {
261                         // Cannot write to cache!
262                         addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
263                 }
264         }
265
266         function writeArray ($array) {
267                 if (is_resource($this->pointer)) {
268                         foreach ($array as $k => $v) {
269                                 if (is_array($v)) {
270                                         // Multi line(s) found
271                                         $LINE = "";
272                                         foreach($v as $k2 => $v2) {
273                                                 // Put every array element in a row...
274                                                 $LINE .= $this->rewriteEntry($k, $v2);
275                                         }
276                                 } else {
277                                         // Single line found
278                                         $LINE = $this->rewriteEntry($k, $v);
279                                 }
280
281                                 // Write line(s)
282                                 fwrite($this->pointer, $LINE);
283                         } // END - foreach
284                 } else {
285                         // Cannot write array!
286                         addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
287                 }
288         }
289
290         // Unused method
291         function replaceEntry ($search, $replace, $search_key, $array) {
292                 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
293                         // Load cache into dummy array
294                         $dummy = $this->getArrayFromCache();
295
296                         // Check if $dummy is valid (prevents some errors)
297                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
298                                 // Search for key in array
299                                 $key_found = array_key_exists($search_key, $dummy[$search]);
300                                 if ($key_found == true) {
301                                         $key = $search_key;
302                                         // Key (hopefully) found?
303                                         foreach ($dummy as $a => $v) {
304                                                 // So we can update all entries
305                                                 if ($a == $search) {
306                                                         // Update now...
307                                                         $dummy[$a][$search_key] = $replace;
308                                                 } // END - if
309                                         } // END - foreach
310
311                                         // Flush array to cache file
312                                         $this->init();
313
314                                         // Write array out
315                                         $this->writeArray($dummy);
316
317                                         // Close cache file
318                                         $this->finalize();
319                                 } // END - if
320                         } // END - if
321                 } else {
322                         // Cannot write to cache!
323                         addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
324                 }
325         }
326
327         function storeExtensionVersion ($ext_name) {
328                 // Valid cache pointer?
329                 if (is_resource($this->pointer)) {
330                         // Get extension version
331                         $ext_ver = GET_EXT_VERSION($ext_name);
332
333                         // Write cache line to file
334                         fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
335
336                         // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
337                         $this->version[$this->name][$ext_name] = $ext_ver;
338                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
339                 } else {
340                         // Cannot create file
341                         addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
342                 }
343         }
344
345         function extensionVersionMatches ($ext_name) {
346                 // Load cache (dummy)
347                 $this->getArrayFromCache();
348
349                 // Get extension version
350                 $ext_ver = GET_EXT_VERSION($ext_name);
351
352                 // Debug messages
353                 if (isset($this->version[$this->name][$ext_name])) {
354                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): cache={$this->name},ext_name={$ext_name} - {$ext_ver}/{$this->version[$this->name][$ext_name]}<br />\n";
355                 } else {
356                         // No cache version found!
357                         DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing entry for extension {$ext_name}!");
358                 }
359
360                 // Compare both
361                 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
362         }
363
364         function rewriteEntry ($key, $value) {
365                 // Init line
366                 $line = "";
367
368                 // String or non-string? ;-)
369                 if (is_string($value)) {
370                         // String...
371                         $line = "\$data['".$key."'][] = \"".$value."\";\n";
372                 } elseif (is_null($value)) {
373                         // Null
374                         $line = "\$data['".$key."'][] = null;\n";
375                 } elseif (is_bool($value)) {
376                         // Boolean value
377                         if ($value === true) {
378                                 $line = "\$data['".$key."'][] = true;\n";
379                         } else {
380                                 $line = "\$data['".$key."'][] = false;\n";
381                         }
382                 } else {
383                         // Non-string
384                         $line = "\$data['".$key."'][] = ".$value.";\n";
385                 }
386
387                 // Return line
388                 return $line;
389         }
390
391         function getStatus () {
392                 return $this->ret;
393         }
394 }
395
396 // Destroy the cache on extension changes
397 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
398         global $cacheInstance;
399
400         // Remove cache
401         if (EXT_IS_ACTIVE("cache")) {
402                 if ($cacheInstance->loadCacheFile("config"))     $cacheInstance->destroyCacheFile();
403                 if ($cacheInstance->loadCacheFile("extensions")) $cacheInstance->destroyCacheFile();
404                 if ($cacheInstance->loadCacheFile("mod_reg"))    $cacheInstance->destroyCacheFile();
405         } // END - if
406
407         // Return it
408         return $data;
409 }
410
411 // Destroy the cache on changing admin
412 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
413         global $cacheInstance;
414
415         // Remove cache
416         if (EXT_IS_ACTIVE("cache")) {
417                 if ($cacheInstance->loadCacheFile("admins")) $cacheInstance->destroyCacheFile();
418         } // END - if
419 }
420
421 // Destroy all cache files
422 function FILTER_CACHE_DESTROY_ALL () {
423         global $cacheInstance;
424
425         // Remove cache
426         if (EXT_IS_ACTIVE("cache")) {
427                 if ($cacheInstance->loadCacheFile("admins"))      $cacheInstance->destroyCacheFile();
428                 if ($cacheInstance->loadCacheFile("admins_acls")) $cacheInstance->destroyCacheFile();
429                 if ($cacheInstance->loadCacheFile("config"))      $cacheInstance->destroyCacheFile();
430                 if ($cacheInstance->loadCacheFile("extensions"))  $cacheInstance->destroyCacheFile();
431                 if ($cacheInstance->loadCacheFile("mod_reg"))     $cacheInstance->destroyCacheFile();
432                 if ($cacheInstance->loadCacheFile("refdepths"))   $cacheInstance->destroyCacheFile();
433                 if ($cacheInstance->loadCacheFile("refsystem"))   $cacheInstance->destroyCacheFile();
434                 if ($cacheInstance->loadCacheFile("themes"))      $cacheInstance->destroyCacheFile();
435         } // END - if
436 }
437
438 // Filter for purging entire admin menu cache
439 function FILTER_CACHE_PURGE_ADMIN_MENU () {
440         // Just call the function
441         CACHE_PURGE_ADMIN_MENU();
442 }
443
444 //
445 ?>