ErrorHandler.php 6.6 KB

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