]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
Fix for unicode string auto-linking bug
[quix0rs-gnu-social.git] / actions / login.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Login form
36  *
37  * @category Personal
38  * @package  Laconica
39  * @author   Evan Prodromou <evan@controlyourself.ca>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://laconi.ca/
42  */
43
44 class LoginAction extends Action
45 {
46     /**
47      * Has there been an error?
48      */
49
50     var $error = null;
51
52     /**
53      * Is this a read-only action?
54      *
55      * @return boolean false
56      */
57
58     function isReadOnly()
59     {
60         return false;
61     }
62
63     /**
64      * Handle input, produce output
65      *
66      * Switches on request method; either shows the form or handles its input.
67      *
68      * @param array $args $_REQUEST data
69      *
70      * @return void
71      */
72
73     function handle($args)
74     {
75         parent::handle($args);
76         if (common_is_real_login()) {
77             $this->clientError(_('Already logged in.'));
78         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
79             $this->checkLogin();
80         } else {
81             common_ensure_session();
82             $this->showForm();
83         }
84     }
85
86     /**
87      * Check the login data
88      *
89      * Determines if the login data is valid. If so, logs the user
90      * in, and redirects to the 'with friends' page, or to the stored
91      * return-to URL.
92      *
93      * @return void
94      */
95
96     function checkLogin()
97     {
98         // XXX: login throttle
99
100         // CSRF protection - token set in common_notice_form()
101         $token = $this->trimmed('token');
102         if (!$token || $token != common_session_token()) {
103             $this->clientError(_('There was a problem with your session token. '.
104                                  'Try again, please.'));
105             return;
106         }
107
108         $nickname = common_canonical_nickname($this->trimmed('nickname'));
109         $password = $this->arg('password');
110         if (common_check_user($nickname, $password)) {
111             // success!
112             if (!common_set_user($nickname)) {
113                 $this->serverError(_('Error setting user.'));
114                 return;
115             }
116             common_real_login(true);
117             if ($this->boolean('rememberme')) {
118                 common_debug('Adding rememberme cookie for ' . $nickname);
119                 common_rememberme();
120             }
121             // success!
122             $url = common_get_returnto();
123             if ($url) {
124                 // We don't have to return to it again
125                 common_set_returnto(null);
126             } else {
127                 $url = common_local_url('all',
128                                         array('nickname' =>
129                                               $nickname));
130             }
131             common_redirect($url);
132         } else {
133             $this->showForm(_('Incorrect username or password.'));
134             return;
135         }
136
137         // success!
138         if (!common_set_user($user)) {
139             $this->serverError(_('Error setting user.'));
140             return;
141         }
142
143         common_real_login(true);
144
145         if ($this->boolean('rememberme')) {
146             common_debug('Adding rememberme cookie for ' . $nickname);
147             common_rememberme($user);
148         }
149         // success!
150         $url = common_get_returnto();
151         if ($url) {
152             // We don't have to return to it again
153             common_set_returnto(null);
154         } else {
155             $url = common_local_url('all',
156                                     array('nickname' =>
157                                           $nickname));
158         }
159         common_redirect($url);
160     }
161
162     /**
163      * Store an error and show the page
164      *
165      * This used to show the whole page; now, it's just a wrapper
166      * that stores the error in an attribute.
167      *
168      * @param string $error error, if any.
169      *
170      * @return void
171      */
172
173     function showForm($error=null)
174     {
175         $this->error = $error;
176         $this->showPage();
177     }
178
179     /**
180      * Title of the page
181      *
182      * @return string title of the page
183      */
184
185     function title()
186     {
187         return _('Login');
188     }
189
190     /**
191      * Show page notice
192      *
193      * Display a notice for how to use the page, or the
194      * error if it exists.
195      *
196      * @return void
197      */
198
199     function showPageNotice()
200     {
201         if ($this->error) {
202             $this->element('p', 'error', $this->error);
203         } else {
204             $instr  = $this->getInstructions();
205             $output = common_markup_to_html($instr);
206
207             $this->raw($output);
208         }
209     }
210
211     /**
212      * Core of the display code
213      *
214      * Shows the login form.
215      *
216      * @return void
217      */
218
219     function showContent()
220     {
221         $this->elementStart('form', array('method' => 'post',
222                                            'id' => 'form_login',
223                                            'class' => 'form_settings',
224                                            'action' => common_local_url('login')));
225         $this->elementStart('fieldset');
226         $this->element('legend', null, _('Login to site'));
227         $this->elementStart('ul', 'form_data');
228         $this->elementStart('li');
229         $this->input('nickname', _('Nickname'));
230         $this->elementEnd('li');
231         $this->elementStart('li');
232         $this->password('password', _('Password'));
233         $this->elementEnd('li');
234         $this->elementStart('li');
235         $this->checkbox('rememberme', _('Remember me'), false,
236                         _('Automatically login in the future; ' .
237                            'not for shared computers!'));
238         $this->elementEnd('li');
239         $this->elementEnd('ul');
240         $this->submit('submit', _('Login'));
241         $this->hidden('token', common_session_token());
242         $this->elementEnd('fieldset');
243         $this->elementEnd('form');
244         $this->elementStart('p');
245         $this->element('a', array('href' => common_local_url('recoverpassword')),
246                        _('Lost or forgotten password?'));
247         $this->elementEnd('p');
248     }
249
250     /**
251      * Instructions for using the form
252      *
253      * For "remembered" logins, we make the user re-login when they
254      * try to change settings. Different instructions for this case.
255      *
256      * @return void
257      */
258
259     function getInstructions()
260     {
261         if (common_logged_in() && !common_is_real_login() &&
262             common_get_returnto()) {
263             // rememberme logins have to reauthenticate before
264             // changing any profile settings (cookie-stealing protection)
265             return _('For security reasons, please re-enter your ' .
266                      'user name and password ' .
267                      'before changing your settings.');
268         } else {
269             return _('Login with your username and password. ' .
270                      'Don\'t have a username yet? ' .
271                      '[Register](%%action.register%%) a new account, or ' .
272                      'try [OpenID](%%action.openidlogin%%). ');
273         }
274     }
275
276     /**
277      * A local menu
278      *
279      * Shows different login/register actions.
280      *
281      * @return void
282      */
283
284     function showLocalNav()
285     {
286         $nav = new LoginGroupNav($this);
287         $nav->show();
288     }
289 }