RenameFileController.php 7.77 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

Nicole Cordes's avatar
Nicole Cordes committed
17
use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
Nicole Cordes's avatar
Nicole Cordes committed
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Nicole Cordes's avatar
Nicole Cordes committed
23

24
25
26
/**
 * Script Class for the rename-file form.
 */
27
class RenameFileController {
28
29
30
31

	/**
	 * Document template object
	 *
32
	 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
33
	 * @internal
34
35
36
37
	 */
	public $doc;

	/**
38
39
	 * Name of the filemount
	 *
40
	 * @var string
41
42
43
44
	 */
	public $title;

	/**
45
46
	 * Target path
	 *
47
	 * @var string
48
	 * @internal
49
50
51
52
53
54
	 */
	public $target;

	/**
	 * The file or folder object that should be renamed
	 *
55
	 * @var \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $fileOrFolderObject
56
57
58
59
	 */
	protected $fileOrFolderObject;

	/**
60
61
	 * Return URL of list module.
	 *
62
	 * @var string
63
64
65
66
	 */
	public $returnUrl;

	/**
67
68
	 * Accumulating content
	 *
69
	 * @var string
70
	 * @internal
71
72
73
74
	 */
	public $content;

	/**
75
76
77
78
79
80
81
82
83
	 * Constructor
	 */
	public function __construct() {
		$GLOBALS['SOBE'] = $this;
		$this->init();
	}

	/**
	 * Initialize
84
	 *
85
	 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
86
	 */
87
	protected function init() {
88
		// Initialize GPvars:
Nicole Cordes's avatar
Nicole Cordes committed
89
90
		$this->target = GeneralUtility::_GP('target');
		$this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
91
92
93
94
95
		// Cleaning and checking target
		if ($this->target) {
			$this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
		}
		if (!$this->fileOrFolderObject) {
96
97
			$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);
98
			throw new \RuntimeException($title . ': ' . $message, 1294586844);
99
		}
100
		if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
101
			throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
102
103
		}

104
		// If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
105
		// so the redirect will NOT end in an error message
106
107
108
109
		// this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
		// rename the folder
		if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
			$parsedUrl = parse_url($this->returnUrl);
Nicole Cordes's avatar
Nicole Cordes committed
110
			$queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
111
112
113
114
115
			if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
				$this->returnUrl = str_replace(urlencode($queryParts['id']), urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()), $this->returnUrl);
			}
		}
		// Setting icon and title
116
117
		/** @var IconFactory $iconFactory */
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
118
		$icon = $iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
119
		$this->title = $icon . htmlspecialchars($this->fileOrFolderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileOrFolderObject->getIdentifier());
120
		// Setting template object
121
		$this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
122
		$this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_rename.html');
123
		$this->doc->JScode = $this->doc->wrapScriptTags('
124
125
			function backToList() {
				top.goToModule("file_FilelistList");
126
127
128
129
130
131
132
133
134
135
136
			}
		');
	}

	/**
	 * Main function, rendering the content of the rename form
	 *
	 * @return void
	 */
	public function main() {
		// Make page header:
137
138
		$this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
		$pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
139
140
141
142
143
		if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
			$fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
		} else {
			$fileIdentifier = $this->fileOrFolderObject->getUid();
		}
144
		$pageContent .= '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform" role="form">';
145
		// Making the formfields for renaming:
146
		$pageContent .= '
147

148
			<div class="form-group">
149
				<input class="form-control" type="text" name="file[rename][0][target]" value="' . htmlspecialchars($this->fileOrFolderObject->getName()) . '" ' . $this->getDocumentTemplate()->formWidth(40) . ' />
150
				<input type="hidden" name="file[rename][0][data]" value="' . htmlspecialchars($fileIdentifier) . '" />
151
152
153
			</div>
		';
		// Making submit button:
154
155
		$pageContent .= '
			<div class="form-group">
156
157
				<input class="btn btn-primary" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.submit', TRUE) . '" />
				<input class="btn btn-danger" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', TRUE) . '" onclick="backToList(); return false;" />
158
				<input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
159
160
			</div>
		';
161
		$pageContent .= '</form>';
162
163
164
		$docHeaderButtons = array(
			'back' => ''
		);
Benni Mack's avatar
Benni Mack committed
165
		$docHeaderButtons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_rename');
166
167
		// Back
		if ($this->returnUrl) {
168
			$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
169
			$docHeaderButtons['back'] = '<a href="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::linkThisUrl($this->returnUrl)) . '" class="typo3-goBack" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">' . $iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL)->render() . '</a>';
170
		}
171
172
173
		// Add the HTML as a section:
		$markerArray = array(
			'CSH' => $docHeaderButtons['csh'],
174
			'FUNC_MENU' => '',
175
176
177
178
179
180
181
182
			'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);
	}

183
184
185
	/**
	 * Processes the request, currently everything is handled and put together via "main()"
	 *
186
187
188
	 * @param ServerRequestInterface $request the current request
	 * @param ResponseInterface $response
	 * @return ResponseInterface the response with the content
189
	 */
190
	public function mainAction(ServerRequestInterface $request, ResponseInterface $response) {
191
		$this->main();
192

193
194
195
196
		$response->getBody()->write($this->content);
		return $response;
	}

197
198
199
200
	/**
	 * Outputting the accumulated content to screen
	 *
	 * @return void
201
	 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the mainAction() method instead
202
203
	 */
	public function printContent() {
204
		GeneralUtility::logDeprecatedFunction();
205
206
207
		echo $this->content;
	}

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	/**
	 * Returns LanguageService
	 *
	 * @return \TYPO3\CMS\Lang\LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

	/**
	 * Returns an instance of DocumentTemplate
	 *
	 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate
	 */
	protected function getDocumentTemplate() {
		return $GLOBALS['TBE_TEMPLATE'];
	}

226
}