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