3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 # You have 24 hours to claim your password
24 define('MAX_RECOVERY_TIME', 24 * 60 * 60);
26 class RecoverpasswordAction extends Action
32 function handle($args)
34 parent::handle($args);
35 if (common_logged_in()) {
36 // TRANS: Client error displayed trying to recover password while already logged in.
37 $this->clientError(_('You are already logged in!'));
39 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
40 if ($this->arg('recover')) {
41 $this->recoverPassword();
42 } else if ($this->arg('reset')) {
43 $this->resetPassword();
45 // TRANS: Client error displayed when unexpected data is posted in the password recovery form.
46 $this->clientError(_('Unexpected form submission.'));
49 if ($this->trimmed('code')) {
59 $code = $this->trimmed('code');
60 $confirm = Confirm_address::staticGet('code', $code);
63 // TRANS: Client error displayed when password recovery code is not correct.
64 $this->clientError(_('No such recovery code.'));
67 if ($confirm->address_type != 'recover') {
68 // TRANS: Client error displayed when no proper password recovery code was submitted.
69 $this->clientError(_('Not a recovery code.'));
73 $user = User::staticGet($confirm->user_id);
76 // TRANS: Server error displayed trying to recover password without providing a user.
77 $this->serverError(_('Recovery code for unknown user.'));
81 $touched = strtotime($confirm->modified);
82 $email = $confirm->address;
86 $result = $confirm->delete();
89 common_log_db_error($confirm, 'DELETE', __FILE__);
90 // TRANS: Server error displayed removing a password recovery code from the database.
91 $this->serverError(_('Error with confirmation code.'));
95 # These should be reaped, but for now we just check mod time
96 # Note: it's still deleted; let's avoid a second attempt!
98 if ((time() - $touched) > MAX_RECOVERY_TIME) {
99 common_log(LOG_WARNING,
100 'Attempted redemption on recovery code ' .
101 'that is ' . $touched . ' seconds old. ');
102 // TRANS: Client error displayed trying to recover password with too old a recovery code.
103 $this->clientError(_('This confirmation code is too old. ' .
104 'Please start again.'));
108 # If we used an outstanding confirmation to send the email,
109 # it's been confirmed at this point.
112 $orig = clone($user);
113 $user->email = $email;
114 $result = $user->updateKeys($orig);
116 common_log_db_error($user, 'UPDATE', __FILE__);
117 // TRANS: Server error displayed when updating a user's e-mail address in the database fails while recovering a password.
118 $this->serverError(_('Could not update user with confirmed email address.'));
125 $this->setTempUser($user);
126 $this->showPasswordForm();
129 function setTempUser(&$user)
131 common_ensure_session();
132 $_SESSION['tempuser'] = $user->id;
135 function getTempUser()
137 common_ensure_session();
138 $user_id = $_SESSION['tempuser'];
140 $user = User::staticGet($user_id);
145 function clearTempUser()
147 common_ensure_session();
148 unset($_SESSION['tempuser']);
151 function showPageNotice()
154 $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
156 $this->elementStart('div', 'instructions');
157 if ($this->mode == 'recover') {
158 $this->element('p', null,
159 // TRANS: Page notice for password recovery page.
160 _('If you have forgotten or lost your' .
161 ' password, you can get a new one sent to' .
162 ' the email address you have stored' .
163 ' in your account.'));
164 } else if ($this->mode == 'reset') {
165 // TRANS: Page notice for password change page.
166 $this->element('p', null,
167 _('You have been identified. Enter a' .
168 ' new password below.'));
170 $this->elementEnd('div');
174 function showForm($msg=null)
177 $this->mode = 'recover';
181 function showContent()
183 if ($this->mode == 'recover') {
184 $this->showRecoverForm();
185 } else if ($this->mode == 'reset') {
186 $this->showResetForm();
190 function showRecoverForm()
192 $this->elementStart('form', array('method' => 'post',
193 'id' => 'form_password_recover',
194 'class' => 'form_settings',
195 'action' => common_local_url('recoverpassword')));
196 $this->elementStart('fieldset');
197 // TRANS: Fieldset legend for password recovery page.
198 $this->element('legend', null, _('Password recovery'));
199 $this->elementStart('ul', 'form_data');
200 $this->elementStart('li');
201 // TRANS: Field label on password recovery page.
202 $this->input('nicknameoremail', _('Nickname or email address'),
203 $this->trimmed('nicknameoremail'),
204 // TRANS: Title for field label on password recovery page.
205 _('Your nickname on this server, ' .
206 'or your registered email address.'));
207 $this->elementEnd('li');
208 $this->elementEnd('ul');
209 $this->element('input', array('name' => 'recover',
211 // TRANS: Field label on password recovery page.
212 'value' => _('Recover')));
213 // TRANS: Button text on password recovery page.
214 $this->submit('recover', _m('BUTTON','Recover'));
215 $this->elementEnd('fieldset');
216 $this->elementEnd('form');
221 switch ($this->mode) {
222 // TRANS: Title for password recovery page in password reset mode.
223 case 'reset': return _('Reset password');
224 // TRANS: Title for password recovery page in password recover mode.
225 case 'recover': return _('Recover password');
226 // TRANS: Title for password recovery page in email sent mode.
227 case 'sent': return _('Password recovery requested');
228 // TRANS: Title for password recovery page in password saved mode.
229 case 'saved': return _('Password saved');
231 // TRANS: Title for password recovery page when an unknown action has been specified.
232 return _('Unknown action');
236 function showPasswordForm($msg=null)
239 $this->mode = 'reset';
243 function showResetForm()
245 $this->elementStart('form', array('method' => 'post',
246 'id' => 'form_password_change',
247 'class' => 'form_settings',
248 'action' => common_local_url('recoverpassword')));
249 $this->elementStart('fieldset');
250 // TRANS: Fieldset legend for password reset form.
251 $this->element('legend', null, _('Password change'));
252 $this->hidden('token', common_session_token());
253 $this->elementStart('ul', 'form_data');
254 $this->elementStart('li');
255 // TRANS: Field label for password reset form.
256 $this->password('newpassword', _('New password'),
257 // TRANS: Title for field label for password reset form.
258 _('6 or more characters, and do not forget it!'));
259 $this->elementEnd('li');
260 $this->elementStart('li');
261 // TRANS: Field label for password reset form where the password has to be typed again.
262 $this->password('confirm', _('Confirm'),
263 // TRANS: Ttile for field label for password reset form where the password has to be typed again.
264 _('Same as password above.'));
265 $this->elementEnd('li');
266 $this->elementEnd('ul');
267 // TRANS: Button text for password reset form.
268 $this->submit('reset', _m('BUTTON','Reset'));
269 $this->elementEnd('fieldset');
270 $this->elementEnd('form');
273 function recoverPassword()
275 $nore = $this->trimmed('nicknameoremail');
277 // TRANS: Form instructions for password recovery form.
278 $this->showForm(_('Enter a nickname or email address.'));
282 $user = User::staticGet('email', common_canonical_email($nore));
285 $user = User::staticGet('nickname', common_canonical_nickname($nore));
288 # See if it's an unconfirmed email address
291 // Warning: it may actually be legit to have multiple folks
292 // who have claimed, but not yet confirmed, the same address.
293 // We'll only send to the first one that comes up.
294 $confirm_email = new Confirm_address();
295 $confirm_email->address = common_canonical_email($nore);
296 $confirm_email->address_type = 'email';
297 $confirm_email->find();
298 if ($confirm_email->fetch()) {
299 $user = User::staticGet($confirm_email->user_id);
301 $confirm_email = null;
304 $confirm_email = null;
308 // TRANS: Information on password recovery form if no known username or e-mail address was specified.
309 $this->showForm(_('No user with that email address or username.'));
313 # Try to get an unconfirmed email address if they used a user name
315 if (!$user->email && !$confirm_email) {
316 $confirm_email = new Confirm_address();
317 $confirm_email->user_id = $user->id;
318 $confirm_email->address_type = 'email';
319 $confirm_email->find();
320 if (!$confirm_email->fetch()) {
321 $confirm_email = null;
325 if (!$user->email && !$confirm_email) {
326 // TRANS: Client error displayed on password recovery form if a user does not have a registered e-mail address.
327 $this->clientError(_('No registered email address for that user.'));
331 # Success! We have a valid user and a confirmed or unconfirmed email address
333 $confirm = new Confirm_address();
334 $confirm->code = common_confirmation_code(128);
335 $confirm->address_type = 'recover';
336 $confirm->user_id = $user->id;
337 $confirm->address = (!empty($user->email)) ? $user->email : $confirm_email->address;
339 if (!$confirm->insert()) {
340 common_log_db_error($confirm, 'INSERT', __FILE__);
341 // TRANS: Server error displayed if e-mail address confirmation fails in the database on the password recovery form.
342 $this->serverError(_('Error saving address confirmation.'));
346 // @todo FIXME: needs i18n.
347 $body = "Hey, $user->nickname.";
349 $body .= 'Someone just asked for a new password ' .
350 'for this account on ' . common_config('site', 'name') . '.';
352 $body .= 'If it was you, and you want to confirm, use the URL below:';
354 $body .= "\t".common_local_url('recoverpassword',
355 array('code' => $confirm->code));
357 $body .= 'If not, just ignore this message.';
359 $body .= 'Thanks for your time, ';
361 $body .= common_config('site', 'name');
364 $headers = _mail_prepare_headers('recoverpassword', $user->nickname, $user->nickname);
365 // TRANS: Subject for password recovery e-mail.
366 mail_to_user($user, _('Password recovery requested'), $body, $headers, $confirm->address);
368 $this->mode = 'sent';
369 // TRANS: User notification after an e-mail with instructions was sent from the password recovery form.
370 $this->msg = _('Instructions for recovering your password ' .
371 'have been sent to the email address registered to your ' .
373 $this->success = true;
377 function resetPassword()
380 $token = $this->trimmed('token');
381 if (!$token || $token != common_session_token()) {
382 // TRANS: Form validation error message.
383 $this->showForm(_('There was a problem with your session token. Try again, please.'));
387 $user = $this->getTempUser();
390 // TRANS: Client error displayed when trying to reset as password without providing a user.
391 $this->clientError(_('Unexpected password reset.'));
395 $newpassword = $this->trimmed('newpassword');
396 $confirm = $this->trimmed('confirm');
398 if (!$newpassword || strlen($newpassword) < 6) {
399 // TRANS: Reset password form validation error message.
400 $this->showPasswordForm(_('Password must be 6 characters or more.'));
403 if ($newpassword != $confirm) {
404 // TRANS: Reset password form validation error message.
405 $this->showPasswordForm(_('Password and confirmation do not match.'));
409 # OK, we're ready to go
411 $original = clone($user);
413 $user->password = common_munge_password($newpassword, $user->id);
415 if (!$user->update($original)) {
416 common_log_db_error($user, 'UPDATE', __FILE__);
417 // TRANS: Reset password form validation error message.
418 $this->serverError(_('Cannot save new password.'));
422 $this->clearTempUser();
424 if (!common_set_user($user->nickname)) {
425 // TRANS: Server error displayed when something does wrong with the user object during password reset.
426 $this->serverError(_('Error setting user.'));
430 common_real_login(true);
432 $this->mode = 'saved';
433 // TRANS: Success message for user after password reset.
434 $this->msg = _('New password successfully saved. ' .
435 'You are now logged in.');
436 $this->success = true;