Cache class rewritten to better convention
[mailer.git] / inc / libs / cache_functions.php
index e0c3fec20cd0b42090b05bc01ce33b78f94ffb07..611073a72ae89cba3603f1129bd3f217afc0c64b 100644 (file)
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
-{
+if (!defined('__SECURITY')) {
        $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
        require($INC);
 }
+
 // Caching class
-class mxchange_cache
-{
+class CacheSystem {
        // Define variables
-       var $update_interval = 0;
        var $ret = "init";
-       var $cache_path = "";
-       var $cache_inc = "";
-       var $cache_ctime = 0;
-       var $cache_pointer = false;
+       var $path = "";
+       var $inc = "";
+       var $pointer = false;
+       var $data = "";
+       var $version = "";
+       var $name = "";
 
        // Constructor
-       function mxchange_cache($interval, $path, $tested) {
-               // Remember interval in class
-               $this->update_interval=$interval;
+       function CacheSystem ($interval, $path, $tested) {
+               // Failed is the default
+               $this->ret = "failed";
 
                // Remeber path
-               $this->cache_path=$path;
+               $this->path = $path;
 
                // Check if path exists
                if ((is_dir($path)) && (!$tested)) {
                        // Check if we can create a file inside the path
-                       @touch($path."dummy.tmp", 'w');
+                       touch($path."dummy.tmp", 'w');
                        if (FILE_READABLE($path."dummy.tmp")) {
                                // Yes, we can do. So let's remove it
-                               @unlink($path."dummy.tmp");
+                               unlink($path."dummy.tmp");
 
                                // Is there a .htaccess file?
                                if (FILE_READABLE($path.".htaccess")) {
@@ -70,77 +70,55 @@ class mxchange_cache
                                        UPDATE_CONFIG("cache_tested", 1);
 
                                        // All done!
-                                       return "done";
+                                       $this->ret = "done";
                                } else {
                                        // Stop! Set a .htaccess file first
-                                       $this->ret="htaccess";
-                                       return "htaccess";
+                                       $this->ret = "htaccess";
                                }
                        }
                } elseif ($tested) {
                        // System already tested
-                       $this->ret="done";
-                       return "done";
+                       $this->ret = "done";
                }
-
-               // Something goes wrong here!
-               $this->ret="failed";
-               return "failed";
        }
 
-       function cache_file($file, $ignore_ctime=false) {
-               global $INC;
-               // Construct FQFN (full qualified file name)
-               $inc = $this->cache_path.$file.".cache";
+       // Checks validity of cache file and if content is given
+       function loadCacheFile ($file, $forceContent = false) {
+               // Remember cache file
+               $this->name = $file;
 
-               // Rember it + filename in class
-               $this->cache_inc = $inc;
-
-               // Check if file exists
-               $status = (FILE_READABLE($inc) && (is_writeable($inc)));
-               if ($status) {
-                       // Yes, it does. So let's get it's last changed date/time
-                       $ctime = filectime($inc);
-               } else {
-                       // No, it doesn't. Zero date/time
-                       $ctime = "0";
-               }
-
-               // Remember change date/time in class
-               $this->cache_ctime = $ctime;
+               // Construct FQFN (full qualified file name)
+               $this->inc = $this->path.$file.".cache";
 
-               // Is the cache file outdated?
-               if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime)) {
-                       // Ok, we need an update!
-                       $status = false;
-               }
+               // Check if file exists and if version matches
+               $status = (FILE_READABLE($this->inc) && (is_writeable($this->inc)) && ($this->extensionVersionMatches("cache")));
 
                // Return status
                return $status;
        }
 
-       function cache_init($array) {
+       function init () {
                // This will destory an existing cache file!
                if ($this->ret == "done") {
                        // Create file
-                       if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
-                       $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
+                       if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
+                       $this->pointer = fopen($this->inc, 'w') or mxchange_die("Cannot write to cache ".$this->inc." !");
 
-                       // Begin of cache file
-                       fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
+                       // Add open PHP tag
+                       fwrite($this->pointer, "<?php\n");
 
-                       // Remember file pointer
-                       $this->cache_pointer = $fp;
+                       // Add default depency
+                       $this->storeExtensionVersion("cache");
                } else {
                        // Cannot create file
-                       ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
                }
        }
 
-       function add_row ($data) {
+       function addRow ($data) {
                global $cacheArray;
 
-               if (is_resource($this->cache_pointer)) {
+               if (is_resource($this->pointer)) {
                        // Write every array element to cache file
                        foreach ($data as $k => $v) {
                                // Write global cache array for immediate access
@@ -153,206 +131,254 @@ class mxchange_cache
                                        if (($k == "ext_keep") && ($v == "Y")) {
                                                $cacheArray['active_extensions'][$data['ext_name']] = $v;
                                        } // END - if
-                               } // END - if
+                               } elseif (is_array($v)) {
+                                       // Serialize and BASE64-encode the array
+                                       $v = base64_encode(serialize($v));
+                               }
 
                                // Write cache line to file
-                               @fwrite($this->cache_pointer, "\$data['".$k."'][] = \"".$v."\";\n");
+                               fwrite($this->pointer, $this->rewriteEntry($k, $v));
                        }
                } else {
                        // Cannot create file
-                       ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
                }
        }
 
-       function cache_close()
-       {
+       function finalize () {
                // Quit function when no pointer is set
-               if (empty($this->cache_pointer)) return;
-               if ($this->cache_pointer)
-               {
+               if (is_resource($this->pointer)) {
+                       // Write footer
+                       fwrite($this->pointer, "?>\n");
+
                        // Close file add destroy handler
-                       @fclose($this->cache_pointer);
+                       fclose($this->pointer);
 
                        // Set rights
-                       if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
+                       if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
 
                        // Remove pointer
-                       unset($this->cache_pointer);
-               }
-               else
-               {
+                       $this->pointer = false;
+               } else {
                        // Cannot create file
-                       ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
                }
        }
 
-       function cache_load()
-       {
-               if (FILE_READABLE($this->cache_inc)) {
+       function getArrayFromCache () {
+               // Is the cache already loaded?
+               if (isset($this->data[$this->name])) {
+                       // Return it's content!
+                       return $this->data[$this->name];
+               } // END - if
+
+               // Is the cache file there?
+               if (FILE_READABLE($this->inc)) {
                        // Prepare temporary array
                        $data = array();
+                       $cache_version = null;
 
                        // Load cache file
-                       $cache = implode("", file($this->cache_inc));
+                       require_once($this->inc);
 
-                       // Execute cache file
-                       eval($cache);
+                       // Is there an array?
                        if (is_array($data)) {
+                               // Cache data
+                               $this->data[$this->name] = $data;
+
+                               // Cache version found?
+                               if ((is_array($cache_version)) && (count($cache_version) > 0)) {
+                                       // Remember it as well...
+                                       $this->version[$this->name] = $cache_version;
+                               } else {
+                                       // Invalid cache so destroy it
+                                       $this->destroyCacheFile();
+
+                                       // Clear cached data
+                                       $this->data[$this->name] = array();
+                               }
+
                                // Return cache
-                               return $data;
+                               return $this->data[$this->name];
                        } else {
                                // Cache problem detected!
-                               $this->cache_destroy();
+                               $this->destroyCacheFile();
                        }
-               }
-               else
-               {
+               } else {
                        // Cache file not found or not readable
-                       ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->inc.CACHE_CANNOT_LOAD_2);
                }
        }
 
-       function cache_destroy()
-       {
-               if (FILE_READABLE($this->cache_inc))
-               {
+       // Destroy an existing cache file
+       function destroyCacheFile () {
+               // Is the cache file there?
+               if (FILE_READABLE($this->inc)) {
+                       // Close cache
+                       $this->finalize();
+
                        // Remove cache file from system
-                       @unlink($this->cache_inc);
-                       if (!FILE_READABLE($this->cache_inc))
-                       {
-                               // Close cache automatically (we don't need it anymore!)
-                               $this->cache_close();
-                       }
-                       else
-                       {
+                       unlink($this->inc);
+
+                       // Is the file there?
+                       if (!FILE_READABLE($this->inc)) {
+                               // The cache does no longer exist so kill the content
+                               unset($this->data[$this->name]);
+                       } else {
                                // Not removed!
-                               ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
+                               ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
                        }
-               }
-               else
-               {
+               } else {
                        // Does not exist!
-                       ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
                }
        }
 
-       function cache_remove($search, $data, $array)
-       {
-               global $ARRAY;
-               if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
-               {
+       // Unused method:
+       function removeEntry ($search, $data, $array) {
+               if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
                        // Load cache into dummy array
-                       $dummy = $this->cache_load();
+                       $dummy = $this->getArrayFromCache();
 
                        // Search for key in array
                        $key = array_search($data, $dummy[$search]);
-                       if (!empty($key))
-                       {
+                       if (!empty($key)) {
                                // Key (hopefully) found?
-                               foreach ($array as $a)
-                               {
+                               foreach ($array as $a) {
                                        // So we can remove all elements as requested
                                        unset($dummy[$a][$key]);
                                }
 
                                // Flush array to cache file
-                               $fp = fopen($this->cache_inc, 'w');
-                               fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
-                               foreach ($dummy as $k => $v)
-                               {
-                                       if (is_array($v))
-                                       {
-                                               // Multi line(s) found
-                                               $LINE = "";
-                                               foreach($v as $k2 => $v2)
-                                               {
-                                                       // Put every array element in a row...
-                                                       $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
-                                               }
-                                       }
-                                       else
-                                       {
-                                               // Single line found
-                                               $LINE = "\$data['".$k."'] = \"".$v."\";\n";
-                                       }
+                               $this->init();
 
-                                       // Write line(s)
-                                       fwrite($fp, $LINE);
-                               }
+                               // Write array out
+                               $this->writeArray($dummy);
 
                                // Close cache file
-                               fclose($fp);
+                               $this->finalize();
                        }
-               }
-               else
-               {
+               } else {
                        // Cannot write to cache!
-                       ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
                }
        }
 
-       function cache_replace($search, $replace, $search_key, $array)
-       {
-               global $ARRAY;
-               if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
-               {
+       function writeArray ($array) {
+               if (is_resource($this->pointer)) {
+                       foreach ($array as $k => $v) {
+                               if (is_array($v)) {
+                                       // Multi line(s) found
+                                       $LINE = "";
+                                       foreach($v as $k2 => $v2) {
+                                               // Put every array element in a row...
+                                               $LINE .= $this->rewriteEntry($k, $v2);
+                                       }
+                               } else {
+                                       // Single line found
+                                       $LINE = $this->rewriteEntry($k, $v);
+                               }
+
+                               // Write line(s)
+                               fwrite($this->pointer, $LINE);
+                       } // END - foreach
+               } else {
+                       // Cannot write array!
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
+               }
+       }
+
+       // Unused method
+       function replaceEntry ($search, $replace, $search_key, $array) {
+               if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
                        // Load cache into dummy array
-                       $dummy = $this->cache_load();
+                       $dummy = $this->getArrayFromCache();
 
                        // Check if $dummy is valid (prevents some errors)
-                       if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
-                       {
+                       if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
                                // Search for key in array
                                $key_found = array_key_exists($search_key, $dummy[$search]);
-                               if ($key_found == true)
-                               {
+                               if ($key_found == true) {
                                        $key = $search_key;
                                        // Key (hopefully) found?
-                                       foreach ($dummy as $a => $v)
-                                       {
+                                       foreach ($dummy as $a => $v) {
                                                // So we can update all entries
-                                               if ($a == $search)
-                                               {
+                                               if ($a == $search) {
                                                        // Update now...
                                                        $dummy[$a][$search_key] = $replace;
                                                }
                                        }
 
                                        // Flush array to cache file
-                                       $fp = fopen($this->cache_inc, 'w');
-                                       fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
-                                       foreach ($dummy as $k => $v)
-                                       {
-                                               if (is_array($v))
-                                               {
-                                                       // Multi line(s) found
-                                                       $LINE = "";
-                                                       foreach($v as $k2 => $v2)
-                                                       {
-                                                               // Put every array element in a row...
-                                                               $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
-                                                       }
-                                               }
-                                               else
-                                               {
-                                                       // Single line found
-                                                       $LINE = "\$data['".$k."'] = \"".$v."\";\n";
-                                               }
+                                       $this->init();
 
-                                               // Write line(s)
-                                               fwrite($fp, $LINE);
-                                       }
+                                       // Write array out
+                                       $this->writeArray($dummy);
 
                                        // Close cache file
-                                       fclose($fp);
+                                       $this->finalize();
                                }
                        }
-               }
-               else
-               {
+               } else {
                        // Cannot write to cache!
-                       ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
+               }
+       }
+
+       function storeExtensionVersion ($ext_name) {
+               // Valid cache pointer?
+               if (is_resource($this->pointer)) {
+                       // Get extension version
+                       $ext_ver = GET_EXT_VERSION($ext_name);
+
+                       // Write cache line to file
+                       fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
+               } else {
+                       // Cannot create file
+                       ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
+               }
+       }
+
+       function extensionVersionMatches ($ext_name) {
+               // Load cache (dummy)
+               $this->getArrayFromCache();
+
+               // Get extension version
+               $ext_ver = GET_EXT_VERSION($ext_name);
+
+               // Compare both
+               return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
+       }
+
+       function rewriteEntry ($key, $value) {
+               // Init line
+               $line = "";
+
+               // String or non-string? ;-)
+               if (is_string($value)) {
+                       // String...
+                       $line = "\$data['".$key."'][] = \"".$value."\";\n";
+               } elseif (is_null($value)) {
+                       // Null
+                       $line = "\$data['".$key."'][] = null;\n";
+               } elseif (is_bool($value)) {
+                       // Boolean value
+                       if ($value === true) {
+                               $line = "\$data['".$key."'][] = true;\n";
+                       } else {
+                               $line = "\$data['".$key."'][] = false;\n";
+                       }
+               } else {
+                       // Non-string
+                       $line = "\$data['".$key."'][] = ".$value.";\n";
                }
+
+               // Return line
+               return $line;
+       }
+
+       function getStatus () {
+               return $this->ret;
        }
 }
 //