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