[BUGFIX] Only log file/directory actions which were done
[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 \TYPO3\CMS\Core\Resource\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 // Log success
445 $this->writelog(4, 0, 1, 'File "%s" deleted', array($fileObject->getIdentifier()));
446 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
447 $this->writelog(4, 1, 112, 'You are not allowed to access the file', array($fileObject->getIdentifier()));
448 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
449 $this->writelog(4, 1, 111, 'Target was not within your mountpoints! T="%s"', array($fileObject->getIdentifier()));
450 } catch (\RuntimeException $e) {
451 $this->writelog(4, 1, 110, 'Could not delete file "%s". Write-permission problem?', array($fileObject->getIdentifier()));
452 }
453 }
454 } else {
455 try {
456 /** @var $fileObject \TYPO3\CMS\Core\Resource\FolderInterface */
457 if ($fileObject->getFileCount() > 0) {
458 // render a message that the folder could not be deleted because it still contains files
459 $flashMessage = GeneralUtility::makeInstance(
460 '\\TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
461 sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderNotDeletedHasFiles'), $fileObject->getName()),
462 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderNotDeletedHasFiles'),
463 \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING,
464 TRUE
465 );
466 $this->addFlashMessage($flashMessage);
467 } else {
468 $result = $fileObject->delete(TRUE);
469
470 // notify the user that the folder was deleted
471 $flashMessage = GeneralUtility::makeInstance(
472 '\\TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
473 sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.description.folderDeleted'), $fileObject->getName()),
474 $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:message.header.folderDeleted'),
475 \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
476 TRUE
477 );
478 $this->addFlashMessage($flashMessage);
479 // Log success
480 $this->writelog(4, 0, 3, 'Directory "%s" deleted', array($fileObject->getIdentifier()));
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 }
491 return $result;
492 }
493
494 /**
495 * Maps results from the fal file reference table on the
496 * structure of the normal reference index table.
497 *
498 * @param array $referenceRecord
499 * @return array
500 */
501 protected function transformFileReferenceToRecordReference(array $referenceRecord) {
502 $fileReference = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
503 '*',
504 'sys_file_reference',
505 'uid=' . (int)$referenceRecord['recuid']
506 );
507 return array(
508 'recuid' => $fileReference['uid_foreign'],
509 'tablename' => $fileReference['tablenames'],
510 'field' => $fileReference['fieldname'],
511 'flexpointer' => '',
512 'softref_key' => '',
513 'sorting' => $fileReference['sorting_foreign']
514 );
515 }
516
517 /**
518 * Gets a File or a Folder object from an identifier [storage]:[fileId]
519 *
520 * @param string $identifier
521 * @return \TYPO3\CMS\Core\Resource\Folder|\TYPO3\CMS\Core\Resource\File
522 * @throws \TYPO3\CMS\Core\Resource\Exception\InvalidFileException
523 */
524 protected function getFileObject($identifier) {
525 $object = $this->fileFactory->retrieveFileOrFolderObject($identifier);
526 if (!is_object($object)) {
527 throw new \TYPO3\CMS\Core\Resource\Exception\InvalidFileException('The item ' . $identifier . ' was not a file or directory!!', 1320122453);
528 }
529 return $object;
530 }
531
532 /**
533 * Copying files and folders (action=2)
534 *
535 * $cmds['data'] (string): The file/folder to copy
536 * + example "4:mypath/tomyfolder/myfile.jpg")
537 * + for backwards compatibility: the identifier was the path+filename
538 * $cmds['target'] (string): The path where to copy to.
539 * + example "2:targetpath/targetfolder/"
540 * $cmds['altName'] (string): Use an alternative name if the target already exists
541 *
542 * @param array $cmds Command details as described above
543 * @return \TYPO3\CMS\Core\Resource\File
544 */
545 protected function func_copy($cmds) {
546 if (!$this->isInit) {
547 return FALSE;
548 }
549 $sourceFileObject = $this->getFileObject($cmds['data']);
550 /** @var $targetFolderObject \TYPO3\CMS\Core\Resource\Folder */
551 $targetFolderObject = $this->getFileObject($cmds['target']);
552 // Basic check
553 if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
554 $this->writelog(2, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
555 return FALSE;
556 }
557 // If this is TRUE, we append _XX to the file name if
558 $appendSuffixOnConflict = (string) $cmds['altName'];
559 $resultObject = NULL;
560 // Copying the file
561 if ($sourceFileObject instanceof File) {
562 try {
563 $conflictMode = $appendSuffixOnConflict !== '' ? 'renameNewFile' : 'cancel';
564 $resultObject = $sourceFileObject->copyTo($targetFolderObject, NULL, $conflictMode);
565 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
566 $this->writelog(2, 1, 114, 'You are not allowed to copy files', '');
567 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
568 $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()));
569 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
570 $this->writelog(2, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
571 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
572 $this->writelog(2, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
573 } catch (\BadMethodCallException $e) {
574 $this->writelog(3, 1, 128, 'The function to copy a file between storages is not yet implemented', array());
575 } catch (\RuntimeException $e) {
576 $this->writelog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
577 }
578 if ($resultObject) {
579 $this->writelog(2, 0, 1, 'File "%s" copied to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
580 }
581 } else {
582 // Else means this is a Folder
583 $sourceFolderObject = $sourceFileObject;
584 try {
585 $conflictMode = $appendSuffixOnConflict !== '' ? 'renameNewFile' : 'cancel';
586 $resultObject = $sourceFolderObject->copyTo($targetFolderObject, NULL, $conflictMode);
587 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
588 $this->writelog(2, 1, 125, 'You are not allowed to copy directories', '');
589 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
590 $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()));
591 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
592 $this->writelog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
593 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
594 $this->writelog(2, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', array($targetFolderObject->getIdentifier(), $sourceFolderObject->getIdentifier()));
595 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
596 $this->writelog(2, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
597 } catch (\BadMethodCallException $e) {
598 $this->writelog(3, 1, 129, 'The function to copy a folder between storages is not yet implemented', array());
599 } catch (\RuntimeException $e) {
600 $this->writelog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
601 }
602 if ($resultObject) {
603 $this->writelog(2, 0, 2, 'Directory "%s" copied to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
604 }
605 }
606 return $resultObject;
607 }
608
609 /**
610 * Moving files and folders (action=3)
611 *
612 * $cmds['data'] (string): The file/folder to move
613 * + example "4:mypath/tomyfolder/myfile.jpg")
614 * + for backwards compatibility: the identifier was the path+filename
615 * $cmds['target'] (string): The path where to move to.
616 * + example "2:targetpath/targetfolder/"
617 * $cmds['altName'] (string): Use an alternative name if the target already exists
618 *
619 * @param array $cmds Command details as described above
620 * @return \TYPO3\CMS\Core\Resource\File
621 */
622 protected function func_move($cmds) {
623 if (!$this->isInit) {
624 return FALSE;
625 }
626 $sourceFileObject = $this->getFileObject($cmds['data']);
627 $targetFolderObject = $this->getFileObject($cmds['target']);
628 // Basic check
629 if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
630 $this->writelog(3, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']));
631 return FALSE;
632 }
633 $alternativeName = (string) $cmds['altName'];
634 $resultObject = NULL;
635 // Moving the file
636 if ($sourceFileObject instanceof File) {
637 try {
638 if ($alternativeName !== '') {
639 // Don't allow overwriting existing files, but find a new name
640 $resultObject = $sourceFileObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
641 } else {
642 // Don't allow overwriting existing files
643 $resultObject = $sourceFileObject->moveTo($targetFolderObject, NULL, 'cancel');
644 }
645 $this->writelog(3, 0, 1, 'File "%s" moved to "%s"', array($sourceFileObject->getIdentifier(), $resultObject->getIdentifier()));
646 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
647 $this->writelog(3, 1, 114, 'You are not allowed to move files', '');
648 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
649 $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()));
650 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
651 $this->writelog(3, 1, 111, 'Extension of file name "%s" is not allowed in "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
652 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
653 $this->writelog(3, 1, 112, 'File "%s" already exists in folder "%s"!', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
654 } catch (\BadMethodCallException $e) {
655 $this->writelog(3, 1, 126, 'The function to move a file between storages is not yet implemented', array());
656 } catch (\RuntimeException $e) {
657 $this->writelog(3, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($sourceFileObject->getIdentifier(), $targetFolderObject->getIdentifier()));
658 }
659 } else {
660 // Else means this is a Folder
661 $sourceFolderObject = $sourceFileObject;
662 try {
663 if ($alternativeName !== '') {
664 // Don't allow overwriting existing files, but find a new name
665 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, $alternativeName, 'renameNewFile');
666 } else {
667 // Don't allow overwriting existing files
668 $resultObject = $sourceFolderObject->moveTo($targetFolderObject, NULL, 'renameNewFile');
669 }
670 $this->writelog(3, 0, 2, 'Directory "%s" moved to "%s"', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
671 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
672 $this->writelog(3, 1, 125, 'You are not allowed to move directories', '');
673 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException $e) {
674 $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()));
675 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
676 $this->writelog(3, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($targetFolderObject->getIdentifier()));
677 } catch (\TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException $e) {
678 $this->writelog(3, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', array($targetFolderObject->getIdentifier(), $sourceFolderObject->getIdentifier()));
679 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
680 $this->writelog(3, 1, 123, 'Target "%s" already exists!', array($targetFolderObject->getIdentifier()));
681 } catch (\BadMethodCallException $e) {
682 $this->writelog(3, 1, 127, 'The function to move a folder between storages is not yet implemented', array());
683 } catch (\RuntimeException $e) {
684 $this->writelog(3, 2, 119, 'Directory "%s" WAS NOT moved to "%s"! Write-permission problem?', array($sourceFolderObject->getIdentifier(), $targetFolderObject->getIdentifier()));
685 }
686 }
687 return $resultObject;
688 }
689
690 /**
691 * Renaming files or foldes (action=5)
692 *
693 * $cmds['data'] (string): The file/folder to copy
694 * + example "4:mypath/tomyfolder/myfile.jpg")
695 * + for backwards compatibility: the identifier was the path+filename
696 * $cmds['target'] (string): New name of the file/folder
697 *
698 * @param array $cmds Command details as described above
699 * @return \TYPO3\CMS\Core\Resource\File Returns the new file upon success
700 * @todo Define visibility
701 */
702 public function func_rename($cmds) {
703 if (!$this->isInit) {
704 return FALSE;
705 }
706 $sourceFileObject = $this->getFileObject($cmds['data']);
707 $targetFile = $cmds['target'];
708 $resultObject = NULL;
709 if ($sourceFileObject instanceof File) {
710 try {
711 // Try to rename the File
712 $resultObject = $sourceFileObject->rename($targetFile);
713 $this->writelog(5, 0, 1, 'File renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
714 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
715 $this->writelog(5, 1, 102, 'You are not allowed to rename files!', '');
716 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
717 $this->writelog(5, 1, 101, 'Extension of file name "%s" was not allowed!', array($targetFile));
718 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
719 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
720 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
721 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
722 } catch (\RuntimeException $e) {
723 $this->writelog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
724 }
725 } else {
726 // Else means this is a Folder
727 try {
728 // Try to rename the Folder
729 $resultObject = $sourceFileObject->rename($targetFile);
730 $this->writelog(5, 0, 2, 'Directory renamed from "%s" to "%s"', array($sourceFileObject->getName(), $targetFile));
731 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
732 $this->writelog(5, 1, 111, 'You are not allowed to rename directories!', '');
733 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
734 $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($targetFile));
735 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
736 $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($targetFile));
737 } catch (\RuntimeException $e) {
738 $this->writelog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', array($sourceFileObject->getName(), $targetFile));
739 }
740 }
741 return $resultObject;
742 }
743
744 /**
745 * This creates a new folder. (action=6)
746 *
747 * $cmds['data'] (string): The new folder name
748 * $cmds['target'] (string): The path where to copy to.
749 * + example "2:targetpath/targetfolder/"
750 *
751 * @param array $cmds Command details as described above
752 * @return \TYPO3\CMS\Core\Resource\Folder Returns the new foldername upon success
753 * @todo Define visibility
754 */
755 public function func_newfolder($cmds) {
756 if (!$this->isInit) {
757 return FALSE;
758 }
759 $targetFolderObject = $this->getFileObject($cmds['target']);
760 if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
761 $this->writelog(6, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
762 return FALSE;
763 }
764 $resultObject = NULL;
765 try {
766 $folderName = $cmds['data'];
767 $resultObject = $targetFolderObject->createFolder($folderName);
768 $this->writelog(6, 0, 1, 'Directory "%s" created in "%s"', array($folderName, $targetFolderObject->getIdentifier() . '/'));
769 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
770 $this->writelog(6, 1, 103, 'You are not allowed to create directories!', '');
771 } catch (\TYPO3\CMS\Core\Resource\Exception\NotInMountPointException $e) {
772 $this->writelog(6, 1, 102, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier() . '/'));
773 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException $e) {
774 $this->writelog(6, 1, 101, 'File or directory "%s" existed already!', array($folderName));
775 } catch (\RuntimeException $e) {
776 $this->writelog(6, 1, 100, 'Directory "%s" not created. Write-permission problem in "%s"?', array($folderName, $targetFolderObject->getIdentifier() . '/'));
777 }
778 return $resultObject;
779 }
780
781 /**
782 * This creates a new file. (action=8)
783 * $cmds['data'] (string): The new file name
784 * $cmds['target'] (string): The path where to create it.
785 * + example "2:targetpath/targetfolder/"
786 *
787 * @param array $cmds Command details as described above
788 * @return string Returns the new filename upon success
789 * @todo Define visibility
790 */
791 public function func_newfile($cmds) {
792 if (!$this->isInit) {
793 return FALSE;
794 }
795 $targetFolderObject = $this->getFileObject($cmds['target']);
796 if (!$targetFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
797 $this->writelog(8, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
798 return FALSE;
799 }
800 $resultObject = NULL;
801 try {
802 $fileName = $cmds['data'];
803 $resultObject = $targetFolderObject->createFile($fileName);
804 $this->writelog(8, 0, 1, 'File created: "%s"', array($fileName));
805 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
806 $this->writeLog(8, 1, 106, 'Extension of file "%s" was not allowed!', 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 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
859 $this->writelog(9, 1, 100, 'File "%s" was not saved! File extension rejected!', array($fileObject->getIdentifier()));
860 return FALSE;
861 }
862 }
863
864 /**
865 * Upload of files (action=1)
866 * when having multiple uploads (HTML5-style), the array $_FILES looks like this:
867 * Array(
868 * [upload_1] => Array(
869 * [name] => Array(
870 * [0] => GData - Content-Elements and Media-Gallery.pdf
871 * [1] => CMS Expo 2011.txt
872 * )
873 * [type] => Array(
874 * [0] => application/pdf
875 * [1] => text/plain
876 * )
877 * [tmp_name] => Array(
878 * [0] => /Applications/MAMP/tmp/php/phpNrOB43
879 * [1] => /Applications/MAMP/tmp/php/phpD2HQAK
880 * )
881 * [size] => Array(
882 * [0] => 373079
883 * [1] => 1291
884 * )
885 * )
886 * )
887 * in HTML you'd need sth like this: <input type="file" name="upload_1[]" multiple="true" />
888 *
889 * @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)
890 * @return string Returns the new filename upon success
891 * @todo Define visibility
892 */
893 public function func_upload($cmds) {
894 if (!$this->isInit) {
895 return FALSE;
896 }
897 $uploadPosition = $cmds['data'];
898 $uploadedFileData = $_FILES['upload_' . $uploadPosition];
899 if (empty($uploadedFileData['name']) || is_array($uploadedFileData['name']) && empty($uploadedFileData['name'][0])) {
900 $this->writelog(1, 2, 108, 'No file was uploaded!', '');
901 return FALSE;
902 }
903 // Example indentifier for $cmds['target'] => "2:targetpath/targetfolder/"
904 $targetFolderObject = $this->getFileObject($cmds['target']);
905 // Uploading with non HTML-5-style, thus, make an array out of it, so we can loop over it
906 if (!is_array($uploadedFileData['name'])) {
907 $uploadedFileData = array(
908 'name' => array($uploadedFileData['name']),
909 'type' => array($uploadedFileData['type']),
910 'tmp_name' => array($uploadedFileData['tmp_name']),
911 'size' => array($uploadedFileData['size'])
912 );
913 }
914 $resultObjects = array();
915 $numberOfUploadedFilesForPosition = count($uploadedFileData['name']);
916 // Loop through all uploaded files
917 for ($i = 0; $i < $numberOfUploadedFilesForPosition; $i++) {
918 $fileInfo = array(
919 'name' => $uploadedFileData['name'][$i],
920 'type' => $uploadedFileData['type'][$i],
921 'tmp_name' => $uploadedFileData['tmp_name'][$i],
922 'size' => $uploadedFileData['size'][$i]
923 );
924 try {
925 // @todo can be improved towards conflict mode naming
926 if ($this->dontCheckForUnique) {
927 $conflictMode = 'replace';
928 } else {
929 $conflictMode = 'cancel';
930 }
931 /** @var $fileObject File */
932 $fileObject = $targetFolderObject->addUploadedFile($fileInfo, $conflictMode);
933 $this->fileRepository->addToIndex($fileObject);
934 $resultObjects[] = $fileObject;
935 $this->writelog(1, 0, 1, 'Uploading file "%s" to "%s"', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
936 } catch (\TYPO3\CMS\Core\Resource\Exception\UploadException $e) {
937 $this->writelog(1, 2, 106, 'The upload has failed, no uploaded file found!', '');
938 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException $e) {
939 $this->writelog(1, 1, 105, 'You are not allowed to upload files!', '');
940 } catch (\TYPO3\CMS\Core\Resource\Exception\UploadSizeException $e) {
941 $this->writelog(1, 1, 104, 'The uploaded file "%s" exceeds the size-limit', array($fileInfo['name']));
942 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException $e) {
943 $this->writelog(1, 1, 103, 'Destination path "%s" was not within your mountpoints!', array($targetFolderObject->getIdentifier()));
944 } catch (\TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException $e) {
945 $this->writelog(1, 1, 102, 'Extension of file name "%s" is not allowed in "%s"!', array($fileInfo['name'], $targetFolderObject->getIdentifier()));
946 } catch (\TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException $e) {
947 $this->writelog(1, 1, 101, 'No unique filename available in "%s"!', array($targetFolderObject->getIdentifier()));
948 } catch (\RuntimeException $e) {
949 $this->writelog(1, 1, 100, 'Uploaded file could not be moved! Write-permission problem in "%s"?', array($targetFolderObject->getIdentifier()));
950 }
951 }
952
953 return $resultObjects;
954 }
955
956 /**
957 * Unzipping file (action=7)
958 * This is permitted only if the user has fullAccess or if the file resides
959 *
960 * @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.
961 * @return boolean Returns TRUE on success
962 * @todo Define visibility
963 */
964 public function func_unzip($cmds) {
965 if (!$this->isInit || $this->dont_use_exec_commands) {
966 return FALSE;
967 }
968 $theFile = $cmds['data'];
969 if (!@is_file($theFile)) {
970 $this->writelog(7, 2, 105, 'The file "%s" did not exist!', array($theFile));
971 return FALSE;
972 }
973 $fI = GeneralUtility::split_fileref($theFile);
974 if (!isset($cmds['target'])) {
975 $cmds['target'] = $fI['path'];
976 }
977 // Clean up destination directory
978 // !!! Method has been put in the local driver, can be saftely removed
979 $theDest = $this->is_directory($cmds['target']);
980 if (!$theDest) {
981 $this->writelog(7, 2, 104, 'Destination "%s" was not a directory', array($cmds['target']));
982 return FALSE;
983 }
984 if (!$this->actionPerms['unzipFile']) {
985 $this->writelog(7, 1, 103, 'You are not allowed to unzip files', '');
986 return FALSE;
987 }
988 if ($fI['fileext'] != 'zip') {
989 $this->writelog(7, 1, 102, 'File extension is not "zip"', '');
990 return FALSE;
991 }
992 if (!$this->checkIfFullAccess($theDest)) {
993 $this->writelog(7, 1, 101, 'You don\'t have full access to the destination directory "%s"!', array($theDest));
994 return FALSE;
995 }
996 // !!! Method has been put in the sotrage driver, can be saftely removed
997 if ($this->checkPathAgainstMounts($theFile) && $this->checkPathAgainstMounts($theDest . '/')) {
998 // No way to do this under windows.
999 $cmd = $this->unzipPath . 'unzip -qq ' . escapeshellarg($theFile) . ' -d ' . escapeshellarg($theDest);
1000 \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
1001 $this->writelog(7, 0, 1, 'Unzipping file "%s" in "%s"', array($theFile, $theDest));
1002 return TRUE;
1003 } else {
1004 $this->writelog(7, 1, 100, 'File "%s" or destination "%s" was not within your mountpoints!', array($theFile, $theDest));
1005 return FALSE;
1006 }
1007 }
1008
1009 /**
1010 * Add flash message to message queue
1011 *
1012 * @param \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage
1013 * @return void
1014 */
1015 protected function addFlashMessage(\TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage) {
1016 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
1017 $flashMessageService = GeneralUtility::makeInstance(
1018 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
1019 );
1020 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
1021 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1022 $defaultFlashMessageQueue->enqueue($flashMessage);
1023 }
1024
1025
1026 /**
1027 * Get database connection
1028 *
1029 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
1030 */
1031 protected function getDatabaseConnection() {
1032 return $GLOBALS['TYPO3_DB'];
1033 }
1034
1035 /**
1036 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1037 */
1038 protected function getBackendUser() {
1039 return $GLOBALS['BE_USER'];
1040 }
1041 }
1042
1043 ?>