Migrate non-hostapd methods to HotspotService class

This commit is contained in:
billz
2025-07-20 02:05:05 -07:00
parent 5f4469ab32
commit 636e04fa78

View File

@@ -1,8 +1,7 @@
<?php
use RaspAP\Networking\Hotspot\DnsmasqManager;
use RaspAP\Networking\Hotspot\HostapdManager;
use RaspAP\Networking\Hotspot\DhcpcdManager;
use RaspAP\Networking\Hotspot\HotspotService;
use RaspAP\Networking\Hotspot\WiFiManager;
use RaspAP\Messages\StatusMessage;
use RaspAP\System\Sysinfo;
@@ -17,19 +16,20 @@ $wifi->getWifiInterface();
function DisplayHostAPDConfig()
{
$hostapd = new HostapdManager();
$hotspot = new HotspotService();
$status = new StatusMessage();
$system = new Sysinfo();
$operatingSystem = $system->operatingSystem();
// set hostapd defaults
$arr80211Standard = $hostapd->get80211Standards();
$arrSecurity = $hostapd->getSecurityModes();
$arrEncType = $hostapd->getEncTypes();
$arr80211w = $hostapd->get80211wOptions();
$arr80211Standard = $hotspot->get80211Standards();
$arrSecurity = $hotspot->getSecurityModes();
$arrEncType = $hotspot->getEncTypes();
$arr80211w = $hotspot->get80211wOptions();
$languageCode = strtok($_SESSION['locale'], '_');
$countryCodes = getCountryCodes($languageCode);
$reg_domain = $hostapd->getRegDomain();
$interfaces = $hostapd->getInterfaces();
$reg_domain = $hotspot->getRegDomain();
$interfaces = $hotspot->getInterfaces();
$arrTxPower = getDefaultNetOpts('txpower','dbm');
$managedModeEnabled = false;
@@ -38,8 +38,8 @@ function DisplayHostAPDConfig()
} else {
$interface = $_SESSION['ap_interface'];
}
$txpower = $hostapd->getTxPower($interface);
$arrHostapdConf = $hostapd->getHostapdIni();
$txpower = $hotspot->getTxPower($interface);
$arrHostapdConf = $hotspot->getHostapdIni();
if (!RASPI_MONITOR_ENABLED) {
if (isset($_POST['StartHotspot']) || isset($_POST['RestartHotspot'])) {
@@ -61,7 +61,7 @@ function DisplayHostAPDConfig()
$status->addMessage($line, 'info');
}
} elseif (isset($_POST['SaveHostAPDSettings'])) {
saveHostapdConfig($arrSecurity, $arrEncType, $arr80211Standard, $interfaces, $reg_domain, $status);
$hotspot->saveSettings($_POST, $arrSecurity, $arrEncType, $arr80211Standard, $interfaces, $reg_domain, $status);
} elseif (isset($_POST['StopHotspot'])) {
$status->addMessage('Attempting to stop hotspot', 'info');
exec('sudo /bin/systemctl stop hostapd.service', $return);
@@ -82,9 +82,9 @@ function DisplayHostAPDConfig()
if (isset($_POST['txpower'])) {
if ($_POST['txpower'] != 'auto') {
$txpower = intval($_POST['txpower']);
$hostapd->maybeSetTxPower($interface, $txpower, $status);
$hotspot->maybeSetTxPower($interface, $txpower, $status);
} elseif ($_POST['txpower'] == 'auto') {
$hostapd->maybeSetTxPower($interface, 'auto', $status);
$hotspot->maybeSetTxPower($interface, 'auto', $status);
}
$txpower = $_POST['txpower'];
}
@@ -127,96 +127,3 @@ function DisplayHostAPDConfig()
);
}
/**
* Validates user input + saves configs for hostapd, dnsmasq & dhcp
*
* @param array $wpa_array
* @param array $enc_types
* @param array $modes
* @param string $interface
* @param string $reg_domain
* @param object $status
* @return boolean
*/
function saveHostapdConfig($wpa_array, $enc_types, $modes, $interfaces, $reg_domain, $status)
{
$hostapd = new HostapdManager();
$dnsmasq = new DnsmasqManager();
$dhcpcd = new DhcpcdManager();
$arrHostapdConf = $hostapd->getHostapdIni();
$dualAPEnable = false;
// derive mode states
$states = $hostapd->deriveModeStates($_POST, $arrHostapdConf);
// determine base interface (validated or fallback)
$baseIface = validateInterface($_POST['interface']) ? $_POST['interface'] : RASPI_WIFI_AP_INTERFACE;
// derive interface roles
[$apIface, $cliIface, $sessionIface] = $hostapd->deriveInterfaces($baseIface, $states);
// persist hostapd.ini
$hostapd->persistHostapdIni($states, $apIface, $cliIface, $arrHostapdConf);
// store session (compatibility)
$_SESSION['ap_interface'] = $sessionIface;
// validate config from $_POST
$validated = $hostapd->validate($_POST, $wpa_array, $enc_types, $modes, $interfaces, $reg_domain, $status);
if ($validated !== false) {
try {
// normalize state flags
$validated['interface'] = $apIface;
$validated['bridge'] = !empty($states['BridgedEnable']);
$validated['apsta'] = !empty($states['WifiAPEnable']);
$validated['repeater'] = !empty($states['RepeaterEnable']);
$validated['dualmode'] = !empty($states['DualAPEnable']);
$validated['txpower'] = $txpower;
// hostapd
$config = $hostapd->buildConfig($validated, $status);
$hostapd->saveConfig($config, $dualAPEnable, $validated['interface']);
$status->addMessage('WiFi hotspot settings saved.', 'success');
// dnsmasq
try {
$syscfg = $dnsmasq->getConfig($ap_iface ?? RASPI_WIFI_AP_INTERFACE);
} catch (\RuntimeException $e) {
error_log('Error: ' . $e->getMessage());
}
try {
$dnsmasqConfig = $dnsmasq->buildConfig(
$syscfg,
$validated['interface'],
$validated['apsta'],
$validated['bridge']
);
$dnsmasq->saveConfig($dnsmasqConfig, $validated['interface']);
} catch (\RuntimeException $e) {
error_log('Error: ' . $e->getMessage());
}
// dhcpcd
try {
$return = $dhcpcd->buildConfig(
$validated['interface'],
$validated['bridge'],
$validated['repeater'],
$validated['apsta'],
$validated['dualmode'],
$status
);
} catch (\RuntimeException $e) {
error_log('Error: ' . $e->getMessage());
}
} catch (\Throwable $e) {
error_log('Error: ' . $e->getMessage());
$status->addMessage('Unable to save WiFi hotspot settings', 'danger');
}
}
return true;
}