Esempio n. 1
0
 /**
  * Upload of files (action=1)
  * when having multiple uploads (HTML5-style), the array $_FILES looks like this:
  * Array(
  * [upload_1] => Array(
  * [name] => Array(
  * [0] => GData - Content-Elements and Media-Gallery.pdf
  * [1] => CMS Expo 2011.txt
  * )
  * [type] => Array(
  * [0] => application/pdf
  * [1] => text/plain
  * )
  * [tmp_name] => Array(
  * [0] => /Applications/MAMP/tmp/php/phpNrOB43
  * [1] => /Applications/MAMP/tmp/php/phpD2HQAK
  * )
  * [size] => Array(
  * [0] => 373079
  * [1] => 1291
  * )
  * )
  * )
  * in HTML you'd need sth like this: <input type="file" name="upload_1[]" multiple="true" />
  *
  * @param array $cmds $cmds['data'] is the ID-number (points to the global var that holds the filename-ref
  *                    ($_FILES['upload_' . $id]['name']) . $cmds['target'] is the target directory, $cmds['charset']
  *                    is the the character set of the file name (utf-8 is needed for JS-interaction)
  * @return File[] | FALSE Returns an array of new file objects upon success. False otherwise
  */
 public function func_upload($cmds)
 {
     $uploadPosition = $cmds['data'];
     $uploadedFileData = $_FILES['upload_' . $uploadPosition];
     if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
         $this->writeLog(1, 2, 108, 'No file was uploaded!', []);
         $this->addMessageToFlashMessageQueue('FileUtility.NoFileWasUploaded');
         return false;
     }
     // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
     $targetFolderObject = $this->getFileObject($cmds['target']);
     // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
     if (!is_array($uploadedFileData['name'])) {
         $uploadedFileData = ['name' => [$uploadedFileData['name']], 'type' => [$uploadedFileData['type']], 'tmp_name' => [$uploadedFileData['tmp_name']], 'size' => [$uploadedFileData['size']]];
     }
     $resultObjects = [];
     $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
     // Loop through all uploaded files
     for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
         $fileInfo = ['name' => $uploadedFileData['name'][$i], 'type' => $uploadedFileData['type'][$i], 'tmp_name' => $uploadedFileData['tmp_name'][$i], 'size' => $uploadedFileData['size'][$i]];
         try {
             /** @var $fileObject File */
             $fileObject = $targetFolderObject->addUploadedFile($fileInfo, (string) $this->existingFilesConflictMode);
             $fileObject = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
             if ($this->existingFilesConflictMode->equals(DuplicationBehavior::REPLACE)) {
                 $this->getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
             }
             $resultObjects[] = $fileObject;
             $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
             $this->writeLog(1, 0, 1, 'Uploading file "%s" to "%s"', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
             $this->addMessageToFlashMessageQueue('FileUtility.UploadingFileTo', [$fileInfo['name'], $targetFolderObject->getIdentifier()], FlashMessage::OK);
         } catch (InsufficientFileWritePermissionsException $e) {
             $this->writeLog(1, 1, 107, 'You are not allowed to override "%s"!', [$fileInfo['name']]);
             $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToOverride', [$fileInfo['name']]);
         } catch (UploadException $e) {
             $this->writeLog(1, 2, 106, 'The upload has failed, no uploaded file found!', []);
             $this->addMessageToFlashMessageQueue('FileUtility.TheUploadHasFailedNoUploadedFileFound');
         } catch (InsufficientUserPermissionsException $e) {
             $this->writeLog(1, 1, 105, 'You are not allowed to upload files!', []);
             $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToUploadFiles');
         } catch (UploadSizeException $e) {
             $this->writeLog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', [$fileInfo['name']]);
             $this->addMessageToFlashMessageQueue('FileUtility.TheUploadedFileExceedsTheSize-limit', [$fileInfo['name']]);
         } catch (InsufficientFolderWritePermissionsException $e) {
             $this->writeLog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', [$targetFolderObject->getIdentifier()]);
             $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', [$targetFolderObject->getIdentifier()]);
         } catch (IllegalFileExtensionException $e) {
             $this->writeLog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
             $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', [$fileInfo['name'], $targetFolderObject->getIdentifier()]);
         } catch (ExistingTargetFileNameException $e) {
             $this->writeLog(1, 1, 101, 'No unique filename available in "%s"!', [$targetFolderObject->getIdentifier()]);
             $this->addMessageToFlashMessageQueue('FileUtility.NoUniqueFilenameAvailableIn', [$targetFolderObject->getIdentifier()]);
         } catch (\RuntimeException $e) {
             $this->writeLog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', [$targetFolderObject->getIdentifier()]);
             $this->addMessageToFlashMessageQueue('FileUtility.UploadedFileCouldNotBeMoved', [$targetFolderObject->getIdentifier()]);
         }
     }
     return $resultObjects;
 }
 /**
  * Upload of files (action=1)
  * when having multiple uploads (HTML5-style), the array $_FILES looks like this:
  * Array(
  * [upload_1] => Array(
  * [name] => Array(
  * [0] => GData - Content-Elements and Media-Gallery.pdf
  * [1] => CMS Expo 2011.txt
  * )
  * [type] => Array(
  * [0] => application/pdf
  * [1] => text/plain
  * )
  * [tmp_name] => Array(
  * [0] => /Applications/MAMP/tmp/php/phpNrOB43
  * [1] => /Applications/MAMP/tmp/php/phpD2HQAK
  * )
  * [size] => Array(
  * [0] => 373079
  * [1] => 1291
  * )
  * )
  * )
  * in HTML you'd need sth like this: <input type="file" name="upload_1[]" multiple="true" />
  *
  * @param array $cmds $cmds['data'] is the ID-number (points to the global var that holds the filename-ref  ($_FILES['upload_' . $id]['name']) . $cmds['target'] is the target directory, $cmds['charset'] is the the character set of the file name (utf-8 is needed for JS-interaction)
  * @return File[] | FALSE Returns an array of new file objects upon success. False otherwise
  */
 public function func_upload($cmds)
 {
     if (!$this->isInit) {
         return false;
     }
     $uploadPosition = $cmds['data'];
     $uploadedFileData = $_FILES['upload_' . $uploadPosition];
     if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
         $this->writelog(1, 2, 108, 'No file was uploaded!', '');
         return false;
     }
     // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
     $targetFolderObject = $this->getFileObject($cmds['target']);
     // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
     if (!is_array($uploadedFileData['name'])) {
         $uploadedFileData = array('name' => array($uploadedFileData['name']), 'type' => array($uploadedFileData['type']), 'tmp_name' => array($uploadedFileData['tmp_name']), 'size' => array($uploadedFileData['size']));
     }
     $resultObjects = array();
     $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
     // Loop through all uploaded files
     for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
         $fileInfo = array('name' => $uploadedFileData['name'][$i], 'type' => $uploadedFileData['type'][$i], 'tmp_name' => $uploadedFileData['tmp_name'][$i], 'size' => $uploadedFileData['size'][$i]);
         try {
             if ((int) $this->dontCheckForUnique === 1) {
                 GeneralUtility::deprecationLog('dontCheckForUnique = 1 is deprecated. Use setExistingFilesConflictMode(DuplicationBehavior::REPLACE);. Support for dontCheckForUnique will be removed in TYPO3 CMS 8.');
                 $this->existingFilesConflictMode = DuplicationBehavior::cast(DuplicationBehavior::REPLACE);
             }
             /** @var $fileObject File */
             $fileObject = $targetFolderObject->addUploadedFile($fileInfo, (string) $this->existingFilesConflictMode);
             $fileObject = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
             if ($this->existingFilesConflictMode->equals(DuplicationBehavior::REPLACE)) {
                 $this->getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
             }
             $resultObjects[] = $fileObject;
             $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
             $this->writelog(1, 0, 1, 'Uploading file "%s" to "%s"', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
         } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileWritePermissionsException $e) {
             $this->writelog(1, 1, 107, 'You are not allowed to override "%s"!', array($fileInfo['name']));
         } catch (\TYPO3\CMS\Core\Resource\Exception\UploadException $e) {
             $this->writelog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
         } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
             $this->writelog(1, 1, 105, 'You are not allowed to upload files!', '');
         } catch (\TYPO3\CMS\Core\Resource\Exception\UploadSizeException $e) {
             $this->writelog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', array($fileInfo['name']));
         } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
             $this->writelog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
         } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
             $this->writelog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
         } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
             $this->writelog(1, 1, 101, 'No unique filename available in "%s"!', array($targetFolderObject->getIdentifier()));
         } catch (\RuntimeException $e) {
             $this->writelog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', array($targetFolderObject->getIdentifier()));
         }
     }
     return $resultObjects;
 }
 /**
  * Adds an uploaded file into the Storage. Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::file_upload()
  *
  * @param array $uploadedFileData contains information about the uploaded file given by $_FILES['file1']
  * @param Folder $targetFolder the target folder
  * @param string $targetFileName the file name to be written
  * @param string $conflictMode a value of the DuplicationBehavior enumeration
  * @return FileInterface The file object
  */
 public function addUploadedFile(array $uploadedFileData, Folder $targetFolder = null, $targetFileName = null, $conflictMode = DuplicationBehavior::CANCEL)
 {
     $conflictMode = DuplicationBehavior::cast($conflictMode);
     $localFilePath = $uploadedFileData['tmp_name'];
     if ($targetFolder === null) {
         $targetFolder = $this->getDefaultFolder();
     }
     if ($targetFileName === null) {
         $targetFileName = $uploadedFileData['name'];
     }
     $targetFileName = $this->driver->sanitizeFileName($targetFileName);
     $this->assureFileUploadPermissions($localFilePath, $targetFolder, $targetFileName, $uploadedFileData['size']);
     if ($this->hasFileInFolder($targetFileName, $targetFolder) && $conflictMode->equals(DuplicationBehavior::REPLACE)) {
         $file = $this->getFileInFolder($targetFileName, $targetFolder);
         $resultObject = $this->replaceFile($file, $localFilePath);
     } else {
         $resultObject = $this->addFile($localFilePath, $targetFolder, $targetFileName, (string) $conflictMode);
     }
     return $resultObject;
 }
Esempio n. 4
0
 /**
  * Initialize variables, file object
  * Incoming GET vars include id, pointer, table, imagemode
  *
  * @return void
  * @throws \RuntimeException
  * @throws Exception\InsufficientFolderAccessPermissionsException
  */
 public function initializeObject()
 {
     $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
     $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf');
     $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_misc.xlf');
     // Setting GPvars:
     $this->id = $combinedIdentifier = GeneralUtility::_GP('id');
     $this->pointer = GeneralUtility::_GP('pointer');
     $this->table = GeneralUtility::_GP('table');
     $this->imagemode = GeneralUtility::_GP('imagemode');
     $this->cmd = GeneralUtility::_GP('cmd');
     $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
     try {
         if ($combinedIdentifier) {
             /** @var $resourceFactory ResourceFactory */
             $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
             $storage = $resourceFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
             $identifier = substr($combinedIdentifier, strpos($combinedIdentifier, ':') + 1);
             if (!$storage->hasFolder($identifier)) {
                 $identifier = $storage->getFolderIdentifierFromFileIdentifier($identifier);
             }
             $this->folderObject = $resourceFactory->getFolderObjectFromCombinedIdentifier($storage->getUid() . ':' . $identifier);
             // Disallow access to fallback storage 0
             if ($storage->getUid() === 0) {
                 throw new Exception\InsufficientFolderAccessPermissionsException('You are not allowed to access files outside your storages', 1434539815);
             }
             // Disallow the rendering of the processing folder (e.g. could be called manually)
             if ($this->folderObject && $storage->isProcessingFolder($this->folderObject)) {
                 $this->folderObject = $storage->getRootLevelFolder();
             }
         } else {
             // Take the first object of the first storage
             $fileStorages = $this->getBackendUser()->getFileStorages();
             $fileStorage = reset($fileStorages);
             if ($fileStorage) {
                 $this->folderObject = $fileStorage->getRootLevelFolder();
             } else {
                 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
             }
         }
         if ($this->folderObject && !$this->folderObject->getStorage()->isWithinFileMountBoundaries($this->folderObject)) {
             throw new \RuntimeException('Folder not accessible.', 1430409089);
         }
     } catch (Exception\InsufficientFolderAccessPermissionsException $permissionException) {
         $this->folderObject = null;
         $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class, sprintf($this->getLanguageService()->getLL('missingFolderPermissionsMessage'), $this->id), $this->getLanguageService()->getLL('missingFolderPermissionsTitle'), FlashMessage::NOTICE);
     } catch (Exception $fileException) {
         // Set folder object to null and throw a message later on
         $this->folderObject = null;
         // Take the first object of the first storage
         $fileStorages = $this->getBackendUser()->getFileStorages();
         $fileStorage = reset($fileStorages);
         if ($fileStorage instanceof \TYPO3\CMS\Core\Resource\ResourceStorage) {
             $this->folderObject = $fileStorage->getRootLevelFolder();
             if (!$fileStorage->isWithinFileMountBoundaries($this->folderObject)) {
                 $this->folderObject = null;
             }
         }
         $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class, sprintf($this->getLanguageService()->getLL('folderNotFoundMessage'), $this->id), $this->getLanguageService()->getLL('folderNotFoundTitle'), FlashMessage::NOTICE);
     } catch (\RuntimeException $e) {
         $this->folderObject = null;
         $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class, $e->getMessage() . ' (' . $e->getCode() . ')', $this->getLanguageService()->getLL('folderNotFoundTitle'), FlashMessage::NOTICE);
     }
     if ($this->folderObject && !$this->folderObject->getStorage()->checkFolderActionPermission('read', $this->folderObject)) {
         $this->folderObject = null;
     }
     // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
     $this->menuConfig();
 }
Esempio n. 5
0
 /**
  * Registering incoming data
  *
  * @return void
  */
 protected function init()
 {
     // Set the GPvars from outside
     $this->file = GeneralUtility::_GP('file');
     $this->CB = GeneralUtility::_GP('CB');
     $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
     $this->vC = GeneralUtility::_GP('vC');
     $this->redirect = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('redirect'));
     $this->initClipboard();
     $this->fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
 }