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