1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class session {
25 public static function get_session_size()
26 {
28 }
30 public static function get_element_size()
31 {
33 }
35 public static function add_channel($name)
36 {
37 /* If there's already such kind of channel, skip... */
38 if (isset($_SESSION[$name])){
39 return (FALSE);
40 }
42 /* Allocate it... */
43 $_SESSION[$name]= array();
44 $_POST["_channel_"]= $name;
45 return (TRUE);
46 }
48 public static function remove_channel($name)
49 {
50 /* If there's already such kind of channel, skip... */
51 if (isset($_SESSION[$name])){
52 unset($_SESSION[$name]);
53 if (isset($_POST["_channel_"])){
54 unset($_POST["_channel_"]);
55 }
56 return (TRUE);
57 }
59 return (FALSE);
60 }
62 public static function channel_exists($name)
63 {
64 return (isset($_SESSION[$name]));
65 }
67 public static function is_set($name)
68 {
69 $channel= "";
70 if (isset($_POST['_channel_'])){
71 $channel= $_POST['_channel_'];
72 }
74 /* Global fallback if not set */
75 if ($channel == ""){
76 return(isset($_SESSION[$name]));
77 }
79 /* Sanity check */
80 if (!session::channel_exists($channel)){
81 msg_dialog::display(_("Internal error"), _("Requested channel does not exist! Please contact your Administrator."), FATAL_ERROR_DIALOG);
82 }
84 $channel= "gch_".$channel;
85 return(isset($_SESSION[$channel][$name]));
86 }
88 public static function global_is_set($name)
89 {
90 return(isset($_SESSION[$name]));
91 }
93 public static function set($name,$value)
94 {
95 $channel= "";
96 if (isset($_POST['_channel_'])){
97 $channel= $_POST['_channel_'];
98 }
100 /* Global fallback if not set */
101 if ($channel == ""){
102 $_SESSION[$name] = $value;
103 } else {
104 /* Sanity check */
105 if (!session::channel_exists($channel)){
106 msg_dialog::display(_("Internal error"), _("Requested channel does not exist! Please contact your Administrator."), FATAL_ERROR_DIALOG);
107 }
108 $_SESSION[$channel][$name] = $value;
109 }
110 }
112 public static function global_set($name,$value)
113 {
114 $_SESSION[$name] = $value;
115 }
117 public static function &get($name)
118 {
119 $channel= "";
120 if (isset($_POST['_channel_'])){
121 $channel= $_POST['_channel_'];
122 }
124 /* Global fallback if not set */
125 if ($channel == ""){
126 $ret = &$_SESSION[$name];
127 return($ret);
128 }
130 /* Sanity check */
131 if (!session::channel_exists($channel)){
132 msg_dialog::display(_("Internal error"), _("Requested channel does not exist! Please contact your Administrator."), FATAL_ERROR_DIALOG);
133 }
135 $channel= "gch_".$channel;
136 $ret = &$_SESSION[$channel][$name];
137 return($ret);
138 }
140 public static function &global_get($name)
141 {
142 $ret = &$_SESSION[$name];
143 return($ret);
144 }
146 public static function delete($name)
147 {
148 $channel= "";
149 if (isset($_POST['_channel_'])){
150 $channel= $_POST['_channel_'];
151 }
153 /* Global fallback if not set */
154 if ($channel == ""){
155 if(isset($_SESSION[$name])){
156 unset($_SESSION[$name]);
157 }
158 } else {
159 if(isset($_SESSION[$channel][$name])){
160 unset($_SESSION[$channel][$name]);
161 }
162 }
163 }
165 public static function global_delete($name)
166 {
167 if($_SESSION[$name]){
168 unset($_SESSION[$name]);
169 }
170 }
172 public static function un_set($name)
173 {
174 return(session::delete($name));
175 }
177 public static function global_un_set($name)
178 {
179 return(session::global_delete($name));
180 }
182 public static function start()
183 {
184 /* Set cookie lifetime to one day (The parameter is in seconds ) */
185 session_set_cookie_params(24*60*60);
187 /* Set cache limter to one day (parameter is minute !!)*/
188 session_cache_expire(60*24); // default is 180
190 /* Set session max lifetime, to prevent the garbage collector to delete session before timeout.
191 !! The garbage collector is a cron job on debian systems, the cronjob will fetch the timeout from
192 the php.ini, so if you use debian, you must hardcode session.gc_maxlifetime in your php.ini */
193 ini_set("session.gc_maxlifetime",24*60*60);
194 session_start();
196 /* Check for changed browsers and bail out */
197 if (isset($_SESSION['HTTP_USER_AGENT']))
198 {
199 if ($_SESSION['HTTP_USER_AGENT'] != md5($_SERVER['HTTP_USER_AGENT'])) {
200 session_destroy();
201 session_start();
202 }
203 } else {
204 $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);
205 }
207 /* Regenerate ID to increase security */
208 if (!isset($_SESSION['started'])){
209 session_regenerate_id();
210 $_SESSION['started'] = true;
211 }
212 }
214 public static function destroy()
215 {
216 @session_destroy();
217 }
219 public static function set_lifetime($seconds = -1)
220 {
221 echo "Not implemented yet";
222 }
224 public static function &get_all()
225 {
226 $ret = &$_SESSION;
227 return($ret);
228 }
229 }
231 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
232 ?>