19 declare(strict_types=1);
55 public static function _getData(
string $a_session_id): string
65 $q =
"SELECT data FROM usr_session WHERE session_id = " .
66 $ilDB->quote($a_session_id,
"text");
68 $rec =
$ilDB->fetchAssoc($set);
69 if (!is_array($rec)) {
74 return (
string) $rec[
"data"];
88 $query =
'SELECT expires FROM usr_session WHERE session_id = ' .
89 $ilDB->quote($a_session_id,
'text');
92 return (
int) $row->expires;
97 public static function _writeData(
string $a_session_id,
string $a_data):
bool 102 $ilDB = $DIC[
'ilDB'];
104 $ilClientIniFile = $DIC[
'ilClientIniFile'];
106 if (self::isWebAccessWithoutSessionEnabled()) {
113 if (!$a_session_id) {
127 if ($ilClientIniFile->readVariable(
'session',
'save_ip')) {
131 if (self::_exists($a_session_id)) {
148 if (class_exists(
'ilContext')) {
152 $insert_fields = implode(
', ', array_keys($fields));
153 $insert_values = implode(
156 static fn(
string $type, $value):
string =>
$ilDB->quote($value, $type),
157 array_column($fields, 0),
158 array_column($fields, 1)
162 $update_fields = array_filter(
164 static fn(
string $field):
bool => !in_array($field, [
'session_id',
'user_id',
'createtime'],
true),
167 $update_values = implode(
170 static fn(
string $field,
string $type, $value):
string => $field .
' = ' .
$ilDB->quote(
174 array_keys($update_fields),
175 array_column($update_fields, 0),
176 array_column($update_fields, 1)
181 'INSERT INTO usr_session (' . $insert_fields .
') ' 182 .
'VALUES (' . $insert_values .
') ' 183 .
'ON DUPLICATE KEY UPDATE ' . $update_values
187 $type = (
int) $fields[
'type'][1];
190 $fields[
'session_id'][1],
192 $fields[
'createtime'][1],
193 $fields[
'user_id'][1]
198 if (!$DIC->cron()->manager()->isJobActive(
'auth_destroy_expired_sessions')) {
199 $r = new \Random\Randomizer();
200 if (
$r->getInt(0, 50) === 2) {
202 self::_destroyExpiredSessions();
218 public static function _exists(
string $a_session_id): bool
220 if (!$a_session_id) {
225 $ilDB = $DIC[
'ilDB'];
227 $q =
"SELECT 1 FROM usr_session WHERE session_id = " .
$ilDB->quote($a_session_id,
"text");
230 return $ilDB->numRows($set) > 0;
240 public static function _destroy($a_session_id, ?
int $a_closing_context =
null, $a_expired_at =
null): bool
244 $ilDB = $DIC[
'ilDB'];
246 if (!$a_closing_context) {
247 $a_closing_context = self::$closing_context;
252 if (!is_array($a_session_id)) {
253 $q =
"DELETE FROM usr_session WHERE session_id = " .
254 $ilDB->quote($a_session_id,
"text");
258 $a_session_id = array_keys($a_session_id);
260 $q =
"DELETE FROM usr_session WHERE " .
261 $ilDB->in(
"session_id", $a_session_id,
false,
"text");
271 if ($DIC->http()->wrapper()->cookie()->has(session_name()) &&
272 $DIC->http()->wrapper()->cookie()->retrieve(
274 $DIC->refinery()->kindlyTo()->string()
275 ) === $a_session_id) {
276 $cookieJar = $DIC->http()->cookieJar()->without(session_name());
277 $cookieJar->renderIntoResponseHeader($DIC->http()->response());
297 $ilDB = $DIC[
'ilDB'];
299 $q =
"DELETE FROM usr_session WHERE user_id = " .
300 $ilDB->quote($a_user_id,
"integer");
314 $ilDB = $DIC[
'ilDB'];
320 $ids[$row[
'session_id']] = (
int) $row[
'expires'];
323 self::_destroy($ids, self::SESSION_CLOSE_EXPIRE,
true);
335 public static function _duplicate(
string $a_session_id): string
339 $ilDB = $DIC[
'ilDB'];
342 $new_session = $a_session_id;
344 $new_session = md5($new_session);
345 $q =
"SELECT * FROM usr_session WHERE " .
346 "session_id = " .
$ilDB->quote($new_session,
"text");
350 $query =
"SELECT * FROM usr_session " .
351 "WHERE session_id = " .
$ilDB->quote($a_session_id,
"text");
355 self::_writeData($new_session, $row->data);
367 return time() + self::getIdleValue();
377 $ilClientIniFile = $DIC[
'ilClientIniFile'];
379 return (
int) $ilClientIniFile->readVariable(
'session',
'expire');
387 return self::getIdleValue();
393 public static function set(
string $a_var, $a_val):
void 395 $_SESSION[$a_var] = $a_val;
401 public static function get(
string $a_var)
403 return $_SESSION[$a_var] ??
null;
406 public static function has($a_var): bool
408 return isset($_SESSION[$a_var]);
414 public static function clear(
string $a_var): void
416 if (isset($_SESSION[$a_var])) {
417 unset($_SESSION[$a_var]);
423 return print_r($_SESSION,
true);
431 self::$closing_context = $a_context;
439 return self::$closing_context;
449 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.
static usesHTTP()
Uses HTTP aka browser.
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.
static getIdleValue()
Returns the idle time in seconds.
static getExpireValue()
Returns the expiration timestamp in seconds.
static _destroyExpiredSessions()
Destroy expired sessions.
static aggretateRaw(int $a_now)
Aggregate raw session data (older than given time)
const SESSION_CLOSE_EXPIRE
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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).
static isWebAccessWithoutSessionEnabled()
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)
static getType()
Get context type.
static getClosingContext()
get closing context (for statistics)
static getSessionExpireValue()
Returns the session expiration value.
static clear(string $a_var)
static int $closing_context