4 use Friendica\Core\System;
5 use Friendica\Core\Config;
6 use Friendica\Database\DBM;
7 use Friendica\Model\User;
9 require_once 'include/security.php';
10 require_once 'include/datetime.php';
12 // When the "Friendica" cookie is set, take the value to authenticate and renew the cookie.
13 if (isset($_COOKIE["Friendica"])) {
14 $data = json_decode($_COOKIE["Friendica"]);
15 if (isset($data->uid)) {
17 $user = dba::select('user',
22 'account_expired' => false,
23 'account_removed' => false,
29 if (DBM::is_result($user)) {
30 if ($data->hash != cookie_hash($user)) {
31 logger("Hash for user " . $data->uid . " doesn't fit.");
33 goaway(System::baseUrl());
37 // Expires after 7 days by default,
38 // can be set via system.auth_cookie_lifetime
39 $authcookiedays = Config::get('system', 'auth_cookie_lifetime', 7);
40 new_cookie($authcookiedays * 24 * 60 * 60, $user);
42 // Do the authentification if not done by now
43 if (!isset($_SESSION) || !isset($_SESSION['authenticated'])) {
44 authenticate_success($user);
46 if (Config::get('system', 'paranoia')) {
47 $_SESSION['addr'] = $data->ip;
57 if (isset($_SESSION) && x($_SESSION, 'authenticated') && (!x($_POST, 'auth-params') || ($_POST['auth-params'] !== 'login'))) {
58 if ((x($_POST, 'auth-params') && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
59 // process logout request
60 call_hooks("logging_out");
62 info(t('Logged out.') . EOL);
63 goaway(System::baseUrl());
66 if (x($_SESSION, 'visitor_id') && !x($_SESSION, 'uid')) {
67 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
68 intval($_SESSION['visitor_id'])
70 if (DBM::is_result($r)) {
75 if (x($_SESSION, 'uid')) {
76 // already logged in user returning
77 $check = Config::get('system', 'paranoia');
78 // extra paranoia - if the IP changed, log them out
79 if ($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
80 logger('Session address changed. Paranoid setting in effect, blocking session. ' .
81 $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
83 goaway(System::baseUrl());
86 $user = dba::select('user',
89 'uid' => $_SESSION['uid'],
91 'account_expired' => false,
92 'account_removed' => false,
97 if (!DBM::is_result($user)) {
99 goaway(System::baseUrl());
102 // Make sure to refresh the last login time for the user if the user
103 // stays logged in for a long time, e.g. with "Remember Me"
104 $login_refresh = false;
105 if (!x($_SESSION['last_login_date'])) {
106 $_SESSION['last_login_date'] = datetime_convert('UTC', 'UTC');
108 if (strcmp(datetime_convert('UTC', 'UTC', 'now - 12 hours'), $_SESSION['last_login_date']) > 0) {
109 $_SESSION['last_login_date'] = datetime_convert('UTC', 'UTC');
110 $login_refresh = true;
112 authenticate_success($user, false, false, $login_refresh);
117 !(x($_POST, 'password') && strlen($_POST['password']))
119 x($_POST, 'openid_url') && strlen($_POST['openid_url'])
120 || x($_POST, 'username') && strlen($_POST['username'])
123 $noid = Config::get('system', 'no_openid');
125 $openid_url = trim(strlen($_POST['openid_url']) ? $_POST['openid_url'] : $_POST['username']);
127 // validate_url alters the calling parameter
129 $temp_string = $openid_url;
131 // if it's an email address or doesn't resolve to a URL, fail.
133 if ($noid || strpos($temp_string, '@') || !validate_url($temp_string)) {
135 notice(t('Login failed.') . EOL);
136 goaway(System::baseUrl());
140 // Otherwise it's probably an openid.
143 require_once('library/openid.php');
144 $openid = new LightOpenID;
145 $openid->identity = $openid_url;
146 $_SESSION['openid'] = $openid_url;
147 $_SESSION['remember'] = $_POST['remember'];
148 $openid->returnUrl = System::baseUrl(true) . '/openid';
149 goaway($openid->authUrl());
150 } catch (Exception $e) {
151 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());
156 if (x($_POST, 'auth-params') && $_POST['auth-params'] === 'login') {
160 'username' => trim($_POST['username']),
161 'password' => trim($_POST['password']),
162 'authenticated' => 0,
163 'user_record' => null
168 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
169 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
170 * and later plugins should not interfere with an earlier one that succeeded.
173 call_hooks('authenticate', $addon_auth);
175 if ($addon_auth['authenticated'] && count($addon_auth['user_record'])) {
176 $record = $addon_auth['user_record'];
178 $user_id = User::authenticate(trim($_POST['username']), trim($_POST['password']));
180 $record = dba::select('user', [], ['uid' => $user_id], ['limit' => 1]);
184 if (!$record || !count($record)) {
185 logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']);
186 notice(t('Login failed.') . EOL);
187 goaway(System::baseUrl());
190 if (!$_POST['remember']) {
191 new_cookie(0); // 0 means delete on browser exit
194 // if we haven't failed up this point, log them in.
195 $_SESSION['remember'] = $_POST['remember'];
196 $_SESSION['last_login_date'] = datetime_convert('UTC', 'UTC');
197 authenticate_success($record, true, true);
202 * @brief Kills the "Friendica" cookie and all session data
204 function nuke_session()
206 new_cookie(-3600); // make sure cookie is deleted on browser close, as a security measure