From 3050ab721985a289e81b6e9fdf1c23e170c7e18a Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 20 Nov 2024 14:52:05 -0600 Subject: [PATCH] Use HTTP code enums instead of ints --- lib/Template.php | 4 ++-- www/artworks/edit.php | 2 +- www/artworks/get.php | 2 +- www/artworks/new.php | 4 ++-- www/artworks/post.php | 8 ++++---- www/bulk-downloads/download.php | 2 +- www/ebooks/get.php | 2 +- www/ebooks/index.php | 2 +- www/feeds/atom/search.php | 2 +- www/feeds/download.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/new.php | 2 +- www/newsletter/subscriptions/post.php | 16 ++++++++-------- www/newsletter/subscriptions/success.php | 2 +- www/polls/votes/get.php | 2 +- www/polls/votes/new.php | 2 +- www/polls/votes/post.php | 8 ++++---- www/sessions/new.php | 6 +++--- www/sessions/post.php | 8 ++++---- www/settings/post.php | 2 +- www/webhooks/github.php | 8 ++++---- www/webhooks/postmark.php | 6 +++--- www/webhooks/zoho.php | 6 +++--- 28 files changed, 56 insertions(+), 56 deletions(-) diff --git a/lib/Template.php b/lib/Template.php index ce02a135..c934daf7 100644 --- a/lib/Template.php +++ b/lib/Template.php @@ -34,13 +34,13 @@ class Template{ } public static function Emit403(): void{ - http_response_code(403); + http_response_code(Enums\HttpCode::Forbidden->value); include(WEB_ROOT . '/403.php'); exit(); } public static function Emit404(): void{ - http_response_code(404); + http_response_code(Enums\HttpCode::NotFound->value); include(WEB_ROOT . '/404.php'); exit(); } diff --git a/www/artworks/edit.php b/www/artworks/edit.php index 11b74a5a..bbc62de2 100644 --- a/www/artworks/edit.php +++ b/www/artworks/edit.php @@ -23,7 +23,7 @@ try{ // We got here because an artwork update had errors and the user has to try again. if($exception){ - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); session_unset(); } } diff --git a/www/artworks/get.php b/www/artworks/get.php index 1c8a0458..d37942ad 100644 --- a/www/artworks/get.php +++ b/www/artworks/get.php @@ -47,7 +47,7 @@ try{ // We got here because an artwork PATCH operation had errors and the user has to try again. if($exception){ - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); // Before we overwrite the original artwork with our new one, restore the old status, because if the new status is 'approved' then it will hide the status form entirely, which will be confusing. $oldStatus = $artwork->Status; diff --git a/www/artworks/new.php b/www/artworks/new.php index 1ddcba7e..77b07147 100644 --- a/www/artworks/new.php +++ b/www/artworks/new.php @@ -20,14 +20,14 @@ try{ // We got here because an artwork was successfully submitted. if($isCreated){ - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); $artwork = null; session_unset(); } // We got here because an artwork submission had errors and the user has to try again. if($exception){ - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); session_unset(); } diff --git a/www/artworks/post.php b/www/artworks/post.php index f9a59873..6acdbee2 100644 --- a/www/artworks/post.php +++ b/www/artworks/post.php @@ -40,7 +40,7 @@ try{ $_SESSION['artwork'] = $artwork; $_SESSION['is-created'] = true; - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: /artworks/new'); } @@ -80,7 +80,7 @@ try{ $_SESSION['artwork'] = $artwork; $_SESSION['is-saved'] = true; - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $artwork->Url); } @@ -121,7 +121,7 @@ try{ $_SESSION['artwork'] = $artwork; $_SESSION['is-saved'] = true; - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $artwork->Url); } } @@ -143,6 +143,6 @@ catch(Exceptions\InvalidArtworkException | Exceptions\InvalidArtworkTagException $_SESSION['artwork'] = $artwork; $_SESSION['exception'] = $ex; - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $exceptionRedirectUrl); } diff --git a/www/bulk-downloads/download.php b/www/bulk-downloads/download.php index eeeeb8c3..e7369b74 100644 --- a/www/bulk-downloads/download.php +++ b/www/bulk-downloads/download.php @@ -46,7 +46,7 @@ catch(Exceptions\LoginRequiredException){ } } catch(Exceptions\InvalidPermissionsException){ - http_response_code(403); + http_response_code(Enums\HttpCode::Forbidden->value); } catch(Exceptions\InvalidFileException){ Template::Emit404(); diff --git a/www/ebooks/get.php b/www/ebooks/get.php index b36b1000..0ee38b70 100644 --- a/www/ebooks/get.php +++ b/www/ebooks/get.php @@ -56,7 +56,7 @@ catch(Exceptions\EbookNotFoundException){ $ebook = Ebook::GetByIdentifierStartingWith($identifier); // Found, redirect. - http_response_code(301); + http_response_code(Enums\HttpCode::MovedPermanently->value); header('Location: ' . $ebook->Url); exit(); } diff --git a/www/ebooks/index.php b/www/ebooks/index.php index 1fbc48ad..45ed8829 100644 --- a/www/ebooks/index.php +++ b/www/ebooks/index.php @@ -99,7 +99,7 @@ catch(Exceptions\PageOutOfBoundsException){ } catch(Exceptions\AppException $ex){ // Something very unexpected happened, log and emit 500. - http_response_code(500); // Internal server error. + http_response_code(Enums\HttpCode::InternalServerError->value); // Internal server error. Log::WriteErrorLogEntry($ex); exit(); } diff --git a/www/feeds/atom/search.php b/www/feeds/atom/search.php index b18576c2..e5e1ad97 100644 --- a/www/feeds/atom/search.php +++ b/www/feeds/atom/search.php @@ -11,7 +11,7 @@ try{ } } catch(\Exception){ - http_response_code(500); + http_response_code(Enums\HttpCode::InternalServerError->value); include(WEB_ROOT . '/404.php'); exit(); } diff --git a/www/feeds/download.php b/www/feeds/download.php index 041a2c0d..e1bccb06 100644 --- a/www/feeds/download.php +++ b/www/feeds/download.php @@ -98,10 +98,10 @@ try{ } catch(Exceptions\LoginRequiredException){ header('WWW-Authenticate: Basic realm="Enter your Patrons Circle email address and leave the password empty."'); - http_response_code(401); + http_response_code(Enums\HttpCode::Unauthorized->value); } catch(Exceptions\InvalidPermissionsException){ - http_response_code(403); + http_response_code(Enums\HttpCode::Forbidden->value); } catch(Exceptions\InvalidFileException){ Template::Emit404(); diff --git a/www/feeds/opds/search.php b/www/feeds/opds/search.php index b5214a7e..a138f422 100644 --- a/www/feeds/opds/search.php +++ b/www/feeds/opds/search.php @@ -11,7 +11,7 @@ try{ } } catch(\Exception){ - http_response_code(500); + http_response_code(Enums\HttpCode::InternalServerError->value); exit(); } print("\n\n"); diff --git a/www/feeds/rss/search.php b/www/feeds/rss/search.php index 20c25642..ee7a4c9f 100644 --- a/www/feeds/rss/search.php +++ b/www/feeds/rss/search.php @@ -11,7 +11,7 @@ try{ } } catch(\Exception){ - http_response_code(500); + http_response_code(Enums\HttpCode::InternalServerError->value); include(WEB_ROOT . '/404.php'); exit(); } diff --git a/www/manual/index.php b/www/manual/index.php index 1d472299..a841849d 100644 --- a/www/manual/index.php +++ b/www/manual/index.php @@ -22,5 +22,5 @@ if($url != ''){ $url = '/' . $url; } -http_response_code(302); +http_response_code(Enums\HttpCode::Found->value); header('Location: /manual/' . $currentManual . $url); diff --git a/www/newsletter/subscriptions/confirm.php b/www/newsletter/subscriptions/confirm.php index 181b4219..e8d6a59c 100644 --- a/www/newsletter/subscriptions/confirm.php +++ b/www/newsletter/subscriptions/confirm.php @@ -11,7 +11,7 @@ try{ $_SESSION['subscription-confirmed'] = $subscription->UserId; } - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $subscription->Url); } catch(Exceptions\NewsletterSubscriptionNotFoundException){ diff --git a/www/newsletter/subscriptions/delete.php b/www/newsletter/subscriptions/delete.php index b50c8fd3..69b4a3b2 100644 --- a/www/newsletter/subscriptions/delete.php +++ b/www/newsletter/subscriptions/delete.php @@ -17,7 +17,7 @@ catch(Exceptions\NewsletterSubscriptionNotFoundException){ Template::Emit404(); } else{ - http_response_code(404); + http_response_code(Enums\HttpCode::NotFound->value); exit(); } } diff --git a/www/newsletter/subscriptions/get.php b/www/newsletter/subscriptions/get.php index 6b842791..71f1ac15 100644 --- a/www/newsletter/subscriptions/get.php +++ b/www/newsletter/subscriptions/get.php @@ -34,7 +34,7 @@ try{ if($created){ // HTTP 201 Created - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); } } catch(Exceptions\AppException){ diff --git a/www/newsletter/subscriptions/new.php b/www/newsletter/subscriptions/new.php index ace2ea46..81125b1f 100644 --- a/www/newsletter/subscriptions/new.php +++ b/www/newsletter/subscriptions/new.php @@ -9,7 +9,7 @@ $subscription = $_SESSION['subscription'] ?? new NewsletterSubscription(); $exception = $_SESSION['exception'] ?? null; if($exception){ - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); session_unset(); } diff --git a/www/newsletter/subscriptions/post.php b/www/newsletter/subscriptions/post.php index a5773e7d..841ef596 100644 --- a/www/newsletter/subscriptions/post.php +++ b/www/newsletter/subscriptions/post.php @@ -14,7 +14,7 @@ try{ if(HttpInput::Str(POST, 'automationtest')){ // A bot filled out this form field, which should always be empty. Pretend like we succeeded. if($requestType == Enums\HttpRequestType::Web){ - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); $uuid = Uuid::uuid4(); $subscription->User = new User(); $subscription->User->Uuid = $uuid->toString(); @@ -23,7 +23,7 @@ try{ } else{ // Access via Enums\HttpRequestType::Rest api; 201 CREATED with location - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); header('Location: /newsletter/subscriptions/success'); } @@ -43,13 +43,13 @@ try{ session_unset(); if($requestType == Enums\HttpRequestType::Web){ - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); $_SESSION['is-subscription-created'] = $subscription->UserId; header('Location: /newsletter/subscriptions/success'); } else{ // Access via Enums\HttpRequestType::Rest api; 201 CREATED with location - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); header('Location: /newsletter/subscriptions/success'); } } @@ -62,7 +62,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){ $subscription->IsConfirmed = $existingSubscription->IsConfirmed; $subscription->Save(); - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); if(!$subscription->IsConfirmed){ // Don't re-send the email after all, to prevent spam @@ -77,7 +77,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){ } else{ // Access via Enums\HttpRequestType::Rest api; 409 CONFLICT - http_response_code(409); + http_response_code(Enums\HttpCode::Conflict->value); } } catch(Exceptions\InvalidNewsletterSubscription $ex){ @@ -86,11 +86,11 @@ catch(Exceptions\InvalidNewsletterSubscription $ex){ $_SESSION['exception'] = $ex; // Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: /newsletter/subscriptions/new'); } else{ // Access via Enums\HttpRequestType::Rest api; 422 Unprocessable Entity - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); } } diff --git a/www/newsletter/subscriptions/success.php b/www/newsletter/subscriptions/success.php index 169e2153..0a34fa27 100644 --- a/www/newsletter/subscriptions/success.php +++ b/www/newsletter/subscriptions/success.php @@ -16,7 +16,7 @@ if(isset($_SESSION['is-subscription-created'])){ if($created){ // HTTP 201 Created - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); } ?> 'Your subscription to the Standard Ebooks newsletter', 'highlight' => 'newsletter', 'description' => 'Your subscription to the Standard Ebooks newsletter.']) ?> diff --git a/www/polls/votes/get.php b/www/polls/votes/get.php index acb6e769..b940ba74 100644 --- a/www/polls/votes/get.php +++ b/www/polls/votes/get.php @@ -11,7 +11,7 @@ try{ if(isset($_SESSION['is-vote-created']) && $_SESSION['is-vote-created'] == $vote->UserId){ $created = true; - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); session_unset(); } } diff --git a/www/polls/votes/new.php b/www/polls/votes/new.php index 8015b584..919950f3 100644 --- a/www/polls/votes/new.php +++ b/www/polls/votes/new.php @@ -36,7 +36,7 @@ try{ } if($exception){ - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); session_unset(); } } diff --git a/www/polls/votes/post.php b/www/polls/votes/post.php index 4a9a28bf..c853d603 100644 --- a/www/polls/votes/post.php +++ b/www/polls/votes/post.php @@ -18,12 +18,12 @@ try{ if($requestType == Enums\HttpRequestType::Web){ $_SESSION['is-vote-created'] = $vote->UserId; - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $vote->Url); } else{ // Access via Enums\HttpRequestType::Rest api; 201 CREATED with location - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); header('Location: ' . $vote->Url); } } @@ -33,11 +33,11 @@ catch(Exceptions\InvalidPollVoteException $ex){ $_SESSION['exception'] = $ex; // Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: /polls/' . (HttpInput::Str(GET, 'pollurlname') ?? '') . '/votes/new'); } else{ // Access via Enums\HttpRequestType::Rest api; 422 Unprocessable Entity - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); } } diff --git a/www/sessions/new.php b/www/sessions/new.php index 393e50d9..9488a01b 100644 --- a/www/sessions/new.php +++ b/www/sessions/new.php @@ -14,18 +14,18 @@ $redirect = HttpInput::Str(SESSION, 'redirect') ?? HttpInput::Str(GET, 'redirect $exception = $_SESSION['exception'] ?? null; $passwordRequired = false; -http_response_code(401); +http_response_code(Enums\HttpCode::Unauthorized->value); if($exception){ if($exception instanceof Exceptions\PasswordRequiredException){ // This login requires a password to proceed. // Prompt the user for a password. - http_response_code(401); + http_response_code(Enums\HttpCode::Unauthorized->value); $passwordRequired = true; $exception = null; // Clear the exception so we don't show an error } else{ - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); } session_unset(); } diff --git a/www/sessions/post.php b/www/sessions/post.php index fcd418da..0d9cfe1b 100644 --- a/www/sessions/post.php +++ b/www/sessions/post.php @@ -22,12 +22,12 @@ try{ session_unset(); if($requestType == Enums\HttpRequestType::Web){ - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $redirect); } else{ // Access via Enums\HttpRequestType::Rest api; 201 CREATED with location - http_response_code(201); + http_response_code(Enums\HttpCode::Created->value); header('Location: ' . $session->Url); } } @@ -38,11 +38,11 @@ catch(Exceptions\InvalidLoginException | Exceptions\PasswordRequiredException $e $_SESSION['exception'] = $ex; // Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity - http_response_code(303); + http_response_code(Enums\HttpCode::SeeOther->value); header('Location: /sessions/new'); } else{ // Access via Enums\HttpRequestType::Rest api; 422 Unprocessable Entity - http_response_code(422); + http_response_code(Enums\HttpCode::UnprocessableContent->value); } } diff --git a/www/settings/post.php b/www/settings/post.php index 9f77692d..301becb9 100644 --- a/www/settings/post.php +++ b/www/settings/post.php @@ -23,7 +23,7 @@ if($colorScheme !== null){ } // HTTP 303, See other -http_response_code(303); +http_response_code(Enums\HttpCode::SeeOther->value); $redirect = $_SERVER['HTTP_REFERER'] ?? '/'; header('Location: ' . $redirect); diff --git a/www/webhooks/github.php b/www/webhooks/github.php index 53015443..04b3cd6a 100644 --- a/www/webhooks/github.php +++ b/www/webhooks/github.php @@ -131,11 +131,11 @@ try{ } // "Success, no content" - http_response_code(204); + http_response_code(Enums\HttpCode::NoContent->value); } catch(Exceptions\InvalidCredentialsException){ // "Forbidden" - http_response_code(403); + http_response_code(Enums\HttpCode::Forbidden->value); } catch(Exceptions\WebhookException $ex){ // Uh oh, something went wrong! @@ -147,13 +147,13 @@ catch(Exceptions\WebhookException $ex){ print($ex->getMessage()); // "Client error" - http_response_code(400); + http_response_code(Enums\HttpCode::BadRequest->value); } catch(Exceptions\NoopException){ // We arrive here because a special case required us to take no action for the request, but execution also had to be interrupted. // For example, we received a request for a known repo for which we must ignore requests. // "Success, no content" - http_response_code(204); + http_response_code(Enums\HttpCode::NoContent->value); } ?> diff --git a/www/webhooks/postmark.php b/www/webhooks/postmark.php index 8108b133..3bc58f79 100644 --- a/www/webhooks/postmark.php +++ b/www/webhooks/postmark.php @@ -74,12 +74,12 @@ try{ $log->Write('Event processed.'); // "Success, no content" - http_response_code(204); + http_response_code(Enums\HttpCode::NoContent->value); } catch(Exceptions\InvalidCredentialsException){ // "Forbidden" $log->Write('Invalid key: ' . ($_SERVER['HTTP_X_SE_KEY'] ?? '')); - http_response_code(403); + http_response_code(Enums\HttpCode::Forbidden->value); } catch(Exceptions\WebhookException $ex){ // Uh oh, something went wrong! @@ -91,5 +91,5 @@ catch(Exceptions\WebhookException $ex){ print($ex->getMessage()); // "Client error" - http_response_code(400); + http_response_code(Enums\HttpCode::BadRequest->value); } diff --git a/www/webhooks/zoho.php b/www/webhooks/zoho.php index 1aeadd05..19d68b99 100644 --- a/www/webhooks/zoho.php +++ b/www/webhooks/zoho.php @@ -51,12 +51,12 @@ try{ $log->Write('Event processed.'); // "Success, no content" - http_response_code(204); + http_response_code(Enums\HttpCode::NoContent->value); } catch(Exceptions\InvalidCredentialsException){ // "Forbidden" $log->Write('Couldn\'t validate POST data.'); - http_response_code(403); + http_response_code(Enums\HttpCode::Forbidden->value); } catch(Exceptions\WebhookException $ex){ // Uh oh, something went wrong! @@ -68,5 +68,5 @@ catch(Exceptions\WebhookException $ex){ print($ex->getMessage()); // "Client error" - http_response_code(400); + http_response_code(Enums\HttpCode::BadRequest->value); }