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