]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
Merge branch '0.8.x' of git://gitorious.org/~brion/statusnet/brion-fixes into 0.8.x
[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      * Checks if only OpenID is allowed and redirects to openidlogin if so.
71      *
72      * @param array $args $_REQUEST data
73      *
74      * @return void
75      */
76
77     function handle($args)
78     {
79         parent::handle($args);
80         if (common_config('site', 'openidonly')) {
81             common_redirect(common_local_url('openidlogin'));
82         } else if (common_is_real_login()) {
83             $this->clientError(_('Already logged in.'));
84         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85             $this->checkLogin();
86         } else {
87             common_ensure_session();
88             $this->showForm();
89         }
90     }
91
92     /**
93      * Check the login data
94      *
95      * Determines if the login data is valid. If so, logs the user
96      * in, and redirects to the 'with friends' page, or to the stored
97      * return-to URL.
98      *
99      * @return void
100      */
101
102     function checkLogin()
103     {
104         // XXX: login throttle
105
106         // CSRF protection - token set in NoticeForm
107         $token = $this->trimmed('token');
108         if (!$token || $token != common_session_token()) {
109             $this->clientError(_('There was a problem with your session token. '.
110                                  'Try again, please.'));
111             return;
112         }
113
114         $nickname = common_canonical_nickname($this->trimmed('nickname'));
115         $password = $this->arg('password');
116
117         $user = common_check_user($nickname, $password);
118
119         if (!$user) {
120             $this->showForm(_('Incorrect username or password.'));
121             return;
122         }
123
124         // success!
125         if (!common_set_user($user)) {
126             $this->serverError(_('Error setting user.'));
127             return;
128         }
129
130         common_real_login(true);
131
132         if ($this->boolean('rememberme')) {
133             common_rememberme($user);
134         }
135
136         $url = common_get_returnto();
137
138         if ($url) {
139             // We don't have to return to it again
140             common_set_returnto(null);
141         } else {
142             $url = common_local_url('all',
143                                     array('nickname' =>
144                                           $nickname));
145         }
146
147         common_redirect($url, 303);
148     }
149
150     /**
151      * Store an error and show the page
152      *
153      * This used to show the whole page; now, it's just a wrapper
154      * that stores the error in an attribute.
155      *
156      * @param string $error error, if any.
157      *
158      * @return void
159      */
160
161     function showForm($error=null)
162     {
163         $this->error = $error;
164         $this->showPage();
165     }
166
167     function showScripts()
168     {
169         parent::showScripts();
170         $this->autofocus('nickname');
171     }
172
173     /**
174      * Title of the page
175      *
176      * @return string title of the page
177      */
178
179     function title()
180     {
181         return _('Login');
182     }
183
184     /**
185      * Show page notice
186      *
187      * Display a notice for how to use the page, or the
188      * error if it exists.
189      *
190      * @return void
191      */
192
193     function showPageNotice()
194     {
195         if ($this->error) {
196             $this->element('p', 'error', $this->error);
197         } else {
198             $instr  = $this->getInstructions();
199             $output = common_markup_to_html($instr);
200
201             $this->raw($output);
202         }
203     }
204
205     /**
206      * Core of the display code
207      *
208      * Shows the login form.
209      *
210      * @return void
211      */
212
213     function showContent()
214     {
215         $this->elementStart('form', array('method' => 'post',
216                                            'id' => 'form_login',
217                                            'class' => 'form_settings',
218                                            'action' => common_local_url('login')));
219         $this->elementStart('fieldset');
220         $this->element('legend', null, _('Login to site'));
221         $this->elementStart('ul', 'form_data');
222         $this->elementStart('li');
223         $this->input('nickname', _('Nickname'));
224         $this->elementEnd('li');
225         $this->elementStart('li');
226         $this->password('password', _('Password'));
227         $this->elementEnd('li');
228         $this->elementStart('li');
229         $this->checkbox('rememberme', _('Remember me'), false,
230                         _('Automatically login in the future; ' .
231                            'not for shared computers!'));
232         $this->elementEnd('li');
233         $this->elementEnd('ul');
234         $this->submit('submit', _('Login'));
235         $this->hidden('token', common_session_token());
236         $this->elementEnd('fieldset');
237         $this->elementEnd('form');
238         $this->elementStart('p');
239         $this->element('a', array('href' => common_local_url('recoverpassword')),
240                        _('Lost or forgotten password?'));
241         $this->elementEnd('p');
242     }
243
244     /**
245      * Instructions for using the form
246      *
247      * For "remembered" logins, we make the user re-login when they
248      * try to change settings. Different instructions for this case.
249      *
250      * @return void
251      */
252
253     function getInstructions()
254     {
255         if (common_logged_in() && !common_is_real_login() &&
256             common_get_returnto()) {
257             // rememberme logins have to reauthenticate before
258             // changing any profile settings (cookie-stealing protection)
259             return _('For security reasons, please re-enter your ' .
260                      'user name and password ' .
261                      'before changing your settings.');
262         } else if (common_config('openid', 'enabled')) {
263             return _('Login with your username and password. ' .
264                      'Don\'t have a username yet? ' .
265                      '[Register](%%action.register%%) a new account, or ' .
266                      'try [OpenID](%%action.openidlogin%%). ');
267         } else {
268             return _('Login with your username and password. ' .
269                      'Don\'t have a username yet? ' .
270                      '[Register](%%action.register%%) a new account.');
271         }
272     }
273
274     /**
275      * A local menu
276      *
277      * Shows different login/register actions.
278      *
279      * @return void
280      */
281
282     function showLocalNav()
283     {
284         $nav = new LoginGroupNav($this);
285         $nav->show();
286     }
287 }