$_SESSION["login_code"]]);
        if ($uidinfo["status"] == "ERROR") {
            throw new Exception();
        }
        if (is_numeric($uidinfo['uid'])) {
            $user = new User($uidinfo['uid'] * 1);
            foreach ($SETTINGS['permissions'] as $perm) {
                if (!$user->hasPermission($perm)) {
                    showHTML("no access permission", "sign out", "./action.php?action=signout");
                    die();
                }
            }
            Session::start($user);
            $_SESSION["login_code"] = null;
            header('Location: app.php');
            showHTML("Logged in", "Continue", "./app.php");
            die();
        } else {
            throw new Exception();
        }
    } catch (Exception $ex) {
        $redirecttologin = true;
    }
}
if ($redirecttologin) {
    try {
        $urlbase = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . (($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) ? ":" . $_SERVER['SERVER_PORT'] : "");
        $iconurl = $urlbase . str_replace("index.php", "", $_SERVER["REQUEST_URI"]) . "static/img/logo.svg";
        $codedata = AccountHubApi::get("getloginkey", ["appname" => $SETTINGS["site_title"], "appicon" => $iconurl]);
        if ($codedata['status'] != "OK") {
            throw new Exception($Strings->get("login server unavailable", false));
        }
        $redirecturl = $urlbase . $_SERVER['REQUEST_URI'];
        $_SESSION["login_code"] = $codedata["code"];
        $locationurl = $codedata["loginurl"] . "?code=" . htmlentities($codedata["code"]) . "&redirect=" . htmlentities($redirecturl);
        header("Location: $locationurl");
        showHTML("Continue", "Continue", $locationurl);
        die();
    } catch (Exception $ex) {
        sendError($ex->getMessage());
    }
}