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