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