[BUGFIX] Root-level folders cannot be displayed
[Packages/TYPO3.CMS.git] / typo3 / sysext / filelist / Classes / Controller / FileListController.php
1 <?php
2 namespace TYPO3\CMS\Filelist\Controller;
3
4 /**
5 * Script Class for creating the list of files in the File > Filelist module
6 *
7 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
8 * @package TYPO3
9 * @subpackage core
10 */
11 class FileListController {
12
13 // Module configuration
14 /**
15 * @todo Define visibility
16 */
17 public $MCONF = array();
18
19 /**
20 * @todo Define visibility
21 */
22 public $MOD_MENU = array();
23
24 /**
25 * @todo Define visibility
26 */
27 public $MOD_SETTINGS = array();
28
29 // Internal:
30 // Accumulated HTML output
31 /**
32 * @todo Define visibility
33 */
34 public $content;
35
36 /**
37 * Document template object
38 *
39 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
40 * @todo Define visibility
41 */
42 public $doc;
43
44 // Internal, static: GPvars:
45 // "id" -> the path to list.
46 /**
47 * @todo Define visibility
48 */
49 public $id;
50
51 /* @var \TYPO3\CMS\Core\Resource\Folder $folderObject */
52 protected $folderObject;
53
54 /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $errorMessage */
55 protected $errorMessage;
56
57 // Pointer to listing
58 /**
59 * @todo Define visibility
60 */
61 public $pointer;
62
63 // "Table"
64 /**
65 * @todo Define visibility
66 */
67 public $table;
68
69 // Thumbnail mode.
70 /**
71 * @todo Define visibility
72 */
73 public $imagemode;
74
75 /**
76 * @todo Define visibility
77 */
78 public $cmd;
79
80 /**
81 * @todo Define visibility
82 */
83 public $overwriteExistingFiles;
84
85 /**
86 * Initialize variables, file object
87 * Incoming GET vars include id, pointer, table, imagemode
88 *
89 * @return void
90 * @todo Define visibility
91 */
92 public function init() {
93 // Setting GPvars:
94 $this->id = ($combinedIdentifier = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id'));
95 $this->pointer = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('pointer');
96 $this->table = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('table');
97 $this->imagemode = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('imagemode');
98 $this->cmd = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('cmd');
99 $this->overwriteExistingFiles = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('overwriteExistingFiles');
100 // Setting module name:
101 $this->MCONF = $GLOBALS['MCONF'];
102 try {
103 if ($combinedIdentifier) {
104 /** @var $fileFactory \TYPO3\CMS\Core\Resource\ResourceFactory */
105 $fileFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
106 $this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
107 // Disallow the rendering of the processing folder (e.g. could be called manually)
108 // and all folders without any defined storage
109 if ($this->folderObject && ($this->folderObject->getStorage()->getUid() == 0 || trim($this->folderObject->getStorage()->getProcessingFolder()->getIdentifier(), '/') === trim($this->folderObject->getIdentifier(), '/'))) {
110 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
111 $this->folderObject = $storage->getRootLevelFolder();
112 }
113 } else {
114 // Take the first object of the first storage
115 $fileStorages = $GLOBALS['BE_USER']->getFileStorages();
116 $fileStorage = reset($fileStorages);
117 if ($fileStorage) {
118 // Validating the input "id" (the path, directory!) and
119 // checking it against the mounts of the user. - now done in the controller
120 $this->folderObject = $fileStorage->getRootLevelFolder();
121 } else {
122 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
123 }
124 }
125 } catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $fileException) {
126 // Set folder object to null and throw a message later on
127 $this->folderObject = NULL;
128 $this->errorMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
129 sprintf($GLOBALS['LANG']->getLL('folderNotFoundMessage', TRUE),
130 htmlspecialchars($this->id)
131 ),
132 $GLOBALS['LANG']->getLL('folderNotFoundTitle', TRUE),
133 \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE
134 );
135 }
136 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
137 $this->menuConfig();
138 }
139
140 /**
141 * Setting the menu/session variables
142 *
143 * @return void
144 * @todo Define visibility
145 */
146 public function menuConfig() {
147 // MENU-ITEMS:
148 // If array, then it's a selector box menu
149 // If empty string it's just a variable, that'll be saved.
150 // Values NOT in this array will not be saved in the settings-array for the module.
151 $this->MOD_MENU = array(
152 'sort' => '',
153 'reverse' => '',
154 'displayThumbs' => '',
155 'clipBoard' => '',
156 'bigControlPanel' => ''
157 );
158 // CLEANSE SETTINGS
159 $this->MOD_SETTINGS = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData($this->MOD_MENU, \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET'), $this->MCONF['name']);
160 }
161
162 /**
163 * Main function, creating the listing
164 *
165 * @return void
166 * @todo Define visibility
167 */
168 public function main() {
169 // Initialize the template object
170 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
171 $this->doc->backPath = $GLOBALS['BACK_PATH'];
172 $this->doc->setModuleTemplate('templates/file_list.html');
173 $this->doc->getPageRenderer()->loadPrototype();
174 // There there was access to this file path, continue, make the list
175 if ($this->folderObject) {
176 // Include the initialization for the flash uploader
177 if ($GLOBALS['BE_USER']->uc['enableFlashUploader']) {
178 $this->doc->JScodeArray['flashUploader'] = '
179 if (top.TYPO3.FileUploadWindow.isFlashAvailable()) {
180 document.observe("dom:loaded", function() {
181 // monitor the button
182 $("button-upload").observe("click", initFlashUploader);
183
184 function initFlashUploader(event) {
185 // set the page specific options for the flashUploader
186 var flashUploadOptions = {
187 uploadURL: top.TS.PATH_typo3 + "ajax.php",
188 uploadFileSizeLimit: "' . \TYPO3\CMS\Core\Utility\GeneralUtility::getMaxUploadFileSize() . '",
189 uploadFileTypes: {
190 allow: "' . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace']['allow'] . '",
191 deny: "' . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace']['deny'] . '"
192 },
193 uploadFilePostName: "upload_1",
194 uploadPostParams: {
195 "file[upload][1][target]": "' . ($this->folderObject ? $this->folderObject->getCombinedIdentifier() : '') . '",
196 "file[upload][1][data]": 1,
197 "file[upload][1][charset]": "utf-8",
198 "ajaxID": "TYPO3\\CMS\\Backend\\Controller\\File\\FileController::process"
199 }
200 };
201
202 // get the flashUploaderWindow instance from the parent frame
203 var flashUploader = top.TYPO3.FileUploadWindow.getInstance(flashUploadOptions);
204 // add an additional function inside the container to show the checkbox option
205 var infoComponent = new top.Ext.Panel({
206 autoEl: { tag: "div" },
207 height: "auto",
208 bodyBorder: false,
209 border: false,
210 hideBorders: true,
211 cls: "t3-upload-window-infopanel",
212 id: "t3-upload-window-infopanel-addition",
213 html: \'<label for="overrideExistingFilesCheckbox"><input id="overrideExistingFilesCheckbox" type="checkbox" onclick="setFlashPostOptionOverwriteExistingFiles(this);" />\' + top.String.format(top.TYPO3.LLL.fileUpload.infoComponentOverrideFiles) + \'</label>\'
214 });
215 flashUploader.add(infoComponent);
216
217 // do a reload of this frame once all uploads are done
218 flashUploader.on("totalcomplete", function() {
219 window.location.reload();
220 });
221
222 // this is the callback function that delivers the additional post parameter to the flash application
223 top.setFlashPostOptionOverwriteExistingFiles = function(checkbox) {
224 var uploader = top.TYPO3.getInstance("FileUploadWindow");
225 if (uploader.isVisible()) {
226 uploader.swf.addPostParam("overwriteExistingFiles", (checkbox.checked == true ? 1 : 0));
227 }
228 };
229
230 event.stop();
231 };
232 });
233 }
234 ';
235 }
236 // Create filelisting object
237 $this->filelist = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Filelist\\FileList');
238 $this->filelist->backPath = $GLOBALS['BACK_PATH'];
239 // Apply predefined values for hidden checkboxes
240 // Set predefined value for DisplayBigControlPanel:
241 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
242 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
243 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
244 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
245 }
246 // Set predefined value for DisplayThumbnails:
247 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
248 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
249 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
250 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
251 }
252 // Set predefined value for Clipboard:
253 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
254 $this->MOD_SETTINGS['clipBoard'] = TRUE;
255 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
256 $this->MOD_SETTINGS['clipBoard'] = FALSE;
257 }
258 // If user never opened the list module, set the value for displayThumbs
259 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
260 $this->MOD_SETTINGS['displayThumbs'] = $GLOBALS['BE_USER']->uc['thumbnailsByDefault'];
261 }
262 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
263 // Create clipboard object and initialize that
264 $this->filelist->clipObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Clipboard\\Clipboard');
265 $this->filelist->clipObj->fileMode = 1;
266 $this->filelist->clipObj->initializeClipboard();
267 $CB = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('CB');
268 if ($this->cmd == 'setCB') {
269 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBH'), \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBC')), '_FILE');
270 }
271 if (!$this->MOD_SETTINGS['clipBoard']) {
272 $CB['setP'] = 'normal';
273 }
274 $this->filelist->clipObj->setCmd($CB);
275 $this->filelist->clipObj->cleanCurrent();
276 // Saves
277 $this->filelist->clipObj->endClipboard();
278 // If the "cmd" was to delete files from the list (clipboard thing), do that:
279 if ($this->cmd == 'delete') {
280 $items = $this->filelist->clipObj->cleanUpCBC(\TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBC'), '_FILE', 1);
281 if (count($items)) {
282 // Make command array:
283 $FILE = array();
284 foreach ($items as $v) {
285 $FILE['delete'][] = array('data' => $v);
286 }
287 // Init file processing object for deleting and pass the cmd array.
288 $fileProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
289 $fileProcessor->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
290 $fileProcessor->init_actionPerms($GLOBALS['BE_USER']->getFileoperationPermissions());
291 $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
292 $fileProcessor->start($FILE);
293 $fileProcessor->processData();
294 $fileProcessor->printLogErrorMessages();
295 }
296 }
297 if (!isset($this->MOD_SETTINGS['sort'])) {
298 // Set default sorting
299 $this->MOD_SETTINGS['sort'] = 'file';
300 $this->MOD_SETTINGS['reverse'] = 0;
301 }
302 // Start up filelisting object, include settings.
303 $this->pointer = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
304 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
305 // Generate the list
306 $this->filelist->generateList();
307 // Write the footer
308 $this->filelist->writeBottom();
309 // Set top JavaScript:
310 $this->doc->JScode = $this->doc->wrapScriptTags('
311
312 if (top.fsMod) top.fsMod.recentIds["file"] = unescape("' . rawurlencode($this->id) . '");
313 function jumpToUrl(URL) { //
314 window.location.href = URL;
315 }
316
317 ' . $this->filelist->CBfunctions());
318 // This will return content necessary for the context sensitive clickmenus to work: bodytag events, JavaScript functions and DIV-layers.
319 $this->doc->getContextMenuCode();
320 // Setting up the buttons and markers for docheader
321 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
322 // add the folder info to the marker array
323 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
324 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
325 // Build the <body> for the module
326 // Create output
327 $pageContent = '';
328 $pageContent .= '<form action="' . htmlspecialchars($this->filelist->listURL()) . '" method="post" name="dblistForm">';
329 $pageContent .= $this->filelist->HTMLcode;
330 $pageContent .= '<input type="hidden" name="cmd" /></form>';
331 // Making listing options:
332 if ($this->filelist->HTMLcode) {
333 $pageContent .= '
334
335 <!--
336 Listing options for extended view, clipboard and thumbnails
337 -->
338 <div id="typo3-listOptions">
339 ';
340 // Add "display bigControlPanel" checkbox:
341 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'selectable') {
342 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->id, 'SET[bigControlPanel]', $this->MOD_SETTINGS['bigControlPanel'], '', '', 'id="bigControlPanel"') . '<label for="bigControlPanel"> ' . $GLOBALS['LANG']->getLL('bigControlPanel', TRUE) . '</label><br />';
343 }
344 // Add "display thumbnails" checkbox:
345 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable') {
346 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->id, 'SET[displayThumbs]', $this->MOD_SETTINGS['displayThumbs'], '', '', 'id="checkDisplayThumbs"') . ' <label for="checkDisplayThumbs">' . $GLOBALS['LANG']->getLL('displayThumbs', TRUE) . '</label><br />';
347 }
348 // Add "clipboard" checkbox:
349 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable') {
350 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->id, 'SET[clipBoard]', $this->MOD_SETTINGS['clipBoard'], '', '', 'id="checkClipBoard"') . ' <label for="checkClipBoard">' . $GLOBALS['LANG']->getLL('clipBoard', TRUE) . '</label>';
351 }
352 $pageContent .= '
353 </div>
354 ';
355 // Set clipboard:
356 if ($this->MOD_SETTINGS['clipBoard']) {
357 $pageContent .= $this->filelist->clipObj->printClipboard();
358 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_clipboard', $GLOBALS['BACK_PATH']);
359 }
360 }
361 $markerArray = array(
362 'CSH' => $docHeaderButtons['csh'],
363 'FUNC_MENU' => \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
364 'CONTENT' => ($this->errorMessage ? $this->errorMessage->render() : '') . $pageContent
365 );
366 $this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
367 // Renders the module page
368 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $this->content);
369 } else {
370 $content = '';
371 if ($this->errorMessage) {
372 $this->errorMessage->setSeverity(\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
373 $content = $this->doc->moduleBody(array(), array_merge(array('LEVEL_UP' => '', 'REFRESH' => ''), $this->getButtons()), array('CSH' => '', 'TITLE' => '', 'FOLDER_INFO' => '', 'PAGE_ICON' => '', 'FUNC_MENU' => '', 'CONTENT' => $this->errorMessage->render()));
374 }
375 // Create output - no access (no warning though)
376 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $content);
377 }
378 }
379
380 /**
381 * Outputting the accumulated content to screen
382 *
383 * @return void
384 * @todo Define visibility
385 */
386 public function printContent() {
387 echo $this->content;
388 }
389
390 /**
391 * Create the panel of buttons for submitting the form or otherwise perform operations.
392 *
393 * @return array All available buttons as an assoc. array
394 * @todo Define visibility
395 */
396 public function getButtons() {
397 $buttons = array(
398 'csh' => '',
399 'shortcut' => '',
400 'upload' => '',
401 'new' => ''
402 );
403 // Add shortcut
404 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
405 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->MCONF['name']);
406 }
407 // FileList Module CSH:
408 $buttons['csh'] = \TYPO3\CMS\Backend\Utility\BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module', $GLOBALS['BACK_PATH'], '', TRUE);
409 // Upload button (only if upload to this directory is allowed)
410 if ($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('upload', 'File') && $this->folderObject->checkActionPermission('write')) {
411 $buttons['upload'] = '<a href="' . $GLOBALS['BACK_PATH'] . 'file_upload.php?target=' . rawurlencode($this->folderObject->getCombinedIdentifier()) . '&amp;returnUrl=' . rawurlencode($this->filelist->listURL()) . '" id="button-upload" title="' . $GLOBALS['LANG']->makeEntities($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:cm.upload', 1)) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-edit-upload') . '</a>';
412 }
413 // New folder button
414 if ($this->folderObject && $this->folderObject->checkActionPermission('add')) {
415 $buttons['new'] = '<a href="' . $GLOBALS['BACK_PATH'] . 'file_newfolder.php?target=' . rawurlencode($this->folderObject->getCombinedIdentifier()) . '&amp;returnUrl=' . rawurlencode($this->filelist->listURL()) . '" title="' . $GLOBALS['LANG']->makeEntities($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:cm.new', 1)) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new') . '</a>';
416 }
417 return $buttons;
418 }
419
420 }
421
422
423 ?>