[TASK] Deprecate second controller action argument
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / RequestHandler.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\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\Backend\Routing\Exception\InvalidRequestTokenException;
22 use TYPO3\CMS\Core\Http\RedirectResponse;
23 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
24 use TYPO3\CMS\Core\Http\Response;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * General RequestHandler for the TYPO3 Backend. This is used for all Backend requests, including AJAX routes.
29 *
30 * If a get/post parameter "route" is set, the Backend Routing is called and searches for a
31 * matching route inside the Router. The corresponding controller / action is called then which returns the response.
32 *
33 * The following get/post parameters are evaluated here:
34 * - route
35 * - token
36 */
37 class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterface
38 {
39 /**
40 * Handles any backend request
41 *
42 * @param ServerRequestInterface $request
43 * @return ResponseInterface
44 */
45 public function handleRequest(ServerRequestInterface $request): ResponseInterface
46 {
47 return $this->handle($request);
48 }
49
50 /**
51 * Handles a backend request, after finishing running middlewares
52 * Dispatch the request to the appropriate controller through the
53 * Backend Dispatcher which resolves the routing
54 *
55 * @param ServerRequestInterface $request
56 * @return ResponseInterface
57 */
58 public function handle(ServerRequestInterface $request): ResponseInterface
59 {
60 // Use a custom pre-created response for AJAX calls
61 // @deprecated since v9, will be removed in v10: No prepared $response to RouteDispatcher any longer
62 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
63 $response = new Response('php://temp', 200, [
64 'Content-Type' => 'application/json; charset=utf-8',
65 'X-JSON' => 'true'
66 ]);
67 } else {
68 $response = new Response();
69 }
70 try {
71 // Check if the router has the available route and dispatch.
72 $dispatcher = GeneralUtility::makeInstance(RouteDispatcher::class);
73 return $dispatcher->dispatch($request, $response);
74 } catch (InvalidRequestTokenException $e) {
75 // When token was invalid redirect to login
76 $url = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir;
77 return new RedirectResponse($url);
78 }
79 }
80
81 /**
82 * This request handler can handle any backend request.
83 *
84 * @param ServerRequestInterface $request
85 * @return bool If the request is BE request TRUE otherwise FALSE
86 */
87 public function canHandleRequest(ServerRequestInterface $request): bool
88 {
89 return (bool)(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE);
90 }
91
92 /**
93 * Returns the priority - how eager the handler is to actually handle the
94 * request.
95 *
96 * @return int The priority of the request handler.
97 */
98 public function getPriority(): int
99 {
100 return 50;
101 }
102 }