3 * StatusNet, 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@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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
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/
46 class LoginAction extends Action
49 * Has there been an error?
55 * Is this a read-only action?
57 * @return boolean false
60 function isReadOnly($args)
66 * Handle input, produce output
68 * Switches on request method; either shows the form or handles its input.
70 * @param array $args $_REQUEST data
75 function handle($args)
77 parent::handle($args);
79 $disabled = common_config('logincommand','disabled');
80 $disabled = isset($disabled) && $disabled;
82 if (common_is_real_login()) {
83 $this->clientError(_('Already logged in.'));
84 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
86 } else if (!$disabled && isset($args['user_id']) && isset($args['token'])){
87 $this->checkLogin($args['user_id'],$args['token']);
89 common_ensure_session();
95 * Check the login data
97 * Determines if the login data is valid. If so, logs the user
98 * in, and redirects to the 'with friends' page, or to the stored
104 function checkLogin($user_id=null, $token=null)
106 if(isset($token) && isset($user_id)){
107 //Token based login (from the LoginCommand)
108 $login_token = Login_token::staticGet('user_id',$user_id);
109 if($login_token && $login_token->token == $token){
110 if($login_token->modified > time()+2*60){
112 //delete the token as it is useless
113 $login_token->delete();
114 $this->showForm(_('Invalid or expired token.'));
117 //delete the token so it cannot be reused
118 $login_token->delete();
119 //it's a valid token - let them log in
120 $user = User::staticGet('id', $user_id);
121 //$user = User::staticGet('nickname', "candrews");
124 $this->showForm(_('Invalid or expired token.'));
128 // Regular form submission login
130 // XXX: login throttle
132 // CSRF protection - token set in NoticeForm
133 $token = $this->trimmed('token');
134 if (!$token || $token != common_session_token()) {
135 $this->clientError(_('There was a problem with your session token. '.
136 'Try again, please.'));
140 $nickname = $this->trimmed('nickname');
141 $password = $this->arg('password');
143 $user = common_check_user($nickname, $password);
147 $this->showForm(_('Incorrect username or password.'));
152 if (!common_set_user($user)) {
153 $this->serverError(_('Error setting user. You are probably not authorized.'));
157 common_real_login(true);
159 if ($this->boolean('rememberme')) {
160 common_rememberme($user);
163 $url = common_get_returnto();
166 // We don't have to return to it again
167 common_set_returnto(null);
169 $url = common_local_url('all',
174 common_redirect($url, 303);
178 * Store an error and show the page
180 * This used to show the whole page; now, it's just a wrapper
181 * that stores the error in an attribute.
183 * @param string $error error, if any.
188 function showForm($error=null)
190 $this->error = $error;
194 function showScripts()
196 parent::showScripts();
197 $this->autofocus('nickname');
203 * @return string title of the page
214 * Display a notice for how to use the page, or the
215 * error if it exists.
220 function showPageNotice()
223 $this->element('p', 'error', $this->error);
225 $instr = $this->getInstructions();
226 $output = common_markup_to_html($instr);
233 * Core of the display code
235 * Shows the login form.
240 function showContent()
242 $this->elementStart('form', array('method' => 'post',
243 'id' => 'form_login',
244 'class' => 'form_settings',
245 'action' => common_local_url('login')));
246 $this->elementStart('fieldset');
247 $this->element('legend', null, _('Login to site'));
248 $this->elementStart('ul', 'form_data');
249 $this->elementStart('li');
250 $this->input('nickname', _('Nickname'));
251 $this->elementEnd('li');
252 $this->elementStart('li');
253 $this->password('password', _('Password'));
254 $this->elementEnd('li');
255 $this->elementStart('li');
256 $this->checkbox('rememberme', _('Remember me'), false,
257 _('Automatically login in the future; ' .
258 'not for shared computers!'));
259 $this->elementEnd('li');
260 $this->elementEnd('ul');
261 $this->submit('submit', _('Login'));
262 $this->hidden('token', common_session_token());
263 $this->elementEnd('fieldset');
264 $this->elementEnd('form');
265 $this->elementStart('p');
266 $this->element('a', array('href' => common_local_url('recoverpassword')),
267 _('Lost or forgotten password?'));
268 $this->elementEnd('p');
272 * Instructions for using the form
274 * For "remembered" logins, we make the user re-login when they
275 * try to change settings. Different instructions for this case.
280 function getInstructions()
282 if (common_logged_in() && !common_is_real_login() &&
283 common_get_returnto()) {
284 // rememberme logins have to reauthenticate before
285 // changing any profile settings (cookie-stealing protection)
286 return _('For security reasons, please re-enter your ' .
287 'user name and password ' .
288 'before changing your settings.');
290 return _('Login with your username and password. ' .
291 'Don\'t have a username yet? ' .
292 '[Register](%%action.register%%) a new account.');
299 * Shows different login/register actions.
304 function showLocalNav()
306 $nav = new LoginGroupNav($this);