/
session_api.php
193 lines (163 loc) · 4.92 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
<?php
# Mantis - a php based bugtracking system
# Copyright (C) 2008 - 2008 Mantis Team - mantisbt-dev@lists.sourceforge.net
# Mantis 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.
#
# Mantis 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 Mantis. 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 SessionAPI
*/
$g_session = null;
/**
* Abstract interface for a Mantis session handler.
*/
/* abstract */ class MantisSession {
var $id;
/* abstract */ function __construct() {}
/* abstract */ function get( $p_name, $p_default=null ) {}
/* abstract */ function set( $p_name, $p_value ) {}
/* abstract */ function delete( $p_name ) {}
/* abstract */ function destroy() {}
}
/**
* Implementation of the abstract Mantis session interface using
* standard PHP sessions stored on the server's filesystem according
* to PHP's session.* settings in 'php.ini'.
*/
class MantisPHPSession extends MantisSession {
function __construct() {
$t_session_save_path = config_get_global( 'session_save_path' );
if ( $t_session_save_path ) {
session_save_path( $t_session_save_path );
}
session_cache_limiter( 'private_no_expire' );
if ( isset( $_SERVER['HTTPS'] ) && ( strtolower( $_SERVER['HTTPS'] ) != 'off' ) ) {
session_set_cookie_params( 0, config_get( 'cookie_path' ), config_get( 'cookie_domain' ), true, true );
} else {
session_set_cookie_params( 0, config_get( 'cookie_path' ), config_get( 'cookie_domain' ), false, true );
}
session_start();
$this->id = session_id();
}
# Chain the PHP4 class constructor
function MantisPHPSession() {
$this->__construct();
}
function get( $p_name, $p_default=null ) {
if ( isset( $_SESSION[ $p_name ] ) ) {
return unserialize( $_SESSION[ $p_name ] );
}
if ( func_num_args() > 1 ) {
return $p_default;
}
error_parameters( $p_name );
trigger_error( ERROR_SESSION_VAR_NOT_FOUND, ERROR );
}
function set( $p_name, $p_value ) {
$_SESSION[ $p_name ] = serialize( $p_value );
}
function delete( $p_name ) {
unset( $_SESSION[ $p_name ] );
}
function destroy() {
if ( isset( $_COOKIE[ session_name() ] ) && !headers_sent() ) {
gpc_set_cookie( session_name(), '', time() - 42000 );
}
unset( $_SESSION );
session_destroy();
}
}
/**
* Initialize the appropriate session handler.
*/
function session_init() {
global $g_session, $g_session_handler;
switch( strtolower( $g_session_handler ) ) {
case 'php':
$g_session = new MantisPHPSession();
break;
case 'adodb':
# Not yet implemented
case 'memcached':
# Not yet implemented
default:
trigger_error( ERROR_SESSION_HANDLER_INVALID, ERROR );
break;
}
}
/**
* 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 );
}
/**
* Destroy the session entirely.
*/
function session_clean() {
global $g_session;
$g_session->destroy();
}
##### Initialize the session
session_init();