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