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