6321bf22645757db6c84e553e06fd5be9bce258b
[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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use Psr\Http\Message\ResponseInterface;
23 use Psr\Http\Message\ServerRequestInterface;
24 use TYPO3\CMS\Core\Http\Response;
25
26 /**
27 * Script Class for rendering the file editing screen
28 */
29 class EditFileController implements \TYPO3\CMS\Core\Http\ControllerInterface {
30
31 /**
32 * Module content accumulated.
33 *
34 * @var string
35 */
36 public $content;
37
38 /**
39 * @var string
40 */
41 public $title;
42
43 /**
44 * Document template object
45 *
46 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
47 */
48 public $doc;
49
50 /**
51 * Original input target
52 *
53 * @var string
54 */
55 public $origTarget;
56
57 /**
58 * The original target, but validated.
59 *
60 * @var string
61 */
62 public $target;
63
64 /**
65 * Return URL of list module.
66 *
67 * @var string
68 */
69 public $returnUrl;
70
71 /**
72 * the file that is being edited on
73 *
74 * @var \TYPO3\CMS\Core\Resource\AbstractFile
75 */
76 protected $fileObject;
77
78 /**
79 * @var IconFactory
80 */
81 protected $iconFactory;
82
83 /**
84 * Constructor
85 */
86 public function __construct() {
87 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
88 $GLOBALS['SOBE'] = $this;
89 $this->init();
90 }
91
92 /**
93 * Initialize script class
94 *
95 * @return void
96 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
97 */
98 protected function init() {
99 // Setting target, which must be a file reference to a file within the mounts.
100 $this->target = ($this->origTarget = ($fileIdentifier = GeneralUtility::_GP('target')));
101 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
102 // create the file object
103 if ($fileIdentifier) {
104 $this->fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileIdentifier);
105 }
106 // Cleaning and checking target directory
107 if (!$this->fileObject) {
108 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
109 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
110 throw new \RuntimeException($title . ': ' . $message, 1294586841);
111 }
112 if ($this->fileObject->getStorage()->getUid() === 0) {
113 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889832);
114 }
115
116 // Setting the title and the icon
117 $icon = IconUtility::getSpriteIcon('apps-filetree-root');
118 $this->title = $icon . htmlspecialchars($this->fileObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileObject->getIdentifier());
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 function backToList() {
125 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 * Main function, redering the actual content of the editing page
133 *
134 * @return void
135 */
136 public function main() {
137 $docHeaderButtons = $this->getButtons();
138 $this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle'));
139 // Hook before compiling the output
140 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 if (is_array($preOutputProcessingHook)) {
143 $hookParameters = array(
144 'content' => &$this->content,
145 'target' => &$this->target
146 );
147 foreach ($preOutputProcessingHook as $hookFunction) {
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 $pageContent .= $this->doc->spacer(2);
154 $code = '';
155 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
156 try {
157 if (!$extList || !GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
158 throw new \Exception('Files with that extension are not editable.');
159 }
160 // Read file content to edit:
161 $fileContent = $this->fileObject->getContents();
162 // Making the formfields
163 $hValue = BackendUtility::getModuleUrl('file_edit', array(
164 'target' => $this->origTarget,
165 'returnUrl' => $this->returnUrl
166 ));
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 <input type="hidden" name="file[editfile][0][target]" value="' . $this->fileObject->getUid() . '" />
172 <input type="hidden" name="redirect" value="' . htmlspecialchars($hValue) . '" />
173 </div>
174 <br />';
175 // Make shortcut:
176 if ($this->getBackendUser()->mayMakeShortcut()) {
177 $docHeaderButtons['shortcut'] = $this->doc->makeShortcutIcon('target', '', 'file_edit', 1);
178 } else {
179 $docHeaderButtons['shortcut'] = '';
180 }
181 } catch (\Exception $e) {
182 $code .= sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.coundNot'), $extList);
183 }
184 // Ending of section and outputting editing form:
185 $pageContent .= $this->doc->sectionEnd();
186 $pageContent .= $code;
187 // Hook after compiling the output
188 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 if (is_array($postOutputProcessingHook)) {
191 $hookParameters = array(
192 'pageContent' => &$pageContent,
193 'target' => &$this->target
194 );
195 foreach ($postOutputProcessingHook as $hookFunction) {
196 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
197 }
198 }
199 }
200 // Add the HTML as a section:
201 $markerArray = array(
202 'CSH' => $docHeaderButtons['csh'],
203 'FUNC_MENU' => '',
204 'BUTTONS' => $docHeaderButtons,
205 'PATH' => $this->title,
206 'CONTENT' => $pageContent
207 );
208 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
209 $this->content .= $this->doc->endPage();
210 $this->content = $this->doc->insertStylesAndJS($this->content);
211 }
212
213 /**
214 * Processes the request, currently everything is handled and put together via "main()"
215 *
216 * @param ServerRequestInterface $request The request object
217 * @return ResponseInterface $response The response, created by the controller
218 */
219 public function processRequest(ServerRequestInterface $request) {
220 $this->main();
221 /** @var Response $response */
222 $response = GeneralUtility::makeInstance(Response::class);
223 $response->getBody()->write($this->content);
224 return $response;
225 }
226
227 /**
228 * Outputting the accumulated content to screen
229 *
230 * @return void
231 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the processRequest() method instead
232 */
233 public function printContent() {
234 GeneralUtility::logDeprecatedFunction();
235 echo $this->content;
236 }
237
238 /**
239 * Builds the buttons for the docheader and returns them as an array
240 *
241 * @return array
242 */
243 public function getButtons() {
244 $lang = $this->getLanguageService();
245 $buttons = array();
246 // CSH button
247 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_edit');
248 // Save button
249 $theIcon = $this->iconFactory->getIcon('actions-document-save', Icon::SIZE_SMALL);
250 $buttons['SAVE'] = '<a href="#" 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);
253 $buttons['SAVE_CLOSE'] = '<a href="#" 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);
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 return $buttons;
258 }
259
260 /**
261 * Returns LanguageService
262 *
263 * @return \TYPO3\CMS\Lang\LanguageService
264 */
265 protected function getLanguageService() {
266 return $GLOBALS['LANG'];
267 }
268
269 /**
270 * Returns the current BE user.
271 *
272 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
273 */
274 protected function getBackendUser() {
275 return $GLOBALS['BE_USER'];
276 }
277
278 }