Password::genPassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 1
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)
0 ignored issues
show
Bug Best Practice introduced by
The expression $userID of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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))
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return password_verify($...word, $storedPassword);.
Loading history...
54
			return false;
55
		return true;
56
	}
57
}