a178291fc772eaf77ac1eaf5f7c6cb848d31b77e
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / EditFileController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\File;
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
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Form\FormResultCompiler;
20 use TYPO3\CMS\Backend\Form\NodeFactory;
21 use TYPO3\CMS\Backend\Module\AbstractModule;
22 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
23 use TYPO3\CMS\Backend\Template\DocumentTemplate;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
25 use TYPO3\CMS\Core\Imaging\Icon;
26 use TYPO3\CMS\Core\Imaging\IconFactory;
27 use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
28 use TYPO3\CMS\Core\Resource\ResourceFactory;
29 use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31 /**
32 * Script Class for rendering the file editing screen
33 */
34 class EditFileController extends AbstractModule
35 {
36 /**
37 * Module content accumulated.
38 *
39 * @var string
40 */
41 public $content;
42
43 /**
44 * @var string
45 */
46 public $title;
47
48 /**
49 * Document template object
50 *
51 * @var DocumentTemplate
52 */
53 public $doc;
54
55 /**
56 * Original input target
57 *
58 * @var string
59 */
60 public $origTarget;
61
62 /**
63 * The original target, but validated.
64 *
65 * @var string
66 */
67 public $target;
68
69 /**
70 * Return URL of list module.
71 *
72 * @var string
73 */
74 public $returnUrl;
75
76 /**
77 * the file that is being edited on
78 *
79 * @var \TYPO3\CMS\Core\Resource\AbstractFile
80 */
81 protected $fileObject;
82
83 /**
84 * Constructor
85 */
86 public function __construct()
87 {
88 parent::__construct();
89 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
90 $GLOBALS['SOBE'] = $this;
91 $this->init();
92 }
93
94 /**
95 * Initialize script class
96 *
97 * @throws InsufficientFileAccessPermissionsException
98 * @throws \InvalidArgumentException
99 * @throws \RuntimeException
100 */
101 protected function init()
102 {
103 // Setting target, which must be a file reference to a file within the mounts.
104 $this->target = ($this->origTarget = ($fileIdentifier = GeneralUtility::_GP('target')));
105 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
106 // create the file object
107 if ($fileIdentifier) {
108 $this->fileObject = ResourceFactory::getInstance()
109 ->retrieveFileOrFolderObject($fileIdentifier);
110 }
111 // Cleaning and checking target directory
112 if (!$this->fileObject) {
113 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError'));
114 $message = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir'));
115 throw new \RuntimeException($title . ': ' . $message, 1294586841);
116 }
117 if ($this->fileObject->getStorage()->getUid() === 0) {
118 throw new InsufficientFileAccessPermissionsException(
119 'You are not allowed to access files outside your storages',
120 1375889832
121 );
122 }
123
124 // Setting the title and the icon
125 $icon = $this->iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
126 $this->title = $icon
127 . htmlspecialchars(
128 $this->fileObject->getStorage()->getName()
129 ) . ': ' . htmlspecialchars(
130 $this->fileObject->getIdentifier()
131 );
132
133 // Setting template object
134 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
135 $this->moduleTemplate->addJavaScriptCode(
136 'FileEditBackToList',
137 'function backToList() {
138 top.goToModule("file_FilelistList");
139 }'
140 );
141 }
142
143 /**
144 * Main function, redering the actual content of the editing page
145 *
146 * @return void
147 */
148 public function main()
149 {
150 $this->getButtons();
151
152 $dataColumnDefinition = [
153 'label' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_common.xlf:file'))
154 . ' ' . htmlspecialchars($this->target),
155 'config' => [
156 'type' => 'text',
157 'cols' => 48,
158 'wrap' => 'OFF',
159 'softref' => 'email[subst],url[subst]'
160 ],
161 'defaultExtras' => 'fixed-font: enable-tab'
162 ];
163
164 // Hook before compiling the output
165 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'])) {
166 $preOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'];
167 if (is_array($preOutputProcessingHook)) {
168 $hookParameters = [
169 'content' => &$this->content,
170 'target' => &$this->target,
171 'dataColumnDefinition' => &$dataColumnDefinition
172 ];
173 foreach ($preOutputProcessingHook as $hookFunction) {
174 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
175 }
176 }
177 }
178
179 $pageContent = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" id="EditFileController" name="editform">';
180 $pageContent .= '<h1>'
181 . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle')
182 . ' ' . htmlspecialchars($this->fileObject->getName()) . '</h1>';
183
184 $code = '';
185 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
186 try {
187 if (!$extList || !GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
188 throw new \Exception('Files with that extension are not editable.');
189 }
190
191 // Read file content to edit:
192 $fileContent = $this->fileObject->getContents();
193
194 // Making the formfields
195 $hValue = BackendUtility::getModuleUrl('file_edit', [
196 'target' => $this->origTarget,
197 'returnUrl' => $this->returnUrl
198 ]);
199 $formData = [
200 'databaseRow' => [
201 'uid' => 0,
202 'data' => $fileContent,
203 'target' => $this->fileObject->getUid(),
204 'redirect' => $hValue
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 $code .= $formResultCompiler->JStop()
239 . $formResultCompiler->printNeededJSFunctions()
240 . $resultArray['html'];
241 } catch (\Exception $e) {
242 $code .= sprintf(
243 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.coundNot'),
244 $extList
245 );
246 }
247
248 // Ending of section and outputting editing form:
249 $pageContent .= $code;
250
251 // Hook after compiling the output
252 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'])) {
253 $postOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'];
254 if (is_array($postOutputProcessingHook)) {
255 $hookParameters = [
256 'pageContent' => &$pageContent,
257 'target' => &$this->target
258 ];
259 foreach ($postOutputProcessingHook as $hookFunction) {
260 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
261 }
262 }
263 }
264 $pageContent .= '</form>';
265 $this->content = $pageContent;
266
267 $this->moduleTemplate->setContent($this->content);
268 }
269
270 /**
271 * Processes the request, currently everything is handled and put together via "main()"
272 *
273 * @param ServerRequestInterface $request the current request
274 * @param ResponseInterface $response
275 * @return ResponseInterface the response with the content
276 */
277 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
278 {
279 $this->main();
280
281 $response->getBody()->write($this->moduleTemplate->renderContent());
282 return $response;
283 }
284
285 /**
286 * Builds the buttons for the docheader and returns them as an array
287 *
288 * @return array
289 */
290 public function getButtons()
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 ->setOnClick('document.editform.submit();')
306 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.submit'))
307 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
308
309 // Save and Close button
310 $saveAndCloseButton = $buttonBar->makeInputButton()
311 ->setName('_saveandclose')
312 ->setValue('1')
313 ->setOnClick(
314 'document.editform.redirect.value='
315 . GeneralUtility::quoteJSvalue($this->returnUrl)
316 . '; document.editform.submit();'
317 )
318 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.saveAndClose'))
319 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
320 'actions-document-save-close',
321 Icon::SIZE_SMALL
322 ));
323
324 $splitButton = $buttonBar->makeSplitButton()
325 ->addItem($saveButton)
326 ->addItem($saveAndCloseButton);
327 $buttonBar->addButton($splitButton, ButtonBar::BUTTON_POSITION_LEFT, 20);
328
329 // Cancel button
330 $closeButton = $buttonBar->makeLinkButton()
331 ->setHref('#')
332 ->setOnClick('backToList(); return false;')
333 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel'))
334 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-close', Icon::SIZE_SMALL));
335 $buttonBar->addButton($closeButton, ButtonBar::BUTTON_POSITION_LEFT, 10);
336
337 // Make shortcut:
338 $shortButton = $buttonBar->makeShortcutButton()
339 ->setModuleName('file_edit')
340 ->setGetVariables(['target']);
341 $buttonBar->addButton($shortButton);
342 }
343
344 /**
345 * Returns LanguageService
346 *
347 * @return \TYPO3\CMS\Lang\LanguageService
348 */
349 protected function getLanguageService()
350 {
351 return $GLOBALS['LANG'];
352 }
353
354 /**
355 * Returns the current BE user.
356 *
357 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
358 */
359 protected function getBackendUser()
360 {
361 return $GLOBALS['BE_USER'];
362 }
363 }