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