<?php
if(!defined('DATALIFEENGINE')){die("Hacking attempt!");}

class cache{
    var $dir = false;
    var $allow_cache = false;
    var $memcache = false;

    private $mcache;
    private $memcache_host = '127.0.0.1';
    private $memcache_port = 11211;

    var $timelife = 3600;

    function __construct($dir) {
        global $config;

        $this->dir = $dir;
        if( $config['allow_cache'] = "yes" ) $this->allow_cache = true;
		if( $config['cache_type']) $this->memcache = true;

        if( $this->memcache AND function_exists('memcache_connect') ) $this->mcache = memcache_connect($this->memcache_host, $this->memcache_port);
        if( $this->memcache AND function_exists('memcache_set_compress_threshold') ){memcache_set_compress_threshold( $this->mcache, 20000, 0.2 );}
   	}

    function off(){
        $this->allow_cache = false;
    }

    function set($file, $data){
        $fp = fopen( $this->dir.'/system/' . $file . '.php', 'wb+' );
        if (!$data) $data = 'a:0:{}';
        fwrite( $fp, serialize( $data ) );
       	fclose( $fp );
       	@chmod( $this->dir.'/system/' . $file . '.php', 0666 );
    }

    function get($file){
        return unserialize( @file_get_contents( $this->dir.'/system/' . $file . '.php' ) );
    }

    function delete($cache_area = false){
   		$fdir = opendir($this->dir.'/system/');
   		while ($file = readdir($fdir)){
   			if ($file != '.' and $file != '..' and $file != '.htaccess' and $file != 'online.php'){
   				if ($cache_area){
   					if (strpos($file, $cache_area) !== false) @unlink($this->dir.'/system/'.$file);
   				}else{
   					@unlink($this->dir.'/system/'.$file);
   				}
   			}
   		}
   	}

    function filename($name){
  		if(!$this->memcache) return $this->dir.'/'.$name.'.tmp';
        else return md5($name);
  	}

    function open($name, $cache_id = false, $member_prefix = false){
        global $is_logged, $member_id;
        if(!$this->allow_cache) return false;

       	if( $is_logged ) $group = $member_id['user_group']; else $group = "0";

        if( !$cache_id ){ $filename = $this->filename($name);
        } else {
            $cache_id = md5( $cache_id );
            if( $member_prefix ) $filename = $this->filename($name . "_" . $cache_id . "_" . $group); else $filename = $this->filename($name . "_" . $cache_id);
        }

        if($this->memcache){
            return memcache_get($this->mcache, $filename);
        } else {
            if(file_exists($filename) && is_readable($filename)  && filesize($filename) > 0 && (time() - $this->timelife < filemtime($filename))) {
                return @file_get_contents( $filename );
      	    } else {
      		    return false;
      	    }
        }
    }

    function save($name, $data, $cache_id = false, $member_prefix = false){
        global $is_logged, $member_id;
        if(empty( $data ) OR !$this->allow_cache) return false;

        if( $is_logged ) $group = $member_id['user_group']; else $group = "0";
        if( !$cache_id ){ $filename = $this->filename($name);
        } else {
            $cache_id = md5( $cache_id );
            if( $member_prefix ) $filename = $this->filename($name . "_" . $cache_id . "_" . $group); else $filename = $this->filename($name . "_" . $cache_id);
        }

        if($this->memcache){
            memcache_set( $this->mcache, $filename, $data, MEMCACHE_COMPRESSED, $this->timelife );
        } else {
            @unlink( $filename );
            file_put_contents ($filename, $data, LOCK_EX);
            @chmod( $filename, 0666 );
        }
    }

    function clear($cache_areas = false) {
        if ( $this->memcache ) {
       		memcache_flush($this->mcache);
       	} else {
        if ( $cache_areas ) {if(!is_array($cache_areas)) {$cache_areas = array($cache_areas);}}
      		$fdir = opendir( $this->dir );
      		while ( $file = readdir( $fdir ) ) {
      			if( $file != '.' and $file != '..' and $file != '.htaccess' and $file != 'system' ) {
      				if( $cache_areas ) {	foreach($cache_areas as $cache_area) if( strpos( $file, $cache_area ) !== false ) @unlink( $this->dir . '/' . $file );
      				} else {@unlink( $this->dir . '/' . $file );}
      	}}}
    }

    function del($file){
        if ($this->memcache){
            memcache_delete($this->mcache, $this->filename($file));
        }else{
            @unlink($this->filename($file));
        }
    }

    function size(){
        if($this->memcache){
            $stat = memcache_get_stats($this->mcache);
            $size = $stat['bytes'];
        }else{
            $size = dirsize($this->dir);
        }
        return $size;
    }

    public function __destruct(){
       if($this->memcache){
          if(memcache_close($this->mcache)){
             return TRUE;
          }else{
             return FALSE;
          }
       }else{
          return FALSE;
       }
    }

}
?>