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