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(_t('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(_t('Unexpected form.'));
42 if ($this->trimmed('code')) {
50 function check_code() {
52 $code = $this->trimmed('code');
53 $confirm = Confirm_address::staticGet($code);
56 $this->client_error(_t('No such recovery code.'));
59 if ($confirm->address_type != 'recover') {
60 $this->client_error(_t('Not a recovery code.'));
64 $user = User::staticGet($confirm->user_id);
67 $this->server_error(_t('Recovery code for unknown user.'));
71 $touched = strtotime($confirm->modified);
75 $result = $confirm->delete();
78 common_log_db_error($confirm, 'DELETE', __FILE__);
79 common_server_error(_t('Error with confirmation code.'));
83 # These should be reaped, but for now we just check mod time
84 # Note: it's still deleted; let's avoid a second attempt!
86 if ((time() - $touched) > MAX_RECOVERY_TIME) {
87 $this->client_error(_t('This confirmation code is too old. ' .
88 'Please start again.'));
94 $this->set_temp_user($user);
95 $this->show_password_form();
98 function set_temp_user(&$user) {
99 common_ensure_session();
100 $_SESSION['tempuser'] = $user->id;
103 function get_temp_user() {
104 common_ensure_session();
105 $user_id = $_SESSION['tempuser'];
107 $user = User::staticGet($user_id);
112 function clear_temp_user() {
113 common_ensure_session();
114 unset($_SESSION['tempuser']);
117 function show_top($msg=NULL) {
119 common_element('div', 'error', $msg);
121 common_element('div', 'instructions',
122 _t('If you\'ve forgotten or lost your' .
123 ' password, you can get a new one sent ' .
124 ' the email address you have stored ' .
125 ' in your account.'));
129 function show_password_top($msg=NULL) {
131 common_element('div', 'error', $msg);
133 common_element('div', 'instructions',
134 _t('You\'ve been identified. Enter a ' .
135 ' new password below. '));
139 function show_form($msg=NULL) {
141 common_show_header(_t('Recover password'), NULL,
142 $msg, array($this, 'show_top'));
144 common_element_start('form', array('method' => 'POST',
145 'id' => 'recoverpassword',
146 'action' => common_local_url('recoverpassword')));
147 common_input('nicknameoremail', _t('Nickname or email'),
148 $this->trimmed('nicknameoremail'),
149 _t('Your nickname on this server, ' .
150 'or your registered email address.'));
151 common_submit('recover', _t('Recover'));
152 common_element_end('form');
153 common_show_footer();
156 function show_password_form($msg=NULL) {
158 common_show_header(_t('Reset password'), NULL,
159 $msg, array($this, 'show_password_top'));
161 common_element_start('form', array('method' => 'POST',
162 'id' => 'recoverpassword',
163 'action' => common_local_url('recoverpassword')));
164 common_password('newpassword', _t('New password'),
165 _t('6 or more characters, and don\'t forget it!'));
166 common_password('confirm', _t('Confirm'),
167 _t('Same as password above'));
168 common_submit('reset', _t('Reset'));
169 common_element_end('form');
170 common_show_footer();
173 function recover_password() {
174 $nore = $this->trimmed('nicknameoremail');
176 $this->show_form(_t('Enter a nickname or email address.'));
179 $user = User::staticGet('email', common_canonical_email($nore));
181 $user = User::staticGet('nickname', common_canonical_nickname($nore));
185 $this->show_form(_t('No such user.'));
189 $this->client_error(_t('No registered email address for that user.'));
193 $confirm = new Confirm_address();
194 $confirm->code = common_confirmation_code(128);
195 $confirm->address_type = 'recover';
196 $confirm->user_id = $user->id;
197 $confirm->address = $user->email;
199 if (!$confirm->insert()) {
200 common_log_db_error($confirm, 'INSERT', __FILE__);
201 $this->server_error(_t('Error saving address confirmation.'));
205 $body = "Hey, $user->nickname.";
207 $body .= 'Someone just asked for a new password ' .
208 'for this account on ' . common_config('site', 'name') . '.';
210 $body .= 'If it was you, and you want to confirm, use the URL below:';
212 $body .= "\t".common_local_url('recoverpassword',
213 array('code' => $confirm->code));
215 $body .= 'If not, just ignore this message.';
217 $body .= 'Thanks for your time, ';
219 $body .= common_config('site', 'name');
222 mail_to_user($user, _t('Password recovery requested'), $body);
224 common_show_header(_('Password recovery requested'));
225 common_element('p', NULL,
226 _t('Instructions for recovering your password ' .
227 'have been sent to the email address registered to your ' .
229 common_show_footer();
232 function reset_password() {
234 $user = $this->get_temp_user();
237 $this->client_error(_t('Unexpected password reset.'));
241 $newpassword = $this->trimmed('newpassword');
242 $confirm = $this->trimmed('confirm');
244 if (!$newpassword || strlen($newpassword) < 6) {
245 $this->show_password_form(_t('Password must be 6 chars or more.'));
248 if ($newpassword != $confirm) {
249 $this->show_password_form(_t('Password and confirmation do not match.'));
253 # OK, we're ready to go
255 $original = clone($user);
257 $user->password = common_munge_password($newpassword, $user->id);
259 if (!$user->update($original)) {
260 common_log_db_error($user, 'UPDATE', __FILE__);
261 common_server_error(_t('Can\'t save new password.'));
265 $this->clear_temp_user();
267 if (!common_set_user($user->nickname)) {
268 common_server_error(_t('Error setting user.'));
272 common_real_login(true);
274 common_show_header(_('Password saved.'));
275 common_element('p', NULL, _t('New password successfully saved. ' .
276 'You are now logged in.'));
277 common_show_footer();