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 $this->element('p', null,
166 // TRANS: Page notice for password change page.
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: Title 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');
278 // TRANS: Form instructions for password recovery form.
279 $this->showForm(_('Enter a nickname or email address.'));
284 User::recoverPassword($nore);
285 $this->mode = 'sent';
286 // TRANS: User notification after an e-mail with instructions was sent from the password recovery form.
287 $this->msg = _('Instructions for recovering your password ' .
288 'have been sent to the email address registered to your ' .
290 $this->success = true;
292 } catch (Exception $e) {
293 $this->success = false;
297 function resetPassword()
300 $token = $this->trimmed('token');
301 if (!$token || $token != common_session_token()) {
302 // TRANS: Form validation error message.
303 $this->showForm(_('There was a problem with your session token. Try again, please.'));
307 $user = $this->getTempUser();
310 // TRANS: Client error displayed when trying to reset as password without providing a user.
311 $this->clientError(_('Unexpected password reset.'));
315 $newpassword = $this->trimmed('newpassword');
316 $confirm = $this->trimmed('confirm');
318 if (!$newpassword || strlen($newpassword) < 6) {
319 // TRANS: Reset password form validation error message.
320 $this->showPasswordForm(_('Password must be 6 characters or more.'));
323 if ($newpassword != $confirm) {
324 // TRANS: Reset password form validation error message.
325 $this->showPasswordForm(_('Password and confirmation do not match.'));
329 // OK, we're ready to go
331 $original = clone($user);
333 $user->password = common_munge_password($newpassword, $user->id);
335 if (!$user->update($original)) {
336 common_log_db_error($user, 'UPDATE', __FILE__);
337 // TRANS: Reset password form validation error message.
338 $this->serverError(_('Cannot save new password.'));
342 $this->clearTempUser();
344 if (!common_set_user($user->nickname)) {
345 // TRANS: Server error displayed when something does wrong with the user object during password reset.
346 $this->serverError(_('Error setting user.'));
350 common_real_login(true);
352 $this->mode = 'saved';
353 // TRANS: Success message for user after password reset.
354 $this->msg = _('New password successfully saved. ' .
355 'You are now logged in.');
356 $this->success = true;
363 * Shows different login/register actions.
368 function showLocalNav()
370 $nav = new LoginGroupNav($this);