3 use \Friendica\Core\Config;
5 require_once('include/security.php');
6 require_once('include/datetime.php');
8 // When the "Friendica" cookie is set, take the value to authenticate and renew the cookie.
9 if (isset($_COOKIE["Friendica"])) {
10 $data = json_decode($_COOKIE["Friendica"]);
11 if (isset($data->uid)) {
12 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
13 FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
18 if ($data->hash != cookie_hash($r[0])) {
19 logger("Hash for user ".$data->uid." doesn't fit.");
25 // Expires after 7 days by default,
26 // can be set via system.auth_cookie_lifetime
27 $authcookiedays = Config::get('system', 'auth_cookie_lifetime', 7);
28 new_cookie($authcookiedays*24*60*60, $r[0]);
30 // Do the authentification if not done by now
31 if (!isset($_SESSION) OR !isset($_SESSION['authenticated'])) {
32 authenticate_success($r[0]);
34 if (get_config('system','paranoia'))
35 $_SESSION['addr'] = $data->ip;
44 if (isset($_SESSION) && x($_SESSION,'authenticated') && (!x($_POST,'auth-params') || ($_POST['auth-params'] !== 'login'))) {
46 if ((x($_POST,'auth-params') && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
48 // process logout request
49 call_hooks("logging_out");
51 info(t('Logged out.').EOL);
55 if (x($_SESSION,'visitor_id') && !x($_SESSION,'uid')) {
56 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
57 intval($_SESSION['visitor_id'])
59 if (dbm::is_result($r)) {
64 if (x($_SESSION,'uid')) {
66 // already logged in user returning
68 $check = get_config('system','paranoia');
69 // extra paranoia - if the IP changed, log them out
70 if ($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
71 logger('Session address changed. Paranoid setting in effect, blocking session. '.
72 $_SESSION['addr'].' != '.$_SERVER['REMOTE_ADDR']);
77 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
78 FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
79 intval($_SESSION['uid'])
82 if (!dbm::is_result($r)) {
87 // Make sure to refresh the last login time for the user if the user
88 // stays logged in for a long time, e.g. with "Remember Me"
89 $login_refresh = false;
90 if (!x($_SESSION['last_login_date'])) {
91 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
93 if (strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0) {
95 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
96 $login_refresh = true;
98 authenticate_success($r[0], false, false, $login_refresh);
104 if (x($_POST,'password') && strlen($_POST['password']))
105 $encrypted = hash('whirlpool',trim($_POST['password']));
107 if ((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
108 (x($_POST,'username')) && strlen($_POST['username'])) {
110 $noid = get_config('system','no_openid');
112 $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']));
114 // validate_url alters the calling parameter
116 $temp_string = $openid_url;
118 // if it's an email address or doesn't resolve to a URL, fail.
120 if ($noid || strpos($temp_string,'@') || !validate_url($temp_string)) {
122 notice(t('Login failed.').EOL);
127 // Otherwise it's probably an openid.
130 require_once('library/openid.php');
131 $openid = new LightOpenID;
132 $openid->identity = $openid_url;
133 $_SESSION['openid'] = $openid_url;
134 $_SESSION['remember'] = $_POST['remember'];
135 $openid->returnUrl = App::get_baseurl(true).'/openid';
136 goaway($openid->authUrl());
137 } catch (Exception $e) {
138 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());
144 if (x($_POST,'auth-params') && $_POST['auth-params'] === 'login') {
149 'username' => trim($_POST['username']),
150 'password' => trim($_POST['password']),
151 'authenticated' => 0,
152 'user_record' => null
157 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
158 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
159 * and later plugins should not interfere with an earlier one that succeeded.
163 call_hooks('authenticate', $addon_auth);
165 if ($addon_auth['authenticated'] && count($addon_auth['user_record']))
166 $record = $addon_auth['user_record'];
169 // process normal login request
171 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
172 FROM `user` WHERE (`email` = '%s' OR `nickname` = '%s')
173 AND `password` = '%s' AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
174 dbesc(trim($_POST['username'])),
175 dbesc(trim($_POST['username'])),
178 if (dbm::is_result($r))
182 if (!$record || !count($record)) {
183 logger('authenticate: failed login attempt: '.notags(trim($_POST['username'])).' from IP '.$_SERVER['REMOTE_ADDR']);
184 notice(t('Login failed.').EOL);
188 if (! $_POST['remember']) {
189 new_cookie(0); // 0 means delete on browser exit
192 // if we haven't failed up this point, log them in.
193 $_SESSION['remember'] = $_POST['remember'];
194 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
195 authenticate_success($record, true, true);
200 * @brief Kills the "Friendica" cookie and all session data
202 function nuke_session() {
204 new_cookie(-3600); // make sure cookie is deleted on browser close, as a security measure