‪TYPO3CMS  9.5
FileController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
38 
46 {
48 
52  private ‪$deprecatedPublicMethods = [
53  'initClipboard' => 'Using FileController::initClipboard() is deprecated and will not be possible anymore in TYPO3 v10.0.',
54  'main' => 'Using FileController::main() is deprecated and will not be possible anymore in TYPO3 v10.0.',
55  ];
56 
62  protected ‪$file;
63 
69  protected ‪$CB;
70 
78 
84  protected ‪$redirect;
85 
92  protected ‪$fileProcessor;
93 
99  protected ‪$fileData;
100 
104  public function ‪__construct()
105  {
106  // @deprecated since TYPO3 v9, will be moved out of __construct() in TYPO3 v10.0
107  $this->‪init(‪$GLOBALS['TYPO3_REQUEST']);
108  }
109 
117  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
118  {
119  $this->‪main();
120 
121  ‪BackendUtility::setUpdateSignal('updateFolderTree');
122 
123  // go and edit the new created file
124  if ($request->getParsedBody()['edit'] ?? '') {
126  ‪$file = $this->fileData['newfile'][0];
127  if (‪$file !== null) {
128  $this->redirect = $this->‪getFileEditRedirect(‪$file) ?? ‪$this->redirect;
129  }
130  }
131  if ($this->redirect) {
132  return new ‪RedirectResponse(
133  GeneralUtility::locationHeaderUrl($this->redirect),
134  303
135  );
136  }
137  // empty response
138  return new ‪HtmlResponse('');
139  }
140 
150  public function ‪processAjaxRequest(ServerRequestInterface $request): ResponseInterface
151  {
152  $this->‪main();
153  ‪$errors = $this->fileProcessor->getErrorMessages();
154  if (!empty(‪$errors)) {
155  return (new ‪HtmlResponse('<t3err>' . implode(',', ‪$errors) . '</t3err>'))->withStatus(500, '(AJAX)');
156  }
157  $flatResult = [];
158  foreach ($this->fileData as $action => $results) {
159  foreach ($results as $result) {
160  if (is_array($result)) {
161  foreach ($result as $subResult) {
162  $flatResult[$action][] = $this->‪flattenResultDataValue($subResult);
163  }
164  } else {
165  $flatResult[$action][] = $this->‪flattenResultDataValue($result);
166  }
167  }
168  }
169  return (new JsonResponse())->setPayload($flatResult);
170  }
171 
178  public function ‪fileExistsInFolderAction(ServerRequestInterface $request): ResponseInterface
179  {
180  $fileName = $request->getParsedBody()['fileName'] ?? $request->getQueryParams()['fileName'] ?? null;
181  $fileTarget = $request->getParsedBody()['fileTarget'] ?? $request->getQueryParams()['fileTarget'] ?? null;
182 
183  $fileFactory = GeneralUtility::makeInstance(ResourceFactory::class);
184  $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
185  $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
186 
187  $result = [];
188  if ($fileTargetObject->hasFile($processedFileName)) {
189  $result = $this->‪flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
190  }
191  return (new ‪JsonResponse())->setPayload($result);
192  }
193 
199  protected function ‪init(ServerRequestInterface $request): void
200  {
201  // Set the GPvars from outside
202  $parsedBody = $request->getParsedBody();
203  $queryParams = $request->getQueryParams();
204  $this->file = $parsedBody['data'] ?? $queryParams['data'] ?? null;
205  if ($this->file === null) {
206  // This happens in clipboard mode only
207  $this->redirect = GeneralUtility::sanitizeLocalUrl($parsedBody['redirect'] ?? $queryParams['redirect'] ?? '');
208  } else {
209  $mode = key($this->file);
210  $elementKey = key($this->file[$mode]);
211  $this->redirect = GeneralUtility::sanitizeLocalUrl($this->file[$mode][$elementKey]['redirect']);
212  }
213  $this->CB = $parsedBody['CB'] ?? $queryParams['CB'] ?? null;
214 
215  if (isset($this->file['rename'][0]['conflictMode'])) {
216  $conflictMode = $this->file['rename'][0]['conflictMode'];
217  unset($this->file['rename'][0]['conflictMode']);
218  $this->overwriteExistingFiles = ‪DuplicationBehavior::cast($conflictMode);
219  } else {
220  $this->overwriteExistingFiles = ‪DuplicationBehavior::cast($parsedBody['overwriteExistingFiles'] ?? $queryParams['overwriteExistingFiles'] ?? null);
221  }
222  $this->‪initClipboard();
223  $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
224  }
225 
229  protected function ‪initClipboard(): void
230  {
231  if (is_array($this->CB)) {
232  $clipObj = GeneralUtility::makeInstance(Clipboard::class);
233  $clipObj->initializeClipboard();
234  if ($this->CB['paste']) {
235  $clipObj->setCurrentPad($this->CB['pad']);
236  $this->file = $clipObj->makePasteCmdArray_file($this->CB['paste'], $this->file);
237  }
238  if ($this->CB['delete']) {
239  $clipObj->setCurrentPad($this->CB['pad']);
240  $this->file = $clipObj->makeDeleteCmdArray_file($this->file);
241  }
242  }
243  }
244 
249  protected function ‪main(): void
250  {
251  // Initializing:
252  $this->fileProcessor->setActionPermissions();
253  $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
254  $this->fileProcessor->start($this->file);
255  $this->fileData = $this->fileProcessor->processData();
256  }
257 
265  protected function ‪getFileEditRedirect(‪File ‪$file): ?string
266  {
267  $textFileExtensionList = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? '';
268  if (!GeneralUtility::inList($textFileExtensionList, ‪$file->getExtension())) {
269  return null;
270  }
271  ‪$properties = ‪$file->getProperties();
272  $urlParameters = [
273  'target' => ‪$properties['storage'] . ':' . ‪$properties['identifier']
274  ];
275  if ($this->redirect) {
276  $urlParameters['returnUrl'] = ‪$this->redirect;
277  }
278  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
279  return (string)$uriBuilder->buildUriFromRoute('file_edit', $urlParameters);
280  }
281 
288  public function ‪finish()
289  {
290  trigger_error('FileController->finish() will be removed in TYPO3 v10.0. Do not call from other extension.', E_USER_DEPRECATED);
291  ‪BackendUtility::setUpdateSignal('updateFolderTree');
292  if ($this->redirect) {
293  ‪HttpUtility::redirect($this->redirect);
294  }
295  }
296 
306  protected function ‪flattenResultDataValue($result)
307  {
308  if ($result instanceof File) {
309  $thumbUrl = '';
310  if (GeneralUtility::inList(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
311  $processedFile = $result->process(‪ProcessedFile::CONTEXT_IMAGEPREVIEW, []);
312  if ($processedFile) {
313  $thumbUrl = $processedFile->getPublicUrl(true);
314  }
315  }
316  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
317  $result = array_merge(
318  $result->toArray(),
319  [
320  'date' => ‪BackendUtility::date($result->getModificationTime()),
321  'icon' => $iconFactory->getIconForFileExtension($result->getExtension(), ‪Icon::SIZE_SMALL)->render(),
322  'thumbUrl' => $thumbUrl
323  ]
324  );
325  } elseif ($result instanceof Folder) {
326  $result = $result->getIdentifier();
327  }
328 
329  return $result;
330  }
331 
337  protected function ‪getBackendUser(): ‪BackendUserAuthentication
338  {
339  return ‪$GLOBALS['BE_USER'];
340  }
341 }
‪TYPO3\CMS\Backend\Controller\File\FileController\main
‪main()
Definition: FileController.php:241
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:29
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:50
‪TYPO3\CMS\Backend\Controller\File\FileController\init
‪init(ServerRequestInterface $request)
Definition: FileController.php:191
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:23
‪TYPO3\CMS\Backend\Clipboard\Clipboard
Definition: Clipboard.php:38
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Backend\Controller\File\FileController\fileExistsInFolderAction
‪ResponseInterface fileExistsInFolderAction(ServerRequestInterface $request)
Definition: FileController.php:170
‪TYPO3\CMS\Backend\Utility\BackendUtility\setUpdateSignal
‪static setUpdateSignal($set='', $params='')
Definition: BackendUtility.php:3164
‪TYPO3\CMS\Backend\Controller\File\FileController\$redirect
‪string $redirect
Definition: FileController.php:78
‪TYPO3\CMS\Backend\Controller\File\FileController\$fileData
‪array $fileData
Definition: FileController.php:91
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:31
‪TYPO3\CMS\Backend\Controller\File\FileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: FileController.php:109
‪TYPO3\CMS\Backend\Controller\File\FileController\finish
‪finish()
Definition: FileController.php:280
‪TYPO3\CMS\Backend\Controller\File\FileController\__construct
‪__construct()
Definition: FileController.php:96
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:182
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
Definition: ExtendedFileUtility.php:63
‪TYPO3\CMS\Backend\Controller\File\FileController\$deprecatedPublicMethods
‪array $deprecatedPublicMethods
Definition: FileController.php:50
‪TYPO3\CMS\Backend\Controller\File\FileController\$overwriteExistingFiles
‪TYPO3 CMS Core Resource DuplicationBehavior $overwriteExistingFiles
Definition: FileController.php:72
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:35
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait
Definition: PublicMethodDeprecationTrait.php:68
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:27
‪$errors
‪$errors
Definition: annotationChecker.php:115
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:42
‪TYPO3\CMS\Backend\Controller\File\FileController
Definition: FileController.php:46
‪TYPO3\CMS\Backend\Controller\File\FileController\$fileProcessor
‪ExtendedFileUtility $fileProcessor
Definition: FileController.php:85
‪TYPO3\CMS\Backend\Controller\File\FileController\flattenResultDataValue
‪bool string array flattenResultDataValue($result)
Definition: FileController.php:298
‪TYPO3\CMS\Backend\Controller\File\FileController\getFileEditRedirect
‪string null getFileEditRedirect(File $file)
Definition: FileController.php:257
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:33
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:21
‪TYPO3\CMS\Backend\Controller\File
Definition: CreateFolderController.php:3
‪TYPO3\CMS\Backend\Controller\File\FileController\processAjaxRequest
‪ResponseInterface processAjaxRequest(ServerRequestInterface $request)
Definition: FileController.php:142
‪TYPO3\CMS\Backend\Controller\File\FileController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: FileController.php:329
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Controller\File\FileController\$CB
‪array $CB
Definition: FileController.php:65
‪TYPO3\CMS\Backend\Utility\BackendUtility\date
‪static string date($tstamp)
Definition: BackendUtility.php:1179
‪TYPO3\CMS\Core\Utility\HttpUtility\redirect
‪static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:103
‪TYPO3\CMS\Backend\Controller\File\FileController\$file
‪array $file
Definition: FileController.php:59
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:25
‪TYPO3\CMS\Backend\Controller\File\FileController\initClipboard
‪initClipboard()
Definition: FileController.php:221