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