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