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