<?php
/*
    Created by FrosT of Emocium Inc.
    Free to use for any purpose. 

    Questions direct to frost [-at-] aeonity [-dot-] com

    Notes: This file is desgined as a guide to help you understand
        how caching works. There is alot of missing peices but
        I placed very descriptive excerts on what could potentially
        happen. I do this so you learn yourself how to code. 
        Just like the old proverb, "You can catch a man a fish and feed
        him for one day, or show the man how to fish and feed him for life."
        
        Remember this file contains 2 classes, the gZip class is after the
        cache class.

    Usage:
    $clgZip = new clGzip();
    $clCache = new clCache($clgZip);

    $content = "<b>This will be cached</b>";
    $cached = $clCache->createCache($content);

    $clCache->saveCache($cached, "someidentifier");

    // Now the next time the page is called grab the cached page.    
    print $clCache->getCache("someidentifier");
*/
class clCache {
    var 
$clgZip;

    function 
clCache($clgZip) {
        
$this->clgZip $clgZip;
    }

    
/*
        Note, you can choose to create more functions here
        for saving and retrieving your cache using this class.
        This will vary depending if you want to store it in a 
        database or file.
    */
    
function createCache($content) {
        
ob_start();        
        print 
$content;
        
$content $this->clgZip->gZip(ob_get_contents());
        
ob_end_clean();
        
        return 
$content;
    }

    function 
updateCache($file=""$cacheID="") {
        
/*
            I would suggest checking file creation time
            and the expire time. If in a database make sure
            your database structure includes a cacheexpires
            column and a cache created. The cache expires I would
            put as seconds and than you can subrtact the current time
            from the created time and tell if the cache needs
            to be updated or not. This is handy when you have
            dynamic content that you know when it will be updated.

            With a file I am not sure maybe put in some code
            in the file that you can parse out to tell you. 
        */
    
}

    function 
saveCache($content,$fileName="") {
        
/*
            This is where you would put the fopen fwrite
            etc. or the mysql database call. If you choose
            to use mySQL I would suggest adding a few parameters
            or using the $_POST output for database entry.
        */
    
        
if ($success) {
            return 
true;
        }else {
            return 
false;
        }
    }

    function 
getCache($file=""$cacheID="") {
        
/*
            This will also vary depending on the system
            you choose to go with. Remove file or cacheID
            depending if you want it stored in the database
            or as a file on the server. Maybe even throw
            in some logic to check the expiration time
            to see if the cache needs to be updated.
            If so call createCache and than saveCache
            and than return the cached contents to be
            printed.
        */

        
return $contents;
    }    

}


/*
    Usage:
    $clGzip = new clGzip();

    $gZippedContent = $clGzip->gZip($contentToZip);

    //Store the gzipped content anywhere or print it to the screen

    // I highly suggest using this function for displaying as it will
    // automatically detect the settings of the user's browser for you.
    print $clGzip->gZipDisplay($gZippedContent); 

    Simple as that. This will provide you with a gzipped data.
*/

class clGzip {
    var 
$compress;

    function 
clGzip() {
        
$this->compress false;
        if (
substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
            
$this->compress true;
        }        
    }
    
    function 
gZip($content$display=false) {
        
$content gzencode($content3FORCE_GZIP);
        if (
$display) {
            return 
$this->gZipDisplay($content);
        }
        
        return 
$content
    }
    
    function 
gZipConfig() {
        if (
$this->compress) {
            
ini_set('zlib.output_compression'"on");
            
ini_set('zlib.output_compression_level'3);                
            
header('Content-Encoding: gzip');
        }
    }
    
    
// $gZipContent should always be gzipped
    
function gZipDisplay($gZipContent) {
        if (!
$this->compress) {
            
$gZipContent $this->ungZip($gZipContent);
        }else {
            
$this->gZipConfig();
        }
        
        return 
$gZipContent;
    }
    
    function 
ungZip($content) {
        return 
gzinflate($content);
    }

}
?>