/
session_api.php
322 lines (274 loc) · 7.69 KB
/
session_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
# MantisBT - a php based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Session API for handling user/browser sessions in an extendable manner.
* New session handlers can be added and configured without affecting how
* the API is used. Calls to session_*() are appropriately directed at the
* session handler class as chosen in config_inc.php.
*
* @package CoreAPI
* @subpackage SessionAPI
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2012 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses gpc_api.php
*/
/**
* requires gpc_api
*/
require_once( 'gpc_api.php' );
/**
*
* @global MantisPHPSession $g_session
*/
$g_session = null;
/**
* Abstract interface for a MantisBT session handler.
* @package MantisBT
* @subpackage classes
*/
abstract class MantisSession {
var $id;
/**
* Constructor
*/
abstract function __construct();
/**
* get session data
* @param string $p_name
* @param mixed $p_default
*/
abstract function get( $p_name, $p_default = null );
/**
* set session data
* @param string $p_name
* @param mixed $p_value
*/
abstract function set( $p_name, $p_value );
/**
* delete session data
* @param string $p_name
*/
abstract function delete( $p_name );
/**
* destroy session
*/
abstract function destroy();
}
/**
* Implementation of the abstract MantisBT session interface using
* standard PHP sessions stored on the server's filesystem according
* to PHP's session.* settings in 'php.ini'.
* @package MantisBT
* @subpackage classes
*/
class MantisPHPSession extends MantisSession {
/**
* Constructor
*/
function __construct( $p_session_id=null ) {
global $g_cookie_secure_flag_enabled;
global $g_cookie_httponly_flag_enabled;
$this->key = config_get_global( 'session_key' );
# Save session information where specified or with PHP's default
$t_session_save_path = config_get_global( 'session_save_path' );
if( $t_session_save_path ) {
session_save_path( $t_session_save_path );
}
# Handle session cookie and caching
session_cache_limiter( 'private_no_expire' );
if ( $g_cookie_httponly_flag_enabled ) {
# The HttpOnly cookie flag is only supported in PHP >= 5.2.0
session_set_cookie_params( 0, config_get( 'cookie_path' ), config_get( 'cookie_domain' ), $g_cookie_secure_flag_enabled, $g_cookie_httponly_flag_enabled );
} else {
session_set_cookie_params( 0, config_get( 'cookie_path' ), config_get( 'cookie_domain' ), $g_cookie_secure_flag_enabled );
}
# Handle existent session ID
if ( !is_null( $p_session_id ) ) {
session_id( $p_session_id );
}
# Initialize the session
session_start();
$this->id = session_id();
# Initialize the keyed session store
if ( !isset( $_SESSION[ $this->key ] ) ) {
$_SESSION[ $this->key ] = array();
}
}
/**
* get session data
* @param string $p_name
* @param mixed $p_default
*/
function get( $p_name, $p_default=null ) {
if ( isset( $_SESSION[ $this->key ][ $p_name ] ) ) {
return unserialize( $_SESSION[ $this->key ][ $p_name ] );
}
if( func_num_args() > 1 ) {
return $p_default;
}
error_parameters( $p_name );
trigger_error( ERROR_SESSION_VAR_NOT_FOUND, ERROR );
}
/**
* set session data
* @param string $p_name
* @param mixed $p_value
*/
function set( $p_name, $p_value ) {
$_SESSION[ $this->key ][ $p_name ] = serialize( $p_value );
}
/**
* delete session data
* @param string $p_name
*/
function delete( $p_name ) {
unset( $_SESSION[ $this->key ][ $p_name ] );
}
/**
* destroy session
*/
function destroy() {
if( isset( $_COOKIE[session_name()] ) && !headers_sent() ) {
gpc_set_cookie( session_name(), '', time() - 42000 );
}
unset( $_SESSION[ $this->key ] );
}
}
/**
* Initialize the appropriate session handler.
* @param string Session ID
*/
function session_init( $p_session_id=null ) {
global $g_session, $g_session_handler;
switch( utf8_strtolower( $g_session_handler ) ) {
case 'php':
$g_session = new MantisPHPSession( $p_session_id );
break;
case 'adodb':
# Not yet implemented
case 'memcached':
# Not yet implemented
default:
trigger_error( ERROR_SESSION_HANDLER_INVALID, ERROR );
break;
}
if ( ON == config_get_global( 'session_validation' ) && session_get( 'secure_session', false ) ) {
session_validate( $g_session );
}
}
/**
* Validate the legitimacy of a session.
* Checks may include last-known IP address, or more.
* Triggers an error when the session is invalid.
* @param object Session object
*/
function session_validate( $p_session ) {
$t_user_ip = '';
if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
$t_user_ip = trim( $_SERVER['REMOTE_ADDR'] );
}
if ( is_null( $t_last_ip = $p_session->get( 'last_ip', null ) ) ) {
# First session usage
$p_session->set( 'last_ip', $t_user_ip );
} else {
# Check a continued session request
if ( $t_user_ip != $t_last_ip ) {
session_clean();
trigger_error( ERROR_SESSION_NOT_VALID, WARNING );
$t_url = config_get_global( 'path' ) . config_get_global( 'default_home_page' );
echo "\t<meta http-equiv=\"Refresh\" content=\"4;URL=$t_url\" />\n";
die();
}
}
}
/**
* Get arbitrary data from the session.
* @param string Session variable name
* @param mixed Default value
* @return mixed Session variable
*/
function session_get( $p_name, $p_default = null ) {
global $g_session;
$t_args = func_get_args();
return call_user_func_array( array( $g_session, 'get' ), $t_args );
}
/**
* Get an integer from the session.
* @param string Session variable name
* @param mixed Default value
* @return int Session variable
*/
function session_get_int( $p_name, $p_default = null ) {
global $g_session;
$t_args = func_get_args();
return (int) call_user_func_array( 'session_get', $t_args );
}
/**
* Get a boolean from the session.
* @param string Session variable name
* @param mixed Default value
* @return boolean Session variable
*/
function session_get_bool( $p_name, $p_default = null ) {
global $g_session;
$t_args = func_get_args();
return true && call_user_func_array( 'session_get', $t_args );
}
/**
* Get a string from the session.
* @param string Session variable name
* @param mixed Default value
* @return string Session variable
*/
function session_get_string( $p_name, $p_default = null ) {
global $g_session;
$t_args = func_get_args();
return '' . call_user_func_array( 'session_get', $t_args );
}
/**
* Set a session variable.
* @param string Session variable name
* @param mixed Variable value
*/
function session_set( $p_name, $p_value ) {
global $g_session;
$g_session->set( $p_name, $p_value );
}
/**
* Delete a session variable.
* @param string Session variable name
*/
function session_delete( $p_name ) {
global $g_session;
$g_session->delete( $p_name );
}
/**
* Destroy the session entirely.
*/
function session_clean() {
global $g_session;
$g_session->destroy();
}
# Initialize the session
if ( PHP_CGI == php_mode() ) {
$t_session_id = gpc_get_string( 'session_id', '' );
if ( empty( $t_session_id ) ) {
session_init();
} else {
session_init( $t_session_id );
}
}