TYPO3 CMS  TYPO3_7-6
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 
23 
28 {
29  const MODE_TYPOSCRIPT = 'typoscript';
30  const MODE_JAVASCRIPT = 'javascript';
31  const MODE_CSS = 'css';
32  const MODE_XML = 'xml';
33  const MODE_HTML = 'html';
34  const MODE_PHP = 'php';
35  const MODE_SPARQL = 'sparql';
36  const MODE_MIXED = 'mixed';
37 
41  protected $mode = '';
42 
46  protected $ajaxSaveType = '';
47 
53  protected $editorCounter = 0;
54 
60  protected $relExtPath = '';
61 
67  protected $codemirrorPath = 'Resources/Public/JavaScript/Contrib/codemirror/js/';
68 
74  protected $codeCompletionComponents = ['TsRef', 'CompletionResult', 'TsParser', 'TsCodeCompletion'];
75 
82  public function setMode($mode)
83  {
84  $this->mode = $mode;
85  return $this;
86  }
87 
94  public function setAjaxSaveType($ajaxSaveType)
95  {
96  $this->ajaxSaveType = $ajaxSaveType;
97  return $this;
98  }
99 
106  public function setModeByFile($file)
107  {
108  $fileInfo = GeneralUtility::split_fileref($file);
109  $this->setModeByType($fileInfo['fileext']);
110  }
111 
118  public function setModeByType($type)
119  {
120  switch ($type) {
121  case 'html':
122  case 'htm':
123  case 'tmpl':
124  $mode = self::MODE_HTML;
125  break;
126  case 'js':
127  $mode = self::MODE_JAVASCRIPT;
128  break;
129  case 'xml':
130  case 'svg':
131  $mode = self::MODE_XML;
132  break;
133  case 'css':
134  $mode = self::MODE_CSS;
135  break;
136  case 'ts':
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 
167  public function isEnabled()
168  {
170  return true;
171  }
172 
176  public function __construct()
177  {
178  $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
179  // Disable pmktextarea to avoid conflicts (thanks Peter Klein for this suggestion)
180  $GLOBALS['BE_USER']->uc['disablePMKTextarea'] = 1;
181 
182  $this->relExtPath = ExtensionManagementUtility::extRelPath('t3editor');
183  $this->codemirrorPath = $this->relExtPath . $this->codemirrorPath;
184  }
185 
191  public function getJavascriptCode()
192  {
194  $pageRenderer = $this->getPageRenderer();
195  $pageRenderer->addCssFile($this->relExtPath . 'Resources/Public/Css/t3editor.css');
196  // Include editor-js-lib
197  $pageRenderer->addJsLibrary('codemirror', $this->codemirrorPath . 'codemirror.js');
198  if ($this->mode === self::MODE_TYPOSCRIPT) {
199  foreach ($this->codeCompletionComponents as $codeCompletionComponent) {
200  $pageRenderer->loadRequireJsModule('TYPO3/CMS/T3editor/Plugins/CodeCompletion/' . $codeCompletionComponent);
201  }
202  }
203  $pageRenderer->loadRequireJsModule('TYPO3/CMS/T3editor/T3editor');
204  return '';
205  }
206 
212  protected function getPreparedTemplate()
213  {
214  $T3editor_template = GeneralUtility::getUrl(
215  GeneralUtility::getFileAbsFileName('EXT:t3editor/Resources/Private/Templates/t3editor.html')
216  );
217  return str_replace([CR, LF], '', $T3editor_template);
218  }
219 
226  protected function getParserfileByMode($mode)
227  {
228  switch ($mode) {
229  case self::MODE_TYPOSCRIPT:
230  $relPath = '../../../parse_typoscript/';
231  $parserfile = [$relPath . 'tokenizetyposcript.js', $relPath . 'parsetyposcript.js'];
232  break;
233  case self::MODE_JAVASCRIPT:
234  $parserfile = ['tokenizejavascript.js', 'parsejavascript.js'];
235  break;
236  case self::MODE_CSS:
237  $parserfile = ['parsecss.js'];
238  break;
239  case self::MODE_XML:
240  $parserfile = ['parsexml.js'];
241  break;
242  case self::MODE_SPARQL:
243  $parserfile = ['parsesparql.js'];
244  break;
245  case self::MODE_HTML:
246  $parserfile = ['tokenizejavascript.js', 'parsejavascript.js', 'parsecss.js', 'parsexml.js', 'parsehtmlmixed.js'];
247  break;
248  case self::MODE_PHP:
249  case self::MODE_MIXED:
250  $parserfile = ['tokenizejavascript.js', 'parsejavascript.js', 'parsecss.js', 'parsexml.js', '../contrib/php/js/tokenizephp.js', '../contrib/php/js/parsephp.js', '../contrib/php/js/parsephphtmlmixed.js'];
251  break;
252  default:
253  $parserfile = [];
254  }
255  return json_encode($parserfile);
256  }
257 
264  protected function getStylesheetByMode($mode)
265  {
266  switch ($mode) {
267  case self::MODE_TYPOSCRIPT:
268  $stylesheet = [$this->relExtPath . 'Resources/Public/Css/typoscriptcolors.css'];
269  break;
270  case self::MODE_JAVASCRIPT:
271  $stylesheet = [$this->codemirrorPath . '../css/jscolors.css'];
272  break;
273  case self::MODE_CSS:
274  $stylesheet = [$this->codemirrorPath . '../css/csscolors.css'];
275  break;
276  case self::MODE_XML:
277  $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css'];
278  break;
279  case self::MODE_HTML:
280  $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css', $this->codemirrorPath . '../css/jscolors.css', $this->codemirrorPath . '../css/csscolors.css'];
281  break;
282  case self::MODE_SPARQL:
283  $stylesheet = [$this->codemirrorPath . '../css/sparqlcolors.css'];
284  break;
285  case self::MODE_PHP:
286  $stylesheet = [$this->codemirrorPath . '../contrib/php/css/phpcolors.css'];
287  break;
288  case self::MODE_MIXED:
289  $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css', $this->codemirrorPath . '../css/jscolors.css', $this->codemirrorPath . '../css/csscolors.css', $this->codemirrorPath . '../contrib/php/css/phpcolors.css'];
290  break;
291  default:
292  $stylesheet = [];
293  }
294  $stylesheet[] = $this->relExtPath . 'Resources/Public/Css/t3editor_inner.css';
295  return json_encode($stylesheet);
296  }
297 
309  public function getCodeEditor($name, $class = '', $content = '', $additionalParams = '', $alt = '', array $hiddenfields = [])
310  {
311  $code = '';
312  $class .= ' t3editor';
313  $alt = trim($alt);
314  $code .=
315  '<div class="t3editor">'
316  . '<div class="t3e_wrap">'
317  . $this->getPreparedTemplate()
318  . '</div>'
319  . '<textarea '
320  . 'id="t3editor_' . (int)$this->editorCounter . '" '
321  . 'name="' . htmlspecialchars($name) . '" '
322  . 'class="' . htmlspecialchars($class) . '" '
323  . $additionalParams . ' '
324  . ($alt !== '' ? ' alt="' . htmlspecialchars($alt) . '"' : '')
325  . ' data-labels="' . htmlspecialchars(json_encode($GLOBALS['LANG']->getLabelsWithPrefix('js.', 'label_'))) . '"'
326  . ' data-instance-number="' . (int)$this->editorCounter . '"'
327  . ' data-editor-path="' . htmlspecialchars($this->relExtPath) . '"'
328  . ' data-codemirror-path="' . htmlspecialchars($this->codemirrorPath) . '"'
329  . ' data-ajaxsavetype="' . htmlspecialchars($this->ajaxSaveType) . '"'
330  . ' data-parserfile="' . htmlspecialchars($this->getParserfileByMode($this->mode)) . '"'
331  . ' data-stylesheet="' . htmlspecialchars($this->getStylesheetByMode($this->mode)) . '"'
332  . '>' . htmlspecialchars($content)
333  . '</textarea>'
334  . '</div>';
335  if (!empty($hiddenfields)) {
336  foreach ($hiddenfields as $name => $value) {
337  $code .= '<input type="hidden" ' . 'name="' . htmlspecialchars($name) . '" ' . 'value="' . htmlspecialchars($value) . '" />';
338  }
339  }
340  $this->editorCounter++;
341  return $code;
342  }
343 
351  public function ajaxSaveCode(ServerRequestInterface $request, ResponseInterface $response)
352  {
353  // cancel if its not an Ajax request
354  if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
355  $codeType = isset($request->getParsedBody()['t3editor_savetype']) ? $request->getParsedBody()['t3editor_savetype'] : $request->getQueryParams()['t3editor_savetype'];
356  $savingsuccess = false;
357  try {
358  if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/t3editor/classes/class.tx_t3editor.php']['ajaxSaveCode'])) {
359  $_params = [
360  'pObj' => &$this,
361  'type' => $codeType,
362  'request' => $request,
363  'response' => $response
364  ];
365  foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/t3editor/classes/class.tx_t3editor.php']['ajaxSaveCode'] as $key => $_funcRef) {
366  $savingsuccess = GeneralUtility::callUserFunction($_funcRef, $_params, $this) || $savingsuccess;
367  }
368  }
369  $responseContent = ['result' => $savingsuccess];
370  } catch (\Exception $e) {
371  $responseContent = [
372  'result' => false,
373  'exceptionMessage' => htmlspecialchars($e->getMessage()),
374  'exceptionCode' => $e->getCode()
375  ];
376  }
378  $response = GeneralUtility::makeInstance(Response::class);
379  $response->getBody()->write(json_encode($responseContent));
380  }
381 
382  return $response;
383  }
384 
393  public function getPlugins(ServerRequestInterface $request, ResponseInterface $response)
394  {
395  $result = [];
396  $plugins = &$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3editor']['plugins'];
397  if (is_array($plugins)) {
398  $result = array_values($plugins);
399  }
400  $response->getBody()->write(json_encode($result));
401  return $response;
402  }
403 
407  protected function getPageRenderer()
408  {
409  return GeneralUtility::makeInstance(PageRenderer::class);
410  }
411 }
setAjaxSaveType($ajaxSaveType)
Definition: T3editor.php:94
static callUserFunction($funcName, &$params, &$ref, $checkPrefix='', $errorMode=0)
getPlugins(ServerRequestInterface $request, ResponseInterface $response)
Definition: T3editor.php:393
static split_fileref($fileNameWithPath)
getCodeEditor($name, $class='', $content='', $additionalParams='', $alt='', array $hiddenfields=[])
Definition: T3editor.php:309
static getUrl($url, $includeHeader=0, $requestHeaders=false, &$report=null)
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']