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 LoginAction extends Action
31 function handle($args)
33 parent::handle($args);
34 if (common_is_real_login()) {
35 $this->clientError(_('Already logged in.'));
36 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
47 # CSRF protection - token set in common_notice_form()
48 $token = $this->trimmed('token');
49 if (!$token || $token != common_session_token()) {
50 $this->clientError(_('There was a problem with your session token. Try again, please.'));
54 $nickname = common_canonical_nickname($this->trimmed('nickname'));
55 $password = $this->arg('password');
56 if (common_check_user($nickname, $password)) {
58 if (!common_set_user($nickname)) {
59 $this->serverError(_('Error setting user.'));
62 common_real_login(true);
63 if ($this->boolean('rememberme')) {
64 common_debug('Adding rememberme cookie for ' . $nickname);
68 $url = common_get_returnto();
70 # We don't have to return to it again
71 common_set_returnto(null);
73 $url = common_local_url('all',
77 common_redirect($url);
79 $this->showForm(_('Incorrect username or password.'));
84 if (!common_set_user($user)) {
85 $this->serverError(_('Error setting user.'));
89 common_real_login(true);
91 if ($this->boolean('rememberme')) {
92 common_debug('Adding rememberme cookie for ' . $nickname);
93 common_rememberme($user);
96 $url = common_get_returnto();
98 # We don't have to return to it again
99 common_set_returnto(null);
101 $url = common_local_url('all',
105 common_redirect($url);
108 function showForm($error=null)
110 $this->error = $error;
119 function showPageNotice()
122 $this->element('p', 'error', $this->error);
124 $instr = $this->getInstructions();
125 $output = common_markup_to_html($instr);
126 $this->elementStart('div', 'instructions');
128 $this->elementEnd('div');
132 function showContent()
134 $this->elementStart('form', array('method' => 'post',
136 'action' => common_local_url('login')));
137 $this->input('nickname', _('Nickname'));
138 $this->password('password', _('Password'));
139 $this->checkbox('rememberme', _('Remember me'), false,
140 _('Automatically login in the future; ' .
141 'not for shared computers!'));
142 $this->submit('submit', _('Login'));
143 $this->hidden('token', common_session_token());
144 $this->elementEnd('form');
145 $this->elementStart('p');
146 $this->element('a', array('href' => common_local_url('recoverpassword')),
147 _('Lost or forgotten password?'));
148 $this->elementEnd('p');
151 function getInstructions()
153 if (common_logged_in() &&
154 !common_is_real_login() &&
155 common_get_returnto())
157 # rememberme logins have to reauthenticate before
158 # changing any profile settings (cookie-stealing protection)
159 return _('For security reasons, please re-enter your ' .
160 'user name and password ' .
161 'before changing your settings.');
163 return _('Login with your username and password. ' .
164 'Don\'t have a username yet? ' .
165 '[Register](%%action.register%%) a new account, or ' .
166 'try [OpenID](%%action.openidlogin%%). ');