diff --git a/lib/Core.php b/lib/Core.php index e01fedc9..ad6d9c7e 100644 --- a/lib/Core.php +++ b/lib/Core.php @@ -68,9 +68,9 @@ if(SITE_STATUS == SITE_STATUS_LIVE){ $GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST); -$GLOBALS['User'] = Session::GetLoggedInUser(); +Session::InitializeFromCookie(); -if($GLOBALS['User'] === null){ +if(Session::$User === null){ $httpBasicAuthLogin = $_SERVER['PHP_AUTH_USER'] ?? null; if($httpBasicAuthLogin !== null){ @@ -83,10 +83,10 @@ if($GLOBALS['User'] === null){ $password = null; } - // Most patrons have a null password, meaning they only need to log in using an email and a blank password. + // Most patrons have a `null` password, meaning they only need to log in using an email and a blank password. // Some users with admin rights need a password to log in. $session->Create($httpBasicAuthLogin, $password); - $GLOBALS['User'] = $session->User; + Session::$User = $session->User; } catch(Exception){ // Do nothing. diff --git a/lib/Session.php b/lib/Session.php index a04cec31..3c017e00 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -3,17 +3,17 @@ use Ramsey\Uuid\Uuid; use Safe\DateTimeImmutable; /** - * @property User $User * @property string $Url */ class Session{ use Traits\Accessor; + public static ?User $User = null; + public int $UserId; public DateTimeImmutable $Created; public string $SessionId; - protected User $_User; public string $_Url; @@ -42,8 +42,8 @@ class Session{ */ public function Create(?string $identifier = null, ?string $password = null): void{ try{ - $this->User = User::GetIfRegistered($identifier, $password); - $this->UserId = $this->User->UserId; + Session::$User = User::GetIfRegistered($identifier, $password); + $this->UserId = Session::$User->UserId; $existingSessions = Db::Query(' SELECT SessionId, @@ -76,26 +76,6 @@ class Session{ } } - public static function GetLoggedInUser(): ?User{ - $sessionId = HttpInput::Str(COOKIE, 'sessionid'); - - if($sessionId !== null){ - $result = Db::Query(' - SELECT u.* - from Users u - inner join Sessions s using (UserId) - where s.SessionId = ? - ', [$sessionId], User::class); - - if(sizeof($result) > 0){ - self::SetSessionCookie($sessionId); - return $result[0]; - } - } - - return null; - } - public static function SetSessionCookie(string $sessionId): void{ /** @throws void */ setcookie('sessionid', $sessionId, ['expires' => intval((new DateTimeImmutable('+1 week'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); // Expires in two weeks @@ -122,4 +102,22 @@ class Session{ return $result[0] ?? throw new Exceptions\SessionNotFoundException(); } + + public static function InitializeFromCookie(): void{ + $sessionId = HttpInput::Str(COOKIE, 'sessionid'); + + if($sessionId !== null){ + $result = Db::Query(' + SELECT u.* + from Users u + inner join Sessions s using (UserId) + where s.SessionId = ? + ', [$sessionId], User::class); + + if(sizeof($result) > 0){ + self::SetSessionCookie($sessionId); + Session::$User = $result[0]; + } + } + } } diff --git a/templates/ArtworkForm.php b/templates/ArtworkForm.php index 64e42e7d..4d6be444 100644 --- a/templates/ArtworkForm.php +++ b/templates/ArtworkForm.php @@ -171,10 +171,10 @@ $isEditForm = $isEditForm ?? false; - if($artwork->CanStatusBeChangedBy($GLOBALS['User'] ?? null) || $artwork->CanEbookUrlBeChangedBy($GLOBALS['User'] ?? null)){ ?> + if($artwork->CanStatusBeChangedBy(Session::$User) || $artwork->CanEbookUrlBeChangedBy(Session::$User)){ ?>