-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSession.class.php
More file actions
213 lines (171 loc) · 6.99 KB
/
Copy pathSession.class.php
File metadata and controls
213 lines (171 loc) · 6.99 KB
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
<?php namespace EmPHyre;
/**
* Session is the session object for the EmPHyre project
*
* PHP version 7
*
* ------
* These files are part of the empiresPHPframework;
* The original framework core (specifically the mysql.php
* the router.php and the errorlog) was started by Timo Ewalds,
* and rewritten to use APC and extended by Julian Haagsma,
* for use in Earth Empires (located at http://www.earthempires.com );
* it was spun out for use on other projects.
*
* The general.php contains content from Earth Empires
* written by Dave McVittie and Joe Obbish.
*
* The example website files were written by Julian Haagsma.
*
* @category Core
* @package EmPHyre
* @author Julian Haagsma <jhaagsma@gmail.com>
* @author Timo Ewalds <tewalds@gmail.com>
* @author Dave McVittie <dave.mcvittie@gmail.com>
* @author Joe Obbish <slagpit@earthempires.com>
* @license All files are licensed under the MIT License.
* @link https://github.com/jhaagsma/emPHyre
* @since Recombined from empiresPHPframework extensions on Nov 4 2016
*/
//THIS IS HOW LONG WE WANT LOGIN SESSIONS TO LAST IN SECONDS (obivously)
define('SESSION_TIME', 16 * 3600);
defined('COOKIE_NAME') or define('COOKIE_NAME', 'emphyre');
defined('APC_LAST_CLEAR_SESSION') or define('APC_LAST_CLEAR_SESSION', 'em:lcs');
defined('APC_ACTIVE_SESSION_PREFIX') or define('APC_ACTIVE_SESSION_PREFIX', 'em:as-');
abstract class Session
{
private static $db;
private static $cookieUser = null;
private static $cookieKey = null;
private static $login_id = null;
private static $expiretime = null;
private static $lastreal = null;
public static function newSession($user_id)
{
self::$db = Container::getDb();
$time = time();
$key = md5(rand());
//THIS IS HOW LONG WE WANT LOGIN SESSIONS TO LAST IN SECONDS (obivously);
$expire = $time + SESSION_TIME;
//$expireday = $time + SESSION_TIME + 24*3600*60;
//set old sessions to expire in the past
$expire_old = $time - 300;
//set old sessions to expire in the past...
self::$db->pquery("UPDATE active_sessions SET expiretime = ? WHERE user_id = ?", $expire_old, $user_id);
//...and then clear old sessions to make sure they are gone
self::clearOldSessions(true);
$session_id = self::$db->pquery(
"INSERT INTO active_sessions SET user_id = ?, `cookieval` = ?, expiretime = ?",
$user_id,
$key,
$expire
)->insertid();
setcookie(COOKIE_NAME, "$user_id:$key", 0, "/", '.'.URL::getDomainName(), false, true);
}//end newSession()
public static function parseCookie()
{
//THIS IS ASSIGNING COOKIE VAL, SO SINGLE EQUALS IS CORRECT
if (!($cookiebits = URL::getCOOKIEval(COOKIE_NAME))) {
return null;
}
$cookiebits = explode(":", $cookiebits);
self::$cookieUser = $cookiebits[0];
self::$cookieKey = $cookiebits[1];
}//end parseCookie()
public static function doUserCheck()
{
//This function needs to be cleaned up and formalized in some manner
self::clearOldSessions();
self::parseCookie();
//THIS IS ASSIGNING ACTIVE ROW, SO SINGLE EQUALS IS CORRECT
if (!self::activeSession()) {
return null;
}
$expiretime = time() + SESSION_TIME;
self::updateActiveSession($expiretime);
return Container::newUser(self::$cookieUser); //Create a User class!
}//end doUserCheck()
//delete a specific key, or ALL of that user's sessions
//(that, of course, assumes that you can have mulitple logins)
public static function expireSession($single = false)
{
self::$db = Container::getDb();
self::parseCookie();
self::clearActiveSession();
if ($single) {
//Just this user session
self::$db->pquery(
"DELETE FROM active_sessions WHERE user_id = ? and `cookieval` = ?",
self::$cookieUser,
self::$cookieKey
);
} else {
//ALL of the user's sessions
self::$db->pquery("DELETE FROM active_sessions WHERE user_id = ?", self::$cookieUser);
}
//clear the cookie to nothing
setcookie(COOKIE_NAME, '', 1);
}//end expireSession()
public static function activeSession($force = false)
{
$activerow = \EmPHyre\Cache::fetch(APC_ACTIVE_SESSION_PREFIX.self::$cookieUser);
if (!$activerow || $activerow['cookieval'] != self::$cookieKey || $force) {
$activerow = self::$db->pquery(
'SELECT login_id, cookieval, expiretime FROM active_sessions WHERE user_id = ? and `cookieval` = ?',
self::$cookieUser,
self::$cookieKey
)->fetchRow();
if (!$activerow) {
return false;
}
$activerow['lastreal'] = 0;
}
self::$login_id = $activerow['login_id'];
self::$expiretime = $activerow['expiretime'];
self::$lastreal = $activerow['lastreal'];
return self::$expiretime > time() ? true : false;
}//end activeSession()
public static function clearOldSessions($forced = false)
{
self::$db = Container::getDb();
$not_due_yet = \EmPHyre\Cache::fetch(APC_LAST_CLEAR_SESSION);
if (!$not_due_yet || $forced) {
\EmPHyre\Cache::store(APC_LAST_CLEAR_SESSION, true, 120);
$clearids = self::$db->pquery('SELECT * FROM active_sessions WHERE expiretime < ?', time())->fetchRowSet();
foreach ($clearids as $clearid) {
\EmPHyre\Cache::delete(APC_ACTIVE_SESSION_PREFIX.$clearid['user_id']);
self::$db->pquery('DELETE FROM active_sessions WHERE login_id = ?', $clearid['login_id']);
}
return true;
}
return false;
}//end clearOldSessions()
private static function updateActiveSession($expiretime)
{
$activerow = [
'lastreal' => self::$lastreal,
'login_id' => self::$login_id,
'cookieval' => self::$cookieKey,
'expiretime' => $expiretime
];
self::$db = Container::getDb();
if (self::$lastreal + 120 < time()) {
$good = self::$db->pquery(
'UPDATE active_sessions SET expiretime = ? WHERE login_id = ?',
$expiretime,
self::$login_id
)->affectedRows();
if (!$good) {
\EmPHyre\Cache::delete(APC_ACTIVE_SESSION_PREFIX.self::$cookieUser);
return;
}
self::$lastreal = $activerow['lastreal'] = time();
}
\EmPHyre\Cache::store(APC_ACTIVE_SESSION_PREFIX.self::$cookieUser, $activerow, 120);
return;
}//end updateActiveSession()
public static function clearActiveSession()
{
\EmPHyre\Cache::delete(APC_ACTIVE_SESSION_PREFIX.self::$cookieUser);
}//end clearActiveSession()
}//end class