You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.7 KiB

2 years ago
  1. <?php
  2. require_once 'functions.php';
  3. function knownWifiStations(&$networks)
  4. {
  5. // Find currently configured networks
  6. exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return);
  7. foreach ($known_return as $line) {
  8. if (preg_match('/network\s*=/', $line)) {
  9. $network = array('visible' => false, 'configured' => true, 'connected' => false);
  10. } elseif (isset($network) && $network !== null) {
  11. if (preg_match('/^\s*}\s*$/', $line)) {
  12. $networks[$ssid] = $network;
  13. $network = null;
  14. $ssid = null;
  15. } elseif ($lineArr = preg_split('/\s*=\s*/', trim($line))) {
  16. switch (strtolower($lineArr[0])) {
  17. case 'ssid':
  18. $ssid = trim($lineArr[1], '"');
  19. break;
  20. case 'psk':
  21. if (array_key_exists('passphrase', $network)) {
  22. break;
  23. }
  24. case '#psk':
  25. $network['protocol'] = 'WPA';
  26. case 'wep_key0': // Untested
  27. $network['passphrase'] = trim($lineArr[1], '"');
  28. break;
  29. case 'key_mgmt':
  30. if (! array_key_exists('passphrase', $network) && $lineArr[1] === 'NONE') {
  31. $network['protocol'] = 'Open';
  32. }
  33. break;
  34. case 'priority':
  35. $network['priority'] = trim($lineArr[1], '"');
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. function nearbyWifiStations(&$networks, $cached = true)
  43. {
  44. $cacheTime = filemtime(RASPI_WPA_SUPPLICANT_CONFIG);
  45. $cacheKey = "nearby_wifi_stations_$cacheTime";
  46. if ($cached == false) {
  47. deleteCache($cacheKey);
  48. }
  49. $scan_results = cache(
  50. $cacheKey, function () {
  51. exec('sudo wpa_cli -i ' .$_SESSION['wifi_client_interface']. ' scan');
  52. sleep(3);
  53. exec('sudo wpa_cli -i ' .$_SESSION['wifi_client_interface']. ' scan_results', $stdout);
  54. array_shift($stdout);
  55. return implode("\n", $stdout);
  56. }
  57. );
  58. // get the name of the AP. Should be excluded from nearby networks
  59. exec('cat '.RASPI_HOSTAPD_CONFIG.' | sed -rn "s/ssid=(.*)\s*$/\1/p" ', $ap_ssid);
  60. $ap_ssid = $ap_ssid[0];
  61. foreach (explode("\n", $scan_results) as $network) {
  62. $arrNetwork = preg_split("/[\t]+/", $network); // split result into array
  63. if (!array_key_exists(4, $arrNetwork) ||
  64. trim($arrNetwork[4]) == $ap_ssid) {
  65. continue;
  66. }
  67. $ssid = trim($arrNetwork[4]);
  68. // filter SSID string: anything invisible in 7bit ASCII or quotes -> ignore network
  69. if (preg_match('[\x00-\x1f\x7f-\xff\'\`\´\"]', $ssid)) {
  70. continue;
  71. }
  72. // If network is saved
  73. if (array_key_exists($ssid, $networks)) {
  74. $networks[$ssid]['visible'] = true;
  75. $networks[$ssid]['channel'] = ConvertToChannel($arrNetwork[1]);
  76. // TODO What if the security has changed?
  77. } else {
  78. $networks[$ssid] = array(
  79. 'configured' => false,
  80. 'protocol' => ConvertToSecurity($arrNetwork[3]),
  81. 'channel' => ConvertToChannel($arrNetwork[1]),
  82. 'passphrase' => '',
  83. 'visible' => true,
  84. 'connected' => false
  85. );
  86. }
  87. // Save RSSI, if the current value is larger than the already stored
  88. if (array_key_exists(4, $arrNetwork) && array_key_exists($arrNetwork[4], $networks)) {
  89. if (! array_key_exists('RSSI', $networks[$arrNetwork[4]]) || $networks[$ssid]['RSSI'] < $arrNetwork[2]) {
  90. $networks[$ssid]['RSSI'] = $arrNetwork[2];
  91. }
  92. }
  93. }
  94. }
  95. function connectedWifiStations(&$networks)
  96. {
  97. exec('iwconfig ' .$_SESSION['wifi_client_interface'], $iwconfig_return);
  98. foreach ($iwconfig_return as $line) {
  99. if (preg_match('/ESSID:\"([^"]+)\"/i', $line, $iwconfig_ssid)) {
  100. $networks[$iwconfig_ssid[1]]['connected'] = true;
  101. }
  102. }
  103. }
  104. function sortNetworksByRSSI(&$networks)
  105. {
  106. $valRSSI = array();
  107. foreach ($networks as $SSID => $net) {
  108. if (!array_key_exists('RSSI', $net)) {
  109. $net['RSSI'] = -1000;
  110. }
  111. $valRSSI[$SSID] = $net['RSSI'];
  112. }
  113. $nets = $networks;
  114. arsort($valRSSI);
  115. $networks = array();
  116. foreach ($valRSSI as $SSID => $RSSI) {
  117. $networks[$SSID] = $nets[$SSID];
  118. $networks[$SSID]['RSSI'] = $RSSI;
  119. }
  120. }
  121. /*
  122. * Determines the configured wireless AP interface
  123. *
  124. * If not saved in /etc/raspap/hostapd.ini, check for a second
  125. * wireless interface with iw dev. Fallback to the constant
  126. * value defined in config.php
  127. */
  128. function getWifiInterface()
  129. {
  130. $arrHostapdConf = parse_ini_file(RASPI_CONFIG.'/hostapd.ini');
  131. $iface = $_SESSION['ap_interface'] = isset($arrHostapdConf['WifiInterface']) ? $arrHostapdConf['WifiInterface'] : RASPI_WIFI_AP_INTERFACE;
  132. // check for 2nd wifi interface -> wifi client on different interface
  133. exec("iw dev | awk '$1==\"Interface\" && $2!=\"$iface\" {print $2}'",$iface2);
  134. $client_iface = $_SESSION['wifi_client_interface'] = (empty($iface2) ? $iface : trim($iface2[0]));
  135. // specifically for rpi0W in AP-STA mode, the above check ends up with the interfaces
  136. // crossed over (wifi_client_interface vs 'ap_interface'), because the second interface (uap0) is
  137. // created by raspap and used as the access point.
  138. if ($client_iface == "uap0" && ($arrHostapdConf['WifiAPEnable'] ?? 0)){
  139. $_SESSION['wifi_client_interface'] = $iface;
  140. $_SESSION['ap_interface'] = $client_iface;
  141. }
  142. }
  143. ?>