3 * @file mod/lostpass.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Model\User;
13 use Friendica\Util\DateTimeFormat;
15 require_once 'boot.php';
16 require_once 'include/enotify.php';
17 require_once 'include/text.php';
19 function lostpass_post(App $a)
21 $loginame = notags(trim($_POST['login-name']));
23 $a->internalRedirect();
26 $condition = ['(`email` = ? OR `nickname` = ?) AND `verified` = 1 AND `blocked` = 0', $loginame, $loginame];
27 $user = DBA::selectFirst('user', ['uid', 'username', 'email', 'language'], $condition);
28 if (!DBA::isResult($user)) {
29 notice(L10n::t('No valid account found.') . EOL);
30 $a->internalRedirect();
33 $pwdreset_token = autoname(12) . mt_rand(1000, 9999);
36 'pwdreset' => $pwdreset_token,
37 'pwdreset_time' => DateTimeFormat::utcNow()
39 $result = DBA::update('user', $fields, ['uid' => $user['uid']]);
41 info(L10n::t('Password reset request issued. Check your email.') . EOL);
44 $sitename = Config::get('config', 'sitename');
45 $resetlink = System::baseUrl() . '/lostpass/' . $pwdreset_token;
47 $preamble = deindent(L10n::t('
49 A request was recently received at "%2$s" to reset your account
50 password. In order to confirm this request, please select the verification link
51 below or paste it into your web browser address bar.
53 If you did NOT request this change, please DO NOT follow the link
54 provided and ignore and/or delete this email, the request will expire shortly.
56 Your password will not be changed unless we can verify that you
57 issued this request.', $user['username'], $sitename));
58 $body = deindent(L10n::t('
59 Follow this link soon to verify your identity:
63 You will then receive a follow-up message containing the new password.
64 You may change that password from your account settings page after logging in.
66 The login details are as follows:
69 Login Name: %3$s', $resetlink, System::baseUrl(), $user['email']));
72 'type' => SYSTEM_EMAIL,
73 'language' => $user['language'],
74 'to_name' => $user['username'],
75 'to_email' => $user['email'],
76 'uid' => $user['uid'],
77 'subject' => L10n::t('Password reset requested at %s', $sitename),
78 'preamble' => $preamble,
82 $a->internalRedirect();
85 function lostpass_content(App $a)
89 $pwdreset_token = $a->argv[1];
91 $user = DBA::selectFirst('user', ['uid', 'username', 'email', 'pwdreset_time', 'language'], ['pwdreset' => $pwdreset_token]);
92 if (!DBA::isResult($user)) {
93 notice(L10n::t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed."));
95 return lostpass_form();
98 // Password reset requests expire in 60 minutes
99 if ($user['pwdreset_time'] < DateTimeFormat::utc('now - 1 hour')) {
102 'pwdreset_time' => null
104 DBA::update('user', $fields, ['uid' => $user['uid']]);
106 notice(L10n::t('Request has expired, please make a new one.'));
108 return lostpass_form();
111 return lostpass_generate_password($user);
113 return lostpass_form();
117 function lostpass_form()
119 $tpl = Renderer::getMarkupTemplate('lostpass.tpl');
120 $o = Renderer::replaceMacros($tpl, [
121 '$title' => L10n::t('Forgot your Password?'),
122 '$desc' => L10n::t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
123 '$name' => L10n::t('Nickname or Email: '),
124 '$submit' => L10n::t('Reset')
130 function lostpass_generate_password($user)
135 $new_password = User::generateNewPassword();
136 $result = User::updatePassword($user['uid'], $new_password);
137 if (DBA::isResult($result)) {
138 $tpl = Renderer::getMarkupTemplate('pwdreset.tpl');
139 $o .= Renderer::replaceMacros($tpl, [
140 '$lbl1' => L10n::t('Password Reset'),
141 '$lbl2' => L10n::t('Your password has been reset as requested.'),
142 '$lbl3' => L10n::t('Your new password is'),
143 '$lbl4' => L10n::t('Save or copy your new password - and then'),
144 '$lbl5' => '<a href="' . System::baseUrl() . '">' . L10n::t('click here to login') . '</a>.',
145 '$lbl6' => L10n::t('Your password may be changed from the <em>Settings</em> page after successful login.'),
146 '$newpass' => $new_password,
147 '$baseurl' => System::baseUrl()
150 info("Your password has been reset." . EOL);
152 $sitename = Config::get('config', 'sitename');
153 $preamble = deindent(L10n::t('
155 Your password has been changed as requested. Please retain this
156 information for your records ' . "\x28" . 'or change your password immediately to
157 something that you will remember' . "\x29" . '.
158 ', $user['username']));
159 $body = deindent(L10n::t('
160 Your login details are as follows:
166 You may change that password from your account settings page after logging in.
167 ', System::baseUrl(), $user['email'], $new_password));
170 'type' => SYSTEM_EMAIL,
171 'language' => $user['language'],
172 'to_name' => $user['username'],
173 'to_email' => $user['email'],
174 'uid' => $user['uid'],
175 'subject' => L10n::t('Your password has been changed at %s', $sitename),
176 'preamble' => $preamble,