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