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