Replace Template::Emit...() functions with more generic function

This commit is contained in:
Alex Cabal 2024-12-18 13:14:16 -06:00
parent f449c024ea
commit 806939ca49
40 changed files with 86 additions and 68 deletions

View file

@ -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'];

View file

@ -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){

View file

@ -21,7 +21,7 @@ try{
}
}
catch(Exceptions\ArtistNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => 'Artwork by ' . $artworks[0]->Artist->Name, 'css' => ['/css/artwork.css']]) ?>
<main class="artworks">

View file

@ -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.
}
?>

View file

@ -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);
}
?><?= Template::Header(['title' => $artwork->Name, 'css' => ['/css/artwork.css']]) ?>

View file

@ -124,7 +124,7 @@ try{
}
}
catch(Exceptions\ArtworkNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
catch(Exceptions\PageOutOfBoundsException){
$url = '/artworks?page=' . $pages;

View file

@ -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.
}
?>

View file

@ -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.

View file

@ -20,7 +20,7 @@ try{
$authorUrl = $ebooks[0]->AuthorsUrl;
}
catch(Exceptions\AuthorNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => '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]) ?>
<main class="ebooks">

View file

@ -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){

View file

@ -49,7 +49,7 @@ catch(Exceptions\InvalidPermissionsException){
http_response_code(Enums\HttpCode::Forbidden->value);
}
catch(Exceptions\InvalidFileException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => 'Downloading Ebook Collections', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?>

View file

@ -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);
}
?><?= Template::Header(['title' => 'Download ', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?>

View file

@ -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);
}
?><?= Template::Header(['title' => $pageTitle, 'feedUrl' => $feedUrl, 'feedTitle' => $feedTitle, 'highlight' => 'ebooks', 'description' => $pageDescription]) ?>
<main class="ebooks">

View file

@ -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);
}
?>
<?= Template::Header(

View file

@ -21,7 +21,7 @@ try{
}
}
catch(Exceptions\EbookNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(
[

View file

@ -55,7 +55,7 @@ catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::Emit403(); // No permissions to create an ebook placeholder.
Template::ExitWithCode(Enums\HttpCode::Forbidden); // No permissions to create an ebook placeholder.
}
?>
<?= Template::Header(

View file

@ -110,7 +110,7 @@ catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException | Exceptions\InvalidHttpMethodException | Exceptions\HttpMethodNotAllowedException){
Template::Emit403();
Template::ExitWithCode(Enums\HttpCode::Forbidden);
}
catch(Exceptions\AppException $ex){
$_SESSION['ebook'] = $ebook;

View file

@ -59,7 +59,7 @@ try{
setcookie('download-count', (string)$downloadCount, ['expires' => 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);
}
?><?= Template::Header(['title' => 'Your Download Has Started!', 'downloadUrl' => $downloadUrl]) ?>
<main class="donate">

View file

@ -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);
}

View file

@ -75,7 +75,7 @@ catch(Exceptions\EbookNotFoundException){
exit();
}
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => 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]) ?>
<main>

View file

@ -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);
}

View file

@ -39,7 +39,7 @@ try{
}
}
catch(Exceptions\EbookNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => 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]) ?>
<main>

View file

@ -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);
}
}
?><?= Template::Header(['title' => $type->GetDisplayName() . ' Ebook Feeds by ' . $ucTitle, 'description' => 'A list of available ' . $type->GetDisplayName() . ' feeds of Standard Ebooks ebooks by ' . $lcTitle . '.']) ?>

View file

@ -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.

View file

@ -53,7 +53,7 @@ try{
$feedUrl = '/' . $collectionType->value . '/' . $target;
}
catch(Exceptions\CollectionNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => $title, 'feedTitle' => $feedTitle, 'feedUrl' => $feedUrl, 'description' => $description]) ?>
<main>

View file

@ -15,7 +15,7 @@ try{
$url = preg_replace('|/$|ius', '', $url);
}
catch(\Exception){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
if($url != ''){

View file

@ -15,5 +15,5 @@ try{
header('Location: ' . $subscription->Url);
}
catch(Exceptions\NewsletterSubscriptionNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}

View file

@ -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);

View file

@ -38,7 +38,7 @@ try{
}
}
catch(Exceptions\AppException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => 'Your Subscription to the Standard Ebooks Newsletter', 'highlight' => 'newsletter', 'description' => 'Your subscription to the Standard Ebooks newsletter.']) ?>

View file

@ -23,7 +23,7 @@ try{
}
}
catch(Exceptions\AppException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => $poll->Name, 'highlight' => '', 'description' => $poll->Description]) ?>

View file

@ -16,7 +16,7 @@ try{
}
}
catch(Exceptions\AppException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => 'Your Vote Has Been Recorded!', 'highlight' => '', 'description' => 'Thank you for voting in a Standard Ebooks poll!']) ?>

View file

@ -5,7 +5,7 @@ try{
$poll = Poll::GetByUrlName(HttpInput::Str(GET, 'pollurlname'));
}
catch(Exceptions\AppException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
?><?= Template::Header(['title' => 'Results for the ' . $poll->Name . ' Poll', 'highlight' => '', 'description' => 'The voting results for the ' . $poll->Name . ' poll.']) ?>

View file

@ -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;

View file

@ -33,7 +33,7 @@ catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::Emit403();
Template::ExitWithCode(Enums\HttpCode::Forbidden);
}
?><?= Template::Header([
'title' => 'Projects',

View file

@ -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);
}
?><?= Template::Header([
'title' => 'New Project',

View file

@ -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;

View file

@ -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.
}
?>
<?= Template::Header(

View file

@ -22,13 +22,13 @@ try{
}
}
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);
}
?><?= Template::Header(
[

View file

@ -58,10 +58,10 @@ catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::Emit403();
Template::ExitWithCode(Enums\HttpCode::Forbidden);
}
catch(Exceptions\UserNotFoundException){
Template::Emit404();
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
catch(Exceptions\InvalidUserException | Exceptions\UserExistsException $ex){
if($generateNewUuid){

View file

@ -20,13 +20,13 @@ try{
$reviewingProjects = Project::GetAllByReviewerUserId($user->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);
}
?><?= Template::Header(
[