‪TYPO3CMS  ‪main
ReplaceFileController.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
27 use TYPO3\CMS\Core\Imaging\IconSize;
35 
41 #[AsController]
43 {
49  protected ‪$uid;
50 
56  protected ‪$fileOrFolderObject;
57 
63  protected ‪$returnUrl;
64 
65  protected ‪ModuleTemplate ‪$view;
66 
67  public function ‪__construct(
68  protected readonly ‪IconFactory $iconFactory,
69  protected readonly ‪PageRenderer $pageRenderer,
70  protected readonly ‪UriBuilder $uriBuilder,
71  protected readonly ‪ResourceFactory $resourceFactory,
72  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
73  ) {}
74 
78  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
79  {
80  $this->view = $this->moduleTemplateFactory->create($request);
81  $this->‪init($request);
82  $this->‪renderContent();
83  return $this->view->renderResponse('File/ReplaceFile');
84  }
85 
90  protected function ‪init(ServerRequestInterface $request): void
91  {
92  $parsedBody = $request->getParsedBody();
93  $queryParams = $request->getQueryParams();
94  $lang = $this->‪getLanguageService();
95 
96  // Initialize GPvars:
97  $this->uid = (int)($parsedBody['uid'] ?? $queryParams['uid'] ?? 0);
98  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
99 
100  // Cleaning and checking uid
101  if ($this->uid > 0) {
102  $this->fileOrFolderObject = $this->resourceFactory->retrieveFileOrFolderObject('file:' . $this->uid);
103  }
104  if (!$this->fileOrFolderObject) {
105  $title = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
106  $message = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
107  throw new \RuntimeException($title . ': ' . $message, 1436895930);
108  }
109  if ($this->fileOrFolderObject->getStorage()->isFallbackStorage()) {
110  throw new InsufficientFileAccessPermissionsException(
111  'You are not allowed to access files outside your storages',
112  1436895931
113  );
114  }
115 
116  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
117  // so the redirect will NOT end in an error message
118  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
119  // rename the folder
120  if ($this->fileOrFolderObject instanceof Folder) {
121  $parsedUrl = parse_url($this->returnUrl);
122  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query'] ?? ''));
123  $queryPartsId = $queryParts['id'] ?? null;
124  if ($queryPartsId === $this->fileOrFolderObject->getCombinedIdentifier()) {
125  $this->returnUrl = str_replace(
126  urlencode($queryPartsId),
127  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
128  $this->returnUrl
129  );
130  }
131  }
132 
133  $this->view->getDocHeaderComponent()->setMetaInformationForResource($this->fileOrFolderObject);
134  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/context-menu.js');
135  $this->pageRenderer->loadJavaScriptModule('@typo3/filelist/file-replace.js');
136  }
137 
141  protected function ‪renderContent(): void
142  {
143  // Assign variables used by the fluid template
144  $assigns = [];
145  $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
146  $assigns['uid'] = ‪$this->uid;
147  $assigns['returnUrl'] = ‪$this->returnUrl;
148 
149  $buttonBar = $this->view->getDocHeaderComponent()->getButtonBar();
150 
151  // Back button
152  if ($this->returnUrl) {
153  $returnButton = $buttonBar->makeLinkButton()
154  ->setHref($this->returnUrl)
155  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
156  ->setShowLabelText(true)
157  ->setIcon($this->iconFactory->getIcon('actions-view-go-back', IconSize::SMALL));
158  $buttonBar->addButton($returnButton);
159  }
160  $this->view->assignMultiple($assigns);
161  }
162 
163  protected function ‪getLanguageService(): ‪LanguageService
164  {
165  return ‪$GLOBALS['LANG'];
166  }
167 }
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$uid
‪int $uid
Definition: ReplaceFileController.php:48
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFileController.php:18
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\init
‪init(ServerRequestInterface $request)
Definition: ReplaceFileController.php:87
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\getLanguageService
‪getLanguageService()
Definition: ReplaceFileController.php:160
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$fileOrFolderObject
‪ResourceInterface null $fileOrFolderObject
Definition: ReplaceFileController.php:54
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:44
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\mainAction
‪mainAction(ServerRequestInterface $request)
Definition: ReplaceFileController.php:75
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$view
‪ModuleTemplate $view
Definition: ReplaceFileController.php:62
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$returnUrl
‪string $returnUrl
Definition: ReplaceFileController.php:60
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\renderContent
‪renderContent()
Definition: ReplaceFileController.php:138
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\__construct
‪__construct(protected readonly IconFactory $iconFactory, protected readonly PageRenderer $pageRenderer, protected readonly UriBuilder $uriBuilder, protected readonly ResourceFactory $resourceFactory, protected readonly ModuleTemplateFactory $moduleTemplateFactory,)
Definition: ReplaceFileController.php:64
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Core\Resource\ResourceInterface
Definition: ResourceInterface.php:21
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController
Definition: ReplaceFileController.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52