'8', '255.255.0.0' => '16', '255.255.128.0' => '17', '255.255.192.0' => '18', '255.255.224.0' => '19', '255.255.240.0' => '20', '255.255.248.0' => '21', '255.255.252.0' => '22', '255.255.254.0' => '23', '255.255.255.0' => '24', '255.255.255.128' => '25', '255.255.255.192' => '26', '255.255.255.224' => '27', '255.255.255.240' => '28', '255.255.255.248' => '29', '255.255.255.252' => '30' ]; // add translations $_SERVER['REQUEST_URI'] = 'settings'; require_once "$docroot/webGui/include/Translations.php"; require_once "$docroot/webGui/include/Helpers.php"; function escapeSSID($text) { return str_replace('"', '\"', $text); } function scanWifi($port) { $wlan = []; exec("iw ".escapeshellarg($port)." scan | grep -P '^BSS|freq:|signal:|SSID:|Authentication suites:' | sed -r ':a;N;\$!ba;s/\\n\\s+/ /g'", $scan); foreach ($scan as $row) { $attr = preg_split('/ (freq|signal|SSID|\* Authentication suites): /', $row); // skip incomplete info if (count($attr) < 4 || (count($attr) == 4 && str_contains($row, 'Authentication suites:'))) continue; $network = $attr[3]; // skip nullified networks if (str_starts_with($network, '\\x00')) continue; if (empty($wlan[$network])) { $wlan[$network] = $attr; // store MAC address only $wlan[$network][0] = substr($wlan[$network][0],4,17); // identify open network $wlan[$network][4] ??= 'open'; } else { // group radio frequencies $wlan[$network][1] .= ' '.$attr[1]; } } return $wlan; } function saveWifi() { global $cfg, $wifi; $text = []; foreach ($wifi as $network => $block) { $text[] = "[$network]"; foreach ($block as $key => $value) $text[] = "$key=\"$value\""; } file_put_contents_atomic($cfg,implode("\n",$text)."\n"); } function saveAttr() { global $tmp, $attr, $md5; $text = []; if (md5(json_encode($attr),true) === $md5) return; foreach ($attr as $network => $block) { $text[] = "[$network]"; foreach ($block as $key => $value) $text[] = "$key=\"$value\""; } file_put_contents_atomic($tmp,implode("\n",$text)."\n"); } switch ($cmd) { case 'list': $load = $_POST['load'] ?? false; $title = _('Connect to WiFi network'); $port = array_key_first($wifi); $carrier = "/sys/class/net/$port/carrier"; $echo = $wlan = []; foreach ($wifi as $network => $block) { if ($network == $port) continue; $wlan[$network][0] = $block['ATTR1'] ?? ''; $wlan[$network][1] = $block['ATTR4'] ?? ''; $wlan[$network][2] = $block['ATTR2'] ?? ''; $wlan[$network][3] = $network; $wlan[$network][4] = $block['ATTR3'] ?? $block['SECURITY'] ?? ''; } if (!$load) $wlan = array_replace_recursive($wlan, scanWifi($port)); if (count($wlan)) { try { $up = @file_get_contents($carrier) == 1; } catch (Exception $e) { $up = false; } $alive = $up ? exec("iw ".escapeshellarg($port)." link 2>/dev/null | grep -Pom1 'SSID: \K.+'") : ''; $state = $up ? _('Connected') : _('Disconnected'); $color = $up ? 'blue' : 'red'; foreach ($wlan as $network => $block) { $attr[$network]['ATTR1'] = $block[0] ?? ''; $attr[$network]['ATTR2'] = $block[2] ?? ''; $attr[$network]['ATTR3'] = $block[4] ?? ''; $attr[$network]['ATTR4'] = $block[1] ?? ''; if (isset($wifi[$network]['GROUP'])) { if ($network == $alive || $wifi[$network]['GROUP'] == 'active') { $echo['active'][] = "
$state:
"; $echo['active'][] = "
$network
"; } else { $echo['saved'][] = empty($echo['saved']) ? "
"._('My networks').":
" : "
 
"; $echo['saved'][] = "
$network
"; } } else { $echo['other'][] = empty($echo['other']) ? "
"._('Other networks').":
" : "
 
"; $echo['other'][] = "
$network
"; } } if (empty($echo['active'])) $echo['active'][] = "
"._('Connected').":
"._('None')."
"; if (empty($echo['saved'])) $echo['saved'][] = "
"._('My networks').":
"._('None')."
"; if (empty($echo['other'])) $echo['other'][] = $load ? "" : "
"._('Other networks').":
"._('None')."
"; $echo['active'] = implode($echo['active']); $echo['saved'] = implode($echo['saved']); $echo['other'] = implode($echo['other']); saveAttr(); } echo json_encode($echo); break; case 'join': if (is_readable($ssl)) extract(parse_ini_file($ssl)); $token = parse_ini_file($var)['csrf_token']; $ssid = escapeSSID(rawurldecode($_POST['ssid'])); $drop = $_POST['task'] == 1; $manual = $_POST['task'] == 3; $user = _var($wifi[$ssid],'USERNAME') && isset($cipher, $key, $iv) ? openssl_decrypt($wifi[$ssid]['USERNAME'], $cipher, $key, 0, $iv) : _var($wifi[$ssid],'USERNAME'); $passwd = _var($wifi[$ssid],'PASSWORD') && isset($cipher, $key, $iv) ? openssl_decrypt($wifi[$ssid]['PASSWORD'], $cipher, $key, 0, $iv) : _var($wifi[$ssid],'PASSWORD'); $join = _var($wifi[$ssid],'AUTOJOIN','no'); $dhcp4 = _var($wifi[$ssid],'DHCP4','yes'); $dns4 = _var($wifi[$ssid],'DNS4','no'); $ip4 = _var($wifi[$ssid],'IP4'); $mask4 = _var($wifi[$ssid],'MASK4','255.255.255.0'); $gw4 = _var($wifi[$ssid],'GATEWAY4'); $server4 = _var($wifi[$ssid],'SERVER4'); $dhcp6 = _var($wifi[$ssid],'DHCP6'); $dns6 = _var($wifi[$ssid],'DNS6','no'); $ip6 = _var($wifi[$ssid],'IP6'); $mask6 = _var($wifi[$ssid],'MASK6','64'); $gw6 = _var($wifi[$ssid],'GATEWAY6'); $server6 = _var($wifi[$ssid],'SERVER6'); $safe = _var($wifi[$ssid],'SECURITY'); $attr1 = $attr[$ssid]['ATTR1'] ?? ''; $attr2 = $attr[$ssid]['ATTR2'] ?? ''; $attr3 = $attr[$ssid]['ATTR3'] ?? ''; $attr4 = $attr[$ssid]['ATTR4'] ?? ''; $ieee1 = strpos($attr3,'IEEE') !== false; $ieee2 = strpos($safe,'IEEE') !== false; $hide0 = ($manual || !$ieee2) && !$ieee1 && $safe != 'auto' ? 'hide' : ''; $hide1 = !$manual && ($safe == 'open' || $attr3 == 'open' || !$attr3) ? 'hide' : ''; $hide2 = $dhcp4 == 'no' ? '' : 'hide'; $hide3 = $dns4 == 'no' ? 'hide' : ''; $hide4 = $dhcp6 == 'no' ? '' : 'hide'; $hide5 = $dhcp6 == '' ? 'hide' : ''; $hide6 = $dns6 == 'no' ? 'hide' : ''; echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; if ($drop && isset($wifi[$ssid])) { echo ""; echo ""; } if ($manual || $safe) { echo ""; } if ($ieee1 || $manual || $safe) echo ""; if ($attr3 || $manual || $safe) echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
 
 
"._('Security')."
"._('Username').":
"._('Password').":
 
"._('IPv4 address assignment').":
"._('IPv4 address').":/
"._('IPv4 default gateway').":
"._('IPv4 DNS assignment').":
"._('DNSv4 server').":
 
"._('IPv6 address assignment').":
"._('IPv6 address').":/
"._('IPv6 default gateway').":
"._('IPv6 DNS assignment').":
"._('DNSv6 server').":
 
"; echo "
"; break; case 'forget': $ssid = escapeSSID(rawurldecode($_POST['ssid'])); if ($wifi[$ssid]['GROUP'] == 'active') exec("/etc/rc.d/rc.wireless stop &>/dev/null &"); unset($wifi[$ssid]); saveWifi(); break; } ?>