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
25 function is_readonly()
30 function handle($args)
32 parent::handle($args);
33 if (common_is_real_login()) {
34 common_user_error(_('Already logged in.'));
35 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
42 function check_login()
46 # CSRF protection - token set in common_notice_form()
47 $token = $this->trimmed('token');
48 if (!$token || $token != common_session_token()) {
49 $this->client_error(_('There was a problem with your session token. Try again, please.'));
53 $nickname = common_canonical_nickname($this->trimmed('nickname'));
54 $password = $this->arg('password');
55 if (common_check_user($nickname, $password)) {
57 if (!common_set_user($nickname)) {
58 common_server_error(_('Error setting user.'));
61 common_real_login(true);
62 if ($this->boolean('rememberme')) {
63 common_debug('Adding rememberme cookie for ' . $nickname);
67 $url = common_get_returnto();
69 # We don't have to return to it again
70 common_set_returnto(null);
72 $url = common_local_url('all',
76 common_redirect($url);
78 $this->show_form(_('Incorrect username or password.'));
83 if (!common_set_user($user)) {
84 common_server_error(_('Error setting user.'));
88 common_real_login(true);
90 if ($this->boolean('rememberme')) {
91 common_debug('Adding rememberme cookie for ' . $nickname);
92 common_rememberme($user);
95 $url = common_get_returnto();
97 # We don't have to return to it again
98 common_set_returnto(null);
100 $url = common_local_url('all',
104 common_redirect($url);
107 function show_form($error=null)
109 common_show_header(_('Login'), null, $error, array($this, 'show_top'));
110 common_element_start('form', array('method' => 'post',
112 'action' => common_local_url('login')));
113 common_input('nickname', _('Nickname'));
114 common_password('password', _('Password'));
115 common_checkbox('rememberme', _('Remember me'), false,
116 _('Automatically login in the future; ' .
117 'not for shared computers!'));
118 common_submit('submit', _('Login'));
119 common_hidden('token', common_session_token());
120 common_element_end('form');
121 common_element_start('p');
122 common_element('a', array('href' => common_local_url('recoverpassword')),
123 _('Lost or forgotten password?'));
124 common_element_end('p');
125 common_show_footer();
128 function get_instructions()
130 if (common_logged_in() &&
131 !common_is_real_login() &&
132 common_get_returnto())
134 # rememberme logins have to reauthenticate before
135 # changing any profile settings (cookie-stealing protection)
136 return _('For security reasons, please re-enter your ' .
137 'user name and password ' .
138 'before changing your settings.');
140 return _('Login with your username and password. ' .
141 'Don\'t have a username yet? ' .
142 '[Register](%%action.register%%) a new account, or ' .
143 'try [OpenID](%%action.openidlogin%%). ');
147 function show_top($error=null)
150 common_element('p', 'error', $error);
152 $instr = $this->get_instructions();
153 $output = common_markup_to_html($instr);
154 common_element_start('div', 'instructions');
156 common_element_end('div');