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