]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
The central item fetch does work now and the API now uses these functions
[friendica.git] / mod / lostpass.php
1 <?php
2 /**
3  * @file mod/lostpass.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\User;
11 use Friendica\Util\DateTimeFormat;
12
13 require_once 'boot.php';
14 require_once 'include/enotify.php';
15 require_once 'include/text.php';
16
17 function lostpass_post(App $a)
18 {
19         $loginame = notags(trim($_POST['login-name']));
20         if (!$loginame) {
21                 goaway(System::baseUrl());
22         }
23
24         $condition = ['(`email` = ? OR `nickname` = ?) AND `verified` = 1 AND `blocked` = 0', $loginame, $loginame];
25         $user = dba::selectFirst('user', ['uid', 'username', 'email'], $condition);
26         if (!DBM::is_result($user)) {
27                 notice(L10n::t('No valid account found.') . EOL);
28                 goaway(System::baseUrl());
29         }
30
31         $pwdreset_token = autoname(12) . mt_rand(1000, 9999);
32
33         $fields = [
34                 'pwdreset' => $pwdreset_token,
35                 'pwdreset_time' => DateTimeFormat::utcNow()
36         ];
37         $result = dba::update('user', $fields, ['uid' => $user['uid']]);
38         if ($result) {
39                 info(L10n::t('Password reset request issued. Check your email.') . EOL);
40         }
41
42         $sitename = $a->config['sitename'];
43         $resetlink = System::baseUrl() . '/lostpass/' . $pwdreset_token;
44
45         $preamble = deindent(L10n::t('
46                 Dear %1$s,
47                         A request was recently received at "%2$s" to reset your account
48                 password. In order to confirm this request, please select the verification link
49                 below or paste it into your web browser address bar.
50
51                 If you did NOT request this change, please DO NOT follow the link
52                 provided and ignore and/or delete this email, the request will expire shortly.
53
54                 Your password will not be changed unless we can verify that you
55                 issued this request.', $user['username'], $sitename));
56         $body = deindent(L10n::t('
57                 Follow this link soon to verify your identity:
58
59                 %1$s
60
61                 You will then receive a follow-up message containing the new password.
62                 You may change that password from your account settings page after logging in.
63
64                 The login details are as follows:
65
66                 Site Location:  %2$s
67                 Login Name:     %3$s', $resetlink, System::baseUrl(), $user['email']));
68
69         notification([
70                 'type'     => SYSTEM_EMAIL,
71                 'to_email' => $user['email'],
72                 'uid'      => $user['uid'],
73                 'subject'  => L10n::t('Password reset requested at %s', $sitename),
74                 'preamble' => $preamble,
75                 'body'     => $body
76         ]);
77
78         goaway(System::baseUrl());
79 }
80
81 function lostpass_content(App $a)
82 {
83         $o = '';
84         if ($a->argc > 1) {
85                 $pwdreset_token = $a->argv[1];
86
87                 $user = dba::selectFirst('user', ['uid', 'username', 'email', 'pwdreset_time'], ['pwdreset' => $pwdreset_token]);
88                 if (!DBM::is_result($user)) {
89                         notice(L10n::t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed."));
90
91                         return lostpass_form();
92                 }
93
94                 // Password reset requests expire in 60 minutes
95                 if ($user['pwdreset_time'] < DateTimeFormat::utc('now - 1 hour')) {
96                         $fields = [
97                                 'pwdreset' => null,
98                                 'pwdreset_time' => null
99                         ];
100                         dba::update('user', $fields, ['uid' => $user['uid']]);
101
102                         notice(L10n::t('Request has expired, please make a new one.'));
103
104                         return lostpass_form();
105                 }
106
107                 return lostpass_generate_password($user);
108         } else {
109                 return lostpass_form();
110         }
111 }
112
113 function lostpass_form()
114 {
115         $tpl = get_markup_template('lostpass.tpl');
116         $o = replace_macros($tpl, [
117                 '$title' => L10n::t('Forgot your Password?'),
118                 '$desc' => L10n::t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
119                 '$name' => L10n::t('Nickname or Email: '),
120                 '$submit' => L10n::t('Reset')
121         ]);
122
123         return $o;
124 }
125
126 function lostpass_generate_password($user)
127 {
128         $o = '';
129         $a = get_app();
130
131         $new_password = User::generateNewPassword();
132         $result = User::updatePassword($user['uid'], $new_password);
133         if (DBM::is_result($result)) {
134                 $tpl = get_markup_template('pwdreset.tpl');
135                 $o .= replace_macros($tpl, [
136                         '$lbl1'    => L10n::t('Password Reset'),
137                         '$lbl2'    => L10n::t('Your password has been reset as requested.'),
138                         '$lbl3'    => L10n::t('Your new password is'),
139                         '$lbl4'    => L10n::t('Save or copy your new password - and then'),
140                         '$lbl5'    => '<a href="' . System::baseUrl() . '">' . L10n::t('click here to login') . '</a>.',
141                         '$lbl6'    => L10n::t('Your password may be changed from the <em>Settings</em> page after successful login.'),
142                         '$newpass' => $new_password,
143                         '$baseurl' => System::baseUrl()
144                 ]);
145
146                 info("Your password has been reset." . EOL);
147
148                 $sitename = $a->config['sitename'];
149                 $preamble = deindent(L10n::t('
150                         Dear %1$s,
151                                 Your password has been changed as requested. Please retain this
152                         information for your records ' . "\x28" . 'or change your password immediately to
153                         something that you will remember' . "\x29" . '.
154                 ', $user['username']));
155                 $body = deindent(L10n::t('
156                         Your login details are as follows:
157
158                         Site Location:  %1$s
159                         Login Name:     %2$s
160                         Password:       %3$s
161
162                         You may change that password from your account settings page after logging in.
163                 ', System::baseUrl(), $user['email'], $new_password));
164
165                 notification([
166                         'type'     => SYSTEM_EMAIL,
167                         'to_email' => $user['email'],
168                         'uid'      => $user['uid'],
169                         'subject'  => L10n::t('Your password has been changed at %s', $sitename),
170                         'preamble' => $preamble,
171                         'body'     => $body
172                 ]);
173         }
174
175         return $o;
176 }