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 {
24 function is_readonly() {
28 function handle($args) {
29 parent::handle($args);
30 if (common_is_real_login()) {
31 common_user_error(_('Already logged in.'));
32 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
39 function check_login() {
40 # XXX: form token in $_SESSION to prevent XSS
42 $nickname = $this->arg('nickname');
43 $password = $this->arg('password');
44 if (common_check_user($nickname, $password)) {
46 if (!common_set_user($nickname)) {
47 common_server_error(_('Error setting user.'));
50 common_real_login(true);
51 if ($this->boolean('rememberme')) {
52 common_debug('Adding rememberme cookie for ' . $nickname);
56 $url = common_get_returnto();
58 # We don't have to return to it again
59 common_set_returnto(NULL);
61 $url = common_local_url('all',
65 common_redirect($url);
67 $this->show_form(_('Incorrect username or password.'));
72 if (!common_set_user($user)) {
73 common_server_error(_('Error setting user.'));
77 common_real_login(true);
79 if ($this->boolean('rememberme')) {
80 common_debug('Adding rememberme cookie for ' . $nickname);
81 common_rememberme($user);
84 $url = common_get_returnto();
86 # We don't have to return to it again
87 common_set_returnto(NULL);
89 $url = common_local_url('all',
93 common_redirect($url);
96 function show_form($error=NULL) {
97 common_show_header(_('Login'), NULL, $error, array($this, 'show_top'));
98 common_element_start('form', array('method' => 'post',
100 'action' => common_local_url('login')));
101 common_input('nickname', _('Nickname'));
102 common_password('password', _('Password'));
103 common_checkbox('rememberme', _('Remember me'), false,
104 _('Automatically login in the future; ' .
105 'not for shared computers!'));
106 common_submit('submit', _('Login'));
107 common_element_end('form');
108 common_element_start('p');
109 common_element('a', array('href' => common_local_url('recoverpassword')),
110 _('Lost or forgotten password?'));
111 common_element_end('p');
112 common_show_footer();
115 function get_instructions() {
116 if (common_logged_in() &&
117 !common_is_real_login() &&
118 common_get_returnto())
120 # rememberme logins have to reauthenticate before
121 # changing any profile settings (cookie-stealing protection)
122 return _('For security reasons, please re-enter your ' .
123 'user name and password ' .
124 'before changing your settings.');
126 return _('Login with your username and password. ' .
127 'Don\'t have a username yet? ' .
128 '[Register](%%action.register%%) a new account, or ' .
129 'try [OpenID](%%action.openidlogin%%). ');
133 function show_top($error=NULL) {
135 common_element('p', 'error', $error);
137 $instr = $this->get_instructions();
138 $output = common_markup_to_html($instr);
139 common_element_start('div', 'instructions');
141 common_element_end('div');