<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.7 |
+ | CiviCRM version 5 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016 |
+ | Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
/**
*
* @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
+ * @copyright CiviCRM LLC (c) 2004-2018
*/
/**
// If the form was submitted, take appropriate action.
if (!empty($_POST['revert'])) {
self::deleteConfigFile($this->preset);
+ self::setConfigDefault();
}
elseif (!empty($_POST['config'])) {
$this->save($_POST);
'settings' => $settings,
));
- $configUrl = self::getConfigUrl($this->preset);
- if (!$configUrl) {
- $configUrl = self::getConfigUrl('default');
- }
+ $configUrl = self::getConfigUrl($this->preset) ?: self::getConfigUrl('default');
$this->assign('preset', $this->preset);
$this->assign('presets', CRM_Core_OptionGroup::values('wysiwyg_presets', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'));
* @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"
+ $config = self::fileHeader()
// Standardize line-endings
. preg_replace('~\R~u', "\n", $params['config']);
$val = trim($val);
if (strpos($key, 'config_') === 0 && strlen($val)) {
if ($val != 'true' && $val != 'false' && $val != 'null' && $val[0] != '{' && $val[0] != '[' && !is_numeric($val)) {
- $val = json_encode($val);
+ $val = json_encode($val, JSON_UNESCAPED_SLASHES);
+ }
+ elseif ($val[0] == '{' || $val[0] == '[') {
+ if (!is_array(json_decode($val, TRUE))) {
+ // Invalid JSON. Do not save.
+ continue;
+ }
}
$pos = strrpos($config, '};');
$key = preg_replace('/^config_/', 'config.', $key);
*/
private function getConfigSettings() {
$matches = $result = array();
- $file = self::getConfigFile($this->preset);
- if (!$file) {
- $file = self::getConfigFile('default');
- }
+ $file = self::getConfigFile($this->preset) ?: self::getConfigFile('default');
$result['skin'] = 'moono';
if ($file) {
$contents = file_get_contents($file);
}
}
+ /**
+ * Create default config file if it doesn't exist
+ */
+ public static function setConfigDefault() {
+ if (!self::getConfigFile()) {
+ $config = self::fileHeader() . "CKEDITOR.editorConfig = function( config ) {\n\tconfig.allowedContent = true;\n};\n";
+ // Make sure directories exist
+ if (!is_dir(Civi::paths()->getPath('[civicrm.files]/persist'))) {
+ mkdir(Civi::paths()->getPath('[civicrm.files]/persist'));
+ }
+ $newFileName = Civi::paths()->getPath(self::CONFIG_FILEPATH . 'default.js');
+ file_put_contents($newFileName, $config);
+ }
+ }
+
+ /**
+ * @return string
+ */
+ public static function fileHeader() {
+ return "/**\n"
+ . " * CKEditor config file auto-generated by CiviCRM (" . date('Y-m-d H:i:s') . ").\n"
+ . " *\n"
+ . " * Note: This file will be overwritten if settings are modified at:\n"
+ . " * @link " . CRM_Utils_System::url('civicrm/admin/ckeditor', NULL, TRUE, NULL, FALSE) . "\n"
+ . " */\n";
+ }
+
}