|
@@ -19,8 +19,11 @@ if (Tools::site_ban_ip($_SERVER['REMOTE_ADDR'])) {
|
19
|
19
|
error('Your IP address has been banned.');
|
20
|
20
|
}
|
21
|
21
|
|
22
|
|
-require(SERVER_ROOT.'/classes/validate.class.php');
|
23
|
|
-$Validate = NEW VALIDATE;
|
|
22
|
+require_once SERVER_ROOT.'/classes/twofa.class.php';
|
|
23
|
+require_once SERVER_ROOT.'/classes/validate.class.php';
|
|
24
|
+
|
|
25
|
+$Validate = new VALIDATE;
|
|
26
|
+$TwoFA = new TwoFactorAuth(SITE_NAME);
|
24
|
27
|
|
25
|
28
|
if (array_key_exists('action', $_GET) && $_GET['action'] == 'disabled') {
|
26
|
29
|
require('disabled.php');
|
|
@@ -231,11 +234,12 @@ else {
|
231
|
234
|
PermissionID,
|
232
|
235
|
CustomPermissions,
|
233
|
236
|
PassHash,
|
|
237
|
+ TwoFactor,
|
234
|
238
|
Enabled
|
235
|
239
|
FROM users_main
|
236
|
240
|
WHERE Username = '".db_string($_POST['username'])."'
|
237
|
241
|
AND Username != ''");
|
238
|
|
- list($UserID, $PermissionID, $CustomPermissions, $PassHash, $Enabled) = $DB->next_record(MYSQLI_NUM, array(2));
|
|
242
|
+ list($UserID, $PermissionID, $CustomPermissions, $PassHash, $TwoFactor, $Enabled) = $DB->next_record(MYSQLI_NUM, array(2));
|
239
|
243
|
if (!$Banned) {
|
240
|
244
|
if ($UserID && Users::check_password($_POST['password'], $PassHash)) {
|
241
|
245
|
// Update hash if better algorithm available
|
|
@@ -245,110 +249,116 @@ else {
|
245
|
249
|
SET PassHash = '".make_sec_hash($_POST['password'])."'
|
246
|
250
|
WHERE Username = '".db_string($_POST['username'])."'");
|
247
|
251
|
}
|
248
|
|
- if ($Enabled == 1) {
|
249
|
252
|
|
250
|
|
- // Check if the current login attempt is from a location previously logged in from
|
251
|
|
- if (apc_exists('DBKEY')) {
|
252
|
|
- $DB->query("
|
253
|
|
- SELECT IP
|
254
|
|
- FROM users_history_ips
|
255
|
|
- WHERE UserID = $UserID");
|
256
|
|
- $IPs = $DB->to_array(false, MYSQLI_NUM);
|
257
|
|
- $QueryParts = array();
|
258
|
|
- foreach ($IPs as $i => $IP) {
|
259
|
|
- $IPs[$i] = DBCrypt::decrypt($IP[0]);
|
260
|
|
- }
|
261
|
|
- $IPs = array_unique($IPs);
|
262
|
|
- if (count($IPs) > 0) { // Always allow first login
|
263
|
|
- foreach ($IPs as $IP) {
|
264
|
|
- $QueryParts[] = "(StartIP<=INET6_ATON('$IP') AND EndIP>=INET6_ATON('$IP'))";
|
|
253
|
+ if (empty($TwoFactor) || $TwoFA->verifyCode($TwoFactor, $_POST['twofa'])) {
|
|
254
|
+ if ($Enabled == 1) {
|
|
255
|
+
|
|
256
|
+ // Check if the current login attempt is from a location previously logged in from
|
|
257
|
+ if (apc_exists('DBKEY')) {
|
|
258
|
+ $DB->query("
|
|
259
|
+ SELECT IP
|
|
260
|
+ FROM users_history_ips
|
|
261
|
+ WHERE UserID = $UserID");
|
|
262
|
+ $IPs = $DB->to_array(false, MYSQLI_NUM);
|
|
263
|
+ $QueryParts = array();
|
|
264
|
+ foreach ($IPs as $i => $IP) {
|
|
265
|
+ $IPs[$i] = DBCrypt::decrypt($IP[0]);
|
265
|
266
|
}
|
266
|
|
- $DB->query('SELECT ASN FROM geoip_asn WHERE '.implode(' OR ', $QueryParts));
|
267
|
|
- $PastASNs = array_column($DB->to_array(false, MYSQLI_NUM), 0);
|
268
|
|
- $DB->query("SELECT ASN FROM geoip_asn WHERE StartIP<=INET6_ATON('$_SERVER[REMOTE_ADDR]') AND EndIP>=INET6_ATON('$_SERVER[REMOTE_ADDR]')");
|
269
|
|
- list($CurrentASN) = $DB->next_record();
|
270
|
|
-
|
271
|
|
- if (!in_array($CurrentASN, $PastASNs)) {
|
272
|
|
- // Never logged in from this location before
|
273
|
|
- if ($Cache->get_value('new_location_'.$UserID.'_'.$CurrentASN) !== true) {
|
274
|
|
- $DB->query("
|
275
|
|
- SELECT
|
276
|
|
- UserName,
|
277
|
|
- Email
|
278
|
|
- FROM users_main
|
279
|
|
- WHERE ID = $UserID");
|
280
|
|
- list($Username, $Email) = $DB->next_record();
|
281
|
|
- Users::auth_location($UserID, $Username, $CurrentASN, DBCrypt::decrypt($Email));
|
282
|
|
- require('newlocation.php');
|
283
|
|
- die();
|
|
267
|
+ $IPs = array_unique($IPs);
|
|
268
|
+ if (count($IPs) > 0) { // Always allow first login
|
|
269
|
+ foreach ($IPs as $IP) {
|
|
270
|
+ $QueryParts[] = "(StartIP<=INET6_ATON('$IP') AND EndIP>=INET6_ATON('$IP'))";
|
|
271
|
+ }
|
|
272
|
+ $DB->query('SELECT ASN FROM geoip_asn WHERE '.implode(' OR ', $QueryParts));
|
|
273
|
+ $PastASNs = array_column($DB->to_array(false, MYSQLI_NUM), 0);
|
|
274
|
+ $DB->query("SELECT ASN FROM geoip_asn WHERE StartIP<=INET6_ATON('$_SERVER[REMOTE_ADDR]') AND EndIP>=INET6_ATON('$_SERVER[REMOTE_ADDR]')");
|
|
275
|
+ list($CurrentASN) = $DB->next_record();
|
|
276
|
+
|
|
277
|
+ if (!in_array($CurrentASN, $PastASNs)) {
|
|
278
|
+ // Never logged in from this location before
|
|
279
|
+ if ($Cache->get_value('new_location_'.$UserID.'_'.$CurrentASN) !== true) {
|
|
280
|
+ $DB->query("
|
|
281
|
+ SELECT
|
|
282
|
+ UserName,
|
|
283
|
+ Email
|
|
284
|
+ FROM users_main
|
|
285
|
+ WHERE ID = $UserID");
|
|
286
|
+ list($Username, $Email) = $DB->next_record();
|
|
287
|
+ Users::auth_location($UserID, $Username, $CurrentASN, DBCrypt::decrypt($Email));
|
|
288
|
+ require('newlocation.php');
|
|
289
|
+ die();
|
|
290
|
+ }
|
284
|
291
|
}
|
285
|
292
|
}
|
286
|
293
|
}
|
287
|
|
- }
|
288
|
294
|
|
289
|
|
- $SessionID = Users::make_secret(64);
|
290
|
|
- $KeepLogged = ($_POST['keeplogged'] ?? false) ? 1 : 0;
|
291
|
|
- setcookie('session', $SessionID, (time()+60*60*24*365)*$KeepLogged, '/', '', true, true);
|
292
|
|
- setcookie('userid', $UserID, (time()+60*60*24*365)*$KeepLogged, '/', '', true, true);
|
293
|
|
-
|
294
|
|
- // Because we <3 our staff
|
295
|
|
- $Permissions = Permissions::get_permissions($PermissionID);
|
296
|
|
- $CustomPermissions = unserialize($CustomPermissions);
|
297
|
|
- if (isset($Permissions['Permissions']['site_disable_ip_history'])
|
298
|
|
- || isset($CustomPermissions['site_disable_ip_history'])
|
299
|
|
- ) {
|
300
|
|
- $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
301
|
|
- }
|
|
295
|
+ $SessionID = Users::make_secret(64);
|
|
296
|
+ $KeepLogged = ($_POST['keeplogged'] ?? false) ? 1 : 0;
|
|
297
|
+ setcookie('session', $SessionID, (time()+60*60*24*365)*$KeepLogged, '/', '', true, true);
|
|
298
|
+ setcookie('userid', $UserID, (time()+60*60*24*365)*$KeepLogged, '/', '', true, true);
|
|
299
|
+
|
|
300
|
+ // Because we <3 our staff
|
|
301
|
+ $Permissions = Permissions::get_permissions($PermissionID);
|
|
302
|
+ $CustomPermissions = unserialize($CustomPermissions);
|
|
303
|
+ if (isset($Permissions['Permissions']['site_disable_ip_history'])
|
|
304
|
+ || isset($CustomPermissions['site_disable_ip_history'])
|
|
305
|
+ ) {
|
|
306
|
+ $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
307
|
+ }
|
302
|
308
|
|
303
|
|
- $DB->query("
|
304
|
|
- INSERT INTO users_sessions
|
305
|
|
- (UserID, SessionID, KeepLogged, Browser, OperatingSystem, IP, LastUpdate, FullUA)
|
306
|
|
- VALUES
|
307
|
|
- ('$UserID', '".db_string($SessionID)."', '$KeepLogged', '$Browser', '$OperatingSystem', '".db_string(apc_exists('DBKEY')?DBCrypt::encrypt($_SERVER['REMOTE_ADDR']):'0.0.0.0')."', '".sqltime()."', '".db_string($_SERVER['HTTP_USER_AGENT'])."')");
|
308
|
|
-
|
309
|
|
- $Cache->begin_transaction("users_sessions_$UserID");
|
310
|
|
- $Cache->insert_front($SessionID, array(
|
311
|
|
- 'SessionID' => $SessionID,
|
312
|
|
- 'Browser' => $Browser,
|
313
|
|
- 'OperatingSystem' => $OperatingSystem,
|
314
|
|
- 'IP' => (apc_exists('DBKEY')?DBCrypt::encrypt($_SERVER['REMOTE_ADDR']):'0.0.0.0'),
|
315
|
|
- 'LastUpdate' => sqltime()
|
316
|
|
- ));
|
317
|
|
- $Cache->commit_transaction(0);
|
318
|
|
-
|
319
|
|
- $Sql = "
|
320
|
|
- UPDATE users_main
|
321
|
|
- SET
|
322
|
|
- LastLogin = '".sqltime()."',
|
323
|
|
- LastAccess = '".sqltime()."'
|
324
|
|
- WHERE ID = '".db_string($UserID)."'";
|
325
|
|
-
|
326
|
|
- $DB->query($Sql);
|
327
|
|
-
|
328
|
|
- if (!empty($_COOKIE['redirect'])) {
|
329
|
|
- $URL = $_COOKIE['redirect'];
|
330
|
|
- setcookie('redirect', '', time() - 60 * 60 * 24, '/', '', false);
|
331
|
|
- header("Location: $URL");
|
332
|
|
- die();
|
|
309
|
+ $DB->query("
|
|
310
|
+ INSERT INTO users_sessions
|
|
311
|
+ (UserID, SessionID, KeepLogged, Browser, OperatingSystem, IP, LastUpdate, FullUA)
|
|
312
|
+ VALUES
|
|
313
|
+ ('$UserID', '".db_string($SessionID)."', '$KeepLogged', '$Browser', '$OperatingSystem', '".db_string(apc_exists('DBKEY')?DBCrypt::encrypt($_SERVER['REMOTE_ADDR']):'0.0.0.0')."', '".sqltime()."', '".db_string($_SERVER['HTTP_USER_AGENT'])."')");
|
|
314
|
+
|
|
315
|
+ $Cache->begin_transaction("users_sessions_$UserID");
|
|
316
|
+ $Cache->insert_front($SessionID, array(
|
|
317
|
+ 'SessionID' => $SessionID,
|
|
318
|
+ 'Browser' => $Browser,
|
|
319
|
+ 'OperatingSystem' => $OperatingSystem,
|
|
320
|
+ 'IP' => (apc_exists('DBKEY')?DBCrypt::encrypt($_SERVER['REMOTE_ADDR']):'0.0.0.0'),
|
|
321
|
+ 'LastUpdate' => sqltime()
|
|
322
|
+ ));
|
|
323
|
+ $Cache->commit_transaction(0);
|
|
324
|
+
|
|
325
|
+ $Sql = "
|
|
326
|
+ UPDATE users_main
|
|
327
|
+ SET
|
|
328
|
+ LastLogin = '".sqltime()."',
|
|
329
|
+ LastAccess = '".sqltime()."'
|
|
330
|
+ WHERE ID = '".db_string($UserID)."'";
|
|
331
|
+
|
|
332
|
+ $DB->query($Sql);
|
|
333
|
+
|
|
334
|
+ if (!empty($_COOKIE['redirect'])) {
|
|
335
|
+ $URL = $_COOKIE['redirect'];
|
|
336
|
+ setcookie('redirect', '', time() - 60 * 60 * 24, '/', '', false);
|
|
337
|
+ header("Location: $URL");
|
|
338
|
+ die();
|
|
339
|
+ } else {
|
|
340
|
+ header('Location: index.php');
|
|
341
|
+ die();
|
|
342
|
+ }
|
333
|
343
|
} else {
|
334
|
|
- header('Location: index.php');
|
335
|
|
- die();
|
|
344
|
+ log_attempt();
|
|
345
|
+ if ($Enabled == 2) {
|
|
346
|
+
|
|
347
|
+ // Save the username in a cookie for the disabled page
|
|
348
|
+ setcookie('username', db_string($_POST['username']), time() + 60 * 60, '/', '', false);
|
|
349
|
+ header('Location: login.php?action=disabled');
|
|
350
|
+ } elseif ($Enabled == 0) {
|
|
351
|
+ $Err = 'Your account has not been confirmed.<br />Please check your email.';
|
|
352
|
+ }
|
|
353
|
+ setcookie('keeplogged', '', time() + 60 * 60 * 24 * 365, '/', '', false);
|
336
|
354
|
}
|
337
|
355
|
} else {
|
338
|
356
|
log_attempt();
|
339
|
|
- if ($Enabled == 2) {
|
340
|
|
-
|
341
|
|
- // Save the username in a cookie for the disabled page
|
342
|
|
- setcookie('username', db_string($_POST['username']), time() + 60 * 60, '/', '', false);
|
343
|
|
- header('Location: login.php?action=disabled');
|
344
|
|
- } elseif ($Enabled == 0) {
|
345
|
|
- $Err = 'Your account has not been confirmed.<br />Please check your email.';
|
346
|
|
- }
|
|
357
|
+ $Err = 'Two-factor authentication failed.';
|
347
|
358
|
setcookie('keeplogged', '', time() + 60 * 60 * 24 * 365, '/', '', false);
|
348
|
359
|
}
|
349
|
360
|
} else {
|
350
|
361
|
log_attempt();
|
351
|
|
-
|
352
|
362
|
$Err = 'Your username or password was incorrect.';
|
353
|
363
|
setcookie('keeplogged', '', time() + 60 * 60 * 24 * 365, '/', '', false);
|
354
|
364
|
}
|