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