mirror of
https://github.com/billz/raspap-webgui.git
synced 2023-10-10 13:37:24 +02:00
Create functions from procedural code
This commit is contained in:
parent
f32d7c8778
commit
de787c424a
@ -70,8 +70,9 @@ function SaveDHCPConfig($status)
|
|||||||
|
|
||||||
// handle disable dhcp option
|
// handle disable dhcp option
|
||||||
if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
|
if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
|
||||||
// remove dhcp conf for selected interface
|
// remove dhcp + dnsmasq configs for selected interface
|
||||||
$return = RemoveDHCPConfig($iface,$status);
|
$return = removeDHCPConfig($iface,$status);
|
||||||
|
$return = removeDnsmasqConfig($iface,$status);
|
||||||
} else {
|
} else {
|
||||||
$errors = ValidateDHCPInput();
|
$errors = ValidateDHCPInput();
|
||||||
if (empty($errors)) {
|
if (empty($errors)) {
|
||||||
@ -87,7 +88,6 @@ function SaveDHCPConfig($status)
|
|||||||
if (($_POST['dhcp-iface'] == "1")) {
|
if (($_POST['dhcp-iface'] == "1")) {
|
||||||
$return = UpdateDnsmasqConfig($iface,$status);
|
$return = UpdateDnsmasqConfig($iface,$status);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($return == 0) {
|
if ($return == 0) {
|
||||||
$status->addMessage('Dnsmasq configuration updated successfully.', 'success');
|
$status->addMessage('Dnsmasq configuration updated successfully.', 'success');
|
||||||
} else {
|
} else {
|
||||||
@ -140,7 +140,8 @@ function ValidateDHCPInput()
|
|||||||
|
|
||||||
function UpdateDnsmasqConfig($iface,$status)
|
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'].
|
'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].
|
||||||
',255.255.255.0,';
|
',255.255.255.0,';
|
||||||
if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') {
|
if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') {
|
||||||
@ -220,26 +221,3 @@ function UpdateDHCPConfig($iface,$status)
|
|||||||
return $result;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,6 +29,98 @@ function cidr2mask($cidr)
|
|||||||
return join ('.', $netmask);
|
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 */
|
/* Functions to write ini files */
|
||||||
|
|
||||||
function write_php_ini($array, $file)
|
function write_php_ini($array, $file)
|
||||||
|
@ -142,7 +142,6 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status)
|
|||||||
$bridgedEnable = 1;
|
$bridgedEnable = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for WiFi client AP mode checkbox
|
// Check for WiFi client AP mode checkbox
|
||||||
$wifiAPEnable = 0;
|
$wifiAPEnable = 0;
|
||||||
if ($bridgedEnable == 0) { // enable client mode actions when not bridged
|
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
|
// Check for Logfile output checkbox
|
||||||
$logEnable = 0;
|
$logEnable = 0;
|
||||||
if ($arrHostapdConf['LogEnable'] == 0) {
|
if ($arrHostapdConf['LogEnable'] == 0) {
|
||||||
@ -185,6 +183,7 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status)
|
|||||||
$cfg['WifiManaged'] = $_POST['interface'];
|
$cfg['WifiManaged'] = $_POST['interface'];
|
||||||
write_php_ini($cfg, RASPI_CONFIG.'/hostapd.ini');
|
write_php_ini($cfg, RASPI_CONFIG.'/hostapd.ini');
|
||||||
$_SESSION['ap_interface'] = $_POST['interface'];
|
$_SESSION['ap_interface'] = $_POST['interface'];
|
||||||
|
$ap_iface = $_POST['interface'];
|
||||||
|
|
||||||
// Verify input
|
// Verify input
|
||||||
if (empty($_POST['ssid']) || strlen($_POST['ssid']) > 32) {
|
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'])) {
|
if (!empty($syscfg['dhcp-option'])) {
|
||||||
$config[] = 'dhcp-option='.$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
|
// 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' ||
|
$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'];
|
$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[] = 'interface='.$_POST['interface'];
|
||||||
$config[] = 'domain-needed';
|
$config[] = 'domain-needed';
|
||||||
$config[] = 'dhcp-range='.$dhcp_range;
|
$config[] = 'dhcp-range='.$dhcp_range;
|
||||||
$ap_iface = $_POST['interface'];
|
|
||||||
if (!empty($syscfg['dhcp-option'])) {
|
if (!empty($syscfg['dhcp-option'])) {
|
||||||
$config[] = 'dhcp-option='.$syscfg['dhcp-option'];
|
$config[] = 'dhcp-option='.$syscfg['dhcp-option'];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
$config[] = PHP_EOL;
|
$config[] = PHP_EOL;
|
||||||
$config = join(PHP_EOL, $config);
|
$config = join(PHP_EOL, $config);
|
||||||
file_put_contents("/tmp/dnsmasqdata", $config);
|
file_put_contents("/tmp/dnsmasqdata", $config);
|
||||||
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return);
|
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return);
|
||||||
|
}
|
||||||
|
|
||||||
// fetch dhcp settings for selected interface
|
// fetch dhcp settings for selected interface
|
||||||
// todo: replace fallback values with defaults from network.json
|
// 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
|
// Set dhcp values from system config, fallback to default if undefined
|
||||||
if ($bridgedEnable == 1) {
|
if ($bridgedEnable == 1) {
|
||||||
$config[] = '# RaspAP br0 configuration';
|
$config = defaultHeader();
|
||||||
$config[] = 'interface '.$ap_iface;
|
$config[] = PHP_EOL.'# RaspAP br0 configuration';
|
||||||
|
$config[] = 'interface br0';
|
||||||
$config[] = 'denyinterfaces eth0 wlan0';
|
$config[] = 'denyinterfaces eth0 wlan0';
|
||||||
|
$config[] = PHP_EOL;
|
||||||
} elseif ($wifiAPEnable == 1) {
|
} elseif ($wifiAPEnable == 1) {
|
||||||
// Enable uap0 configuration for AP-STA
|
// Enable uap0 configuration for AP-STA
|
||||||
$ip_address = ($jsonData['StaticIP'] == '') ? '192.168.50.1/24' : $jsonData['StaticIP'];
|
$ip_address = ($jsonData['StaticIP'] == '') ? '192.168.50.1/24' : $jsonData['StaticIP'];
|
||||||
$config = [ '# RaspAP uap0 configuration' ];
|
$config = defaultHeader();
|
||||||
$config[] = 'interface '.$ap_ifacee;
|
$config[] = PHP_EOL.'# RaspAP uap0 configuration';
|
||||||
|
$config[] = 'interface uap0';
|
||||||
$config[] = 'static ip_address='.$ip_address;
|
$config[] = 'static ip_address='.$ip_address;
|
||||||
$config[] = 'nohook wpa_supplicant';
|
$config[] = 'nohook wpa_supplicant';
|
||||||
|
$config[] = PHP_EOL;
|
||||||
} else {
|
} else {
|
||||||
// Default wlan0 config
|
// Default wlan0 config
|
||||||
$ip_address = ($jsonData['StaticIP'] == '') ? '10.3.141.1/24' : $jsonData['StaticIP'];
|
$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 ip_address='.$ip_address;
|
||||||
$config[] = 'static routers='.$routers;
|
$config[] = 'static routers='.$routers;
|
||||||
$config[] = 'static domain_name_server='.$domain_name_server;
|
$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);
|
$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[] = PHP_EOL;
|
||||||
$config= join(PHP_EOL, $config);
|
$config= join(PHP_EOL, $config);
|
||||||
|
$dhcp_cfg = removeDHCPIface($dhcp_cfg,'br0');
|
||||||
|
$dhcp_cfg = removeDHCPIface($dhcp_cfg,'uap0');
|
||||||
$dhcp_cfg .= $config;
|
$dhcp_cfg .= $config;
|
||||||
$status->addMessage('DHCP configuration for '.$ap_iface.' added.', 'success');
|
$status->addMessage('DHCP configuration for '.$ap_iface.' added.', 'success');
|
||||||
} else {
|
} else {
|
||||||
$config = join(PHP_EOL, $config);
|
$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);
|
$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');
|
$status->addMessage('DHCP configuration for '.$ap_iface.' updated.', 'success');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user