f9c3dc94e9cd2f88808664b3469065eb9e8d5c36
[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 t3lib_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 // Create the folder object
103 try {
104 if ($combinedIdentifier) {
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 $this->folderObject = NULL;
111 }
112 } else {
113 // Take the first object of the first storage
114 $fileStorages = $GLOBALS['BE_USER']->getFileStorages();
115 $fileStorage = reset($fileStorages);
116 if ($fileStorage) {
117 // Validating the input "id" (the path, directory!) and
118 // checking it against the mounts of the user. - now done in the controller
119 $this->folderObject = $fileStorage->getRootLevelFolder();
120 } else {
121 $this->folderObject = NULL;
122 }
123 }
124 } catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $fileException) {
125 // Set folder object to null and throw a message later on
126 $this->folderObject = NULL;
127 $this->errorMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', sprintf($GLOBALS['LANG']->getLL('folderNotFoundMessage', TRUE), htmlspecialchars($this->id)), $GLOBALS['LANG']->getLL('folderNotFoundTitle', TRUE), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
128 }
129 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
130 $this->menuConfig();
131 }
132
133 /**
134 * Setting the menu/session variables
135 *
136 * @return void
137 * @todo Define visibility
138 */
139 public function menuConfig() {
140 // MENU-ITEMS:
141 // If array, then it's a selector box menu
142 // If empty string it's just a variable, that'll be saved.
143 // Values NOT in this array will not be saved in the settings-array for the module.
144 $this->MOD_MENU = array(
145 'sort' => '',
146 'reverse' => '',
147 'displayThumbs' => '',
148 'clipBoard' => '',
149 'bigControlPanel' => ''
150 );
151 // CLEANSE SETTINGS
152 $this->MOD_SETTINGS = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData($this->MOD_MENU, \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET'), $this->MCONF['name']);
153 }
154
155 /**
156 * Main function, creating the listing
157 *
158 * @return void
159 * @todo Define visibility
160 */
161 public function main() {
162 // Initialize the template object
163 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
164 $this->doc->backPath = $GLOBALS['BACK_PATH'];
165 $this->doc->setModuleTemplate('templates/file_list.html');
166 $this->doc->getPageRenderer()->loadPrototype();
167 // There there was access to this file path, continue, make the list
168 if ($this->folderObject) {
169 // Include the initialization for the flash uploader
170 if ($GLOBALS['BE_USER']->uc['enableFlashUploader']) {
171 $this->doc->JScodeArray['flashUploader'] = ((((((('
172 if (top.TYPO3.FileUploadWindow.isFlashAvailable()) {
173 document.observe("dom:loaded", function() {
174 // monitor the button
175 $("button-upload").observe("click", initFlashUploader);
176
177 function initFlashUploader(event) {
178 // set the page specific options for the flashUploader
179 var flashUploadOptions = {
180 uploadURL: top.TS.PATH_typo3 + "ajax.php",
181 uploadFileSizeLimit: "' . \TYPO3\CMS\Core\Utility\GeneralUtility::getMaxUploadFileSize()) . '",
182 uploadFileTypes: {
183 allow: "') . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace']['allow']) . '",
184 deny: "') . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace']['deny']) . '"
185 },
186 uploadFilePostName: "upload_1",
187 uploadPostParams: {
188 "file[upload][1][target]": "') . ($this->folderObject ? $this->folderObject->getCombinedIdentifier() : '')) . '",
189 "file[upload][1][data]": 1,
190 "file[upload][1][charset]": "utf-8",
191 "ajaxID": "TYPO3\\CMS\\Backend\\Controller\\File\\FileController::process"
192 }
193 };
194
195 // get the flashUploaderWindow instance from the parent frame
196 var flashUploader = top.TYPO3.FileUploadWindow.getInstance(flashUploadOptions);
197 // add an additional function inside the container to show the checkbox option
198 var infoComponent = new top.Ext.Panel({
199 autoEl: { tag: "div" },
200 height: "auto",
201 bodyBorder: false,
202 border: false,
203 hideBorders: true,
204 cls: "t3-upload-window-infopanel",
205 id: "t3-upload-window-infopanel-addition",
206 html: \'<label for="overrideExistingFilesCheckbox"><input id="overrideExistingFilesCheckbox" type="checkbox" onclick="setFlashPostOptionOverwriteExistingFiles(this);" />\' + top.String.format(top.TYPO3.LLL.fileUpload.infoComponentOverrideFiles) + \'</label>\'
207 });
208 flashUploader.add(infoComponent);
209
210 // do a reload of this frame once all uploads are done
211 flashUploader.on("totalcomplete", function() {
212 window.location.reload();
213 });
214
215 // this is the callback function that delivers the additional post parameter to the flash application
216 top.setFlashPostOptionOverwriteExistingFiles = function(checkbox) {
217 var uploader = top.TYPO3.getInstance("FileUploadWindow");
218 if (uploader.isVisible()) {
219 uploader.swf.addPostParam("overwriteExistingFiles", (checkbox.checked == true ? 1 : 0));
220 }
221 };
222
223 event.stop();
224 };
225 });
226 }
227 ';
228 }
229 // Create filelisting object
230 $this->filelist = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Filelist\\FileList');
231 $this->filelist->backPath = $GLOBALS['BACK_PATH'];
232 // Apply predefined values for hidden checkboxes
233 // Set predefined value for DisplayBigControlPanel:
234 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
235 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
236 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
237 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
238 }
239 // Set predefined value for DisplayThumbnails:
240 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
241 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
242 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
243 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
244 }
245 // Set predefined value for Clipboard:
246 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
247 $this->MOD_SETTINGS['clipBoard'] = TRUE;
248 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
249 $this->MOD_SETTINGS['clipBoard'] = FALSE;
250 }
251 // If user never opened the list module, set the value for displayThumbs
252 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
253 $this->MOD_SETTINGS['displayThumbs'] = $GLOBALS['BE_USER']->uc['thumbnailsByDefault'];
254 }
255 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
256 // Create clipboard object and initialize that
257 $this->filelist->clipObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Clipboard\\Clipboard');
258 $this->filelist->clipObj->fileMode = 1;
259 $this->filelist->clipObj->initializeClipboard();
260 $CB = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('CB');
261 if ($this->cmd == 'setCB') {
262 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBH'), \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBC')), '_FILE');
263 }
264 if (!$this->MOD_SETTINGS['clipBoard']) {
265 $CB['setP'] = 'normal';
266 }
267 $this->filelist->clipObj->setCmd($CB);
268 $this->filelist->clipObj->cleanCurrent();
269 // Saves
270 $this->filelist->clipObj->endClipboard();
271 // If the "cmd" was to delete files from the list (clipboard thing), do that:
272 if ($this->cmd == 'delete') {
273 $items = $this->filelist->clipObj->cleanUpCBC(\TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBC'), '_FILE', 1);
274 if (count($items)) {
275 // Make command array:
276 $FILE = array();
277 foreach ($items as $v) {
278 $FILE['delete'][] = array('data' => $v);
279 }
280 // Init file processing object for deleting and pass the cmd array.
281 $fileProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
282 $fileProcessor->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
283 $fileProcessor->init_actionPerms($GLOBALS['BE_USER']->getFileoperationPermissions());
284 $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
285 $fileProcessor->start($FILE);
286 $fileProcessor->processData();
287 $fileProcessor->printLogErrorMessages();
288 }
289 }
290 if (!isset($this->MOD_SETTINGS['sort'])) {
291 // Set default sorting
292 $this->MOD_SETTINGS['sort'] = 'file';
293 $this->MOD_SETTINGS['reverse'] = 0;
294 }
295 // Start up filelisting object, include settings.
296 $this->pointer = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
297 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
298 // Generate the list
299 $this->filelist->generateList();
300 // Write the footer
301 $this->filelist->writeBottom();
302 // Set top JavaScript:
303 $this->doc->JScode = $this->doc->wrapScriptTags((('
304
305 if (top.fsMod) top.fsMod.recentIds["file"] = unescape("' . rawurlencode($this->id)) . '");
306 function jumpToUrl(URL) { //
307 window.location.href = URL;
308 }
309
310 ') . $this->filelist->CBfunctions());
311 // This will return content necessary for the context sensitive clickmenus to work: bodytag events, JavaScript functions and DIV-layers.
312 $this->doc->getContextMenuCode();
313 // Setting up the buttons and markers for docheader
314 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
315 // add the folder info to the marker array
316 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
317 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
318 // Build the <body> for the module
319 // Create output
320 $pageContent = '';
321 $pageContent .= ('<form action="' . htmlspecialchars($this->filelist->listURL())) . '" method="post" name="dblistForm">';
322 $pageContent .= $this->filelist->HTMLcode;
323 $pageContent .= '<input type="hidden" name="cmd" /></form>';
324 // Making listing options:
325 if ($this->filelist->HTMLcode) {
326 $pageContent .= '
327
328 <!--
329 Listing options for extended view, clipboard and thumbnails
330 -->
331 <div id="typo3-listOptions">
332 ';
333 // Add "display bigControlPanel" checkbox:
334 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'selectable') {
335 $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 />';
336 }
337 // Add "display thumbnails" checkbox:
338 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable') {
339 $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 />';
340 }
341 // Add "clipboard" checkbox:
342 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable') {
343 $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>';
344 }
345 $pageContent .= '
346 </div>
347 ';
348 // Set clipboard:
349 if ($this->MOD_SETTINGS['clipBoard']) {
350 $pageContent .= $this->filelist->clipObj->printClipboard();
351 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_clipboard', $GLOBALS['BACK_PATH']);
352 }
353 }
354 $markerArray = array(
355 'CSH' => $docHeaderButtons['csh'],
356 'FUNC_MENU' => \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
357 'CONTENT' => $pageContent
358 );
359 $this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
360 // Renders the module page
361 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $this->content);
362 } else {
363 $content = '';
364 if ($this->errorMessage) {
365 $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()));
366 }
367 // Create output - no access (no warning though)
368 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $content);
369 }
370 }
371
372 /**
373 * Outputting the accumulated content to screen
374 *
375 * @return void
376 * @todo Define visibility
377 */
378 public function printContent() {
379 echo $this->content;
380 }
381
382 /**
383 * Create the panel of buttons for submitting the form or otherwise perform operations.
384 *
385 * @return array All available buttons as an assoc. array
386 * @todo Define visibility
387 */
388 public function getButtons() {
389 $buttons = array(
390 'csh' => '',
391 'shortcut' => '',
392 'upload' => '',
393 'new' => ''
394 );
395 // Add shortcut
396 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
397 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->MCONF['name']);
398 }
399 // FileList Module CSH:
400 $buttons['csh'] = \TYPO3\CMS\Backend\Utility\BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module', $GLOBALS['BACK_PATH'], '', TRUE);
401 // Upload button (only if upload to this directory is allowed)
402 if (($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('upload', 'File')) && $this->folderObject->checkActionPermission('write')) {
403 $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>';
404 }
405 // New folder button
406 if ($this->folderObject && $this->folderObject->checkActionPermission('add')) {
407 $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>';
408 }
409 return $buttons;
410 }
411
412 }
413
414
415 ?>