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