<?php
set_time_limit(600);
ignore_user_abort(true);

header('Content-Type: application/json');
include '../../../auth.php';

require_once "network.class.php";
$wpa = new wpa();
$ifconfig = new ifconfig();
$ap = new ap();

if(isset($_GET["opr"])){
	if($_GET["opr"] == "current_connecting"){
		echo json_encode($wpa->current_connecting());
	}else if($_GET["opr"] == "internet"){
		echo $ifconfig->is_connected(); 
	}else if($_GET["opr"] == "scan"){
		echo json_encode($wpa->scan()); 
	}else if($_GET["opr"] == "view_wifistorage"){
		echo json_encode($wpa->view_wifistorage());
	}else if($_GET["opr"] == "listnic"){
		echo json_encode($ifconfig->list_nic()); 
	}else if($_GET["opr"] == "top_priority"){
		$wpa->top_priority($_GET["ssid"]);
		$wpa->save();
	}else if($_GET["opr"] == "remove"){
		$wpa->remove($_GET["ssid"]);
		$wpa->save();
		//print_r($wpa->view_wifistorage());
	}else if($_GET["opr"] == "restart"){
		$wpa->restart();
	}else if($_GET["opr"] == "viewapsetting"){
		echo json_encode($ap->view_ap_settings());
	}else if($_GET["opr"] == "setap"){
		$ap->change_ap_settings("ssid",$_POST["ssid"]);
		$ap->change_ap_settings("wpa_passphrase",$_POST["psk"]);
	}else if($_GET["opr"] == "addwifi"){
		$wifiarr = [];
		if(isset($_POST["ssid"])){
			$wifiarr["ssid"] = $_POST["ssid"];
		}else{
			header('HTTP 1.1 400 Bad Request');
		}
		
		if($_POST["encryption"] == "no"){
			$wpa->add($wifiarr);
		}else if($_POST["encryption"] == "802.1x"){
			$wifiarr["phase1"] = "peaplabel=0";
			$wifiarr["phase2"] = "auth=MSCHAPV2";
			$wifiarr["password"] = $_POST["psk"];
			$wifiarr["anonymous_identity"] = $_POST["identity"];
			$wifiarr["identity"] = $_POST["identity"];
			$wifiarr["eap"] = "PEAP";
			$wifiarr["pairwise"] = "CCMP TKIP";
			$wifiarr["proto"] = "WPA RSN";
			$wifiarr["key_mgmt"] = "WPA-EAP";
			$wifiarr["scan_ssid"] = "1";
			$wifiarr["ssid"] = $_POST["ssid"];
			$wifiarr["auth_alg"] = "OPEN";
			$wifiarr["disabled"] = "0";
			$wpa->add($wifiarr);
		}else if($_POST["encryption"] == "WPA2"){
			$wifiarr["psk"] = $_POST["psk"];
			$wpa->add($wifiarr);
			$wpa->save();
		}else{
			header('HTTP 1.1 400 Bad Request');
		}
	}
}

