1
0
mirror of https://github.com/billz/raspap-webgui.git synced 2023-10-10 13:37:24 +02:00

Bugfix: add/update/delete config

This commit is contained in:
billz 2020-11-27 16:28:37 +00:00
parent f8bc24ce6f
commit 2ff8f6f179
2 changed files with 43 additions and 36 deletions

View File

@ -39,7 +39,7 @@ if (isset($interface)) {
// fetch dhcpcd.conf settings for interface // fetch dhcpcd.conf settings for interface
$conf = file_get_contents(RASPI_DHCPCD_CONFIG); $conf = file_get_contents(RASPI_DHCPCD_CONFIG);
preg_match('/^#\sRaspAP\s'.$interface.'.*?(?=\s*^\s*$)/ms', $conf, $matched); preg_match('/^#\sRaspAP\s'.$interface.'\s.*?(?=\s*+$)/ms', $conf, $matched);
preg_match('/metric\s(\d*)/', $matched[0], $metric); preg_match('/metric\s(\d*)/', $matched[0], $metric);
preg_match('/static\sip_address=(.*)/', $matched[0], $static_ip); preg_match('/static\sip_address=(.*)/', $matched[0], $static_ip);
preg_match('/static\srouters=(.*)/', $matched[0], $static_routers); preg_match('/static\srouters=(.*)/', $matched[0], $static_routers);

View File

@ -68,7 +68,11 @@ function SaveDHCPConfig($status)
$iface = $_POST['interface']; $iface = $_POST['interface'];
$return = 1; $return = 1;
if (($_POST['dhcp-iface'] == "1")) { // handle disable dhcp option
if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
// remove dhcp conf for selected interface
$return = RemoveDHCPConfig($iface,$status);
} else {
$errors = ValidateDHCPInput(); $errors = ValidateDHCPInput();
if (empty($errors)) { if (empty($errors)) {
$return = UpdateDHCPConfig($iface,$status); $return = UpdateDHCPConfig($iface,$status);
@ -79,12 +83,9 @@ function SaveDHCPConfig($status)
$status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
return false; return false;
} }
$return = UpdateDnsmasqConfig($iface,$status);
// process disable dhcp option if (($_POST['dhcp-iface'] == "1")) {
} elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { $return = UpdateDnsmasqConfig($iface,$status);
// remove dhcp conf for selected interface
$return = RemoveDHCPConfig($iface,$status);
} }
if ($return == 0) { if ($return == 0) {
@ -94,6 +95,7 @@ function SaveDHCPConfig($status)
return false; return false;
} }
return true; return true;
}
} }
function ValidateDHCPInput() function ValidateDHCPInput()
@ -111,6 +113,7 @@ function ValidateDHCPInput()
if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP)) { if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP)) {
$errors .= _('Invalid default gateway.').'<br />'.PHP_EOL; $errors .= _('Invalid default gateway.').'<br />'.PHP_EOL;
} }
if (($_POST['dhcp-iface'] == "1")) {
if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) { if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) {
$errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL; $errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL;
} }
@ -126,6 +129,7 @@ function ValidateDHCPInput()
if ($_POST['Metric'] !== '' && !ctype_digit($_POST['Metric'])) { if ($_POST['Metric'] !== '' && !ctype_digit($_POST['Metric'])) {
$errors .= _('Invalid metric value, not a number.').'<br />'.PHP_EOL; $errors .= _('Invalid metric value, not a number.').'<br />'.PHP_EOL;
} }
}
return $errors; return $errors;
} }
@ -195,29 +199,32 @@ function UpdateDHCPConfig($iface,$status)
if ($_POST['Fallback'] == 1) { if ($_POST['Fallback'] == 1) {
$cfg[] = 'fallback static_'.$iface; $cfg[] = 'fallback static_'.$iface;
} }
$cfg[] = PHP_EOL;
$cfg = join(PHP_EOL, $cfg);
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
if (!preg_match('/^$\s*\z/m', $dhcp_cfg) && !preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) {
echo '===== no ending newline found ====<br>';
}
if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) {
$cfg[] = PHP_EOL;
$cfg = join(PHP_EOL, $cfg);
$dhcp_cfg .= $cfg; $dhcp_cfg .= $cfg;
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result);
$status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); $status->addMessage('DHCP configuration for '.$iface.' added.', 'success');
} else { } else {
$cfg = join(PHP_EOL, $cfg);
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', $cfg, $dhcp_cfg, 1); $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', $cfg, $dhcp_cfg, 1);
file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL);
$status->addMessage('DHCP configuration for '.$iface.' updated.', 'success'); $status->addMessage('DHCP configuration for '.$iface.' updated.', 'success');
} }
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result);
return $result; return $result;
} }
function RemoveDHCPConfig($iface,$status) function RemoveDHCPConfig($iface,$status)
{ {
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', '', $dhcp_cfg, 1); $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1);
file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL.PHP_EOL); file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result);
if ($result == 0) { if ($result == 0) {
$status->addMessage('DHCP configuration for '.$iface.' removed.', 'success'); $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success');