From ed3a3f491d3f92bf9080ea4ebab4800a6d5fe4e2 Mon Sep 17 00:00:00 2001 From: Leo Feyer Date: Tue, 12 May 2020 16:19:37 +0200 Subject: [PATCH] Redirect if a news/event has an external target and is called via the default URL (see #1732) Description ----------- | Q | A | -----------------| --- | Fixed issues | Fixes #1707 #1498 | Docs PR or issue | - Commits ------- 972627f9 Redirect if a news/event has an external target and is called via the default URL ef111218 Show the teaser if there are no details --- .../src/Resources/contao/classes/Events.php | 1 - .../contao/modules/ModuleEventReader.php | 36 +++++++++++++++++-- .../contao/templates/events/event_full.html5 | 2 +- .../contao/templates/events/event_list.html5 | 2 +- .../contao/modules/ModuleNewsReader.php | 35 ++++++++++++++++-- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/calendar-bundle/src/Resources/contao/classes/Events.php b/calendar-bundle/src/Resources/contao/classes/Events.php index cbb999575b1..764e11b8a10 100644 --- a/calendar-bundle/src/Resources/contao/classes/Events.php +++ b/calendar-bundle/src/Resources/contao/classes/Events.php @@ -333,7 +333,6 @@ protected function addEvent($objEvents, $intStart, $intEnd, $intBegin, $intLimit // Display the "read more" button for external/article links if ($objEvents->source != 'default') { - $arrEvent['details'] = true; $arrEvent['hasDetails'] = true; } diff --git a/calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php b/calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php index 951ca24e5ab..26d10672198 100644 --- a/calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php +++ b/calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php @@ -12,6 +12,7 @@ use Contao\CoreBundle\Exception\InternalServerErrorException; use Contao\CoreBundle\Exception\PageNotFoundException; +use Contao\CoreBundle\Exception\RedirectResponseException; use FOS\HttpCache\ResponseTagger; use Patchwork\Utf8; @@ -91,12 +92,42 @@ protected function compile() // Get the current event $objEvent = CalendarEventsModel::findPublishedByParentAndIdOrAlias(Input::get('events'), $this->cal_calendar); - // The event does not exist or has an external target (see #33) - if (null === $objEvent || $objEvent->source != 'default') + // The event does not exist (see #33) + if ($objEvent === null) { throw new PageNotFoundException('Page not found: ' . Environment::get('uri')); } + // Redirect if the event has a target URL (see #1498) + switch ($objEvent->source) { + case 'internal': + if ($page = PageModel::findPublishedById($objEvent->jumpTo)) + { + throw new RedirectResponseException($page->getAbsoluteUrl(), 301); + } + + throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public'); + break; + + case 'article': + if (($article = ArticleModel::findByPk($objEvent->articleId)) && ($page = PageModel::findPublishedById($article->pid))) + { + throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301); + } + + throw new InternalServerErrorException('Invalid "articleId" value or target page not public'); + break; + + case 'external': + if ($objEvent->url) + { + throw new RedirectResponseException($objEvent->url, 301); + } + + throw new InternalServerErrorException('Empty target URL'); + break; + } + // Overwrite the page title (see #2853, #4955 and #87) if ($objEvent->pageTitle) { @@ -228,7 +259,6 @@ protected function compile() // Display the "read more" button for external/article links if ($objEvent->source != 'default') { - $objTemplate->details = true; $objTemplate->hasDetails = true; } diff --git a/calendar-bundle/src/Resources/contao/templates/events/event_full.html5 b/calendar-bundle/src/Resources/contao/templates/events/event_full.html5 index 58aacc0ba35..44aa39f3028 100644 --- a/calendar-bundle/src/Resources/contao/templates/events/event_full.html5 +++ b/calendar-bundle/src/Resources/contao/templates/events/event_full.html5 @@ -18,7 +18,7 @@

- hasDetails): ?> + details): ?> details ?>
diff --git a/calendar-bundle/src/Resources/contao/templates/events/event_list.html5 b/calendar-bundle/src/Resources/contao/templates/events/event_list.html5 index b45b1e03465..eb291305e5e 100644 --- a/calendar-bundle/src/Resources/contao/templates/events/event_list.html5 +++ b/calendar-bundle/src/Resources/contao/templates/events/event_list.html5 @@ -23,7 +23,7 @@ insert('image', $this->arrData); ?> - hasDetails): ?> + details): ?> details ?>
diff --git a/news-bundle/src/Resources/contao/modules/ModuleNewsReader.php b/news-bundle/src/Resources/contao/modules/ModuleNewsReader.php index c258987bd61..ddcd525e905 100644 --- a/news-bundle/src/Resources/contao/modules/ModuleNewsReader.php +++ b/news-bundle/src/Resources/contao/modules/ModuleNewsReader.php @@ -12,6 +12,7 @@ use Contao\CoreBundle\Exception\InternalServerErrorException; use Contao\CoreBundle\Exception\PageNotFoundException; +use Contao\CoreBundle\Exception\RedirectResponseException; use Patchwork\Utf8; /** @@ -89,12 +90,42 @@ protected function compile() // Get the news item $objArticle = NewsModel::findPublishedByParentAndIdOrAlias(Input::get('items'), $this->news_archives); - // The news item does not exist or has an external target (see #33) - if (null === $objArticle || $objArticle->source != 'default') + // The news item does not exist (see #33) + if ($objArticle === null) { throw new PageNotFoundException('Page not found: ' . Environment::get('uri')); } + // Redirect if the news item has a target URL (see #1498) + switch ($objArticle->source) { + case 'internal': + if ($page = PageModel::findPublishedById($objArticle->jumpTo)) + { + throw new RedirectResponseException($page->getAbsoluteUrl(), 301); + } + + throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public'); + break; + + case 'article': + if (($article = ArticleModel::findByPk($objArticle->articleId)) && ($page = PageModel::findPublishedById($article->pid))) + { + throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301); + } + + throw new InternalServerErrorException('Invalid "articleId" value or target page not public'); + break; + + case 'external': + if ($objArticle->url) + { + throw new RedirectResponseException($objArticle->url, 301); + } + + throw new InternalServerErrorException('Empty target URL'); + break; + } + // Set the default template if (!$this->news_template) {