We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

recoverpassword.php 13.3 KB
Newer Older
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21

22
// You have 24 hours to claim your password
23

Rasmus Lerdorf's avatar
Rasmus Lerdorf committed
24
define('MAX_RECOVERY_TIME', 24 * 60 * 60);
25

26 27
class RecoverpasswordAction extends Action
{
Evan Prodromou's avatar
Evan Prodromou committed
28 29 30
    var $mode = null;
    var $msg = null;
    var $success = null;
31

32 33
    function handle($args)
    {
34 35
        parent::handle($args);
        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!'));
38
        } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
39
            if ($this->arg('recover')) {
Evan Prodromou's avatar
Evan Prodromou committed
40
                $this->recoverPassword();
41
            } else if ($this->arg('reset')) {
Evan Prodromou's avatar
Evan Prodromou committed
42
                $this->resetPassword();
43
            } else {
44
                // TRANS: Client error displayed when unexpected data is posted in the password recovery form.
45
                $this->clientError(_('Unexpected form submission.'));
46 47 48
            }
        } else {
            if ($this->trimmed('code')) {
Evan Prodromou's avatar
Evan Prodromou committed
49
                $this->checkCode();
50
            } else {
Evan Prodromou's avatar
Evan Prodromou committed
51
                $this->showForm();
52 53 54 55
            }
        }
    }

Evan Prodromou's avatar
Evan Prodromou committed
56
    function checkCode()
57
    {
58
        $code = $this->trimmed('code');
59
        $confirm = Confirm_address::getKV('code', $code);
60 61

        if (!$confirm) {
62
            // TRANS: Client error displayed when password recovery code is not correct.
63
            $this->clientError(_('No such recovery code.'));
64 65
        }
        if ($confirm->address_type != 'recover') {
66
            // TRANS: Client error displayed when no proper password recovery code was submitted.
67
            $this->clientError(_('Not a recovery code.'));
68 69
        }

70
        $user = User::getKV($confirm->user_id);
71 72

        if (!$user) {
73
            // TRANS: Server error displayed trying to recover password without providing a user.
74
            $this->serverError(_('Recovery code for unknown user.'));
75 76 77 78 79
        }

        $touched = strtotime($confirm->modified);
        $email = $confirm->address;

80
        // Burn this code
81 82 83 84 85

        $result = $confirm->delete();

        if (!$result) {
            common_log_db_error($confirm, 'DELETE', __FILE__);
86
            // TRANS: Server error displayed removing a password recovery code from the database.
87
            $this->serverError(_('Error with confirmation code.'));
88 89
        }

90 91
        // These should be reaped, but for now we just check mod time
        // Note: it's still deleted; let's avoid a second attempt!
92 93

        if ((time() - $touched) > MAX_RECOVERY_TIME) {
Evan Prodromou's avatar
Evan Prodromou committed
94
            common_log(LOG_WARNING,
95 96
                       'Attempted redemption on recovery code ' .
                       'that is ' . $touched . ' seconds old. ');
97
            // TRANS: Client error displayed trying to recover password with too old a recovery code.
98
            $this->clientError(_('This confirmation code is too old. ' .
99 100 101
                                   'Please start again.'));
        }

102 103
        // If we used an outstanding confirmation to send the email,
        // it's been confirmed at this point.
104 105 106 107

        if (!$user->email) {
            $orig = clone($user);
            $user->email = $email;
108 109
            // Throws exception on failure.
            $user->updateWithKeys($orig);
110 111
        }

112
        // Success!
113

Evan Prodromou's avatar
Evan Prodromou committed
114 115
        $this->setTempUser($user);
        $this->showPasswordForm();
116 117
    }

Evan Prodromou's avatar
Evan Prodromou committed
118
    function setTempUser(&$user)
119
    {
120 121 122 123
        common_ensure_session();
        $_SESSION['tempuser'] = $user->id;
    }

Evan Prodromou's avatar
Evan Prodromou committed
124
    function getTempUser()
125
    {
126 127 128
        common_ensure_session();
        $user_id = $_SESSION['tempuser'];
        if ($user_id) {
129
            $user = User::getKV($user_id);
130 131 132 133
        }
        return $user;
    }

Evan Prodromou's avatar
Evan Prodromou committed
134
    function clearTempUser()
135
    {
136 137 138 139
        common_ensure_session();
        unset($_SESSION['tempuser']);
    }

Evan Prodromou's avatar
Evan Prodromou committed
140
    function showPageNotice()
141
    {
Evan Prodromou's avatar
Evan Prodromou committed
142 143
        if ($this->msg) {
            $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
144
        } else {
145
            $this->elementStart('div', 'instructions');
Evan Prodromou's avatar
Evan Prodromou committed
146 147
            if ($this->mode == 'recover') {
                $this->element('p', null,
148
                               // TRANS: Page notice for password recovery page.
149
                               _('If you have forgotten or lost your' .
Evan Prodromou's avatar
Evan Prodromou committed
150
                                 ' password, you can get a new one sent to' .
Ori Avtalion's avatar
Ori Avtalion committed
151
                                 ' the email address you have stored' .
Evan Prodromou's avatar
Evan Prodromou committed
152 153 154
                                 ' in your account.'));
            } else if ($this->mode == 'reset') {
                $this->element('p', null,
155
                               // TRANS: Page notice for password change page.
156
                               _('You have been identified. Enter a' .
157
                                 ' new password below.'));
Evan Prodromou's avatar
Evan Prodromou committed
158
            }
159
            $this->elementEnd('div');
160 161 162
        }
    }

Evan Prodromou's avatar
Evan Prodromou committed
163
    function showForm($msg=null)
164
    {
Evan Prodromou's avatar
Evan Prodromou committed
165 166 167
        $this->msg = $msg;
        $this->mode = 'recover';
        $this->showPage();
168 169
    }

Evan Prodromou's avatar
Evan Prodromou committed
170
    function showContent()
171
    {
Evan Prodromou's avatar
Evan Prodromou committed
172 173 174 175 176 177
        if ($this->mode == 'recover') {
            $this->showRecoverForm();
        } else if ($this->mode == 'reset') {
            $this->showResetForm();
        }
    }
178

Evan Prodromou's avatar
Evan Prodromou committed
179 180
    function showRecoverForm()
    {
181
        $this->elementStart('form', array('method' => 'post',
182 183
                                           'id' => 'form_password_recover',
                                           'class' => 'form_settings',
184
                                           'action' => common_local_url('recoverpassword')));
185
        $this->elementStart('fieldset');
186
        // TRANS: Fieldset legend for password recovery page.
187
        $this->element('legend', null, _('Password recovery'));
188 189
        $this->elementStart('ul', 'form_data');
        $this->elementStart('li');
190
        // TRANS: Field label on password recovery page.
191
        $this->input('nicknameoremail', _('Nickname or email address'),
192
                     $this->trimmed('nicknameoremail'),
193
                     // TRANS: Title for field label on password recovery page.
194 195
                     _('Your nickname on this server, ' .
                        'or your registered email address.'));
196 197
        $this->elementEnd('li');
        $this->elementEnd('ul');
198 199
        $this->element('input', array('name' => 'recover',
                                      'type' => 'hidden',
200
                                      // TRANS: Field label on password recovery page.
201
                                      'value' => _('Recover')));
202 203
        // TRANS: Button text on password recovery page.
        $this->submit('recover', _m('BUTTON','Recover'));
204
        $this->elementEnd('fieldset');
205
        $this->elementEnd('form');
206 207
    }

Evan Prodromou's avatar
Evan Prodromou committed
208
    function title()
209
    {
Evan Prodromou's avatar
Evan Prodromou committed
210
        switch ($this->mode) {
211
         // TRANS: Title for password recovery page in password reset mode.
Evan Prodromou's avatar
Evan Prodromou committed
212
         case 'reset': return _('Reset password');
213
         // TRANS: Title for password recovery page in password recover mode.
Evan Prodromou's avatar
Evan Prodromou committed
214
         case 'recover': return _('Recover password');
215
         // TRANS: Title for password recovery page in email sent mode.
Evan Prodromou's avatar
Evan Prodromou committed
216
         case 'sent': return _('Password recovery requested');
217
         // TRANS: Title for password recovery page in password saved mode.
218
         case 'saved': return _('Password saved');
Evan Prodromou's avatar
Evan Prodromou committed
219
         default:
220
            // TRANS: Title for password recovery page when an unknown action has been specified.
Evan Prodromou's avatar
Evan Prodromou committed
221 222 223
            return _('Unknown action');
        }
    }
224

Evan Prodromou's avatar
Evan Prodromou committed
225 226 227 228 229 230
    function showPasswordForm($msg=null)
    {
        $this->msg = $msg;
        $this->mode = 'reset';
        $this->showPage();
    }
231

Evan Prodromou's avatar
Evan Prodromou committed
232 233
    function showResetForm()
    {
234
        $this->elementStart('form', array('method' => 'post',
235 236
                                           'id' => 'form_password_change',
                                           'class' => 'form_settings',
237
                                           'action' => common_local_url('recoverpassword')));
238
        $this->elementStart('fieldset');
239
         // TRANS: Fieldset legend for password reset form.
240
        $this->element('legend', null, _('Password change'));
241
        $this->hidden('token', common_session_token());
242 243
        $this->elementStart('ul', 'form_data');
        $this->elementStart('li');
244
         // TRANS: Field label for password reset form.
245
        $this->password('newpassword', _('New password'),
246 247
                        // TRANS: Title for field label for password reset form.
                        _('6 or more characters, and do not forget it!'));
248 249
        $this->elementEnd('li');
        $this->elementStart('li');
250
         // TRANS: Field label for password reset form where the password has to be typed again.
251
        $this->password('confirm', _('Confirm'),
252
                        // TRANS: Title for field label for password reset form where the password has to be typed again.
253
                        _('Same as password above.'));
254 255
        $this->elementEnd('li');
        $this->elementEnd('ul');
256 257
         // TRANS: Button text for password reset form.
        $this->submit('reset', _m('BUTTON','Reset'));
258
        $this->elementEnd('fieldset');
259
        $this->elementEnd('form');
260 261
    }

Evan Prodromou's avatar
Evan Prodromou committed
262
    function recoverPassword()
263
    {
264
        $nore = $this->trimmed('nicknameoremail');
265

266
        if (!$nore) {
267
            // TRANS: Form instructions for password recovery form.
Evan Prodromou's avatar
Evan Prodromou committed
268
            $this->showForm(_('Enter a nickname or email address.'));
269 270 271
            return;
        }

272 273 274
        try {
            User::recoverPassword($nore);
            $this->mode = 'sent';
275 276 277 278 279 280 281 282 283 284
            if (common_is_email($nore) && common_config('site', 'fakeaddressrecovery')) {
                // TRANS: User notification when recovering password by giving email address,
                //        regardless if the mail was sent or not (to hide registered email status).
                $this->msg = _('If the email address you provided was found in the database, a recovery mail with instructions has been sent there.');
            } else {
                // TRANS: User notification after an e-mail with instructions was sent from the password recovery form.
                $this->msg = _('Instructions for recovering your password ' .
                               'have been sent to the email address registered to your ' .
                               'account.');
            }
285 286 287
            $this->success = true;
        } catch (Exception $e) {
            $this->success = false;
288
            $this->msg = $e->getMessage();
289
        }
290
        $this->showPage();
291 292
    }

Evan Prodromou's avatar
Evan Prodromou committed
293
    function resetPassword()
294
    {
295
        // CSRF protection
296 297
        $token = $this->trimmed('token');
        if (!$token || $token != common_session_token()) {
298
            // TRANS: Form validation error message.
Evan Prodromou's avatar
Evan Prodromou committed
299
            $this->showForm(_('There was a problem with your session token. Try again, please.'));
300 301 302
            return;
        }

Evan Prodromou's avatar
Evan Prodromou committed
303
        $user = $this->getTempUser();
304 305

        if (!$user) {
306
            // TRANS: Client error displayed when trying to reset as password without providing a user.
307
            $this->clientError(_('Unexpected password reset.'));
308 309 310 311 312 313
        }

        $newpassword = $this->trimmed('newpassword');
        $confirm = $this->trimmed('confirm');

        if (!$newpassword || strlen($newpassword) < 6) {
314
            // TRANS: Reset password form validation error message.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
315
            $this->showPasswordForm(_('Password must be 6 characters or more.'));
316 317 318
            return;
        }
        if ($newpassword != $confirm) {
319
            // TRANS: Reset password form validation error message.
Evan Prodromou's avatar
Evan Prodromou committed
320
            $this->showPasswordForm(_('Password and confirmation do not match.'));
321 322 323
            return;
        }

324
        // OK, we're ready to go
325 326 327

        $original = clone($user);

328
        $user->password = common_munge_password($newpassword, $user->getProfile());
329 330 331

        if (!$user->update($original)) {
            common_log_db_error($user, 'UPDATE', __FILE__);
332 333
            // TRANS: Reset password form validation error message.
            $this->serverError(_('Cannot save new password.'));
334 335
        }

Evan Prodromou's avatar
Evan Prodromou committed
336
        $this->clearTempUser();
337 338

        if (!common_set_user($user->nickname)) {
339
            // TRANS: Server error displayed when something does wrong with the user object during password reset.
340
            $this->serverError(_('Error setting user.'));
341 342 343 344
        }

        common_real_login(true);

Evan Prodromou's avatar
Evan Prodromou committed
345
        $this->mode = 'saved';
346
        // TRANS: Success message for user after password reset.
Evan Prodromou's avatar
Evan Prodromou committed
347 348 349 350
        $this->msg = _('New password successfully saved. ' .
                       'You are now logged in.');
        $this->success = true;
        $this->showPage();
351
    }
352 353 354 355 356 357 358 359 360 361 362 363 364 365

    /**
     * A local menu
     *
     * Shows different login/register actions.
     *
     * @return void
     */

    function showLocalNav()
    {
        $nav = new LoginGroupNav($this);
        $nav->show();
    }
366
}