<?

//=============================//
//                             //
//   cache                     //
//   cache.class.php           //
//   by Razvan Stanga          //
//   razvan_stanga@yahoo.com   //
//   http://www.phprebel.org   //
//                             //
//       MADE IN ROMANIA       //
//                             //
//=============================//

class Cache {
    
    var 
$cacheUrl;
    var 
$cachePath;
    var 
$cacheDir;
    var 
$cacheTime;
    var 
$cacheGenTime;
    var 
$cacheData;
    var 
$cacheDebug;
    var 
$gzipLevel;
    
    function 
Cache () {
        
$this->cacheUrl     $_SERVER['REQUEST_URI'];
        
$this->cachePath     "cache/";
        
$this->cacheDir     "";
        
$this->cacheTime     120;
        
$this->cacheGenTime 0;
        
$this->cacheData     "";
        
$this->cacheDebug   0;
        
$this->gzipLevel    7;
    }
    
    function 
setCacheDir ($dir) {
        
$this->cacheDir $dir;
        
$this->cachePath .= $dir."/";
        if ( !
is_dir $this->cachePath ) ) {
            
mkdir ($this->cachePath777);
        }
    }
    
    function 
setCacheTime ($time) {
        
$this->cacheTime     $time;
    }
    
    function 
setCacheDebug ($debug) {
        
$this->cacheDebug    $debug;
    }
    
    function 
setGzipLevel ($level) {
        
$this->gzipLevel    $level;
    }
    
    function 
startCache () {
        
        if ( 
file_exists ($this->cachePath.md5 ($this->cacheUrl).'.cache') AND ( ( time() - filemtime ($this->cachePath.md5 ($this->cacheUrl).'.cache') )  < $this->cacheTime) ) {
            
$this->openBuffer ();
            
$this->loadCache ();
            
$this->closeBuffer ();
            
$this->gzipOutput ();
            exit;
        } else {
            
$this->openBuffer ();
        }
    }
    
    function 
loadCache () {

        include ( 
$this->cachePath.md5 ($this->cacheUrl).'.cache' );
        
/*
        $filesize            =    filesize ($path);
        $filenum            =    fopen ($path, "r");
        $this->cacheData    =    fread ($filenum, $filesize);
        fclose ($filenum);
        */
    
}
    
    function 
writeCache () {

        
$path $this->cachePath.md5 ($this->cacheUrl).'.cache';

        
$filenum fopen ($path"w");
        
fwrite ($filenum$this->cacheData);
        
fclose ($filenum);
    }
    
    function 
openBuffer () {
        if ( 
PHP_VERSION >= '4.0.4' AND $this->gzipModule () ) {
            
ob_start ('ob_gzhandler');
        } else {
            
ob_start ();
            
ob_implicit_flush ();
        }
    }
    
    function 
endCache () {
        
$this->closeBuffer ();
        
$this->writeCache ();
    }
    
    function 
closeBuffer () {
        if ( 
$this->checkGzip () ) {
            
$this->cacheData ob_get_contents ();
            
ob_end_clean ();
        }
    }

    function 
gzipOutput () {

        if ( 
$encoding $this->checkGzip () AND $this->gzipModule () == TRUE ) {

            
$gzdata     "\x1f\x8b\x08\x00\x00\x00\x00\x00";
            
$size         strlen ($this->cacheData);
            
$crc         crc32 ($this->cacheData);
            
$gzdata     .= gzcompress ($this->cacheData$this->gziplevel);
            
$gzdata     substr ($gzdata0strlen ($gzdata) - 4);
            
$gzdata     .= pack ("V"$crc) . pack ("V"$size);

            
$this->gzsize strlen ($gzdata);

            
Header ('Content-Encoding: ' $encoding);
            
Header ('Content-Length: ' strlen ($gzdata));

            echo 
$gzdata;
              
        } else {
              
ob_end_flush ();
        }
    }
    
    function 
checkGzip () {

        if ( 
headers_sent () OR connection_aborted() ) {
              return 
FALSE;
        }

        if ( 
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE ) {
            return 
'x-gzip';
        }

        if ( 
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE ) {
            return 
'gzip';
        }

    return 
FALSE;
    }
    
    function 
gzipModule () {
        if ( 
extension_loaded ('zlib') AND (PHP_VERSION >= '4')  ) {
            return 
TRUE;
        } else {
            return 
FALSE;
        }
    }

    function 
checkINI () {
        return (int) 
ini_get ('zlib.output_compression');
    }
    
}

?>