]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
Twitter-compatible API - removed debugging statement
[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: form token in $_SESSION to prevent XSS
41                 # XXX: login throttle
42                 $nickname = common_canonical_nickname($this->trimmed('nickname'));
43                 $password = $this->arg('password');
44                 if (common_check_user($nickname, $password)) {
45                         # success!
46                         if (!common_set_user($nickname)) {
47                                 common_server_error(_('Error setting user.'));
48                                 return;
49                         }
50                         common_real_login(true);
51                         if ($this->boolean('rememberme')) {
52                                 common_debug('Adding rememberme cookie for ' . $nickname);
53                                 common_rememberme();
54                         }
55                         # success!
56                         $url = common_get_returnto();
57                         if ($url) {
58                                 # We don't have to return to it again
59                                 common_set_returnto(NULL);
60                         } else {
61                                 $url = common_local_url('all',
62                                                                                 array('nickname' =>
63                                                                                           $nickname));
64                         }
65                         common_redirect($url);
66                 } else {
67                         $this->show_form(_('Incorrect username or password.'));
68                         return;
69                 }
70
71                 # success!
72                 if (!common_set_user($user)) {
73                         common_server_error(_('Error setting user.'));
74                         return;
75                 }
76
77                 common_real_login(true);
78
79                 if ($this->boolean('rememberme')) {
80                         common_debug('Adding rememberme cookie for ' . $nickname);
81                         common_rememberme($user);
82                 }
83                 # success!
84                 $url = common_get_returnto();
85                 if ($url) {
86                         # We don't have to return to it again
87                         common_set_returnto(NULL);
88                 } else {
89                         $url = common_local_url('all',
90                                                                         array('nickname' =>
91                                                                                   $nickname));
92                 }
93                 common_redirect($url);
94         }
95
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',
99                                                                                    'id' => 'login',
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();
113         }
114
115         function get_instructions() {
116                 if (common_logged_in() &&
117                         !common_is_real_login() &&
118                         common_get_returnto())
119                 {
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.');
125                 } else {
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%%). ');
130                 }
131         }
132
133         function show_top($error=NULL) {
134                 if ($error) {
135                         common_element('p', 'error', $error);
136                 } else {
137                         $instr = $this->get_instructions();
138                         $output = common_markup_to_html($instr);
139                         common_element_start('div', 'instructions');
140                         common_raw($output);
141                         common_element_end('div');
142                 }
143         }
144 }