Several rewrites/renames, fixes for installation. Resolves #104
[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 = array();
48         var $version = array();
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(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
116                 }
117         }
118
119         function addRow ($data) {
120                 // Is the pointe rvalid?
121                 if (is_resource($this->pointer)) {
122                         // Write every array element to cache file
123                         foreach ($data as $k => $v) {
124                                 // Write global cache array for immediate access
125                                 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
126                                         if ($k != "ext_name") {
127                                                 $GLOBALS['cache_array']['extensions'][$k][$data['ext_name']] = $v;
128                                         } else {
129                                                 $GLOBALS['cache_array']['extensions'][$k][$data['ext_id']] = $v;
130                                         }
131                                         if (($k == "ext_keep") && ($v == "Y")) {
132                                                 $GLOBALS['cache_array']['active_extensions'][$data['ext_name']] = $v;
133                                         } // END - if
134                                 } elseif (is_array($v)) {
135                                         // Serialize and BASE64-encode the array
136                                         $v = base64_encode(serialize($v));
137                                 }
138
139                                 // Write cache line to file
140                                 fwrite($this->pointer, $this->rewriteEntry($k, $v));
141                         }
142                 } else {
143                         // Cannot create file
144                         addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
145                 }
146         }
147
148         function finalize () {
149                 // Quit function when no pointer is set
150                 if (is_resource($this->pointer)) {
151                         // Write footer
152                         fwrite($this->pointer, "?>\n");
153
154                         // Close file add destroy handler
155                         fclose($this->pointer);
156
157                         // Set rights
158                         if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
159
160                         // Remove pointer
161                         $this->pointer = false;
162                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />\n";
163                 } // END - if
164         }
165
166         function getArrayFromCache () {
167                 // Is the cache already loaded?
168                 if (isset($this->data[$this->name])) {
169                         // Return it's content!
170                         return $this->data[$this->name];
171                 } // END - if
172
173                 // Is the cache file there?
174                 if (FILE_READABLE($this->inc)) {
175                         // Prepare temporary array
176                         $data = array();
177                         $cache_version = null;
178
179                         // Load cache file
180                         LOAD_INC_ONCE($this->inc);
181
182                         // Is there an array?
183                         if (is_array($data)) {
184                                 // Cache data
185                                 $this->data[$this->name] = $data;
186
187                                 // Cache version found?
188                                 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
189                                         // Remember it as well...
190                                         $this->version[$this->name] = $cache_version;
191                                 } else {
192                                         // Invalid cache so destroy it
193                                         $this->destroyCacheFile();
194
195                                         // Clear cached data
196                                         $this->data[$this->name] = array();
197                                 }
198
199                                 // Return cache
200                                 return $this->data[$this->name];
201                         } else {
202                                 // Cache problem detected!
203                                 $this->destroyCacheFile();
204                         }
205                 } else {
206                         // Cache file not found or not readable
207                         addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->inc.CACHE_CANNOT_LOAD_2);
208                 }
209         }
210
211         // Destroy an existing cache file
212         function destroyCacheFile () {
213                 // Is the cache file there?
214                 if ((!isset($this->rebuilt[$this->name])) && (FILE_READABLE($this->inc))) {
215                         // Close cache
216                         $this->finalize();
217
218                         // Remove cache file from system
219                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - DESTROYED!<br />\n";
220                         unlink($this->inc);
221
222                         // Is the file there?
223                         if (!FILE_READABLE($this->inc)) {
224                                 // The cache does no longer exist so kill the content
225                                 unset($this->data[$this->name]);
226                                 unset($this->version[$this->name]);
227                                 $this->rebuilt[$this->name] = true;
228                         } else {
229                                 // Not removed!
230                                 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
231                         }
232                 } // END - if
233         }
234
235         // Unused method:
236         function removeEntry ($search, $data, $array) {
237                 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
238                         // Load cache into dummy array
239                         $dummy = $this->getArrayFromCache();
240
241                         // Search for key in array
242                         $key = array_search($data, $dummy[$search]);
243                         if (!empty($key)) {
244                                 // Key (hopefully) found?
245                                 foreach ($array as $a) {
246                                         // So we can remove all elements as requested
247                                         unset($dummy[$a][$key]);
248                                 } // END - foreach
249
250                                 // Flush array to cache file
251                                 $this->init();
252
253                                 // Write array out
254                                 $this->writeArray($dummy);
255
256                                 // Close cache file
257                                 $this->finalize();
258                         }
259                 } else {
260                         // Cannot write to cache!
261                         addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
262                 }
263         }
264
265         function writeArray ($array) {
266                 if (is_resource($this->pointer)) {
267                         foreach ($array as $k => $v) {
268                                 if (is_array($v)) {
269                                         // Multi line(s) found
270                                         $LINE = "";
271                                         foreach($v as $k2 => $v2) {
272                                                 // Put every array element in a row...
273                                                 $LINE .= $this->rewriteEntry($k, $v2);
274                                         }
275                                 } else {
276                                         // Single line found
277                                         $LINE = $this->rewriteEntry($k, $v);
278                                 }
279
280                                 // Write line(s)
281                                 fwrite($this->pointer, $LINE);
282                         } // END - foreach
283                 } else {
284                         // Cannot write array!
285                         addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
286                 }
287         }
288
289         // Unused method
290         function replaceEntry ($search, $replace, $search_key, $array) {
291                 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
292                         // Load cache into dummy array
293                         $dummy = $this->getArrayFromCache();
294
295                         // Check if $dummy is valid (prevents some errors)
296                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
297                                 // Search for key in array
298                                 $key_found = array_key_exists($search_key, $dummy[$search]);
299                                 if ($key_found == true) {
300                                         $key = $search_key;
301                                         // Key (hopefully) found?
302                                         foreach ($dummy as $a => $v) {
303                                                 // So we can update all entries
304                                                 if ($a == $search) {
305                                                         // Update now...
306                                                         $dummy[$a][$search_key] = $replace;
307                                                 } // END - if
308                                         } // END - foreach
309
310                                         // Flush array to cache file
311                                         $this->init();
312
313                                         // Write array out
314                                         $this->writeArray($dummy);
315
316                                         // Close cache file
317                                         $this->finalize();
318                                 } // END - if
319                         } // END - if
320                 } else {
321                         // Cannot write to cache!
322                         addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
323                 }
324         }
325
326         function storeExtensionVersion ($ext_name) {
327                 // Valid cache pointer?
328                 if (is_resource($this->pointer)) {
329                         // Get extension version
330                         $ext_ver = GET_EXT_VERSION($ext_name);
331
332                         // Write cache line to file
333                         fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
334
335                         // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
336                         $this->version[$this->name][$ext_name] = $ext_ver;
337                         //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
338                 } else {
339                         // Cannot create file
340                         addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
341                 }
342         }
343
344         function extensionVersionMatches ($ext_name) {
345                 // Load cache (dummy)
346                 $this->getArrayFromCache();
347
348                 // Get extension version
349                 $ext_ver = GET_EXT_VERSION($ext_name);
350
351                 // Debug messages
352                 if (isset($this->version[$this->name][$ext_name])) {
353                         //* 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";
354                 } else {
355                         // No cache version found!
356                         DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing entry for extension {$ext_name}!");
357                 }
358
359                 // Compare both
360                 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
361         }
362
363         function rewriteEntry ($key, $value) {
364                 // Init line
365                 $line = "";
366
367                 // String or non-string? ;-)
368                 if (is_string($value)) {
369                         // String...
370                         $line = "\$data['".$key."'][] = \"".$value."\";\n";
371                 } elseif (is_null($value)) {
372                         // Null
373                         $line = "\$data['".$key."'][] = null;\n";
374                 } elseif (is_bool($value)) {
375                         // Boolean value
376                         if ($value === true) {
377                                 $line = "\$data['".$key."'][] = true;\n";
378                         } else {
379                                 $line = "\$data['".$key."'][] = false;\n";
380                         }
381                 } else {
382                         // Non-string
383                         $line = "\$data['".$key."'][] = ".$value.";\n";
384                 }
385
386                 // Return line
387                 return $line;
388         }
389
390         function getStatus () {
391                 return $this->ret;
392         }
393 }
394
395 // Destroy the cache on extension changes
396 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
397         // Remove cache
398         if (EXT_IS_ACTIVE("cache")) {
399                 if ($GLOBALS['cache_instance']->loadCacheFile("config"))     $GLOBALS['cache_instance']->destroyCacheFile();
400                 if ($GLOBALS['cache_instance']->loadCacheFile("extensions")) $GLOBALS['cache_instance']->destroyCacheFile();
401                 if ($GLOBALS['cache_instance']->loadCacheFile("modreg"))     $GLOBALS['cache_instance']->destroyCacheFile();
402         } // END - if
403
404         // Return it
405         return $data;
406 }
407
408 // Destroy the cache on changing admin
409 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
410         // Remove cache
411         if (EXT_IS_ACTIVE("cache")) {
412                 if ($GLOBALS['cache_instance']->loadCacheFile("admins")) $GLOBALS['cache_instance']->destroyCacheFile();
413         } // END - if
414 }
415
416 // Destroy all cache files
417 function FILTER_CACHE_DESTROY_ALL () {
418         // Remove cache
419         if (EXT_IS_ACTIVE("cache")) {
420                 if ($GLOBALS['cache_instance']->loadCacheFile("admins"))      $GLOBALS['cache_instance']->destroyCacheFile();
421                 if ($GLOBALS['cache_instance']->loadCacheFile("admins_acls")) $GLOBALS['cache_instance']->destroyCacheFile();
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                 if ($GLOBALS['cache_instance']->loadCacheFile("refdepths"))   $GLOBALS['cache_instance']->destroyCacheFile();
426                 if ($GLOBALS['cache_instance']->loadCacheFile("refsystem"))   $GLOBALS['cache_instance']->destroyCacheFile();
427                 if ($GLOBALS['cache_instance']->loadCacheFile("themes"))      $GLOBALS['cache_instance']->destroyCacheFile();
428         } // END - if
429 }
430
431 // Filter for purging entire admin menu cache
432 function FILTER_CACHE_PURGE_ADMIN_MENU () {
433         // Just call the function
434         CACHE_PURGE_ADMIN_MENU();
435 }
436
437 //
438 ?>