Skip to content

Commit

Permalink
[BUGIX] Disable locate on configured error pages because a subrespons…
Browse files Browse the repository at this point in the history
…e in PageContentErrorHandler.php cannot handle redirect statuscodes
  • Loading branch information
MFabse committed Apr 2, 2024
1 parent ecbce99 commit 50f03f9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
87 changes: 62 additions & 25 deletions Classes/Middleware/LanguageRedirectMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,76 @@
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager;
use TYPO3\CMS\Core\LinkHandling\LinkService;

class LanguageRedirectMiddleware implements MiddlewareInterface
final class LanguageRedirectMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly BackendConfigurationManager $backendConfigurationManager,
private readonly LinkService $link
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$typoScript = $this->getTypoScriptSetup();

if (isset($typoScript['config.']['tx_locate']) && (int)$typoScript['config.']['tx_locate'] === 1) {
$locateSetup = $typoScript['config.']['tx_locate.'];

$config = [
'verdicts' => $locateSetup['verdicts.'] ?? [],
'facts' => $locateSetup['facts.'] ?? [],
'judges' => $locateSetup['judges.'] ?? [],
'settings' => [
'dryRun' => (bool)($locateSetup['dryRun'] ?? false),
'overrideQueryParameter' => $locateSetup['overrideQueryParameter'] ?? Redirect::OVERRIDE_PARAMETER,
'overrideSessionValue' => (bool)($locateSetup['overrideSessionValue'] ?? 0),
'sessionHandling' => (bool)($locateSetup['sessionHandling'] ?? 0),
'excludeBots' => (bool)($locateSetup['excludeBots'] ?? 1),
'simulateIp' => (string)($locateSetup['simulateIp'] ?? ''),
],
];

return GeneralUtility::makeInstance(Court::class, $config)->run() ?? $handler->handle($request);
if (!$this->isErrorPage($request)) {
$typoScript = $this->backendConfigurationManager->getTypoScriptSetup();

if (isset($typoScript['config.']['tx_locate']) && (int)$typoScript['config.']['tx_locate'] === 1) {

$locateSetup = $typoScript['config.']['tx_locate.'];

$config = [
'verdicts' => $locateSetup['verdicts.'] ?? [],
'facts' => $locateSetup['facts.'] ?? [],
'judges' => $locateSetup['judges.'] ?? [],
'settings' => [
'dryRun' => (bool)($locateSetup['dryRun'] ?? false),
'overrideQueryParameter' => $locateSetup['overrideQueryParameter'] ?? Redirect::OVERRIDE_PARAMETER,
'overrideSessionValue' => (bool)($locateSetup['overrideSessionValue'] ?? 0),
'sessionHandling' => (bool)($locateSetup['sessionHandling'] ?? 0),
'excludeBots' => (bool)($locateSetup['excludeBots'] ?? 1),
'simulateIp' => (string)($locateSetup['simulateIp'] ?? ''),
],
];

return GeneralUtility::makeInstance(Court::class, $config)->run() ?? $handler->handle($request);
}
}

return $handler->handle($request);
}

protected function getTypoScriptSetup(): array
private function isErrorPage(ServerRequestInterface $request): bool
{
$siteConfig = $request->getAttribute('site')->getConfiguration();
$routing = $request->getAttribute('routing');

if ($routing && is_array($siteConfig['errorHandling'] ?? null)) {
$errorHandlers = $siteConfig['errorHandling'];
$requestPageUid = $routing->getPageId();

if (in_array($requestPageUid, $this->getErrorPageUids($errorHandlers))) {
return true;
}
}

return false;
}

private function getErrorPageUids(array $errorHandlers): array
{
$backendConfigurationManager = GeneralUtility::makeInstance(BackendConfigurationManager::class);
return $backendConfigurationManager->getTypoScriptSetup();
$errorPageUids = [];

foreach ($errorHandlers as $errorHandler) {
if (isset($errorHandler['errorContentSource'])) {
$pageUid = $this->link->resolve($errorHandler['errorContentSource'])['pageuid'] ?? null;
if ($pageUid) {
$errorPageUids[] = $pageUid;
}
}
}

return $errorPageUids;
}
}
}
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ services:
- name: console.command
command: locate:update
schedulable: false

Leuchtfeuer\Locate\Middleware\LanguageRedirectMiddleware:
public: true

0 comments on commit 50f03f9

Please sign in to comment.