‪TYPO3CMS  10.4
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;
39 
45 {
51  protected ‪$content;
52 
58  protected ‪$origTarget;
59 
65  protected ‪$target;
66 
72  protected ‪$returnUrl;
73 
79  protected ‪$fileObject;
80 
86  protected ‪$moduleTemplate;
87 
91  protected ‪$uriBuilder;
92 
96  public function ‪__construct()
97  {
98  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
99  $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
100  }
101 
108  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
109  {
110  $this->‪init($request);
111  if ($response = $this->‪process()) {
112  return $response;
113  }
114 
115  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
116  }
117 
125  protected function ‪init(ServerRequestInterface $request): void
126  {
127  $parsedBody = $request->getParsedBody();
128  $queryParams = $request->getQueryParams();
129 
130  // Setting target, which must be a file reference to a file within the mounts.
131  $this->target = $this->origTarget = $parsedBody['target'] ?? $queryParams['target'] ?? '';
132  // create the file object
133  if ($this->target) {
134  $this->fileObject = GeneralUtility::makeInstance(ResourceFactory::class)
135  ->retrieveFileOrFolderObject($this->target);
136  }
137  // Cleaning and checking target directory
138  if (!$this->fileObject) {
139  $title = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
140  $message = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
141  throw new \RuntimeException($title . ': ' . $message, 1294586841);
142  }
143  if ($this->fileObject->getStorage()->getUid() === 0) {
144  throw new InsufficientFileAccessPermissionsException(
145  'You are not allowed to access files outside your storages',
146  1375889832
147  );
148  }
149  $this->returnUrl = GeneralUtility::sanitizeLocalUrl(
150  $parsedBody['returnUrl']
151  ?? $queryParams['returnUrl']
152  ?? (string)$this->uriBuilder->buildUriFromRoute('file_FilelistList', [
153  'id' => $this->fileObject->getParentFolder()->getCombinedIdentifier()
154  ])
155  );
156  }
157 
163  protected function ‪process(): ?ResponseInterface
164  {
165  $dataColumnDefinition = [
166  'label' => htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:file'))
167  . ' ' . htmlspecialchars($this->target),
168  'config' => [
169  'type' => 'text',
170  'cols' => 48,
171  'wrap' => 'OFF',
172  ],
173  'defaultExtras' => 'fixed-font: enable-tab'
174  ];
175 
176  $this->‪getButtonsInternal();
177  // Hook: before compiling the output
178  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'] ?? [] as $hookFunction) {
179  $hookParameters = [
180  'content' => &‪$this->content,
181  'target' => &‪$this->target,
182  'dataColumnDefinition' => &$dataColumnDefinition,
183  ];
184  GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
185  }
186 
187  $assigns = [];
188  $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
189  $assigns['fileName'] = $this->fileObject->getName();
190 
191  try {
192  $extList = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
193  if (!$this->fileObject->isTextFile()) {
194  // @todo throw a minor exception here, not the global one
195  throw new \Exception('Files with that extension are not editable. Allowed extensions are: ' . $extList, 1476050135);
196  }
197 
198  // Making the formfields
199  $formData = [
200  'databaseRow' => [
201  'uid' => 0,
202  'data' => $this->fileObject->getContents(),
203  'target' => $this->fileObject->getUid(),
204  'redirect' => ‪$this->returnUrl,
205  ],
206  'tableName' => 'editfile',
207  'processedTca' => [
208  'columns' => [
209  'data' => $dataColumnDefinition,
210  'target' => [
211  'config' => [
212  'type' => 'input',
213  'renderType' => 'hidden',
214  ],
215  ],
216  'redirect' => [
217  'config' => [
218  'type' => 'input',
219  'renderType' => 'hidden',
220  ],
221  ],
222  ],
223  'types' => [
224  1 => [
225  'showitem' => 'data,target,redirect',
226  ],
227  ],
228  ],
229  'recordTypeValue' => 1,
230  'inlineStructure' => [],
231  'renderType' => 'fullRecordContainer',
232  ];
233 
234  $resultArray = GeneralUtility::makeInstance(NodeFactory::class)->create($formData)->render();
235  $formResultCompiler = GeneralUtility::makeInstance(FormResultCompiler::class);
236  $formResultCompiler->mergeResult($resultArray);
237 
238  $form = $formResultCompiler->addCssFiles()
239  . $resultArray['html']
240  . $formResultCompiler->printNeededJSFunctions();
241 
242  $assigns['form'] = $form;
243  } catch (\Exception $e) {
244  // @todo catch dedicated exceptions, not the global one, if possible
245  $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $e->getMessage(), '', ‪FlashMessage::ERROR, true);
246 
247  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
248  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
249  $defaultFlashMessageQueue->enqueue($flashMessage);
250 
251  return new RedirectResponse($this->returnUrl, 500);
252  }
253 
254  // Rendering of the output via fluid
255  $view = GeneralUtility::makeInstance(StandaloneView::class);
256  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
257  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
258  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
259  'EXT:filelist/Resources/Private/Templates/File/EditFile.html'
260  ));
261  $view->assignMultiple($assigns);
262  $pageContent = $view->render();
263 
264  // Hook: after compiling the output
265  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'] ?? [] as $hookFunction) {
266  $hookParameters = [
267  'pageContent' => &$pageContent,
268  'target' => &‪$this->target
269  ];
270  GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
271  }
272 
273  $this->content .= $pageContent;
274  $this->moduleTemplate->setContent($this->content);
275  return null;
276  }
277 
281  protected function ‪getButtonsInternal(): void
282  {
283  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
284 
285  $lang = $this->‪getLanguageService();
286  // CSH button
287  $helpButton = $buttonBar->makeHelpButton()
288  ->setFieldName('file_edit')
289  ->setModuleName('xMOD_csh_corebe');
290  $buttonBar->addButton($helpButton);
291 
292  // Save button
293  $saveButton = $buttonBar->makeInputButton()
294  ->setName('_save')
295  ->setValue('1')
296  ->setForm('EditFileController')
297  ->setTitle($lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_edit.php.submit'))
298  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save', ‪Icon::SIZE_SMALL));
299 
300  // Save and Close button
301  $saveAndCloseButton = $buttonBar->makeInputButton()
302  ->setName('_saveandclosedok')
303  ->setValue('1')
304  ->setForm('EditFileController')
305  ->setTitle($lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_edit.php.saveAndClose'))
306  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
307  'actions-document-save-close',
309  ));
310 
311  $splitButton = $buttonBar->makeSplitButton()
312  ->addItem($saveButton)
313  ->addItem($saveAndCloseButton);
314  $buttonBar->addButton($splitButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 20);
315 
316  // Cancel button
317  $closeButton = $buttonBar->makeLinkButton()
318  ->setShowLabelText(true)
319  ->setHref($this->returnUrl)
320  ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.cancel'))
321  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-close', ‪Icon::SIZE_SMALL));
322  $buttonBar->addButton($closeButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 10);
323 
324  // Make shortcut:
325  $shortButton = $buttonBar->makeShortcutButton()
326  ->setModuleName('file_edit')
327  ->setGetVariables(['target']);
328  $buttonBar->addButton($shortButton);
329  }
330 
336  protected function ‪getLanguageService(): ‪LanguageService
337  {
338  return ‪$GLOBALS['LANG'];
339  }
340 
346  protected function ‪getBackendUser(): ‪BackendUserAuthentication
347  {
348  return ‪$GLOBALS['BE_USER'];
349  }
350 }
‪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:50
‪TYPO3\CMS\Backend\Form\FormResultCompiler
Definition: FormResultCompiler.php:30
‪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\init
‪init(ServerRequestInterface $request)
Definition: EditFileController.php:118
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:24
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\process
‪ResponseInterface null process()
Definition: EditFileController.php:156
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: EditFileController.php:84
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\getLanguageService
‪LanguageService getLanguageService()
Definition: EditFileController.php:329
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: EditFileController.php:101
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪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:274
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: EditFileController.php:80
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$returnUrl
‪string $returnUrl
Definition: EditFileController.php:68
‪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:22
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: EditFileController.php:339
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$target
‪string $target
Definition: EditFileController.php:62
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Filelist\Controller\File\EditFileController
Definition: EditFileController.php:45
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$fileObject
‪File $fileObject
Definition: EditFileController.php:74
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\$origTarget
‪string $origTarget
Definition: EditFileController.php:56
‪TYPO3\CMS\Filelist\Controller\File\EditFileController\__construct
‪__construct()
Definition: EditFileController.php:89
‪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