From ff6778736478ac0622762b1599e5192ad89bc139 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 9 Sep 2019 15:01:30 +0200 Subject: [locations] trim() subnet address fields, both empty == delete Closes #3550 --- modules-available/locations/pages/details.inc.php | 40 +++++++++++++---------- modules-available/locations/pages/subnets.inc.php | 20 +++++++++--- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/modules-available/locations/pages/details.inc.php b/modules-available/locations/pages/details.inc.php index 6acf31bf..81b58456 100644 --- a/modules-available/locations/pages/details.inc.php +++ b/modules-available/locations/pages/details.inc.php @@ -135,20 +135,16 @@ class SubPage // Deletion first $dels = Request::post('deletesubnet', false); + $deleteCount = 0; if (is_array($dels)) { - $count = 0; $stmt = Database::prepare('DELETE FROM subnet WHERE subnetid = :id'); - foreach ($dels as $key => $value) { - if (!is_numeric($key) || $value !== 'on') + foreach ($dels as $subnetid => $value) { + if (!is_numeric($subnetid) || $value !== 'on') continue; - if ($stmt->execute(array('id' => $key))) { - $count += $stmt->rowCount(); + if ($stmt->execute(array('id' => $subnetid))) { + $deleteCount += $stmt->rowCount(); } } - if ($count > 0) { - Message::addInfo('subnets-deleted', $count); - $change = true; - } } // Now actual updates @@ -157,23 +153,33 @@ class SubPage if (!is_array($starts) || !is_array($ends)) { return $change; } - $count = 0; + $editCount = 0; $stmt = Database::prepare('UPDATE subnet SET startaddr = :start, endaddr = :end' . ' WHERE subnetid = :id'); - foreach ($starts as $key => $start) { - if (!isset($ends[$key]) || !is_numeric($key)) + foreach ($starts as $subnetid => $start) { + if (!isset($ends[$subnetid]) || !is_numeric($subnetid)) continue; - $end = $ends[$key]; + $start = trim($start); + $end = trim($ends[$subnetid]); + if (empty($start) && empty($end)) { + $ret = Database::exec('DELETE FROM subnet WHERE subnetid = :id', ['id' => $subnetid]); + $deleteCount += $ret; + continue; + } $range = LocationUtil::rangeToLongVerbose($start, $end); if ($range === false) continue; list($startLong, $endLong) = $range; - if ($stmt->execute(array('id' => $key, 'start' => $startLong, 'end' => $endLong))) { - $count += $stmt->rowCount(); + if ($stmt->execute(array('id' => $subnetid, 'start' => $startLong, 'end' => $endLong))) { + $editCount += $stmt->rowCount(); } } - if ($count > 0) { - Message::addInfo('subnets-updated', $count); + if ($editCount > 0) { + Message::addSuccess('subnets-updated', $editCount); + $change = true; + } + if ($deleteCount > 0) { + Message::addSuccess('subnets-deleted', $deleteCount); $change = true; } return $change; diff --git a/modules-available/locations/pages/subnets.inc.php b/modules-available/locations/pages/subnets.inc.php index 381023d2..fb1e1e80 100644 --- a/modules-available/locations/pages/subnets.inc.php +++ b/modules-available/locations/pages/subnets.inc.php @@ -15,7 +15,8 @@ class SubPage private static function updateSubnets() { User::assertPermission('subnets.edit', NULL, '?do=locations'); - $count = 0; + $editCount = 0; + $deleteCount = 0; $starts = Request::post('startaddr', false); $ends = Request::post('endaddr', false); $locs = Request::post('location', false); @@ -29,7 +30,13 @@ class SubPage if (!isset($ends[$subnetid]) || !isset($locs[$subnetid])) continue; $loc = (int)$locs[$subnetid]; - $end = $ends[$subnetid]; + $start = trim($start); + $end = trim($ends[$subnetid]); + if (empty($start) && empty($end)) { + $ret = Database::exec('DELETE FROM subnet WHERE subnetid = :subnetid', compact('subnetid')); + $deleteCount += $ret; + continue; + } if (!isset($existingLocs[$loc])) { Message::addError('main.value-invalid', 'locationid', $loc); continue; @@ -39,11 +46,16 @@ class SubPage continue; list($startLong, $endLong) = $range; if ($stmt->execute(compact('startLong', 'endLong', 'loc', 'subnetid'))) { - $count += $stmt->rowCount(); + $editCount += $stmt->rowCount(); } } AutoLocation::rebuildAll(); - Message::addSuccess('subnets-updated', $count); + if ($editCount > 0) { + Message::addSuccess('subnets-updated', $editCount); + } + if ($deleteCount > 0) { + Message::addSuccess('subnets-deleted', $deleteCount); + } Util::redirect('?do=Locations'); } -- cgit v1.2.3-55-g7522