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