Skip to content

Commit

Permalink
[BUGFIX] Properly handle unknown link type
Browse files Browse the repository at this point in the history
In case an invalid link type is used in
a link field, the link browser does no
longer throw an exception but instead
displays a flash message with the
corresponding information.

Additionally, an undefined array key access
is resolved, which might happen e.g. in
case such invalid link is being analyzed
by link validator.

Resolves: #102804
Releases: main, 12.4
Change-Id: I5ad35ff46360cc4b27556b8e2c75da69ecbe76d9
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/82387
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Nikita Hovratov <nikita.h@live.de>
Tested-by: Nikita Hovratov <nikita.h@live.de>
  • Loading branch information
o-ba authored and nhovratov committed Jan 10, 2024
1 parent 17bab36 commit 931fc9a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
19 changes: 15 additions & 4 deletions typo3/sysext/backend/Classes/Controller/LinkBrowserController.php
Expand Up @@ -21,9 +21,13 @@
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\LinkHandling\Exception\UnknownLinkHandlerException;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand All @@ -34,6 +38,7 @@ class LinkBrowserController extends AbstractLinkBrowserController
public function __construct(
protected readonly LinkService $linkService,
protected readonly TypoLinkCodecService $typoLinkCodecService,
protected readonly FlashMessageService $flashMessageService,
) {}

public function getConfiguration(): array
Expand Down Expand Up @@ -96,10 +101,16 @@ protected function initCurrentUrl(): void
unset($currentLinkParts['additionalParams']);

if (!empty($currentLinkParts['url'])) {
$data = $this->linkService->resolve($currentLinkParts['url']);
$currentLinkParts['type'] = $data['type'];
unset($data['type']);
$currentLinkParts['url'] = $data;
try {
$data = $this->linkService->resolve($currentLinkParts['url']);
$currentLinkParts['type'] = $data['type'];
unset($data['type']);
$currentLinkParts['url'] = $data;
} catch (UnknownLinkHandlerException $e) {
$this->flashMessageService->getMessageQueueByIdentifier()->enqueue(
new FlashMessage(message: $e->getMessage(), severity: ContextualFeedbackSeverity::ERROR)
);
}
}

$this->currentLinkParts = $currentLinkParts;
Expand Down
Expand Up @@ -162,7 +162,7 @@ protected function setTypoLinkPartsElement($tLP, &$elements, $content, $idx)
$elements[$tokenID . ':' . $idx] = [];
$elements[$tokenID . ':' . $idx]['matchString'] = $content;
// Based on link type, maybe do more:
switch ((string)$tLP['type']) {
switch ((string)($tLP['type'] ?? '')) {
case LinkService::TYPE_EMAIL:
// Mail addresses can be substituted manually:
$elements[$tokenID . ':' . $idx]['subst'] = [
Expand Down
Expand Up @@ -20,10 +20,14 @@
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Controller\AbstractLinkBrowserController;
use TYPO3\CMS\Core\Configuration\Richtext;
use TYPO3\CMS\Core\LinkHandling\Exception\UnknownLinkHandlerException;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\View\ViewInterface;

Expand Down Expand Up @@ -52,6 +56,7 @@ public function __construct(
protected readonly LinkService $linkService,
protected readonly Richtext $richtext,
protected readonly LanguageServiceFactory $languageServiceFactory,
protected readonly FlashMessageService $flashMessageService,
) {}

/**
Expand Down Expand Up @@ -117,12 +122,18 @@ protected function initCurrentUrl(): void
return;
}
if (!empty($this->currentLinkParts['url'])) {
$data = $this->linkService->resolve($this->currentLinkParts['url']);
$this->currentLinkParts['type'] = $data['type'];
unset($data['type']);
$this->currentLinkParts['url'] = $data;
if (!empty($this->currentLinkParts['url']['parameters'])) {
$this->currentLinkParts['params'] = '&' . $this->currentLinkParts['url']['parameters'];
try {
$data = $this->linkService->resolve($this->currentLinkParts['url']);
$this->currentLinkParts['type'] = $data['type'];
unset($data['type']);
$this->currentLinkParts['url'] = $data;
if (!empty($this->currentLinkParts['url']['parameters'])) {
$this->currentLinkParts['params'] = '&' . $this->currentLinkParts['url']['parameters'];
}
} catch (UnknownLinkHandlerException $e) {
$this->flashMessageService->getMessageQueueByIdentifier()->enqueue(
new FlashMessage(message: $e->getMessage(), severity: ContextualFeedbackSeverity::ERROR)
);
}
}
parent::initCurrentUrl();
Expand Down

0 comments on commit 931fc9a

Please sign in to comment.