]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
Centralize password hashing in Model\User
[friendica.git] / mod / lostpass.php
1 <?php
2
3 /**
4  * @file mod/lostpass.php
5  */
6
7 use Friendica\App;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\User;
11
12 require_once 'include/boot.php';
13 require_once 'include/enotify.php';
14 require_once 'include/text.php';
15 require_once 'include/pgettext.php';
16
17 function lostpass_post(App $a)
18 {
19         $loginame = notags(trim($_POST['login-name']));
20         if (!$loginame) {
21                 goaway(System::baseUrl());
22         }
23
24         $condition = ['(`email` = ? OR `nickname` = ?) AND `verified` = 1 AND `blocked` = 0', $loginame, $loginame];
25         $user = dba::selectFirst('user', ['uid', 'username', 'email'], $condition);
26         if (!DBM::is_result($user)) {
27                 notice(t('No valid account found.') . EOL);
28                 goaway(System::baseUrl());
29         }
30
31         $pwdreset_token = autoname(12) . mt_rand(1000, 9999);
32
33         $result = dba::update('user', ['pwdreset' => $pwdreset_token], ['uid' => $user['uid']]);
34         if ($result) {
35                 info(t('Password reset request issued. Check your email.') . EOL);
36         }
37
38         $sitename = $a->config['sitename'];
39         $resetlink = System::baseUrl() . '/lostpass?verify=' . $pwdreset_token;
40
41         $preamble = deindent(t('
42                 Dear %1$s,
43                         A request was recently received at "%2$s" to reset your account
44                 password. In order to confirm this request, please select the verification link
45                 below or paste it into your web browser address bar.
46
47                 If you did NOT request this change, please DO NOT follow the link
48                 provided and ignore and/or delete this email.
49
50                 Your password will not be changed unless we can verify that you
51                 issued this request.', $user['username'], $sitename));
52         $body = deindent(t('
53                 Follow this link to verify your identity:
54
55                 %1$s
56
57                 You will then receive a follow-up message containing the new password.
58                 You may change that password from your account settings page after logging in.
59
60                 The login details are as follows:
61
62                 Site Location:  %2$s
63                 Login Name:     %3$s', $resetlink, System::baseUrl(), $user['email']));
64
65         notification([
66                 'type'     => SYSTEM_EMAIL,
67                 'to_email' => $user['email'],
68                 'subject'  => t('Password reset requested at %s', $sitename),
69                 'preamble' => $preamble,
70                 'body'     => $body
71         ]);
72
73         goaway(System::baseUrl());
74 }
75
76 function lostpass_content(App $a)
77 {
78         $o = '';
79         if (x($_GET, 'verify')) {
80                 $pwdreset_token = $_GET['verify'];
81
82                 $user = dba::selectFirst('user', ['uid', 'username', 'email'], ['pwdreset' => $pwdreset_token]);
83                 if (!DBM::is_result($user)) {
84                         $o = t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed.");
85                         return $o;
86                 }
87
88                 $new_password = User::generateNewPassword();
89                 $result = User::updatePassword($user['uid'], $new_password);
90                 if (DBM::is_result($result)) {
91                         $tpl = get_markup_template('pwdreset.tpl');
92                         $o .= replace_macros($tpl,
93                                 [
94                                 '$lbl1'    => t('Password Reset'),
95                                 '$lbl2'    => t('Your password has been reset as requested.'),
96                                 '$lbl3'    => t('Your new password is'),
97                                 '$lbl4'    => t('Save or copy your new password - and then'),
98                                 '$lbl5'    => '<a href="' . System::baseUrl() . '">' . t('click here to login') . '</a>.',
99                                 '$lbl6'    => t('Your password may be changed from the <em>Settings</em> page after successful login.'),
100                                 '$newpass' => $new_password,
101                                 '$baseurl' => System::baseUrl()
102                         ]);
103
104                         info("Your password has been reset." . EOL);
105
106                         $sitename = $a->config['sitename'];
107                         $preamble = deindent(t('
108                                 Dear %1$s,
109                                         Your password has been changed as requested. Please retain this
110                                 information for your records (or change your password immediately to
111                                 something that you will remember).
112                         ', $user['username']));
113                         $body = deindent(t('
114                                 Your login details are as follows:
115
116                                 Site Location:  %1$s
117                                 Login Name:     %2$s
118                                 Password:       %3$s
119
120                                 You may change that password from your account settings page after logging in.
121                         ', System::baseUrl(), $user['email'], $new_password));
122
123                         notification([
124                                 'type'     => SYSTEM_EMAIL,
125                                 'to_email' => $user['email'],
126                                 'subject'  => t('Your password has been changed at %s', $sitename),
127                                 'preamble' => $preamble,
128                                 'body'     => $body
129                         ]);
130
131                         return $o;
132                 }
133         } else {
134                 $tpl = get_markup_template('lostpass.tpl');
135                 $o .= replace_macros($tpl, [
136                         '$title'  => t('Forgot your Password?'),
137                         '$desc'   => t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
138                         '$name'   => t('Nickname or Email: '),
139                         '$submit' => t('Reset')
140                 ]);
141
142                 return $o;
143         }
144 }