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