<?php

//main db
$db = new SQLite3("\$/api/.db.db");

//dns
$rows3 = $db->query("SELECT * FROM dns");
$rows = array();
while ($row = $rows3->fetchArray(SQLITE3_ASSOC)) {
	$rows[] = $row['url'];
}

//curl dns
function call_api($api_link){
	$returnData = "0";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $api_link);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-US) AppleWebKit/532.8 (KHTML, like Gecko) Chrome/4.0.302.2 Safari/532.8");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_AUTOREFERER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	$result = json_decode(curl_exec($ch));
	if (!empty($result)) {
		$returndata = $result;
		return ["result" => "success", "data" => $returndata];
	}
}

//loop to check dns
foreach ($rows as $portal) {
	$api_link = $portal . "/get.php?" . http_build_query($_POST) . str_replace("%40", "@", $_SERVER["QUERY_STRING"]);
	$api_req = call_api($api_link);
	
	
	if ($api_req["result"] == "success") {
		if ($api_req["data"]->user_info->auth != 0) {
			if ($api_req["data"]->user_info->status == "Active") {
				header("Location: {$api_link}", true, false ? 301 : 302);
				exit();
			}
		}
	}
}
echo "Failed";
?>