887a1059985a76ae7022ffb45c253386e999b676
[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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Script Class for the create-new script; Displays a form for creating up to 10 folders or one new text file
22 */
23 class CreateFolderController {
24
25 /**
26 * @var int
27 */
28 public $folderNumber = 10;
29
30 /**
31 * document template object
32 *
33 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
34 */
35 public $doc;
36
37 /**
38 * Name of the filemount
39 *
40 * @var string
41 */
42 public $title;
43
44 /**
45 * @var int
46 */
47 public $number;
48
49 /**
50 * Set with the target path inputted in &target
51 *
52 * @var string
53 */
54 public $target;
55
56 /**
57 * The folder object which is the target directory
58 *
59 * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
60 */
61 protected $folderObject;
62
63 /**
64 * Return URL of list module.
65 *
66 * @var string
67 */
68 public $returnUrl;
69
70 /**
71 * Accumulating content
72 *
73 * @var string
74 */
75 public $content;
76
77 /**
78 * Constructor
79 */
80 public function __construct() {
81 $GLOBALS['SOBE'] = $this;
82 $GLOBALS['BACK_PATH'] = '';
83
84 $this->init();
85 }
86
87 /**
88 * Initialize
89 *
90 * @return void
91 */
92 protected function init() {
93 // Initialize GPvars:
94 $this->number = GeneralUtility::_GP('number');
95 $this->target = ($combinedIdentifier = GeneralUtility::_GP('target'));
96 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
97 // create the folder object
98 if ($combinedIdentifier) {
99 $this->folderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
100 }
101 // Cleaning and checking target directory
102 if (!$this->folderObject) {
103 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
104 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
105 throw new \RuntimeException($title . ': ' . $message, 1294586845);
106 }
107 if ($this->folderObject->getStorage()->getUid() === 0) {
108 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException('You are not allowed to access folders outside your storages', 1375889838);
109 }
110
111 // Setting the title and the icon
112 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-filetree-root');
113 $this->title = $icon . htmlspecialchars($this->folderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->folderObject->getIdentifier());
114 // Setting template object
115 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
116 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_newfolder.html');
117 $this->doc->backPath = $GLOBALS['BACK_PATH'];
118 $this->doc->JScode = $this->doc->wrapScriptTags('
119 var path = "' . $this->target . '";
120
121 function reload(a) { //
122 if (!changed || (changed && confirm(' . GeneralUtility::quoteJSvalue($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:mess.redraw')) . '))) {
123 var params = "&target="+encodeURIComponent(path)+"&number="+a+"&returnUrl=' . rawurlencode($this->returnUrl) . '";
124 window.location.href = ' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('file_newfolder')) . '+params;
125 }
126 }
127 function backToList() { //
128 top.goToModule("file_list");
129 }
130
131 var changed = 0;
132 ');
133 }
134
135 /**
136 * Main function, rendering the main module content
137 *
138 * @return void
139 */
140 public function main() {
141 $lang = $this->getLanguageService();
142 // Start content compilation
143 $this->content .= $this->doc->startPage($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.pagetitle'));
144 // Make page header:
145 $pageContent = $this->doc->header($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.pagetitle'));
146
147 if ($this->folderObject->checkActionPermission('add')) {
148 $code = '<form role="form" action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform">';
149 // Making the selector box for the number of concurrent folder-creations
150 $this->number = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->number, 1, 10);
151 $code .= '
152 <div class="form-group">
153 <div class="form-section">
154 <div class="form-group">
155 <label for="number-of-new-folders">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.number_of_folders') . ' ' . BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfolder') . '</label>
156 <div class="form-control-wrap">
157 <div class="input-group">
158 <select class="form-control form-control-adapt" name="number" id="number-of-new-folders" onchange="reload(this.options[this.selectedIndex].value);">';
159 for ($a = 1; $a <= $this->folderNumber; $a++) {
160 $code .= '<option value="' . $a . '"' . ($this->number == $a ? ' selected="selected"' : '') . '>' . $a . '</option>';
161 }
162 $code .= '
163 </select>
164 </div>
165 </div>
166 </div>
167 </div>
168 ';
169 // Making the number of new-folder boxes needed:
170 for ($a = 0; $a < $this->number; $a++) {
171 $code .= '
172 <div class="form-section">
173 <div class="form-group">
174 <label for="folder_new_' . $a . '">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.label_newfolder') . ' ' . ($a + 1) . ':</label>
175 <div class="form-control-wrap">
176 <input type="text" class="form-control" id="folder_new_' . $a . '" name="file[newfolder][' . $a . '][data]" onchange="changed=true;" />
177 <input type="hidden" name="file[newfolder][' . $a . '][target]" value="' . htmlspecialchars($this->target) . '" />
178 </div>
179 </div>
180 </div>';
181 }
182 // Making submit button for folder creation:
183 $code .= '
184 </div><div class="form-group">
185 <input class="btn btn-default" type="submit" value="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.submit', TRUE) . '" />
186 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
187 ' . \TYPO3\CMS\Backend\Form\FormEngine::getHiddenTokenField('tceAction') . '
188 </div>
189 ';
190 // Switching form tags:
191 $pageContent .= $this->doc->section($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfolders'), $code);
192 $pageContent .= $this->doc->sectionEnd() . '</form>';
193 }
194
195 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
196 $pageContent .= '<form action="' . BackendUtility::getModuleUrl('tce_file') . '" method="post" name="editform2">';
197 // Create a list of allowed file extensions with the nice format "*.jpg, *.gif" etc.
198 $fileExtList = array();
199 $textFileExt = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], TRUE);
200 foreach ($textFileExt as $fileExt) {
201 if (!preg_match(('/' . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'] . '/i'), ('.' . $fileExt))) {
202 $fileExtList[] = '<span class="label label-success">' . strtoupper(htmlspecialchars($fileExt)) . '</span>';
203 }
204 }
205 // Add form fields for creation of a new, blank text file:
206 $code = '
207 <div class="form-group">
208 <div class="form-section">
209 <div class="form-group">
210 <label for="newfile">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.label_newfile') . ' ' . BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfile') . '</label>
211 <div class="form-control-wrap">
212 <input class="form-control" type="text" id="newfile" name="file[newfile][0][data]" onchange="changed=true;" />
213 <input type="hidden" name="file[newfile][0][target]" value="' . htmlspecialchars($this->target) . '" />
214 </div>
215 <div class="help-block">
216 ' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:cm.allowedFileExtensions') . '<br>
217 ' . implode(' ', $fileExtList) . '
218 </div>
219 </div>
220 </div>
221 </div>
222 ';
223 // Submit button for creation of a new file:
224 $code .= '
225 <div class="form-group">
226 <input class="btn btn-default" type="submit" value="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfile_submit', TRUE) . '" />
227 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
228 ' . \TYPO3\CMS\Backend\Form\FormEngine::getHiddenTokenField('tceAction') . '
229 </div>
230 ';
231 $pageContent .= $this->doc->section($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfile'), $code);
232 $pageContent .= $this->doc->sectionEnd();
233 $pageContent .= '</form>';
234 }
235
236 $docHeaderButtons = array(
237 'back' => ''
238 );
239 // Back
240 if ($this->returnUrl) {
241 $docHeaderButtons['back'] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisUrl($this->returnUrl)) . '" class="typo3-goBack" title="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-view-go-back') . '</a>';
242 }
243 // Add the HTML as a section:
244 $markerArray = array(
245 'CSH' => $docHeaderButtons['csh'],
246 'FUNC_MENU' => '',
247 'CONTENT' => $pageContent,
248 'PATH' => $this->title
249 );
250 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
251 $this->content .= $this->doc->endPage();
252 $this->content = $this->doc->insertStylesAndJS($this->content);
253 }
254
255 /**
256 * Outputting the accumulated content to screen
257 *
258 * @return void
259 */
260 public function printContent() {
261 echo $this->content;
262 }
263
264 /**
265 * Returns LanguageService
266 *
267 * @return \TYPO3\CMS\Lang\LanguageService
268 */
269 protected function getLanguageService() {
270 return $GLOBALS['LANG'];
271 }
272
273 }