diff --git a/lib/Template.php b/lib/Template.php index c934daf7..ea2fd880 100644 --- a/lib/Template.php +++ b/lib/Template.php @@ -33,19 +33,39 @@ class Template{ } } - public static function Emit403(): void{ - http_response_code(Enums\HttpCode::Forbidden->value); - include(WEB_ROOT . '/403.php'); + /** + * Exit the script while outputting the given HTTP code. + * + * @param bool $showPage If **`TRUE`**, show a special page given the HTTP code (like a 404 page). + * + * @return never + */ + public static function ExitWithCode(Enums\HttpCode $httpCode, bool $showPage = true, Enums\HttpRequestType $requestType = Enums\HttpRequestType::Web): void{ + http_response_code($httpCode->value); + + if($requestType == Enums\HttpRequestType::Web && $showPage){ + switch($httpCode){ + case Enums\HttpCode::Forbidden: + include(WEB_ROOT . '/403.php'); + break; + case Enums\HttpCode::NotFound: + include(WEB_ROOT . '/404.php'); + break; + } + } + exit(); } - public static function Emit404(): void{ - http_response_code(Enums\HttpCode::NotFound->value); - include(WEB_ROOT . '/404.php'); - exit(); - } - - public static function RedirectToLogin(bool $redirectToDestination = true, string $destinationUrl = null): void{ + /** + * Redirect the user to the login page. + * + * @param bool $redirectToDestination After login, redirect the user to the page they came from. + * @param ?string $destinationUrl If `$redirectToDestination` is **`TRUE`**, redirect to this URL instead of hte page they came from. + * + * @return never + */ + public static function RedirectToLogin(bool $redirectToDestination = true, ?string $destinationUrl = null): void{ if($redirectToDestination){ if($destinationUrl === null){ $destinationUrl = $_SERVER['SCRIPT_URL']; diff --git a/scripts/update-ebook-database b/scripts/update-ebook-database index 0ec8219e..48678492 100755 --- a/scripts/update-ebook-database +++ b/scripts/update-ebook-database @@ -41,9 +41,7 @@ try{ $ebook->CreateOrUpdate(); // If there was an `EbookPlaceholder` for this ebook, delete it. - if($ebook->EbookPlaceholder !== null){ - $ebook->EbookPlaceholder->Delete(); - } + $ebook->EbookPlaceholder?->Delete(); // If there was a `Project` for this ebook, mark it as completed. if($ebook->ProjectInProgress !== null){ diff --git a/www/artists/get.php b/www/artists/get.php index 90612894..3913077f 100644 --- a/www/artists/get.php +++ b/www/artists/get.php @@ -21,7 +21,7 @@ try{ } } catch(Exceptions\ArtistNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Artwork by ' . $artworks[0]->Artist->Name, 'css' => ['/css/artwork.css']]) ?>
diff --git a/www/artworks/edit.php b/www/artworks/edit.php index fc145cea..7980719d 100644 --- a/www/artworks/edit.php +++ b/www/artworks/edit.php @@ -26,13 +26,13 @@ try{ } } catch(Exceptions\ArtworkNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); // No permissions to edit artwork. + Template::ExitWithCode(Enums\HttpCode::Forbidden); // No permissions to edit artwork. } ?> diff --git a/www/artworks/get.php b/www/artworks/get.php index 3a46e4bd..39f70959 100644 --- a/www/artworks/get.php +++ b/www/artworks/get.php @@ -58,10 +58,10 @@ try{ } } catch(Exceptions\ArtworkNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } ?> $artwork->Name, 'css' => ['/css/artwork.css']]) ?> diff --git a/www/artworks/index.php b/www/artworks/index.php index bca42fd5..9472e5aa 100644 --- a/www/artworks/index.php +++ b/www/artworks/index.php @@ -124,7 +124,7 @@ try{ } } catch(Exceptions\ArtworkNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\PageOutOfBoundsException){ $url = '/artworks?page=' . $pages; diff --git a/www/artworks/new.php b/www/artworks/new.php index 8dc275f5..163acaec 100644 --- a/www/artworks/new.php +++ b/www/artworks/new.php @@ -41,7 +41,7 @@ catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); // No permissions to submit artwork. + Template::ExitWithCode(Enums\HttpCode::Forbidden); // No permissions to submit artwork. } ?> diff --git a/www/artworks/post.php b/www/artworks/post.php index 9df07d1c..0844818b 100644 --- a/www/artworks/post.php +++ b/www/artworks/post.php @@ -130,10 +130,10 @@ catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } catch(Exceptions\ArtworkNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\InvalidArtworkException | Exceptions\InvalidArtworkTagException | Exceptions\InvalidArtistException | Exceptions\InvalidFileUploadException $ex){ // If we were passed a more generic file upload exception from `HttpInput`, swap it for a more specific exception to show to the user. diff --git a/www/authors/get.php b/www/authors/get.php index 77c2c701..e15de7ba 100644 --- a/www/authors/get.php +++ b/www/authors/get.php @@ -20,7 +20,7 @@ try{ $authorUrl = $ebooks[0]->AuthorsUrl; } catch(Exceptions\AuthorNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Ebooks by ' . $author, 'feedUrl' => str_replace('/ebooks/', '/authors/', $authorUrl), 'feedTitle' => 'Standard Ebooks - Ebooks by ' . $author, 'highlight' => 'ebooks', 'description' => 'All of the Standard Ebooks ebooks by ' . $author, 'canonicalUrl' => SITE_URL . $authorUrl]) ?>
diff --git a/www/bulk-downloads/collection.php b/www/bulk-downloads/collection.php index 78651d82..94be94e0 100644 --- a/www/bulk-downloads/collection.php +++ b/www/bulk-downloads/collection.php @@ -6,7 +6,7 @@ $canDownload = false; $class = HttpInput::Str(GET, 'class'); if($class === null || ($class != 'authors' && $class != 'collections' && $class != 'subjects' && $class != 'months')){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } if(Session::$User?->Benefits->CanBulkDownload){ diff --git a/www/bulk-downloads/download.php b/www/bulk-downloads/download.php index 886a75e1..370745f5 100644 --- a/www/bulk-downloads/download.php +++ b/www/bulk-downloads/download.php @@ -49,7 +49,7 @@ catch(Exceptions\InvalidPermissionsException){ http_response_code(Enums\HttpCode::Forbidden->value); } catch(Exceptions\InvalidFileException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Downloading Ebook Collections', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?> diff --git a/www/bulk-downloads/get.php b/www/bulk-downloads/get.php index b1780df6..2ffdb110 100644 --- a/www/bulk-downloads/get.php +++ b/www/bulk-downloads/get.php @@ -67,10 +67,10 @@ try{ } } catch(Exceptions\AuthorNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\CollectionNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Download ', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?> diff --git a/www/collections/get.php b/www/collections/get.php index d47d2a84..12780bed 100644 --- a/www/collections/get.php +++ b/www/collections/get.php @@ -14,7 +14,7 @@ try{ $feedTitle = 'Standard Ebooks - Ebooks in the ' . Formatter::EscapeHtml($collectionName) . ' ' . $collectionType; } catch(Exceptions\CollectionNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> $pageTitle, 'feedUrl' => $feedUrl, 'feedTitle' => $feedTitle, 'highlight' => 'ebooks', 'description' => $pageDescription]) ?>
diff --git a/www/ebook-placeholders/edit.php b/www/ebook-placeholders/edit.php index d46ab12e..290043e4 100644 --- a/www/ebook-placeholders/edit.php +++ b/www/ebook-placeholders/edit.php @@ -32,13 +32,13 @@ try{ } } catch(Exceptions\EbookNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } ?> intval((new DateTimeImmutable('+2 week'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); } catch(Exceptions\InvalidFileException | Exceptions\EbookNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Your Download Has Started!', 'downloadUrl' => $downloadUrl]) ?>
diff --git a/www/ebooks/edit.php b/www/ebooks/edit.php index 97870f43..72cfe67a 100644 --- a/www/ebooks/edit.php +++ b/www/ebooks/edit.php @@ -21,15 +21,15 @@ try{ } // Editing published `Ebooks` is not supported. - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\EbookNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } diff --git a/www/ebooks/get.php b/www/ebooks/get.php index 30792cc7..875e1d3e 100644 --- a/www/ebooks/get.php +++ b/www/ebooks/get.php @@ -75,7 +75,7 @@ catch(Exceptions\EbookNotFoundException){ exit(); } - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> strip_tags($ebook->TitleWithCreditsHtml) . ' - Free ebook download', 'ogType' => 'book', 'coverUrl' => $ebook->DistCoverUrl, 'highlight' => 'ebooks', 'description' => 'Free epub ebook download of the Standard Ebooks edition of ' . $ebook->Title . ': ' . $ebook->Description, 'canonicalUrl' => SITE_URL . $ebook->Url]) ?>
diff --git a/www/ebooks/post.php b/www/ebooks/post.php index 0fbb3dd4..e94da4f4 100644 --- a/www/ebooks/post.php +++ b/www/ebooks/post.php @@ -21,15 +21,15 @@ try{ } // POSTing published `Ebooks` is not supported. - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\EbookNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } diff --git a/www/ebooks/public-domain-day-placeholder.php b/www/ebooks/public-domain-day-placeholder.php index 0088eee2..a8974ce3 100644 --- a/www/ebooks/public-domain-day-placeholder.php +++ b/www/ebooks/public-domain-day-placeholder.php @@ -39,7 +39,7 @@ try{ } } catch(Exceptions\EbookNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> strip_tags($ebook->TitleWithCreditsHtml) . ' - Free ebook download', 'highlight' => 'ebooks', 'description' => 'Free epub ebook download of the Standard Ebooks edition of ' . $ebook->Title . ': ' . $ebook->Description, 'canonicalUrl' => SITE_URL . $ebook->Url]) ?>
diff --git a/www/feeds/collection.php b/www/feeds/collection.php index 1650f17b..2dbbb90f 100644 --- a/www/feeds/collection.php +++ b/www/feeds/collection.php @@ -6,11 +6,11 @@ $collectionType = Enums\FeedCollectionType::tryFrom(HttpInput::Str(GET, 'class') $type = Enums\FeedType::tryFrom(HttpInput::Str(GET, 'type') ?? ''); if($collectionType === null){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } if($type === null || ($type != Enums\FeedType::Rss && $type != Enums\FeedType::Atom)){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } $feeds = []; @@ -26,7 +26,7 @@ catch(Safe\Exceptions\ApcuException){ $feeds = Feed::RebuildFeedsCache($type, $collectionType); if($feeds === null){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } } ?> $type->GetDisplayName() . ' Ebook Feeds by ' . $ucTitle, 'description' => 'A list of available ' . $type->GetDisplayName() . ' feeds of Standard Ebooks ebooks by ' . $lcTitle . '.']) ?> diff --git a/www/feeds/download.php b/www/feeds/download.php index e1bccb06..ba1a661f 100644 --- a/www/feeds/download.php +++ b/www/feeds/download.php @@ -104,7 +104,7 @@ catch(Exceptions\InvalidPermissionsException){ http_response_code(Enums\HttpCode::Forbidden->value); } catch(Exceptions\InvalidFileException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } // Print the login info page. diff --git a/www/feeds/get.php b/www/feeds/get.php index 423196e0..042954bf 100644 --- a/www/feeds/get.php +++ b/www/feeds/get.php @@ -53,7 +53,7 @@ try{ $feedUrl = '/' . $collectionType->value . '/' . $target; } catch(Exceptions\CollectionNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> $title, 'feedTitle' => $feedTitle, 'feedUrl' => $feedUrl, 'description' => $description]) ?>
diff --git a/www/manual/index.php b/www/manual/index.php index a841849d..879c6168 100644 --- a/www/manual/index.php +++ b/www/manual/index.php @@ -15,7 +15,7 @@ try{ $url = preg_replace('|/$|ius', '', $url); } catch(\Exception){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } if($url != ''){ diff --git a/www/newsletter/subscriptions/confirm.php b/www/newsletter/subscriptions/confirm.php index e8d6a59c..d7ee7a31 100644 --- a/www/newsletter/subscriptions/confirm.php +++ b/www/newsletter/subscriptions/confirm.php @@ -15,5 +15,5 @@ try{ header('Location: ' . $subscription->Url); } catch(Exceptions\NewsletterSubscriptionNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } diff --git a/www/newsletter/subscriptions/delete.php b/www/newsletter/subscriptions/delete.php index 875e7faa..8700b163 100644 --- a/www/newsletter/subscriptions/delete.php +++ b/www/newsletter/subscriptions/delete.php @@ -14,7 +14,7 @@ try{ } catch(Exceptions\NewsletterSubscriptionNotFoundException){ if($requestType == Enums\HttpRequestType::Web){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } else{ http_response_code(Enums\HttpCode::NotFound->value); diff --git a/www/newsletter/subscriptions/get.php b/www/newsletter/subscriptions/get.php index ba88759d..98b63994 100644 --- a/www/newsletter/subscriptions/get.php +++ b/www/newsletter/subscriptions/get.php @@ -38,7 +38,7 @@ try{ } } catch(Exceptions\AppException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Your Subscription to the Standard Ebooks Newsletter', 'highlight' => 'newsletter', 'description' => 'Your subscription to the Standard Ebooks newsletter.']) ?> diff --git a/www/polls/get.php b/www/polls/get.php index aec3337b..bce3aecc 100644 --- a/www/polls/get.php +++ b/www/polls/get.php @@ -23,7 +23,7 @@ try{ } } catch(Exceptions\AppException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> $poll->Name, 'highlight' => '', 'description' => $poll->Description]) ?> diff --git a/www/polls/votes/get.php b/www/polls/votes/get.php index 2e100431..ef764190 100644 --- a/www/polls/votes/get.php +++ b/www/polls/votes/get.php @@ -16,7 +16,7 @@ try{ } } catch(Exceptions\AppException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Your Vote Has Been Recorded!', 'highlight' => '', 'description' => 'Thank you for voting in a Standard Ebooks poll!']) ?> diff --git a/www/polls/votes/index.php b/www/polls/votes/index.php index fb4ef3b0..13cc26c8 100644 --- a/www/polls/votes/index.php +++ b/www/polls/votes/index.php @@ -5,7 +5,7 @@ try{ $poll = Poll::GetByUrlName(HttpInput::Str(GET, 'pollurlname')); } catch(Exceptions\AppException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } ?> 'Results for the ' . $poll->Name . ' Poll', 'highlight' => '', 'description' => 'The voting results for the ' . $poll->Name . ' poll.']) ?> diff --git a/www/polls/votes/new.php b/www/polls/votes/new.php index b34df93c..4d81949f 100644 --- a/www/polls/votes/new.php +++ b/www/polls/votes/new.php @@ -38,7 +38,7 @@ catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\PollNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\PollVoteExistsException $ex){ $redirect = $poll->Url; diff --git a/www/projects/index.php b/www/projects/index.php index 1034f90a..ec4b0413 100644 --- a/www/projects/index.php +++ b/www/projects/index.php @@ -33,7 +33,7 @@ catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } ?> 'Projects', diff --git a/www/projects/new.php b/www/projects/new.php index bec85412..f6cc0648 100644 --- a/www/projects/new.php +++ b/www/projects/new.php @@ -44,13 +44,13 @@ try{ } } catch(Exceptions\EbookNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } ?> 'New Project', diff --git a/www/projects/post.php b/www/projects/post.php index 031276e1..77a34a90 100644 --- a/www/projects/post.php +++ b/www/projects/post.php @@ -28,13 +28,13 @@ try{ } } catch(Exceptions\EbookNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } catch(Exceptions\InvalidProjectException | Exceptions\ProjectExistsException | Exceptions\EbookIsNotAPlaceholderException $ex){ $_SESSION['project'] = $project; diff --git a/www/users/edit.php b/www/users/edit.php index 84ef1f49..94a962a1 100644 --- a/www/users/edit.php +++ b/www/users/edit.php @@ -28,13 +28,13 @@ try{ } } catch(Exceptions\UserNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); // No permissions to edit artwork. + Template::ExitWithCode(Enums\HttpCode::Forbidden); // No permissions to edit artwork. } ?> UserId); } catch(Exceptions\UserNotFoundException){ - Template::Emit404(); + Template::ExitWithCode(Enums\HttpCode::NotFound); } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException){ - Template::Emit403(); + Template::ExitWithCode(Enums\HttpCode::Forbidden); } ?>