‪TYPO3CMS  9.5
ReplaceFileController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
31 
37 {
39 
43  protected ‪$deprecatedPublicProperties = [
44  'doc' => 'Using $doc of class ReplaceFileController from outside is discouraged as this variable is only used for internal storage.',
45  'title' => 'Using $title of class ReplaceFileController from outside is discouraged as this variable is only used for internal storage.',
46  'uid' => 'Using $uid of class ReplaceFileController from outside is discouraged as this variable is only used for internal storage.',
47  'returnUrl' => 'Using $returnUrl of class ReplaceFileController from outside is discouraged as this variable is only used for internal storage.',
48  'content' => 'Using $content of class ReplaceFileController from outside is discouraged as this variable is only used for internal storage.',
49  ];
50 
56  protected ‪$doc;
57 
63  protected ‪$title;
64 
70  protected ‪$uid;
71 
78 
84  protected ‪$returnUrl;
85 
91  protected ‪$content;
92 
98  protected ‪$moduleTemplate;
99 
103  public function ‪__construct()
104  {
105  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
106 
107  // @deprecated since TYPO3 v9, will be moved out of __construct() in TYPO3 v10.0
108  $this->‪init(‪$GLOBALS['TYPO3_REQUEST']);
109  }
110 
117  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
118  {
119  $this->‪renderContent();
120  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
121  }
122 
126  public function ‪main()
127  {
128  trigger_error('ReplaceFileController->main() will be replaced by protected method renderContent() in TYPO3 v10.0. Do not call from other extension.', E_USER_DEPRECATED);
129  $this->‪renderContent();
130  }
131 
139  protected function ‪init(ServerRequestInterface $request): void
140  {
141  $parsedBody = $request->getParsedBody();
142  $queryParams = $request->getQueryParams();
143  $lang = $this->‪getLanguageService();
144 
145  // Initialize GPvars:
146  $this->uid = (int)($parsedBody['uid'] ?? $queryParams['uid'] ?? 0);
147  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
148 
149  // Cleaning and checking uid
150  if ($this->uid > 0) {
151  $this->fileOrFolderObject = ‪ResourceFactory::getInstance()
152  ->‪retrieveFileOrFolderObject('file:' . $this->uid);
153  }
154  if (!$this->fileOrFolderObject) {
155  ‪$title = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
156  $message = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
157  throw new \RuntimeException(‪$title . ': ' . $message, 1436895930);
158  }
159  if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
160  throw new InsufficientFileAccessPermissionsException(
161  'You are not allowed to access files outside your storages',
162  1436895931
163  );
164  }
165 
166  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
167  // so the redirect will NOT end in an error message
168  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
169  // rename the folder
170  if ($this->fileOrFolderObject instanceof Folder) {
171  $parsedUrl = parse_url($this->returnUrl);
172  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
173  if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
174  $this->returnUrl = str_replace(
175  urlencode($queryParts['id']),
176  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
177  $this->returnUrl
178  );
179  }
180  }
181 
182  $pathInfo = [
183  'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
184  ];
185  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
186  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
187  $this->moduleTemplate->addJavaScriptCode(
188  'ReplaceFileOnlineJavaScript',
189  'function backToList() {top.goToModule("file_FilelistList");}'
190  );
191  }
192 
196  protected function ‪renderContent(): void
197  {
198  // Assign variables used by the fluid template
199  $assigns = [];
201  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
202  $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
203  $assigns['uid'] = ‪$this->uid;
204  $assigns['returnUrl'] = ‪$this->returnUrl;
205 
206  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
207  // csh button
208  $cshButton = $buttonBar->makeHelpButton()
209  ->setModuleName('xMOD_csh_corebe')
210  ->setFieldName('file_rename');
211  $buttonBar->addButton($cshButton);
212 
213  // Back button
214  if ($this->returnUrl) {
215  $returnButton = $buttonBar->makeLinkButton()
216  ->setHref($this->returnUrl)
217  ->setTitle($this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
218  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', ‪Icon::SIZE_SMALL));
219  $buttonBar->addButton($returnButton);
220  }
221 
222  // Rendering of the output via fluid
223  $view = GeneralUtility::makeInstance(StandaloneView::class);
224  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
225  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
226  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
227  'EXT:backend/Resources/Private/Templates/File/ReplaceFile.html'
228  ));
229  $view->assignMultiple($assigns);
230  $this->content = $view->render();
231 
232  $this->moduleTemplate->setContent($this->content);
233  }
234 
238  protected function ‪getLanguageService(): ‪LanguageService
239  {
240  return ‪$GLOBALS['LANG'];
241  }
242 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:29
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: ReplaceFileController.php:108
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: ReplaceFileController.php:89
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$content
‪string $content
Definition: ReplaceFileController.php:83
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$fileOrFolderObject
‪TYPO3 CMS Core Resource ResourceInterface $fileOrFolderObject
Definition: ReplaceFileController.php:71
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$deprecatedPublicProperties
‪array $deprecatedPublicProperties
Definition: ReplaceFileController.php:41
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\init
‪init(ServerRequestInterface $request)
Definition: ReplaceFileController.php:130
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\__construct
‪__construct()
Definition: ReplaceFileController.php:94
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:40
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:21
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$doc
‪TYPO3 CMS Backend Template DocumentTemplate $doc
Definition: ReplaceFileController.php:53
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:35
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\getLanguageService
‪LanguageService getLanguageService()
Definition: ReplaceFileController.php:229
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$returnUrl
‪string $returnUrl
Definition: ReplaceFileController.php:77
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\main
‪main()
Definition: ReplaceFileController.php:117
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$uid
‪int $uid
Definition: ReplaceFileController.php:65
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\renderContent
‪renderContent()
Definition: ReplaceFileController.php:187
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController\$title
‪string $title
Definition: ReplaceFileController.php:59
‪TYPO3\CMS\Backend\Controller\File
Definition: CreateFolderController.php:3
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait
Definition: PublicPropertyDeprecationTrait.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\ResourceFactory\retrieveFileOrFolderObject
‪File Folder null retrieveFileOrFolderObject($input)
Definition: ResourceFactory.php:491
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:25
‪TYPO3\CMS\Backend\Controller\File\ReplaceFileController
Definition: ReplaceFileController.php:37