8af138e819178ad643cc60f9548aea9c6a505c0b
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Http / EidRequestHandler.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Http;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Core\Core\Bootstrap;
19 use TYPO3\CMS\Core\Exception;
20 use TYPO3\CMS\Core\Http\Dispatcher;
21 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
22 use TYPO3\CMS\Core\Http\Response;
23 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Lightweight alternative to the regular RequestHandler used when $_GET[eID] is set.
28 * In the future, logic from the EidUtility will be moved to this class.
29 */
30 class EidRequestHandler implements RequestHandlerInterface
31 {
32 /**
33 * Instance of the current TYPO3 bootstrap
34 * @var Bootstrap
35 */
36 protected $bootstrap;
37
38 /**
39 * Constructor handing over the bootstrap and the original request
40 *
41 * @param Bootstrap $bootstrap
42 */
43 public function __construct(Bootstrap $bootstrap)
44 {
45 $this->bootstrap = $bootstrap;
46 }
47
48 /**
49 * Handles a frontend request based on the _GP "eID" variable.
50 *
51 * @param ServerRequestInterface $request
52 * @return \Psr\Http\Message\ResponseInterface|null
53 */
54 public function handleRequest(ServerRequestInterface $request)
55 {
56 // Starting time tracking
57 $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']) ?: 'be_typo_user';
58
59 /** @var TimeTracker $timeTracker */
60 $timeTracker = GeneralUtility::makeInstance(TimeTracker::class, ($request->getCookieParams()[$configuredCookieName] ? true : false));
61 $timeTracker->start();
62
63 // Hook to preprocess the current request
64 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] ?? [] as $hookFunction) {
65 $hookParameters = [];
66 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $hookParameters);
67 }
68
69 // Remove any output produced until now
70 $this->bootstrap->endOutputBufferingAndCleanPreviousOutput();
71 return $this->dispatch($request);
72 }
73
74 /**
75 * This request handler can handle any frontend request.
76 *
77 * @param ServerRequestInterface $request The request to process
78 * @return bool If the request is not an eID request, TRUE otherwise FALSE
79 */
80 public function canHandleRequest(ServerRequestInterface $request)
81 {
82 return !empty($request->getQueryParams()['eID']) || !empty($request->getParsedBody()['eID']);
83 }
84
85 /**
86 * Returns the priority - how eager the handler is to actually handle the
87 * request.
88 *
89 * @return int The priority of the request handler.
90 */
91 public function getPriority()
92 {
93 return 80;
94 }
95
96 /**
97 * Dispatches the request to the corresponding eID class or eID script
98 *
99 * @param ServerRequestInterface $request
100 * @return \Psr\Http\Message\ResponseInterface|null
101 * @throws Exception
102 */
103 protected function dispatch($request)
104 {
105 /** @var Response $response */
106 $response = GeneralUtility::makeInstance(Response::class);
107
108 $eID = isset($request->getParsedBody()['eID'])
109 ? $request->getParsedBody()['eID']
110 : (isset($request->getQueryParams()['eID']) ? $request->getQueryParams()['eID'] : '');
111
112 if (empty($eID) || !isset($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID])) {
113 return $response->withStatus(404, 'eID not registered');
114 }
115
116 $configuration = $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID];
117
118 // Simple check to make sure that it's not an absolute file (to use the fallback)
119 if (strpos($configuration, '::') !== false || is_callable($configuration)) {
120 /** @var Dispatcher $dispatcher */
121 $dispatcher = GeneralUtility::makeInstance(Dispatcher::class);
122 $request = $request->withAttribute('target', $configuration);
123 return $dispatcher->dispatch($request, $response);
124 }
125
126 $scriptPath = GeneralUtility::getFileAbsFileName($configuration);
127 if ($scriptPath === '') {
128 throw new Exception('Registered eID has invalid script path.', 1416391467);
129 }
130 include $scriptPath;
131 return null;
132 }
133 }