ffc74c2b9907aef618f936441a922ecdc428a33c
[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 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
135 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
136 $this->moduleTemplate->addJavaScriptCode(
137 'CreateFolderInlineJavaScript',
138 'var path = "' . $this->target . '";
139 var confirmTitle = '
140 . GeneralUtility::quoteJSvalue(
141 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:pleaseConfirm')
142 )
143 . ';
144 var confirmText = '
145 . GeneralUtility::quoteJSvalue(
146 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:mess.redraw')
147 )
148 . ';
149 function reload(a) {
150 var params = "&target="+encodeURIComponent(path)+"&number="+a+"&returnUrl=' . rawurlencode($this->returnUrl) . '";
151 var url = \'' . BackendUtility::getModuleUrl('file_newfolder') . '\';
152 if (!changed) {
153 window.location.href = url + params;
154 } else {
155 var modal = top.TYPO3.Modal.confirm(confirmTitle, confirmText);
156 modal.on(\'confirm.button.cancel\', function(e) {
157 top.TYPO3.Modal.currentModal.trigger(\'modal-dismiss\');
158 });
159 modal.on(\'confirm.button.ok\', function(e) {
160 top.TYPO3.Modal.currentModal.trigger(\'modal-dismiss\');
161 window.location.href = url + params;
162 });
163 }
164 }
165 function backToList() {
166 top.goToModule("file_FilelistList");
167 }
168 var changed = 0;'
169 );
170 }
171
172 /**
173 * Main function, rendering the main module content
174 */
175 public function main()
176 {
177 $lang = $this->getLanguageService();
178 $assigns = [];
179 $assigns['target'] = $this->target;
180 if ($this->folderObject->checkActionPermission('add')) {
181 $assigns['moduleUrlTceFile'] = BackendUtility::getModuleUrl('tce_file');
182 $assigns['cshFileNewFolder'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfolder');
183 // Making the selector box for the number of concurrent folder-creations
184 $this->number = MathUtility::forceIntegerInRange($this->number, 1, 10);
185 for ($a = 1; $a <= $this->folderNumber; $a++) {
186 $options = [];
187 $options['value'] = $a;
188 $options['selected'] = ($this->number == $a ? ' selected="selected"' : '');
189 $assigns['options'][] = $options;
190 }
191 // Making the number of new-folder boxes needed:
192 for ($a = 0; $a < $this->number; $a++) {
193 $folder = [];
194 $folder['this'] = $a;
195 $folder['next'] = $a + 1;
196 $assigns['folders'][] = $folder;
197 }
198 // Making submit button for folder creation:
199 $assigns['returnUrl'] = $this->returnUrl;
200 }
201
202 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
203 $assigns['moduleUrlOnlineMedia'] = BackendUtility::getModuleUrl('online_media');
204 $assigns['cshFileNewMedia'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newMedia');
205 // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
206 $fileExtList = [];
207 $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
208 foreach ($onlineMediaFileExt as $fileExt) {
209 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
210 $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
211 }
212 }
213 $assigns['fileExtList'] = $fileExtList;
214
215 $assigns['moduleUrlTceFile'] = BackendUtility::getModuleUrl('tce_file');
216 $assigns['cshFileNewFile'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfile');
217 // Create a list of allowed file extensions with a text format "*.txt, *.css" etc.
218 $fileExtList = [];
219 $textFileExt = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], true);
220 foreach ($textFileExt as $fileExt) {
221 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
222 $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
223 }
224 }
225 $assigns['txtFileExtList'] = $fileExtList;
226 }
227
228 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
229 // CSH button
230 $helpButton = $buttonBar->makeHelpButton()
231 ->setFieldName('file_new')
232 ->setModuleName('xMOD_csh_corebe');
233 $buttonBar->addButton($helpButton);
234
235 // Back
236 if ($this->returnUrl) {
237 $backButton = $buttonBar->makeLinkButton()
238 ->setHref($this->returnUrl)
239 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
240 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
241 $buttonBar->addButton($backButton);
242 }
243
244 // Rendering of the output via fluid
245 $view = GeneralUtility::makeInstance(StandaloneView::class);
246 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
247 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
248 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
249 'EXT:backend/Resources/Private/Templates/File/CreateFolder.html'
250 ));
251 $view->assignMultiple($assigns);
252 $this->content = $view->render();
253 $this->moduleTemplate->setContent($this->content);
254 }
255
256 /**
257 * Processes the request, currently everything is handled and put together via "main()"
258 *
259 * @param ServerRequestInterface $request the current request
260 * @param ResponseInterface $response
261 * @return ResponseInterface the response with the content
262 */
263 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
264 {
265 $this->main();
266 $response->getBody()->write($this->moduleTemplate->renderContent());
267 return $response;
268 }
269
270 /**
271 * Returns LanguageService
272 *
273 * @return \TYPO3\CMS\Core\Localization\LanguageService
274 */
275 protected function getLanguageService()
276 {
277 return $GLOBALS['LANG'];
278 }
279 }