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