‪TYPO3CMS  9.5
PreviewSimulator.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
27 
33 class ‪PreviewSimulator implements MiddlewareInterface
34 {
38  private ‪$context;
39 
40  public function ‪__construct()
41  {
42  $this->context = GeneralUtility::makeInstance(Context::class);
43  }
44 
53  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
54  {
55  if ((bool)$this->context->getPropertyFromAspect('backend.user', 'isLoggedIn', false)) {
56  $simulatingDate = $this->‪simulateDate($request);
57  $simulatingGroup = $this->‪simulateUserGroup($request);
58  $showHiddenRecords = ($this->context->hasAspect('visibility') ? $this->context->getAspect('visibility')->includeHidden() : false);
59  ‪$GLOBALS['TSFE']->fePreview = ($simulatingDate || $simulatingGroup || $showHiddenRecords);
60  }
61 
62  return $handler->handle($request);
63  }
64 
77  protected function ‪simulateDate(ServerRequestInterface $request): bool
78  {
79  $simulatedDate = null;
80  $queryTime = $request->getQueryParams()['ADMCMD_simTime'] ?? false;
81  if (!$queryTime) {
82  return false;
83  }
84 
85  $simulatedDate = new \DateTimeImmutable('@' . $queryTime);
86  if (!$simulatedDate) {
87  return false;
88  }
89 
90  ‪$GLOBALS['SIM_EXEC_TIME'] = $queryTime;
91  ‪$GLOBALS['SIM_ACCESS_TIME'] = $queryTime - $queryTime % 60;
92  $this->context->setAspect(
93  'date',
94  GeneralUtility::makeInstance(
95  DateTimeAspect::class,
96  $simulatedDate
97  )
98  );
99  return true;
100  }
101 
114  protected function ‪simulateUserGroup(ServerRequestInterface $request): bool
115  {
116  $simulateUserGroup = (int)($request->getQueryParams()['ADMCMD_simUser'] ?? 0);
117  if (!$simulateUserGroup) {
118  return false;
119  }
120 
121  $frontendUser = ‪$GLOBALS['TSFE']->fe_user;
122  $frontendUser->user[$frontendUser->usergroup_column] = $simulateUserGroup;
123  // let's fake having a user with that group, too
124  $frontendUser->user['uid'] = PHP_INT_MAX;
125  $this->context->setAspect(
126  'frontend.user',
127  GeneralUtility::makeInstance(
128  UserAspect::class,
129  $frontendUser,
130  [$simulateUserGroup]
131  )
132  );
133  return true;
134  }
135 }
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\__construct
‪__construct()
Definition: PreviewSimulator.php:39
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\simulateUserGroup
‪bool simulateUserGroup(ServerRequestInterface $request)
Definition: PreviewSimulator.php:113
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:4
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\$context
‪TYPO3 CMS Core Context Context $context
Definition: PreviewSimulator.php:37
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PreviewSimulator.php:52
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator
Definition: PreviewSimulator.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\simulateDate
‪bool simulateDate(ServerRequestInterface $request)
Definition: PreviewSimulator.php:76
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:33
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36