ErrorHandler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Opentalent\OtTemplating\Page;
  3. use Opentalent\OtCore\Website\OtPageRepository;
  4. use Opentalent\OtCore\Website\OtWebsiteRepository;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use TYPO3\CMS\Core\Error\PageErrorHandler\PageContentErrorHandler;
  8. use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
  9. use TYPO3\CMS\Core\Http\HtmlResponse;
  10. use TYPO3\CMS\Core\Utility\GeneralUtility;
  11. use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
  12. use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
  13. use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
  14. use TYPO3\CMS\Extbase\Object\ObjectManager;
  15. use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
  16. use TYPO3\CMS\Fluid\View\TemplateView;
  17. class ErrorHandler implements PageErrorHandlerInterface
  18. {
  19. const TEMPLATES_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Templates';
  20. const LAYOUTS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Layouts';
  21. const PARTIALS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Partials';
  22. const TEMPLATE_FILES = [
  23. 403 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/403.html',
  24. 404 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/404.html',
  25. 500 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/500.html'
  26. ];
  27. const REDIRECT_FALLBACK = 'https://opentalent.fr';
  28. /**
  29. * Status code of the response
  30. * @var int
  31. */
  32. protected int $statusCode;
  33. /**
  34. * Configuration of the error handler as set in site configuration.
  35. * @var array
  36. */
  37. protected array $errorHandlerConfiguration;
  38. /**
  39. * @var ViewInterface
  40. */
  41. protected $view;
  42. public function __construct(int $statusCode, array $configuration)
  43. {
  44. $this->statusCode = $statusCode;
  45. $this->errorHandlerConfiguration = $configuration;
  46. $this->view = GeneralUtility::makeInstance(TemplateView::class);
  47. $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
  48. $controllerContext = $objectManager->get(ControllerContext::class);
  49. $renderingContext = $objectManager->get(RenderingContext::class);
  50. $webRequest = $objectManager->get(\TYPO3\CMS\Extbase\Mvc\Request::class);
  51. $webRequest->setControllerExtensionName('ot_templating');
  52. $controllerContext->setRequest($webRequest);
  53. $renderingContext->setControllerContext($controllerContext);
  54. $this->view->setRenderingContext($renderingContext);
  55. $this->view->setTemplateRootPaths([self::TEMPLATES_ROOT_PATHS]);
  56. $this->view->setLayoutRootPaths([self::LAYOUTS_ROOT_PATHS]);
  57. $this->view->setPartialRootPaths([self::PARTIALS_ROOT_PATHS]);
  58. $this->view->setTemplatePathAndFilename(
  59. GeneralUtility::getFileAbsFileName(self::TEMPLATE_FILES[$statusCode])
  60. );
  61. }
  62. /**
  63. * @param ServerRequestInterface $request
  64. * @param string $message
  65. * @param array $reasons
  66. * @return ResponseInterface
  67. */
  68. public function handlePageError(
  69. ServerRequestInterface $request,
  70. string $message,
  71. array $reasons = []
  72. ): ResponseInterface {
  73. $homeUri = "https://opentalent.fr";
  74. $title = 'Page Introuvable';
  75. // This variable aims to prevent redirection loop
  76. $isCircular = preg_match('/.*\/page-introuvable/', $request->getUri()->getPath());
  77. $otWebRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtWebsiteRepository::class);
  78. $website = $otWebRepository->getCurrentWebsiteFromFEGlobals();
  79. if ($website && !$isCircular){
  80. $homeUri = $otWebRepository->resolveWebsiteBaseUri($website);
  81. $rootUid = $otWebRepository->getWebsiteRootUid($website['uid']);
  82. $pageRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtPageRepository::class);
  83. $rootPage = $pageRepository->getPage($rootUid);
  84. if (!empty($rootPage)) {
  85. $title = $rootPage['title'];
  86. $subPages = $pageRepository->getAllSubpagesForPage($rootUid, true);
  87. foreach ($subPages as $page) {
  88. if ($page['tx_fed_page_controller_action'] == 'OpenTalent.OtTemplating->error/404') {
  89. $errorHandler = GeneralUtility::makeInstance(
  90. PageContentErrorHandler::class,
  91. $this->statusCode,
  92. [
  93. 'errorHandler' => 'Page',
  94. 'errorContentSource' => 't3://page?uid=' . $page['uid']
  95. ]
  96. );
  97. return $errorHandler->handlePageError($request, $message, $reasons);
  98. }
  99. }
  100. } else {
  101. $title = 'Page Introuvable';
  102. $homeUri = self::REDIRECT_FALLBACK;
  103. }
  104. }
  105. // At this point, the site has not been determined or
  106. // this site has no subpage with slug '/page-introuvable'
  107. $this->view->assignMultiple([
  108. 'request' => $request,
  109. 'message' => $message,
  110. 'reasons' => $reasons,
  111. 'homeUri' => $homeUri,
  112. 'siteTitle' => $title
  113. ]);
  114. return new HtmlResponse($this->view->render(), $this->statusCode);
  115. }
  116. }