[BUGFIX] FileList uses wrong basefolder for isWithinFolder
[Packages/TYPO3.CMS.git] / typo3 / sysext / filelist / Classes / FileList.php
1 <?php
2 namespace TYPO3\CMS\Filelist;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Clipboard\Clipboard;
18 use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
19 use TYPO3\CMS\Backend\RecordList\AbstractRecordList;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Backend\Utility\IconUtility;
22 use TYPO3\CMS\Core\Database\DatabaseConnection;
23 use TYPO3\CMS\Core\Messaging\FlashMessage;
24 use TYPO3\CMS\Core\Resource\File;
25 use TYPO3\CMS\Core\Resource\Folder;
26 use TYPO3\CMS\Core\Resource\InaccessibleFolder;
27 use TYPO3\CMS\Core\Resource\ProcessedFile;
28 use TYPO3\CMS\Core\Resource\ResourceFactory;
29 use TYPO3\CMS\Core\Resource\Utility\ListUtility;
30 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32 use TYPO3\CMS\Core\Resource\FolderInterface;
33
34 /**
35 * Class for rendering of File>Filelist
36 *
37 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
38 */
39 class FileList extends AbstractRecordList {
40
41 /**
42 * Default Max items shown
43 *
44 * @var int
45 */
46 public $iLimit = 40;
47
48 /**
49 * Thumbnails on records containing files (pictures)
50 *
51 * @var bool
52 */
53 public $thumbs = FALSE;
54
55 /**
56 * Space icon used for alignment when no button is available
57 *
58 * @var string
59 */
60 public $spaceIcon;
61
62 /**
63 * Max length of strings
64 *
65 * @var int
66 */
67 public $fixedL = 30;
68
69 /**
70 * @var string
71 */
72 public $script = '';
73
74 /**
75 * If TRUE click menus are generated on files and folders
76 *
77 * @var bool
78 */
79 public $clickMenus = 1;
80
81 /**
82 * The field to sort by
83 *
84 * @var string
85 */
86 public $sort = '';
87
88 /**
89 * Reverse sorting flag
90 *
91 * @var bool
92 */
93 public $sortRev = 1;
94
95 /**
96 * @var int
97 */
98 public $firstElementNumber = 0;
99
100 /**
101 * @var bool
102 */
103 public $clipBoard = 0;
104
105 /**
106 * @var bool
107 */
108 public $bigControlPanel = 0;
109
110 /**
111 * @var string
112 */
113 public $JScode = '';
114
115 /**
116 * @var string
117 */
118 public $HTMLcode = '';
119
120 /**
121 * @var int
122 */
123 public $totalbytes = 0;
124
125 /**
126 * @var array
127 */
128 public $dirs = array();
129
130 /**
131 * @var array
132 */
133 public $files = array();
134
135 /**
136 * @var string
137 */
138 public $path = '';
139
140 /**
141 * @var Folder
142 */
143 protected $folderObject;
144
145 /**
146 * Counting the elements no matter what
147 *
148 * @var int
149 */
150 public $eCounter = 0;
151
152 /**
153 * @var int
154 */
155 public $dirCounter = 0;
156
157 /**
158 * @var string
159 */
160 public $totalItems = '';
161
162 /**
163 * @var array
164 */
165 public $CBnames = array();
166
167 /**
168 * @var Clipboard $clipObj
169 */
170 public $clipObj;
171
172 /**
173 * @var ResourceFactory
174 */
175 protected $resourceFactory;
176
177 /**
178 * @param ResourceFactory $resourceFactory
179 */
180 public function injectResourceFactory(ResourceFactory $resourceFactory) {
181 $this->resourceFactory = $resourceFactory;
182 }
183
184 /**
185 * Initialization of class
186 *
187 * @param Folder $folderObject The folder to work on
188 * @param int $pointer Pointer
189 * @param bool $sort Sorting column
190 * @param bool $sortRev Sorting direction
191 * @param bool $clipBoard
192 * @param bool $bigControlPanel Show clipboard flag
193 * @return void
194 */
195 public function start(Folder $folderObject, $pointer, $sort, $sortRev, $clipBoard = FALSE, $bigControlPanel = FALSE) {
196 $this->script = BackendUtility::getModuleUrl('file_list');
197 $this->folderObject = $folderObject;
198 $this->counter = 0;
199 $this->totalbytes = 0;
200 $this->JScode = '';
201 $this->HTMLcode = '';
202 $this->path = $folderObject->getIdentifier();
203 $this->sort = $sort;
204 $this->sortRev = $sortRev;
205 $this->firstElementNumber = $pointer;
206 $this->clipBoard = $clipBoard;
207 $this->bigControlPanel = $bigControlPanel;
208 // Setting the maximum length of the filenames to the user's settings or minimum 30 (= $this->fixedL)
209 $this->fixedL = max($this->fixedL, $this->getBackendUser()->uc['titleLen']);
210 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_common.xlf');
211 $this->resourceFactory = ResourceFactory::getInstance();
212 }
213
214 /**
215 * Reading files and directories, counting elements and generating the list in ->HTMLcode
216 *
217 * @return void
218 */
219 public function generateList() {
220 $this->HTMLcode .= $this->getTable('fileext,tstamp,size,rw,_REF_');
221 }
222
223 /**
224 * Return the buttons used by the file list to include in the top header
225 *
226 * @param Folder $folderObject
227 * @return array
228 */
229 public function getButtonsAndOtherMarkers(Folder $folderObject) {
230 $otherMarkers = array(
231 'PAGE_ICON' => '',
232 'TITLE' => ''
233 );
234 $buttons = array(
235 'level_up' => $this->getLinkToParentFolder($folderObject),
236 'refresh' => '',
237 'title' => '',
238 'page_icon' => '',
239 'PASTE' => ''
240 );
241 // Makes the code for the folder icon in the top
242 if ($folderObject) {
243 $title = htmlspecialchars($folderObject->getIdentifier());
244 // Start compiling the HTML
245 // If this is some subFolder under the mount root....
246 if ($folderObject->getStorage()->isWithinFileMountBoundaries($folderObject)) {
247 // The icon with link
248 $otherMarkers['PAGE_ICON'] = IconUtility::getSpriteIconForResource($folderObject, array('title' => $title));
249 // No HTML specialchars here - HTML like <strong> </strong> is allowed
250 $otherMarkers['TITLE'] .= GeneralUtility::removeXSS(GeneralUtility::fixed_lgd_cs($title, -($this->fixedL + 20)));
251 } else {
252 // This is the root folder
253 $otherMarkers['PAGE_ICON'] = IconUtility::getSpriteIconForResource($folderObject, array('title' => $title, 'mount-root' => TRUE));
254 $otherMarkers['TITLE'] .= htmlspecialchars(GeneralUtility::fixed_lgd_cs($title, -($this->fixedL + 20)));
255 }
256 if ($this->clickMenus) {
257 $otherMarkers['PAGE_ICON'] = $GLOBALS['SOBE']->doc->wrapClickMenuOnIcon($otherMarkers['PAGE_ICON'], $folderObject->getCombinedIdentifier());
258 }
259 // Add paste button if clipboard is initialized
260 if ($this->clipObj instanceof Clipboard && $folderObject->checkActionPermission('write')) {
261 $elFromTable = $this->clipObj->elFromTable('_FILE');
262 if (count($elFromTable)) {
263 $addPasteButton = TRUE;
264 foreach ($elFromTable as $element) {
265 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
266 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $folderObject)) {
267 $addPasteButton = FALSE;
268 }
269 }
270 if ($addPasteButton) {
271 $buttons['PASTE'] = '<a href="' . htmlspecialchars($this->clipObj->pasteUrl('_FILE', $folderObject->getCombinedIdentifier())) . '" onclick="return ' . htmlspecialchars($this->clipObj->confirmMsg('_FILE', $this->path, 'into', $elFromTable)) . '" title="' . $this->getLanguageService()->getLL('clip_paste', TRUE) . '">' . IconUtility::getSpriteIcon('actions-document-paste-after') . '</a>';
272 }
273 }
274 }
275
276 }
277 $buttons['refresh'] = '<a href="' . htmlspecialchars($this->listURL()) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.reload', TRUE) . '">' . IconUtility::getSpriteIcon('actions-system-refresh') . '</a>';
278 return array($buttons, $otherMarkers);
279 }
280
281 /**
282 * Wrapping input string in a link with clipboard command.
283 *
284 * @param string $string String to be linked - must be htmlspecialchar'ed / prepared before.
285 * @param string $table table - NOT USED
286 * @param string $cmd "cmd" value
287 * @param string $warning Warning for JS confirm message
288 * @return string Linked string
289 */
290 public function linkClipboardHeaderIcon($string, $table, $cmd, $warning = '') {
291 $onClickEvent = 'document.dblistForm.cmd.value=\'' . $cmd . '\';document.dblistForm.submit();';
292 if ($warning) {
293 $onClickEvent = 'if (confirm(' . GeneralUtility::quoteJSvalue($warning) . ')){' . $onClickEvent . '}';
294 }
295 return '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($onClickEvent) . 'return false;">' . $string . '</a>';
296 }
297
298 /**
299 * Returns a table with directories and files listed.
300 *
301 * @param array $rowlist Array of files from path
302 * @return string HTML-table
303 */
304 public function getTable($rowlist) {
305 // prepare space icon
306 $this->spaceIcon = '<span class="btn btn-default disabled">' . IconUtility::getSpriteIcon('empty-empty') . '</span>';
307
308 // @todo use folder methods directly when they support filters
309 $storage = $this->folderObject->getStorage();
310 $storage->resetFileAndFolderNameFiltersToDefault();
311
312 // Only render the contents of a browsable storage
313 if ($this->folderObject->getStorage()->isBrowsable()) {
314 $folders = $storage->getFolderIdentifiersInFolder($this->folderObject->getIdentifier());
315 $files = $this->folderObject->getFiles();
316 $this->sort = trim($this->sort);
317 if ($this->sort !== '') {
318 $filesToSort = array();
319 /** @var $fileObject File */
320 foreach ($files as $fileObject) {
321 switch ($this->sort) {
322 case 'size':
323 $sortingKey = $fileObject->getSize();
324 break;
325 case 'rw':
326 $sortingKey = ($fileObject->checkActionPermission('read') ? 'R' : '' . $fileObject->checkActionPermission('write')) ? 'W' : '';
327 break;
328 case 'fileext':
329 $sortingKey = $fileObject->getExtension();
330 break;
331 case 'tstamp':
332 $sortingKey = $fileObject->getModificationTime();
333 break;
334 case 'file':
335 $sortingKey = $fileObject->getName();
336 break;
337 default:
338 if ($fileObject->hasProperty($this->sort)) {
339 $sortingKey = $fileObject->getProperty($this->sort);
340 } else {
341 $sortingKey = $fileObject->getName();
342 }
343 }
344 $i = 1000000;
345 while (isset($filesToSort[$sortingKey . $i])) {
346 $i++;
347 }
348 $filesToSort[$sortingKey . $i] = $fileObject;
349 }
350 uksort($filesToSort, 'strnatcasecmp');
351 if ((int)$this->sortRev === 1) {
352 $filesToSort = array_reverse($filesToSort);
353 }
354 $files = $filesToSort;
355 }
356 $this->totalItems = count($folders) + count($files);
357 // Adds the code of files/dirs
358 $out = '';
359 $titleCol = 'file';
360 // Cleaning rowlist for duplicates and place the $titleCol as the first column always!
361 $rowlist = '_LOCALIZATION_,' . $rowlist;
362 $rowlist = GeneralUtility::rmFromList($titleCol, $rowlist);
363 $rowlist = GeneralUtility::uniqueList($rowlist);
364 $rowlist = $rowlist ? $titleCol . ',' . $rowlist : $titleCol;
365 if ($this->clipBoard) {
366 $rowlist = str_replace('_LOCALIZATION_,', '_LOCALIZATION_,_CLIPBOARD_,', $rowlist);
367 $this->addElement_tdCssClass['_CLIPBOARD_'] = 'col-clipboard';
368 }
369 if ($this->bigControlPanel) {
370 $rowlist = str_replace('_LOCALIZATION_,', '_LOCALIZATION_,_CONTROL_,', $rowlist);
371 $this->addElement_tdCssClass['_CONTROL_'] = 'col-control';
372 }
373 $this->fieldArray = explode(',', $rowlist);
374 $folderObjects = array();
375 foreach ($folders as $folder) {
376 $folderObjects[] = $storage->getFolder($folder, TRUE);
377 }
378 // Add classes to table cells
379 $this->addElement_tdCssClass[$titleCol] = 'col-title';
380 $this->addElement_tdCssClass['_LOCALIZATION_'] = 'col-localizationa';
381
382 $folderObjects = ListUtility::resolveSpecialFolderNames($folderObjects);
383 uksort($folderObjects, 'strnatcasecmp');
384
385 // Directories are added
386 $iOut = $this->formatDirList($folderObjects);
387 // Files are added
388 $iOut .= $this->formatFileList($files, $titleCol);
389 // Header line is drawn
390 $theData = array();
391 foreach ($this->fieldArray as $v) {
392 if ($v == '_CLIPBOARD_' && $this->clipBoard) {
393 $cells = array();
394 $table = '_FILE';
395 $elFromTable = $this->clipObj->elFromTable($table);
396 if (count($elFromTable) && $this->folderObject->checkActionPermission('write')) {
397 $addPasteButton = TRUE;
398 foreach ($elFromTable as $element) {
399 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
400 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $this->folderObject)) {
401 $addPasteButton = FALSE;
402 }
403 }
404 if ($addPasteButton) {
405 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->pasteUrl('_FILE', $this->folderObject->getCombinedIdentifier())) . '" onclick="return ' . htmlspecialchars($this->clipObj->confirmMsg('_FILE', $this->path, 'into', $elFromTable)) . '" title="' . $this->getLanguageService()->getLL('clip_paste', 1) . '">' . IconUtility::getSpriteIcon('actions-document-paste-after') . '</a>';
406 }
407 }
408 if ($this->clipObj->current !== 'normal' && $iOut) {
409 $cells[] = $this->linkClipboardHeaderIcon(IconUtility::getSpriteIcon('actions-edit-copy', array('title' => $this->getLanguageService()->getLL('clip_selectMarked', TRUE))), $table, 'setCB');
410 $cells[] = $this->linkClipboardHeaderIcon(IconUtility::getSpriteIcon('actions-edit-delete', array('title' => $this->getLanguageService()->getLL('clip_deleteMarked'))), $table, 'delete', $this->getLanguageService()->getLL('clip_deleteMarkedWarning'));
411 $onClick = 'checkOffCB(\'' . implode(',', $this->CBnames) . '\', this); return false;';
412 $cells[] = '<a class="btn btn-default" rel="" href="#" onclick="' . htmlspecialchars($onClick) . '" title="' . $this->getLanguageService()->getLL('clip_markRecords', TRUE) . '">' . IconUtility::getSpriteIcon('actions-document-select') . '</a>';
413 }
414 $theData[$v] = implode('', $cells);
415 } else {
416 // Normal row:
417 $theT = $this->linkWrapSort($this->getLanguageService()->getLL('c_' . $v, TRUE), $this->folderObject->getCombinedIdentifier(), $v);
418 $theData[$v] = $theT;
419 }
420 }
421
422 $out .= '<thead>' . $this->addelement(1, '', $theData, '', '', '', 'th') . '</thead>';
423 $out .= '<tbody>' . $iOut . '</tbody>';
424 // half line is drawn
425 // finish
426 $out = '
427 <!--
428 File list table:
429 -->
430 <div class="table-fit">
431 <table class="table table-striped table-hover" id="typo3-filelist">
432 ' . $out . '
433 </table>
434 </div>';
435
436 } else {
437 /** @var $flashMessage FlashMessage */
438 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('storageNotBrowsableMessage'), $this->getLanguageService()->getLL('storageNotBrowsableTitle'), FlashMessage::INFO);
439 $out = $flashMessage->render();
440 }
441 return $out;
442 }
443
444 /**
445 * If there is a parent folder and user has access to it, return an icon
446 * which is linked to the filelist of the parent folder.
447 *
448 * @param Folder $currentFolder
449 * @return string
450 */
451 protected function getLinkToParentFolder(Folder $currentFolder) {
452 $levelUp = '';
453 try {
454 $currentStorage = $currentFolder->getStorage();
455 $parentFolder = $currentFolder->getParentFolder();
456 if ($parentFolder->getIdentifier() !== $currentFolder->getIdentifier() && $currentStorage->isWithinFileMountBoundaries($parentFolder)) {
457 $levelUp = $this->linkWrapDir(
458 IconUtility::getSpriteIcon(
459 'actions-view-go-up',
460 array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.php:labels.upOneLevel', TRUE))
461 ),
462 $parentFolder
463 );
464 }
465 } catch (\Exception $e) {}
466 return $levelUp;
467 }
468
469 /**
470 * Gets the number of files and total size of a folder
471 *
472 * @return string
473 */
474 public function getFolderInfo() {
475 if ($this->counter == 1) {
476 $fileLabel = $this->getLanguageService()->getLL('file', TRUE);
477 } else {
478 $fileLabel = $this->getLanguageService()->getLL('files', TRUE);
479 }
480 return $this->counter . ' ' . $fileLabel . ', ' . GeneralUtility::formatSize($this->totalbytes, $this->getLanguageService()->getLL('byteSizeUnits', TRUE));
481 }
482
483 /**
484 * This returns tablerows for the directories in the array $items['sorting'].
485 *
486 * @param Folder[] $folders Folders of \TYPO3\CMS\Core\Resource\Folder
487 * @return string HTML table rows.
488 */
489 public function formatDirList(array $folders) {
490 $out = '';
491 foreach ($folders as $folderName => $folderObject) {
492 $role = $folderObject->getRole();
493 if ($role === FolderInterface::ROLE_PROCESSING) {
494 // don't show processing-folder
495 continue;
496 }
497 if ($role !== FolderInterface::ROLE_DEFAULT) {
498 $displayName = '<strong>' . htmlspecialchars($folderName) . '</strong>';
499 } else {
500 $displayName = htmlspecialchars($folderName);
501 }
502
503 list($flag, $code) = $this->fwd_rwd_nav();
504 $out .= $code;
505 if ($flag) {
506 $isLocked = $folderObject instanceof InaccessibleFolder;
507 $isWritable = $folderObject->checkActionPermission('write');
508
509 // Initialization
510 $this->counter++;
511
512 // The icon with link
513 $theIcon = IconUtility::getSpriteIconForResource($folderObject, array('title' => $folderName));
514 if (!$isLocked && $this->clickMenus) {
515 $theIcon = $GLOBALS['SOBE']->doc->wrapClickMenuOnIcon($theIcon, $folderObject->getCombinedIdentifier());
516 }
517
518 // Preparing and getting the data-array
519 $theData = array();
520 if ($isLocked) {
521 foreach ($this->fieldArray as $field) {
522 $theData[$field] = '';
523 }
524 $theData['file'] = $displayName;
525 } else {
526 foreach ($this->fieldArray as $field) {
527 switch ($field) {
528 case 'size':
529 $numFiles = $folderObject->getFileCount();
530 $theData[$field] = $numFiles . ' ' . $this->getLanguageService()->getLL(($numFiles === 1 ? 'file' : 'files'), TRUE);
531 break;
532 case 'rw':
533 $theData[$field] = '<strong class="text-danger">' . $this->getLanguageService()->getLL('read', TRUE) . '</strong>' . (!$isWritable ? '' : '<strong class="text-danger">' . $this->getLanguageService()->getLL('write', TRUE) . '</strong>');
534 break;
535 case 'fileext':
536 $theData[$field] = $this->getLanguageService()->getLL('folder', TRUE);
537 break;
538 case 'tstamp':
539 // @todo: FAL: how to get the mtime info -- $theData[$field] = \TYPO3\CMS\Backend\Utility\BackendUtility::date($theFile['tstamp']);
540 $theData[$field] = '-';
541 break;
542 case 'file':
543 $theData[$field] = $this->linkWrapDir($displayName, $folderObject);
544 break;
545 case '_CONTROL_':
546 $theData[$field] = $this->makeEdit($folderObject);
547 break;
548 case '_CLIPBOARD_':
549 $theData[$field] = $this->makeClip($folderObject);
550 break;
551 case '_REF_':
552 $theData[$field] = $this->makeRef($folderObject);
553 break;
554 default:
555 $theData[$field] = GeneralUtility::fixed_lgd_cs($theData[$field], $this->fixedL);
556 }
557 }
558 }
559 $out .= $this->addelement(1, $theIcon, $theData);
560 }
561 $this->eCounter++;
562 $this->dirCounter = $this->eCounter;
563 }
564 return $out;
565 }
566
567 /**
568 * Wraps the directory-titles
569 *
570 * @param string $title String to be wrapped in links
571 * @param Folder $folderObject Folder to work on
572 * @return string HTML
573 */
574 public function linkWrapDir($title, Folder $folderObject) {
575 $href = $this->backPath . $this->script . '&id=' . rawurlencode($folderObject->getCombinedIdentifier());
576 $onclick = ' onclick="' . htmlspecialchars(('top.document.getElementsByName("navigation")[0].contentWindow.Tree.highlightActiveItem("file","folder' . GeneralUtility::md5int($folderObject->getCombinedIdentifier()) . '_"+top.fsMod.currentBank)')) . '"';
577 // Sometimes $code contains plain HTML tags. In such a case the string should not be modified!
578 if ((string)$title === strip_tags($title)) {
579 return '<a href="' . htmlspecialchars($href) . '"' . $onclick . ' title="' . htmlspecialchars($title) . '">' . GeneralUtility::fixed_lgd_cs($title, $this->fixedL) . '</a>';
580 } else {
581 return '<a href="' . htmlspecialchars($href) . '"' . $onclick . '>' . $title . '</a>';
582 }
583 }
584
585 /**
586 * Wraps filenames in links which opens the metadata editor.
587 *
588 * @param string $code String to be wrapped in links
589 * @param File $fileObject File to be linked
590 * @return string HTML
591 */
592 public function linkWrapFile($code, File $fileObject) {
593 try {
594 if ($fileObject instanceof File && $fileObject->isIndexed() && $fileObject->checkActionPermission('write') && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata')) {
595 $metaData = $fileObject->_getMetaData();
596 $data = array(
597 'sys_file_metadata' => array($metaData['uid'] => 'edit')
598 );
599 $editOnClick = BackendUtility::editOnClick(GeneralUtility::implodeArrayForUrl('edit', $data), $GLOBALS['BACK_PATH'], $this->listUrl());
600 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.editMetadata'));
601 $code = '<a href="#" title="' . $title . '" onclick="' . htmlspecialchars($editOnClick) . '">' . GeneralUtility::fixed_lgd_cs($code, $this->fixedL) . '</a>';
602 }
603 } catch (\Exception $e) {
604 // intentional fall-through
605 }
606 return $code;
607 }
608
609 /**
610 * Returns list URL; This is the URL of the current script with id and imagemode parameters, that's all.
611 * The URL however is not relative (with the backpath), otherwise GeneralUtility::sanitizeLocalUrl() would say that
612 * the URL would be invalid
613 *
614 * @return string URL
615 */
616 public function listURL() {
617 return GeneralUtility::linkThisScript(array(
618 'target' => rawurlencode($this->folderObject->getCombinedIdentifier()),
619 'imagemode' => $this->thumbs
620 ));
621 }
622
623 /**
624 * This returns tablerows for the files in the array $items['sorting'].
625 *
626 * @param File[] $files File items
627 * @return string HTML table rows.
628 */
629 public function formatFileList(array $files) {
630 $out = '';
631 // first two keys are "0" (default) and "-1" (multiple), after that comes the "other languages"
632 $allSystemLanguages = GeneralUtility::makeInstance(TranslationConfigurationProvider::class)->getSystemLanguages();
633 $systemLanguages = array_filter($allSystemLanguages, function($languageRecord) {
634 if ($languageRecord['uid'] === -1 || $languageRecord['uid'] === 0 || !$this->getBackendUser()->checkLanguageAccess($languageRecord['uid'])) {
635 return FALSE;
636 } else {
637 return TRUE;
638 }
639 });
640
641 foreach ($files as $fileObject) {
642 list($flag, $code) = $this->fwd_rwd_nav();
643 $out .= $code;
644 if ($flag) {
645 // Initialization
646 $this->counter++;
647 $this->totalbytes += $fileObject->getSize();
648 $ext = $fileObject->getExtension();
649 $fileName = trim($fileObject->getName());
650 // The icon with link
651 $theIcon = IconUtility::getSpriteIconForResource($fileObject, array('title' => $fileName . ' [' . (int)$fileObject->getUid() . ']'));
652 if ($this->clickMenus) {
653 $theIcon = $GLOBALS['SOBE']->doc->wrapClickMenuOnIcon($theIcon, $fileObject->getCombinedIdentifier());
654 }
655 // Preparing and getting the data-array
656 $theData = array();
657 foreach ($this->fieldArray as $field) {
658 switch ($field) {
659 case 'size':
660 $theData[$field] = GeneralUtility::formatSize($fileObject->getSize(), $this->getLanguageService()->getLL('byteSizeUnits', TRUE));
661 break;
662 case 'rw':
663 $theData[$field] = '' . (!$fileObject->checkActionPermission('read') ? ' ' : '<strong class="text-danger">' . $this->getLanguageService()->getLL('read', TRUE) . '</strong>') . (!$fileObject->checkActionPermission('write') ? '' : '<strong class="text-danger">' . $this->getLanguageService()->getLL('write', TRUE) . '</strong>');
664 break;
665 case 'fileext':
666 $theData[$field] = strtoupper($ext);
667 break;
668 case 'tstamp':
669 $theData[$field] = BackendUtility::date($fileObject->getModificationTime());
670 break;
671 case '_CONTROL_':
672 $theData[$field] = $this->makeEdit($fileObject);
673 break;
674 case '_CLIPBOARD_':
675 $theData[$field] = $this->makeClip($fileObject);
676 break;
677 case '_LOCALIZATION_':
678 if (!empty($systemLanguages) && $fileObject->isIndexed() && $fileObject->checkActionPermission('write') && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata')) {
679 $metaDataRecord = $fileObject->_getMetaData();
680 $translations = $this->getTranslationsForMetaData($metaDataRecord);
681 $languageCode = '';
682
683 foreach ($systemLanguages as $language) {
684 $languageId = $language['uid'];
685 $flagIcon = $language['flagIcon'];
686 if (array_key_exists($languageId, $translations)) {
687 $flagButtonIcon = IconUtility::getSpriteIcon(
688 'actions-document-open',
689 array('title' => sprintf($GLOBALS['LANG']->getLL('editMetadataForLanguage'), $language['title'])),
690 array($flagIcon . '-overlay' => array()));
691 $data = array(
692 'sys_file_metadata' => array($translations[$languageId]['uid'] => 'edit')
693 );
694 $editOnClick = BackendUtility::editOnClick(GeneralUtility::implodeArrayForUrl('edit', $data), $GLOBALS['BACK_PATH'], $this->listUrl());
695 $languageCode .= '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($editOnClick) . '">' . $flagButtonIcon . '</a>';
696 } else {
697 $href = $GLOBALS['SOBE']->doc->issueCommand(
698 '&cmd[sys_file_metadata][' . $metaDataRecord['uid'] . '][localize]=' . $languageId,
699 $this->backPath . 'alt_doc.php?justLocalized=' . rawurlencode(('sys_file_metadata:' . $metaDataRecord['uid'] . ':' . $languageId)) .
700 '&returnUrl=' . rawurlencode($this->listURL()) . BackendUtility::getUrlToken('editRecord')
701 );
702 $flagButtonIcon = IconUtility::getSpriteIcon(
703 $flagIcon,
704 array('title' => sprintf($GLOBALS['LANG']->getLL('createMetadataForLanguage'), $language['title'])),
705 array($flagIcon . '-overlay' => array())
706 );
707 $languageCode .= '<a href="' . htmlspecialchars($href) . '" class="btn btn-default">' . $flagButtonIcon . '</a> ';
708 }
709 }
710
711 // Hide flag button bar when not translated yet
712 $theData[$field] = ' <div class="localisationData btn-group" data-fileid="' . $fileObject->getUid() . '"' .
713 (empty($translations) ? ' style="display: none;"' : '') . '>' . $languageCode . '</div>';
714 $theData[$field] .= '<a class="btn btn-default filelist-translationToggler" data-fileid="' . $fileObject->getUid() . '">' .
715 IconUtility::getSpriteIcon(
716 'mimetypes-x-content-page-language-overlay',
717 array(
718 'title' => $GLOBALS['LANG']->getLL('translateMetadata')
719 )
720 ) . '</a>';
721 }
722 break;
723 case '_REF_':
724 $theData[$field] = $this->makeRef($fileObject);
725 break;
726 case 'file':
727 // Edit metadata of file
728 $theData[$field] = $this->linkWrapFile(htmlspecialchars($fileName), $fileObject);
729
730 if ($fileObject->isMissing()) {
731 $flashMessage = \TYPO3\CMS\Core\Resource\Utility\BackendUtility::getFlashMessageForMissingFile($fileObject);
732 $theData[$field] .= $flashMessage->render();
733 // Thumbnails?
734 } elseif ($this->thumbs && $this->isImage($ext)) {
735 $processedFile = $fileObject->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, array());
736 if ($processedFile) {
737 $thumbUrl = $processedFile->getPublicUrl(TRUE);
738 $theData[$field] .= '<br /><img src="' . $thumbUrl . '" title="' . htmlspecialchars($fileName) . '" alt="" />';
739 }
740 }
741 break;
742 default:
743 $theData[$field] = '';
744 if ($fileObject->hasProperty($field)) {
745 $theData[$field] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($fileObject->getProperty($field), $this->fixedL));
746 }
747 }
748 }
749 $out .= $this->addelement(1, $theIcon, $theData);
750
751 }
752 $this->eCounter++;
753 }
754 return $out;
755 }
756
757 /**
758 * Fetch the translations for a sys_file_metadata record
759 *
760 * @param $metaDataRecord
761 * @return array keys are the sys_language uids, values are the $rows
762 */
763 protected function getTranslationsForMetaData($metaDataRecord) {
764 $where = $GLOBALS['TCA']['sys_file_metadata']['ctrl']['transOrigPointerField'] . '=' . (int)$metaDataRecord['uid'] .
765 ' AND ' . $GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField'] . '>0';
766 $translationRecords = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_file_metadata', $where);
767 $translations = array();
768 foreach ($translationRecords as $record) {
769 $translations[$record[$GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField']]] = $record;
770 }
771 return $translations;
772 }
773
774 /**
775 * Returns TRUE if $ext is an image-extension according to $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
776 *
777 * @param string $ext File extension
778 * @return bool
779 */
780 public function isImage($ext) {
781 return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($ext));
782 }
783
784 /**
785 * Wraps the directory-titles ($code) in a link to filelist/mod1/index.php (id=$path) and sorting commands...
786 *
787 * @param string $code String to be wrapped
788 * @param string $folderIdentifier ID (path)
789 * @param string $col Sorting column
790 * @return string HTML
791 */
792 public function linkWrapSort($code, $folderIdentifier, $col) {
793 if ($this->sort === $col) {
794 // Check reverse sorting
795 $params = '&SET[sort]=' . $col . '&SET[reverse]=' . ($this->sortRev ? '0' : '1');
796 $sortArrow = IconUtility::getSpriteIcon('status-status-sorting-light-' . ($this->sortRev ? 'desc' : 'asc'));
797 } else {
798 $params = '&SET[sort]=' . $col . '&SET[reverse]=0';
799 $sortArrow = '';
800 }
801 $href = GeneralUtility::resolveBackPath(($GLOBALS['BACK_PATH'] . $this->script)) . '&id=' . rawurlencode($folderIdentifier) . $params;
802 return '<a href="' . htmlspecialchars($href) . '">' . $code . ' ' . $sortArrow . '</a>';
803 }
804
805 /**
806 * Creates the clipboard control pad
807 *
808 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the clipboard panel for the listing.
809 * @return string HTML-table
810 */
811 public function makeClip($fileOrFolderObject) {
812 if (!$fileOrFolderObject->checkActionPermission('read')) {
813 return '';
814 }
815 $cells = array();
816 $fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
817 $md5 = GeneralUtility::shortmd5($fullIdentifier);
818 // For normal clipboard, add copy/cut buttons:
819 if ($this->clipObj->current == 'normal') {
820 $isSel = $this->clipObj->isSelected('_FILE', $md5);
821 $cells[] = '<a class="btn btn-default"" href="' . htmlspecialchars($this->clipObj->selUrlFile($fullIdentifier, 1, ($isSel == 'copy'))) . '">' . IconUtility::getSpriteIcon(('actions-edit-copy' . ($isSel == 'copy' ? '-release' : '')), array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.copy', TRUE))) . '</a>';
822 // we can only cut if file can be moved
823 if ($fileOrFolderObject->checkActionPermission('move')) {
824 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->selUrlFile($fullIdentifier, 0, ($isSel == 'cut'))) . '">' . IconUtility::getSpriteIcon(('actions-edit-cut' . ($isSel == 'cut' ? '-release' : '')), array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.cut', TRUE))) . '</a>';
825 } else {
826 $cells[] = $this->spaceIcon;
827 }
828 } else {
829 // For numeric pads, add select checkboxes:
830 $n = '_FILE|' . $md5;
831 $this->CBnames[] = $n;
832 $checked = $this->clipObj->isSelected('_FILE', $md5) ? ' checked="checked"' : '';
833 $cells[] = '<label class="btn btn-default btn-checkbox"><input type="hidden" name="CBH[' . $n . ']" value="0" /><input type="checkbox" name="CBC[' . $n . ']" value="' . htmlspecialchars($fullIdentifier) . '" ' . $checked . ' /><span class="t3-icon fa"></span></label>';
834 }
835 // Display PASTE button, if directory:
836 $elFromTable = $this->clipObj->elFromTable('_FILE');
837 if ($fileOrFolderObject instanceof Folder && count($elFromTable) && $fileOrFolderObject->checkActionPermission('write')) {
838 $addPasteButton = TRUE;
839 foreach ($elFromTable as $element) {
840 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
841 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $fileOrFolderObject)) {
842 $addPasteButton = FALSE;
843 }
844 }
845 if ($addPasteButton) {
846 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->pasteUrl('_FILE', $fullIdentifier)) . '" onclick="return ' . htmlspecialchars($this->clipObj->confirmMsg('_FILE', $fullIdentifier, 'into', $elFromTable)) . '" title="' . $this->getLanguageService()->getLL('clip_pasteInto', TRUE) . '">' . IconUtility::getSpriteIcon('actions-document-paste-into') . '</a>';
847 }
848 }
849 // Compile items into a DIV-element:
850 return ' <div class="btn-group" role="group">' . implode('', $cells) . '</div>';
851 }
852
853 /**
854 * Creates the edit control section
855 *
856 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the edit control section for the listing.
857 * @return string HTML-table
858 */
859 public function makeEdit($fileOrFolderObject) {
860 $cells = array();
861 $fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
862 // Edit file content (if editable)
863 if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('write') && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $fileOrFolderObject->getExtension())) {
864 $url = BackendUtility::getModuleUrl('file_edit', array('target' => $fullIdentifier));
865 $editOnClick = 'top.content.list_frame.location.href=top.TS.PATH_typo3+' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+top.rawurlencode(top.content.list_frame.document.location.pathname+top.content.list_frame.document.location.search);return false;';
866 $cells['edit'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($editOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.editcontent') . '">' . IconUtility::getSpriteIcon('actions-page-open') . '</a>';
867 } else {
868 $cells['edit'] = $this->spaceIcon;
869 }
870 if ($fileOrFolderObject instanceof File) {
871 $fileUrl = $fileOrFolderObject->getPublicUrl(TRUE);
872 if ($fileUrl) {
873 $aOnClick = 'return top.openUrlInWindow(' . GeneralUtility::quoteJSvalue($fileUrl) . ', \'WebFile\');';
874 $cells['view'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($aOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.view') . '">' . IconUtility::getSpriteIcon('actions-document-view') . '</a>';
875 } else {
876 $cells['view'] = $this->spaceIcon;
877 }
878 } else {
879 $cells['view'] = $this->spaceIcon;
880 }
881 // rename the file
882 if ($fileOrFolderObject->checkActionPermission('rename')) {
883 $url = BackendUtility::getModuleUrl('file_rename', array('target' => $fullIdentifier));
884 $renameOnClick = 'top.content.list_frame.location.href = top.TS.PATH_typo3+' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+top.rawurlencode(top.content.list_frame.document.location.pathname+top.content.list_frame.document.location.search);return false;';
885 $cells['rename'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($renameOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.rename') . '">' . IconUtility::getSpriteIcon('actions-edit-rename') . '</a>';
886 } else {
887 $cells['rename'] = $this->spaceIcon;
888 }
889 if ($fileOrFolderObject->checkActionPermission('read')) {
890 $infoOnClick = '';
891 if ($fileOrFolderObject instanceof Folder) {
892 $infoOnClick = 'top.launchView( \'_FOLDER\', ' . GeneralUtility::quoteJSvalue($fullIdentifier) . ');return false;';
893 } elseif ($fileOrFolderObject instanceof File) {
894 $infoOnClick = 'top.launchView( \'_FILE\', ' . GeneralUtility::quoteJSvalue($fullIdentifier) . ');return false;';
895 }
896 $cells['info'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($infoOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.info') . '">' . IconUtility::getSpriteIcon('status-dialog-information') . '</a>';
897 } else {
898 $cells['info'] = $this->spaceIcon;
899 }
900
901 // delete the file
902 if ($fileOrFolderObject->checkActionPermission('delete')) {
903 $identifier = $fileOrFolderObject->getIdentifier();
904 if ($fileOrFolderObject instanceof Folder) {
905 $referenceCountText = BackendUtility::referenceCount('_FILE', $identifier, ' (There are %s reference(s) to this folder!)');
906 } else {
907 $referenceCountText = BackendUtility::referenceCount('sys_file', $fileOrFolderObject->getUid(), ' (There are %s reference(s) to this file!)');
908 }
909
910 if ($this->getBackendUser()->jsConfirmation(JsConfirmation::DELETE)) {
911 $confirmationCheck = 'confirm(' . GeneralUtility::quoteJSvalue(sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:mess.delete'), $fileOrFolderObject->getName()) . $referenceCountText) . ')';
912 } else {
913 $confirmationCheck = '1 == 1';
914 }
915
916 $removeOnClick = 'if (' . $confirmationCheck . ') { top.content.list_frame.location.href=top.TS.PATH_typo3+' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('tce_file') .'&file[delete][0][data]=' . rawurlencode($fileOrFolderObject->getCombinedIdentifier()) . '&vC=' . $this->getBackendUser()->veriCode() . BackendUtility::getUrlToken('tceAction') . '&redirect=') . '+top.rawurlencode(top.content.list_frame.document.location.pathname+top.content.list_frame.document.location.search);};';
917
918 $cells['delete'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($removeOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.delete') . '">' . IconUtility::getSpriteIcon('actions-edit-delete') . '</a>';
919 } else {
920 $cells['delete'] = $this->spaceIcon;
921 }
922
923 // Hook for manipulating edit icons.
924 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['fileList']['editIconsHook'])) {
925 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['fileList']['editIconsHook'] as $classData) {
926 $hookObject = GeneralUtility::getUserObj($classData);
927 if (!$hookObject instanceof FileListEditIconHookInterface) {
928 throw new \UnexpectedValueException(
929 '$hookObject must implement interface \\TYPO3\\CMS\\Filelist\\FileListEditIconHookInterface',
930 1235225797
931 );
932 }
933 $hookObject->manipulateEditIcons($cells, $this);
934 }
935 }
936 // Compile items into a DIV-element:
937 return '<div class="btn-group">' . implode('', $cells) . '</div>';
938 }
939
940 /**
941 * Make reference count
942 *
943 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the clipboard panel for the listing.
944 * @return string HTML
945 */
946 public function makeRef($fileOrFolderObject) {
947 if ($fileOrFolderObject instanceof FolderInterface) {
948 return '-';
949 }
950 // Look up the file in the sys_refindex.
951 // Exclude sys_file_metadata records as these are no use references
952 $referenceCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
953 '*',
954 'sys_refindex',
955 'ref_table=\'sys_file\' AND ref_uid = ' . (int)$fileOrFolderObject->getUid() . ' AND deleted=0 AND tablename != "sys_file_metadata"'
956 );
957 return $this->generateReferenceToolTip($referenceCount, '\'_FILE\', ' . GeneralUtility::quoteJSvalue($fileOrFolderObject->getCombinedIdentifier()));
958 }
959
960 /**
961 * Returns the database connection
962 * @return DatabaseConnection
963 */
964 protected function getDatabaseConnection() {
965 return $GLOBALS['TYPO3_DB'];
966 }
967
968 /**
969 * Returns an instance of LanguageService
970 *
971 * @return \TYPO3\CMS\Lang\LanguageService
972 */
973 protected function getLanguageService() {
974 return $GLOBALS['LANG'];
975 }
976
977 /**
978 * Returns the current BE user.
979 *
980 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
981 */
982 protected function getBackendUser() {
983 return $GLOBALS['BE_USER'];
984 }
985
986 }