Sources for file plugins/cache/file.php in version 2.0



Click on a comment to hide it. Click here to show all comments.
<?PHP

/*
 * Project:     Onyx: PHP Application Backend
 * File:        cache/file.php
 *              Cache handling functions
 *
 * Version:     2.0
 * CVS tag:     $Id: file.php,v 1.18 2003/05/31 08:29:39 bok Exp $
 * Author:      Robert Amos <bok[at]ausmac.net>
 *              Andrew Wellington <proton[at]wiretapped.net>
 * Copyright:   2001,2002,2003 odynia.org.
*/

class cache_file_plugin {

    
/**
     * Method:            _make_seed
     * Description:    Make a file mask for writing to the disk
     * Arguments:        none
     * Returns:            seed
    **/
    
function _make_seed () {

        
// make seed
        
$seed crc32($_SERVER['QUERY_STRING']);

        
// die.
        
return $seed;
    }

    
/**
     * Method:            write
     * Description:    write a cache file
     * Arguments:        output to write
     * Returns:            true/false
    **/
    
function write ($buffer) {

        global 
$onyx_parent;

        
// well first things first, get the seed we need
        
$seed $this->_make_seed ();

        
// we dont need to worry about checking for an older file
        // just open it, if we can

        // the location in this case is a directory, check it
        
$dir $onyx_parent->cache_location;
        if (!
preg_match('/\/$/'$dir))
            
$dir .= DIRSEP;

        
// try ONYXDIR first, SCRIPT_DIR second
        // cant write to the dir? uh oh!
        
if (is_dir(ONYXDIR.$dir) && is_writable(ONYXDIR.$dir)) {
            
$dir ONYXDIR.$dir;
        } elseif (
is_dir(SCRIPT_DIR.$dir) && is_writable(SCRIPT_DIR.$dir)) {
            
$dir SCRIPT_DIR.$dir;
        } elseif (
is_dir('.'.DIRSEP.$dir) && is_writable('.'.DIRSEP.$dir)) {
            
$dir '.'.DIRSEP.$dir;
        } else {
            
$onyx_parent->trigger_error('Cache directory '.$dir.' doesnt exist or is not writable, please
check your configuration.'
WARNING);
            return 
false;
        }

        if (empty(
$onyx_parent->cache_filename)) {
            
// there is no specified file, lets go the default
            
$dir .= $_SESSION['location'].DIRSEP;
            if (!
is_dir($dir))
                
mkdir($dir0700);

            if (!empty(
$_SESSION['auth']['user'])) {
                
$dir .= $_SESSION['auth']['user'].DIRSEP;
                if (!
is_dir($dir))
                    
mkdir($dir0700);
            }

            
$page str_replace('/''_'substr($_SERVER['SCRIPT_FILENAME'], 0,
strpos($_SERVER['SCRIPT_FILENAME'], '.')));

            
// our filename
            
$file "$page.$seed.html";

            
// proper caching, headers are good
            
$headers true;
        } else {
            
// use what we are given
            
$file $onyx_parent->cache_filename;

            
// we dont want headers
            
$headers false;
        }

        
// write the cache i guess
        
$fp fopen($dir.$file"w");

        
// calculate file expire time
        
if (!is_null($onyx_parent->cache_expire))
            
$expire $onyx_parent->cache_expire;
        elseif (
$onyx_parent->cache_lifetime === 0)
            
// dont bother caching if we have a file that has no life!
            
return true;
        else
            
$expire time() + $onyx_parent->cache_lifetime;

        
// output headers and the file
        
if ($headersfputs ($fp"expire: $expire\n\n");
        
fputs ($fp$buffer);

        
fclose($fp);

        
// update the access time because we can
        
touch ($dir.$file);

        
// finished, hard eh?
        
return true;
    }

    
/**
     * Method:            read
     * Description:    read, and output a cache file
     * Arguments:        none
     * Returns:            true/false
    **/
    
function read () {

        global 
$onyx_parent;

        
// are we given a filename?
        
if (empty($onyx_parent->cache_location))
        {

            
// make our seed
            
$seed $this->_make_seed ();

            
$page str_replace('/''_'substr($_SERVER['SCRIPT_FILENAME'], 0,
strpos($_SERVER['SCRIPT_FILENAME'], '.')));
            
$file "$page.$seed.html";

            if (empty(
$_SESSION['location'])) {
                
$onyx_parent->load_plugin('access');
                global 
$access;
                
$dir .= $access->location().DIRSEP;
            } else {
                
$dir .= $_SESSION['location'].DIRSEP;
            }

            if (!empty(
$_SESSION['auth']['user']))
                
$dir .= $_SESSION['auth']['user'].DIRSEP;

        } else {
            
$file $onyx_parent->cache_location;
        }

        
// the location in this case is a directory, check it
        
$dir $onyx_parent->cache_location.DIRSEP;

                
// try ONYXDIR first, SCRIPT_DIR second
                // cant write to the dir? uh oh!
        
if (file_exists($dir.$file) && is_readable($dir.$file)) {
            
$dir $dir;
        } elseif (
file_exists(ONYXDIR.$dir.$file) && is_readable(ONYXDIR.$dir.$file)) {
            
$dir ONYXDIR.$dir;
        } elseif (
file_exists(SCRIPT_DIR.$dir.$file) && is_writable(SCRIPT_DIR.$dir.$file)) {
            
$dir SCRIPT_DIR.$dir;
        } elseif (
file_exists('.'.DIRSEP.$dir.$file) && is_writable('.'.DIRSEP.$dir.$file)) {
            
$dir '.'.DIRSEP.$dir;
        } elseif (
file_exists(SCRIPT_DIR.$dir.$file)) {
            
$dir SCRIPT_DIR.$dir;
        } else {
            return 
false;
        }
        
// do we have a set time to rebuild?

        // check if our executing file is more recent than the cache
        
if (filemtime($_SERVER['SCRIPT_FILENAME']) > filemtime($dir.$file)) {
            
unlink ($dir.$file);
            return 
false;
        }

        
// go!
        
$fp fopen($dir.$file"r");

        
// get config data
        
while (!feof($fp)) {
            
$header fgets($fp);

            if (
$header == "\n" || $header == "\r\n")
                break;

            
$header explode(': 'chop($header));

            
// store data
            
$config[$header[0]] = $header[1];

        }

        
// check to see if the time now is past the expiration date
        
if (!empty($config['expire']) && $config['expire'] !== && time() > $config['expire']) {

            
// close that file pointer, annoying thing
            
fclose($fp);

            
// delete the cache file!
            
unlink($dir.$file);

            return 
false;
        }


        
// well all checks worked? fire away?
        // get the rest of the file i guess!
        
while (!feof($fp))
            
$buffer .= fgets($fp);

        
// done
        
fclose($fp);

        
// content-length juarez first
        
header('Content-Length: '.strlen($buffer));

        
// go!
        
echo $buffer;

        
// and we're done
        
return true;
    }

}

?>


Website is Copyright © Odynia.org 2000-2005 - Xnyo is released under a BSD license.