2019-01-31 00:05:43 +08:00
|
|
|
<?php
|
|
|
|
// ZM session start function support timestamp management
|
|
|
|
function zm_session_start() {
|
|
|
|
|
2020-08-03 22:55:54 +08:00
|
|
|
if ( ini_get('session.name') != 'ZMSESSID' ) {
|
|
|
|
// Make sure use_strict_mode is enabled.
|
|
|
|
// use_strict_mode is mandatory for security reasons.
|
|
|
|
ini_set('session.use_strict_mode', 1);
|
2019-01-31 00:05:43 +08:00
|
|
|
|
2020-08-03 22:55:54 +08:00
|
|
|
$currentCookieParams = session_get_cookie_params();
|
|
|
|
$currentCookieParams['lifetime'] = ZM_COOKIE_LIFETIME;
|
|
|
|
$currentCookieParams['httponly'] = true;
|
2020-08-08 21:58:18 +08:00
|
|
|
if ( version_compare(phpversion(), '7.3.0', '<') ) {
|
|
|
|
session_set_cookie_params(
|
|
|
|
$currentCookieParams['lifetime'],
|
|
|
|
$currentCookieParams['path'],
|
|
|
|
$currentCookieParams['domain'],
|
|
|
|
$currentCookieParams['secure'],
|
2020-08-09 02:27:37 +08:00
|
|
|
$currentCookieParams['httponly']
|
2020-08-08 21:58:18 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
# samesite was introduced in 7.3.0
|
|
|
|
$currentCookieParams['samesite'] = 'Strict';
|
|
|
|
session_set_cookie_params($currentCookieParams);
|
|
|
|
}
|
2019-01-31 00:05:43 +08:00
|
|
|
|
2020-08-03 22:55:54 +08:00
|
|
|
ini_set('session.name', 'ZMSESSID');
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug('Setting cookie parameters to '.print_r($currentCookieParams, true));
|
2020-08-03 22:55:54 +08:00
|
|
|
}
|
2019-01-31 00:05:43 +08:00
|
|
|
session_start();
|
2019-08-20 21:46:53 +08:00
|
|
|
$_SESSION['remoteAddr'] = $_SERVER['REMOTE_ADDR']; // To help prevent session hijacking
|
2019-09-05 00:14:32 +08:00
|
|
|
$now = time();
|
2019-02-06 00:45:09 +08:00
|
|
|
// Do not allow to use expired session ID
|
2019-09-05 00:14:32 +08:00
|
|
|
if ( !empty($_SESSION['last_time']) && ($_SESSION['last_time'] < ($now - 180)) ) {
|
2020-08-03 22:55:54 +08:00
|
|
|
ZM\Info('Destroying session due to timeout.');
|
2019-01-31 00:05:43 +08:00
|
|
|
session_destroy();
|
|
|
|
session_start();
|
2019-09-05 00:14:32 +08:00
|
|
|
} else if ( !empty($_SESSION['generated_at']) ) {
|
|
|
|
if ( $_SESSION['generated_at']<($now-(ZM_COOKIE_LIFETIME/2)) ) {
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug('Regenerating session because generated_at ' . $_SESSION['generated_at'] . ' < ' . $now . '-'.ZM_COOKIE_LIFETIME.'/2 = '.($now-ZM_COOKIE_LIFETIME/2));
|
2019-09-05 00:14:32 +08:00
|
|
|
zm_session_regenerate_id();
|
|
|
|
}
|
2019-01-31 00:05:43 +08:00
|
|
|
}
|
2019-02-06 00:45:09 +08:00
|
|
|
} // function zm_session_start()
|
2019-01-31 00:05:43 +08:00
|
|
|
|
2019-08-20 21:46:53 +08:00
|
|
|
// session regenerate id function
|
|
|
|
// Assumes that zm_session_start has been called previously
|
2019-01-31 00:05:43 +08:00
|
|
|
function zm_session_regenerate_id() {
|
|
|
|
if ( session_status() != PHP_SESSION_ACTIVE ) {
|
|
|
|
session_start();
|
|
|
|
}
|
2019-01-31 05:08:09 +08:00
|
|
|
|
2019-01-31 00:05:43 +08:00
|
|
|
// Set deleted timestamp. Session data must not be deleted immediately for reasons.
|
|
|
|
$_SESSION['last_time'] = time();
|
|
|
|
// Finish session
|
2019-01-31 05:08:09 +08:00
|
|
|
session_write_close();
|
|
|
|
|
2019-01-31 00:05:43 +08:00
|
|
|
session_start();
|
2019-01-31 05:08:09 +08:00
|
|
|
session_regenerate_id();
|
2019-02-06 00:45:09 +08:00
|
|
|
unset($_SESSION['last_time']);
|
2019-09-05 00:14:32 +08:00
|
|
|
$_SESSION['generated_at'] = time();
|
2019-02-06 00:45:09 +08:00
|
|
|
} // function zm_session_regenerate_id()
|
2019-01-31 00:05:43 +08:00
|
|
|
|
2019-01-31 01:52:01 +08:00
|
|
|
function is_session_started() {
|
|
|
|
if ( php_sapi_name() !== 'cli' ) {
|
|
|
|
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
|
|
|
|
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
|
|
|
|
} else {
|
|
|
|
return session_id() === '' ? FALSE : TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Warning("php_sapi_name === 'cli'");
|
|
|
|
}
|
|
|
|
return FALSE;
|
2019-02-06 00:45:09 +08:00
|
|
|
} // function is_session_started()
|
2019-01-31 01:52:01 +08:00
|
|
|
|
|
|
|
function zm_session_clear() {
|
|
|
|
session_start();
|
|
|
|
$_SESSION = array();
|
|
|
|
if ( ini_get('session.use_cookies') ) {
|
|
|
|
$p = session_get_cookie_params();
|
|
|
|
# Update the cookie to expire in the past.
|
|
|
|
setcookie(session_name(), '', time() - 31536000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
|
|
|
|
}
|
|
|
|
session_unset();
|
|
|
|
session_destroy();
|
2019-08-20 21:46:53 +08:00
|
|
|
session_write_close();
|
|
|
|
session_start();
|
2019-02-06 00:45:09 +08:00
|
|
|
} // function zm_session_clear()
|
2020-10-03 02:50:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Session {
|
|
|
|
private $db;
|
|
|
|
public function __construct() {
|
|
|
|
global $dbConn;
|
|
|
|
$this->db = $dbConn;
|
|
|
|
|
|
|
|
// Set handler to overide SESSION
|
|
|
|
session_set_save_handler(
|
|
|
|
array($this, '_open'),
|
|
|
|
array($this, '_close'),
|
|
|
|
array($this, '_read'),
|
|
|
|
array($this, '_write'),
|
|
|
|
array($this, '_destroy'),
|
|
|
|
array($this, '_gc')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start the session
|
|
|
|
//zm_session_start();
|
|
|
|
}
|
|
|
|
public function _open() {
|
|
|
|
return $this->db ? true : false;
|
|
|
|
}
|
|
|
|
public function _close(){
|
|
|
|
// The example code closed the db connection.. I don't think we care to.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public function _read($id){
|
|
|
|
$sth = $this->db->prepare('SELECT data FROM Sessions WHERE id = :id');
|
|
|
|
$sth->bindParam(':id', $id, PDO::PARAM_STR, 32);
|
|
|
|
|
|
|
|
if ( $sth->execute() and ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) ) {
|
|
|
|
return $row['data'];
|
|
|
|
}
|
|
|
|
// Return an empty string
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
public function _write($id, $data){
|
|
|
|
// Create time stamp
|
|
|
|
$access = time();
|
|
|
|
|
|
|
|
$sth = $this->db->prepare('REPLACE INTO Sessions VALUES (:id, :access, :data)');
|
|
|
|
|
|
|
|
$sth->bindParam(':id', $id, PDO::PARAM_STR, 32);
|
|
|
|
$sth->bindParam(':access', $access, PDO::PARAM_INT);
|
|
|
|
$sth->bindParam(':data', $data);
|
|
|
|
|
|
|
|
return $sth->execute() ? true : false;
|
|
|
|
}
|
|
|
|
public function _destroy($id) {
|
|
|
|
$sth = $this->db->prepare('DELETE FROM Sessions WHERE Id = :id');
|
|
|
|
$sth->bindParam(':id', $id, PDO::PARAM_STR, 32);
|
|
|
|
return $sth->execute() ? true : false;
|
|
|
|
}
|
|
|
|
public function _gc($max) {
|
|
|
|
// Calculate what is to be deemed old
|
|
|
|
$old = time() - $max;
|
|
|
|
$sth = $this->db->prepare('DELETE * FROM Sessions WHERE access < :old');
|
|
|
|
$sth->bindParam(':old', $old, PDO::PARAM_INT);
|
|
|
|
return $sth->execute() ? true : false;
|
|
|
|
}
|
|
|
|
} # end class Session
|
|
|
|
|
|
|
|
$session = new Session;
|
2019-01-31 00:05:43 +08:00
|
|
|
?>
|