network.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. class wpa
  3. {
  4. public $wpa_supplicant = [];
  5. public $wpa_supplicantconf = "";
  6. function __construct() {
  7. $filename = "/etc/wpa_supplicant/wpa_supplicant.conf";
  8. $handle = fopen($filename, "r");
  9. $input_lines = fread($handle, filesize($filename));
  10. fclose($handle);
  11. $this->wpa_supplicantconf = $input_lines; //this line for backup wpa_supplicant.conf to var
  12. $priority = 1;
  13. $primary_output = [];
  14. preg_match_all("/network={[\r\n\t]([^}]*)}/m", $input_lines, $primary_output);
  15. foreach($primary_output[1] as $output_line){
  16. $secondary_output = [];
  17. $tmp = [];
  18. preg_match_all('/([^=\t\n\r]*)=["]{0,1}([^"\n]*)["]{0,1}/', $output_line, $secondary_output);
  19. for($i = 0; $i <= sizeof($secondary_output[1]); $i++){
  20. if(isset($secondary_output[1][$i]) && isset($secondary_output[2][$i])){
  21. $tmp[$secondary_output[1][$i]] = $secondary_output[2][$i];
  22. }
  23. }
  24. if(!isset($tmp["priority"])){
  25. $tmp["priority"] = $priority;
  26. }
  27. $priority += 1;
  28. array_push($this->wpa_supplicant,$tmp);
  29. }
  30. $this->sort_wpa();
  31. }
  32. function sort_wpa(){
  33. usort($this->wpa_supplicant, function ($a, $b) {return $a['priority'] <=> $b['priority'];});
  34. $wifipriority = 1;
  35. foreach($this->wpa_supplicant as &$wifiarray){
  36. $wifiarray["priority"] = $wifipriority;
  37. $wifipriority += 1;
  38. }
  39. }
  40. function remove($ssid){
  41. $i = 0;
  42. foreach($this->wpa_supplicant as $wifiarray){
  43. if($wifiarray["ssid"] == $ssid){
  44. unset($this->wpa_supplicant[$i]);
  45. break;
  46. }
  47. $i += 1;
  48. }
  49. $this->sort_wpa();
  50. }
  51. function add($array){
  52. array_push($this->wpa_supplicant,$array);
  53. $this->sort_wpa();
  54. }
  55. function restart(){
  56. exec("sudo wpa_cli reconfigure");
  57. }
  58. function scan(){
  59. $result = [];
  60. $output = shell_exec('sudo iwlist wlan0 scan |grep -e ESSID -e IEEE -e "Encryption key" -e "Quality" -e "Cell" -e "Authentication Suites (1) :"');
  61. $prased_data = explode("Cell",$output);
  62. array_shift($prased_data);
  63. foreach ($prased_data as $data){
  64. preg_match_all("/.+Address: ([0-9A-Fa-f]+:[0-9A-Fa-f]+:[0-9A-Fa-f]+:[0-9A-Fa-f]+:[0-9A-Fa-f]+:[0-9A-Fa-f]+)/", $data, $tmp);
  65. $split["Address"] = $tmp[1][0];
  66. preg_match_all("/.+Quality=([0-9]+\/[0-9]+)/", $data, $tmp);
  67. $split["Quality"] = $tmp[1][0];
  68. if($split["Quality"] == null){
  69. $split["Quality"] = "0/70";
  70. }
  71. preg_match_all("/.+Signal level=-([0-9]+ dBm)/", $data, $tmp);
  72. $split["Signal_Level"] = $tmp[1][0];
  73. if($split["Encryption_Key"] == null){
  74. $split["Encryption_Key"] = "0 dBm";
  75. }
  76. preg_match_all("/.+Encryption key:(on|off)/", $data, $tmp);
  77. $split["Encryption_Key"] = $tmp[1][0];
  78. if($split["Encryption_Key"] == null){
  79. $split["Encryption_Key"] = "off";
  80. }
  81. preg_match_all('/.+ESSID:"(.+)"/', $data, $tmp);
  82. if(strpos($tmp[1][0],"\\x00") !== false){
  83. $split["ESSID"] = "-Hidden Network-";
  84. }else{
  85. $split["ESSID"] = $tmp[1][0];
  86. }
  87. if($split["ESSID"] == null){
  88. $split["ESSID"] = "-NO SSID-";
  89. }
  90. preg_match_all('/.+IE: (.+)/', $data, $tmp);
  91. $split["Encrpytion_Method"] = explode("Authentication Suites (1) : ",$tmp[1][0])[0];
  92. if($split["Encrpytion_Method"] == null){
  93. $split["Encrpytion_Method"] = "No Encryption";
  94. }
  95. if($split["Encryption_Key"] == "on" && $split["Encrpytion_Method"] !== null){
  96. if(strpos($split["Encrpytion_Method"],"WPA2") !== false){
  97. $split["Encrpytion_Method_Shortname"] = "WPA2";
  98. }else if(strpos($split["Encrpytion_Method"],"802.1x") !== false){
  99. $split["Encrpytion_Method_Shortname"] = "802.1x";
  100. }else{
  101. $split["Encrpytion_Method_Shortname"] = "No";
  102. }
  103. }else{
  104. $split["Encrpytion_Method_Shortname"] = "No";
  105. }
  106. preg_match_all('/.+Authentication Suites \(1\) : (.+)/', $data, $tmp);
  107. $split["Encryption_Suites"] = $tmp[1][0];
  108. if($split["Encryption_Suites"] == null){
  109. $split["Encryption_Suites"] = "No encryption";
  110. }
  111. array_push($result,$split);
  112. }
  113. return $result;
  114. }
  115. function current_connecting(){
  116. $result=[];
  117. if(exec('iw dev wlan0 link') !== 'Not connected.'){
  118. $ssid = exec("iwgetid -r");
  119. $result["ssid"] = $ssid;
  120. $c = exec("iwgetid -c -r");
  121. $result["channel"] = $c;
  122. $f = exec("iwgetid -f -r");
  123. $result["frequency"] = $f;
  124. }else{
  125. $result["ssid"] = "";
  126. $result["channel"] = "";
  127. $result["frequency"] = "";
  128. }
  129. return $result;
  130. }
  131. function view_wifistorage(){
  132. return $this->wpa_supplicant;
  133. }
  134. function top_priority($ssid){
  135. $i = 0;
  136. foreach($this->wpa_supplicant as $wifiarray){
  137. if($wifiarray["ssid"] == $ssid){
  138. $this->wpa_supplicant[$i]["priority"] = 0;
  139. break;
  140. }
  141. $i += 1;
  142. }
  143. $this->sort_wpa();
  144. }
  145. function save(){
  146. $noquote = array("disabled","auth_alg","scan_ssid","key_mgmt","proto","pairwise","eap","priority");
  147. $fp = fopen('/etc/wpa_supplicant/wpa_supplicant.conf', 'w');
  148. fwrite($fp, 'country=DE'.PHP_EOL);
  149. fwrite($fp, 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev'.PHP_EOL);
  150. fwrite($fp, 'update_config=1'.PHP_EOL);
  151. fwrite($fp, PHP_EOL);
  152. foreach($this->wpa_supplicant as $wifiarray){
  153. fwrite($fp, 'network={'.PHP_EOL);
  154. foreach($wifiarray as $key => $value){
  155. if(in_array($key,$noquote)){
  156. fwrite($fp, " ".$key.'='.$value.''.PHP_EOL);
  157. }else{
  158. fwrite($fp, " ".$key.'="'.$value.'"'.PHP_EOL);
  159. }
  160. }
  161. fwrite($fp, '}'.PHP_EOL);
  162. fwrite($fp, PHP_EOL);
  163. }
  164. fclose($fp);
  165. $this->restart_wifi();
  166. }
  167. function restart_wifi(){
  168. shell_exec('sudo ifconfig wlan0 up');
  169. shell_exec('sudo ifconfig wlan1 up');
  170. shell_exec('sudo /etc/init.d/networking restart');
  171. $loss = shell_exec("ping -c 1 -q 255.255.255.0 -I wlan0 | grep -oP '\d+(?=% packet loss)'");
  172. if($loss == "0"){
  173. $failed = false;
  174. $output = shell_exec('sudo journalctl -xu networking.service -o json --since="$(sudo systemctl show networking --property=ExecMainStartTimestamp --value)"');
  175. $outputarr = preg_split('/\r\n|\r|\n/', $output);
  176. foreach($outputarr as $msg){
  177. $msgarray = json_decode($msg,true);
  178. if(isset($msgarray["MESSAGE"])){
  179. if(strpos($msgarray["MESSAGE"],"reason=WRONG_KEY") !== false){
  180. $failed = true;
  181. }
  182. if(strpos($msgarray["MESSAGE"],"Failed to read or parse configuration") !== false){
  183. $failed = true;
  184. }
  185. if(strpos($msgarray["MESSAGE"],"Failed to start Raise network interfaces") !== false){
  186. $failed = true;
  187. }
  188. }
  189. }
  190. if($failed){
  191. $fpwpa_supplicant_bk = fopen('/etc/wpa_supplicant/wpa_supplicant.conf', 'w');
  192. fwrite($fpwpa_supplicant_bk, $this->wpa_supplicantconf);
  193. fclose($fpwpa_supplicant_bk);
  194. return "ERROR_RESETED";
  195. }else{
  196. $fpwpa_supplicant_bk = fopen('/etc/wpa_supplicant/wpa_supplicant.conf', 'w');
  197. fwrite($fpwpa_supplicant_bk, $this->wpa_supplicantconf);
  198. fclose($fpwpa_supplicant_bk);
  199. return "ERROR_UNKNOWN_RESETED";
  200. }
  201. }else{
  202. return "SUCCESS";
  203. }
  204. }
  205. }
  206. class ifconfig
  207. {
  208. function is_connected(){
  209. $connected = fopen("http://www.google.com:80/","r");
  210. if($connected){
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. function list_nic(){
  217. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  218. exec("getNICinfo.exe");
  219. sleep(1);
  220. $nics = [];
  221. $content = remove_utf8_bom(file_get_contents("NICinfo.txt"));
  222. $data = explode(PHP_EOL,$content);
  223. foreach ($data as $nicinfo){
  224. array_push($nics,explode(",",$nicinfo)[0]);
  225. }
  226. return $nics;
  227. } else {
  228. $result = [];
  229. exec('sudo ifconfig',$arrayoutput);
  230. foreach ($arrayoutput as $line){
  231. $line = trim($line);
  232. $inlinedoutput .= $line."\n";
  233. }
  234. $InterfaceRawInformation = explode("\n\n",$inlinedoutput);
  235. array_pop($InterfaceRawInformation);
  236. //$prased_data = preg_split("/wlan.|lo|eth./", $unprased_data,-1,PREG_SPLIT_NO_EMPTY);
  237. foreach ($InterfaceRawInformation as $data){
  238. $data = preg_replace('/\n/', '', $data);
  239. preg_match('/(wlan[0-9]+|eth[0-9]+|lo)/', $data, $tmp);
  240. if(isset($tmp[1])){
  241. //$exported_information["InterfaceName"] = $tmp[1];
  242. $exported_information["InterfaceID"] = preg_replace('/\w+([0-9]+)/', '$1', $tmp[1]);
  243. if(strpos($tmp[1], 'eth') !== false){
  244. $exported_information["InterfaceIcon"] = "Ethernet";
  245. }else if(strpos($tmp[1], 'wlan') !== false){
  246. $exported_information["InterfaceIcon"] = "WiFi";
  247. }else if(strpos($tmp[1], 'lo') !== false){
  248. continue;
  249. }else{
  250. $exported_information["InterfaceIcon"] = "Unknown";
  251. }
  252. }else{
  253. $exported_information["InterfaceName"] = "Unknown";
  254. $exported_information["InterfaceIcon"] = "Unknown";
  255. }
  256. preg_match('/HWaddr ([0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z])/', $data, $tmp);
  257. if(isset($tmp[1])){
  258. $exported_information["HardwareAddress"] = $tmp[1];
  259. }else{
  260. preg_match('/ether ([0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z])/', $data, $tmp);
  261. if(isset($tmp[1])){
  262. $exported_information["HardwareAddress"] = $tmp[1];
  263. }else{
  264. $exported_information["HardwareAddress"] = "Unknown";
  265. }
  266. }
  267. preg_match('/inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
  268. if(isset($tmp[1])){
  269. $exported_information["IPv4Address"] = $tmp[1];
  270. }else{
  271. preg_match('/inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
  272. if(isset($tmp[1])){
  273. $exported_information["IPv4Address"] = $tmp[1];
  274. }else{
  275. $exported_information["IPv4Address"] = "Unknown";
  276. }
  277. }
  278. preg_match('/Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
  279. if(isset($tmp[1])){
  280. $exported_information["IPv4SubNetMask"] = $tmp[1];
  281. }else{
  282. preg_match('/mask ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
  283. if(isset($tmp[1])){
  284. $exported_information["IPv4SubNetMask"] = $tmp[1];
  285. }else{
  286. $exported_information["IPv4SubNetMask"] = "Unknown";
  287. }
  288. }
  289. preg_match('/inet6 addr: ([a-zA-Z0-9:]+\/[0-9]+)/', $data, $tmp);
  290. if(isset($tmp[1])){
  291. $exported_information["IPv6Address"] = $tmp[1];
  292. }else{
  293. preg_match('/inet6 ([a-zA-Z0-9:]+)/', $data, $tmp);
  294. if(isset($tmp[1])){
  295. $exported_information["IPv6Address"] = $tmp[1];
  296. }else{
  297. $exported_information["IPv6Address"] = "Unknown";
  298. }
  299. }
  300. array_push($result,$exported_information);
  301. }
  302. return $result;
  303. }
  304. }
  305. }
  306. class ap
  307. {
  308. public $settings = [];
  309. function __construct() {
  310. $filename = "/etc/hostapd/hostapd.conf";
  311. $handle = fopen($filename, "r");
  312. $input_lines = fread($handle, filesize($filename));
  313. fclose($handle);
  314. $primary_output = [];
  315. preg_match_all('/([^=\t\n\r]*)=["]{0,1}([^"\n]*)["]{0,1}/', $input_lines, $primary_output);
  316. for($i = 0; $i <= sizeof($primary_output[1]); $i++){
  317. if(isset($primary_output[1][$i]) && isset($primary_output[2][$i])){
  318. $this->settings[$primary_output[1][$i]] = $primary_output[2][$i];
  319. }
  320. }
  321. }
  322. function view_ap_settings(){
  323. return $this->settings;
  324. }
  325. function change_ap_settings($key,$val){
  326. $this->settings[$key] = $val;
  327. }
  328. }