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