3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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('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
29 function handle($args)
31 parent::handle($args);
32 if (common_logged_in()) {
33 $this->client_error(_('You are already logged in!'));
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();
41 $this->client_error(_('Unexpected form submission.'));
44 if ($this->trimmed('code')) {
55 $code = $this->trimmed('code');
56 $confirm = Confirm_address::staticGet('code', $code);
59 $this->client_error(_('No such recovery code.'));
62 if ($confirm->address_type != 'recover') {
63 $this->client_error(_('Not a recovery code.'));
67 $user = User::staticGet($confirm->user_id);
70 $this->server_error(_('Recovery code for unknown user.'));
74 $touched = strtotime($confirm->modified);
75 $email = $confirm->address;
79 $result = $confirm->delete();
82 common_log_db_error($confirm, 'DELETE', __FILE__);
83 common_server_error(_('Error with confirmation code.'));
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!
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.'));
99 # If we used an outstanding confirmation to send the email,
100 # it's been confirmed at this point.
103 $orig = clone($user);
104 $user->email = $email;
105 $result = $user->updateKeys($orig);
107 common_log_db_error($user, 'UPDATE', __FILE__);
108 $this->server_error(_('Could not update user with confirmed email address.'));
115 $this->set_temp_user($user);
116 $this->show_password_form();
119 function set_temp_user(&$user)
121 common_ensure_session();
122 $_SESSION['tempuser'] = $user->id;
125 function get_temp_user()
127 common_ensure_session();
128 $user_id = $_SESSION['tempuser'];
130 $user = User::staticGet($user_id);
135 function clear_temp_user()
137 common_ensure_session();
138 unset($_SESSION['tempuser']);
141 function show_top($msg=null)
144 common_element('div', 'error', $msg);
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');
156 function show_password_top($msg=null)
159 common_element('div', 'error', $msg);
161 common_element('div', 'instructions',
162 _('You\'ve been identified. Enter a ' .
163 ' new password below. '));
167 function show_form($msg=null)
170 common_show_header(_('Recover password'), null,
171 $msg, array($this, 'show_top'));
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();
185 function show_password_form($msg=null)
188 common_show_header(_('Reset password'), null,
189 $msg, array($this, 'show_password_top'));
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();
204 function recover_password()
206 $nore = $this->trimmed('nicknameoremail');
208 $this->show_form(_('Enter a nickname or email address.'));
212 $user = User::staticGet('email', common_canonical_email($nore));
215 $user = User::staticGet('nickname', common_canonical_nickname($nore));
218 # See if it's an unconfirmed email address
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);
228 $this->show_form(_('No user with that email address or username.'));
232 # Try to get an unconfirmed email address if they used a user name
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;
242 if (!$user->email && !$confirm_email) {
243 $this->client_error(_('No registered email address for that user.'));
247 # Success! We have a valid user and a confirmed or unconfirmed email address
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;
255 if (!$confirm->insert()) {
256 common_log_db_error($confirm, 'INSERT', __FILE__);
257 $this->server_error(_('Error saving address confirmation.'));
261 $body = "Hey, $user->nickname.";
263 $body .= 'Someone just asked for a new password ' .
264 'for this account on ' . common_config('site', 'name') . '.';
266 $body .= 'If it was you, and you want to confirm, use the URL below:';
268 $body .= "\t".common_local_url('recoverpassword',
269 array('code' => $confirm->code));
271 $body .= 'If not, just ignore this message.';
273 $body .= 'Thanks for your time, ';
275 $body .= common_config('site', 'name');
278 mail_to_user($user, _('Password recovery requested'), $body, $confirm->address);
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 ' .
285 common_show_footer();
288 function reset_password()
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.'));
298 $user = $this->get_temp_user();
301 $this->client_error(_('Unexpected password reset.'));
305 $newpassword = $this->trimmed('newpassword');
306 $confirm = $this->trimmed('confirm');
308 if (!$newpassword || strlen($newpassword) < 6) {
309 $this->show_password_form(_('Password must be 6 chars or more.'));
312 if ($newpassword != $confirm) {
313 $this->show_password_form(_('Password and confirmation do not match.'));
317 # OK, we're ready to go
319 $original = clone($user);
321 $user->password = common_munge_password($newpassword, $user->id);
323 if (!$user->update($original)) {
324 common_log_db_error($user, 'UPDATE', __FILE__);
325 common_server_error(_('Can\'t save new password.'));
329 $this->clear_temp_user();
331 if (!common_set_user($user->nickname)) {
332 common_server_error(_('Error setting user.'));
336 common_real_login(true);
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();