88use Psr \Http \Message \ServerRequestInterface ;
99use Psr \Http \Server \MiddlewareInterface ;
1010use Psr \Http \Server \RequestHandlerInterface ;
11- use RuntimeException ;
1211use TYPO3 \CMS \Backend \Middleware \JavaScriptLabelImportMapEntryResolver ;
1312use TYPO3 \CMS \Backend \Routing \Router ;
1413use TYPO3 \CMS \Backend \Routing \UriBuilder ;
1716use TYPO3 \CMS \Core \Context \VisibilityAspect ;
1817use TYPO3 \CMS \Core \Error \Http \UnauthorizedException ;
1918use TYPO3 \CMS \Core \EventDispatcher \ListenerProvider ;
20- use TYPO3 \CMS \Core \FormProtection \FormProtectionFactory ;
2119use TYPO3 \CMS \Core \Http \HtmlResponse ;
2220use TYPO3 \CMS \Core \Http \ImmediateResponseException ;
23- use TYPO3 \CMS \Core \Http \JsonResponse ;
2421use TYPO3 \CMS \Core \Information \Typo3Version ;
2522use TYPO3 \CMS \Core \Page \Event \ResolveVirtualJavaScriptImportEvent ;
2623use TYPO3 \CMS \Core \Page \PageRenderer ;
27- use TYPO3 \CMS \Core \Type \Bitmask \Permission ;
2824use TYPO3 \CMS \Core \Utility \GeneralUtility ;
2925use TYPO3 \CMS \Core \View \ViewFactoryData ;
3026use TYPO3 \CMS \Core \View \ViewFactoryInterface ;
31- use TYPO3 \CMS \Frontend \Page \PageInformation ;
32- use TYPO3 \CMS \VisualEditor \Service \DataHandlerService ;
3327
34- use function array_keys ;
35- use function implode ;
36- use function json_decode ;
3728use function substr ;
3829
39- readonly class PersistenceMiddleware implements MiddlewareInterface
30+ readonly class EditModeMiddleware implements MiddlewareInterface
4031{
4132 public function __construct (
4233 private Context $ context ,
43- private DataHandlerService $ dataHandlerService ,
4434 private UriBuilder $ uriBuilder ,
4535 private ViewFactoryInterface $ viewFactory ,
46- private FormProtectionFactory $ formProtectionFactory ,
4736 private Typo3Version $ typo3Version ,
4837 private ListenerProvider $ listenerProvider ,
4938 private PageRenderer $ pageRenderer ,
@@ -52,54 +41,19 @@ public function __construct(
5241
5342 public function process (ServerRequestInterface $ request , RequestHandlerInterface $ handler ): ResponseInterface
5443 {
55- return match ($ this ->whatToDo ($ request )) {
56- MiddlewareAction::Edit => $ this ->handleEdit ($ request , $ handler ),
57- MiddlewareAction::Save => $ this ->saveStuff ($ request ),
58- MiddlewareAction::None => $ handler ->handle ($ request ),
59- };
60- }
61-
62- private function saveStuff (ServerRequestInterface $ request ): ResponseInterface
63- {
64- $ token = $ request ->getHeaderLine ('X-Request-Token ' );
65- if (!$ token || !$ this ->formProtectionFactory ->createForType ('backend ' )->validateToken ($ token , 'visual_editor ' , 'save ' )) {
66- throw new UnauthorizedException ('Invalid or missing request token ' , 8148623595 );
67- }
68-
69- $ input = $ request ->getParsedBody () ??
70- json_decode ($ request ->getBody ()->getContents (), true , 512 , JSON_THROW_ON_ERROR );
71-
72- $ data = $ input ['data ' ] ?? [];
73- unset($ input ['data ' ]);
74- $ cmdArray = $ input ['cmdArray ' ] ?? [];
75- unset($ input ['cmdArray ' ]);
76-
77- if (!empty ($ input )) {
78- throw new RuntimeException ('Unknown data operations: ' . implode (', ' , array_keys ($ input )) . ' only data and cmdArray are allowed ' , 8110225095 );
44+ if ($ this ->shouldInitEditMode ($ request )) {
45+ return $ this ->handleEdit ($ request , $ handler );
7946 }
8047
81- // Required by DefaultSanitizerBuilder when processing RTE fields via DataHandler;
82- // this middleware short-circuits before the FE RequestHandler which normally sets this global.
83- $ GLOBALS ['TYPO3_REQUEST ' ] = $ request ;
84- $ errorLog = $ this ->dataHandlerService ->run ($ data , []);
85-
86- foreach ($ cmdArray as $ cmd ) {
87- $ errorLog = [...$ errorLog , ...$ this ->dataHandlerService ->run ([], $ cmd )];
88- }
89-
90- if ($ errorLog ) {
91- return new JsonResponse (['success ' => false , 'errorLog ' => $ errorLog ], 500 );
92- }
93-
94- return new JsonResponse (['success ' => true ]);
48+ return $ handler ->handle ($ request );
9549 }
9650
97- private function whatToDo (ServerRequestInterface $ request ): MiddlewareAction
51+ private function shouldInitEditMode (ServerRequestInterface $ request ): bool
9852 {
9953 // parameter editMode must be set
10054 $ params = $ request ->getQueryParams ();
10155 if (!isset ($ params ['editMode ' ])) {
102- return MiddlewareAction::None ;
56+ return false ;
10357 }
10458
10559 // backend user required
@@ -123,42 +77,7 @@ private function whatToDo(ServerRequestInterface $request): MiddlewareAction
12377 throw new UnauthorizedException ('No $GLOBALS[ \'BE_USER \'] available ' , 8725323237 );
12478 }
12579
126- // only do something on POST requests
127- if ($ request ->getMethod () !== 'POST ' ) {
128- return MiddlewareAction::Edit;
129- }
130-
131- // only allow application/json content type
132- if ($ request ->getHeaderLine ('Content-Type ' ) !== 'application/json ' ) {
133- throw new UnauthorizedException ('Content-Type must be application/json to save stuff with visual_editor ' , 5015404100 );
134- }
135-
136- if ($ user ->isAdmin ()) {
137- return MiddlewareAction::Save;
138- }
139-
140- // check permissions of user on page
141- $ pageInformation = $ this ->getPageInformation ($ request );
142-
143- if (!$ beUser ->isInWebMount ($ pageInformation ->getId ())) {
144- throw new UnauthorizedException ('No permission to access this page ' , 1610177162 );
145- }
146-
147- if (!$ beUser ->doesUserHaveAccess ($ pageInformation ->getPageRecord (), Permission::CONTENT_EDIT )) {
148- throw new UnauthorizedException ('No permission to edit content on this page ' , 7668402611 );
149- }
150-
151- return MiddlewareAction::Save;
152- }
153-
154- private function getPageInformation (ServerRequestInterface $ request ): PageInformation
155- {
156- $ frontendPageInformation = $ request ->getAttribute ('frontend.page.information ' );
157- if (!$ frontendPageInformation instanceof PageInformation) {
158- throw new RuntimeException ('No frontend page information available ' , 7005099635 );
159- }
160-
161- return $ frontendPageInformation ;
80+ return true ;
16281 }
16382
16483 private function handleEdit (ServerRequestInterface $ request , RequestHandlerInterface $ handler ): ResponseInterface
0 commit comments