OtPageResolver.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Opentalent\OtOptimizer\Middleware\Frontend;
  3. use Opentalent\OtCore\Website\OtWebsiteRepository;
  4. use Psr\Http\Message\ResponseInterface;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Psr\Http\Server\RequestHandlerInterface;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Frontend\Controller\ErrorController;
  9. use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
  10. class OtPageResolver extends \TYPO3\CMS\Frontend\Middleware\PageResolver
  11. {
  12. private OtWebsiteRepository $otWebsiteRepository;
  13. /**
  14. * @param OtWebsiteRepository $otWebsiteRepository
  15. * @return void
  16. */
  17. public function injectOtWebsiteRepository(OtWebsiteRepository $otWebsiteRepository): void
  18. {
  19. $this->otWebsiteRepository = $otWebsiteRepository;
  20. }
  21. /**
  22. * Resolve the page ID
  23. *
  24. * @param ServerRequestInterface $request
  25. * @param RequestHandlerInterface $handler
  26. * @return ResponseInterface
  27. * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException
  28. * @throws \TYPO3\CMS\Core\Error\Http\PageNotFoundException
  29. * @throws \TYPO3\CMS\Extbase\Object\Exception
  30. */
  31. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  32. {
  33. // Shall fall back on non-optimized mode if a cookie 'optimize' exists and is different from 1, or if
  34. // a server global variable TYPO3_OPTIMIZE is set and is different from 1
  35. $shallFallback = (isset($_COOKIE['optimize']) && (int)$_COOKIE['optimize'] !== 1 && (int)$_SERVER['TYPO3_OPTIMIZE'] !== 1);
  36. if ($shallFallback) {
  37. return parent::process($request, $handler);
  38. }
  39. $devMode = $_SERVER['TYPO3_CONTEXT'] == "Development";
  40. if (!$GLOBALS['TYPO3_REQUEST']) {
  41. return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
  42. $request,
  43. 'The requested website does not exist',
  44. ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
  45. );
  46. }
  47. $website = $GLOBALS['TYPO3_REQUEST']->getAttribute('ot_website');
  48. $params = $request->getQueryParams();
  49. if (!($website['uid'] ?? 0) > 0) {
  50. return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
  51. $request,
  52. 'The requested website does not exist',
  53. ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
  54. );
  55. }
  56. // if the page is requested from the BE module Viewpage or FrontendEditing, it shall be displayed even if hidden
  57. // a backend user shall be authenticated for this
  58. $requestedFromBE = (
  59. preg_match("/.+\/typo3\/index.php\?route=.*(Viewpage)|(FrontendEditing).*/", ($_SERVER['HTTP_REFERER'] ?? '')) ||
  60. ($request->getQueryParams()['frontend_editing'] ?? null) === 'true'
  61. )
  62. && $GLOBALS['BE_USER'];
  63. $pageUid = $this->otWebsiteRepository->matchUriToPage($website, $request->getUri(), $devMode, !$requestedFromBE);
  64. if (!$pageUid > 0) {
  65. return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
  66. $request,
  67. 'The requested page does not exist',
  68. ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
  69. );
  70. }
  71. $params['id'] = $pageUid;
  72. $request = $request->withQueryParams($params);
  73. return parent::process($request, $handler);
  74. }
  75. }