4a7588aed70881fae8b7aa073f05ca5f51791d03
[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 $this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
149 // Disallow the rendering of the processing folder (e.g. could be called manually)
150 // and all folders without any defined storage
151 if ($this->folderObject && ($this->folderObject->getStorage()->getUid() == 0 || trim($this->folderObject->getStorage()->getProcessingFolder()->getIdentifier(), '/') === trim($this->folderObject->getIdentifier(), '/'))) {
152 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
153 $this->folderObject = $storage->getRootLevelFolder();
154 }
155 } else {
156 // Take the first object of the first storage
157 $fileStorages = $GLOBALS['BE_USER']->getFileStorages();
158 $fileStorage = reset($fileStorages);
159 if ($fileStorage) {
160 // Validating the input "id" (the path, directory!) and
161 // checking it against the mounts of the user. - now done in the controller
162 $this->folderObject = $fileStorage->getRootLevelFolder();
163 } else {
164 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
165 }
166 }
167 } catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $fileException) {
168 // Set folder object to null and throw a message later on
169 $this->folderObject = NULL;
170 $this->errorMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
171 sprintf($GLOBALS['LANG']->getLL('folderNotFoundMessage', TRUE),
172 htmlspecialchars($this->id)
173 ),
174 $GLOBALS['LANG']->getLL('folderNotFoundTitle', TRUE),
175 \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE
176 );
177 }
178 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
179 $this->menuConfig();
180 }
181
182 /**
183 * Setting the menu/session variables
184 *
185 * @return void
186 * @todo Define visibility
187 */
188 public function menuConfig() {
189 // MENU-ITEMS:
190 // If array, then it's a selector box menu
191 // If empty string it's just a variable, that'll be saved.
192 // Values NOT in this array will not be saved in the settings-array for the module.
193 $this->MOD_MENU = array(
194 'sort' => '',
195 'reverse' => '',
196 'displayThumbs' => '',
197 'clipBoard' => '',
198 'bigControlPanel' => ''
199 );
200 // CLEANSE SETTINGS
201 $this->MOD_SETTINGS = BackendUtility::getModuleData(
202 $this->MOD_MENU,
203 GeneralUtility::_GP('SET'),
204 $this->MCONF['name']
205 );
206 }
207
208 /**
209 * Main function, creating the listing
210 *
211 * @return void
212 * @todo Define visibility
213 */
214 public function main() {
215 // Initialize the template object
216 $this->doc = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
217 $this->doc->backPath = $GLOBALS['BACK_PATH'];
218 $this->doc->setModuleTemplate('EXT:filelist/Resources/Private/Templates/file_list.html');
219 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
220 $pageRenderer = $this->doc->getPageRenderer();
221 $pageRenderer->loadPrototype();
222 $pageRenderer->loadJQuery();
223 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DragUploader');
224 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/FileListLocalisation');
225 $pageRenderer->addInlineLanguagelabelFile(
226 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
227 'file_upload'
228 );
229 // There there was access to this file path, continue, make the list
230 if ($this->folderObject) {
231
232 // Create filelisting object
233 $this->filelist = GeneralUtility::makeInstance('TYPO3\\CMS\\Filelist\\FileList');
234 $this->filelist->backPath = $GLOBALS['BACK_PATH'];
235 // Apply predefined values for hidden checkboxes
236 // Set predefined value for DisplayBigControlPanel:
237 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
238 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
239 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
240 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
241 }
242 // Set predefined value for DisplayThumbnails:
243 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
244 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
245 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
246 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
247 }
248 // Set predefined value for Clipboard:
249 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
250 $this->MOD_SETTINGS['clipBoard'] = TRUE;
251 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
252 $this->MOD_SETTINGS['clipBoard'] = FALSE;
253 }
254 // If user never opened the list module, set the value for displayThumbs
255 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
256 $this->MOD_SETTINGS['displayThumbs'] = $GLOBALS['BE_USER']->uc['thumbnailsByDefault'];
257 }
258 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
259 // Create clipboard object and initialize that
260 $this->filelist->clipObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Clipboard\\Clipboard');
261 $this->filelist->clipObj->fileMode = 1;
262 $this->filelist->clipObj->initializeClipboard();
263 $CB = GeneralUtility::_GET('CB');
264 if ($this->cmd == 'setCB') {
265 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(GeneralUtility::_POST('CBH'), GeneralUtility::_POST('CBC')), '_FILE');
266 }
267 if (!$this->MOD_SETTINGS['clipBoard']) {
268 $CB['setP'] = 'normal';
269 }
270 $this->filelist->clipObj->setCmd($CB);
271 $this->filelist->clipObj->cleanCurrent();
272 // Saves
273 $this->filelist->clipObj->endClipboard();
274 // If the "cmd" was to delete files from the list (clipboard thing), do that:
275 if ($this->cmd == 'delete') {
276 $items = $this->filelist->clipObj->cleanUpCBC(GeneralUtility::_POST('CBC'), '_FILE', 1);
277 if (count($items)) {
278 // Make command array:
279 $FILE = array();
280 foreach ($items as $v) {
281 $FILE['delete'][] = array('data' => $v);
282 }
283 // Init file processing object for deleting and pass the cmd array.
284 $fileProcessor = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
285 $fileProcessor->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
286 $fileProcessor->setActionPermissions();
287 $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
288 $fileProcessor->start($FILE);
289 $fileProcessor->processData();
290 $fileProcessor->printLogErrorMessages();
291 }
292 }
293 if (!isset($this->MOD_SETTINGS['sort'])) {
294 // Set default sorting
295 $this->MOD_SETTINGS['sort'] = 'file';
296 $this->MOD_SETTINGS['reverse'] = 0;
297 }
298 // Start up filelisting object, include settings.
299 $this->pointer = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
300 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
301 // Generate the list
302 $this->filelist->generateList();
303 // Set top JavaScript:
304 $this->doc->JScode = $this->doc->wrapScriptTags('
305
306 if (top.fsMod) top.fsMod.recentIds["file"] = "' . rawurlencode($this->id) . '";
307 function jumpToUrl(URL) { //
308 window.location.href = URL;
309 }
310
311 ' . $this->filelist->CBfunctions());
312 // This will return content necessary for the context sensitive clickmenus to work: bodytag events, JavaScript functions and DIV-layers.
313 $this->doc->getContextMenuCode();
314 // Setting up the buttons and markers for docheader
315 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
316 // add the folder info to the marker array
317 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
318 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
319
320 // Build the <body> for the module
321 $moduleHeadline = $this->getModuleHeadline();
322 // Create output
323 $pageContent = $moduleHeadline !== '' ? '<h1>' . $moduleHeadline . '</h1>' : '';
324
325 $pageContent .= '<form action="' . htmlspecialchars($this->filelist->listURL()) . '" method="post" name="dblistForm">';
326 $pageContent .= $this->filelist->HTMLcode;
327 $pageContent .= '<input type="hidden" name="cmd" /></form>';
328
329 // Making listing options:
330 if ($this->filelist->HTMLcode) {
331 $pageContent .= '
332
333 <!--
334 Listing options for extended view, clipboard and thumbnails
335 -->
336 <div id="typo3-listOptions">
337 ';
338 // Add "display bigControlPanel" checkbox:
339 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'selectable') {
340 $pageContent .= BackendUtility::getFuncCheck($this->id, 'SET[bigControlPanel]', $this->MOD_SETTINGS['bigControlPanel'], '', '', 'id="bigControlPanel"') . '<label for="bigControlPanel"> ' . $GLOBALS['LANG']->getLL('bigControlPanel', TRUE) . '</label><br />';
341 }
342 // Add "display thumbnails" checkbox:
343 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable') {
344 $pageContent .= BackendUtility::getFuncCheck($this->id, 'SET[displayThumbs]', $this->MOD_SETTINGS['displayThumbs'], '', '', 'id="checkDisplayThumbs"') . ' <label for="checkDisplayThumbs">' . $GLOBALS['LANG']->getLL('displayThumbs', TRUE) . '</label><br />';
345 }
346 // Add "clipboard" checkbox:
347 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable') {
348 $pageContent .= BackendUtility::getFuncCheck($this->id, 'SET[clipBoard]', $this->MOD_SETTINGS['clipBoard'], '', '', 'id="checkClipBoard"') . ' <label for="checkClipBoard">' . $GLOBALS['LANG']->getLL('clipBoard', TRUE) . '</label>';
349 }
350 $pageContent .= '
351 </div>
352 ';
353 // Set clipboard:
354 if ($this->MOD_SETTINGS['clipBoard']) {
355 $pageContent .= $this->filelist->clipObj->printClipboard();
356 $pageContent .= BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_clipboard', $GLOBALS['BACK_PATH']);
357 }
358 }
359 $markerArray = array(
360 'CSH' => $docHeaderButtons['csh'],
361 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
362 'CONTENT' => ($this->errorMessage ? $this->errorMessage->render() : '') . $pageContent,
363 'FOLDER_IDENTIFIER' => $this->folderObject->getCombinedIdentifier(),
364 'FILEDENYPATERN' => $GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],
365 'MAXFILESIZE' => GeneralUtility::getMaxUploadFileSize() * 1024,
366 );
367 $this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
368 // Renders the module page
369 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $this->content);
370 } else {
371 $content = '';
372 if ($this->errorMessage) {
373 $this->errorMessage->setSeverity(\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
374 $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()));
375 }
376 // Create output - no access (no warning though)
377 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $content);
378 }
379 }
380
381 /**
382 * Get main headline based on active folder or storage for backend module
383 *
384 * Folder names are resolved to their special names like done in the tree view.
385 *
386 * @return string
387 */
388 protected function getModuleHeadline() {
389 $name = $this->folderObject->getName();
390 if ($name === '') {
391 // Show storage name on storage root
392 if ($this->folderObject->getIdentifier() === '/') {
393 $name = $this->folderObject->getStorage()->getName();
394 }
395 } else {
396 $name = key(\TYPO3\CMS\Core\Resource\Utility\ListUtility::resolveSpecialFolderNames(
397 array($name => $this->folderObject)
398 ));
399 }
400 return $name;
401 }
402
403 /**
404 * Outputting the accumulated content to screen
405 *
406 * @return void
407 * @todo Define visibility
408 */
409 public function printContent() {
410 echo $this->content;
411 }
412
413 /**
414 * Create the panel of buttons for submitting the form or otherwise perform operations.
415 *
416 * @return array All available buttons as an assoc. array
417 * @todo Define visibility
418 */
419 public function getButtons() {
420 $buttons = array(
421 'csh' => '',
422 'shortcut' => '',
423 'upload' => '',
424 'new' => ''
425 );
426 // Add shortcut
427 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
428 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->MCONF['name']);
429 }
430 // FileList Module CSH:
431 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module', $GLOBALS['BACK_PATH'], '', TRUE);
432 // Upload button (only if upload to this directory is allowed)
433 if ($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('add', 'File') && $this->folderObject->checkActionPermission('write')) {
434 $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>';
435 }
436 // New folder button
437 if ($this->folderObject && ($this->folderObject->checkActionPermission('add') || $this->folderObject->getStorage()->checkUserActionPermission('add', 'File'))) {
438 $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>';
439 }
440 return $buttons;
441 }
442
443 }