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