‪TYPO3CMS  ‪main
EditFileController.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\EventDispatcher\EventDispatcherInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Message\StreamFactoryInterface;
34 use TYPO3\CMS\Core\Imaging\IconSize;
43 
49 #[AsController]
51 {
52  protected array ‪$dataColumnTca = [
53  'label' => '',
54  'config' => [
55  'type' => 'text',
56  'cols' => 48,
57  'wrap' => 'off',
58  'enableTabulator' => true,
59  'fixedFont' => true,
60  ],
61  ];
62 
63  protected array ‪$formEngineData = [
64  'databaseRow' => [
65  'uid' => 0,
66  'data' => '',
67  'target' => 0,
68  'redirect' => '',
69  ],
70  'tableName' => 'editfile',
71  'processedTca' => [
72  'columns' => [
73  'data' => [],
74  'target' => [
75  'config' => [
76  'type' => 'input',
77  'renderType' => 'hidden',
78  ],
79  ],
80  'redirect' => [
81  'config' => [
82  'type' => 'input',
83  'renderType' => 'hidden',
84  ],
85  ],
86  ],
87  'types' => [
88  1 => [
89  'showitem' => 'data,target,redirect',
90  ],
91  ],
92  ],
93  'recordTypeValue' => 1,
94  'inlineStructure' => [],
95  'renderType' => 'fullRecordContainer',
96  ];
97 
98  public function ‪__construct(
99  protected readonly ‪IconFactory $iconFactory,
100  protected readonly ‪UriBuilder $uriBuilder,
101  protected readonly ‪ResourceFactory $resourceFactory,
102  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
103  protected readonly ‪ResponseFactory $responseFactory,
104  protected readonly StreamFactoryInterface $streamFactory,
105  protected readonly EventDispatcherInterface $eventDispatcher,
106  ) {}
107 
111  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
112  {
113  $languageService = $this->‪getLanguageService();
114  $view = $this->moduleTemplateFactory->create($request);
115  $parsedBody = $request->getParsedBody();
116  $queryParams = $request->getQueryParams();
117 
118  $combinedIdentifier = $parsedBody['target'] ?? $queryParams['target'] ?? '';
119  $file = $this->resourceFactory->retrieveFileOrFolderObject($combinedIdentifier);
120 
121  if (!$file instanceof ‪FileInterface) {
122  throw new ‪InvalidFileException('Referenced target "' . $combinedIdentifier . '" could not be resolved to a valid file', 1294586841);
123  }
124  if ($file->getStorage()->isFallbackStorage()) {
125  throw new ‪InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889832);
126  }
127 
128  $returnUrl = GeneralUtility::sanitizeLocalUrl(
129  $parsedBody['returnUrl']
130  ?? $queryParams['returnUrl']
131  ?? (string)$this->uriBuilder->buildUriFromRoute('media_management', [
132  'id' => $file->getParentFolder()->getCombinedIdentifier(),
133  ])
134  );
135 
136  if (!$file->isTextFile()) {
137  $extList = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
138  $view->addFlashMessage('Files with that extension are not editable. Allowed extensions are: ' . $extList, '', ContextualFeedbackSeverity::ERROR, true);
139  return $this->responseFactory->createResponse(400)->withHeader('location', $returnUrl);
140  }
141 
142  $this->‪addDocHeaderButtons($view, $returnUrl);
143 
144  $dataColumnDefinition = ‪$this->dataColumnTca;
145  $dataColumnDefinition['label'] = htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:file')) . ' ' . htmlspecialchars($combinedIdentifier);
146 
147  $formData = ‪$this->formEngineData;
148  $formData['databaseRow']['data'] = $file->getContents();
149  $formData['databaseRow']['target'] = $file->getUid();
150  $formData['databaseRow']['redirect'] = (string)$this->uriBuilder->buildUriFromRoute('file_edit', ['target' => $combinedIdentifier]);
151  $formData['processedTca']['columns']['data'] = $dataColumnDefinition;
152 
153  $formData = $this->eventDispatcher->dispatch(
154  new ‪ModifyEditFileFormDataEvent($formData, $file, $request)
155  )->getFormData();
156 
157  $resultArray = GeneralUtility::makeInstance(NodeFactory::class)->create($formData)->render();
158  $formResultCompiler = GeneralUtility::makeInstance(FormResultCompiler::class);
159  $formResultCompiler->mergeResult($resultArray);
160 
161  // Rendering of the output via fluid
162  $view->assignMultiple([
163  'moduleUrlTceFile' => (string)$this->uriBuilder->buildUriFromRoute('tce_file'),
164  'fileName' => $file->getName(),
165  'form' => $formResultCompiler->addCssFiles() . ($resultArray['html'] ?? '') . $formResultCompiler->printNeededJSFunctions(),
166  ]);
167  $content = $view->render('File/EditFile');
168 
169  return $this->responseFactory->createResponse()
170  ->withHeader('Content-Type', 'text/html; charset=utf-8')
171  ->withBody($this->streamFactory->createStream($content));
172  }
173 
174  protected function ‪addDocHeaderButtons(‪ModuleTemplate $view, string $returnUrl): void
175  {
176  $languageService = $this->‪getLanguageService();
177  $buttonBar = $view->‪getDocHeaderComponent()->getButtonBar();
178 
179  // Save button
180  $saveButton = $buttonBar->makeInputButton()
181  ->setName('_save')
182  ->setValue('1')
183  ->setForm('EditFileController')
184  ->setShowLabelText(true)
185  ->setTitle($languageService->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_edit.php.submit'))
186  ->setIcon($this->iconFactory->getIcon('actions-document-save', IconSize::SMALL));
187  $buttonBar->addButton($saveButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 20);
188 
189  // Cancel button
190  $closeButton = $buttonBar->makeLinkButton()
191  ->setShowLabelText(true)
192  ->setHref($returnUrl)
193  ->setTitle($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.cancel'))
194  ->setIcon($this->iconFactory->getIcon('actions-close', IconSize::SMALL));
195  $buttonBar->addButton($closeButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 10);
196  }
197 
199  {
200  return ‪$GLOBALS['LANG'];
201  }
202 
204  {
205  return ‪$GLOBALS['BE_USER'];
206  }
207 }
‪TYPO3\CMS\Filelist\Event\ModifyEditFileFormDataEvent
Definition: ModifyEditFileFormDataEvent.php:28
‪TYPO3\CMS\Backend\Form\FormResultCompiler
Definition: FormResultCompiler.php:34
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\mainAction
‪mainAction(ServerRequestInterface $request)
Definition: EditFileController.php:111
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_LEFT
‪const BUTTON_POSITION_LEFT
Definition: ButtonBar.php:37
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:33
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\getLanguageService
‪getLanguageService()
Definition: EditFileController.php:198
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFileController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\getBackendUser
‪getBackendUser()
Definition: EditFileController.php:203
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$dataColumnTca
‪array $dataColumnTca
Definition: EditFileController.php:52
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\Exception\InvalidFileException
Definition: InvalidFileException.php:23
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\addDocHeaderButtons
‪addDocHeaderButtons(ModuleTemplate $view, string $returnUrl)
Definition: EditFileController.php:174
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:40
‪TYPO3\CMS\Backend\Template\ModuleTemplate\getDocHeaderComponent
‪getDocHeaderComponent()
Definition: ModuleTemplate.php:181
‪TYPO3\CMS\Core\Http\ResponseFactory
Definition: ResponseFactory.php:27
‪$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\EditFileController\__construct
‪__construct(protected readonly IconFactory $iconFactory, protected readonly UriBuilder $uriBuilder, protected readonly ResourceFactory $resourceFactory, protected readonly ModuleTemplateFactory $moduleTemplateFactory, protected readonly ResponseFactory $responseFactory, protected readonly StreamFactoryInterface $streamFactory, protected readonly EventDispatcherInterface $eventDispatcher,)
Definition: EditFileController.php:98
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$formEngineData
‪array $formEngineData
Definition: EditFileController.php:63
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Filelist\Controller\File\EditFileController
Definition: EditFileController.php:51