[CLEANUP] Initialize variables and fix invalid function calls
[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\Module\AbstractModule;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Imaging\Icon;
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 use TYPO3\CMS\Lang\LanguageService;
28
29 /**
30 * Script Class for the rename-file form
31 */
32 class ReplaceFileController extends AbstractModule
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 * Constructor
78 */
79 public function __construct()
80 {
81 parent::__construct();
82 $GLOBALS['SOBE'] = $this;
83 $this->init();
84 }
85
86 /**
87 * Init
88 *
89 * @return void
90 * @throws \RuntimeException
91 * @throws InsufficientFileAccessPermissionsException
92 */
93 protected function init()
94 {
95 // Initialize GPvars:
96 $this->uid = (int)GeneralUtility::_GP('uid');
97 $lang = $this->getLanguageService();
98
99 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
100 // Cleaning and checking uid
101 if ($this->uid > 0) {
102 $this->fileOrFolderObject = ResourceFactory::getInstance()
103 ->retrieveFileOrFolderObject('file:' . $this->uid);
104 }
105 if (!$this->fileOrFolderObject) {
106 $title = $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
107 $message = $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
108 throw new \RuntimeException($title . ': ' . $message, 1436895930);
109 }
110 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
111 throw new InsufficientFileAccessPermissionsException(
112 'You are not allowed to access files outside your storages',
113 1436895931
114 );
115 }
116
117 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
118 // so the redirect will NOT end in an error message
119 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
120 // rename the folder
121 if ($this->fileOrFolderObject instanceof Folder) {
122 $parsedUrl = parse_url($this->returnUrl);
123 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
124 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
125 $this->returnUrl = str_replace(
126 urlencode($queryParts['id']),
127 urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
128 $this->returnUrl
129 );
130 }
131 }
132
133 $pathInfo = [
134 'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
135 ];
136 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
137 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
138 $this->moduleTemplate->addJavaScriptCode(
139 'ReplaceFileOnlineJavaScript',
140 'function backToList() {top.goToModule("file_FilelistList");}'
141 );
142 }
143
144 /**
145 * Main function, rendering the content of the rename form
146 *
147 * @return void
148 */
149 public function main()
150 {
151 // Assign variables used by the fluid template
152 $assigns = [];
153 $assigns['moduleUrlTceFile'] = BackendUtility::getModuleUrl('tce_file');
154 $assigns['uid'] = $this->uid;
155 $assigns['returnUrl'] = $this->returnUrl;
156
157 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
158 // csh button
159 $cshButton = $buttonBar->makeHelpButton()
160 ->setModuleName('xMOD_csh_corebe')
161 ->setFieldName('file_rename');
162 $buttonBar->addButton($cshButton);
163
164 // Back button
165 if ($this->returnUrl) {
166 $returnButton = $buttonBar->makeLinkButton()
167 ->setHref($this->returnUrl)
168 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
169 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
170 $buttonBar->addButton($returnButton);
171 }
172
173 // Rendering of the output via fluid
174 $view = GeneralUtility::makeInstance(StandaloneView::class);
175 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
176 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
177 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
178 'EXT:backend/Resources/Private/Templates/File/ReplaceFile.html'
179 ));
180 $view->assignMultiple($assigns);
181 $this->content = $view->render();
182
183 $this->moduleTemplate->setContent($this->content);
184 }
185
186 /**
187 * Processes the request, currently everything is handled and put together via "main()"
188 *
189 * @param ServerRequestInterface $request the current request
190 * @param ResponseInterface $response
191 * @return ResponseInterface the response with the content
192 */
193 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
194 {
195 $this->main();
196
197 $response->getBody()->write($this->moduleTemplate->renderContent());
198 return $response;
199 }
200
201 /**
202 * @return LanguageService
203 */
204 protected function getLanguageService()
205 {
206 return $GLOBALS['LANG'];
207 }
208 }