Allow adding groups with spaces in them when wrapping groups names in quotation marks.

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2020-05-14 18:04:31 +02:00
parent 56990c5c34
commit 402e56e054

View File

@@ -39,6 +39,37 @@ function JSON_error($message = null)
echo json_encode($response);
}
function space_aware_explode($input)
{
$ret = array();
$quoted = false;
$pos = 0;
// Loop over input string
for ($i = 0; $i < strlen($input); $i++)
{
// Get current character
$c = $input[$i];
// If current character is a space (or comma) and we're outside
// of a quoted region, we accept this character as separator
if (($c == ' ' || $c == ',') && !$quoted) {
$ret[] = str_replace('"', '', substr($input, $pos, $i - $pos));
$pos = $i+1;
}
elseif($c == '"' && !$quoted)
$quoted = true; // Quotation begins
elseif($c == '"' && $quoted)
$quoted = false; // Quotation ends here
}
// Get last element of the string
if ($pos > 0) {
$ret[] = substr($input, $pos);
}
return $ret;
}
if ($_POST['action'] == 'get_groups') {
// List all available groups
try {
@@ -54,7 +85,7 @@ if ($_POST['action'] == 'get_groups') {
} elseif ($_POST['action'] == 'add_group') {
// Add new group
try {
$names = explode(' ', trim($_POST['name']));
$names = space_aware_explode(trim($_POST['name']));
$total = count($names);
$added = 0;
$stmt = $db->prepare('INSERT INTO "group" (name,description) VALUES (:name,:desc)');