From 17597cd2cc8897211d5380cc56e5345996eff564 Mon Sep 17 00:00:00 2001 From: Fabrice Colas Date: Tue, 27 Jul 2021 18:33:00 +0200 Subject: [PATCH] DHCP static leases w/ optional comment * Ability to add an optional Comment to DHCP static Leases * Sort DHCP static leases by IP (more human readable) --- app/js/custom.js | 5 ++++- includes/dhcp.php | 28 +++++++++++++++++++++++++++- templates/dhcp/static_leases.php | 22 ++++++++++++++++------ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/js/custom.js b/app/js/custom.js index 4bb85e07..da92097d 100644 --- a/app/js/custom.js +++ b/app/js/custom.js @@ -54,16 +54,19 @@ $(document).on("click", ".js-add-dhcp-static-lease", function(e) { var container = $(".js-new-dhcp-static-lease"); var mac = $("input[name=mac]", container).val().trim(); var ip = $("input[name=ip]", container).val().trim(); + var comment = $("input[name=comment]", container).val().trim(); if (mac == "" || ip == "") { return; } var row = $("#js-dhcp-static-lease-row").html() .replace("{{ mac }}", mac) - .replace("{{ ip }}", ip); + .replace("{{ ip }}", ip) + .replace("{{ comment }}", comment); $(".js-dhcp-static-lease-container").append(row); $("input[name=mac]", container).val(""); $("input[name=ip]", container).val(""); + $("input[name=comment]", container).val(""); }); $(document).on("click", ".js-remove-dhcp-static-lease", function(e) { diff --git a/includes/dhcp.php b/includes/dhcp.php index 38d69ea5..9fd7e338 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -154,6 +154,20 @@ function validateDHCPInput() return $errors; } +/** + * Compares to string IPs + * + * @param string $ip1 + * @param string $ip2 + * @return boolean $result + */ +function compareIPs($ip1, $ip2) +{ + $ipu1 = sprintf('%u', ip2long($ip1["ip"])) + 0; + $ipu2 = sprintf('%u', ip2long($ip2["ip"])) + 0; + return $ipu1 > $ipu2; +} + /** * Updates a dnsmasq configuration * @@ -171,13 +185,25 @@ function updateDnsmasqConfig($iface,$status) $config .= $_POST['RangeLeaseTime']; } $config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL; + // Static leases + $staticLeases = array(); for ($i=0; $i < count($_POST["static_leases"]["mac"]); $i++) { $mac = trim($_POST["static_leases"]["mac"][$i]); $ip = trim($_POST["static_leases"]["ip"][$i]); + $comment = trim($_POST["static_leases"]["comment"][$i]); if ($mac != "" && $ip != "") { - $config .= "dhcp-host=$mac,$ip".PHP_EOL; + $staticLeases[] = array('mac' => $mac, 'ip' => $ip, 'comment' => $comment); } } + // Sort ascending by IPs + usort($staticLeases, "compareIPs"); + // Update config + for ($i = 0; $i < count($staticLeases); $i++) { + $mac = $staticLeases[$i]['mac']; + $ip = $staticLeases[$i]['ip']; + $comment = $staticLeases[$i]['comment']; + $config .= "dhcp-host=$mac,$ip # $comment".PHP_EOL; + } if ($_POST['no-resolv'] == "1") { $config .= "no-resolv".PHP_EOL; } diff --git a/templates/dhcp/static_leases.php b/templates/dhcp/static_leases.php index 638ff064..8dbf412d 100644 --- a/templates/dhcp/static_leases.php +++ b/templates/dhcp/static_leases.php @@ -4,14 +4,18 @@
+
-
+
" class="form-control">
-
+
" class="form-control">
+
+ " class="form-control"> +
@@ -21,12 +25,15 @@
-
+
" class="form-control" autofocus="autofocus">
-
+
" class="form-control">
+
+ " class="form-control"> +
@@ -34,12 +41,15 @@