Sources for file plugins/cache/database.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/database.php
* Cache handling functions
*
* Version: 2.0
* CVS tag: $Id: database.php,v 1.10 2003/05/31 08:29:38 bok Exp $
* Author: Robert Amos <bok[at]ausmac.net>
* Andrew Wellington <proton[at]wiretapped.net>
* Copyright: 2001,2002,2003 odynia.org.
*/
class cache_database_plugin {
/**
* Method: _make_seed
* Description: Make a unique seed for this page
* Arguments: none
* Returns: string
**/
function _make_seed () {
// make seed
$seed = crc32($_SERVER['QUERY_STRING']);
// die.
return $seed;
}
/**
* Method: write
* Description: write a cache "file" in the database
* Arguments: string - output to cache
* Returns: true/false
**/
function write ($buffer) {
global $onyx_parent, $db, $input;
// well first things first, get the seed we need
$seed = $this->_make_seed ();
// go the table
$table = $db->spec('cache');
$db->select($table->_database);
// get our page juarez
$page = $_SERVER['SCRIPT_FILENAME'];
// clear out any existing bitches first
$sql = "DELETE FROM $table->_title WHERE $table->page = '$page' AND $table->seed = '$seed' AND
$table->location = '".$_SESSION['location']."' AND $table->user = '".$_SESSION['user']."'";
$db->exec($sql);
// make expire time
if (!is_null($onyx_parent->cache_expire))
$expire = $onyx_parent->cache_expire;
elseif ($onyx_parent->cache_lifetime != 0)
$expire = time() + $onyx_parent->cache_lifetime;
else
// dont cache things that have no life, duh
return false;
$sql = "INSERT INTO $table->_title ($table->page, $table->location, $table->user, $table->seed,
$table->time, $table->content) VALUES ('$page', '".$_SESSION['location']."',
'".$_SESSION['user']."', '$seed', '$expire', '".$input->sqltext($buffer)."')";
$db->exec($sql);
if (!$db->affected_rows()) {
$onyx_parent->trigger_error('Unable to write to cache', WARNING);
return false;
}
// finished, hard eh?
return true;
}
/**
* Method: read
* Description: read, and output a cache file
* Arguments: none
* Returns: true/false
**/
function read () {
global $onyx_parent;
$onyx_parent->load_plugin($onyx_parent->database_type, 'database');
$onyx_parent->load_plugin('input');
global $db, $input;
// make our seed
$seed = $this->_make_seed();
$page = $_SERVER['SCRIPT_FILENAME'];
// ok we need to find our lovely cache files
$table = $db->spec('cache');
$db->select($table->_database);
$sql = "SELECT * FROM $table->_title WHERE $table->page = '$page' AND $table->seed = '$seed'";
if (!empty($_SESSION['location']))
$sql .= " AND $table->location = '".$_SESSION['location']."'";
if (!empty($_SESSION['user']))
$sql .= " AND $table->user = '".$_SESSION['user']."'";
$db->exec($sql);
if (!$db->num_rows())
return false;
$data = $db->fetch_object(0);
// check to see if our script is newer than this
if (($time = filemtime($_SERVER['SCRIPT_FILENAME'])) > $data->time) {
$this->delete();
return false;
}
// do expiry checking
if (time() > $data->time) {
$this->delete();
return false;
}
// Content-Length juarez
$content = $input->unsqltext($data->content);
header("Content-Length: ".strlen($content));
echo $content;
// and we're done
return true;
}
/**
* Method: delete
* Description: delete a cache file
* Arguments: string - page name (optional)
* string - location (optional)
* string - username (optional)
* string - seed (optional)
* boolean - expired (optional)
* Returns: true/false
**/
function delete ($page=NULL, $location=NULL, $user=NULL, $seed=NULL, $expired=NULL) {
global $onyx_parent, $db;
$table = $db->spec('cache');
$db->select($table->_database);
// delete that stuff first i guess
if (is_array($params)) {
$sql = "DELETE FROM $table->_title WHERE ";
// ok what are we looking forward to deleting?
if (!is_null($page))
$sql .= "$table->page = '$page' AND ";
if (!is_null($location))
$sql .= "$table->location = '$location' AND ";
if (!is_null($user))
$sql .= "$table->user = '$user' AND ";
$sql .= "$table->seed = '".$this->_make_seed()."'";
$db->exec($sql);
} else {
// delete cache data for current page i guess
$sql = "DELETE FROM $table->_title WHERE $table->page = '".$_SERVER['SCRIPT_FILENAME']."' AND
$table->location = '".$_SESSION['location']."' AND $table->user = '".$_SESSION['user']."' AND
$table->seed = '".$this->_make_seed()."'";
$db->exec($sql);
}
// delete expired pages
if ($expired) {
$sql = "SELECT FROM $table->_title WHERE $table->time + $onyx->cache_lifetime > ".time();
$db->exec($sql);
}
return true;
}
}
?>
