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