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