[FEATURE] Introduce .typoscript file extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / T3editor.php
1 <?php
2 namespace TYPO3\CMS\T3editor;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Http\Response;
20 use TYPO3\CMS\Core\Page\PageRenderer;
21 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\PathUtility;
24
25 /**
26 * Provides a javascript-driven code editor with syntax highlighting for TS, HTML, CSS and more
27 */
28 class T3editor implements \TYPO3\CMS\Core\SingletonInterface
29 {
30 const MODE_TYPOSCRIPT = 'typoscript';
31 const MODE_JAVASCRIPT = 'javascript';
32 const MODE_CSS = 'css';
33 const MODE_XML = 'xml';
34 const MODE_HTML = 'html';
35 const MODE_PHP = 'php';
36 const MODE_SPARQL = 'sparql';
37 const MODE_MIXED = 'mixed';
38
39 /**
40 * @var string
41 */
42 protected $mode = '';
43
44 /**
45 * @var string
46 */
47 protected $ajaxSaveType = '';
48
49 /**
50 * Counts the editors on the current page
51 *
52 * @var int
53 */
54 protected $editorCounter = 0;
55
56 /**
57 * Path to EXT:t3editor
58 *
59 * @var string
60 */
61 protected $extPath = '';
62
63 /**
64 * Relative directory to codemirror
65 *
66 * @var string
67 */
68 protected $codemirrorPath = 'Resources/Public/JavaScript/Contrib/codemirror/js/';
69
70 /**
71 * RequireJS modules loaded for code completion
72 *
73 * @var array
74 */
75 protected $codeCompletionComponents = ['TsRef', 'CompletionResult', 'TsParser', 'TsCodeCompletion'];
76
77 /**
78 * sets the type of code to edit (::MODE_TYPOSCRIPT, ::MODE_JAVASCRIPT)
79 *
80 * @param $mode string Expects one of the predefined constants
81 * @return \TYPO3\CMS\T3editor\T3editor
82 */
83 public function setMode($mode)
84 {
85 $this->mode = $mode;
86 return $this;
87 }
88
89 /**
90 * Set the AJAX save type
91 *
92 * @param string $ajaxSaveType
93 * @return \TYPO3\CMS\T3editor\T3editor
94 */
95 public function setAjaxSaveType($ajaxSaveType)
96 {
97 $this->ajaxSaveType = $ajaxSaveType;
98 return $this;
99 }
100
101 /**
102 * Set mode by file
103 *
104 * @param string $file
105 */
106 public function setModeByFile($file)
107 {
108 $fileInfo = GeneralUtility::split_fileref($file);
109 $this->setModeByType($fileInfo['fileext']);
110 }
111
112 /**
113 * Set mode by type
114 *
115 * @param string $type
116 */
117 public function setModeByType($type)
118 {
119 switch ($type) {
120 case 'html':
121 case 'htm':
122 case 'tmpl':
123 $mode = self::MODE_HTML;
124 break;
125 case 'js':
126 $mode = self::MODE_JAVASCRIPT;
127 break;
128 case 'xml':
129 case 'svg':
130 $mode = self::MODE_XML;
131 break;
132 case 'css':
133 $mode = self::MODE_CSS;
134 break;
135 case 'ts':
136 case 'typoscript':
137 $mode = self::MODE_TYPOSCRIPT;
138 break;
139 case 'sparql':
140 $mode = self::MODE_SPARQL;
141 break;
142 case 'php':
143 case 'phpsh':
144 case 'inc':
145 $mode = self::MODE_PHP;
146 break;
147 default:
148 $mode = self::MODE_MIXED;
149 }
150 $this->setMode($mode);
151 }
152
153 /**
154 * Get mode
155 *
156 * @return string
157 */
158 public function getMode()
159 {
160 return $this->mode;
161 }
162
163 /**
164 * Creates a new instance of the class
165 */
166 public function __construct()
167 {
168 $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
169 // Disable pmktextarea to avoid conflicts (thanks Peter Klein for this suggestion)
170 $GLOBALS['BE_USER']->uc['disablePMKTextarea'] = 1;
171
172 $this->extPath = PathUtility::getAbsoluteWebPath(ExtensionManagementUtility::extPath('t3editor'));
173 $this->codemirrorPath = $this->extPath . $this->codemirrorPath;
174 }
175
176 /**
177 * Retrieves JavaScript code (header part) for editor
178 *
179 * @return string
180 */
181 public function getJavascriptCode()
182 {
183 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
184 $pageRenderer = $this->getPageRenderer();
185 $pageRenderer->addCssFile($this->extPath . 'Resources/Public/Css/t3editor.css');
186 // Include editor-js-lib
187 $pageRenderer->addJsLibrary('codemirror', $this->codemirrorPath . 'codemirror.js');
188 if ($this->mode === self::MODE_TYPOSCRIPT) {
189 foreach ($this->codeCompletionComponents as $codeCompletionComponent) {
190 $pageRenderer->loadRequireJsModule('TYPO3/CMS/T3editor/Plugins/CodeCompletion/' . $codeCompletionComponent);
191 }
192 }
193 $pageRenderer->loadRequireJsModule('TYPO3/CMS/T3editor/T3editor');
194 return '';
195 }
196
197 /**
198 * Get the template code, prepared for javascript (no line breaks, quoted in single quotes)
199 *
200 * @return string The template code, prepared to use in javascript
201 */
202 protected function getPreparedTemplate()
203 {
204 $T3editor_template = file_get_contents(
205 GeneralUtility::getFileAbsFileName('EXT:t3editor/Resources/Private/Templates/t3editor.html')
206 );
207 return str_replace([CR, LF], '', $T3editor_template);
208 }
209
210 /**
211 * Determine the correct parser js file for given mode
212 *
213 * @param string $mode
214 * @return string Parser file name
215 */
216 protected function getParserfileByMode($mode)
217 {
218 switch ($mode) {
219 case self::MODE_TYPOSCRIPT:
220 $relPath = '../../../parse_typoscript/';
221 $parserfile = [$relPath . 'tokenizetyposcript.js', $relPath . 'parsetyposcript.js'];
222 break;
223 case self::MODE_JAVASCRIPT:
224 $parserfile = ['tokenizejavascript.js', 'parsejavascript.js'];
225 break;
226 case self::MODE_CSS:
227 $parserfile = ['parsecss.js'];
228 break;
229 case self::MODE_XML:
230 $parserfile = ['parsexml.js'];
231 break;
232 case self::MODE_SPARQL:
233 $parserfile = ['parsesparql.js'];
234 break;
235 case self::MODE_HTML:
236 $parserfile = ['tokenizejavascript.js', 'parsejavascript.js', 'parsecss.js', 'parsexml.js', 'parsehtmlmixed.js'];
237 break;
238 case self::MODE_PHP:
239 case self::MODE_MIXED:
240 $parserfile = ['tokenizejavascript.js', 'parsejavascript.js', 'parsecss.js', 'parsexml.js', '../contrib/php/js/tokenizephp.js', '../contrib/php/js/parsephp.js', '../contrib/php/js/parsephphtmlmixed.js'];
241 break;
242 default:
243 $parserfile = [];
244 }
245 return json_encode($parserfile);
246 }
247
248 /**
249 * Determine the correct css file for given mode
250 *
251 * @param string $mode
252 * @return string css file name
253 */
254 protected function getStylesheetByMode($mode)
255 {
256 switch ($mode) {
257 case self::MODE_TYPOSCRIPT:
258 $stylesheet = [$this->extPath . 'Resources/Public/Css/t3editor_typoscript_colors.css'];
259 break;
260 case self::MODE_JAVASCRIPT:
261 $stylesheet = [$this->codemirrorPath . '../css/jscolors.css'];
262 break;
263 case self::MODE_CSS:
264 $stylesheet = [$this->codemirrorPath . '../css/csscolors.css'];
265 break;
266 case self::MODE_XML:
267 $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css'];
268 break;
269 case self::MODE_HTML:
270 $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css', $this->codemirrorPath . '../css/jscolors.css', $this->codemirrorPath . '../css/csscolors.css'];
271 break;
272 case self::MODE_SPARQL:
273 $stylesheet = [$this->codemirrorPath . '../css/sparqlcolors.css'];
274 break;
275 case self::MODE_PHP:
276 $stylesheet = [$this->codemirrorPath . '../contrib/php/css/phpcolors.css'];
277 break;
278 case self::MODE_MIXED:
279 $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css', $this->codemirrorPath . '../css/jscolors.css', $this->codemirrorPath . '../css/csscolors.css', $this->codemirrorPath . '../contrib/php/css/phpcolors.css'];
280 break;
281 default:
282 $stylesheet = [];
283 }
284 $stylesheet[] = $this->extPath . 'Resources/Public/Css/t3editor_inner.css';
285 return json_encode($stylesheet);
286 }
287
288 /**
289 * Generates HTML with code editor
290 *
291 * @param string $name Name attribute of HTML tag
292 * @param string $class Class attribute of HTML tag
293 * @param string $content Content of the editor
294 * @param string $additionalParams Any additional editor parameters
295 * @param string $alt Alt attribute
296 * @param array $hiddenfields
297 * @return string Generated HTML code for editor
298 */
299 public function getCodeEditor($name, $class = '', $content = '', $additionalParams = '', $alt = '', array $hiddenfields = [])
300 {
301 $code = '';
302 $class .= ' t3editor';
303 $alt = trim($alt);
304 $code .=
305 '<div class="t3editor">'
306 . '<div class="t3e_wrap">'
307 . $this->getPreparedTemplate()
308 . '</div>'
309 . '<textarea '
310 . 'id="t3editor_' . (int)$this->editorCounter . '" '
311 . 'name="' . htmlspecialchars($name) . '" '
312 . 'class="' . htmlspecialchars($class) . '" '
313 . $additionalParams . ' '
314 . ($alt !== '' ? ' alt="' . htmlspecialchars($alt) . '"' : '')
315 . ' data-labels="' . htmlspecialchars(json_encode($GLOBALS['LANG']->getLabelsWithPrefix('js.', 'label_'))) . '"'
316 . ' data-instance-number="' . (int)$this->editorCounter . '"'
317 . ' data-editor-path="' . htmlspecialchars($this->extPath) . '"'
318 . ' data-codemirror-path="' . htmlspecialchars($this->codemirrorPath) . '"'
319 . ' data-ajaxsavetype="' . htmlspecialchars($this->ajaxSaveType) . '"'
320 . ' data-parserfile="' . htmlspecialchars($this->getParserfileByMode($this->mode)) . '"'
321 . ' data-stylesheet="' . htmlspecialchars($this->getStylesheetByMode($this->mode)) . '"'
322 . '>' . htmlspecialchars($content)
323 . '</textarea>'
324 . '</div>';
325 if (!empty($hiddenfields)) {
326 foreach ($hiddenfields as $name => $value) {
327 $code .= '<input type="hidden" ' . 'name="' . htmlspecialchars($name) . '" ' . 'value="' . htmlspecialchars($value) . '" />';
328 }
329 }
330 $this->editorCounter++;
331 return $code;
332 }
333
334 /**
335 * Save the content from t3editor retrieved via Ajax
336 *
337 * @param ServerRequestInterface $request
338 * @param ResponseInterface $response
339 * @return ResponseInterface
340 */
341 public function ajaxSaveCode(ServerRequestInterface $request, ResponseInterface $response)
342 {
343 // cancel if its not an Ajax request
344 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
345 $codeType = isset($request->getParsedBody()['t3editor_savetype']) ? $request->getParsedBody()['t3editor_savetype'] : $request->getQueryParams()['t3editor_savetype'];
346 $savingsuccess = false;
347 try {
348 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/t3editor/classes/class.tx_t3editor.php']['ajaxSaveCode'])) {
349 $_params = [
350 'pObj' => &$this,
351 'type' => $codeType,
352 'request' => $request,
353 'response' => $response
354 ];
355 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/t3editor/classes/class.tx_t3editor.php']['ajaxSaveCode'] as $key => $_funcRef) {
356 $savingsuccess = GeneralUtility::callUserFunction($_funcRef, $_params, $this) || $savingsuccess;
357 }
358 }
359 $responseContent = ['result' => $savingsuccess];
360 } catch (\Exception $e) {
361 $responseContent = [
362 'result' => false,
363 'exceptionMessage' => htmlspecialchars($e->getMessage()),
364 'exceptionCode' => $e->getCode()
365 ];
366 }
367 /** @var Response $response */
368 $response = GeneralUtility::makeInstance(Response::class);
369 $response->getBody()->write(json_encode($responseContent));
370 }
371
372 return $response;
373 }
374
375 /**
376 * Gets plugins that are defined at $TYPO3_CONF_VARS['EXTCONF']['t3editor']['plugins']
377 * Called by AjaxRequestHandler
378 *
379 * @param ServerRequestInterface $request
380 * @param ResponseInterface $response
381 * @return ResponseInterface
382 */
383 public function getPlugins(ServerRequestInterface $request, ResponseInterface $response)
384 {
385 $result = [];
386 $plugins = &$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3editor']['plugins'];
387 if (is_array($plugins)) {
388 $result = array_values($plugins);
389 }
390 $response->getBody()->write(json_encode($result));
391 return $response;
392 }
393
394 /**
395 * @return PageRenderer
396 */
397 protected function getPageRenderer()
398 {
399 return GeneralUtility::makeInstance(PageRenderer::class);
400 }
401 }