'moono', 'extraPlugins' => '', ); /** * Run page. * * @return string */ public function run() { // If the form was submitted, take appropriate action. if (!empty($_POST['revert'])) { self::deleteConfigFile(); } elseif (!empty($_POST['config'])) { $this->save($_POST); } CRM_Core_Resources::singleton() ->addScriptFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/js/fulltoolbareditor.js', 1) ->addScriptFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/js/abstracttoolbarmodifier.js', 2) ->addScriptFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/js/toolbarmodifier.js', 3) ->addScriptFile('civicrm', 'js/wysiwyg/admin.ckeditor-configurator.js', 10) ->addStyleFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/css/fontello.css') ->addStyleFile('civicrm', 'bower_components/ckeditor/samples/css/samples.css') ->addVars('ckConfig', array( 'plugins' => array_values($this->getCKPlugins()), )); $this->assign('skins', $this->getCKSkins()); $this->assign('skin', $this->getConfigSetting('skin')); $this->assign('extraPlugins', $this->getConfigSetting('extraPlugins')); $this->assign('configUrl', self::getConfigUrl()); $this->assign('revertConfirm', htmlspecialchars(ts('Are you sure you want to revert all changes?', array('escape' => 'js')))); CRM_Utils_System::appendBreadCrumb(array(array( 'url' => CRM_Utils_System::url('civicrm/admin/setting/preferences/display', 'reset=1'), 'title' => ts('Display Preferences'), ))); return parent::run(); } /** * Generate the config js file based on posted data. * * @param array $params */ public function save($params) { $config = "/**\n" . " * CKEditor config file auto-generated by CiviCRM.\n" . " *\n" . " * Note: This file will be overwritten if settings are modified at:\n" . " * @link " . CRM_Utils_System::url(CRM_Utils_System::currentPath(), NULL, TRUE, NULL, FALSE) . "\n" . " */\n\n" // Standardize line-endings . preg_replace('~\R~u', "\n", $params['config']); // Use defaultSettings as a whitelist so we don't just insert any old junk into the file foreach ($this->defaultSettings as $key => $default) { if (isset($params[$key]) && strlen($params[$key])) { $pos = strrpos($config, '};'); $setting = "\n\tconfig.$key = '{$params[$key]}';\n"; $config = substr_replace($config, $setting, $pos, 0); } } self::saveConfigFile($config); if (!empty($params['save'])) { CRM_Core_Session::setStatus(ts("You may need to clear your browser's cache to see the changes in CiviCRM."), ts('CKEditor Saved'), 'success'); } } /** * Get CKEditor plugins. * * @return array */ private function getCKPlugins() { $plugins = array(); global $civicrm_root; $pluginDir = CRM_Utils_file::addTrailingSlash($civicrm_root, '/') . 'bower_components/ckeditor/plugins'; foreach (glob($pluginDir . '/*', GLOB_ONLYDIR) as $dir) { $dir = rtrim(str_replace('\\', '/', $dir), '/'); $name = substr($dir, strrpos($dir, '/') + 1); $dir = CRM_Utils_file::addTrailingSlash($dir, '/'); if (is_file($dir . 'plugin.js')) { $plugins[$name] = array( 'id' => $name, 'text' => ucfirst($name), 'icon' => NULL, ); if (is_dir($dir . "icons")) { if (is_file($dir . "icons/$name.png")) { $plugins[$name]['icon'] = "bower_components/ckeditor/plugins/$name/icons/$name.png"; } elseif (glob($dir . "icons/*.png")) { $icon = CRM_Utils_Array::first(glob($dir . "icons/*.png")); $icon = rtrim(str_replace('\\', '/', $icon), '/'); $plugins[$name]['icon'] = "bower_components/ckeditor/plugins/$name/icons/" . substr($icon, strrpos($icon, '/') + 1); } } } } return $plugins; } /** * Get CK Editor skins. * * @return array */ private function getCKSkins() { $skins = array(); global $civicrm_root; $skinDir = CRM_Utils_file::addTrailingSlash($civicrm_root, '/') . 'bower_components/ckeditor/skins'; foreach (glob($skinDir . '/*', GLOB_ONLYDIR) as $dir) { $dir = rtrim(str_replace('\\', '/', $dir), '/'); $skins[] = substr($dir, strrpos($dir, '/') + 1); } return $skins; } /** * @param $setting * @return string */ private function getConfigSetting($setting) { $value = CRM_Utils_Array::value($setting, $this->defaultSettings, ''); $file = self::getConfigFile(); if ($file) { $contents = file_get_contents($file); $matches = array(); preg_match("/\sconfig\.$setting\s?=\s?'([^']*)'/", $contents, $matches); if ($matches) { $value = $matches[1]; } } return $value; } /** * @return null|string */ public static function getConfigUrl() { if (self::getConfigFile()) { // FIXME: Basing file path off imageUploadURL sucks, but it's all we got $url = CRM_Utils_file::addTrailingSlash(CRM_Core_Config::singleton()->imageUploadURL, '/'); $url = str_replace('/persist/contribute/', '/persist/', $url); return $url . self::CONFIG_FILENAME; } return NULL; } /** * @param bool $checkIfFileExists * If false, this fn will return fileName even if it doesn't exist * * @return null|string */ public static function getConfigFile($checkIfFileExists = TRUE) { // FIXME: Basing file path off imageUploadDir sucks, but it's all we got $dir = CRM_Core_Config::singleton()->imageUploadDir; $dir = CRM_Utils_file::addTrailingSlash(str_replace('\\', '/', $dir), '/'); $dir = str_replace('/persist/contribute/', '/persist/', $dir); $fileName = $dir . self::CONFIG_FILENAME; return !$checkIfFileExists || is_file($fileName) ? $fileName : NULL; } /** * @param string $contents */ public static function saveConfigFile($contents) { $file = self::getConfigFile(FALSE); file_put_contents($file, $contents); } /** * Delete config file. */ public static function deleteConfigFile() { $file = self::getConfigFile(); if ($file) { unlink($file); } } }