From c35c47b79364d95a4536662b1517759079d3d83d Mon Sep 17 00:00:00 2001
From: Alex Cabal
Date: Fri, 8 Nov 2024 16:41:15 -0600
Subject: [PATCH] Add DateTimeFormat enum
---
lib/AtomFeed.php | 4 ++--
lib/Enums/DateTimeFormat.php | 12 ++++++++++++
lib/Session.php | 2 +-
templates/AtomFeed.php | 2 +-
templates/AtomFeedEntry.php | 4 ++--
templates/OpdsAcquisitionEntry.php | 6 +++---
templates/OpdsAcquisitionFeed.php | 2 +-
templates/OpdsNavigationFeed.php | 4 ++--
templates/RssEntry.php | 2 +-
templates/RssFeed.php | 2 +-
www/ebooks/download.php | 2 +-
www/feeds/atom/search.php | 2 +-
www/feeds/opds/search.php | 2 +-
www/feeds/rss/search.php | 2 +-
www/polls/get.php | 6 +++---
www/polls/votes/get.php | 2 +-
www/polls/votes/index.php | 4 ++--
www/settings/post.php | 4 ++--
18 files changed, 38 insertions(+), 26 deletions(-)
create mode 100644 lib/Enums/DateTimeFormat.php
diff --git a/lib/AtomFeed.php b/lib/AtomFeed.php
index 5a33dfd3..f3aa0887 100644
--- a/lib/AtomFeed.php
+++ b/lib/AtomFeed.php
@@ -54,11 +54,11 @@ class AtomFeed extends Feed{
foreach($this->Entries as $entry){
$obj = new StdClass();
if($entry instanceof Ebook){
- $obj->Updated = $entry->EbookUpdated->format('Y-m-d\TH:i:s\Z');
+ $obj->Updated = $entry->EbookUpdated->format(Enums\DateTimeFormat::Iso->value);
$obj->Id = SITE_URL . $entry->Url;
}
else{
- $obj->Updated = $entry->Updated !== null ? $entry->Updated->format('Y-m-d\TH:i:s\Z') : '';
+ $obj->Updated = $entry->Updated !== null ? $entry->Updated->format(Enums\DateTimeFormat::Iso->value) : '';
$obj->Id = $entry->Id;
}
$currentEntries[] = $obj;
diff --git a/lib/Enums/DateTimeFormat.php b/lib/Enums/DateTimeFormat.php
new file mode 100644
index 00000000..5312e3e2
--- /dev/null
+++ b/lib/Enums/DateTimeFormat.php
@@ -0,0 +1,12 @@
+
+namespace Enums;
+
+enum DateTimeFormat: string{
+ case Sql = 'Y-m-d H:i:s'; // `2022-01-05 23:42:12`
+ case Iso = 'Y-m-d\TH:i:s\Z'; // `2022-01-05T23:42:12Z`
+ case FullDateTime = 'F j, Y g:i a'; // `January 5, 2022 2:04 pm`
+ case Ical = 'Ymd\THis\Z'; //20220105T234212Z
+ case Html = 'Y-m-d\TH:i:s'; // `2022-01-05T23:42:12`
+ case Rss = 'r'; // `D, d M Y H:i:s`
+ case UnixTimestamp = 'U';
+}
diff --git a/lib/Session.php b/lib/Session.php
index 853115b1..3ca8a9bc 100644
--- a/lib/Session.php
+++ b/lib/Session.php
@@ -98,7 +98,7 @@ class Session{
public static function SetSessionCookie(string $sessionId): void{
/** @throws void */
- setcookie('sessionid', $sessionId, ['expires' => intval((new DateTimeImmutable('+1 week'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); // Expires in two weeks
+ 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
}
/**
diff --git a/templates/AtomFeed.php b/templates/AtomFeed.php
index 7cfed045..b34f9c6f 100644
--- a/templates/AtomFeed.php
+++ b/templates/AtomFeed.php
@@ -21,7 +21,7 @@ print("\n");
= Formatter::EscapeXml($title) ?>
if($subtitle !== null){ ?>= Formatter::EscapeXml($subtitle) ?> } ?>
= SITE_URL ?>/images/logo.png
- = $updated->format('Y-m-d\TH:i:s\Z') ?>
+ = $updated->format(Enums\DateTimeFormat::Iso->value) ?>
Standard Ebooks
= SITE_URL ?>
diff --git a/templates/AtomFeedEntry.php b/templates/AtomFeedEntry.php
index d7d31aad..a6f03073 100644
--- a/templates/AtomFeedEntry.php
+++ b/templates/AtomFeedEntry.php
@@ -15,8 +15,8 @@ use function Safe\filesize;
= SITE_URL . Formatter::EscapeXml($entry->AuthorsUrl) ?>
} ?>
- = $entry->Created->format('Y-m-d\TH:i:s\Z') ?>
- = $entry->Updated->format('Y-m-d\TH:i:s\Z') ?>
+ = $entry->Created->format(Enums\DateTimeFormat::Iso->value) ?>
+ = $entry->Updated->format(Enums\DateTimeFormat::Iso->value) ?>
Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
= Formatter::EscapeXml($entry->Description) ?>
= Formatter::EscapeXml($entry->LongDescription) ?>
diff --git a/templates/OpdsAcquisitionEntry.php b/templates/OpdsAcquisitionEntry.php
index 5e36f508..28621844 100644
--- a/templates/OpdsAcquisitionEntry.php
+++ b/templates/OpdsAcquisitionEntry.php
@@ -24,9 +24,9 @@ use function Safe\filesize;
} ?>
} ?>
- = $entry->EbookCreated->format('Y-m-d\TH:i:s\Z') ?>
- = $entry->EbookCreated->format('Y-m-d\TH:i:s\Z') ?>
- = $entry->EbookUpdated->format('Y-m-d\TH:i:s\Z') ?>
+ = $entry->EbookCreated->format(Enums\DateTimeFormat::Iso->value) ?>
+ = $entry->EbookCreated->format(Enums\DateTimeFormat::Iso->value) ?>
+ = $entry->EbookUpdated->format(Enums\DateTimeFormat::Iso->value) ?>
= Formatter::EscapeXml($entry->Language) ?>
Standard Ebooks
Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
diff --git a/templates/OpdsAcquisitionFeed.php b/templates/OpdsAcquisitionFeed.php
index 1bcb0bd2..3e9812f6 100644
--- a/templates/OpdsAcquisitionFeed.php
+++ b/templates/OpdsAcquisitionFeed.php
@@ -36,7 +36,7 @@ print("\n");
= Formatter::EscapeXml($subtitle) ?>
} ?>
= SITE_URL ?>/images/logo.png
- = $updated->format('Y-m-d\TH:i:s\Z') ?>
+ = $updated->format(Enums\DateTimeFormat::Iso->value) ?>
if($isCrawlable){ ?>
} ?>
diff --git a/templates/OpdsNavigationFeed.php b/templates/OpdsNavigationFeed.php
index fc35a27a..2cc453be 100644
--- a/templates/OpdsNavigationFeed.php
+++ b/templates/OpdsNavigationFeed.php
@@ -30,7 +30,7 @@ print("\n");
= Formatter::EscapeXml($subtitle) ?>
} ?>
= SITE_URL ?>/images/logo.png
- = $updated->format('Y-m-d\TH:i:s\Z') ?>
+ = $updated->format(Enums\DateTimeFormat::Iso->value) ?>
Standard Ebooks
= SITE_URL ?>
@@ -39,7 +39,7 @@ print("\n");
= Formatter::EscapeXml($entry->Title) ?>
- if($entry->Updated !== null){ ?>= $entry->Updated->format('Y-m-d\TH:i:s\Z') ?> } ?>
+ if($entry->Updated !== null){ ?>= $entry->Updated->format(Enums\DateTimeFormat::Iso->value) ?> } ?>
= Formatter::EscapeXml($entry->Id) ?>
= Formatter::EscapeXml($entry->Description) ?>
diff --git a/templates/RssEntry.php b/templates/RssEntry.php
index 258ff54d..66ae7a85 100644
--- a/templates/RssEntry.php
+++ b/templates/RssEntry.php
@@ -10,7 +10,7 @@ use function Safe\preg_replace;
= Formatter::EscapeXml($entry->Title) ?>, by = Formatter::EscapeXml(strip_tags($entry->AuthorsHtml)) ?>
= SITE_URL . Formatter::EscapeXml($entry->Url) ?>
= Formatter::EscapeXml($entry->Description) ?>
- = $entry->EbookCreated->format('r') ?>
+ = $entry->EbookCreated->format(Enums\DateTimeFormat::Rss->value) ?>
= Formatter::EscapeXml(preg_replace('/^url:/ius', '', $entry->Identifier)) ?>
foreach($entry->Tags as $tag){ ?>
= Formatter::EscapeXml($tag->Name) ?>
diff --git a/templates/RssFeed.php b/templates/RssFeed.php
index f365e73c..74fff983 100644
--- a/templates/RssFeed.php
+++ b/templates/RssFeed.php
@@ -18,7 +18,7 @@ print("\n");
= Formatter::EscapeXml($description) ?>
en-US
https://creativecommons.org/publicdomain/zero/1.0/
- = $updated->format('r'); ?>
+ = $updated->format(Enums\DateTimeFormat::Rss->value); ?>
http://blogs.law.harvard.edu/tech/rss
diff --git a/www/ebooks/download.php b/www/ebooks/download.php
index d10ba59d..8eee98aa 100644
--- a/www/ebooks/download.php
+++ b/www/ebooks/download.php
@@ -52,7 +52,7 @@ try{
// Increment local download count, expires in 2 weeks
$downloadCount++;
- setcookie('download-count', (string)$downloadCount, ['expires' => intval((new DateTimeImmutable('+2 week'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']);
+ 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();
diff --git a/www/feeds/atom/search.php b/www/feeds/atom/search.php
index 8ff93c1d..d1805035 100644
--- a/www/feeds/atom/search.php
+++ b/www/feeds/atom/search.php
@@ -27,7 +27,7 @@ print("\nSearch Results
Results for “= Formatter::EscapeXml($query) ?>”.
= SITE_URL ?>/images/logo.png
- = NOW->format('Y-m-d\TH:i:s\Z') ?>
+ = NOW->format(Enums\DateTimeFormat::Iso->value) ?>
Standard Ebooks
= SITE_URL ?>
diff --git a/www/feeds/opds/search.php b/www/feeds/opds/search.php
index a61a3e94..10045b54 100644
--- a/www/feeds/opds/search.php
+++ b/www/feeds/opds/search.php
@@ -28,7 +28,7 @@ print("\nSearch Results
Results for “= Formatter::EscapeXml($query) ?>”.
= SITE_URL ?>/images/logo.png
- = NOW->format('Y-m-d\TH:i:s\Z') ?>
+ = NOW->format(Enums\DateTimeFormat::Iso->value) ?>
Standard Ebooks
= SITE_URL ?>
diff --git a/www/feeds/rss/search.php b/www/feeds/rss/search.php
index a318e56f..61ce562e 100644
--- a/www/feeds/rss/search.php
+++ b/www/feeds/rss/search.php
@@ -26,7 +26,7 @@ print("\nResults for “= Formatter::EscapeXml($query) ?>”.
en-US
https://creativecommons.org/publicdomain/zero/1.0/
- = NOW->format('r') ?>
+ = NOW->format(Enums\DateTimeFormat::Rss->value) ?>
http://blogs.law.harvard.edu/tech/rss
diff --git a/www/polls/get.php b/www/polls/get.php
index 075046fa..26aff60f 100644
--- a/www/polls/get.php
+++ b/www/polls/get.php
@@ -35,7 +35,7 @@ catch(Exceptions\AppException){
= $poll->Description ?>
if($poll->IsActive()){ ?>
if($poll->End !== null){ ?>
- This poll closes on = $poll->End->format('F j, Y g:i a') ?>.
+ This poll closes on = $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.
} ?>
if(!$canVote){ ?>
You’ve already voted in this poll.
@@ -48,9 +48,9 @@ catch(Exceptions\AppException){
}else{ ?>
if($poll->Start !== null && $poll->Start > NOW){ ?>
- This poll opens on = $poll->Start->format('F j, Y g:i a') ?>.
+ This poll opens on = $poll->Start->format(Enums\DateTimeFormat::FullDateTime->value) ?>.
}else{ ?>
- This poll closed on = $poll->End->format('F j, Y g:i a') ?>.
+ This poll closed on = $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.
View results
} ?>
} ?>
diff --git a/www/polls/votes/get.php b/www/polls/votes/get.php
index 283201a3..acb6e769 100644
--- a/www/polls/votes/get.php
+++ b/www/polls/votes/get.php
@@ -26,7 +26,7 @@ catch(Exceptions\AppException){
if($created){ ?>
Thank you for voting in the = Formatter::EscapeHtml($vote->PollItem->Poll->Name) ?> poll.
}else{ ?>
- Your vote in the = Formatter::EscapeHtml($vote->PollItem->Poll->Name) ?> poll was submitted on = $vote->Created->format('F j, Y g:i a') ?>.
+ Your vote in the = Formatter::EscapeHtml($vote->PollItem->Poll->Name) ?> poll was submitted on = $vote->Created->format(Enums\DateTimeFormat::FullDateTime->value) ?>.
} ?>
view results
diff --git a/www/polls/votes/index.php b/www/polls/votes/index.php
index 3e3ebee8..58b5c15b 100644
--- a/www/polls/votes/index.php
+++ b/www/polls/votes/index.php
@@ -15,10 +15,10 @@ catch(Exceptions\AppException){
Total votes: = number_format($poll->VoteCount) ?>
if($poll->IsActive()){ ?>
if($poll->End !== null){ ?>
- This poll closes on = $poll->End->format('F j, Y g:i a') ?>.
+ This poll closes on = $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.
} ?>
}elseif($poll->End !== null){ ?>
- This poll closed on = $poll->End->format('F j, Y g:i a') ?>.
+ This poll closed on = $poll->End->format(Enums\DateTimeFormat::FullDateTime->value) ?>.
} ?>
diff --git a/www/settings/post.php b/www/settings/post.php
index 2f0da111..9f77692d 100644
--- a/www/settings/post.php
+++ b/www/settings/post.php
@@ -5,7 +5,7 @@ $hideDonationAlert = HttpInput::Bool(POST, 'hide-donation-alert');
$colorScheme = HttpInput::Str(POST, 'color-scheme');
if($hideDonationAlert !== null){
- setcookie('hide-donation-alert', $hideDonationAlert ? 'true' : 'false', ['expires' => intval((new DateTimeImmutable('+1 month'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
+ setcookie('hide-donation-alert', $hideDonationAlert ? 'true' : 'false', ['expires' => intval((new DateTimeImmutable('+1 month'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
}
if($colorScheme !== null){
@@ -18,7 +18,7 @@ if($colorScheme !== null){
setcookie('color-scheme', '', ['expires' => 0, 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
}
else{
- setcookie('color-scheme', $colorScheme, ['expires' => intval((new DateTimeImmutable('+1 year'))->format('U')), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
+ setcookie('color-scheme', $colorScheme, ['expires' => intval((new DateTimeImmutable('+1 year'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
}
}