<?php 
class session
{
	var $lifetime = 1800;
	var $db;
	var $table;

    function __construct()
    {
		global $db;
	    $this->lifetime = SESSION_TTL;
		$this->db = &$db;
		$this->table = '`'.DB_NAME.'`.`'.DB_PRE.'session`';
    	session_set_save_handler(array(&$this,'open'), array(&$this,'close'), array(&$this,'read'), array(&$this,'write'), array(&$this,'destroy'), array(&$this,'gc'));
    }

    function session(){
		$this->__construct();
    }

    function open($save_path, $session_name){
		return true;
    }

    function close(){
        return $this->gc($this->lifetime);
    } 

    function read($id){
		$r = $this->db->get_one("SELECT `data` FROM $this->table WHERE `sessionid`='$id'");
		return $r ? $r['data'] : '';
    } 

    function write($id, $data){
		if(strlen($data) > 255) $data = '';
		return $this->db->query("REPLACE INTO $this->table (`sessionid`, `ip`, `lastvisit`, `data`) VALUES('$id', '".IP."', '".TIME."', '".addslashes($data)."')");
    } 

    function destroy($id){
		return $this->db->query("DELETE FROM $this->table WHERE `sessionid`='$id'");
    } 

    function gc($maxlifetime){
		$expiretime = TIME - $maxlifetime;
		return $this->db->query("DELETE FROM $this->table WHERE `lastvisit`<$expiretime"); 
    }
}
?>