Custom sessionhandler support, as requested

This commit is contained in:
Mark Nowiasz 2019-01-29 21:41:23 +01:00
parent 793d78fba6
commit 4b516fdb09

View File

@ -15,7 +15,7 @@ class Session {
private $handler = null; private $handler = null;
private $session_started = false; private $session_started = false;
private $custom_handler = false;
public function init() { public function init() {
$gc_probability = 50; $gc_probability = 50;
@ -24,10 +24,29 @@ class Session {
ini_set('session.use_only_cookies', 1); ini_set('session.use_only_cookies', 1);
ini_set('session.cookie_httponly', 1); ini_set('session.cookie_httponly', 1);
$this->custom_handler = boolval(get_config('system', 'session_custom', false));
/* /*
* Set our session storage functions. * Set our session storage functions.
*/ */
if ($this->custom_handler) {
/* Custom handler (files, memached, redis..) */
$session_save_handler = strval(get_config('system', 'session_save_handler', Null));
$session_save_path = strval(get_config('system', 'session_save_path', Null));
$session_gc_probability = intval(get_config('system', 'session_gc_probability', 1));
$session_gc_divisor = intval(get_config('system', 'session_gc_divisor', 100));
if (!$session_save_handler || !$session_save_path) {
logger('Session save handler or path not set.',LOGGER_NORMAL,LOG_ERR);
} else {
ini_set('session.save_handler', $session_save_handler);
ini_set('session.save_path', $session_save_path);
ini_set('session.gc_probability', $session_gc_probability);
ini_set('session.gc_divisor', $session_gc_divisor);
}
} else {
$handler = new \Zotlabs\Web\SessionHandler(); $handler = new \Zotlabs\Web\SessionHandler();
$this->handler = $handler; $this->handler = $handler;
@ -35,7 +54,7 @@ class Session {
$x = session_set_save_handler($handler,false); $x = session_set_save_handler($handler,false);
if(! $x) if(! $x)
logger('Session save handler initialisation failed.',LOGGER_NORMAL,LOG_ERR); logger('Session save handler initialisation failed.',LOGGER_NORMAL,LOG_ERR);
};
// Force cookies to be secure (https only) if this site is SSL enabled. // Force cookies to be secure (https only) if this site is SSL enabled.
// Must be done before session_start(). // Must be done before session_start().
@ -86,15 +105,16 @@ class Session {
$arr = session_get_cookie_params(); $arr = session_get_cookie_params();
if($this->handler && $this->session_started) { if(($this->handler || $this->custom_handler) && $this->session_started) {
session_regenerate_id(true); session_regenerate_id(true);
// force SessionHandler record creation with the new session_id // force SessionHandler record creation with the new session_id
// which occurs as a side effect of read() // which occurs as a side effect of read()
if (! $this->custom_handler) {
$this->handler->read(session_id()); $this->handler->read(session_id());
} }
}
else else
logger('no session handler'); logger('no session handler');