From beda605f329dfc118f222ed3485ca63f90c9c010 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 23 Mar 2022 13:49:00 -0500 Subject: [PATCH] Pull out apcu code into a function to improve error handling --- lib/Constants.php | 1 - lib/Email.php | 6 ++- lib/Library.php | 92 +++++++++++++++------------------------ www/webhooks/postmark.php | 4 +- 4 files changed, 43 insertions(+), 60 deletions(-) diff --git a/lib/Constants.php b/lib/Constants.php index 9256d4f0..1945d9ed 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -1,7 +1,6 @@ Port = 587; $phpMailer->Host = EMAIL_SMTP_HOST; $phpMailer->Username = EMAIL_SMTP_USERNAME; - $phpMailer->Password = EMAIL_SMTP_PASSWORD; + $phpMailer->Password = EMAIL_SMTP_USERNAME; if($this->PostmarkStream !== null){ $phpMailer->addCustomHeader('X-PM-Message-Stream', $this->PostmarkStream); diff --git a/lib/Library.php b/lib/Library.php index 4cb8000a..547ab60e 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -101,64 +101,22 @@ class Library{ * @return array */ public static function GetEbooks(): array{ - $ebooks = []; - // Get all ebooks, unsorted. - try{ - $ebooks = apcu_fetch('ebooks'); - } - catch(Safe\Exceptions\ApcuException $ex){ - Library::RebuildCache(); - try{ - $ebooks = apcu_fetch('ebooks'); - } - catch(Safe\Exceptions\ApcuException $ex){ - // We can get here if the cache is currently rebuilding from a different process. - // Nothing we can do but wait, so wait 20 seconds before retrying - sleep(20); - - try{ - $ebooks = apcu_fetch('ebooks'); - } - catch(Safe\Exceptions\ApcuException $ex){ - // Cache STILL rebuilding... give up silently for now - } - } - } - - return $ebooks; + return self::GetFromApcu('ebooks'); } /** * @return array */ public static function GetEbooksByAuthor(string $wwwFilesystemPath): array{ - // Do we have the author's ebooks cached? - $ebooks = []; - - try{ - $ebooks = apcu_fetch('author-' . $wwwFilesystemPath); - } - catch(Safe\Exceptions\ApcuException $ex){ - } - - return $ebooks; + return self::GetFromApcu('author-' . $wwwFilesystemPath); } /** * @return array */ public static function GetEbooksByTag(string $tag): array{ - // Do we have the tag's ebooks cached? - $ebooks = []; - - try{ - $ebooks = apcu_fetch('tag-' . $tag); - } - catch(Safe\Exceptions\ApcuException $ex){ - } - - return $ebooks; + return self::GetFromApcu('tag-' . $tag); } /** @@ -166,23 +124,45 @@ class Library{ */ public static function GetEbooksByCollection(string $collection): array{ // Do we have the tag's ebooks cached? - $ebooks = []; - - try{ - $ebooks = apcu_fetch('collection-' . $collection); - } - catch(Safe\Exceptions\ApcuException $ex){ - } - - return $ebooks; + return self::GetFromApcu('collection-' . $collection); } - /** * @return array */ public static function GetTags(): array{ - return apcu_fetch('tags'); + return self::GetFromApcu('tags'); + } + + /** + * @return array + */ + private static function GetFromApcu(string $variable): array{ + $results = []; + + try{ + $results = apcu_fetch($variable); + } + catch(Safe\Exceptions\ApcuException $ex){ + Library::RebuildCache(); + try{ + $results = apcu_fetch($variable); + } + catch(Safe\Exceptions\ApcuException $ex){ + // We can get here if the cache is currently rebuilding from a different process. + // Nothing we can do but wait, so wait 20 seconds before retrying + sleep(20); + + try{ + $results = apcu_fetch($variable); + } + catch(Safe\Exceptions\ApcuException $ex){ + // Cache STILL rebuilding... give up silently for now + } + } + } + + return $results; } /** diff --git a/www/webhooks/postmark.php b/www/webhooks/postmark.php index 8a8ddb8c..a67123aa 100644 --- a/www/webhooks/postmark.php +++ b/www/webhooks/postmark.php @@ -12,6 +12,8 @@ use function Safe\substr; $requestId = substr(sha1(time() . rand()), 0, 8); try{ + $smtpUsername = trim(file_get_contents(POSTMARK_SECRET_FILE_PATH)) ?: ''; + Logger::WritePostmarkWebhookLogEntry($requestId, 'Received Postmark webhook.'); if($_SERVER['REQUEST_METHOD'] != 'POST'){ @@ -50,7 +52,7 @@ try{ $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, 'https://api.postmarkapp.com/message-streams/' . $post->MessageStream . '/suppressions/delete'); curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($handle, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', 'X-Postmark-Server-Token: ' . EMAIL_SMTP_USERNAME]); + curl_setopt($handle, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', 'X-Postmark-Server-Token: ' . $smtpUsername]); curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($handle, CURLOPT_POSTFIELDS, '{"Suppressions": [{"EmailAddress": "' . $email . '"}]}'); curl_exec($handle);