2 declare(strict_types = 1);
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
64 if (empty($configuration[
'errorContentSource'])) {
65 throw new \InvalidArgumentException(
'PageContentErrorHandler needs to have a proper link set.', 1522826413);
67 $this->errorHandlerConfiguration = $configuration;
78 public function handlePageError(ServerRequestInterface $request,
string $message, array $reasons = []): ResponseInterface
81 $resolvedUrl = $this->
resolveUrl($request, $this->errorHandlerConfiguration[
'errorContentSource']);
83 $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache(
'cache_pages');
84 $cacheIdentifier =
'errorPage_' . md5($resolvedUrl);
85 $cacheContent = $cache->get($cacheIdentifier);
87 if (!$cacheContent && $resolvedUrl !== (
string)$request->getUri()) {
89 $subResponse = GeneralUtility::makeInstance(RequestFactory::class)
92 throw new \RuntimeException(
'Error handler could not fetch error page "' . $resolvedUrl .
'", reason: ' . $e->getMessage(), 1544172838);
94 if ($subResponse->getStatusCode() >= 300) {
95 throw new \RuntimeException(
'Error handler could not fetch error page "' . $resolvedUrl .
'", status code: ' . $subResponse->getStatusCode(), 1544172839);
98 $body = $subResponse->getBody()->getContents();
99 $contentType = $subResponse->getHeader(
'Content-Type');
102 if ($subResponse->getStatusCode() === 200) {
103 $cacheTags = [
'errorPage'];
104 if ($this->pageUid > 0) {
110 'headers' => [
'Content-Type' => $contentType],
112 $cache->set($cacheIdentifier, $cacheContent, $cacheTags);
115 if ($cacheContent && $cacheContent[
'body'] && $cacheContent[
'headers']) {
117 return new HtmlResponse($cacheContent[
'body'], $this->statusCode, $cacheContent[
'headers']);
120 $content =
'Invalid error handler configuration: ' . $this->errorHandlerConfiguration[
'errorContentSource'];
133 if ((
int)
$GLOBALS[
'TYPO3_CONF_VARS'][
'HTTP'][
'timeout'] === 0) {
150 protected function resolveUrl(ServerRequestInterface $request,
string $typoLinkUrl):
string
152 $linkService = GeneralUtility::makeInstance(LinkService::class);
153 $urlParams = $linkService->resolve($typoLinkUrl);
154 if ($urlParams[
'type'] !==
'page' && $urlParams[
'type'] !==
'url') {
155 throw new \InvalidArgumentException(
'PageContentErrorHandler can only handle TYPO3 urls of types "page" or "url"', 1522826609);
157 if ($urlParams[
'type'] ===
'url') {
158 return $urlParams[
'url'];
161 $this->pageUid = (int)$urlParams[
'pageuid'];
164 $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($this->pageUid);
166 if (!$site instanceof
Site) {
167 $site = $request->getAttribute(
'site',
null);
170 $requestLanguage = $request->getAttribute(
'language',
null);
174 $language = $site->getLanguageById($requestLanguage->getLanguageId());
175 }
catch (\InvalidArgumentException $e) {
176 $language = $site->getDefaultLanguage();
179 $language = $site->getDefaultLanguage();
183 $uri = $site->getRouter()->generateUri(
184 (
int)$urlParams[
'pageuid'],
185 [
'_language' => $language]
189 $currentUri = $request->getUri();
190 if (empty($uri->getScheme())) {
191 $uri = $uri->withScheme($currentUri->getScheme());
193 if (empty($uri->getUserInfo())) {
194 $uri = $uri->withUserInfo($currentUri->getUserInfo());
196 if (empty($uri->getHost())) {
197 $uri = $uri->withHost($currentUri->getHost());
199 if ($uri->getPort() ===
null) {
200 $uri = $uri->withPort($currentUri->getPort());