mirror of
				https://github.com/billz/raspap-webgui.git
				synced 2025-03-01 10:31:47 +00:00 
			
		
		
		
	Cleanup: procedural code into functions
This commit is contained in:
		| @@ -70,6 +70,61 @@ function SaveDHCPConfig($status) | ||||
|     if (($_POST['dhcp-iface'] == "1")) { | ||||
|        $errors = ValidateDHCPInput(); | ||||
|        if (empty($errors)) { | ||||
|            $return = UpdateDnsmasqCfg($iface,$status); | ||||
|         } else { | ||||
|             $status->addMessage($errors, 'danger'); | ||||
|         } | ||||
|         if ($return == 1) { | ||||
|             $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); | ||||
|             return false; | ||||
|         } | ||||
|         $return = UpdateDHCPCfg($iface,$status); | ||||
|  | ||||
|     // process disable dhcp option | ||||
|     } elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { | ||||
|         // remove dhcp conf for selected interface | ||||
|         $return = RemoveDHCPCfg($iface,$status); | ||||
|     } | ||||
|  | ||||
|     if ($return == 0) { | ||||
|         $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); | ||||
|     } else { | ||||
|         $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); | ||||
|         return false; | ||||
|     } | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| function ValidateDHCPInput() | ||||
| { | ||||
|     define('IFNAMSIZ', 16); | ||||
|     $iface = $_POST['interface']; | ||||
|     if (!preg_match('/^[a-zA-Z0-9]+$/', $iface) | ||||
|         || strlen($iface) >= IFNAMSIZ | ||||
|     ) { | ||||
|         $errors .= _('Invalid interface name.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart']) | ||||
|         && !empty($_POST['RangeStart']) | ||||
|     ) {  // allow ''/null ? | ||||
|         $errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd']) | ||||
|         && !empty($_POST['RangeEnd']) | ||||
|     ) {  // allow ''/null ? | ||||
|         $errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { | ||||
|         $errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { | ||||
|         $errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     return $errors; | ||||
| } | ||||
|  | ||||
| function UpdateDnsmasqCfg($iface,$status) | ||||
| { | ||||
|     $config = 'interface='.$iface.PHP_EOL. | ||||
|         'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd']. | ||||
|         ',255.255.255.0,'; | ||||
| @@ -110,18 +165,15 @@ function SaveDHCPConfig($status) | ||||
|     } | ||||
|     file_put_contents("/tmp/dnsmasqdata", $config); | ||||
|     $msg = file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf') ? 'updated' : 'added'; | ||||
|             system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|         } else { | ||||
|             $status->addMessage($errors, 'danger'); | ||||
|         } | ||||
|  | ||||
|         if ($return == 0) { | ||||
|     system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result); | ||||
|     if ($result == 0) { | ||||
|         $status->addMessage('Dnsmasq configuration for '.$iface.' '.$msg.'.', 'success'); | ||||
|         } else { | ||||
|             $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); | ||||
|             return false; | ||||
|     } | ||||
|     return $result; | ||||
| } | ||||
|  | ||||
| function UpdateDHCPCfg($iface,$status) | ||||
| { | ||||
|     $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'); | ||||
| @@ -137,69 +189,42 @@ function SaveDHCPConfig($status) | ||||
|  | ||||
|             // append interface config to dhcpcd.conf | ||||
|             $cfg = $dhcp_conf; | ||||
|                 $cfg[] = '# RaspAP '.$_POST['interface'].' configuration'; | ||||
|                 $cfg[] = 'interface '.$_POST['interface']; | ||||
|             $cfg[] = '# RaspAP '.$iface.' configuration'; | ||||
|             $cfg[] = 'interface '.$iface; | ||||
|             $cfg[] = 'static ip_address='.$ip_address; | ||||
|             $cfg[] = 'static domain_name_server='.$domain_name_server; | ||||
|             $cfg[] = PHP_EOL; | ||||
|             $cfg = join(PHP_EOL, $cfg); | ||||
|             $dhcp_cfg .= $cfg; | ||||
|             file_put_contents("/tmp/dhcpddata", $dhcp_cfg); | ||||
|                 system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return); | ||||
|             system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); | ||||
|             $status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); | ||||
|         } else { | ||||
|             $status->addMessage('DHCP for '.$iface.' already enabled.', 'success'); | ||||
|         } | ||||
|     } | ||||
|     // process disable dhcp option | ||||
|     } elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { | ||||
|         // remove dhcp conf for selected interface | ||||
|     return $result; | ||||
| } | ||||
|  | ||||
| function RemoveDHCPCfg($iface,$status) | ||||
| { | ||||
|     $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); | ||||
|     $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'.*\n(.*\n){3}/m', '', $dhcp_cfg); | ||||
|     file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL); | ||||
|         system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return); | ||||
|     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 eth0 conf | ||||
|         system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|     system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result); | ||||
|     if ($result == 0) { | ||||
|         $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); | ||||
|     } else { | ||||
|         system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return); | ||||
|         $status->addMessage('Failed to remove dnsmasq configuration for '.$iface.'.', 'danger'); | ||||
|     } | ||||
|  | ||||
|     if ($return == 0) { | ||||
|         $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); | ||||
|     } else { | ||||
|         $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); | ||||
|         return false; | ||||
|     } | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| function ValidateDHCPInput() | ||||
| { | ||||
|     define('IFNAMSIZ', 16); | ||||
|     $iface = $_POST['interface']; | ||||
|     if (!preg_match('/^[a-zA-Z0-9]+$/', $iface) | ||||
|         || strlen($iface) >= IFNAMSIZ | ||||
|     ) { | ||||
|         $errors .= _('Invalid interface name.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart']) | ||||
|         && !empty($_POST['RangeStart']) | ||||
|     ) {  // allow ''/null ? | ||||
|         $errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd']) | ||||
|         && !empty($_POST['RangeEnd']) | ||||
|     ) {  // allow ''/null ? | ||||
|         $errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { | ||||
|         $errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { | ||||
|         $errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL; | ||||
|     } | ||||
|     return $errors; | ||||
|     return $result; | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user