From 0a53f11dafcc414da9b36c471bdc614cb6e5e77e Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 6 Mar 2024 18:54:54 -0500 Subject: [PATCH] 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 | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/services/Redirects.php b/src/services/Redirects.php index 8cf36d93..e59d7dd8 100644 --- a/src/services/Redirects.php +++ b/src/services/Redirects.php @@ -25,6 +25,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; @@ -706,11 +707,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)], ], ]; @@ -860,7 +861,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 5899ef08..1e613b5b 100644 --- a/src/services/Statistics.php +++ b/src/services/Statistics.php @@ -16,6 +16,8 @@ use craft\db\Query; 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; @@ -193,7 +195,7 @@ public function incrementStatistics(string $url, $handled = false, $siteId = nul // 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) { @@ -211,7 +213,7 @@ public function incrementStatistics(string $url, $handled = false, $siteId = nul $stats->exceptionMessage = $exceptionMessage; $stats->exceptionFilePath = $exceptionFilePath; $stats->exceptionFileLine = (int)$exceptionFileLine; - $stats->hitLastTime = Db::prepareDateForDb(new \DateTime()); + $stats->hitLastTime = Db::prepareDateForDb(new DateTime()); $stats->handledByRetour = (int)$handled; $stats->hitCount++; $statsConfig = $stats->getAttributes();