628f2a0f5e02b661b521271e0fd360f08e2c0a98
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Middleware / AdminPanelRenderer.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Middleware;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
23 use TYPO3\CMS\Adminpanel\Controller\MainController;
24 use TYPO3\CMS\Adminpanel\Utility\StateUtility;
25 use TYPO3\CMS\Core\Http\NullResponse;
26 use TYPO3\CMS\Core\Http\Stream;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
29
30 /**
31 * Render the admin panel via PSR-15 middleware
32 *
33 * @internal
34 */
35 class AdminPanelRenderer implements MiddlewareInterface
36 {
37
38 /**
39 * Render the admin panel if activated
40 */
41 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
42 {
43 $response = $handler->handle($request);
44 if (
45 !($response instanceof NullResponse)
46 && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
47 && $GLOBALS['TSFE']->isOutputting()
48 && StateUtility::isActivatedForUser()
49 && StateUtility::isActivatedInTypoScript()
50 && !StateUtility::isHiddenForUser()
51 ) {
52 $mainController = GeneralUtility::makeInstance(MainController::class);
53 $body = $response->getBody();
54 $body->rewind();
55 $contents = $response->getBody()->getContents();
56 $content = str_ireplace(
57 '</body>',
58 $mainController->render($request) . '</body>',
59 $contents
60 );
61 $body = new Stream('php://temp', 'rw');
62 $body->write($content);
63 $response = $response->withBody($body);
64 }
65 return $response;
66 }
67 }