[FEATURE] Add symfony dependency injection for core and extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / EidHandler.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Middleware;
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\MiddlewareInterface;
21 use Psr\Http\Server\RequestHandlerInterface;
22 use TYPO3\CMS\Core\Exception;
23 use TYPO3\CMS\Core\Http\DispatcherInterface;
24 use TYPO3\CMS\Core\Http\NullResponse;
25 use TYPO3\CMS\Core\Http\Response;
26
27 /**
28 * Lightweight alternative to regular frontend requests; used when $_GET[eID] is set.
29 * In the future, logic from the EidUtility will be moved to this class, however in most cases
30 * a custom PSR-15 middleware will be better suited for whatever job the eID functionality does currently.
31 *
32 * @internal
33 */
34 class EidHandler implements MiddlewareInterface
35 {
36 /**
37 * @var DispatcherInterface
38 */
39 protected $dispatcher;
40
41 /**
42 * @param DispatcherInterface $dispatcher
43 */
44 public function __construct(DispatcherInterface $dispatcher)
45 {
46 $this->dispatcher = $dispatcher;
47 }
48
49 /**
50 * Dispatches the request to the corresponding eID class or eID script
51 *
52 * @param ServerRequestInterface $request
53 * @param RequestHandlerInterface $handler
54 * @return ResponseInterface
55 * @throws Exception
56 */
57 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58 {
59 $eID = $request->getParsedBody()['eID'] ?? $request->getQueryParams()['eID'] ?? null;
60
61 if ($eID === null) {
62 return $handler->handle($request);
63 }
64
65 // Remove any output produced until now
66 ob_clean();
67
68 $target = $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID] ?? null;
69 if (empty($target)) {
70 return (new Response())->withStatus(404, 'eID not registered');
71 }
72
73 $request = $request->withAttribute('target', $target);
74 return $this->dispatcher->dispatch($request) ?? new NullResponse();
75 }
76 }