[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / ReplaceFileController.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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Template\ModuleTemplate;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Localization\LanguageService;
22 use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
23 use TYPO3\CMS\Core\Resource\Folder;
24 use TYPO3\CMS\Core\Resource\ResourceFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Fluid\View\StandaloneView;
27
28 /**
29 * Script Class for the rename-file form
30 */
31 class ReplaceFileController
32 {
33 /**
34 * Document template object
35 *
36 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
37 */
38 public $doc;
39
40 /**
41 * Name of the filemount
42 *
43 * @var string
44 */
45 public $title;
46
47 /**
48 * sys_file uid
49 *
50 * @var int
51 */
52 public $uid;
53
54 /**
55 * The file or folder object that should be renamed
56 *
57 * @var \TYPO3\CMS\Core\Resource\ResourceInterface $fileOrFolderObject
58 */
59 protected $fileOrFolderObject;
60
61 /**
62 * Return URL of list module.
63 *
64 * @var string
65 */
66 public $returnUrl;
67
68 /**
69 * Accumulating content
70 *
71 * @var string
72 */
73 public $content;
74
75 /**
76 * ModuleTemplate object
77 *
78 * @var ModuleTemplate
79 */
80 protected $moduleTemplate;
81
82 /**
83 * Constructor
84 */
85 public function __construct()
86 {
87 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
88 $GLOBALS['SOBE'] = $this;
89 $this->init();
90 }
91
92 /**
93 * Init
94 *
95 * @throws \RuntimeException
96 * @throws InsufficientFileAccessPermissionsException
97 */
98 protected function init()
99 {
100 // Initialize GPvars:
101 $this->uid = (int)GeneralUtility::_GP('uid');
102 $lang = $this->getLanguageService();
103
104 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
105 // Cleaning and checking uid
106 if ($this->uid > 0) {
107 $this->fileOrFolderObject = ResourceFactory::getInstance()
108 ->retrieveFileOrFolderObject('file:' . $this->uid);
109 }
110 if (!$this->fileOrFolderObject) {
111 $title = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
112 $message = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
113 throw new \RuntimeException($title . ': ' . $message, 1436895930);
114 }
115 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
116 throw new InsufficientFileAccessPermissionsException(
117 'You are not allowed to access files outside your storages',
118 1436895931
119 );
120 }
121
122 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
123 // so the redirect will NOT end in an error message
124 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
125 // rename the folder
126 if ($this->fileOrFolderObject instanceof Folder) {
127 $parsedUrl = parse_url($this->returnUrl);
128 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
129 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
130 $this->returnUrl = str_replace(
131 urlencode($queryParts['id']),
132 urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
133 $this->returnUrl
134 );
135 }
136 }
137
138 $pathInfo = [
139 'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
140 ];
141 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
142 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
143 $this->moduleTemplate->addJavaScriptCode(
144 'ReplaceFileOnlineJavaScript',
145 'function backToList() {top.goToModule("file_FilelistList");}'
146 );
147 }
148
149 /**
150 * Main function, rendering the content of the rename form
151 */
152 public function main()
153 {
154 // Assign variables used by the fluid template
155 $assigns = [];
156 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
157 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
158 $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
159 $assigns['uid'] = $this->uid;
160 $assigns['returnUrl'] = $this->returnUrl;
161
162 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
163 // csh button
164 $cshButton = $buttonBar->makeHelpButton()
165 ->setModuleName('xMOD_csh_corebe')
166 ->setFieldName('file_rename');
167 $buttonBar->addButton($cshButton);
168
169 // Back button
170 if ($this->returnUrl) {
171 $returnButton = $buttonBar->makeLinkButton()
172 ->setHref($this->returnUrl)
173 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
174 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
175 $buttonBar->addButton($returnButton);
176 }
177
178 // Rendering of the output via fluid
179 $view = GeneralUtility::makeInstance(StandaloneView::class);
180 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
181 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
182 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
183 'EXT:backend/Resources/Private/Templates/File/ReplaceFile.html'
184 ));
185 $view->assignMultiple($assigns);
186 $this->content = $view->render();
187
188 $this->moduleTemplate->setContent($this->content);
189 }
190
191 /**
192 * Processes the request, currently everything is handled and put together via "main()"
193 *
194 * @param ServerRequestInterface $request the current request
195 * @param ResponseInterface $response
196 * @return ResponseInterface the response with the content
197 */
198 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
199 {
200 $this->main();
201
202 $response->getBody()->write($this->moduleTemplate->renderContent());
203 return $response;
204 }
205
206 /**
207 * @return LanguageService
208 */
209 protected function getLanguageService()
210 {
211 return $GLOBALS['LANG'];
212 }
213 }