[TASK] Speed up ReST file reference check
[Packages/TYPO3.CMS.git] / typo3 / sysext / feedit / Classes / Middleware / FrontendEditInitiator.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Feedit\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\Backend\FrontendBackendUserAuthentication;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler;
26
27 /**
28 * PSR-15 middleware initializing frontend editing
29 *
30 * @internal this is a concrete TYPO3 implementation and solely used for EXT:feedit and not part of TYPO3's Core API.
31 */
32 class FrontendEditInitiator implements MiddlewareInterface
33 {
34
35 /**
36 * Process an incoming server request and return a response, optionally delegating
37 * response creation to a handler.
38 *
39 * @param ServerRequestInterface $request
40 * @param RequestHandlerInterface $handler
41 * @return ResponseInterface
42 */
43 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
44 {
45 if (isset($GLOBALS['BE_USER']) && $GLOBALS['BE_USER'] instanceof FrontendBackendUserAuthentication) {
46 $config = $GLOBALS['BE_USER']->getTSConfig()['admPanel.'] ?? [];
47 $active = (int)$GLOBALS['TSFE']->displayEditIcons === 1 || (int)$GLOBALS['TSFE']->displayFieldEditIcons === 1;
48 // Include classes for editing IF editing module in Admin Panel is open
49 if ($active && isset($config['enable.'])) {
50 foreach ($config['enable.'] as $value) {
51 if ($value) {
52 $parameters = $request->getParsedBody()['TSFE_EDIT'] ?? $request->getQueryParams()['TSFE_EDIT'] ?? null;
53 if ($this->isValidEditAction($parameters)) {
54 GeneralUtility::makeInstance(FrontendEditDataHandler::class, $parameters)->editAction();
55 }
56 break;
57 }
58 }
59 }
60 }
61 return $handler->handle($request);
62 }
63
64 /**
65 * Returns TRUE if an edit-action is sent from the Admin Panel
66 *
67 * @param array|null $parameters
68 * @return bool
69 */
70 protected function isValidEditAction(array &$parameters = null): bool
71 {
72 if (!is_array($parameters)) {
73 return false;
74 }
75 if ($parameters['cancel']) {
76 unset($parameters['cmd']);
77 } else {
78 $cmd = (string)$parameters['cmd'];
79 if (($cmd !== 'edit' || is_array($parameters['data']) && ($parameters['doSave'] || $parameters['update'] || $parameters['update_close'])) && $cmd !== 'new') {
80 // $cmd can be a command like "hide" or "move". If $cmd is "edit" or "new" it's an indication to show the formfields. But if data is sent with update-flag then $cmd = edit is accepted because edit may be sent because of .keepGoing flag.
81 return true;
82 }
83 }
84 return false;
85 }
86 }