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