addData([ 'me' => $me, 'whoami' => "$me->username#$me->userDiscriminator", 'noStatus' => false, 'isLogin' => $isLogin, 'isAllowed' => $isLogin && $me !== null && static::isAllowed($me->userId), ]); return $templates; } public static function getU(): ?Token { return Auth::decrypt($_COOKIE['u'] ?? ''); } public static function checkU(): bool { $token = static::getU(); if ($token === null) return false; if ($token->timestamp + $token->expires_in < time()) return false; return static::me() !== null; } public static function setU(Token $token): void { try { setcookie('u', Auth::encrypt($token), [ 'expires' => $token->timestamp + $token->expires_in, 'path' => '/', 'domain' => 'akanyan.oho.tw', 'samesite' => 'None', 'secure' => true, 'httponly' => true, ]); } catch (Exception) { error_log('Failed to setU.'); } } public static function unsetU(): void { Redis::unsetMe($_COOKIE['u'] ?? ''); try { setcookie('u', null, [ 'expires' => time() - 3600, 'path' => '/', 'domain' => 'akanyan.oho.tw', 'samesite' => 'None', 'secure' => true, 'httponly' => true, ]); } catch (Exception) { error_log('Failed to unsetU.'); } } public static function requireAuth(): void { if (!static::checkU()) { header('location: /login.php'); http_response_code(302); exit; } } public static function requireNonAuth(): void { if (static::checkU()) { header('location: /'); http_response_code(302); exit; } } public static function requireAllowed(): void { static::requireAuth(); $me = static::me(); if ($me === null || !static::isAllowed($me->userId)) { static::template([ 'title' => '您無權限使用本系統', 'body' => <<
您的 Discord 帳號不在白名單中。
若您認為這是個錯誤,請聯絡 小喵#3521 並提供您的使用者編號 $me->userId。
$me->username#$me->userDiscriminator 登入,點選此處以登出系統。
HTML;
}
$html = <<