Location: PHPKode > scripts > gCache > gcache/gCache.php
<?php
/*
 +------------------------------------------------------------+
 | The author discailms the copyright over this files. You    |
 | legally free to use the source code as you wish.           |
 |                                                            |
 | coded by crodas (hide@address.com)                    |
 | (spanish) http://cesar.la/                                 |
 | (english) http://www.phpclasses.org/gcache                 |
 +------------------------------------------------------------+
*/

define("SIO_READ","rb");
define("SIO_WRITE","w+b");
define("SIO_APPEND","ab");

class safeIO
{
    var $file;
    var $openas;
    var $fp;
    
    function safeIO()
    {
    }
    
    function addFile($file)
    {
        if ( strlen($file) > 0  )
        {
            $this->file[] = $file;
            return count($this->file)-1;
        }
        return false;
    }
    
    function delFile($file)
    {
        if ( strlen($file) > 0 && ($key=array_search($file,$this->file))!==false )
        {
            unset($this->file[$key]);
            return true;
        }
        return false;
    }
    
    function open($op=SIO_READ)
    {
        if ( count($this->file) == 0) return false;
        $this->openas = $op;
        if ( !$this->lock() )
            return false;
        return true;
    }
    
    function close()
    {
        foreach($this->fp as $id => $fp)
            fclose($fp);
    }
    
    function lock()
    {
        foreach($this->file as $id => $file)
        {
            if ( !file_exists($file) && $this->openas != SIO_WRITE )
                return false;
            $this->fp[$id] = fopen($file,$this->openas);
            if ( !flock($this->fp[$id], ($this->openas == SIO_READ ? LOCK_SH : LOCK_EX) ) ) {  
                $this->close();
                return false;
            }
        }
        return true;
    }
    
    function read($fid)
    {
        if ( !isset($this->fp[$fid])) return false;
        return fread($this->fp[$fid]);
    }
    
    function write($fid,$content)
    {
        if ( !isset($this->fp[$fid])) return false;
        return fwrite($this->fp[$fid],$content);
    }
    
    function get_content($fid)
    {
        if ( !isset($this->fp[$fid])) return false;
        fseek($this->fp[$fid],0,SEEK_SET);
        return fread($this->fp[$fid], filesize($this->file[$fid]) );
    }
    
    function get_stat($fid)
    {
        if ( !isset($this->fp[$fid])) return false;
        @clearstatcache();
        return fstat($this->fp[$fid]);
    }
}

class gCache
{
    var $contentId;
    var $folder;
    var $timeout;
    var $foldering;
    var $isPage=false;
    function gCache()
    {
        
    }
    
    function enableFoldering()
    {
        $this->foldering = true;
    }
    
    function disableFoldering()
    {
        $this->foldering = false;
    }
    
    function isValid(&$expires)
    {
        $sio  = new safeIO();
        $name = $this->getCacheName();
        $sio->addFile($name);
        if ($this->isPage) {
            $sio->addFile($name.".z");
        }
        if ( $sio->open(SIO_READ) )
        {
            $info = $sio->get_stat(0);
            $expires = $info['mtime']+$this->timeout*60;
            if ( $info['mtime']+$this->timeout*60 < time()  )
                return false;
            $this->content = $sio->get_content( $this->userSupportGZIP() ? 1 : 0);
            $sio->close();    
            return true;
        }
        return false;
    }
    
    function capture() {
        ob_start( array(&$this,'writeCache') );
    }
    
    function endcapture() {
        ob_end_flush();
    }
    
    function Valid(&$expires)
    {
        return $this->isValid($expires);
    }
    
    function writeCache($content)
    {
        $sio  = new safeIO();
        $name = $this->getCacheName();
        $sio->addFile($name);
        
        if ($this->isPage)
        {
            $sio->addFile($name.".z");
        }
        if ( $sio->open(SIO_WRITE) )
        {
            $sio->write(0,$content);
            if ($this->isPage) {
                $ccontent = "\x1f\x8b\x08\x00\x00\x00\x00\x00".gzcompress($content,9);
                $sio->write(1,$ccontent);
            }
            $sio->close();
        }
        $sio = null;
        return $content;
    }
    
    function userSupportGZIP()
    {
        $r = false;
        $aEncoding = isset($_SERVER["HTTP_ACCEPT_ENCODING"]) ? $_SERVER["HTTP_ACCEPT_ENCODING"] : "";
        /* bug reported by Nadeem */
        /* missing $this->isPage in previous version*/
        $compressed = $this->isPage && !isset($GLOBALS['cache_gzip']) && !headers_sent() && strpos($aEncoding, 'gzip') !== false;
#        && ( !isset($_SERVER["HTTP_CONNECTION"]) || $_SERVER["HTTP_CONNECTION"]!="keep-alive");
        if ( $compressed )
        {
            header("Content-Encoding: ".(strpos($aEncoding, 'x-gzip') ? "x-gzip" : "gzip" ));
            $GLOBALS['gcache_gzip']=true;
            $r = true;
        }
        return $r;
    }
    
    function getCacheName()
    {
        return $this->folder."/".md5($this->contentId);
    }

}
?>
Return current item: gCache