[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / CreateFolderController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\File;
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\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Template\ModuleTemplate;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
23 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
24 use TYPO3\CMS\Core\Resource\ResourceFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Core\Utility\MathUtility;
27 use TYPO3\CMS\Fluid\View\StandaloneView;
28
29 /**
30 * Script class for the create-new script
31 *
32 * Displays forms for creating folders (1 to 10), a media asset or a new file.
33 */
34 class CreateFolderController
35 {
36 /**
37 * @var int
38 */
39 public $folderNumber = 10;
40
41 /**
42 * Name of the filemount
43 *
44 * @var string
45 */
46 public $title;
47
48 /**
49 * @var int
50 */
51 public $number;
52
53 /**
54 * Set with the target path inputted in &target
55 *
56 * @var string
57 */
58 public $target;
59
60 /**
61 * The folder object which is the target directory
62 *
63 * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
64 */
65 protected $folderObject;
66
67 /**
68 * Return URL of list module.
69 *
70 * @var string
71 */
72 public $returnUrl;
73
74 /**
75 * @var array
76 */
77 protected $pathInfo;
78
79 /**
80 * Accumulating content
81 *
82 * @var string
83 */
84 public $content;
85
86 /**
87 * ModuleTemplate object
88 *
89 * @var ModuleTemplate
90 */
91 protected $moduleTemplate;
92
93 /**
94 * Constructor
95 */
96 public function __construct()
97 {
98 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
99 $GLOBALS['SOBE'] = $this;
100 $this->init();
101 }
102
103 /**
104 * @throws InsufficientFolderAccessPermissionsException
105 * @throws \RuntimeException
106 */
107 protected function init()
108 {
109 // Initialize GPvars:
110 $this->number = GeneralUtility::_GP('number');
111 $this->target = ($combinedIdentifier = GeneralUtility::_GP('target'));
112 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
113 // create the folder object
114 if ($combinedIdentifier) {
115 $this->folderObject = ResourceFactory::getInstance()
116 ->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
117 }
118 // Cleaning and checking target directory
119 if (!$this->folderObject) {
120 $title = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
121 $message = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
122 throw new \RuntimeException($title . ': ' . $message, 1294586845);
123 }
124 if ($this->folderObject->getStorage()->getUid() === 0) {
125 throw new InsufficientFolderAccessPermissionsException(
126 'You are not allowed to access folders outside your storages',
127 1375889838
128 );
129 }
130
131 $pathInfo = [
132 'combined_identifier' => $this->folderObject->getCombinedIdentifier(),
133 ];
134 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
135 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
136
137 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
138 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
139 $this->moduleTemplate->addJavaScriptCode(
140 'CreateFolderInlineJavaScript',
141 'var path = "' . $this->target . '";
142 var confirmTitle = '
143 . GeneralUtility::quoteJSvalue(
144 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:pleaseConfirm')
145 )
146 . ';
147 var confirmText = '
148 . GeneralUtility::quoteJSvalue(
149 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:mess.redraw')
150 )
151 . ';
152 function reload(a) {
153 var params = "&target="+encodeURIComponent(path)+"&number="+a+"&returnUrl=' . rawurlencode($this->returnUrl) . '";
154 var url = \'' . (string)$uriBuilder->buildUriFromRoute('file_newfolder') . '\';
155 if (!changed) {
156 window.location.href = url + params;
157 } else {
158 var modal = top.TYPO3.Modal.confirm(confirmTitle, confirmText);
159 modal.on(\'confirm.button.cancel\', function(e) {
160 top.TYPO3.Modal.currentModal.trigger(\'modal-dismiss\');
161 });
162 modal.on(\'confirm.button.ok\', function(e) {
163 top.TYPO3.Modal.currentModal.trigger(\'modal-dismiss\');
164 window.location.href = url + params;
165 });
166 }
167 }
168 function backToList() {
169 top.goToModule("file_FilelistList");
170 }
171 var changed = 0;'
172 );
173 }
174
175 /**
176 * Main function, rendering the main module content
177 */
178 public function main()
179 {
180 $lang = $this->getLanguageService();
181 $assigns = [];
182 $assigns['target'] = $this->target;
183 if ($this->folderObject->checkActionPermission('add')) {
184 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
185 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
186 $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
187 $assigns['cshFileNewFolder'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfolder');
188 // Making the selector box for the number of concurrent folder-creations
189 $this->number = MathUtility::forceIntegerInRange($this->number, 1, 10);
190 for ($a = 1; $a <= $this->folderNumber; $a++) {
191 $options = [];
192 $options['value'] = $a;
193 $options['selected'] = ($this->number == $a ? ' selected="selected"' : '');
194 $assigns['options'][] = $options;
195 }
196 // Making the number of new-folder boxes needed:
197 for ($a = 0; $a < $this->number; $a++) {
198 $folder = [];
199 $folder['this'] = $a;
200 $folder['next'] = $a + 1;
201 $assigns['folders'][] = $folder;
202 }
203 // Making submit button for folder creation:
204 $assigns['returnUrl'] = $this->returnUrl;
205 }
206
207 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
208 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
209 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
210 $assigns['moduleUrlOnlineMedia'] = (string)$uriBuilder->buildUriFromRoute('online_media');
211 $assigns['cshFileNewMedia'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newMedia');
212 // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
213 $fileExtList = [];
214 $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
215 foreach ($onlineMediaFileExt as $fileExt) {
216 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
217 $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
218 }
219 }
220 $assigns['fileExtList'] = $fileExtList;
221
222 $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
223 $assigns['cshFileNewFile'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfile');
224 // Create a list of allowed file extensions with a text format "*.txt, *.css" etc.
225 $fileExtList = [];
226 $textFileExt = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], true);
227 foreach ($textFileExt as $fileExt) {
228 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
229 $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
230 }
231 }
232 $assigns['txtFileExtList'] = $fileExtList;
233 }
234
235 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
236 // CSH button
237 $helpButton = $buttonBar->makeHelpButton()
238 ->setFieldName('file_new')
239 ->setModuleName('xMOD_csh_corebe');
240 $buttonBar->addButton($helpButton);
241
242 // Back
243 if ($this->returnUrl) {
244 $backButton = $buttonBar->makeLinkButton()
245 ->setHref($this->returnUrl)
246 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
247 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
248 $buttonBar->addButton($backButton);
249 }
250
251 // Rendering of the output via fluid
252 $view = GeneralUtility::makeInstance(StandaloneView::class);
253 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
254 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
255 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
256 'EXT:backend/Resources/Private/Templates/File/CreateFolder.html'
257 ));
258 $view->assignMultiple($assigns);
259 $this->content = $view->render();
260 $this->moduleTemplate->setContent($this->content);
261 }
262
263 /**
264 * Processes the request, currently everything is handled and put together via "main()"
265 *
266 * @param ServerRequestInterface $request the current request
267 * @param ResponseInterface $response
268 * @return ResponseInterface the response with the content
269 */
270 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
271 {
272 $this->main();
273 $response->getBody()->write($this->moduleTemplate->renderContent());
274 return $response;
275 }
276
277 /**
278 * Returns LanguageService
279 *
280 * @return \TYPO3\CMS\Core\Localization\LanguageService
281 */
282 protected function getLanguageService()
283 {
284 return $GLOBALS['LANG'];
285 }
286 }