EditFileController.php 9.62 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Backend\Controller\File;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

Nicole Cordes's avatar
Nicole Cordes committed
17
use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
Nicole Cordes's avatar
Nicole Cordes committed
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Nicole Cordes's avatar
Nicole Cordes committed
23

24
25
26
/**
 * Script Class for rendering the file editing screen
 */
27
class EditFileController {
28

29
30
31
32
33
	/**
	 * Module content accumulated.
	 *
	 * @var string
	 */
34
35
	public $content;

36
37
38
	/**
	 * @var string
	 */
39
40
41
42
43
44
45
46
47
	public $title;

	/**
	 * Document template object
	 *
	 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
	 */
	public $doc;

48
49
50
51
52
	/**
	 * Original input target
	 *
	 * @var string
	 */
53
54
	public $origTarget;

55
56
57
58
59
	/**
	 * The original target, but validated.
	 *
	 * @var string
	 */
60
61
	public $target;

62
63
64
65
66
	/**
	 * Return URL of list module.
	 *
	 * @var string
	 */
67
68
69
70
71
72
73
74
75
	public $returnUrl;

	/**
	 * the file that is being edited on
	 *
	 * @var \TYPO3\CMS\Core\Resource\AbstractFile
	 */
	protected $fileObject;

76
77
78
79
80
	/**
	 * @var IconFactory
	 */
	protected $iconFactory;

81
82
83
84
	/**
	 * Constructor
	 */
	public function __construct() {
85
		$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
86
87
88
89
		$GLOBALS['SOBE'] = $this;
		$this->init();
	}

90
91
92
	/**
	 * Initialize script class
	 *
93
	 * @return void
94
	 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
95
	 */
96
	protected function init() {
97
		// Setting target, which must be a file reference to a file within the mounts.
Nicole Cordes's avatar
Nicole Cordes committed
98
99
		$this->target = ($this->origTarget = ($fileIdentifier = GeneralUtility::_GP('target')));
		$this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
100
101
102
103
104
105
		// create the file object
		if ($fileIdentifier) {
			$this->fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileIdentifier);
		}
		// Cleaning and checking target directory
		if (!$this->fileObject) {
106
107
			$title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
			$message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
108
			throw new \RuntimeException($title . ': ' . $message, 1294586841);
109
		}
110
		if ($this->fileObject->getStorage()->getUid() === 0) {
111
			throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889832);
112
113
		}

114
		// Setting the title and the icon
115
116
		/** @var IconFactory $iconFactory */
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
117
		$icon = $iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
118
		$this->title = $icon . htmlspecialchars($this->fileObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileObject->getIdentifier());
Nicole Cordes's avatar
Nicole Cordes committed
119

120
		// Setting template object
121
		$this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
122
		$this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_edit.html');
123
		$this->doc->JScode = $this->doc->wrapScriptTags('
124
125
			function backToList() {
				top.goToModule("file_FilelistList");
126
127
			}
		');
128
		$this->doc->form = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform">';
129
130
131
132
133
134
135
136
137
	}

	/**
	 * Main function, redering the actual content of the editing page
	 *
	 * @return void
	 */
	public function main() {
		$docHeaderButtons = $this->getButtons();
138
		$this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle'));
139
140
		// Hook	before compiling the output
		if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'])) {
141
			$preOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'];
142
143
144
145
146
147
			if (is_array($preOutputProcessingHook)) {
				$hookParameters = array(
					'content' => &$this->content,
					'target' => &$this->target
				);
				foreach ($preOutputProcessingHook as $hookFunction) {
Nicole Cordes's avatar
Nicole Cordes committed
148
					GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
149
150
151
				}
			}
		}
152
		$pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle') . ' ' . htmlspecialchars($this->fileObject->getName()));
153
154
155
		$pageContent .= $this->doc->spacer(2);
		$code = '';
		$extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
156
157
158
159
		try {
			if (!$extList || !GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
				throw new \Exception('Files with that extension are not editable.');
			}
160
161
162
			// Read file content to edit:
			$fileContent = $this->fileObject->getContents();
			// Making the formfields
163
164
165
166
			$hValue = BackendUtility::getModuleUrl('file_edit', array(
				'target' => $this->origTarget,
				'returnUrl' => $this->returnUrl
			));
167
			// Edit textarea:
168
			$code .= '
169
				<div id="c-edit">
170
					<textarea rows="30" name="file[editfile][0][data]" wrap="off" ' . $this->doc->formWidth(48, TRUE, 'width:98%;height:80%') . ' class="text-monospace t3js-enable-tab">' . htmlspecialchars($fileContent) . '</textarea>
171
172
					<input type="hidden" name="file[editfile][0][target]" value="' . $this->fileObject->getUid() . '" />
					<input type="hidden" name="redirect" value="' . htmlspecialchars($hValue) . '" />
173
174
175
				</div>
				<br />';
			// Make shortcut:
176
			if ($this->getBackendUser()->mayMakeShortcut()) {
177
				$docHeaderButtons['shortcut'] = $this->doc->makeShortcutIcon('target', '', 'file_edit', 1);
178
179
180
			} else {
				$docHeaderButtons['shortcut'] = '';
			}
181
		} catch (\Exception $e) {
182
			$code .= sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.coundNot'), $extList);
183
184
185
186
187
188
		}
		// Ending of section and outputting editing form:
		$pageContent .= $this->doc->sectionEnd();
		$pageContent .= $code;
		// Hook	after compiling the output
		if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'])) {
189
			$postOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'];
190
191
192
193
194
195
			if (is_array($postOutputProcessingHook)) {
				$hookParameters = array(
					'pageContent' => &$pageContent,
					'target' => &$this->target
				);
				foreach ($postOutputProcessingHook as $hookFunction) {
Nicole Cordes's avatar
Nicole Cordes committed
196
					GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
197
198
199
200
201
202
				}
			}
		}
		// Add the HTML as a section:
		$markerArray = array(
			'CSH' => $docHeaderButtons['csh'],
203
			'FUNC_MENU' => '',
204
205
206
207
208
209
210
211
212
			'BUTTONS' => $docHeaderButtons,
			'PATH' => $this->title,
			'CONTENT' => $pageContent
		);
		$this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
		$this->content .= $this->doc->endPage();
		$this->content = $this->doc->insertStylesAndJS($this->content);
	}

213
214
215
	/**
	 * Processes the request, currently everything is handled and put together via "main()"
	 *
216
217
218
	 * @param ServerRequestInterface $request the current request
	 * @param ResponseInterface $response
	 * @return ResponseInterface the response with the content
219
	 */
220
	public function mainAction(ServerRequestInterface $request, ResponseInterface $response) {
221
		$this->main();
222

223
224
225
226
		$response->getBody()->write($this->content);
		return $response;
	}

227
228
229
230
	/**
	 * Outputting the accumulated content to screen
	 *
	 * @return void
231
	 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the mainAction() method instead
232
233
	 */
	public function printContent() {
234
		GeneralUtility::logDeprecatedFunction();
235
236
237
238
239
240
241
242
243
		echo $this->content;
	}

	/**
	 * Builds the buttons for the docheader and returns them as an array
	 *
	 * @return array
	 */
	public function getButtons() {
244
		$lang = $this->getLanguageService();
245
246
		$buttons = array();
		// CSH button
Benni Mack's avatar
Benni Mack committed
247
		$buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_edit');
248
		// Save button
249
		$theIcon = $this->iconFactory->getIcon('actions-document-save', Icon::SIZE_SMALL)->render();
250
		$buttons['SAVE'] = '<a href="#" class="t3js-fileedit-save" onclick="document.editform.submit();" title="' . $lang->makeEntities($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.submit', TRUE)) . '">' . $theIcon . '</a>';
251
		// Save and Close button
252
		$theIcon = $this->iconFactory->getIcon('actions-document-save-close', Icon::SIZE_SMALL)->render();
253
		$buttons['SAVE_CLOSE'] = '<a href="#" class="t3js-fileedit-save-close" onclick="document.editform.redirect.value=' . htmlspecialchars(GeneralUtility::quoteJSvalue($this->returnUrl)) . '; document.editform.submit();" title="' . $lang->makeEntities($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.saveAndClose', TRUE)) . '">' . $theIcon . '</a>';
254
		// Cancel button
255
		$theIcon = $this->iconFactory->getIcon('actions-document-close', Icon::SIZE_SMALL)->render();
256
		$buttons['CANCEL'] = '<a href="#" onclick="backToList(); return false;" title="' . $lang->makeEntities($lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', TRUE)) . '">' . $theIcon . '</a>';
257
258
259
		return $buttons;
	}

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
	/**
	 * Returns LanguageService
	 *
	 * @return \TYPO3\CMS\Lang\LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

	/**
	 * Returns the current BE user.
	 *
	 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
	 */
	protected function getBackendUser() {
		return $GLOBALS['BE_USER'];
	}

278
}