‪TYPO3CMS  10.4
FileController.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
39 
47 {
53  protected ‪$file;
54 
60  protected ‪$CB;
61 
69 
75  protected ‪$redirect;
76 
83  protected ‪$fileProcessor;
84 
90  protected ‪$fileData;
91 
100  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
101  {
102  $this->‪init($request);
103  $this->‪main();
104 
105  ‪BackendUtility::setUpdateSignal('updateFolderTree');
106 
107  // go and edit the new created file
108  if ($request->getParsedBody()['edit'] ?? '') {
110  ‪$file = $this->fileData['newfile'][0];
111  if (‪$file !== null) {
112  $this->redirect = $this->‪getFileEditRedirect(‪$file) ?? ‪$this->redirect;
113  }
114  }
115  if ($this->redirect) {
116  return new ‪RedirectResponse(
117  GeneralUtility::locationHeaderUrl($this->redirect),
118  303
119  );
120  }
121  // empty response
122  return new ‪HtmlResponse('');
123  }
124 
132  public function ‪processAjaxRequest(ServerRequestInterface $request): ResponseInterface
133  {
134  $this->‪init($request);
135  $this->‪main();
136  ‪$errors = $this->fileProcessor->getErrorMessages();
137  if (!empty(‪$errors)) {
138  ‪$errors = array_map('htmlspecialchars', ‪$errors);
139  return (new ‪HtmlResponse('<t3err>' . implode(',', ‪$errors) . '</t3err>'))->withStatus(500, '(AJAX)');
140  }
141  $flatResult = [];
142  foreach ($this->fileData as $action => $results) {
143  foreach ($results as $result) {
144  if (is_array($result)) {
145  foreach ($result as $subResult) {
146  $flatResult[$action][] = $this->‪flattenResultDataValue($subResult);
147  }
148  } else {
149  $flatResult[$action][] = $this->‪flattenResultDataValue($result);
150  }
151  }
152  }
153  return (new JsonResponse())->setPayload($flatResult);
154  }
155 
162  public function ‪fileExistsInFolderAction(ServerRequestInterface $request): ResponseInterface
163  {
164  $this->‪init($request);
165  $fileName = $request->getParsedBody()['fileName'] ?? $request->getQueryParams()['fileName'] ?? null;
166  $fileTarget = $request->getParsedBody()['fileTarget'] ?? $request->getQueryParams()['fileTarget'] ?? null;
167 
168  $fileFactory = GeneralUtility::makeInstance(ResourceFactory::class);
169  $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
170  $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
171 
172  $result = [];
173  if ($fileTargetObject->hasFile($processedFileName)) {
174  $result = $this->‪flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
175  }
176  return (new JsonResponse())->setPayload($result);
177  }
178 
184  protected function ‪init(ServerRequestInterface $request): void
185  {
186  // Set the GPvars from outside
187  $parsedBody = $request->getParsedBody();
188  $queryParams = $request->getQueryParams();
189  $this->file = $parsedBody['data'] ?? $queryParams['data'] ?? null;
190  if ($this->file === null) {
191  // This happens in clipboard mode only
192  $this->redirect = GeneralUtility::sanitizeLocalUrl($parsedBody['redirect'] ?? $queryParams['redirect'] ?? '');
193  } else {
194  $mode = key($this->file);
195  $elementKey = key($this->file[$mode]);
196  $this->redirect = GeneralUtility::sanitizeLocalUrl($this->file[$mode][$elementKey]['redirect']);
197  }
198  $this->CB = $parsedBody['CB'] ?? $queryParams['CB'] ?? null;
199 
200  if (isset($this->file['rename'][0]['conflictMode'])) {
201  $conflictMode = $this->file['rename'][0]['conflictMode'];
202  unset($this->file['rename'][0]['conflictMode']);
203  $this->overwriteExistingFiles = ‪DuplicationBehavior::cast($conflictMode);
204  } else {
205  $this->overwriteExistingFiles = ‪DuplicationBehavior::cast($parsedBody['overwriteExistingFiles'] ?? $queryParams['overwriteExistingFiles'] ?? null);
206  }
207  $this->‪initClipboard();
208  $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
209  }
210 
214  protected function ‪initClipboard(): void
215  {
216  if (is_array($this->CB)) {
217  $clipObj = GeneralUtility::makeInstance(Clipboard::class);
218  $clipObj->initializeClipboard();
219  if ($this->CB['paste']) {
220  $clipObj->setCurrentPad($this->CB['pad']);
221  $this->file = $clipObj->makePasteCmdArray_file($this->CB['paste'], $this->file);
222  }
223  if ($this->CB['delete']) {
224  $clipObj->setCurrentPad($this->CB['pad']);
225  $this->file = $clipObj->makeDeleteCmdArray_file($this->file);
226  }
227  }
228  }
229 
234  protected function ‪main(): void
235  {
236  // Initializing:
237  $this->fileProcessor->setActionPermissions();
238  $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
239  $this->fileProcessor->start($this->file);
240  $this->fileData = $this->fileProcessor->processData();
241  }
242 
250  protected function ‪getFileEditRedirect(‪File ‪$file): ?string
251  {
252  if (!‪$file->isTextFile()) {
253  return null;
254  }
255  ‪$properties = ‪$file->getProperties();
256  $urlParameters = [
257  'target' => ‪$properties['storage'] . ':' . ‪$properties['identifier']
258  ];
259  if ($this->redirect) {
260  $urlParameters['returnUrl'] = ‪$this->redirect;
261  }
262  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
263  try {
264  return (string)$uriBuilder->buildUriFromRoute('file_edit', $urlParameters);
265  } catch (RouteNotFoundException $exception) {
266  // no route for editing files available
267  return '';
268  }
269  }
270 
280  protected function ‪flattenResultDataValue($result)
281  {
282  if ($result instanceof File) {
283  $thumbUrl = '';
284  if ($result->isImage()) {
285  $processedFile = $result->process(‪ProcessedFile::CONTEXT_IMAGEPREVIEW, []);
286  if ($processedFile) {
287  $thumbUrl = $processedFile->getPublicUrl(true);
288  }
289  }
290  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
291  $result = array_merge(
292  $result->toArray(),
293  [
294  'date' => ‪BackendUtility::date($result->getModificationTime()),
295  'icon' => $iconFactory->getIconForFileExtension($result->getExtension(), ‪Icon::SIZE_SMALL)->render(),
296  'thumbUrl' => $thumbUrl
297  ]
298  );
299  } elseif ($result instanceof Folder) {
300  $result = $result->getIdentifier();
301  }
302 
303  return $result;
304  }
305 
311  protected function ‪getBackendUser(): ‪BackendUserAuthentication
312  {
313  return ‪$GLOBALS['BE_USER'];
314  }
315 }
‪TYPO3\CMS\Backend\Controller\File\FileController\main
‪main()
Definition: FileController.php:228
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:52
‪TYPO3\CMS\Backend\Controller\File\FileController\init
‪init(ServerRequestInterface $request)
Definition: FileController.php:178
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:24
‪TYPO3\CMS\Backend\Clipboard\Clipboard
Definition: Clipboard.php:43
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Controller\File\FileController\fileExistsInFolderAction
‪ResponseInterface fileExistsInFolderAction(ServerRequestInterface $request)
Definition: FileController.php:156
‪TYPO3\CMS\Backend\Utility\BackendUtility\setUpdateSignal
‪static setUpdateSignal($set='', $params='')
Definition: BackendUtility.php:2798
‪TYPO3\CMS\Backend\Controller\File\FileController\$redirect
‪string $redirect
Definition: FileController.php:71
‪TYPO3\CMS\Backend\Controller\File\FileController\$fileData
‪array $fileData
Definition: FileController.php:84
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Backend\Controller\File\FileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: FileController.php:94
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
Definition: ExtendedFileUtility.php:70
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:22
‪TYPO3\CMS\Backend\Controller\File\FileController\$overwriteExistingFiles
‪TYPO3 CMS Core Resource DuplicationBehavior $overwriteExistingFiles
Definition: FileController.php:65
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪$errors
‪$errors
Definition: annotationChecker.php:121
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Backend\Controller\File\FileController
Definition: FileController.php:47
‪TYPO3\CMS\Backend\Controller\File\FileController\$fileProcessor
‪ExtendedFileUtility $fileProcessor
Definition: FileController.php:78
‪TYPO3\CMS\Backend\Controller\File\FileController\flattenResultDataValue
‪bool string array flattenResultDataValue($result)
Definition: FileController.php:274
‪TYPO3\CMS\Backend\Controller\File\FileController\getFileEditRedirect
‪string null getFileEditRedirect(File $file)
Definition: FileController.php:244
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:35
‪TYPO3\CMS\Backend\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Backend\Controller\File\FileController\processAjaxRequest
‪ResponseInterface processAjaxRequest(ServerRequestInterface $request)
Definition: FileController.php:126
‪TYPO3\CMS\Backend\Controller\File\FileController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: FileController.php:305
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Controller\File\FileController\$CB
‪array $CB
Definition: FileController.php:58
‪TYPO3\CMS\Backend\Utility\BackendUtility\date
‪static string date($tstamp)
Definition: BackendUtility.php:978
‪TYPO3\CMS\Backend\Controller\File\FileController\$file
‪array $file
Definition: FileController.php:52
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Backend\Controller\File\FileController\initClipboard
‪initClipboard()
Definition: FileController.php:208