1
|
|
|
<?php |
2
|
|
|
/* zKillboard |
3
|
|
|
* Copyright (C) 2012-2015 EVE-KILL Team and EVSCO. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
class User |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param string $username |
22
|
|
|
* @param string $password |
23
|
|
|
* @param bool $autoLogin |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public static function setLogin($username, $password, $autoLogin) |
27
|
|
|
{ |
28
|
|
|
global $cookie_name, $cookie_time, $cookie_ssl, $baseAddr, $app; |
29
|
|
|
$hash = Password::genPassword($password); |
30
|
|
|
if ($autoLogin) { |
31
|
|
|
$hash = $username."/".hash("sha256", $username.$hash.time()); |
32
|
|
|
$validTill = date("Y-m-d H:i:s", time() + $cookie_time); |
33
|
|
|
$userID = Db::queryField("SELECT id FROM zz_users WHERE username = :username", "id", array(":username" => $username), 0); |
34
|
|
|
$userAgent = $_SERVER["HTTP_USER_AGENT"]; |
35
|
|
|
$ip = IP::get(); |
36
|
|
|
Db::execute("INSERT INTO zz_users_sessions (userID, sessionHash, validTill, userAgent, ip) VALUES (:userID, :sessionHash, :validTill, :userAgent, :ip)", |
37
|
|
|
array(":userID" => $userID, ":sessionHash" => $hash, ":validTill" => $validTill, ":userAgent" => $userAgent, ":ip" => $ip)); |
38
|
|
|
$app->setEncryptedCookie($cookie_name, $hash, time() + $cookie_time, "/", $baseAddr, $cookie_ssl, true); |
39
|
|
|
} |
40
|
|
|
$_SESSION["loggedin"] = $username; |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $username |
46
|
|
|
* @param string $password |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public static function checkLogin($username, $password) |
50
|
|
|
{ |
51
|
|
|
$p = Db::query("SELECT username, password FROM zz_users WHERE username = :username", array(":username" => $username), 0); |
52
|
|
|
if(!empty($p[0])) |
53
|
|
|
{ |
54
|
|
|
$pw = $p[0]["password"]; |
55
|
|
|
|
56
|
|
|
if(Password::checkPassword($password, $pw)) |
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $userID |
65
|
|
|
* @return array|null |
66
|
|
|
*/ |
67
|
|
|
public static function checkLoginHashed($userID) |
68
|
|
|
{ |
69
|
|
|
return Db::query("SELECT sessionHash FROM zz_users_sessions WHERE userID = :userID AND now() < validTill", array(":userID" => $userID), 0); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public static function autoLogin() |
76
|
|
|
{ |
77
|
|
|
global $cookie_name, $cookie_time, $app; |
78
|
|
|
$sessionCookie = $app->getEncryptedCookie($cookie_name, false); |
79
|
|
|
|
80
|
|
|
if (!empty($sessionCookie)) { |
81
|
|
|
$cookie = explode("/", $sessionCookie); |
82
|
|
|
$username = $cookie[0]; |
83
|
|
|
//$cookieHash = $cookie[1]; |
84
|
|
|
$userID = Db::queryField("SELECT id FROM zz_users WHERE username = :username", "id", array(":username" => $username), 0); |
85
|
|
|
$hashes = self::checkLoginHashed($userID); |
86
|
|
|
foreach($hashes as $hash) |
87
|
|
|
{ |
88
|
|
|
$hash = $hash["sessionHash"]; |
89
|
|
|
if ($sessionCookie == $hash) { |
90
|
|
|
$_SESSION["loggedin"] = $username; |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public static function isLoggedIn() |
103
|
|
|
{ |
104
|
|
|
return isset($_SESSION["loggedin"]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array|null |
109
|
|
|
*/ |
110
|
|
|
public static function getUserInfo() |
111
|
|
|
{ |
112
|
|
|
if (isset($_SESSION["loggedin"])) { |
113
|
|
|
$id = Db::query("SELECT id, username, email, dateCreated, admin, moderator, characterID FROM zz_users WHERE username = :username", array(":username" => $_SESSION["loggedin"]), 1); |
114
|
|
|
return @array("id" => $id[0]["id"], "username" => $id[0]["username"], "admin" => $id[0]["admin"], "moderator" => $id[0]["moderator"], "email" => $id[0]["email"], "characterID" => $id[0]["characterID"], "dateCreated" => $id[0]["dateCreated"]); |
115
|
|
|
} |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int|null |
121
|
|
|
*/ |
122
|
|
|
public static function getUserID() |
123
|
|
|
{ |
124
|
|
|
if (isset($_SESSION["loggedin"])) { |
125
|
|
|
$id = Db::queryField("SELECT id FROM zz_users WHERE username = :username", "id", array(":username" => $_SESSION["loggedin"]), 1); |
126
|
|
|
return (int) $id; |
127
|
|
|
} |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public static function isModerator() |
135
|
|
|
{ |
136
|
|
|
$info = self::getUserInfo(); |
137
|
|
|
return $info["moderator"] == 1; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public static function isAdmin() |
144
|
|
|
{ |
145
|
|
|
$info = self::getUserInfo(); |
146
|
|
|
return $info["admin"] == 1; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param int $userID |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public static function getUsername($userID) |
154
|
|
|
{ |
155
|
|
|
return Db::queryField("SELECT username FROM zz_users WHERE userID = :userID", array(":userID" => $userID)); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $userID |
160
|
|
|
* @return array|null |
161
|
|
|
*/ |
162
|
|
|
public static function getSessions($userID) |
163
|
|
|
{ |
164
|
|
|
return Db::query("SELECT sessionHash, dateCreated, validTill, userAgent, ip FROM zz_users_sessions WHERE userID = :userID", array(":userID" => $userID), 0); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param int $userID |
169
|
|
|
* @param string $sessionHash |
170
|
|
|
*/ |
171
|
|
|
public static function deleteSession($userID, $sessionHash) |
172
|
|
|
{ |
173
|
|
|
Db::execute("DELETE FROM zz_users_sessions WHERE userID = :userID AND sessionHash = :sessionHash", array(":userID" => $userID, ":sessionHash" => $sessionHash)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public static function getBalance($userID) |
177
|
|
|
{ |
178
|
|
|
$balance = Db::queryField("select balance from zz_account_balance where userID = :userID", "balance", array(":userID" => $userID), 0); |
179
|
|
|
if ($balance == null) $balance = 0; |
180
|
|
|
return $balance; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public static function getPaymentHistory($userID) |
184
|
|
|
{ |
185
|
|
|
return Db::query("select * from zz_account_history where userID = :userID", array(":userID" => $userID), 0); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|