[BUGFIX] Allow processed folders in different storage
[Packages/TYPO3.CMS.git] / typo3 / sysext / filelist / Classes / Controller / FileListController.php
1 <?php
2 namespace TYPO3\CMS\Filelist\Controller;
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\Template\DocumentTemplate;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Backend\Utility\IconUtility;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Resource\Exception;
22 use TYPO3\CMS\Core\Resource\ResourceFactory;
23 use TYPO3\CMS\Core\Resource\Utility\ListUtility;
24 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
25 use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Core\Utility\MathUtility;
28 use TYPO3\CMS\Filelist\FileList;
29
30 /**
31 * Script Class for creating the list of files in the File > Filelist module
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 class FileListController {
36
37 /**
38 * Module configuration
39 *
40 * @var array
41 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. The Module gets configured by ExtensionManagementUtility::addModule() in ext_tables.php
42 */
43 public $MCONF = array();
44
45 /**
46 * @var array
47 */
48 public $MOD_MENU = array();
49
50 /**
51 * @var array
52 */
53 public $MOD_SETTINGS = array();
54
55 /**
56 * Accumulated HTML output
57 *
58 * @var string
59 */
60 public $content;
61
62 /**
63 * Document template object
64 *
65 * @var DocumentTemplate
66 */
67 public $doc;
68
69 /**
70 * "id" -> the path to list.
71 *
72 * @var string
73 */
74 public $id;
75
76 /**
77 * @var \TYPO3\CMS\Core\Resource\Folder
78 */
79 protected $folderObject;
80
81 /**
82 * @var FlashMessage
83 */
84 protected $errorMessage;
85
86 /**
87 * Pointer to listing
88 *
89 * @var int
90 */
91 public $pointer;
92
93 /**
94 * "Table"
95 *
96 * @var string
97 */
98 public $table;
99
100 /**
101 * Thumbnail mode.
102 *
103 * @var string
104 */
105 public $imagemode;
106
107 /**
108 * @var string
109 */
110 public $cmd;
111
112 /**
113 * @var bool
114 */
115 public $overwriteExistingFiles;
116
117 /**
118 * The file list object
119 *
120 * @var FileList
121 */
122 public $filelist = NULL;
123
124 /**
125 * The name of the module
126 *
127 * @var string
128 */
129 protected $moduleName = 'file_list';
130
131 /**
132 * Constructor
133 */
134 public function __construct() {
135 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_mod_file_list.xlf');
136 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_misc.xlf');
137 }
138
139 /**
140 * Initialize variables, file object
141 * Incoming GET vars include id, pointer, table, imagemode
142 *
143 * @return void
144 * @throws \RuntimeException
145 */
146 public function init() {
147 // Setting GPvars:
148 $this->id = ($combinedIdentifier = GeneralUtility::_GP('id'));
149 $this->pointer = GeneralUtility::_GP('pointer');
150 $this->table = GeneralUtility::_GP('table');
151 $this->imagemode = GeneralUtility::_GP('imagemode');
152 $this->cmd = GeneralUtility::_GP('cmd');
153 $this->overwriteExistingFiles = GeneralUtility::_GP('overwriteExistingFiles');
154
155 try {
156 if ($combinedIdentifier) {
157 /** @var $fileFactory ResourceFactory */
158 $fileFactory = GeneralUtility::makeInstance(ResourceFactory::class);
159 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
160 $identifier = substr($combinedIdentifier, strpos($combinedIdentifier, ':') + 1);
161 if (!$storage->hasFolder($identifier)) {
162 $identifier = $storage->getFolderIdentifierFromFileIdentifier($identifier);
163 }
164
165 $this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($storage->getUid() . ':' . $identifier);
166 // Disallow the rendering of the processing folder (e.g. could be called manually)
167 // and all folders without any defined storage
168 if ($this->folderObject && ($storage->getUid() === 0 || $storage->isProcessingFolder($this->folderObject))) {
169 $this->folderObject = $storage->getRootLevelFolder();
170 }
171 } else {
172 // Take the first object of the first storage
173 $fileStorages = $this->getBackendUser()->getFileStorages();
174 $fileStorage = reset($fileStorages);
175 if ($fileStorage) {
176 // Validating the input "id" (the path, directory!) and
177 // checking it against the mounts of the user. - now done in the controller
178 $this->folderObject = $fileStorage->getRootLevelFolder();
179 } else {
180 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
181 }
182 }
183 } catch (Exception $fileException) {
184 // Take the first object of the first storage
185 $fileStorages = $this->getBackendUser()->getFileStorages();
186 $fileStorage = reset($fileStorages);
187 if ($fileStorage) {
188 // Set folder object to null and throw a message later on
189 $this->folderObject = $fileStorage->getRootLevelFolder();
190 } else {
191 $this->folderObject = NULL;
192 }
193 $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class,
194 sprintf($this->getLanguageService()->getLL('folderNotFoundMessage', TRUE),
195 htmlspecialchars($this->id)
196 ),
197 $this->getLanguageService()->getLL('folderNotFoundTitle', TRUE),
198 FlashMessage::NOTICE
199 );
200 }
201 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
202 $this->menuConfig();
203 }
204
205 /**
206 * Setting the menu/session variables
207 *
208 * @return void
209 */
210 public function menuConfig() {
211 // MENU-ITEMS:
212 // If array, then it's a selector box menu
213 // If empty string it's just a variable, that will be saved.
214 // Values NOT in this array will not be saved in the settings-array for the module.
215 $this->MOD_MENU = array(
216 'sort' => '',
217 'reverse' => '',
218 'displayThumbs' => '',
219 'clipBoard' => '',
220 'bigControlPanel' => ''
221 );
222 // CLEANSE SETTINGS
223 $this->MOD_SETTINGS = BackendUtility::getModuleData(
224 $this->MOD_MENU,
225 GeneralUtility::_GP('SET'),
226 $this->moduleName
227 );
228 }
229
230 /**
231 * Main function, creating the listing
232 *
233 * @return void
234 */
235 public function main() {
236 // Initialize the template object
237 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
238 $this->doc->backPath = $GLOBALS['BACK_PATH'];
239 $this->doc->setModuleTemplate('EXT:filelist/Resources/Private/Templates/file_list.html');
240
241 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
242 $pageRenderer = $this->doc->getPageRenderer();
243 $pageRenderer->loadPrototype();
244 $pageRenderer->loadJQuery();
245 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileListLocalisation');
246
247 // There there was access to this file path, continue, make the list
248 if ($this->folderObject) {
249
250 // Create filelisting object
251 $this->filelist = GeneralUtility::makeInstance(FileList::class);
252 $this->filelist->backPath = $GLOBALS['BACK_PATH'];
253 // Apply predefined values for hidden checkboxes
254 // Set predefined value for DisplayBigControlPanel:
255 $backendUser = $this->getBackendUser();
256 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
257 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
258 } elseif ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
259 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
260 }
261 // Set predefined value for DisplayThumbnails:
262 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
263 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
264 } elseif ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
265 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
266 }
267 // Set predefined value for Clipboard:
268 if ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
269 $this->MOD_SETTINGS['clipBoard'] = TRUE;
270 } elseif ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
271 $this->MOD_SETTINGS['clipBoard'] = FALSE;
272 }
273 // If user never opened the list module, set the value for displayThumbs
274 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
275 $this->MOD_SETTINGS['displayThumbs'] = $backendUser->uc['thumbnailsByDefault'];
276 }
277 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
278 // Create clipboard object and initialize that
279 $this->filelist->clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
280 $this->filelist->clipObj->fileMode = 1;
281 $this->filelist->clipObj->initializeClipboard();
282 $CB = GeneralUtility::_GET('CB');
283 if ($this->cmd == 'setCB') {
284 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(GeneralUtility::_POST('CBH'), (array)GeneralUtility::_POST('CBC')), '_FILE');
285 }
286 if (!$this->MOD_SETTINGS['clipBoard']) {
287 $CB['setP'] = 'normal';
288 }
289 $this->filelist->clipObj->setCmd($CB);
290 $this->filelist->clipObj->cleanCurrent();
291 // Saves
292 $this->filelist->clipObj->endClipboard();
293 // If the "cmd" was to delete files from the list (clipboard thing), do that:
294 if ($this->cmd == 'delete') {
295 $items = $this->filelist->clipObj->cleanUpCBC(GeneralUtility::_POST('CBC'), '_FILE', 1);
296 if (count($items)) {
297 // Make command array:
298 $FILE = array();
299 foreach ($items as $v) {
300 $FILE['delete'][] = array('data' => $v);
301 }
302 // Init file processing object for deleting and pass the cmd array.
303 $fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
304 $fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
305 $fileProcessor->setActionPermissions();
306 $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
307 $fileProcessor->start($FILE);
308 $fileProcessor->processData();
309 $fileProcessor->pushErrorMessagesToFlashMessageQueue();
310 }
311 }
312 if (!isset($this->MOD_SETTINGS['sort'])) {
313 // Set default sorting
314 $this->MOD_SETTINGS['sort'] = 'file';
315 $this->MOD_SETTINGS['reverse'] = 0;
316 }
317 // Start up filelisting object, include settings.
318 $this->pointer = MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
319 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
320 // Generate the list
321 $this->filelist->generateList();
322 // Set top JavaScript:
323 $this->doc->JScode = $this->doc->wrapScriptTags('if (top.fsMod) top.fsMod.recentIds["file"] = "' . rawurlencode($this->id) . '";' . $this->filelist->CBfunctions());
324 // This will return content necessary for the context sensitive clickmenus to work: bodytag events, JavaScript functions and DIV-layers.
325 $this->doc->getContextMenuCode();
326 // Setting up the buttons and markers for docheader
327 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
328 // add the folder info to the marker array
329 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
330 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
331
332 // Include DragUploader only if we have write access
333 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')
334 && $this->folderObject->checkActionPermission('write')
335 ) {
336 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DragUploader');
337 $pageRenderer->addInlineLanguagelabelFile(
338 ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
339 'file_upload'
340 );
341 }
342
343 // Build the <body> for the module
344 $moduleHeadline = $this->getModuleHeadline();
345 // Create output
346 $pageContent = $moduleHeadline !== '' ? '<h1>' . $moduleHeadline . '</h1>' : '';
347
348 $pageContent .= '<form action="' . htmlspecialchars($this->filelist->listURL()) . '" method="post" name="dblistForm">';
349 $pageContent .= $this->filelist->HTMLcode;
350 $pageContent .= '<input type="hidden" name="cmd" /></form>';
351
352 // Making listing options:
353 if ($this->filelist->HTMLcode) {
354 $pageContent .= '
355
356 <!--
357 Listing options for extended view, clipboard and thumbnails
358 -->
359 <div id="typo3-listOptions">
360 ';
361 // Add "display bigControlPanel" checkbox:
362 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'selectable') {
363 $pageContent .= '<div class="checkbox">' .
364 '<label for="bigControlPanel">' .
365 BackendUtility::getFuncCheck($this->id, 'SET[bigControlPanel]', $this->MOD_SETTINGS['bigControlPanel'], '', '', 'id="bigControlPanel"') .
366 $this->getLanguageService()->getLL('bigControlPanel', TRUE) .
367 '</label>' .
368 '</div>';
369 }
370 // Add "display thumbnails" checkbox:
371 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable') {
372 $pageContent .= '<div class="checkbox">' .
373 '<label for="checkDisplayThumbs">' .
374 BackendUtility::getFuncCheck($this->id, 'SET[displayThumbs]', $this->MOD_SETTINGS['displayThumbs'], '', '', 'id="checkDisplayThumbs"') .
375 $this->getLanguageService()->getLL('displayThumbs', TRUE) .
376 '</label>' .
377 '</div>';
378 }
379 // Add "clipboard" checkbox:
380 if ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable') {
381 $pageContent .= '<div class="checkbox">' .
382 '<label for="checkClipBoard">' .
383 BackendUtility::getFuncCheck($this->id, 'SET[clipBoard]', $this->MOD_SETTINGS['clipBoard'], '', '', 'id="checkClipBoard"') .
384 $this->getLanguageService()->getLL('clipBoard', TRUE) .
385 '</label>' .
386 '</div>';
387 }
388 $pageContent .= '
389 </div>
390 ';
391 // Set clipboard:
392 if ($this->MOD_SETTINGS['clipBoard']) {
393 $pageContent .= $this->filelist->clipObj->printClipboard();
394 $pageContent .= BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_clipboard');
395 }
396 }
397 $markerArray = array(
398 'CSH' => $docHeaderButtons['csh'],
399 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
400 'CONTENT' => ($this->errorMessage ? $this->errorMessage->render() : '') . $pageContent,
401 'FOLDER_IDENTIFIER' => $this->folderObject->getCombinedIdentifier(),
402 'FILEDENYPATERN' => $GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],
403 'MAXFILESIZE' => GeneralUtility::getMaxUploadFileSize() * 1024,
404 );
405 $this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
406 // Renders the module page
407 $this->content = $this->doc->render($this->getLanguageService()->getLL('files'), $this->content);
408 } else {
409 $content = '';
410 if ($this->errorMessage) {
411 $this->errorMessage->setSeverity(FlashMessage::ERROR);
412 $content = $this->doc->moduleBody(array(), array_merge(array('REFRESH' => '', 'PASTE' => '', 'LEVEL_UP' => ''), $this->getButtons()), array('CSH' => '', 'TITLE' => '', 'FOLDER_INFO' => '', 'PAGE_ICON' => '', 'FUNC_MENU' => '', 'CONTENT' => $this->errorMessage->render()));
413 }
414 // Create output - no access (no warning though)
415 $this->content = $this->doc->render($this->getLanguageService()->getLL('files'), $content);
416 }
417 }
418
419 /**
420 * Get main headline based on active folder or storage for backend module
421 *
422 * Folder names are resolved to their special names like done in the tree view.
423 *
424 * @return string
425 */
426 protected function getModuleHeadline() {
427 $name = $this->folderObject->getName();
428 if ($name === '') {
429 // Show storage name on storage root
430 if ($this->folderObject->getIdentifier() === '/') {
431 $name = $this->folderObject->getStorage()->getName();
432 }
433 } else {
434 $name = key(ListUtility::resolveSpecialFolderNames(
435 array($name => $this->folderObject)
436 ));
437 }
438 return $name;
439 }
440
441 /**
442 * Outputting the accumulated content to screen
443 *
444 * @return void
445 */
446 public function printContent() {
447 echo $this->content;
448 }
449
450 /**
451 * Create the panel of buttons for submitting the form or otherwise perform operations.
452 *
453 * @return array All available buttons as an assoc. array
454 */
455 public function getButtons() {
456 $buttons = array(
457 'csh' => '',
458 'shortcut' => '',
459 'upload' => '',
460 'new' => ''
461 );
462 // Add shortcut
463 if ($this->getBackendUser()->mayMakeShortcut()) {
464 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->moduleName);
465 }
466 // FileList Module CSH:
467 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module');
468 // Upload button (only if upload to this directory is allowed)
469 if ($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('add', 'File') && $this->folderObject->checkActionPermission('write')) {
470 $buttons['upload'] = '<a href="' . htmlspecialchars($GLOBALS['BACK_PATH']
471 . BackendUtility::getModuleUrl(
472 'file_upload',
473 array(
474 'target' => $this->folderObject->getCombinedIdentifier(),
475 'returnUrl' => $this->filelist->listURL(),
476 )
477 )) . '" id="button-upload" title="' . $this->getLanguageService()->makeEntities($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.upload', TRUE)) . '">' . IconUtility::getSpriteIcon('actions-edit-upload') . '</a>';
478 }
479 // New folder button
480 if ($this->folderObject && $this->folderObject->checkActionPermission('write')
481 && ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File') || $this->folderObject->checkActionPermission('add'))
482 ) {
483 $buttons['new'] = '<a href="' . htmlspecialchars($GLOBALS['BACK_PATH']
484 . BackendUtility::getModuleUrl(
485 'file_newfolder',
486 array(
487 'target' => $this->folderObject->getCombinedIdentifier(),
488 'returnUrl' => $this->filelist->listURL(),
489 )
490 )) . '" title="' . $this->getLanguageService()->makeEntities($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.new', TRUE)) . '">' . IconUtility::getSpriteIcon('actions-document-new') . '</a>';
491 }
492 return $buttons;
493 }
494
495 /**
496 * Returns an instance of LanguageService
497 *
498 * @return \TYPO3\CMS\Lang\LanguageService
499 */
500 protected function getLanguageService() {
501 return $GLOBALS['LANG'];
502 }
503
504 /**
505 * Returns the current BE user.
506 *
507 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
508 */
509 protected function getBackendUser() {
510 return $GLOBALS['BE_USER'];
511 }
512
513 }