ILIAS  eassessment Revision 61809
 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 
11 class ilSession
12 {
19  static function _getData($a_session_id)
20  {
21  global $ilDB;
22 
23  $q = "SELECT data FROM usr_session WHERE session_id = ".
24  $ilDB->quote($a_session_id, "text");
25  $set = $ilDB->query($q);
26  $rec = $ilDB->fetchAssoc($set);
27 
28  return $rec["data"];
29  }
30 
37  static function _writeData($a_session_id, $a_data)
38  {
39  global $ilDB, $ilSetting, $ilClientIniFile;
40 
41  if ($GLOBALS['WEB_ACCESS_WITHOUT_SESSION'])
42  {
43  // Prevent session data written for web access checker
44  // when no cookie was sent (e.g. for pdf files linking others).
45  // This would result in new session records for each request.
46  return false;
47  }
48 
49  if( $ilSetting->get('session_handling_type', 0) == 0)
50  {
51  // fixed session
52  $expires = time() + ini_get("session.gc_maxlifetime");
53  }
54  else if( $ilSetting->get('session_handling_type', 0) == 1)
55  {
56  // load dependent session settings
57  $expires = time() + (int)($ilSetting->get('session_max_idle', ilSessionControl::DEFAULT_MAX_IDLE) * 60);
58  }
59 
60  if (ilSession::_exists($a_session_id))
61  {
62  /*$q = "UPDATE usr_session SET ".
63  "expires = ".$ilDB->quote($expires, "integer").", ".
64  "data = ".$ilDB->quote($a_data, "clob").
65  ", ctime = ".$ilDB->quote(time(), "integer").
66  ", user_id = ".$ilDB->quote((int) $_SESSION["AccountId"], "integer").
67  " WHERE session_id = ".$ilDB->quote($a_session_id, "text");
68  array("integer", "clob", "integer", "integer", "text");
69  $ilDB->manipulate($q);*/
70 
71  if ($ilClientIniFile->readVariable("session","save_ip"))
72  {
73  $ilDB->update("usr_session", array(
74  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
75  "expires" => array("integer", $expires),
76  "data" => array("clob", $a_data),
77  "ctime" => array("integer", time()),
78  "type" => array("integer", (int) $_SESSION["SessionType"]),
79  "remote_addr" => array("text", $_SERVER["REMOTE_ADDR"])
80  ), array(
81  "session_id" => array("text", $a_session_id)
82  ));
83  }
84  else
85  {
86  $ilDB->update("usr_session", array(
87  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
88  "expires" => array("integer", $expires),
89  "data" => array("clob", $a_data),
90  "ctime" => array("integer", time()),
91  "type" => array("integer", (int) $_SESSION["SessionType"])
92  ), array(
93  "session_id" => array("text", $a_session_id)
94  ));
95  }
96 
97  }
98  else
99  {
100  /*$q = "INSERT INTO usr_session (session_id, expires, data, ctime,user_id) ".
101  "VALUES(".$ilDB->quote($a_session_id, "text").",".
102  $ilDB->quote($expires, "integer").",".
103  $ilDB->quote($a_data, "clob").",".
104  $ilDB->quote(time(), "integer").",".
105  $ilDB->quote((int) $_SESSION["AccountId"], "integer").")";
106  $ilDB->manipulate($q);*/
107 
108  if ($ilClientIniFile->readVariable("session","save_ip"))
109  {
110  $ilDB->insert("usr_session", array(
111  "session_id" => array("text", $a_session_id),
112  "expires" => array("integer", $expires),
113  "data" => array("clob", $a_data),
114  "ctime" => array("integer", time()),
115  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
116  "type" => array("integer", (int) $_SESSION["SessionType"]),
117  "createtime" => array("integer", time()),
118  "remote_addr" => array("text", $_SERVER["REMOTE_ADDR"])
119  ));
120  }
121  else
122  {
123  $ilDB->insert("usr_session", array(
124  "session_id" => array("text", $a_session_id),
125  "expires" => array("integer", $expires),
126  "data" => array("clob", $a_data),
127  "ctime" => array("integer", time()),
128  "user_id" => array("integer", (int) $_SESSION["AccountId"]),
129  "type" => array("integer", (int) $_SESSION["SessionType"]),
130  "createtime" => array("integer", time())
131  ));
132  }
133 
134  }
135 
136  // finally delete deprecated sessions
137  if(rand(0, 50) == 2)
138  {
140  }
141 
142  return true;
143  }
144 
151  static function _exists($a_session_id)
152  {
153  global $ilDB;
154 
155  $q = "SELECT session_id FROM usr_session WHERE session_id = ".
156  $ilDB->quote($a_session_id, "text");
157  $set = $ilDB->query($q);
158  if ($ilDB->fetchAssoc($set))
159  {
160  return true;
161  }
162  return false;
163  }
164 
170  static function _destroy($a_session_id)
171  {
172  global $ilDB;
173 
174  $q = "DELETE FROM usr_session WHERE session_id = ".
175  $ilDB->quote($a_session_id, "text");
176  $ilDB->manipulate($q);
177 
178  return true;
179  }
180 
186  static function _destroyByUserId($a_user_id)
187  {
188  global $ilDB;
189 
190  $q = "DELETE FROM usr_session WHERE user_id = ".
191  $ilDB->quote($a_user_id, "integer");
192  $ilDB->manipulate($q);
193 
194  return true;
195  }
196 
200  static function _destroyExpiredSessions()
201  {
202  global $ilDB;
203 
204  $q = "DELETE FROM usr_session WHERE expires < ".
205  $ilDB->quote(time(), "integer");
206  $ilDB->manipulate($q);
207 
208  return true;
209  }
210 
217  static function _duplicate($a_session_id)
218  {
219  global $ilDB;
220 
221  // Create new session id
222  $new_session = $a_session_id;
223  do
224  {
225  $new_session = md5($new_session);
226  $q ="SELECT * FROM usr_session WHERE ".
227  "session_id = ".$ilDB->quote($new_session, "text");
228  $res = $ilDB->query($q);
229  } while($ilDB->fetchAssoc($res));
230 
231  $query = "SELECT * FROM usr_session ".
232  "WHERE session_id = ".$ilDB->quote($a_session_id, "text");
233  $res = $ilDB->query($query);
234 
235  while ($row = $ilDB->fetchObject($res))
236  {
237  ilSession::_writeData($new_session,$row->data);
238  return $new_session;
239  }
240  return false;
241  }
242 
249  static function _getUsersWithIp($a_ip)
250  {
251  global $ilDB;
252 
253  $users = array();
254 
255  if (!$ilDB->tableColumnExists('usr_session', 'remote_addr'))
256  {
257  return $users;
258  }
259 
260  $query = "SELECT DISTINCT user_id FROM usr_session"
261  . " WHERE remote_addr = " . $ilDB->quote($a_ip, "text")
262  . " AND user_id > 0";
263  $result = $ilDB->query($query);
264 
265  while ($row = $ilDB->fetchObject($result))
266  {
267  $users[] = $row->user_id;
268  }
269  return $users;
270  }
271 }
272 ?>