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