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