From 14491489898d0cba23c5f7f8a610c7cd0f9bce04 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sun, 10 Nov 2024 22:37:59 -0600 Subject: [PATCH] Switch logged in user to static typed variable instead of in $GLOBALS --- lib/Core.php | 8 +++--- lib/Session.php | 46 +++++++++++++++---------------- templates/ArtworkForm.php | 6 ++-- templates/DonationAlert.php | 4 +-- templates/DonationProgress.php | 2 +- templates/FeedHowTo.php | 4 +-- www/artists/get.php | 4 +-- www/artworks/edit.php | 4 +-- www/artworks/get.php | 18 ++++++------ www/artworks/index.php | 6 ++-- www/artworks/new.php | 6 ++-- www/artworks/post.php | 22 +++++++-------- www/bulk-downloads/collection.php | 2 +- www/bulk-downloads/download.php | 4 +-- www/bulk-downloads/get.php | 2 +- www/bulk-downloads/index.php | 2 +- www/ebooks/download.php | 2 +- www/feeds/atom/index.php | 3 +- www/feeds/collection.php | 2 +- www/feeds/download.php | 4 +-- www/feeds/get.php | 2 +- www/feeds/index.php | 2 +- www/polls/get.php | 14 ++++------ www/polls/votes/new.php | 8 +++--- www/sessions/new.php | 2 +- 25 files changed, 88 insertions(+), 91 deletions(-) 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; -CanStatusBeChangedBy($GLOBALS['User'] ?? null) || $artwork->CanEbookUrlBeChangedBy($GLOBALS['User'] ?? null)){ ?> +CanStatusBeChangedBy(Session::$User) || $artwork->CanEbookUrlBeChangedBy(Session::$User)){ ?>
Editor options - CanStatusBeChangedBy($GLOBALS['User'] ?? null)){ ?> + CanStatusBeChangedBy(Session::$User)){ ?> - CanEbookUrlBeChangedBy($GLOBALS['User'] ?? null)){ ?> + CanEbookUrlBeChangedBy(Session::$User)){ ?>