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