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