‪TYPO3CMS  11.5
FileUploadController.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;
29 use TYPO3\CMS\Core\Page\PageRenderer;
35 
41 {
47  protected ‪$target;
48 
54  protected ‪$returnUrl;
55 
61  protected ‪$content;
62 
68  protected ‪$folderObject;
69 
75  protected ‪$moduleTemplate;
76 
78  protected PageRenderer ‪$pageRenderer;
79  protected ‪UriBuilder ‪$uriBuilder;
82 
83  public function ‪__construct(
85  PageRenderer ‪$pageRenderer,
89  ) {
90  $this->iconFactory = ‪$iconFactory;
91  $this->pageRenderer = ‪$pageRenderer;
92  $this->uriBuilder = ‪$uriBuilder;
93  $this->resourceFactory = ‪$resourceFactory;
94  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
95  }
96 
103  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
104  {
105  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
106  $this->‪getLanguageService()->‪includeLLFile('EXT:core/Resources/Private/Language/locallang_misc.xlf');
107  $this->‪init($request);
108  $this->‪renderContent();
109  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
110  }
111 
118  protected function ‪init(ServerRequestInterface $request): void
119  {
120  $parsedBody = $request->getParsedBody();
121  $queryParams = $request->getQueryParams();
122 
123  // Initialize GPvars:
124  $this->target = $parsedBody['target'] ?? $queryParams['target'] ?? null;
125  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
126  if (!$this->returnUrl) {
127  $this->returnUrl = (string)$this->uriBuilder->buildUriFromRoute('file_list', [
128  'id' => rawurlencode($this->target),
129  ]);
130  }
131  // Create the folder object
132  if ($this->target) {
133  $this->folderObject = $this->resourceFactory->retrieveFileOrFolderObject($this->target);
134  }
135  if ($this->folderObject->getStorage()->isFallbackStorage()) {
136  throw new InsufficientFolderAccessPermissionsException(
137  'You are not allowed to access folders outside your storages',
138  1375889834
139  );
140  }
141 
142  // Cleaning and checking target directory
143  if (!$this->folderObject) {
144  $title = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
145  $message = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
146  throw new \RuntimeException($title . ': ' . $message, 1294586843);
147  }
148 
149  // Setting up the context sensitive menu
150  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
151 
152  // building pathInfo for metaInformation
153  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformationForResource($this->folderObject);
154  }
155 
159  protected function ‪renderContent(): void
160  {
161  $lang = $this->‪getLanguageService();
162 
163  // set page title
164  $this->moduleTemplate->setTitle($lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_upload.php.pagetitle'));
165 
166  $pageContent = '<form action="'
167  . htmlspecialchars((string)$this->uriBuilder->buildUriFromRoute('tce_file'))
168  . '" method="post" id="FileUploadController" name="editform" enctype="multipart/form-data">';
169  // Make page header:
170  $pageContent .= '<h1>' . $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_upload.php.pagetitle') . '</h1>';
171  $pageContent .= $this->‪renderUploadFormInternal();
172 
173  // Header Buttons
174  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
175 
176  // csh button
177  $cshButton = $buttonBar->makeHelpButton()
178  ->setModuleName('xMOD_csh_corebe')
179  ->setFieldName('file_upload');
180  $buttonBar->addButton($cshButton);
181 
182  // back button
183  if ($this->returnUrl) {
184  $backButton = $buttonBar->makeLinkButton()
185  ->setHref($this->returnUrl)
186  ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
187  ->setIcon($this->iconFactory->getIcon('actions-view-go-back', ‪Icon::SIZE_SMALL));
188  $buttonBar->addButton($backButton);
189  }
190 
191  $pageContent .= '</form>';
192  $this->content .= '<div>' . $pageContent . '</div>';
193  $this->moduleTemplate->setContent($this->content);
194  }
195 
201  protected function ‪renderUploadFormInternal(): string
202  {
203  ‪$content = '
204  <div class="form-group">
205  <input type="file" multiple="multiple" class="form-control" name="upload_1[]" />
206  <input type="hidden" name="data[upload][1][target]" value="' . htmlspecialchars($this->folderObject->getCombinedIdentifier()) . '" />
207  <input type="hidden" name="data[upload][1][data]" value="1" />
208  </div>
209  <div class="form-check">
210  <input class="form-check-input" type="checkbox" name="overwriteExistingFiles" id="overwriteExistingFiles" value="replace" />
211  <label class="form-check-label" for="overwriteExistingFiles"> ' . htmlspecialchars($this->‪getLanguageService()->getLL('overwriteExistingFiles')) . '</label>
212  </div>
213  <div>
214  <input type="hidden" name="data[upload][1][redirect]" value="' . $this->returnUrl . '" />
215  <input class="btn btn-primary" type="submit" value="' . htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_upload.php.submit')) . '" />
216  </div>
217  <div class="callout callout-warning">
218  ' . htmlspecialchars($this->‪getLanguageService()->getLL('uploadMultipleFilesInfo')) . '
219  </div>
220  ';
221 
222  return ‪$content;
223  }
224 
228  protected function ‪getLanguageService(): ‪LanguageService
229  {
230  return ‪$GLOBALS['LANG'];
231  }
232 }
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$returnUrl
‪string $returnUrl
Definition: FileUploadController.php:52
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$iconFactory
‪IconFactory $iconFactory
Definition: FileUploadController.php:72
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: FileUploadController.php:98
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\init
‪init(ServerRequestInterface $request)
Definition: FileUploadController.php:113
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$pageRenderer
‪PageRenderer $pageRenderer
Definition: FileUploadController.php:73
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:23
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\renderUploadFormInternal
‪string renderUploadFormInternal()
Definition: FileUploadController.php:196
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:161
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\__construct
‪__construct(IconFactory $iconFactory, PageRenderer $pageRenderer, UriBuilder $uriBuilder, ResourceFactory $resourceFactory, ModuleTemplateFactory $moduleTemplateFactory)
Definition: FileUploadController.php:78
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$content
‪string $content
Definition: FileUploadController.php:58
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\renderContent
‪renderContent()
Definition: FileUploadController.php:154
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$folderObject
‪File Folder null $folderObject
Definition: FileUploadController.php:64
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\getLanguageService
‪LanguageService getLanguageService()
Definition: FileUploadController.php:223
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController
Definition: FileUploadController.php:41
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: FileUploadController.php:74
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$target
‪string $target
Definition: FileUploadController.php:46
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: FileUploadController.php:70
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageService\includeLLFile
‪array includeLLFile($fileRef)
Definition: LanguageService.php:271
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: FileUploadController.php:76
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Filelist\Controller\File\FileUploadController\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: FileUploadController.php:75
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26