]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
remove redirect to OTP on login from login, register
[quix0rs-gnu-social.git] / actions / login.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Login form
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Login
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Login form
37  *
38  * @category Personal
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @author   Sarven Capadisli <csarven@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class LoginAction extends Action
47 {
48     /**
49      * Has there been an error?
50      */
51
52     var $error = null;
53
54     /**
55      * Is this a read-only action?
56      *
57      * @return boolean false
58      */
59
60     function isReadOnly($args)
61     {
62         return false;
63     }
64
65     /**
66      * Handle input, produce output
67      *
68      * Switches on request method; either shows the form or handles its input.
69      *
70      * @param array $args $_REQUEST data
71      *
72      * @return void
73      */
74
75     function handle($args)
76     {
77         parent::handle($args);
78
79         if (common_is_real_login()) {
80             $this->clientError(_('Already logged in.'));
81         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
82             $this->checkLogin();
83         } else {
84             common_ensure_session();
85             $this->showForm();
86         }
87     }
88
89     /**
90      * Check the login data
91      *
92      * Determines if the login data is valid. If so, logs the user
93      * in, and redirects to the 'with friends' page, or to the stored
94      * return-to URL.
95      *
96      * @return void
97      */
98
99     function checkLogin($user_id=null, $token=null)
100     {
101         // XXX: login throttle
102
103         // CSRF protection - token set in NoticeForm
104         $token = $this->trimmed('token');
105         if (!$token || $token != common_session_token()) {
106             $this->clientError(_('There was a problem with your session token. '.
107                                  'Try again, please.'));
108             return;
109         }
110
111         $nickname = $this->trimmed('nickname');
112         $password = $this->arg('password');
113
114         $user = common_check_user($nickname, $password);
115
116         if (!$user) {
117             $this->showForm(_('Incorrect username or password.'));
118             return;
119         }
120
121         // success!
122         if (!common_set_user($user)) {
123             $this->serverError(_('Error setting user. You are probably not authorized.'));
124             return;
125         }
126
127         common_real_login(true);
128
129         if ($this->boolean('rememberme')) {
130             common_rememberme($user);
131         }
132
133         $url = common_get_returnto();
134
135         if ($url) {
136             // We don't have to return to it again
137             common_set_returnto(null);
138         } else {
139             $url = common_local_url('all',
140                                     array('nickname' =>
141                                           $user->nickname));
142         }
143
144         common_redirect($url, 303);
145     }
146
147     /**
148      * Store an error and show the page
149      *
150      * This used to show the whole page; now, it's just a wrapper
151      * that stores the error in an attribute.
152      *
153      * @param string $error error, if any.
154      *
155      * @return void
156      */
157
158     function showForm($error=null)
159     {
160         $this->error = $error;
161         $this->showPage();
162     }
163
164     function showScripts()
165     {
166         parent::showScripts();
167         $this->autofocus('nickname');
168     }
169
170     /**
171      * Title of the page
172      *
173      * @return string title of the page
174      */
175
176     function title()
177     {
178         return _('Login');
179     }
180
181     /**
182      * Show page notice
183      *
184      * Display a notice for how to use the page, or the
185      * error if it exists.
186      *
187      * @return void
188      */
189
190     function showPageNotice()
191     {
192         if ($this->error) {
193             $this->element('p', 'error', $this->error);
194         } else {
195             $instr  = $this->getInstructions();
196             $output = common_markup_to_html($instr);
197
198             $this->raw($output);
199         }
200     }
201
202     /**
203      * Core of the display code
204      *
205      * Shows the login form.
206      *
207      * @return void
208      */
209
210     function showContent()
211     {
212         $this->elementStart('form', array('method' => 'post',
213                                           'id' => 'form_login',
214                                           'class' => 'form_settings',
215                                           'action' => common_local_url('login')));
216         $this->elementStart('fieldset');
217         $this->element('legend', null, _('Login to site'));
218         $this->elementStart('ul', 'form_data');
219         $this->elementStart('li');
220         $this->input('nickname', _('Nickname'));
221         $this->elementEnd('li');
222         $this->elementStart('li');
223         $this->password('password', _('Password'));
224         $this->elementEnd('li');
225         $this->elementStart('li');
226         $this->checkbox('rememberme', _('Remember me'), false,
227                         _('Automatically login in the future; ' .
228                           'not for shared computers!'));
229         $this->elementEnd('li');
230         $this->elementEnd('ul');
231         $this->submit('submit', _('Login'));
232         $this->hidden('token', common_session_token());
233         $this->elementEnd('fieldset');
234         $this->elementEnd('form');
235         $this->elementStart('p');
236         $this->element('a', array('href' => common_local_url('recoverpassword')),
237                        _('Lost or forgotten password?'));
238         $this->elementEnd('p');
239     }
240
241     /**
242      * Instructions for using the form
243      *
244      * For "remembered" logins, we make the user re-login when they
245      * try to change settings. Different instructions for this case.
246      *
247      * @return void
248      */
249
250     function getInstructions()
251     {
252         if (common_logged_in() && !common_is_real_login() &&
253             common_get_returnto()) {
254             // rememberme logins have to reauthenticate before
255             // changing any profile settings (cookie-stealing protection)
256             return _('For security reasons, please re-enter your ' .
257                      'user name and password ' .
258                      'before changing your settings.');
259         } else {
260             return _('Login with your username and password. ' .
261                      'Don\'t have a username yet? ' .
262                      '[Register](%%action.register%%) a new account.');
263         }
264     }
265
266     /**
267      * A local menu
268      *
269      * Shows different login/register actions.
270      *
271      * @return void
272      */
273
274     function showLocalNav()
275     {
276         $nav = new LoginGroupNav($this);
277         $nav->show();
278     }
279 }