-
Notifications
You must be signed in to change notification settings - Fork 204
/
SessionData.php
428 lines (377 loc) · 11.8 KB
/
SessionData.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
require_once 'Flux/DataObject.php';
require_once 'Flux/ItemShop/Cart.php';
require_once 'Flux/LoginError.php';
/**
* Contains all of Flux's session data.
*/
class Flux_SessionData {
/**
* Actual session data array.
*
* @access private
* @var array
*/
private $sessionData;
/**
* Session data filters.
*
* @access private
* @var array
*/
private $dataFilters = array();
/**
* Selected login server group.
*
* @access public
* @var Flux_LoginAthenaGroup
*/
public $loginAthenaGroup;
/**
* Selected login server.
*
* @access public
* @var Flux_LoginServer
*/
public $loginServer;
/**
* Account object.
*
* @access public
* @var Flux_DataObject
*/
public $account;
/**
* Create new SessionData instance.
*
* @param array $sessionData
* @access public
*/
public function __construct(array &$sessionData, $logout = false)
{
$this->sessionData = &$sessionData;
if ($logout) {
$this->logout();
}
else {
$this->initialize();
}
}
/**
* Initialize session data.
*
* @param bool $force
* @return bool
* @access private
*/
private function initialize($force = false)
{
$keysToInit = array('username', 'serverName', 'athenaServerName', 'securityCode', 'theme');
foreach ($keysToInit as $key) {
if ($force || !$this->{$key}) {
$method = ucfirst($key);
$method = "set{$method}Data";
$this->$method(null);
}
}
$loggedIn = true;
if (!$this->username) {
$loggedIn = false;
$cfgAthenaServerName = Flux::config('DefaultCharMapServer');
$cfgLoginAthenaGroup = Flux::config('DefaultLoginGroup');
if (Flux::getServerGroupByName($cfgLoginAthenaGroup)){
$this->setServerNameData($cfgLoginAthenaGroup);
}
else {
$defaultServerName = current(array_keys(Flux::$loginAthenaGroupRegistry));
$this->setServerNameData($defaultServerName);
}
}
if ($this->serverName && ($this->loginAthenaGroup = Flux::getServerGroupByName($this->serverName))) {
$this->loginServer = $this->loginAthenaGroup->loginServer;
if (!$loggedIn && $cfgAthenaServerName && $this->getAthenaServer($cfgAthenaServerName)) {
$this->setAthenaServerNameData($cfgAthenaServerName);
}
if (!$this->athenaServerName || ((!$loggedIn && !$this->getAthenaServer($cfgAthenaServerName)) || !$this->getAthenaServer($this->athenaServerName))) {
$this->setAthenaServerNameData(current($this->getAthenaServerNames()));
}
}
// Get new account data every request.
if ($this->loginAthenaGroup && $this->username && ($account = $this->getAccount($this->loginAthenaGroup, $this->username))) {
$this->account = $account;
$this->account->group_level = AccountLevel::getGroupLevel($account->group_id);
// Automatically log out of account when detected as banned.
$permBan = ($account->state == 5 && !Flux::config('AllowPermBanLogin'));
$tempBan = (($account->unban_time > 0 && $account->unban_time < time()) && !Flux::config('AllowTempBanLogin'));
if ($permBan || $tempBan) {
$this->logout();
}
}
else {
$this->account = new Flux_DataObject(null, array('group_level' => AccountLevel::UNAUTH));
}
if (!is_array($this->cart)) {
$this->setCartData(array());
}
if ($this->account->account_id && $this->loginAthenaGroup) {
if (!array_key_exists($this->loginAthenaGroup->serverName, $this->cart)) {
$this->cart[$this->loginAthenaGroup->serverName] = array();
}
foreach ($this->getAthenaServerNames() as $athenaServerName) {
$athenaServer = $this->getAthenaServer($athenaServerName);
$cartArray = &$this->cart[$this->loginAthenaGroup->serverName];
$accountID = $this->account->account_id;
if (!array_key_exists($accountID, $cartArray)) {
$cartArray[$accountID] = array();
}
if (!array_key_exists($athenaServerName, $cartArray[$accountID])) {
$cartArray[$accountID][$athenaServerName] = new Flux_ItemShop_Cart();
}
$cartArray[$accountID][$athenaServerName]->setAccount($this->account);
$athenaServer->setCart($cartArray[$accountID][$athenaServerName]);
}
}
if (!$this->theme || $this->theme === 'installer') { // always update if coming from installer
$this->setThemeData(Flux::config('ThemeName.0'));
}
return true;
}
/**
* Log current user out.
*
* @return bool
* @access public
*/
public function logout()
{
$this->loginAthenaGroup = null;
$this->loginServer = null;
return $this->initialize(true);
}
public function __call($method, $args)
{
if (count($args) && preg_match('/set(.+?)Data/', $method, $m)) {
$arg = current($args);
$meth = $m[1];
$meth[0] = strtolower($meth[0]);
if (array_key_exists($meth, $this->dataFilters)) {
foreach ($this->dataFilters[$meth] as $callback) {
$arg = call_user_func($callback, $arg);
}
}
$this->sessionData[$meth] = $arg;
}
}
public function &__get($prop)
{
$value = null;
if (array_key_exists($prop, $this->sessionData)) {
$value = &$this->sessionData[$prop];
}
return $value;
}
/**
* Set session data.
*
* @param array $keys Session keys to be affected.
* @param mixed $value Value to be assigned to all specified keys.
* @return mixed whatever was set
* @access public
*/
public function setData(array $keys, $value)
{
foreach ($keys as $key) {
$key = ucfirst($key);
$this->{"set{$key}Data"}($value);
}
return $value;
}
/**
* Add a session data setter filter.
*
* @param string $key Which session key
* @param string $callback Function callback.
* @return string Callback
* @access public
*/
public function addDataFilter($key, $callback)
{
if (!array_key_exists($key, $this->dataFilters)) {
$this->dataFilters[$key] = array();
}
$this->dataFilters[$key][] = $callback;
return $callback;
}
/**
* Checks whether the current user is logged in.
*/
public function isLoggedIn()
{
return $this->account->group_level >= AccountLevel::NORMAL;
}
/**
* Check securityCode from user with $this->securityCode or reCaptcha
* @param $securityCode Code from user
* @param $recaptcha True if check using recaptcha
* @return true on success and false on failure
*/
public function checkSecurityCode($securityCode, $recaptcha = false) {
if ($recaptcha) {
if (Flux::config('ReCaptchaPrivateKey') && Flux::config('ReCaptchaPublicKey')) {
$responseKeys = array();
if (isset($_POST['g-recaptcha-response']) && $_POST['g-recaptcha-response'] != "") {
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".Flux::config('ReCaptchaPrivateKey')."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$responseKeys = json_decode($response,true);
}
if (count($responseKeys) && intval($responseKeys["success"]) == 1) {
return true;
}
}
}
else if ($securityCode && !empty($securityCode) && strtolower($securityCode) == strtolower($this->securityCode)) {
return true;
}
return false;
}
/**
* User login.
*
* @param string $server Server name
* @param string $username
* @param string $password
* @throws Flux_LoginError
* @access public
*/
public function login($server, $username, $password, $securityCode = null)
{
$loginAthenaGroup = Flux::getServerGroupByName($server);
if (!$loginAthenaGroup) {
throw new Flux_LoginError('Invalid server.', Flux_LoginError::INVALID_SERVER);
}
if ($loginAthenaGroup->loginServer->isIpBanned() && !Flux::config('AllowIpBanLogin')) {
throw new Flux_LoginError('IP address is banned', Flux_LoginError::IPBANNED);
}
if (Flux::config('UseLoginCaptcha') && !self::checkSecurityCode($securityCode, Flux::config('EnableReCaptcha'))) {
throw new Flux_LoginError('Invalid security code', Flux_LoginError::INVALID_SECURITY_CODE);
}
if (!$loginAthenaGroup->isAuth($username, $password)) {
throw new Flux_LoginError('Invalid login', Flux_LoginError::INVALID_LOGIN);
}
$creditsTable = Flux::config('FluxTables.CreditsTable');
$creditColumns = 'credits.balance, credits.last_donation_date, credits.last_donation_amount';
$sql = "SELECT login.*, {$creditColumns} FROM {$loginAthenaGroup->loginDatabase}.login ";
$sql .= "LEFT OUTER JOIN {$loginAthenaGroup->loginDatabase}.{$creditsTable} AS credits ON login.account_id = credits.account_id ";
$sql .= "WHERE login.sex != 'S' AND login.group_id >= 0 AND login.userid = ? LIMIT 1";
$smt = $loginAthenaGroup->connection->getStatement($sql);
$res = $smt->execute(array($username));
if ($res && ($row = $smt->fetch())) {
if ($row->unban_time > 0) {
if (time() >= $row->unban_time) {
$row->unban_time = 0;
$sql = "UPDATE {$loginAthenaGroup->loginDatabase}.login SET unban_time = 0 WHERE account_id = ?";
$sth = $loginAthenaGroup->connection->getStatement($sql);
$sth->execute(array($row->account_id));
}
elseif (!Flux::config('AllowTempBanLogin')) {
throw new Flux_LoginError('Temporarily banned', Flux_LoginError::BANNED);
}
}
if ($row->state == 5) {
$createTable = Flux::config('FluxTables.AccountCreateTable');
$sql = "SELECT id FROM {$loginAthenaGroup->loginDatabase}.$createTable ";
$sql .= "WHERE account_id = ? AND confirmed = 0";
$sth = $loginAthenaGroup->connection->getStatement($sql);
$sth->execute(array($row->account_id));
$row2 = $sth->fetch();
if ($row2 && $row2->id) {
throw new Flux_LoginError('Pending confirmation', Flux_LoginError::PENDING_CONFIRMATION);
}
}
if (!Flux::config('AllowPermBanLogin') && $row->state == 5) {
throw new Flux_LoginError('Permanently banned', Flux_LoginError::PERMABANNED);
}
$this->setServerNameData($server);
$this->setUsernameData($username);
$this->initialize(false);
}
else {
$message = "Unexpected error during login.\n";
$message .= 'PDO error info, if any: '.print_r($smt->errorInfo(), true);
throw new Flux_LoginError($message, Flux_LoginError::UNEXPECTED);
}
return true;
}
/**
* Get account object for a particular user name.
*
* @param Flux_LoginAthenaGroup $loginAthenaGroup
* @param string $username
* @return mixed
* @access private
*/
private function getAccount(Flux_LoginAthenaGroup $loginAthenaGroup, $username)
{
$creditsTable = Flux::config('FluxTables.CreditsTable');
$creditColumns = 'credits.balance, credits.last_donation_date, credits.last_donation_amount';
$sql = "SELECT login.*, {$creditColumns} FROM {$loginAthenaGroup->loginDatabase}.login ";
$sql .= "LEFT OUTER JOIN {$loginAthenaGroup->loginDatabase}.{$creditsTable} AS credits ON login.account_id = credits.account_id ";
$sql .= "WHERE login.sex != 'S' AND login.group_id >= 0 AND login.userid = ? LIMIT 1";
$smt = $loginAthenaGroup->connection->getStatement($sql);
$res = $smt->execute(array($username));
if ($res && ($row = $smt->fetch())) {
return $row;
}
else {
return false;
}
}
/**
* Get available server names.
*
* @access public
*/
public function getAthenaServerNames()
{
if ($this->loginAthenaGroup) {
$names = array();
foreach ($this->loginAthenaGroup->athenaServers as $server) {
$names[] = $server->serverName;
}
return $names;
}
else {
return array();
}
}
/**
* Get a Flux_Athena instance by its name based on current server settings.
*
* @param string $name
* @access public
*/
public function getAthenaServer($name = null)
{
if (is_null($name) && $this->athenaServerName) {
return $this->getAthenaServer($this->athenaServerName);
}
if ($this->loginAthenaGroup && ($server = Flux::getAthenaServerByName($this->serverName, $name))) {
return $server;
}
else {
return false;
}
}
/**
* Get flash message.
*
* @return string
* @access public
*/
public function getMessage()
{
$message = $this->message;
$this->setMessageData(null);
return $message;
}
}
?>