eb67627d839b615b2aed511ce3e49c27c89e436f
[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\ControllerInterface;
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 $eID = isset($request->getParsedBody()['eID'])
112 ? $request->getParsedBody()['eID']
113 : (isset($request->getQueryParams()['eID']) ? $request->getQueryParams()['eID'] : '');
114
115 if (empty($eID) || !isset($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID])) {
116 return GeneralUtility::makeInstance(Response::class)->withStatus(404, 'eID not registered');
117 }
118
119 $configuration = $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID];
120 if (class_exists($configuration)) {
121 $controller = GeneralUtility::makeInstance($configuration);
122 if (!$controller instanceof ControllerInterface) {
123 throw new Exception('The provided eID class "' . $configuration . '" does not implement "ControllerInterface".', 1436909478);
124 }
125 return $controller->processRequest($request);
126 }
127
128 $scriptPath = GeneralUtility::getFileAbsFileName($configuration);
129 if ($scriptPath === '') {
130 throw new Exception('Registered eID has invalid script path.', 1416391467);
131 }
132 include $scriptPath;
133 return NULL;
134 }
135
136 }