modularization of the storage of sessions
This commit is contained in:
parent
e858c68969
commit
6235188a29
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
require_once("lassospkit_config.inc.php");
|
||||
require_once("lassospkit_datadir.inc.php");
|
||||
|
||||
require_once("lassospkit_session_file.inc.php");
|
||||
require_once("lassospkit_session_php.inc.php");
|
||||
/** This object encapsulate the communication between the frontend and the
|
||||
backend of the LassoSPkit.
|
||||
|
||||
|
@ -13,19 +14,19 @@ require_once("lassospkit_datadir.inc.php");
|
|||
|
||||
*/
|
||||
LassoSPKitUtilsSession::$cookiename = LassoSPKitConfig::get('cookiename');
|
||||
if (LassoSPKitUtilsSession::$use_session) {
|
||||
if (LassoSPKitUtilsSession::$use_session && ! isset($_SESSION)) {
|
||||
session_start();
|
||||
} else {
|
||||
LassoSPKitUtilsSession::getSingleton();
|
||||
}
|
||||
class LassoSPKitUtilsSession {
|
||||
private static $key = "__LassoSPKitSessionObject";
|
||||
static $key = "__LassoSPKitSessionObject";
|
||||
public static $cookiename;
|
||||
private static $THIS;
|
||||
private $vars;
|
||||
static $THIS;
|
||||
var $vars;
|
||||
|
||||
/** If we getted the last error, clear it. */
|
||||
private $clears = array();
|
||||
var $clears = array();
|
||||
/** The supported keys */
|
||||
static $keys = array(
|
||||
'NameID'=>0,
|
||||
|
@ -59,38 +60,15 @@ class LassoSPKitUtilsSession {
|
|||
'sloParams'=>0,
|
||||
'defederationParams'=>0);
|
||||
public static $use_session = 0;
|
||||
static private $timeout = 3600;
|
||||
private $id = null;
|
||||
static $timeout = 3600;
|
||||
var $id = null;
|
||||
|
||||
private function __construct() {
|
||||
function __construct() {
|
||||
$content = null;
|
||||
if (self::$use_session) {
|
||||
if (! isset($_SESSION)) {
|
||||
throw new Exception("LassoSPKit cannot work without sessions.");
|
||||
}
|
||||
if (isset($_SESSION[self::$key])) {
|
||||
$content = $_SESSION[self::$key];
|
||||
}
|
||||
$content = LassoSPKitSessionPHP::retrieve($this, self::$timeout);
|
||||
} else {
|
||||
if (isset($_COOKIE[self::$cookiename])) {
|
||||
$this->id = $_COOKIE[self::$cookiename];
|
||||
$valid = ereg("^[[:alnum:]]+$",$this->id);
|
||||
if ($valid) {
|
||||
$filepath = lassospkit_datadir() . "/cookie_session_" . $this->id;
|
||||
if (file_exists($filepath) && time()-filemtime($filepath) < self::$timeout) {
|
||||
$content = @file_get_contents($filepath);
|
||||
if ($content === FALSE) {
|
||||
lassospkit_debuglog("cannot read $filepath");
|
||||
}
|
||||
} else {
|
||||
$this->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! $content) {
|
||||
$this->id = md5("lasso" . rand());
|
||||
setcookie(self::$cookiename, $this->id, time()+3600, '/');
|
||||
}
|
||||
$content = LassoSPKitSessionFile::retrieve($this, self::$timeout);
|
||||
}
|
||||
if ($content) {
|
||||
$t = @unserialize($content);
|
||||
|
@ -113,14 +91,9 @@ class LassoSPKitUtilsSession {
|
|||
}
|
||||
$content = serialize($this->vars);
|
||||
if (self::$use_session) {
|
||||
$_SESSION[self::$key] = $content;
|
||||
LassoSPKitSessionPHP::store($this, $content);
|
||||
} else {
|
||||
if ($this->id) {
|
||||
$ret = @file_put_contents(lassospkit_datadir() . "/cookie_session_" . $this->id, $content);
|
||||
if ($ret === FALSE) {
|
||||
lassospkit_debuglog("cannot write into " . lassospkit_datadir() . "/cookie_session_" . $this->id);
|
||||
}
|
||||
}
|
||||
LassoSPKitSessionFile::store($this, $content);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,8 +145,11 @@ class LassoSPKitUtilsSession {
|
|||
/* Helper static functions */
|
||||
|
||||
function delete() {
|
||||
$filepath = lassospkit_datadir() . "/cookie_session_" . $this->id;
|
||||
@unlink($filepath);
|
||||
if (self::$use_session) {
|
||||
LassoSPKitSessionPHP::delete($this);
|
||||
} else {
|
||||
LassoSPKitSessionFile::delete($this);
|
||||
}
|
||||
}
|
||||
/** Clear the session object of all communication
|
||||
from the LassoSPKit. */
|
||||
|
|
Reference in New Issue