8cfc4320b571f6591ebaf8a8719be57d7bb12b4f
[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 NULL|\Psr\Http\Message\ResponseInterface
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 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'])) {
65 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] as $hookFunction) {
66 $hookParameters = [];
67 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $hookParameters);
68 }
69 unset($hookFunction);
70 unset($hookParameters);
71 }
72
73 // Remove any output produced until now
74 $this->bootstrap->endOutputBufferingAndCleanPreviousOutput();
75 return $this->dispatch($request);
76 }
77
78 /**
79 * This request handler can handle any frontend request.
80 *
81 * @param ServerRequestInterface $request The request to process
82 * @return bool If the request is not an eID request, TRUE otherwise FALSE
83 */
84 public function canHandleRequest(ServerRequestInterface $request)
85 {
86 return !empty($request->getQueryParams()['eID']) || !empty($request->getParsedBody()['eID']);
87 }
88
89 /**
90 * Returns the priority - how eager the handler is to actually handle the
91 * request.
92 *
93 * @return int The priority of the request handler.
94 */
95 public function getPriority()
96 {
97 return 80;
98 }
99
100 /**
101 * Dispatches the request to the corresponding eID class or eID script
102 *
103 * @param ServerRequestInterface $request
104 * @return NULL|\Psr\Http\Message\ResponseInterface
105 * @throws Exception
106 */
107 protected function dispatch($request)
108 {
109 /** @var Response $response */
110 $response = GeneralUtility::makeInstance(Response::class);
111
112 $eID = isset($request->getParsedBody()['eID'])
113 ? $request->getParsedBody()['eID']
114 : (isset($request->getQueryParams()['eID']) ? $request->getQueryParams()['eID'] : '');
115
116 if (empty($eID) || !isset($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID])) {
117 return $response->withStatus(404, 'eID not registered');
118 }
119
120 $configuration = $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID];
121
122 // Simple check to make sure that it's not an absolute file (to use the fallback)
123 if (strpos($configuration, '::') !== false || is_callable($configuration)) {
124 /** @var Dispatcher $dispatcher */
125 $dispatcher = GeneralUtility::makeInstance(Dispatcher::class);
126 $request = $request->withAttribute('target', $configuration);
127 return $dispatcher->dispatch($request, $response);
128 }
129
130 $scriptPath = GeneralUtility::getFileAbsFileName($configuration);
131 if ($scriptPath === '') {
132 throw new Exception('Registered eID has invalid script path.', 1416391467);
133 }
134 include $scriptPath;
135 return null;
136 }
137 }