<?php
/*
 * Created on 16.2.2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

/**
 * Provides database handler for sessions
 * @package SimplePHPUcto
 * @subpackage Session
 */

require_once LIBS_PATH.DS.'application'.DS.'database'.DS.'table'.DS.'session.php';
class Session_Database extends Session {
	private $_table=null;
	private $_life_time=null;
	function __construct() {
		$this->_table=new TableSession();
		$this->_life_time=ini_get('session.gc_maxlifetime');
	}
	
	function open($save_path,$session_name) {
		return true;
	}
	
	function close() {
		return true;
	}
	
	function read($session_id) {
		$where=clone $this->_table;
		$get=clone $this->_table;
		
		$where->expire["where"]["string"]=strftime('%Y-%m-%d %H:%M:%S',time()+$this->_life_time);
		$where->expire["where"]["format"]=STRING;
		$where->expire["where"]["type"]=GT;
		$where->id["where"]["string"]=$session_id;
		$where->id["where"]["format"]=STRING;
		$where->id["where"]["type"]=EQUAL;
		
		$get->data=1;
		
		return $this->_table->setQuery(SELECT,$get,$where,null,'result');
	}
	
	function write($session_id,$session_data) {
		$req=Factory::getRequest();
		$insert=clone $this->_table;
		$insert->id=array('format'=>STRING,'string'=>$session_id);
		$insert->username=array('format'=>STRING,'string'=>$req->getString('username','session'));
		$insert->group=array('format'=>NUMBER,'string'=>$req->getInt('group','session'));
		$insert->data=array('format'=>STRING,'string'=>$session_data);
		$insert->expire=array('format'=>STRING,'string'=>strftime('%Y-%m-%d %H:%M:%S',time()+$this->_life_time));
		$select=clone $this->_table;
		$select->all=2;
		$where=clone $this->_table;
		$where->id["where"]=array("string"=>$session_id,"format"=>STRING,"type"=>EQUAL);
		
		$count=$this->_table->setQuery(SELECT,$select,$where,null,'result');
		
		echo $count?'UPDATE':'INSERT';
		if(!$this->_table->setQuery($count?UPDATE:INSERT,$insert,$where,null,null)) {
			return false;
		}
		
		return true;
	}
	
	function destroy($session_id) {
		$where=clone $this->_table;
		
		$where->id["where"]["string"]=$session_id;
		$where->id["where"]["format"]=STRING;
		$where->id["where"]["type"]=EQUAL;
		
		if(!$this->_table->setQuery(DELETE,null,$where,null,null)) {
			return false;
		}
		 
		return true;
	}
	
	function gc($session_life_time) {
		$where= clone $this->_table;
		
		$where->id["where"]["string"]=strftime('%Y-%m-%d %H:%M:%S',time()+$session_life_time);
		$where->id["where"]["format"]=STRING;
		$where->id["where"]["type"]=GT;
		
		if(!$this->_table->setQuery(DELETE,null,$where,null,null)) {
			return false;
		}
		return true;
	}
	
	function verify() {
		$req=Factory::getRequest();
		$group=$req->getInt('group','session');
		if($group==0) {
			return false;
		}
		$select= clone $this->_table;
		$where= clone $this->_table;
		
		$select->all=2;
		$where->username["where"]["string"]=$req->getString('username','session');
		$where->username["where"]["format"]=STRING;
		$where->username["where"]["type"]=EQUAL;
		$where->group["where"]["string"]=$group;
		$where->group["where"]["format"]=NUMBER;
		$where->group["where"]["type"]=EQUAL;
		
		$cnt=$this->_table->setQuery(SELECT,$select,$where,null,'result');
		
		if($cnt) {
			return true;
		} else {
			return false;
		}
	}
}
?>