-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathFlipSession.php
242 lines (227 loc) · 6.06 KB
/
FlipSession.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
<?php
namespace Flipside;
require_once('Autoload.php');
$settings = \Flipside\Settings::getInstance();
//Use PHP based session handling if DB session handling isn't setup
if($settings->getDataSetData('profiles') !== false)
{
try
{
$handler = new \Flipside\Data\DataTableSessionHandler('profiles', 'sessions');
session_set_save_handler($handler, true);
}
catch(\Exception $e)
{
//Use default PHP session handling...
}
}
if(!isset($_SESSION) && php_sapi_name() !== 'cli')
{
session_start();
}
if(!isset($_SESSION['ip_address']) && isset($_SERVER['REMOTE_ADDR']))
{
$_SESSION['ip_address'] = $_SERVER['REMOTE_ADDR'];
}
if(!isset($_SESSION['init_time']))
{
$_SESSION['init_time'] = date('c');
}
class FlipSession extends Singleton
{
/**
* Does the variable exist in the session
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function doesVarExist($name)
{
return isset($_SESSION[$name]);
}
/**
* Get a variable from the session
*
* @param string $name The name of variable to obtain from the session
* @param mixed $default The default value
*
* @return mixed The value stored in the session or the default if not set
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function getVar($name, $default = false)
{
if(FlipSession::doesVarExist($name))
{
return $_SESSION[$name];
}
else
{
return $default;
}
}
/**
* Set a variable in the session
*
* @param string $name The name of variable to set in the session
* @param mixed $value The value to store in the variable
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function setVar($name, $value)
{
$_SESSION[$name] = $value;
}
/**
* Is a user currently logged in?
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function isLoggedIn()
{
if(isset($_SESSION['flipside_user']))
{
return true;
}
else if(isset($_SESSION['AuthMethod']) && isset($_SESSION['AuthData']))
{
$auth = AuthProvider::getInstance();
return $auth->isLoggedIn($_SESSION['AuthData'], $_SESSION['AuthMethod']);
}
else
{
return false;
}
}
/**
* Get the currently logged in user
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function getUser()
{
if(isset($_SESSION['flipside_user']))
{
return $_SESSION['flipside_user'];
}
else if(isset($_SESSION['AuthMethod']) && isset($_SESSION['AuthData']))
{
$auth = AuthProvider::getInstance();
$user = $auth->getUser($_SESSION['AuthData'], $_SESSION['AuthMethod']);
if($user !== null)
{
$_SESSION['flipside_user'] = $user;
}
return $user;
}
else
{
return null;
}
}
/**
* Set the currently logged in user
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function setUser($user)
{
$_SESSION['flipside_user'] = $user;
}
/**
* Obtain the current users email address
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function getUserEmail()
{
if(isset($_SESSION['flipside_email']))
{
return $_SESSION['flipside_email'];
}
$user = FlipSession::getUser();
if($user === false || $user === null)
{
return false;
}
if(isset($user->mail) && isset($user->mail[0]))
{
$_SESSION['flipside_email'] = $user->mail[0];
return $_SESSION['flipside_email'];
}
return false;
}
/**
* This will end your session
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function end()
{
if(isset($_SESSION) && !empty($_SESSION))
{
$_SESSION = array();
session_destroy();
}
}
public static function unserializePhpSession($sessionData)
{
$res = array();
$offset = 0;
$length = strlen($sessionData);
while($offset < $length)
{
$pos = strpos($sessionData, "|", $offset);
$len = $pos - $offset;
$name = substr($sessionData, $offset, $len);
if($name === false)
{
break;
}
$offset += $len + 1;
$data = @unserialize(substr($sessionData, $offset));
$res[$name] = $data;
$offset += strlen(serialize($data));
}
return $res;
}
public static function getAllSessions()
{
$res = array();
$sessFiles = scandir(ini_get('session.save_path'));
$count = count($sessFiles);
for($i = 0; $i < $count; $i++)
{
if($sessFiles[$i][0] === '.')
{
continue;
}
$sessionId = substr($sessFiles[$i], 5);
$sessionData = file_get_contents(ini_get('session.save_path').'/'.$sessFiles[$i]);
if($sessionData === false)
{
array_push($res, array('sid' => $sessionId));
}
else
{
$tmp = FlipSession::unserializePhpSession($sessionData);
$tmp['sid'] = $sessionId;
array_push($res, $tmp);
}
}
if(count($res) == 0)
{
return false;
}
return $res;
}
public static function getSessionById($sid)
{
$sessionData = file_get_contents(ini_get('session.save_path').'/sess_'.$sid);
return FlipSession::unserializePhpSession($sessionData);
}
public static function deleteSessionById($sid)
{
return unlink(ini_get('session.save_path').'/sess_'.$sid);
}
}
/* vim: set tabstop=4 shiftwidth=4 expandtab: */