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 {
28 function handle($args)
30 parent::handle($args);
31 if (common_logged_in()) {
32 $this->client_error(_('You are already logged in!'));
34 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
35 if ($this->arg('recover')) {
36 $this->recover_password();
37 } else if ($this->arg('reset')) {
38 $this->reset_password();
40 $this->client_error(_('Unexpected form submission.'));
43 if ($this->trimmed('code')) {
54 $code = $this->trimmed('code');
55 $confirm = Confirm_address::staticGet('code', $code);
58 $this->client_error(_('No such recovery code.'));
61 if ($confirm->address_type != 'recover') {
62 $this->client_error(_('Not a recovery code.'));
66 $user = User::staticGet($confirm->user_id);
69 $this->server_error(_('Recovery code for unknown user.'));
73 $touched = strtotime($confirm->modified);
74 $email = $confirm->address;
78 $result = $confirm->delete();
81 common_log_db_error($confirm, 'DELETE', __FILE__);
82 common_server_error(_('Error with confirmation code.'));
86 # These should be reaped, but for now we just check mod time
87 # Note: it's still deleted; let's avoid a second attempt!
89 if ((time() - $touched) > MAX_RECOVERY_TIME) {
90 common_log(LOG_WARNING,
91 'Attempted redemption on recovery code ' .
92 'that is ' . $touched . ' seconds old. ');
93 $this->client_error(_('This confirmation code is too old. ' .
94 'Please start again.'));
98 # If we used an outstanding confirmation to send the email,
99 # it's been confirmed at this point.
102 $orig = clone($user);
103 $user->email = $email;
104 $result = $user->updateKeys($orig);
106 common_log_db_error($user, 'UPDATE', __FILE__);
107 $this->server_error(_('Could not update user with confirmed email address.'));
114 $this->set_temp_user($user);
115 $this->show_password_form();
118 function set_temp_user(&$user)
120 common_ensure_session();
121 $_SESSION['tempuser'] = $user->id;
124 function get_temp_user()
126 common_ensure_session();
127 $user_id = $_SESSION['tempuser'];
129 $user = User::staticGet($user_id);
134 function clear_temp_user()
136 common_ensure_session();
137 unset($_SESSION['tempuser']);
140 function show_top($msg=null)
143 common_element('div', 'error', $msg);
145 common_element_start('div', 'instructions');
146 common_element('p', null,
147 _('If you\'ve forgotten or lost your' .
148 ' password, you can get a new one sent to' .
149 ' the email address you have stored ' .
150 ' in your account.'));
151 common_element_end('div');
155 function show_password_top($msg=null)
158 common_element('div', 'error', $msg);
160 common_element('div', 'instructions',
161 _('You\'ve been identified. Enter a ' .
162 ' new password below. '));
166 function show_form($msg=null)
169 common_show_header(_('Recover password'), null,
170 $msg, array($this, 'show_top'));
172 common_element_start('form', array('method' => 'post',
173 'id' => 'recoverpassword',
174 'action' => common_local_url('recoverpassword')));
175 common_input('nicknameoremail', _('Nickname or email'),
176 $this->trimmed('nicknameoremail'),
177 _('Your nickname on this server, ' .
178 'or your registered email address.'));
179 common_submit('recover', _('Recover'));
180 common_element_end('form');
181 common_show_footer();
184 function show_password_form($msg=null)
187 common_show_header(_('Reset password'), null,
188 $msg, array($this, 'show_password_top'));
190 common_element_start('form', array('method' => 'post',
191 'id' => 'recoverpassword',
192 'action' => common_local_url('recoverpassword')));
193 common_hidden('token', common_session_token());
194 common_password('newpassword', _('New password'),
195 _('6 or more characters, and don\'t forget it!'));
196 common_password('confirm', _('Confirm'),
197 _('Same as password above'));
198 common_submit('reset', _('Reset'));
199 common_element_end('form');
200 common_show_footer();
203 function recover_password()
205 $nore = $this->trimmed('nicknameoremail');
207 $this->show_form(_('Enter a nickname or email address.'));
211 $user = User::staticGet('email', common_canonical_email($nore));
214 $user = User::staticGet('nickname', common_canonical_nickname($nore));
217 # See if it's an unconfirmed email address
220 $confirm_email = Confirm_address::staticGet('address', common_canonical_email($nore));
221 if ($confirm_email && $confirm_email->address_type == 'email') {
222 $user = User::staticGet($confirm_email->user_id);
227 $this->show_form(_('No user with that email address or username.'));
231 # Try to get an unconfirmed email address if they used a user name
233 if (!$user->email && !$confirm_email) {
234 $confirm_email = Confirm_address::staticGet('user_id', $user->id);
235 if ($confirm_email && $confirm_email->address_type != 'email') {
236 # Skip non-email confirmations
237 $confirm_email = null;
241 if (!$user->email && !$confirm_email) {
242 $this->client_error(_('No registered email address for that user.'));
246 # Success! We have a valid user and a confirmed or unconfirmed email address
248 $confirm = new Confirm_address();
249 $confirm->code = common_confirmation_code(128);
250 $confirm->address_type = 'recover';
251 $confirm->user_id = $user->id;
252 $confirm->address = (isset($user->email)) ? $user->email : $confirm_email->address;
254 if (!$confirm->insert()) {
255 common_log_db_error($confirm, 'INSERT', __FILE__);
256 $this->server_error(_('Error saving address confirmation.'));
260 $body = "Hey, $user->nickname.";
262 $body .= 'Someone just asked for a new password ' .
263 'for this account on ' . common_config('site', 'name') . '.';
265 $body .= 'If it was you, and you want to confirm, use the URL below:';
267 $body .= "\t".common_local_url('recoverpassword',
268 array('code' => $confirm->code));
270 $body .= 'If not, just ignore this message.';
272 $body .= 'Thanks for your time, ';
274 $body .= common_config('site', 'name');
277 mail_to_user($user, _('Password recovery requested'), $body, $confirm->address);
279 common_show_header(_('Password recovery requested'));
280 common_element('p', null,
281 _('Instructions for recovering your password ' .
282 'have been sent to the email address registered to your ' .
284 common_show_footer();
287 function reset_password()
291 $token = $this->trimmed('token');
292 if (!$token || $token != common_session_token()) {
293 $this->show_form(_('There was a problem with your session token. Try again, please.'));
297 $user = $this->get_temp_user();
300 $this->client_error(_('Unexpected password reset.'));
304 $newpassword = $this->trimmed('newpassword');
305 $confirm = $this->trimmed('confirm');
307 if (!$newpassword || strlen($newpassword) < 6) {
308 $this->show_password_form(_('Password must be 6 chars or more.'));
311 if ($newpassword != $confirm) {
312 $this->show_password_form(_('Password and confirmation do not match.'));
316 # OK, we're ready to go
318 $original = clone($user);
320 $user->password = common_munge_password($newpassword, $user->id);
322 if (!$user->update($original)) {
323 common_log_db_error($user, 'UPDATE', __FILE__);
324 common_server_error(_('Can\'t save new password.'));
328 $this->clear_temp_user();
330 if (!common_set_user($user->nickname)) {
331 common_server_error(_('Error setting user.'));
335 common_real_login(true);
337 common_show_header(_('Password saved.'));
338 common_element('p', null, _('New password successfully saved. ' .
339 'You are now logged in.'));
340 common_show_footer();