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



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.0b4
 * CVS tag:     $Id: file.php,v 1.9 2002/10/28 14:49:52 bok Exp $
 * Author:      Robert Amos <bok[at]ausmac.net>
 *              Andrew Wellington <proton[at]wiretapped.net>
 * Copyright:   2001,2002 Shiznatz Inc.
*/

class cache_file_plugin {

    
/***************************************************************\
    * Method:    Make file mask                    *
    * Description:    Make a file mask for writing to the disk    *
    * Syntax:    $this->_make_seed();            *
    * Returns:    file name                    *
    \***************************************************************/
    
function _make_seed () {

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

        
// die.
        
return $seed;
    }

    
/***************************************************************\
    * Method:    write                        *
    * Description:    write a cache file                *
    * Syntax:    $this->write (string buffer);            *
    * 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->_transform_path($onyx_parent->cache_location).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 {
            
trigger_error('Cache directory '.$dir.' doesnt exist or is not writable, please check your
configuration.'
WARNING);
            return 
false;
        }

        
$dir .= $_SESSION['location'].DIRSEP;
        if (!
is_dir($dir))
            
mkdir($dir0700);

        if (!empty(
$_SESSION['user'])) {
            
$dir .= $_SESSION['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";

        
// 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
        
fputs ($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            *
    * Syntax:    $this->read ();                    *
    * Returns:    true/false                    *
    \***************************************************************/
    
function read () {

        global 
$onyx_parent;

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

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

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

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

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

                
// try ONYXDIR first, SCRIPT_DIR second
                // cant write to the dir? uh oh!
                
if (is_file(ONYXDIR.$dir.$file) && is_readable(ONYXDIR.$dir.$file)) {
                        
$dir ONYXDIR.$dir;
                } elseif (
is_file(SCRIPT_DIR.$dir.$file) && is_writable(SCRIPT_DIR.$dir.$file)) {
            
$dir SCRIPT_DIR.$dir;
                } elseif (
is_file('.'.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.