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.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user