3 * @file mod/lostpass.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\User;
12 use Friendica\Util\DateTimeFormat;
14 require_once 'boot.php';
15 require_once 'include/enotify.php';
16 require_once 'include/text.php';
18 function lostpass_post(App $a)
20 $loginame = notags(trim($_POST['login-name']));
22 goaway(System::baseUrl());
25 $condition = ['(`email` = ? OR `nickname` = ?) AND `verified` = 1 AND `blocked` = 0', $loginame, $loginame];
26 $user = DBA::selectFirst('user', ['uid', 'username', 'email', 'language'], $condition);
27 if (!DBA::isResult($user)) {
28 notice(L10n::t('No valid account found.') . EOL);
29 goaway(System::baseUrl());
32 $pwdreset_token = autoname(12) . mt_rand(1000, 9999);
35 'pwdreset' => $pwdreset_token,
36 'pwdreset_time' => DateTimeFormat::utcNow()
38 $result = DBA::update('user', $fields, ['uid' => $user['uid']]);
40 info(L10n::t('Password reset request issued. Check your email.') . EOL);
43 $sitename = Config::get('config', 'sitename');
44 $resetlink = System::baseUrl() . '/lostpass/' . $pwdreset_token;
46 $preamble = deindent(L10n::t('
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.
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.
55 Your password will not be changed unless we can verify that you
56 issued this request.', $user['username'], $sitename));
57 $body = deindent(L10n::t('
58 Follow this link soon to verify your identity:
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.
65 The login details are as follows:
68 Login Name: %3$s', $resetlink, System::baseUrl(), $user['email']));
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,
81 goaway(System::baseUrl());
84 function lostpass_content(App $a)
88 $pwdreset_token = $a->argv[1];
90 $user = DBA::selectFirst('user', ['uid', 'username', 'email', 'pwdreset_time', 'language'], ['pwdreset' => $pwdreset_token]);
91 if (!DBA::isResult($user)) {
92 notice(L10n::t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed."));
94 return lostpass_form();
97 // Password reset requests expire in 60 minutes
98 if ($user['pwdreset_time'] < DateTimeFormat::utc('now - 1 hour')) {
101 'pwdreset_time' => null
103 DBA::update('user', $fields, ['uid' => $user['uid']]);
105 notice(L10n::t('Request has expired, please make a new one.'));
107 return lostpass_form();
110 return lostpass_generate_password($user);
112 return lostpass_form();
116 function lostpass_form()
118 $tpl = get_markup_template('lostpass.tpl');
119 $o = replace_macros($tpl, [
120 '$title' => L10n::t('Forgot your Password?'),
121 '$desc' => L10n::t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
122 '$name' => L10n::t('Nickname or Email: '),
123 '$submit' => L10n::t('Reset')
129 function lostpass_generate_password($user)
134 $new_password = User::generateNewPassword();
135 $result = User::updatePassword($user['uid'], $new_password);
136 if (DBA::isResult($result)) {
137 $tpl = get_markup_template('pwdreset.tpl');
138 $o .= replace_macros($tpl, [
139 '$lbl1' => L10n::t('Password Reset'),
140 '$lbl2' => L10n::t('Your password has been reset as requested.'),
141 '$lbl3' => L10n::t('Your new password is'),
142 '$lbl4' => L10n::t('Save or copy your new password - and then'),
143 '$lbl5' => '<a href="' . System::baseUrl() . '">' . L10n::t('click here to login') . '</a>.',
144 '$lbl6' => L10n::t('Your password may be changed from the <em>Settings</em> page after successful login.'),
145 '$newpass' => $new_password,
146 '$baseurl' => System::baseUrl()
149 info("Your password has been reset." . EOL);
151 $sitename = Config::get('config', 'sitename');
152 $preamble = deindent(L10n::t('
154 Your password has been changed as requested. Please retain this
155 information for your records ' . "\x28" . 'or change your password immediately to
156 something that you will remember' . "\x29" . '.
157 ', $user['username']));
158 $body = deindent(L10n::t('
159 Your login details are as follows:
165 You may change that password from your account settings page after logging in.
166 ', System::baseUrl(), $user['email'], $new_password));
169 'type' => SYSTEM_EMAIL,
170 'language' => $user['language'],
171 'to_name' => $user['username'],
172 'to_email' => $user['email'],
173 'uid' => $user['uid'],
174 'subject' => L10n::t('Your password has been changed at %s', $sitename),
175 'preamble' => $preamble,