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