Naming convention applied
[mailer.git] / inc / classes / cachesystem.class.php
index a8223aa640d60dabaf3d4e8ac73859c910011cbc..587a5281a14b8ad033f1a6c2c1050d63948bdf27 100644 (file)
  * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
  * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -43,35 +42,46 @@ if (!defined('__SECURITY')) {
 
 // Caching class
 class CacheSystem {
-       // Define variables
-       var $ret = 'init';
+       // Status code
+       var $statusCode = 'init';
+
+       // Full-qualified filename
        var $fqfn = '';
+
+       // Resource to cache file
        var $pointer = false;
+
+       // Data array from cache
        var $data = array();
+
+       // Version data from cache
        var $version = array();
+
+       // Cache name
        var $name = '';
        var $rebuilt = array();
+
+       // File extension
        var $extension = '.cache';
-       var $statusDone = 'done';
        var $status = array();
        var $readable = array();
 
        // Constructor
        function CacheSystem () {
                // Failed is the default
-               $this->ret = 'failed';
+               $this->setStatusCode('failed');
 
                // Remeber path
 
                // Check if path exists
-               if (isDirectory(getConfig('CACHE_PATH'))) {
+               if (isDirectory(getCachePath())) {
                        // Is there a .htaccess file?
-                       if (isFileReadable(getConfig('CACHE_PATH') . '.htaccess')) {
+                       if (isFileReadable(getCachePath() . '.htaccess')) {
                                // All done!
-                               $this->ret = $this->statusDone;
+                               $this->setStatusCode('done');
                        } else {
                                // Stop! Set a .htaccess file first
-                               $this->ret = 'htaccess';
+                               $this->setStatusCode('htaccess');
                        }
                } // END - if
        }
@@ -82,17 +92,17 @@ class CacheSystem {
                $this->name = $cacheName;
 
                // Construct FQFN (full qualified file name)
-               $this->fqfn = getConfig('CACHE_PATH') . $cacheName . $this->extension;
+               $this->fqfn = getCachePath() . $cacheName . $this->extension;
 
                // Check if file exists and if version matches
                if (!isset($this->status[$cacheName])) {
                        // Pre-fetch cache here if found
                        if ($this->isCacheReadable()) $this->getArrayFromCache();
 
-                       //* DEBUG: */ print($cacheName.'='.intval($this->isCacheReadable()).'/'.intval(is_writeable($this->fqfn)).'/'.intval($this->extensionVersionMatches('cache')).'<br />');
+                       //* DEBUG: */ debugOutput('cacheName='.$cacheName.',isCacheReadable='.intval($this->isCacheReadable()).',is_writeable='.intval(is_writeable($this->fqfn)).',extensionMatches='.intval($this->extensionVersionMatches('cache')));
                        $this->status[$cacheName] = ($this->isCacheReadable() && (is_writeable($this->fqfn)) && ($this->extensionVersionMatches('cache')));
                } // END - if
-               //* DEBUG: */ print($cacheName.'='.intval($this->status[$cacheName]).'<br />');
+               //* DEBUG: */ debugOutput('cacheName='.$cacheName.',status='.intval($this->status[$cacheName]));
 
                // Return status
                return $this->status[$cacheName];
@@ -101,19 +111,35 @@ class CacheSystem {
        // Initializes the cache file
        function init () {
                // This will destory an existing cache file!
-               if ($this->ret == $this->statusDone) {
+               if ($this->getStatusCode() == 'done') {
                        // Reset read status
                        $this->resetCacheReadStatus();
 
                        // Create file
                        if ($this->isCacheReadable()) changeMode($this->fqfn, 0666);
-                       $this->pointer = fopen($this->fqfn, 'w') or app_die(__METHOD__, __LINE__, "Cannot write to cache ".$this->fqfn." !");
+                       $this->pointer = fopen($this->fqfn, 'w') or debug_report_bug(__METHOD__, __LINE__, 'Cannot write to cache ' . $this->fqfn . ' !');
 
                        // Add open PHP tag
-                       fwrite($this->pointer, "<?php\n");
+                       $this->writeLine('<?php');
                } else {
                        // Cannot create file
-                       addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
+                       addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
+               }
+       }
+
+       /**
+        * Writes a line to the pointer and adds a \n (new-line) to the end
+        *
+        * @access private
+        */
+       function writeLine ($line) {
+               // Is the pointer a valid resource?
+               if (is_resource($this->pointer)) {
+                       // Write the line
+                       fwrite($this->pointer, $line . "\n");
+               } else {
+                       // Something bad happened
+                       debug_report_bug(__METHOD__, __LINE__, 'Pointer type is ' . gettype($this->pointer) . ', expected is resource.');
                }
        }
 
@@ -139,9 +165,6 @@ class CacheSystem {
                                        if (($k == 'ext_keep') && ($v == 'Y')) {
                                                $GLOBALS['cache_array']['always_active'][$data['ext_name']] = $v;
                                        } // END - if
-                               } elseif (is_array($v)) {
-                                       // Serialize and BASE64-encode the array
-                                       $v = base64_encode(serialize($v));
                                } elseif ($this->name == 'config') {
                                        // Configuration
                                        $GLOBALS['cache_array']['config'][$data['config']][$k] = $v;
@@ -154,11 +177,11 @@ class CacheSystem {
                                        // Modules
                                        $GLOBALS['cache_array']['modules'][$k][$data['module']] = $v;
                                } elseif ($this->name == 'admin') {
-                                       // Modules
-                                       if ($k == 'login') {
-                                               $GLOBALS['cache_array']['admin'][$k][$data['admin_id']] = $v;
-                                       } else {
+                                       // Admin logins
+                                       if ($k == 'admin_id') {
                                                $GLOBALS['cache_array']['admin'][$k][$data['login']] = $v;
+                                       } else {
+                                               $GLOBALS['cache_array']['admin'][$k][$data['admin_id']] = $v;
                                        }
                                } elseif ($this->name == 'refdepths') {
                                        // Referal levels
@@ -168,7 +191,7 @@ class CacheSystem {
                                        $GLOBALS['cache_array']['refsystem'][$k][$data['id']] = $v;
                                } elseif ($this->name == 'revision') {
                                        // Revision data
-                                       $GLOBALS['cache_array']['revision'][$k] = $v;
+                                       $GLOBALS['cache_array']['revision'][$k][0] = $v;
                                } elseif ($this->name == 'themes') {
                                        // Themes
                                        if ($k == 'theme_path') {
@@ -176,6 +199,12 @@ class CacheSystem {
                                        } else {
                                                $GLOBALS['cache_array']['themes'][$k][$data['theme_path']] = $v;
                                        }
+                               } elseif ($this->name == 'imprint') {
+                                       // Imprint
+                                       $GLOBALS['cache_array']['imprint'][$k][$data['imprint_id']] = $v;
+                               } elseif (is_array($v)) {
+                                       // Serialize and BASE64-encode the array
+                                       $v = base64_encode(serialize($v));
                                } else {
                                        // Finialize the cache and close it
                                        $this->finalize();
@@ -184,15 +213,15 @@ class CacheSystem {
                                        $this->removeCacheFile(true);
 
                                        // Unsupported cache found!
-                                       debug_report_bug('Unsupported cache ' . $this->name . ' detected.');
+                                       debug_report_bug(__METHOD__, __LINE__, 'Unsupported cache ' . $this->name . ' detected.');
                                }
 
                                // Write cache line to file
-                               fwrite($this->pointer, $this->rewriteEntry($k, $v));
+                               $this->writeLine($this->rewriteEntry($k, $v));
                        } // END - foreach
                } else {
                        // Cannot create file
-                       addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
+                       addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
                }
        }
 
@@ -203,7 +232,7 @@ class CacheSystem {
                        $this->storeExtensionVersion('cache');
 
                        // Write footer
-                       fwrite($this->pointer, "?>\n");
+                       $this->writeLine('?>');
 
                        // Close file add destroy handler
                        fclose($this->pointer);
@@ -217,7 +246,7 @@ class CacheSystem {
                        // Remove pointer and status
                        unset($this->status[$this->name]);
                        $this->pointer = false;
-                       //* DEBUG: */ outputHtml(__METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />");
+                       //* DEBUG: */ debugOutput(__METHOD__ . '(<font color="#0000aa">' . __LINE__.'</font>): '.$this->name.' - FINALIZED!');
                } // END - if
        }
 
@@ -256,8 +285,8 @@ class CacheSystem {
                        } // END - if
                } else {
                        // Cache file not found or not readable
-                       debug_report_bug($this->name);
-                       addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMaskedMessage('CACHE_CANNOT_LOAD', $this->fqfn));
+                       debug_report_bug(__METHOD__, __LINE__, $this->name);
+                       addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): ' . getMaskedMessage('CACHE_CANNOT_LOAD', $this->fqfn));
 
                        // Try to remove it
                        $this->removeCacheFile();
@@ -273,16 +302,16 @@ class CacheSystem {
                $this->resetCacheReadStatus();
 
                // Debug message
-               /* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("%s should be removed.", $this->name));
+               //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("%s should be removed.", $this->name));
 
                // Is the cache file not yet rebuilt?
                if ((!isset($this->rebuilt[$this->name])) && ($this->isCacheReadable())) {
                        // Only run in regular output mode
-                       if ((getOutputMode() != 0) && ($force === false)) {
+                       if ((!isHtmlOutputMode()) && ($force === false)) {
                                // Debug message if allowed
                                if (isDebugModeEnabled()) {
                                        // Debug message
-                                       logDebugMessage(__METHOD__, __LINE__, 'Not removing cache ' . $this->name . ' in output_mode=' . getOutputMode());
+                                       logDebugMessage(__METHOD__, __LINE__, 'Not removing cache ' . $this->name . ' in output_mode=' . getScriptOutputMode());
                                } // END - if
 
                                // Abort here
@@ -312,14 +341,14 @@ class CacheSystem {
                                $this->rebuilt[$this->name] = true;
                        } else {
                                // Not removed!
-                               addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMaskedMessage('CACHE_CANNOT_UNLINK', $this->fqfn));
+                               addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): ' . getMaskedMessage('CACHE_CANNOT_UNLINK', $this->fqfn));
                        }
                } // END - if
        }
 
        // Unused method:
        function removeEntry ($search, $data, $array) {
-               if ($this->status[$this->name] == $this->statusDone) {
+               if ($this->status[$this->name] == 'done') {
                        // Load cache into dummy array
                        $dummy = $this->getArrayFromCache();
 
@@ -343,7 +372,7 @@ class CacheSystem {
                        }
                } else {
                        // Cannot write to cache!
-                       addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
+                       addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
                }
        }
 
@@ -356,24 +385,24 @@ class CacheSystem {
                                        foreach($v as $k2 => $v2) {
                                                // Put every array element in a row...
                                                $LINE .= $this->rewriteEntry($k, $v2);
-                                       }
+                                       } // END - foreach
                                } else {
                                        // Single line found
                                        $LINE = $this->rewriteEntry($k, $v);
                                }
 
                                // Write line(s)
-                               fwrite($this->pointer, $LINE);
+                               $this->writeLine($LINE);
                        } // END - foreach
                } else {
                        // Cannot write array!
-                       addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
+                       addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
                }
        }
 
        // Unused method
        function replaceEntry ($search, $replace, $search_key, $array) {
-               if ($this->status[$this->name] == $this->statusDone) {
+               if ($this->status[$this->name] == 'done') {
                        // Load cache into dummy array
                        $dummy = $this->getArrayFromCache();
 
@@ -404,7 +433,7 @@ class CacheSystem {
                        } // END - if
                } else {
                        // Cannot write to cache!
-                       addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
+                       addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
                }
        }
 
@@ -417,47 +446,53 @@ class CacheSystem {
                                // Get extension version
                                $ext_ver = getExtensionVersion($ext_name);
 
-                               // Write cache line to file
-                               fwrite($this->pointer, $this->rewriteEntry($ext_name, $ext_ver, 'version', true));
-
                                // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
                                $this->version[$this->name][$ext_name] = $ext_ver;
+
+                               // Write cache line to file
+                               $this->writeLine($this->rewriteEntry($ext_name, $ext_ver, 'version', true));
                        } // END - if
-                       //* DEBUG: */ outputHtml(__METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />");
+                       //* DEBUG: */ debugOutput(__METHOD__ . '(<font color="#0000aa">' . __LINE__ . '</font>): '.$this->name.' - '.$ext_name.'='.$ext_ver);
                } else {
                        // Cannot create file
-                       addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
+                       addFatalMessage(__METHOD__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--CACHE_PROBLEMS_DETECTED');
                }
        }
 
        // Checks wether versions from cache and extension matches
        function extensionVersionMatches ($ext_name) {
-               // Default is not matching
-               $matches = false;
-
-               // Compare only if installed
-               if (isExtensionInstalled($ext_name)) {
-                       // Get extension version
-                       $ext_ver = getExtensionVersion($ext_name);
-
-                       // Debug messages
-                       if (isset($this->version[$this->name][$ext_name])) {
-                               // Does it match?
-                               $matches = ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
-                       } elseif ($this->isCacheReadable()) {
-                               // No cache version found!
-                               logDebugMessage(__METHOD__, __LINE__, "Cache {$this->name} has missing version entry for extension {$ext_name}! Purging cache...");
-
-                               // Remove the cache file
-                               $this->removeCacheFile(true);
+               // Check cache
+               if (!isset($GLOBALS[__METHOD__][$ext_name])) {
+                       // Does never match by default
+                       $GLOBALS[__METHOD__][$ext_name] = false;
+
+                       // Compare only if installed
+                       if (isExtensionInstalled($ext_name)) {
+                               // Get extension version
+                               $ext_ver = getExtensionVersion($ext_name);
+
+                               // Debug messages
+                               if (isset($this->version[$this->name][$ext_name])) {
+                                       // Does it match?
+                                       $GLOBALS[__METHOD__][$ext_name] = ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
+                               } elseif ($this->isCacheReadable()) {
+                                       // No cache version found!
+                                       logDebugMessage(__METHOD__, __LINE__, 'Cache ' . $this->name . ' has missing version entry for extension ' . $ext_name . '! Purging cache...');
+       
+                                       // Remove the cache file
+                                       $this->removeCacheFile(true);
+                               }
+                       } else {
+                               // Not installed, does always match
+                               $GLOBALS[__METHOD__][$ext_name] = true;
                        }
                } else {
-                       // Not installed, does always match
-                       $matches = true;
+                       // Cache entry found, log debug message
+                       //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, 'ext_name=' . $ext_name . ', matches=' . intval($GLOBALS[__METHOD__][$ext_name]));
                }
 
                // Compare both
-               return $matches;
+               return $GLOBALS[__METHOD__][$ext_name];
        }
 
        // Rewrit the entry so it can be stored in cache file
@@ -467,7 +502,9 @@ class CacheSystem {
                $extender = '[]';
 
                // Add only for single array entry?
-               if ($single === true) $extender = '';
+               if ($single === true) {
+                       $extender = '';
+               } // END - if
 
                // Init line
                $line = '';
@@ -475,31 +512,39 @@ class CacheSystem {
                // String or non-string? ;-)
                if (is_string($value)) {
                        // String...
-                       $line = '$this->' . $prefix . "['".$this->name."']['" . $key . "']" . $extender . " = '" . escapeQuotes($value) . "';\n";
+                       $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = '" . escapeQuotes($value) . "';";
                } elseif (is_null($value)) {
                        // Null
-                       $line = '$this->' . $prefix . "['".$this->name."']['" . $key . "']" . $extender . " = null;\n";
+                       $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = null;';
                } elseif (is_bool($value)) {
                        // Boolean value
                        if ($value === true) {
                                // True
-                               $line = '$this->' . $prefix . "['".$this->name."']['" . $key . "']" . $extender . " = true;\n";
+                               $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = true;';
                        } else {
                                // False
-                               $line = '$this->' . $prefix . "['".$this->name."']['" . $key . "']" . $extender . " = false;\n";
+                               $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = false;';
                        }
+               } elseif (isset($value[0])) {
+                       // These lines needs fixing
+                       debug_report_bug(__METHOD__, __LINE__, 'Invalid entry with [0] found. key=' . $key);
                } else {
                        // Non-string
-                       $line = '$this->' . $prefix . "['".$this->name."']['" . $key . "']" . $extender . " = " . $value . ";\n";
+                       $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = ' . $value . ';';
                }
 
                // Return line
                return $line;
        }
 
-       // Getter for cache status
-       function getStatus () {
-               return $this->ret;
+       // Getter for cache status code
+       function getStatusCode () {
+               return $this->statusCode;
+       }
+
+       // Setter for cache status code
+       function setStatusCode ($status) {
+               $this->statusCode = $status;
        }
 
        // Checks wether the current cache file is readable
@@ -514,6 +559,11 @@ class CacheSystem {
                return $this->readable[$this->name];
        }
 
+       // Cloning not allowed
+       function __clone () {
+               // Please do not clone this class
+               debug_report_bug(__METHOD__, __LINE__, 'Cloning of this class is not allowed.');
+       }
 } // END - class
 
 // [EOF]