Skip to content

Commit

Permalink
Add DHCPSubnetMask option WebUI
Browse files Browse the repository at this point in the history
New DHCPSubnetMask option for the WebUI DHCP settings:

/settings.php
* a default value of 255.255.255.0
* new field in the DHCP form. changing the router field css so that subnet should appear alongside router field. Similar to the To: & From: IP range fields.

../savesettings.php
* Handler for DHCPSubnetMask form submission
* New positional parameter for pihole_execute call for enabledhcp function.

../func.php
* validSubnetMask function to validate format of input value

Signed-off-by: biodigitalfish <11165243+biodigitalfish@users.noreply.github.com>
  • Loading branch information
biodigitalfish committed Nov 2, 2023
1 parent e38522b commit 9bae2f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
22 changes: 22 additions & 0 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ function validIP($address)
return !filter_var($address, FILTER_VALIDATE_IP) === false;
}

function validSubnetMask($subnetMask)
{
$octets = explode('.', $subnetMask);
if (count($octets) != 4) {
return false;
}

$subnetBinary = '';
foreach ($octets as $octet) {
if (!is_numeric($octet) || $octet < 0 || $octet > 255) {
return false;
}
$subnetBinary .= str_pad(decbin($octet), 8, '0', STR_PAD_LEFT);
}

if (strpos($subnetBinary, '01') !== false) {
return false;
}

return true;
}

function validCIDRIP($address)
{
// This validation strategy has been taken from ../js/groups-common.js
Expand Down
8 changes: 7 additions & 1 deletion scripts/pi-hole/php/savesettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ function addStaticDHCPLease($mac, $ip, $hostname)
$error .= 'To IP ('.htmlspecialchars($to).') is invalid!<br>';
}

// Validate to Subnet Mask
$subnetMask = $_POST['subnetMask'];
if (!validSubnetMask($subnetMask)) {
$error .= 'Subnet Mask ('.htmlspecialchars($subnetMask).') is invalid!<br>';
}

// Validate router IP
$router = $_POST['router'];
if (!validIP($router)) {
Expand Down Expand Up @@ -531,7 +537,7 @@ function addStaticDHCPLease($mac, $ip, $hostname)
}

if (!strlen($error)) {
pihole_execute('-a enabledhcp '.$from.' '.$to.' '.$router.' '.$leasetime.' '.$domain.' '.$ipv6.' '.$rapidcommit);
pihole_execute('-a enabledhcp '.$from.' '.$to.' '.$subnetMask.' '.$router.' '.$leasetime.' '.$domain.' '.$ipv6.' '.$rapidcommit);
$success .= 'The DHCP server has been activated '.htmlspecialchars($type);
}
} else {
Expand Down
20 changes: 19 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@
} else {
$DHCPend = '';
}
if (isset($setupVars['DHCP_SUBNET_MASK'])) {
$DHCPSubnetMask = $setupVars['DHCP_SUBNET_MASK'];
} else {
$DHCPSubnetMask = '255.255.255.0'; // Setting default value here
}
if (isset($setupVars['DHCP_ROUTER'])) {
$DHCProuter = $setupVars['DHCP_ROUTER'];
} else {
Expand Down Expand Up @@ -442,6 +447,7 @@
$DHCP = false;
$DHCPstart = '';
$DHCPend = '';
$DHCPsubnetMask = '255.255.255.0';
$DHCProuter = '';

// Try to guess initial settings
Expand Down Expand Up @@ -506,7 +512,19 @@
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="col-xs-12 col-sm-6 col-md-12 col-lg-6">
<label>Subnet Mask</label>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">Subnet</div>
<input type="text" class="form-control DHCPgroup" name="subnetMask"
autocomplete="off" spellcheck="false" autocapitalize="none"
autocorrect="off" value="<?php echo $DHCPSubnetMask; ?>"
<?php if (!$DHCP) { ?>disabled<?php } ?>>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-12 col-lg-6">
<label>Router (gateway) IP address</label>
<div class="form-group">
<div class="input-group">
Expand Down

0 comments on commit 9bae2f3

Please sign in to comment.