<?php
$Tag = $_GET['tag'];
@$UserID = $_GET['userid'];

$key = "mysecretkeywsdef";
$iv = "myuniqueivparamu";
$PING = date('d-m-Y H:i:s');

$json_file = "main.json";
$jsondata = file_get_contents($json_file);
$arrayData = json_decode($jsondata, true);
$json = $arrayData['app'];



$l =  $_REQUEST['l'];
$an = $_REQUEST['an'];
$el = $_REQUEST['el'];
$ea = $_REQUEST['ea'];
$eb = $_REQUEST['eb'];
$key2 = $an.$json['customerid'];

function encode($str, $key){
	$sb = "";
	for ($i = 0; $i < strlen($str); $i++){
		 $sb .= (chr((ord($str[$i]) ^ ord($key[$i % strlen($key)]))));
	}
	return base64_encode($sb);
}

function sanitize($data) {
	$data = trim($data);
	$data = htmlspecialchars($data, ENT_QUOTES );
	$data = SQLite3::escapeString($data);
	return $data;
}

if (isset($_GET['tag'])){
	if ($Tag == "licV3"){
		echo bin2hex(openssl_encrypt(file_get_contents($json_file) , "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv));
	}
	if ($Tag == "licV4"){
		echo '
{
  "success": "1",
  "status": "ACTIVE",
  "cid": "'.$json["customerid"].'",
  "which": "licV4",
  "app": "'.encode(' {
    "id": "'.$json["id"].'",
    "appname": "'.$json["appname"].'",
    "expire": "'.$json["expire"].'",
    "login_type": "'.$json["login_type"].'",
    "version_code": "'.$json["version_code"].'",
    "filter_status": "'.$json["filter_status"].'",
    "epg_mode": "'.$json["epg_mode"].'",
    "apkautoupdate": "'.$json["apkautoupdate"].'",
    "show_expire": "'.$json["show_expire"].'"
  }', $key2.'app') .'",
  "portal": "'.encode(' {
    "panel": "'.$json["panel"].'",
    "portal": "'.$json["portal"].'",
    "portal2": "'.$json["portal2"].'",
    "portal3": "'.$json["portal3"].'",
    "portal4": "'.$json["portal4"].'",
    "portal5": "'.$json["portal5"].'",
    "portal_name": "'.$json["portal_name"].'",
    "portal2_name": "'.$json["portal2_name"].'",
    "portal3_name": "'.$json["portal3_name"].'",
    "portal4_name": "'.$json["portal4_name"].'",
    "portal5_name": "'.$json["portal5_name"].'",
    "portal_vod": "'.$json["portal_vod"].'",
    "portal_series": "'.$json["portal_series"].'"
  }', $key2.'portal') .'",
  "urls": "'.encode(' {
    "apkurl": "'.$json["apkurl"].'",
    "backupurl": "'.$json["backupurl"].'",
    "logurl": "'.$json["logurl"].'",
    "activation_url": "'.$json["activation_url"].'",
    "socket_url": "no",
    "epg_url": "'.$json["epg_url"].'",
    "ovpn_url": "'.$json["ovpn_url"].'"
  }', $key2.'urls') .'",
  "support": {
    "support_email": "'.$json["support_email"].'",
    "support_phone": "'.$json["support_phone"].'"
  },
  "button": "'.encode(' {
    "btn_live": "'.$json["btn_live"].'",
    "btn_live2": "'.$json["btn_live2"].'",
    "btn_live3": "'.$json["btn_live3"].'",
    "btn_live4": "'.$json["btn_live4"].'",
    "btn_live5": "'.$json["btn_live5"].'",
    "btn_vod": "'.$json["btn_vod"].'",
    "btn_vod2": "'.$json["btn_vod2"].'",
    "btn_vod3": "'.$json["btn_vod3"].'",
    "btn_vod4": "'.$json["btn_vod4"].'",
    "btn_vod5": "'.$json["btn_vod5"].'",
    "btn_epg": "'.$json["btn_epg"].'",
    "btn_epg2": "'.$json["btn_epg2"].'",
    "btn_epg3": "'.$json["btn_epg3"].'",
    "btn_epg4": "'.$json["btn_epg4"].'",
    "btn_epg5": "'.$json["btn_epg5"].'",
    "btn_series": "'.$json["btn_series"].'",
    "btn_series2": "'.$json["btn_series2"].'",
    "btn_series3": "'.$json["btn_series3"].'",
    "btn_series4": "'.$json["btn_series4"].'",
    "btn_series5": "'.$json["btn_series5"].'",
    "btn_radio": "'.$json["btn_radio"].'",
    "btn_radio2": "'.$json["btn_radio2"].'",
    "btn_radio3": "'.$json["btn_radio3"].'",
    "btn_radio4": "'.$json["btn_radio4"].'",
    "btn_radio5": "'.$json["btn_radio5"].'",
    "btn_catchup": "'.$json["btn_catchup"].'",
    "btn_catchup2": "'.$json["btn_catchup2"].'",
    "btn_catchup3": "'.$json["btn_catchup3"].'",
    "btn_catchup4": "'.$json["btn_catchup4"].'",
    "btn_catchup5": "'.$json["btn_catchup5"].'",
    "btn_account": "'.$json["btn_account"].'",
    "btn_account2": "'.$json["btn_account2"].'",
    "btn_account3": "'.$json["btn_account3"].'",
    "btn_pr": "'.$json["btn_pr"].'",
    "btn_rec": "'.$json["btn_rec"].'",
    "btn_vpn": "'.$json["btn_vpn"].'",
    "btn_noti": "'.$json["btn_noti"].'",
    "btn_update": "'.$json["btn_update"].'",
    "btn_login_settings": "'.$json["btn_login_settings"].'",
    "btn_login_account": "'.$json["btn_login_account"].'",
    "btn_fav": "'.$json["btn_fav"].'",
    "btn_fav2": "'.$json["btn_fav2"].'",
    "btn_fav3": "'.$json["btn_fav3"].'",
    "btn_signup": "'.$json["btn_signup"].'",
    "ms": "'.$json["ms"].'",
    "ms2": "'.$json["ms2"].'",
    "ms3": "'.$json["ms3"].'"
  }', $key2.'buttons') .'",
  "settings": "'.encode(' {
    "agent": "'.$json["agent"].'",
    "all_cat": "'.$json["all_cat"].'",
    "message_enabled": "'.$json["message_enabled"].'",
    "announcement_enabled": "'.$json["announcement_enabled"].'",
    "updateuserinfo_enabled": "'.$json["updateuserinfo_enabled"].'",
    "whatsupcheck_enabled": "'.$json["whatsupcheck_enabled"].'",
    "login_logo": "'.$json["login_logo"].'",
    "bjob": "'.$json["bjob"].'",
    "settings_app": "'.$json["settings_app"].'",
    "settings_account": "'.$json["settings_account"].'",
    "logs": "'.$json["logs"].'",
    "app_language": "'.$json["app_language"].'",
    "load_last_channel": "'.$json["load_last_channel"].'",
    "admob_banner_id": "'.$json["admob_banner_id"].'",
    "admob_interstitial_id": "'.$json["admob_interstitial_id"].'",
    "show_cat_count": "'.$json["show_cat_count"].'",
    "send_udid": "'.$json["send_udid"].'",
    "theme": "'.$json["theme"].'",
    "vpn_login_view": "yes"
  }', $key2.'sett') .'",
  "admobconfig": null,
  "prebid": null,
  "vastconfig": null,
  "freestar": "no",
  "players": {
    "player": "'.$json["player"].'",
    "player_tv": "'.$json["player_tv"].'",
    "player_vod": "'.$json["player_vod"].'",
    "player_series": "'.$json["player_series"].'",
    "player_catchup": "'.$json["player_catchup"].'",
    "stream_type": "'.$json["stream_type"].'"
  },
  "ort_settings": {
    "exo_hw": "'.$json["ort_settings"]["exo_hw"].'",
    "vlc_hw": "'.$json["ort_settings"]["vlc_hw"].'",
    "last_volume_exo": "'.$json["ort_settings"]["last_volume_exo"].'",
    "last_volume_vlc": "'.$json["ort_settings"]["last_volume_vlc"].'",
    "plyer_exo_buffer": "'.$json["ort_settings"]["plyer_exo_buffer"].'",
    "plyer_vlc_buffer": "'.$json["ort_settings"]["plyer_vlc_buffer"].'",
    "video_resize_exo": "'.$json["ort_settings"]["video_resize_exo"].'",
    "video_resize_vlc": "'.$json["ort_settings"]["video_resize_vlc"].'",
    "video_subtiltes_exo": "'.$json["ort_settings"]["video_subtiltes_exo"].'",
    "video_subtiltes_vlc": "'.$json["ort_settings"]["video_subtiltes_vlc"].'"
  },
  "maintenance": {
    "mnt_message": "'.$json["mnt_message"].'",
    "mnt_status": "'.$json["mnt_status"].'",
    "mnt_expire": "'.$json["mnt_expire"].'"
  }
}';
	}
	if ($Tag == "man" || $Tag == "ann"){
		echo file_get_contents("./" . $Tag . ".json");
	}
	if ($Tag == "conn" || $Tag == "msg_conf"){
		echo '{"tag":"' . $Tag . '","success":"1","api_ver":"1.0v"}';
	}
	if ($Tag == "whatsup"){
		echo '{"tag":"whatsup","success":"0","api_ver":"1.0v","whatsup":"no"}';
	}
	if ($Tag == "gfilter"){
		echo '{"tag":"gfilter_n","success":"1","api_ver":"1.0v","status":"No","filter":[]}';
	}
	if ($Tag == "msg_cat_view" || $Tag == "msg"){
		$db = new SQLite3('./user_message.db');
		$stmt = $db->prepare("SELECT * FROM messages WHERE userid=?");
		$stmt->bindValue(1, sanitize($UserID));
		$res = $stmt->execute();
		$row = $res->fetchArray() ;
		$message = $row['message'];
		$userid = $row['userid'];
		$status = $row['status'];
		$expire = $row['expire'];
		if (empty($message)){
			echo '{"tag":"' . $Tag . '","success":"0","api_ver":"1.0v","message":"None Messages"}';
		}
		else{
			echo '{"tag":"' . $Tag . '","success":"1","api_ver":"1.0v","status":"$status","msgid":"1646","message":"$message"}';
		}
	}
	if($Tag == "connv2"){
		$LASTTIME = date('d-m-Y H:i:s');
		$TIME = date('d-m-Y H:i:s');
		$IPADDRESS = real_ip();

		if (isset($_GET['userid'])) {
			$UserID = $_GET['userid'];
		}else {
			$UserID = '';
		}

		$db1 = new SQLite3('./user_logs.db');
		$db1->exec('CREATE TABLE IF NOT EXISTS logging(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, appid TEXT, version TEXT, device TEXT, pkg TEXT, app TEXT, cid TEXT, uid TEXT, status TEXT, d TEXT, time TEXT, last_online TEXT, ping TEXT, IP TEXT)');
		$res1 = $db1->query("SELECT * FROM logging WHERE uid='".$UserID."'" );

		while ($row5 = $res1->fetchArray()) {
			$app_id = $row5['appid'];
		}

		if (empty($app_id)) {
			$db1->exec("INSERT INTO logging(appid, version, device, pkg, app, cid, uid, status, d, time, ping, IP) 
						VALUES( '" . $_GET['appid'] . "',
								'" . $_GET['version'] . "',
								'" . $_GET['device_type'] . "',
								'" . $_GET['p'] . "',
								'" . $_GET['an'] . "',
								'" . $_GET['customerid'] . "',
								'" . $_GET['userid'] . "',
								'" . $_GET['online'] . "',
								'" . $_GET['did'] . "',
								'" . $TIME . "',
								'" . $PING . "',
								'" . $IPADDRESS . "'
								)");
		}
		else if ($_GET['online'] == 'yes') {
			$db1->exec("UPDATE logging SET  appid='" . $_GET['appid'] . "',
											version='" . $_GET['version'] . "',
											device='" . $_GET['device_type'] . "',
											pkg='" . $_GET['p'] . "',
											app='" . $_GET['an'] . "',
											cid='" . $_GET['customerid'] . "',
											status='" . $_GET['online'] . "',
											d='" . $_GET['did'] . "',
											last_online='" . $LASTTIME . "',
											ping='" . $PING . "',
											IP='" . $IPADDRESS . "'
										WHERE uid='" . $_GET['userid'] . "'
										");
		}
		else {
			$db1->exec("UPDATE logging SET  appid='" . $_GET['appid'] . "',
											version='" . $_GET['version'] . "',
											device='" . $_GET['device_type'] . "',
											pkg='" . $_GET['p'] . "',
											app='" . $_GET['an'] . "',
											cid='" . $_GET['customerid'] . "',
											status='" . $_GET['online'] . "',
											d='" . $_GET['did'] . "',
											ping='" . $PING . "',
											IP='" . $IPADDRESS . "'
										WHERE uid='" . $_GET['userid'] . "'
										");
		}



		$db = new SQLite3('./user_message.db');
		$stmt = $db->prepare("SELECT * FROM messages WHERE userid=?");
		$stmt->bindValue(1, sanitize($UserID));
		$res = $stmt->execute();
		$row = $res->fetchArray() ;
		$msgid = $row['id'];
		$message = $row['message'];
		$userid = $row['userid'];
		$status = $row['status'];
		$expire = $row['expire'];
		$jsonData = file_get_contents("./connv2.json");
		if($message != ''){
			$arrayData = json_decode($jsonData, true);
			$replacementData = array(
				'success' => '1',
				'message' => $message,
				'msg_status' => $status,
				'msg_expire' => $expire,
				'msgid' => '1646'
			);
			$newArrayData = array_replace_recursive($arrayData, $replacementData);
			$newJsonData = json_encode($newArrayData, JSON_PRETTY_PRINT);
			echo $newJsonData;
		}else{
			echo file_get_contents("./connv2.json");
		}
	}
	if($Tag == "vpnconfigV2"){
		$db = new SQLite3('./.db.db');
		$vpn = $db->query('SELECT * FROM vpn');
		while ($vpna = $vpn -> fetchArray(SQLITE3_ASSOC)) {
			$dataA[]=array(
				"id" => $vpna['id'],
				"userid" => $vpna['userid'],
				"vpn_appid" => $vpna['vpn_appid'],
				"vpn_country" => $vpna['vpn_country'],
				"vpn_state" => $vpna['vpn_state'],
				"vpn_config" => $vpna['vpn_config'],
				"vpn_status" => $vpna['vpn_status'],
				"auth_type" => $vpna['auth_type'],
				"auth_embedded" => $vpna['auth_embedded'],
				"username" => $vpna['username'],
				"password" => $vpna['password'],
				"date" => $vpna['date']
				);
		}
		$jdata = json_encode($dataA);
		$vpn_out = "{\"tag\": \"vpnconfigV2\",\"success\": \"1\",\"api_ver\": \"1.0v\",\"vpnconfigs\": {$jdata}}";
		echo bin2hex(openssl_encrypt($vpn_out , "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv));
	}
}else{
	include ('index.php');
}

?>
