ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilSession.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once('Services/Authentication/classes/class.ilSessionControl.php');
5 require_once('Services/Authentication/classes/class.ilSessionStatistics.php');
6 require_once('Services/Authentication/classes/class.ilSessionIStorage.php');
7 
15 class ilSession
16 {
25 
34 
40  const SESSION_CLOSE_USER = 1; // manual logout
41  const SESSION_CLOSE_EXPIRE = 2; // has expired
42  const SESSION_CLOSE_FIRST = 3; // kicked by session control (first abidencer)
43  const SESSION_CLOSE_IDLE = 4; // kickey by session control (ilde time)
44  const SESSION_CLOSE_LIMIT = 5; // kicked by session control (limit reached)
45  const SESSION_CLOSE_LOGIN = 6; // anonymous => login
46  const SESSION_CLOSE_PUBLIC = 7; // => anonymous
47  const SESSION_CLOSE_TIME = 8; // account time limit reached
48  const SESSION_CLOSE_IP = 9; // wrong ip
49  const SESSION_CLOSE_SIMUL = 10; // simultaneous login
50  const SESSION_CLOSE_INACTIVE = 11; // inactive account
51  const SESSION_CLOSE_CAPTCHA = 12; // invalid captcha
52 
53  private static $closing_context = null;
54 
58  protected static $enable_web_access_without_session = false;
59 
69  public static function _getData($a_session_id)
70  {
71  if (!$a_session_id) {
72  // fix for php #70520
73  return '';
74  }
75  global $DIC;
76 
77  $ilDB = $DIC['ilDB'];
78 
79  $q = "SELECT data FROM usr_session WHERE session_id = " .
80  $ilDB->quote($a_session_id, "text");
81  $set = $ilDB->query($q);
82  $rec = $ilDB->fetchAssoc($set);
83 
84  // fix for php #70520
85  return (string) $rec["data"];
86  }
87 
94  public static function lookupExpireTime($a_session_id)
95  {
96  global $DIC;
97 
98  $ilDB = $DIC['ilDB'];
99 
100  $query = 'SELECT expires FROM usr_session WHERE session_id = ' .
101  $ilDB->quote($a_session_id, 'text');
102  $res = $ilDB->query($query);
103  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
104  return (int) $row->expires;
105  }
106  return 0;
107  }
108 
109 
116  public static function _writeData($a_session_id, $a_data)
117  {
118  global $DIC;
119 
120  $ilDB = $DIC['ilDB'];
121  $ilClientIniFile = $DIC['ilClientIniFile'];
122 
123  if (self::isWebAccessWithoutSessionEnabled()) {
124  // Prevent session data written for web access checker
125  // when no cookie was sent (e.g. for pdf files linking others).
126  // This would result in new session records for each request.
127  return true;
128  }
129 
130  if (!$a_session_id) {
131  return true;
132  }
133 
134  $now = time();
135 
136  // prepare session data
137  $fields = array(
138  "user_id" => array("integer", (int) $_SESSION['_authsession_user_id']),
139  "expires" => array("integer", self::getExpireValue()),
140  "data" => array("clob", $a_data),
141  "ctime" => array("integer", $now),
142  "type" => array("integer", (int) $_SESSION["SessionType"])
143  );
144  if ($ilClientIniFile->readVariable("session", "save_ip")) {
145  $fields["remote_addr"] = array("text", $_SERVER["REMOTE_ADDR"]);
146  }
147 
148  if (ilSession::_exists($a_session_id)) {
149  // note that we do this only when inserting the new record
150  // updating may get us other contexts for the same session, especially ilContextWAC, which we do not want
151  if (class_exists("ilContext")) {
153  $fields["context"] = array("text", ilContext::getType());
154  }
155  }
156 
157  $ilDB->update(
158  "usr_session",
159  $fields,
160  array("session_id" => array("text", $a_session_id))
161  );
162  } else {
163  $fields["session_id"] = array("text", $a_session_id);
164  $fields["createtime"] = array("integer", $now);
165 
166  // note that we do this only when inserting the new record
167  // updating may get us other contexts for the same session, especially ilContextWAC, which we do not want
168  if (class_exists("ilContext")) {
169  $fields["context"] = array("text", ilContext::getType());
170  }
171 
172  $ilDB->insert("usr_session", $fields);
173 
174  // check type against session control
175  $type = $fields["type"][1];
178  $fields["session_id"][1],
179  $type,
180  $fields["createtime"][1],
181  $fields["user_id"][1]
182  );
183  }
184  }
185 
186  // finally delete deprecated sessions
187  $random = new \ilRandom();
188  if ($random->int(0, 50) == 2) {
189  // get time _before_ destroying expired sessions
190  self::_destroyExpiredSessions();
192  }
193 
194  return true;
195  }
196 
197 
198 
205  public static function _exists($a_session_id)
206  {
207  if (!$a_session_id) {
208  return false;
209  }
210  global $DIC;
211 
212  $ilDB = $DIC['ilDB'];
213 
214  $q = "SELECT 1 FROM usr_session WHERE session_id = " . $ilDB->quote($a_session_id, "text");
215  $set = $ilDB->query($q);
216 
217  return $ilDB->numRows($set) > 0;
218  }
219 
227  public static function _destroy($a_session_id, $a_closing_context = null, $a_expired_at = null)
228  {
229  global $DIC;
230 
231  $ilDB = $DIC['ilDB'];
232 
233  if (!$a_closing_context) {
234  $a_closing_context = self::$closing_context;
235  }
236 
237  ilSessionStatistics::closeRawEntry($a_session_id, $a_closing_context, $a_expired_at);
238 
239 
240  if (!is_array($a_session_id)) {
241  $q = "DELETE FROM usr_session WHERE session_id = " .
242  $ilDB->quote($a_session_id, "text");
243  } else {
244  // array: id => timestamp - so we get rid of timestamps
245  if ($a_expired_at) {
246  $a_session_id = array_keys($a_session_id);
247  }
248  $q = "DELETE FROM usr_session WHERE " .
249  $ilDB->in("session_id", $a_session_id, "", "text");
250  }
251 
252  ilSessionIStorage::destroySession($a_session_id);
253 
254  $ilDB->manipulate($q);
255 
256  return true;
257  }
258 
264  public static function _destroyByUserId($a_user_id)
265  {
266  global $DIC;
267 
268  $ilDB = $DIC['ilDB'];
269 
270  $q = "DELETE FROM usr_session WHERE user_id = " .
271  $ilDB->quote($a_user_id, "integer");
272  $ilDB->manipulate($q);
273 
274  return true;
275  }
276 
280  public static function _destroyExpiredSessions()
281  {
282  global $DIC;
283 
284  $ilDB = $DIC['ilDB'];
285 
286  $q = "SELECT session_id,expires FROM usr_session WHERE expires < " .
287  $ilDB->quote(time(), "integer");
288  $res = $ilDB->query($q);
289  $ids = array();
290  while ($row = $ilDB->fetchAssoc($res)) {
291  $ids[$row["session_id"]] = $row["expires"];
292  }
293  if (sizeof($ids)) {
294  self::_destroy($ids, self::SESSION_CLOSE_EXPIRE, true);
295  }
296 
297  return true;
298  }
299 
306  public static function _duplicate($a_session_id)
307  {
308  global $DIC;
309 
310  $ilDB = $DIC['ilDB'];
311 
312  // Create new session id
313  $new_session = $a_session_id;
314  do {
315  $new_session = md5($new_session);
316  $q = "SELECT * FROM usr_session WHERE " .
317  "session_id = " . $ilDB->quote($new_session, "text");
318  $res = $ilDB->query($q);
319  } while ($ilDB->fetchAssoc($res));
320 
321  $query = "SELECT * FROM usr_session " .
322  "WHERE session_id = " . $ilDB->quote($a_session_id, "text");
323  $res = $ilDB->query($query);
324 
325  while ($row = $ilDB->fetchObject($res)) {
326  ilSession::_writeData($new_session, $row->data);
327  return $new_session;
328  }
329  return false;
330  }
331 
342  public static function getExpireValue($fixedMode = false)
343  {
344  global $DIC;
345 
346  if ($fixedMode) {
347  // fixed session
348  return time() + self::getIdleValue($fixedMode);
349  }
350 
351  $ilSetting = $DIC['ilSetting'];
352  if ($ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_FIXED) {
353  return time() + self::getIdleValue($fixedMode);
354  } elseif ($ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_LOAD_DEPENDENT) {
355  // load dependent session settings
356  return time() + (int) ($ilSetting->get('session_max_idle', ilSessionControl::DEFAULT_MAX_IDLE) * 60);
357  }
358  }
359 
370  public static function getIdleValue($fixedMode = false)
371  {
372  global $DIC;
373 
374  $ilSetting = $DIC['ilSetting'];
375  $ilClientIniFile = $DIC['ilClientIniFile'];
376 
377  if ($fixedMode || $ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_FIXED) {
378  // fixed session
379  return $ilClientIniFile->readVariable('session', 'expire');
380  } elseif ($ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_LOAD_DEPENDENT) {
381  // load dependent session settings
382  return (int) ($ilSetting->get('session_max_idle', ilSessionControl::DEFAULT_MAX_IDLE) * 60);
383  }
384  }
385 
395  public static function getSessionExpireValue()
396  {
397  return self::getIdleValue(true);
398  }
399 
406  public static function _getUsersWithIp($a_ip)
407  {
408  global $DIC;
409 
410  $ilDB = $DIC['ilDB'];
411 
412  $query = "SELECT DISTINCT user_id FROM usr_session"
413  . " WHERE remote_addr = " . $ilDB->quote($a_ip, "text")
414  . " AND user_id > 0";
415  $result = $ilDB->query($query);
416 
417  $users = array();
418  while ($row = $ilDB->fetchObject($result)) {
419  $users[] = $row->user_id;
420  }
421  return $users;
422  }
423 
430  public static function set($a_var, $a_val)
431  {
432  $_SESSION[$a_var] = $a_val;
433  }
434 
441  public static function get($a_var)
442  {
443  return $_SESSION[$a_var];
444  }
445 
452  public static function clear($a_var)
453  {
454  unset($_SESSION[$a_var]);
455  }
456 
462  public static function setClosingContext($a_context)
463  {
464  self::$closing_context = (int) $a_context;
465  }
466 
472  public static function getClosingContext()
473  {
474  return self::$closing_context;
475  }
476 
477 
478 
482  public static function isWebAccessWithoutSessionEnabled()
483  {
484  return (bool) self::$enable_web_access_without_session;
485  }
486 
491  {
492  self::$enable_web_access_without_session = (bool) $enable_web_access_without_session;
493  }
494 }
const SESSION_CLOSE_IDLE
static _destroy($a_session_id, $a_closing_context=null, $a_expired_at=null)
Destroy session.
static enableWebAccessWithoutSession($enable_web_access_without_session)
const SESSION_CLOSE_CAPTCHA
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$_SESSION["AccountId"]
$result
$type
global $DIC
Definition: saml.php:7
const SESSION_CLOSE_INACTIVE
const SESSION_CLOSE_LOGIN
const SESSION_CLOSE_TIME
static isSessionMainContext()
Context that are not only temporary in a session (e.g.
const SESSION_HANDLING_FIXED
static _destroyExpiredSessions()
Destroy expired sessions.
const SESSION_CLOSE_EXPIRE
static getExpireValue($fixedMode=false)
Returns the expiration timestamp in seconds.
foreach($_POST as $key=> $value) $res
static _exists($a_session_id)
Check whether session exists.
const SESSION_CLOSE_USER
static destroySession($a_session_id)
Destroy session(s).
static clear($a_var)
Unset a value.
static _getData($a_session_id)
Get session data from table.
$query
static createRawEntry($a_session_id, $a_session_type, $a_timestamp, $a_user_id)
Create raw data entry.
static _destroyByUserId($a_user_id)
Destroy session.
static getIdleValue($fixedMode=false)
Returns the idle time in seconds.
static isWebAccessWithoutSessionEnabled()
const SESSION_CLOSE_LIMIT
$users
Definition: authpage.php:44
static lookupExpireTime($a_session_id)
Lookup expire time for a specific session ilDB $ilDB.
$row
static $enable_web_access_without_session
const SESSION_CLOSE_PUBLIC
const SESSION_CLOSE_SIMUL
static _writeData($a_session_id, $a_data)
Write session data.
static _duplicate($a_session_id)
Duplicate session.
static setClosingContext($a_context)
set closing context (for statistics)
global $ilSetting
Definition: privfeed.php:17
global $ilDB
const SESSION_HANDLING_LOAD_DEPENDENT
static _getUsersWithIp($a_ip)
Get the active users with a specific remote ip address.
static getType()
Get context type.
static getClosingContext()
get closing context (for statistics)
static getSessionExpireValue()
Returns the session expiration value.
static aggretateRaw($a_now)
Aggregate raw session data (older than given time)
static $closing_context
const SESSION_CLOSE_FIRST
const SESSION_CLOSE_IP
static closeRawEntry($a_session_id, $a_context=null, $a_expired_at=null)
Close raw data entry.