]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/recoverpassword.php
Twitter-compatible API - moved show() to the right file
[quix0rs-gnu-social.git] / actions / recoverpassword.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 # You have 24 hours to claim your password
23
24 define(MAX_RECOVERY_TIME, 24 * 60 * 60);
25
26 class RecoverpasswordAction extends Action {
27
28     function handle($args) {
29         parent::handle($args);
30         if (common_logged_in()) {
31                         $this->client_error(_('You are already logged in!'));
32             return;
33         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
34                 if ($this->arg('recover')) {
35                 $this->recover_password();
36             } else if ($this->arg('reset')) {
37                 $this->reset_password();
38                         } else {
39                                 $this->client_error(_('Unexpected form submission.'));
40                         }
41                 } else {
42                         if ($this->trimmed('code')) {
43                         $this->check_code();
44                 } else {
45                         $this->show_form();
46                         }
47                 }
48         }
49
50         function check_code() {
51
52                 $code = $this->trimmed('code');
53                 $confirm = Confirm_address::staticGet($code);
54
55                 if (!$confirm) {
56                         $this->client_error(_('No such recovery code.'));
57                         return;
58                 }
59                 if ($confirm->address_type != 'recover') {
60                         $this->client_error(_('Not a recovery code.'));
61                         return;
62                 }
63
64                 $user = User::staticGet($confirm->user_id);
65
66                 if (!$user) {
67                         $this->server_error(_('Recovery code for unknown user.'));
68                         return;
69                 }
70
71                 $touched = strtotime($confirm->modified);
72                 $email = $confirm->address;
73
74                 # Burn this code
75
76                 $result = $confirm->delete();
77
78                 if (!$result) {
79                         common_log_db_error($confirm, 'DELETE', __FILE__);
80                         common_server_error(_('Error with confirmation code.'));
81                         return;
82                 }
83
84                 # These should be reaped, but for now we just check mod time
85                 # Note: it's still deleted; let's avoid a second attempt!
86
87                 if ((time() - $touched) > MAX_RECOVERY_TIME) {
88                         $this->client_error(_('This confirmation code is too old. ' .
89                                                'Please start again.'));
90                         return;
91                 }
92
93                 # If we used an outstanding confirmation to send the email,
94                 # it's been confirmed at this point.
95
96                 if (!$user->email) {
97                         $orig = clone($user);
98                         $user->email = $email;
99                         $result = $user->updateKeys($orig);
100                         if (!$result) {
101                                 common_log_db_error($user, 'UPDATE', __FILE__);
102                                 $this->server_error(_('Could not update user with confirmed email address.'));
103                                 return;
104                         }
105                 }
106
107                 # Success!
108
109                 $this->set_temp_user($user);
110                 $this->show_password_form();
111         }
112
113         function set_temp_user(&$user) {
114                 common_ensure_session();
115                 $_SESSION['tempuser'] = $user->id;
116         }
117
118         function get_temp_user() {
119                 common_ensure_session();
120                 $user_id = $_SESSION['tempuser'];
121                 if ($user_id) {
122                         $user = User::staticGet($user_id);
123                 }
124                 return $user;
125         }
126
127         function clear_temp_user() {
128                 common_ensure_session();
129                 unset($_SESSION['tempuser']);
130         }
131
132         function show_top($msg=NULL) {
133                 if ($msg) {
134             common_element('div', 'error', $msg);
135                 } else {
136                         common_element('div', 'instructions',
137                                                    _('If you\'ve forgotten or lost your' .
138                                                       ' password, you can get a new one sent to' .
139                                                       ' the email address you have stored ' .
140                                                       ' in your account.'));
141                 }
142         }
143
144         function show_password_top($msg=NULL) {
145                 if ($msg) {
146             common_element('div', 'error', $msg);
147                 } else {
148                         common_element('div', 'instructions',
149                                                    _('You\'ve been identified. Enter a ' .
150                                                       ' new password below. '));
151                 }
152         }
153
154         function show_form($msg=NULL) {
155
156                 common_show_header(_('Recover password'), NULL,
157                 $msg, array($this, 'show_top'));
158
159                 common_element_start('form', array('method' => 'post',
160                                                                                    'id' => 'recoverpassword',
161                                                                                    'action' => common_local_url('recoverpassword')));
162                 common_input('nicknameoremail', _('Nickname or email'),
163                                          $this->trimmed('nicknameoremail'),
164                              _('Your nickname on this server, ' .
165                                 'or your registered email address.'));
166                 common_submit('recover', _('Recover'));
167                 common_element_end('form');
168                 common_show_footer();
169         }
170
171         function show_password_form($msg=NULL) {
172
173                 common_show_header(_('Reset password'), NULL,
174                 $msg, array($this, 'show_password_top'));
175
176                 common_element_start('form', array('method' => 'post',
177                                                                                    'id' => 'recoverpassword',
178                                                                                    'action' => common_local_url('recoverpassword')));
179                 common_password('newpassword', _('New password'),
180                                                 _('6 or more characters, and don\'t forget it!'));
181                 common_password('confirm', _('Confirm'),
182                                                 _('Same as password above'));
183                 common_submit('reset', _('Reset'));
184                 common_element_end('form');
185                 common_show_footer();
186         }
187
188         function recover_password() {
189                 $nore = $this->trimmed('nicknameoremail');
190                 if (!$nore) {
191                         $this->show_form(_('Enter a nickname or email address.'));
192                         return;
193                 }
194
195                 $user = User::staticGet('email', common_canonical_email($nore));
196
197                 if (!$user) {
198                         $user = User::staticGet('nickname', common_canonical_nickname($nore));
199                 }
200
201                 # See if it's an unconfirmed email address
202
203                 if (!$user) {
204                         $confirm_email = Confirm_address::staticGet('address', common_canonical_email($nore));
205                         if ($confirm_email && $confirm_email->address_type == 'email') {
206                                 $user = User::staticGet($confirm_email->user_id);
207                         }
208                 }
209
210                 if (!$user) {
211                         $this->show_form(_('No user with that email address or username.'));
212                         return;
213                 }
214
215                 # Try to get an unconfirmed email address if they used a user name
216
217                 if (!$user->email && !$confirm_email) {
218                         $confirm_email = Confirm_address::staticGet('user_id', $user->id);
219                         if ($confirm_email && $confirm_email->address_type != 'email') {
220                                 # Skip non-email confirmations
221                                 $confirm_email = NULL;
222                         }
223                 }
224
225                 if (!$user->email && !$confirm_email) {
226                         $this->client_error(_('No registered email address for that user.'));
227                         return;
228                 }
229
230                 # Success! We have a valid user and a confirmed or unconfirmed email address
231
232                 $confirm = new Confirm_address();
233                 $confirm->code = common_confirmation_code(128);
234                 $confirm->address_type = 'recover';
235                 $confirm->user_id = $user->id;
236                 $confirm->address = (isset($user->email)) ? $user->email : $confirm_email->address;
237
238                 if (!$confirm->insert()) {
239                         common_log_db_error($confirm, 'INSERT', __FILE__);
240                         $this->server_error(_('Error saving address confirmation.'));
241                         return;
242                 }
243
244                 $body = "Hey, $user->nickname.";
245                 $body .= "\n\n";
246                 $body .= 'Someone just asked for a new password ' .
247                          'for this account on ' . common_config('site', 'name') . '.';
248                 $body .= "\n\n";
249                 $body .= 'If it was you, and you want to confirm, use the URL below:';
250                 $body .= "\n\n";
251                 $body .= "\t".common_local_url('recoverpassword',
252                                                                    array('code' => $confirm->code));
253                 $body .= "\n\n";
254                 $body .= 'If not, just ignore this message.';
255                 $body .= "\n\n";
256                 $body .= 'Thanks for your time, ';
257                 $body .= "\n";
258                 $body .= common_config('site', 'name');
259                 $body .= "\n";
260
261                 mail_to_user($user, _('Password recovery requested'), $body, $confirm->address);
262
263                 common_show_header(_('Password recovery requested'));
264                 common_element('p', NULL,
265                                _('Instructions for recovering your password ' .
266                                   'have been sent to the email address registered to your ' .
267                                   'account.'));
268                 common_show_footer();
269         }
270
271         function reset_password() {
272
273                 $user = $this->get_temp_user();
274
275                 if (!$user) {
276                         $this->client_error(_('Unexpected password reset.'));
277                         return;
278                 }
279
280                 $newpassword = $this->trimmed('newpassword');
281                 $confirm = $this->trimmed('confirm');
282
283                 if (!$newpassword || strlen($newpassword) < 6) {
284                         $this->show_password_form(_('Password must be 6 chars or more.'));
285                         return;
286                 }
287                 if ($newpassword != $confirm) {
288                         $this->show_password_form(_('Password and confirmation do not match.'));
289                         return;
290                 }
291
292                 # OK, we're ready to go
293
294                 $original = clone($user);
295
296                 $user->password = common_munge_password($newpassword, $user->id);
297
298                 if (!$user->update($original)) {
299                         common_log_db_error($user, 'UPDATE', __FILE__);
300                         common_server_error(_('Can\'t save new password.'));
301                         return;
302                 }
303
304                 $this->clear_temp_user();
305
306                 if (!common_set_user($user->nickname)) {
307                         common_server_error(_('Error setting user.'));
308                         return;
309                 }
310
311                 common_real_login(true);
312
313                 common_show_header(_('Password saved.'));
314                 common_element('p', NULL, _('New password successfully saved. ' .
315                                              'You are now logged in.'));
316                 common_show_footer();
317         }
318 }