raspap-webgui/ajax/networking/get_netcfg.php

69 lines
3.1 KiB
PHP
Raw Normal View History

2020-11-16 18:18:51 +00:00
<?php
require '../../includes/csrf.php';
2025-01-21 04:59:57 -08:00
require_once '../../includes/session.php';
2020-11-16 18:18:51 +00:00
require_once '../../includes/config.php';
require_once '../../src/RaspAP/Auth/HTTPAuth.php';
require_once '../../includes/authenticate.php';
2020-11-16 18:18:51 +00:00
$interface = $_POST['iface'];
2020-11-16 18:18:51 +00:00
if (isset($interface)) {
2020-11-26 17:15:42 +00:00
// fetch dnsmasq.conf settings for interface
2021-05-02 07:15:12 +01:00
exec('cat '. escapeshellarg(RASPI_DNSMASQ_PREFIX.$interface.'.conf'), $return);
2020-11-16 18:18:51 +00:00
$conf = ParseConfig($return);
$dhcpdata['DHCPEnabled'] = empty($conf) ? false : true;
2024-02-08 08:38:09 +01:00
if (is_string($conf['dhcp-range'])) {
$arrRange = explode(",", $conf['dhcp-range']);
} else {
$arrRange = explode(",", $conf['dhcp-range'][0]);
}
$dhcpdata['RangeStart'] = $arrRange[0] ?? null;
$dhcpdata['RangeEnd'] = $arrRange[1] ?? null;
$dhcpdata['RangeMask'] = $arrRange[2] ?? null;
$dhcpdata['leaseTime'] = $arrRange[3] ?? null;
$dhcpHost = $conf["dhcp-host"] ?? null;
2020-11-16 18:18:51 +00:00
$dhcpHost = empty($dhcpHost) ? [] : $dhcpHost;
$dhcpdata['dhcpHost'] = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ];
$upstreamServers = is_array($conf['server'] ?? null) ? $conf['server'] : [ $conf['server'] ?? '' ];
2020-11-17 11:18:10 +00:00
$dhcpdata['upstreamServersEnabled'] = empty($conf['server']) ? false: true;
2020-11-26 17:15:42 +00:00
$dhcpdata['upstreamServers'] = array_filter($upstreamServers);
2020-11-16 18:18:51 +00:00
preg_match('/([0-9]*)([a-z])/i', $dhcpdata['leaseTime'], $arrRangeLeaseTime);
$dhcpdata['leaseTime'] = $arrRangeLeaseTime[1];
$dhcpdata['leaseTimeInterval'] = $arrRangeLeaseTime[2];
if (isset($conf['dhcp-option'])) {
$arrDns = explode(",", $conf['dhcp-option']);
if ($arrDns[0] == '6') {
if (count($arrDns) > 1) {
$dhcpdata['DNS1'] = $arrDns[1] ?? null;
2020-11-16 18:18:51 +00:00
}
if (count($arrDns) > 2) {
$dhcpdata['DNS2'] = $arrDns[2] ?? null;
2020-11-16 18:18:51 +00:00
}
}
}
2020-11-26 17:15:42 +00:00
// fetch dhcpcd.conf settings for interface
$conf = file_get_contents(RASPI_DHCPCD_CONFIG);
2020-11-27 16:28:37 +00:00
preg_match('/^#\sRaspAP\s'.$interface.'\s.*?(?=\s*+$)/ms', $conf, $matched);
2020-11-26 17:15:42 +00:00
preg_match('/metric\s(\d*)/', $matched[0], $metric);
preg_match('/static\sip_address=(.*)/', $matched[0], $static_ip);
preg_match('/static\srouters=(.*)/', $matched[0], $static_routers);
2020-12-01 18:39:23 +00:00
preg_match('/static\sdomain_name_server=(.*)/', $matched[0], $static_dns);
2020-11-26 17:15:42 +00:00
preg_match('/fallback\sstatic_'.$interface.'/', $matched[0], $fallback);
preg_match('/(?:no)?gateway/', $matched[0], $gateway);
2023-05-19 12:18:25 +01:00
preg_match('/nohook\swpa_supplicant/', $matched[0], $nohook_wpa_supplicant);
$dhcpdata['Metric'] = $metric[1] ?? null;
$dhcpdata['StaticIP'] = isset($static_ip[1]) && strpos($static_ip[1], '/') !== false
? substr($static_ip[1], 0, strpos($static_ip[1], '/'))
: ($static_ip[1] ?? '');
$dhcpdata['SubnetMask'] = cidr2mask($static_ip[1] ?? '');
$dhcpdata['StaticRouters'] = $static_routers[1] ?? null;
$dhcpdata['StaticDNS'] = $static_dns[1] ?? null;
2020-11-26 17:15:42 +00:00
$dhcpdata['FallbackEnabled'] = empty($fallback) ? false: true;
2022-01-02 19:31:07 +00:00
$dhcpdata['DefaultRoute'] = $gateway[0] == "gateway";
$dhcpdata['NoHookWPASupplicant'] = ($nohook_wpa_supplicant[0] ?? '') == "nohook wpa_supplicant";
2020-11-16 18:18:51 +00:00
echo json_encode($dhcpdata);
}