]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
fix check for ssl diff in login
[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 (common_config('site', 'ssl') == 'sometimes' && // mixed environment
136             0 != strcasecmp(common_config('site', 'server'), common_config('site', 'sslserver'))) {
137             $this->redirectFromSSL($user, $url, $this->boolean('rememberme'));
138             return;
139         }
140
141         if ($url) {
142             // We don't have to return to it again
143             common_set_returnto(null);
144         } else {
145             $url = common_local_url('all',
146                                     array('nickname' =>
147                                           $user->nickname));
148         }
149
150         common_redirect($url, 303);
151     }
152
153     /**
154      * Store an error and show the page
155      *
156      * This used to show the whole page; now, it's just a wrapper
157      * that stores the error in an attribute.
158      *
159      * @param string $error error, if any.
160      *
161      * @return void
162      */
163
164     function showForm($error=null)
165     {
166         $this->error = $error;
167         $this->showPage();
168     }
169
170     function showScripts()
171     {
172         parent::showScripts();
173         $this->autofocus('nickname');
174     }
175
176     /**
177      * Title of the page
178      *
179      * @return string title of the page
180      */
181
182     function title()
183     {
184         return _('Login');
185     }
186
187     /**
188      * Show page notice
189      *
190      * Display a notice for how to use the page, or the
191      * error if it exists.
192      *
193      * @return void
194      */
195
196     function showPageNotice()
197     {
198         if ($this->error) {
199             $this->element('p', 'error', $this->error);
200         } else {
201             $instr  = $this->getInstructions();
202             $output = common_markup_to_html($instr);
203
204             $this->raw($output);
205         }
206     }
207
208     /**
209      * Core of the display code
210      *
211      * Shows the login form.
212      *
213      * @return void
214      */
215
216     function showContent()
217     {
218         $this->elementStart('form', array('method' => 'post',
219                                           'id' => 'form_login',
220                                           'class' => 'form_settings',
221                                           'action' => common_local_url('login')));
222         $this->elementStart('fieldset');
223         $this->element('legend', null, _('Login to site'));
224         $this->elementStart('ul', 'form_data');
225         $this->elementStart('li');
226         $this->input('nickname', _('Nickname'));
227         $this->elementEnd('li');
228         $this->elementStart('li');
229         $this->password('password', _('Password'));
230         $this->elementEnd('li');
231         $this->elementStart('li');
232         $this->checkbox('rememberme', _('Remember me'), false,
233                         _('Automatically login in the future; ' .
234                           'not for shared computers!'));
235         $this->elementEnd('li');
236         $this->elementEnd('ul');
237         $this->submit('submit', _('Login'));
238         $this->hidden('token', common_session_token());
239         $this->elementEnd('fieldset');
240         $this->elementEnd('form');
241         $this->elementStart('p');
242         $this->element('a', array('href' => common_local_url('recoverpassword')),
243                        _('Lost or forgotten password?'));
244         $this->elementEnd('p');
245     }
246
247     /**
248      * Instructions for using the form
249      *
250      * For "remembered" logins, we make the user re-login when they
251      * try to change settings. Different instructions for this case.
252      *
253      * @return void
254      */
255
256     function getInstructions()
257     {
258         if (common_logged_in() && !common_is_real_login() &&
259             common_get_returnto()) {
260             // rememberme logins have to reauthenticate before
261             // changing any profile settings (cookie-stealing protection)
262             return _('For security reasons, please re-enter your ' .
263                      'user name and password ' .
264                      'before changing your settings.');
265         } else {
266             return _('Login with your username and password. ' .
267                      'Don\'t have a username yet? ' .
268                      '[Register](%%action.register%%) a new account.');
269         }
270     }
271
272     /**
273      * A local menu
274      *
275      * Shows different login/register actions.
276      *
277      * @return void
278      */
279
280     function showLocalNav()
281     {
282         $nav = new LoginGroupNav($this);
283         $nav->show();
284     }
285
286     function redirectFromSSL($user, $returnto, $rememberme)
287     {
288         try {
289             $login_token = Login_token::makeNew($user);
290         } catch (Exception $e) {
291             $this->serverError($e->getMessage());
292             return;
293         }
294
295         $params = array();
296
297         if (!empty($returnto)) {
298             $params['returnto'] = $returnto;
299         }
300
301         if (!empty($rememberme)) {
302             $params['rememberme'] = $rememberme;
303         }
304
305         $target = common_local_url('otp',
306                                    array('user_id' => $login_token->user_id,
307                                          'token' => $login_token->token),
308                                    $params);
309
310         common_redirect($target, 303);
311     }
312 }