‪TYPO3CMS  11.5
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\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
42 
48 {
54  protected ‪$content;
55 
61  protected ‪$origTarget;
62 
68  protected ‪$target;
69 
75  protected ‪$returnUrl;
76 
82  protected ‪$fileObject;
83 
89  protected ‪$moduleTemplate;
90 
92  protected ‪UriBuilder ‪$uriBuilder;
95 
96  public function ‪__construct(
101  ) {
102  $this->iconFactory = ‪$iconFactory;
103  $this->uriBuilder = ‪$uriBuilder;
104  $this->resourceFactory = ‪$resourceFactory;
105  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
106  }
107 
114  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
115  {
116  $this->‪init($request);
117  if ($response = $this->‪process()) {
118  return $response;
119  }
120 
121  return new HtmlResponse($this->moduleTemplate->renderContent());
122  }
123 
131  protected function ‪init(ServerRequestInterface $request): void
132  {
133  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
134  $parsedBody = $request->getParsedBody();
135  $queryParams = $request->getQueryParams();
136 
137  // Setting target, which must be a file reference to a file within the mounts.
138  $this->target = $this->origTarget = $parsedBody['target'] ?? $queryParams['target'] ?? '';
139  // create the file object
140  if ($this->target) {
141  $this->fileObject = $this->resourceFactory->retrieveFileOrFolderObject($this->target);
142  }
143  // Cleaning and checking target directory
144  if (!$this->fileObject) {
145  $title = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
146  $message = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
147  throw new \RuntimeException($title . ': ' . $message, 1294586841);
148  }
149  if ($this->fileObject->getStorage()->isFallbackStorage()) {
150  throw new InsufficientFileAccessPermissionsException(
151  'You are not allowed to access files outside your storages',
152  1375889832
153  );
154  }
155  $this->returnUrl = GeneralUtility::sanitizeLocalUrl(
156  $parsedBody['returnUrl']
157  ?? $queryParams['returnUrl']
158  ?? (string)$this->uriBuilder->buildUriFromRoute('file_FilelistList', [
159  'id' => $this->fileObject->getParentFolder()->getCombinedIdentifier(),
160  ])
161  );
162  }
163 
169  protected function ‪process(): ?ResponseInterface
170  {
171  $dataColumnDefinition = [
172  'label' => htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:file'))
173  . ' ' . htmlspecialchars($this->target),
174  'config' => [
175  'type' => 'text',
176  'cols' => 48,
177  'wrap' => 'OFF',
178  ],
179  'defaultExtras' => 'fixed-font: enable-tab',
180  ];
181 
182  $this->‪getButtonsInternal();
183  // Hook: before compiling the output
184  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'] ?? [] as $hookFunction) {
185  $hookParameters = [
186  'content' => &‪$this->content,
187  'target' => &‪$this->target,
188  'dataColumnDefinition' => &$dataColumnDefinition,
189  ];
190  GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
191  }
192 
193  $assigns = [];
194  $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
195  $assigns['fileName'] = $this->fileObject->getName();
196 
197  try {
198  $extList = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
199  if (!$this->fileObject->isTextFile()) {
200  // @todo throw a minor exception here, not the global one
201  throw new \Exception('Files with that extension are not editable. Allowed extensions are: ' . $extList, 1476050135);
202  }
203 
204  $fullIdentifier = $this->fileObject->getCombinedIdentifier();
205  ‪$returnUrl = (string)$this->uriBuilder->buildUriFromRoute('file_edit', ['target' => $fullIdentifier]);
206 
207  // Making the formfields
208  $formData = [
209  'databaseRow' => [
210  'uid' => 0,
211  'data' => $this->fileObject->getContents(),
212  'target' => $this->fileObject->getUid(),
213  'redirect' => ‪$returnUrl,
214  ],
215  'tableName' => 'editfile',
216  'processedTca' => [
217  'columns' => [
218  'data' => $dataColumnDefinition,
219  'target' => [
220  'config' => [
221  'type' => 'input',
222  'renderType' => 'hidden',
223  ],
224  ],
225  'redirect' => [
226  'config' => [
227  'type' => 'input',
228  'renderType' => 'hidden',
229  ],
230  ],
231  ],
232  'types' => [
233  1 => [
234  'showitem' => 'data,target,redirect',
235  ],
236  ],
237  ],
238  'recordTypeValue' => 1,
239  'inlineStructure' => [],
240  'renderType' => 'fullRecordContainer',
241  ];
242 
243  $resultArray = GeneralUtility::makeInstance(NodeFactory::class)->create($formData)->render();
244  $formResultCompiler = GeneralUtility::makeInstance(FormResultCompiler::class);
245  $formResultCompiler->mergeResult($resultArray);
246 
247  $form = $formResultCompiler->addCssFiles()
248  . ($resultArray['html'] ?? '')
249  . $formResultCompiler->printNeededJSFunctions();
250 
251  $assigns['form'] = $form;
252  } catch (\‪Exception $e) {
253  // @todo catch dedicated exceptions, not the global one, if possible
254  $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $e->getMessage(), '', ‪FlashMessage::ERROR, true);
255 
256  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
257  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
258  $defaultFlashMessageQueue->enqueue($flashMessage);
259 
260  return new ‪RedirectResponse($this->returnUrl, 500);
261  }
262 
263  // Rendering of the output via fluid
264  $view = GeneralUtility::makeInstance(StandaloneView::class);
265  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
266  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
267  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
268  'EXT:filelist/Resources/Private/Templates/File/EditFile.html'
269  ));
270  $view->assignMultiple($assigns);
271  $pageContent = $view->render();
272 
273  // Hook: after compiling the output
274  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'] ?? [] as $hookFunction) {
275  $hookParameters = [
276  'pageContent' => &$pageContent,
277  'target' => &‪$this->target,
278  ];
279  GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
280  }
281 
282  $this->content .= $pageContent;
283  $this->moduleTemplate->setContent($this->content);
284  return null;
285  }
286 
290  protected function ‪getButtonsInternal(): void
291  {
292  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
293 
294  $lang = $this->‪getLanguageService();
295  // CSH button
296  $helpButton = $buttonBar->makeHelpButton()
297  ->setFieldName('file_edit')
298  ->setModuleName('xMOD_csh_corebe');
299  $buttonBar->addButton($helpButton);
300 
301  // Save button
302  $saveButton = $buttonBar->makeInputButton()
303  ->setName('_save')
304  ->setValue('1')
305  ->setForm('EditFileController')
306  ->setShowLabelText(true)
307  ->setTitle($lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_edit.php.submit'))
308  ->setIcon($this->iconFactory->getIcon('actions-document-save', ‪Icon::SIZE_SMALL));
309  $buttonBar->addButton($saveButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 20);
310 
311  // Cancel button
312  $closeButton = $buttonBar->makeLinkButton()
313  ->setShowLabelText(true)
314  ->setHref($this->returnUrl)
315  ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.cancel'))
316  ->setIcon($this->iconFactory->getIcon('actions-close', ‪Icon::SIZE_SMALL));
317  $buttonBar->addButton($closeButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 10);
318  }
319 
325  protected function ‪getLanguageService(): ‪LanguageService
326  {
327  return ‪$GLOBALS['LANG'];
328  }
329 
335  protected function ‪getBackendUser(): ‪BackendUserAuthentication
336  {
337  return ‪$GLOBALS['BE_USER'];
338  }
339 }
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$iconFactory
‪IconFactory $iconFactory
Definition: EditFileController.php:85
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$content
‪string $content
Definition: EditFileController.php:53
‪TYPO3\CMS\Backend\Form\FormResultCompiler
Definition: FormResultCompiler.php:31
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_LEFT
‪const BUTTON_POSITION_LEFT
Definition: ButtonBar.php:36
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:32
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$fileObject
‪File Folder null $fileObject
Definition: EditFileController.php:77
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\init
‪init(ServerRequestInterface $request)
Definition: EditFileController.php:125
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: EditFileController.php:87
‪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
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\process
‪ResponseInterface null process()
Definition: EditFileController.php:163
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: EditFileController.php:86
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\getLanguageService
‪LanguageService getLanguageService()
Definition: EditFileController.php:319
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: EditFileController.php:108
‪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\EditFileController\getButtonsInternal
‪getButtonsInternal()
Definition: EditFileController.php:284
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: EditFileController.php:83
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\__construct
‪__construct(IconFactory $iconFactory, UriBuilder $uriBuilder, ResourceFactory $resourceFactory, ModuleTemplateFactory $moduleTemplateFactory)
Definition: EditFileController.php:90
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$returnUrl
‪string $returnUrl
Definition: EditFileController.php:71
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:37
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: EditFileController.php:329
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$target
‪string $target
Definition: EditFileController.php:65
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Filelist\Controller\File\EditFileController
Definition: EditFileController.php:48
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$origTarget
‪string $origTarget
Definition: EditFileController.php:59
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: EditFileController.php:88