ErrorHandler.php 5.8 KB

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