+use Friendica\App;
+use Friendica\Core\Config;
+
+require_once('include/security.php');
+require_once('include/datetime.php');
+
+// When the "Friendica" cookie is set, take the value to authenticate and renew the cookie.
+if (isset($_COOKIE["Friendica"])) {
+ $data = json_decode($_COOKIE["Friendica"]);
+ if (isset($data->uid)) {
+ $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
+ FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
+ intval($data->uid)
+ );
+
+ if ($r) {
+ if ($data->hash != cookie_hash($r[0])) {
+ logger("Hash for user ".$data->uid." doesn't fit.");
+ nuke_session();
+ goaway(z_root());
+ }
+
+ // Renew the cookie
+ // Expires after 7 days by default,
+ // can be set via system.auth_cookie_lifetime
+ $authcookiedays = Config::get('system', 'auth_cookie_lifetime', 7);
+ new_cookie($authcookiedays*24*60*60, $r[0]);
+
+ // Do the authentification if not done by now
+ if (!isset($_SESSION) OR !isset($_SESSION['authenticated'])) {
+ authenticate_success($r[0]);
+
+ if (get_config('system','paranoia'))
+ $_SESSION['addr'] = $data->ip;
+ }
+ }
+ }
+}
+
+
+// login/logout
+
+if (isset($_SESSION) && x($_SESSION,'authenticated') && (!x($_POST,'auth-params') || ($_POST['auth-params'] !== 'login'))) {