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);
130 function showContent()
132 $this->elementStart('form', array('method' => 'post',
133 'id' => 'form_login',
134 'class' => 'form_settings',
135 'action' => common_local_url('login')));
136 $this->elementStart('fieldset');
137 $this->element('legend', null, _('Login to site'));
138 $this->elementStart('ul', 'form_datas');
139 $this->elementStart('li');
140 $this->input('nickname', _('Nickname'));
141 $this->elementEnd('li');
142 $this->elementStart('li');
143 $this->password('password', _('Password'));
144 $this->elementEnd('li');
145 $this->elementStart('li');
146 $this->checkbox('rememberme', _('Remember me'), false,
147 _('Automatically login in the future; ' .
148 'not for shared computers!'));
149 $this->elementEnd('li');
150 $this->elementEnd('ul');
151 $this->submit('submit', _('Login'));
152 $this->hidden('token', common_session_token());
153 $this->elementEnd('fieldset');
154 $this->elementEnd('form');
155 $this->elementStart('p');
156 $this->element('a', array('href' => common_local_url('recoverpassword')),
157 _('Lost or forgotten password?'));
158 $this->elementEnd('p');
161 function getInstructions()
163 if (common_logged_in() &&
164 !common_is_real_login() &&
165 common_get_returnto())
167 # rememberme logins have to reauthenticate before
168 # changing any profile settings (cookie-stealing protection)
169 return _('For security reasons, please re-enter your ' .
170 'user name and password ' .
171 'before changing your settings.');
173 return _('Login with your username and password. ' .
174 'Don\'t have a username yet? ' .
175 '[Register](%%action.register%%) a new account, or ' .
176 'try [OpenID](%%action.openidlogin%%). ');