OtPageResolver.php 3.2 KB

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