[BUGFIX] Enable overriding header of backend preview again
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / View / FolderUtilityRenderer.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Recordlist\View;
17
18 use TYPO3\CMS\Backend\Routing\UriBuilder;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Resource\Folder;
22 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
23 use TYPO3\CMS\Core\Resource\Security\FileNameValidator;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\HttpUtility;
26 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
27
28 /**
29 * Renders utility forms used in the views for files/folders of Element and Link Browser
30 * @internal
31 */
32 class FolderUtilityRenderer
33 {
34 /**
35 * @var LinkParameterProviderInterface
36 */
37 protected $parameterProvider;
38 protected UriBuilder $uriBuilder;
39
40 /**
41 * @param LinkParameterProviderInterface $parameterProvider
42 */
43 public function __construct(LinkParameterProviderInterface $parameterProvider)
44 {
45 $this->parameterProvider = $parameterProvider;
46 $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
47 }
48
49 /**
50 * For TBE: Makes a form for creating new folders in the filemount the user is browsing.
51 * The folder creation request is sent to the tce_file.php script in the core which will handle the creation.
52 *
53 * @param Folder $folderObject Absolute filepath on server in which to create the new folder.
54 *
55 * @return string HTML for the create folder form.
56 */
57 public function createFolder(Folder $folderObject)
58 {
59 $backendUser = $this->getBackendUser();
60 $userTsConfig = $backendUser->getTSConfig();
61 $lang = $this->getLanguageService();
62
63 if (!$folderObject->checkActionPermission('write')
64 || (!$backendUser->isAdmin() && !($userTsConfig['options.']['createFoldersInEB'] ?? false))
65 || ($userTsConfig['options.']['folderTree.']['hideCreateFolder'] ?? false)
66 ) {
67 // Do not show create folder form if it is denied
68 return '';
69 }
70
71 $formAction = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
72 $markup = [];
73 $markup[] = '<form class="pt-3 pb-3" action="' . htmlspecialchars($formAction)
74 . '" method="post" name="editform" enctype="multipart/form-data">';
75 $markup[] = '<h4>' . htmlspecialchars($lang->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang.xlf:create_folder.title')) . '</h4>';
76 $markup[] = '<div class="input-group">';
77 $markup[] = '<input class="form-control" type="text" name="data[newfolder][0][data]" placeholder="' . htmlspecialchars($lang->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang.xlf:create_folder.placeholder')) . '" />';
78 $markup[] = '<span class="input-group-btn">';
79 $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
80 . htmlspecialchars($lang->sL('LLL:EXT:recordlist/Resources/Private/Language/locallang.xlf:create_folder.submit')) . '" />';
81 $markup[] = '</span>';
82 $markup[] = '</div>';
83 $markup[] = '<input type="hidden" name="data[newfolder][0][target]" value="'
84 . htmlspecialchars($folderObject->getCombinedIdentifier()) . '" />';
85
86 // Make footer of upload form, including the submit button:
87 $redirectValue = $this->parameterProvider->getScriptUrl() . HttpUtility::buildQueryString(
88 $this->parameterProvider->getUrlParameters(
89 ['identifier' => $folderObject->getCombinedIdentifier()]
90 ),
91 '&'
92 );
93 $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
94
95 $markup[] = '</form>';
96
97 return implode(LF, $markup);
98 }
99
100 /**
101 * Makes an upload form for uploading files to the filemount the user is browsing.
102 * The files are uploaded to the tce_file.php script in the core which will handle the upload.
103 *
104 * @param Folder $folderObject
105 * @param string[] $allowedExtensions
106 *
107 * @return string HTML for an upload form.
108 */
109 public function uploadForm(Folder $folderObject, array $allowedExtensions)
110 {
111 if (!$folderObject->checkActionPermission('write')) {
112 return '';
113 }
114 // Read configuration of upload field count
115 $allowUpload = (bool)($this->getBackendUser()->getTSConfig()['options.']['folderTree.']['uploadFieldsInLinkBrowser'] ?? true);
116 if (!$allowUpload) {
117 return '';
118 }
119
120 $lang = $this->getLanguageService();
121 // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
122 $fileExtList = [];
123 $fileNameVerifier = GeneralUtility::makeInstance(FileNameValidator::class);
124 foreach ($allowedExtensions as $fileExt) {
125 if ($fileNameVerifier->isValid('.' . $fileExt)) {
126 $fileExtList[] = '<span class="label label-success">'
127 . strtoupper(htmlspecialchars($fileExt)) . '</span>';
128 }
129 }
130 $formAction = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
131 $combinedIdentifier = $folderObject->getCombinedIdentifier();
132 $markup = [];
133 if (!empty($fileExtList)) {
134 $markup[] = '<div class="row">';
135 $markup[] = ' <label>';
136 $markup[] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.allowedFileExtensions')) . '<br/>';
137 $markup[] = ' </label>';
138 $markup[] = ' <div>' . implode(' ', $fileExtList) . '</div>';
139 $markup[] = '</div>';
140 }
141
142 $redirectValue = $this->parameterProvider->getScriptUrl() . HttpUtility::buildQueryString(
143 $this->parameterProvider->getUrlParameters(['identifier' => $combinedIdentifier]),
144 '&'
145 );
146 $markup[] = '<form class="pt-3 pb-3" action="' . htmlspecialchars($formAction) . '" method="post" name="editform" enctype="multipart/form-data">';
147 $markup[] = '<input type="hidden" name="data[upload][0][target]" value="' . htmlspecialchars($combinedIdentifier) . '" />';
148 $markup[] = '<input type="hidden" name="data[upload][0][data]" value="0" />';
149 $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
150 $markup[] = '<div class="row">';
151 $markup[] = '<div class="col col-6">';
152 $markup[] = ' <h4>' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_upload.php.pagetitle')) . '</h4>';
153 $markup[] = '</div>';
154 $markup[] = '<div class="col col-6">';
155 $markup[] = '<div class="form-check form-switch float-end">';
156 $markup[] = ' <input class="form-check-input" type="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" />';
157 $markup[] = ' <label class="form-check-label" for="overwriteExistingFiles">';
158 $markup[] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:overwriteExistingFiles'));
159 $markup[] = ' </label>';
160 $markup[] = '</div>';
161 $markup[] = '</div>';
162 $markup[] = '<div class="col">';
163 $markup[] = '<div class="input-group">';
164 $markup[] = '<input type="file" multiple="multiple" name="upload_0[]" class="form-control" />';
165 $markup[] = '<div class="input-group-btn">';
166 $markup[] = '<input class="btn btn-default" type="submit" name="submit" value="'
167 . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_upload.php.submit')) . '" />';
168 $markup[] = '</div>';
169 $markup[] = '</div>';
170 $markup[] = '</div>';
171
172 $markup[] = '</div>';
173 $markup[] = '</form>';
174
175 $code = implode(LF, $markup);
176
177 // Add online media
178 // Create a list of allowed file extensions in a readable format "youtube, vimeo" etc.
179 $fileExtList = [];
180 $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
181 foreach ($onlineMediaFileExt as $fileExt) {
182 if ($fileNameVerifier->isValid('.' . $fileExt)
183 && (empty($allowedExtensions) || in_array($fileExt, $allowedExtensions, true))
184 ) {
185 $fileExtList[] = '<span class="label label-success">' . strtoupper(htmlspecialchars($fileExt)) . '</span>';
186 }
187 }
188 if (!empty($fileExtList)) {
189 $formAction = (string)$this->uriBuilder->buildUriFromRoute('online_media');
190
191 $markup = [];
192 $markup[] = '<form class="pt-3 pb-3" action="' . htmlspecialchars($formAction)
193 . '" method="post" name="editform1" id="typo3-addMediaForm" enctype="multipart/form-data">';
194 $markup[] = '<input type="hidden" name="redirect" value="' . htmlspecialchars($redirectValue) . '" />';
195 $markup[] = '<input type="hidden" name="data[newMedia][0][target]" value="' . htmlspecialchars($folderObject->getCombinedIdentifier()) . '" />';
196 $markup[] = '<input type="hidden" name="data[newMedia][0][allowed]" value="' . htmlspecialchars(implode(',', $allowedExtensions)) . '" />';
197 $markup[] = '<h4>' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media')) . '</h4>';
198 $markup[] = '<div class="row">';
199 $markup[] = '<div class="col">';
200 $markup[] = '<div class="input-group">';
201 $markup[] = '<input type="url" name="data[newMedia][0][url]" class="form-control" placeholder="'
202 . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.placeholder')) . '" />';
203 $markup[] = '<div class="input-group-btn">';
204 $markup[] = '<button class="btn btn-default">'
205 . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.submit')) . '</button>';
206 $markup[] = '</div>';
207 $markup[] = '</div>';
208 $markup[] = '</div>';
209 $markup[] = '<div class="row mt-1">';
210 $markup[] = '<div class="col-auto">';
211 $markup[] = $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:online_media.new_media.allowedProviders');
212 $markup[] = '</div>';
213 $markup[] = '<div class="col">';
214 $markup[] = implode(' ', $fileExtList);
215 $markup[] = '</div>';
216 $markup[] = '</div>';
217 $markup[] = '</div>';
218 $markup[] = '</form>';
219
220 $code .= implode(LF, $markup);
221 }
222
223 return $code;
224 }
225
226 protected function getLanguageService(): LanguageService
227 {
228 return $GLOBALS['LANG'];
229 }
230
231 protected function getBackendUser(): BackendUserAuthentication
232 {
233 return $GLOBALS['BE_USER'];
234 }
235 }