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