#
# vim:set ts=4:
# vim:set shiftwidth=4;
#
require_once "../include/Tools.inc";
class Session
{
var $id = false;
var $ref = false;
var $db = false;
var $messages = false;
var $cookie_name = false;
/**
* create a new Session
*
* creates a new session by calling get_id()
and
* throwing an error on failure.
*
* @params object a Database object
* @params string Name of the cookie holding session id
* @params object a Messages object
*/
function Session(&$database, $cookie_name, &$messages) {
assert($cookie_name);
assert($database);
assert($messages);
$this->cookie_name = $cookie_name;
$this->db = &$database;
$this->messages = &$messages;
$this->get_id();
}
/**
* stores current session to database
*
* This function stores the current session to the database by
* calling store_data
and sets id
and
* ref
to false.
*
* @returns boolean true on success, false otherwhise
*/
function close() {
if (! $this->id)
return true;
assert($this->id != '');
assert($this->db);
if ($this->store_data() === false)
return false;
$this->id = false;
$this->ref = false;
$this->data = false;
return true;
}
/**
* Get data from database
*
* Fetches data for the current id from the database
* and sets session_data_ref
.
*
* @returns boolean true on success, false otherwhise
*/
function get_data()
{
assert($this->id != '');
assert($this->db);
$row=$this->db->query_row('select ref, data from session where id=?', array($this->id));
if ( $row === false )
return false;
$this->ref = $row['ref'];
$this->data = ($row['data']) ? unserialize($row['data']) : false;
assert($this->ref);
return true;
}
/**
* Store data in database
*
* Dumps data for the current id into the database.
*
* @returns boolean true on success, false otherwhise
*/
function store_data()
{
assert($this->ref);
assert($this->id != '');
assert($this->db);
return $this->db->update('session', $this->ref, array(data => serialize($this->data), last_seen => 'now'));
}
/**
* Create a session in the database with the current id
*
* Creates a session in the database and fills local variables by
* calling get_data()
.
*
* @returns boolean true if success
*/
function create_session()
{
assert($this->id != '');
assert($this->db);
$result = $this->db->insert('session',array(id => $this->id));
if ( $result === false )
return false;
return $this->get_data();
}
/**
* Create a unique string useable as session ID
*
* @returns string a unique session ID
*/
function create_id() {
$id = $_SERVER["UNIQUE_ID"];
$id .= md5(time.rand(0,1000000));
return $id;
}
/**
* Set session ID for current session
*
* Should there be no session ID a new session ID is created, inserted into
* the database (creating a new session), sent as a cookie to the luser,
* and returned.
*
* Also makes sure ref is set and persistent session data is loaded.
*
* @returns string session ID, boolean false on error
*/
function set_id() {
if ( ! $this->id ) {
$this->id = $this->create_id();
if ($this->create_session() === false) {
$this->id = false;
return false;
}
setcookie($this->cookie_name, $this->id);
} else {
if ($this->get_data() === false) {
$this->id = false;
return false;
}
}
return $this->id;
}
/**
* Get session ID for current session
*
* This function takes the session ID of a cookie, checks wheter it is
* database and if yes, returns it.
*
* Also makes sure ref is set and persistent session data is loaded.
*
* @returns string session ID, boolean false on error
*/
function get_id() {
$this->id=$_COOKIE[$this->cookie_name];
if ($this->id)
if ($this->get_data() === false)
$this->id = false;
return $this->id;
}
}
?>