diff --git a/ajax/networking/wifi_stations.php b/ajax/networking/wifi_stations.php new file mode 100644 index 00000000..e04c2612 --- /dev/null +++ b/ajax/networking/wifi_stations.php @@ -0,0 +1,94 @@ + false, 'configured' => true, 'connected' => false); + } elseif ($network !== null) { + if (preg_match('/^\s*}\s*$/', $line)) { + $networks[$ssid] = $network; + $network = null; + $ssid = null; + } elseif ($lineArr = preg_split('/\s*=\s*/', trim($line))) { + switch (strtolower($lineArr[0])) { + case 'ssid': + $ssid = trim($lineArr[1], '"'); + break; + case 'psk': + if (array_key_exists('passphrase', $network)) { + break; + } + case '#psk': + $network['protocol'] = 'WPA'; + case 'wep_key0': // Untested + $network['passphrase'] = trim($lineArr[1], '"'); + break; + case 'key_mgmt': + if (! array_key_exists('passphrase', $network) && $lineArr[1] === 'NONE') { + $network['protocol'] = 'Open'; + } + break; + case 'priority': + $network['priority'] = trim($lineArr[1], '"'); + break; + } + } + } + } + + exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' scan'); + sleep(3); + exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' scan_results', $scan_return); + array_shift($scan_return); + + foreach ($scan_return as $network) { + $arrNetwork = preg_split("/[\t]+/", $network); // split result into array + + // If network is saved + if (array_key_exists(4, $arrNetwork) && array_key_exists($arrNetwork[4], $networks)) { + $networks[$arrNetwork[4]]['visible'] = true; + $networks[$arrNetwork[4]]['channel'] = ConvertToChannel($arrNetwork[1]); + // TODO What if the security has changed? + } else { + $networks[$arrNetwork[4]] = array( + 'configured' => false, + 'protocol' => ConvertToSecurity($arrNetwork[3]), + 'channel' => ConvertToChannel($arrNetwork[1]), + 'passphrase' => '', + 'visible' => true, + 'connected' => false + ); + } + + // Save RSSI + if (array_key_exists(4, $arrNetwork)) { + $networks[$arrNetwork[4]]['RSSI'] = $arrNetwork[2]; + } + } + + exec('iwconfig ' . RASPI_WIFI_CLIENT_INTERFACE, $iwconfig_return); + foreach ($iwconfig_return as $line) { + if (preg_match('/ESSID:\"([^"]+)\"/i', $line, $iwconfig_ssid)) { + $networks[$iwconfig_ssid[1]]['connected'] = true; + } + } + + return renderTemplate('wifi_stations', compact('networks')); +}); diff --git a/dist/css/custom.css b/dist/css/custom.css index 8901c950..6177909c 100644 --- a/dist/css/custom.css +++ b/dist/css/custom.css @@ -69,3 +69,9 @@ pre.unstyled { margin-top: 0.5em; margin-bottom: 0.5em; } + +.loading-spinner { + background: url("../../img/loading-spinner.gif") no-repeat scroll center center transparent; + min-height: 150px; + width: 100%; +} diff --git a/img/loading-spinner.gif b/img/loading-spinner.gif new file mode 100644 index 00000000..f7e0378a Binary files /dev/null and b/img/loading-spinner.gif differ diff --git a/includes/config.php b/includes/config.php index 8dfd9173..0931caf5 100755 --- a/includes/config.php +++ b/includes/config.php @@ -5,6 +5,7 @@ define('RASPI_CONFIG', '/etc/raspap'); define('RASPI_CONFIG_NETWORKING', RASPI_CONFIG.'/networking'); define('RASPI_ADMIN_DETAILS', RASPI_CONFIG.'/raspap.auth'); define('RASPI_WIFI_CLIENT_INTERFACE', 'wlan0'); +define('RASPI_CACHE_PATH', sys_get_temp_dir() . '/raspap'); // Constants for configuration file paths. // These are typical for default RPi installs. Modify if needed. diff --git a/includes/configure_client.php b/includes/configure_client.php index 1253de82..dccbeaf8 100755 --- a/includes/configure_client.php +++ b/includes/configure_client.php @@ -7,48 +7,6 @@ function DisplayWPAConfig() { $status = new StatusMessages(); - $networks = array(); - - // Find currently configured networks - exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return); - - $network = null; - $ssid = null; - - foreach ($known_return as $line) { - if (preg_match('/network\s*=/', $line)) { - $network = array('visible' => false, 'configured' => true, 'connected' => false); - } elseif ($network !== null) { - if (preg_match('/^\s*}\s*$/', $line)) { - $networks[$ssid] = $network; - $network = null; - $ssid = null; - } elseif ($lineArr = preg_split('/\s*=\s*/', trim($line))) { - switch (strtolower($lineArr[0])) { - case 'ssid': - $ssid = trim($lineArr[1], '"'); - break; - case 'psk': - if (array_key_exists('passphrase', $network)) { - break; - } - case '#psk': - $network['protocol'] = 'WPA'; - case 'wep_key0': // Untested - $network['passphrase'] = trim($lineArr[1], '"'); - break; - case 'key_mgmt': - if (! array_key_exists('passphrase', $network) && $lineArr[1] === 'NONE') { - $network['protocol'] = 'Open'; - } - break; - case 'priority': - $network['priority'] = trim($lineArr[1], '"'); - break; - } - } - } - } if (isset($_POST['connect'])) { $result = 0; @@ -128,45 +86,6 @@ function DisplayWPAConfig() } } - exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' scan'); - sleep(3); - exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' scan_results', $scan_return); - - array_shift($scan_return); - - // display output - foreach ($scan_return as $network) { - $arrNetwork = preg_split("/[\t]+/", $network); // split result into array - - // If network is saved - if (array_key_exists(4, $arrNetwork) && array_key_exists($arrNetwork[4], $networks)) { - $networks[$arrNetwork[4]]['visible'] = true; - $networks[$arrNetwork[4]]['channel'] = ConvertToChannel($arrNetwork[1]); - // TODO What if the security has changed? - } else { - $networks[$arrNetwork[4]] = array( - 'configured' => false, - 'protocol' => ConvertToSecurity($arrNetwork[3]), - 'channel' => ConvertToChannel($arrNetwork[1]), - 'passphrase' => '', - 'visible' => true, - 'connected' => false - ); - } - - // Save RSSI - if (array_key_exists(4, $arrNetwork)) { - $networks[$arrNetwork[4]]['RSSI'] = $arrNetwork[2]; - } - - } - - exec('iwconfig ' . RASPI_WIFI_CLIENT_INTERFACE, $iwconfig_return); - foreach ($iwconfig_return as $line) { - if (preg_match('/ESSID:\"([^"]+)\"/i', $line, $iwconfig_ssid)) { - $networks[$iwconfig_ssid[1]]['connected'] = true; - } - } ?>
diff --git a/includes/functions.php b/includes/functions.php index 31286d73..67180fc1 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -571,3 +571,67 @@ function SaveTORAndVPNConfig() } } +/** + * Renders a simple PHP template + */ +function renderTemplate($name, $data) +{ + $file = "../../templates/$name.php"; + if (!file_exists($file)) { + return "template $name ($file) not found"; + } + + if (is_array($data)){ + extract($data); + } + + ob_start(); + include $file; + return ob_get_clean(); +} + +function expandCacheKey($key) +{ + return RASPI_CACHE_PATH . "/" . $key; +} + +function hasCache($key) +{ + $cacheKey = expandCacheKey($key); + return file_exists($cacheKey); +} + +function readCache($key) +{ + $cacheKey = expandCacheKey($key); + if (!file_exists($cacheKey)) { + return null; + } + return file_get_contents($cacheKey); +} + +function writeCache($key, $data) +{ + mkdir(RASPI_CACHE_PATH, 0777, true); + $cacheKey = expandCacheKey($key); + file_put_contents($cacheKey, $data); +} + +function deleteCache($key) +{ + if (hasCache($key)) { + $cacheKey = expandCacheKey($key); + unlink($cacheKey); + } +} + +function cache($key, $callback) +{ + if (hasCache($key)) { + return readCache($key); + } else { + $data = $callback(); + writeCache($key, $data); + return $data; + } +} diff --git a/js/custom.js b/js/custom.js index ac1ec75b..958658b2 100644 --- a/js/custom.js +++ b/js/custom.js @@ -180,6 +180,20 @@ function contentLoaded() { } } +function loadWifiStations(refresh) { + return function() { + var complete = function() { $(this).removeClass('loading-spinner'); } + var qs = refresh === true ? '?refresh' : ''; + $('.js-wifi-stations') + .addClass('loading-spinner') + .empty() + .load('/ajax/networking/wifi_stations.php'+qs, complete); + }; +} + +$(".js-reload-wifi-stations").on("click", loadWifiStations(true)); + $(document) .ajaxSend(setCSRFTokenHeader) - .ready(contentLoaded); + .ready(contentLoaded) + .ready(loadWifiStations()); diff --git a/templates/wifi_stations.php b/templates/wifi_stations.php new file mode 100644 index 00000000..a002b85d --- /dev/null +++ b/templates/wifi_stations.php @@ -0,0 +1,93 @@ + + + + + + $network): ?> +