‪TYPO3CMS  9.5
ExtendedFileUtility.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 
45 
63 {
71 
79  public ‪$actionPerms = [
80  // File permissions
81  'addFile' => false,
82  'readFile' => false,
83  'writeFile' => false,
84  'copyFile' => false,
85  'moveFile' => false,
86  'renameFile' => false,
87  'deleteFile' => false,
88  // Folder permissions
89  'addFolder' => false,
90  'readFolder' => false,
91  'writeFolder' => false,
92  'copyFolder' => false,
93  'moveFolder' => false,
94  'renameFolder' => false,
95  'deleteFolder' => false,
96  'recursivedeleteFolder' => false
97  ];
98 
104  public ‪$internalUploadMap = [];
105 
111  protected ‪$errorMessages = [];
112 
118  protected ‪$flashMessages = [];
119 
123  protected ‪$fileCmdMap;
124 
130  protected ‪$fileFactory;
131 
137  public function ‪getExistingFilesConflictMode()
138  {
140  }
141 
149  {
150  try {
151  $this->existingFilesConflictMode = ‪DuplicationBehavior::cast(‪$existingFilesConflictMode);
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 
169  public function ‪start($fileCmds)
170  {
171  // Initialize Object Factory
172  $this->fileFactory = ‪ResourceFactory::getInstance();
173  // Initializing file processing commands:
174  $this->fileCmdMap = $fileCmds;
175  }
176 
183  public function ‪setActionPermissions(array $permissions = [])
184  {
185  if (empty($permissions)) {
186  $permissions = $this->‪getBackendUser()->getFilePermissions();
187  }
188  $this->actionPerms = $permissions;
189  }
190 
197  public function ‪processData()
198  {
199  $result = [];
200  if (is_array($this->fileCmdMap)) {
201  // Check if there were uploads expected, but no one made
202  if ($this->fileCmdMap['upload']) {
203  $uploads = $this->fileCmdMap['upload'];
204  foreach ($uploads as $upload) {
205  if (empty($_FILES['upload_' . $upload['data']]['name'])
206  || (
207  is_array($_FILES['upload_' . $upload['data']]['name'])
208  && empty($_FILES['upload_' . $upload['data']]['name'][0])
209  )
210  ) {
211  unset($this->fileCmdMap['upload'][$upload['data']]);
212  }
213  }
214  if (empty($this->fileCmdMap['upload'])) {
215  $this->‪writeLog(1, 1, 108, 'No file was uploaded!', []);
216  $this->‪addMessageToFlashMessageQueue('FileUtility.NoFileWasUploaded');
217  }
218  }
219 
220  // Check if there were new folder names expected, but non given
221  if ($this->fileCmdMap['newfolder']) {
222  foreach ($this->fileCmdMap['newfolder'] as $key => $cmdArr) {
223  if (empty($cmdArr['data'])) {
224  unset($this->fileCmdMap['newfolder'][$key]);
225  }
226  }
227  if (empty($this->fileCmdMap['newfolder'])) {
228  $this->‪writeLog(6, 1, 108, 'No name for new folder given!', []);
229  $this->‪addMessageToFlashMessageQueue('FileUtility.NoNameForNewFolderGiven');
230  }
231  }
232 
233  // Traverse each set of actions
234  foreach ($this->fileCmdMap as $action => $actionData) {
235  // Traverse all action data. More than one file might be affected at the same time.
236  if (is_array($actionData)) {
237  $result[$action] = [];
238  foreach ($actionData as $cmdArr) {
239  // Clear file stats
240  clearstatcache();
241  // Branch out based on command:
242  switch ($action) {
243  case 'delete':
244  $result[$action][] = $this->‪func_delete($cmdArr);
245  break;
246  case 'copy':
247  $result[$action][] = $this->‪func_copy($cmdArr);
248  break;
249  case 'move':
250  $result[$action][] = $this->‪func_move($cmdArr);
251  break;
252  case 'rename':
253  $result[$action][] = $this->‪func_rename($cmdArr);
254  break;
255  case 'newfolder':
256  $result[$action][] = $this->‪func_newfolder($cmdArr);
257  break;
258  case 'newfile':
259  $result[$action][] = $this->‪func_newfile($cmdArr);
260  break;
261  case 'editfile':
262  $result[$action][] = $this->‪func_edit($cmdArr);
263  break;
264  case 'upload':
265  $result[$action][] = $this->‪func_upload($cmdArr);
266  break;
267  case 'replace':
268  $result[$action][] = $this->‪replaceFile($cmdArr);
269  break;
270  }
271  // Hook for post-processing the action
272  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'] ?? [] as $className) {
273  $hookObject = GeneralUtility::makeInstance($className);
274  if (!$hookObject instanceof ‪ExtendedFileUtilityProcessDataHookInterface) {
275  throw new \UnexpectedValueException($className . ' must implement interface ' . ExtendedFileUtilityProcessDataHookInterface::class, 1279719168);
276  }
277  $hookObject->processData_postProcessAction($action, $cmdArr, $result[$action], $this);
278  }
279  }
280  }
281  }
282  }
283  return $result;
284  }
285 
291  public function ‪getErrorMessages()
292  {
294  }
295 
303  public function ‪writeLog($action, $error, $details_nr, $details, $data)
304  {
305  // Type value for tce_file.php
306  $type = 2;
307  if (is_object($this->‪getBackendUser())) {
308  $this->‪getBackendUser()->writelog($type, $action, $error, $details_nr, $details, $data);
309  }
310  if ($error > 0) {
311  $this->errorMessages[] = vsprintf($details, $data);
312  }
313  }
314 
323  protected function ‪addMessageToFlashMessageQueue($localizationKey, array $replaceMarkers = [], $severity = ‪FlashMessage::ERROR)
324  {
325  if (TYPO3_MODE !== 'BE') {
326  return;
327  }
328  $label = $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/fileMessages.xlf:' . $localizationKey);
329  $message = vsprintf($label, $replaceMarkers);
330  $flashMessage = GeneralUtility::makeInstance(
331  FlashMessage::class,
332  $message,
333  '',
334  $severity,
335  true
336  );
337  $this->‪addFlashMessage($flashMessage);
338  }
339 
340  /*************************************
341  *
342  * File operation functions
343  *
344  **************************************/
351  public function ‪func_delete(array $cmds)
352  {
353  $result = false;
354  // Example identifier for $cmds['data'] => "4:mypath/tomyfolder/myfile.jpg"
355  // for backwards compatibility: the combined file identifier was the path+filename
356  try {
357  $fileObject = $this->‪getFileObject($cmds['data']);
358  } catch (‪ResourceDoesNotExistException $e) {
359  $flashMessage = GeneralUtility::makeInstance(
360  FlashMessage::class,
361  sprintf(
362  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.fileNotFound'),
363  $cmds['data']
364  ),
365  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.fileNotFound'),
367  true
368  );
369  $this->‪addFlashMessage($flashMessage);
370 
371  return false;
372  }
373  // checks to delete the file
374  if ($fileObject instanceof ‪File) {
375  // check if the file still has references
376  // Exclude sys_file_metadata records as these are no use references
377  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
378  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
379  $refIndexRecords = $queryBuilder
380  ->select('tablename', 'recuid', 'ref_uid')
381  ->from('sys_refindex')
382  ->where(
383  $queryBuilder->expr()->eq(
384  'ref_table',
385  $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
386  ),
387  $queryBuilder->expr()->eq(
388  'ref_uid',
389  $queryBuilder->createNamedParameter($fileObject->getUid(), \PDO::PARAM_INT)
390  ),
391  $queryBuilder->expr()->neq(
392  'tablename',
393  $queryBuilder->createNamedParameter('sys_file_metadata', \PDO::PARAM_STR)
394  ),
395  $queryBuilder->expr()->eq(
396  'deleted',
397  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
398  )
399  )
400  ->execute()
401  ->fetchAll();
402  $deleteFile = true;
403  if (!empty($refIndexRecords)) {
404  $shortcutContent = [];
405  $brokenReferences = [];
406 
407  foreach ($refIndexRecords as $fileReferenceRow) {
408  if ($fileReferenceRow['tablename'] === 'sys_file_reference') {
409  $row = $this->‪transformFileReferenceToRecordReference($fileReferenceRow);
410  $shortcutRecord = ‪BackendUtility::getRecord($row['tablename'], $row['recuid']);
411 
412  if ($shortcutRecord) {
413  $shortcutContent[] = '[record:' . $row['tablename'] . ':' . $row['recuid'] . ']';
414  } else {
415  $brokenReferences[] = $fileReferenceRow['ref_uid'];
416  }
417  } else {
418  $shortcutContent[] = '[record:' . $fileReferenceRow['tablename'] . ':' . $fileReferenceRow['recuid'] . ']';
419  }
420  }
421  if (!empty($brokenReferences)) {
422  // render a message that the file has broken references
423  $flashMessage = GeneralUtility::makeInstance(
424  FlashMessage::class,
425  sprintf($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.fileHasBrokenReferences'), count($brokenReferences)),
426  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.fileHasBrokenReferences'),
428  true
429  );
430  $this->‪addFlashMessage($flashMessage);
431  }
432  if (!empty($shortcutContent)) {
433  // render a message that the file could not be deleted
434  $flashMessage = GeneralUtility::makeInstance(
435  FlashMessage::class,
436  sprintf($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.fileNotDeletedHasReferences'), $fileObject->getName()) . ' ' . implode(', ', $shortcutContent),
437  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.fileNotDeletedHasReferences'),
439  true
440  );
441  $this->‪addFlashMessage($flashMessage);
442  $deleteFile = false;
443  }
444  }
445 
446  if ($deleteFile) {
447  try {
448  $result = $fileObject->delete();
449 
450  // show the user that the file was deleted
451  $flashMessage = GeneralUtility::makeInstance(
452  FlashMessage::class,
453  sprintf($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.fileDeleted'), $fileObject->getName()),
454  $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.fileDeleted'),
456  true
457  );
458  $this->‪addFlashMessage($flashMessage);
459  // Log success
460  $this->‪writeLog(4, 0, 1, 'File "%s" deleted', [$fileObject->getIdentifier()]);
462  $this->‪writeLog(4, 1, 112, 'You are not allowed to access the file', [$fileObject->getIdentifier()]);
463  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToAccessTheFile', [$fileObject->getIdentifier()]);
464  } catch (‪NotInMountPointException $e) {
465  $this->‪writeLog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', [$fileObject->getIdentifier()]);
466  $this->‪addMessageToFlashMessageQueue('FileUtility.TargetWasNotWithinYourMountpoints', [$fileObject->getIdentifier()]);
467  } catch (\RuntimeException $e) {
468  $this->‪writeLog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', [$fileObject->getIdentifier()]);
469  $this->‪addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteFile', [$fileObject->getIdentifier()]);
470  }
471  }
472  } else {
474  if (!$this->‪folderHasFilesInUse($fileObject)) {
475  try {
476  $result = $fileObject->delete(true);
477  if ($result) {
478  // notify the user that the folder was deleted
480  $flashMessage = GeneralUtility::makeInstance(
481  FlashMessage::class,
482  sprintf($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.folderDeleted'), $fileObject->getName()),
483  $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.folderDeleted'),
485  true
486  );
487  $this->‪addFlashMessage($flashMessage);
488  // Log success
489  $this->‪writeLog(4, 0, 3, 'Directory "%s" deleted', [$fileObject->getIdentifier()]);
490  }
492  $this->‪writeLog(4, 1, 120, 'Could not delete directory! Is directory "%s" empty? (You are not allowed to delete directories recursively).', [$fileObject->getIdentifier()]);
493  $this->‪addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteDirectory', [$fileObject->getIdentifier()]);
495  $this->‪writeLog(4, 1, 123, 'You are not allowed to access the directory', [$fileObject->getIdentifier()]);
496  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToAccessTheDirectory', [$fileObject->getIdentifier()]);
497  } catch (‪NotInMountPointException $e) {
498  $this->‪writeLog(4, 1, 121, 'Target was not within your mountpoints! T="%s"', [$fileObject->getIdentifier()]);
499  $this->‪addMessageToFlashMessageQueue('FileUtility.TargetWasNotWithinYourMountpoints', [$fileObject->getIdentifier()]);
500  } catch (\‪TYPO3\CMS\Core\Resource\‪Exception\‪FileOperationErrorException $e) {
501  $this->‪writeLog(4, 1, 120, 'Could not delete directory "%s"! Write-permission problem?', [$fileObject->getIdentifier()]);
502  $this->‪addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteDirectory', [$fileObject->getIdentifier()]);
503  }
504  }
505  }
506 
507  return $result;
508  }
509 
518  public function ‪folderHasFilesInUse(‪Folder $folder)
519  {
521  if (empty($files)) {
522  return false;
523  }
524 
526  $fileUids = [];
527  foreach ($files as $file) {
528  $fileUids[] = $file->‪getUid();
529  }
530 
531  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
532  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
533  $numberOfReferences = $queryBuilder
534  ->count('hash')
535  ->from('sys_refindex')
536  ->where(
537  $queryBuilder->expr()->eq(
538  'ref_table',
539  $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
540  ),
541  $queryBuilder->expr()->in(
542  'ref_uid',
543  $queryBuilder->createNamedParameter($fileUids, Connection::PARAM_INT_ARRAY)
544  ),
545  $queryBuilder->expr()->neq(
546  'tablename',
547  $queryBuilder->createNamedParameter('sys_file_metadata', \PDO::PARAM_STR)
548  ),
549  $queryBuilder->expr()->eq(
550  'deleted',
551  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
552  )
553  )->execute()->fetchColumn(0);
554 
555  $hasReferences = $numberOfReferences > 0;
556  if ($hasReferences) {
558  $flashMessage = GeneralUtility::makeInstance(
559  FlashMessage::class,
560  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences'),
561  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences'),
563  true
564  );
565  $this->‪addFlashMessage($flashMessage);
566  }
567 
568  return $hasReferences;
569  }
570 
578  protected function ‪transformFileReferenceToRecordReference(array $referenceRecord)
579  {
580  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
581  $queryBuilder->getRestrictions()->removeAll();
582  $fileReference = $queryBuilder
583  ->select('uid_foreign', 'tablenames', 'fieldname', 'sorting_foreign')
584  ->from('sys_file_reference')
585  ->where(
586  $queryBuilder->expr()->eq(
587  'uid',
588  $queryBuilder->createNamedParameter($referenceRecord['recuid'], \PDO::PARAM_INT)
589  )
590  )
591  ->execute()
592  ->fetch();
593 
594  return [
595  'recuid' => $fileReference['uid_foreign'],
596  'tablename' => $fileReference['tablenames'],
597  'field' => $fileReference['fieldname'],
598  'flexpointer' => '',
599  'softref_key' => '',
600  'sorting' => $fileReference['sorting_foreign']
601  ];
602  }
603 
612  protected function ‪getFileObject($identifier)
613  {
614  $object = $this->fileFactory->retrieveFileOrFolderObject($identifier);
615  if (!is_object($object)) {
616  throw new \TYPO3\CMS\Core\Resource\Exception\InvalidFileException('The item ' . $identifier . ' was not a file or directory!!', 1320122453);
617  }
618  if ($object->getStorage()->getUid() === 0) {
619  throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889830);
620  }
621  return $object;
622  }
623 
637  protected function ‪func_copy($cmds)
638  {
639  $sourceFileObject = $this->‪getFileObject($cmds['data']);
641  $targetFolderObject = $this->‪getFileObject($cmds['target']);
642  // Basic check
643  if (!$targetFolderObject instanceof Folder) {
644  $this->‪writeLog(2, 2, 100, 'Destination "%s" was not a directory', [$cmds['target']]);
645  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
646  return false;
647  }
648  // If this is TRUE, we append _XX to the file name if
649  $appendSuffixOnConflict = (string)$cmds['altName'];
650  $resultObject = null;
651  $conflictMode = $appendSuffixOnConflict !== '' ? ‪DuplicationBehavior::RENAME : ‪DuplicationBehavior::CANCEL;
652  // Copying the file
653  if ($sourceFileObject instanceof File) {
654  try {
655  $resultObject = $sourceFileObject->copyTo($targetFolderObject, null, $conflictMode);
656  } catch (InsufficientUserPermissionsException $e) {
657  $this->‪writeLog(2, 1, 114, 'You are not allowed to copy files', []);
658  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCopyFiles');
659  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
660  $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()]);
661  $this->‪addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
662  } catch (IllegalFileExtensionException $e) {
663  $this->‪writeLog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
664  $this->‪addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
665  } catch (ExistingTargetFileNameException $e) {
666  $this->‪writeLog(2, 1, 112, 'File "%s" already exists in folder "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
667  $this->‪addMessageToFlashMessageQueue('FileUtility.FileAlreadyExistsInFolder', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
668  } catch (NotImplementedMethodException $e) {
669  $this->‪writeLog(3, 1, 128, 'The function to copy a file between storages is not yet implemented', []);
670  $this->‪addMessageToFlashMessageQueue('FileUtility.TheFunctionToCopyAFileBetweenStoragesIsNotYetImplemented');
671  } catch (\RuntimeException $e) {
672  $this->‪writeLog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
673  $this->‪addMessageToFlashMessageQueue('FileUtility.FileWasNotCopiedTo', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
674  }
675  if ($resultObject) {
676  $this->‪writeLog(2, 0, 1, 'File "%s" copied to "%s"', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()]);
677  $this->‪addMessageToFlashMessageQueue('FileUtility.FileCopiedTo', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()], ‪FlashMessage::OK);
678  }
679  } else {
680  // Else means this is a Folder
681  $sourceFolderObject = $sourceFileObject;
682  try {
683  $resultObject = $sourceFolderObject->copyTo($targetFolderObject, null, $conflictMode);
684  } catch (InsufficientUserPermissionsException $e) {
685  $this->‪writeLog(2, 1, 125, 'You are not allowed to copy directories', []);
686  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCopyDirectories');
687  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
688  $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()]);
689  $this->‪addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
690  } catch (InsufficientFolderAccessPermissionsException $e) {
691  $this->‪writeLog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', [$targetFolderObject->getIdentifier()]);
692  $this->‪addMessageToFlashMessageQueue('FileUtility.YouDontHaveFullAccessToTheDestinationDirectory', [$targetFolderObject->getIdentifier()]);
693  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
694  $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()]);
695  $this->‪addMessageToFlashMessageQueue('FileUtility.CannotCopyFolderIntoTargetFolderBecauseTheTargetFolderIsAlreadyWithinTheFolderToBeCopied', [$sourceFolderObject->getName(), $targetFolderObject->getName()]);
696  } catch (ExistingTargetFolderException $e) {
697  $this->‪writeLog(2, 1, 123, 'Target "%s" already exists!', [$targetFolderObject->getIdentifier()]);
698  $this->‪addMessageToFlashMessageQueue('FileUtility.TargetAlreadyExists', [$targetFolderObject->getIdentifier()]);
699  } catch (NotImplementedMethodException $e) {
700  $this->‪writeLog(3, 1, 129, 'The function to copy a folder between storages is not yet implemented', []);
701  $this->‪addMessageToFlashMessageQueue('FileUtility.TheFunctionToCopyAFolderBetweenStoragesIsNotYetImplemented');
702  } catch (\RuntimeException $e) {
703  $this->‪writeLog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
704  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotCopiedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
705  }
706  if ($resultObject) {
707  $this->‪writeLog(2, 0, 2, 'Directory "%s" copied to "%s"', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
708  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryCopiedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()], ‪FlashMessage::OK);
709  }
710  }
711  return $resultObject;
712  }
713 
727  protected function ‪func_move($cmds)
728  {
729  $sourceFileObject = $this->‪getFileObject($cmds['data']);
730  $targetFolderObject = $this->‪getFileObject($cmds['target']);
731  // Basic check
732  if (!$targetFolderObject instanceof Folder) {
733  $this->‪writeLog(3, 2, 100, 'Destination "%s" was not a directory', [$cmds['target']]);
734  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
735  return false;
736  }
737  $alternativeName = (string)$cmds['altName'];
738  $resultObject = null;
739  // Moving the file
740  if ($sourceFileObject instanceof File) {
741  try {
742  if ($alternativeName !== '') {
743  // Don't allow overwriting existing files, but find a new name
744  $resultObject = $sourceFileObject->moveTo($targetFolderObject, $alternativeName, ‪DuplicationBehavior::RENAME);
745  } else {
746  // Don't allow overwriting existing files
747  $resultObject = $sourceFileObject->moveTo($targetFolderObject, null, ‪DuplicationBehavior::CANCEL);
748  }
749  $this->‪writeLog(3, 0, 1, 'File "%s" moved to "%s"', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()]);
750  $this->‪addMessageToFlashMessageQueue('FileUtility.FileMovedTo', [$sourceFileObject->getIdentifier(), $resultObject->getIdentifier()], ‪FlashMessage::OK);
751  } catch (InsufficientUserPermissionsException $e) {
752  $this->‪writeLog(3, 1, 114, 'You are not allowed to move files', []);
753  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToMoveFiles');
754  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
755  $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()]);
756  $this->‪addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
757  } catch (IllegalFileExtensionException $e) {
758  $this->‪writeLog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
759  $this->‪addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
760  } catch (ExistingTargetFileNameException $e) {
761  $this->‪writeLog(3, 1, 112, 'File "%s" already exists in folder "%s"!', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
762  $this->‪addMessageToFlashMessageQueue('FileUtility.FileAlreadyExistsInFolder', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
763  } catch (NotImplementedMethodException $e) {
764  $this->‪writeLog(3, 1, 126, 'The function to move a file between storages is not yet implemented', []);
765  $this->‪addMessageToFlashMessageQueue('FileUtility.TheFunctionToMoveAFileBetweenStoragesIsNotYetImplemented');
766  } catch (\RuntimeException $e) {
767  $this->‪writeLog(3, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
768  $this->‪addMessageToFlashMessageQueue('FileUtility.FileWasNotCopiedTo', [$sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
769  }
770  } else {
771  // Else means this is a Folder
772  $sourceFolderObject = $sourceFileObject;
773  try {
774  if ($alternativeName !== '') {
775  // Don't allow overwriting existing files, but find a new name
776  $resultObject = $sourceFolderObject->moveTo($targetFolderObject, $alternativeName, ‪DuplicationBehavior::RENAME);
777  } else {
778  // Don't allow overwriting existing files
779  $resultObject = $sourceFolderObject->moveTo($targetFolderObject, null, ‪DuplicationBehavior::RENAME);
780  }
781  $this->‪writeLog(3, 0, 2, 'Directory "%s" moved to "%s"', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
782  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryMovedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()], ‪FlashMessage::OK);
783  } catch (InsufficientUserPermissionsException $e) {
784  $this->‪writeLog(3, 1, 125, 'You are not allowed to move directories', []);
785  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToMoveDirectories');
786  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
787  $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()]);
788  $this->‪addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
789  } catch (InsufficientFolderAccessPermissionsException $e) {
790  $this->‪writeLog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', [$targetFolderObject->getIdentifier()]);
791  $this->‪addMessageToFlashMessageQueue('FileUtility.YouDontHaveFullAccessToTheDestinationDirectory', [$targetFolderObject->getIdentifier()]);
792  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
793  $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()]);
794  $this->‪addMessageToFlashMessageQueue('FileUtility.CannotMoveFolderIntoTargetFolderBecauseTheTargetFolderIsAlreadyWithinTheFolderToBeMoved', [$sourceFolderObject->getName(), $targetFolderObject->getName()]);
795  } catch (ExistingTargetFolderException $e) {
796  $this->‪writeLog(3, 1, 123, 'Target "%s" already exists!', [$targetFolderObject->getIdentifier()]);
797  $this->‪addMessageToFlashMessageQueue('FileUtility.TargetAlreadyExists', [$targetFolderObject->getIdentifier()]);
798  } catch (NotImplementedMethodException $e) {
799  $this->‪writeLog(3, 1, 127, 'The function to move a folder between storages is not yet implemented', []);
800  $this->‪addMessageToFlashMessageQueue('FileUtility.TheFunctionToMoveAFolderBetweenStoragesIsNotYetImplemented', []);
801  } catch (\RuntimeException $e) {
802  $this->‪writeLog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
803  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotMovedTo', [$sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()]);
804  }
805  }
806  return $resultObject;
807  }
808 
820  public function ‪func_rename($cmds)
821  {
822  $sourceFileObject = $this->‪getFileObject($cmds['data']);
823  $sourceFile = $sourceFileObject->getName();
824  $targetFile = $cmds['target'];
825  $resultObject = null;
826  if ($sourceFileObject instanceof File) {
827  try {
828  // Try to rename the File
829  $resultObject = $sourceFileObject->rename($targetFile, $this->existingFilesConflictMode);
830  if ($resultObject->getName() !== $targetFile) {
831  $this->‪writeLog(5, 1, 1, 'File renamed from "%s" to "%s". Filename had to be sanitized!', [$sourceFile, $targetFile]);
832  $this->‪addMessageToFlashMessageQueue('FileUtility.FileNameSanitized', [$targetFile, $resultObject->getName()], ‪FlashMessage::WARNING);
833  } else {
834  $this->‪writeLog(5, 0, 1, 'File renamed from "%s" to "%s"', [$sourceFile, $targetFile]);
835  }
836  if ($sourceFile === $resultObject->getName()) {
837  $this->‪addMessageToFlashMessageQueue('FileUtility.FileRenamedSameName', [$sourceFile], ‪FlashMessage::INFO);
838  } else {
839  $this->‪addMessageToFlashMessageQueue('FileUtility.FileRenamedFromTo', [$sourceFile, $resultObject->getName()], ‪FlashMessage::OK);
840  }
841  } catch (InsufficientUserPermissionsException $e) {
842  $this->‪writeLog(5, 1, 102, 'You are not allowed to rename files!', []);
843  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToRenameFiles');
844  } catch (IllegalFileExtensionException $e) {
845  $this->‪writeLog(5, 1, 101, 'Extension of file name "%s" or "%s" was not allowed!', [$sourceFileObject->getName(), $targetFile]);
846  $this->‪addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameOrWasNotAllowed', [$sourceFileObject->getName(), $targetFile]);
847  } catch (ExistingTargetFileNameException $e) {
848  $this->‪writeLog(5, 1, 120, 'Destination "%s" existed already!', [$targetFile]);
849  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationExistedAlready', [$targetFile]);
850  } catch (NotInMountPointException $e) {
851  $this->‪writeLog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', [$targetFile]);
852  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFile]);
853  } catch (\RuntimeException $e) {
854  $this->‪writeLog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', [$sourceFileObject->getName(), $targetFile]);
855  $this->‪addMessageToFlashMessageQueue('FileUtility.FileWasNotRenamed', [$sourceFileObject->getName(), $targetFile]);
856  }
857  } else {
858  // Else means this is a Folder
859  try {
860  // Try to rename the Folder
861  $resultObject = $sourceFileObject->rename($targetFile);
862  $newFolderName = $resultObject->getName();
863  $this->‪writeLog(5, 0, 2, 'Directory renamed from "%s" to "%s"', [$sourceFile, $targetFile]);
864  if ($sourceFile === $newFolderName) {
865  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryRenamedSameName', [$sourceFile], ‪FlashMessage::INFO);
866  } else {
867  if ($newFolderName === $targetFile) {
868  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryRenamedFromTo', [$sourceFile, $newFolderName], ‪FlashMessage::OK);
869  } else {
870  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryRenamedFromToCharReplaced', [$sourceFile, $newFolderName], ‪FlashMessage::WARNING);
871  }
872  }
873  } catch (InsufficientUserPermissionsException $e) {
874  $this->‪writeLog(5, 1, 111, 'You are not allowed to rename directories!', []);
875  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToRenameDirectories');
876  } catch (ExistingTargetFileNameException $e) {
877  $this->‪writeLog(5, 1, 120, 'Destination "%s" existed already!', [$targetFile]);
878  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationExistedAlready', [$targetFile]);
879  } catch (NotInMountPointException $e) {
880  $this->‪writeLog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', [$targetFile]);
881  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFile]);
882  } catch (\RuntimeException $e) {
883  $this->‪writeLog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', [$sourceFileObject->getName(), $targetFile]);
884  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotRenamed', [$sourceFileObject->getName(), $targetFile]);
885  }
886  }
887  return $resultObject;
888  }
889 
900  public function ‪func_newfolder($cmds)
901  {
902  $targetFolderObject = $this->‪getFileObject($cmds['target']);
903  if (!$targetFolderObject instanceof Folder) {
904  $this->‪writeLog(6, 2, 104, 'Destination "%s" was not a directory', [$cmds['target']]);
905  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
906  return false;
907  }
908  $folderName = $cmds['data'];
909  try {
910  $resultObject = $targetFolderObject->createFolder($folderName);
911  $this->‪writeLog(6, 0, 1, 'Directory "%s" created in "%s"', [$folderName, $targetFolderObject->getIdentifier()]);
912  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryCreatedIn', [$folderName, $targetFolderObject->getIdentifier()], ‪FlashMessage::OK);
913  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException $e) {
914  $this->‪writeLog(6, 1, 104, 'Invalid folder name "%s"!', [$folderName]);
915  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateDirectories', [$folderName]);
916  } catch (\‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
917  $this->‪writeLog(6, 1, 103, 'You are not allowed to create directories!', []);
918  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateDirectories');
919  } catch (\‪TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
920  $this->‪writeLog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', [$targetFolderObject->getIdentifier()]);
921  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFolderObject->getIdentifier()]);
922  } catch (\‪TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
923  $this->‪writeLog(6, 1, 101, 'File or directory "%s" existed already!', [$folderName]);
924  $this->‪addMessageToFlashMessageQueue('FileUtility.FileOrDirectoryExistedAlready', [$folderName]);
925  } catch (\RuntimeException $e) {
926  $this->‪writeLog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', [$folderName, $targetFolderObject->getIdentifier()]);
927  $this->‪addMessageToFlashMessageQueue('FileUtility.DirectoryNotCreated', [$folderName, $targetFolderObject->getIdentifier()]);
928  }
929  return $resultObject;
930  }
931 
941  public function ‪func_newfile($cmds)
942  {
943  $targetFolderObject = $this->‪getFileObject($cmds['target']);
944  if (!$targetFolderObject instanceof Folder) {
945  $this->‪writeLog(8, 2, 104, 'Destination "%s" was not a directory', [$cmds['target']]);
946  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', [$cmds['target']]);
947  return false;
948  }
949  $resultObject = null;
950  $fileName = $cmds['data'];
951  try {
952  $resultObject = $targetFolderObject->createFile($fileName);
953  $this->‪writeLog(8, 0, 1, 'File created: "%s"', [$fileName]);
954  if ($resultObject->getName() !== $fileName) {
955  $this->‪addMessageToFlashMessageQueue('FileUtility.FileNameSanitized', [$fileName, $resultObject->getName()], ‪FlashMessage::WARNING);
956  }
957  $this->‪addMessageToFlashMessageQueue('FileUtility.FileCreated', [$resultObject->getName()], ‪FlashMessage::OK);
958  } catch (IllegalFileExtensionException $e) {
959  $this->‪writeLog(8, 1, 106, 'Extension of file "%s" was not allowed!', [$fileName]);
960  $this->‪addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileWasNotAllowed', [$fileName]);
961  } catch (InsufficientFolderWritePermissionsException $e) {
962  $this->‪writeLog(8, 1, 103, 'You are not allowed to create files!', []);
963  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateFiles');
964  } catch (NotInMountPointException $e) {
965  $this->‪writeLog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', [$targetFolderObject->getIdentifier()]);
966  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFolderObject->getIdentifier()]);
967  } catch (ExistingTargetFileNameException $e) {
968  $this->‪writeLog(8, 1, 101, 'File existed already in "%s"!', [$targetFolderObject->getIdentifier()]);
969  $this->‪addMessageToFlashMessageQueue('FileUtility.FileExistedAlreadyIn', [$targetFolderObject->getIdentifier()]);
970  } catch (InvalidFileNameException $e) {
971  $this->‪writeLog(8, 1, 106, 'File name "%s" was not allowed!', [$fileName]);
972  $this->‪addMessageToFlashMessageQueue('FileUtility.FileNameWasNotAllowed', [$fileName]);
973  } catch (\RuntimeException $e) {
974  $this->‪writeLog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', [$fileName, $targetFolderObject->getIdentifier()]);
975  $this->‪addMessageToFlashMessageQueue('FileUtility.FileWasNotCreated', [$fileName, $targetFolderObject->getIdentifier()]);
976  }
977  return $resultObject;
978  }
979 
986  public function ‪func_edit($cmds)
987  {
988  // Example identifier for $cmds['target'] => "4:mypath/tomyfolder/myfile.jpg"
989  // for backwards compatibility: the combined file identifier was the path+filename
990  $fileIdentifier = $cmds['target'];
991  $fileObject = $this->‪getFileObject($fileIdentifier);
992  // Example identifier for $cmds['target'] => "2:targetpath/targetfolder/"
993  $content = $cmds['data'];
994  if (!$fileObject instanceof ‪File) {
995  $this->‪writeLog(9, 2, 123, 'Target "%s" was not a file!', [$fileIdentifier]);
996  $this->‪addMessageToFlashMessageQueue('FileUtility.TargetWasNotAFile', [$fileIdentifier]);
997  return false;
998  }
999  $extList = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
1000  if (!GeneralUtility::inList($extList, $fileObject->getExtension())) {
1001  $this->‪writeLog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', [$fileObject->getExtension(), $extList]);
1002  $this->‪addMessageToFlashMessageQueue('FileUtility.FileExtensionIsNotATextfileFormat', [$fileObject->getExtension(), $extList]);
1003  return false;
1004  }
1005  try {
1006  $fileObject->setContents($content);
1007  clearstatcache();
1008  $this->‪writeLog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', [$fileObject->getIdentifier(), $fileObject->getSize(), md5($content)]);
1009  $this->‪addMessageToFlashMessageQueue('FileUtility.FileSavedToBytesMd5', [$fileObject->getIdentifier(), $fileObject->getSize(), md5($content)], ‪FlashMessage::OK);
1010  return true;
1012  $this->‪writeLog(9, 1, 104, 'You are not allowed to edit files!', []);
1013  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToEditFiles');
1014  return false;
1016  $this->‪writeLog(9, 1, 100, 'File "%s" was not saved! Write-permission problem?', [$fileObject->getIdentifier()]);
1017  $this->‪addMessageToFlashMessageQueue('FileUtility.FileWasNotSaved', [$fileObject->getIdentifier()]);
1018  return false;
1019  } catch (‪IllegalFileExtensionException $e) {
1020  $this->‪writeLog(9, 1, 100, 'File "%s" was not saved! File extension rejected!', [$fileObject->getIdentifier()]);
1021  $this->‪addMessageToFlashMessageQueue('FileUtility.FileWasNotSaved', [$fileObject->getIdentifier()]);
1022  return false;
1023  } catch (\RuntimeException $e) {
1024  $this->‪writeLog(9, 1, 100, 'File "%s" was not saved! File extension rejected!', [$fileObject->getIdentifier()]);
1025  $this->‪addMessageToFlashMessageQueue('FileUtility.FileWasNotSaved', [$fileObject->getIdentifier()]);
1026  return false;
1027  }
1028  }
1029 
1060  public function ‪func_upload($cmds)
1061  {
1062  $uploadPosition = $cmds['data'];
1063  $uploadedFileData = $_FILES['upload_' . $uploadPosition];
1064  if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
1065  $this->‪writeLog(1, 2, 108, 'No file was uploaded!', []);
1066  $this->‪addMessageToFlashMessageQueue('FileUtility.NoFileWasUploaded');
1067  return false;
1068  }
1069  // Example identifier for $cmds['target'] => "2:targetpath/targetfolder/"
1070  $targetFolderObject = $this->‪getFileObject($cmds['target']);
1071  // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
1072  if (!is_array($uploadedFileData['name'])) {
1073  $uploadedFileData = [
1074  'name' => [$uploadedFileData['name']],
1075  'type' => [$uploadedFileData['type']],
1076  'tmp_name' => [$uploadedFileData['tmp_name']],
1077  'size' => [$uploadedFileData['size']]
1078  ];
1079  }
1080  $resultObjects = [];
1081  $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
1082  // Loop through all uploaded files
1083  for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
1084  $fileInfo = [
1085  'name' => $uploadedFileData['name'][$i],
1086  'type' => $uploadedFileData['type'][$i],
1087  'tmp_name' => $uploadedFileData['tmp_name'][$i],
1088  'size' => $uploadedFileData['size'][$i]
1089  ];
1090  try {
1092  $fileObject = $targetFolderObject->addUploadedFile($fileInfo, (string)$this->existingFilesConflictMode);
1093  $fileObject = ‪ResourceFactory::getInstance()->‪getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
1094  if ($this->existingFilesConflictMode->equals(‪DuplicationBehavior::REPLACE)) {
1095  $this->‪getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
1096  }
1097  $resultObjects[] = $fileObject;
1098  $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
1099  if ($fileObject->getName() !== $fileInfo['name']) {
1100  $this->‪addMessageToFlashMessageQueue('FileUtility.FileNameSanitized', [$fileInfo['name'], $fileObject->getName()], ‪FlashMessage::WARNING);
1101  }
1102  $this->‪writeLog(1, 0, 1, 'Uploading file "%s" to "%s"', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
1103  $this->‪addMessageToFlashMessageQueue('FileUtility.UploadingFileTo', [$fileInfo['name'], $targetFolderObject->getIdentifier()], ‪FlashMessage::OK);
1104  } catch (InsufficientFileWritePermissionsException $e) {
1105  $this->‪writeLog(1, 1, 107, 'You are not allowed to override "%s"!', [$fileInfo['name']]);
1106  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToOverride', [$fileInfo['name']]);
1107  } catch (UploadException $e) {
1108  $this->‪writeLog(1, 2, 106, 'The upload has failed, no uploaded file found!', []);
1109  $this->‪addMessageToFlashMessageQueue('FileUtility.TheUploadHasFailedNoUploadedFileFound');
1110  } catch (InsufficientUserPermissionsException $e) {
1111  $this->‪writeLog(1, 1, 105, 'You are not allowed to upload files!', []);
1112  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToUploadFiles');
1113  } catch (UploadSizeException $e) {
1114  $this->‪writeLog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', [$fileInfo['name']]);
1115  $this->‪addMessageToFlashMessageQueue('FileUtility.TheUploadedFileExceedsTheSize-limit', [$fileInfo['name']]);
1116  } catch (InsufficientFolderWritePermissionsException $e) {
1117  $this->‪writeLog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', [$targetFolderObject->getIdentifier()]);
1118  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFolderObject->getIdentifier()]);
1119  } catch (IllegalFileExtensionException $e) {
1120  $this->‪writeLog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
1121  $this->‪addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
1122  } catch (ExistingTargetFileNameException $e) {
1123  $this->‪writeLog(1, 1, 101, 'No unique filename available in "%s"!', [$targetFolderObject->getIdentifier()]);
1124  $this->‪addMessageToFlashMessageQueue('FileUtility.NoUniqueFilenameAvailableIn', [$targetFolderObject->getIdentifier()]);
1125  } catch (\RuntimeException $e) {
1126  $this->‪writeLog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', [$targetFolderObject->getIdentifier()]);
1127  $this->‪addMessageToFlashMessageQueue('FileUtility.UploadedFileCouldNotBeMoved', [$targetFolderObject->getIdentifier()]);
1128  }
1129  }
1130 
1131  return $resultObjects;
1132  }
1133 
1144  protected function ‪replaceFile(array $cmdArr)
1145  {
1146  $uploadPosition = $cmdArr['data'];
1147  $fileInfo = $_FILES['replace_' . $uploadPosition];
1148  if (empty($fileInfo['name'])) {
1149  $this->‪writeLog(1, 2, 108, 'No file was uploaded for replacing!', []);
1150  $this->‪addMessageToFlashMessageQueue('FileUtility.NoFileWasUploadedForReplacing');
1151  return false;
1152  }
1153 
1154  $keepFileName = ($cmdArr['keepFilename'] == 1) ? true : false;
1155  $resultObjects = [];
1156 
1157  try {
1158  $fileObjectToReplace = $this->‪getFileObject($cmdArr['uid']);
1159  $folder = $fileObjectToReplace->‪getParentFolder();
1160  $resourceStorage = $fileObjectToReplace->‪getStorage();
1161 
1162  $fileObject = $resourceStorage->‪addUploadedFile($fileInfo, $folder, $fileObjectToReplace->getName(), ‪DuplicationBehavior::REPLACE);
1163 
1164  // Check if there is a file that is going to be uploaded that has a different name as the replacing one
1165  // but exists in that folder as well.
1166  // rename to another name, but check if the name is already given
1167  if ($keepFileName === false) {
1168  // if a file with the same name already exists, we need to change it to _01 etc.
1169  // if the file does not exist, we can do a simple rename
1170  $resourceStorage->moveFile($fileObject, $folder, $fileInfo['name'], ‪DuplicationBehavior::RENAME);
1171  }
1172 
1173  $resultObjects[] = $fileObject;
1174  $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
1175 
1176  $this->‪writeLog(1, 0, 1, 'Replacing file "%s" to "%s"', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()]);
1177  $this->‪addMessageToFlashMessageQueue('FileUtility.ReplacingFileTo', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()], ‪FlashMessage::OK);
1178  } catch (InsufficientFileWritePermissionsException $e) {
1179  $this->‪writeLog(1, 1, 107, 'You are not allowed to override "%s"!', [$fileInfo['name']]);
1180  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToOverride', [$fileInfo['name']]);
1181  } catch (UploadException $e) {
1182  $this->‪writeLog(1, 2, 106, 'The upload has failed, no uploaded file found!', []);
1183  $this->‪addMessageToFlashMessageQueue('FileUtility.TheUploadHasFailedNoUploadedFileFound');
1184  } catch (InsufficientUserPermissionsException $e) {
1185  $this->‪writeLog(1, 1, 105, 'You are not allowed to upload files!', []);
1186  $this->‪addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToUploadFiles');
1187  } catch (UploadSizeException $e) {
1188  $this->‪writeLog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', [$fileInfo['name']]);
1189  $this->‪addMessageToFlashMessageQueue('FileUtility.TheUploadedFileExceedsTheSize-limit', [$fileInfo['name']]);
1190  } catch (InsufficientFolderWritePermissionsException $e) {
1191  $this->‪writeLog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', [$fileObjectToReplace->getIdentifier()]);
1192  $this->‪addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$fileObjectToReplace->getIdentifier()]);
1193  } catch (IllegalFileExtensionException $e) {
1194  $this->‪writeLog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()]);
1195  $this->‪addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$fileInfo['name'], $fileObjectToReplace->getIdentifier()]);
1196  } catch (ExistingTargetFileNameException $e) {
1197  $this->‪writeLog(1, 1, 101, 'No unique filename available in "%s"!', [$fileObjectToReplace->getIdentifier()]);
1198  $this->‪addMessageToFlashMessageQueue('FileUtility.NoUniqueFilenameAvailableIn', [$fileObjectToReplace->getIdentifier()]);
1199  } catch (\RuntimeException $e) {
1200  throw $e;
1201  }
1202  return $resultObjects;
1203  }
1204 
1210  protected function ‪addFlashMessage(‪FlashMessage $flashMessage)
1211  {
1213  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1214 
1216  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1217  $defaultFlashMessageQueue->enqueue($flashMessage);
1218  }
1226  protected function ‪getIndexer(‪ResourceStorage $storage)
1227  {
1228  return GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Resource\Index\Indexer::class, $storage);
1229  }
1230 
1234  protected function ‪getBackendUser()
1235  {
1236  return ‪$GLOBALS['BE_USER'];
1237  }
1238 
1244  protected function ‪getLanguageService()
1245  {
1246  return ‪$GLOBALS['LANG'];
1247  }
1248 }
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\addMessageToFlashMessageQueue
‪addMessageToFlashMessageQueue($localizationKey, array $replaceMarkers=[], $severity=FlashMessage::ERROR)
Definition: ExtendedFileUtility.php:316
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\folderHasFilesInUse
‪bool folderHasFilesInUse(Folder $folder)
Definition: ExtendedFileUtility.php:511
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: ExtendedFileUtility.php:1227
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_delete
‪bool func_delete(array $cmds)
Definition: ExtendedFileUtility.php:344
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\addFlashMessage
‪addFlashMessage(FlashMessage $flashMessage)
Definition: ExtendedFileUtility.php:1203
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileWritePermissionsException
Definition: InsufficientFileWritePermissionsException.php:21
‪TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException
Definition: InsufficientUserPermissionsException.php:21
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\getExistingFilesConflictMode
‪string getExistingFilesConflictMode()
Definition: ExtendedFileUtility.php:130
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtilityProcessDataHookInterface
Definition: ExtendedFileUtilityProcessDataHookInterface.php:21
‪TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException
Definition: ExistingTargetFolderException.php:21
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:23
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\CANCEL
‪const CANCEL
Definition: DuplicationBehavior.php:45
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\setExistingFilesConflictMode
‪setExistingFilesConflictMode($existingFilesConflictMode)
Definition: ExtendedFileUtility.php:141
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:21
‪TYPO3\CMS\Core\Utility\File\BasicFileUtility
Definition: BasicFileUtility.php:32
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_upload
‪File[] func_upload($cmds)
Definition: ExtendedFileUtility.php:1053
‪TYPO3
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\getFileObject
‪File Folder getFileObject($identifier)
Definition: ExtendedFileUtility.php:605
‪TYPO3\CMS\Core\Resource\ResourceStorage\addUploadedFile
‪FileInterface addUploadedFile(array $uploadedFileData, Folder $targetFolder=null, $targetFileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: ResourceStorage.php:2064
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\processData
‪mixed processData()
Definition: ExtendedFileUtility.php:190
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\getLanguageService
‪LanguageService getLanguageService()
Definition: ExtendedFileUtility.php:1237
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
‪const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:66
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Resource\Folder\getParentFolder
‪Folder getParentFolder()
Definition: Folder.php:533
‪TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException
Definition: ExistingTargetFileNameException.php:21
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_edit
‪bool func_edit($cmds)
Definition: ExtendedFileUtility.php:979
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException
Definition: IllegalFileExtensionException.php:21
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\writeLog
‪writeLog($action, $error, $details_nr, $details, $data)
Definition: ExtendedFileUtility.php:296
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:21
‪TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException
Definition: InvalidTargetFolderException.php:21
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\replaceFile
‪array bool replaceFile(array $cmdArr)
Definition: ExtendedFileUtility.php:1137
‪TYPO3\CMS\Core\Resource\Exception\UploadException
Definition: UploadException.php:21
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:146
‪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\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\getErrorMessages
‪array getErrorMessages()
Definition: ExtendedFileUtility.php:284
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\$fileFactory
‪TYPO3 CMS Core Resource ResourceFactory $fileFactory
Definition: ExtendedFileUtility.php:123
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\setActionPermissions
‪setActionPermissions(array $permissions=[])
Definition: ExtendedFileUtility.php:176
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_newfolder
‪TYPO3 CMS Core Resource Folder false func_newfolder($cmds)
Definition: ExtendedFileUtility.php:893
‪TYPO3\CMS\Core\Resource\Exception\UploadSizeException
Definition: UploadSizeException.php:21
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_move
‪TYPO3 CMS Core Resource File false func_move($cmds)
Definition: ExtendedFileUtility.php:720
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:21
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:31
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_newfile
‪string func_newfile($cmds)
Definition: ExtendedFileUtility.php:934
‪TYPO3\CMS\Core\Utility\File
Definition: BasicFileUtility.php:2
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:200
‪TYPO3\CMS\Core\Resource\Folder\getFiles
‪TYPO3 CMS Core Resource File[] getFiles($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false, $sort='', $sortRev=false)
Definition: Folder.php:215
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\getIndexer
‪TYPO3 CMS Core Resource Index Indexer getIndexer(ResourceStorage $storage)
Definition: ExtendedFileUtility.php:1219
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:130
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\$flashMessages
‪array $flashMessages
Definition: ExtendedFileUtility.php:113
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:27
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_copy
‪TYPO3 CMS Core Resource File false func_copy($cmds)
Definition: ExtendedFileUtility.php:630
‪TYPO3\CMS\Core\Type\Enumeration\getConstants
‪static array getConstants($include_default=false)
Definition: Enumeration.php:166
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\start
‪start($fileCmds)
Definition: ExtendedFileUtility.php:162
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\$fileCmdMap
‪array $fileCmdMap
Definition: ExtendedFileUtility.php:117
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
Definition: InvalidEnumerationValueException.php:21
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\$internalUploadMap
‪array $internalUploadMap
Definition: ExtendedFileUtility.php:101
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException
Definition: InsufficientFolderWritePermissionsException.php:21
‪TYPO3\CMS\Core\Utility\Exception\NotImplementedMethodException
Definition: NotImplementedMethodException.php:25
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\REPLACE
‪const REPLACE
Definition: DuplicationBehavior.php:38
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Exception\FileOperationErrorException
Definition: FileOperationErrorException.php:21
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\func_rename
‪TYPO3 CMS Core Resource File func_rename($cmds)
Definition: ExtendedFileUtility.php:813
‪TYPO3\CMS\Core\Resource\Exception\NotInMountPointException
Definition: NotInMountPointException.php:21
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\$errorMessages
‪array $errorMessages
Definition: ExtendedFileUtility.php:107
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\$existingFilesConflictMode
‪TYPO3 CMS Core Resource DuplicationBehavior $existingFilesConflictMode
Definition: ExtendedFileUtility.php:69
‪TYPO3\CMS\Core\Resource\Exception
Definition: AbstractFileOperationException.php:2
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:25
‪TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException
Definition: InvalidFileNameException.php:21
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\transformFileReferenceToRecordReference
‪array transformFileReferenceToRecordReference(array $referenceRecord)
Definition: ExtendedFileUtility.php:571
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility\$actionPerms
‪array $actionPerms
Definition: ExtendedFileUtility.php:77
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObjectByStorageAndIdentifier
‪File ProcessedFile null getFileObjectByStorageAndIdentifier($storageUid, &$fileIdentifier)
Definition: ResourceFactory.php:454