forked from projectsend/projectsend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset-password.php
280 lines (243 loc) · 11.4 KB
/
reset-password.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Show the form to reset the password.
*
* @package ProjectSend
*
*/
$allowed_levels = array(9,8,7,0);
require_once 'bootstrap.php';
$page_title = __('Lost password','cftp_admin');
$page_id = 'reset_password_enter_email';
if (!empty($_GET['token']) && !empty($_GET['user'])) {
$page_id = 'reset_password_enter_new';
}
include_once ADMIN_VIEWS_DIR . DS . 'header-unlogged.php';
$show_form = 'enter_email';
if (!empty($_GET['token']) && !empty($_GET['user'])) {
$got_token = $_GET['token'];
$got_user = $_GET['user'];
/**
* Get the user's id
*/
$user_data = get_user_by_username($got_user);
$sql_request = $dbh->prepare("SELECT * FROM " . TABLE_PASSWORD_RESET . " WHERE BINARY token = :token AND user_id = :id");
$sql_request->bindParam(':token', $got_token);
$sql_request->bindParam(':id', $user_data['id'], PDO::PARAM_INT);
$sql_request->execute();
$count_request = $sql_request->rowCount();
if ($count_request > 0) {
$sql_request->setFetchMode(PDO::FETCH_ASSOC);
$token_info = $sql_request->fetch();
/** Check if the token has been used already */
if ($token_info['used'] == '1') {
$errorstate = 'token_used';
}
/** Check if the token has expired. */
elseif (time() - strtotime($token_info['timestamp']) > PASSWORD_RECOVERY_TOKEN_EXPIRATION_TIME) {
$errorstate = 'token_expired';
}
else {
$show_form = 'enter_new_password';
}
}
else {
$errorstate = 'token_invalid';
$show_form = 'none';
}
}
/** Fix CVE-2020-28874 */
if (!empty($errorstate)) {
unset($user_data);
}
/** The form was submitted */
if ($_POST) {
/**
* Clean the posted form values.
*/
$form_type = encode_html($_POST['form_type']);
switch ($form_type) {
/**
* The form submitted contains a new token request
*/
case 'new_request':
recaptcha2ValidateRequest();
$get_user = get_user_by('user', 'email', $_POST['email']);
if ( $get_user ) {
/** Email exists on the database */
$token = generateRandomString(32);
/**
* Count how many request were made by this user today.
* No more than 3 unused should exist at a time.
*/
$sql_amount = $dbh->prepare("SELECT * FROM " . TABLE_PASSWORD_RESET . " WHERE user_id = :id AND used = '0' AND timestamp > NOW() - INTERVAL 1 DAY");
$sql_amount->bindParam(':id', $get_user['id'], PDO::PARAM_INT);
$sql_amount->execute();
$count_requests = $sql_amount->rowCount();
if ($count_requests >= 3){
$errorstate = 'too_many_today';
}
else {
$sql_pass = $dbh->prepare("INSERT INTO " . TABLE_PASSWORD_RESET . " (user_id, token)"
."VALUES (:id, :token)");
$sql_pass->bindParam(':token', $token);
$sql_pass->bindParam(':id', $get_user['id'], PDO::PARAM_INT);
$sql_pass->execute();
/** Send email */
$notify_user = new \ProjectSend\Classes\Emails;
if ($notify_user->send([
'type' => 'password_reset',
'address' => $get_user['email'],
'username' => $get_user['username'],
'token' => $token
])) {
$state['email'] = 1;
}
else {
$state['email'] = 0;
}
}
$show_form = 'none';
}
else {
//$errorstate = 'email_not_found';
// Simulate that the request has been set, do not show that email exists or not on the database
$state['email'] = 1;
$show_form = 'none';
}
break;
/**
* The form submitted contains the new password
*/
case 'new_password':
if (!empty($user_data['id'])) {
$reset_password_new = $_POST['password'];
/** Password checks */
$validation = new \ProjectSend\Classes\Validation;
$validation->validate('completed',$reset_password_new,$json_strings['validation']['no_pass']);
$validation->validate('password',$reset_password_new,$json_strings['validation']['valid_pass'].' '.$json_strings['validation']['valid_chars']);
$validation->validate('pass_rules',$reset_password_new,$json_strings['validation']['rules_pass']);
$validation->validate('length',$reset_password_new,$json_strings['validation']['length_pass'],MIN_PASS_CHARS,MAX_PASS_CHARS);
if ($validation->passed()) {
$enc_password = password_hash($reset_password_new, PASSWORD_DEFAULT, [ 'cost' => HASH_COST_LOG2 ]);
if (strlen($enc_password) >= 20) {
$state['hash'] = 1;
/** SQL queries */
$sql_query = $dbh->prepare("UPDATE " . TABLE_USERS . " SET
password = :password
WHERE id = :id"
);
$sql_query->bindParam(':password', $enc_password);
$sql_query->bindParam(':id', $user_data['id'], PDO::PARAM_INT);
$sql_query->execute();
if ($sql_query) {
$state['reset'] = 1;
$sql_query = $dbh->prepare("UPDATE " . TABLE_PASSWORD_RESET . " SET
used = '1'
WHERE id = :id"
);
$sql_query->bindParam(':id', $token_info['id'], PDO::PARAM_INT);
$sql_query->execute();
$show_form = 'none';
}
else {
$state['reset'] = 0;
}
}
else {
$state['hash'] = 0;
}
}
}
break;
}
}
?>
<div class="col-xs-12 col-sm-12 col-lg-4 col-lg-offset-4">
<?php echo get_branding_layout(true); ?>
<div class="white-box">
<div class="white-box-interior">
<?php
/**
* If the form was submitted with errors, show them here.
*/
if (!empty($validation)) {
echo $validation->list_errors();
}
?>
<?php
/**
* Show status message
*/
if (isset($errorstate)) {
switch ($errorstate) {
case 'email_not_found':
$login_err_message = __("The supplied email address does not correspond to any user.",'cftp_admin');
break;
case 'token_invalid':
$login_err_message = __("The request is not valid.",'cftp_admin');
break;
case 'token_expired':
$login_err_message = __("This request has expired. Please make a new one.",'cftp_admin');
break;
case 'token_used':
$login_err_message = __("This request has already been completed. Please make a new one.",'cftp_admin');
break;
case 'too_many_today':
$login_err_message = __("There are 3 unused requests done in less than 24 hs. Please wait until one expires (1 day since made) to make a new one.",'cftp_admin');
break;
}
echo system_message('danger',$login_err_message,'login_error');
}
/**
* Show the ok or error message for the email.
*/
if (isset($state['email'])) {
switch ($state['email']) {
case 1:
$msg = __('An e-mail with further instructions has been sent. Please check your inbox to proceed.','cftp_admin');
echo system_message('success',$msg);
break;
case 0:
$msg = __("E-mail couldn't be sent.",'cftp_admin');
$msg .= ' ' . __("If the problem persists, please contact an administrator.",'cftp_admin');
echo system_message('danger',$msg);
break;
}
}
/**
* Show the ok or error message for the password reset.
*/
if (isset($state['reset'])) {
switch ($state['reset']) {
case 1:
$msg = __('Your new password has been set. You can now log in using it.','cftp_admin');
echo system_message('success',$msg);
break;
case 0:
$msg = __("Your new password couldn't be set.",'cftp_admin');
$msg .= ' ' . __("If the problem persists, please contact an administrator.",'cftp_admin');
echo system_message('danger',$msg);
break;
}
}
switch ($show_form) {
case 'enter_email':
default:
include_once FORMS_DIR . DS . 'reset-password' . DS . 'enter-email.php';
break;
case 'enter_new_password':
include_once FORMS_DIR . DS . 'reset-password' . DS . 'enter-password.php';
break;
case 'none':
break;
}
?>
<div class="login_form_links">
<p><a href="<?php echo BASE_URI; ?>" target="_self"><?php _e('Go back to the homepage.','cftp_admin'); ?></a></p>
</div>
</div>
</div> <!-- container-custom -->
</div>
<?php
include_once ADMIN_VIEWS_DIR . DS . 'footer.php';