Passed
Push — master ( d5a28b...d12ca0 )
by
unknown
17:01
created

ReplaceFileController::init()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 48
rs 8.8497
c 0
b 0
f 0
cc 6
nc 10
nop 1
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
18
namespace TYPO3\CMS\Filelist\Controller\File;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use TYPO3\CMS\Backend\Routing\UriBuilder;
23
use TYPO3\CMS\Backend\Template\ModuleTemplate;
24
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
25
use TYPO3\CMS\Core\Http\HtmlResponse;
26
use TYPO3\CMS\Core\Imaging\Icon;
27
use TYPO3\CMS\Core\Imaging\IconFactory;
28
use TYPO3\CMS\Core\Localization\LanguageService;
29
use TYPO3\CMS\Core\Page\PageRenderer;
30
use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
31
use TYPO3\CMS\Core\Resource\Folder;
32
use TYPO3\CMS\Core\Resource\ResourceFactory;
33
use TYPO3\CMS\Core\Resource\ResourceInterface;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Fluid\View\StandaloneView;
36
37
/**
38
 * Script Class for the replace-file form
39
 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
40
 */
41
class ReplaceFileController
42
{
43
    /**
44
     * sys_file uid
45
     *
46
     * @var int
47
     */
48
    protected $uid;
49
50
    /**
51
     * The file or folder object that should be renamed
52
     *
53
     * @var ResourceInterface $fileOrFolderObject
54
     */
55
    protected $fileOrFolderObject;
56
57
    /**
58
     * Return URL of list module.
59
     *
60
     * @var string
61
     */
62
    protected $returnUrl;
63
64
    /**
65
     * ModuleTemplate object
66
     *
67
     * @var ModuleTemplate
68
     */
69
    protected $moduleTemplate;
70
71
    protected IconFactory $iconFactory;
72
    protected PageRenderer $pageRenderer;
73
    protected UriBuilder $uriBuilder;
74
    protected ResourceFactory $resourceFactory;
75
    protected ModuleTemplateFactory $moduleTemplateFactory;
76
77
    public function __construct(
78
        IconFactory $iconFactory,
79
        PageRenderer $pageRenderer,
80
        UriBuilder $uriBuilder,
81
        ResourceFactory $resourceFactory,
82
        ModuleTemplateFactory $moduleTemplateFactory
83
    ) {
84
        $this->iconFactory = $iconFactory;
85
        $this->pageRenderer = $pageRenderer;
86
        $this->uriBuilder = $uriBuilder;
87
        $this->resourceFactory = $resourceFactory;
88
        $this->moduleTemplateFactory = $moduleTemplateFactory;
89
    }
90
91
    /**
92
     * Processes the request, currently everything is handled and put together via "main()"
93
     *
94
     * @param ServerRequestInterface $request the current request
95
     * @return ResponseInterface the response with the content
96
     */
97
    public function mainAction(ServerRequestInterface $request): ResponseInterface
98
    {
99
        $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
100
        $this->init($request);
101
        $this->renderContent();
102
        return new HtmlResponse($this->moduleTemplate->renderContent());
103
    }
104
105
    /**
106
     * Init
107
     *
108
     * @param ServerRequestInterface $request
109
     * @throws \RuntimeException
110
     * @throws InsufficientFileAccessPermissionsException
111
     */
112
    protected function init(ServerRequestInterface $request): void
113
    {
114
        $parsedBody = $request->getParsedBody();
115
        $queryParams = $request->getQueryParams();
116
        $lang = $this->getLanguageService();
117
118
        // Initialize GPvars:
119
        $this->uid = (int)($parsedBody['uid'] ?? $queryParams['uid'] ?? 0);
120
        $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
121
122
        // Cleaning and checking uid
123
        if ($this->uid > 0) {
124
            $this->fileOrFolderObject = $this->resourceFactory->retrieveFileOrFolderObject('file:' . $this->uid);
125
        }
126
        if (!$this->fileOrFolderObject) {
127
            $title = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
128
            $message = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
129
            throw new \RuntimeException($title . ': ' . $message, 1436895930);
130
        }
131
        if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
132
            throw new InsufficientFileAccessPermissionsException(
133
                'You are not allowed to access files outside your storages',
134
                1436895931
135
            );
136
        }
137
138
        // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
139
        // so the redirect will NOT end in an error message
140
        // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
141
        // rename the folder
142
        if ($this->fileOrFolderObject instanceof Folder) {
143
            $parsedUrl = parse_url($this->returnUrl);
144
            $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
145
            if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
146
                $this->returnUrl = str_replace(
147
                    urlencode($queryParts['id']),
148
                    urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
149
                    $this->returnUrl
150
                );
151
            }
152
        }
153
154
        $pathInfo = [
155
            'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
0 ignored issues
show
Bug introduced by
The method getCombinedIdentifier() does not exist on TYPO3\CMS\Core\Resource\ResourceInterface. It seems like you code against a sub-type of said class. However, the method does not exist in TYPO3\CMS\Core\Resource\FileInterface or TYPO3\CMS\Core\Resource\FolderInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
            'combined_identifier' => $this->fileOrFolderObject->/** @scrutinizer ignore-call */ getCombinedIdentifier(),
Loading history...
156
        ];
157
        $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
158
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
159
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileReplace');
160
    }
161
162
    /**
163
     * Render module content
164
     */
165
    protected function renderContent(): void
166
    {
167
        // Assign variables used by the fluid template
168
        $assigns = [];
169
        $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
170
        $assigns['uid'] = $this->uid;
171
        $assigns['returnUrl'] = $this->returnUrl;
172
173
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
174
        // csh button
175
        $cshButton = $buttonBar->makeHelpButton()
176
            ->setModuleName('xMOD_csh_corebe')
177
            ->setFieldName('file_rename');
178
        $buttonBar->addButton($cshButton);
179
180
        // Back button
181
        if ($this->returnUrl) {
182
            $returnButton = $buttonBar->makeLinkButton()
183
                ->setHref($this->returnUrl)
184
                ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
185
                ->setIcon($this->iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
186
            $buttonBar->addButton($returnButton);
187
        }
188
189
        // Rendering of the output via fluid
190
        $view = GeneralUtility::makeInstance(StandaloneView::class);
191
        $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
192
        $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
193
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
194
            'EXT:filelist/Resources/Private/Templates/File/ReplaceFile.html'
195
        ));
196
        $view->assignMultiple($assigns);
197
        $this->moduleTemplate->setContent($view->render());
198
    }
199
200
    /**
201
     * @return LanguageService
202
     */
203
    protected function getLanguageService(): LanguageService
204
    {
205
        return $GLOBALS['LANG'];
206
    }
207
}
208