[BUGFIX] EXT:filelist Fixed exception when creating new text file
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / FileController.php
index cc75579..136439f 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Backend\Controller\File;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Http\JsonResponse;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
@@ -56,14 +57,6 @@ class FileController
     protected $overwriteExistingFiles;
 
     /**
-     * VeriCode - a hash of server specific value and other things which
-     * identifies if a submission is OK. (see $GLOBALS['BE_USER']->veriCode())
-     *
-     * @var string
-     */
-    protected $vC;
-
-    /**
      * The page where the user should be redirected after everything is done
      *
      * @var string
@@ -96,25 +89,34 @@ class FileController
 
     /**
      * Registering incoming data
-     *
-     * @return void
      */
     protected function init()
     {
         // Set the GPvars from outside
         $this->file = GeneralUtility::_GP('data');
+        if ($this->file === null) {
+            // This happens in clipboard mode only
+            $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
+        } else {
+            $mode = key($this->file);
+            $elementKey = key($this->file[$mode]);
+            $this->redirect = GeneralUtility::sanitizeLocalUrl($this->file[$mode][$elementKey]['redirect']);
+        }
         $this->CB = GeneralUtility::_GP('CB');
-        $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
-        $this->vC = GeneralUtility::_GP('vC');
-        $this->redirect = $this->file['editfile'][0]['redirect'];
+
+        if (isset($this->file['rename'][0]['conflictMode'])) {
+            $conflictMode = $this->file['rename'][0]['conflictMode'];
+            unset($this->file['rename'][0]['conflictMode']);
+            $this->overwriteExistingFiles = DuplicationBehavior::cast($conflictMode);
+        } else {
+            $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
+        }
         $this->initClipboard();
         $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
     }
 
     /**
      * Initialize the Clipboard. This will fetch the data about files to paste/delete if such an action has been sent.
-     *
-     * @return void
      */
     public function initClipboard()
     {
@@ -135,8 +137,6 @@ class FileController
     /**
      * Performing the file admin action:
      * Initializes the objects, setting permissions, sending data to object.
-     *
-     * @return void
      */
     public function main()
     {
@@ -146,7 +146,7 @@ class FileController
         // Checking referrer / executing:
         $refInfo = parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
         $httpHost = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
-        if ($httpHost !== $refInfo['host'] && $this->vC !== $this->getBackendUser()->veriCode() && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
+        if ($httpHost !== $refInfo['host'] && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer']) {
             $this->fileProcessor->writeLog(0, 2, 1, 'Referrer host "%s" and server host "%s" did not match!', [$refInfo['host'], $httpHost]);
         } else {
             $this->fileProcessor->start($this->file);
@@ -157,8 +157,6 @@ class FileController
     /**
      * Redirecting the user after the processing has been done.
      * Might also display error messages directly, if any.
-     *
-     * @return void
      */
     public function finish()
     {
@@ -184,22 +182,26 @@ class FileController
 
         // go and edit the new created file
         if ($request->getParsedBody()['edit']) {
+            /** @var \TYPO3\CMS\Core\Resource\File $file */
+            $file = $this->fileData['newfile'][0];
+            $properties = $file->getProperties();
             $urlParameters = [
-                'target' => $this->file['newfile'][0]['target'] . $this->file['newfile'][0]['data']
+                'target' =>  $properties['storage'] . ':' . $properties['identifier']
             ];
             if ($this->redirect) {
                 $urlParameters['returnUrl'] = $this->redirect;
             }
-            $this->redirect = BackendUtility::getModuleUrl('file_edit', $urlParameters);
+            /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
+            $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
+            $this->redirect = (string)$uriBuilder->buildUriFromRoute('file_edit', $urlParameters);
         }
         if ($this->redirect) {
             return $response
                     ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
                     ->withStatus(303);
-        } else {
-            // empty response
-            return $response;
         }
+        // empty response
+        return $response;
     }
 
     /**
@@ -217,8 +219,10 @@ class FileController
         $this->main();
         $errors = $this->fileProcessor->getErrorMessages();
         if (!empty($errors)) {
-            $response->getBody()->write(implode(',', $errors));
-            $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
+            $response->getBody()->write('<t3err>' . implode(',', $errors) . '</t3err>');
+            $response = $response
+                ->withHeader('Content-Type', 'text/html; charset=utf-8')
+                ->withStatus(500, '(AJAX)');
         } else {
             $flatResult = [];
             foreach ($this->fileData as $action => $results) {
@@ -232,7 +236,7 @@ class FileController
                     }
                 }
             }
-            $response->getBody()->write(json_encode($flatResult));
+            return GeneralUtility::makeInstance(JsonResponse::class)->setPayload($flatResult);
         }
         return $response;
     }
@@ -241,13 +245,12 @@ class FileController
      * Ajax entry point to check if a file exists in a folder
      *
      * @param ServerRequestInterface $request
-     * @param ResponseInterface $response
      * @return ResponseInterface
      */
-    public function fileExistsInFolderAction(ServerRequestInterface $request, ResponseInterface $response)
+    public function fileExistsInFolderAction(ServerRequestInterface $request)
     {
-        $fileName = isset($request->getParsedBody()['fileName']) ? $request->getParsedBody()['fileName'] : $request->getQueryParams()['fileName'];
-        $fileTarget = isset($request->getParsedBody()['fileTarget']) ? $request->getParsedBody()['fileTarget'] : $request->getQueryParams()['fileTarget'];
+        $fileName = $request->getParsedBody()['fileName'] ?? $request->getQueryParams()['fileName'];
+        $fileTarget = $request->getParsedBody()['fileTarget'] ?? $request->getQueryParams()['fileTarget'];
 
         /** @var \TYPO3\CMS\Core\Resource\ResourceFactory $fileFactory */
         $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
@@ -255,12 +258,11 @@ class FileController
         $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
         $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
 
-        $result = false;
+        $result = [];
         if ($fileTargetObject->hasFile($processedFileName)) {
             $result = $this->flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
         }
-        $response->getBody()->write(json_encode($result));
-        return $response;
+        return GeneralUtility::makeInstance(JsonResponse::class)->setPayload($result);
     }
 
     /**