[TASK] Have eIDs with PSR-7 without ControllerInterface
[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 TYPO3\CMS\Core\Core\Bootstrap;
18 use TYPO3\CMS\Core\Exception;
19 use TYPO3\CMS\Core\Http\Dispatcher;
20 use TYPO3\CMS\Core\Http\Response;
21 use TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
22 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
25 use Psr\Http\Message\ServerRequestInterface;
26
27 /**
28 * Lightweight alternative to the regular RequestHandler used when $_GET[eID] is set.
29 * In the future, logic from the EidUtility will be moved to this class.
30 */
31 class EidRequestHandler implements RequestHandlerInterface {
32
33 /**
34 * Instance of the current TYPO3 bootstrap
35 * @var Bootstrap
36 */
37 protected $bootstrap;
38
39 /**
40 * Constructor handing over the bootstrap and the original request
41 *
42 * @param Bootstrap $bootstrap
43 */
44 public function __construct(Bootstrap $bootstrap) {
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 // Timetracking started
56 $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']);
57 if (empty($configuredCookieName)) {
58 $configuredCookieName = 'be_typo_user';
59 }
60 if ($request->getCookieParams()[$configuredCookieName]) {
61 $GLOBALS['TT'] = new TimeTracker();
62 } else {
63 $GLOBALS['TT'] = new NullTimeTracker();
64 }
65
66 $GLOBALS['TT']->start();
67
68 // Hook to preprocess the current request
69 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'])) {
70 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] as $hookFunction) {
71 $hookParameters = array();
72 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $hookParameters);
73 }
74 unset($hookFunction);
75 unset($hookParameters);
76 }
77
78 // Remove any output produced until now
79 $this->bootstrap->endOutputBufferingAndCleanPreviousOutput();
80 return $this->dispatch($request);
81 }
82
83 /**
84 * This request handler can handle any frontend request.
85 *
86 * @param ServerRequestInterface $request The request to process
87 * @return bool If the request is not an eID request, TRUE otherwise FALSE
88 */
89 public function canHandleRequest(ServerRequestInterface $request) {
90 return !empty($request->getQueryParams()['eID']) || !empty($request->getParsedBody()['eID']);
91 }
92
93 /**
94 * Returns the priority - how eager the handler is to actually handle the
95 * request.
96 *
97 * @return int The priority of the request handler.
98 */
99 public function getPriority() {
100 return 80;
101 }
102
103 /**
104 * Dispatches the request to the corresponding eID class or eID script
105 *
106 * @param ServerRequestInterface $request
107 * @return NULL|\Psr\Http\Message\ResponseInterface
108 * @throws Exception
109 */
110 protected function dispatch($request) {
111 /** @var Response $response */
112 $response = GeneralUtility::makeInstance(Response::class);
113
114 $eID = isset($request->getParsedBody()['eID'])
115 ? $request->getParsedBody()['eID']
116 : (isset($request->getQueryParams()['eID']) ? $request->getQueryParams()['eID'] : '');
117
118 if (empty($eID) || !isset($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID])) {
119 return $response->withStatus(404, 'eID not registered');
120 }
121
122 $configuration = $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID];
123
124 // Simple check to make sure that it's not an absolute file (to use the fallback)
125 if (strpos($configuration, '::') !== FALSE || is_callable($configuration)) {
126 /** @var Dispatcher $dispatcher */
127 $dispatcher = GeneralUtility::makeInstance(Dispatcher::class);
128 $request = $request->withAttribute('target', $configuration);
129 return $dispatcher->dispatch($request, $response);
130 }
131
132 $scriptPath = GeneralUtility::getFileAbsFileName($configuration);
133 if ($scriptPath === '') {
134 throw new Exception('Registered eID has invalid script path.', 1416391467);
135 }
136 include $scriptPath;
137 return NULL;
138 }
139
140 }