ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilSessionReminderCheck.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
10{
15 public function getJsonResponse(string $sessionIdHash) : string
16 {
22 global $DIC;
23
24 $ilDB = $DIC['ilDB'];
25 $lng = $DIC['lng'];
26 $ilClientIniFile = $DIC['ilClientIniFile'];
27
28 $GLOBALS['DIC']->logger()->auth()->debug('Session reminder call for session id hash: ' . $sessionIdHash);
29
30 // disable session writing and extension of expiration time
32
33 $response = array('remind' => false);
34
35 $res = $ilDB->queryF(
36 '
37 SELECT expires, user_id, data
38 FROM usr_session
39 WHERE MD5(session_id) = %s
40 ',
41 ['text'],
42 [$sessionIdHash]
43 );
44
45 $num = (int) $ilDB->numRows($res);
46
47 if (0 === $num) {
48 $response['message'] = 'ILIAS could not determine the session data.';
49 return json_encode($response);
50 }
51
52 if ($num > 1) {
53 $response['message'] = 'The determined session data is not unique.';
54 return json_encode($response);
55 }
56
57 $data = $ilDB->fetchAssoc($res);
58 if (!$this->isAuthenticatedUsrSession($data)) {
59 $response['message'] = 'ILIAS could not fetch the session data or the corresponding user is no more authenticated.';
60 return json_encode($response);
61 }
62
63 $expirationTime = $data['expires'];
64 if (null === $expirationTime) {
65 $response['message'] = 'ILIAS could not determine the expiration time from the session data.';
66 return json_encode($response);
67 }
68
69 if ($this->isSessionAlreadyExpired((int) $expirationTime)) {
70 $response['message'] = 'The session is already expired. The client should have received a remind command before.';
71 return json_encode($response);
72 }
73
76
77 $reminderTime = $expirationTime - ((int) max(
79 (float) $ilUser->getPref('session_reminder_lead_time')
80 )) * 60;
81 if ($reminderTime > time()) {
82 // session will expire in <lead_time> minutes
83 $response['message'] = 'Lead time not reached, yet. Current time: ' .
84 date('Y-m-d H:i:s', time()) . ', Reminder time: ' . date('Y-m-d H:i:s', $reminderTime);
85 return json_encode($response);
86 }
87
88 $dateTime = new ilDateTime($expirationTime, IL_CAL_UNIX);
89 switch ($ilUser->getTimeFormat()) {
91 $formatted_expiration_time = $dateTime->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
92 break;
93
95 default:
96 $formatted_expiration_time = $dateTime->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
97 break;
98 }
99
100 $response = array(
101 'extend_url' => './ilias.php?baseClass=ilDashboardGUI',
102 'txt' => str_replace(
103 "\\n",
104 '%0A',
105 sprintf(
106 $lng->txt('session_reminder_alert'),
107 ilDatePresentation::secondsToString($expirationTime - time()),
108 $formatted_expiration_time,
109 $ilClientIniFile->readVariable('client', 'name') . ' | ' . ilUtil::_getHttpPath()
110 )
111 ),
112 'remind' => true
113 );
114
115 return json_encode($response);
116 }
117
122 protected function isSessionAlreadyExpired(int $expirationTime) : bool
123 {
124 return $expirationTime < time();
125 }
126
131 protected function isAuthenticatedUsrSession(?array $data) : bool
132 {
133 return (
134 is_array($data) &&
135 isset($data['user_id']) &&
136 (int) $data['user_id'] > 0 &&
137 (int) $data['user_id'] !== (int) ANONYMOUS_USER_ID
138 );
139 }
140}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
static secondsToString($seconds, $force_with_seconds=false, $a_lng=null)
converts seconds to string: Long: 7 days 4 hour(s) ...
@classDescription Date and time handling
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
isSessionAlreadyExpired(int $expirationTime)
static enableWebAccessWithoutSession($enable_web_access_without_session)
static _getHttpPath()
$response
$lng
foreach($_POST as $key=> $value) $res
global $ilDB
$data
Definition: storeScorm.php:23
$ilUser
Definition: imgupload.php:18
$DIC
Definition: xapitoken.php:46