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