1
|
|
|
<?php |
2
|
|
|
require_once('Autoload.php'); |
3
|
|
|
if(!isset($_SESSION) && php_sapi_name() !== 'cli') { session_start(); } |
4
|
|
|
if(!isset($_SESSION['ip_address']) && isset($_SERVER['REMOTE_ADDR'])) |
5
|
|
|
{ |
6
|
|
|
$_SESSION['ip_address'] = $_SERVER['REMOTE_ADDR']; |
7
|
|
|
} |
8
|
|
|
if(!isset($_SESSION['init_time'])) |
9
|
|
|
{ |
10
|
|
|
$_SESSION['init_time'] = date('c'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
class FlipSession extends Singleton |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Does the variable exist in the session |
17
|
|
|
* |
18
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
19
|
|
|
*/ |
20
|
|
|
static function doesVarExist($name) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
return isset($_SESSION[$name]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get a variable from the session |
27
|
|
|
* |
28
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
29
|
|
|
*/ |
30
|
|
|
static function getVar($name, $default = false) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
if(FlipSession::doesVarExist($name)) |
33
|
|
|
{ |
34
|
|
|
return $_SESSION[$name]; |
35
|
|
|
} |
36
|
|
|
else |
37
|
|
|
{ |
38
|
|
|
return $default; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set a variable in the session |
44
|
|
|
* |
45
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
46
|
|
|
*/ |
47
|
|
|
static function setVar($name, $value) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$_SESSION[$name] = $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Is a user currently logged in? |
54
|
|
|
* |
55
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
56
|
|
|
*/ |
57
|
|
|
static function isLoggedIn() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if(isset($_SESSION['flipside_user'])) |
60
|
|
|
{ |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
else if(isset($_SESSION['AuthMethod']) && isset($_SESSION['AuthData'])) |
64
|
|
|
{ |
65
|
|
|
$auth = AuthProvider::getInstance(); |
66
|
|
|
return $auth->isLoggedIn($_SESSION['AuthData'], $_SESSION['AuthMethod']); |
67
|
|
|
} |
68
|
|
|
else |
69
|
|
|
{ |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the currently logged in user |
76
|
|
|
* |
77
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
78
|
|
|
*/ |
79
|
|
|
static function getUser() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if(isset($_SESSION['flipside_user'])) |
82
|
|
|
{ |
83
|
|
|
return $_SESSION['flipside_user']; |
84
|
|
|
} |
85
|
|
|
else if(isset($_SESSION['AuthMethod']) && isset($_SESSION['AuthData'])) |
86
|
|
|
{ |
87
|
|
|
$auth = AuthProvider::getInstance(); |
88
|
|
|
$user = $auth->getUser($_SESSION['AuthData'], $_SESSION['AuthMethod']); |
89
|
|
|
if($user !== null) |
90
|
|
|
{ |
91
|
|
|
$_SESSION['flipside_user'] = $user; |
92
|
|
|
} |
93
|
|
|
return $user; |
94
|
|
|
} |
95
|
|
|
else |
96
|
|
|
{ |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set the currently logged in user |
103
|
|
|
* |
104
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
105
|
|
|
*/ |
106
|
|
|
static function setUser($user) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$_SESSION['flipside_user'] = $user; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Obtain the current users email address |
113
|
|
|
* |
114
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
115
|
|
|
*/ |
116
|
|
|
static function getUserEmail() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
if(isset($_SESSION['flipside_email'])) |
119
|
|
|
{ |
120
|
|
|
return $_SESSION['flipside_email']; |
121
|
|
|
} |
122
|
|
|
$user = FlipSession::getUser(); |
123
|
|
|
if($user === false || $user === null) |
124
|
|
|
{ |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
if(isset($user->mail) && isset($user->mail[0])) |
128
|
|
|
{ |
129
|
|
|
$_SESSION['flipside_email'] = $user->mail[0]; |
130
|
|
|
return $_SESSION['flipside_email']; |
131
|
|
|
} |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* This will end your session |
137
|
|
|
* |
138
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
139
|
|
|
*/ |
140
|
|
|
static function end() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
if(isset($_SESSION) && !empty($_SESSION)) |
143
|
|
|
{ |
144
|
|
|
$_SESSION = array(); |
145
|
|
|
session_destroy(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
static function unserializePhpSession($sessionData) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$res = array(); |
152
|
|
|
$offset = 0; |
153
|
|
|
$length = strlen($sessionData); |
154
|
|
|
while($offset < $length) |
155
|
|
|
{ |
156
|
|
|
$pos = strpos($sessionData, "|", $offset); |
157
|
|
|
$len = $pos - $offset; |
158
|
|
|
$name = substr($sessionData, $offset, $len); |
159
|
|
|
if($name === false) break; |
160
|
|
|
$offset += $len + 1; |
161
|
|
|
$data = @unserialize(substr($sessionData, $offset)); |
162
|
|
|
$res[$name] = $data; |
163
|
|
|
$offset += strlen(serialize($data)); |
164
|
|
|
} |
165
|
|
|
return $res; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
static function getAllSessions() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$res = array(); |
171
|
|
|
$sessFiles = scandir(ini_get('session.save_path')); |
172
|
|
|
$count = count($sessFiles); |
173
|
|
|
for($i = 0; $i < $count; $i++) |
174
|
|
|
{ |
175
|
|
|
if($sessFiles[$i][0] === '.') |
176
|
|
|
{ |
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
$sessionId = substr($sessFiles[$i], 5); |
180
|
|
|
$sessionData = file_get_contents(ini_get('session.save_path').'/'.$sessFiles[$i]); |
181
|
|
|
if($sessionData === false) |
182
|
|
|
{ |
183
|
|
|
array_push($res, array('sid' => $sessionId)); |
184
|
|
|
} |
185
|
|
|
else |
186
|
|
|
{ |
187
|
|
|
$tmp = FlipSession::unserializePhpSession($sessionData); |
188
|
|
|
$tmp['sid'] = $sessionId; |
189
|
|
|
array_push($res, $tmp); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
if(count($res) == 0) |
193
|
|
|
{ |
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
return $res; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
static function getSessionById($sid) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$sessionData = file_get_contents(ini_get('session.save_path').'/sess_'.$sid); |
202
|
|
|
return FlipSession::unserializePhpSession($sessionData); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
static function deleteSessionById($sid) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
return unlink(ini_get('session.save_path').'/sess_'.$sid); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
211
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.