2 namespace TYPO3\CMS\Rtehtmlarea
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Utility\GeneralUtility
;
18 use TYPO3\CMS\Lang\LanguageService
;
19 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility
;
20 use TYPO3\CMS\Core\FrontendEditing\FrontendEditingController
;
23 * API for extending htmlArea RTE
25 abstract class RteHtmlAreaApi
28 * The key of the extension that is extending htmlArea RTE
32 protected $extensionKey = 'rtehtmlarea';
35 * The name of the plugin registered by the extension
39 protected $pluginName;
42 * Path to the skin (css) file that should be added to the RTE skin when the registered plugin is enabled, relative to the extension dir
46 protected $relativePathToSkin = '';
56 * The comma-separated list of button names that the registered plugin is adding to the htmlArea RTE toolbar
60 protected $pluginButtons = '';
63 * The comma-separated list of label names that the registered plugin is adding to the htmlArea RTE toolbar
67 protected $pluginLabels = '';
70 * Boolean indicating whether the plugin is adding buttons or not
74 protected $pluginAddsButtons = true
;
77 * The name-converting array, converting the button names used in the RTE PageTSConfing to the button id's used by the JS scripts
81 protected $convertToolbarForHtmlAreaArray = array();
84 * TRUE if the registered plugin requires the PageTSConfig Classes configuration
88 protected $requiresClassesConfiguration = false
;
91 * The comma-separated list of names of prerequisite plugins
95 protected $requiredPlugins = '';
98 * Configuration array with settings given down from calling class
102 protected $configuration;
105 * Returns TRUE if the plugin is available and correctly initialized
107 * @param array $configuration Configuration array given from calling object down to the single plugins
108 * @return bool TRUE if this plugin object should be made available in the current environment and is correctly initialized
110 public function main(array $configuration)
112 $this->configuration
= $configuration;
113 // Set the value of this boolean based on the initial value of $this->pluginButtons
114 $this->pluginAddsButtons
= !empty($this->pluginButtons
);
115 // Check if the plugin should be disabled in frontend
116 if ($this->isFrontend() && $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rtehtmlarea']['plugins'][$this->pluginName
]['disableInFE']) {
123 * Return JS configuration of the htmlArea plugins registered by the extension
125 * @return string JS configuration for registered plugins
127 public function buildJavascriptConfiguration()
130 $pluginButtons = GeneralUtility
::trimExplode(',', $this->pluginButtons
, true
);
131 foreach ($pluginButtons as $button) {
132 if (in_array($button, $this->toolbar
)) {
133 if (!is_array($this->configuration
['thisConfig']['buttons.']) ||
!is_array($this->configuration
['thisConfig']['buttons.'][($button . '.')])) {
134 $jsArray[] = 'RTEarea[editornumber].buttons.' . $button . ' = new Object();';
138 return implode(LF
, $jsArray);
142 * Returns the extension key
144 * @return string the extension key
146 public function getExtensionKey()
148 return $this->extensionKey
;
152 * Returns a boolean indicating whether the plugin adds buttons or not to the toolbar
156 public function addsButtons()
158 return $this->pluginAddsButtons
;
162 * Returns the list of buttons implemented by the plugin
164 * @return string the list of buttons implemented by the plugin
166 public function getPluginButtons()
168 return $this->pluginButtons
;
172 * Returns the list of toolbar labels implemented by the plugin
174 * @return string the list of labels implemented by the plugin
176 public function getPluginLabels()
178 return $this->pluginLabels
;
182 * Returns the conversion array from TYPO3 button names to htmlArea button names
184 * @return array the conversion array from TYPO3 button names to htmlArea button names
186 public function getConvertToolbarForHtmlAreaArray()
188 return $this->convertToolbarForHtmlAreaArray
;
192 * Returns TRUE if the extension requires the PageTSConfig Classes configuration
194 * @return bool TRUE if the extension requires the PageTSConfig Classes configuration
196 public function requiresClassesConfiguration()
198 return $this->requiresClassesConfiguration
;
202 * Returns the list of plugins required by the plugin
204 * @return string the list of plugins required by the plugin
206 public function getRequiredPlugins()
208 return $this->requiredPlugins
;
214 * @param array $toolbar
216 public function setToolbar(array $toolbar)
218 $this->toolbar
= $toolbar;
227 protected function cleanList($str)
229 if (strstr($str, '*')) {
232 $str = implode(',', array_unique(GeneralUtility
::trimExplode(',', $str, true
)));
238 * Resolve a label and do some funny quoting.
240 * @param string $string Given label name
241 * @return string Resolved label
243 protected function getPageConfigLabel($string)
245 $label = $this->getLanguageService()->sL(trim($string));
246 // @todo: find out why this is done and if it could be substituted with quoteJSvalue
247 $label = str_replace('"', '\\"', str_replace('\\\'', '\'', $label));
252 * Return TRUE if we are in the FE, but not in the FE editing feature of BE.
256 protected function isFrontend()
258 return is_object($GLOBALS['TSFE'])
259 && !$this->isFrontendEditActive()
260 && TYPO3_MODE
== 'FE';
264 * Checks whether frontend editing is active.
268 protected function isFrontendEditActive()
270 return is_object($GLOBALS['TSFE'])
271 && $GLOBALS['TSFE']->beUserLogin
272 && $GLOBALS['BE_USER']->frontendEdit
instanceof FrontendEditingController
;
276 * Make a file name relative to the PATH_site or to the PATH_typo3
278 * @param string $filename: a file name of the form EXT:.... or relative to the PATH_site
279 * @return string the file name relative to the PATH_site if in frontend or relative to the PATH_typo3 if in backend
281 protected function getFullFileName($filename)
283 if (substr($filename, 0, 4) === 'EXT:') {
285 list($extKey, $local) = explode('/', substr($filename, 4), 2);
287 if ((string)$extKey !== '' && ExtensionManagementUtility
::isLoaded($extKey) && (string)$local !== '') {
288 $newFilename = ($this->isFrontend() ||
$this->isFrontendEditActive()
289 ? ExtensionManagementUtility
::siteRelPath($extKey)
290 : ExtensionManagementUtility
::extRelPath($extKey))
294 $path = ($this->isFrontend() ||
$this->isFrontendEditActive() ?
'' : '../');
295 $newFilename = $path . ($filename[0] === '/' ?
substr($filename, 1) : $filename);
297 return GeneralUtility
::resolveBackPath($newFilename);
301 * Writes contents in a file in typo3temp and returns the file name
303 * @param string $label: A label to insert at the beginning of the name of the file
304 * @param string $fileExtension: The file extension of the file, defaulting to 'js'
305 * @param string $contents: The contents to write into the file
306 * @return string The name of the file written to typo3temp
307 * @throws \RuntimeException If writing to file failed
309 protected function writeTemporaryFile($label, $fileExtension = 'js', $contents = '')
311 $relativeFilename = 'typo3temp/RteHtmlArea/' . str_replace('-', '_', $label) . '_' . GeneralUtility
::shortMD5($contents, 20) . '.' . $fileExtension;
312 $destination = PATH_site
. $relativeFilename;
313 if (!file_exists($destination)) {
314 $minifiedJavaScript = '';
315 if ($fileExtension === 'js' && $contents !== '') {
316 $minifiedJavaScript = GeneralUtility
::minifyJavaScript($contents);
318 $failure = GeneralUtility
::writeFileToTypo3tempDir($destination, $minifiedJavaScript ?
$minifiedJavaScript : $contents);
320 throw new \
RuntimeException($failure, 1294585668);
323 if ($this->isFrontend() ||
$this->isFrontendEditActive()) {
324 $fileName = $relativeFilename;
326 $fileName = '../' . $relativeFilename;
328 return GeneralUtility
::resolveBackPath($fileName);
332 * Get language service, instantiate if not there, yet
334 * @return LanguageService
336 protected function getLanguageService()
338 return $GLOBALS['LANG'];