Completed
Push — master ( 3da3e5...9199f2 )
by
unknown
18:43
created

CreateFolderController::init()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 6
nop 1
dl 0
loc 33
rs 9.584
c 0
b 0
f 0
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\Utility\BackendUtility;
25
use TYPO3\CMS\Core\Http\HtmlResponse;
26
use TYPO3\CMS\Core\Imaging\Icon;
27
use TYPO3\CMS\Core\Localization\LanguageService;
28
use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
29
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
30
use TYPO3\CMS\Core\Resource\ResourceFactory;
31
use TYPO3\CMS\Core\Resource\Security\FileNameValidator;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Core\Utility\MathUtility;
34
use TYPO3\CMS\Fluid\View\StandaloneView;
35
36
/**
37
 * Script class for the create-new script
38
 *
39
 * Displays forms for creating folders (1 to 10), a media asset or a new file.
40
 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
41
 */
42
class CreateFolderController
43
{
44
    /**
45
     * @var int
46
     */
47
    protected $folderNumber = 10;
48
49
    /**
50
     * @var int
51
     */
52
    protected $number;
53
54
    /**
55
     * Set with the target path inputted in &target
56
     *
57
     * @var string
58
     */
59
    protected $target;
60
61
    /**
62
     * The folder object which is  the target directory
63
     *
64
     * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
65
     */
66
    protected $folderObject;
67
68
    /**
69
     * Return URL of list module.
70
     *
71
     * @var string
72
     */
73
    protected $returnUrl;
74
75
    /**
76
     * @var array
77
     */
78
    protected $pathInfo;
79
80
    /**
81
     * ModuleTemplate object
82
     *
83
     * @var ModuleTemplate
84
     */
85
    protected $moduleTemplate;
86
87
    /**
88
     * @var UriBuilder
89
     */
90
    protected $uriBuilder;
91
92
    public function __construct()
93
    {
94
        $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
95
    }
96
97
    /**
98
     * Processes the request, currently everything is handled and put together via "main()"
99
     *
100
     * @param ServerRequestInterface $request the current request
101
     * @return ResponseInterface the response with the content
102
     */
103
    public function mainAction(ServerRequestInterface $request): ResponseInterface
104
    {
105
        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
106
        $this->init($request);
107
        $this->main();
108
        return new HtmlResponse($this->moduleTemplate->renderContent());
109
    }
110
111
    /**
112
     * @param ServerRequestInterface $request
113
     *
114
     * @throws InsufficientFolderAccessPermissionsException
115
     * @throws \RuntimeException
116
     */
117
    protected function init(ServerRequestInterface $request): void
118
    {
119
        $parsedBody = $request->getParsedBody();
120
        $queryParams = $request->getQueryParams();
121
122
        $this->number = $parsedBody['number'] ?? $queryParams['number'] ?? 0;
123
        $this->target = ($combinedIdentifier = $parsedBody['target'] ?? $queryParams['target'] ?? '');
124
        $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
125
        // create the folder object
126
        if ($combinedIdentifier) {
127
            $this->folderObject = GeneralUtility::makeInstance(ResourceFactory::class)
128
                ->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
129
        }
130
        // Cleaning and checking target directory
131
        if (!$this->folderObject) {
132
            $title = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
133
            $message = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
134
            throw new \RuntimeException($title . ': ' . $message, 1294586845);
135
        }
136
        if ($this->folderObject->getStorage()->getUid() === 0) {
137
            throw new InsufficientFolderAccessPermissionsException(
138
                'You are not allowed to access folders outside your storages',
139
                1375889838
140
            );
141
        }
142
143
        $pathInfo = [
144
            'combined_identifier' => $this->folderObject->getCombinedIdentifier(),
145
        ];
146
147
        $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
148
        $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
149
        $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Filelist/CreateFolder');
150
    }
151
152
    /**
153
     * Main function, rendering the main module content
154
     */
155
    protected function main()
156
    {
157
        $lang = $this->getLanguageService();
158
        $assigns = [
159
            'target' => $this->target,
160
            'confirmTitle' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:pleaseConfirm'),
161
            'confirmText' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:mess.redraw'),
162
            'selfUrl' => (string)$this->uriBuilder->buildUriFromRoute('file_newfolder', [
163
                'target' => $this->target,
164
                'returnUrl' => $this->returnUrl,
165
                'number' => 'AMOUNT',
166
            ]),
167
        ];
168
        if ($this->folderObject->checkActionPermission('add')) {
169
            $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
170
            $assigns['cshFileNewFolder'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfolder');
171
            // Making the selector box for the number of concurrent folder-creations
172
            $this->number = MathUtility::forceIntegerInRange($this->number, 1, 10);
173
            for ($a = 1; $a <= $this->folderNumber; $a++) {
174
                $options = [];
175
                $options['value'] = $a;
176
                $options['selected'] = ($this->number == $a ? ' selected="selected"' : '');
177
                $assigns['options'][] = $options;
178
            }
179
            // Making the number of new-folder boxes needed:
180
            for ($a = 0; $a < $this->number; $a++) {
181
                $folder = [];
182
                $folder['this'] = $a;
183
                $folder['next'] = $a + 1;
184
                $assigns['folders'][] = $folder;
185
            }
186
            // Making submit button for folder creation:
187
            $assigns['returnUrl'] = $this->returnUrl;
188
        }
189
190
        if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
191
            $assigns['moduleUrlOnlineMedia'] = (string)$this->uriBuilder->buildUriFromRoute('online_media');
192
            $assigns['cshFileNewMedia'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newMedia');
193
            // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
194
            $fileExtList = [];
195
            $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
196
            $fileNameVerifier = GeneralUtility::makeInstance(FileNameValidator::class);
197
            foreach ($onlineMediaFileExt as $fileExt) {
198
                if ($fileNameVerifier->isValid('.' . $fileExt)) {
199
                    $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
200
                }
201
            }
202
            $assigns['fileExtList'] = $fileExtList;
203
204
            $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
205
            $assigns['cshFileNewFile'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfile');
206
            // Create a list of allowed file extensions with a text format "*.txt, *.css" etc.
207
            $fileExtList = [];
208
            $textFileExt = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], true);
209
            foreach ($textFileExt as $fileExt) {
210
                if ($fileNameVerifier->isValid('.' . $fileExt)) {
211
                    $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
212
                }
213
            }
214
            $assigns['txtFileExtList'] = $fileExtList;
215
        }
216
217
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
218
        // CSH button
219
        $helpButton = $buttonBar->makeHelpButton()
220
            ->setFieldName('file_new')
221
            ->setModuleName('xMOD_csh_corebe');
222
        $buttonBar->addButton($helpButton);
223
224
        // Back
225
        if ($this->returnUrl) {
226
            $backButton = $buttonBar->makeLinkButton()
227
                ->setHref($this->returnUrl)
228
                ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
229
                ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
230
            $buttonBar->addButton($backButton);
231
        }
232
233
        // Rendering of the output via fluid
234
        $view = GeneralUtility::makeInstance(StandaloneView::class);
235
        $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
236
        $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
237
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
238
            'EXT:filelist/Resources/Private/Templates/File/CreateFolder.html'
239
        ));
240
        $view->assignMultiple($assigns);
241
        $this->moduleTemplate->setContent($view->render());
242
    }
243
244
    /**
245
     * Returns LanguageService
246
     *
247
     * @return LanguageService
248
     */
249
    protected function getLanguageService(): LanguageService
250
    {
251
        return $GLOBALS['LANG'];
252
    }
253
}
254