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