Fixes for extension removal, now directly sent to [UN]REGISTER_FILTER()
[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 ($file, $forceContent = false) {
88                 // Remember cache file
89                 $this->name = $file;
90
91                 // Construct FQFN (full qualified file name)
92                 $this->inc = $this->path.$file.".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                         ADD_FATAL(__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                         ADD_FATAL(__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                         require_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                         ADD_FATAL(__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                                 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
232                         }
233                 } else {
234                         // Does not exist!
235                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
236                 }
237         }
238
239         // Unused method:
240         function removeEntry ($search, $data, $array) {
241                 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
242                         // Load cache into dummy array
243                         $dummy = $this->getArrayFromCache();
244
245                         // Search for key in array
246                         $key = array_search($data, $dummy[$search]);
247                         if (!empty($key)) {
248                                 // Key (hopefully) found?
249                                 foreach ($array as $a) {
250                                         // So we can remove all elements as requested
251                                         unset($dummy[$a][$key]);
252                                 } // END - foreach
253
254                                 // Flush array to cache file
255                                 $this->init();
256
257                                 // Write array out
258                                 $this->writeArray($dummy);
259
260                                 // Close cache file
261                                 $this->finalize();
262                         }
263                 } else {
264                         // Cannot write to cache!
265                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
266                 }
267         }
268
269         function writeArray ($array) {
270                 if (is_resource($this->pointer)) {
271                         foreach ($array as $k => $v) {
272                                 if (is_array($v)) {
273                                         // Multi line(s) found
274                                         $LINE = "";
275                                         foreach($v as $k2 => $v2) {
276                                                 // Put every array element in a row...
277                                                 $LINE .= $this->rewriteEntry($k, $v2);
278                                         }
279                                 } else {
280                                         // Single line found
281                                         $LINE = $this->rewriteEntry($k, $v);
282                                 }
283
284                                 // Write line(s)
285                                 fwrite($this->pointer, $LINE);
286                         } // END - foreach
287                 } else {
288                         // Cannot write array!
289                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
290                 }
291         }
292
293         // Unused method
294         function replaceEntry ($search, $replace, $search_key, $array) {
295                 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
296                         // Load cache into dummy array
297                         $dummy = $this->getArrayFromCache();
298
299                         // Check if $dummy is valid (prevents some errors)
300                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
301                                 // Search for key in array
302                                 $key_found = array_key_exists($search_key, $dummy[$search]);
303                                 if ($key_found == true) {
304                                         $key = $search_key;
305                                         // Key (hopefully) found?
306                                         foreach ($dummy as $a => $v) {
307                                                 // So we can update all entries
308                                                 if ($a == $search) {
309                                                         // Update now...
310                                                         $dummy[$a][$search_key] = $replace;
311                                                 } // END - if
312                                         } // END - foreach
313
314                                         // Flush array to cache file
315                                         $this->init();
316
317                                         // Write array out
318                                         $this->writeArray($dummy);
319
320                                         // Close cache file
321                                         $this->finalize();
322                                 } // END - if
323                         } // END - if
324                 } else {
325                         // Cannot write to cache!
326                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
327                 }
328         }
329
330         function storeExtensionVersion ($ext_name) {
331                 // Valid cache pointer?
332                 if (is_resource($this->pointer)) {
333                         // Get extension version
334                         $ext_ver = GET_EXT_VERSION($ext_name);
335
336                         // Write cache line to file
337                         fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
338
339                         // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
340                         $this->version[$this->name][$ext_name] = $ext_ver;
341                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
342                 } else {
343                         // Cannot create file
344                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
345                 }
346         }
347
348         function extensionVersionMatches ($ext_name) {
349                 // Load cache (dummy)
350                 $this->getArrayFromCache();
351
352                 // Get extension version
353                 $ext_ver = GET_EXT_VERSION($ext_name);
354
355                 // Debug messages
356                 if (isset($this->version[$this->name][$ext_name])) {
357                         //* 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";
358                 } else {
359                         // No cache version found!
360                         DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing entry for extension {$ext_name}!");
361                 }
362
363                 // Compare both
364                 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
365         }
366
367         function rewriteEntry ($key, $value) {
368                 // Init line
369                 $line = "";
370
371                 // String or non-string? ;-)
372                 if (is_string($value)) {
373                         // String...
374                         $line = "\$data['".$key."'][] = \"".$value."\";\n";
375                 } elseif (is_null($value)) {
376                         // Null
377                         $line = "\$data['".$key."'][] = null;\n";
378                 } elseif (is_bool($value)) {
379                         // Boolean value
380                         if ($value === true) {
381                                 $line = "\$data['".$key."'][] = true;\n";
382                         } else {
383                                 $line = "\$data['".$key."'][] = false;\n";
384                         }
385                 } else {
386                         // Non-string
387                         $line = "\$data['".$key."'][] = ".$value.";\n";
388                 }
389
390                 // Return line
391                 return $line;
392         }
393
394         function getStatus () {
395                 return $this->ret;
396         }
397 }
398
399 // Destroy the cache on extension changes
400 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
401         global $cacheInstance;
402
403         // Remove cache
404         if (EXT_IS_ACTIVE("cache")) {
405                 if ($cacheInstance->loadCacheFile("config"))     $cacheInstance->destroyCacheFile();
406                 if ($cacheInstance->loadCacheFile("extensions")) $cacheInstance->destroyCacheFile();
407                 if ($cacheInstance->loadCacheFile("mod_reg"))    $cacheInstance->destroyCacheFile();
408         } // END - if
409
410         // Return it
411         return $DATA;
412 }
413
414 // Destroy the cache on changing admin
415 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
416         global $cacheInstance;
417
418         // Remove cache
419         if (EXT_IS_ACTIVE("cache")) {
420                 if ($cacheInstance->loadCacheFile("admins")) $cacheInstance->destroyCacheFile();
421         } // END - if
422 }
423
424 // Destroy all cache files
425 function FILTER_CACHE_DESTROY_ALL () {
426         global $cacheInstance;
427
428         // Remove cache
429         if (EXT_IS_ACTIVE("cache")) {
430                 if ($cacheInstance->loadCacheFile("admins"))      $cacheInstance->destroyCacheFile();
431                 if ($cacheInstance->loadCacheFile("admins_acls")) $cacheInstance->destroyCacheFile();
432                 if ($cacheInstance->loadCacheFile("config"))      $cacheInstance->destroyCacheFile();
433                 if ($cacheInstance->loadCacheFile("extensions"))  $cacheInstance->destroyCacheFile();
434                 if ($cacheInstance->loadCacheFile("mod_reg"))     $cacheInstance->destroyCacheFile();
435                 if ($cacheInstance->loadCacheFile("refdepths"))   $cacheInstance->destroyCacheFile();
436                 if ($cacheInstance->loadCacheFile("refsystem"))   $cacheInstance->destroyCacheFile();
437                 if ($cacheInstance->loadCacheFile("themes"))      $cacheInstance->destroyCacheFile();
438         } // END - if
439 }
440
441 // Filter for purging entire admin menu cache
442 function FILTER_CACHE_PURGE_ADMIN_MENU () {
443         // Just call the function
444         CACHE_PURGE_ADMIN_MENU();
445 }
446
447 //
448 ?>