ErrorHandler.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Database\ConnectionPool;
  8. use TYPO3\CMS\Core\Domain\Repository\PageRepository;
  9. use TYPO3\CMS\Core\Error\PageErrorHandler\PageContentErrorHandler;
  10. use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
  11. use TYPO3\CMS\Core\Http\HtmlResponse;
  12. use TYPO3\CMS\Core\Utility\GeneralUtility;
  13. use TYPO3\CMS\Extbase\Mvc\Request;
  14. use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
  15. use TYPO3\CMS\Fluid\View\TemplateView;
  16. use TYPO3Fluid\Fluid\View\ViewInterface;
  17. class ErrorHandler implements PageErrorHandlerInterface
  18. {
  19. const string TEMPLATES_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Templates';
  20. const string LAYOUTS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Layouts';
  21. const string PARTIALS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Partials';
  22. const array 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 string 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. private OtWebsiteRepository $otWebsiteRepository;
  39. private OtPageRepository $otPageRepository;
  40. private RenderingContext $renderingContext;
  41. private Request $request;
  42. /**
  43. * @var ViewInterface
  44. */
  45. protected $view;
  46. public function __construct(int $statusCode, array $configuration)
  47. {
  48. $this->statusCode = $statusCode;
  49. $this->errorHandlerConfiguration = $configuration;
  50. $this->view = GeneralUtility::makeInstance(TemplateView::class);
  51. $this->otWebsiteRepository = GeneralUtility::makeInstance(OtWebsiteRepository::class);
  52. $this->otPageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
  53. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
  54. $this->otWebsiteRepository->injectConnectionPool($connectionPool);
  55. $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
  56. $this->otPageRepository->injectPageRepository($pageRepository);
  57. $this->otPageRepository->injectConnectionPool($connectionPool);
  58. $this->request = GeneralUtility::makeInstance(Request::class);
  59. $this->renderingContext = GeneralUtility::makeInstance(RenderingContext::class);
  60. }
  61. /**
  62. * @param OtWebsiteRepository $otWebsiteRepository
  63. * @return void
  64. */
  65. public function injectOtWebsiteRepository(OtWebsiteRepository $otWebsiteRepository) {
  66. $this->otWebsiteRepository = $otWebsiteRepository;
  67. }
  68. /**
  69. * @param OtPageRepository $otWebsiteRepository
  70. * @return void
  71. */
  72. public function injectOtPageRepository(OtPageRepository $otPageRepository) {
  73. $this->otPageRepository = $otPageRepository;
  74. }
  75. /**
  76. * @param RenderingContext $renderingContext
  77. * @return void
  78. */
  79. public function injectRenderingContext(RenderingContext $renderingContext) {
  80. $this->renderingContext = $renderingContext;
  81. }
  82. /**
  83. * @param Request $request
  84. * @return void
  85. */
  86. public function injectRequest(Request $request) {
  87. $this->request = $request;
  88. }
  89. /**
  90. * @param ServerRequestInterface $request
  91. * @param string $message
  92. * @param array $reasons
  93. * @return ResponseInterface
  94. */
  95. public function handlePageError(
  96. ServerRequestInterface $request,
  97. string $message,
  98. array $reasons = []
  99. ): ResponseInterface {
  100. $this->request->setControllerExtensionName('ot_templating');
  101. $this->renderingContext->setRequest($this->request);
  102. $this->view->setRenderingContext($this->renderingContext);
  103. $this->view->setTemplateRootPaths([self::TEMPLATES_ROOT_PATHS]);
  104. $this->view->setLayoutRootPaths([self::LAYOUTS_ROOT_PATHS]);
  105. $this->view->setPartialRootPaths([self::PARTIALS_ROOT_PATHS]);
  106. $this->view->setTemplatePathAndFilename(
  107. GeneralUtility::getFileAbsFileName(self::TEMPLATE_FILES[$this->statusCode])
  108. );
  109. $homeUri = "https://opentalent.fr";
  110. $title = 'Page Introuvable';
  111. // This variable aims to prevent redirection loop
  112. $isCircular = preg_match('/.*\/page-introuvable/', $request->getUri()->getPath());
  113. $website = $this->otWebsiteRepository->getCurrentWebsiteFromFEGlobals();
  114. if ($website && !$isCircular){
  115. $homeUri = $this->otWebsiteRepository->resolveWebsiteBaseUri($website);
  116. $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
  117. $rootPage = $this->otPageRepository->getPage($rootUid);
  118. if (!empty($rootPage)) {
  119. $title = $rootPage['title'];
  120. $subPages = $this->otPageRepository->getAllSubpagesForPage($rootUid, true);
  121. foreach ($subPages as $page) {
  122. if ($page['tx_fed_page_controller_action'] == 'OpenTalent.OtTemplating->error/404') {
  123. $errorHandler = GeneralUtility::makeInstance(
  124. PageContentErrorHandler::class,
  125. $this->statusCode,
  126. [
  127. 'errorHandler' => 'Page',
  128. 'errorContentSource' => 't3://page?uid=' . $page['uid']
  129. ]
  130. );
  131. return $errorHandler->handlePageError($request, $message, $reasons);
  132. }
  133. }
  134. } else {
  135. $title = 'Page Introuvable';
  136. $homeUri = self::REDIRECT_FALLBACK;
  137. }
  138. }
  139. // At this point, the site has not been determined or
  140. // this site has no subpage with slug '/page-introuvable'
  141. $this->view->assignMultiple([
  142. 'request' => $request,
  143. 'message' => $message,
  144. 'reasons' => $reasons,
  145. 'homeUri' => $homeUri,
  146. 'siteTitle' => $title
  147. ]);
  148. return new HtmlResponse($this->view->render(), $this->statusCode);
  149. }
  150. }