TYPO3 CMS  TYPO3_8-7
T3editor.php
Go to the documentation of this file.
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 
24 
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 
42  protected $mode = '';
43 
47  protected $ajaxSaveType = '';
48 
54  protected $editorCounter = 0;
55 
61  protected $extPath = '';
62 
68  protected $codemirrorPath = 'Resources/Public/JavaScript/Contrib/codemirror/js/';
69 
75  protected $codeCompletionComponents = ['TsRef', 'CompletionResult', 'TsParser', 'TsCodeCompletion'];
76 
83  public function setMode($mode)
84  {
85  $this->mode = $mode;
86  return $this;
87  }
88 
95  public function setAjaxSaveType($ajaxSaveType)
96  {
97  $this->ajaxSaveType = $ajaxSaveType;
98  return $this;
99  }
100 
106  public function setModeByFile($file)
107  {
108  $fileInfo = GeneralUtility::split_fileref($file);
109  $this->setModeByType($fileInfo['fileext']);
110  }
111 
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 
158  public function getMode()
159  {
160  return $this->mode;
161  }
162 
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 
173  $this->codemirrorPath = $this->extPath . $this->codemirrorPath;
174  }
175 
181  public function getJavascriptCode()
182  {
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 
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 
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 
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 
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 
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  }
368  $response = GeneralUtility::makeInstance(Response::class);
369  $response->getBody()->write(json_encode($responseContent));
370  }
371 
372  return $response;
373  }
374 
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 
397  protected function getPageRenderer()
398  {
399  return GeneralUtility::makeInstance(PageRenderer::class);
400  }
401 }
setAjaxSaveType($ajaxSaveType)
Definition: T3editor.php:95
static callUserFunction($funcName, &$params, &$ref, $_='', $errorMode=0)
static getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:40
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)
getPlugins(ServerRequestInterface $request, ResponseInterface $response)
Definition: T3editor.php:383
static split_fileref($fileNameWithPath)
getCodeEditor($name, $class='', $content='', $additionalParams='', $alt='', array $hiddenfields=[])
Definition: T3editor.php:299
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']