d4dd83e00fa9f1d8328b1420f3c0e3db25591d2f
[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\Core\Bootstrap;
22 use TYPO3\CMS\Core\Exception;
23 use TYPO3\CMS\Core\Http\Dispatcher;
24 use TYPO3\CMS\Core\Http\NullResponse;
25 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
26 use TYPO3\CMS\Core\Http\Response;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Frontend\Middleware\EidHandler as EidMiddleware;
29
30 /**
31 * Lightweight alternative to the regular RequestHandler used when $_GET[eID] is set.
32 * In the future, logic from the EidUtility will be moved to this class.
33 *
34 * @deprecated since TYPO3 v9.2, will be removed in TYPO3 v10
35 */
36 class EidRequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterface
37 {
38 /**
39 * Instance of the current TYPO3 bootstrap
40 * @var Bootstrap
41 */
42 protected $bootstrap;
43
44 /**
45 * Constructor handing over the bootstrap and the original request
46 *
47 * @param Bootstrap $bootstrap
48 */
49 public function __construct(Bootstrap $bootstrap)
50 {
51 trigger_error(self::class . ' will be removed in TYPO3 v10. Use ' . EidMiddleware::class . ' instead.', E_USER_DEPRECATED);
52 $this->bootstrap = $bootstrap;
53 }
54
55 /**
56 * Handles a frontend request based on the _GP "eID" variable.
57 *
58 * @param ServerRequestInterface $request
59 * @return ResponseInterface
60 */
61 public function handleRequest(ServerRequestInterface $request): ResponseInterface
62 {
63 trigger_error(self::class . ' will be removed in TYPO3 v10. Use ' . EidMiddleware::class . ' instead.', E_USER_DEPRECATED);
64 return $this->handle($request);
65 }
66
67 /**
68 * This request handler can handle any frontend request.
69 *
70 * @param ServerRequestInterface $request The request to process
71 * @return bool If the request is not an eID request, TRUE otherwise FALSE
72 */
73 public function canHandleRequest(ServerRequestInterface $request): bool
74 {
75 trigger_error(self::class . ' will be removed in TYPO3 v10. Use ' . EidMiddleware::class . ' instead.', E_USER_DEPRECATED);
76 return !empty($request->getQueryParams()['eID']) || !empty($request->getParsedBody()['eID']);
77 }
78
79 /**
80 * Returns the priority - how eager the handler is to actually handle the
81 * request.
82 *
83 * @return int The priority of the request handler.
84 */
85 public function getPriority(): int
86 {
87 trigger_error(self::class . ' will be removed in TYPO3 v10. Use ' . EidMiddleware::class . ' instead.', E_USER_DEPRECATED);
88 return 80;
89 }
90
91 /**
92 * Dispatches the request to the corresponding eID class or eID script
93 *
94 * @param ServerRequestInterface $request
95 * @return ResponseInterface
96 * @throws Exception
97 */
98 public function handle(ServerRequestInterface $request): ResponseInterface
99 {
100 trigger_error(self::class . ' will be removed in TYPO3 v10. Use ' . EidMiddleware::class . ' instead.', E_USER_DEPRECATED);
101 // Remove any output produced until now
102 $this->bootstrap->endOutputBufferingAndCleanPreviousOutput();
103
104 /** @var Response $response */
105 $response = GeneralUtility::makeInstance(Response::class);
106
107 $eID = $request->getParsedBody()['eID'] ?? $request->getQueryParams()['eID'] ?? '';
108
109 if (empty($eID) || !isset($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID])) {
110 return $response->withStatus(404, 'eID not registered');
111 }
112
113 $configuration = $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID];
114
115 // Simple check to make sure that it's not an absolute file (to use the fallback)
116 if (strpos($configuration, '::') !== false || is_callable($configuration)) {
117 /** @var Dispatcher $dispatcher */
118 $dispatcher = GeneralUtility::makeInstance(Dispatcher::class);
119 $request = $request->withAttribute('target', $configuration);
120 return $dispatcher->dispatch($request, $response);
121 }
122
123 $scriptPath = GeneralUtility::getFileAbsFileName($configuration);
124 if ($scriptPath === '') {
125 throw new Exception('Registered eID has invalid script path.', 1416391467);
126 }
127 include $scriptPath;
128 return new NullResponse();
129 }
130 }