- $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
- require($INC);
-}
-
-// Caching class
-class CacheSystem {
- // Define variables
- var $ret = "init";
- var $path = "";
- var $inc = "";
- var $pointer = false;
- var $data = "";
- var $version = "";
- var $name = "";
-
- // Constructor
- function CacheSystem ($interval, $path, $tested) {
- // Failed is the default
- $this->ret = "failed";
-
- // Remeber 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');
- if (FILE_READABLE($path."dummy.tmp")) {
- // Yes, we can do. So let's remove it
- unlink($path."dummy.tmp");
-
- // Is there a .htaccess file?
- if (FILE_READABLE($path.".htaccess")) {
- // Update database that we have tested it
- UPDATE_CONFIG("cache_tested", 1);
-
- // All done!
- $this->ret = "done";
- } else {
- // Stop! Set a .htaccess file first
- $this->ret = "htaccess";
- }
- }
- } elseif ($tested) {
- // System already tested
- $this->ret = "done";
- }
- }
-
- // Checks validity of cache file and if content is given
- function loadCacheFile ($file, $forceContent = false) {
- // Remember cache file
- $this->name = $file;
-
- // Construct FQFN (full qualified file name)
- $this->inc = $this->path.$file.".cache";
-
- // 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 init () {
- // This will destory an existing cache file!
- if ($this->ret == "done") {
- // Create file
- if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
- $this->pointer = fopen($this->inc, 'w') or mxchange_die("Cannot write to cache ".$this->inc." !");
-
- // Add open PHP tag
- fwrite($this->pointer, "<?php\n");
-
- // Add default depency
- $this->storeExtensionVersion("cache");
- } else {
- // Cannot create file
- ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
- }
- }
-
- function addRow ($data) {
- global $cacheArray;
-
- if (is_resource($this->pointer)) {
- // Write every array element to cache file
- foreach ($data as $k => $v) {
- // Write global cache array for immediate access
- if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
- if ($k != "ext_name") {
- $cacheArray['extensions'][$k][$data['ext_name']] = $v;
- } else {
- $cacheArray['extensions'][$k][$data['ext_id']] = $v;
- }
- if (($k == "ext_keep") && ($v == "Y")) {
- $cacheArray['active_extensions'][$data['ext_name']] = $v;
- } // END - if
- } elseif (is_array($v)) {
- // Serialize and BASE64-encode the array
- $v = base64_encode(serialize($v));
- }
-
- // Write cache line to file
- fwrite($this->pointer, $this->rewriteEntry($k, $v));
- }
- } else {
- // Cannot create file
- ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
- }
- }
-
- function finalize () {
- // Quit function when no pointer is set
- if (is_resource($this->pointer)) {
- // Write footer
- fwrite($this->pointer, "?>\n");
-
- // Close file add destroy handler
- fclose($this->pointer);
-
- // Set rights
- if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
-
- // Remove pointer
- $this->pointer = false;
- } // END - if
- }
-
- 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
- require_once($this->inc);
-
- // 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();