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) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
return (int) ($this->is_activated > UserSettingsLoader::BAN_OFFSET ? $this->is_activated - UserSettingsLoader::BAN_OFFSET : $this->is_activated); |
|
|
|
|
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)); |
|
|
|
|
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)); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return password_verify($password, $this->passwd); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.