]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
generate an etag for shownotice
[quix0rs-gnu-social.git] / actions / login.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 class LoginAction extends Action {
23
24         function is_readonly() {
25                 return true;
26         }
27
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') {
33                         $this->check_login();
34                 } else {
35                         $this->show_form();
36                 }
37         }
38
39         function check_login() {
40                 # XXX: login throttle
41
42                 # CSRF protection - token set in common_notice_form()
43                 $token = $this->trimmed('token');
44                 if (!$token || $token != common_session_token()) {
45                         $this->client_error(_('There was a problem with your session token. Try again, please.'));
46                         return;
47                 }
48
49                 $nickname = common_canonical_nickname($this->trimmed('nickname'));
50                 $password = $this->arg('password');
51                 if (common_check_user($nickname, $password)) {
52                         # success!
53                         if (!common_set_user($nickname)) {
54                                 common_server_error(_('Error setting user.'));
55                                 return;
56                         }
57                         common_real_login(true);
58                         if ($this->boolean('rememberme')) {
59                                 common_debug('Adding rememberme cookie for ' . $nickname);
60                                 common_rememberme();
61                         }
62                         # success!
63                         $url = common_get_returnto();
64                         if ($url) {
65                                 # We don't have to return to it again
66                                 common_set_returnto(NULL);
67                         } else {
68                                 $url = common_local_url('all',
69                                                                                 array('nickname' =>
70                                                                                           $nickname));
71                         }
72                         common_redirect($url);
73                 } else {
74                         $this->show_form(_('Incorrect username or password.'));
75                         return;
76                 }
77
78                 # success!
79                 if (!common_set_user($user)) {
80                         common_server_error(_('Error setting user.'));
81                         return;
82                 }
83
84                 common_real_login(true);
85
86                 if ($this->boolean('rememberme')) {
87                         common_debug('Adding rememberme cookie for ' . $nickname);
88                         common_rememberme($user);
89                 }
90                 # success!
91                 $url = common_get_returnto();
92                 if ($url) {
93                         # We don't have to return to it again
94                         common_set_returnto(NULL);
95                 } else {
96                         $url = common_local_url('all',
97                                                                         array('nickname' =>
98                                                                                   $nickname));
99                 }
100                 common_redirect($url);
101         }
102
103         function show_form($error=NULL) {
104                 common_show_header(_('Login'), NULL, $error, array($this, 'show_top'));
105                 common_element_start('form', array('method' => 'post',
106                                                                                    'id' => 'login',
107                                                                                    'action' => common_local_url('login')));
108                 common_input('nickname', _('Nickname'));
109                 common_password('password', _('Password'));
110                 common_checkbox('rememberme', _('Remember me'), false,
111                                 _('Automatically login in the future; ' .
112                                    'not for shared computers!'));
113                 common_submit('submit', _('Login'));
114                 common_hidden('token', common_session_token());
115                 common_element_end('form');
116                 common_element_start('p');
117                 common_element('a', array('href' => common_local_url('recoverpassword')),
118                                            _('Lost or forgotten password?'));
119                 common_element_end('p');
120                 common_show_footer();
121         }
122
123         function get_instructions() {
124                 if (common_logged_in() &&
125                         !common_is_real_login() &&
126                         common_get_returnto())
127                 {
128                         # rememberme logins have to reauthenticate before
129                         # changing any profile settings (cookie-stealing protection)
130                         return _('For security reasons, please re-enter your ' .
131                                          'user name and password ' .
132                                          'before changing your settings.');
133                 } else {
134                         return _('Login with your username and password. ' .
135                                          'Don\'t have a username yet? ' .
136                                          '[Register](%%action.register%%) a new account, or ' .
137                                          'try [OpenID](%%action.openidlogin%%). ');
138                 }
139         }
140
141         function show_top($error=NULL) {
142                 if ($error) {
143                         common_element('p', 'error', $error);
144                 } else {
145                         $instr = $this->get_instructions();
146                         $output = common_markup_to_html($instr);
147                         common_element_start('div', 'instructions');
148                         common_raw($output);
149                         common_element_end('div');
150                 }
151         }
152 }