[TASK] Deleting currently opened folder in filelist results in error
[Packages/TYPO3.CMS.git] / typo3 / sysext / filelist / Classes / Controller / FileListController.php
1 <?php
2 namespace TYPO3\CMS\Filelist\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33 /**
34 * Script Class for creating the list of files in the File > Filelist module
35 *
36 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
37 */
38 class FileListController {
39
40 /**
41 * Module configuration
42 *
43 * @todo Define visibility
44 */
45 public $MCONF = array();
46
47 /**
48 * @todo Define visibility
49 */
50 public $MOD_MENU = array();
51
52 /**
53 * @todo Define visibility
54 */
55 public $MOD_SETTINGS = array();
56
57 /**
58 * Accumulated HTML output
59 *
60 * @todo Define visibility
61 */
62 public $content;
63
64 /**
65 * Document template object
66 *
67 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
68 * @todo Define visibility
69 */
70 public $doc;
71
72 /**
73 * "id" -> the path to list.
74 *
75 * @todo Define visibility
76 */
77 public $id;
78
79 /**
80 * @var \TYPO3\CMS\Core\Resource\Folder
81 */
82 protected $folderObject;
83
84 /**
85 * @var \TYPO3\CMS\Core\Messaging\FlashMessage
86 */
87 protected $errorMessage;
88
89 /**
90 * Pointer to listing
91 *
92 * @todo Define visibility
93 */
94 public $pointer;
95
96 /**
97 * "Table"
98 *
99 * @todo Define visibility
100 */
101 public $table;
102
103 /**
104 * Thumbnail mode.
105 *
106 * @todo Define visibility
107 */
108 public $imagemode;
109
110 /**
111 * @todo Define visibility
112 */
113 public $cmd;
114
115 /**
116 * @todo Define visibility
117 */
118 public $overwriteExistingFiles;
119
120 /**
121 * The file list object
122 *
123 * @var \TYPO3\CMS\Filelist\FileList
124 */
125 public $filelist = NULL;
126
127 /**
128 * Initialize variables, file object
129 * Incoming GET vars include id, pointer, table, imagemode
130 *
131 * @return void
132 * @todo Define visibility
133 */
134 public function init() {
135 // Setting GPvars:
136 $this->id = ($combinedIdentifier = GeneralUtility::_GP('id'));
137 $this->pointer = GeneralUtility::_GP('pointer');
138 $this->table = GeneralUtility::_GP('table');
139 $this->imagemode = GeneralUtility::_GP('imagemode');
140 $this->cmd = GeneralUtility::_GP('cmd');
141 $this->overwriteExistingFiles = GeneralUtility::_GP('overwriteExistingFiles');
142 // Setting module name:
143 $this->MCONF = $GLOBALS['MCONF'];
144 try {
145 if ($combinedIdentifier) {
146 /** @var $fileFactory \TYPO3\CMS\Core\Resource\ResourceFactory */
147 $fileFactory = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
148 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
149 $identifier = substr($combinedIdentifier, strpos($combinedIdentifier, ':') + 1);
150 if (!$storage->hasFolder($identifier)) {
151 $identifier = $storage->getFolderIdentifierFromFileIdentifier($identifier);
152 }
153
154 $this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($storage->getUid() . ':' . $identifier);
155 // Disallow the rendering of the processing folder (e.g. could be called manually)
156 // and all folders without any defined storage
157 if ($this->folderObject && ($this->folderObject->getStorage()->getUid() == 0 || trim($this->folderObject->getStorage()->getProcessingFolder()->getIdentifier(), '/') === trim($this->folderObject->getIdentifier(), '/'))) {
158 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
159 $this->folderObject = $storage->getRootLevelFolder();
160 }
161 } else {
162 // Take the first object of the first storage
163 $fileStorages = $GLOBALS['BE_USER']->getFileStorages();
164 $fileStorage = reset($fileStorages);
165 if ($fileStorage) {
166 // Validating the input "id" (the path, directory!) and
167 // checking it against the mounts of the user. - now done in the controller
168 $this->folderObject = $fileStorage->getRootLevelFolder();
169 } else {
170 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
171 }
172 }
173 } catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $fileException) {
174 // Take the first object of the first storage
175 $fileStorages = $GLOBALS['BE_USER']->getFileStorages();
176 $fileStorage = reset($fileStorages);
177 if ($fileStorage) {
178 // Set folder object to null and throw a message later on
179 $this->folderObject = $fileStorage->getRootLevelFolder();
180 } else {
181 $this->folderObject = NULL;
182 }
183 $this->errorMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
184 sprintf($GLOBALS['LANG']->getLL('folderNotFoundMessage', TRUE),
185 htmlspecialchars($this->id)
186 ),
187 $GLOBALS['LANG']->getLL('folderNotFoundTitle', TRUE),
188 \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE
189 );
190 }
191 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
192 $this->menuConfig();
193 }
194
195 /**
196 * Setting the menu/session variables
197 *
198 * @return void
199 * @todo Define visibility
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'll 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->MCONF['name']
218 );
219 }
220
221 /**
222 * Main function, creating the listing
223 *
224 * @return void
225 * @todo Define visibility
226 */
227 public function main() {
228 // Initialize the template object
229 $this->doc = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
230 $this->doc->backPath = $GLOBALS['BACK_PATH'];
231 $this->doc->setModuleTemplate('EXT:filelist/Resources/Private/Templates/file_list.html');
232 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
233 $pageRenderer = $this->doc->getPageRenderer();
234 $pageRenderer->loadPrototype();
235 $pageRenderer->loadJQuery();
236 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DragUploader');
237 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/FileListLocalisation');
238 $pageRenderer->addInlineLanguagelabelFile(
239 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
240 'file_upload'
241 );
242 // There there was access to this file path, continue, make the list
243 if ($this->folderObject) {
244
245 // Create filelisting object
246 $this->filelist = GeneralUtility::makeInstance('TYPO3\\CMS\\Filelist\\FileList');
247 $this->filelist->backPath = $GLOBALS['BACK_PATH'];
248 // Apply predefined values for hidden checkboxes
249 // Set predefined value for DisplayBigControlPanel:
250 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
251 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
252 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
253 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
254 }
255 // Set predefined value for DisplayThumbnails:
256 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
257 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
258 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
259 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
260 }
261 // Set predefined value for Clipboard:
262 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
263 $this->MOD_SETTINGS['clipBoard'] = TRUE;
264 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
265 $this->MOD_SETTINGS['clipBoard'] = FALSE;
266 }
267 // If user never opened the list module, set the value for displayThumbs
268 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
269 $this->MOD_SETTINGS['displayThumbs'] = $GLOBALS['BE_USER']->uc['thumbnailsByDefault'];
270 }
271 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
272 // Create clipboard object and initialize that
273 $this->filelist->clipObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Clipboard\\Clipboard');
274 $this->filelist->clipObj->fileMode = 1;
275 $this->filelist->clipObj->initializeClipboard();
276 $CB = GeneralUtility::_GET('CB');
277 if ($this->cmd == 'setCB') {
278 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(GeneralUtility::_POST('CBH'), GeneralUtility::_POST('CBC')), '_FILE');
279 }
280 if (!$this->MOD_SETTINGS['clipBoard']) {
281 $CB['setP'] = 'normal';
282 }
283 $this->filelist->clipObj->setCmd($CB);
284 $this->filelist->clipObj->cleanCurrent();
285 // Saves
286 $this->filelist->clipObj->endClipboard();
287 // If the "cmd" was to delete files from the list (clipboard thing), do that:
288 if ($this->cmd == 'delete') {
289 $items = $this->filelist->clipObj->cleanUpCBC(GeneralUtility::_POST('CBC'), '_FILE', 1);
290 if (count($items)) {
291 // Make command array:
292 $FILE = array();
293 foreach ($items as $v) {
294 $FILE['delete'][] = array('data' => $v);
295 }
296 // Init file processing object for deleting and pass the cmd array.
297 $fileProcessor = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
298 $fileProcessor->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
299 $fileProcessor->setActionPermissions();
300 $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
301 $fileProcessor->start($FILE);
302 $fileProcessor->processData();
303 $fileProcessor->printLogErrorMessages();
304 }
305 }
306 if (!isset($this->MOD_SETTINGS['sort'])) {
307 // Set default sorting
308 $this->MOD_SETTINGS['sort'] = 'file';
309 $this->MOD_SETTINGS['reverse'] = 0;
310 }
311 // Start up filelisting object, include settings.
312 $this->pointer = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
313 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
314 // Generate the list
315 $this->filelist->generateList();
316 // Set top JavaScript:
317 $this->doc->JScode = $this->doc->wrapScriptTags('
318
319 if (top.fsMod) top.fsMod.recentIds["file"] = "' . rawurlencode($this->id) . '";
320 function jumpToUrl(URL) { //
321 window.location.href = URL;
322 }
323
324 ' . $this->filelist->CBfunctions());
325 // This will return content necessary for the context sensitive clickmenus to work: bodytag events, JavaScript functions and DIV-layers.
326 $this->doc->getContextMenuCode();
327 // Setting up the buttons and markers for docheader
328 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
329 // add the folder info to the marker array
330 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
331 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
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 .= BackendUtility::getFuncCheck($this->id, 'SET[bigControlPanel]', $this->MOD_SETTINGS['bigControlPanel'], '', '', 'id="bigControlPanel"') . '<label for="bigControlPanel"> ' . $GLOBALS['LANG']->getLL('bigControlPanel', TRUE) . '</label><br />';
354 }
355 // Add "display thumbnails" checkbox:
356 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable') {
357 $pageContent .= BackendUtility::getFuncCheck($this->id, 'SET[displayThumbs]', $this->MOD_SETTINGS['displayThumbs'], '', '', 'id="checkDisplayThumbs"') . ' <label for="checkDisplayThumbs">' . $GLOBALS['LANG']->getLL('displayThumbs', TRUE) . '</label><br />';
358 }
359 // Add "clipboard" checkbox:
360 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable') {
361 $pageContent .= BackendUtility::getFuncCheck($this->id, 'SET[clipBoard]', $this->MOD_SETTINGS['clipBoard'], '', '', 'id="checkClipBoard"') . ' <label for="checkClipBoard">' . $GLOBALS['LANG']->getLL('clipBoard', TRUE) . '</label>';
362 }
363 $pageContent .= '
364 </div>
365 ';
366 // Set clipboard:
367 if ($this->MOD_SETTINGS['clipBoard']) {
368 $pageContent .= $this->filelist->clipObj->printClipboard();
369 $pageContent .= BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_clipboard', $GLOBALS['BACK_PATH']);
370 }
371 }
372 $markerArray = array(
373 'CSH' => $docHeaderButtons['csh'],
374 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
375 'CONTENT' => ($this->errorMessage ? $this->errorMessage->render() : '') . $pageContent,
376 'FOLDER_IDENTIFIER' => $this->folderObject->getCombinedIdentifier(),
377 'FILEDENYPATERN' => $GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],
378 'MAXFILESIZE' => GeneralUtility::getMaxUploadFileSize() * 1024,
379 );
380 $this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
381 // Renders the module page
382 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $this->content);
383 } else {
384 $content = '';
385 if ($this->errorMessage) {
386 $this->errorMessage->setSeverity(\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
387 $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()));
388 }
389 // Create output - no access (no warning though)
390 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $content);
391 }
392 }
393
394 /**
395 * Get main headline based on active folder or storage for backend module
396 *
397 * Folder names are resolved to their special names like done in the tree view.
398 *
399 * @return string
400 */
401 protected function getModuleHeadline() {
402 $name = $this->folderObject->getName();
403 if ($name === '') {
404 // Show storage name on storage root
405 if ($this->folderObject->getIdentifier() === '/') {
406 $name = $this->folderObject->getStorage()->getName();
407 }
408 } else {
409 $name = key(\TYPO3\CMS\Core\Resource\Utility\ListUtility::resolveSpecialFolderNames(
410 array($name => $this->folderObject)
411 ));
412 }
413 return $name;
414 }
415
416 /**
417 * Outputting the accumulated content to screen
418 *
419 * @return void
420 * @todo Define visibility
421 */
422 public function printContent() {
423 echo $this->content;
424 }
425
426 /**
427 * Create the panel of buttons for submitting the form or otherwise perform operations.
428 *
429 * @return array All available buttons as an assoc. array
430 * @todo Define visibility
431 */
432 public function getButtons() {
433 $buttons = array(
434 'csh' => '',
435 'shortcut' => '',
436 'upload' => '',
437 'new' => ''
438 );
439 // Add shortcut
440 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
441 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->MCONF['name']);
442 }
443 // FileList Module CSH:
444 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module', $GLOBALS['BACK_PATH'], '', TRUE);
445 // Upload button (only if upload to this directory is allowed)
446 if ($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('add', 'File') && $this->folderObject->checkActionPermission('write')) {
447 $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.xlf:cm.upload', TRUE)) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-edit-upload') . '</a>';
448 }
449 // New folder button
450 if ($this->folderObject && ($this->folderObject->checkActionPermission('add') || $this->folderObject->getStorage()->checkUserActionPermission('add', 'File'))) {
451 $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.xlf:cm.new', TRUE)) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new') . '</a>';
452 }
453 return $buttons;
454 }
455
456 }