3e5a32c2174ab90c21d08ad985b5f888bd286b60
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / PageErrorHandler / PageContentErrorHandler.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\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\Backend\Routing\PageUriBuilder;
22 use TYPO3\CMS\Core\Http\HtmlResponse;
23 use TYPO3\CMS\Core\LinkHandling\LinkService;
24 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException;
27
28 /**
29 * Renders the content of a page to be displayed (also in relation to language etc)
30 * This is typically configured via the "Sites configuration" module in the backend.
31 */
32 class PageContentErrorHandler implements PageErrorHandlerInterface
33 {
34
35 /**
36 * @var int
37 */
38 protected $statusCode;
39
40 /**
41 * @var array
42 */
43 protected $errorHandlerConfiguration;
44
45 /**
46 * PageContentErrorHandler constructor.
47 * @param int $statusCode
48 * @param array $configuration
49 * @throws InvalidArgumentException
50 */
51 public function __construct(int $statusCode, array $configuration)
52 {
53 $this->statusCode = $statusCode;
54 if (empty($configuration['errorContentSource'])) {
55 throw new InvalidArgumentException('PageContentErrorHandler needs to have a proper link set.', 1522826413);
56 }
57 $this->errorHandlerConfiguration = $configuration;
58 }
59
60 /**
61 * @param ServerRequestInterface $request
62 * @param string $message
63 * @param array $reasons
64 * @return ResponseInterface
65 */
66 public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
67 {
68 $resolvedUrl = $this->resolveUrl($request, $this->errorHandlerConfiguration['errorContentSource']);
69 $content = GeneralUtility::getUrl($resolvedUrl);
70 return new HtmlResponse($content, $this->statusCode);
71 }
72
73 /**
74 * Resolve the URL (currently only page and external URL are supported)
75 *
76 * @param ServerRequestInterface $request
77 * @param string $typoLinkUrl
78 * @return string
79 */
80 protected function resolveUrl(ServerRequestInterface $request, string $typoLinkUrl): string
81 {
82 $linkService = GeneralUtility::makeInstance(LinkService::class);
83 $urlParams = $linkService->resolve($typoLinkUrl);
84 if ($urlParams['type'] !== 'page' && $urlParams['type'] !== 'url') {
85 throw new \InvalidArgumentException('PageContentErrorHandler can only handle TYPO3 urls of types "page" or "url"', 1522826609);
86 }
87 if ($urlParams['type'] === 'url') {
88 return $urlParams['url'];
89 }
90
91 // Build Url
92 $languageUid = null;
93 $siteLanguage = $request->getAttribute('language');
94 if ($siteLanguage instanceof SiteLanguage) {
95 $languageUid = $siteLanguage->getLanguageId();
96 }
97 $uriBuilder = GeneralUtility::makeInstance(PageUriBuilder::class);
98 return (string)$uriBuilder->buildUri(
99 (int)$urlParams['pageuid'],
100 [],
101 null,
102 ['language' => $languageUid],
103 PageUriBuilder::ABSOLUTE_URL
104 );
105 }
106 }