4 * @file mod/lostpass.php
8 use Friendica\Core\Renderer;
9 use Friendica\Database\DBA;
11 use Friendica\Model\User;
12 use Friendica\Util\DateTimeFormat;
13 use Friendica\Util\Strings;
15 function lostpass_post(App $a)
17 $loginame = Strings::escapeTags(trim($_POST['login-name']));
19 DI::baseUrl()->redirect();
22 $condition = ['(`email` = ? OR `nickname` = ?) AND `verified` = 1 AND `blocked` = 0', $loginame, $loginame];
23 $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'email', 'language'], $condition);
24 if (!DBA::isResult($user)) {
25 notice(DI::l10n()->t('No valid account found.') . EOL);
26 DI::baseUrl()->redirect();
29 $pwdreset_token = Strings::getRandomName(12) . random_int(1000, 9999);
32 'pwdreset' => $pwdreset_token,
33 'pwdreset_time' => DateTimeFormat::utcNow()
35 $result = DBA::update('user', $fields, ['uid' => $user['uid']]);
37 info(DI::l10n()->t('Password reset request issued. Check your email.') . EOL);
40 $sitename = DI::config()->get('config', 'sitename');
41 $resetlink = DI::baseUrl() . '/lostpass/' . $pwdreset_token;
43 $preamble = Strings::deindent(DI::l10n()->t('
45 A request was recently received at "%2$s" to reset your account
46 password. In order to confirm this request, please select the verification link
47 below or paste it into your web browser address bar.
49 If you did NOT request this change, please DO NOT follow the link
50 provided and ignore and/or delete this email, the request will expire shortly.
52 Your password will not be changed unless we can verify that you
53 issued this request.', $user['username'], $sitename));
54 $body = Strings::deindent(DI::l10n()->t('
55 Follow this link soon to verify your identity:
59 You will then receive a follow-up message containing the new password.
60 You may change that password from your account settings page after logging in.
62 The login details are as follows:
65 Login Name: %3$s', $resetlink, DI::baseUrl(), $user['nickname']));
67 $email = DI::emailer()
69 ->withMessage(DI::l10n()->t('Password reset requested at %s', $sitename), $preamble, $body)
71 ->withRecipient($user['email'])
74 DI::emailer()->send($email);
75 DI::baseUrl()->redirect();
78 function lostpass_content(App $a)
81 $pwdreset_token = $a->argv[1];
83 $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'email', 'pwdreset_time', 'language'], ['pwdreset' => $pwdreset_token]);
84 if (!DBA::isResult($user)) {
85 notice(DI::l10n()->t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed."));
87 return lostpass_form();
90 // Password reset requests expire in 60 minutes
91 if ($user['pwdreset_time'] < DateTimeFormat::utc('now - 1 hour')) {
94 'pwdreset_time' => null
96 DBA::update('user', $fields, ['uid' => $user['uid']]);
98 notice(DI::l10n()->t('Request has expired, please make a new one.'));
100 return lostpass_form();
103 return lostpass_generate_password($user);
105 return lostpass_form();
109 function lostpass_form()
111 $tpl = Renderer::getMarkupTemplate('lostpass.tpl');
112 $o = Renderer::replaceMacros($tpl, [
113 '$title' => DI::l10n()->t('Forgot your Password?'),
114 '$desc' => DI::l10n()->t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
115 '$name' => DI::l10n()->t('Nickname or Email: '),
116 '$submit' => DI::l10n()->t('Reset')
122 function lostpass_generate_password($user)
126 $new_password = User::generateNewPassword();
127 $result = User::updatePassword($user['uid'], $new_password);
128 if (DBA::isResult($result)) {
129 $tpl = Renderer::getMarkupTemplate('pwdreset.tpl');
130 $o .= Renderer::replaceMacros($tpl, [
131 '$lbl1' => DI::l10n()->t('Password Reset'),
132 '$lbl2' => DI::l10n()->t('Your password has been reset as requested.'),
133 '$lbl3' => DI::l10n()->t('Your new password is'),
134 '$lbl4' => DI::l10n()->t('Save or copy your new password - and then'),
135 '$lbl5' => '<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('click here to login') . '</a>.',
136 '$lbl6' => DI::l10n()->t('Your password may be changed from the <em>Settings</em> page after successful login.'),
137 '$newpass' => $new_password,
140 info("Your password has been reset." . EOL);
142 $sitename = DI::config()->get('config', 'sitename');
143 $preamble = Strings::deindent(DI::l10n()->t('
145 Your password has been changed as requested. Please retain this
146 information for your records ' . "\x28" . 'or change your password immediately to
147 something that you will remember' . "\x29" . '.
148 ', $user['username']));
149 $body = Strings::deindent(DI::l10n()->t('
150 Your login details are as follows:
156 You may change that password from your account settings page after logging in.
157 ', DI::baseUrl(), $user['nickname'], $new_password));
159 $email = DI::emailer()
161 ->withMessage(DI::l10n()->t('Your password has been changed at %s', $sitename), $preamble, $body)
163 ->withRecipient($user['email'])
165 DI::emailer()->send($email);