From 370acc0b3578317dc58366ba31c0f0d207a4f8e4 Mon Sep 17 00:00:00 2001 From: Ben Croker <57572400+bencroker@users.noreply.github.com> Date: Fri, 23 Feb 2024 11:11:13 -0600 Subject: [PATCH 1/8] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9d3758bd..61aed459 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require-dev": { }, "require": { - "php": "^8.0.2", + "php": "^8.2", "craftcms/cms": "^5.0.0-beta.1", "nystudio107/craft-plugin-vite": "^5.0.0-beta.1", "league/csv": "^8.2 || ^9.0", From 247ddf3581a05451ac542cda8e29bf38070ff540 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 6 Mar 2024 18:55:23 -0500 Subject: [PATCH 2/8] fix: Fixed an issue with impropertly text-encoded characters in URLs potentially causing a db exception ([#291](https://github.com/nystudio107/craft-retour/issues/291)) --- src/services/Redirects.php | 7 +++--- src/services/Statistics.php | 47 +++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/services/Redirects.php b/src/services/Redirects.php index c9309ee8..f02a12cc 100644 --- a/src/services/Redirects.php +++ b/src/services/Redirects.php @@ -24,6 +24,7 @@ use nystudio107\retour\events\RedirectResolvedEvent; use nystudio107\retour\events\ResolveRedirectEvent; use nystudio107\retour\fields\ShortLink; +use nystudio107\retour\helpers\Text as TextHelper; use nystudio107\retour\helpers\UrlHelper; use nystudio107\retour\models\StaticRedirects as StaticRedirectsModel; use nystudio107\retour\Retour; @@ -467,11 +468,11 @@ public function getStaticRedirect(string $fullUrl, string $pathOnly, $siteId, bo 'or', ['and', ['redirectSrcMatch' => 'pathonly'], - ['redirectSrcUrlParsed' => $pathOnly], + ['redirectSrcUrlParsed' => TextHelper::cleanupText($pathOnly)], ], ['and', ['redirectSrcMatch' => 'fullurl'], - ['redirectSrcUrlParsed' => $fullUrl], + ['redirectSrcUrlParsed' => TextHelper::cleanupText($fullUrl)], ], ]; @@ -1227,7 +1228,7 @@ public function getRedirectByRedirectSrcUrl(string $redirectSrcUrl, int $siteId // Query the db table $query = (new Query()) ->from(['{{%retour_static_redirects}}']) - ->where(['redirectSrcUrl' => $redirectSrcUrl]); + ->where(['redirectSrcUrl' => TextHelper::cleanupText($redirectSrcUrl)]); if ($siteId) { $query ->andWhere(['or', [ diff --git a/src/services/Statistics.php b/src/services/Statistics.php index ab1be1db..a09ac9c1 100644 --- a/src/services/Statistics.php +++ b/src/services/Statistics.php @@ -17,6 +17,7 @@ use craft\helpers\Db; use craft\helpers\UrlHelper; use DateTime; +use nystudio107\retour\helpers\Text as TextHelper; use nystudio107\retour\models\Stats as StatsModel; use nystudio107\retour\Retour; use yii\db\Exception; @@ -194,7 +195,7 @@ public function incrementStatistics(string $url, bool $handled = false, $siteId // Find any existing retour_stats record $statsConfig = (new Query()) ->from(['{{%retour_stats}}']) - ->where(['redirectSrcUrl' => $stats->redirectSrcUrl]) + ->where(['redirectSrcUrl' => TextHelper::cleanupText($stats->redirectSrcUrl)]) ->one(); // If no record is found, initialize some values if ($statsConfig === null) { @@ -275,28 +276,6 @@ public function saveStatistics(array $statsConfig): void } } - /** - * Don't trim more than a given interval, so that performance is not affected - * - * @return bool - */ - protected function rateLimited(): bool - { - $limited = false; - $now = round(microtime(true) * 1000); - $cache = Craft::$app->getCache(); - $then = $cache->get(self::LAST_STATISTICS_TRIM_CACHE_KEY); - if (($then !== false) && ($now - (int)$then < Retour::$settings->statisticsRateLimitMs)) { - $limited = true; - } - $cache->set(self::LAST_STATISTICS_TRIM_CACHE_KEY, $now, 0); - - return $limited; - } - - // Protected Methods - // ========================================================================= - /** * Trim the retour_stats db table based on the statsStoredLimit config.php * setting @@ -364,4 +343,26 @@ public function trimStatistics(int $limit = null): int return $affectedRows; } + + // Protected Methods + // ========================================================================= + + /** + * Don't trim more than a given interval, so that performance is not affected + * + * @return bool + */ + protected function rateLimited(): bool + { + $limited = false; + $now = round(microtime(true) * 1000); + $cache = Craft::$app->getCache(); + $then = $cache->get(self::LAST_STATISTICS_TRIM_CACHE_KEY); + if (($then !== false) && ($now - (int)$then < Retour::$settings->statisticsRateLimitMs)) { + $limited = true; + } + $cache->set(self::LAST_STATISTICS_TRIM_CACHE_KEY, $now, 0); + + return $limited; + } } From 36852e66bb85196b5d562c497413afbbde8da879 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 6 Mar 2024 18:56:19 -0500 Subject: [PATCH 3/8] chore: Version 5.0.0-beta.5 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff64a0b3..af1f2d61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Retour Changelog +## 5.0.0-beta.5 - UNRELEASED +### Fixed +* Fixed an issue with impropertly text-encoded characters in URLs potentially causing a db exception ([#291](https://github.com/nystudio107/craft-retour/issues/291)) + ## 5.0.0-beta.4 - 2024.02.09 ### Fixed * Fixed an issue with the Sites menu styling diff --git a/composer.json b/composer.json index 61aed459..32abb544 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-retour", "description": "Retour allows you to intelligently redirect legacy URLs, so that you don't lose SEO value when rebuilding & restructuring a website", "type": "craft-plugin", - "version": "5.0.0-beta.4", + "version": "5.0.0-beta.5", "keywords": [ "craftcms", "craft-plugin", From cc515313f701e31e0e6c4a733185c0b68ca28e02 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 7 Mar 2024 16:44:00 -0500 Subject: [PATCH 4/8] refactor: Add codeception --- composer.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/composer.json b/composer.json index 32abb544..62569a9a 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,12 @@ "jean85/pretty-package-versions": "^1.5 || ^2.0" }, "require-dev": { + "codeception/codeception": "^5.0.11", + "codeception/module-asserts": "^3.0.0", + "codeception/module-datafactory": "^3.0.0", + "codeception/module-phpbrowser": "^3.0.0", + "codeception/module-rest": "^3.3.2", + "codeception/module-yii2": "^1.1.9", "craftcms/ecs": "dev-main", "craftcms/phpstan": "dev-main", "craftcms/rector": "dev-main" From 67fbc844e46d5c34e7a66598d0564989baa81ab6 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sun, 10 Mar 2024 12:12:12 -0400 Subject: [PATCH 5/8] refactor: Add version faceted search to config --- docs/docs/.vitepress/config.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/docs/.vitepress/config.ts b/docs/docs/.vitepress/config.ts index b24da2a6..6713dbe6 100644 --- a/docs/docs/.vitepress/config.ts +++ b/docs/docs/.vitepress/config.ts @@ -24,7 +24,10 @@ export default defineConfig({ algolia: { appId: 'PBLZ7FT9Z3', apiKey: 'ab56b755c575dc94a58f7d1cae6e4e0e', - indexName: 'retour' + indexName: 'retour', + searchParameters: { + facetFilters: ["version:v5"], + }, }, lastUpdatedText: 'Last Updated', sidebar: [ From 630b4c7faf761080aa6a98a1e2b7f95f01593c12 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sun, 10 Mar 2024 12:15:58 -0400 Subject: [PATCH 6/8] fix: Fix API key --- docs/docs/.vitepress/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/.vitepress/config.ts b/docs/docs/.vitepress/config.ts index 6713dbe6..1787e831 100644 --- a/docs/docs/.vitepress/config.ts +++ b/docs/docs/.vitepress/config.ts @@ -23,7 +23,7 @@ export default defineConfig({ }, algolia: { appId: 'PBLZ7FT9Z3', - apiKey: 'ab56b755c575dc94a58f7d1cae6e4e0e', + apiKey: '953923b236f39535b8553f4143cab98d', indexName: 'retour', searchParameters: { facetFilters: ["version:v5"], From 8fe60a1092580bff68dd0eb9e4792c638509f2c2 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 25 Mar 2024 09:50:19 -0400 Subject: [PATCH 7/8] fix: Fixed an issue where an empty redirect in the `excludePatterns` list could cause redirects to stop functioning, add logging when a redirect is excluded ([#297](https://github.com/nystudio107/craft-retour/issues/297)) --- src/services/Redirects.php | 18 ++++++++++++++++-- src/translations/en/retour.php | 1 + 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/services/Redirects.php b/src/services/Redirects.php index f02a12cc..1f777e08 100644 --- a/src/services/Redirects.php +++ b/src/services/Redirects.php @@ -16,6 +16,7 @@ use craft\base\ElementInterface; use craft\base\Plugin; use craft\db\Query; +use craft\errors\ElementNotFoundException; use craft\errors\SiteNotFoundException; use craft\helpers\Db; use craft\helpers\StringHelper; @@ -28,6 +29,7 @@ use nystudio107\retour\helpers\UrlHelper; use nystudio107\retour\models\StaticRedirects as StaticRedirectsModel; use nystudio107\retour\Retour; +use Throwable; use yii\base\ExitException; use yii\base\InvalidConfigException; use yii\base\InvalidRouteException; @@ -273,9 +275,21 @@ public function excludeUri($uri): bool $uri = '/' . ltrim($uri, '/'); if (!empty(Retour::$settings->excludePatterns)) { foreach (Retour::$settings->excludePatterns as $excludePattern) { + if (empty($excludePattern['pattern'])) { + continue; + } $pattern = '`' . $excludePattern['pattern'] . '`i'; try { if (preg_match($pattern, $uri) === 1) { + Craft::info( + Craft::t( + 'retour', + 'Excluded URI: {uri} due to match of pattern: {pattern}', + ['uri' => $uri, 'pathOnly' => $pattern] + ), + __METHOD__ + ); + return true; } } catch (\Exception $e) { @@ -1042,8 +1056,8 @@ public function getRedirectsByElementId(int $elementId, int $siteId = null) * Delete a short link by its ID. * * @param int $redirectId - * @throws \Throwable - * @throws \craft\errors\ElementNotFoundException + * @throws Throwable + * @throws ElementNotFoundException * @throws \yii\base\Exception */ public function deleteShortlinkById(int $redirectId): bool diff --git a/src/translations/en/retour.php b/src/translations/en/retour.php index d623eb5d..36885d26 100644 --- a/src/translations/en/retour.php +++ b/src/translations/en/retour.php @@ -171,4 +171,5 @@ 'Some errors occured .' => 'Some errors occured .', 'Some errors occured importing the CSV file.' => 'Some errors occured importing the CSV file.', 'Select the priority that will determine the order of mathching the redirect.' => 'Select the priority that will determine the order of mathching the redirect.', + 'Excluded URI: {uri} due to match of pattern: {pattern}' => 'Excluded URI: {uri} due to match of pattern: {pattern}' ]; From f200bc206a33f4441860593c961f2a34338f9ab9 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 25 Mar 2024 09:51:46 -0400 Subject: [PATCH 8/8] chore: Version 5.0.0-beta.5 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af1f2d61..a8b5dadd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Retour Changelog -## 5.0.0-beta.5 - UNRELEASED +## 5.0.0-beta.5 - 2024.03.25 ### Fixed * Fixed an issue with impropertly text-encoded characters in URLs potentially causing a db exception ([#291](https://github.com/nystudio107/craft-retour/issues/291)) +* Fixed an issue where an empty redirect in the `excludePatterns` list could cause redirects to stop functioning, add logging when a redirect is excluded ([#297](https://github.com/nystudio107/craft-retour/issues/297)) ## 5.0.0-beta.4 - 2024.02.09 ### Fixed