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 $this->clientError(_('You are already logged in!'));
38 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
39 if ($this->arg('recover')) {
40 $this->recoverPassword();
41 } else if ($this->arg('reset')) {
42 $this->resetPassword();
44 $this->clientError(_('Unexpected form submission.'));
47 if ($this->trimmed('code')) {
58 $code = $this->trimmed('code');
59 $confirm = Confirm_address::staticGet('code', $code);
62 $this->clientError(_('No such recovery code.'));
65 if ($confirm->address_type != 'recover') {
66 $this->clientError(_('Not a recovery code.'));
70 $user = User::staticGet($confirm->user_id);
73 $this->serverError(_('Recovery code for unknown user.'));
77 $touched = strtotime($confirm->modified);
78 $email = $confirm->address;
82 $result = $confirm->delete();
85 common_log_db_error($confirm, 'DELETE', __FILE__);
86 $this->serverError(_('Error with confirmation code.'));
90 # These should be reaped, but for now we just check mod time
91 # Note: it's still deleted; let's avoid a second attempt!
93 if ((time() - $touched) > MAX_RECOVERY_TIME) {
94 common_log(LOG_WARNING,
95 'Attempted redemption on recovery code ' .
96 'that is ' . $touched . ' seconds old. ');
97 $this->clientError(_('This confirmation code is too old. ' .
98 'Please start again.'));
102 # If we used an outstanding confirmation to send the email,
103 # it's been confirmed at this point.
106 $orig = clone($user);
107 $user->email = $email;
108 $result = $user->updateKeys($orig);
110 common_log_db_error($user, 'UPDATE', __FILE__);
111 $this->serverError(_('Could not update user with confirmed email address.'));
118 $this->setTempUser($user);
119 $this->showPasswordForm();
122 function setTempUser(&$user)
124 common_ensure_session();
125 $_SESSION['tempuser'] = $user->id;
128 function getTempUser()
130 common_ensure_session();
131 $user_id = $_SESSION['tempuser'];
133 $user = User::staticGet($user_id);
138 function clearTempUser()
140 common_ensure_session();
141 unset($_SESSION['tempuser']);
144 function showPageNotice()
147 $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
149 $this->elementStart('div', 'instructions');
150 if ($this->mode == 'recover') {
151 $this->element('p', null,
152 _('If you have forgotten or lost your' .
153 ' password, you can get a new one sent to' .
154 ' the email address you have stored' .
155 ' in your account.'));
156 } else if ($this->mode == 'reset') {
157 $this->element('p', null,
158 _('You have been identified. Enter a' .
159 ' new password below. '));
161 $this->elementEnd('div');
165 function showForm($msg=null)
168 $this->mode = 'recover';
172 function showContent()
174 if ($this->mode == 'recover') {
175 $this->showRecoverForm();
176 } else if ($this->mode == 'reset') {
177 $this->showResetForm();
181 function showRecoverForm()
183 $this->elementStart('form', array('method' => 'post',
184 'id' => 'form_password_recover',
185 'class' => 'form_settings',
186 'action' => common_local_url('recoverpassword')));
187 $this->elementStart('fieldset');
188 $this->element('legend', null, _('Password recovery'));
189 $this->elementStart('ul', 'form_data');
190 $this->elementStart('li');
191 $this->input('nicknameoremail', _('Nickname or email address'),
192 $this->trimmed('nicknameoremail'),
193 _('Your nickname on this server, ' .
194 'or your registered email address.'));
195 $this->elementEnd('li');
196 $this->elementEnd('ul');
197 $this->element('input', array('name' => 'recover',
199 'value' => _('Recover')));
200 $this->submit('recover', _('Recover'));
201 $this->elementEnd('fieldset');
202 $this->elementEnd('form');
207 switch ($this->mode) {
208 case 'reset': return _('Reset password');
209 case 'recover': return _('Recover password');
210 case 'sent': return _('Password recovery requested');
211 case 'saved': return _('Password saved.');
213 return _('Unknown action');
217 function showPasswordForm($msg=null)
220 $this->mode = 'reset';
224 function showResetForm()
226 $this->elementStart('form', array('method' => 'post',
227 'id' => 'form_password_change',
228 'class' => 'form_settings',
229 'action' => common_local_url('recoverpassword')));
230 $this->elementStart('fieldset');
231 $this->element('legend', null, _('Password change'));
232 $this->hidden('token', common_session_token());
233 $this->elementStart('ul', 'form_data');
234 $this->elementStart('li');
235 $this->password('newpassword', _('New password'),
236 _('6 or more characters, and don\'t forget it!'));
237 $this->elementEnd('li');
238 $this->elementStart('li');
239 $this->password('confirm', _('Confirm'),
240 _('Same as password above'));
241 $this->elementEnd('li');
242 $this->elementEnd('ul');
243 $this->submit('reset', _('Reset'));
244 $this->elementEnd('fieldset');
245 $this->elementEnd('form');
248 function recoverPassword()
250 $nore = $this->trimmed('nicknameoremail');
252 $this->showForm(_('Enter a nickname or email address.'));
256 $user = User::staticGet('email', common_canonical_email($nore));
259 $user = User::staticGet('nickname', common_canonical_nickname($nore));
262 # See if it's an unconfirmed email address
265 // Warning: it may actually be legit to have multiple folks
266 // who have claimed, but not yet confirmed, the same address.
267 // We'll only send to the first one that comes up.
268 $confirm_email = new Confirm_address();
269 $confirm_email->address = common_canonical_email($nore);
270 $confirm_email->address_type = 'email';
271 $confirm_email->find();
272 if ($confirm_email->fetch()) {
273 $user = User::staticGet($confirm_email->user_id);
275 $confirm_email = null;
278 $confirm_email = null;
282 $this->showForm(_('No user with that email address or username.'));
286 # Try to get an unconfirmed email address if they used a user name
288 if (!$user->email && !$confirm_email) {
289 $confirm_email = new Confirm_address();
290 $confirm_email->user_id = $user->id;
291 $confirm_email->address_type = 'email';
292 $confirm_email->find();
293 if (!$confirm_email->fetch()) {
294 $confirm_email = null;
298 if (!$user->email && !$confirm_email) {
299 $this->clientError(_('No registered email address for that user.'));
303 # Success! We have a valid user and a confirmed or unconfirmed email address
305 $confirm = new Confirm_address();
306 $confirm->code = common_confirmation_code(128);
307 $confirm->address_type = 'recover';
308 $confirm->user_id = $user->id;
309 $confirm->address = (!empty($user->email)) ? $user->email : $confirm_email->address;
311 if (!$confirm->insert()) {
312 common_log_db_error($confirm, 'INSERT', __FILE__);
313 $this->serverError(_('Error saving address confirmation.'));
317 $body = "Hey, $user->nickname.";
319 $body .= 'Someone just asked for a new password ' .
320 'for this account on ' . common_config('site', 'name') . '.';
322 $body .= 'If it was you, and you want to confirm, use the URL below:';
324 $body .= "\t".common_local_url('recoverpassword',
325 array('code' => $confirm->code));
327 $body .= 'If not, just ignore this message.';
329 $body .= 'Thanks for your time, ';
331 $body .= common_config('site', 'name');
334 $headers = _mail_prepare_headers('recoverpassword', $user->nickname, $user->nickname);
335 mail_to_user($user, _('Password recovery requested'), $body, $headers, $confirm->address);
337 $this->mode = 'sent';
338 $this->msg = _('Instructions for recovering your password ' .
339 'have been sent to the email address registered to your ' .
341 $this->success = true;
345 function resetPassword()
348 $token = $this->trimmed('token');
349 if (!$token || $token != common_session_token()) {
350 $this->showForm(_('There was a problem with your session token. Try again, please.'));
354 $user = $this->getTempUser();
357 $this->clientError(_('Unexpected password reset.'));
361 $newpassword = $this->trimmed('newpassword');
362 $confirm = $this->trimmed('confirm');
364 if (!$newpassword || strlen($newpassword) < 6) {
365 $this->showPasswordForm(_('Password must be 6 chars or more.'));
368 if ($newpassword != $confirm) {
369 $this->showPasswordForm(_('Password and confirmation do not match.'));
373 # OK, we're ready to go
375 $original = clone($user);
377 $user->password = common_munge_password($newpassword, $user->id);
379 if (!$user->update($original)) {
380 common_log_db_error($user, 'UPDATE', __FILE__);
381 $this->serverError(_('Can\'t save new password.'));
385 $this->clearTempUser();
387 if (!common_set_user($user->nickname)) {
388 $this->serverError(_('Error setting user.'));
392 common_real_login(true);
394 $this->mode = 'saved';
395 $this->msg = _('New password successfully saved. ' .
396 'You are now logged in.');
397 $this->success = true;