login.php 4.12 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
Evan Prodromou's avatar
Evan Prodromou committed
2
/*
3 4
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, Controlez-Vous, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
5
 *
6 7 8 9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Evan Prodromou's avatar
Evan Prodromou committed
10
 *
11 12 13 14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
Evan Prodromou's avatar
Evan Prodromou committed
15
 *
16 17 18 19
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('LACONICA')) { exit(1); }
Evan Prodromou's avatar
Evan Prodromou committed
21

Evan Prodromou's avatar
Evan Prodromou committed
22
class LoginAction extends Action {
Evan Prodromou's avatar
Evan Prodromou committed
23

24 25 26 27
	function is_readonly() {
		return true;
	}

Evan Prodromou's avatar
Evan Prodromou committed
28 29
	function handle($args) {
		parent::handle($args);
30
		if (common_is_real_login()) {
31
			common_user_error(_('Already logged in.'));
Evan Prodromou's avatar
Evan Prodromou committed
32
		} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
Evan Prodromou's avatar
Evan Prodromou committed
33
			$this->check_login();
Evan Prodromou's avatar
Evan Prodromou committed
34
		} else {
Evan Prodromou's avatar
Evan Prodromou committed
35
			$this->show_form();
Evan Prodromou's avatar
Evan Prodromou committed
36
		}
Evan Prodromou's avatar
Evan Prodromou committed
37
	}
38

Evan Prodromou's avatar
Evan Prodromou committed
39 40 41 42 43
	function check_login() {
		# XXX: form token in $_SESSION to prevent XSS
		# XXX: login throttle
		$nickname = $this->arg('nickname');
		$password = $this->arg('password');
44 45 46
		if (common_check_user($nickname, $password)) {
			# success!
			if (!common_set_user($nickname)) {
47
				common_server_error(_('Error setting user.'));
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
				return;
			}
			common_real_login(true);
			if ($this->boolean('rememberme')) {
				common_debug('Adding rememberme cookie for ' . $nickname);
				common_rememberme();
			}
			# success!
			$url = common_get_returnto();
			if ($url) {
				# We don't have to return to it again
				common_set_returnto(NULL);
			} else {
				$url = common_local_url('all',
										array('nickname' =>
											  $nickname));
			}
			common_redirect($url);
		} else {
67 68 69 70 71 72
			$this->show_form(_('Incorrect username or password.'));
			return;
		}

		# success!
		if (!common_set_user($user)) {
73
			common_server_error(_('Error setting user.'));
74
			return;
75
		}
76

77
		common_real_login(true);
78

79 80 81 82 83 84 85 86 87 88 89 90 91
		if ($this->boolean('rememberme')) {
			common_debug('Adding rememberme cookie for ' . $nickname);
			common_rememberme($user);
		}
		# success!
		$url = common_get_returnto();
		if ($url) {
			# We don't have to return to it again
			common_set_returnto(NULL);
		} else {
			$url = common_local_url('all',
									array('nickname' =>
										  $nickname));
Evan Prodromou's avatar
Evan Prodromou committed
92
		}
93
		common_redirect($url);
Evan Prodromou's avatar
Evan Prodromou committed
94
	}
Evan Prodromou's avatar
Evan Prodromou committed
95

Evan Prodromou's avatar
Evan Prodromou committed
96
	function show_form($error=NULL) {
97
		common_show_header(_('Login'), NULL, $error, array($this, 'show_top'));
Evan Prodromou's avatar
Evan Prodromou committed
98
		common_element_start('form', array('method' => 'post',
Evan Prodromou's avatar
Evan Prodromou committed
99 100
										   'id' => 'login',
										   'action' => common_local_url('login')));
101 102 103 104
		common_input('nickname', _('Nickname'));
		common_password('password', _('Password'));
		common_checkbox('rememberme', _('Remember me'), false,
		                _('Automatically login in the future; ' .
105
		                   'not for shared computers!'));
106
		common_submit('submit', _('Login'));
107
		common_element_end('form');
108 109
		common_element_start('p');
		common_element('a', array('href' => common_local_url('recoverpassword')),
110
					   _('Lost or forgotten password?'));
111
		common_element_end('p');
112
		common_show_footer();
Evan Prodromou's avatar
Evan Prodromou committed
113
	}
114

115
	function get_instructions() {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		if (common_logged_in() &&
			!common_is_real_login() &&
			common_get_returnto())
		{
			# rememberme logins have to reauthenticate before
			# changing any profile settings (cookie-stealing protection)
			return _('For security reasons, please re-enter your ' .
					 'user name and password ' .
					 'before changing your settings.');
		} else {
			return _('Login with your username and password. ' .
					 'Don\'t have a username yet? ' .
					 '[Register](%%action.register%%) a new account, or ' .
					 'try [OpenID](%%action.openidlogin%%). ');
		}
131 132
	}

133 134 135 136
	function show_top($error=NULL) {
		if ($error) {
			common_element('p', 'error', $error);
		} else {
137 138
			$instr = $this->get_instructions();
			$output = common_markup_to_html($instr);
139
			common_element_start('div', 'instructions');
140
			common_raw($output);
141
			common_element_end('div');
142 143
		}
	}
Evan Prodromou's avatar
Evan Prodromou committed
144
}