3 * Laconica, the distributed open-source microblogging tool
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.
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.
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/>.
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/
30 if (!defined('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/
44 class LoginAction extends Action
47 * Has there been an error?
53 * Is this a read-only action?
55 * @return boolean false
58 function isReadOnly($args)
64 * Handle input, produce output
66 * Switches on request method; either shows the form or handles its input.
68 * @param array $args $_REQUEST data
73 function handle($args)
75 parent::handle($args);
76 if (common_is_real_login()) {
77 $this->clientError(_('Already logged in.'));
78 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
81 common_ensure_session();
87 * Check the login data
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
98 // XXX: login throttle
100 // CSRF protection - token set in NoticeForm
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.'));
108 $nickname = common_canonical_nickname($this->trimmed('nickname'));
109 $password = $this->arg('password');
111 $user = common_check_user($nickname, $password);
114 $this->showForm(_('Incorrect username or password.'));
119 if (!common_set_user($user)) {
120 $this->serverError(_('Error setting user.'));
124 common_real_login(true);
126 if ($this->boolean('rememberme')) {
127 common_rememberme($user);
130 $url = common_get_returnto();
133 // We don't have to return to it again
134 common_set_returnto(null);
136 $url = common_local_url('all',
141 common_redirect($url, 303);
145 * Store an error and show the page
147 * This used to show the whole page; now, it's just a wrapper
148 * that stores the error in an attribute.
150 * @param string $error error, if any.
155 function showForm($error=null)
157 $this->error = $error;
164 * @return string title of the page
175 * Display a notice for how to use the page, or the
176 * error if it exists.
181 function showPageNotice()
184 $this->element('p', 'error', $this->error);
186 $instr = $this->getInstructions();
187 $output = common_markup_to_html($instr);
194 * Core of the display code
196 * Shows the login form.
201 function showContent()
203 $this->elementStart('form', array('method' => 'post',
204 'id' => 'form_login',
205 'class' => 'form_settings',
206 'action' => common_local_url('login')));
207 $this->elementStart('fieldset');
208 $this->element('legend', null, _('Login to site'));
209 $this->elementStart('ul', 'form_data');
210 $this->elementStart('li');
211 $this->input('nickname', _('Nickname'));
212 $this->elementEnd('li');
213 $this->elementStart('li');
214 $this->password('password', _('Password'));
215 $this->elementEnd('li');
216 $this->elementStart('li');
217 $this->checkbox('rememberme', _('Remember me'), false,
218 _('Automatically login in the future; ' .
219 'not for shared computers!'));
220 $this->elementEnd('li');
221 $this->elementEnd('ul');
222 $this->submit('submit', _('Login'));
223 $this->hidden('token', common_session_token());
224 $this->elementEnd('fieldset');
225 $this->elementEnd('form');
226 $this->elementStart('p');
227 $this->element('a', array('href' => common_local_url('recoverpassword')),
228 _('Lost or forgotten password?'));
229 $this->elementEnd('p');
233 * Instructions for using the form
235 * For "remembered" logins, we make the user re-login when they
236 * try to change settings. Different instructions for this case.
241 function getInstructions()
243 if (common_logged_in() && !common_is_real_login() &&
244 common_get_returnto()) {
245 // rememberme logins have to reauthenticate before
246 // changing any profile settings (cookie-stealing protection)
247 return _('For security reasons, please re-enter your ' .
248 'user name and password ' .
249 'before changing your settings.');
251 return _('Login with your username and password. ' .
252 'Don\'t have a username yet? ' .
253 '[Register](%%action.register%%) a new account, or ' .
254 'try [OpenID](%%action.openidlogin%%). ');
261 * Shows different login/register actions.
266 function showLocalNav()
268 $nav = new LoginGroupNav($this);