TYPO3 CMS  TYPO3_8-7
EditFileController.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
28 
33 {
39  public $content;
40 
44  public $title;
45 
51  public $doc;
52 
58  public $origTarget;
59 
65  public $target;
66 
72  public $returnUrl;
73 
79  protected $fileObject;
80 
84  public function __construct()
85  {
86  parent::__construct();
87  $GLOBALS['SOBE'] = $this;
88  $this->init();
89  }
90 
98  protected function init()
99  {
100  // Setting target, which must be a file reference to a file within the mounts.
101  $this->target = ($this->origTarget = ($fileIdentifier = GeneralUtility::_GP('target')));
102  $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
103  // create the file object
104  if ($fileIdentifier) {
105  $this->fileObject = ResourceFactory::getInstance()
106  ->retrieveFileOrFolderObject($fileIdentifier);
107  }
108  // Cleaning and checking target directory
109  if (!$this->fileObject) {
110  $title = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
111  $message = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
112  throw new \RuntimeException($title . ': ' . $message, 1294586841);
113  }
114  if ($this->fileObject->getStorage()->getUid() === 0) {
116  'You are not allowed to access files outside your storages',
117  1375889832
118  );
119  }
120 
121  // Setting the title and the icon
122  $icon = $this->moduleTemplate->getIconFactory()->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
123  $this->title = $icon
124  . htmlspecialchars(
125  $this->fileObject->getStorage()->getName()
126  ) . ': ' . htmlspecialchars(
127  $this->fileObject->getIdentifier()
128  );
129 
130  // Setting template object
131  $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
132  $this->moduleTemplate->addJavaScriptCode(
133  'FileEditBackToList',
134  'function backToList() {
135  top.goToModule("file_FilelistList");
136  }'
137  );
138  }
139 
143  public function main()
144  {
145  $this->getButtons();
146  // Hook: before compiling the output
147  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'])) {
148  $preOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'];
149  if (is_array($preOutputProcessingHook)) {
150  $hookParameters = [
151  'content' => &$this->content,
152  'target' => &$this->target
153  ];
154  foreach ($preOutputProcessingHook as $hookFunction) {
155  GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
156  }
157  }
158  }
159 
160  $assigns = [];
161  $assigns['moduleUrlTceFile'] = BackendUtility::getModuleUrl('tce_file');
162  $assigns['fileName'] = $this->fileObject->getName();
163 
164  $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
165  try {
166  if (!$extList || !GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
167  throw new \Exception('Files with that extension are not editable.', 1476050135);
168  }
169 
170  // Read file content to edit:
171  $fileContent = $this->fileObject->getContents();
172 
173  // Making the formfields
174  $hValue = BackendUtility::getModuleUrl('file_edit', [
175  'target' => $this->origTarget,
176  'returnUrl' => $this->returnUrl
177  ]);
178  $assigns['uid'] = $this->fileObject->getUid();
179  $assigns['fileContent'] = $fileContent;
180  $assigns['hValue'] = $hValue;
181  } catch (\Exception $e) {
182  $assigns['extList'] = $extList;
183  }
184 
185  // Rendering of the output via fluid
186  $view = GeneralUtility::makeInstance(StandaloneView::class);
187  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
188  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
189  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
190  'EXT:backend/Resources/Private/Templates/File/EditFile.html'
191  ));
192  $view->assignMultiple($assigns);
193  $pageContent = $view->render();
194 
195  // Hook: after compiling the output
196  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'])) {
197  $postOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'];
198  if (is_array($postOutputProcessingHook)) {
199  $hookParameters = [
200  'pageContent' => &$pageContent,
201  'target' => &$this->target
202  ];
203  foreach ($postOutputProcessingHook as $hookFunction) {
204  GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
205  }
206  }
207  }
208 
209  $this->content .= $pageContent;
210  $this->moduleTemplate->setContent($this->content);
211  }
212 
220  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
221  {
222  $this->main();
223 
224  $response->getBody()->write($this->moduleTemplate->renderContent());
225  return $response;
226  }
227 
233  public function getButtons()
234  {
235  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
236 
237  $lang = $this->getLanguageService();
238  // CSH button
239  $helpButton = $buttonBar->makeHelpButton()
240  ->setFieldName('file_edit')
241  ->setModuleName('xMOD_csh_corebe');
242  $buttonBar->addButton($helpButton);
243 
244  // Save button
245  $saveButton = $buttonBar->makeInputButton()
246  ->setName('_save')
247  ->setValue('1')
248  ->setOnClick('document.editform.submit();')
249  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_edit.php.submit'))
250  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
251 
252  // Save and Close button
253  $saveAndCloseButton = $buttonBar->makeInputButton()
254  ->setName('_saveandclose')
255  ->setValue('1')
256  ->setOnClick(
257  'document.editform.redirect.value='
258  . GeneralUtility::quoteJSvalue($this->returnUrl)
259  . '; document.editform.submit();'
260  )
261  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_edit.php.saveAndClose'))
262  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
263  'actions-document-save-close',
265  ));
266 
267  $splitButton = $buttonBar->makeSplitButton()
268  ->addItem($saveButton)
269  ->addItem($saveAndCloseButton);
270  $buttonBar->addButton($splitButton, ButtonBar::BUTTON_POSITION_LEFT, 20);
271 
272  // Cancel button
273  $closeButton = $buttonBar->makeLinkButton()
274  ->setHref('#')
275  ->setOnClick('backToList(); return false;')
276  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.cancel'))
277  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-close', Icon::SIZE_SMALL));
278  $buttonBar->addButton($closeButton, ButtonBar::BUTTON_POSITION_LEFT, 10);
279 
280  // Make shortcut:
281  $shortButton = $buttonBar->makeShortcutButton()
282  ->setModuleName('file_edit')
283  ->setGetVariables(['target']);
284  $buttonBar->addButton($shortButton);
285  }
286 
292  protected function getLanguageService()
293  {
294  return $GLOBALS['LANG'];
295  }
296 
302  protected function getBackendUser()
303  {
304  return $GLOBALS['BE_USER'];
305  }
306 }
static callUserFunction($funcName, &$params, &$ref, $_='', $errorMode=0)
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)
mainAction(ServerRequestInterface $request, ResponseInterface $response)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']