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 class RecoverpasswordAction extends Action {
24 function handle($args) {
25 parent::handle($args);
26 if (common_logged_in()) {
27 $this->client_error(_t('You are already logged in!'));
29 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30 if ($this->arg('recover')) {
31 $this->recover_password();
32 } else if ($this->arg('reset')) {
33 $this->reset_password();
35 $this->client_error(_t('Unexpected form.'));
38 if ($this->trimmed('code')) {
46 function check_code() {
47 $code = $this->trimmed('code');
48 $confirm = Confirm_address::staticGet($code);
49 if ($confirm && $confirm->address_type == 'recover') {
50 $user = User::staticGet($confirm->user_id);
52 $result = $confirm->delete();
54 common_log_db_error($confirm, 'DELETE', __FILE__);
55 common_server_error(_t('Error with confirmation code.'));
58 $this->set_temp_user($user);
59 $this->show_password_form();
64 function set_temp_user(&$user) {
65 common_ensure_session();
66 $_SESSION['tempuser'] = $user->id;
69 function get_temp_user() {
70 common_ensure_session();
71 $user_id = $_SESSION['tempuser'];
73 $user = User::staticGet($user_id);
78 function clear_temp_user() {
79 common_ensure_session();
80 unset($_SESSION['tempuser']);
83 function show_top($msg=NULL) {
85 common_element('div', 'error', $msg);
87 common_element('div', 'instructions',
88 _t('If you\'ve forgotten or lost your' .
89 ' password, you can get a new one sent ' .
90 ' the email address you have stored ' .
91 ' in your account.'));
95 function show_password_top($msg=NULL) {
97 common_element('div', 'error', $msg);
99 common_element('div', 'instructions',
100 _t('You\ve been identified . Enter a ' .
101 ' new password below. '));
105 function show_form($msg=NULL) {
107 common_show_header(_t('Recover password'), NULL,
108 $msg, array($this, 'show_top'));
110 common_element_start('form', array('method' => 'POST',
111 'id' => 'recoverpassword',
112 'action' => common_local_url('recoverpassword')));
113 common_input('nicknameoremail', _t('Nickname or email'),
114 $this->trimmed('nicknameoremail'),
115 _t('Your nickname on this server, ' .
116 'or your registered email address.'));
117 common_submit('recover', _t('Recover'));
118 common_element_end('form');
119 common_show_footer();
122 function show_password_form($msg=NULL) {
124 common_show_header(_t('Reset password'), NULL,
125 $msg, array($this, 'show_password_top'));
127 common_element_start('form', array('method' => 'POST',
128 'id' => 'recoverpassword',
129 'action' => common_local_url('recoverpassword')));
130 common_password('newpassword', _t('New password'),
131 _t('6 or more characters, and don\'t forget it!'));
132 common_password('confirm', _t('Confirm'),
133 _t('Same as password above'));
134 common_submit('reset', _t('Reset'));
135 common_element_end('form');
136 common_show_footer();
139 function recover_password() {
140 $nore = $this->trimmed('nicknameoremail');
142 $this->show_form(_t('Enter a nickname or email address.'));
145 $user = User::staticGet('email', common_canonical_email($nore));
147 $user = User::staticGet('nickname', common_canonical_nickname($nore));
151 $this->show_form(_t('No such user.'));
155 $this->client_error(_t('No registered email address for that user.'));
159 $confirm = new Confirm_address();
160 $confirm->code = common_confirmation_code(128);
161 $confirm->address_type = 'recover';
162 $confirm->user_id = $user->id;
163 $confirm->address = $user->email;
165 if (!$confirm->insert()) {
166 common_log_db_error($confirm, 'INSERT', __FILE__);
167 $this->server_error(_t('Error saving address confirmation.'));
171 $body = "Hey, $user->nickname.";
173 $body .= 'Someone just asked for a new password ' .
174 'for this account on ' . common_config('site', 'name') . '.';
176 $body .= 'If it was you, and you want to confirm, use the URL below:';
178 $body .= "\t".common_local_url('recoverpassword',
179 array('code' => $confirm->code));
181 $body .= 'If not, just ignore this message.';
183 $body .= 'Thanks for your time, ';
185 $body .= common_config('site', 'name');
188 mail_to_user($user, _t('Password recovery requested'), $body);
190 common_show_header(_('Password recovery requested'));
191 common_element('p', NULL,
192 _t('Instructions for recovering your password ' .
193 'have been sent to the email address registered to your ' .
195 common_show_footer();
198 function reset_password() {
200 $user = $this->get_temp_user();
203 $this->client_error(_t('Unexpected password reset.'));
207 $password = $this->trimmed('newpassword');
208 $confirm = $this->trimmed('confirm');
210 if (!$password || strlen($password) < 6) {
211 $this->show_password_form(_t('Password must be 6 chars or more.'));
214 if ($password != $confirm) {
215 $this->show_password_form(_t('Password and confirmation do not match.'));
219 # OK, we're ready to go
221 $original = clone($user);
223 $user->password = common_munge_password($newpassword, $user->id);
225 if (!$user->update($original)) {
226 common_log_db_error($user, 'UPDATE', __FILE__);
227 common_server_error(_t('Can\'t save new password.'));
231 $this->clear_temp_user();
233 if (!common_set_user($user->nickname)) {
234 common_server_error(_t('Error setting user.'));
238 common_real_login(true);
240 common_show_header(_('Password saved.'));
241 common_element('p', NULL, _t('New password successfully saved. ' .
242 'You are now logged in.'));
243 common_show_footer();