ErrorHandler.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. }
  59. /**
  60. * @param OtWebsiteRepository $otWebsiteRepository
  61. * @return void
  62. */
  63. public function injectOtWebsiteRepository(OtWebsiteRepository $otWebsiteRepository) {
  64. $this->otWebsiteRepository = $otWebsiteRepository;
  65. }
  66. /**
  67. * @param OtPageRepository $otWebsiteRepository
  68. * @return void
  69. */
  70. public function injectOtPageRepository(OtPageRepository $otPageRepository) {
  71. $this->otPageRepository = $otPageRepository;
  72. }
  73. /**
  74. * @param RenderingContext $renderingContext
  75. * @return void
  76. */
  77. public function injectRenderingContext(RenderingContext $renderingContext) {
  78. $this->renderingContext = $renderingContext;
  79. }
  80. /**
  81. * @param Request $request
  82. * @return void
  83. */
  84. public function injectRequest(Request $request) {
  85. $this->request = $request;
  86. }
  87. /**
  88. * @param ServerRequestInterface $request
  89. * @param string $message
  90. * @param array $reasons
  91. * @return ResponseInterface
  92. */
  93. public function handlePageError(
  94. ServerRequestInterface $request,
  95. string $message,
  96. array $reasons = []
  97. ): ResponseInterface {
  98. $this->view->setTemplateRootPaths([self::TEMPLATES_ROOT_PATHS]);
  99. $this->view->setLayoutRootPaths([self::LAYOUTS_ROOT_PATHS]);
  100. $this->view->setPartialRootPaths([self::PARTIALS_ROOT_PATHS]);
  101. $this->view->setTemplatePathAndFilename(
  102. GeneralUtility::getFileAbsFileName(self::TEMPLATE_FILES[$this->statusCode])
  103. );
  104. $homeUri = "https://opentalent.fr";
  105. $title = 'Page Introuvable';
  106. // This variable aims to prevent redirection loop
  107. $isCircular = preg_match('/.*\/page-introuvable/', $request->getUri()->getPath());
  108. $website = $this->otWebsiteRepository->getCurrentWebsiteFromFEGlobals();
  109. if ($website && !$isCircular){
  110. $homeUri = $this->otWebsiteRepository->resolveWebsiteBaseUri($website);
  111. $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
  112. $rootPage = $this->otPageRepository->getPage($rootUid);
  113. if (!empty($rootPage)) {
  114. $title = $rootPage['title'];
  115. $subPages = $this->otPageRepository->getAllSubpagesForPage($rootUid, true);
  116. foreach ($subPages as $page) {
  117. if ($page['tx_fed_page_controller_action'] == 'OpenTalent.OtTemplating->error/404') {
  118. $errorHandler = GeneralUtility::makeInstance(
  119. PageContentErrorHandler::class,
  120. $this->statusCode,
  121. [
  122. 'errorHandler' => 'Page',
  123. 'errorContentSource' => 't3://page?uid=' . $page['uid']
  124. ]
  125. );
  126. return $errorHandler->handlePageError($request, $message, $reasons);
  127. }
  128. }
  129. } else {
  130. $title = 'Page Introuvable';
  131. $homeUri = self::REDIRECT_FALLBACK;
  132. }
  133. }
  134. // At this point, the site has not been determined or
  135. // this site has no subpage with slug '/page-introuvable'
  136. $this->view->assignMultiple([
  137. 'request' => $request,
  138. 'message' => $message,
  139. 'reasons' => $reasons,
  140. 'homeUri' => $homeUri,
  141. 'siteTitle' => $title
  142. ]);
  143. return new HtmlResponse($this->view->render(), $this->statusCode);
  144. }
  145. }