]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/recoverpassword.php
06a7f62c8aa796d591c4a4fd0f0135cd86852ed9
[quix0rs-gnu-social.git] / actions / recoverpassword.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 # You have 24 hours to claim your password
23
24 define(MAX_RECOVERY_TIME, 24 * 60 * 60);
25
26 class RecoverpasswordAction extends Action {
27
28     function handle($args) {
29         parent::handle($args);
30         if (common_logged_in()) {
31                         $this->client_error(_t('You are already logged in!'));
32             return;
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();
38                         } else {
39                                 $this->client_error(_t('Unexpected form.'));
40                         }
41                 } else {
42                         if ($this->trimmed('code')) {
43                         $this->check_code();
44                 } else {
45                         $this->show_form();
46                         }
47                 }
48         }
49
50         function check_code() {
51
52                 $code = $this->trimmed('code');
53                 $confirm = Confirm_address::staticGet($code);
54
55                 if (!$confirm) {
56                         $this->client_error(_t('No such recovery code.'));
57                         return;
58                 }
59                 if ($confirm->address_type != 'recover') {
60                         $this->client_error(_t('Not a recovery code.'));
61                         return;
62                 }
63
64                 $user = User::staticGet($confirm->user_id);
65
66                 if (!$user) {
67                         $this->server_error(_t('Recovery code for unknown user.'));
68                         return;
69                 }
70
71                 $touched = strtotime($confirm->modified);
72
73                 # Burn this code
74
75                 $result = $confirm->delete();
76
77                 if (!$result) {
78                         common_log_db_error($confirm, 'DELETE', __FILE__);
79                         common_server_error(_t('Error with confirmation code.'));
80                         return;
81                 }
82
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!
85
86                 if ((time() - $touched) > MAX_RECOVERY_TIME) {
87                         $this->client_error(_t('This confirmation code is too old. ' .
88                                                'Please start again.'));
89                         return;
90                 }
91
92                 # Success!
93
94                 $this->set_temp_user($user);
95                 $this->show_password_form();
96         }
97
98         function set_temp_user(&$user) {
99                 common_ensure_session();
100                 $_SESSION['tempuser'] = $user->id;
101         }
102
103         function get_temp_user() {
104                 common_ensure_session();
105                 $user_id = $_SESSION['tempuser'];
106                 if ($user_id) {
107                         $user = User::staticGet($user_id);
108                 }
109                 return $user;
110         }
111
112         function clear_temp_user() {
113                 common_ensure_session();
114                 unset($_SESSION['tempuser']);
115         }
116
117         function show_top($msg=NULL) {
118                 if ($msg) {
119             common_element('div', 'error', $msg);
120                 } else {
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.'));
126                 }
127         }
128
129         function show_password_top($msg=NULL) {
130                 if ($msg) {
131             common_element('div', 'error', $msg);
132                 } else {
133                         common_element('div', 'instructions',
134                                                    _t('You\'ve been identified. Enter a ' .
135                                                       ' new password below. '));
136                 }
137         }
138
139         function show_form($msg=NULL) {
140
141                 common_show_header(_t('Recover password'), NULL,
142                 $msg, array($this, 'show_top'));
143
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();
154         }
155
156         function show_password_form($msg=NULL) {
157
158                 common_show_header(_t('Reset password'), NULL,
159                 $msg, array($this, 'show_password_top'));
160
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();
171         }
172
173         function recover_password() {
174                 $nore = $this->trimmed('nicknameoremail');
175                 if (!$nore) {
176                         $this->show_form(_t('Enter a nickname or email address.'));
177                         return;
178                 }
179                 $user = User::staticGet('email', common_canonical_email($nore));
180                 if (!$user) {
181                         $user = User::staticGet('nickname', common_canonical_nickname($nore));
182                 }
183
184                 if (!$user) {
185                         $this->show_form(_t('No such user.'));
186                         return;
187                 }
188                 if (!$user->email) {
189                         $this->client_error(_t('No registered email address for that user.'));
190                         return;
191                 }
192
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;
198
199                 if (!$confirm->insert()) {
200                         common_log_db_error($confirm, 'INSERT', __FILE__);
201                         $this->server_error(_t('Error saving address confirmation.'));
202                         return;
203                 }
204
205                 $body = "Hey, $user->nickname.";
206                 $body .= "\n\n";
207                 $body .= 'Someone just asked for a new password ' .
208                          'for this account on ' . common_config('site', 'name') . '.';
209                 $body .= "\n\n";
210                 $body .= 'If it was you, and you want to confirm, use the URL below:';
211                 $body .= "\n\n";
212                 $body .= "\t".common_local_url('recoverpassword',
213                                                                    array('code' => $confirm->code));
214                 $body .= "\n\n";
215                 $body .= 'If not, just ignore this message.';
216                 $body .= "\n\n";
217                 $body .= 'Thanks for your time, ';
218                 $body .= "\n";
219                 $body .= common_config('site', 'name');
220                 $body .= "\n";
221
222                 mail_to_user($user, _t('Password recovery requested'), $body);
223
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 ' .
228                                   'account.'));
229                 common_show_footer();
230         }
231
232         function reset_password() {
233
234                 $user = $this->get_temp_user();
235
236                 if (!$user) {
237                         $this->client_error(_t('Unexpected password reset.'));
238                         return;
239                 }
240
241                 $newpassword = $this->trimmed('newpassword');
242                 $confirm = $this->trimmed('confirm');
243
244                 if (!$newpassword || strlen($newpassword) < 6) {
245                         $this->show_password_form(_t('Password must be 6 chars or more.'));
246                         return;
247                 }
248                 if ($newpassword != $confirm) {
249                         $this->show_password_form(_t('Password and confirmation do not match.'));
250                         return;
251                 }
252
253                 # OK, we're ready to go
254
255                 $original = clone($user);
256
257                 $user->password = common_munge_password($newpassword, $user->id);
258
259                 if (!$user->update($original)) {
260                         common_log_db_error($user, 'UPDATE', __FILE__);
261                         common_server_error(_t('Can\'t save new password.'));
262                         return;
263                 }
264
265                 $this->clear_temp_user();
266
267                 if (!common_set_user($user->nickname)) {
268                         common_server_error(_t('Error setting user.'));
269                         return;
270                 }
271
272                 common_real_login(true);
273
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();
278         }
279 }