|
1
|
|
|
<?php |
|
2
|
|
|
require_once("gfunctions.php"); |
|
3
|
|
|
/** |
|
4
|
|
|
* Class login |
|
5
|
|
|
* handles the user's login and logout process |
|
6
|
|
|
*/ |
|
7
|
|
|
class Login |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array Collection of error messages |
|
11
|
|
|
*/ |
|
12
|
|
|
public $errors = array(); |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array Collection of success / neutral messages |
|
15
|
|
|
*/ |
|
16
|
|
|
public $messages = array(); |
|
17
|
|
|
/** |
|
18
|
|
|
* @var object The database connection |
|
19
|
|
|
*/ |
|
20
|
|
|
private $db_connection = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* the function "__construct()" automatically starts whenever an object of this class is created, |
|
24
|
|
|
* you know, when you do "$login = new Login();" |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
|
|
// create/read session, absolutely necessary |
|
29
|
|
|
//session_start(); |
|
30
|
|
|
// check the possible login actions: |
|
31
|
|
|
// if user tried to log out (happen when user clicks logout button) |
|
32
|
|
|
if (isset($_GET["logout"])) { |
|
33
|
|
|
$this->doLogout(); |
|
34
|
|
|
} // login via post data (if user just submitted a login form) |
|
35
|
|
|
elseif (isset($_POST["login"])) { |
|
36
|
|
|
$this->dologinWithPostData(); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* perform the logout |
|
43
|
|
|
*/ |
|
44
|
|
|
public function doLogout() |
|
45
|
|
|
{ |
|
46
|
|
|
// delete the session of the user |
|
47
|
|
|
if (isset($_SESSION['user_name'])) { |
|
48
|
|
|
logAction($_SESSION['user_name'], 'Logged Out', 1); |
|
49
|
|
|
} |
|
50
|
|
|
$_SESSION = array(); |
|
51
|
|
|
session_destroy(); |
|
52
|
|
|
// return a little feeedback message |
|
53
|
|
|
$this->messages[] = 'You have been logged out'; |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* log in with post data |
|
59
|
|
|
*/ |
|
60
|
|
|
private function dologinWithPostData() |
|
61
|
|
|
{ |
|
62
|
|
|
$settings = require('config/settings.php'); |
|
63
|
|
|
|
|
64
|
|
|
// check login form contents |
|
65
|
|
|
if (empty($_POST['user_name'])) { |
|
66
|
|
|
$this->errors[] = "Username field was empty."; |
|
67
|
|
|
} elseif (empty($_POST['user_password'])) { |
|
68
|
|
|
$this->errors[] = "Password field was empty."; |
|
69
|
|
|
} elseif (!empty($_POST['user_name']) && !empty($_POST['user_password'])) { |
|
70
|
|
|
|
|
71
|
|
|
if (isset($settings['db']['port'])) { |
|
72
|
|
|
$this->db_connection = new mysqli(decrypt($settings['db']['host']), decrypt($settings['db']['user']), decrypt($settings['db']['pass']), decrypt($settings['db']['name']), decrypt($settings['db']['port'])); |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->db_connection = new mysqli(decrypt($settings['db']['host']), decrypt($settings['db']['user']), decrypt($settings['db']['pass']), decrypt($settings['db']['name'])); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// change character set to utf8 and check it |
|
78
|
|
|
if (!$this->db_connection->set_charset("utf8")) { |
|
79
|
|
|
$this->errors[] = $this->db_connection->error; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// if no connection errors (= working database connection) |
|
83
|
|
|
if (!$this->db_connection->connect_errno) { |
|
84
|
|
|
|
|
85
|
|
|
// escape the POST stuff |
|
86
|
|
|
$user_name = $this->db_connection->real_escape_string($_POST['user_name']); |
|
87
|
|
|
|
|
88
|
|
|
// database query, getting all the info of the selected user (allows login via email address in the |
|
89
|
|
|
// username field) |
|
90
|
|
|
$sql = "SELECT user_name, user_email, user_level, user_profile, permissions, user_password_hash, user_id, playerid, twoFactor, token |
|
91
|
|
|
FROM users |
|
92
|
|
|
WHERE user_name = '" . $user_name . "' OR user_email = '" . $user_name . "';"; |
|
93
|
|
|
$result_of_login_check = $this->db_connection->query($sql); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
// if this user exists |
|
96
|
|
|
if ($result_of_login_check->num_rows == 1) { |
|
97
|
|
|
|
|
98
|
|
|
// get result row (as an object) |
|
99
|
|
|
$result_row = $result_of_login_check->fetch_object(); |
|
100
|
|
|
|
|
101
|
|
|
// using PHP 5.5's password_verify() function to check if the provided password fits |
|
102
|
|
|
// the hash of that user's password |
|
103
|
|
|
//var_dump(password_hash($_POST['user_password'], PASSWORD_DEFAULT)); |
|
|
|
|
|
|
104
|
|
|
if (password_verify($_POST['user_password'], $result_row->user_password_hash)) { |
|
105
|
|
|
if ($result_row->user_level <> 0) { |
|
106
|
|
|
//$verify = json_decode(file_get_contents('http://cyberbyte.org.uk/hooks/cyberworks/messages.php?id=' . $settings['id'])); |
|
|
|
|
|
|
107
|
|
|
//if (!isset($verify->verify)) { |
|
|
|
|
|
|
108
|
|
|
$_SESSION['2factor'] = 0; |
|
109
|
|
|
if (!empty($result_row->twoFactor)) { |
|
110
|
|
|
if ($settings['2factor']) $_SESSION['2factor'] = 1; else { |
|
111
|
|
|
$sql = "UPDATE `users` SET `backup`=NULL,`twoFactor`=NULL WHERE `userid` = '" . $result_row->user_id . "';"; |
|
112
|
|
|
$this->db_connection->query($sql); |
|
113
|
|
|
$this->errors[] = $lang['2factorForceRevoke']; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (isset($_COOKIE['token']) && !empty($result_row->token)) { |
|
118
|
|
|
if (decrypt($result_row->token) == $_COOKIE['token']) { |
|
119
|
|
|
$_SESSION['2factor'] = 2; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
$_SESSION['sudo'] = time(); |
|
123
|
|
|
//$_SESSION['message'] = $verify; |
|
|
|
|
|
|
124
|
|
|
$_SESSION['user_name'] = $result_row->user_name; |
|
125
|
|
|
$_SESSION['user_level'] = $result_row->user_level; |
|
126
|
|
|
$_SESSION['user_profile'] = $result_row->user_profile; |
|
127
|
|
|
$_SESSION['user_email'] = $result_row->user_email; |
|
128
|
|
|
$_SESSION['playerid'] = $result_row->playerid; |
|
129
|
|
|
$_SESSION['user_id'] = $result_row->user_id; |
|
130
|
|
|
$_SESSION['steamsignon'] = false; |
|
131
|
|
|
$_SESSION['permissions'] = json_decode($result_row->permissions, true); |
|
132
|
|
View Code Duplication |
if (isset($result_row->items))$_SESSION['items'] = $result_row->items; else $_SESSION['items'] = $settings['items']; |
|
|
|
|
|
|
133
|
|
|
if (isset($_POST['lang'])) { |
|
134
|
|
|
setcookie('lang', $_POST['lang'], time() + (3600 * 24 * 30)); |
|
135
|
|
|
$_SESSION['lang'] = $_POST['lang']; |
|
136
|
|
|
} |
|
137
|
|
|
$_SESSION['steamsignon'] = false; |
|
138
|
|
|
$_SESSION['user_login_status'] = 1; |
|
139
|
|
|
|
|
140
|
|
|
multiDB(); |
|
141
|
|
|
logAction($_SESSION['user_name'], 'Successful Login (' . $_SERVER['REMOTE_ADDR'] . ')', 2); |
|
142
|
|
|
/*} else { |
|
|
|
|
|
|
143
|
|
|
if (isset($verify->message)) { |
|
144
|
|
|
$this->errors[] = $verify->message; |
|
145
|
|
|
} else { |
|
146
|
|
|
$this->errors[] = "Verifcation Failed"; |
|
147
|
|
|
} |
|
148
|
|
|
}*/ |
|
149
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
150
|
|
|
$this->errors[] = "User is banned."; |
|
151
|
|
|
logAction($_POST['user_name'], 'Login Failed - Banned User (' . $_SERVER['REMOTE_ADDR'] . ')', 3); |
|
152
|
|
|
} |
|
153
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
154
|
|
|
$this->errors[] = "Wrong password. Try again."; |
|
155
|
|
|
logAction($_POST['user_name'], 'Login Failed - Wrong Password (' . $_SERVER['REMOTE_ADDR'] . ')', 3); |
|
156
|
|
|
} |
|
157
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
158
|
|
|
$this->errors[] = "This user does not exist."; |
|
159
|
|
|
logAction($_POST['user_name'], 'Login Failed - Wrong Username (' . $_SERVER['REMOTE_ADDR'] . ')', 3); |
|
160
|
|
|
} |
|
161
|
|
|
} else { |
|
162
|
|
|
$this->errors[] = "Database connection problem."; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* simply return the current state of the user's login |
|
169
|
|
|
* @return boolean user's login status |
|
170
|
|
|
*/ |
|
171
|
|
|
public function isUserLoggedIn() |
|
172
|
|
|
{ |
|
173
|
|
|
if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) { |
|
174
|
|
|
return true; |
|
175
|
|
|
} |
|
176
|
|
|
// default return |
|
177
|
|
|
return false; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
|
|
|
|
|
180
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.