<?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 sessions {
private static $_SESS;
public static function start($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return $SESSRef; }
if ( isset(self::$_SESS[$SESSRef]) ){ return (FALSE !== self::$_SESS[$SESSRef]); }
$type = configs::get('sessions', $SESSRef, 'type');
if ( FALSE === $type ){
errors::raise("Sessions : $SESSRef, Missing 'type' configuration, check your config file !", CORE_LOG_ERROR, 'SESS');
self::$_SESS[$SESSRef] = FALSE;
return FALSE;
}
$class = 'sess_'.$type;
self::$_SESS[$SESSRef] = new $class($SESSRef);
if ( FALSE === self::cleanup($SESSRef) ){
errors::raise("Sessions : $SESSRef, could not be cleaned, abording !", CORE_LOG_ERROR, 'SESS');
self::$_SESS[$SESSRef] = FALSE;
return FALSE;
}
$r = self::$_SESS[$SESSRef]->start();
if ( FALSE === $r ){
errors::raise("Sessions : $SESSRef, could not be started, abording !", CORE_LOG_ERROR, 'SESS');
self::$_SESS[$SESSRef] = FALSE;
return FALSE;
}
return $r;
}
private static function cleanup($SESSRef){
while( FALSE !== ($tmp = self::$_SESS[$SESSRef]->tocleanup()) ){ $tocleanup[] = $tmp; }
if ( !isset($tocleanup) ){
errors::raise("Sessions : $SESSRef, No sessions need to be cleaned", CORE_LOG_NOTICE, 'SESS');
return TRUE;
}
errors::raise("Sessions : $SESSRef, cleaning ".count($tocleanup).' sessions.', CORE_LOG_NOTICE, 'SESS');
/*
if ( CORE_AUTH ){
$confs = CORE::getconf(realm::CONF);
foreach($confs['realm'] as $realm => $conf ){
if ( $conf['SESSRef'] == $SESSRef ){
$auth = new authority($realm);
$auth->cleanup_sessionwatch($tocleanup);
}
}
}
*/
return self::$_SESS[$SESSRef]->cleanup($tocleanup);
}
public static function sessid($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( CORE::isError($SESSRef) ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
return self::$_SESS[$SESSRef]->get_sessid();
}
public static function istarted($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( isset(self::$_SESS[$SESSRef]) ){ return ( FALSE !== self::$_SESS[$SESSRef]); }
return FALSE;
}
public static function stop($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
unset(self::$_SESS[$SESSRef]); //destroy object
return TRUE;
}
public static function store($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
return self::$_SESS[$SESSRef]->store();//store var cache
}
public static function destroy($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
self::$_SESS[$SESSRef]->destroy();//remove stored var cache
self::$_SESS[$SESSRef]->clearcache();//unset cache
unset(self::$_SESS[$SESSRef]); //destroy object
return TRUE;
}
public static function clear($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
self::$_SESS[$SESSRef]->clearcache();//unset cache
return TRUE;
}
public static function isdefined($var, $SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
return self::$_SESS[$SESSRef]->isdefined($var);
}
public static function read($var, $SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
return self::$_SESS[$SESSRef]->read($var);
}
public static function readall($SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
return self::$_SESS[$SESSRef]->readall();
}
public static function set($var, $val, $SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
return self::$_SESS[$SESSRef]->set($var, $val);
}
public static function del($var, $SESSRef=NULL){
$SESSRef = self::getref($SESSRef);
if ( FALSE === $SESSRef ){ return FALSE; }
if ( FALSE === self::istarted($SESSRef) ){
errors::raise('You must start a session before using it !', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
return self::$_SESS[$SESSRef]->del($var);
}
private static function getref($SESSRef){
if ( NULL === $SESSRef OR empty($SESSRef) ){ //no args gived, we return default session handler
if ( FALSE !== ( $SESSRef = configs::get('sessions', 'Default')) ){ return $SESSRef; }
} elseif ( is_array( configs::get('sessions', $SESSRef)) ){ return $SESSRef; }
errors::raise('Invalid Sessions reference ! ', CORE_LOG_ERROR, 'SESS');
return FALSE;
}
public static function cookref($SESSRef=NULL){ return ini_get('session.name'); }
}