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