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