19 declare(strict_types=1);
76 public static function _getData(
string $a_session_id): string
86 $q =
"SELECT data FROM usr_session WHERE session_id = " .
87 $ilDB->quote($a_session_id,
"text");
88 $set =
$ilDB->query($q);
89 $rec =
$ilDB->fetchAssoc($set);
90 if (!is_array($rec)) {
95 return (
string) $rec[
"data"];
107 $ilDB = $DIC[
'ilDB'];
109 $query =
'SELECT expires FROM usr_session WHERE session_id = ' .
110 $ilDB->quote($a_session_id,
'text');
113 return (
int) $row->expires;
118 public static function _writeData(
string $a_session_id,
string $a_data):
bool 123 $ilDB = $DIC[
'ilDB'];
125 $ilClientIniFile = $DIC[
'ilClientIniFile'];
127 if (self::isWebAccessWithoutSessionEnabled()) {
134 if (!$a_session_id) {
148 if ($ilClientIniFile->readVariable(
'session',
'save_ip')) {
152 if (self::_exists($a_session_id)) {
169 if (class_exists(
'ilContext')) {
173 $insert_fields = implode(
', ', array_keys($fields));
174 $insert_values = implode(
177 static fn (
string $type, $value):
string =>
$ilDB->quote($value, $type),
178 array_column($fields, 0),
179 array_column($fields, 1)
183 $update_fields = array_filter(
185 static fn (
string $field):
bool => !in_array($field, [
'session_id',
'user_id',
'createtime'],
true),
188 $update_values = implode(
191 static fn (
string $field,
string $type, $value):
string => $field .
' = ' .
$ilDB->quote(
195 array_keys($update_fields),
196 array_column($update_fields, 0),
197 array_column($update_fields, 1)
202 'INSERT INTO usr_session (' . $insert_fields .
') ' 203 .
'VALUES (' . $insert_values .
') ' 204 .
'ON DUPLICATE KEY UPDATE ' . $update_values
208 $type = (
int) $fields[
'type'][1];
211 $fields[
'session_id'][1],
213 $fields[
'createtime'][1],
214 $fields[
'user_id'][1]
221 if ($random->int(0, 50) === 2) {
223 self::_destroyExpiredSessions();
238 public static function _exists(
string $a_session_id): bool
240 if (!$a_session_id) {
245 $ilDB = $DIC[
'ilDB'];
247 $q =
"SELECT 1 FROM usr_session WHERE session_id = " .
$ilDB->quote($a_session_id,
"text");
248 $set =
$ilDB->query($q);
250 return $ilDB->numRows($set) > 0;
260 public static function _destroy($a_session_id, ?
int $a_closing_context = null, $a_expired_at = null): bool
264 $ilDB = $DIC[
'ilDB'];
266 if (!$a_closing_context) {
267 $a_closing_context = self::$closing_context;
272 if (!is_array($a_session_id)) {
273 $q =
"DELETE FROM usr_session WHERE session_id = " .
274 $ilDB->quote($a_session_id,
"text");
278 $a_session_id = array_keys($a_session_id);
280 $q =
"DELETE FROM usr_session WHERE " .
281 $ilDB->in(
"session_id", $a_session_id,
false,
"text");
286 $ilDB->manipulate($q);
290 if ($DIC->http()->wrapper()->cookie()->has(session_name()) &&
291 $DIC->http()->wrapper()->cookie()->retrieve(session_name(), $DIC->refinery()->kindlyTo()->string()) === $a_session_id) {
292 $cookieJar = $DIC->http()->cookieJar()->without(session_name());
293 $cookieJar->renderIntoResponseHeader($DIC->http()->response());
312 $ilDB = $DIC[
'ilDB'];
314 $q =
"DELETE FROM usr_session WHERE user_id = " .
315 $ilDB->quote($a_user_id,
"integer");
316 $ilDB->manipulate($q);
329 $ilDB = $DIC[
'ilDB'];
331 $q =
"SELECT session_id,expires FROM usr_session WHERE expires < " .
332 $ilDB->quote(time(),
"integer");
336 $ids[$row[
"session_id"]] = $row[
"expires"];
339 self::_destroy($ids, self::SESSION_CLOSE_EXPIRE,
true);
351 public static function _duplicate(
string $a_session_id): string
355 $ilDB = $DIC[
'ilDB'];
358 $new_session = $a_session_id;
360 $new_session = md5($new_session);
361 $q =
"SELECT * FROM usr_session WHERE " .
362 "session_id = " .
$ilDB->quote($new_session,
"text");
366 $query =
"SELECT * FROM usr_session " .
367 "WHERE session_id = " .
$ilDB->quote($a_session_id,
"text");
371 self::_writeData($new_session, $row->data);
387 public static function getExpireValue(
bool $fixedMode =
false):
int 393 return time() + self::getIdleValue($fixedMode);
398 if (
$ilSetting->get(
'session_handling_type', (
string) self::SESSION_HANDLING_FIXED) === (string) self::SESSION_HANDLING_FIXED) {
399 return time() + self::getIdleValue($fixedMode);
402 if (
$ilSetting->get(
'session_handling_type', (
string) self::SESSION_HANDLING_FIXED) === (string) self::SESSION_HANDLING_LOAD_DEPENDENT) {
405 return time() + $max_idle * 60;
422 $ilClientIniFile = $DIC[
'ilClientIniFile'];
424 if ($fixedMode ||
$ilSetting->get(
'session_handling_type', (
string) self::SESSION_HANDLING_FIXED) === (string) self::SESSION_HANDLING_FIXED) {
426 return (
int) $ilClientIniFile->readVariable(
'session',
'expire');
429 if (
$ilSetting->get(
'session_handling_type', (
string) self::SESSION_HANDLING_FIXED) === (string) self::SESSION_HANDLING_LOAD_DEPENDENT) {
445 return self::getIdleValue(
true);
451 public static function set(
string $a_var, $a_val):
void 453 $_SESSION[$a_var] = $a_val;
459 public static function get(
string $a_var)
461 return $_SESSION[$a_var] ?? null;
464 public static function has($a_var): bool
466 return isset($_SESSION[$a_var]);
472 public static function clear(
string $a_var): void
474 if (isset($_SESSION[$a_var])) {
475 unset($_SESSION[$a_var]);
481 return print_r($_SESSION,
true);
489 self::$closing_context = $a_context;
497 return self::$closing_context;
507 return self::$enable_web_access_without_session;
static enableWebAccessWithoutSession(bool $enable_web_access_without_session)
static _duplicate(string $a_session_id)
Duplicate session.
static _destroyByUserId(int $a_user_id)
Destroy session.
static createRawEntry(string $a_session_id, int $a_session_type, int $a_timestamp, int $a_user_id)
Create raw data entry.
static _getData(string $a_session_id)
Get session data from table.
const SESSION_CLOSE_INACTIVE
static _exists(string $a_session_id)
Check whether session exists.
static bool $enable_web_access_without_session
const SESSION_CLOSE_LOGIN
static isSessionMainContext()
Context that are not only temporary in a session (e.g.
static lookupExpireTime(string $a_session_id)
Lookup expire time for a specific session.
const SESSION_HANDLING_FIXED
static _destroyExpiredSessions()
Destroy expired sessions.
static aggretateRaw(int $a_now)
Aggregate raw session data (older than given time)
const SESSION_CLOSE_EXPIRE
static closeRawEntry($a_session_id, ?int $a_context=null, $a_expired_at=null)
Close raw data entry.
static destroySession($a_session_id)
Destroy session(s).
get(string $key, Refinery\Transformation $t)
Get passed parameter, if not data passed, get key from http request.
static isWebAccessWithoutSessionEnabled()
const SESSION_CLOSE_LIMIT
const SESSION_CLOSE_PUBLIC
static _destroy($a_session_id, ?int $a_closing_context=null, $a_expired_at=null)
Destroy session.
static array $session_types_controlled
const SESSION_CLOSE_SIMUL
static setClosingContext(int $a_context)
set closing context (for statistics)
Wrapper for generation of random numbers, strings, bytes.
static getIdleValue(bool $fixedMode=false)
Returns the idle time in seconds.
const SESSION_HANDLING_LOAD_DEPENDENT
static getType()
Get context type.
static getClosingContext()
get closing context (for statistics)
static getSessionExpireValue()
Returns the session expiration value.
static clear(string $a_var)
const SESSION_CLOSE_FIRST
static int $closing_context