ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
13 class ilSession
14 {
23 
32 
39  static function _getData($a_session_id)
40  {
41  global $ilDB;
42 
43  $q = "SELECT data FROM usr_session WHERE session_id = ".
44  $ilDB->quote($a_session_id, "text");
45  $set = $ilDB->query($q);
46  $rec = $ilDB->fetchAssoc($set);
47 
48  return $rec["data"];
49  }
50 
57  static function _writeData($a_session_id, $a_data)
58  {
59  global $ilDB, $ilSetting, $ilClientIniFile;
60 
61  if ($GLOBALS['WEB_ACCESS_WITHOUT_SESSION'])
62  {
63  // Prevent session data written for web access checker
64  // when no cookie was sent (e.g. for pdf files linking others).
65  // This would result in new session records for each request.
66  return false;
67  }
68 
69  $expires = self::getExpireValue();
70 
71  if (ilSession::_exists($a_session_id))
72  {
73  /*$q = "UPDATE usr_session SET ".
74  "expires = ".$ilDB->quote($expires, "integer").", ".
75  "data = ".$ilDB->quote($a_data, "clob").
76  ", ctime = ".$ilDB->quote(time(), "integer").
77  ", user_id = ".$ilDB->quote((int) $_SESSION["AccountId"], "integer").
78  " WHERE session_id = ".$ilDB->quote($a_session_id, "text");
79  array("integer", "clob", "integer", "integer", "text");
80  $ilDB->manipulate($q);*/
81 
82  if ($ilClientIniFile->readVariable("session","save_ip"))
83  {
84  $ilDB->update("usr_session", array(
85  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
86  "expires" => array("integer", $expires),
87  "data" => array("clob", $a_data),
88  "ctime" => array("integer", time()),
89  "type" => array("integer", (int) $_SESSION["SessionType"]),
90  "remote_addr" => array("text", $_SERVER["REMOTE_ADDR"])
91  ), array(
92  "session_id" => array("text", $a_session_id)
93  ));
94  }
95  else
96  {
97  $ilDB->update("usr_session", array(
98  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
99  "expires" => array("integer", $expires),
100  "data" => array("clob", $a_data),
101  "ctime" => array("integer", time()),
102  "type" => array("integer", (int) $_SESSION["SessionType"])
103  ), array(
104  "session_id" => array("text", $a_session_id)
105  ));
106  }
107 
108  }
109  else
110  {
111  /*$q = "INSERT INTO usr_session (session_id, expires, data, ctime,user_id) ".
112  "VALUES(".$ilDB->quote($a_session_id, "text").",".
113  $ilDB->quote($expires, "integer").",".
114  $ilDB->quote($a_data, "clob").",".
115  $ilDB->quote(time(), "integer").",".
116  $ilDB->quote((int) $_SESSION["AccountId"], "integer").")";
117  $ilDB->manipulate($q);*/
118 
119  if ($ilClientIniFile->readVariable("session","save_ip"))
120  {
121  $ilDB->insert("usr_session", array(
122  "session_id" => array("text", $a_session_id),
123  "expires" => array("integer", $expires),
124  "data" => array("clob", $a_data),
125  "ctime" => array("integer", time()),
126  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
127  "type" => array("integer", (int) $_SESSION["SessionType"]),
128  "createtime" => array("integer", time()),
129  "remote_addr" => array("text", $_SERVER["REMOTE_ADDR"])
130  ));
131  }
132  else
133  {
134  $ilDB->insert("usr_session", array(
135  "session_id" => array("text", $a_session_id),
136  "expires" => array("integer", $expires),
137  "data" => array("clob", $a_data),
138  "ctime" => array("integer", time()),
139  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
140  "type" => array("integer", (int) $_SESSION["SessionType"]),
141  "createtime" => array("integer", time())
142  ));
143  }
144 
145  }
146 
147  // finally delete deprecated sessions
148  if(rand(0, 50) == 2)
149  {
151  }
152 
153  return true;
154  }
155 
162  static function _exists($a_session_id)
163  {
164  global $ilDB;
165 
166  $q = "SELECT session_id FROM usr_session WHERE session_id = ".
167  $ilDB->quote($a_session_id, "text");
168  $set = $ilDB->query($q);
169  if ($ilDB->fetchAssoc($set))
170  {
171  return true;
172  }
173  return false;
174  }
175 
181  static function _destroy($a_session_id)
182  {
183  global $ilDB;
184 
185  $q = "DELETE FROM usr_session WHERE session_id = ".
186  $ilDB->quote($a_session_id, "text");
187  $ilDB->manipulate($q);
188 
189  return true;
190  }
191 
197  static function _destroyByUserId($a_user_id)
198  {
199  global $ilDB;
200 
201  $q = "DELETE FROM usr_session WHERE user_id = ".
202  $ilDB->quote($a_user_id, "integer");
203  $ilDB->manipulate($q);
204 
205  return true;
206  }
207 
211  static function _destroyExpiredSessions()
212  {
213  global $ilDB;
214 
215  $q = "DELETE FROM usr_session WHERE expires < ".
216  $ilDB->quote(time(), "integer");
217  $ilDB->manipulate($q);
218 
219  return true;
220  }
221 
228  static function _duplicate($a_session_id)
229  {
230  global $ilDB;
231 
232  // Create new session id
233  $new_session = $a_session_id;
234  do
235  {
236  $new_session = md5($new_session);
237  $q ="SELECT * FROM usr_session WHERE ".
238  "session_id = ".$ilDB->quote($new_session, "text");
239  $res = $ilDB->query($q);
240  } while($ilDB->fetchAssoc($res));
241 
242  $query = "SELECT * FROM usr_session ".
243  "WHERE session_id = ".$ilDB->quote($a_session_id, "text");
244  $res = $ilDB->query($query);
245 
246  while ($row = $ilDB->fetchObject($res))
247  {
248  ilSession::_writeData($new_session,$row->data);
249  return $new_session;
250  }
251  return false;
252  }
253 
264  public static function getExpireValue($fixedMode = false)
265  {
266  global $ilSetting;
267 
268  if( $fixedMode || $ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_FIXED )
269  {
270  // fixed session
271  return time() + ini_get('session.gc_maxlifetime');
272  }
273  else if( $ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_LOAD_DEPENDENT )
274  {
275  // load dependent session settings
276  return time() + (int) ($ilSetting->get('session_max_idle', ilSessionControl::DEFAULT_MAX_IDLE) * 60);
277  }
278  }
279 
290  public static function getIdleValue($fixedMode = false)
291  {
292  global $ilSetting, $ilClientIniFile;
293 
294  if( $fixedMode || $ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_FIXED )
295  {
296  // fixed session
297  return $ilClientIniFile->readVariable('session','expire');
298  }
299  else if( $ilSetting->get('session_handling_type', self::SESSION_HANDLING_FIXED) == self::SESSION_HANDLING_LOAD_DEPENDENT )
300  {
301  // load dependent session settings
302  return (int) ($ilSetting->get('session_max_idle', ilSessionControl::DEFAULT_MAX_IDLE) * 60);
303  }
304  }
305 
315  public static function getSessionExpireValue()
316  {
317  return self::getIdleValue(true);
318  }
319 
326  static function _getUsersWithIp($a_ip)
327  {
328  global $ilDB;
329 
330  $query = "SELECT DISTINCT user_id FROM usr_session"
331  . " WHERE remote_addr = " . $ilDB->quote($a_ip, "text")
332  . " AND user_id > 0";
333  $result = $ilDB->query($query);
334 
335  $users = array();
336  while ($row = $ilDB->fetchObject($result))
337  {
338  $users[] = $row->user_id;
339  }
340  return $users;
341  }
342 }
343 ?>