FusionDirectory
 All Data Structures Files Functions Variables
class_session.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2016 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
30 class session {
36  public static function is_set($name)
37  {
38  $channel = "";
39  if (isset($_POST['_channel_'])) {
40  $channel = $_POST['_channel_'];
41  }
42 
43  /* Global fallback if not set */
44  if ($channel == "") {
45  return isset($_SESSION[$name]);
46  }
47 
48  /* Sanity check */
49  if (!session::channel_exists($channel)) {
50  msg_dialog::display(_("Internal error"), _("Requested channel does not exist! Please contact your Administrator."), FATAL_ERROR_DIALOG);
51  exit;
52  }
53 
54  $channel = "gch_".$channel;
55  return isset($_SESSION[$channel][$name]);
56  }
57 
63  public static function global_is_set($name)
64  {
65  return isset($_SESSION[$name]);
66  }
67 
75  public static function set($name, $value)
76  {
77  $channel = "";
78  if (isset($_POST['_channel_'])) {
79  $channel = $_POST['_channel_'];
80  }
81 
82  /* Global fallback if not set */
83  if ($channel == "") {
84  $_SESSION[$name] = $value;
85  } else {
86  /* Sanity check */
87  if (!session::channel_exists($channel)) {
88  msg_dialog::display(_("Internal error"), _("Requested channel does not exist! Please contact your Administrator."), FATAL_ERROR_DIALOG);
89  exit;
90  }
91  $_SESSION[$channel][$name] = $value;
92  }
93  }
94 
102  public static function global_set($name, $value)
103  {
104  $_SESSION[$name] = $value;
105  }
106 
112  public static function &get($name)
113  {
114  $channel = "";
115  if (isset($_POST['_channel_'])) {
116  $channel = $_POST['_channel_'];
117  }
118 
119  /* Global fallback if not set */
120  if ($channel == "") {
121  $ret = &$_SESSION[$name];
122  return $ret;
123  }
124 
125  /* Sanity check */
126  if (!session::channel_exists($channel)) {
127  msg_dialog::display(_("Internal error"), _("Requested channel does not exist! Please contact your Administrator."), FATAL_ERROR_DIALOG);
128  exit;
129  }
130 
131  $channel = "gch_".$channel;
132  $ret = &$_SESSION[$channel][$name];
133  return $ret;
134  }
135 
141  public static function global_get($name)
142  {
143  if (isset($_SESSION[$name])) {
144  return $_SESSION[$name];
145  } else {
146  return NULL;
147  }
148  }
149 
155  public static function &global_get_ref($name)
156  {
157  $ret = &$_SESSION[$name];
158  return $ret;
159  }
160 
166  public static function delete($name)
167  {
168  $channel = "";
169  if (isset($_POST['_channel_'])) {
170  $channel = $_POST['_channel_'];
171  }
172 
173  /* Global fallback if not set */
174  if ($channel == "") {
175  if (isset($_SESSION[$name])) {
176  unset($_SESSION[$name]);
177  }
178  } else {
179  if (isset($_SESSION[$channel][$name])) {
180  unset($_SESSION[$channel][$name]);
181  }
182  }
183  }
184 
190  public static function global_delete($name)
191  {
192  if (isset($_SESSION[$name])) {
193  unset($_SESSION[$name]);
194  }
195  }
196 
202  public static function un_set($name)
203  {
204  return session::delete($name);
205  }
206 
212  public static function global_un_set($name)
213  {
214  return session::global_delete($name);
215  }
216 
220  public static function start($id = NULL)
221  {
222  session_name("FusionDirectory");
223  /* Set cookie lifetime to one day (The parameter is in seconds ) */
224  session_set_cookie_params(24 * 60 * 60);
225 
226  /* Set cache limiter to one day (parameter is minute !!) - default is 180 */
227  session_cache_expire(60 * 24);
228 
229  /* Set session max lifetime, to prevent the garbage collector to delete session before timeout.
230  !! The garbage collector is a cron job on debian systems, the cronjob will fetch the timeout from
231  the php.ini, so if you use debian, you must hardcode session.gc_maxlifetime in your php.ini */
232  ini_set("session.gc_maxlifetime", 24 * 60 * 60);
233  if ($id !== NULL) {
234  session_id($id);
235  }
236  session_start();
237 
238  /* Check for changed browsers and bail out */
239  if (isset($_SESSION['HTTP_USER_AGENT'])) {
240  if ($_SESSION['HTTP_USER_AGENT'] != md5($_SERVER['HTTP_USER_AGENT'])) {
241  session_destroy();
242  session_name("FusionDirectory");
243  session_start();
244  }
245  } else {
246  $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);
247  }
248 
249  /* Regenerate ID to increase security */
250  if (!isset($_SESSION['started'])) {
251  session_regenerate_id();
252  $_SESSION['started'] = TRUE;
253  }
254  }
255 
259  public static function destroy()
260  {
261  @session_destroy();
262  }
263 
267  public static function &get_all()
268  {
269  $ret = &$_SESSION;
270  return $ret;
271  }
272 }
273 ?>
static delete($name)
Delete a session.
This class contains all the function needed to manage sessions.
static start($id=NULL)
Start a session.
static set($name, $value)
Set a value in a session.
static destroy()
Destroy a session.
static global_un_set($name)
Unset a session.
static global_set($name, $value)
Set a value in a session.
static un_set($name)
Unset a session.
static global_delete($name)
Delete a session.
static global_get($name)
Accessor of a session var.
static display($s_title, $s_message, $i_type=INFO_DIALOG)
Display a message dialog.
static & get_all()
Get all sessions.
static & global_get_ref($name)
Accessor of a session var by reference.
static global_is_set($name)
Check if a session is defined.
static is_set($name)
Check if the name of the session is set.