[TASK] Call explicit render() on icon objects
[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\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Messaging\FlashMessage;
22 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
23 use TYPO3\CMS\Core\Resource\Exception;
24 use TYPO3\CMS\Core\Resource\Folder;
25 use TYPO3\CMS\Core\Resource\ResourceFactory;
26 use TYPO3\CMS\Core\Resource\Utility\ListUtility;
27 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
28 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
29 use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31 use TYPO3\CMS\Core\Utility\MathUtility;
32 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
33 use TYPO3\CMS\Filelist\FileList;
34
35 /**
36 * Script Class for creating the list of files in the File > Filelist module
37 */
38 class FileListController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
39
40 /**
41 * Module configuration
42 *
43 * @var array
44 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. The Module gets configured by ExtensionManagementUtility::addModule() in ext_tables.php
45 */
46 public $MCONF = array();
47
48 /**
49 * @var array
50 */
51 public $MOD_MENU = array();
52
53 /**
54 * @var array
55 */
56 public $MOD_SETTINGS = array();
57
58 /**
59 * Document template object
60 *
61 * @var DocumentTemplate
62 */
63 public $doc;
64
65 /**
66 * "id" -> the path to list.
67 *
68 * @var string
69 */
70 public $id;
71
72 /**
73 * @var Folder
74 */
75 protected $folderObject;
76
77 /**
78 * @var FlashMessage
79 */
80 protected $errorMessage;
81
82 /**
83 * Pointer to listing
84 *
85 * @var int
86 */
87 public $pointer;
88
89 /**
90 * "Table"
91 *
92 * @var string
93 */
94 public $table;
95
96 /**
97 * Thumbnail mode.
98 *
99 * @var string
100 */
101 public $imagemode;
102
103 /**
104 * @var string
105 */
106 public $cmd;
107
108 /**
109 * Defines behaviour when uploading files with names that already exist; possible values are
110 * the values of the \TYPO3\CMS\Core\Resource\DuplicationBehavior enumeration
111 *
112 * @var \TYPO3\CMS\Core\Resource\DuplicationBehavior
113 */
114 protected $overwriteExistingFiles;
115
116 /**
117 * The filelist object
118 *
119 * @var FileList
120 */
121 public $filelist = NULL;
122
123 /**
124 * The name of the module
125 *
126 * @var string
127 */
128 protected $moduleName = 'file_list';
129
130 /**
131 * @var \TYPO3\CMS\Core\Resource\FileRepository
132 */
133 protected $fileRepository;
134
135 /**
136 * @param \TYPO3\CMS\Core\Resource\FileRepository $fileRepository
137 */
138 public function injectFileRepository(\TYPO3\CMS\Core\Resource\FileRepository $fileRepository) {
139 $this->fileRepository = $fileRepository;
140 }
141
142 /**
143 * Initialize variables, file object
144 * Incoming GET vars include id, pointer, table, imagemode
145 *
146 * @return void
147 * @throws \RuntimeException
148 * @throws Exception\InsufficientFolderAccessPermissionsException
149 */
150 public function initializeObject() {
151 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
152 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_mod_file_list.xlf');
153 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_misc.xlf');
154
155 // Setting GPvars:
156 $this->id = ($combinedIdentifier = GeneralUtility::_GP('id'));
157 $this->pointer = GeneralUtility::_GP('pointer');
158 $this->table = GeneralUtility::_GP('table');
159 $this->imagemode = GeneralUtility::_GP('imagemode');
160 $this->cmd = GeneralUtility::_GP('cmd');
161 $this->overwriteExistingFiles = DuplicationBehavior::cast(GeneralUtility::_GP('overwriteExistingFiles'));
162
163 try {
164 if ($combinedIdentifier) {
165 /** @var $fileFactory ResourceFactory */
166 $fileFactory = GeneralUtility::makeInstance(ResourceFactory::class);
167 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
168 $identifier = substr($combinedIdentifier, strpos($combinedIdentifier, ':') + 1);
169 if (!$storage->hasFolder($identifier)) {
170 $identifier = $storage->getFolderIdentifierFromFileIdentifier($identifier);
171 }
172
173 $this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($storage->getUid() . ':' . $identifier);
174 // Disallow access to fallback storage 0
175 if ($storage->getUid() === 0) {
176 throw new Exception\InsufficientFolderAccessPermissionsException('You are not allowed to access files outside your storages', 1434539815);
177 }
178 // Disallow the rendering of the processing folder (e.g. could be called manually)
179 if ($this->folderObject && $storage->isProcessingFolder($this->folderObject)) {
180 $this->folderObject = $storage->getRootLevelFolder();
181 }
182 } else {
183 // Take the first object of the first storage
184 $fileStorages = $this->getBackendUser()->getFileStorages();
185 $fileStorage = reset($fileStorages);
186 if ($fileStorage) {
187 $this->folderObject = $fileStorage->getRootLevelFolder();
188 } else {
189 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
190 }
191 }
192
193 if ($this->folderObject && !$this->folderObject->getStorage()->isWithinFileMountBoundaries($this->folderObject)) {
194 throw new \RuntimeException('Folder not accessible.', 1430409089);
195 }
196 } catch (Exception\InsufficientFolderAccessPermissionsException $permissionException) {
197 $this->folderObject = NULL;
198 $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class,
199 sprintf(
200 $this->getLanguageService()->getLL('missingFolderPermissionsMessage', TRUE),
201 htmlspecialchars($this->id)
202 ),
203 $this->getLanguageService()->getLL('missingFolderPermissionsTitle', TRUE),
204 FlashMessage::NOTICE
205 );
206 } catch (Exception $fileException) {
207 // Set folder object to null and throw a message later on
208 $this->folderObject = NULL;
209 // Take the first object of the first storage
210 $fileStorages = $this->getBackendUser()->getFileStorages();
211 $fileStorage = reset($fileStorages);
212 if ($fileStorage instanceof \TYPO3\CMS\Core\Resource\ResourceStorage) {
213 $this->folderObject = $fileStorage->getRootLevelFolder();
214 if (!$fileStorage->isWithinFileMountBoundaries($this->folderObject)) {
215 $this->folderObject = NULL;
216 }
217 }
218 $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class,
219 sprintf(
220 $this->getLanguageService()->getLL('folderNotFoundMessage', TRUE),
221 htmlspecialchars($this->id)
222 ),
223 $this->getLanguageService()->getLL('folderNotFoundTitle', TRUE),
224 FlashMessage::NOTICE
225 );
226 } catch (\RuntimeException $e) {
227 $this->folderObject = NULL;
228 $this->errorMessage = GeneralUtility::makeInstance(FlashMessage::class,
229 $e->getMessage() . ' (' . $e->getCode() . ')',
230 $this->getLanguageService()->getLL('folderNotFoundTitle', TRUE),
231 FlashMessage::NOTICE
232 );
233 }
234
235 if ($this->folderObject && !$this->folderObject->getStorage()->checkFolderActionPermission('read', $this->folderObject)) {
236 $this->folderObject = NULL;
237 }
238
239 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
240 $this->menuConfig();
241 }
242
243 /**
244 * Setting the menu/session variables
245 *
246 * @return void
247 */
248 public function menuConfig() {
249 // MENU-ITEMS:
250 // If array, then it's a selector box menu
251 // If empty string it's just a variable, that will be saved.
252 // Values NOT in this array will not be saved in the settings-array for the module.
253 $this->MOD_MENU = array(
254 'sort' => '',
255 'reverse' => '',
256 'displayThumbs' => '',
257 'clipBoard' => '',
258 'bigControlPanel' => ''
259 );
260 // CLEANSE SETTINGS
261 $this->MOD_SETTINGS = BackendUtility::getModuleData(
262 $this->MOD_MENU,
263 GeneralUtility::_GP('SET'),
264 $this->moduleName
265 );
266 }
267
268 /**
269 * @return void
270 */
271 public function initializeIndexAction() {
272 // Apply predefined values for hidden checkboxes
273 // Set predefined value for DisplayBigControlPanel:
274 $backendUser = $this->getBackendUser();
275 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
276 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
277 } elseif ($backendUser->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
278 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
279 }
280 // Set predefined value for DisplayThumbnails:
281 if ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
282 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
283 } elseif ($backendUser->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
284 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
285 }
286 // Set predefined value for Clipboard:
287 if ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
288 $this->MOD_SETTINGS['clipBoard'] = TRUE;
289 } elseif ($backendUser->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
290 $this->MOD_SETTINGS['clipBoard'] = FALSE;
291 }
292 // If user never opened the list module, set the value for displayThumbs
293 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
294 $this->MOD_SETTINGS['displayThumbs'] = $backendUser->uc['thumbnailsByDefault'];
295 }
296 if (!isset($this->MOD_SETTINGS['sort'])) {
297 // Set default sorting
298 $this->MOD_SETTINGS['sort'] = 'file';
299 $this->MOD_SETTINGS['reverse'] = 0;
300 }
301 }
302
303 /**
304 * @return void
305 */
306 public function indexAction() {
307
308 // There there was access to this file path, continue, make the list
309 if ($this->folderObject) {
310
311 $requireJsModules = ['TYPO3/CMS/Filelist/FileListLocalisation', 'TYPO3/CMS/Filelist/FileSearch'];
312 $addJsInlineLabels = [];
313
314 // Create filelisting object
315 $this->filelist = GeneralUtility::makeInstance(FileList::class, $this);
316 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
317 // Create clipboard object and initialize that
318 $this->filelist->clipObj = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Clipboard\Clipboard::class);
319 $this->filelist->clipObj->fileMode = 1;
320 $this->filelist->clipObj->initializeClipboard();
321 $CB = GeneralUtility::_GET('CB');
322 if ($this->cmd == 'setCB') {
323 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(GeneralUtility::_POST('CBH'), (array)GeneralUtility::_POST('CBC')), '_FILE');
324 }
325 if (!$this->MOD_SETTINGS['clipBoard']) {
326 $CB['setP'] = 'normal';
327 }
328 $this->filelist->clipObj->setCmd($CB);
329 $this->filelist->clipObj->cleanCurrent();
330 // Saves
331 $this->filelist->clipObj->endClipboard();
332 // If the "cmd" was to delete files from the list (clipboard thing), do that:
333 if ($this->cmd == 'delete') {
334 $items = $this->filelist->clipObj->cleanUpCBC(GeneralUtility::_POST('CBC'), '_FILE', 1);
335 if (!empty($items)) {
336 // Make command array:
337 $FILE = array();
338 foreach ($items as $v) {
339 $FILE['delete'][] = array('data' => $v);
340 }
341 // Init file processing object for deleting and pass the cmd array.
342 /** @var ExtendedFileUtility $fileProcessor */
343 $fileProcessor = GeneralUtility::makeInstance(ExtendedFileUtility::class);
344 $fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
345 $fileProcessor->setActionPermissions();
346 $fileProcessor->setExistingFilesConflictMode($this->overwriteExistingFiles);
347 $fileProcessor->start($FILE);
348 $fileProcessor->processData();
349 $fileProcessor->pushErrorMessagesToFlashMessageQueue();
350 }
351 }
352 // Start up filelisting object, include settings.
353 $this->pointer = MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
354 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
355 // Generate the list
356 $this->filelist->generateList();
357 // Set top JavaScript:
358 $addJsInline = 'if (top.fsMod) top.fsMod.recentIds["file"] = "' . rawurlencode($this->id) . '";' . $this->filelist->CBfunctions();
359 // Setting up the buttons and markers for docheader
360 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
361 // add the folder info to the marker array
362 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
363 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
364
365 // Include DragUploader only if we have write access
366 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')
367 && $this->folderObject->checkActionPermission('write')
368 ) {
369 $requireJsModules[] = 'TYPO3/CMS/Backend/DragUploader';
370 $addJsInlineLabelFiles[] = [
371 'file' => ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
372 'prefix' => 'file_upload'
373 ];
374 }
375
376 $this->view->assign('otherMarkers', $otherMarkers);
377 $this->view->assign('docHeaderButtons', $docHeaderButtons);
378 $this->view->assign('pageTitle', $this->getLanguageService()->getLL('files'));
379 $this->view->assign('requireJsModules', $requireJsModules);
380 $this->view->assign('addJsInlineLabelFiles', $addJsInlineLabelFiles);
381 $this->view->assign('addJsInline', $addJsInline);
382 $this->view->assign('headline', $this->getModuleHeadline());
383 $this->view->assign('listHtml', $this->filelist->HTMLcode);
384 $this->view->assign('checkboxes', [
385 'bigControlPanel' => [
386 'enabled' => $this->getBackendUser()->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'selectable',
387 'label' => $this->getLanguageService()->getLL('bigControlPanel', TRUE),
388 'html' => BackendUtility::getFuncCheck($this->id, 'SET[bigControlPanel]', $this->MOD_SETTINGS['bigControlPanel'], '', '', 'id="bigControlPanel"'),
389 ],
390 'displayThumbs' => [
391 'enabled' => $this->getBackendUser()->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable',
392 'label' => $this->getLanguageService()->getLL('displayThumbs', TRUE),
393 'html' => BackendUtility::getFuncCheck($this->id, 'SET[displayThumbs]', $this->MOD_SETTINGS['displayThumbs'], '', '', 'id="checkDisplayThumbs"'),
394 ],
395 'enableClipBoard' => [
396 'enabled' => $this->getBackendUser()->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable',
397 'label' => $this->getLanguageService()->getLL('clipBoard', TRUE),
398 'html' => BackendUtility::getFuncCheck($this->id, 'SET[clipBoard]', $this->MOD_SETTINGS['clipBoard'], '', '', 'id="checkClipBoard"'),
399 ]
400 ]);
401 $this->view->assign('showClipBoard', (bool)$this->MOD_SETTINGS['clipBoard']);
402 $this->view->assign('clipBoardHtml', $this->filelist->clipObj->printClipboard());
403 $this->view->assign('folderIdentifier', $this->folderObject->getCombinedIdentifier());
404 $this->view->assign('fileDenyPattern', $GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern']);
405 $this->view->assign('maxFileSize', GeneralUtility::getMaxUploadFileSize() * 1024);
406 } else {
407 $this->forward('missingFolder');
408 }
409 }
410
411 /**
412 * @return void
413 */
414 public function missingFolderAction() {
415 if ($this->errorMessage) {
416 $this->errorMessage->setSeverity(FlashMessage::ERROR);
417 $this->controllerContext->getFlashMessageQueue('core.template.flashMessages')->addMessage($this->errorMessage);
418 }
419 }
420
421 /**
422 * Search for files by name and pass them with a facade to fluid
423 *
424 * @param string $searchWord
425 */
426 public function searchAction($searchWord = '') {
427 if (empty($searchWord)) {
428 $this->forward('index');
429 }
430
431 $fileFacades = [];
432 $files = $this->fileRepository->searchByName($this->folderObject, $searchWord);
433
434 if (empty($files)) {
435 $this->controllerContext->getFlashMessageQueue('core.template.flashMessages')->addMessage(
436 new FlashMessage(LocalizationUtility::translate('flashmessage.no_results', 'filelist'), '', FlashMessage::INFO)
437 );
438 } else {
439 foreach ($files as $file) {
440 $fileFacades[] = new \TYPO3\CMS\Filelist\FileFacade($file);
441 }
442 }
443
444 $this->view->assign('requireJsModules', ['TYPO3/CMS/Filelist/FileList', 'TYPO3/CMS/Filelist/FileSearch']);
445 $this->view->assign('searchWord', $searchWord);
446 $this->view->assign('files', $fileFacades);
447 $this->view->assign('settings', [
448 'jsConfirmationDelete' => $this->getBackendUser()->jsConfirmation(JsConfirmation::DELETE)
449 ]);
450 }
451
452 /**
453 * Get main headline based on active folder or storage for backend module
454 *
455 * Folder names are resolved to their special names like done in the tree view.
456 *
457 * @return string
458 */
459 protected function getModuleHeadline() {
460 $name = $this->folderObject->getName();
461 if ($name === '') {
462 // Show storage name on storage root
463 if ($this->folderObject->getIdentifier() === '/') {
464 $name = $this->folderObject->getStorage()->getName();
465 }
466 } else {
467 $name = key(ListUtility::resolveSpecialFolderNames(
468 array($name => $this->folderObject)
469 ));
470 }
471 return $name;
472 }
473
474 /**
475 * Create the panel of buttons for submitting the form or otherwise perform operations.
476 *
477 * @return array All available buttons as an assoc. array
478 */
479 public function getButtons() {
480 /** @var IconFactory $iconFactory */
481 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
482
483 $buttons = array(
484 'csh' => '',
485 'shortcut' => '',
486 'upload' => '',
487 'new' => ''
488 );
489 // Add shortcut
490 if ($this->getBackendUser()->mayMakeShortcut()) {
491 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->moduleName);
492 }
493 // FileList Module CSH:
494 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module');
495 // Upload button (only if upload to this directory is allowed)
496 if ($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('add', 'File') && $this->folderObject->checkActionPermission('write')) {
497 $buttons['upload'] = '<a href="' . htmlspecialchars(
498 BackendUtility::getModuleUrl(
499 'file_upload',
500 array(
501 'target' => $this->folderObject->getCombinedIdentifier(),
502 'returnUrl' => $this->filelist->listURL(),
503 )
504 )) . '" id="button-upload" title="' . $this->getLanguageService()->makeEntities($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.upload', TRUE)) . '">' . $iconFactory->getIcon('actions-edit-upload', Icon::SIZE_SMALL)->render() . '</a>';
505 }
506 // New folder button
507 if ($this->folderObject && $this->folderObject->checkActionPermission('write')
508 && ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File') || $this->folderObject->checkActionPermission('add'))
509 ) {
510 $buttons['new'] = '<a href="' . htmlspecialchars(
511 BackendUtility::getModuleUrl(
512 'file_newfolder',
513 array(
514 'target' => $this->folderObject->getCombinedIdentifier(),
515 'returnUrl' => $this->filelist->listURL(),
516 )
517 )) . '" title="' . $this->getLanguageService()->makeEntities($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.new', TRUE)) . '">' . $iconFactory->getIcon('actions-document-new', Icon::SIZE_SMALL)->render() . '</a>';
518 }
519 return $buttons;
520 }
521
522 /**
523 * Returns an instance of LanguageService
524 *
525 * @return \TYPO3\CMS\Lang\LanguageService
526 */
527 protected function getLanguageService() {
528 return $GLOBALS['LANG'];
529 }
530
531 /**
532 * Returns the current BE user.
533 *
534 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
535 */
536 protected function getBackendUser() {
537 return $GLOBALS['BE_USER'];
538 }
539 }