[TASK] Set TYPO3 version to 9.5.0-dev
[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\FrontendEditing\FrontendEditingController;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler;
27 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
28
29 /**
30 * PSR-15 middleware initializing frontend editing
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 if ($active && isset($config['enable.'])) {
49 foreach ($config['enable.'] as $value) {
50 if ($value) {
51 if ($GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
52 // Grab the Page TSConfig property that determines which controller to use.
53 $pageTSConfig = $GLOBALS['TSFE']->getPagesTSconfig();
54 $controllerKey = $pageTSConfig['TSFE.']['frontendEditingController'] ?? 'default';
55 } else {
56 $controllerKey = 'default';
57 }
58 $controllerClassName = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController'][$controllerKey] ?? '';
59 if (!empty($controllerClassName)) {
60 $parameters = $request->getParsedBody()['TSFE_EDIT'] ?? $request->getQueryParams()['TSFE_EDIT'] ?? null;
61 $isValidEditAction = $this->isValidEditAction($parameters);
62 $GLOBALS['BE_USER']->frontendEdit = GeneralUtility::makeInstance(
63 $controllerClassName,
64 $parameters
65 );
66 // Include classes for editing IF editing module in Admin Panel is open
67 if ($GLOBALS['BE_USER']->frontendEdit instanceof FrontendEditingController && $isValidEditAction) {
68 GeneralUtility::makeInstance(FrontendEditDataHandler::class, $parameters)->editAction();
69 }
70 }
71 break;
72 }
73 }
74 }
75 }
76 return $handler->handle($request);
77 }
78
79 /**
80 * Returns TRUE if an edit-action is sent from the Admin Panel
81 *
82 * @param array|null $parameters
83 * @return bool
84 */
85 protected function isValidEditAction(array &$parameters = null): bool
86 {
87 if (!is_array($parameters)) {
88 return false;
89 }
90 if ($parameters['cancel']) {
91 unset($parameters['cmd']);
92 } else {
93 $cmd = (string)$parameters['cmd'];
94 if (($cmd !== 'edit' || is_array($parameters['data']) && ($parameters['doSave'] || $parameters['update'] || $parameters['update_close'])) && $cmd !== 'new') {
95 // $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.
96 return true;
97 }
98 }
99 return false;
100 }
101 }