[BUGFIX] Enable file upload from Frontend context
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / File / ExtendedFileUtility.php
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Messaging\FlashMessage;
19 use TYPO3\CMS\Core\Messaging\FlashMessageService;
20 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
21 use TYPO3\CMS\Core\Resource\Exception;
22 use TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException;
23 use TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException;
24 use TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException;
25 use TYPO3\CMS\Core\Resource\Exception\InsufficientFileWritePermissionsException;
26 use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
27 use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException;
28 use TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException;
29 use TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException;
30 use TYPO3\CMS\Core\Resource\Exception\NotInMountPointException;
31 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
32 use TYPO3\CMS\Core\Resource\Exception\UploadException;
33 use TYPO3\CMS\Core\Resource\Exception\UploadSizeException;
34 use TYPO3\CMS\Core\Resource\File;
35 use TYPO3\CMS\Core\Resource\Folder;
36 use TYPO3\CMS\Core\Resource\ResourceFactory;
37 use TYPO3\CMS\Core\Resource\ResourceStorage;
38 use TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException;
39 use TYPO3\CMS\Core\Utility\GeneralUtility;
40 use TYPO3\CMS\Lang\LanguageService;
41
42 /**
43 * Contains functions for performing file operations like copying, pasting, uploading, moving,
44 * deleting etc. through the TCE
45 *
46 * See document "TYPO3 Core API" for syntax
47 *
48 * This class contains functions primarily used by tce_file.php (TYPO3 Core Engine for file manipulation)
49 * Functions include copying, moving, deleting, uploading and so on...
50 *
51 * Important internal variables:
52 *
53 * $filemounts (see basicFileFunctions)
54 * $f_ext (see basicFileFunctions)
55 *
56 * All fileoperations must be within the filemount-paths. Further the fileextension
57 * MUST validate TRUE with the f_ext array
58 *
59 * The unzip-function allows unzip only if the destination path has it's f_ext[]['allow'] set to '*'!!
60 * You are allowed to copy/move folders within the same 'space' (web/ftp).
61 * You are allowed to copy/move folders between spaces (web/ftp) IF the destination has it's f_ext[]['allow'] set to '*'!
62 *
63 * Advice:
64 * You should always exclude php-files from the webspace. This will keep people from uploading, copy/moving and renaming files to become executable php scripts.
65 * You should never mount a ftp_space 'below' the webspace so that it reaches into the webspace. This is because if somebody unzips a zip-file in the ftp-space so that it reaches out into the webspace this will be a violation of the safety
66 * For example this is a bad idea: you have an ftp-space that is '/www/' and a web-space that is '/www/htdocs/'
67 */
68 class ExtendedFileUtility extends BasicFileUtility
69 {
70 /**
71 * Defines behaviour when uploading files with names that already exist; possible values are
72 * the values of the \TYPO3\CMS\Core\Resource\DuplicationBehavior enumeration
73 *
74 * @var \TYPO3\CMS\Core\Resource\DuplicationBehavior
75 */
76 protected $existingFilesConflictMode;
77
78 /**
79 * This array is self-explaining (look in the class below).
80 * It grants access to the functions. This could be set from outside in order to enabled functions to users.
81 * See also the function setActionPermissions() which takes input directly from the user-record
82 *
83 * @var array
84 */
85 public $actionPerms = array(
86 // File permissions
87 'addFile' => false,
88 'readFile' => false,
89 'writeFile' => false,
90 'copyFile' => false,
91 'moveFile' => false,
92 'renameFile' => false,
93 'deleteFile' => false,
94 // Folder permissions
95 'addFolder' => false,
96 'readFolder' => false,
97 'writeFolder' => false,
98 'copyFolder' => false,
99 'moveFolder' => false,
100 'renameFolder' => false,
101 'deleteFolder' => false,
102 'recursivedeleteFolder' => false
103 );
104
105 /**
106 * This is regarded to be the recycler folder
107 *
108 * @var string
109 */
110 public $recyclerFN = '_recycler_';
111
112 /**
113 * Will contain map between upload ID and the final filename
114 *
115 * @var array
116 */
117 public $internalUploadMap = array();
118
119 /**
120 * @var string
121 */
122 public $lastError = '';
123
124 /**
125 * All error messages from the file operations of this script instance
126 *
127 * @var array
128 */
129 protected $errorMessages = array();
130
131 /**
132 * Container for FlashMessages so they can be localized
133 *
134 * @var array
135 */
136 protected $flashMessages = [];
137
138 /**
139 * @var array
140 */
141 protected $fileCmdMap;
142
143 /**
144 * The File Factory
145 *
146 * @var \TYPO3\CMS\Core\Resource\ResourceFactory
147 */
148 protected $fileFactory;
149
150 /**
151 * Get existingFilesConflictMode
152 *
153 * @return string
154 */
155 public function getExistingFilesConflictMode()
156 {
157 return (string)$this->existingFilesConflictMode;
158 }
159
160 /**
161 * Set existingFilesConflictMode
162 *
163 * @param \TYPO3\CMS\Core\Resource\DuplicationBehavior|string $existingFilesConflictMode Instance or constant of \TYPO3\CMS\Core\Resource\DuplicationBehavior
164 * @return void
165 * @throws Exception
166 */
167 public function setExistingFilesConflictMode($existingFilesConflictMode)
168 {
169 try {
170 $this->existingFilesConflictMode = DuplicationBehavior::cast($existingFilesConflictMode);
171 } catch (InvalidEnumerationValueException $e) {
172 throw new Exception(
173 sprintf(
174 'Invalid argument, received: "%s", expected a value from enumeration \TYPO3\CMS\Core\Resource\DuplicationBehavior (%s)',
175 $existingFilesConflictMode,
176 implode(', ', DuplicationBehavior::getConstants())
177 )
178 );
179 }
180 }
181
182 /**
183 * Initialization of the class
184 *
185 * @param array $fileCmds Array with the commands to execute. See "TYPO3 Core API" document
186 * @return void
187 */
188 public function start($fileCmds)
189 {
190 // Initialize Object Factory
191 $this->fileFactory = ResourceFactory::getInstance();
192 // Initializing file processing commands:
193 $this->fileCmdMap = $fileCmds;
194 }
195
196 /**
197 * Sets the file action permissions.
198 * If no argument is given, permissions of the currently logged in backend user are taken into account.
199 *
200 * @param array $permissions File Permissions.
201 * @return void
202 */
203 public function setActionPermissions(array $permissions = array())
204 {
205 if (empty($permissions)) {
206 $permissions = $this->getBackendUser()->getFilePermissions();
207 }
208 $this->actionPerms = $permissions;
209 }
210
211 /**
212 * Processing the command array in $this->fileCmdMap
213 *
214 * @return mixed FALSE, if the file functions were not initialized
215 * @throws \UnexpectedValueException
216 */
217 public function processData()
218 {
219 $result = array();
220 if (!$this->isInit) {
221 return false;
222 }
223 if (is_array($this->fileCmdMap)) {
224 // Check if there were uploads expected, but no one made
225 if ($this->fileCmdMap['upload']) {
226 $uploads = $this->fileCmdMap['upload'];
227 foreach ($uploads as $upload) {
228 if (empty($_FILES['upload_' . $upload['data']]['name'])
229 || (is_array($_FILES['upload_' . $upload['data']]['name'])
230 && empty($_FILES['upload_' . $upload['data']]['name'][0])
231 )
232 ) {
233 unset($this->fileCmdMap['upload'][$upload['data']]);
234 }
235 }
236 if (empty($this->fileCmdMap['upload'])) {
237 $this->writeLog(1, 1, 108, 'No file was uploaded!', '');
238 $this->addMessageToFlashMessageQueue('FileUtility.NoFileWasUploaded');
239 }
240 }
241
242 // Check if there were new folder names expected, but non given
243 if ($this->fileCmdMap['newfolder']) {
244 foreach ($this->fileCmdMap['newfolder'] as $key => $cmdArr) {
245 if (empty($cmdArr['data'])) {
246 unset($this->fileCmdMap['newfolder'][$key]);
247 }
248 }
249 if (empty($this->fileCmdMap['newfolder'])) {
250 $this->writeLog(6, 1, 108, 'No name for new folder given!', '');
251 $this->addMessageToFlashMessageQueue('FileUtility.NoNameForNewFolderGiven');
252 }
253 }
254
255 // Traverse each set of actions
256 foreach ($this->fileCmdMap as $action => $actionData) {
257 // Traverse all action data. More than one file might be affected at the same time.
258 if (is_array($actionData)) {
259 $result[$action] = array();
260 foreach ($actionData as $cmdArr) {
261 // Clear file stats
262 clearstatcache();
263 // Branch out based on command:
264 switch ($action) {
265 case 'delete':
266 $result[$action][] = $this->func_delete($cmdArr);
267 break;
268 case 'copy':
269 $result[$action][] = $this->func_copy($cmdArr);
270 break;
271 case 'move':
272 $result[$action][] = $this->func_move($cmdArr);
273 break;
274 case 'rename':
275 $result[$action][] = $this->func_rename($cmdArr);
276 break;
277 case 'newfolder':
278 $result[$action][] = $this->func_newfolder($cmdArr);
279 break;
280 case 'newfile':
281 $result[$action][] = $this->func_newfile($cmdArr);
282 break;
283 case 'editfile':
284 $result[$action][] = $this->func_edit($cmdArr);
285 break;
286 case 'upload':
287 $result[$action][] = $this->func_upload($cmdArr);
288 break;
289 case 'replace':
290 $result[$action][] = $this->replaceFile($cmdArr);
291 break;
292 }
293 // Hook for post-processing the action
294 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'])) {
295 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'] as $classRef) {
296 $hookObject = GeneralUtility::getUserObj($classRef);
297 if (!$hookObject instanceof ExtendedFileUtilityProcessDataHookInterface) {
298 throw new \UnexpectedValueException('$hookObject must implement interface TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtilityProcessDataHookInterface', 1279719168);
299 }
300 $hookObject->processData_postProcessAction($action, $cmdArr, $result[$action], $this);
301 }
302 }
303 }
304 }
305 }
306 }
307 return $result;
308 }
309
310 /**
311 * Adds all log error messages from the operations of this script instance to the FlashMessageQueue
312 *
313 * @return void
314 * @deprecated since TYPO3 CMS 8, will be removed in TYPO3 CMS 9
315 */
316 public function pushErrorMessagesToFlashMessageQueue()
317 {
318 GeneralUtility::logDeprecatedFunction();
319 foreach ($this->getErrorMessages() as $msg) {
320 $flashMessage = GeneralUtility::makeInstance(
321 FlashMessage::class,
322 $msg,
323 '',
324 FlashMessage::ERROR,
325 true
326 );
327 $this->addFlashMessage($flashMessage);
328 }
329 }
330
331 /**
332 * Return all error messages from the file operations of this script instance
333 *
334 * @return array all errorMessages as a numerical array
335 */
336 public function getErrorMessages()
337 {
338 return $this->errorMessages;
339 }
340
341 /**
342 * @param int $action The action number. See the functions in the class for a hint. Eg. edit is '9', upload is '1' ...
343 * @param int $error The severity: 0 = message, 1 = error, 2 = System Error, 3 = security notice (admin)
344 * @param int $details_nr This number is unique for every combination of $type and $action. This is the error-message number, which can later be used to translate error messages.
345 * @param string $details This is the default, raw error message in english
346 * @param array $data Array with special information that may go into $details by "%s" marks / sprintf() when the log is shown
347 * @return void
348 */
349 public function writeLog($action, $error, $details_nr, $details, $data)
350 {
351 // Type value for tce_file.php
352 $type = 2;
353 if (is_object($this->getBackendUser())) {
354 $this->getBackendUser()->writelog($type, $action, $error, $details_nr, $details, $data);
355 }
356 if ($error > 0) {
357 $this->lastError = vsprintf($details, $data);
358 $this->errorMessages[] = $this->lastError;
359 }
360 }
361
362 /**
363 * Adds a localized FlashMessage to the message queue
364 *
365 * @param string $localizationKey
366 * @param array $replaceMarkers
367 * @param int $severity
368 * @return void
369 * @throws \InvalidArgumentException
370 */
371 protected function addMessageToFlashMessageQueue($localizationKey, array $replaceMarkers = [], $severity = FlashMessage::ERROR)
372 {
373 if (TYPO3_MODE !== 'BE') {
374 return;
375 }
376 $label = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/fileMessages.xlf:' . $localizationKey);
377 $message = vsprintf($label, $replaceMarkers);
378 $flashMessage = GeneralUtility::makeInstance(
379 FlashMessage::class,
380 $message,
381 '',
382 $severity,
383 true
384 );
385 $this->addFlashMessage($flashMessage);
386 }
387
388 /*************************************
389 *
390 * File operation functions
391 *
392 **************************************/
393 /**
394 * Deleting files and folders (action=4)
395 *
396 * @param array $cmds $cmds['data'] is the file/folder to delete
397 * @return bool Returns TRUE upon success
398 */
399 public function func_delete(array $cmds)
400 {
401 $result = false;
402 if (!$this->isInit) {
403 return $result;
404 }
405 // Example indentifier for $cmds['data'] => "4:mypath/tomyfolder/myfile.jpg"
406 // for backwards compatibility: the combined file identifier was the path+filename
407 try {
408 $fileObject = $this->getFileObject($cmds['data']);
409 } catch (ResourceDoesNotExistException $e) {
410 $flashMessage = GeneralUtility::makeInstance(
411 FlashMessage::class,
412 sprintf(
413 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileNotFound'),
414 $cmds['data']
415 ),
416 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileNotFound'),
417 FlashMessage::ERROR,
418 true
419 );
420 $this->addFlashMessage($flashMessage);
421
422 return false;
423 }
424 // @todo implement the recycler feature which has been removed from the original implementation
425 // checks to delete the file
426 if ($fileObject instanceof File) {
427 // check if the file still has references
428 // Exclude sys_file_metadata records as these are no use references
429 $databaseConnection = $this->getDatabaseConnection();
430 $table = 'sys_refindex';
431 $refIndexRecords = $databaseConnection->exec_SELECTgetRows(
432 '*',
433 $table,
434 'deleted=0 AND ref_table=' . $databaseConnection->fullQuoteStr('sys_file', $table)
435 . ' AND ref_uid=' . (int)$fileObject->getUid()
436 . ' AND tablename != ' . $databaseConnection->fullQuoteStr('sys_file_metadata', $table)
437 );
438 $deleteFile = true;
439 if (!empty($refIndexRecords)) {
440 $shortcutContent = array();
441 $brokenReferences = array();
442
443 foreach ($refIndexRecords as $fileReferenceRow) {
444 if ($fileReferenceRow['tablename'] === 'sys_file_reference') {
445 $row = $this->transformFileReferenceToRecordReference($fileReferenceRow);
446 $shortcutRecord = BackendUtility::getRecord($row['tablename'], $row['recuid']);
447
448 if ($shortcutRecord) {
449 $shortcutContent[] = '[record:' . $row['tablename'] . ':' . $row['recuid'] . ']';
450 } else {
451 $brokenReferences[] = $fileReferenceRow['ref_uid'];
452 }
453 }
454 }
455 if (!empty($brokenReferences)) {
456 // render a message that the file has broken references
457 $flashMessage = GeneralUtility::makeInstance(
458 FlashMessage::class,
459 sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileHasBrokenReferences'), count($brokenReferences)),
460 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileHasBrokenReferences'),
461 FlashMessage::INFO,
462 true
463 );
464 $this->addFlashMessage($flashMessage);
465 }
466 if (!empty($shortcutContent)) {
467 // render a message that the file could not be deleted
468 $flashMessage = GeneralUtility::makeInstance(
469 FlashMessage::class,
470 sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileNotDeletedHasReferences'), $fileObject->getName()) . ' ' . implode(', ', $shortcutContent),
471 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileNotDeletedHasReferences'),
472 FlashMessage::WARNING,
473 true
474 );
475 $this->addFlashMessage($flashMessage);
476 $deleteFile = false;
477 }
478 }
479
480 if ($deleteFile) {
481 try {
482 $result = $fileObject->delete();
483
484 // show the user that the file was deleted
485 $flashMessage = GeneralUtility::makeInstance(
486 FlashMessage::class,
487 sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileDeleted'), $fileObject->getName()),
488 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileDeleted'),
489 FlashMessage::OK,
490 true
491 );
492 $this->addFlashMessage($flashMessage);
493 // Log success
494 $this->writeLog(4, 0, 1, 'File "%s" deleted', array($fileObject->getIdentifier()));
495 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
496 $this->writeLog(4, 1, 112, 'You are not allowed to access the file', array($fileObject->getIdentifier()));
497 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToAccessTheFile', array($fileObject->getIdentifier()));
498 } catch (NotInMountPointException $e) {
499 $this->writeLog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
500 $this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotWithinYourMountpoints', array($fileObject->getIdentifier()));
501 } catch (\RuntimeException $e) {
502 $this->writeLog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', array($fileObject->getIdentifier()));
503 $this->addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteFile', array($fileObject->getIdentifier()));
504 }
505 }
506 } else {
507 /** @var Folder $fileObject */
508 if (!$this->folderHasFilesInUse($fileObject)) {
509 try {
510 $result = $fileObject->delete(true);
511 if ($result) {
512 // notify the user that the folder was deleted
513 /** @var FlashMessage $flashMessage */
514 $flashMessage = GeneralUtility::makeInstance(
515 FlashMessage::class,
516 sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderDeleted'), $fileObject->getName()),
517 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderDeleted'),
518 FlashMessage::OK,
519 true
520 );
521 $this->addFlashMessage($flashMessage);
522 // Log success
523 $this->writeLog(4, 0, 3, 'Directory "%s" deleted', array($fileObject->getIdentifier()));
524 }
525 } catch (InsufficientUserPermissionsException $e) {
526 $this->writeLog(4, 1, 120, 'Could not delete directory! Is directory "%s" empty? (You are not allowed to delete directories recursively).', array($fileObject->getIdentifier()));
527 $this->addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteDirectory', array($fileObject->getIdentifier()));
528 } catch (InsufficientFolderAccessPermissionsException $e) {
529 $this->writeLog(4, 1, 123, 'You are not allowed to access the directory', array($fileObject->getIdentifier()));
530 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToAccessTheDirectory', array($fileObject->getIdentifier()));
531 } catch (NotInMountPointException $e) {
532 $this->writeLog(4, 1, 121, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
533 $this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotWithinYourMountpoints', array($fileObject->getIdentifier()));
534 } catch (\TYPO3\CMS\Core\Resource\Exception\FileOperationErrorException $e) {
535 $this->writeLog(4, 1, 120, 'Could not delete directory "%s"! Write-permission problem?', array($fileObject->getIdentifier()));
536 $this->addMessageToFlashMessageQueue('FileUtility.CouldNotDeleteDirectory', array($fileObject->getIdentifier()));
537 }
538 }
539 }
540
541 return $result;
542 }
543
544 /**
545 * Checks files in given folder recursively for for existing references.
546 *
547 * Creates a flash message if there are references.
548 *
549 * @param Folder $folder
550 * @return bool TRUE if folder has files in use, FALSE otherwise
551 */
552 public function folderHasFilesInUse(Folder $folder)
553 {
554 $files = $folder->getFiles(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true);
555 if (empty($files)) {
556 return false;
557 }
558
559 /** @var int[] $fileUids */
560 $fileUids = array();
561 foreach ($files as $file) {
562 $fileUids[] = $file->getUid();
563 }
564 $numberOfReferences = $this->getDatabaseConnection()->exec_SELECTcountRows(
565 '*',
566 'sys_refindex',
567 'deleted=0 AND ref_table="sys_file" AND ref_uid IN (' . implode(',', $fileUids) . ') AND tablename<>"sys_file_metadata"'
568 );
569
570 $hasReferences = $numberOfReferences > 0;
571 if ($hasReferences) {
572 /** @var FlashMessage $flashMessage */
573 $flashMessage = GeneralUtility::makeInstance(
574 FlashMessage::class,
575 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences'),
576 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences'),
577 FlashMessage::WARNING,
578 true
579 );
580 $this->addFlashMessage($flashMessage);
581 }
582
583 return $hasReferences;
584 }
585
586 /**
587 * Maps results from the fal file reference table on the
588 * structure of the normal reference index table.
589 *
590 * @param array $referenceRecord
591 * @return array
592 */
593 protected function transformFileReferenceToRecordReference(array $referenceRecord)
594 {
595 $fileReference = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
596 '*',
597 'sys_file_reference',
598 'uid=' . (int)$referenceRecord['recuid']
599 );
600 return array(
601 'recuid' => $fileReference['uid_foreign'],
602 'tablename' => $fileReference['tablenames'],
603 'field' => $fileReference['fieldname'],
604 'flexpointer' => '',
605 'softref_key' => '',
606 'sorting' => $fileReference['sorting_foreign']
607 );
608 }
609
610 /**
611 * Gets a File or a Folder object from an identifier [storage]:[fileId]
612 *
613 * @param string $identifier
614 * @return File|Folder
615 * @throws Exception\InsufficientFileAccessPermissionsException
616 * @throws Exception\InvalidFileException
617 */
618 protected function getFileObject($identifier)
619 {
620 $object = $this->fileFactory->retrieveFileOrFolderObject($identifier);
621 if (!is_object($object)) {
622 throw new \TYPO3\CMS\Core\Resource\Exception\InvalidFileException('The item ' . $identifier . ' was not a file or directory!!', 1320122453);
623 }
624 if ($object->getStorage()->getUid() === 0) {
625 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889830);
626 }
627 return $object;
628 }
629
630 /**
631 * Copying files and folders (action=2)
632 *
633 * $cmds['data'] (string): The file/folder to copy
634 * + example "4:mypath/tomyfolder/myfile.jpg")
635 * + for backwards compatibility: the identifier was the path+filename
636 * $cmds['target'] (string): The path where to copy to.
637 * + example "2:targetpath/targetfolder/"
638 * $cmds['altName'] (string): Use an alternative name if the target already exists
639 *
640 * @param array $cmds Command details as described above
641 * @return \TYPO3\CMS\Core\Resource\File
642 */
643 protected function func_copy($cmds)
644 {
645 if (!$this->isInit) {
646 return false;
647 }
648 $sourceFileObject = $this->getFileObject($cmds['data']);
649 /** @var $targetFolderObject \TYPO3\CMS\Core\Resource\Folder */
650 $targetFolderObject = $this->getFileObject($cmds['target']);
651 // Basic check
652 if (!$targetFolderObject instanceof Folder) {
653 $this->writeLog(2, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
654 $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', array($cmds['target']));
655 return false;
656 }
657 // If this is TRUE, we append _XX to the file name if
658 $appendSuffixOnConflict = (string)$cmds['altName'];
659 $resultObject = null;
660 $conflictMode = $appendSuffixOnConflict !== '' ? DuplicationBehavior::RENAME : DuplicationBehavior::CANCEL;
661 // Copying the file
662 if ($sourceFileObject instanceof File) {
663 try {
664 $resultObject = $sourceFileObject->copyTo($targetFolderObject, null, $conflictMode);
665 } catch (InsufficientUserPermissionsException $e) {
666 $this->writeLog(2, 1, 114, 'You are not allowed to copy files', '');
667 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCopyFiles');
668 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
669 $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"', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
670 $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
671 } catch (IllegalFileExtensionException $e) {
672 $this->writeLog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
673 $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
674 } catch (ExistingTargetFileNameException $e) {
675 $this->writeLog(2, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
676 $this->addMessageToFlashMessageQueue('FileUtility.FileAlreadyExistsInFolder', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
677 } catch (\BadMethodCallException $e) {
678 $this->writeLog(3, 1, 128, 'The function to copy a file between storages is not yet implemented', array());
679 $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToCopyAFileBetweenStoragesIsNotYetImplemented');
680 } catch (\RuntimeException $e) {
681 $this->writeLog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
682 $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotCopiedTo', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
683 }
684 if ($resultObject) {
685 $this->writeLog(2, 0, 1, 'File "%s" copied to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
686 $this->addMessageToFlashMessageQueue('FileUtility.FileCopiedTo', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()), FlashMessage::OK);
687 }
688 } else {
689 // Else means this is a Folder
690 $sourceFolderObject = $sourceFileObject;
691 try {
692 $resultObject = $sourceFolderObject->copyTo($targetFolderObject, null, $conflictMode);
693 } catch (InsufficientUserPermissionsException $e) {
694 $this->writeLog(2, 1, 125, 'You are not allowed to copy directories', '');
695 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCopyDirectories');
696 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
697 $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"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
698 $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
699 } catch (InsufficientFolderAccessPermissionsException $e) {
700 $this->writeLog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
701 $this->addMessageToFlashMessageQueue('FileUtility.YouDontHaveFullAccessToTheDestinationDirectory', array($targetFolderObject->getIdentifier()));
702 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
703 $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!', array($sourceFolderObject->getName(), $targetFolderObject->getName()));
704 $this->addMessageToFlashMessageQueue('FileUtility.CannotCopyFolderIntoTargetFolderBecauseTheTargetFolderIsAlreadyWithinTheFolderToBeCopied', array($sourceFolderObject->getName(), $targetFolderObject->getName()));
705 } catch (ExistingTargetFolderException $e) {
706 $this->writeLog(2, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
707 $this->addMessageToFlashMessageQueue('FileUtility.TargetAlreadyExists', array($targetFolderObject->getIdentifier()));
708 } catch (\BadMethodCallException $e) {
709 $this->writeLog(3, 1, 129, 'The function to copy a folder between storages is not yet implemented', array());
710 $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToCopyAFolderBetweenStoragesIsNotYetImplemented');
711 } catch (\RuntimeException $e) {
712 $this->writeLog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
713 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotCopiedTo', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
714 }
715 if ($resultObject) {
716 $this->writeLog(2, 0, 2, 'Directory "%s" copied to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
717 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryCopiedTo', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()), FlashMessage::OK);
718 }
719 }
720 return $resultObject;
721 }
722
723 /**
724 * Moving files and folders (action=3)
725 *
726 * $cmds['data'] (string): The file/folder to move
727 * + example "4:mypath/tomyfolder/myfile.jpg")
728 * + for backwards compatibility: the identifier was the path+filename
729 * $cmds['target'] (string): The path where to move to.
730 * + example "2:targetpath/targetfolder/"
731 * $cmds['altName'] (string): Use an alternative name if the target already exists
732 *
733 * @param array $cmds Command details as described above
734 * @return \TYPO3\CMS\Core\Resource\File
735 */
736 protected function func_move($cmds)
737 {
738 if (!$this->isInit) {
739 return false;
740 }
741 $sourceFileObject = $this->getFileObject($cmds['data']);
742 $targetFolderObject = $this->getFileObject($cmds['target']);
743 // Basic check
744 if (!$targetFolderObject instanceof Folder) {
745 $this->writeLog(3, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
746 $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', array($cmds['target']));
747 return false;
748 }
749 $alternativeName = (string)$cmds['altName'];
750 $resultObject = null;
751 // Moving the file
752 if ($sourceFileObject instanceof File) {
753 try {
754 if ($alternativeName !== '') {
755 // Don't allow overwriting existing files, but find a new name
756 $resultObject = $sourceFileObject->moveTo($targetFolderObject, $alternativeName, DuplicationBehavior::RENAME);
757 } else {
758 // Don't allow overwriting existing files
759 $resultObject = $sourceFileObject->moveTo($targetFolderObject, null, DuplicationBehavior::CANCEL);
760 }
761 $this->writeLog(3, 0, 1, 'File "%s" moved to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
762 $this->addMessageToFlashMessageQueue('FileUtility.FileMovedTo', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()), FlashMessage::OK);
763 } catch (InsufficientUserPermissionsException $e) {
764 $this->writeLog(3, 1, 114, 'You are not allowed to move files', '');
765 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToMoveFiles');
766 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
767 $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"', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
768 $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
769 } catch (IllegalFileExtensionException $e) {
770 $this->writeLog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
771 $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
772 } catch (ExistingTargetFileNameException $e) {
773 $this->writeLog(3, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
774 $this->addMessageToFlashMessageQueue('FileUtility.FileAlreadyExistsInFolder', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
775 } catch (\BadMethodCallException $e) {
776 $this->writeLog(3, 1, 126, 'The function to move a file between storages is not yet implemented', array());
777 $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToMoveAFileBetweenStoragesIsNotYetImplemented');
778 } catch (\RuntimeException $e) {
779 $this->writeLog(3, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
780 $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotCopiedTo', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
781 }
782 } else {
783 // Else means this is a Folder
784 $sourceFolderObject = $sourceFileObject;
785 try {
786 if ($alternativeName !== '') {
787 // Don't allow overwriting existing files, but find a new name
788 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, $alternativeName, DuplicationBehavior::RENAME);
789 } else {
790 // Don't allow overwriting existing files
791 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, null, DuplicationBehavior::RENAME);
792 }
793 $this->writeLog(3, 0, 2, 'Directory "%s" moved to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
794 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryMovedTo', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()), FlashMessage::OK);
795 } catch (InsufficientUserPermissionsException $e) {
796 $this->writeLog(3, 1, 125, 'You are not allowed to move directories', '');
797 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToMoveDirectories');
798 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
799 $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"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
800 $this->addMessageToFlashMessageQueue('FileUtility.CouldNotAccessAllNecessaryResources', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
801 } catch (InsufficientFolderAccessPermissionsException $e) {
802 $this->writeLog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
803 $this->addMessageToFlashMessageQueue('FileUtility.YouDontHaveFullAccessToTheDestinationDirectory', array($targetFolderObject->getIdentifier()));
804 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
805 $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!', array($sourceFolderObject->getName(), $targetFolderObject->getName()));
806 $this->addMessageToFlashMessageQueue('FileUtility.CannotMoveFolderIntoTargetFolderBecauseTheTargetFolderIsAlreadyWithinTheFolderToBeMoved', array($sourceFolderObject->getName(), $targetFolderObject->getName()));
807 } catch (ExistingTargetFolderException $e) {
808 $this->writeLog(3, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
809 $this->addMessageToFlashMessageQueue('FileUtility.TargetAlreadyExists', array($targetFolderObject->getIdentifier()));
810 } catch (\BadMethodCallException $e) {
811 $this->writeLog(3, 1, 127, 'The function to move a folder between storages is not yet implemented', array());
812 $this->addMessageToFlashMessageQueue('FileUtility.TheFunctionToMoveAFolderBetweenStoragesIsNotYetImplemented', array());
813 } catch (\RuntimeException $e) {
814 $this->writeLog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
815 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotMovedTo', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
816 }
817 }
818 return $resultObject;
819 }
820
821 /**
822 * Renaming files or foldes (action=5)
823 *
824 * $cmds['data'] (string): The file/folder to copy
825 * + example "4:mypath/tomyfolder/myfile.jpg")
826 * + for backwards compatibility: the identifier was the path+filename
827 * $cmds['target'] (string): New name of the file/folder
828 *
829 * @param array $cmds Command details as described above
830 * @return \TYPO3\CMS\Core\Resource\File Returns the new file upon success
831 */
832 public function func_rename($cmds)
833 {
834 if (!$this->isInit) {
835 return false;
836 }
837 $sourceFileObject = $this->getFileObject($cmds['data']);
838 $sourceFile = $sourceFileObject->getName();
839 $targetFile = $cmds['target'];
840 $resultObject = null;
841 if ($sourceFileObject instanceof File) {
842 try {
843 // Try to rename the File
844 $resultObject = $sourceFileObject->rename($targetFile);
845 $this->writeLog(5, 0, 1, 'File renamed from "%s" to "%s"', array($sourceFile, $targetFile));
846 $this->addMessageToFlashMessageQueue('FileUtility.FileRenamedFromTo', array($sourceFile, $targetFile), FlashMessage::OK);
847 } catch (InsufficientUserPermissionsException $e) {
848 $this->writeLog(5, 1, 102, 'You are not allowed to rename files!', '');
849 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToRenameFiles');
850 } catch (IllegalFileExtensionException $e) {
851 $this->writeLog(5, 1, 101, 'Extension of file name "%s" or "%s" was not allowed!', array($sourceFileObject->getName(), $targetFile));
852 $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameOrWasNotAllowed', array($sourceFileObject->getName(), $targetFile));
853 } catch (ExistingTargetFileNameException $e) {
854 $this->writeLog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
855 $this->addMessageToFlashMessageQueue('FileUtility.DestinationExistedAlready', array($targetFile));
856 } catch (NotInMountPointException $e) {
857 $this->writeLog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
858 $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', array($targetFile));
859 } catch (\RuntimeException $e) {
860 $this->writeLog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
861 $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotRenamed', array($sourceFileObject->getName(), $targetFile));
862 }
863 } else {
864 // Else means this is a Folder
865 try {
866 // Try to rename the Folder
867 $resultObject = $sourceFileObject->rename($targetFile);
868 $this->writeLog(5, 0, 2, 'Directory renamed from "%s" to "%s"', array($sourceFile, $targetFile));
869 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryRenamedFromTo', array($sourceFile, $targetFile), FlashMessage::OK);
870 } catch (InsufficientUserPermissionsException $e) {
871 $this->writeLog(5, 1, 111, 'You are not allowed to rename directories!', '');
872 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToRenameDirectories');
873 } catch (ExistingTargetFileNameException $e) {
874 $this->writeLog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
875 $this->addMessageToFlashMessageQueue('FileUtility.DestinationExistedAlready', array($targetFile));
876 } catch (NotInMountPointException $e) {
877 $this->writeLog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
878 $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', array($targetFile));
879 } catch (\RuntimeException $e) {
880 $this->writeLog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
881 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryWasNotRenamed', array($sourceFileObject->getName(), $targetFile));
882 }
883 }
884 return $resultObject;
885 }
886
887 /**
888 * This creates a new folder. (action=6)
889 *
890 * $cmds['data'] (string): The new folder name
891 * $cmds['target'] (string): The path where to copy to.
892 * + example "2:targetpath/targetfolder/"
893 *
894 * @param array $cmds Command details as described above
895 * @return \TYPO3\CMS\Core\Resource\Folder Returns the new foldername upon success
896 */
897 public function func_newfolder($cmds)
898 {
899 if (!$this->isInit) {
900 return false;
901 }
902 $targetFolderObject = $this->getFileObject($cmds['target']);
903 if (!$targetFolderObject instanceof Folder) {
904 $this->writeLog(6, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
905 $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', array($cmds['target']));
906 return false;
907 }
908 $resultObject = null;
909 try {
910 $folderName = $cmds['data'];
911 $resultObject = $targetFolderObject->createFolder($folderName);
912 $this->writeLog(6, 0, 1, 'Directory "%s" created in "%s"', array($folderName, $targetFolderObject->getIdentifier()));
913 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryCreatedIn', array($folderName, $targetFolderObject->getIdentifier()), FlashMessage::OK);
914 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException $e) {
915 $this->writeLog(6, 1, 104, 'Invalid folder name "%s"!', [$folderName]);
916 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateDirectories', [$folderName]);
917 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
918 $this->writeLog(6, 1, 103, 'You are not allowed to create directories!', '');
919 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateDirectories');
920 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
921 $this->writeLog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
922 $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', array($targetFolderObject->getIdentifier()));
923 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
924 $this->writeLog(6, 1, 101, 'File or directory "%s" existed already!', array($folderName));
925 $this->addMessageToFlashMessageQueue('FileUtility.FileOrDirectoryExistedAlready', array($folderName));
926 } catch (\RuntimeException $e) {
927 $this->writeLog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', array($folderName, $targetFolderObject->getIdentifier()));
928 $this->addMessageToFlashMessageQueue('FileUtility.DirectoryNotCreated', array($folderName, $targetFolderObject->getIdentifier()));
929 }
930 return $resultObject;
931 }
932
933 /**
934 * This creates a new file. (action=8)
935 * $cmds['data'] (string): The new file name
936 * $cmds['target'] (string): The path where to create it.
937 * + example "2:targetpath/targetfolder/"
938 *
939 * @param array $cmds Command details as described above
940 * @return string Returns the new filename upon success
941 */
942 public function func_newfile($cmds)
943 {
944 if (!$this->isInit) {
945 return false;
946 }
947 $targetFolderObject = $this->getFileObject($cmds['target']);
948 if (!$targetFolderObject instanceof Folder) {
949 $this->writeLog(8, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
950 $this->addMessageToFlashMessageQueue('FileUtility.DestinationWasNotADirectory', array($cmds['target']));
951 return false;
952 }
953 $resultObject = null;
954 $fileName = $cmds['data'];
955 try {
956 $resultObject = $targetFolderObject->createFile($fileName);
957 $this->writeLog(8, 0, 1, 'File created: "%s"', array($fileName));
958 $this->addMessageToFlashMessageQueue('FileUtility.FileCreated', array($fileName), FlashMessage::OK);
959 } catch (IllegalFileExtensionException $e) {
960 $this->writeLog(8, 1, 106, 'Extension of file "%s" was not allowed!', array($fileName));
961 $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileWasNotAllowed', array($fileName));
962 } catch (InsufficientFolderWritePermissionsException $e) {
963 $this->writeLog(8, 1, 103, 'You are not allowed to create files!', '');
964 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToCreateFiles');
965 } catch (NotInMountPointException $e) {
966 $this->writeLog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
967 $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', array($targetFolderObject->getIdentifier()));
968 } catch (ExistingTargetFileNameException $e) {
969 $this->writeLog(8, 1, 101, 'File existed already in "%s"!', array($targetFolderObject->getIdentifier()));
970 $this->addMessageToFlashMessageQueue('FileUtility.FileExistedAlreadyIn', array($targetFolderObject->getIdentifier()));
971 } catch (InvalidFileNameException $e) {
972 $this->writeLog(8, 1, 106, 'File name "%s" was not allowed!', $fileName);
973 $this->addMessageToFlashMessageQueue('FileUtility.FileNameWasNotAllowed', $fileName);
974 } catch (\RuntimeException $e) {
975 $this->writeLog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', array($fileName, $targetFolderObject->getIdentifier()));
976 $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotCreated', array($fileName, $targetFolderObject->getIdentifier()));
977 }
978 return $resultObject;
979 }
980
981 /**
982 * Editing textfiles or folders (action=9)
983 *
984 * @param array $cmds $cmds['data'] is the new content. $cmds['target'] is the target (file or dir)
985 * @return bool Returns TRUE on success
986 */
987 public function func_edit($cmds)
988 {
989 if (!$this->isInit) {
990 return false;
991 }
992 // Example indentifier for $cmds['target'] => "4:mypath/tomyfolder/myfile.jpg"
993 // for backwards compatibility: the combined file identifier was the path+filename
994 $fileIdentifier = $cmds['target'];
995 $fileObject = $this->getFileObject($fileIdentifier);
996 // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
997 $content = $cmds['data'];
998 if (!$fileObject instanceof File) {
999 $this->writeLog(9, 2, 123, 'Target "%s" was not a file!', array($fileIdentifier));
1000 $this->addMessageToFlashMessageQueue('FileUtility.TargetWasNotAFile', array($fileIdentifier));
1001 return false;
1002 }
1003 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
1004 if (!GeneralUtility::inList($extList, $fileObject->getExtension())) {
1005 $this->writeLog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', array($fileObject->getExtension(), $extList));
1006 $this->addMessageToFlashMessageQueue('FileUtility.FileExtensionIsNotATextfileFormat', array($fileObject->getExtension(), $extList));
1007 return false;
1008 }
1009 try {
1010 $fileObject->setContents($content);
1011 clearstatcache();
1012 $this->writeLog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', array($fileObject->getIdentifier(), $fileObject->getSize(), md5($content)));
1013 $this->addMessageToFlashMessageQueue('FileUtility.FileSavedToBytesMd5', array($fileObject->getIdentifier(), $fileObject->getSize(), md5($content)), FlashMessage::OK);
1014 return true;
1015 } catch (InsufficientUserPermissionsException $e) {
1016 $this->writeLog(9, 1, 104, 'You are not allowed to edit files!', '');
1017 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToEditFiles');
1018 return false;
1019 } catch (InsufficientFileWritePermissionsException $e) {
1020 $this->writeLog(9, 1, 100, 'File "%s" was not saved! Write-permission problem?', array($fileObject->getIdentifier()));
1021 $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotSaved', array($fileObject->getIdentifier()));
1022 return false;
1023 } catch (IllegalFileExtensionException $e) {
1024 $this->writeLog(9, 1, 100, 'File "%s" was not saved! File extension rejected!', array($fileObject->getIdentifier()));
1025 $this->addMessageToFlashMessageQueue('FileUtility.FileWasNotSaved', array($fileObject->getIdentifier()));
1026 return false;
1027 }
1028 }
1029
1030 /**
1031 * Upload of files (action=1)
1032 * when having multiple uploads (HTML5-style), the array $_FILES looks like this:
1033 * Array(
1034 * [upload_1] => Array(
1035 * [name] => Array(
1036 * [0] => GData - Content-Elements and Media-Gallery.pdf
1037 * [1] => CMS Expo 2011.txt
1038 * )
1039 * [type] => Array(
1040 * [0] => application/pdf
1041 * [1] => text/plain
1042 * )
1043 * [tmp_name] => Array(
1044 * [0] => /Applications/MAMP/tmp/php/phpNrOB43
1045 * [1] => /Applications/MAMP/tmp/php/phpD2HQAK
1046 * )
1047 * [size] => Array(
1048 * [0] => 373079
1049 * [1] => 1291
1050 * )
1051 * )
1052 * )
1053 * in HTML you'd need sth like this: <input type="file" name="upload_1[]" multiple="true" />
1054 *
1055 * @param array $cmds $cmds['data'] is the ID-number (points to the global var that holds the filename-ref
1056 * ($_FILES['upload_' . $id]['name']) . $cmds['target'] is the target directory, $cmds['charset']
1057 * is the the character set of the file name (utf-8 is needed for JS-interaction)
1058 * @return File[] | FALSE Returns an array of new file objects upon success. False otherwise
1059 */
1060 public function func_upload($cmds)
1061 {
1062 if (!$this->isInit) {
1063 return false;
1064 }
1065 $uploadPosition = $cmds['data'];
1066 $uploadedFileData = $_FILES['upload_' . $uploadPosition];
1067 if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
1068 $this->writeLog(1, 2, 108, 'No file was uploaded!', '');
1069 $this->addMessageToFlashMessageQueue('FileUtility.NoFileWasUploaded');
1070 return false;
1071 }
1072 // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
1073 $targetFolderObject = $this->getFileObject($cmds['target']);
1074 // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
1075 if (!is_array($uploadedFileData['name'])) {
1076 $uploadedFileData = array(
1077 'name' => array($uploadedFileData['name']),
1078 'type' => array($uploadedFileData['type']),
1079 'tmp_name' => array($uploadedFileData['tmp_name']),
1080 'size' => array($uploadedFileData['size'])
1081 );
1082 }
1083 $resultObjects = array();
1084 $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
1085 // Loop through all uploaded files
1086 for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
1087 $fileInfo = array(
1088 'name' => $uploadedFileData['name'][$i],
1089 'type' => $uploadedFileData['type'][$i],
1090 'tmp_name' => $uploadedFileData['tmp_name'][$i],
1091 'size' => $uploadedFileData['size'][$i]
1092 );
1093 try {
1094 /** @var $fileObject File */
1095 $fileObject = $targetFolderObject->addUploadedFile($fileInfo, (string)$this->existingFilesConflictMode);
1096 $fileObject = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
1097 if ($this->existingFilesConflictMode->equals(DuplicationBehavior::REPLACE)) {
1098 $this->getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
1099 }
1100 $resultObjects[] = $fileObject;
1101 $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
1102 $this->writeLog(1, 0, 1, 'Uploading file "%s" to "%s"', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
1103 $this->addMessageToFlashMessageQueue('FileUtility.UploadingFileTo', array($fileInfo['name'], $targetFolderObject->getIdentifier()), FlashMessage::OK);
1104 } catch (InsufficientFileWritePermissionsException $e) {
1105 $this->writeLog(1, 1, 107, 'You are not allowed to override "%s"!', array($fileInfo['name']));
1106 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToOverride', array($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', array($fileInfo['name']));
1115 $this->addMessageToFlashMessageQueue('FileUtility.TheUploadedFileExceedsTheSize-limit', array($fileInfo['name']));
1116 } catch (InsufficientFolderWritePermissionsException $e) {
1117 $this->writeLog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
1118 $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', array($targetFolderObject->getIdentifier()));
1119 } catch (IllegalFileExtensionException $e) {
1120 $this->writeLog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
1121 $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
1122 } catch (ExistingTargetFileNameException $e) {
1123 $this->writeLog(1, 1, 101, 'No unique filename available in "%s"!', array($targetFolderObject->getIdentifier()));
1124 $this->addMessageToFlashMessageQueue('FileUtility.NoUniqueFilenameAvailableIn', array($targetFolderObject->getIdentifier()));
1125 } catch (\RuntimeException $e) {
1126 $this->writeLog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', array($targetFolderObject->getIdentifier()));
1127 $this->addMessageToFlashMessageQueue('FileUtility.UploadedFileCouldNotBeMoved', array($targetFolderObject->getIdentifier()));
1128 }
1129 }
1130
1131 return $resultObjects;
1132 }
1133
1134 /**
1135 * Replaces a file on the filesystem and changes the identifier of the persisted file object in sys_file if
1136 * keepFilename is not checked. If keepFilename is checked, only the file content will be replaced.
1137 *
1138 * @param array $cmdArr
1139 * @return array|bool
1140 * @throws Exception\InsufficientFileAccessPermissionsException
1141 * @throws Exception\InvalidFileException
1142 * @throws \RuntimeException
1143 */
1144 protected function replaceFile(array $cmdArr)
1145 {
1146 if (!$this->isInit) {
1147 return false;
1148 }
1149
1150 $uploadPosition = $cmdArr['data'];
1151 $fileInfo = $_FILES['replace_' . $uploadPosition];
1152 if (empty($fileInfo['name'])) {
1153 $this->writeLog(1, 2, 108, 'No file was uploaded for replacing!', '');
1154 $this->addMessageToFlashMessageQueue('FileUtility.NoFileWasUploadedForReplacing');
1155 return false;
1156 }
1157
1158 $keepFileName = ($cmdArr['keepFilename'] == 1) ? true : false;
1159 $resultObjects = array();
1160
1161 try {
1162 $fileObjectToReplace = $this->getFileObject($cmdArr['uid']);
1163 $folder = $fileObjectToReplace->getParentFolder();
1164 $resourceStorage = $fileObjectToReplace->getStorage();
1165
1166 $fileObject = $resourceStorage->addUploadedFile($fileInfo, $folder, $fileObjectToReplace->getName(), DuplicationBehavior::REPLACE);
1167
1168 // Check if there is a file that is going to be uploaded that has a different name as the replacing one
1169 // but exists in that folder as well.
1170 // rename to another name, but check if the name is already given
1171 if ($keepFileName === false) {
1172 // if a file with the same name already exists, we need to change it to _01 etc.
1173 // if the file does not exist, we can do a simple rename
1174 $resourceStorage->moveFile($fileObject, $folder, $fileInfo['name'], DuplicationBehavior::RENAME);
1175 }
1176
1177 $resultObjects[] = $fileObject;
1178 $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
1179
1180 $this->writeLog(1, 0, 1, 'Replacing file "%s" to "%s"', array($fileInfo['name'], $fileObjectToReplace->getIdentifier()));
1181 $this->addMessageToFlashMessageQueue('FileUtility.ReplacingFileTo', array($fileInfo['name'], $fileObjectToReplace->getIdentifier()), FlashMessage::OK);
1182 } catch (InsufficientFileWritePermissionsException $e) {
1183 $this->writeLog(1, 1, 107, 'You are not allowed to override "%s"!', array($fileInfo['name']));
1184 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToOverride', array($fileInfo['name']));
1185 } catch (UploadException $e) {
1186 $this->writeLog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
1187 $this->addMessageToFlashMessageQueue('FileUtility.TheUploadHasFailedNoUploadedFileFound');
1188 } catch (InsufficientUserPermissionsException $e) {
1189 $this->writeLog(1, 1, 105, 'You are not allowed to upload files!', '');
1190 $this->addMessageToFlashMessageQueue('FileUtility.YouAreNotAllowedToUploadFiles');
1191 } catch (UploadSizeException $e) {
1192 $this->writeLog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', array($fileInfo['name']));
1193 $this->addMessageToFlashMessageQueue('FileUtility.TheUploadedFileExceedsTheSize-limit', array($fileInfo['name']));
1194 } catch (InsufficientFolderWritePermissionsException $e) {
1195 $this->writeLog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', array($fileObjectToReplace->getIdentifier()));
1196 $this->addMessageToFlashMessageQueue('FileUtility.DestinationPathWasNotWithinYourMountpoints', array($fileObjectToReplace->getIdentifier()));
1197 } catch (IllegalFileExtensionException $e) {
1198 $this->writeLog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', array($fileInfo['name'], $fileObjectToReplace->getIdentifier()));
1199 $this->addMessageToFlashMessageQueue('FileUtility.ExtensionOfFileNameIsNotAllowedIn', array($fileInfo['name'], $fileObjectToReplace->getIdentifier()));
1200 } catch (ExistingTargetFileNameException $e) {
1201 $this->writeLog(1, 1, 101, 'No unique filename available in "%s"!', array($fileObjectToReplace->getIdentifier()));
1202 $this->addMessageToFlashMessageQueue('FileUtility.NoUniqueFilenameAvailableIn', array($fileObjectToReplace->getIdentifier()));
1203 } catch (\RuntimeException $e) {
1204 throw $e;
1205 }
1206 return $resultObjects;
1207 }
1208
1209 /**
1210 * Add flash message to message queue
1211 *
1212 * @param FlashMessage $flashMessage
1213 * @return void
1214 */
1215 protected function addFlashMessage(FlashMessage $flashMessage)
1216 {
1217 /** @var $flashMessageService FlashMessageService */
1218 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1219
1220 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
1221 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1222 $defaultFlashMessageQueue->enqueue($flashMessage);
1223 }
1224
1225 /**
1226 * Gets Indexer
1227 *
1228 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storage
1229 * @return \TYPO3\CMS\Core\Resource\Index\Indexer
1230 */
1231 protected function getIndexer(ResourceStorage $storage)
1232 {
1233 return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\Indexer::class, $storage);
1234 }
1235
1236 /**
1237 * Get database connection
1238 *
1239 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
1240 */
1241 protected function getDatabaseConnection()
1242 {
1243 return $GLOBALS['TYPO3_DB'];
1244 }
1245
1246 /**
1247 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1248 */
1249 protected function getBackendUser()
1250 {
1251 return $GLOBALS['BE_USER'];
1252 }
1253
1254 /**
1255 * Returns LanguageService
1256 *
1257 * @return LanguageService
1258 */
1259 protected function getLanguageService()
1260 {
1261 return $GLOBALS['LANG'];
1262 }
1263 }