4 use Friendica\Core\Config;
6 require_once('include/security.php');
7 require_once('include/datetime.php');
9 // When the "Friendica" cookie is set, take the value to authenticate and renew the cookie.
10 if (isset($_COOKIE["Friendica"])) {
11 $data = json_decode($_COOKIE["Friendica"]);
12 if (isset($data->uid)) {
13 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
14 FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
19 if ($data->hash != cookie_hash($r[0])) {
20 logger("Hash for user ".$data->uid." doesn't fit.");
26 // Expires after 7 days by default,
27 // can be set via system.auth_cookie_lifetime
28 $authcookiedays = Config::get('system', 'auth_cookie_lifetime', 7);
29 new_cookie($authcookiedays*24*60*60, $r[0]);
31 // Do the authentification if not done by now
32 if (!isset($_SESSION) OR !isset($_SESSION['authenticated'])) {
33 authenticate_success($r[0]);
35 if (get_config('system','paranoia'))
36 $_SESSION['addr'] = $data->ip;
45 if (isset($_SESSION) && x($_SESSION,'authenticated') && (!x($_POST,'auth-params') || ($_POST['auth-params'] !== 'login'))) {
47 if ((x($_POST,'auth-params') && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
49 // process logout request
50 call_hooks("logging_out");
52 info(t('Logged out.').EOL);
56 if (x($_SESSION,'visitor_id') && !x($_SESSION,'uid')) {
57 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
58 intval($_SESSION['visitor_id'])
60 if (dbm::is_result($r)) {
65 if (x($_SESSION,'uid')) {
67 // already logged in user returning
69 $check = get_config('system','paranoia');
70 // extra paranoia - if the IP changed, log them out
71 if ($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
72 logger('Session address changed. Paranoid setting in effect, blocking session. '.
73 $_SESSION['addr'].' != '.$_SERVER['REMOTE_ADDR']);
78 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
79 FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
80 intval($_SESSION['uid'])
83 if (!dbm::is_result($r)) {
88 // Make sure to refresh the last login time for the user if the user
89 // stays logged in for a long time, e.g. with "Remember Me"
90 $login_refresh = false;
91 if (!x($_SESSION['last_login_date'])) {
92 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
94 if (strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0) {
96 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
97 $login_refresh = true;
99 authenticate_success($r[0], false, false, $login_refresh);
105 if (x($_POST,'password') && strlen($_POST['password']))
106 $encrypted = hash('whirlpool',trim($_POST['password']));
108 if ((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
109 (x($_POST,'username')) && strlen($_POST['username'])) {
111 $noid = get_config('system','no_openid');
113 $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']));
115 // validate_url alters the calling parameter
117 $temp_string = $openid_url;
119 // if it's an email address or doesn't resolve to a URL, fail.
121 if ($noid || strpos($temp_string,'@') || !validate_url($temp_string)) {
123 notice(t('Login failed.').EOL);
128 // Otherwise it's probably an openid.
131 require_once('library/openid.php');
132 $openid = new LightOpenID;
133 $openid->identity = $openid_url;
134 $_SESSION['openid'] = $openid_url;
135 $_SESSION['remember'] = $_POST['remember'];
136 $openid->returnUrl = App::get_baseurl(true).'/openid';
137 goaway($openid->authUrl());
138 } catch (Exception $e) {
139 notice(t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.').'<br /><br >'.t('The error message was:').' '.$e->getMessage());
145 if (x($_POST,'auth-params') && $_POST['auth-params'] === 'login') {
150 'username' => trim($_POST['username']),
151 'password' => trim($_POST['password']),
152 'authenticated' => 0,
153 'user_record' => null
158 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
159 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
160 * and later plugins should not interfere with an earlier one that succeeded.
164 call_hooks('authenticate', $addon_auth);
166 if ($addon_auth['authenticated'] && count($addon_auth['user_record']))
167 $record = $addon_auth['user_record'];
170 // process normal login request
172 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
173 FROM `user` WHERE (`email` = '%s' OR `nickname` = '%s')
174 AND `password` = '%s' AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
175 dbesc(trim($_POST['username'])),
176 dbesc(trim($_POST['username'])),
179 if (dbm::is_result($r))
183 if (!$record || !count($record)) {
184 logger('authenticate: failed login attempt: '.notags(trim($_POST['username'])).' from IP '.$_SERVER['REMOTE_ADDR']);
185 notice(t('Login failed.').EOL);
189 if (! $_POST['remember']) {
190 new_cookie(0); // 0 means delete on browser exit
193 // if we haven't failed up this point, log them in.
194 $_SESSION['remember'] = $_POST['remember'];
195 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
196 authenticate_success($record, true, true);
201 * @brief Kills the "Friendica" cookie and all session data
203 function nuke_session() {
205 new_cookie(-3600); // make sure cookie is deleted on browser close, as a security measure