|
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 Password |
|
19
|
|
|
{ |
|
20
|
|
|
public static function genPassword($password) |
|
21
|
|
|
{ |
|
22
|
|
|
return password_hash($password, PASSWORD_BCRYPT); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function updatePassword($password) |
|
26
|
|
|
{ |
|
27
|
|
|
$userID = user::getUserID(); |
|
28
|
|
|
$password = self::genPassword($password); |
|
29
|
|
|
Db::execute("UPDATE zz_users SET password = :password WHERE id = :userID", array(":password" => $password, ":userID" => $userID)); |
|
30
|
|
|
return "Updated password"; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $plainTextPassword |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function checkPassword($plainTextPassword, $storedPassword = NULL) |
|
37
|
|
|
{ |
|
38
|
|
|
if($plainTextPassword && $storedPassword) |
|
39
|
|
|
return self::pwCheck($plainTextPassword, $storedPassword); |
|
40
|
|
|
else |
|
41
|
|
|
{ |
|
42
|
|
|
$userID = user::getUserID(); |
|
43
|
|
|
if($userID) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$storedPw = Db::queryField("SELECT password FROM zz_users WHERE id = :userID", "password", array(":userID" => $userID), 0); |
|
46
|
|
|
return self::pwCheck($plainTextPassword, $storedPw); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private static function pwCheck($plainTextPassword, $storedPassword) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!password_verify($plainTextPassword, $storedPassword)) |
|
|
|
|
|
|
54
|
|
|
return false; |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: