[TASK] Deprecate FrontendEditingController
[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 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
27
28 /**
29 * PSR-15 middleware initializing frontend editing
30 */
31 class FrontendEditInitiator implements MiddlewareInterface
32 {
33
34 /**
35 * Process an incoming server request and return a response, optionally delegating
36 * response creation to a handler.
37 *
38 * @param ServerRequestInterface $request
39 * @param RequestHandlerInterface $handler
40 * @return ResponseInterface
41 */
42 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
43 {
44 if (isset($GLOBALS['BE_USER']) && $GLOBALS['BE_USER'] instanceof FrontendBackendUserAuthentication) {
45 $config = $GLOBALS['BE_USER']->getTSConfig()['admPanel.'] ?? [];
46 $active = (int)$GLOBALS['TSFE']->displayEditIcons === 1 || (int)$GLOBALS['TSFE']->displayFieldEditIcons === 1;
47 // Include classes for editing IF editing module in Admin Panel is open
48 if ($active && isset($config['enable.'])) {
49 foreach ($config['enable.'] as $value) {
50 if ($value) {
51 $parameters = $request->getParsedBody()['TSFE_EDIT'] ?? $request->getQueryParams()['TSFE_EDIT'] ?? null;
52 $isValidEditAction = $this->isValidEditAction($parameters);
53 if ($GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
54 // Grab the Page TSConfig property that determines which controller to use.
55 $pageTSConfig = $GLOBALS['TSFE']->getPagesTSconfig();
56 $controllerKey = $pageTSConfig['TSFE.']['frontendEditingController'] ?? 'default';
57 } else {
58 $controllerKey = 'default';
59 }
60 /** @deprecated will be removed in TYPO3 v10.0. */
61 $controllerClassName = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController'][$controllerKey] ?? \TYPO3\CMS\Core\FrontendEditing\FrontendEditingController::class;
62 if (!empty($controllerClassName)) {
63 /** @deprecated will be removed in TYPO3 v10.0. */
64 $GLOBALS['BE_USER']->frontendEdit = GeneralUtility::makeInstance(
65 $controllerClassName,
66 $parameters
67 );
68 }
69 if ($isValidEditAction) {
70 GeneralUtility::makeInstance(FrontendEditDataHandler::class, $parameters)->editAction();
71 }
72 break;
73 }
74 }
75 }
76 }
77 return $handler->handle($request);
78 }
79
80 /**
81 * Returns TRUE if an edit-action is sent from the Admin Panel
82 *
83 * @param array|null $parameters
84 * @return bool
85 */
86 protected function isValidEditAction(array &$parameters = null): bool
87 {
88 if (!is_array($parameters)) {
89 return false;
90 }
91 if ($parameters['cancel']) {
92 unset($parameters['cmd']);
93 } else {
94 $cmd = (string)$parameters['cmd'];
95 if (($cmd !== 'edit' || is_array($parameters['data']) && ($parameters['doSave'] || $parameters['update'] || $parameters['update_close'])) && $cmd !== 'new') {
96 // $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.
97 return true;
98 }
99 }
100 return false;
101 }
102 }