‪TYPO3CMS  ‪main
FolderUtilityRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
30 
36 {
41  protected ‪UriBuilder ‪$uriBuilder;
42 
44  {
45  $this->parameterProvider = ‪$parameterProvider;
46  $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
47  }
48 
57  public function ‪createFolder(ServerRequestInterface $request, ‪Folder $folderObject)
58  {
59  $lang = $this->‪getLanguageService();
60 
61  if (!$folderObject->‪checkActionPermission('write')) {
62  // Do not show create folder form if it is denied
63  return '';
64  }
65 
66  $formAction = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
67  $markup = [];
68  $markup[] = '<form class="pt-3 pb-3" action="' . htmlspecialchars($formAction)
69  . '" method="post" name="editform" enctype="multipart/form-data">';
70  $markup[] = '<h4>' . htmlspecialchars($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:create_folder.title')) . '</h4>';
71  $markup[] = '<div class="input-group">';
72  $markup[] = '<input class="form-control" type="text" name="data[newfolder][0][data]" placeholder="' . htmlspecialchars($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:create_folder.placeholder')) . '" />';
73  $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
74  . htmlspecialchars($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:create_folder.submit')) . '" />';
75  $markup[] = '</div>';
76  $markup[] = '<input type="hidden" name="data[newfolder][0][target]" value="'
77  . htmlspecialchars($folderObject->‪getCombinedIdentifier()) . '" />';
78 
79  // Make footer of upload form, including the submit button:
80  $redirectValue = (string)$this->uriBuilder->buildUriFromRequest(
81  $request,
82  $this->parameterProvider->getUrlParameters(
83  ['identifier' => $folderObject->‪getCombinedIdentifier()]
84  )
85  );
86  $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
87 
88  $markup[] = '</form>';
89 
90  return implode(LF, $markup);
91  }
92 
99  public function ‪uploadForm(ServerRequestInterface $request, Folder $folderObject, ?FileExtensionFilter $fileExtensionFilter = null)
100  {
101  if (!$folderObject->checkActionPermission('write')) {
102  return '';
103  }
104  // Read configuration of upload field count
105  $allowUpload = (bool)($this->‪getBackendUser()->getTSConfig()['options.']['folderTree.']['uploadFieldsInLinkBrowser'] ?? true);
106  if (!$allowUpload) {
107  return '';
108  }
109 
110  $list = ['*'];
111  $denyList = false;
112  $allowedOnlineMediaList = [];
113  $lang = $this->‪getLanguageService();
114 
115  if ($fileExtensionFilter !== null) {
116  $resolvedFileExtensions = $fileExtensionFilter->getFilteredFileExtensions();
117  if (($resolvedFileExtensions['allowedFileExtensions'] ?? []) !== []) {
118  $list = $resolvedFileExtensions['allowedFileExtensions'];
119  } elseif (($resolvedFileExtensions['disallowedFileExtensions'] ?? []) !== []) {
120  $denyList = true;
121  $list = $resolvedFileExtensions['disallowedFileExtensions'];
122  }
123  }
124 
125  $fileNameVerifier = GeneralUtility::makeInstance(FileNameValidator::class);
126  foreach ($list as $fileExt) {
127  if (($fileExt === '*' && !$denyList) || $fileNameVerifier->isValid('.' . $fileExt)) {
128  $allowedOnlineMediaList[] = '<span class="badge badge-' . ($denyList ? 'danger' : 'success') . '">' . strtoupper(htmlspecialchars($fileExt)) . '</span>';
129  }
130  }
131  $markup = [];
132  if (!empty($allowedOnlineMediaList)) {
133  $markup[] = '<div class="row">';
134  $markup[] = ' <label>';
135  $markup[] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.' . ($denyList ? 'disallowedFileExtensions' : 'allowedFileExtensions'))) . '<br/>';
136  $markup[] = ' </label>';
137  $markup[] = ' <div>' . implode(' ', $allowedOnlineMediaList) . '</div>';
138  $markup[] = '</div>';
139  }
140 
141  $formAction = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
142  $combinedIdentifier = $folderObject->getCombinedIdentifier();
143  $redirectValue = (string)$this->uriBuilder->buildUriFromRequest($request, $this->parameterProvider->getUrlParameters(['identifier' => $combinedIdentifier]));
144  $markup[] = '<form class="pt-3 pb-3" action="' . htmlspecialchars($formAction) . '" method="post" name="editform" enctype="multipart/form-data">';
145  $markup[] = '<input type="hidden" name="data[upload][0][target]" value="' . htmlspecialchars($combinedIdentifier) . '" />';
146  $markup[] = '<input type="hidden" name="data[upload][0][data]" value="0" />';
147  $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
148  $markup[] = '<div class="row">';
149  $markup[] = '<div class="col-auto me-auto">';
150  $markup[] = ' <h4>' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_upload.php.pagetitle')) . '</h4>';
151  $markup[] = '</div>';
152  $markup[] = '<div class="col-auto">';
153  $markup[] = '<div class="form-check form-switch">';
154  $markup[] = ' <input class="form-check-input" type="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" />';
155  $markup[] = ' <label class="form-check-label" for="overwriteExistingFiles">';
156  $markup[] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:overwriteExistingFiles'));
157  $markup[] = ' </label>';
158  $markup[] = '</div>';
159  $markup[] = '</div>';
160  $markup[] = '<div class="col-12">';
161  $markup[] = '<div class="input-group">';
162  $markup[] = '<input type="file" multiple="multiple" name="upload_0[]" class="form-control" />';
163  $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
164  . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_upload.php.submit')) . '" />';
165  $markup[] = '</div>';
166  $markup[] = '</div>';
167 
168  $markup[] = '</div>';
169  $markup[] = '</form>';
170 
171  $code = implode(LF, $markup);
172 
173  // Add online media
174  // Create a list of allowed file extensions in a readable format "youtube, vimeo" etc.
175  $allowedOnlineMediaList = [];
176  foreach (GeneralUtility::makeInstance(OnlineMediaHelperRegistry::class)->getSupportedFileExtensions() as $supportedFileExtension) {
177  if ($fileNameVerifier->isValid('.' . $supportedFileExtension)
178  && ($fileExtensionFilter === null || $fileExtensionFilter->isAllowed($supportedFileExtension))
179  ) {
180  $allowedOnlineMediaList[$supportedFileExtension] = '<span class="badge badge-success">' . strtoupper(htmlspecialchars($supportedFileExtension)) . '</span>';
181  }
182  }
183  if (!empty($allowedOnlineMediaList)) {
184  $formAction = (string)$this->uriBuilder->buildUriFromRoute('online_media');
185 
186  $markup = [];
187  $markup[] = '<form class="pt-3 pb-3" action="' . htmlspecialchars($formAction) . '" method="post" name="editform1" id="typo3-addMediaForm" enctype="multipart/form-data">';
188  $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
189  $markup[] = '<input type="hidden" name="data[newMedia][0][target]" value="' . htmlspecialchars($folderObject->getCombinedIdentifier()) . '" />';
190  $markup[] = '<input type="hidden" name="data[newMedia][0][allowed]" value="' . htmlspecialchars(implode(',', array_keys($allowedOnlineMediaList))) . '" />';
191  $markup[] = '<h4>' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:onlinemedia.new_media')) . '</h4>';
192  $markup[] = '<div class="row">';
193  $markup[] = '<div class="col">';
194  $markup[] = '<div class="input-group">';
195  $markup[] = '<input type="url" name="data[newMedia][0][url]" class="form-control" placeholder="'
196  . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.placeholder')) . '" />';
197  $markup[] = '<button class="btn btn-default">'
198  . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.submit')) . '</button>';
199  $markup[] = '</div>';
200  $markup[] = '</div>';
201  $markup[] = '<div class="row mt-1">';
202  $markup[] = '<div class="col-auto">';
203  $markup[] = $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.allowedProviders');
204  $markup[] = '</div>';
205  $markup[] = '<div class="col">';
206  $markup[] = implode(' ', $allowedOnlineMediaList);
207  $markup[] = '</div>';
208  $markup[] = '</div>';
209  $markup[] = '</div>';
210  $markup[] = '</form>';
211 
212  $code .= implode(LF, $markup);
213  }
214 
215  return $code;
216  }
217 
218  protected function ‪getLanguageService(): ‪LanguageService
219  {
220  return ‪$GLOBALS['LANG'];
221  }
222 
223  protected function ‪getBackendUser(): ‪BackendUserAuthentication
224  {
225  return ‪$GLOBALS['BE_USER'];
226  }
227 }
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer\$uriBuilder
‪UriBuilder $uriBuilder
Definition: FolderUtilityRenderer.php:40
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Core\Resource\Security\FileNameValidator
Definition: FileNameValidator.php:25
‪TYPO3\CMS\Backend\View
Definition: AuthenticationStyleInformation.php:18
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer\$parameterProvider
‪LinkParameterProviderInterface $parameterProvider
Definition: FolderUtilityRenderer.php:39
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer\getBackendUser
‪getBackendUser()
Definition: FolderUtilityRenderer.php:222
‪TYPO3\CMS\Core\Resource\Folder\checkActionPermission
‪bool checkActionPermission($action)
Definition: Folder.php:442
‪TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter
Definition: FileExtensionFilter.php:31
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer
Definition: FolderUtilityRenderer.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer\uploadForm
‪string uploadForm(ServerRequestInterface $request, Folder $folderObject, ?FileExtensionFilter $fileExtensionFilter=null)
Definition: FolderUtilityRenderer.php:98
‪TYPO3\CMS\Core\Resource\Folder\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: Folder.php:166
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer\createFolder
‪string createFolder(ServerRequestInterface $request, Folder $folderObject)
Definition: FolderUtilityRenderer.php:56
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer\getLanguageService
‪getLanguageService()
Definition: FolderUtilityRenderer.php:217
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer\__construct
‪__construct(LinkParameterProviderInterface $parameterProvider)
Definition: FolderUtilityRenderer.php:42