ILIAS  release_8 Revision v8.24
class.ilSessionReminderCheck.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24use Psr\Http\Message\ResponseInterface;
26use ILIAS\Refinery\Factory as Refinery;
27
29{
31 private Refinery $refinery;
37
38 public function __construct(
40 Refinery $refinery,
45 ClockInterface $utcClock
46 ) {
47 $this->http = $http;
48 $this->refinery = $refinery;
49 $this->lng = $lng;
50 $this->db = $db;
51 $this->clientIni = $clientIni;
52 $this->logger = $logger;
53 $this->clock = $utcClock;
54 }
55
56 public function handle(): ResponseInterface
57 {
58 $sessionIdHash = ilUtil::stripSlashes(
59 $this->http->wrapper()->post()->retrieve(
60 'hash',
61 $this->refinery->kindlyTo()->string()
62 )
63 );
64
65 $this->logger->debug('Session reminder call for session id hash: ' . $sessionIdHash);
66
67 // disable session writing and extension of expiration time
69
70 $response = ['remind' => false];
71
72 $res = $this->db->queryF(
73 'SELECT expires, user_id, data FROM usr_session WHERE MD5(session_id) = %s',
74 ['text'],
75 [$sessionIdHash]
76 );
77
78 $num = $this->db->numRows($res);
79
80 if (0 === $num) {
81 $response['message'] = 'ILIAS could not determine the session data.';
82 return $this->toJsonResponse($response);
83 }
84
85 if ($num > 1) {
86 $response['message'] = 'The determined session data is not unique.';
87 return $this->toJsonResponse($response);
88 }
89
90 $data = $this->db->fetchAssoc($res);
91 if (!$this->isAuthenticatedUsrSession($data)) {
92 $response['message'] = 'ILIAS could not fetch the session data or the corresponding user is no more authenticated.';
93 return $this->toJsonResponse($response);
94 }
95
96 $expirationTime = (int) $data['expires'];
97 if (null === $expirationTime) {
98 $response['message'] = 'ILIAS could not determine the expiration time from the session data.';
99 return $this->toJsonResponse($response);
100 }
101
102 if ($this->isSessionAlreadyExpired($expirationTime)) {
103 $response['message'] = 'The session is already expired. The client should have received a remind command before.';
104 return $this->toJsonResponse($response);
105 }
106
108 $ilUser = ilObjectFactory::getInstanceByObjId((int) $data['user_id'], false);
109 if (!($ilUser instanceof ilObjUser)) {
110 $response['message'] = 'ILIAS could not fetch the session data or the corresponding user is no more authenticated.';
111 return $this->toJsonResponse($response);
112 }
113
114 $reminderTime = $expirationTime - ((int) max(
116 (float) $ilUser->getPref('session_reminder_lead_time')
117 )) * 60;
118 if ($reminderTime > $this->clock->now()->getTimestamp()) {
119 // session will expire in <lead_time> minutes
120 $response['message'] = 'Lead time not reached, yet. Current time: ' .
121 date('Y-m-d H:i:s') . ', Reminder time: ' . date('Y-m-d H:i:s', $reminderTime);
122 return $this->toJsonResponse($response);
123 }
124
125 $dateTime = new ilDateTime($expirationTime, IL_CAL_UNIX);
126 switch ($ilUser->getTimeFormat()) {
128 $formatted_expiration_time = $dateTime->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
129 break;
130
132 default:
133 $formatted_expiration_time = $dateTime->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
134 break;
135 }
136
137 $response = [
138 'extend_url' => './ilias.php?baseClass=ilDashboardGUI',
139 'txt' => str_replace(
140 "\\n",
141 '%0A',
142 sprintf(
143 $this->lng->txt('session_reminder_alert'),
144 ilDatePresentation::secondsToString($expirationTime - $this->clock->now()->getTimestamp()),
145 $formatted_expiration_time,
146 $this->clientIni->readVariable('client', 'name') . ' | ' . ilUtil::_getHttpPath()
147 )
148 ),
149 'remind' => true
150 ];
151
152 return $this->toJsonResponse($response);
153 }
154
159 private function toJsonResponse($data): ResponseInterface
160 {
161 return $this->http->response()
162 ->withHeader(ResponseHeader::CONTENT_TYPE, 'application/json')
163 ->withBody(Streams::ofString(json_encode($data, JSON_THROW_ON_ERROR)));
164 }
165
166 private function isSessionAlreadyExpired(int $expirationTime): bool
167 {
168 return $expirationTime < $this->clock->now()->getTimestamp();
169 }
170
171 private function isAuthenticatedUsrSession(?array $data): bool
172 {
173 return (
174 is_array($data) &&
175 isset($data['user_id']) &&
176 (int) $data['user_id'] > 0 &&
177 (int) $data['user_id'] !== ANONYMOUS_USER_ID
178 );
179 }
180}
Builds data types.
Definition: Factory.php:21
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
return true
static secondsToString(int $seconds, bool $force_with_seconds=false, ?ilLanguage $a_lng=null)
converts seconds to string: Long: 7 days 4 hour(s) ...
@classDescription Date and time handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
Component logger with individual log levels by component id.
User class.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
__construct(GlobalHttpState $http, Refinery $refinery, ilLanguage $lng, ilDBInterface $db, ilIniFile $clientIni, ilLogger $logger, ClockInterface $utcClock)
isSessionAlreadyExpired(int $expirationTime)
static enableWebAccessWithoutSession(bool $enable_web_access_without_session)
static _getHttpPath()
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
const ANONYMOUS_USER_ID
Definition: constants.php:27
$ilUser
Definition: imgupload.php:34
Interface GlobalHttpState.
Interface ResponseHeader.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
static http()
Fetches the global http state from ILIAS.
$response