TYPO3 CMS  TYPO3_8-7
FileController.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
27 
36 {
42  protected $file;
43 
49  protected $CB;
50 
58 
64  protected $redirect;
65 
72  protected $fileProcessor;
73 
79  protected $fileData;
80 
84  public function __construct()
85  {
86  $GLOBALS['SOBE'] = $this;
87  $this->init();
88  }
89 
93  protected function init()
94  {
95  // Set the GPvars from outside
96  $this->file = GeneralUtility::_GP('file');
97  $this->CB = GeneralUtility::_GP('CB');
98  if (isset($this->file['rename'][0]['conflictMode'])) {
99  $conflictMode = $this->file['rename'][0]['conflictMode'];
100  unset($this->file['rename'][0]['conflictMode']);
101  $this->overwriteExistingFiles = DuplicationBehavior::cast($conflictMode);
102  } else {
103  $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
104  }
105  $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
106  $this->initClipboard();
107  $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
108  }
109 
113  public function initClipboard()
114  {
115  if (is_array($this->CB)) {
116  $clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
117  $clipObj->initializeClipboard();
118  if ($this->CB['paste']) {
119  $clipObj->setCurrentPad($this->CB['pad']);
120  $this->file = $clipObj->makePasteCmdArray_file($this->CB['paste'], $this->file);
121  }
122  if ($this->CB['delete']) {
123  $clipObj->setCurrentPad($this->CB['pad']);
124  $this->file = $clipObj->makeDeleteCmdArray_file($this->file);
125  }
126  }
127  }
128 
133  public function main()
134  {
135  // Initializing:
136  $this->fileProcessor->setActionPermissions();
137  $this->fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
138  $this->fileProcessor->start($this->file);
139  $this->fileData = $this->fileProcessor->processData();
140  }
141 
146  public function finish()
147  {
148  BackendUtility::setUpdateSignal('updateFolderTree');
149  if ($this->redirect) {
151  }
152  }
153 
162  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
163  {
164  $this->main();
165 
166  BackendUtility::setUpdateSignal('updateFolderTree');
167 
168  // go and edit the new created file
169  if ($request->getParsedBody()['edit']) {
171  $file = $this->fileData['newfile'][0];
172  if ($file !== null) {
173  $this->redirect = $this->getFileEditRedirect($file) ?? $this->redirect;
174  }
175  }
176  if ($this->redirect) {
177  return $response
178  ->withHeader('Location', GeneralUtility::locationHeaderUrl($this->redirect))
179  ->withStatus(303);
180  }
181  // empty response
182  return $response;
183  }
184 
192  protected function getFileEditRedirect(File $file)
193  {
194  $textFileExtensionList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? '';
195  if (!GeneralUtility::inList($textFileExtensionList, $file->getExtension())) {
196  return null;
197  }
198  $properties = $file->getProperties();
199  $urlParameters = [
200  'target' => $properties['storage'] . ':' . $properties['identifier']
201  ];
202  if ($this->redirect) {
203  $urlParameters['returnUrl'] = $this->redirect;
204  }
205  return BackendUtility::getModuleUrl('file_edit', $urlParameters);
206  }
207 
218  public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
219  {
220  $this->main();
221  $errors = $this->fileProcessor->getErrorMessages();
222  if (!empty($errors)) {
223  $response->getBody()->write('<t3err>' . implode(',', $errors) . '</t3err>');
224  $response = $response
225  ->withHeader('Content-Type', 'text/html; charset=utf-8')
226  ->withStatus(500, '(AJAX)');
227  } else {
228  $flatResult = [];
229  foreach ($this->fileData as $action => $results) {
230  foreach ($results as $result) {
231  if (is_array($result)) {
232  foreach ($result as $subResult) {
233  $flatResult[$action][] = $this->flattenResultDataValue($subResult);
234  }
235  } else {
236  $flatResult[$action][] = $this->flattenResultDataValue($result);
237  }
238  }
239  }
240  $response->getBody()->write(json_encode($flatResult));
241  }
242  return $response;
243  }
244 
252  public function fileExistsInFolderAction(ServerRequestInterface $request, ResponseInterface $response)
253  {
254  $fileName = isset($request->getParsedBody()['fileName']) ? $request->getParsedBody()['fileName'] : $request->getQueryParams()['fileName'];
255  $fileTarget = isset($request->getParsedBody()['fileTarget']) ? $request->getParsedBody()['fileTarget'] : $request->getQueryParams()['fileTarget'];
256 
258  $fileFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
260  $fileTargetObject = $fileFactory->retrieveFileOrFolderObject($fileTarget);
261  $processedFileName = $fileTargetObject->getStorage()->sanitizeFileName($fileName, $fileTargetObject);
262 
263  $result = false;
264  if ($fileTargetObject->hasFile($processedFileName)) {
265  $result = $this->flattenResultDataValue($fileTargetObject->getStorage()->getFileInFolder($processedFileName, $fileTargetObject));
266  }
267  $response->getBody()->write(json_encode($result));
268  return $response;
269  }
270 
279  protected function flattenResultDataValue($result)
280  {
281  if ($result instanceof File) {
282  $thumbUrl = '';
283  if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $result->getExtension())) {
284  $processedFile = $result->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, []);
285  if ($processedFile) {
286  $thumbUrl = $processedFile->getPublicUrl(true);
287  }
288  }
289  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
290  $result = array_merge(
291  $result->toArray(),
292  [
293  'date' => BackendUtility::date($result->getModificationTime()),
294  'icon' => $iconFactory->getIconForFileExtension($result->getExtension(), Icon::SIZE_SMALL)->render(),
295  'thumbUrl' => $thumbUrl
296  ]
297  );
298  } elseif ($result instanceof \TYPO3\CMS\Core\Resource\Folder) {
299  $result = $result->getIdentifier();
300  }
301 
302  return $result;
303  }
304 
310  protected function getBackendUser()
311  {
312  return $GLOBALS['BE_USER'];
313  }
314 }
static makeInstance($className,... $constructorArguments)
static setUpdateSignal($set='', $params='')
processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
static redirect($url, $httpStatus=self::HTTP_STATUS_303)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']