6c1496383e93b3fd7f7a7fbe543a7ad5c7145c25
[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 && ($this->folderObject->getStorage()->getUid() == 0 || trim($this->folderObject->getStorage()->getProcessingFolder()->getIdentifier(), '/') === trim($this->folderObject->getIdentifier(), '/'))) {
169 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
170 $this->folderObject = $storage->getRootLevelFolder();
171 }
172 } else {
173 // Take the first object of the first storage
174 $fileStorages = $this->getBackendUser()->getFileStorages();
175 $fileStorage = reset($fileStorages);
176 if ($fileStorage) {
177 // Validating the input "id" (the path, directory!) and
178 // checking it against the mounts of the user. - now done in the controller
179 $this->folderObject = $fileStorage->getRootLevelFolder();
180 } else {
181 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
182 }
183 }
184 } catch (Exception $fileException) {
185 // Take the first object of the first storage
186 $fileStorages = $this->getBackendUser()->getFileStorages();
187 $fileStorage = reset($fileStorages);
188 if ($fileStorage) {
189 // Set folder object to null and throw a message later on
190 $this->folderObject = $fileStorage->getRootLevelFolder();
191 } else {
192 $this->folderObject = NULL;
193 }
194 $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class,
195 sprintf($this->getLanguageService()->getLL('folderNotFoundMessage', TRUE),
196 htmlspecialchars($this->id)
197 ),
198 $this->getLanguageService()->getLL('folderNotFoundTitle', TRUE),
199 FlashMessage::NOTICE
200 );
201 }
202 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
203 $this->menuConfig();
204 }
205
206 /**
207 * Setting the menu/session variables
208 *
209 * @return void
210 */
211 public function menuConfig() {
212 // MENU-ITEMS:
213 // If array, then it's a selector box menu
214 // If empty string it's just a variable, that will be saved.
215 // Values NOT in this array will not be saved in the settings-array for the module.
216 $this->MOD_MENU = array(
217 'sort' => '',
218 'reverse' => '',
219 'displayThumbs' => '',
220 'clipBoard' => '',
221 'bigControlPanel' => ''
222 );
223 // CLEANSE SETTINGS
224 $this->MOD_SETTINGS = BackendUtility::getModuleData(
225 $this->MOD_MENU,
226 GeneralUtility::_GP('SET'),
227 $this->moduleName
228 );
229 }
230
231 /**
232 * Main function, creating the listing
233 *
234 * @return void
235 */
236 public function main() {
237 // Initialize the template object
238 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
239 $this->doc->backPath = $GLOBALS['BACK_PATH'];
240 $this->doc->setModuleTemplate('EXT:filelist/Resources/Private/Templates/file_list.html');
241
242 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
243 $pageRenderer = $this->doc->getPageRenderer();
244 $pageRenderer->loadPrototype();
245 $pageRenderer->loadJQuery();
246 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileListLocalisation');
247
248 // There there was access to this file path, continue, make the list
249 if ($this->folderObject) {
250
251 // Create filelisting object
252 $this->filelist = GeneralUtility::makeInstance(FileList::class);
253 $this->filelist->backPath = $GLOBALS['BACK_PATH'];
254 // Apply predefined values for hidden checkboxes
255 // Set predefined value for DisplayBigControlPanel:
256 $backendUser = $this->getBackendUser();
257 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
258 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
259 } elseif ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
260 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
261 }
262 // Set predefined value for DisplayThumbnails:
263 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
264 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
265 } elseif ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
266 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
267 }
268 // Set predefined value for Clipboard:
269 if ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
270 $this->MOD_SETTINGS['clipBoard'] = TRUE;
271 } elseif ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
272 $this->MOD_SETTINGS['clipBoard'] = FALSE;
273 }
274 // If user never opened the list module, set the value for displayThumbs
275 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
276 $this->MOD_SETTINGS['displayThumbs'] = $backendUser->uc['thumbnailsByDefault'];
277 }
278 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
279 // Create clipboard object and initialize that
280 $this->filelist->clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
281 $this->filelist->clipObj->fileMode = 1;
282 $this->filelist->clipObj->initializeClipboard();
283 $CB = GeneralUtility::_GET('CB');
284 if ($this->cmd == 'setCB') {
285 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(GeneralUtility::_POST('CBH'), (array)GeneralUtility::_POST('CBC')), '_FILE');
286 }
287 if (!$this->MOD_SETTINGS['clipBoard']) {
288 $CB['setP'] = 'normal';
289 }
290 $this->filelist->clipObj->setCmd($CB);
291 $this->filelist->clipObj->cleanCurrent();
292 // Saves
293 $this->filelist->clipObj->endClipboard();
294 // If the "cmd" was to delete files from the list (clipboard thing), do that:
295 if ($this->cmd == 'delete') {
296 $items = $this->filelist->clipObj->cleanUpCBC(GeneralUtility::_POST('CBC'), '_FILE', 1);
297 if (count($items)) {
298 // Make command array:
299 $FILE = array();
300 foreach ($items as $v) {
301 $FILE['delete'][] = array('data' => $v);
302 }
303 // Init file processing object for deleting and pass the cmd array.
304 $fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
305 $fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
306 $fileProcessor->setActionPermissions();
307 $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
308 $fileProcessor->start($FILE);
309 $fileProcessor->processData();
310 $fileProcessor->pushErrorMessagesToFlashMessageQueue();
311 }
312 }
313 if (!isset($this->MOD_SETTINGS['sort'])) {
314 // Set default sorting
315 $this->MOD_SETTINGS['sort'] = 'file';
316 $this->MOD_SETTINGS['reverse'] = 0;
317 }
318 // Start up filelisting object, include settings.
319 $this->pointer = MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
320 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
321 // Generate the list
322 $this->filelist->generateList();
323 // Set top JavaScript:
324 $this->doc->JScode = $this->doc->wrapScriptTags('if (top.fsMod) top.fsMod.recentIds["file"] = "' . rawurlencode($this->id) . '";' . $this->filelist->CBfunctions());
325 // This will return content necessary for the context sensitive clickmenus to work: bodytag events, JavaScript functions and DIV-layers.
326 $this->doc->getContextMenuCode();
327 // Setting up the buttons and markers for docheader
328 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
329 // add the folder info to the marker array
330 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
331 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
332
333 // Include DragUploader only if we have write access
334 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')
335 && $this->folderObject->checkActionPermission('write')
336 ) {
337 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DragUploader');
338 $pageRenderer->addInlineLanguagelabelFile(
339 ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
340 'file_upload'
341 );
342 }
343
344 // Build the <body> for the module
345 $moduleHeadline = $this->getModuleHeadline();
346 // Create output
347 $pageContent = $moduleHeadline !== '' ? '<h1>' . $moduleHeadline . '</h1>' : '';
348
349 $pageContent .= '<form action="' . htmlspecialchars($this->filelist->listURL()) . '" method="post" name="dblistForm">';
350 $pageContent .= $this->filelist->HTMLcode;
351 $pageContent .= '<input type="hidden" name="cmd" /></form>';
352
353 // Making listing options:
354 if ($this->filelist->HTMLcode) {
355 $pageContent .= '
356
357 <!--
358 Listing options for extended view, clipboard and thumbnails
359 -->
360 <div id="typo3-listOptions">
361 ';
362 // Add "display bigControlPanel" checkbox:
363 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'selectable') {
364 $pageContent .= '<div class="checkbox">' .
365 '<label for="bigControlPanel">' .
366 BackendUtility::getFuncCheck($this->id, 'SET[bigControlPanel]', $this->MOD_SETTINGS['bigControlPanel'], '', '', 'id="bigControlPanel"') .
367 $this->getLanguageService()->getLL('bigControlPanel', TRUE) .
368 '</label>' .
369 '</div>';
370 }
371 // Add "display thumbnails" checkbox:
372 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable') {
373 $pageContent .= '<div class="checkbox">' .
374 '<label for="checkDisplayThumbs">' .
375 BackendUtility::getFuncCheck($this->id, 'SET[displayThumbs]', $this->MOD_SETTINGS['displayThumbs'], '', '', 'id="checkDisplayThumbs"') .
376 $this->getLanguageService()->getLL('displayThumbs', TRUE) .
377 '</label>' .
378 '</div>';
379 }
380 // Add "clipboard" checkbox:
381 if ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable') {
382 $pageContent .= '<div class="checkbox">' .
383 '<label for="checkClipBoard">' .
384 BackendUtility::getFuncCheck($this->id, 'SET[clipBoard]', $this->MOD_SETTINGS['clipBoard'], '', '', 'id="checkClipBoard"') .
385 $this->getLanguageService()->getLL('clipBoard', TRUE) .
386 '</label>' .
387 '</div>';
388 }
389 $pageContent .= '
390 </div>
391 ';
392 // Set clipboard:
393 if ($this->MOD_SETTINGS['clipBoard']) {
394 $pageContent .= $this->filelist->clipObj->printClipboard();
395 $pageContent .= BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_clipboard');
396 }
397 }
398 $markerArray = array(
399 'CSH' => $docHeaderButtons['csh'],
400 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
401 'CONTENT' => ($this->errorMessage ? $this->errorMessage->render() : '') . $pageContent,
402 'FOLDER_IDENTIFIER' => $this->folderObject->getCombinedIdentifier(),
403 'FILEDENYPATERN' => $GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],
404 'MAXFILESIZE' => GeneralUtility::getMaxUploadFileSize() * 1024,
405 );
406 $this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
407 // Renders the module page
408 $this->content = $this->doc->render($this->getLanguageService()->getLL('files'), $this->content);
409 } else {
410 $content = '';
411 if ($this->errorMessage) {
412 $this->errorMessage->setSeverity(FlashMessage::ERROR);
413 $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()));
414 }
415 // Create output - no access (no warning though)
416 $this->content = $this->doc->render($this->getLanguageService()->getLL('files'), $content);
417 }
418 }
419
420 /**
421 * Get main headline based on active folder or storage for backend module
422 *
423 * Folder names are resolved to their special names like done in the tree view.
424 *
425 * @return string
426 */
427 protected function getModuleHeadline() {
428 $name = $this->folderObject->getName();
429 if ($name === '') {
430 // Show storage name on storage root
431 if ($this->folderObject->getIdentifier() === '/') {
432 $name = $this->folderObject->getStorage()->getName();
433 }
434 } else {
435 $name = key(ListUtility::resolveSpecialFolderNames(
436 array($name => $this->folderObject)
437 ));
438 }
439 return $name;
440 }
441
442 /**
443 * Outputting the accumulated content to screen
444 *
445 * @return void
446 */
447 public function printContent() {
448 echo $this->content;
449 }
450
451 /**
452 * Create the panel of buttons for submitting the form or otherwise perform operations.
453 *
454 * @return array All available buttons as an assoc. array
455 */
456 public function getButtons() {
457 $buttons = array(
458 'csh' => '',
459 'shortcut' => '',
460 'upload' => '',
461 'new' => ''
462 );
463 // Add shortcut
464 if ($this->getBackendUser()->mayMakeShortcut()) {
465 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->moduleName);
466 }
467 // FileList Module CSH:
468 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module');
469 // Upload button (only if upload to this directory is allowed)
470 if ($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('add', 'File') && $this->folderObject->checkActionPermission('write')) {
471 $buttons['upload'] = '<a href="' . htmlspecialchars($GLOBALS['BACK_PATH']
472 . BackendUtility::getModuleUrl(
473 'file_upload',
474 array(
475 'target' => $this->folderObject->getCombinedIdentifier(),
476 'returnUrl' => $this->filelist->listURL(),
477 )
478 )) . '" 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>';
479 }
480 // New folder button
481 if ($this->folderObject && $this->folderObject->checkActionPermission('write')
482 && ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File') || $this->folderObject->checkActionPermission('add'))
483 ) {
484 $buttons['new'] = '<a href="' . htmlspecialchars($GLOBALS['BACK_PATH']
485 . BackendUtility::getModuleUrl(
486 'file_newfolder',
487 array(
488 'target' => $this->folderObject->getCombinedIdentifier(),
489 'returnUrl' => $this->filelist->listURL(),
490 )
491 )) . '" title="' . $this->getLanguageService()->makeEntities($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.new', TRUE)) . '">' . IconUtility::getSpriteIcon('actions-document-new') . '</a>';
492 }
493 return $buttons;
494 }
495
496 /**
497 * Returns an instance of LanguageService
498 *
499 * @return \TYPO3\CMS\Lang\LanguageService
500 */
501 protected function getLanguageService() {
502 return $GLOBALS['LANG'];
503 }
504
505 /**
506 * Returns the current BE user.
507 *
508 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
509 */
510 protected function getBackendUser() {
511 return $GLOBALS['BE_USER'];
512 }
513
514 }