From 110c091a7b17d862d47d6b9fc82e9e2a0420af67 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sun, 12 May 2024 12:29:30 -0500 Subject: [PATCH] Define some constants to make HTTP input code less wordy --- lib/Artwork.php | 32 ++++++++++++------------ lib/Constants.php | 6 +++++ lib/Session.php | 7 ++---- www/artists/get.php | 2 +- www/artworks/edit.php | 2 +- www/artworks/get.php | 4 +-- www/artworks/index.php | 12 ++++----- www/artworks/new.php | 2 +- www/artworks/post.php | 10 ++++---- www/bulk-downloads/collection.php | 2 +- www/bulk-downloads/download.php | 2 +- www/bulk-downloads/get.php | 4 +-- www/collections/get.php | 2 +- www/ebooks/author.php | 2 +- www/ebooks/download.php | 4 +-- www/ebooks/ebook.php | 2 +- www/ebooks/index.php | 12 ++++----- www/feeds/atom/search.php | 2 +- www/feeds/collection.php | 6 ++--- www/feeds/download.php | 2 +- www/feeds/get.php | 4 +-- www/feeds/opds/search.php | 2 +- www/feeds/rss/search.php | 2 +- www/manual/index.php | 2 +- www/newsletter/subscriptions/confirm.php | 2 +- www/newsletter/subscriptions/delete.php | 2 +- www/newsletter/subscriptions/get.php | 2 +- www/newsletter/subscriptions/post.php | 12 ++++----- www/polls/get.php | 2 +- www/polls/votes/get.php | 2 +- www/polls/votes/index.php | 2 +- www/polls/votes/new.php | 2 +- www/polls/votes/post.php | 6 ++--- www/sessions/new.php | 4 +-- www/sessions/post.php | 6 ++--- www/settings/post.php | 4 +-- 36 files changed, 87 insertions(+), 86 deletions(-) diff --git a/lib/Artwork.php b/lib/Artwork.php index 8da1c357..c5efb7b5 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -876,23 +876,23 @@ class Artwork{ $artwork = new Artwork(); $artwork->Artist = new Artist(); - $artwork->Artist->Name = HttpInput::Str(HttpVariableSource::Post, 'artist-name'); - $artwork->Artist->DeathYear = HttpInput::Int(HttpVariableSource::Post, 'artist-year-of-death'); + $artwork->Artist->Name = HttpInput::Str(POST, 'artist-name'); + $artwork->Artist->DeathYear = HttpInput::Int(POST, 'artist-year-of-death'); - $artwork->Name = HttpInput::Str(HttpVariableSource::Post, 'artwork-name'); - $artwork->CompletedYear = HttpInput::Int(HttpVariableSource::Post, 'artwork-year'); - $artwork->CompletedYearIsCirca = HttpInput::Bool(HttpVariableSource::Post, 'artwork-year-is-circa') ?? false; - $artwork->Tags = HttpInput::Str(HttpVariableSource::Post, 'artwork-tags') ?? []; - $artwork->Status = ArtworkStatus::tryFrom(HttpInput::Str(HttpVariableSource::Post, 'artwork-status') ?? '') ?? ArtworkStatus::Unverified; - $artwork->EbookUrl = HttpInput::Str(HttpVariableSource::Post, 'artwork-ebook-url'); - $artwork->IsPublishedInUs = HttpInput::Bool(HttpVariableSource::Post, 'artwork-is-published-in-us') ?? false; - $artwork->PublicationYear = HttpInput::Int(HttpVariableSource::Post, 'artwork-publication-year'); - $artwork->PublicationYearPageUrl = HttpInput::Str(HttpVariableSource::Post, 'artwork-publication-year-page-url'); - $artwork->CopyrightPageUrl = HttpInput::Str(HttpVariableSource::Post, 'artwork-copyright-page-url'); - $artwork->ArtworkPageUrl = HttpInput::Str(HttpVariableSource::Post, 'artwork-artwork-page-url'); - $artwork->MuseumUrl = HttpInput::Str(HttpVariableSource::Post, 'artwork-museum-url'); - $artwork->Exception = HttpInput::Str(HttpVariableSource::Post, 'artwork-exception'); - $artwork->Notes = HttpInput::Str(HttpVariableSource::Post, 'artwork-notes'); + $artwork->Name = HttpInput::Str(POST, 'artwork-name'); + $artwork->CompletedYear = HttpInput::Int(POST, 'artwork-year'); + $artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa') ?? false; + $artwork->Tags = HttpInput::Str(POST, 'artwork-tags') ?? []; + $artwork->Status = ArtworkStatus::tryFrom(HttpInput::Str(POST, 'artwork-status') ?? '') ?? ArtworkStatus::Unverified; + $artwork->EbookUrl = HttpInput::Str(POST, 'artwork-ebook-url'); + $artwork->IsPublishedInUs = HttpInput::Bool(POST, 'artwork-is-published-in-us') ?? false; + $artwork->PublicationYear = HttpInput::Int(POST, 'artwork-publication-year'); + $artwork->PublicationYearPageUrl = HttpInput::Str(POST, 'artwork-publication-year-page-url'); + $artwork->CopyrightPageUrl = HttpInput::Str(POST, 'artwork-copyright-page-url'); + $artwork->ArtworkPageUrl = HttpInput::Str(POST, 'artwork-artwork-page-url'); + $artwork->MuseumUrl = HttpInput::Str(POST, 'artwork-museum-url'); + $artwork->Exception = HttpInput::Str(POST, 'artwork-exception'); + $artwork->Notes = HttpInput::Str(POST, 'artwork-notes'); return $artwork; } diff --git a/lib/Constants.php b/lib/Constants.php index 92f3c682..c37cb5e9 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -44,6 +44,12 @@ const ARTWORK_IMAGE_MINIMUM_HEIGHT = 300; const CAPTCHA_IMAGE_HEIGHT = 72; const CAPTCHA_IMAGE_WIDTH = 230; +// These are defined for convenience, so that getting HTTP input isn't so wordy +const GET = HttpVariableSource::Get; +const POST = HttpVariableSource::Post; +const SESSION = HttpVariableSource::Session; +const COOKIE = HttpVariableSource::Cookie; + define('NO_REPLY_EMAIL_ADDRESS', get_cfg_var('se.secrets.email.no_reply_address')); define('ADMIN_EMAIL_ADDRESS', get_cfg_var('se.secrets.email.admin_address')); define('EDITOR_IN_CHIEF_EMAIL_ADDRESS', get_cfg_var('se.secrets.email.editor_in_chief_address')); diff --git a/lib/Session.php b/lib/Session.php index 6a3b212e..75a52b5a 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -1,9 +1,6 @@ SessionId); } catch(Exceptions\UserNotFoundException){ - throw new InvalidLoginException(); + throw new Exceptions\InvalidLoginException(); } } public static function GetLoggedInUser(): ?User{ - $sessionId = HttpInput::Str(HttpVariableSource::Cookie, 'sessionid'); + $sessionId = HttpInput::Str(COOKIE, 'sessionid'); if($sessionId !== null){ $result = Db::Query(' diff --git a/www/artists/get.php b/www/artists/get.php index 9e0ce16c..5bb500f1 100644 --- a/www/artists/get.php +++ b/www/artists/get.php @@ -14,7 +14,7 @@ if($isSubmitterView){ } try{ - $artworks = Library::GetArtworksByArtist(HttpInput::Str(HttpVariableSource::Get, 'artist-url-name'), $filterArtworkStatus, $submitterUserId); + $artworks = Library::GetArtworksByArtist(HttpInput::Str(GET, 'artist-url-name'), $filterArtworkStatus, $submitterUserId); if(sizeof($artworks) == 0){ throw new Exceptions\ArtistNotFoundException(); diff --git a/www/artworks/edit.php b/www/artworks/edit.php index bd6a53be..d99efa7f 100644 --- a/www/artworks/edit.php +++ b/www/artworks/edit.php @@ -13,7 +13,7 @@ try{ } if($artwork === null){ - $artwork = Artwork::GetByUrl(HttpInput::Str(HttpVariableSource::Get, 'artist-url-name'), HttpInput::Str(HttpVariableSource::Get, 'artwork-url-name')); + $artwork = Artwork::GetByUrl(HttpInput::Str(GET, 'artist-url-name'), HttpInput::Str(GET, 'artwork-url-name')); } if(!$artwork->CanBeEditedBy($GLOBALS['User'])){ diff --git a/www/artworks/get.php b/www/artworks/get.php index 2e13a652..bc2a4d9b 100644 --- a/www/artworks/get.php +++ b/www/artworks/get.php @@ -3,11 +3,11 @@ use function Safe\session_unset; session_start(); -$saved = HttpInput::Bool(HttpVariableSource::Session, 'artwork-saved') ?? false; +$saved = HttpInput::Bool(SESSION, 'artwork-saved') ?? false; $exception = $_SESSION['exception'] ?? null; try{ - $artwork = Artwork::GetByUrl(HttpInput::Str(HttpVariableSource::Get, 'artist-url-name'), HttpInput::Str(HttpVariableSource::Get, 'artwork-url-name')); + $artwork = Artwork::GetByUrl(HttpInput::Str(GET, 'artist-url-name'), HttpInput::Str(GET, 'artwork-url-name')); $isReviewerView = $GLOBALS['User']->Benefits->CanReviewArtwork ?? false; $isAdminView = $GLOBALS['User']->Benefits->CanReviewOwnArtwork ?? false; diff --git a/www/artworks/index.php b/www/artworks/index.php index 1dfcc52d..fefeaa8f 100644 --- a/www/artworks/index.php +++ b/www/artworks/index.php @@ -1,11 +1,11 @@ CanBeEditedBy($GLOBALS['User'])){ throw new Exceptions\InvalidPermissionsException(); @@ -67,7 +67,7 @@ try{ $artwork->SubmitterUserId = $originalArtwork->SubmitterUserId; $artwork->Status = $originalArtwork->Status; // Overwrite any value got from POST because we need permission to change the status - $newStatus = ArtworkStatus::tryFrom(HttpInput::Str(HttpVariableSource::Post, 'artwork-status') ?? ''); + $newStatus = ArtworkStatus::tryFrom(HttpInput::Str(POST, 'artwork-status') ?? ''); if($newStatus !== null){ if($originalArtwork->Status != $newStatus && !$originalArtwork->CanStatusBeChangedBy($GLOBALS['User'])){ throw new Exceptions\InvalidPermissionsException(); @@ -102,13 +102,13 @@ try{ // PATCHing a new artwork if($httpMethod == HttpMethod::Patch){ - $artwork = Artwork::GetByUrl(HttpInput::Str(HttpVariableSource::Get, 'artist-url-name'), HttpInput::Str(HttpVariableSource::Get, 'artwork-url-name')); + $artwork = Artwork::GetByUrl(HttpInput::Str(GET, 'artist-url-name'), HttpInput::Str(GET, 'artwork-url-name')); $exceptionRedirectUrl = $artwork->Url; // We can PATCH the status, the ebook www filesystem path, or both. if(isset($_POST['artwork-status'])){ - $newStatus = ArtworkStatus::tryFrom(HttpInput::Str(HttpVariableSource::Post, 'artwork-status') ?? ''); + $newStatus = ArtworkStatus::tryFrom(HttpInput::Str(POST, 'artwork-status') ?? ''); if($newStatus !== null){ if($artwork->Status != $newStatus && !$artwork->CanStatusBeChangedBy($GLOBALS['User'])){ throw new Exceptions\InvalidPermissionsException(); @@ -121,7 +121,7 @@ try{ } if(isset($_POST['artwork-ebook-url'])){ - $newEbookUrl = HttpInput::Str(HttpVariableSource::Post, 'artwork-ebook-url'); + $newEbookUrl = HttpInput::Str(POST, 'artwork-ebook-url'); if($artwork->EbookUrl != $newEbookUrl && !$artwork->CanEbookUrlBeChangedBy($GLOBALS['User'])){ throw new Exceptions\InvalidPermissionsException(); } diff --git a/www/bulk-downloads/collection.php b/www/bulk-downloads/collection.php index ccf1eb2e..9749a34d 100644 --- a/www/bulk-downloads/collection.php +++ b/www/bulk-downloads/collection.php @@ -3,7 +3,7 @@ use function Safe\apcu_fetch; use function Safe\preg_replace; $canDownload = false; -$class = HttpInput::Str(HttpVariableSource::Get, 'class'); +$class = HttpInput::Str(GET, 'class'); if($class === null || ($class != 'authors' && $class != 'collections' && $class != 'subjects' && $class != 'months')){ Template::Emit404(); diff --git a/www/bulk-downloads/download.php b/www/bulk-downloads/download.php index 63041e0a..a75d545e 100644 --- a/www/bulk-downloads/download.php +++ b/www/bulk-downloads/download.php @@ -1,7 +1,7 @@ IsConfirmed){ $subscription->Confirm(); diff --git a/www/newsletter/subscriptions/delete.php b/www/newsletter/subscriptions/delete.php index 7ed22590..60f266f7 100644 --- a/www/newsletter/subscriptions/delete.php +++ b/www/newsletter/subscriptions/delete.php @@ -5,7 +5,7 @@ try{ $requestType = HttpInput::RequestType(); - $subscription = NewsletterSubscription::Get(HttpInput::Str(HttpVariableSource::Get, 'uuid')); + $subscription = NewsletterSubscription::Get(HttpInput::Str(GET, 'uuid')); $subscription->Delete(); if($requestType == HttpRequestType::Rest){ diff --git a/www/newsletter/subscriptions/get.php b/www/newsletter/subscriptions/get.php index f81e0c4c..7290f523 100644 --- a/www/newsletter/subscriptions/get.php +++ b/www/newsletter/subscriptions/get.php @@ -13,7 +13,7 @@ try{ $created = true; } else{ - $subscription = NewsletterSubscription::Get(HttpInput::Str(HttpVariableSource::Get, 'uuid')); + $subscription = NewsletterSubscription::Get(HttpInput::Str(GET, 'uuid')); if(isset($_SESSION['subscription-created']) && $_SESSION['subscription-created'] == $subscription->UserId){ $created = true; diff --git a/www/newsletter/subscriptions/post.php b/www/newsletter/subscriptions/post.php index 663cffc5..3beae6b9 100644 --- a/www/newsletter/subscriptions/post.php +++ b/www/newsletter/subscriptions/post.php @@ -11,7 +11,7 @@ try{ $subscription = new NewsletterSubscription(); - if(HttpInput::Str(HttpVariableSource::Post, 'automationtest')){ + if(HttpInput::Str(POST, 'automationtest')){ // A bot filled out this form field, which should always be empty. Pretend like we succeeded. if($requestType == HttpRequestType::Web){ http_response_code(303); @@ -32,12 +32,12 @@ try{ $subscription->User = new User(); - $subscription->User->Email = HttpInput::Str(HttpVariableSource::Post, 'email'); - $subscription->IsSubscribedToNewsletter = HttpInput::Bool(HttpVariableSource::Post, 'issubscribedtonewsletter') ?? false; - $subscription->IsSubscribedToSummary = HttpInput::Bool(HttpVariableSource::Post, 'issubscribedtosummary') ?? false; + $subscription->User->Email = HttpInput::Str(POST, 'email'); + $subscription->IsSubscribedToNewsletter = HttpInput::Bool(POST, 'issubscribedtonewsletter') ?? false; + $subscription->IsSubscribedToSummary = HttpInput::Bool(POST, 'issubscribedtosummary') ?? false; - $expectedCaptcha = HttpInput::Str(HttpVariableSource::Session, 'captcha') ?? ''; - $receivedCaptcha = HttpInput::Str(HttpVariableSource::Post, 'captcha'); + $expectedCaptcha = HttpInput::Str(SESSION, 'captcha') ?? ''; + $receivedCaptcha = HttpInput::Str(POST, 'captcha'); $subscription->Create($expectedCaptcha, $receivedCaptcha); diff --git a/www/polls/get.php b/www/polls/get.php index d1cee61e..dafdf815 100644 --- a/www/polls/get.php +++ b/www/polls/get.php @@ -5,7 +5,7 @@ $poll = new Poll(); $canVote = true; // Allow non-logged-in users to see the 'vote' button try{ - $poll = Poll::GetByUrlName(HttpInput::Str(HttpVariableSource::Get, 'pollurlname')); + $poll = Poll::GetByUrlName(HttpInput::Str(GET, 'pollurlname')); if(!$poll->IsActive() && $poll->End !== null && $poll->End < new DateTimeImmutable()){ // If the poll ended, redirect to the results diff --git a/www/polls/votes/get.php b/www/polls/votes/get.php index 6a455662..19f3bf68 100644 --- a/www/polls/votes/get.php +++ b/www/polls/votes/get.php @@ -7,7 +7,7 @@ $vote = new PollVote(); $created = false; try{ - $vote = PollVote::Get(HttpInput::Str(HttpVariableSource::Get, 'pollurlname'), HttpInput::Int(HttpVariableSource::Get, 'userid')); + $vote = PollVote::Get(HttpInput::Str(GET, 'pollurlname'), HttpInput::Int(GET, 'userid')); if(isset($_SESSION['vote-created']) && $_SESSION['vote-created'] == $vote->UserId){ $created = true; diff --git a/www/polls/votes/index.php b/www/polls/votes/index.php index 12f6a3a4..3e3ebee8 100644 --- a/www/polls/votes/index.php +++ b/www/polls/votes/index.php @@ -2,7 +2,7 @@ $poll = new Poll(); try{ - $poll = Poll::GetByUrlName(HttpInput::Str(HttpVariableSource::Get, 'pollurlname')); + $poll = Poll::GetByUrlName(HttpInput::Str(GET, 'pollurlname')); } catch(Exceptions\AppException){ Template::Emit404(); diff --git a/www/polls/votes/new.php b/www/polls/votes/new.php index f17bff4a..7d0300b6 100644 --- a/www/polls/votes/new.php +++ b/www/polls/votes/new.php @@ -19,7 +19,7 @@ try{ $vote->User = $GLOBALS['User']; } - $poll = Poll::GetByUrlName(HttpInput::Str(HttpVariableSource::Get, 'pollurlname')); + $poll = Poll::GetByUrlName(HttpInput::Str(GET, 'pollurlname')); try{ $vote = PollVote::Get($poll->UrlName, $GLOBALS['User']->UserId); diff --git a/www/polls/votes/post.php b/www/polls/votes/post.php index ab13af4b..9d5c345f 100644 --- a/www/polls/votes/post.php +++ b/www/polls/votes/post.php @@ -10,9 +10,9 @@ try{ $vote = new PollVote(); - $vote->PollItemId = HttpInput::Int(HttpVariableSource::Post, 'pollitemid'); + $vote->PollItemId = HttpInput::Int(POST, 'pollitemid'); - $vote->Create(HttpInput::Str(HttpVariableSource::Post, 'email')); + $vote->Create(HttpInput::Str(POST, 'email')); session_unset(); @@ -34,7 +34,7 @@ catch(Exceptions\InvalidPollVoteException $ex){ // Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity http_response_code(303); - header('Location: /polls/' . (HttpInput::Str(HttpVariableSource::Get, 'pollurlname') ?? '') . '/votes/new'); + header('Location: /polls/' . (HttpInput::Str(GET, 'pollurlname') ?? '') . '/votes/new'); } else{ // Access via HttpRequestType::Rest api; 422 Unprocessable Entity diff --git a/www/sessions/new.php b/www/sessions/new.php index 3c135970..6179e1ac 100644 --- a/www/sessions/new.php +++ b/www/sessions/new.php @@ -8,8 +8,8 @@ if($GLOBALS['User'] !== null){ exit(); } -$email = HttpInput::Str(HttpVariableSource::Session, 'email'); -$redirect = HttpInput::Str(HttpVariableSource::Session, 'redirect') ?? HttpInput::Str(HttpVariableSource::Get, 'redirect'); +$email = HttpInput::Str(SESSION, 'email'); +$redirect = HttpInput::Str(SESSION, 'redirect') ?? HttpInput::Str(GET, 'redirect'); $exception = $_SESSION['exception'] ?? null; $passwordRequired = false; diff --git a/www/sessions/post.php b/www/sessions/post.php index e2ee4249..28523f79 100644 --- a/www/sessions/post.php +++ b/www/sessions/post.php @@ -9,9 +9,9 @@ try{ $requestType = HttpInput::RequestType(); $session = new Session(); - $email = HttpInput::Str(HttpVariableSource::Post, 'email'); - $password = HttpInput::Str(HttpVariableSource::Post, 'password'); - $redirect = HttpInput::Str(HttpVariableSource::Post, 'redirect'); + $email = HttpInput::Str(POST, 'email'); + $password = HttpInput::Str(POST, 'password'); + $redirect = HttpInput::Str(POST, 'redirect'); if($redirect === null){ $redirect = '/'; diff --git a/www/settings/post.php b/www/settings/post.php index 0fa87b0f..0f90cce1 100644 --- a/www/settings/post.php +++ b/www/settings/post.php @@ -1,8 +1,8 @@ strtotime('+1 month'), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);