]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
Merge remote-tracking branch 'upstream/develop' into diasppora-delivery
[friendica.git] / mod / lostpass.php
1 <?php
2 /**
3  * @file mod/lostpass.php
4  */
5
6 use Friendica\App;
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;
14
15 require_once 'boot.php';
16 require_once 'include/enotify.php';
17 require_once 'include/text.php';
18
19 function lostpass_post(App $a)
20 {
21         $loginame = notags(trim($_POST['login-name']));
22         if (!$loginame) {
23                 $a->internalRedirect();
24         }
25
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();
31         }
32
33         $pwdreset_token = autoname(12) . mt_rand(1000, 9999);
34
35         $fields = [
36                 'pwdreset' => $pwdreset_token,
37                 'pwdreset_time' => DateTimeFormat::utcNow()
38         ];
39         $result = DBA::update('user', $fields, ['uid' => $user['uid']]);
40         if ($result) {
41                 info(L10n::t('Password reset request issued. Check your email.') . EOL);
42         }
43
44         $sitename = Config::get('config', 'sitename');
45         $resetlink = System::baseUrl() . '/lostpass/' . $pwdreset_token;
46
47         $preamble = deindent(L10n::t('
48                 Dear %1$s,
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.
52
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.
55
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:
60
61                 %1$s
62
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.
65
66                 The login details are as follows:
67
68                 Site Location:  %2$s
69                 Login Name:     %3$s', $resetlink, System::baseUrl(), $user['email']));
70
71         notification([
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,
79                 'body'     => $body
80         ]);
81
82         $a->internalRedirect();
83 }
84
85 function lostpass_content(App $a)
86 {
87         $o = '';
88         if ($a->argc > 1) {
89                 $pwdreset_token = $a->argv[1];
90
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."));
94
95                         return lostpass_form();
96                 }
97
98                 // Password reset requests expire in 60 minutes
99                 if ($user['pwdreset_time'] < DateTimeFormat::utc('now - 1 hour')) {
100                         $fields = [
101                                 'pwdreset' => null,
102                                 'pwdreset_time' => null
103                         ];
104                         DBA::update('user', $fields, ['uid' => $user['uid']]);
105
106                         notice(L10n::t('Request has expired, please make a new one.'));
107
108                         return lostpass_form();
109                 }
110
111                 return lostpass_generate_password($user);
112         } else {
113                 return lostpass_form();
114         }
115 }
116
117 function lostpass_form()
118 {
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')
125         ]);
126
127         return $o;
128 }
129
130 function lostpass_generate_password($user)
131 {
132         $o = '';
133         $a = get_app();
134
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()
148                 ]);
149
150                 info("Your password has been reset." . EOL);
151
152                 $sitename = Config::get('config', 'sitename');
153                 $preamble = deindent(L10n::t('
154                         Dear %1$s,
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:
161
162                         Site Location:  %1$s
163                         Login Name:     %2$s
164                         Password:       %3$s
165
166                         You may change that password from your account settings page after logging in.
167                 ', System::baseUrl(), $user['email'], $new_password));
168
169                 notification([
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,
177                         'body'     => $body
178                 ]);
179         }
180
181         return $o;
182 }