mirror of
https://github.com/billz/raspap-webgui.git
synced 2025-12-27 15:34:28 +01:00
188 lines
6.3 KiB
PHP
Executable File
188 lines
6.3 KiB
PHP
Executable File
<?php
|
|
|
|
require_once 'config.php';
|
|
|
|
use RaspAP\Networking\Hotspot\DhcpcdManager;
|
|
use RaspAP\Networking\Hotspot\DnsmasqManager;
|
|
use RaspAP\Networking\Hotspot\WiFiManager;
|
|
use RaspAP\Messages\StatusMessage;
|
|
|
|
/**
|
|
* Displays DHCP configuration
|
|
*/
|
|
function DisplayDHCPConfig()
|
|
{
|
|
$status = new StatusMessage();
|
|
$wifi = new WiFiManager();
|
|
$wifi->getWifiInterface();
|
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
if (isset($_POST['savedhcpdsettings'])) {
|
|
saveDHCPConfig($status);
|
|
}
|
|
}
|
|
exec('pidof dnsmasq | wc -l', $dnsmasq);
|
|
$dnsmasq_state = ($dnsmasq[0] > 0);
|
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
if (isset($_POST['startdhcpd'])) {
|
|
if ($dnsmasq_state) {
|
|
$status->addMessage('dnsmasq already running', 'info');
|
|
} else {
|
|
exec('sudo /bin/systemctl start dnsmasq.service', $dnsmasq, $return);
|
|
if ($return == 0) {
|
|
$status->addMessage('Successfully started dnsmasq', 'success');
|
|
$dnsmasq_state = true;
|
|
} else {
|
|
$status->addMessage('Failed to start dnsmasq', 'danger');
|
|
}
|
|
}
|
|
} elseif (isset($_POST['stopdhcpd'])) {
|
|
if ($dnsmasq_state) {
|
|
exec('sudo /bin/systemctl stop dnsmasq.service', $dnsmasq, $return);
|
|
if ($return == 0) {
|
|
$status->addMessage('Successfully stopped dnsmasq', 'success');
|
|
$dnsmasq_state = false;
|
|
} else {
|
|
$status->addMessage('Failed to stop dnsmasq', 'danger');
|
|
}
|
|
} else {
|
|
$status->addMessage('dnsmasq already stopped', 'info');
|
|
}
|
|
}
|
|
}
|
|
$ap_iface = $_SESSION['ap_interface'];
|
|
$serviceStatus = $dnsmasq_state ? "up" : "down";
|
|
exec('cat '. RASPI_DNSMASQ_PREFIX.'raspap.conf', $return);
|
|
$log_dhcp = (preg_grep('/log-dhcp/', $return));
|
|
$log_queries = (preg_grep('/log-queries/', $return));
|
|
$conf = ParseConfig($return);
|
|
exec('cat '. RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return);
|
|
$conf = array_merge(ParseConfig($return));
|
|
$hosts = (array)($conf['dhcp-host'] ?? []);
|
|
$upstreamServers = (array)($conf['server'] ?? []);
|
|
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
|
|
exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
|
|
|
|
count($log_dhcp) > 0 ? $conf['log-dhcp'] = true : false ;
|
|
count($log_queries) > 0 ? $conf['log-queries'] = true : false ;
|
|
|
|
exec('sudo /bin/chmod o+r '.RASPI_DHCPCD_LOG);
|
|
$logdata = getLogLimited(RASPI_DHCPCD_LOG);
|
|
|
|
echo renderTemplate(
|
|
"dhcp", compact(
|
|
"status",
|
|
"serviceStatus",
|
|
"dnsmasq_state",
|
|
"ap_iface",
|
|
"conf",
|
|
"hosts",
|
|
"upstreamServers",
|
|
"interfaces",
|
|
"leases",
|
|
"logdata"
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Saves a DHCP configuration
|
|
*
|
|
* @return object $status
|
|
*/
|
|
function saveDHCPConfig($status)
|
|
{
|
|
$dhcpcd = new DhcpcdManager();
|
|
$dnsmasq = new DnsmasqManager();
|
|
$iface = $_POST['interface'];
|
|
$return = 1;
|
|
|
|
// dhcp
|
|
if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
|
|
// remove dhcp + dnsmasq configs for selected interface
|
|
$return = $dhcpcd->remove($iface, $status);
|
|
$return = $dnsmasq->remove($iface, $status);
|
|
} else {
|
|
$errors = $dhcpcd->validate($_POST);
|
|
if (empty($errors)) {
|
|
$return = updateDHCPConfig($iface, $status);
|
|
} else {
|
|
foreach ($errors as $error) {
|
|
$status->addMessage($error, 'danger');
|
|
}
|
|
}
|
|
if ($return == 1) {
|
|
$status->addMessage('DHCP configuration failed to be updated.', 'danger');
|
|
return false;
|
|
}
|
|
|
|
// dnsmasq
|
|
if (($_POST['dhcp-iface'] == "1") || (isset($_POST['mac']))) {
|
|
$errors = $dnsmasq->validate($_POST);
|
|
if (empty($errors)) {
|
|
$config = $dnsmasq->buildEx($iface, $_POST);
|
|
$return = $dnsmasq->saveConfig($config, $iface);
|
|
$config = $dnsmasq->buildDefault();
|
|
$return = $dnsmasq->saveConfigDefault($config);
|
|
} else {
|
|
foreach ($errors as $error) {
|
|
$status->addMessage($error, 'danger');
|
|
}
|
|
$return = 1;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates a dhcp configuration
|
|
*
|
|
* @param string $iface
|
|
* @param object $status
|
|
* @return boolean $result
|
|
*/
|
|
function updateDHCPConfig($iface,$status)
|
|
{
|
|
$cfg[] = '# RaspAP '.$iface.' configuration';
|
|
$cfg[] = 'interface '.$iface;
|
|
if (isset($_POST['StaticIP']) && $_POST['StaticIP'] !== '') {
|
|
$mask = ($_POST['SubnetMask'] !== '' && $_POST['SubnetMask'] !== '0.0.0.0') ? '/'.mask2cidr($_POST['SubnetMask']) : null;
|
|
$cfg[] = 'static ip_address='.$_POST['StaticIP'].$mask;
|
|
}
|
|
if (isset($_POST['DefaultGateway']) && $_POST['DefaultGateway'] !== '') {
|
|
$cfg[] = 'static routers='.$_POST['DefaultGateway'];
|
|
}
|
|
if ($_POST['DNS1'] !== '' || $_POST['DNS2'] !== '') {
|
|
$cfg[] = 'static domain_name_server='.$_POST['DNS1'].' '.$_POST['DNS2'];
|
|
}
|
|
if ($_POST['Metric'] !== '') {
|
|
$cfg[] = 'metric '.$_POST['Metric'];
|
|
}
|
|
if (($_POST['Fallback'] ?? 0) == 1) {
|
|
$cfg[] = 'profile static_'.$iface;
|
|
$cfg[] = 'fallback static_'.$iface;
|
|
}
|
|
$cfg[] = ($_POST['DefaultRoute'] ?? '') == '1' ? 'gateway' : 'nogateway';
|
|
if (substr($iface, 0, 2) === "wl" && ($_POST['NoHookWPASupplicant'] ?? '') == '1') {
|
|
$cfg[] = 'nohook wpa_supplicant';
|
|
}
|
|
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
|
|
if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) {
|
|
$cfg[] = PHP_EOL;
|
|
$cfg = join(PHP_EOL, $cfg);
|
|
$dhcp_cfg .= $cfg;
|
|
$status->addMessage('DHCP configuration for '.$iface.' added.', 'success');
|
|
} else {
|
|
$cfg = join(PHP_EOL, $cfg);
|
|
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', $cfg, $dhcp_cfg, 1);
|
|
$status->addMessage('DHCP configuration for '.$iface.' updated.', 'success');
|
|
}
|
|
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
|
|
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result);
|
|
|
|
return $result;
|
|
}
|
|
|