‪TYPO3CMS  10.4
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;
29 
35 class ‪PreviewSimulator implements MiddlewareInterface
36 {
40  private ‪$context;
41 
42  public function ‪__construct(‪Context ‪$context)
43  {
44  $this->context = ‪$context;
45  }
46 
55  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
56  {
57  if ((bool)$this->context->getPropertyFromAspect('backend.user', 'isLoggedIn', false)) {
58  $simulatingDate = $this->‪simulateDate($request);
59  $simulatingGroup = $this->‪simulateUserGroup($request);
60  $showHiddenRecords = ($this->context->hasAspect('visibility') ? $this->context->getAspect('visibility')->includeHidden() : false);
61  $isPreview = (int)($simulatingDate || $simulatingGroup || $showHiddenRecords);
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  $simulatedDate = null;
84  $queryTime = $request->getQueryParams()['ADMCMD_simTime'] ?? false;
85  if (!$queryTime) {
86  return false;
87  }
88 
89  $simulatedDate = new \DateTimeImmutable('@' . $queryTime);
90  if (!$simulatedDate) {
91  return false;
92  }
93 
94  ‪$GLOBALS['SIM_EXEC_TIME'] = $queryTime;
95  ‪$GLOBALS['SIM_ACCESS_TIME'] = $queryTime - $queryTime % 60;
96  $this->context->setAspect(
97  'date',
98  GeneralUtility::makeInstance(
99  DateTimeAspect::class,
100  $simulatedDate
101  )
102  );
103  return true;
104  }
105 
118  protected function ‪simulateUserGroup(ServerRequestInterface $request): bool
119  {
120  $simulateUserGroup = (int)($request->getQueryParams()['ADMCMD_simUser'] ?? 0);
121  if (!$simulateUserGroup) {
122  return false;
123  }
124 
125  $frontendUser = $request->getAttribute('frontend.user');
126  $frontendUser->user[$frontendUser->usergroup_column] = $simulateUserGroup;
127  // let's fake having a user with that group, too
128  $frontendUser->user['uid'] = PHP_INT_MAX;
129  $this->context->setAspect(
130  'frontend.user',
131  GeneralUtility::makeInstance(
132  UserAspect::class,
133  $frontendUser,
134  [$simulateUserGroup]
135  )
136  );
137  return true;
138  }
139 }
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\simulateUserGroup
‪bool simulateUserGroup(ServerRequestInterface $request)
Definition: PreviewSimulator.php:117
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\$context
‪TYPO3 CMS Core Context Context $context
Definition: PreviewSimulator.php:39
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\__construct
‪__construct(Context $context)
Definition: PreviewSimulator.php:41
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PreviewSimulator.php:54
‪TYPO3\CMS\Frontend\Middleware\PreviewSimulator
Definition: PreviewSimulator.php:36
‪$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:80
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Frontend\Aspect\PreviewAspect
Definition: PreviewAspect.php:30
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38