ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSessionReminderCheck.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
10 {
15  public function getJsonResponse($sessionId)
16  {
23  global $ilDB, $ilUser, $lng, $ilClientIniFile;
24 
25  include_once 'Services/JSON/classes/class.ilJsonUtil.php';
26 
27  $response = array('remind' => false);
28 
29  $res = $ilDB->queryF('
30  SELECT expires, user_id, data
31  FROM usr_session
32  WHERE session_id = %s',
33  array('text'),
34  array($sessionId)
35  );
36 
37  $num = $ilDB->numRows($res);
38 
39  if($num > 1)
40  {
41  $response['message'] = 'The determined session data is not unique.';
42  return ilJsonUtil::encode($response);
43  }
44 
45  if($num == 0)
46  {
47  $response['message'] = 'ILIAS could not determine the session data.';
48  return ilJsonUtil::encode($response);
49  }
50 
51  $data = $ilDB->fetchAssoc($res);
52  if(!$this->isAuthenticatedUsrSession($data))
53  {
54  $response['message'] = 'ILIAS could not fetch the session data or the corresponding user is no more authenticated.';
55  return ilJsonUtil::encode($response);
56  }
57 
58  $session = ilUtil::unserializeSession($data['data']);
59  $idletime = null;
60  foreach((array)$session as $key => $entry)
61  {
62  if(strpos($key, '_auth__') === 0)
63  {
64  $idletime = $entry['idle'];
65  break;
66  }
67  }
68 
69  if(null === $idletime)
70  {
71  $response['message'] = 'ILIAS could not determine the idle time from the session data.';
72  return ilJsonUtil::encode($response);
73  }
74 
75  $expiretime = $idletime + ilSession::getIdleValue();
76  if($this->isSessionAlreadyExpired($expiretime))
77  {
78  $response['message'] = 'The session is already expired. The client should have received a remind command before.';
79  return ilJsonUtil::encode($response);
80  }
81 
85  $ilUser = ilObjectFactory::getInstanceByObjId($data['user_id']);
86 
87  include_once './Services/Authentication/classes/class.ilSessionReminder.php';
88  $remind_time = $expiretime - max(ilSessionReminder::MIN_LEAD_TIME, (float)$ilUser->getPref('session_reminder_lead_time')) * 60;
89  if($remind_time > time())
90  {
91  // session will expire in <lead_time> minutes
92  $response['message'] = 'Lead time not reached, yet. Current time: ' . date('Y-m-d H:i:s', time()) . ', Reminder time: ' . date('Y-m-d H:i:s', $remind_time);
93  return ilJsonUtil::encode($response);
94  }
95 
96  $dateTime = new ilDateTime($expiretime, IL_CAL_UNIX);
97  switch($ilUser->getTimeFormat())
98  {
100  $formatted_expiration_time = $dateTime->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
101  break;
102 
104  default:
105  $formatted_expiration_time = $dateTime->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
106  break;
107  }
108 
109  $response = array(
110  'extend_url' => './ilias.php?baseClass=ilPersonalDesktopGUI',
111  'txt' => str_replace("\\n", '%0A', sprintf($lng->txt('session_reminder_alert'), ilFormat::_secondsToString($expiretime - time()), $formatted_expiration_time, $ilClientIniFile->readVariable('client', 'name') . ' | ' . ilUtil::_getHttpPath())),
112  'remind' => true
113  );
114 
115  return ilJsonUtil::encode($response);
116  }
117 
122  protected function isSessionAlreadyExpired($expiretime)
123  {
124  return $expiretime < time();
125  }
126 
131  protected function isAuthenticatedUsrSession($data)
132  {
133  return is_array($data) && isset($data['user_id']) && $data['user_id'] > 0 && $data['user_id'] != ANONYMOUS_USER_ID;
134  }
135 }