- $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
- require($INC);
-}
-
-// Caching class
-class mxchange_cache
-{
- // Define variables
- var $update_interval = 0;
- var $ret = "init";
- var $cache_path = "";
- var $cache_inc = "";
- var $cache_ctime = 0;
- var $cache_pointer = false;
- var $cache_data = "";
- var $cache_version = "";
- var $cache_file = "";
-
- // Constructor
- function mxchange_cache($interval, $path, $tested) {
- // Failed is the default
- $this->ret = "failed";
-
- // Remember interval in class
- $this->update_interval = $interval;
-
- // Remeber path
- $this->cache_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 cache_file ($file, $forceContent = false) {
- // Remember cache file
- $this->cache_file = $file;
-
- // Construct FQFN (full qualified file name)
- $inc = $this->cache_path.$file.".cache";
-
- // 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;
-
- // Is the cache file outdated?
- if (($status) && ((time() - $ctime) >= $this->update_interval)) {
- // Ok, we need an update!
- $status = false;
- } elseif ($status) {
- // Check on 'cache' extension
- $status = $this->ext_version_matches("cache");
- }
-
- // Return status
- return $status;
- }
-
- function cache_init($array) {
- // This will destory an existing cache file!
- if ($this->ret == "done") {
- // Create file
- if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
- $this->cache_pointer = fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
-
- // Begin of cache file
- fwrite($this->cache_pointer, "<?php\n\$ARRAY = \"".$array."\";\n\n");
-
- // Add default depency
- $this->store_extension_version("cache");
- } else {
- // Cannot create file
- ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
- }
- }
-
- function add_row ($data) {
- global $cacheArray;
-
- if (is_resource($this->cache_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->cache_pointer, $this->add_raw_row($k, $v));
- }
- } else {
- // Cannot create file
- ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
- }
- }
-
- function cache_close() {
- // Quit function when no pointer is set
- if (empty($this->cache_pointer)) return;
- if (is_resource($this->cache_pointer)) {
- // Write footer
- fwrite($this->cache_pointer, "?>\n");
-
- // Close file add destroy handler
- fclose($this->cache_pointer);
-
- // Set rights
- if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);