From d057cf93cece1d5a11d6f289b247748d03a79065 Mon Sep 17 00:00:00 2001 From: Brad Bell Date: Sat, 16 Apr 2022 15:08:52 -0700 Subject: [PATCH 1/3] Added checks for relying on the default `@web` alias for the site URL and asset base URLs. --- CHANGELOG.md | 5 ++ server/requirements/RequirementsChecker.php | 53 +++++++++++++++++++++ server/requirements/requirements.php | 2 + 3 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd82563..eb4d1c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog for Craft CMS Server Check +## Unreleased + +### Added +- Added checks for relying on the default `@web` alias for the site URL and asset base URLs. + ## 1.2.1 - 2021-05-25 ### Added diff --git a/server/requirements/RequirementsChecker.php b/server/requirements/RequirementsChecker.php index 6778fe4..66534ed 100644 --- a/server/requirements/RequirementsChecker.php +++ b/server/requirements/RequirementsChecker.php @@ -538,6 +538,59 @@ function maxExecutionTimeRequirement() ); } + /** + * @return array + */ + function siteWebAliasRequirement() + { + $pass = true; + $memo = 'Your @web alias is set correctly for the site.'; + if (Craft::$app->getRequest()->isWebAliasSetDynamically) { + $baseUrl = Craft::$app->getSites()->getCurrentSite()->getBaseUrl(false); + $pass = !\craft\helpers\StringHelper::contains($baseUrl, '@web'); + if (!$pass) { + $memo = 'We recommend explicitly overriding the @web alias for your site if you plan on using it.'; + } + } + + return array( + 'name' => 'Ensure @web alias is explicitly overridden for site', + 'mandatory' => false, + 'condition' => $pass, + 'memo' => $memo, + ); + } + + /** + * @return array + */ + function volumeWebAliasRequirement() + { + $pass = true; + $memo = 'Your @web alias is set correctly for your volumes.'; + if (Craft::$app->getRequest()->isWebAliasSetDynamically) { + $failedVolumes = []; + + foreach (Craft::$app->getVolumes()->getAllVolumes() as $volume) { + if ($volume->hasUrls && \craft\helpers\StringHelper::contains($volume->url, '@web')) { + $failedVolumes[] = $volume->name; + } + } + + if (!empty($failedVolumes)) { + $pass = false; + $memo = 'We recommend explicitly overriding the @web alias for the "' . (count($failedVolumes) === 1 ? $failedVolumes[0] . '" volume' : implode('", "', $failedVolumes) . '" volumes') . ' if you plan on using it.'; + } + } + + return array( + 'name' => 'Ensure @web alias is explicitly overridden for volumes', + 'mandatory' => false, + 'condition' => $pass, + 'memo' => $memo, + ); + } + /** * @return array */ diff --git a/server/requirements/requirements.php b/server/requirements/requirements.php index d24f35b..064299f 100644 --- a/server/requirements/requirements.php +++ b/server/requirements/requirements.php @@ -65,6 +65,8 @@ // Only run this requirement check if we're running in the context of Craft. if (class_exists('Craft')) { $requirements[] = $this->webrootRequirement(); + $requirements[] = $this->siteWebAliasRequirement(); + $requirements[] = $this->volumeWebAliasRequirement(); } $requirements = array_merge($requirements, array( From c44c2481bfc275832fb9d78a837dd51994d67cfe Mon Sep 17 00:00:00 2001 From: Brad Bell Date: Sat, 16 Apr 2022 17:49:38 -0700 Subject: [PATCH 2/3] web alias check take 2 --- server/requirements/RequirementsChecker.php | 47 ++++----------------- server/requirements/requirements.php | 3 +- 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/server/requirements/RequirementsChecker.php b/server/requirements/RequirementsChecker.php index 66534ed..9fa4413 100644 --- a/server/requirements/RequirementsChecker.php +++ b/server/requirements/RequirementsChecker.php @@ -541,50 +541,19 @@ function maxExecutionTimeRequirement() /** * @return array */ - function siteWebAliasRequirement() + function webAliasRequirement() { - $pass = true; - $memo = 'Your @web alias is set correctly for the site.'; - if (Craft::$app->getRequest()->isWebAliasSetDynamically) { - $baseUrl = Craft::$app->getSites()->getCurrentSite()->getBaseUrl(false); - $pass = !\craft\helpers\StringHelper::contains($baseUrl, '@web'); - if (!$pass) { - $memo = 'We recommend explicitly overriding the @web alias for your site if you plan on using it.'; - } - } - - return array( - 'name' => 'Ensure @web alias is explicitly overridden for site', - 'mandatory' => false, - 'condition' => $pass, - 'memo' => $memo, - ); - } + $aliases = Craft::$app->getConfig()->getGeneral()->aliases; + $memo = 'We recommend explicitly overriding the @web alias.'; + $pass = false; - /** - * @return array - */ - function volumeWebAliasRequirement() - { - $pass = true; - $memo = 'Your @web alias is set correctly for your volumes.'; - if (Craft::$app->getRequest()->isWebAliasSetDynamically) { - $failedVolumes = []; - - foreach (Craft::$app->getVolumes()->getAllVolumes() as $volume) { - if ($volume->hasUrls && \craft\helpers\StringHelper::contains($volume->url, '@web')) { - $failedVolumes[] = $volume->name; - } - } - - if (!empty($failedVolumes)) { - $pass = false; - $memo = 'We recommend explicitly overriding the @web alias for the "' . (count($failedVolumes) === 1 ? $failedVolumes[0] . '" volume' : implode('", "', $failedVolumes) . '" volumes') . ' if you plan on using it.'; - } + if (isset($aliases['web']) || isset($aliases['@web'])) { + $memo = 'Your @web alias is set correctly'; + $pass = true; } return array( - 'name' => 'Ensure @web alias is explicitly overridden for volumes', + 'name' => 'Ensure @web alias is explicitly overridden', 'mandatory' => false, 'condition' => $pass, 'memo' => $memo, diff --git a/server/requirements/requirements.php b/server/requirements/requirements.php index 064299f..3da2519 100644 --- a/server/requirements/requirements.php +++ b/server/requirements/requirements.php @@ -65,8 +65,7 @@ // Only run this requirement check if we're running in the context of Craft. if (class_exists('Craft')) { $requirements[] = $this->webrootRequirement(); - $requirements[] = $this->siteWebAliasRequirement(); - $requirements[] = $this->volumeWebAliasRequirement(); + $requirements[] = $this->webAliasRequirement(); } $requirements = array_merge($requirements, array( From e431d7ed18365868f593b2e767fb7f8e9ade3fd7 Mon Sep 17 00:00:00 2001 From: Brad Bell Date: Sat, 16 Apr 2022 17:50:02 -0700 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb4d1c8..d3e6181 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased ### Added -- Added checks for relying on the default `@web` alias for the site URL and asset base URLs. +- Added checks for relying on the default `@web` alias. ## 1.2.1 - 2021-05-25