UserSettings::rehashPassword()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 18
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package   ElkArte Forum
5
 * @copyright ElkArte Forum contributors
6
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
7
 *
8
 * This file contains code covered by:
9
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
10
 *
11
 * @version 2.0 dev
12
 *
13
 */
14
15
namespace ElkArte;
16
17
use ElkArte\Helper\TokenHash;
18
use ElkArte\Helper\Util;
19
use ElkArte\Helper\ValuesContainerReadOnly;
20
21
/**
22
 * This class holds all the data belonging to a certain member.
23
 */
24
class UserSettings extends ValuesContainerReadOnly
25 2
{
26
	/**
27 2
	 * Sets last_login to the current time
28 2
	 */
29
	public function updateLastLogin()
30
	{
31
		$this->data['last_login'] = time();
32
	}
33
34
	/**
35
	 * Changes the password to the provided one in $this->settings
36
	 * Doesn't actually change the database.
37
	 *
38
	 * @param string $password The hashed password
39
	 */
40
	public function updatePassword($password)
41
	{
42
		$this->data['passwd'] = $password;
43
44
		$tokenizer = new TokenHash();
45
		$this->data['password_salt'] = $tokenizer->generate_hash(UserSettingsLoader::HASH_LENGTH);
46
	}
47
48
	/**
49
	 * Updates total_time_logged_in
50
	 *
51
	 * @param int $increment_offset
52
	 */
53
	public function updateTotalTimeLoggedIn($increment_offset)
54
	{
55
		$this->data['total_time_logged_in'] += time() - $increment_offset;
56
	}
57
58
	/**
59
	 * Fixes the password salt if not present or if it needs to be changed
60
	 *
61
	 * @param bool $force - If true the salt is changed no matter what
62
	 */
63
	public function fixSalt($force = false)
64
	{
65
		// Correct password, but they've got no salt or not enough; fix it!
66
		if ($this->data['password_salt'] === '' || $force || strlen($this->data['password_salt']) < 10)
67
		{
68
			$tokenizer = new TokenHash();
69
70
			$this->data['password_salt'] = $tokenizer->generate_hash(UserSettingsLoader::HASH_LENGTH);
71
72
			return true;
73
		}
74
75
		return false;
76
	}
77
78
	/**
79
	 * Returns the true activation status of an account
80
	 *
81
	 * @param bool $strip_ban
82
	 * @return int
83
	 */
84
	public function getActivationStatus($strip_ban = true)
0 ignored issues
show
Unused Code introduced by
The parameter $strip_ban is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

84
	public function getActivationStatus(/** @scrutinizer ignore-unused */ $strip_ban = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
	{
86
		return (int) ($this->is_activated > UserSettingsLoader::BAN_OFFSET ? $this->is_activated - UserSettingsLoader::BAN_OFFSET : $this->is_activated);
0 ignored issues
show
Bug Best Practice introduced by
The property is_activated does not exist on ElkArte\UserSettings. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
	}
88
89
	/**
90
	 * Repeat the hashing of the password
91
	 *
92
	 * @param string $password The plain text (or sha256 hashed) password
93
	 * @return bool|null Returns false if something fails
94
	 */
95
	public function rehashPassword($password)
96
	{
97
		// If the password is not already 64 characters, lets make it a (SHA-256)
98
		if (strlen($password) !== 64)
99
		{
100
			$password = hash('sha256', Util::strtolower($this->member_name) . un_htmlspecialchars($password));
0 ignored issues
show
Bug Best Practice introduced by
The property member_name does not exist on ElkArte\UserSettings. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
		}
102
103
		$passhash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
104
105
		// Something is not right
106
		if ($passhash === false)
107
		{
108
			// @todo here we should throw an exception
109
			return false;
110
		}
111
112
		$this->updatePassword($passhash);
113
	}
114
115
	/**
116
	 * Checks whether a password meets the current forum rules
117
	 *
118
	 * What it does:
119
	 *
120
	 * - called when registering/choosing a password.
121
	 * - checks the password obeys the current forum settings for password strength.
122
	 * - if password checking is enabled, will check that none of the words in restrict_in appear in the password.
123
	 * - returns an error identifier if the password is invalid.
124
	 *
125
	 * @param string $password
126
	 * @return bool
127
	 */
128
	public function validatePassword($password)
129
	{
130
		// If the password is not 64 characters, lets make it a (SHA-256)
131
		if (strlen($password) !== 64)
132
		{
133
			$password = hash('sha256', Util::strtolower($this->member_name) . un_htmlspecialchars($password));
0 ignored issues
show
Bug Best Practice introduced by
The property member_name does not exist on ElkArte\UserSettings. Since you implemented __get, consider adding a @property annotation.
Loading history...
134
		}
135
136
		return password_verify($password, $this->passwd);
0 ignored issues
show
Bug Best Practice introduced by
The property passwd does not exist on ElkArte\UserSettings. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $this->passwd can also be of type null; however, parameter $hash of password_verify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
		return password_verify($password, /** @scrutinizer ignore-type */ $this->passwd);
Loading history...
137
	}
138
}
139