]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
Merge branch 'testing' into 0.9.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      * @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             $st = common_session_token();
107             if (empty($token)) {
108                 common_log(LOG_WARNING, 'No token provided by client.');
109             } else if (empty($st)) {
110                 common_log(LOG_WARNING, 'No session token stored.');
111             } else {
112                 common_log(LOG_WARNING, 'Token = ' . $token . ' and session token = ' . $st);
113             }
114
115             $this->clientError(_('There was a problem with your session token. '.
116                                  'Try again, please.'));
117             return;
118         }
119
120         $nickname = $this->trimmed('nickname');
121         $password = $this->arg('password');
122
123         $user = common_check_user($nickname, $password);
124
125         if (!$user) {
126             $this->showForm(_('Incorrect username or password.'));
127             return;
128         }
129
130         // success!
131         if (!common_set_user($user)) {
132             $this->serverError(_('Error setting user. You are probably not authorized.'));
133             return;
134         }
135
136         common_real_login(true);
137
138         if ($this->boolean('rememberme')) {
139             common_rememberme($user);
140         }
141
142         $url = common_get_returnto();
143
144         if ($url) {
145             // We don't have to return to it again
146             common_set_returnto(null);
147             $url = common_inject_session($url);
148         } else {
149             $url = common_local_url('all',
150                                     array('nickname' =>
151                                           $user->nickname));
152         }
153
154         common_redirect($url, 303);
155     }
156
157     /**
158      * Store an error and show the page
159      *
160      * This used to show the whole page; now, it's just a wrapper
161      * that stores the error in an attribute.
162      *
163      * @param string $error error, if any.
164      *
165      * @return void
166      */
167
168     function showForm($error=null)
169     {
170         $this->error = $error;
171         $this->showPage();
172     }
173
174     function showScripts()
175     {
176         parent::showScripts();
177         $this->autofocus('nickname');
178     }
179
180     /**
181      * Title of the page
182      *
183      * @return string title of the page
184      */
185
186     function title()
187     {
188         return _('Login');
189     }
190
191     /**
192      * Show page notice
193      *
194      * Display a notice for how to use the page, or the
195      * error if it exists.
196      *
197      * @return void
198      */
199
200     function showPageNotice()
201     {
202         if ($this->error) {
203             $this->element('p', 'error', $this->error);
204         } else {
205             $instr  = $this->getInstructions();
206             $output = common_markup_to_html($instr);
207
208             $this->raw($output);
209         }
210     }
211
212     /**
213      * Core of the display code
214      *
215      * Shows the login form.
216      *
217      * @return void
218      */
219
220     function showContent()
221     {
222         $this->elementStart('form', array('method' => 'post',
223                                           'id' => 'form_login',
224                                           'class' => 'form_settings',
225                                           'action' => common_local_url('login')));
226         $this->elementStart('fieldset');
227         $this->element('legend', null, _('Login to site'));
228         $this->elementStart('ul', 'form_data');
229         $this->elementStart('li');
230         $this->input('nickname', _('Nickname'));
231         $this->elementEnd('li');
232         $this->elementStart('li');
233         $this->password('password', _('Password'));
234         $this->elementEnd('li');
235         $this->elementStart('li');
236         $this->checkbox('rememberme', _('Remember me'), false,
237                         _('Automatically login in the future; ' .
238                           'not for shared computers!'));
239         $this->elementEnd('li');
240         $this->elementEnd('ul');
241         $this->submit('submit', _('Login'));
242         $this->hidden('token', common_session_token());
243         $this->elementEnd('fieldset');
244         $this->elementEnd('form');
245         $this->elementStart('p');
246         $this->element('a', array('href' => common_local_url('recoverpassword')),
247                        _('Lost or forgotten password?'));
248         $this->elementEnd('p');
249     }
250
251     /**
252      * Instructions for using the form
253      *
254      * For "remembered" logins, we make the user re-login when they
255      * try to change settings. Different instructions for this case.
256      *
257      * @return void
258      */
259
260     function getInstructions()
261     {
262         if (common_logged_in() && !common_is_real_login() &&
263             common_get_returnto()) {
264             // rememberme logins have to reauthenticate before
265             // changing any profile settings (cookie-stealing protection)
266             return _('For security reasons, please re-enter your ' .
267                      'user name and password ' .
268                      'before changing your settings.');
269         } else {
270             $prompt = _('Login with your username and password.');
271             if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
272                 $prompt .= ' ';
273                 $prompt .= _('Don\'t have a username yet? ' .
274                              '[Register](%%action.register%%) a new account.');
275             }
276             return $prompt;
277         }
278     }
279
280     /**
281      * A local menu
282      *
283      * Shows different login/register actions.
284      *
285      * @return void
286      */
287
288     function showLocalNav()
289     {
290         $nav = new LoginGroupNav($this);
291         $nav->show();
292     }
293 }