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