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