TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ExtendedFileUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility\File;
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 
44 
62 {
70 
78  public $actionPerms = [
79  // File permissions
80  'addFile' => false,
81  'readFile' => false,
82  'writeFile' => false,
83  'copyFile' => false,
84  'moveFile' => false,
85  'renameFile' => false,
86  'deleteFile' => false,
87  // Folder permissions
88  'addFolder' => false,
89  'readFolder' => false,
90  'writeFolder' => false,
91  'copyFolder' => false,
92  'moveFolder' => false,
93  'renameFolder' => false,
94  'deleteFolder' => false,
95  'recursivedeleteFolder' => false
96  ];
97 
103  public $internalUploadMap = [];
104 
110  protected $errorMessages = [];
111 
117  protected $flashMessages = [];
118 
122  protected $fileCmdMap;
123 
129  protected $fileFactory;
130 
137  {
138  return (string)$this->existingFilesConflictMode;
139  }
140 
149  {
150  try {
151  $this->existingFilesConflictMode = DuplicationBehavior::cast($existingFilesConflictMode);
152  } catch (InvalidEnumerationValueException $e) {
153  throw new Exception(
154  sprintf(
155  'Invalid argument, received: "%s", expected a value from enumeration \TYPO3\CMS\Core\Resource\DuplicationBehavior (%s)',
157  implode(', ', DuplicationBehavior::getConstants())
158  ),
159  1476046229
160  );
161  }
162  }
163 
170  public function start($fileCmds)
171  {
172  // Initialize Object Factory
173  $this->fileFactory = ResourceFactory::getInstance();
174  // Initializing file processing commands:
175  $this->fileCmdMap = $fileCmds;
176  }
177 
185  public function setActionPermissions(array $permissions = [])
186  {
187  if (empty($permissions)) {
188  $permissions = $this->getBackendUser()->getFilePermissions();
189  }
190  $this->actionPerms = $permissions;
191  }
192 
199  public function processData()
200  {
201  $result = [];
202  if (is_array($this->fileCmdMap)) {
203  // Check if there were uploads expected, but no one made
204  if ($this->fileCmdMap['upload']) {
205  $uploads = $this->fileCmdMap['upload'];
206  foreach ($uploads as $upload) {
207  if (empty($_FILES['upload_' . $upload['data']]['name'])
208  || (is_array($_FILES['upload_' . $upload['data']]['name'])
209  && empty($_FILES['upload_' . $upload['data']]['name'][0])
210  )
211  ) {
212  unset($this->fileCmdMap['upload'][$upload['data']]);
213  }
214  }
215  if (empty($this->fileCmdMap['upload'])) {
216  $this->writeLog(1, 1, 108, 'No file was uploaded!', []);
217  $this->addMessageToFlashMessageQueue('FileUtility.NoFileWasUploaded');
218  }
219  }
220 
221  // Check if there were new folder names expected, but non given
222  if ($this->fileCmdMap['newfolder']) {
223  foreach ($this->fileCmdMap['newfolder'] as $key => $cmdArr) {
224  if (empty($cmdArr['data'])) {
225  unset($this->fileCmdMap['newfolder'][$key]);
226  }
227  }
228  if (empty($this->fileCmdMap['newfolder'])) {
229  $this->writeLog(6, 1, 108, 'No name for new folder given!', []);
230  $this->addMessageToFlashMessageQueue('FileUtility.NoNameForNewFolderGiven');
231  }
232  }
233 
234  // Traverse each set of actions
235  foreach ($this->fileCmdMap as $action => $actionData) {
236  // Traverse all action data. More than one file might be affected at the same time.
237  if (is_array($actionData)) {
238  $result[$action] = [];
239  foreach ($actionData as $cmdArr) {
240  // Clear file stats
241  clearstatcache();
242  // Branch out based on command:
243  switch ($action) {
244  case 'delete':
245  $result[$action][] = $this->func_delete($cmdArr);
246  break;
247  case 'copy':
248  $result[$action][] = $this->func_copy($cmdArr);
249  break;
250  case 'move':
251  $result[$action][] = $this->func_move($cmdArr);
252  break;
253  case 'rename':
254  $result[$action][] = $this->func_rename($cmdArr);
255  break;
256  case 'newfolder':
257  $result[$action][] = $this->func_newfolder($cmdArr);
258  break;
259  case 'newfile':
260  $result[$action][] = $this->func_newfile($cmdArr);
261  break;
262  case 'editfile':
263  $result[$action][] = $this->func_edit($cmdArr);
264  break;
265  case 'upload':
266  $result[$action][] = $this->func_upload($cmdArr);
267  break;
268  case 'replace':
269  $result[$action][] = $this->replaceFile($cmdArr);
270  break;
271  }
272  // Hook for post-processing the action
273  if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'])) {
274  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'] as $classRef) {
275  $hookObject = GeneralUtility::getUserObj($classRef);
276  if (!$hookObject instanceof ExtendedFileUtilityProcessDataHookInterface) {
277  throw new \UnexpectedValueException($classRef . ' must implement interface ' . ExtendedFileUtilityProcessDataHookInterface::class, 1279719168);
278  }
279  $hookObject->processData_postProcessAction($action, $cmdArr, $result[$action], $this);
280  }
281  }
282  }
283  }
284  }
285  }
286  return $result;
287  }
288 
296  {
298  foreach ($this->getErrorMessages() as $msg) {
299  $flashMessage = GeneralUtility::makeInstance(
300  FlashMessage::class,
301  $msg,
302  '',
304  true
305  );
306  $this->addFlashMessage($flashMessage);
307  }
308  }
309 
315  public function getErrorMessages()
316  {
317  return $this->errorMessages;
318  }
319 
328  public function writeLog($action, $error, $details_nr, $details, $data)
329  {
330  // Type value for tce_file.php
331  $type = 2;
332  if (is_object($this->getBackendUser())) {
333  $this->getBackendUser()->writelog($type, $action, $error, $details_nr, $details, $data);
334  }
335  if ($error > 0) {
336  $this->errorMessages[] = vsprintf($details, $data);
337  }
338  }
339 
349  protected function addMessageToFlashMessageQueue($localizationKey, array $replaceMarkers = [], $severity = FlashMessage::ERROR)
350  {
351  if (TYPO3_MODE !== 'BE') {
352  return;
353  }
354  $label = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/fileMessages.xlf:' . $localizationKey);
355  $message = vsprintf($label, $replaceMarkers);
356  $flashMessage = GeneralUtility::makeInstance(
357  FlashMessage::class,
358  $message,
359  '',
360  $severity,
361  true
362  );
363  $this->addFlashMessage($flashMessage);
364  }
365 
366  /*************************************
367  *
368  * File operation functions
369  *
370  **************************************/
377  public function func_delete(array $cmds)
378  {
379  $result = false;
380  // Example identifier for $cmds['data'] => "4:mypath/tomyfolder/myfile.jpg"
381  // for backwards compatibility: the combined file identifier was the path+filename
382  try {
383  $fileObject = $this->getFileObject($cmds['data']);
384  } catch (ResourceDoesNotExistException $e) {
385  $flashMessage = GeneralUtility::makeInstance(
386  FlashMessage::class,
387  sprintf(
388  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.description.fileNotFound'),
389  $cmds['data']
390  ),
391  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.header.fileNotFound'),
393  true
394  );
395  $this->addFlashMessage($flashMessage);
396 
397  return false;
398  }
399  // checks to delete the file
400  if ($fileObject instanceof File) {
401  // check if the file still has references
402  // Exclude sys_file_metadata records as these are no use references
403  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
404  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
405  $refIndexRecords = $queryBuilder
406  ->select('tablename', 'recuid', 'ref_uid')
407  ->from('sys_refindex')
408  ->where(
409  $queryBuilder->expr()->eq(
410  'ref_table',
411  $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
412  ),
413  $queryBuilder->expr()->eq(
414  'ref_uid',
415  $queryBuilder->createNamedParameter($fileObject->getUid(), \PDO::PARAM_INT)
416  ),
417  $queryBuilder->expr()->neq(
418  'tablename',
419  $queryBuilder->createNamedParameter('sys_file_metadata', \PDO::PARAM_STR)
420  )
421  )
422  ->execute()
423  ->fetchAll();
424  $deleteFile = true;
425  if (!empty($refIndexRecords)) {
426  $shortcutContent = [];
427  $brokenReferences = [];
428 
429  foreach ($refIndexRecords as $fileReferenceRow) {
430  if ($fileReferenceRow['tablename'] === 'sys_file_reference') {
431  $row = $this->transformFileReferenceToRecordReference($fileReferenceRow);
432  $shortcutRecord = BackendUtility::getRecord($row['tablename'], $row['recuid']);
433 
434  if ($shortcutRecord) {
435  $shortcutContent[] = '[record:' . $row['tablename'] . ':' . $row['recuid'] . ']';
436  } else {
437  $brokenReferences[] = $fileReferenceRow['ref_uid'];
438  }
439  }
440  }
441  if (!empty($brokenReferences)) {
442  // render a message that the file has broken references
443  $flashMessage = GeneralUtility::makeInstance(
444  FlashMessage::class,
445  sprintf($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.description.fileHasBrokenReferences'), count($brokenReferences)),
446  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.header.fileHasBrokenReferences'),
448  true
449  );
450  $this->addFlashMessage($flashMessage);
451  }
452  if (!empty($shortcutContent)) {
453  // render a message that the file could not be deleted
454  $flashMessage = GeneralUtility::makeInstance(
455  FlashMessage::class,
456  sprintf($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.description.fileNotDeletedHasReferences'), $fileObject->getName()) . ' ' . implode(', ', $shortcutContent),
457  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.header.fileNotDeletedHasReferences'),
459  true
460  );
461  $this->addFlashMessage($flashMessage);
462  $deleteFile = false;
463  }
464  }
465 
466  if ($deleteFile) {
467  try {
468  $result = $fileObject->delete();
469 
470  // show the user that the file was deleted
471  $flashMessage = GeneralUtility::makeInstance(
472  FlashMessage::class,
473  sprintf($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.description.fileDeleted'), $fileObject->getName()),
474  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.header.fileDeleted'),
476  true
477  );
478  $this->addFlashMessage($flashMessage);
479  // Log success
480  $this->writeLog(4, 0, 1, 'File "%s" deleted', [$fileObject->getIdentifier()]);
481  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
482  $this->writeLog(4, 1, 112, 'You are not allowed to access the file', [$fileObject->getIdentifier()]);
483  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToAccessTheFile', [$fileObject->getIdentifier()]);
484  } catch (NotInMountPointException $e) {
485  $this->writeLog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', [$fileObject->getIdentifier()]);
486  $this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotWithinYourMountpoints', [$fileObject->getIdentifier()]);
487  } catch (\RuntimeException $e) {
488  $this->writeLog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', [$fileObject->getIdentifier()]);
489  $this->addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteFile', [$fileObject->getIdentifier()]);
490  }
491  }
492  } else {
494  if (!$this->folderHasFilesInUse($fileObject)) {
495  try {
496  $result = $fileObject->delete(true);
497  if ($result) {
498  // notify the user that the folder was deleted
500  $flashMessage = GeneralUtility::makeInstance(
501  FlashMessage::class,
502  sprintf($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.description.folderDeleted'), $fileObject->getName()),
503  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.header.folderDeleted'),
505  true
506  );
507  $this->addFlashMessage($flashMessage);
508  // Log success
509  $this->writeLog(4, 0, 3, 'Directory "%s" deleted', [$fileObject->getIdentifier()]);
510  }
511  } catch (InsufficientUserPermissionsException $e) {
512  $this->writeLog(4, 1, 120, 'Could not delete directory! Is directory "%s" empty? (You are not allowed to delete directories recursively).', [$fileObject->getIdentifier()]);
513  $this->addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteDirectory', [$fileObject->getIdentifier()]);
514  } catch (InsufficientFolderAccessPermissionsException $e) {
515  $this->writeLog(4, 1, 123, 'You are not allowed to access the directory', [$fileObject->getIdentifier()]);
516  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToAccessTheDirectory', [$fileObject->getIdentifier()]);
517  } catch (NotInMountPointException $e) {
518  $this->writeLog(4, 1, 121, 'Target was not within your mountpoints! T="%s"', [$fileObject->getIdentifier()]);
519  $this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotWithinYourMountpoints', [$fileObject->getIdentifier()]);
520  } catch (\TYPO3\CMS\Core\Resource\Exception\FileOperationErrorException $e) {
521  $this->writeLog(4, 1, 120, 'Could not delete directory "%s"! Write-permission problem?', [$fileObject->getIdentifier()]);
522  $this->addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteDirectory', [$fileObject->getIdentifier()]);
523  }
524  }
525  }
526 
527  return $result;
528  }
529 
538  public function folderHasFilesInUse(Folder $folder)
539  {
540  $files = $folder->getFiles(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true);
541  if (empty($files)) {
542  return false;
543  }
544 
546  $fileUids = [];
547  foreach ($files as $file) {
548  $fileUids[] = $file->getUid();
549  }
550 
551  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
552  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
553  $numberOfReferences = $queryBuilder
554  ->count('hash')
555  ->from('sys_refindex')
556  ->where(
557  $queryBuilder->expr()->eq(
558  'ref_table',
559  $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
560  ),
561  $queryBuilder->expr()->in(
562  'ref_uid',
563  $queryBuilder->createNamedParameter($fileUids, Connection::PARAM_INT_ARRAY)
564  ),
565  $queryBuilder->expr()->neq(
566  'tablename',
567  $queryBuilder->createNamedParameter('sys_file_metadata', \PDO::PARAM_STR)
568  )
569  )->execute()->fetchColumn(0);
570 
571  $hasReferences = $numberOfReferences > 0;
572  if ($hasReferences) {
574  $flashMessage = GeneralUtility::makeInstance(
575  FlashMessage::class,
576  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences'),
577  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences'),
579  true
580  );
581  $this->addFlashMessage($flashMessage);
582  }
583 
584  return $hasReferences;
585  }
586 
594  protected function transformFileReferenceToRecordReference(array $referenceRecord)
595  {
596  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
597  $queryBuilder->getRestrictions()->removeAll();
598  $fileReference = $queryBuilder
599  ->select('uid_foreign', 'tablenames', 'fieldname', 'sorting_foreign')
600  ->from('sys_file_reference')
601  ->where(
602  $queryBuilder->expr()->eq(
603  'uid',
604  $queryBuilder->createNamedParameter($referenceRecord['recuid'], \PDO::PARAM_INT)
605  )
606  )
607  ->execute()
608  ->fetch();
609 
610  return [
611  'recuid' => $fileReference['uid_foreign'],
612  'tablename' => $fileReference['tablenames'],
613  'field' => $fileReference['fieldname'],
614  'flexpointer' => '',
615  'softref_key' => '',
616  'sorting' => $fileReference['sorting_foreign']
617  ];
618  }
619 
628  protected function getFileObject($identifier)
629  {
630  $object = $this->fileFactory->retrieveFileOrFolderObject($identifier);
631  if (!is_object($object)) {
632  throw new \TYPO3\CMS\Core\Resource\Exception\InvalidFileException('The item ' . $identifier . ' was not a file or directory!!', 1320122453);
633  }
634  if ($object->getStorage()->getUid() === 0) {
635  throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889830);
636  }
637  return $object;
638  }
639 
653  protected function func_copy($cmds)
654  {
655  $sourceFileObject = $this->getFileObject($cmds['data']);
657  $targetFolderObject = $this->getFileObject($cmds['target']);
658  // Basic check
659  if (!$targetFolderObject instanceof Folder) {
660  $this->writeLog(2, 2, 100, 'Destination "%s" was not a directory', [$cmds['target']]);
661  $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
662  return false;
663  }
664  // If this is TRUE, we append _XX to the file name if
665  $appendSuffixOnConflict = (string)$cmds['altName'];
666  $resultObject = null;
667  $conflictMode = $appendSuffixOnConflict !== '' ? DuplicationBehavior::RENAME : DuplicationBehavior::CANCEL;
668  // Copying the file
669  if ($sourceFileObject instanceof File) {
670  try {
671  $resultObject = $sourceFileObject->copyTo($targetFolderObject, null, $conflictMode);
672  } catch (InsufficientUserPermissionsException $e) {
673  $this->writeLog(2, 1, 114, 'You are not allowed to copy files', []);
674  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCopyFiles');
675  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
676  $this->writeLog(2, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
677  $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
678  } catch (IllegalFileExtensionException $e) {
679  $this->writeLog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
680  $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
681  } catch (ExistingTargetFileNameException $e) {
682  $this->writeLog(2, 1, 112, 'File "%s" already exists in folder "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
683  $this->addMessageToFlashMessageQueue('FileUtility.FileAlreadyExistsInFolder', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
684  } catch (\BadMethodCallException $e) {
685  $this->writeLog(3, 1, 128, 'The function to copy a file between storages is not yet implemented', []);
686  $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToCopyAFileBetweenStoragesIsNotYetImplemented');
687  } catch (\RuntimeException $e) {
688  $this->writeLog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
689  $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotCopiedTo', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
690  }
691  if ($resultObject) {
692  $this->writeLog(2, 0, 1, 'File "%s" copied to "%s"', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()]);
693  $this->addMessageToFlashMessageQueue('FileUtility.FileCopiedTo', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()], FlashMessage::OK);
694  }
695  } else {
696  // Else means this is a Folder
697  $sourceFolderObject = $sourceFileObject;
698  try {
699  $resultObject = $sourceFolderObject->copyTo($targetFolderObject, null, $conflictMode);
700  } catch (InsufficientUserPermissionsException $e) {
701  $this->writeLog(2, 1, 125, 'You are not allowed to copy directories', []);
702  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCopyDirectories');
703  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
704  $this->writeLog(2, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
705  $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
706  } catch (InsufficientFolderAccessPermissionsException $e) {
707  $this->writeLog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', [$targetFolderObject->getIdentifier()]);
708  $this->addMessageToFlashMessageQueue('FileUtility.YouDontHaveFullAccessToTheDestinationDirectory', [$targetFolderObject->getIdentifier()]);
709  } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
710  $this->writeLog(2, 1, 122, 'Cannot copy folder "%s" into target folder "%s", because the target folder is already within the folder to be copied!', [$sourceFolderObject->getName(), $targetFolderObject->getName()]);
711  $this->addMessageToFlashMessageQueue('FileUtility.CannotCopyFolderIntoTargetFolderBecauseTheTargetFolderIsAlreadyWithinTheFolderToBeCopied', [$sourceFolderObject->getName(), $targetFolderObject->getName()]);
712  } catch (ExistingTargetFolderException $e) {
713  $this->writeLog(2, 1, 123, 'Target "%s" already exists!', [$targetFolderObject->getIdentifier()]);
714  $this->addMessageToFlashMessageQueue('FileUtility.TargetAlreadyExists', [$targetFolderObject->getIdentifier()]);
715  } catch (\BadMethodCallException $e) {
716  $this->writeLog(3, 1, 129, 'The function to copy a folder between storages is not yet implemented', []);
717  $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToCopyAFolderBetweenStoragesIsNotYetImplemented');
718  } catch (\RuntimeException $e) {
719  $this->writeLog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
720  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotCopiedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
721  }
722  if ($resultObject) {
723  $this->writeLog(2, 0, 2, 'Directory "%s" copied to "%s"', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
724  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryCopiedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()], FlashMessage::OK);
725  }
726  }
727  return $resultObject;
728  }
729 
743  protected function func_move($cmds)
744  {
745  $sourceFileObject = $this->getFileObject($cmds['data']);
746  $targetFolderObject = $this->getFileObject($cmds['target']);
747  // Basic check
748  if (!$targetFolderObject instanceof Folder) {
749  $this->writeLog(3, 2, 100, 'Destination "%s" was not a directory', [$cmds['target']]);
750  $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
751  return false;
752  }
753  $alternativeName = (string)$cmds['altName'];
754  $resultObject = null;
755  // Moving the file
756  if ($sourceFileObject instanceof File) {
757  try {
758  if ($alternativeName !== '') {
759  // Don't allow overwriting existing files, but find a new name
760  $resultObject = $sourceFileObject->moveTo($targetFolderObject, $alternativeName, DuplicationBehavior::RENAME);
761  } else {
762  // Don't allow overwriting existing files
763  $resultObject = $sourceFileObject->moveTo($targetFolderObject, null, DuplicationBehavior::CANCEL);
764  }
765  $this->writeLog(3, 0, 1, 'File "%s" moved to "%s"', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()]);
766  $this->addMessageToFlashMessageQueue('FileUtility.FileMovedTo', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()], FlashMessage::OK);
768  $this->writeLog(3, 1, 114, 'You are not allowed to move files', []);
769  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToMoveFiles');
770  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
771  $this->writeLog(3, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
772  $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
773  } catch (IllegalFileExtensionException $e) {
774  $this->writeLog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
775  $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
776  } catch (ExistingTargetFileNameException $e) {
777  $this->writeLog(3, 1, 112, 'File "%s" already exists in folder "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
778  $this->addMessageToFlashMessageQueue('FileUtility.FileAlreadyExistsInFolder', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
779  } catch (\BadMethodCallException $e) {
780  $this->writeLog(3, 1, 126, 'The function to move a file between storages is not yet implemented', []);
781  $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToMoveAFileBetweenStoragesIsNotYetImplemented');
782  } catch (\RuntimeException $e) {
783  $this->writeLog(3, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
784  $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotCopiedTo', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
785  }
786  } else {
787  // Else means this is a Folder
788  $sourceFolderObject = $sourceFileObject;
789  try {
790  if ($alternativeName !== '') {
791  // Don't allow overwriting existing files, but find a new name
792  $resultObject = $sourceFolderObject->moveTo($targetFolderObject, $alternativeName, DuplicationBehavior::RENAME);
793  } else {
794  // Don't allow overwriting existing files
795  $resultObject = $sourceFolderObject->moveTo($targetFolderObject, null, DuplicationBehavior::RENAME);
796  }
797  $this->writeLog(3, 0, 2, 'Directory "%s" moved to "%s"', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
798  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryMovedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()], FlashMessage::OK);
800  $this->writeLog(3, 1, 125, 'You are not allowed to move directories', []);
801  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToMoveDirectories');
802  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
803  $this->writeLog(3, 1, 110, 'Could not access all necessary resources. Source file or destination maybe was not within your mountpoints? T="%s", D="%s"', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
804  $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
806  $this->writeLog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', [$targetFolderObject->getIdentifier()]);
807  $this->addMessageToFlashMessageQueue('FileUtility.YouDontHaveFullAccessToTheDestinationDirectory', [$targetFolderObject->getIdentifier()]);
808  } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
809  $this->writeLog(3, 1, 122, 'Cannot move folder "%s" into target folder "%s", because the target folder is already within the folder to be moved!', [$sourceFolderObject->getName(), $targetFolderObject->getName()]);
810  $this->addMessageToFlashMessageQueue('FileUtility.CannotMoveFolderIntoTargetFolderBecauseTheTargetFolderIsAlreadyWithinTheFolderToBeMoved', [$sourceFolderObject->getName(), $targetFolderObject->getName()]);
811  } catch (ExistingTargetFolderException $e) {
812  $this->writeLog(3, 1, 123, 'Target "%s" already exists!', [$targetFolderObject->getIdentifier()]);
813  $this->addMessageToFlashMessageQueue('FileUtility.TargetAlreadyExists', [$targetFolderObject->getIdentifier()]);
814  } catch (\BadMethodCallException $e) {
815  $this->writeLog(3, 1, 127, 'The function to move a folder between storages is not yet implemented', []);
816  $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToMoveAFolderBetweenStoragesIsNotYetImplemented', []);
817  } catch (\RuntimeException $e) {
818  $this->writeLog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
819  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotMovedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
820  }
821  }
822  return $resultObject;
823  }
824 
836  public function func_rename($cmds)
837  {
838  $sourceFileObject = $this->getFileObject($cmds['data']);
839  $sourceFile = $sourceFileObject->getName();
840  $targetFile = $cmds['target'];
841  $resultObject = null;
842  if ($sourceFileObject instanceof File) {
843  try {
844  // Try to rename the File
845  $resultObject = $sourceFileObject->rename($targetFile);
846  $this->writeLog(5, 0, 1, 'File renamed from "%s" to "%s"', [$sourceFile, $targetFile]);
847  $this->addMessageToFlashMessageQueue('FileUtility.FileRenamedFromTo', [$sourceFile, $targetFile], FlashMessage::OK);
849  $this->writeLog(5, 1, 102, 'You are not allowed to rename files!', []);
850  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToRenameFiles');
851  } catch (IllegalFileExtensionException $e) {
852  $this->writeLog(5, 1, 101, 'Extension of file name "%s" or "%s" was not allowed!', [$sourceFileObject->getName(), $targetFile]);
853  $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameOrWasNotAllowed', [$sourceFileObject->getName(), $targetFile]);
854  } catch (ExistingTargetFileNameException $e) {
855  $this->writeLog(5, 1, 120, 'Destination "%s" existed already!', [$targetFile]);
856  $this->addMessageToFlashMessageQueue('FileUtility.DestinationExistedAlready', [$targetFile]);
857  } catch (NotInMountPointException $e) {
858  $this->writeLog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', [$targetFile]);
859  $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFile]);
860  } catch (\RuntimeException $e) {
861  $this->writeLog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', [$sourceFileObject->getName(), $targetFile]);
862  $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotRenamed', [$sourceFileObject->getName(), $targetFile]);
863  }
864  } else {
865  // Else means this is a Folder
866  try {
867  // Try to rename the Folder
868  $resultObject = $sourceFileObject->rename($targetFile);
869  $this->writeLog(5, 0, 2, 'Directory renamed from "%s" to "%s"', [$sourceFile, $targetFile]);
870  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryRenamedFromTo', [$sourceFile, $targetFile], FlashMessage::OK);
872  $this->writeLog(5, 1, 111, 'You are not allowed to rename directories!', []);
873  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToRenameDirectories');
874  } catch (ExistingTargetFileNameException $e) {
875  $this->writeLog(5, 1, 120, 'Destination "%s" existed already!', [$targetFile]);
876  $this->addMessageToFlashMessageQueue('FileUtility.DestinationExistedAlready', [$targetFile]);
877  } catch (NotInMountPointException $e) {
878  $this->writeLog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', [$targetFile]);
879  $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFile]);
880  } catch (\RuntimeException $e) {
881  $this->writeLog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', [$sourceFileObject->getName(), $targetFile]);
882  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotRenamed', [$sourceFileObject->getName(), $targetFile]);
883  }
884  }
885  return $resultObject;
886  }
887 
898  public function func_newfolder($cmds)
899  {
900  $targetFolderObject = $this->getFileObject($cmds['target']);
901  if (!$targetFolderObject instanceof Folder) {
902  $this->writeLog(6, 2, 104, 'Destination "%s" was not a directory', [$cmds['target']]);
903  $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
904  return false;
905  }
906  $resultObject = null;
907  try {
908  $folderName = $cmds['data'];
909  $resultObject = $targetFolderObject->createFolder($folderName);
910  $this->writeLog(6, 0, 1, 'Directory "%s" created in "%s"', [$folderName, $targetFolderObject->getIdentifier()]);
911  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryCreatedIn', [$folderName, $targetFolderObject->getIdentifier()], FlashMessage::OK);
912  } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException $e) {
913  $this->writeLog(6, 1, 104, 'Invalid folder name "%s"!', [$folderName]);
914  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateDirectories', [$folderName]);
915  } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
916  $this->writeLog(6, 1, 103, 'You are not allowed to create directories!', []);
917  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateDirectories');
918  } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
919  $this->writeLog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', [$targetFolderObject->getIdentifier()]);
920  $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFolderObject->getIdentifier()]);
921  } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
922  $this->writeLog(6, 1, 101, 'File or directory "%s" existed already!', [$folderName]);
923  $this->addMessageToFlashMessageQueue('FileUtility.FileOrDirectoryExistedAlready', [$folderName]);
924  } catch (\RuntimeException $e) {
925  $this->writeLog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', [$folderName, $targetFolderObject->getIdentifier()]);
926  $this->addMessageToFlashMessageQueue('FileUtility.DirectoryNotCreated', [$folderName, $targetFolderObject->getIdentifier()]);
927  }
928  return $resultObject;
929  }
930 
940  public function func_newfile($cmds)
941  {
942  $targetFolderObject = $this->getFileObject($cmds['target']);
943  if (!$targetFolderObject instanceof Folder) {
944  $this->writeLog(8, 2, 104, 'Destination "%s" was not a directory', [$cmds['target']]);
945  $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
946  return false;
947  }
948  $resultObject = null;
949  $fileName = $cmds['data'];
950  try {
951  $resultObject = $targetFolderObject->createFile($fileName);
952  $this->writeLog(8, 0, 1, 'File created: "%s"', [$fileName]);
953  $this->addMessageToFlashMessageQueue('FileUtility.FileCreated', [$fileName], FlashMessage::OK);
954  } catch (IllegalFileExtensionException $e) {
955  $this->writeLog(8, 1, 106, 'Extension of file "%s" was not allowed!', [$fileName]);
956  $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileWasNotAllowed', [$fileName]);
958  $this->writeLog(8, 1, 103, 'You are not allowed to create files!', []);
959  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateFiles');
960  } catch (NotInMountPointException $e) {
961  $this->writeLog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', [$targetFolderObject->getIdentifier()]);
962  $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFolderObject->getIdentifier()]);
963  } catch (ExistingTargetFileNameException $e) {
964  $this->writeLog(8, 1, 101, 'File existed already in "%s"!', [$targetFolderObject->getIdentifier()]);
965  $this->addMessageToFlashMessageQueue('FileUtility.FileExistedAlreadyIn', [$targetFolderObject->getIdentifier()]);
966  } catch (InvalidFileNameException $e) {
967  $this->writeLog(8, 1, 106, 'File name "%s" was not allowed!', [$fileName]);
968  $this->addMessageToFlashMessageQueue('FileUtility.FileNameWasNotAllowed', [$fileName]);
969  } catch (\RuntimeException $e) {
970  $this->writeLog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', [$fileName, $targetFolderObject->getIdentifier()]);
971  $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotCreated', [$fileName, $targetFolderObject->getIdentifier()]);
972  }
973  return $resultObject;
974  }
975 
982  public function func_edit($cmds)
983  {
984  // Example indentifier for $cmds['target'] => "4:mypath/tomyfolder/myfile.jpg"
985  // for backwards compatibility: the combined file identifier was the path+filename
986  $fileIdentifier = $cmds['target'];
987  $fileObject = $this->getFileObject($fileIdentifier);
988  // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
989  $content = $cmds['data'];
990  if (!$fileObject instanceof File) {
991  $this->writeLog(9, 2, 123, 'Target "%s" was not a file!', [$fileIdentifier]);
992  $this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotAFile', [$fileIdentifier]);
993  return false;
994  }
995  $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
996  if (!GeneralUtility::inList($extList, $fileObject->getExtension())) {
997  $this->writeLog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', [$fileObject->getExtension(), $extList]);
998  $this->addMessageToFlashMessageQueue('FileUtility.FileExtensionIsNotATextfileFormat', [$fileObject->getExtension(), $extList]);
999  return false;
1000  }
1001  try {
1002  $fileObject->setContents($content);
1003  clearstatcache();
1004  $this->writeLog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', [$fileObject->getIdentifier(), $fileObject->getSize(), md5($content)]);
1005  $this->addMessageToFlashMessageQueue('FileUtility.FileSavedToBytesMd5', [$fileObject->getIdentifier(), $fileObject->getSize(), md5($content)], FlashMessage::OK);
1006  return true;
1008  $this->writeLog(9, 1, 104, 'You are not allowed to edit files!', []);
1009  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToEditFiles');
1010  return false;
1012  $this->writeLog(9, 1, 100, 'File "%s" was not saved! Write-permission problem?', [$fileObject->getIdentifier()]);
1013  $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotSaved', [$fileObject->getIdentifier()]);
1014  return false;
1015  } catch (IllegalFileExtensionException $e) {
1016  $this->writeLog(9, 1, 100, 'File "%s" was not saved! File extension rejected!', [$fileObject->getIdentifier()]);
1017  $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotSaved', [$fileObject->getIdentifier()]);
1018  return false;
1019  }
1020  }
1021 
1052  public function func_upload($cmds)
1053  {
1054  $uploadPosition = $cmds['data'];
1055  $uploadedFileData = $_FILES['upload_' . $uploadPosition];
1056  if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
1057  $this->writeLog(1, 2, 108, 'No file was uploaded!', []);
1058  $this->addMessageToFlashMessageQueue('FileUtility.NoFileWasUploaded');
1059  return false;
1060  }
1061  // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
1062  $targetFolderObject = $this->getFileObject($cmds['target']);
1063  // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
1064  if (!is_array($uploadedFileData['name'])) {
1065  $uploadedFileData = [
1066  'name' => [$uploadedFileData['name']],
1067  'type' => [$uploadedFileData['type']],
1068  'tmp_name' => [$uploadedFileData['tmp_name']],
1069  'size' => [$uploadedFileData['size']]
1070  ];
1071  }
1072  $resultObjects = [];
1073  $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
1074  // Loop through all uploaded files
1075  for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
1076  $fileInfo = [
1077  'name' => $uploadedFileData['name'][$i],
1078  'type' => $uploadedFileData['type'][$i],
1079  'tmp_name' => $uploadedFileData['tmp_name'][$i],
1080  'size' => $uploadedFileData['size'][$i]
1081  ];
1082  try {
1084  $fileObject = $targetFolderObject->addUploadedFile($fileInfo, (string)$this->existingFilesConflictMode);
1085  $fileObject = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
1086  if ($this->existingFilesConflictMode->equals(DuplicationBehavior::REPLACE)) {
1087  $this->getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
1088  }
1089  $resultObjects[] = $fileObject;
1090  $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
1091  $this->writeLog(1, 0, 1, 'Uploading file "%s" to "%s"', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
1092  $this->addMessageToFlashMessageQueue('FileUtility.UploadingFileTo', [$fileInfo['name'], $targetFolderObject->getIdentifier()], FlashMessage::OK);
1093  } catch (InsufficientFileWritePermissionsException $e) {
1094  $this->writeLog(1, 1, 107, 'You are not allowed to override "%s"!', [$fileInfo['name']]);
1095  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToOverride', [$fileInfo['name']]);
1096  } catch (UploadException $e) {
1097  $this->writeLog(1, 2, 106, 'The upload has failed, no uploaded file found!', []);
1098  $this->addMessageToFlashMessageQueue('FileUtility.TheUploadHasFailedNoUploadedFileFound');
1099  } catch (InsufficientUserPermissionsException $e) {
1100  $this->writeLog(1, 1, 105, 'You are not allowed to upload files!', []);
1101  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToUploadFiles');
1102  } catch (UploadSizeException $e) {
1103  $this->writeLog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', [$fileInfo['name']]);
1104  $this->addMessageToFlashMessageQueue('FileUtility.TheUploadedFileExceedsTheSize-limit', [$fileInfo['name']]);
1105  } catch (InsufficientFolderWritePermissionsException $e) {
1106  $this->writeLog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', [$targetFolderObject->getIdentifier()]);
1107  $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFolderObject->getIdentifier()]);
1108  } catch (IllegalFileExtensionException $e) {
1109  $this->writeLog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
1110  $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
1111  } catch (ExistingTargetFileNameException $e) {
1112  $this->writeLog(1, 1, 101, 'No unique filename available in "%s"!', [$targetFolderObject->getIdentifier()]);
1113  $this->addMessageToFlashMessageQueue('FileUtility.NoUniqueFilenameAvailableIn', [$targetFolderObject->getIdentifier()]);
1114  } catch (\RuntimeException $e) {
1115  $this->writeLog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', [$targetFolderObject->getIdentifier()]);
1116  $this->addMessageToFlashMessageQueue('FileUtility.UploadedFileCouldNotBeMoved', [$targetFolderObject->getIdentifier()]);
1117  }
1118  }
1119 
1120  return $resultObjects;
1121  }
1122 
1133  protected function replaceFile(array $cmdArr)
1134  {
1135  $uploadPosition = $cmdArr['data'];
1136  $fileInfo = $_FILES['replace_' . $uploadPosition];
1137  if (empty($fileInfo['name'])) {
1138  $this->writeLog(1, 2, 108, 'No file was uploaded for replacing!', []);
1139  $this->addMessageToFlashMessageQueue('FileUtility.NoFileWasUploadedForReplacing');
1140  return false;
1141  }
1142 
1143  $keepFileName = ($cmdArr['keepFilename'] == 1) ? true : false;
1144  $resultObjects = [];
1145 
1146  try {
1147  $fileObjectToReplace = $this->getFileObject($cmdArr['uid']);
1148  $folder = $fileObjectToReplace->getParentFolder();
1149  $resourceStorage = $fileObjectToReplace->getStorage();
1150 
1151  $fileObject = $resourceStorage->addUploadedFile($fileInfo, $folder, $fileObjectToReplace->getName(), DuplicationBehavior::REPLACE);
1152 
1153  // Check if there is a file that is going to be uploaded that has a different name as the replacing one
1154  // but exists in that folder as well.
1155  // rename to another name, but check if the name is already given
1156  if ($keepFileName === false) {
1157  // if a file with the same name already exists, we need to change it to _01 etc.
1158  // if the file does not exist, we can do a simple rename
1159  $resourceStorage->moveFile($fileObject, $folder, $fileInfo['name'], DuplicationBehavior::RENAME);
1160  }
1161 
1162  $resultObjects[] = $fileObject;
1163  $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
1164 
1165  $this->writeLog(1, 0, 1, 'Replacing file "%s" to "%s"', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()]);
1166  $this->addMessageToFlashMessageQueue('FileUtility.ReplacingFileTo', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()], FlashMessage::OK);
1168  $this->writeLog(1, 1, 107, 'You are not allowed to override "%s"!', [$fileInfo['name']]);
1169  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToOverride', [$fileInfo['name']]);
1170  } catch (UploadException $e) {
1171  $this->writeLog(1, 2, 106, 'The upload has failed, no uploaded file found!', []);
1172  $this->addMessageToFlashMessageQueue('FileUtility.TheUploadHasFailedNoUploadedFileFound');
1174  $this->writeLog(1, 1, 105, 'You are not allowed to upload files!', []);
1175  $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToUploadFiles');
1176  } catch (UploadSizeException $e) {
1177  $this->writeLog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', [$fileInfo['name']]);
1178  $this->addMessageToFlashMessageQueue('FileUtility.TheUploadedFileExceedsTheSize-limit', [$fileInfo['name']]);
1180  $this->writeLog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', [$fileObjectToReplace->getIdentifier()]);
1181  $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$fileObjectToReplace->getIdentifier()]);
1182  } catch (IllegalFileExtensionException $e) {
1183  $this->writeLog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()]);
1184  $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()]);
1185  } catch (ExistingTargetFileNameException $e) {
1186  $this->writeLog(1, 1, 101, 'No unique filename available in "%s"!', [$fileObjectToReplace->getIdentifier()]);
1187  $this->addMessageToFlashMessageQueue('FileUtility.NoUniqueFilenameAvailableIn', [$fileObjectToReplace->getIdentifier()]);
1188  } catch (\RuntimeException $e) {
1189  throw $e;
1190  }
1191  return $resultObjects;
1192  }
1193 
1200  protected function addFlashMessage(FlashMessage $flashMessage)
1201  {
1203  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1204 
1206  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1207  $defaultFlashMessageQueue->enqueue($flashMessage);
1208  }
1209 
1216  protected function getIndexer(ResourceStorage $storage)
1217  {
1218  return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\Indexer::class, $storage);
1219  }
1220 
1224  protected function getBackendUser()
1225  {
1226  return $GLOBALS['BE_USER'];
1227  }
1228 
1234  protected function getLanguageService()
1235  {
1236  return $GLOBALS['LANG'];
1237  }
1238 }
writeLog($action, $error, $details_nr, $details, $data)
addMessageToFlashMessageQueue($localizationKey, array $replaceMarkers=[], $severity=FlashMessage::ERROR)
static getRecord($table, $uid, $fields= '*', $where= '', $useDeleteClause=true)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static getConstants($include_default=false)