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