]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
Refactor deprecated App::internalRedirect() to DI::baseUrl()->redirect()
[friendica.git] / mod / lostpass.php
1 <?php
2
3 /**
4  * @file mod/lostpass.php
5  */
6
7 use Friendica\App;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\DI;
14 use Friendica\Model\User;
15 use Friendica\Util\DateTimeFormat;
16 use Friendica\Util\Strings;
17
18 function lostpass_post(App $a)
19 {
20         $loginame = Strings::escapeTags(trim($_POST['login-name']));
21         if (!$loginame) {
22                 DI::baseUrl()->redirect();
23         }
24
25         $condition = ['(`email` = ? OR `nickname` = ?) AND `verified` = 1 AND `blocked` = 0', $loginame, $loginame];
26         $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'email', 'language'], $condition);
27         if (!DBA::isResult($user)) {
28                 notice(L10n::t('No valid account found.') . EOL);
29                 DI::baseUrl()->redirect();
30         }
31
32         $pwdreset_token = Strings::getRandomName(12) . random_int(1000, 9999);
33
34         $fields = [
35                 'pwdreset' => $pwdreset_token,
36                 'pwdreset_time' => DateTimeFormat::utcNow()
37         ];
38         $result = DBA::update('user', $fields, ['uid' => $user['uid']]);
39         if ($result) {
40                 info(L10n::t('Password reset request issued. Check your email.') . EOL);
41         }
42
43         $sitename = Config::get('config', 'sitename');
44         $resetlink = System::baseUrl() . '/lostpass/' . $pwdreset_token;
45
46         $preamble = Strings::deindent(L10n::t('
47                 Dear %1$s,
48                         A request was recently received at "%2$s" to reset your account
49                 password. In order to confirm this request, please select the verification link
50                 below or paste it into your web browser address bar.
51
52                 If you did NOT request this change, please DO NOT follow the link
53                 provided and ignore and/or delete this email, the request will expire shortly.
54
55                 Your password will not be changed unless we can verify that you
56                 issued this request.', $user['username'], $sitename));
57         $body = Strings::deindent(L10n::t('
58                 Follow this link soon to verify your identity:
59
60                 %1$s
61
62                 You will then receive a follow-up message containing the new password.
63                 You may change that password from your account settings page after logging in.
64
65                 The login details are as follows:
66
67                 Site Location:  %2$s
68                 Login Name:     %3$s', $resetlink, System::baseUrl(), $user['nickname']));
69
70         notification([
71                 'type'     => SYSTEM_EMAIL,
72                 'language' => $user['language'],
73                 'to_name'  => $user['username'],
74                 'to_email' => $user['email'],
75                 'uid'      => $user['uid'],
76                 'subject'  => L10n::t('Password reset requested at %s', $sitename),
77                 'preamble' => $preamble,
78                 'body'     => $body
79         ]);
80
81         DI::baseUrl()->redirect();
82 }
83
84 function lostpass_content(App $a)
85 {
86         if ($a->argc > 1) {
87                 $pwdreset_token = $a->argv[1];
88
89                 $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'email', 'pwdreset_time', 'language'], ['pwdreset' => $pwdreset_token]);
90                 if (!DBA::isResult($user)) {
91                         notice(L10n::t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed."));
92
93                         return lostpass_form();
94                 }
95
96                 // Password reset requests expire in 60 minutes
97                 if ($user['pwdreset_time'] < DateTimeFormat::utc('now - 1 hour')) {
98                         $fields = [
99                                 'pwdreset' => null,
100                                 'pwdreset_time' => null
101                         ];
102                         DBA::update('user', $fields, ['uid' => $user['uid']]);
103
104                         notice(L10n::t('Request has expired, please make a new one.'));
105
106                         return lostpass_form();
107                 }
108
109                 return lostpass_generate_password($user);
110         } else {
111                 return lostpass_form();
112         }
113 }
114
115 function lostpass_form()
116 {
117         $tpl = Renderer::getMarkupTemplate('lostpass.tpl');
118         $o = Renderer::replaceMacros($tpl, [
119                 '$title' => L10n::t('Forgot your Password?'),
120                 '$desc' => L10n::t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
121                 '$name' => L10n::t('Nickname or Email: '),
122                 '$submit' => L10n::t('Reset')
123         ]);
124
125         return $o;
126 }
127
128 function lostpass_generate_password($user)
129 {
130         $o = '';
131
132         $new_password = User::generateNewPassword();
133         $result = User::updatePassword($user['uid'], $new_password);
134         if (DBA::isResult($result)) {
135                 $tpl = Renderer::getMarkupTemplate('pwdreset.tpl');
136                 $o .= Renderer::replaceMacros($tpl, [
137                         '$lbl1'    => L10n::t('Password Reset'),
138                         '$lbl2'    => L10n::t('Your password has been reset as requested.'),
139                         '$lbl3'    => L10n::t('Your new password is'),
140                         '$lbl4'    => L10n::t('Save or copy your new password - and then'),
141                         '$lbl5'    => '<a href="' . System::baseUrl() . '">' . L10n::t('click here to login') . '</a>.',
142                         '$lbl6'    => L10n::t('Your password may be changed from the <em>Settings</em> page after successful login.'),
143                         '$newpass' => $new_password,
144                 ]);
145
146                 info("Your password has been reset." . EOL);
147
148                 $sitename = Config::get('config', 'sitename');
149                 $preamble = Strings::deindent(L10n::t('
150                         Dear %1$s,
151                                 Your password has been changed as requested. Please retain this
152                         information for your records ' . "\x28" . 'or change your password immediately to
153                         something that you will remember' . "\x29" . '.
154                 ', $user['username']));
155                 $body = Strings::deindent(L10n::t('
156                         Your login details are as follows:
157
158                         Site Location:  %1$s
159                         Login Name:     %2$s
160                         Password:       %3$s
161
162                         You may change that password from your account settings page after logging in.
163                 ', System::baseUrl(), $user['nickname'], $new_password));
164
165                 notification([
166                         'type'     => SYSTEM_EMAIL,
167                         'language' => $user['language'],
168                         'to_name'  => $user['username'],
169                         'to_email' => $user['email'],
170                         'uid'      => $user['uid'],
171                         'subject'  => L10n::t('Your password has been changed at %s', $sitename),
172                         'preamble' => $preamble,
173                         'body'     => $body
174                 ]);
175         }
176
177         return $o;
178 }