From 2200ad4dd0038c4b7d8114a16fe29bde9a324539 Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Mon, 6 Jul 2020 19:59:11 +0200 Subject: [PATCH] Avoid errors while fetching alt text when no image is set or image isn't found. Also improve locale/domain selection and add descriptive message when a match isn't found. WIP #3771 @5 --- .../Magic/Models/CubistMagicAbstractModel.php | 10 ++++++++-- src/app/Middleware/LocaleSelector.php | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 86f86d8..04a9402 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -574,9 +574,15 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $this->getMedia($c); } - public function getFirstMediaAlt($collection, $default = '') + public function getFirstMediaAlt($collection = 'default', $default = '') { - return $this->getFirstMedia($collection)->getCustomProperty('alt') ?? $default; + $media = $this->getFirstMedia($collection); + if (!$media) return $default; + + $alt = $media->getCustomProperty('alt'); + if (!$alt) return $default; + + return $alt; } public function getAttribute($key) diff --git a/src/app/Middleware/LocaleSelector.php b/src/app/Middleware/LocaleSelector.php index f5af9cc..69f8f9a 100644 --- a/src/app/Middleware/LocaleSelector.php +++ b/src/app/Middleware/LocaleSelector.php @@ -32,9 +32,23 @@ class LocaleSelector extends CubistMiddleware $domain = $this->_getDomainByLocale(Locale::getLocaleData($defaultLocale)); // redirect to default locale if (null === $domain) { - abort(401); + // This should only happen in development but it is really hard to debug without a helpful message... + abort(401, 'Domain not found. Ensure APP_ENV is correct and domain exists in database (cubist_locales)'); } - $protocol = $_SERVER['HTTPS'] ? 'https://' : 'http://'; + + // Determine if connection is secure, supporting proxies + // Ref: https://stackoverflow.com/a/16076965 + $is_secure = false; + if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { + $is_secure = true; + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) + && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' + || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) + && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') { + $is_secure = true; + } + $protocol = $is_secure ? 'https://' : 'http://'; + $url = $protocol . $domain; return redirect($url); } -- 2.39.5