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