-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.php
64 lines (64 loc) · 2.4 KB
/
auth.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
<?php
define('NO_HEAD', true);
require_once __DIR__ . '/includes/globals_out.php';
require_once __DIR__ . '/includes/class/class_mtg_functions.php';
require_once __DIR__ . '/includes/class/class_mtg_users.php';
$_POST['username'] = isset($_POST['username']) ? trim($_POST['username']) : null;
$_POST['password'] = isset($_POST['password']) ? trim($_POST['password']) : null;
if(empty($_POST['username'])) {
$_SESSION['msg'] = [
'type' => 'error',
'content' => 'You didn\'t enter your username'
];
exit(header("Location: login.php"));
}
if(empty($_POST['password'])) {
$_SESSION['msg'] = [
'type' => 'error',
'content' => 'You didn\'t enter your password'
];
exit(header("Location: login.php"));
}
$db->query("SELECT `id`, `password`, `account_locked`, `login_attempts` FROM `users` WHERE `username` = ?");
$db->execute([$_POST['username']]);
if(!$db->num_rows()) {
$_SESSION['msg'] = [
'type' => 'error',
'content' => 'An account with that name wasn\'t found'
];
exit(header("Location: login.php"));
}
$user = $db->fetch_row(true);
if(strtotime($user['account_locked']) >= time() && $user['login_attempts'] >= 5) {
$_SESSION['msg'] = [
'type' => 'error',
'content' => 'Your account has been temporarily locked due to too many failed login attempts.<br />You can try again in '.$mtg->time_format(strtotime($user['account_locked']) - time())
];
exit(header('Location: login.php'));
} else if(strtotime($user['account_locked']) < time() && $user['login_attempts']) {
$db->query('UPDATE `users` SET `account_locked` = "0000-00-00 00:00:00", `login_attempts` = 0 WHERE `id` = ?');
$db->execute([$user['id']]);
}
if(!password_verify($_POST['password'], $user['password'])) {
$_SESSION['msg'] = [
'type' => 'error',
'content' => 'That password was incorrect'
];
$db->startTrans();
$db->query('UPDATE `users` SET `login_attempts` = `login_attempts` + 1 WHERE `id` = ?');
$db->execute([$user['id']]);
if($user['login_attempts'] + 1 == 5) {
$db->query('UPDATE `users` SET `account_locked` = ? WHERE `id` = ?');
$db->execute([date('Y-m-d H:i:s', time() + 900), $user['id']]);
}
$db->endTrans();
exit(header("Location: login.php"));
}
$db->query('SELECT `id` FROM `users_ips` WHERE `ip` = ?');
$db->execute([$mtg->_ip()]);
if(!$db->num_rows()) {
$db->query('INSERT INTO `users_ips` (`user`, `ip`) VALUES (?, ?)');
$db->execute([$user['id'], $mtg->_ip()]);
}
$_SESSION['userid'] = $user['id'];
header('Location: index.php');