aaaebe605205fd0b843abcb2761c5b5f5a1e5333
[mailer.git] / inc / classes / cachesystem.class.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/22/2009 *
4  * ===================                          Last change: 10/22/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : cachesystem.class.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : CacheSystem class                                *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : CacheSystem-Klasse                               *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Caching class
44 class CacheSystem {
45         // Status code
46         var $statusCode = 'init';
47
48         // Full-qualified filename
49         var $fqfn = '';
50
51         // Resource to cache file
52         var $pointer = false;
53
54         // Data array from cache
55         var $data = array();
56
57         // Version data from cache
58         var $version = array();
59
60         // Cache name
61         var $name = '';
62         var $rebuilt = array();
63
64         // File extension
65         var $extension = '.cache';
66         var $status = array();
67         var $readable = array();
68         var $fullPath = '';
69
70         // Constructor
71         function CacheSystem () {
72                 // Construct full path
73                 $this->fullPath = getPath() . getCachePath();
74
75                 // Failed is the default
76                 $this->setStatusCode('failed');
77
78                 // Check if path exists
79                 if (isDirectory($this->fullPath)) {
80                         // Is there a .htaccess file?
81                         if (isFileReadable($this->fullPath . '.htaccess')) {
82                                 // All done!
83                                 $this->setStatusCode('done');
84                         } else {
85                                 // Stop! Set a .htaccess file first
86                                 $this->setStatusCode('htaccess');
87                         }
88                 } // END - if
89         }
90
91         // Checks validity of cache file and if content is given
92         function loadCacheFile ($cacheName) {
93                 // Remember cache file
94                 $this->name = $cacheName;
95
96                 // Construct FQFN (full qualified file name)
97                 $this->fqfn = $this->fullPath . $cacheName . $this->extension;
98
99                 // Check if file exists and if version matches
100                 if (!isset($this->status[$cacheName])) {
101                         // Pre-fetch cache here if found
102                         if ($this->isCacheReadable()) $this->getArrayFromCache();
103
104                         //* DEBUG: */ debugOutput('cacheName='.$cacheName.',isCacheReadable='.intval($this->isCacheReadable()).',is_writeable='.intval(is_writeable($this->fqfn)).',extensionMatches='.intval($this->extensionVersionMatches('cache')));
105                         $this->status[$cacheName] = ($this->isCacheReadable() && (is_writeable($this->fqfn)) && ($this->extensionVersionMatches('cache')));
106                 } // END - if
107                 //* DEBUG: */ debugOutput('cacheName='.$cacheName.',status='.intval($this->status[$cacheName]));
108
109                 // Return status
110                 return $this->status[$cacheName];
111         }
112
113         // Initializes the cache file
114         function init () {
115                 // This will destory an existing cache file!
116                 if ($this->getStatusCode() == 'done') {
117                         // Reset read status
118                         $this->resetCacheReadStatus();
119
120                         // Create file
121                         if ($this->isCacheReadable()) changeMode($this->fqfn, 0666);
122                         $this->pointer = fopen($this->fqfn, 'w') or debug_report_bug(__METHOD__, __LINE__, 'Cannot write to cache ' . $this->fqfn . ' !');
123
124                         // Add open PHP tag
125                         $this->writeLine('<?php');
126                 } else {
127                         // Cannot create file
128                         addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
129                 }
130         }
131
132         /**
133          * Writes a line to the pointer and adds a \n (new-line) to the end
134          *
135          * @access private
136          */
137         function writeLine ($line) {
138                 // Is the pointer a valid resource?
139                 if (is_resource($this->pointer)) {
140                         // Write the line
141                         fwrite($this->pointer, $line . "\n");
142                 } else {
143                         // Something bad happened
144                         debug_report_bug(__METHOD__, __LINE__, 'Pointer type is ' . gettype($this->pointer) . ', expected is resource.');
145                 }
146         }
147
148         // Reset the read status
149         function resetCacheReadStatus () {
150                 unset($this->readable[$this->name]);
151                 unset($GLOBALS['file_readable'][$this->fqfn]);
152                 unset($this->status[$this->name]);
153         }
154
155         function addRow ($data) {
156                 // Is the pointe rvalid?
157                 if (is_resource($this->pointer)) {
158                         // Write every array element to cache file
159                         foreach ($data as $k => $v) {
160                                 // Write global cache array for immediate access
161                                 if ((substr($k, 0, 4) == 'ext_') && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
162                                         if ($k == 'ext_name') {
163                                                 $GLOBALS['cache_array']['extension'][$k][$data['ext_id']] = $v;
164                                         } else {
165                                                 $GLOBALS['cache_array']['extension'][$k][$data['ext_name']] = $v;
166                                         }
167                                         if (($k == 'ext_keep') && ($v == 'Y')) {
168                                                 $GLOBALS['cache_array']['always_active'][$data['ext_name']] = $v;
169                                         } // END - if
170                                 } elseif ($this->name == 'config') {
171                                         // Configuration
172                                         $GLOBALS['cache_array']['config'][$data['config']][$k] = $v;
173                                 } elseif ($this->name == 'filter') {
174                                         // Filter
175                                         $GLOBALS['cache_array']['filter']['chains'][$data['filter_name']][$data['filter_function']] = $data['filter_active'];
176                                         $GLOBALS['cache_array']['filter']['counter'][$data['filter_name']][$data['filter_function']] = $data['filter_counter'];
177                                         $GLOBALS['cache_array']['filter']['loaded'][$data['filter_name']][$data['filter_function']] = true;
178                                 } elseif ($this->name == 'modules') {
179                                         // Modules
180                                         $GLOBALS['cache_array']['modules'][$k][$data['module']] = $v;
181                                 } elseif ($this->name == 'admin') {
182                                         // Admin logins
183                                         if ($k == 'admin_id') {
184                                                 $GLOBALS['cache_array']['admin'][$k][$data['login']] = $v;
185                                         } else {
186                                                 $GLOBALS['cache_array']['admin'][$k][$data['admin_id']] = $v;
187                                         }
188                                 } elseif ($this->name == 'admin_acls') {
189                                         // Access control lines
190                                         $GLOBALS['cache_array']['admin_acls'][$k][$data['admin_id']][] = $v;
191                                 } elseif ($this->name == 'refdepths') {
192                                         // Referal levels
193                                         $GLOBALS['cache_array']['refdepths'][$k][$data['id']] = $v;
194                                 } elseif ($this->name == 'refsystem') {
195                                         // Referal system
196                                         $GLOBALS['cache_array']['refsystem'][$k][$data['id']] = $v;
197                                 } elseif ($this->name == 'revision') {
198                                         // Revision data
199                                         $GLOBALS['cache_array']['revision'][$k][0] = $v;
200                                 } elseif ($this->name == 'themes') {
201                                         // Themes
202                                         if ($k == 'theme_path') {
203                                                 $GLOBALS['cache_array']['themes'][$k][$data['id']] = $v;
204                                         } else {
205                                                 $GLOBALS['cache_array']['themes'][$k][$data['theme_path']] = $v;
206                                         }
207                                 } elseif ($this->name == 'imprint') {
208                                         // Imprint
209                                         $GLOBALS['cache_array']['imprint'][$k][$data['imprint_id']] = $v;
210                                 } elseif (is_array($v)) {
211                                         // Serialize and BASE64-encode the array
212                                         $v = base64_encode(serialize($v));
213                                 } else {
214                                         // Finialize the cache and close it
215                                         $this->finalize();
216
217                                         // Remove cache
218                                         $this->removeCacheFile(true);
219
220                                         // Unsupported/unhandled cache detected
221                                         debug_report_bug(__METHOD__, __LINE__, 'Unsupported cache ' . $this->name . ' detected.');
222                                 }
223
224                                 // Write cache line to file
225                                 $this->writeLine($this->rewriteEntry($k, $v));
226                         } // END - foreach
227                 } else {
228                         // Cannot create file
229                         addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
230                 }
231         }
232
233         function finalize () {
234                 // Quit function when no pointer is set
235                 if (is_resource($this->pointer)) {
236                         // Add default depency
237                         $this->storeExtensionVersion('cache');
238
239                         // Write footer
240                         $this->writeLine('?>');
241
242                         // Close file add destroy handler
243                         fclose($this->pointer);
244
245                         // Reset readable status
246                         $this->resetCacheReadStatus();
247
248                         // Set rights
249                         if ($this->isCacheReadable()) changeMode($this->fqfn, 0666);
250
251                         // Remove pointer and status
252                         unset($this->status[$this->name]);
253                         $this->pointer = false;
254                         //* DEBUG: */ debugOutput(__METHOD__ . '(<font color="#0000aa">' . __LINE__.'</font>): '.$this->name.' - FINALIZED!');
255                 } // END - if
256         }
257
258         function getArrayFromCache () {
259                 // Is the cache already loaded?
260                 if (isset($this->data[$this->name])) {
261                         // Return it's content!
262                         return $this->data[$this->name];
263                 } // END - if
264
265                 // Is the cache file there?
266                 if ($this->isCacheReadable()) {
267                         // Load cache file
268                         include($this->fqfn);
269
270                         // Is there an array?
271                         if (isset($this->data[$this->name])) {
272                                 // Cache version found?
273                                 if (isset($this->version[$this->name])) {
274                                         // Check it here if cache matches ext-cache version
275                                         if (!$this->extensionVersionMatches('cache')) {
276                                                 // Invalid
277                                                 logDebugMessage(__METHOD__, __LINE__, 'Invalid cache data ' . $this->name . ' detected.');
278                                                 $this->removeCacheFile();
279                                         } // END - if
280                                 } // END - if
281
282                                 // Return cache if detected
283                                 if (isset($this->data[$this->name])) {
284                                         return $this->data[$this->name];
285                                 } else {
286                                         // Damaged!
287                                         logDebugMessage(__METHOD__, __LINE__, 'Possible damaged cache ' . $this->name . ' detected.');
288                                         $this->removeCacheFile();
289                                 }
290                         } // END - if
291                 } else {
292                         // Cache file not found or not readable
293                         debug_report_bug(__METHOD__, __LINE__, $this->name);
294                         addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): ' . getMaskedMessage('CACHE_CANNOT_LOAD', $this->fqfn));
295
296                         // Try to remove it
297                         $this->removeCacheFile();
298                 }
299
300                 // Always return an empty array if we have trouble or no data
301                 return array();
302         }
303
304         // Destroy an existing cache file
305         function removeCacheFile ($force = false) {
306                 // Reset read status
307                 $this->resetCacheReadStatus();
308
309                 // Debug message
310                 //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("%s should be removed.", $this->name));
311
312                 // Is the cache file not yet rebuilt?
313                 if ((!isset($this->rebuilt[$this->name])) && ($this->isCacheReadable())) {
314                         // Only run in regular output mode
315                         if ((!isHtmlOutputMode()) && ($force === false)) {
316                                 // Debug message if allowed
317                                 if (isDebugModeEnabled()) {
318                                         // Debug message
319                                         logDebugMessage(__METHOD__, __LINE__, 'Not removing cache ' . $this->name . ' in output_mode=' . getScriptOutputMode());
320                                 } // END - if
321
322                                 // Abort here
323                                 return;
324                         } // END - if
325
326                         // Close cache
327                         $this->finalize();
328
329                         // Debug-mode enabled?
330                         if (isDebugModeEnabled()) {
331                                 // Log removal of cache
332                                 logDebugMessage(__METHOD__, __LINE__, 'removing cache: ' . $this->name);
333                         } // END - if
334
335                         // Remove cache file from system
336                         //* DEBUG: */ debug_report_bug(__METHOD__, __LINE__, 'About to remove ' . basename($this->fqfn) . '!');
337                         removeFile($this->fqfn);
338
339                         // Reset read status
340                         $this->resetCacheReadStatus();
341
342                         // Is the file there?
343                         if (!$this->isCacheReadable()) {
344                                 // The cache does no longer exist so kill the content
345                                 unset($this->data[$this->name]);
346                                 unset($this->version[$this->name]);
347                                 $this->rebuilt[$this->name] = true;
348                         } else {
349                                 // Not removed!
350                                 addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): ' . getMaskedMessage('CACHE_CANNOT_UNLINK', $this->fqfn));
351                         }
352                 } // END - if
353         }
354
355         // Unused method:
356         function removeEntry ($search, $data, $array) {
357                 if ($this->status[$this->name] == 'done') {
358                         // Load cache into dummy array
359                         $dummy = $this->getArrayFromCache();
360
361                         // Search for key in array
362                         $key = array_search($data, $dummy[$search]);
363                         if (!empty($key)) {
364                                 // Key (hopefully) found?
365                                 foreach ($array as $a) {
366                                         // So we can remove all elements as requested
367                                         unset($dummy[$a][$key]);
368                                 } // END - foreach
369
370                                 // Flush array to cache file
371                                 $this->init();
372
373                                 // Write array out
374                                 $this->writeArray($dummy);
375
376                                 // Close cache file
377                                 $this->finalize();
378                         }
379                 } else {
380                         // Cannot write to cache!
381                         addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
382                 }
383         }
384
385         function writeArray ($array) {
386                 if (is_resource($this->pointer)) {
387                         foreach ($array as $k => $v) {
388                                 if (is_array($v)) {
389                                         // Multi line(s) found
390                                         $LINE = '';
391                                         foreach ($v as $k2 => $v2) {
392                                                 // Put every array element in a row...
393                                                 $LINE .= $this->rewriteEntry($k, $v2);
394                                         } // END - foreach
395                                 } else {
396                                         // Single line found
397                                         $LINE = $this->rewriteEntry($k, $v);
398                                 }
399
400                                 // Write line(s)
401                                 $this->writeLine($LINE);
402                         } // END - foreach
403                 } else {
404                         // Cannot write array!
405                         addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
406                 }
407         }
408
409         // Unused method
410         function replaceEntry ($search, $replace, $search_key, $array) {
411                 if ($this->status[$this->name] == 'done') {
412                         // Load cache into dummy array
413                         $dummy = $this->getArrayFromCache();
414
415                         // Check if $dummy is valid (prevents some errors)
416                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
417                                 // Search for key in array
418                                 $key_found = array_key_exists($search_key, $dummy[$search]);
419                                 if ($key_found == true) {
420                                         $key = $search_key;
421                                         // Key (hopefully) found?
422                                         foreach ($dummy as $a => $v) {
423                                                 // So we can update all entries
424                                                 if ($a == $search) {
425                                                         // Update now...
426                                                         $dummy[$a][$search_key] = $replace;
427                                                 } // END - if
428                                         } // END - foreach
429
430                                         // Flush array to cache file
431                                         $this->init();
432
433                                         // Write array out
434                                         $this->writeArray($dummy);
435
436                                         // Close cache file
437                                         $this->finalize();
438                                 } // END - if
439                         } // END - if
440                 } else {
441                         // Cannot write to cache!
442                         addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
443                 }
444         }
445
446         // Writes the version of given extension to the cache file
447         function storeExtensionVersion ($ext_name) {
448                 // Valid cache pointer?
449                 if (is_resource($this->pointer)) {
450                         // Write only if extension is installed
451                         if (isExtensionInstalled($ext_name)) {
452                                 // Get extension version
453                                 $ext_ver = getExtensionVersion($ext_name);
454
455                                 // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
456                                 $this->version[$this->name][$ext_name] = $ext_ver;
457
458                                 // Write cache line to file
459                                 $this->writeLine($this->rewriteEntry($ext_name, $ext_ver, 'version', true));
460                         } // END - if
461                         //* DEBUG: */ debugOutput(__METHOD__ . '(<font color="#0000aa">' . __LINE__ . '</font>): '.$this->name.' - '.$ext_name.'='.$ext_ver);
462                 } else {
463                         // Cannot create file
464                         addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
465                 }
466         }
467
468         // Checks wether versions from cache and extension matches
469         function extensionVersionMatches ($ext_name) {
470                 // Check cache
471                 if (!isset($GLOBALS[__METHOD__][$ext_name])) {
472                         // Does never match by default
473                         $GLOBALS[__METHOD__][$ext_name] = false;
474
475                         // Compare only if installed
476                         if (isExtensionInstalled($ext_name)) {
477                                 // Get extension version
478                                 $ext_ver = getExtensionVersion($ext_name);
479
480                                 // Debug messages
481                                 if (isset($this->version[$this->name][$ext_name])) {
482                                         // Does it match?
483                                         $GLOBALS[__METHOD__][$ext_name] = ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
484                                 } elseif ($this->isCacheReadable()) {
485                                         // No cache version found
486                                         logDebugMessage(__METHOD__, __LINE__, 'Cache ' . $this->name . ' has missing version entry for extension ' . $ext_name . '! Purging cache...');
487         
488                                         // Remove the cache file
489                                         $this->removeCacheFile(true);
490                                 }
491                         } else {
492                                 // Not installed, does always match
493                                 $GLOBALS[__METHOD__][$ext_name] = true;
494                         }
495                 } else {
496                         // Cache entry found, log debug message
497                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, 'ext_name=' . $ext_name . ', matches=' . intval($GLOBALS[__METHOD__][$ext_name]));
498                 }
499
500                 // Compare both
501                 return $GLOBALS[__METHOD__][$ext_name];
502         }
503
504         // Rewrit the entry so it can be stored in cache file
505         // @TODO Add support for more types which break in last else-block
506         function rewriteEntry ($key, $value, $prefix = 'data', $single = false) {
507                 // Default is not single entry
508                 $extender = '[]';
509
510                 // Add only for single array entry?
511                 if ($single === true) {
512                         $extender = '';
513                 } // END - if
514
515                 // Init line
516                 $line = '';
517
518                 // String or non-string? ;-)
519                 if (is_string($value)) {
520                         // String...
521                         $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = '" . escapeQuotes($value) . "';";
522                 } elseif (is_null($value)) {
523                         // Null
524                         $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = null;';
525                 } elseif (is_bool($value)) {
526                         // Boolean value
527                         if ($value === true) {
528                                 // True
529                                 $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = true;';
530                         } else {
531                                 // False
532                                 $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = false;';
533                         }
534                 } elseif (isset($value[0])) {
535                         // These lines needs fixing
536                         debug_report_bug(__METHOD__, __LINE__, 'Invalid entry with [0] found. key=' . $key);
537                 } else {
538                         // Non-string
539                         $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = ' . $value . ';';
540                 }
541
542                 // Return line
543                 return $line;
544         }
545
546         // Getter for cache status code
547         function getStatusCode () {
548                 return $this->statusCode;
549         }
550
551         // Setter for cache status code
552         function setStatusCode ($status) {
553                 $this->statusCode = $status;
554         }
555
556         // Checks wether the current cache file is readable
557         function isCacheReadable () {
558                 // Array entry found?
559                 if (!isset($this->readable[$this->name])) {
560                         // Not found, so create it
561                         $this->readable[$this->name] = isFileReadable($this->fqfn);
562                 } // END - if
563
564                 // Return result
565                 return $this->readable[$this->name];
566         }
567
568         // Cloning not allowed
569         function __clone () {
570                 // Please do not clone this class
571                 debug_report_bug(__METHOD__, __LINE__, 'Cloning of this class is not allowed.');
572         }
573 } // END - class
574
575 // [EOF]
576 ?>