[TASK] Cleanup CSH calls
[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 */
90 protected function init() {
91 // Setting target, which must be a file reference to a file within the mounts.
92 $this->target = ($this->origTarget = ($fileIdentifier = GeneralUtility::_GP('target')));
93 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
94 // create the file object
95 if ($fileIdentifier) {
96 $this->fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileIdentifier);
97 }
98 // Cleaning and checking target directory
99 if (!$this->fileObject) {
100 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
101 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
102 throw new \RuntimeException($title . ': ' . $message, 1294586841);
103 }
104 if ($this->fileObject->getStorage()->getUid() === 0) {
105 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889832);
106 }
107
108 // Setting the title and the icon
109 $icon = IconUtility::getSpriteIcon('apps-filetree-root');
110 $this->title = $icon . htmlspecialchars($this->fileObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileObject->getIdentifier());
111
112 // Setting template object
113 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
114 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_edit.html');
115 $this->doc->backPath = $GLOBALS['BACK_PATH'];
116 $this->doc->JScode = $this->doc->wrapScriptTags('
117 function backToList() { //
118 top.goToModule("file_list");
119 }
120 ');
121 $this->doc->form = '<form action="tce_file.php" method="post" name="editform">';
122 }
123
124 /**
125 * Main function, redering the actual content of the editing page
126 *
127 * @return void
128 */
129 public function main() {
130 $docHeaderButtons = $this->getButtons();
131 $this->content = $this->doc->startPage($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle'));
132 // Hook before compiling the output
133 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'])) {
134 $preOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'];
135 if (is_array($preOutputProcessingHook)) {
136 $hookParameters = array(
137 'content' => &$this->content,
138 'target' => &$this->target
139 );
140 foreach ($preOutputProcessingHook as $hookFunction) {
141 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
142 }
143 }
144 }
145 $pageContent = $this->doc->header($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle') . ' ' . htmlspecialchars($this->fileObject->getName()));
146 $pageContent .= $this->doc->spacer(2);
147 $code = '';
148 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
149 try {
150 if (!$extList || !GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
151 throw new \Exception('Files with that extension are not editable.');
152 }
153 // Read file content to edit:
154 $fileContent = $this->fileObject->getContents();
155 // Making the formfields
156 $hValue = BackendUtility::getModuleUrl('file_edit', array(
157 'target' => $this->origTarget,
158 'returnUrl' => $this->returnUrl
159 ));
160 // Edit textarea:
161 $code .= '
162 <div id="c-edit">
163 <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>
164 <input type="hidden" name="file[editfile][0][target]" value="' . $this->fileObject->getUid() . '" />
165 <input type="hidden" name="redirect" value="' . htmlspecialchars($hValue) . '" />
166 ' . \TYPO3\CMS\Backend\Form\FormEngine::getHiddenTokenField('tceAction') . '
167 </div>
168 <br />';
169 // Make shortcut:
170 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
171 $docHeaderButtons['shortcut'] = $this->doc->makeShortcutIcon('target', '', 'file_edit', 1);
172 } else {
173 $docHeaderButtons['shortcut'] = '';
174 }
175 } catch (\Exception $e) {
176 $code .= sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.coundNot'), $extList);
177 }
178 // Ending of section and outputting editing form:
179 $pageContent .= $this->doc->sectionEnd();
180 $pageContent .= $code;
181 // Hook after compiling the output
182 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'])) {
183 $postOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'];
184 if (is_array($postOutputProcessingHook)) {
185 $hookParameters = array(
186 'pageContent' => &$pageContent,
187 'target' => &$this->target
188 );
189 foreach ($postOutputProcessingHook as $hookFunction) {
190 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
191 }
192 }
193 }
194 // Add the HTML as a section:
195 $markerArray = array(
196 'CSH' => $docHeaderButtons['csh'],
197 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
198 'BUTTONS' => $docHeaderButtons,
199 'PATH' => $this->title,
200 'CONTENT' => $pageContent
201 );
202 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
203 $this->content .= $this->doc->endPage();
204 $this->content = $this->doc->insertStylesAndJS($this->content);
205 }
206
207 /**
208 * Outputting the accumulated content to screen
209 *
210 * @return void
211 */
212 public function printContent() {
213 echo $this->content;
214 }
215
216 /**
217 * Builds the buttons for the docheader and returns them as an array
218 *
219 * @return array
220 */
221 public function getButtons() {
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="' . $GLOBALS['LANG']->makeEntities($GLOBALS['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($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>';
231 // Cancel button
232 $theIcon = IconUtility::getSpriteIcon('actions-document-close');
233 $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>';
234 return $buttons;
235 }
236
237 }