<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class sess_db extends sess {
private $DBOBJ;
public function __construct($SESSRef){
if ( !isset($conf['cookref']) ){
$conf['cookref'] = NULL;
} elseif (! isset($conf['timeout']) ){
$conf['timeout'] = 60*15;
} elseif (! isset($conf['lifetime']) ){
$conf['lifetime'] = 0;
}
parent::__construct($SESSRef);
$cnf = configs::get('sessions', $SESSRef, 'CNF');
$this->DBOBJ = new dbobject($cnf);
}
public function __destruct(){
errors::raise('DB sessions destruct', CORE_LOG_NOTICE, 'SESS');
$this->store();
$this->clearcache();//unset cache
unset($this);
}
public function start(){
if ( $this->restoreid() ){ //sessid could be restored from cookies
if ( FALSE !== $this->exists() ){
//sess restoration, return true or false,
return $this->load(); //known sessid we load it
}
}
$this->genid();
$r = $this->exportid();
if ( CORE::isError($r) ){
return errors::getnew('Session ID could not be stored on client side :'.$r->getmsg(), CORE_LOG_ERROR, 'SESS');
}
if ( FALSE === $this->creat() ){
return errors::getnew('Session data could not be stored on server side.', CORE_LOG_ERROR, 'SESS');
}
errors::raise('Session has been created : '.$this->get_sessid(), CORE_LOG_NOTICE, 'SESS');
if ( FALSE === $this->exists() ){
errors::raise('Session has been created BUT do not exists', CORE_LOG_WARNING, 'SESS');
}
//new sess created we return NULL
return NULL;
}
public function store(){
if ( !isset($this->DBOBJ) ){ return FALSE; }
$arg=Array('id'=>$this->get_sessid(), 'sessref'=>$this->SESSRef,'data'=>$this->storedata(), 'time'=>time() );
return $this->DBOBJ->dbquery($arg, __FUNCTION__);
}
public function cleanup(){
if ( !isset($this->DBOBJ) ){ return FALSE; }
$lifetime = configs::get('sessions', $this->SESSRef, 'lifetime');
if ( $lifetime == 0 ){
$lt = 0;
} else {
$lt = (time()-$lifetime);
}
$timeout = configs::get('sessions', $this->SESSRef, 'timeout');
$arg=Array('lt'=>$lt, 'to'=>(time()-$timeout), 'sessref'=>$this->SESSRef);
unset($lt, $lifetime, $timeout);
return $this->DBOBJ->dbquery($arg, __FUNCTION__);
}
public function tocleanup(){
if ( !isset($this->DBOBJ) ){ return FALSE; }
$lifetime = configs::get('sessions', $this->SESSRef, 'lifetime');
$timeout = configs::get('sessions', $this->SESSRef, 'timeout');
if ( $lifetime == 0 AND $timeout == 0 ){
errors::raise('This sessions will never drop any session !', CORE_LOG_WARNING, 'SESS');
fclose($hdl);$hdl=TRUE;return FALSE;
}
if ( $lifetime == 0 ){
$lt = 0;
} else {
$lt = (time()-$lifetime);
}
$arg=Array('lt'=>$lt, 'to'=>(time()-$timeout), 'sessref'=>$this->SESSRef);
unset($lt, $lifetime, $timeout);
return $this->DBOBJ->dbquery($arg, __FUNCTION__, 'sessid');
}
public function destroy(){
if ( !isset($this->DBOBJ) ){ return FALSE; }
$arg=Array('id'=>$this->get_sessid(), 'sessref'=>$this->SESSRef);
return $this->DBOBJ->dbquery($arg, __FUNCTION__);
}
private function exists($sessid=NULL){
if ( !isset($this->DBOBJ) ){ return FALSE; }
if ( $sessid === NULL ){ $sessid = $this->get_sessid(); }
$arg=Array('id'=>$sessid, 'sessref'=>$this->SESSRef);
return $this->DBOBJ->dbquery($arg, __FUNCTION__);
}
private function creat($sessid=NULL){
if ( !isset($this->DBOBJ) ){ return FALSE; }
if ( $sessid === NULL ){ $sessid = $this->get_sessid(); }
$arg=Array('id'=>$sessid, 'time'=>time(), 'sessref'=>$this->SESSRef);
return $this->DBOBJ->dbquery($arg, __FUNCTION__);
}
private function load($sessid=NULL){
if ( !isset($this->DBOBJ) ){ return FALSE; }
if ( $sessid === NULL ){ $sessid = $this->get_sessid(); }
$arg=Array('id'=>$sessid, 'sessref'=>$this->SESSRef);
return $this->restoredata( $this->DBOBJ->dbquery($arg, __FUNCTION__, 'data') );
}
}