726ddde9ff23305e7e27a6a96c5261bc3bab3458
[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 'unzip':
249 $result[$action][] = $this->func_unzip($cmdArr);
250 break;
251 }
252 // Hook for post-processing the action
253 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'])) {
254 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_extfilefunc.php']['processData'] as $classRef) {
255 $hookObject = GeneralUtility::getUserObj($classRef);
256 if (!$hookObject instanceof ExtendedFileUtilityProcessDataHookInterface) {
257 throw new \UnexpectedValueException('$hookObject must implement interface TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtilityProcessDataHookInterface', 1279719168);
258 }
259 $hookObject->processData_postProcessAction($action, $cmdArr, $result[$action], $this);
260 }
261 }
262 }
263 }
264 }
265 }
266 return $result;
267 }
268
269 /**
270 * Adds all log error messages from the operations of this script instance to the FlashMessageQueue
271 *
272 * @return void
273 */
274 public function pushErrorMessagesToFlashMessageQueue() {
275 foreach ($this->getErrorMessages() as $msg) {
276 $flashMessage = GeneralUtility::makeInstance(
277 FlashMessage::class,
278 $msg,
279 '',
280 FlashMessage::ERROR,
281 TRUE
282 );
283 $this->addFlashMessage($flashMessage);
284 }
285 }
286
287 /**
288 * Return all error messages from the file operations of this script instance
289 *
290 * @return array all errorMessages as a numerical array
291 */
292 public function getErrorMessages() {
293 return $this->errorMessages;
294 }
295
296 /**
297 * @param int $action The action number. See the functions in the class for a hint. Eg. edit is '9', upload is '1' ...
298 * @param int $error The severity: 0 = message, 1 = error, 2 = System Error, 3 = security notice (admin)
299 * @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.
300 * @param string $details This is the default, raw error message in english
301 * @param array $data Array with special information that may go into $details by "%s" marks / sprintf() when the log is shown
302 * @return void
303 */
304 public function writeLog($action, $error, $details_nr, $details, $data) {
305 // Type value for tce_file.php
306 $type = 2;
307 if (is_object($this->getBackendUser())) {
308 $this->getBackendUser()->writelog($type, $action, $error, $details_nr, $details, $data);
309 }
310 if ($error > 0) {
311 $this->lastError = vsprintf($details, $data);
312 $this->errorMessages[] = $this->lastError;
313 }
314 }
315
316 /*************************************
317 *
318 * File operation functions
319 *
320 **************************************/
321 /**
322 * Deleting files and folders (action=4)
323 *
324 * @param array $cmds $cmds['data'] is the file/folder to delete
325 * @return bool Returns TRUE upon success
326 */
327 public function func_delete(array $cmds) {
328 $result = FALSE;
329 if (!$this->isInit) {
330 return $result;
331 }
332 // Example indentifier for $cmds['data'] => "4:mypath/tomyfolder/myfile.jpg"
333 // for backwards compatibility: the combined file identifier was the path+filename
334 try {
335 $fileObject = $this->getFileObject($cmds['data']);
336 } catch (ResourceDoesNotExistException $e) {
337 $flashMessage = GeneralUtility::makeInstance(
338 FlashMessage::class,
339 sprintf( $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileNotFound'), $cmds['data']),
340 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileNotFound'),
341 FlashMessage::ERROR,
342 TRUE
343 );
344 $this->addFlashMessage($flashMessage);
345
346 return FALSE;
347 }
348 // @todo implement the recycler feature which has been removed from the original implementation
349 // checks to delete the file
350 if ($fileObject instanceof File) {
351 // check if the file still has references
352 // Exclude sys_file_metadata records as these are no use references
353 $databaseConnection = $this->getDatabaseConnection();
354 $table = 'sys_refindex';
355 $refIndexRecords = $databaseConnection->exec_SELECTgetRows(
356 '*',
357 $table,
358 'deleted=0 AND ref_table=' . $databaseConnection->fullQuoteStr('sys_file', $table)
359 . ' AND ref_uid=' . (int)$fileObject->getUid()
360 . ' AND tablename != ' . $databaseConnection->fullQuoteStr('sys_file_metadata', $table)
361 );
362 $deleteFile = TRUE;
363 if (!empty($refIndexRecords)) {
364 $shortcutContent = array();
365 $brokenReferences = array();
366
367 foreach ($refIndexRecords as $fileReferenceRow) {
368 if ($fileReferenceRow['tablename'] === 'sys_file_reference') {
369 $row = $this->transformFileReferenceToRecordReference($fileReferenceRow);
370 $shortcutRecord = BackendUtility::getRecord($row['tablename'], $row['recuid']);
371
372 if ($shortcutRecord) {
373 $icon = IconUtility::getSpriteIconForRecord($row['tablename'], $shortcutRecord);
374 $tagParameters = array(
375 'class' => 't3-js-clickmenutrigger',
376 'data-table' => $row['tablename'],
377 'data-uid' => $row['recuid'],
378 'data-listframe' => 1,
379 'data-iteminfo' => '%2Binfo,history,edit'
380 );
381 $icon = '<a href="#" ' . GeneralUtility::implodeAttributes($tagParameters, TRUE) . '>' . $icon . '</a>';
382 $shortcutContent[] = $icon . htmlspecialchars((BackendUtility::getRecordTitle($row['tablename'], $shortcutRecord) . ' [' . BackendUtility::getRecordPath($shortcutRecord['pid'], '', 80) . ']'));
383 } else {
384 $brokenReferences[] = $fileReferenceRow['ref_uid'];
385 }
386 }
387 }
388 if (!empty($brokenReferences)) {
389 // render a message that the file has broken references
390 $flashMessage = GeneralUtility::makeInstance(
391 FlashMessage::class,
392 sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileHasBrokenReferences'), count($brokenReferences)),
393 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileHasBrokenReferences'),
394 FlashMessage::INFO,
395 TRUE
396 );
397 $this->addFlashMessage($flashMessage);
398 }
399 if (!empty($shortcutContent)) {
400 // render a message that the file could not be deleted
401 $flashMessage = GeneralUtility::makeInstance(
402 FlashMessage::class,
403 sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileNotDeletedHasReferences'), $fileObject->getName()) . '<br />' . implode('<br />', $shortcutContent),
404 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileNotDeletedHasReferences'),
405 FlashMessage::WARNING,
406 TRUE
407 );
408 $this->addFlashMessage($flashMessage);
409 $deleteFile = FALSE;
410 }
411 }
412
413 if ($deleteFile) {
414 try {
415 $result = $fileObject->delete();
416
417 // show the user that the file was deleted
418 $flashMessage = GeneralUtility::makeInstance(
419 FlashMessage::class,
420 sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.fileDeleted'), $fileObject->getName()),
421 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.fileDeleted'),
422 FlashMessage::OK,
423 TRUE
424 );
425 $this->addFlashMessage($flashMessage);
426 // Log success
427 $this->writelog(4, 0, 1, 'File "%s" deleted', array($fileObject->getIdentifier()));
428 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
429 $this->writelog(4, 1, 112, 'You are not allowed to access the file', array($fileObject->getIdentifier()));
430 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
431 $this->writelog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
432 } catch (\RuntimeException $e) {
433 $this->writelog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', array($fileObject->getIdentifier()));
434 }
435 }
436 } else {
437 /** @var Folder $fileObject */
438 if (!$this->folderHasFilesInUse($fileObject)) {
439 try {
440 $result = $fileObject->delete(TRUE);
441 if ($result) {
442 // notify the user that the folder was deleted
443 /** @var FlashMessage $flashMessage */
444 $flashMessage = GeneralUtility::makeInstance(
445 FlashMessage::class,
446 sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderDeleted'), $fileObject->getName()),
447 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderDeleted'),
448 FlashMessage::OK,
449 TRUE
450 );
451 $this->addFlashMessage($flashMessage);
452 // Log success
453 $this->writelog(4, 0, 3, 'Directory "%s" deleted', array($fileObject->getIdentifier()));
454 }
455 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
456 $this->writelog(4, 1, 120, 'Could not delete directory! Is directory "%s" empty? (You are not allowed to delete directories recursively).', array($fileObject->getIdentifier()));
457 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
458 $this->writelog(4, 1, 123, 'You are not allowed to access the directory', array($fileObject->getIdentifier()));
459 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
460 $this->writelog(4, 1, 121, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
461 } catch (\TYPO3\CMS\Core\Resource\Exception\FileOperationErrorException $e) {
462 $this->writelog(4, 1, 120, 'Could not delete directory "%s"! Write-permission problem?', array($fileObject->getIdentifier()));
463 }
464 }
465 }
466
467 return $result;
468 }
469
470 /**
471 * Checks files in given folder recursively for for existing references.
472 *
473 * Creates a flash message if there are references.
474 *
475 * @param Folder $folder
476 * @return bool TRUE if folder has files in use, FALSE otherwise
477 */
478 public function folderHasFilesInUse(Folder $folder) {
479 $files = $folder->getFiles(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, TRUE);
480 if (empty($files)) {
481 return FALSE;
482 }
483
484 /** @var int[] $fileUids */
485 $fileUids = array();
486 foreach ($files as $file) {
487 $fileUids[] = $file->getUid();
488 }
489 $numberOfReferences = $this->getDatabaseConnection()->exec_SELECTcountRows(
490 '*',
491 'sys_refindex',
492 'deleted=0 AND ref_table="sys_file" AND ref_uid IN (' . implode(',', $fileUids) . ') AND tablename<>"sys_file_metadata"'
493 );
494
495 $hasReferences = $numberOfReferences > 0;
496 if ($hasReferences) {
497 /** @var FlashMessage $flashMessage */
498 $flashMessage = GeneralUtility::makeInstance(
499 FlashMessage::class,
500 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderNotDeletedHasFilesWithReferences'),
501 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderNotDeletedHasFilesWithReferences'),
502 FlashMessage::WARNING,
503 TRUE
504 );
505 $this->addFlashMessage($flashMessage);
506 }
507
508 return $hasReferences;
509 }
510
511 /**
512 * Maps results from the fal file reference table on the
513 * structure of the normal reference index table.
514 *
515 * @param array $referenceRecord
516 * @return array
517 */
518 protected function transformFileReferenceToRecordReference(array $referenceRecord) {
519 $fileReference = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
520 '*',
521 'sys_file_reference',
522 'uid=' . (int)$referenceRecord['recuid']
523 );
524 return array(
525 'recuid' => $fileReference['uid_foreign'],
526 'tablename' => $fileReference['tablenames'],
527 'field' => $fileReference['fieldname'],
528 'flexpointer' => '',
529 'softref_key' => '',
530 'sorting' => $fileReference['sorting_foreign']
531 );
532 }
533
534 /**
535 * Gets a File or a Folder object from an identifier [storage]:[fileId]
536 *
537 * @param string $identifier
538 * @return \TYPO3\CMS\Core\Resource\Folder|\TYPO3\CMS\Core\Resource\File
539 * @throws \TYPO3\CMS\Core\Resource\Exception\InvalidFileException
540 */
541 protected function getFileObject($identifier) {
542 $object = $this->fileFactory->retrieveFileOrFolderObject($identifier);
543 if (!is_object($object)) {
544 throw new \TYPO3\CMS\Core\Resource\Exception\InvalidFileException('The item ' . $identifier . ' was not a file or directory!!', 1320122453);
545 }
546 if ($object->getStorage()->getUid() === 0) {
547 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889830);
548 }
549 return $object;
550 }
551
552 /**
553 * Copying files and folders (action=2)
554 *
555 * $cmds['data'] (string): The file/folder to copy
556 * + example "4:mypath/tomyfolder/myfile.jpg")
557 * + for backwards compatibility: the identifier was the path+filename
558 * $cmds['target'] (string): The path where to copy to.
559 * + example "2:targetpath/targetfolder/"
560 * $cmds['altName'] (string): Use an alternative name if the target already exists
561 *
562 * @param array $cmds Command details as described above
563 * @return \TYPO3\CMS\Core\Resource\File
564 */
565 protected function func_copy($cmds) {
566 if (!$this->isInit) {
567 return FALSE;
568 }
569 $sourceFileObject = $this->getFileObject($cmds['data']);
570 /** @var $targetFolderObject \TYPO3\CMS\Core\Resource\Folder */
571 $targetFolderObject = $this->getFileObject($cmds['target']);
572 // Basic check
573 if (!$targetFolderObject instanceof Folder) {
574 $this->writelog(2, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
575 return FALSE;
576 }
577 // If this is TRUE, we append _XX to the file name if
578 $appendSuffixOnConflict = (string)$cmds['altName'];
579 $resultObject = NULL;
580 // Copying the file
581 if ($sourceFileObject instanceof File) {
582 try {
583 $conflictMode = $appendSuffixOnConflict !== '' ? 'renameNewFile' : 'cancel';
584 $resultObject = $sourceFileObject->copyTo($targetFolderObject, NULL, $conflictMode);
585 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
586 $this->writelog(2, 1, 114, 'You are not allowed to copy files', '');
587 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
588 $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()));
589 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
590 $this->writelog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
591 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
592 $this->writelog(2, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
593 } catch (\BadMethodCallException $e) {
594 $this->writelog(3, 1, 128, 'The function to copy a file between storages is not yet implemented', array());
595 } catch (\RuntimeException $e) {
596 $this->writelog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
597 }
598 if ($resultObject) {
599 $this->writelog(2, 0, 1, 'File "%s" copied to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
600 }
601 } else {
602 // Else means this is a Folder
603 $sourceFolderObject = $sourceFileObject;
604 try {
605 $conflictMode = $appendSuffixOnConflict !== '' ? 'renameNewFile' : 'cancel';
606 $resultObject = $sourceFolderObject->copyTo($targetFolderObject, NULL, $conflictMode);
607 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
608 $this->writelog(2, 1, 125, 'You are not allowed to copy directories', '');
609 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
610 $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()));
611 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
612 $this->writelog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
613 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
614 $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()));
615 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
616 $this->writelog(2, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
617 } catch (\BadMethodCallException $e) {
618 $this->writelog(3, 1, 129, 'The function to copy a folder between storages is not yet implemented', array());
619 } catch (\RuntimeException $e) {
620 $this->writelog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
621 }
622 if ($resultObject) {
623 $this->writelog(2, 0, 2, 'Directory "%s" copied to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
624 }
625 }
626 return $resultObject;
627 }
628
629 /**
630 * Moving files and folders (action=3)
631 *
632 * $cmds['data'] (string): The file/folder to move
633 * + example "4:mypath/tomyfolder/myfile.jpg")
634 * + for backwards compatibility: the identifier was the path+filename
635 * $cmds['target'] (string): The path where to move to.
636 * + example "2:targetpath/targetfolder/"
637 * $cmds['altName'] (string): Use an alternative name if the target already exists
638 *
639 * @param array $cmds Command details as described above
640 * @return \TYPO3\CMS\Core\Resource\File
641 */
642 protected function func_move($cmds) {
643 if (!$this->isInit) {
644 return FALSE;
645 }
646 $sourceFileObject = $this->getFileObject($cmds['data']);
647 $targetFolderObject = $this->getFileObject($cmds['target']);
648 // Basic check
649 if (!$targetFolderObject instanceof Folder) {
650 $this->writelog(3, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
651 return FALSE;
652 }
653 $alternativeName = (string)$cmds['altName'];
654 $resultObject = NULL;
655 // Moving the file
656 if ($sourceFileObject instanceof File) {
657 try {
658 if ($alternativeName !== '') {
659 // Don't allow overwriting existing files, but find a new name
660 $resultObject = $sourceFileObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
661 } else {
662 // Don't allow overwriting existing files
663 $resultObject = $sourceFileObject->moveTo($targetFolderObject, NULL, 'cancel');
664 }
665 $this->writelog(3, 0, 1, 'File "%s" moved to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
666 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
667 $this->writelog(3, 1, 114, 'You are not allowed to move files', '');
668 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
669 $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()));
670 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
671 $this->writelog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
672 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
673 $this->writelog(3, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
674 } catch (\BadMethodCallException $e) {
675 $this->writelog(3, 1, 126, 'The function to move a file between storages is not yet implemented', array());
676 } catch (\RuntimeException $e) {
677 $this->writelog(3, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
678 }
679 } else {
680 // Else means this is a Folder
681 $sourceFolderObject = $sourceFileObject;
682 try {
683 if ($alternativeName !== '') {
684 // Don't allow overwriting existing files, but find a new name
685 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
686 } else {
687 // Don't allow overwriting existing files
688 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, NULL, 'renameNewFile');
689 }
690 $this->writelog(3, 0, 2, 'Directory "%s" moved to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
691 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
692 $this->writelog(3, 1, 125, 'You are not allowed to move directories', '');
693 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
694 $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()));
695 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
696 $this->writelog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
697 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
698 $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()));
699 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
700 $this->writelog(3, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
701 } catch (\BadMethodCallException $e) {
702 $this->writelog(3, 1, 127, 'The function to move a folder between storages is not yet implemented', array());
703 } catch (\RuntimeException $e) {
704 $this->writelog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
705 }
706 }
707 return $resultObject;
708 }
709
710 /**
711 * Renaming files or foldes (action=5)
712 *
713 * $cmds['data'] (string): The file/folder to copy
714 * + example "4:mypath/tomyfolder/myfile.jpg")
715 * + for backwards compatibility: the identifier was the path+filename
716 * $cmds['target'] (string): New name of the file/folder
717 *
718 * @param array $cmds Command details as described above
719 * @return \TYPO3\CMS\Core\Resource\File Returns the new file upon success
720 */
721 public function func_rename($cmds) {
722 if (!$this->isInit) {
723 return FALSE;
724 }
725 $sourceFileObject = $this->getFileObject($cmds['data']);
726 $targetFile = $cmds['target'];
727 $resultObject = NULL;
728 if ($sourceFileObject instanceof File) {
729 try {
730 // Try to rename the File
731 $resultObject = $sourceFileObject->rename($targetFile);
732 $this->writelog(5, 0, 1, 'File renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
733 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
734 $this->writelog(5, 1, 102, 'You are not allowed to rename files!', '');
735 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
736 $this->writelog(5, 1, 101, 'Extension of file name "%s" or "%s" was not allowed!', array($sourceFileObject->getName(), $targetFile));
737 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
738 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
739 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
740 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
741 } catch (\RuntimeException $e) {
742 $this->writelog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
743 }
744 } else {
745 // Else means this is a Folder
746 try {
747 // Try to rename the Folder
748 $resultObject = $sourceFileObject->rename($targetFile);
749 $this->writelog(5, 0, 2, 'Directory renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
750 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
751 $this->writelog(5, 1, 111, 'You are not allowed to rename directories!', '');
752 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
753 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
754 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
755 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
756 } catch (\RuntimeException $e) {
757 $this->writelog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
758 }
759 }
760 return $resultObject;
761 }
762
763 /**
764 * This creates a new folder. (action=6)
765 *
766 * $cmds['data'] (string): The new folder name
767 * $cmds['target'] (string): The path where to copy to.
768 * + example "2:targetpath/targetfolder/"
769 *
770 * @param array $cmds Command details as described above
771 * @return \TYPO3\CMS\Core\Resource\Folder Returns the new foldername upon success
772 */
773 public function func_newfolder($cmds) {
774 if (!$this->isInit) {
775 return FALSE;
776 }
777 $targetFolderObject = $this->getFileObject($cmds['target']);
778 if (!$targetFolderObject instanceof Folder) {
779 $this->writelog(6, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
780 return FALSE;
781 }
782 $resultObject = NULL;
783 try {
784 $folderName = $cmds['data'];
785 $resultObject = $targetFolderObject->createFolder($folderName);
786 $this->writelog(6, 0, 1, 'Directory "%s" created in "%s"', array($folderName, $targetFolderObject->getIdentifier() . '/'));
787 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
788 $this->writelog(6, 1, 103, 'You are not allowed to create directories!', '');
789 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
790 $this->writelog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier() . '/'));
791 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
792 $this->writelog(6, 1, 101, 'File or directory "%s" existed already!', array($folderName));
793 } catch (\RuntimeException $e) {
794 $this->writelog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', array($folderName, $targetFolderObject->getIdentifier() . '/'));
795 }
796 return $resultObject;
797 }
798
799 /**
800 * This creates a new file. (action=8)
801 * $cmds['data'] (string): The new file name
802 * $cmds['target'] (string): The path where to create it.
803 * + example "2:targetpath/targetfolder/"
804 *
805 * @param array $cmds Command details as described above
806 * @return string Returns the new filename upon success
807 */
808 public function func_newfile($cmds) {
809 if (!$this->isInit) {
810 return FALSE;
811 }
812 $targetFolderObject = $this->getFileObject($cmds['target']);
813 if (!$targetFolderObject instanceof Folder) {
814 $this->writelog(8, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
815 return FALSE;
816 }
817 $resultObject = NULL;
818 try {
819 $fileName = $cmds['data'];
820 $resultObject = $targetFolderObject->createFile($fileName);
821 $this->writelog(8, 0, 1, 'File created: "%s"', array($fileName));
822 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
823 $this->writeLog(8, 1, 106, 'Extension of file "%s" was not allowed!', array($fileName));
824 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
825 $this->writelog(8, 1, 103, 'You are not allowed to create files!', '');
826 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
827 $this->writelog(8, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
828 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
829 $this->writelog(8, 1, 101, 'File existed already in "%s"!', array($targetFolderObject->getIdentifier()));
830 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidFileNameException $e) {
831 $this->writelog(8, 1, 106, 'File name "%s" was not allowed!', $fileName);
832 } catch (\RuntimeException $e) {
833 $this->writelog(8, 1, 100, 'File "%s" was not created! Write-permission problem in "%s"?', array($fileName, $targetFolderObject->getIdentifier()));
834 }
835 return $resultObject;
836 }
837
838 /**
839 * Editing textfiles or folders (action=9)
840 *
841 * @param array $cmds $cmds['data'] is the new content. $cmds['target'] is the target (file or dir)
842 * @return bool Returns TRUE on success
843 */
844 public function func_edit($cmds) {
845 if (!$this->isInit) {
846 return FALSE;
847 }
848 // Example indentifier for $cmds['target'] => "4:mypath/tomyfolder/myfile.jpg"
849 // for backwards compatibility: the combined file identifier was the path+filename
850 $fileIdentifier = $cmds['target'];
851 $fileObject = $this->getFileObject($fileIdentifier);
852 // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
853 $content = $cmds['data'];
854 if (!$fileObject instanceof File) {
855 $this->writelog(9, 2, 123, 'Target "%s" was not a file!', array($fileIdentifier));
856 return FALSE;
857 }
858 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
859 if (!GeneralUtility::inList($extList, $fileObject->getExtension())) {
860 $this->writelog(9, 1, 102, 'File extension "%s" is not a textfile format! (%s)', array($fileObject->getExtension(), $extList));
861 return FALSE;
862 }
863 try {
864 $fileObject->setContents($content);
865 clearstatcache();
866 $this->writelog(9, 0, 1, 'File saved to "%s", bytes: %s, MD5: %s ', array($fileObject->getIdentifier(), $fileObject->getSize(), md5($content)));
867 return TRUE;
868 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
869 $this->writelog(9, 1, 104, 'You are not allowed to edit files!', '');
870 return FALSE;
871 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileWritePermissionsException $e) {
872 $this->writelog(9, 1, 100, 'File "%s" was not saved! Write-permission problem?', array($fileObject->getIdentifier()));
873 return FALSE;
874 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
875 $this->writelog(9, 1, 100, 'File "%s" was not saved! File extension rejected!', array($fileObject->getIdentifier()));
876 return FALSE;
877 }
878 }
879
880 /**
881 * Upload of files (action=1)
882 * when having multiple uploads (HTML5-style), the array $_FILES looks like this:
883 * Array(
884 * [upload_1] => Array(
885 * [name] => Array(
886 * [0] => GData - Content-Elements and Media-Gallery.pdf
887 * [1] => CMS Expo 2011.txt
888 * )
889 * [type] => Array(
890 * [0] => application/pdf
891 * [1] => text/plain
892 * )
893 * [tmp_name] => Array(
894 * [0] => /Applications/MAMP/tmp/php/phpNrOB43
895 * [1] => /Applications/MAMP/tmp/php/phpD2HQAK
896 * )
897 * [size] => Array(
898 * [0] => 373079
899 * [1] => 1291
900 * )
901 * )
902 * )
903 * in HTML you'd need sth like this: <input type="file" name="upload_1[]" multiple="true" />
904 *
905 * @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)
906 * @return File[] | FALSE Returns an array of new file objects upon success. False otherwise
907 */
908 public function func_upload($cmds) {
909 if (!$this->isInit) {
910 return FALSE;
911 }
912 $uploadPosition = $cmds['data'];
913 $uploadedFileData = $_FILES['upload_' . $uploadPosition];
914 if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
915 $this->writelog(1, 2, 108, 'No file was uploaded!', '');
916 return FALSE;
917 }
918 // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
919 $targetFolderObject = $this->getFileObject($cmds['target']);
920 // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
921 if (!is_array($uploadedFileData['name'])) {
922 $uploadedFileData = array(
923 'name' => array($uploadedFileData['name']),
924 'type' => array($uploadedFileData['type']),
925 'tmp_name' => array($uploadedFileData['tmp_name']),
926 'size' => array($uploadedFileData['size'])
927 );
928 }
929 $resultObjects = array();
930 $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
931 // Loop through all uploaded files
932 for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
933 $fileInfo = array(
934 'name' => $uploadedFileData['name'][$i],
935 'type' => $uploadedFileData['type'][$i],
936 'tmp_name' => $uploadedFileData['tmp_name'][$i],
937 'size' => $uploadedFileData['size'][$i]
938 );
939 try {
940 // @todo can be improved towards conflict mode naming
941 if ($this->dontCheckForUnique) {
942 $conflictMode = 'replace';
943 } else {
944 $conflictMode = 'cancel';
945 }
946 /** @var $fileObject File */
947 $fileObject = $targetFolderObject->addUploadedFile($fileInfo, $conflictMode);
948 $fileObject = ResourceFactory::getInstance()->getFileObjectByStorageAndIdentifier($targetFolderObject->getStorage()->getUid(), $fileObject->getIdentifier());
949 if ($conflictMode === 'replace') {
950 $this->getIndexer($fileObject->getStorage())->updateIndexEntry($fileObject);
951 }
952 $resultObjects[] = $fileObject;
953 $this->internalUploadMap[$uploadPosition] = $fileObject->getCombinedIdentifier();
954 $this->writelog(1, 0, 1, 'Uploading file "%s" to "%s"', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
955 } catch (\TYPO3\CMS\Core\Resource\Exception\UploadException $e) {
956 $this->writelog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
957 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
958 $this->writelog(1, 1, 105, 'You are not allowed to upload files!', '');
959 } catch (\TYPO3\CMS\Core\Resource\Exception\UploadSizeException $e) {
960 $this->writelog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', array($fileInfo['name']));
961 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
962 $this->writelog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
963 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
964 $this->writelog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
965 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
966 $this->writelog(1, 1, 101, 'No unique filename available in "%s"!', array($targetFolderObject->getIdentifier()));
967 } catch (\RuntimeException $e) {
968 $this->writelog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', array($targetFolderObject->getIdentifier()));
969 }
970 }
971
972 return $resultObjects;
973 }
974
975 /**
976 * Unzipping file (action=7)
977 * This is permitted only if the user has fullAccess or if the file resides
978 *
979 * @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.
980 * @return bool Returns TRUE on success
981 */
982 public function func_unzip($cmds) {
983 if (!$this->isInit || $this->dont_use_exec_commands) {
984 return FALSE;
985 }
986 $theFile = $cmds['data'];
987 if (!@is_file($theFile)) {
988 $this->writelog(7, 2, 105, 'The file "%s" did not exist!', array($theFile));
989 return FALSE;
990 }
991 $fI = GeneralUtility::split_fileref($theFile);
992 if (!isset($cmds['target'])) {
993 $cmds['target'] = $fI['path'];
994 }
995 // Clean up destination directory
996 // !!! Method has been put in the local driver, can be saftely removed
997 $theDest = $this->is_directory($cmds['target']);
998 if (!$theDest) {
999 $this->writelog(7, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
1000 return FALSE;
1001 }
1002 if (!$this->actionPerms['unzipFile']) {
1003 $this->writelog(7, 1, 103, 'You are not allowed to unzip files', '');
1004 return FALSE;
1005 }
1006 if ($fI['fileext'] != 'zip') {
1007 $this->writelog(7, 1, 102, 'File extension is not "zip"', '');
1008 return FALSE;
1009 }
1010 if (!$this->checkIfFullAccess($theDest)) {
1011 $this->writelog(7, 1, 101, 'You don\'t have full access to the destination directory "%s"!', array($theDest));
1012 return FALSE;
1013 }
1014 // !!! Method has been put in the sotrage driver, can be saftely removed
1015 if ($this->checkPathAgainstMounts($theFile) && $this->checkPathAgainstMounts($theDest . '/')) {
1016 // No way to do this under windows.
1017 $cmd = $this->unzipPath . 'unzip -qq ' . escapeshellarg($theFile) . ' -d ' . escapeshellarg($theDest);
1018 CommandUtility::exec($cmd);
1019 $this->writelog(7, 0, 1, 'Unzipping file "%s" in "%s"', array($theFile, $theDest));
1020 return TRUE;
1021 } else {
1022 $this->writelog(7, 1, 100, 'File "%s" or destination "%s" was not within your mountpoints!', array($theFile, $theDest));
1023 return FALSE;
1024 }
1025 }
1026
1027 /**
1028 * Add flash message to message queue
1029 *
1030 * @param FlashMessage $flashMessage
1031 * @return void
1032 */
1033 protected function addFlashMessage(FlashMessage $flashMessage) {
1034 /** @var $flashMessageService FlashMessageService */
1035 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1036
1037 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
1038 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1039 $defaultFlashMessageQueue->enqueue($flashMessage);
1040 }
1041
1042 /**
1043 * Gets Indexer
1044 *
1045 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storage
1046 * @return \TYPO3\CMS\Core\Resource\Index\Indexer
1047 */
1048 protected function getIndexer(ResourceStorage $storage) {
1049 return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\Indexer::class, $storage);
1050 }
1051
1052 /**
1053 * Get database connection
1054 *
1055 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
1056 */
1057 protected function getDatabaseConnection() {
1058 return $GLOBALS['TYPO3_DB'];
1059 }
1060
1061 /**
1062 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1063 */
1064 protected function getBackendUser() {
1065 return $GLOBALS['BE_USER'];
1066 }
1067
1068 }