[BUGFIX] Detect recursions in PageContentErrorHandler
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / PageErrorHandler / PageContentErrorHandler.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Error\PageErrorHandler;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use TYPO3\CMS\Core\Exception\SiteNotFoundException;
22 use TYPO3\CMS\Core\Http\HtmlResponse;
23 use TYPO3\CMS\Core\LinkHandling\LinkService;
24 use TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException;
25 use TYPO3\CMS\Core\Site\Entity\Site;
26 use TYPO3\CMS\Core\Site\SiteFinder;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Renders the content of a page to be displayed (also in relation to language etc)
31 * This is typically configured via the "Sites configuration" module in the backend.
32 */
33 class PageContentErrorHandler implements PageErrorHandlerInterface
34 {
35
36 /**
37 * @var int
38 */
39 protected $statusCode;
40
41 /**
42 * @var array
43 */
44 protected $errorHandlerConfiguration;
45
46 /**
47 * PageContentErrorHandler constructor.
48 * @param int $statusCode
49 * @param array $configuration
50 * @throws \InvalidArgumentException
51 */
52 public function __construct(int $statusCode, array $configuration)
53 {
54 $this->statusCode = $statusCode;
55 if (empty($configuration['errorContentSource'])) {
56 throw new \InvalidArgumentException('PageContentErrorHandler needs to have a proper link set.', 1522826413);
57 }
58 $this->errorHandlerConfiguration = $configuration;
59 }
60
61 /**
62 * @param ServerRequestInterface $request
63 * @param string $message
64 * @param array $reasons
65 * @return ResponseInterface
66 */
67 public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
68 {
69 try {
70 $resolvedUrl = $this->resolveUrl($request, $this->errorHandlerConfiguration['errorContentSource']);
71 $content = null;
72 if ($resolvedUrl !== (string)$request->getUri()) {
73 $content = GeneralUtility::getUrl($resolvedUrl);
74 }
75 if (!$content) {
76 $content = 'Error handler could not fetch error page: Possible recursion detected.';
77 }
78 } catch (InvalidRouteArgumentsException | SiteNotFoundException $e) {
79 $content = 'Invalid error handler configuration: ' . $this->errorHandlerConfiguration['errorContentSource'];
80 }
81 return new HtmlResponse($content, $this->statusCode);
82 }
83
84 /**
85 * Resolve the URL (currently only page and external URL are supported)
86 *
87 * @param ServerRequestInterface $request
88 * @param string $typoLinkUrl
89 * @return string
90 * @throws SiteNotFoundException
91 * @throws InvalidRouteArgumentsException
92 */
93 protected function resolveUrl(ServerRequestInterface $request, string $typoLinkUrl): string
94 {
95 $linkService = GeneralUtility::makeInstance(LinkService::class);
96 $urlParams = $linkService->resolve($typoLinkUrl);
97 if ($urlParams['type'] !== 'page' && $urlParams['type'] !== 'url') {
98 throw new \InvalidArgumentException('PageContentErrorHandler can only handle TYPO3 urls of types "page" or "url"', 1522826609);
99 }
100 if ($urlParams['type'] === 'url') {
101 return $urlParams['url'];
102 }
103
104 $site = $request->getAttribute('site', null);
105 if (!$site instanceof Site) {
106 $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId((int)$urlParams['pageuid']);
107 }
108 // Build Url
109 return (string)$site->getRouter()->generateUri(
110 (int)$urlParams['pageuid'],
111 ['_language' => $request->getAttribute('language', null)]
112 );
113 }
114 }