2 declare(strict_types = 1);
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
46 public function unavailableAction(ServerRequestInterface $request,
string $message, array $reasons = []): ResponseInterface
53 return $errorHandler->handlePageError($request, $message, $reasons);
56 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling'],
57 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling_statheader'],
73 public function pageNotFoundAction(ServerRequestInterface $request,
string $message, array $reasons = []): ResponseInterface
77 return $errorHandler->handlePageError($request, $message, $reasons);
79 if (!
$GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling']) {
83 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling'],
84 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling_statheader'],
99 public function accessDeniedAction(ServerRequestInterface $request,
string $message, array $reasons = []): ResponseInterface
103 return $errorHandler->handlePageError($request, $message, $reasons);
106 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling'],
107 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling_accessdeniedheader'],
122 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling']
123 && !GeneralUtility::cmpIP(
124 GeneralUtility::getIndpEnv(
'REMOTE_ADDR'),
125 $GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'devIPmask']
140 protected function handlePageError($errorHandler,
string $header =
'',
string $reason =
'', array $pageAccessFailureReasons = []): ResponseInterface
145 if (is_bool($errorHandler) || strtolower($errorHandler) ===
'true' || (
string)$errorHandler ===
'1') {
146 $content = GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
148 'The page did not exist or was inaccessible.' . ($reason ?
' Reason: ' . $reason :
'')
150 } elseif (GeneralUtility::isFirstPartOfStr($errorHandler,
'USER_FUNCTION:')) {
151 $funcRef = trim(substr($errorHandler, 14));
153 'currentUrl' => GeneralUtility::getIndpEnv(
'REQUEST_URI'),
154 'reasonText' => $reason,
155 'pageAccessFailureReasons' => $pageAccessFailureReasons
158 $content = GeneralUtility::callUserFunction($funcRef, $params, $this);
160 throw new \RuntimeException(
'Error: 404 page by USER_FUNCTION "' . $funcRef .
'" failed.', 1518472235, $e);
162 } elseif (GeneralUtility::isFirstPartOfStr($errorHandler,
'READFILE:')) {
163 $readFile = GeneralUtility::getFileAbsFileName(trim(substr($errorHandler, 9)));
164 if (@is_file($readFile)) {
165 $content = str_replace(
171 GeneralUtility::getIndpEnv(
'REQUEST_URI'),
172 htmlspecialchars($reason)
174 file_get_contents($readFile)
177 throw new \RuntimeException(
'Configuration Error: 404 page "' . $readFile .
'" could not be found.', 1518472245);
179 } elseif (GeneralUtility::isFirstPartOfStr($errorHandler,
'REDIRECT:')) {
181 } elseif ($errorHandler !==
'') {
183 $urlParts = parse_url($errorHandler);
185 if (empty($urlParts[
'host'])) {
186 $urlParts[
'host'] = GeneralUtility::getIndpEnv(
'HTTP_HOST');
187 if ($errorHandler[0] ===
'/') {
188 $errorHandler = GeneralUtility::getIndpEnv(
'TYPO3_REQUEST_HOST') . $errorHandler;
190 $errorHandler = GeneralUtility::getIndpEnv(
'TYPO3_REQUEST_DIR') . $errorHandler;
192 $checkBaseTag =
false;
194 $checkBaseTag =
true;
197 if ($errorHandler === GeneralUtility::getIndpEnv(
'TYPO3_REQUEST_URL')) {
198 $reason = $reason ?:
'Page cannot be found.';
199 $reason .= LF . LF .
'Additionally, ' . $errorHandler .
' was not found while trying to retrieve the error document.';
200 throw new \RuntimeException(nl2br(htmlspecialchars($reason)), 1518472252);
204 'User-agent' => GeneralUtility::getIndpEnv(
'HTTP_USER_AGENT'),
205 'Referer' => GeneralUtility::getIndpEnv(
'TYPO3_REQUEST_URL')
208 $res = GeneralUtility::getUrl($errorHandler, 1, $requestHeaders, $report);
209 if ((
int)$report[
'error'] !== 0 && (
int)$report[
'error'] !== 200) {
210 throw new \RuntimeException(
'Failed to fetch error page "' . $errorHandler .
'", reason: ' . $report[
'message'], 1518472257);
212 if ($res ===
false) {
217 list($returnedHeaders, $content) = explode(CRLF . CRLF, $res, 2);
223 $headerArr = preg_split(
'/\\r|\\n/', $returnedHeaders, -1, PREG_SPLIT_NO_EMPTY);
224 foreach ($headerArr as $headerLine) {
225 foreach ($forwardHeaders as $h) {
226 if (preg_match(
'/^' . $h .
'/', $headerLine)) {
227 $header .= CRLF . $headerLine;
234 if (
false === stristr($content,
'<base ')) {
236 $base = $urlParts[
'scheme'] .
'://';
237 if ($urlParts[
'user'] !=
'') {
238 $base .= $urlParts[
'user'];
239 if ($urlParts[
'pass'] !=
'') {
240 $base .=
':' . $urlParts[
'pass'];
244 $base .= $urlParts[
'host'];
246 $base .= preg_replace(
'/(.*\\/)[^\\/]*/',
'${1}', $urlParts[
'path']);
248 $replacement = LF .
'<base href="' . htmlentities($base) .
'" />' . LF;
249 if (stristr($content,
'<head>')) {
250 $content = preg_replace(
'/(<head>)/i',
'\\1' . $replacement, $content);
252 $content = preg_replace(
'/(<html[^>]*>)/i',
'\\1<head>' . $replacement .
'</head>', $content);
258 $content = GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
260 $reason ?
'Reason: ' . $reason :
'Page cannot be found.'
280 if (!empty($headers)) {
281 $headerArr = preg_split(
'/\\r|\\n/', $headers, -1, PREG_SPLIT_NO_EMPTY);
282 foreach ($headerArr as $headerLine) {
283 if (strpos($headerLine,
'HTTP/') === 0 && strpos($headerLine,
':') ===
false) {
284 list($protocolVersion, $statusCode, $reasonPhrase) = explode(
' ', $headerLine, 3);
285 list(, $protocolVersion) = explode(
'/', $protocolVersion, 2);
286 $response = $response
287 ->withProtocolVersion((
int)$protocolVersion)
288 ->withStatus($statusCode, $reasonPhrase);
290 list($headerName, $value) = GeneralUtility::trimExplode(
':', $headerLine, 2);
291 $response = $response->withHeader($headerName, $value);
307 $site = $request->getAttribute(
'site');
308 if ($site instanceof
Site) {
310 return $site->getErrorHandler($statusCode);