diff --git a/class.tinymce.php b/class.tinymce.php index ec8c9e4cc49013888c1d1e8e9a1f8be5b20a120d..e3c784b146d7c1e2fd537c630c00c5c1e13ca159 100644 --- a/class.tinymce.php +++ b/class.tinymce.php @@ -22,244 +22,183 @@ * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ -/** - * Initialisation flag - * - * @var boolean - */ -$tinyMCEInitFlag = FALSE; - /** * tinyMCE initialisation class * + * Usage: + * $tinyMCE = t3lib_div::makeInstance('tinyMCE'); + * $tinyMCE->loadConfiguration($configuration); + * $javascript = $tinyMCE->getJS(); + * * @author Stefan Galinski <stefan.galinski@gmail.com> * @package TYPO3 * @subpackage tx_tinymce */ class tinyMCE { /** - * @var array configuration + * Internal extension configuration array + * + * @var array */ - protected $extConfig = array(); + protected $extensionConfiguration = array(); /** + * TinyMCE configuration + * * @var array */ - protected $config = array(); + protected $tinymceConfiguration = array(); /** - * @var array + * Initialization flag + * + * @var bool */ - protected $GZconfig = array(); + static protected $init = FALSE; /** * Constructor - * - * @param string $config configuration file or string (optional ... default is basic configuration) - * @param string $GZconfig gzip configuration file or string (optional ... default is basic configuration) - * @param boolean $autoLang set to false if you dont want the automatic language replacement (default true) */ - public function __construct($config = '', $GZconfig = '', $autoLang = TRUE) { - // prepare extension and given tinyMCE configurations - $this->prepareConfig(); - $this->prepareTinyMCEConfig($config, FALSE); - $this->prepareTinyMCEConfig($GZconfig, TRUE); + public function __construct() { + $this->extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['tinymce']); + } - // language replacement - if ($autoLang) { - // get main object - if (TYPO3_MODE == 'FE') { - $lang =& $GLOBALS['TSFE']; - } else { - $lang =& $GLOBALS['LANG']; - } + /** + * @param string $configuration file reference or configuration string (defaults to basic configuration) + * @param boolean $forceLanguage set this to true if you want to force your language set by the configuration + * @return void + */ + public function loadConfiguration($configuration = '', $forceLanguage = FALSE) { + self::$init = FALSE; + $this->tinymceConfiguration = $this->prepareTinyMCEConfiguration($configuration); - // language conversion from TLD to iso631 - if (array_key_exists($this->extConfig['lang'], $lang->csConvObj->isoArray)) { - $this->extConfig['lang'] = $lang->csConvObj->isoArray[$this->extConfig['lang']]; - } + if (!$forceLanguage) { + $this->setLanguage(); + } - if (!is_file(PATH_site . t3lib_extMgm::siteRelPath('tinymce') . - 'tinyMCE/langs/' . $this->extConfig['lang'] . '.js') - ) { - $this->extConfig['lang'] = 'en'; - } + if ($this->extensionConfiguration['compressed']) { + $this->tinymceConfiguration['disk_cache'] = ($this->extensionConfiguration['diskCache'] ? 'true' : 'false'); + } + } - // language replacement - $this->replaceInConfig(array('language' => $this->extConfig['lang'])); - if ($this->extConfig['compressed']) { - $this->replaceInConfig(array('languages' => $this->extConfig['lang']), TRUE); - } + /** + * Calculates and sets the current language + * + * @return void + */ + protected function setLanguage() { + $languageInstance = (TYPO3_MODE == 'FE' ? $GLOBALS['TSFE'] : $GLOBALS['LANG']); + $languageKey = $languageInstance->lang; + + $groupOrUserProps = t3lib_BEfunc::getModTSconfig('', 'tx_tinyMCE'); + if (trim($groupOrUserProps['properties']['prefLang']) !== '') { + $languageKey = $groupOrUserProps['properties']['prefLang']; } - // activate disk cache - if ($this->extConfig['compressed'] && $this->extConfig['diskCache']) { - $this->replaceInConfig(array('disk_cache' => 'true'), TRUE); - } elseif ($this->extConfig['compressed'] && !$this->extConfig['diskCache']) { - $this->replaceInConfig(array('disk_cache', 'false'), TRUE); + // language conversion from TLD to iso631 + if (array_key_exists($languageKey, $languageInstance->csConvObj->isoArray)) { + $languageKey = $languageInstance->csConvObj->isoArray[$languageKey]; } + + $languageFile = PATH_site . t3lib_extMgm::siteRelPath('tinymce') . 'tinymce/langs/' . $languageKey . '.js'; + if (!is_file($languageFile)) { + $languageKey = 'en'; + } + + $this->tinymceConfiguration['language'] = $languageKey; } /** - * Generates a configuration string from the array information + * Returns a configuration string from the tinymce configuration array * - * @param boolean $gzip set to true if the options should be set into the gzip config - * @return string generated configuration + * @return string */ - protected function buildConfigString($gzip) { - $curConfig = ($gzip ? $this->GZconfig : $this->config); - - // generate configuration strings from array - $config = $curConfig['preJS']; - $config .= (($gzip) ? 'tinyMCE_GZ' : 'tinyMCE') . '.init({' . "\n"; - $configOptions = array(); - if (count($curConfig)) { - foreach ($curConfig as $option => $value) { - if ($option == 'preJS' || $option == 'postJS') { + protected function buildConfigString() { + $configuration = $this->tinymceConfiguration['preJS']; + $configuration .= ($this->extensionConfiguration['compressed'] ? 'tinyMCE_GZ' : 'tinyMCE'); + $configuration .= '.init({' . "\n"; + + $configurationOptions = array(); + if (count($this->tinymceConfiguration)) { + foreach ($this->tinymceConfiguration as $option => $value) { + if (in_array($option, array('preJS', 'postJS'))) { continue; } - if ($value != 'false' && $value != 'true') { + if (!in_array($value, array('false', 'true'))) { $value = '\'' . $value . '\''; } - $configOptions[] = "\t" . $option . ' : ' . $value; + $configurationOptions[] = "\t" . $option . ' : ' . $value; } } - $config .= implode(",\n", $configOptions); - $config .= "\n" . '});'; - $config .= $curConfig['postJS']; + $configuration .= implode(",\n", $configurationOptions); + $configuration .= "\n" . '});'; + $configuration .= $this->tinymceConfiguration['postJS']; - return $config; + return $configuration; } /** - * generates and returns the needed javascript inclusion code + * Returns the needed javascript inclusion code * - * Note: this function can only be called one time + * Note: This function can only be called once for each loaded configuration. * - * @return string generated javascript inclusion code + * @return string */ public function getJS() { - // check init flag - if ($GLOBALS['tinyMCEInitFlag']) { - return ''; + $output = ''; + if (self::$init) { + return $output; } - $GLOBALS['tinyMCEInitFlag'] = TRUE; + self::$init = TRUE; - // build configuration strings - $config = $this->buildConfigString(FALSE); - $GZconfig = $this->buildConfigString(TRUE); + $script = $GLOBALS['BACK_PATH'] . t3lib_extMgm::extRelPath('tinymce') . 'tinymce/' . + ($this->extensionConfiguration['compressed'] ? 'tiny_mce_gzip.js' : 'tiny_mce.js'); + $output = '<script type="text/javascript" src="' . $script . '"></script> + <script type="text/javascript">' . "\n" . $this->buildConfigString() . "\n" . '</script>'; - return '<script type="text/javascript" src="' . $GLOBALS['BACK_PATH'] . - t3lib_extMgm::extRelPath('tinymce') . 'tinyMCE/' . - ($this->extConfig['compressed'] ? 'tiny_mce_gzip.js' : 'tiny_mce.js') . '"></script> - <script type="text/javascript">' . "\n" . $GZconfig . "\n" . '</script> - <script type="text/javascript">' . "\n" . $config . "\n" . '</script>'; + return $output; } /** - * Prepares a tinyMCE configuration - * All options, post and pre javascript is saved into the config or GZconfig array + * Parses and processes the tinyMCE configuration * - * @param string $config file reference or configuration string - * @param boolean $gzip set to true if the options should be set into the gzip config - * @return void + * @param string $configuration file reference or configuration string + * @return array */ - protected function prepareTinyMCEConfig($config, $gzip) { - // get file contents if necessary - if (is_file($config)) { - $config = t3lib_div::getURL($config); - } - - // get config variable - if ($gzip) { - $curConfig =& $this->GZconfig; - } else { - $curConfig =& $this->config; + protected function prepareTinyMCEConfiguration($configuration) { + $configurationArray = array(); + if (is_file($configuration)) { + $configuration = file_get_contents($configuration); } - // split config into first and last javascript parts (applied later again into the config variables) - // additionaly the config part is matched to get the options + // split config into first and last javascript parts (applied later again into the config variables) + // additionally the config part is matched to get the options $start = '(.*)((tinyMCE|tinyMCE_GZ)\.init.*?\(.*?\{.*?'; $end = '.*?\}.*?\).*?;)(.*)'; $pattern = '/' . $start . $end . '/is'; - preg_match($pattern, $config, $matches); + preg_match($pattern, $configuration, $matches); - // add preJS and postJS - $curConfig['preJS'] = $matches[1]; - $curConfig['postJS'] = $matches[4]; + // add preJS and postJS + $configurationArray['preJS'] = $matches[1]; + $configurationArray['postJS'] = $matches[4]; - // split options into an array (first time strings and the second call splits bool values) + // split options into an array (first time strings and the second call splits bool values) $pattern = '([[:print:]]+?)[\s]*?:[\s]*["|\']{1}(.*?)["|\']{1}[,|\n|}]{1}.*?'; preg_match_all('/' . $pattern . '/i', $matches[2], $options); - $configOptions = NULL; for ($i = 0; $i < count($options[1]); ++$i) { - $configOptions[$options[1][$i]] = $options[2][$i]; + $configurationArray[$options[1][$i]] = $options[2][$i]; } $options = array(); $boolPattern = '([[:print:]]+?)[\s]*?:[\s]*(false|true)[,|\n|}]{1}.*?'; preg_match_all('/' . $boolPattern . '/i', $matches[2], $options); for ($i = 0; $i < count($options[1]); ++$i) { - $configOptions[$options[1][$i]] = $options[2][$i]; + $configurationArray[$options[1][$i]] = $options[2][$i]; } - // add config options - if (is_array($configOptions)) { - $curConfig = array_merge($curConfig, $configOptions); - } - } - - /** - * replaces/adds an option in the configuration - * - * @param array $options option => value - * @param boolean $gzip set to true if the options should be set into the gzip config - * @return void - */ - function replaceInConfig($options, $gzip = FALSE) { - foreach ($options as $option => $value) { - if ($gzip) { - $this->GZconfig[$option] = $value; - } else { - $this->config[$option] = $value; - } - } - } - - /** - * return false if the browser isnt supported - * - * Note: Currently only Opera until version 9 and Konqueror are in the unsupported list. - * Browsers like lynx or IE4 are not in the list, because they are outdated and have only a very - * low market share or arent supported by TYPO3. - * - * Feel free to send other Browsers which should be added here. - * - * @return boolean return true if the browser is supported - */ - public function checkBrowser() { - return TRUE; - } - - /** - * preparation and check of the configuration - * - * Note that the default value will be set, if a option check fails. - * - * @return void - */ - protected function prepareConfig() { - $this->extConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['tinymce']); - - // get current/forced language - $groupOrUserProps = t3lib_BEfunc::getModTSconfig('', 'tx_tinyMCE'); - if (!empty($groupOrUserProps['properties']['prefLang'])) { - $this->extConfig['lang'] = $groupOrUserProps['properties']['prefLang']; - } else { - $this->extConfig['lang'] = (TYPO3_MODE == 'FE' ? $GLOBALS['TSFE']->lang : $GLOBALS['LANG']->lang); - } + return $configurationArray; } }