[BUGFIX] RTE Image Wizard
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / ImageHandler / AddImageHandler.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\ImageHandler;
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 Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Backend\Tree\View\ElementBrowserFolderTreeView;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Imaging\IconFactory;
23 use TYPO3\CMS\Core\Page\PageRenderer;
24 use TYPO3\CMS\Core\Resource\Exception;
25 use TYPO3\CMS\Core\Resource\File;
26 use TYPO3\CMS\Core\Resource\FileInterface;
27 use TYPO3\CMS\Core\Resource\FileRepository;
28 use TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter;
29 use TYPO3\CMS\Core\Resource\Folder;
30 use TYPO3\CMS\Core\Resource\ProcessedFile;
31 use TYPO3\CMS\Core\Resource\ResourceFactory;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33 use TYPO3\CMS\Lang\LanguageService;
34 use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController;
35 use TYPO3\CMS\Recordlist\LinkHandler\LinkHandlerInterface;
36 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
37 use TYPO3\CMS\Recordlist\View\FolderUtilityRenderer;
38 use TYPO3\CMS\Rtehtmlarea\Controller\SelectImageController;
39
40 class AddImageHandler implements LinkParameterProviderInterface, LinkHandlerInterface
41 {
42 /**
43 * Current mode: One of 'magic' or 'plain'
44 *
45 * @var string
46 */
47 protected $mode;
48
49 /**
50 * @var SelectImageController
51 */
52 protected $selectImageController;
53
54 /**
55 * Relevant for RTE mode "plain": the maximum width an image must have to be selectable.
56 *
57 * @var int
58 */
59 protected $plainMaxWidth;
60
61 /**
62 * Relevant for RTE mode "plain": the maximum height an image must have to be selectable.
63 *
64 * @var int
65 */
66 protected $plainMaxHeight;
67
68 /**
69 * @var string|NULL
70 */
71 protected $expandFolder;
72
73 /**
74 * @var string
75 */
76 protected $defaultClass;
77
78 /**
79 * @var Folder
80 */
81 protected $selectedFolder;
82
83 /**
84 * Holds information about files
85 *
86 * @var mixed[][]
87 */
88 protected $elements = array();
89
90 /**
91 * @var string
92 */
93 protected $searchWord;
94
95 /**
96 * @var FileRepository
97 */
98 protected $fileRepository;
99
100 /**
101 * URL of current request
102 *
103 * @var string
104 */
105 protected $thisScript = '';
106
107 /**
108 * @var IconFactory
109 */
110 protected $iconFactory;
111
112 /**
113 * Initialize the handler
114 *
115 * @param AbstractLinkBrowserController $linkBrowser
116 * @param string $identifier
117 * @param array $configuration Page TSconfig
118 *
119 * @return void
120 * @throws \InvalidArgumentException
121 */
122 public function initialize(AbstractLinkBrowserController $linkBrowser, $identifier, array $configuration)
123 {
124 $this->fileRepository = GeneralUtility::makeInstance(FileRepository::class);
125 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
126
127 $this->expandFolder = GeneralUtility::_GP('expandFolder');
128 $this->searchWord = (string)GeneralUtility::_GP('searchWord');
129
130 if ($identifier !== 'plain' && $identifier !== 'magic') {
131 throw new \InvalidArgumentException('The given identifier "' . $identifier . '" is not supported by this handler."', 1455499720);
132 }
133 if (!$linkBrowser instanceof SelectImageController) {
134 throw new \InvalidArgumentException('The given $linkBrowser must be of type SelectImageController."', 1455499721);
135 }
136 $this->mode = $identifier;
137 $this->selectImageController = $linkBrowser;
138
139 $buttonConfiguration = $linkBrowser->getButtonConfiguration();
140 $this->plainMaxWidth = empty($buttonConfiguration['options.']['plain.']['maxWidth'])
141 ? 640
142 : $buttonConfiguration['options.']['plain.']['maxWidth'];
143 $this->plainMaxHeight = empty($buttonConfiguration['options.']['plain.']['maxHeight'])
144 ? 680
145 : $buttonConfiguration['options.']['plain.']['maxHeight'];
146
147 $this->getLanguageService()->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_selectimagecontroller.xlf');
148
149 }
150
151 /**
152 * Render the link handler
153 *
154 * @param ServerRequestInterface $request
155 *
156 * @return string
157 */
158 public function render(ServerRequestInterface $request)
159 {
160 GeneralUtility::makeInstance(PageRenderer::class)->loadRequireJsModule('TYPO3/CMS/Rtehtmlarea/AddImage');
161
162 $backendUser = $this->getBackendUser();
163
164 // The key number 3 of the bparams contains the "allowed" string. Disallowed is not passed to
165 // the element browser at all but only filtered out in TCEMain afterwards
166 $bparams = explode('|', $this->selectImageController->getUrlParameters()['bparams']);
167 if (isset($bparams[3])) {
168 $allowedFileExtensions = GeneralUtility::trimExplode(',', $bparams[3], true);
169 } else {
170 $allowedFileExtensions = GeneralUtility::trimExplode(
171 ',',
172 $this->mode === 'plain'
173 ? SelectImageController::PLAIN_MODE_IMAGE_FILE_EXTENSIONS
174 : $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
175 true
176 );
177 }
178 if (!empty($allowedFileExtensions) && $allowedFileExtensions[0] !== 'sys_file' && $allowedFileExtensions[0] !== '*') {
179 // Create new filter object
180 $filterObject = GeneralUtility::makeInstance(FileExtensionFilter::class);
181 $filterObject->setAllowedFileExtensions($allowedFileExtensions);
182 // Set file extension filters on all storages
183 $storages = $backendUser->getFileStorages();
184 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
185 foreach ($storages as $storage) {
186 $storage->addFileAndFolderNameFilter(array($filterObject, 'filterFileList'));
187 }
188 }
189 if ($this->expandFolder) {
190 $fileOrFolderObject = null;
191
192 // Try to fetch the folder the user had open the last time he browsed files
193 // Fallback to the default folder in case the last used folder is not existing
194 try {
195 $fileOrFolderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->expandFolder);
196 } catch (Exception $accessException) {
197 // We're just catching the exception here, nothing to be done if folder does not exist or is not accessible.
198 } catch (\InvalidArgumentException $driverMissingExecption) {
199 // We're just catching the exception here, nothing to be done if the driver does not exist anymore.
200 }
201
202 if ($fileOrFolderObject instanceof Folder) {
203 // It's a folder
204 $this->selectedFolder = $fileOrFolderObject;
205 } elseif ($fileOrFolderObject instanceof FileInterface) {
206 // It's a file
207 $this->selectedFolder = $fileOrFolderObject->getParentFolder();
208 }
209 }
210 // Or get the user's default upload folder
211 if (!$this->selectedFolder) {
212 try {
213 $this->selectedFolder = $backendUser->getDefaultUploadFolder();
214 } catch (\Exception $e) {
215 // The configured default user folder does not exist
216 }
217 }
218 // Build the file upload and folder creation form
219 $uploadForm = '';
220 $createFolder = '';
221 if ($this->selectedFolder) {
222 $folderUtilityRenderer = GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this);
223 $uploadForm = $folderUtilityRenderer->uploadForm($this->selectedFolder, $allowedFileExtensions);
224 $createFolder = $folderUtilityRenderer->createFolder($this->selectedFolder);
225 }
226
227 // Getting flag for showing/not showing thumbnails:
228 $noThumbs = $backendUser->getTSConfigVal('options.noThumbsInRTEimageSelect');
229 $_MOD_SETTINGS = array();
230 if (!$noThumbs) {
231 // MENU-ITEMS, fetching the setting for thumbnails from File>List module:
232 $_MOD_MENU = array('displayThumbs' => '');
233 $_MCONF['name'] = 'file_list';
234 $_MOD_SETTINGS = BackendUtility::getModuleData($_MOD_MENU, GeneralUtility::_GP('SET'), $_MCONF['name']);
235 }
236 $noThumbs = $noThumbs ?: !$_MOD_SETTINGS['displayThumbs'];
237 // Create folder tree:
238 /** @var ElementBrowserFolderTreeView $folderTree */
239 $folderTree = GeneralUtility::makeInstance(ElementBrowserFolderTreeView::class);
240 $folderTree->setLinkParameterProvider($this);
241 $tree = $folderTree->getBrowsableTree();
242 if ($this->selectedFolder) {
243 $files = $this->renderFilesInFolder($this->selectedFolder, $allowedFileExtensions, $noThumbs);
244 } else {
245 $files = '';
246 }
247
248 $content = '';
249 // Insert the upload form on top, if so configured
250 if ($backendUser->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
251 $content .= $uploadForm;
252 }
253 // Putting the parts together, side by side:
254 $content .= '
255
256 <!--
257 Wrapper table for folder tree / filelist:
258 -->
259 <div class="element-browser-section element-browser-filetree">
260 <table border="0" cellpadding="0" cellspacing="0" id="typo3-EBfiles">
261 <tr>
262 <td class="c-wCell" valign="top"><h3>' . $this->getLanguageService()->getLL('folderTree', true) . ':</h3>' . $tree . '</td>
263 <td class="c-wCell" valign="top">' . $files . '</td>
264 </tr>
265 </table>
266 </div>
267 ';
268 // Add help message
269 switch ($this->mode) {
270 case 'plain':
271 $content .= sprintf($this->getLanguageService()->getLL('plainImage_msg'), $this->plainMaxWidth, $this->plainMaxHeight);
272 break;
273 case 'magic':
274 $content .= sprintf($this->getLanguageService()->getLL('magicImage_msg'));
275 break;
276 }
277 // Adding create folder + upload forms if applicable:
278 if (!$backendUser->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
279 $content .= $uploadForm;
280 }
281 $content .= $createFolder;
282 // Add some space
283 $content .= '<br /><br />';
284
285 return $content;
286 }
287
288 /**
289 * For TYPO3 Element Browser: Expand folder of files.
290 *
291 * @param Folder $folder The folder path to expand
292 * @param array $extensionList List of fileextensions to show
293 * @param bool $noThumbs Whether to show thumbnails or not. If set, no thumbnails are shown.
294 * @return string HTML output
295 */
296 public function renderFilesInFolder(Folder $folder, array $extensionList = [], $noThumbs = false)
297 {
298 if (!$folder->checkActionPermission('read')) {
299 return '';
300 }
301 $lang = $this->getLanguageService();
302 $titleLen = (int)$this->getBackendUser()->uc['titleLen'];
303
304 if ($this->searchWord !== '') {
305 $files = $this->fileRepository->searchByName($folder, $this->searchWord);
306 } else {
307 $extensionList = !empty($extensionList) && $extensionList[0] === '*' ? [] : $extensionList;
308 $files = $this->getFilesInFolder($folder, $extensionList);
309 }
310 $filesCount = count($files);
311
312 $lines = array();
313
314 // Create the header of current folder:
315 $folderIcon = $this->iconFactory->getIconForResource($folder, Icon::SIZE_SMALL);
316
317 $lines[] = '
318 <tr class="t3-row-header">
319 <th class="col-title" nowrap="nowrap">' . $folderIcon . ' ' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($folder->getIdentifier(), $titleLen)) . '</th>
320 <th class="col-control" nowrap="nowrap"></th>
321 <th class="col-clipboard" nowrap="nowrap">
322 <a href="#" class="btn btn-default" id="t3js-importSelection" title="' . $lang->getLL('importSelection', true) . '">' . $this->iconFactory->getIcon('actions-document-import-t3d', Icon::SIZE_SMALL) . '</a>
323 <a href="#" class="btn btn-default" id="t3js-toggleSelection" title="' . $lang->getLL('toggleSelection', true) . '">' . $this->iconFactory->getIcon('actions-document-select', Icon::SIZE_SMALL) . '</a>
324 </th>
325 <th nowrap="nowrap">&nbsp;</th>
326 </tr>';
327
328 if ($filesCount === 0) {
329 $lines[] = '
330 <tr>
331 <td colspan="4">No files found.</td>
332 </tr>';
333 }
334
335 foreach ($files as $fileObject) {
336 $fileExtension = $fileObject->getExtension();
337 // Thumbnail/size generation:
338 $imgInfo = array();
339 if (!$noThumbs && GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] . ',' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']), strtolower($fileExtension))) {
340 $processedFile = $fileObject->process(
341 ProcessedFile::CONTEXT_IMAGEPREVIEW,
342 array('width' => 64, 'height' => 64)
343 );
344 $imageUrl = $processedFile->getPublicUrl(true);
345 $imgInfo = array(
346 $fileObject->getProperty('width'),
347 $fileObject->getProperty('height')
348 );
349 $pDim = $imgInfo[0] . 'x' . $imgInfo[1] . ' pixels';
350 $clickIcon = '<img src="' . $imageUrl . '"'
351 . ' width="' . $processedFile->getProperty('width') . '"'
352 . ' height="' . $processedFile->getProperty('height') . '"'
353 . ' hspace="5" vspace="5" border="1" />';
354 } else {
355 $clickIcon = '';
356 $pDim = '';
357 }
358 // Create file icon:
359 $size = ' (' . GeneralUtility::formatSize($fileObject->getSize()) . 'bytes' . ($pDim ? ', ' . $pDim : '') . ')';
360 $icon = '<span title="' . htmlspecialchars($fileObject->getName() . $size) . '">' . $this->iconFactory->getIconForResource($fileObject, Icon::SIZE_SMALL) . '</span>';
361 // Create links for adding the file:
362 $filesIndex = count($this->elements);
363 $this->elements['file_' . $filesIndex] = array(
364 'type' => 'file',
365 'table' => 'sys_file',
366 'uid' => $fileObject->getUid(),
367 'fileName' => $fileObject->getName(),
368 'filePath' => $fileObject->getUid(),
369 'fileExt' => $fileExtension,
370 'fileIcon' => $icon
371 );
372 if ($this->fileIsSelectableInFileList($fileObject, $imgInfo)) {
373 $ATag = '<a href="#" class="btn btn-default" title="' . htmlspecialchars($fileObject->getName()) . '" data-file-index="' . htmlspecialchars($filesIndex) . '" data-close="0">';
374 $ATag_alt = '<a href="#" title="' . htmlspecialchars($fileObject->getName()) . '" data-file-index="' . htmlspecialchars($filesIndex) . '" data-close="1">';
375 $ATag_e = '</a>';
376 $bulkCheckBox = '<label class="btn btn-default btn-checkbox"><input type="checkbox" class="typo3-bulk-item" name="file_' . $filesIndex . '" value="0" /><span class="t3-icon fa"></span></label>';
377 } else {
378 $ATag = '';
379 $ATag_alt = '';
380 $ATag_e = '';
381 $bulkCheckBox = '';
382 }
383 // Create link to showing details about the file in a window:
384 $Ahref = BackendUtility::getModuleUrl('show_item', array(
385 'type' => 'file',
386 'table' => '_FILE',
387 'uid' => $fileObject->getCombinedIdentifier(),
388 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
389 ));
390
391 // Combine the stuff:
392 $filenameAndIcon = $ATag_alt . $icon . htmlspecialchars(GeneralUtility::fixed_lgd_cs($fileObject->getName(), $titleLen)) . $ATag_e;
393 // Show element:
394 $lines[] = '
395 <tr class="file_list_normal">
396 <td class="col-title" nowrap="nowrap">' . $filenameAndIcon . '&nbsp;</td>
397 <td class="col-control">
398 <div class="btn-group">' . $ATag . '<span title="' . $lang->getLL('addToList', true) . '">' . $this->iconFactory->getIcon('actions-edit-add', Icon::SIZE_SMALL)->render() . '</span>' . $ATag_e . '
399 <a href="' . htmlspecialchars($Ahref) . '" class="btn btn-default" title="' . $lang->getLL('info', true) . '">' . $this->iconFactory->getIcon('actions-document-info', Icon::SIZE_SMALL) . '</a>
400 </td>
401 <td class="col-clipboard" valign="top">'. $bulkCheckBox .'</td>
402 <td nowrap="nowrap">&nbsp;' . $pDim . '</td>
403 </tr>';
404 if ($pDim) {
405 $lines[] = '
406 <tr>
407 <td class="filelistThumbnail" colspan="4">' . $ATag_alt . $clickIcon . $ATag_e . '</td>
408 </tr>';
409 }
410 }
411
412 $out = '<h3>' . $lang->getLL('files', true) . ' ' . $filesCount . ':</h3>';
413 $out .= GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this)->getFileSearchField($this->searchWord);
414 $out .= '<div id="filelist">';
415 $out .= $this->getBulkSelector($filesCount);
416
417 // Wrap all the rows in table tags:
418 $out .= '
419
420 <!--
421 Filelisting
422 -->
423 <table class="table table-striped table-hover" id="typo3-filelist">
424 ' . implode('', $lines) . '
425 </table>';
426 // Return accumulated content for filelisting:
427 $out .= '</div>';
428 return $out;
429 }
430
431
432 /**
433 * Get a list of Files in a folder filtered by extension
434 *
435 * @param Folder $folder
436 * @param array $extensionList
437 * @return File[]
438 */
439 protected function getFilesInFolder(Folder $folder, array $extensionList)
440 {
441 if (!empty($extensionList)) {
442 /** @var FileExtensionFilter $filter */
443 $filter = GeneralUtility::makeInstance(FileExtensionFilter::class);
444 $filter->setAllowedFileExtensions($extensionList);
445 $folder->setFileAndFolderNameFilters(array(array($filter, 'filterFileList')));
446 }
447 return $folder->getFiles();
448 }
449
450 /**
451 * Get the HTML data required for a bulk selection of files of the TYPO3 Element Browser.
452 *
453 * @param int $filesCount Number of files currently displayed
454 * @return string HTML data required for a bulk selection of files - if $filesCount is 0, nothing is returned
455 */
456 protected function getBulkSelector($filesCount)
457 {
458 if (!$filesCount) {
459 return '';
460 }
461
462 $lang = $this->getLanguageService();
463 $out = '';
464
465 // Getting flag for showing/not showing thumbnails:
466 $noThumbsInEB = $this->getBackendUser()->getTSConfigVal('options.noThumbsInEB');
467 if (!$noThumbsInEB && $this->selectedFolder) {
468 // MENU-ITEMS, fetching the setting for thumbnails from File>List module:
469 $_MOD_MENU = array('displayThumbs' => '');
470 $_MCONF['name'] = 'file_list';
471 $_MOD_SETTINGS = BackendUtility::getModuleData($_MOD_MENU, GeneralUtility::_GP('SET'), $_MCONF['name']);
472 $addParams = GeneralUtility::implodeArrayForUrl('', $this->getUrlParameters(['identifier' => $this->selectedFolder->getCombinedIdentifier()]));
473 $thumbNailCheck = '<div class="checkbox" style="padding:5px 0 15px 0"><label for="checkDisplayThumbs">'
474 . BackendUtility::getFuncCheck(
475 '',
476 'SET[displayThumbs]',
477 $_MOD_SETTINGS['displayThumbs'],
478 $this->thisScript,
479 $addParams,
480 'id="checkDisplayThumbs"'
481 )
482 . $lang->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:displayThumbs', true) . '</label></div>';
483 $out .= $thumbNailCheck;
484 } else {
485 $out .= '<div style="padding-top: 15px;"></div>';
486 }
487 return $out;
488 }
489
490 /**
491 * Checks if the given file is selectable in the filelist.
492 *
493 * In "plain" RTE mode only image files with a maximum width and height are selectable.
494 *
495 * @param FileInterface $file
496 * @param array $imgInfo Image dimensions from \TYPO3\CMS\Core\Imaging\GraphicalFunctions::getImageDimensions()
497 * @return bool TRUE if file is selectable.
498 */
499 protected function fileIsSelectableInFileList(FileInterface $file, array $imgInfo)
500 {
501 return $this->mode !== 'plain'
502 || (GeneralUtility::inList(SelectImageController::PLAIN_MODE_IMAGE_FILE_EXTENSIONS, strtolower($file->getExtension()))
503 && $imgInfo[0] <= $this->plainMaxWidth && $imgInfo[1] <= $this->plainMaxHeight);
504 }
505
506 /**
507 * @return string[] Array of body-tag attributes
508 */
509 public function getBodyTagAttributes()
510 {
511 return [
512 'data-elements' => json_encode($this->elements)
513 ];
514 }
515
516 /**
517 * Returns the URL of the current script
518 *
519 * @return string
520 */
521 public function getScriptUrl()
522 {
523 return $this->selectImageController->getScriptUrl();
524 }
525
526 /**
527 * Provides an array or GET parameters for URL generation
528 *
529 * @param array $values Array of values to include into the parameters or which might influence the parameters
530 *
531 * @return string[] Array of parameters which have to be added to URLs
532 */
533 public function getUrlParameters(array $values)
534 {
535 $parameters = [
536 'expandFolder' => isset($values['identifier']) ? $values['identifier'] : (string)$this->expandFolder
537 ];
538 return array_merge($this->selectImageController->getUrlParameters($values), $parameters);
539 }
540
541 /**
542 * Return TRUE if the handler supports to update a link.
543 *
544 * This is useful for file or page links, when only attributes are changed.
545 *
546 * @return bool
547 */
548 public function isUpdateSupported()
549 {
550 return false;
551 }
552
553 /**
554 * @return array
555 */
556 public function getLinkAttributes()
557 {
558 return [];
559 }
560
561 /**
562 * @param string[] $fieldDefinitions Array of link attribute field definitions
563 * @return string[]
564 */
565 public function modifyLinkAttributes(array $fieldDefinitions)
566 {
567 return $fieldDefinitions;
568 }
569
570 /**
571 * Check if given value is currently the selected item
572 *
573 * This method is only used in the page tree.
574 *
575 * @param array $values Values to be checked
576 *
577 * @return bool Returns TRUE if the given values match the currently selected item
578 */
579 public function isCurrentlySelectedItem(array $values)
580 {
581 return false;
582 }
583
584 /**
585 * Checks if this is the handler for the given link
586 *
587 * The handler may store this information locally for later usage.
588 *
589 * @param array $linkParts Link parts as returned from TypoLinkCodecService
590 *
591 * @return bool
592 */
593 public function canHandleLink(array $linkParts)
594 {
595 return false;
596 }
597
598 /**
599 * Format the current link for HTML output
600 *
601 * @return string
602 */
603 public function formatCurrentUrl()
604 {
605 return '';
606 }
607
608 /**
609 * @return LanguageService
610 */
611 protected function getLanguageService()
612 {
613 return $GLOBALS['LANG'];
614 }
615
616 /**
617 * @return BackendUserAuthentication
618 */
619 protected function getBackendUser()
620 {
621 return $GLOBALS['BE_USER'];
622 }
623 }