From 356ec27f087e11f6f4094e604cf5cc30475013f7 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 28 Mar 2014 11:04:46 +0800 Subject: [PATCH 1/2] MDL-44747 editor_atto: Improved validation of toolbar config This ensures that: - Plugins are only used once, - Group names are unique, - Lines match: group = plugin[, plugin[, plugin ...]], - There are some groups and plugins defined, - The plugins used are installed. --- lib/editor/atto/adminlib.php | 72 +++++++++++++++++++++++++ lib/editor/atto/lang/en/editor_atto.php | 5 ++ lib/editor/atto/settings.php | 5 +- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/lib/editor/atto/adminlib.php b/lib/editor/atto/adminlib.php index 8c5a623af21..36dffe0a9e8 100644 --- a/lib/editor/atto/adminlib.php +++ b/lib/editor/atto/adminlib.php @@ -24,6 +24,78 @@ defined('MOODLE_INTERNAL') || die(); +/** + * Admin setting for toolbar. + * + * @package editor_atto + * @copyright 2014 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class editor_atto_toolbar_setting extends admin_setting_configtextarea { + + /** + * Validate data. + * + * This ensures that: + * - Plugins are only used once, + * - Group names are unique, + * - Lines match: group = plugin[, plugin[, plugin ...]], + * - There are some groups and plugins defined, + * - The plugins used are installed. + * + * @param string $data + * @return mixed True on success, else error message. + */ + public function validate($data) { + $result = parent::validate($data); + if ($result !== true) { + return $result; + } + + $lines = explode("\n", $data); + $groups = array(); + $plugins = array(); + + foreach ($lines as $line) { + if (empty(trim($line))) { + continue; + } + + $matches = array(); + if (!preg_match('/^\s*([a-z0-9]+)\s*=\s*([a-z0-9]+(\s*,\s*[a-z0-9]+)*)+\s*$/', $line, $matches)) { + $result = get_string('errorcannotparseline', 'editor_atto', $line); + break; + } + + $group = $matches[1]; + if (isset($groups[$group])) { + $result = get_string('errorgroupisusedtwice', 'editor_atto', $group); + break; + } + $groups[$group] = true; + + $lineplugins = array_map('trim', explode(',', $matches[2])); + foreach ($lineplugins as $plugin) { + if (isset($plugins[$plugin])) { + $result = get_string('errorpluginisusedtwice', 'editor_atto', $plugin); + break 2; + } else if (!core_component::get_component_directory('atto_' . $plugin)) { + $result = get_string('errorpluginnotfound', 'editor_atto', $plugin); + break 2; + } + $plugins[$plugin] = true; + } + } + + // We did not find any groups or plugins. + if (empty($groups) || empty($plugins)) { + $result = get_string('errornopluginsorgroupsfound', 'editor_atto'); + } + + return $result; + } + +} /** * Special class for Atto plugins administration. diff --git a/lib/editor/atto/lang/en/editor_atto.php b/lib/editor/atto/lang/en/editor_atto.php index 61ceb0f27ce..3a44ff3a6c8 100644 --- a/lib/editor/atto/lang/en/editor_atto.php +++ b/lib/editor/atto/lang/en/editor_atto.php @@ -22,6 +22,11 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['errorcannotparseline'] = 'The line \'{$a}\' could not be parsed.'; +$string['errorgroupisusedtwice'] = 'The group \'{$a}\' is defined twice, group names must be unique.'; +$string['errornopluginsorgroupsfound'] = 'No plugins or groups found, please add some groups and plugins.'; +$string['errorpluginnotfound'] = 'The plugin \'{$a}\' cannot be used, it does not appear to be installed.'; +$string['errorpluginisusedtwice'] = 'The plugin \'{$a}\' is used twice, plugins can only be defined once.'; $string['pluginname'] = 'Atto HTML editor'; $string['subplugintype_atto'] = 'Atto plugin'; $string['subplugintype_atto_plural'] = 'Atto plugins'; diff --git a/lib/editor/atto/settings.php b/lib/editor/atto/settings.php index 34c061376f4..3e8523c4018 100644 --- a/lib/editor/atto/settings.php +++ b/lib/editor/atto/settings.php @@ -44,10 +44,7 @@ insert = equation, charmap, table, clear undo = undo accessibility = accessibilitychecker, accessibilityhelper other = html'; - $setting = new admin_setting_configtextarea('editor_atto/toolbar', - $name, - $desc, - $default); + $setting = new editor_atto_toolbar_setting('editor_atto/toolbar', $name, $desc, $default); $settings->add($setting); } From 73afb59759a576394e8d7d99f1bca8eb3d7230b4 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 28 Mar 2014 11:08:17 +0800 Subject: [PATCH 2/2] MDL-44747 editor_atto: Renamed subplugin setting class --- lib/editor/atto/adminlib.php | 2 +- lib/editor/atto/settings.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/editor/atto/adminlib.php b/lib/editor/atto/adminlib.php index 36dffe0a9e8..511d4667100 100644 --- a/lib/editor/atto/adminlib.php +++ b/lib/editor/atto/adminlib.php @@ -104,7 +104,7 @@ class editor_atto_toolbar_setting extends admin_setting_configtextarea { * @copyright 2014 Jerome Mouneyrac * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class atto_subplugins_settings extends admin_setting { +class editor_atto_subplugins_setting extends admin_setting { /** * Constructor. diff --git a/lib/editor/atto/settings.php b/lib/editor/atto/settings.php index 3e8523c4018..3ab47531346 100644 --- a/lib/editor/atto/settings.php +++ b/lib/editor/atto/settings.php @@ -29,7 +29,7 @@ $ADMIN->add('editorsettings', new admin_category('editoratto', $editor->displayn $settings = new admin_settingpage('editorsettingsatto', new lang_string('settings', 'editor_atto')); if ($ADMIN->fulltree) { require_once(__DIR__ . '/adminlib.php'); - $settings->add(new atto_subplugins_settings()); + $settings->add(new editor_atto_subplugins_setting()); $name = new lang_string('toolbarconfig', 'editor_atto'); $desc = new lang_string('toolbarconfig_desc', 'editor_atto'); $default = 'collapse = collapse