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

Enable DHCP option for all interfaces

This commit is contained in:
billz 2020-11-15 10:55:33 +00:00
parent d440dd805c
commit daa6b877e6

View File

@ -83,21 +83,23 @@ function DisplayDHCPConfig()
$config .= "conf-dir=/etc/dnsmasq.d".PHP_EOL;
file_put_contents("/tmp/dnsmasqdata", $config);
// handle DHCP for eth0 option
if ($_POST['dhcp-eth0'] == "1" && $_POST['interface'] == "eth0") {
if (!file_exists(RASPI_CONFIG_NETWORKING.'/eth0.ini') || filesize(RASPI_CONFIG_NETWORKING.'/eth0.ini') ==0 ) {
$status->addMessage('Static IP address for eth0 not found.', 'danger');
$status->addMessage('Configure this interface in Networking > eth0.', 'danger');
$iface = $_POST['interface'];
// handle DHCP for selected interface option
if ($_POST['dhcp-iface'] == "1") {
$net_cfg = RASPI_CONFIG_NETWORKING.'/'.$iface.'.ini';
if (!file_exists($net_cfg) || filesize($net_cfg) ==0 ) {
$status->addMessage('Static IP address for '.$iface.' not found.', 'danger');
$status->addMessage('Configure this interface in Networking > '.$iface.'.', 'danger');
$return = 1;
} else {
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
if (!preg_match('/^interface\seth0$/m', $dhcp_cfg)) {
if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) {
// set dhcp values from ini
$eth0_cfg = parse_ini_file(RASPI_CONFIG_NETWORKING.'/eth0.ini', false, INI_SCANNER_RAW);
$ip_address = $eth0_cfg['ip_address'];
$domain_name_server = ($eth0_cfg['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $eth0_cfg['domain_name_server'];
$iface_cfg = parse_ini_file($net_cfg, false, INI_SCANNER_RAW);
$ip_address = $iface_cfg['ip_address'];
$domain_name_server = ($iface_cfg['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $iface_cfg['domain_name_server'];
// append eth0 config to dhcpcd.conf
// append interface config to dhcpcd.conf
$cfg = $dhcp_conf;
$cfg[] = '# RaspAP '.$_POST['interface'].' configuration';
$cfg[] = 'interface '.$_POST['interface'];
@ -108,23 +110,24 @@ function DisplayDHCPConfig()
$dhcp_cfg .= $cfg;
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return);
$status->addMessage('DHCP configuration for eth0 added.', 'success');
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_ETH0, $return);
$status->addMessage('Dnsmasq configuration for eth0 added.', 'success');
$status->addMessage('DHCP configuration for '.$iface.' added.', 'success');
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return);
$status->addMessage('Dnsmasq configuration for '.$iface.' added.', 'success');
} else {
$status->addMessage('DHCP for '.$_POST['interface'].' already enabled.', 'danger');
$status->addMessage('DHCP for '.$iface.' already enabled.', 'danger');
}
}
} elseif (!isset($_POST['dhcp-eth0']) && file_exists(RASPI_DNSMASQ_ETH0)) {
// remove dhcpcd eth0 conf
} elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
// remove dhcp conf for selected interface
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
$dhcp_cfg = preg_replace('/^#\sRaspAP\seth0.*(\n.*){3,}/m', '', $dhcp_cfg);
// todo: improve by removing extra blank lines
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'.*\n(.*\n){3}/m', '', $dhcp_cfg);
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return);
$status->addMessage('DHCP configuration for eth0 removed.', 'success');
$status->addMessage('DHCP configuration for '.$iface.' removed.', 'success');
// remove dnsmasq eth0 conf
system('sudo rm '.RASPI_DNSMASQ_ETH0, $return);
$status->addMessage('Dnsmasq configuration for eth0 removed.', 'success');
system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return);
$status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success');
} else {
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_CONFIG, $return);
}
@ -221,8 +224,8 @@ function DisplayDHCPConfig()
break;
}
}
if (file_exists(RASPI_DNSMASQ_ETH0)) {
$dhcp_eth0 = 1;
if (file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
$dhcp_iface_enable = 1;
}
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
@ -246,7 +249,7 @@ function DisplayDHCPConfig()
"dhcpHost",
"interfaces",
"leases",
"dhcp_eth0"
"dhcp_iface_enable"
)
);
}