Raspbian WiFi Configuration Portal

'; switch($page) { case "dhcpd_conf": exec('cat /etc/dnsmasq.conf',$return); $conf = ParseConfig($return); $arrRange = explode(",",$conf['dhcp-range']); $RangeStart = $arrRange[0]; $RangeEnd = $arrRange[1]; $RangeMask = $arrRange[2]; preg_match('/([0-9]*)([a-z])/i',$arrRange[3],$arrRangeLeaseTime); switch($arrRangeLeaseTime[2]) { case "h": $hselected = " selected"; break; case "m": $mselected = " selected"; break; case "d": $dselected = " selected"; break; } exec('pidof dnsmasq | wc -l',$dnsmasq); if($dnsmasq[0] == 0) { $status = 'dnsmasq not running'; } else { $status = 'dnsmasq is running'; } echo 'DHCP Server Options
Status : '.$status.'
Interface :
Starting IP Address :
Ending IP Address :
Lease Time
'; if($dnsmasq[0] == 0) { echo''; } else { echo ''; } echo'

Client list
'; exec('cat /var/lib/misc/dnsmasq.leases',$leases); foreach($leases as $lease) { echo $lease; } if(isset($_POST['savedhcpdsettings'])) { $config = 'interface='.$_POST['interface'].' dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].',255.255.255.0,'.$_POST['RangeLeaseTime'].''.$_POST['RangeLeaseTimeUnits']; exec('echo "'.$config.'" > /tmp/dhcpddata',$temp); system('sudo cp /tmp/dhcpddata /etc/dnsmasq.conf',$return); if($return == 0) { echo "dnsmasq configuration updated successfully"; } else { echo "Dnsmasq configuration failed to be updated"; } } if(isset($_POST['startdhcpd'])) { $line = system('sudo /etc/init.d/dnsmasq start',$return); echo "Attempting to start dnsmasq"; } if(isset($_POST['stopdhcpd'])) { $line = system('sudo /etc/init.d/dnsmasq stop',$return); echo "Stopping dnsmasq"; } break; case "wlan0_info": exec('ifconfig wlan0',$return); exec('iwconfig wlan0',$return); $strWlan0 = implode(" ",$return); $strWlan0 = preg_replace('/\s\s+/', ' ', $strWlan0); preg_match('/HWaddr ([0-9a-f:]+)/i',$strWlan0,$result); $strHWAddress = $result[1]; preg_match('/inet addr:([0-9.]+)/i',$strWlan0,$result); $strIPAddress = $result[1]; preg_match('/Mask:([0-9.]+)/i',$strWlan0,$result); $strNetMask = $result[1]; preg_match('/RX packets:(\d+)/',$strWlan0,$result); $strRxPackets = $result[1]; preg_match('/TX packets:(\d+)/',$strWlan0,$result); $strTxPackets = $result[1]; preg_match('/RX Bytes:(\d+ \(\d+.\d+ MiB\))/i',$strWlan0,$result); $strRxBytes = $result[1]; preg_match('/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result); $strTxBytes = $result[1]; preg_match('/ESSID:\"([a-zA-Z0-9\s]+)\"/i',$strWlan0,$result); $strSSID = str_replace('"','',$result[1]); preg_match('/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result); $strBSSID = $result[1]; preg_match('/Bit Rate=([0-9]+ Mb\/s)/i',$strWlan0,$result); $strBitrate = $result[1]; preg_match('/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result); $strTxPower = $result[1]; preg_match('/Link Quality=([0-9]+\/[0-9]+)/i',$strWlan0,$result); $strLinkQuality = $result[1]; preg_match('/Signal Level=(-[0-9]+ dBm)/i',$strWlan0,$result); $strSignalLevel = $result[1]; if(strpos($strWlan0, "UP") !== false && strpos($strWlan0, "RUNNING") !== false) { $strStatus = 'Interface is up'; } else { $strStatus = 'Interface is down'; } if(isset($_POST['ifdown_wlan0'])) { exec('ifconfig wlan0 | grep -i running | wc -l',$test); if($test[0] == 1) { exec('sudo ifdown wlan0',$return); } else { echo 'Interface already down'; } } elseif(isset($_POST['ifup_wlan0'])) { exec('ifconfig wlan0 | grep -i running | wc -l',$test); if($test[0] == 0) { exec('sudo ifup wlan0',$return); } else { echo 'Interface already up'; } } echo '
Wireless Information and Statistics
Interface Information
Interface Name : wlan0
Interface Status : ' . $strStatus . '
IP Address : ' . $strIPAddress . '
Subnet Mask : ' . $strNetMask . '
Mac Address : ' . $strHWAddress . '
Interface Statistics
Received Packets : ' . $strRxPackets . '
Received Bytes : ' . $strRxBytes . '

Transferred Packets : ' . $strTxPackets . '
Transferred Bytes : ' . $strTxBytes . '
Wireless Information
Connected To : ' . $strSSID . '
AP Mac Address : ' . $strBSSID . '
Bitrate : ' . $strBitrate . '
Transmit Power : ' . $strTxPower .'
Link Quality : ' . $strLinkQuality . '
Signal Level : ' . $strSignalLevel . '
Information provided by ifconfig and iwconfig
'; break; case "wpa_conf": exec('sudo cat /etc/wpa_supplicant/wpa_supplicant.conf',$return); $ssid = array(); $psk = array(); foreach($return as $a) { if(preg_match('/SSID/i',$a)) { $arrssid = explode("=",$a); $ssid[] = str_replace('"','',$arrssid[1]); } if(preg_match('/\#psk/i',$a)) { $arrpsk = explode("=",$a); $psk[] = str_replace('"','',$arrpsk[1]); } } $numSSIDs = count($ssid); $output = '
'; for($ssids = 0; $ssids < $numSSIDs; $ssids++) { $output .= '
Network '.$ssids.'
SSID :
PSK :
'; } $output .= '
'; echo $output; echo ''; if(isset($_POST['SaveWPAPSKSettings'])) { $config = 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 '; $networks = $_POST['Networks']; for($x = 0; $x < $networks; $x++) { $network = ''; $ssid = escapeshellarg($_POST['ssid'.$x]); $psk = escapeshellarg($_POST['psk'.$x]); exec('wpa_passphrase '.$ssid. ' ' . $psk,$network); foreach($network as $b) { $config .= "$b "; } } exec("echo '$config' > /tmp/wifidata",$return); system('sudo cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf',$returnval); if($returnval == 0) { echo "Wifi Settings Updated Successfully"; } else { echo "Wifi settings failed to be updated"; } } elseif(isset($_POST['Scan'])) { $return = ''; exec('sudo wpa_cli scan',$return); sleep(5); exec('sudo wpa_cli scan_results',$return); for($shift = 0; $shift < 4; $shift++ ) { array_shift($return); } echo "Networks found :
"; foreach($return as $network) { $arrNetwork = preg_split("/[\t]+/",$network); $bssid = $arrNetwork[0]; $channel = ConvertToChannel($arrNetwork[1]); $signal = $arrNetwork[2] . " dBm"; $security = $arrNetwork[3]; $ssid = $arrNetwork[4]; echo '' . $ssid . " on channel " . $channel . " with " . $signal . "(".ConvertToSecurity($security)." Security)
"; } } break; case "hostapd_conf": exec('cat /etc/hostapd/hostapd.conf',$return); exec('pidof hostapd | wc -l',$hostapdstatus); if($hostapdstatus[0] == 0) { $status = 'hostapd is not running'; } else { $status = 'hostapd is running'; } $arrConfig = array(); $arrChannel = array('a','b','g'); $arrSecurity = array( 1 => 'WPA', 2 => 'WPA2',3=> 'WPA+WPA2'); $arrEncType = array('TKIP' => 'TKIP', 'CCMP' => 'CCMP', 'TKIP CCMP' => 'TKIP+CCMP'); foreach($return as $a) { if($a[0] != "#") { $arrLine = explode("=",$a); $arrConfig[$arrLine[0]]=$arrLine[1]; } } echo '
HostAPD status : ' . $status . '
Interface :
SSID :
Wireless Mode :
Channel :
Security type :
Encryption Type :
PSK :
'; if($hostapdstatus[0] == 0) { echo ''; } else { echo ''; } echo'
Advanced Settings
Country Code :
'; break; case "save_hostapd_conf": if(isset($_POST['SaveHostAPDSettings'])) { $config = 'driver=nl80211 ctrl_interface=/var/run/hostapd ctrl_interface_group=0 beacon_int=100 auth_algs=1 wpa_key_mgmt=WPA-PSK '; $config .= "interface=".$_POST['interface']." "; $config .= "ssid=".$_POST['ssid']." "; $config .= "hw_mode=".$_POST['hw_mode']." "; $config .= "channel=".$_POST['channel']." "; $config .= "wpa=".$_POST['wpa']." "; $config .='wpa_passphrase='.$_POST['wpa_passphrase'].' '; $config .="wpa_pairwise=".$_POST['wpa_pairwise']." "; $config .="country_code=".$_POST['country_code']; exec("echo '$config' > /tmp/hostapddata",$return); system("sudo cp /tmp/hostapddata /etc/hostapd/hostapd.conf",$return); if($return == 0) { echo "Wifi Hotspot settings saved"; } else { echo "Wifi Hotspot settings failed to be saved"; } } elseif(isset($_POST['StartHotspot'])) { echo "Attempting to start hotspot"; exec('sudo /etc/init.d/hostapd start',$return); foreach($return as $line) { echo $line."
"; } } elseif(isset($_POST['StopHotspot'])) { echo "Attempting to stop hotspot"; exec('sudo /etc/init.d/hostapd stop',$return); foreach($return as $line) { echo $line."
"; } } break; } echo '
'; ?>