‪TYPO3CMS  ‪main
CreateFileController.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;
26 use TYPO3\CMS\Core\Imaging\IconSize;
35 
41 #[AsController]
43 {
47  protected string ‪$target = '';
48 
52  protected ?‪Folder ‪$folderObject = null;
53 
57  protected string ‪$returnUrl = '';
58 
60 
61  public function ‪__construct(
62  protected readonly ‪IconFactory $iconFactory,
63  protected readonly ‪PageRenderer $pageRenderer,
64  protected readonly ‪ResourceFactory $resourceFactory,
65  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
66  protected readonly ‪OnlineMediaHelperRegistry $onlineMediaHelperRegistry,
67  ) {}
68 
69  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
70  {
71  $this->view = $this->moduleTemplateFactory->create($request);
72  $this->‪initialize($request);
73  $hasPermission = $this->folderObject->getStorage()->checkUserActionPermission('add', 'File');
74  $assigns = [
75  'target' => ‪$this->target,
76  'hasPermission' => $hasPermission,
77  'returnUrl' => ‪$this->returnUrl,
78  ];
79 
80  if ($hasPermission) {
81  // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
82  $fileExtList = [];
83  $onlineMediaFileExt = $this->onlineMediaHelperRegistry->getSupportedFileExtensions();
84  $fileNameVerifier = GeneralUtility::makeInstance(FileNameValidator::class);
85  foreach ($onlineMediaFileExt as $fileExt) {
86  if ($fileNameVerifier->isValid('.' . $fileExt)) {
87  $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
88  }
89  }
90  $assigns['fileExtList'] = $fileExtList;
91 
92  // Create a list of allowed file extensions with a text format "*.txt, *.css" etc.
93  $fileExtList = [];
94  $textFileExt = ‪GeneralUtility::trimExplode(',', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], true);
95  foreach ($textFileExt as $fileExt) {
96  if ($fileNameVerifier->isValid('.' . $fileExt)) {
97  $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
98  }
99  }
100  $assigns['txtFileExtList'] = $fileExtList;
101  }
102 
103  $this->view->assignMultiple($assigns);
104  return $this->view->renderResponse('File/CreateFile');
105  }
106 
107  protected function ‪initialize(ServerRequestInterface $request): void
108  {
109  $parsedBody = $request->getParsedBody();
110  $queryParams = $request->getQueryParams();
111 
112  $this->target = $parsedBody['target'] ?? $queryParams['target'] ?? '';
113  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
114  // create the folder object
115  if ($this->target) {
116  $this->folderObject = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($this->target);
117  }
118  // Cleaning and checking target directory
119  if (!$this->folderObject instanceof ‪Folder) {
120  $title = $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
121  $message = $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
122  throw new \RuntimeException($title . ': ' . $message, 1667565756);
123  }
124  if ($this->folderObject->getStorage()->isFallbackStorage()) {
126  'You are not allowed to access folders outside your storages',
127  1667565757
128  );
129  }
130 
131  $this->view->getDocHeaderComponent()->setMetaInformationForResource($this->folderObject);
132  if ($this->returnUrl) {
133  $buttonBar = $this->view->getDocHeaderComponent()->getButtonBar();
134  $backButton = $buttonBar->makeLinkButton()
135  ->setHref($this->returnUrl)
136  ->setShowLabelText(true)
137  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
138  ->setIcon($this->iconFactory->getIcon('actions-view-go-back', IconSize::SMALL));
139  $buttonBar->addButton($backButton);
140  }
141 
142  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/context-menu.js');
143  }
144 
146  {
147  return ‪$GLOBALS['LANG'];
148  }
149 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Resource\Security\FileNameValidator
Definition: FileNameValidator.php:25
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\$target
‪string $target
Definition: CreateFileController.php:47
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\$folderObject
‪Folder $folderObject
Definition: CreateFileController.php:52
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\getLanguageService
‪getLanguageService()
Definition: CreateFileController.php:145
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\$view
‪ModuleTemplate $view
Definition: CreateFileController.php:59
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFileController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\mainAction
‪mainAction(ServerRequestInterface $request)
Definition: CreateFileController.php:69
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:44
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\$returnUrl
‪string $returnUrl
Definition: CreateFileController.php:57
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController
Definition: CreateFileController.php:43
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\initialize
‪initialize(ServerRequestInterface $request)
Definition: CreateFileController.php:107
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Filelist\Controller\File\CreateFileController\__construct
‪__construct(protected readonly IconFactory $iconFactory, protected readonly PageRenderer $pageRenderer, protected readonly ResourceFactory $resourceFactory, protected readonly ModuleTemplateFactory $moduleTemplateFactory, protected readonly OnlineMediaHelperRegistry $onlineMediaHelperRegistry,)
Definition: CreateFileController.php:61
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822