]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/actions/recoverpassword.php
move opening brace of class declaration to next line
[quix0rs-gnu-social.git] / _darcs / pristine / 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
29     function handle($args)
30     {
31         parent::handle($args);
32         if (common_logged_in()) {
33             $this->client_error(_('You are already logged in!'));
34             return;
35         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36             if ($this->arg('recover')) {
37                 $this->recover_password();
38             } else if ($this->arg('reset')) {
39                 $this->reset_password();
40             } else {
41                 $this->client_error(_('Unexpected form submission.'));
42             }
43         } else {
44             if ($this->trimmed('code')) {
45                 $this->check_code();
46             } else {
47                 $this->show_form();
48             }
49         }
50     }
51
52     function check_code()
53     {
54
55         $code = $this->trimmed('code');
56         $confirm = Confirm_address::staticGet('code', $code);
57
58         if (!$confirm) {
59             $this->client_error(_('No such recovery code.'));
60             return;
61         }
62         if ($confirm->address_type != 'recover') {
63             $this->client_error(_('Not a recovery code.'));
64             return;
65         }
66
67         $user = User::staticGet($confirm->user_id);
68
69         if (!$user) {
70             $this->server_error(_('Recovery code for unknown user.'));
71             return;
72         }
73
74         $touched = strtotime($confirm->modified);
75         $email = $confirm->address;
76
77         # Burn this code
78
79         $result = $confirm->delete();
80
81         if (!$result) {
82             common_log_db_error($confirm, 'DELETE', __FILE__);
83             common_server_error(_('Error with confirmation code.'));
84             return;
85         }
86
87         # These should be reaped, but for now we just check mod time
88         # Note: it's still deleted; let's avoid a second attempt!
89
90         if ((time() - $touched) > MAX_RECOVERY_TIME) {
91             common_log(LOG_WARNING, 
92                        'Attempted redemption on recovery code ' .
93                        'that is ' . $touched . ' seconds old. ');
94             $this->client_error(_('This confirmation code is too old. ' .
95                                    'Please start again.'));
96             return;
97         }
98
99         # If we used an outstanding confirmation to send the email,
100         # it's been confirmed at this point.
101
102         if (!$user->email) {
103             $orig = clone($user);
104             $user->email = $email;
105             $result = $user->updateKeys($orig);
106             if (!$result) {
107                 common_log_db_error($user, 'UPDATE', __FILE__);
108                 $this->server_error(_('Could not update user with confirmed email address.'));
109                 return;
110             }
111         }
112
113         # Success!
114
115         $this->set_temp_user($user);
116         $this->show_password_form();
117     }
118
119     function set_temp_user(&$user)
120     {
121         common_ensure_session();
122         $_SESSION['tempuser'] = $user->id;
123     }
124
125     function get_temp_user()
126     {
127         common_ensure_session();
128         $user_id = $_SESSION['tempuser'];
129         if ($user_id) {
130             $user = User::staticGet($user_id);
131         }
132         return $user;
133     }
134
135     function clear_temp_user()
136     {
137         common_ensure_session();
138         unset($_SESSION['tempuser']);
139     }
140
141     function show_top($msg=null)
142     {
143         if ($msg) {
144             common_element('div', 'error', $msg);
145         } else {
146             common_element_start('div', 'instructions');
147             common_element('p', null, 
148                            _('If you\'ve forgotten or lost your' .
149                               ' password, you can get a new one sent to' .
150                               ' the email address you have stored ' .
151                               ' in your account.'));
152             common_element_end('div');
153         }
154     }
155
156     function show_password_top($msg=null)
157     {
158         if ($msg) {
159             common_element('div', 'error', $msg);
160         } else {
161             common_element('div', 'instructions',
162                            _('You\'ve been identified. Enter a ' .
163                               ' new password below. '));
164         }
165     }
166
167     function show_form($msg=null)
168     {
169
170         common_show_header(_('Recover password'), null,
171         $msg, array($this, 'show_top'));
172
173         common_element_start('form', array('method' => 'post',
174                                            'id' => 'recoverpassword',
175                                            'action' => common_local_url('recoverpassword')));
176         common_input('nicknameoremail', _('Nickname or email'),
177                      $this->trimmed('nicknameoremail'),
178                      _('Your nickname on this server, ' .
179                         'or your registered email address.'));
180         common_submit('recover', _('Recover'));
181         common_element_end('form');
182         common_show_footer();
183     }
184
185     function show_password_form($msg=null)
186     {
187
188         common_show_header(_('Reset password'), null,
189         $msg, array($this, 'show_password_top'));
190
191         common_element_start('form', array('method' => 'post',
192                                            'id' => 'recoverpassword',
193                                            'action' => common_local_url('recoverpassword')));
194         common_hidden('token', common_session_token());
195         common_password('newpassword', _('New password'),
196                         _('6 or more characters, and don\'t forget it!'));
197         common_password('confirm', _('Confirm'),
198                         _('Same as password above'));
199         common_submit('reset', _('Reset'));
200         common_element_end('form');
201         common_show_footer();
202     }
203
204     function recover_password()
205     {
206         $nore = $this->trimmed('nicknameoremail');
207         if (!$nore) {
208             $this->show_form(_('Enter a nickname or email address.'));
209             return;
210         }
211
212         $user = User::staticGet('email', common_canonical_email($nore));
213
214         if (!$user) {
215             $user = User::staticGet('nickname', common_canonical_nickname($nore));
216         }
217
218         # See if it's an unconfirmed email address
219
220         if (!$user) {
221             $confirm_email = Confirm_address::staticGet('address', common_canonical_email($nore));
222             if ($confirm_email && $confirm_email->address_type == 'email') {
223                 $user = User::staticGet($confirm_email->user_id);
224             }
225         }
226
227         if (!$user) {
228             $this->show_form(_('No user with that email address or username.'));
229             return;
230         }
231
232         # Try to get an unconfirmed email address if they used a user name
233
234         if (!$user->email && !$confirm_email) {
235             $confirm_email = Confirm_address::staticGet('user_id', $user->id);
236             if ($confirm_email && $confirm_email->address_type != 'email') {
237                 # Skip non-email confirmations
238                 $confirm_email = null;
239             }
240         }
241
242         if (!$user->email && !$confirm_email) {
243             $this->client_error(_('No registered email address for that user.'));
244             return;
245         }
246
247         # Success! We have a valid user and a confirmed or unconfirmed email address
248
249         $confirm = new Confirm_address();
250         $confirm->code = common_confirmation_code(128);
251         $confirm->address_type = 'recover';
252         $confirm->user_id = $user->id;
253         $confirm->address = (isset($user->email)) ? $user->email : $confirm_email->address;
254
255         if (!$confirm->insert()) {
256             common_log_db_error($confirm, 'INSERT', __FILE__);
257             $this->server_error(_('Error saving address confirmation.'));
258             return;
259         }
260
261         $body = "Hey, $user->nickname.";
262         $body .= "\n\n";
263         $body .= 'Someone just asked for a new password ' .
264                  'for this account on ' . common_config('site', 'name') . '.';
265         $body .= "\n\n";
266         $body .= 'If it was you, and you want to confirm, use the URL below:';
267         $body .= "\n\n";
268         $body .= "\t".common_local_url('recoverpassword',
269                                    array('code' => $confirm->code));
270         $body .= "\n\n";
271         $body .= 'If not, just ignore this message.';
272         $body .= "\n\n";
273         $body .= 'Thanks for your time, ';
274         $body .= "\n";
275         $body .= common_config('site', 'name');
276         $body .= "\n";
277
278         mail_to_user($user, _('Password recovery requested'), $body, $confirm->address);
279
280         common_show_header(_('Password recovery requested'));
281         common_element('p', null,
282                        _('Instructions for recovering your password ' .
283                           'have been sent to the email address registered to your ' .
284                           'account.'));
285         common_show_footer();
286     }
287
288     function reset_password()
289     {
290
291         # CSRF protection
292         $token = $this->trimmed('token');
293         if (!$token || $token != common_session_token()) {
294             $this->show_form(_('There was a problem with your session token. Try again, please.'));
295             return;
296         }
297
298         $user = $this->get_temp_user();
299
300         if (!$user) {
301             $this->client_error(_('Unexpected password reset.'));
302             return;
303         }
304
305         $newpassword = $this->trimmed('newpassword');
306         $confirm = $this->trimmed('confirm');
307
308         if (!$newpassword || strlen($newpassword) < 6) {
309             $this->show_password_form(_('Password must be 6 chars or more.'));
310             return;
311         }
312         if ($newpassword != $confirm) {
313             $this->show_password_form(_('Password and confirmation do not match.'));
314             return;
315         }
316
317         # OK, we're ready to go
318
319         $original = clone($user);
320
321         $user->password = common_munge_password($newpassword, $user->id);
322
323         if (!$user->update($original)) {
324             common_log_db_error($user, 'UPDATE', __FILE__);
325             common_server_error(_('Can\'t save new password.'));
326             return;
327         }
328
329         $this->clear_temp_user();
330
331         if (!common_set_user($user->nickname)) {
332             common_server_error(_('Error setting user.'));
333             return;
334         }
335
336         common_real_login(true);
337
338         common_show_header(_('Password saved.'));
339         common_element('p', null, _('New password successfully saved. ' .
340                                      'You are now logged in.'));
341         common_show_footer();
342     }
343 }