[TASK] Fix CGL errors
[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\Module\AbstractModule;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
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 extends AbstractModule
28 {
29 /**
30 * Name of the filemount
31 *
32 * @var string
33 */
34 public $title;
35
36 /**
37 * Target path
38 *
39 * @var string
40 * @internal
41 */
42 public $target;
43
44 /**
45 * The file or folder object that should be renamed
46 *
47 * @var \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $fileOrFolderObject
48 */
49 protected $fileOrFolderObject;
50
51 /**
52 * Return URL of list module.
53 *
54 * @var string
55 */
56 public $returnUrl;
57
58 /**
59 * Accumulating content
60 *
61 * @var string
62 * @internal
63 */
64 public $content;
65
66 /**
67 * Constructor
68 */
69 public function __construct()
70 {
71 parent::__construct();
72 $GLOBALS['SOBE'] = $this;
73 $this->init();
74 }
75
76 /**
77 * Initialize
78 *
79 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
80 */
81 protected function init()
82 {
83 // Initialize GPvars:
84 $this->target = GeneralUtility::_GP('target');
85 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
86 // Cleaning and checking target
87 if ($this->target) {
88 $this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
89 }
90 if (!$this->fileOrFolderObject) {
91 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', true);
92 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', true);
93 throw new \RuntimeException($title . ': ' . $message, 1294586844);
94 }
95 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
96 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
97 }
98
99 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
100 // so the redirect will NOT end in an error message
101 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
102 // rename the folder
103 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
104 $parsedUrl = parse_url($this->returnUrl);
105 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
106 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
107 $this->returnUrl = str_replace(urlencode($queryParts['id']),
108 urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
109 $this->returnUrl);
110 }
111 }
112
113 // building pathInfo for metaInformation
114 $pathInfo = [
115 'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
116 ];
117 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
118
119 // Setting up the context sensitive menu
120 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
121
122 // Add javaScript
123 $this->moduleTemplate->addJavaScriptCode(
124 'RenameFileInlineJavaScript',
125 'function backToList() {top.goToModule("file_FilelistList");}'
126 );
127 }
128
129 /**
130 * Main function, rendering the content of the rename form
131 *
132 * @return void
133 */
134 public function main()
135 {
136 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
137 $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
138 } else {
139 $fileIdentifier = $this->fileOrFolderObject->getUid();
140 }
141 $pageContent = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform" role="form">';
142 // Making the formfields for renaming:
143 $pageContent .= '
144
145 <div class="form-group">
146 <input class="form-control" type="text" name="file[rename][0][target]" value="' . htmlspecialchars($this->fileOrFolderObject->getName()) . '" ' . $this->getDocumentTemplate()->formWidth(40) . ' />
147 <input type="hidden" name="file[rename][0][data]" value="' . htmlspecialchars($fileIdentifier) . '" />
148 </div>
149 ';
150 // Making submit button:
151 $pageContent .= '
152 <div class="form-group">
153 <input class="btn btn-primary" type="submit" value="' .
154 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.submit', true) . '" />
155 <input class="btn btn-danger" type="submit" value="' .
156 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', true) .
157 '" onclick="backToList(); return false;" />
158 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
159 </div>
160 ';
161 $pageContent .= '</form>';
162
163 // Create buttons
164 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
165
166 // csh button
167 $cshButton = $buttonBar->makeHelpButton()
168 ->setModuleName('xMOD_csh_corebe')
169 ->setFieldName('file_rename');
170 $buttonBar->addButton($cshButton);
171
172 // back button
173 if ($this->returnUrl) {
174 $backButton = $buttonBar->makeLinkButton()
175 ->sethref(GeneralUtility::linkThisUrl($this->returnUrl))
176 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', true))
177 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
178 $buttonBar->addButton($backButton);
179 }
180
181 // set header
182 $this->content = '<h1>' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle') . '</h1>';
183
184 // add section
185 $this->content .= '<div>' . $pageContent . '</div>';
186 $this->moduleTemplate->setContent($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 $response->getBody()->write($this->moduleTemplate->renderContent());
200 return $response;
201 }
202
203 /**
204 * Returns LanguageService
205 *
206 * @return \TYPO3\CMS\Lang\LanguageService
207 */
208 protected function getLanguageService()
209 {
210 return $GLOBALS['LANG'];
211 }
212
213 /**
214 * Returns an instance of DocumentTemplate
215 *
216 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate
217 */
218 protected function getDocumentTemplate()
219 {
220 return $GLOBALS['TBE_TEMPLATE'];
221 }
222 }