From de787c424ab0fc3974296ca8229f24f9bf58c89b Mon Sep 17 00:00:00 2001 From: billz Date: Fri, 4 Dec 2020 14:40:54 +0000 Subject: [PATCH] Create functions from procedural code --- includes/dhcp.php | 32 +++----------- includes/functions.php | 94 +++++++++++++++++++++++++++++++++++++++++- includes/hostapd.php | 42 ++++++++++++------- 3 files changed, 126 insertions(+), 42 deletions(-) diff --git a/includes/dhcp.php b/includes/dhcp.php index e16d4194..362397f8 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -70,8 +70,9 @@ function SaveDHCPConfig($status) // 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); + // remove dhcp + dnsmasq configs for selected interface + $return = removeDHCPConfig($iface,$status); + $return = removeDnsmasqConfig($iface,$status); } else { $errors = ValidateDHCPInput(); if (empty($errors)) { @@ -87,7 +88,6 @@ function SaveDHCPConfig($status) if (($_POST['dhcp-iface'] == "1")) { $return = UpdateDnsmasqConfig($iface,$status); } - if ($return == 0) { $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); } else { @@ -140,7 +140,8 @@ function ValidateDHCPInput() function UpdateDnsmasqConfig($iface,$status) { - $config = 'interface='.$iface.PHP_EOL. + $config = '# RaspAP '.$iface.' configuration'.PHP_EOL; + $config .= 'interface='.$iface.PHP_EOL. 'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd']. ',255.255.255.0,'; if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') { @@ -220,26 +221,3 @@ function UpdateDHCPConfig($iface,$status) return $result; } -function RemoveDHCPConfig($iface,$status) -{ - $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1); - file_put_contents("/tmp/dhcpddata", $dhcp_cfg); - system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); - if ($result == 0) { - $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success'); - } else { - $status->addMessage('Failed to remove DHCP configuration for '.$iface.'.', 'danger'); - return $result; - } - // remove dnsmasq conf - system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result); - if ($result == 0) { - $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); - } else { - $status->addMessage('Failed to remove dnsmasq configuration for '.$iface.'.', 'danger'); - } - return $result; -} - - diff --git a/includes/functions.php b/includes/functions.php index e7508ded..78f4720c 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -19,7 +19,7 @@ function mask2cidr($mask) * * @param string $cidr * @return string -*/ + */ function cidr2mask($cidr) { $ta = substr ($cidr, strpos ($cidr, '/') + 1) * 1; @@ -29,6 +29,98 @@ function cidr2mask($cidr) return join ('.', $netmask); } +/** + * Returns a dhcp default config header + * + * @return array $config + */ +function defaultHeader() +{ + $config = [ '# RaspAP default configuration' ]; + $config[] = 'hostname'; + $config[] = 'clientid'; + $config[] = 'persistent'; + $config[] = 'option rapid_commit'; + $config[] = 'option domain_name_servers, domain_name, domain_search, host_name'; + $config[] = 'option classless_static_routes'; + $config[] = 'option ntp_servers'; + $config[] = 'require dhcp_server_identifier'; + $config[] = 'slaac private'; + $config[] = 'nohook lookup-hostname'; + return $config; +} + +/** + * Removes a dhcp configuration block for the specified interface + * + * @param string $iface + * @param object $status + * @return boolean $result + */ +function removeDHCPConfig($iface,$status) +{ + $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1); + file_put_contents("/tmp/dhcpddata", $dhcp_cfg); + system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); + if ($result == 0) { + $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success'); + } else { + $status->addMessage('Failed to remove DHCP configuration for '.$iface.'.', 'danger'); + return $result; + } +} + +/** + * Removes a dhcp configuration block for the specified interface + * + * @param string $dhcp_cfg + * @param string $iface + * @return string $dhcp_cfg + */ +function removeDHCPIface($dhcp_cfg,$iface) +{ + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1); + return $dhcp_cfg; +} + +/** + * Removes a dnsmasq configuration block for the specified interface + * + * @param string $iface + * @param object $status + * @return boolean $result + */ +function removeDnsmasqConfig($iface,$status) +{ + system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result); + if ($result == 0) { + $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); + } else { + $status->addMessage('Failed to remove dnsmasq configuration for '.$iface.'.', 'danger'); + } + return $result; +} + +/** + * Scans dnsmasq configuration dir for the specified interface + * Non-matching configs are removed, optional adblock.conf is protected + * + * @param string $dir_conf + * @param string $interface + * @param object $status + */ +function scanConfigDir($dir_conf,$interface,$status) +{ + $syscnf = preg_grep('~\.(conf)$~', scandir($dir_conf)); + foreach ($syscnf as $key => $file) { + if ($file !== '090_adblock.conf' && !preg_match('/.*_'.$interface.'.conf/', $file)) { + removeDnsmasqConfig($interface,$status); + } + } + return $status; +} + /* Functions to write ini files */ function write_php_ini($array, $file) diff --git a/includes/hostapd.php b/includes/hostapd.php index 3327365c..901b6f07 100755 --- a/includes/hostapd.php +++ b/includes/hostapd.php @@ -142,7 +142,6 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $bridgedEnable = 1; } } - // Check for WiFi client AP mode checkbox $wifiAPEnable = 0; if ($bridgedEnable == 0) { // enable client mode actions when not bridged @@ -156,7 +155,6 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) } } } - // Check for Logfile output checkbox $logEnable = 0; if ($arrHostapdConf['LogEnable'] == 0) { @@ -185,6 +183,7 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $cfg['WifiManaged'] = $_POST['interface']; write_php_ini($cfg, RASPI_CONFIG.'/hostapd.ini'); $_SESSION['ap_interface'] = $_POST['interface']; + $ap_iface = $_POST['interface']; // Verify input if (empty($_POST['ssid']) || strlen($_POST['ssid']) > 32) { @@ -321,7 +320,12 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) if (!empty($syscfg['dhcp-option'])) { $config[] = 'dhcp-option='.$syscfg['dhcp-option']; } - } else { + $config[] = PHP_EOL; + scanConfigDir('/etc/dnsmasq.d/','uap0',$status); + $config = join(PHP_EOL, $config); + file_put_contents("/tmp/dnsmasqdata", $config); + system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); + } elseif ($bridgedEnable !==1) { // Set dhcp-range from system config. If undefined, fallback to default $dhcp_range = ($syscfg['dhcp-range'] =='192.168.50.50,192.168.50.150,12h' || $syscfg['dhcp-range'] =='') ? '10.3.141.50,10.3.141.255,255.255.255.0,12h' : $syscfg['dhcp-range']; @@ -329,15 +333,14 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $config[] = 'interface='.$_POST['interface']; $config[] = 'domain-needed'; $config[] = 'dhcp-range='.$dhcp_range; - $ap_iface = $_POST['interface']; if (!empty($syscfg['dhcp-option'])) { $config[] = 'dhcp-option='.$syscfg['dhcp-option']; } + $config[] = PHP_EOL; + $config = join(PHP_EOL, $config); + file_put_contents("/tmp/dnsmasqdata", $config); + system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); } - $config[] = PHP_EOL; - $config = join(PHP_EOL, $config); - file_put_contents("/tmp/dnsmasqdata", $config); - system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); // fetch dhcp settings for selected interface // todo: replace fallback values with defaults from network.json @@ -347,16 +350,20 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) // Set dhcp values from system config, fallback to default if undefined if ($bridgedEnable == 1) { - $config[] = '# RaspAP br0 configuration'; - $config[] = 'interface '.$ap_iface; + $config = defaultHeader(); + $config[] = PHP_EOL.'# RaspAP br0 configuration'; + $config[] = 'interface br0'; $config[] = 'denyinterfaces eth0 wlan0'; + $config[] = PHP_EOL; } elseif ($wifiAPEnable == 1) { // Enable uap0 configuration for AP-STA $ip_address = ($jsonData['StaticIP'] == '') ? '192.168.50.1/24' : $jsonData['StaticIP']; - $config = [ '# RaspAP uap0 configuration' ]; - $config[] = 'interface '.$ap_ifacee; + $config = defaultHeader(); + $config[] = PHP_EOL.'# RaspAP uap0 configuration'; + $config[] = 'interface uap0'; $config[] = 'static ip_address='.$ip_address; $config[] = 'nohook wpa_supplicant'; + $config[] = PHP_EOL; } else { // Default wlan0 config $ip_address = ($jsonData['StaticIP'] == '') ? '10.3.141.1/24' : $jsonData['StaticIP']; @@ -369,17 +376,24 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $config[] = 'static ip_address='.$ip_address; $config[] = 'static routers='.$routers; $config[] = 'static domain_name_server='.$domain_name_server; - $config[] = 'metric '.$jsonData['Metric']; + if (! is_null($jsonData['Metric'])) { $config[] = 'metric '.$jsonData['Metric']; } } $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - if (!preg_match('/^interface\s'.$ap_iface.'$/m', $dhcp_cfg)) { + if ($bridgedEnable == 1 || $wifiAPEnable == 1) { + $dhcp_cfg = join(PHP_EOL, $config); + $status->addMessage('DHCP configuration for '.$ap_iface.' enabled.', 'success'); + } elseif (!preg_match('/^interface\s'.$ap_iface.'$/m', $dhcp_cfg)) { $config[] = PHP_EOL; $config= join(PHP_EOL, $config); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'br0'); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'uap0'); $dhcp_cfg .= $config; $status->addMessage('DHCP configuration for '.$ap_iface.' added.', 'success'); } else { $config = join(PHP_EOL, $config); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'br0'); + $dhcp_cfg = removeDHCPIface($dhcp_cfg,'uap0'); $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$ap_iface.'\s.*?(?=\s*^\s*$)/ms', $config, $dhcp_cfg, 1); $status->addMessage('DHCP configuration for '.$ap_iface.' updated.', 'success'); }