Validate JSON input in configurator
authorColeman Watts <coleman@civicrm.org>
Fri, 2 Mar 2018 17:50:26 +0000 (12:50 -0500)
committerColeman Watts <coleman@civicrm.org>
Fri, 2 Mar 2018 17:50:26 +0000 (12:50 -0500)
CRM/Admin/Page/CKEditorConfig.php
js/wysiwyg/admin.ckeditor-configurator.js
templates/CRM/Admin/Page/CKEditorConfig.tpl

index 8286bfe06cebbab36206ac053756285d71089e78..3cb00094dd8fa0b755b6e63b86a0e883542c23b1 100644 (file)
@@ -134,6 +134,12 @@ class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page {
         if ($val != 'true' && $val != 'false' && $val != 'null' && $val[0] != '{' && $val[0] != '[' && !is_numeric($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);
         $setting = "\n\t{$key} = {$val};\n";
index b7ff14825b590050ded1bf76887c01e0b35b7df4..6c44806754b7628f144356f1aeac4fb869401c76 100644 (file)
@@ -45,6 +45,7 @@
       }
       else {
         $el.after('<span>&nbsp; = &nbsp;<input class="crm-form-text ' + (type==='Number' ? 'eight" type="number"' : 'huge" type="text"') + ' name="config_' + name + '"/></span>');
+        $el.next('span').find('input.crm-form-text[type=text]').change(validateJson);
       }
     } else {
       $el.closest('div').remove();
     return {results: list, text: 'id'};
   }
 
+  function validateJson() {
+    var val = $(this).val();
+    $(this).parent().removeClass('crm-error');
+    if (val[0] === '[' || val[0] === '{') {
+      try {
+        JSON.parse(val);
+      } catch (e) {
+        $(this).parent().addClass('crm-error');
+      }
+    }
+  }
+
   function addOption() {
     $('#crm-custom-config-options').append($(configRowTpl({})));
-    $('div:last input.crm-config-option-name', '#crm-custom-config-options').crmSelect2({
+    $('.crm-config-option-row:last input.crm-config-option-name', '#crm-custom-config-options').crmSelect2({
       data: getOptionList,
       formatSelection: function(field) {
         return '<strong>' + field.id + '</strong> (' + field.type + ')';
index 496a963c076019089f2b89ca972a9da4a6f49ec6..b92082c3bd3be1f86eb9206924a5bcf19e556e2a 100644 (file)
     border-bottom: 0 none;
     padding: 3px 10px 1px !important;
   }
+  .crm-config-option-row span.crm-error:after {
+    font-family: FontAwesome;
+    content: " \f071 Invalid JSON"
+  }
 {/literal}</style>
 {* Force the custom config file to reload by appending a new query string *}
 <script type="text/javascript">