CreateFolderController.php 12.8 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Backend\Controller\File;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
use TYPO3\CMS\Backend\Template\DocumentTemplate;
Nicole Cordes's avatar
Nicole Cordes committed
18
use TYPO3\CMS\Backend\Utility\BackendUtility;
19
20
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
21
22
23
use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
use TYPO3\CMS\Core\Resource\ResourceFactory;
Nicole Cordes's avatar
Nicole Cordes committed
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
27
use TYPO3\CMS\Core\Utility\MathUtility;
Nicole Cordes's avatar
Nicole Cordes committed
28

29
30
31
/**
 * Script Class for the create-new script; Displays a form for creating up to 10 folders or one new text file
 */
32
class CreateFolderController {
33

34
35
36
	/**
	 * @var int
	 */
37
38
39
40
41
	public $folderNumber = 10;

	/**
	 * document template object
	 *
42
	 * @var DocumentTemplate
43
44
45
	 */
	public $doc;

46
47
48
49
50
	/**
	 * Name of the filemount
	 *
	 * @var string
	 */
51
52
	public $title;

53
54
55
	/**
	 * @var int
	 */
56
57
	public $number;

58
59
60
61
62
	/**
	 * Set with the target path inputted in &target
	 *
	 * @var string
	 */
63
64
65
66
67
68
69
70
71
	public $target;

	/**
	 * The folder object which is  the target directory
	 *
	 * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
	 */
	protected $folderObject;

72
73
74
75
76
	/**
	 * Return URL of list module.
	 *
	 * @var string
	 */
77
78
	public $returnUrl;

79
80
81
82
83
	/**
	 * Accumulating content
	 *
	 * @var string
	 */
84
85
86
	public $content;

	/**
87
88
89
90
91
92
93
94
95
	 * Constructor
	 */
	public function __construct() {
		$GLOBALS['SOBE'] = $this;
		$this->init();
	}

	/**
	 * Initialize
96
	 *
97
	 * @throws InsufficientFolderAccessPermissionsException
98
99
	 * @return void
	 */
100
	protected function init() {
101
		// Initialize GPvars:
Nicole Cordes's avatar
Nicole Cordes committed
102
103
104
		$this->number = GeneralUtility::_GP('number');
		$this->target = ($combinedIdentifier = GeneralUtility::_GP('target'));
		$this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
105
106
		// create the folder object
		if ($combinedIdentifier) {
107
			$this->folderObject = ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
108
109
110
		}
		// Cleaning and checking target directory
		if (!$this->folderObject) {
111
112
			$title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
			$message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
113
			throw new \RuntimeException($title . ': ' . $message, 1294586845);
114
		}
115
		if ($this->folderObject->getStorage()->getUid() === 0) {
116
			throw new InsufficientFolderAccessPermissionsException('You are not allowed to access folders outside your storages', 1375889838);
117
118
		}

119
		// Setting the title and the icon
120
121
		/** @var IconFactory $iconFactory */
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
122
		$icon = $iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
123
		$this->title = $icon . htmlspecialchars($this->folderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->folderObject->getIdentifier());
124
		// Setting template object
125
		$this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
126
		$this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_newfolder.html');
127
128
		$this->doc->JScode = $this->doc->wrapScriptTags('
			var path = "' . $this->target . '";
129

130
			function reload(a) {
131
				if (!changed || (changed && confirm(' . GeneralUtility::quoteJSvalue($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:mess.redraw')) . '))) {
132
					var params = "&target="+encodeURIComponent(path)+"&number="+a+"&returnUrl=' . rawurlencode($this->returnUrl) . '";
133
					window.location.href = ' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('file_newfolder')) . '+params;
134
135
				}
			}
136
137
			function backToList() {
				top.goToModule("file_FilelistList");
138
139
140
141
142
143
144
145
146
147
148
149
			}

			var changed = 0;
		');
	}

	/**
	 * Main function, rendering the main module content
	 *
	 * @return void
	 */
	public function main() {
150
		$lang = $this->getLanguageService();
151
		// Start content compilation
152
		$this->content .= $this->doc->startPage($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.pagetitle'));
153
		// Make page header:
154
		$pageContent = $this->doc->header($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.pagetitle'));
155
		if ($this->folderObject->checkActionPermission('add')) {
156
			$code = '<form role="form" action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform">';
157
			// Making the selector box for the number of concurrent folder-creations
158
			$this->number = MathUtility::forceIntegerInRange($this->number, 1, 10);
159
			$code .= '
160
				<div class="form-group">
161
162
					<div class="form-section">
						<div class="form-group">
163
							<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>
164
165
166
167
168
169
170
171
172
173
174
175
							<div class="form-control-wrap">
								<div class="input-group">
									<select class="form-control form-control-adapt" name="number" id="number-of-new-folders" onchange="reload(this.options[this.selectedIndex].value);">';
										for ($a = 1; $a <= $this->folderNumber; $a++) {
											$code .= '<option value="' . $a . '"' . ($this->number == $a ? ' selected="selected"' : '') . '>' . $a . '</option>';
										}
										$code .= '
									</select>
								</div>
							</div>
						</div>
					</div>
176
177
178
179
				';
			// Making the number of new-folder boxes needed:
			for ($a = 0; $a < $this->number; $a++) {
				$code .= '
180
181
					<div class="form-section">
						<div class="form-group">
182
							<label for="folder_new_' . $a . '">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.label_newfolder') . ' ' . ($a + 1) . ':</label>
183
184
185
186
187
188
							<div class="form-control-wrap">
								<input type="text" class="form-control" id="folder_new_' . $a . '" name="file[newfolder][' . $a . '][data]" onchange="changed=true;" />
								<input type="hidden" name="file[newfolder][' . $a . '][target]" value="' . htmlspecialchars($this->target) . '" />
							</div>
						</div>
					</div>';
189
190
			}
			// Making submit button for folder creation:
191
			$code .= '
192
				</div><div class="form-group">
193
					<input class="btn btn-default" type="submit" value="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.submit', TRUE) . '" />
194
195
					<input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
				</div>
196
				';
197
			// Switching form tags:
198
			$pageContent .= $this->doc->section($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfolders'), $code);
199
			$pageContent .= $this->doc->sectionEnd() . '</form>';
200
		}
201
202

		if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

			$pageContent .= '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('online_media')) . '" method="post" name="editform2">';
			// Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
			$fileExtList = array();
			$onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
			foreach ($onlineMediaFileExt as $fileExt) {
				if (GeneralUtility::verifyFilenameAgainstDenyPattern($fileExt)) {
					$fileExtList[] = '<span class="label label-success">' . strtoupper(htmlspecialchars($fileExt)) . '</span>';
				}
			}
			// Add form fields for adding media files:
			$code = '
				<div class="form-group">
					<div class="form-section">
						<div class="form-group">
							<label for="newMedia">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media.label', TRUE) . ' ' . BackendUtility::cshItem('xMOD_csh_corebe', 'file_newMedia') . '</label>
							<div class="form-control-wrap">
								<input class="form-control" type="text" id="newMedia" name="file[newMedia][0][url]"
									placeholder="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media.placeholder', TRUE) . '" />
								<input type="hidden" name="file[newMedia][0][target]" value="' . htmlspecialchars($this->target) . '" />
							</div>
							<div class="help-block">
								' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media.allowedProviders', TRUE) . '<br>
								' . implode(' ', $fileExtList) . '
							</div>
						</div>
					</div>
				</div>
				';
			// Submit button for creation of a new media:
			$code .= '
				<div class="form-group">
					<input class="btn btn-default" type="submit" value="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media.submit', TRUE) . '" />
					<input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
				</div>
				';
			$pageContent .= $this->doc->section($lang->sL('LLL:EXT:lang/locallang_core.xlf:online_media.new_media', TRUE), $code);
			$pageContent .= $this->doc->sectionEnd();
			$pageContent .= '</form>';

			$pageContent .= '<form action="' . BackendUtility::getModuleUrl('tce_file') . '" method="post" name="editform3">';
244
245
			// Create a list of allowed file extensions with the nice format "*.jpg, *.gif" etc.
			$fileExtList = array();
246
247
			$textFileExt = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], TRUE);
			foreach ($textFileExt as $fileExt) {
248
				if (GeneralUtility::verifyFilenameAgainstDenyPattern($fileExt)) {
249
					$fileExtList[] = '<span class="label label-success">' . strtoupper(htmlspecialchars($fileExt)) . '</span>';
250
				}
251
			}
252
253
			// Add form fields for creation of a new, blank text file:
			$code = '
254
				<div class="form-group">
255
256
					<div class="form-section">
						<div class="form-group">
257
							<label for="newfile">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.label_newfile', TRUE) . ' ' . BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfile') . '</label>
258
259
260
261
262
							<div class="form-control-wrap">
								<input class="form-control" type="text" id="newfile" name="file[newfile][0][data]" onchange="changed=true;" />
								<input type="hidden" name="file[newfile][0][target]" value="' . htmlspecialchars($this->target) . '" />
							</div>
							<div class="help-block">
263
								' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:cm.allowedFileExtensions', TRUE) . '<br>
264
265
266
267
								' . implode(' ', $fileExtList) . '
							</div>
						</div>
					</div>
268
269
270
271
				</div>
				';
			// Submit button for creation of a new file:
			$code .= '
272
				<div class="form-group">
273
					<input class="btn btn-default" type="submit" value="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfile_submit', TRUE) . '" />
274
275
276
					<input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
				</div>
				';
277
			$pageContent .= $this->doc->section($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfile', TRUE), $code);
278
279
			$pageContent .= $this->doc->sectionEnd();
			$pageContent .= '</form>';
280
		}
281

282
283
284
		$docHeaderButtons = array(
			'back' => ''
		);
285
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
286
287
		// Back
		if ($this->returnUrl) {
288
			$docHeaderButtons['back'] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisUrl($this->returnUrl)) . '" class="typo3-goBack" title="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">' . $iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL)->render() . '</a>';
289
		}
290
291
292
		// Add the HTML as a section:
		$markerArray = array(
			'CSH' => $docHeaderButtons['csh'],
293
			'FUNC_MENU' => '',
294
295
296
297
298
299
300
301
			'CONTENT' => $pageContent,
			'PATH' => $this->title
		);
		$this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
		$this->content .= $this->doc->endPage();
		$this->content = $this->doc->insertStylesAndJS($this->content);
	}

302
303
304
	/**
	 * Processes the request, currently everything is handled and put together via "main()"
	 *
305
306
307
	 * @param ServerRequestInterface $request the current request
	 * @param ResponseInterface $response
	 * @return ResponseInterface the response with the content
308
	 */
309
	public function mainAction(ServerRequestInterface $request, ResponseInterface $response) {
310
		$this->main();
311

312
313
314
315
		$response->getBody()->write($this->content);
		return $response;
	}

316
317
318
319
	/**
	 * Outputting the accumulated content to screen
	 *
	 * @return void
320
	 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the mainAction() method instead
321
322
	 */
	public function printContent() {
323
		GeneralUtility::logDeprecatedFunction();
324
325
326
		echo $this->content;
	}

327
328
329
330
331
332
333
334
335
	/**
	 * Returns LanguageService
	 *
	 * @return \TYPO3\CMS\Lang\LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

336
}