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;
 }