19c6a90f21453e27c0ee35def1d71145762f96e2
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / RenameFileController.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\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use TYPO3\CMS\Core\Http\Response;
24
25 /**
26 * Script Class for the rename-file form.
27 */
28 class RenameFileController implements \TYPO3\CMS\Core\Http\ControllerInterface {
29
30 /**
31 * Document template object
32 *
33 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
34 * @internal
35 */
36 public $doc;
37
38 /**
39 * Name of the filemount
40 *
41 * @var string
42 */
43 public $title;
44
45 /**
46 * Target path
47 *
48 * @var string
49 * @internal
50 */
51 public $target;
52
53 /**
54 * The file or folder object that should be renamed
55 *
56 * @var \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $fileOrFolderObject
57 */
58 protected $fileOrFolderObject;
59
60 /**
61 * Return URL of list module.
62 *
63 * @var string
64 */
65 public $returnUrl;
66
67 /**
68 * Accumulating content
69 *
70 * @var string
71 * @internal
72 */
73 public $content;
74
75 /**
76 * Constructor
77 */
78 public function __construct() {
79 $GLOBALS['SOBE'] = $this;
80 $this->init();
81 }
82
83 /**
84 * Initialize
85 *
86 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
87 */
88 protected function init() {
89 // Initialize GPvars:
90 $this->target = GeneralUtility::_GP('target');
91 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
92 // Cleaning and checking target
93 if ($this->target) {
94 $this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
95 }
96 if (!$this->fileOrFolderObject) {
97 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
98 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
99 throw new \RuntimeException($title . ': ' . $message, 1294586844);
100 }
101 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
102 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
103 }
104
105 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
106 // so the redirect will NOT end in an error message
107 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
108 // rename the folder
109 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
110 $parsedUrl = parse_url($this->returnUrl);
111 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
112 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
113 $this->returnUrl = str_replace(urlencode($queryParts['id']), urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()), $this->returnUrl);
114 }
115 }
116 // Setting icon and title
117 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-filetree-root');
118 $this->title = $icon . htmlspecialchars($this->fileOrFolderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileOrFolderObject->getIdentifier());
119 // Setting template object
120 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
121 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_rename.html');
122 $this->doc->JScode = $this->doc->wrapScriptTags('
123 function backToList() {
124 top.goToModule("file_FilelistList");
125 }
126 ');
127 }
128
129 /**
130 * Main function, rendering the content of the rename form
131 *
132 * @return void
133 */
134 public function main() {
135 // Make page header:
136 $this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
137 $pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
138 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
139 $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
140 } else {
141 $fileIdentifier = $this->fileOrFolderObject->getUid();
142 }
143 $pageContent .= '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform" role="form">';
144 // Making the formfields for renaming:
145 $pageContent .= '
146
147 <div class="form-group">
148 <input class="form-control" type="text" name="file[rename][0][target]" value="' . htmlspecialchars($this->fileOrFolderObject->getName()) . '" ' . $this->getDocumentTemplate()->formWidth(40) . ' />
149 <input type="hidden" name="file[rename][0][data]" value="' . htmlspecialchars($fileIdentifier) . '" />
150 </div>
151 ';
152 // Making submit button:
153 $pageContent .= '
154 <div class="form-group">
155 <input class="btn btn-primary" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.submit', TRUE) . '" />
156 <input class="btn btn-danger" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', TRUE) . '" onclick="backToList(); return false;" />
157 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
158 </div>
159 ';
160 $pageContent .= '</form>';
161 $docHeaderButtons = array(
162 'back' => ''
163 );
164 $docHeaderButtons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_rename');
165 // Back
166 if ($this->returnUrl) {
167 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
168 $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) . '</a>';
169 }
170 // Add the HTML as a section:
171 $markerArray = array(
172 'CSH' => $docHeaderButtons['csh'],
173 'FUNC_MENU' => '',
174 'CONTENT' => $pageContent,
175 'PATH' => $this->title
176 );
177 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
178 $this->content .= $this->doc->endPage();
179 $this->content = $this->doc->insertStylesAndJS($this->content);
180 }
181
182 /**
183 * Processes the request, currently everything is handled and put together via "main()"
184 *
185 * @param ServerRequestInterface $request The request object
186 * @return ResponseInterface $response The response, created by the controller
187 */
188 public function processRequest(ServerRequestInterface $request) {
189 $this->main();
190 /** @var Response $response */
191 $response = GeneralUtility::makeInstance(Response::class);
192 $response->getBody()->write($this->content);
193 return $response;
194 }
195
196 /**
197 * Outputting the accumulated content to screen
198 *
199 * @return void
200 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the processRequest() method instead
201 */
202 public function printContent() {
203 GeneralUtility::logDeprecatedFunction();
204 echo $this->content;
205 }
206
207 /**
208 * Returns LanguageService
209 *
210 * @return \TYPO3\CMS\Lang\LanguageService
211 */
212 protected function getLanguageService() {
213 return $GLOBALS['LANG'];
214 }
215
216 /**
217 * Returns an instance of DocumentTemplate
218 *
219 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate
220 */
221 protected function getDocumentTemplate() {
222 return $GLOBALS['TBE_TEMPLATE'];
223 }
224
225 }