[TASK] Call explicit render() on icon objects
[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\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23
24 /**
25 * Script Class for rendering the file editing screen
26 */
27 class EditFileController {
28
29 /**
30 * Module content accumulated.
31 *
32 * @var string
33 */
34 public $content;
35
36 /**
37 * @var string
38 */
39 public $title;
40
41 /**
42 * Document template object
43 *
44 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
45 */
46 public $doc;
47
48 /**
49 * Original input target
50 *
51 * @var string
52 */
53 public $origTarget;
54
55 /**
56 * The original target, but validated.
57 *
58 * @var string
59 */
60 public $target;
61
62 /**
63 * Return URL of list module.
64 *
65 * @var string
66 */
67 public $returnUrl;
68
69 /**
70 * the file that is being edited on
71 *
72 * @var \TYPO3\CMS\Core\Resource\AbstractFile
73 */
74 protected $fileObject;
75
76 /**
77 * @var IconFactory
78 */
79 protected $iconFactory;
80
81 /**
82 * Constructor
83 */
84 public function __construct() {
85 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
86 $GLOBALS['SOBE'] = $this;
87 $this->init();
88 }
89
90 /**
91 * Initialize script class
92 *
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.
98 $this->target = ($this->origTarget = ($fileIdentifier = GeneralUtility::_GP('target')));
99 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
100 // create the file object
101 if ($fileIdentifier) {
102 $this->fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileIdentifier);
103 }
104 // Cleaning and checking target directory
105 if (!$this->fileObject) {
106 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
107 $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 /** @var IconFactory $iconFactory */
116 $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());
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 current request
217 * @param ResponseInterface $response
218 * @return ResponseInterface the response with the content
219 */
220 public function mainAction(ServerRequestInterface $request, ResponseInterface $response) {
221 $this->main();
222
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 mainAction() 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)->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 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 }