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) {
29 parent::handle($args);
30 if (common_logged_in()) {
31 $this->client_error(_('You are already logged in!'));
33 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
34 if ($this->arg('recover')) {
35 $this->recover_password();
36 } else if ($this->arg('reset')) {
37 $this->reset_password();
39 $this->client_error(_('Unexpected form submission.'));
42 if ($this->trimmed('code')) {
50 function check_code() {
52 $code = $this->trimmed('code');
53 $confirm = Confirm_address::staticGet('code', $code);
56 $this->client_error(_('No such recovery code.'));
59 if ($confirm->address_type != 'recover') {
60 $this->client_error(_('Not a recovery code.'));
64 $user = User::staticGet($confirm->user_id);
67 $this->server_error(_('Recovery code for unknown user.'));
71 $touched = strtotime($confirm->modified);
72 $email = $confirm->address;
76 $result = $confirm->delete();
79 common_log_db_error($confirm, 'DELETE', __FILE__);
80 common_server_error(_('Error with confirmation code.'));
84 # These should be reaped, but for now we just check mod time
85 # Note: it's still deleted; let's avoid a second attempt!
87 if ((time() - $touched) > MAX_RECOVERY_TIME) {
88 common_log(LOG_WARNING,
89 'Attempted redemption on recovery code ' .
90 'that is ' . $touched . ' seconds old. ');
91 $this->client_error(_('This confirmation code is too old. ' .
92 'Please start again.'));
96 # If we used an outstanding confirmation to send the email,
97 # it's been confirmed at this point.
100 $orig = clone($user);
101 $user->email = $email;
102 $result = $user->updateKeys($orig);
104 common_log_db_error($user, 'UPDATE', __FILE__);
105 $this->server_error(_('Could not update user with confirmed email address.'));
112 $this->set_temp_user($user);
113 $this->show_password_form();
116 function set_temp_user(&$user) {
117 common_ensure_session();
118 $_SESSION['tempuser'] = $user->id;
121 function get_temp_user() {
122 common_ensure_session();
123 $user_id = $_SESSION['tempuser'];
125 $user = User::staticGet($user_id);
130 function clear_temp_user() {
131 common_ensure_session();
132 unset($_SESSION['tempuser']);
135 function show_top($msg=NULL) {
137 common_element('div', 'error', $msg);
139 common_element_start('div', 'instructions');
140 common_element('p', NULL,
141 _('If you\'ve forgotten or lost your' .
142 ' password, you can get a new one sent to' .
143 ' the email address you have stored ' .
144 ' in your account.'));
145 common_element_end('div');
149 function show_password_top($msg=NULL) {
151 common_element('div', 'error', $msg);
153 common_element('div', 'instructions',
154 _('You\'ve been identified. Enter a ' .
155 ' new password below. '));
159 function show_form($msg=NULL) {
161 common_show_header(_('Recover password'), NULL,
162 $msg, array($this, 'show_top'));
164 common_element_start('form', array('method' => 'post',
165 'id' => 'recoverpassword',
166 'action' => common_local_url('recoverpassword')));
167 common_input('nicknameoremail', _('Nickname or email'),
168 $this->trimmed('nicknameoremail'),
169 _('Your nickname on this server, ' .
170 'or your registered email address.'));
171 common_submit('recover', _('Recover'));
172 common_element_end('form');
173 common_show_footer();
176 function show_password_form($msg=NULL) {
178 common_show_header(_('Reset password'), NULL,
179 $msg, array($this, 'show_password_top'));
181 common_element_start('form', array('method' => 'post',
182 'id' => 'recoverpassword',
183 'action' => common_local_url('recoverpassword')));
184 common_hidden('token', common_session_token());
185 common_password('newpassword', _('New password'),
186 _('6 or more characters, and don\'t forget it!'));
187 common_password('confirm', _('Confirm'),
188 _('Same as password above'));
189 common_submit('reset', _('Reset'));
190 common_element_end('form');
191 common_show_footer();
194 function recover_password() {
195 $nore = $this->trimmed('nicknameoremail');
197 $this->show_form(_('Enter a nickname or email address.'));
201 $user = User::staticGet('email', common_canonical_email($nore));
204 $user = User::staticGet('nickname', common_canonical_nickname($nore));
207 # See if it's an unconfirmed email address
210 $confirm_email = Confirm_address::staticGet('address', common_canonical_email($nore));
211 if ($confirm_email && $confirm_email->address_type == 'email') {
212 $user = User::staticGet($confirm_email->user_id);
217 $this->show_form(_('No user with that email address or username.'));
221 # Try to get an unconfirmed email address if they used a user name
223 if (!$user->email && !$confirm_email) {
224 $confirm_email = Confirm_address::staticGet('user_id', $user->id);
225 if ($confirm_email && $confirm_email->address_type != 'email') {
226 # Skip non-email confirmations
227 $confirm_email = NULL;
231 if (!$user->email && !$confirm_email) {
232 $this->client_error(_('No registered email address for that user.'));
236 # Success! We have a valid user and a confirmed or unconfirmed email address
238 $confirm = new Confirm_address();
239 $confirm->code = common_confirmation_code(128);
240 $confirm->address_type = 'recover';
241 $confirm->user_id = $user->id;
242 $confirm->address = (isset($user->email)) ? $user->email : $confirm_email->address;
244 if (!$confirm->insert()) {
245 common_log_db_error($confirm, 'INSERT', __FILE__);
246 $this->server_error(_('Error saving address confirmation.'));
250 $body = "Hey, $user->nickname.";
252 $body .= 'Someone just asked for a new password ' .
253 'for this account on ' . common_config('site', 'name') . '.';
255 $body .= 'If it was you, and you want to confirm, use the URL below:';
257 $body .= "\t".common_local_url('recoverpassword',
258 array('code' => $confirm->code));
260 $body .= 'If not, just ignore this message.';
262 $body .= 'Thanks for your time, ';
264 $body .= common_config('site', 'name');
267 mail_to_user($user, _('Password recovery requested'), $body, $confirm->address);
269 common_show_header(_('Password recovery requested'));
270 common_element('p', NULL,
271 _('Instructions for recovering your password ' .
272 'have been sent to the email address registered to your ' .
274 common_show_footer();
277 function reset_password() {
280 $token = $this->trimmed('token');
281 if (!$token || $token != common_session_token()) {
282 $this->show_form(_('There was a problem with your session token. Try again, please.'));
286 $user = $this->get_temp_user();
289 $this->client_error(_('Unexpected password reset.'));
293 $newpassword = $this->trimmed('newpassword');
294 $confirm = $this->trimmed('confirm');
296 if (!$newpassword || strlen($newpassword) < 6) {
297 $this->show_password_form(_('Password must be 6 chars or more.'));
300 if ($newpassword != $confirm) {
301 $this->show_password_form(_('Password and confirmation do not match.'));
305 # OK, we're ready to go
307 $original = clone($user);
309 $user->password = common_munge_password($newpassword, $user->id);
311 if (!$user->update($original)) {
312 common_log_db_error($user, 'UPDATE', __FILE__);
313 common_server_error(_('Can\'t save new password.'));
317 $this->clear_temp_user();
319 if (!common_set_user($user->nickname)) {
320 common_server_error(_('Error setting user.'));
324 common_real_login(true);
326 common_show_header(_('Password saved.'));
327 common_element('p', NULL, _('New password successfully saved. ' .
328 'You are now logged in.'));
329 common_show_footer();