]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
Just some more fixed notice
[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\System;
10 use Friendica\Database\dba;
11 use Friendica\Database\DBM;
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                 goaway(System::baseUrl());
24         }
25
26         $condition = ['(`email` = ? OR `nickname` = ?) AND `verified` = 1 AND `blocked` = 0', $loginame, $loginame];
27         $user = dba::selectFirst('user', ['uid', 'username', 'email'], $condition);
28         if (!DBM::is_result($user)) {
29                 notice(L10n::t('No valid account found.') . EOL);
30                 goaway(System::baseUrl());
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                 'to_email' => $user['email'],
74                 'uid'      => $user['uid'],
75                 'subject'  => L10n::t('Password reset requested at %s', $sitename),
76                 'preamble' => $preamble,
77                 'body'     => $body
78         ]);
79
80         goaway(System::baseUrl());
81 }
82
83 function lostpass_content(App $a)
84 {
85         $o = '';
86         if ($a->argc > 1) {
87                 $pwdreset_token = $a->argv[1];
88
89                 $user = dba::selectFirst('user', ['uid', 'username', 'email', 'pwdreset_time'], ['pwdreset' => $pwdreset_token]);
90                 if (!DBM::is_result($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 = get_markup_template('lostpass.tpl');
118         $o = replace_macros($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         $a = get_app();
132
133         $new_password = User::generateNewPassword();
134         $result = User::updatePassword($user['uid'], $new_password);
135         if (DBM::is_result($result)) {
136                 $tpl = get_markup_template('pwdreset.tpl');
137                 $o .= replace_macros($tpl, [
138                         '$lbl1'    => L10n::t('Password Reset'),
139                         '$lbl2'    => L10n::t('Your password has been reset as requested.'),
140                         '$lbl3'    => L10n::t('Your new password is'),
141                         '$lbl4'    => L10n::t('Save or copy your new password - and then'),
142                         '$lbl5'    => '<a href="' . System::baseUrl() . '">' . L10n::t('click here to login') . '</a>.',
143                         '$lbl6'    => L10n::t('Your password may be changed from the <em>Settings</em> page after successful login.'),
144                         '$newpass' => $new_password,
145                         '$baseurl' => System::baseUrl()
146                 ]);
147
148                 info("Your password has been reset." . EOL);
149
150                 $sitename = Config::get('config', 'sitename');
151                 $preamble = deindent(L10n::t('
152                         Dear %1$s,
153                                 Your password has been changed as requested. Please retain this
154                         information for your records ' . "\x28" . 'or change your password immediately to
155                         something that you will remember' . "\x29" . '.
156                 ', $user['username']));
157                 $body = deindent(L10n::t('
158                         Your login details are as follows:
159
160                         Site Location:  %1$s
161                         Login Name:     %2$s
162                         Password:       %3$s
163
164                         You may change that password from your account settings page after logging in.
165                 ', System::baseUrl(), $user['email'], $new_password));
166
167                 notification([
168                         'type'     => SYSTEM_EMAIL,
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 }