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:
Frederic Massart
2014-03-31 11:28:43 +08:00
parent ad32dda90c
commit 356ec27f08
3 changed files with 78 additions and 4 deletions
+72
View File
@@ -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.