diff options
author | Peter Palfrader <peter@palfrader.org> | 2004-11-15 09:20:11 +0000 |
---|---|---|
committer | Peter Palfrader <peter@palfrader.org> | 2004-11-15 09:20:11 +0000 |
commit | 5e95090defff64bc8cd7a318a73aa930948fb66d (patch) | |
tree | d977ded4207e51914d539b0ecd20b8583d37c8ea /web/include/Session.inc | |
parent | 6c3e0ba0a82307abf825bf1cde85638464ab1713 (diff) |
Initial import
Diffstat (limited to 'web/include/Session.inc')
-rw-r--r-- | web/include/Session.inc | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/web/include/Session.inc b/web/include/Session.inc new file mode 100644 index 0000000..d6c926a --- /dev/null +++ b/web/include/Session.inc @@ -0,0 +1,180 @@ +<? +# +# 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 <code>get_id()</code> 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 <code>store_data</code> and sets <code>id</code> and + * <code>ref</code> 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 <code>session_data_ref</code>. + * + * @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 <code>get_data()</code>. + * + * @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; + } +} + + +?> |