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