<?php
include 'anti.php';
include 'konak.php';
if(!isset($_POST['username']))
{
    $jsonarray = ["status"=> "error","message" => "you dont send username"];
    echo json_encode($jsonarray);
    die();
    
}
$username = anti_injection($_POST['username']);
if($username == "" ){
    $jsonarray = ["status"=> "error","message" => "Empty username"];
    echo json_encode($jsonarray);
    die();    
}
if(!isset($_POST['password']))
{
    $jsonarray = ["status"=> "error","message" => "you dont send Password"];
    echo json_encode($jsonarray);
    die();
}
$password = anti_injection($_POST['password']);
if($password == "" ){
     $jsonarray = ["status"=> "error","message" => "Empty Password"];
     echo json_encode($jsonarray);
     die();
}
if(!isset($_POST['serviceId']))
{
    $jsonarray = ["status"=> "error","message" => "you dont send serviceid"];
    echo json_encode($jsonarray);
    die();
}
$serviceid = anti_injection($_POST['serviceId']);
if($serviceid == "" ){
    $jsonarray = ["status"=> "error","message" => "Empty serviceid"];
    echo json_encode($jsonarray);
    die();
}
if(!isset($_POST['configblob']))
{
    $jsonarray = ["status"=> "error","message" => "you dont send configblob"];
    echo json_encode($jsonarray);
    echo "";
    die();
    
}
$configblob = anti_injection($_POST['configblob']);
if($configblob == "" )
{
    $jsonarray = ["status"=> "error","message" => "Empty configblob"];
    echo json_encode($jsonarray);
    die();
}
$priceQuery = mysqli_query($koneksi, "SELECT harga,servicename FROM price WHERE serviceId='".$serviceid."' LIMIT 1");
if (mysqli_num_rows($priceQuery) == 0) {
    echo json_encode(["status"  => "error","message" => "Service price not found"]);
    die();
}
$priceData = mysqli_fetch_assoc($priceQuery);
$service_price = intval($priceData["harga"]);
$service_name = $priceData["servicename"];
$info = json_encode(["XiaomiAuth"  => $service_name,"blob" => $configblob]);
$data = mysqli_query($koneksi, "SELECT * FROM user_id WHERE username='".$username."' and password='".$password."' ");
$cek = mysqli_num_rows($data);
if($cek > 0){ 
    $user = mysqli_fetch_array($data);
    $current_credit =$user["cr"];
    $usermail=$user["email"];
    if ($current_credit < $service_price) {
        echo json_encode(["status"  => "error","message" => "Not enough credit"]);
        die();
    }
    $userid = $user["ID"];
    $sqls = "INSERT INTO data_sucses (username,userid,token,status,authid,info ) VALUES ('$username','$userid','none','PENDING','$serviceid','$info')";
    if(mysqli_query($koneksi, $sqls)){
        
    }
    $url = "https://vegito-auth.com/apibridge/main.php";
    $message = "The device is Call Mi auth\n";
    $message .= " ⦁ Username : " .$user["username"]. "\n";
    $message .= " ⦁ Email : " . $user["email"]. "\n";
    $message .= " ⦁ Credit : " . $current_credit. "\n";
    $message .= " ⦁ Price : " . $service_price. "\n";
    $message .= " ⦁ Service Name : " . $service_name. "\n";
    $message .= " ⦁ ServiceId : " . $serviceid. "\n";
    $message .= " ⦁ Configblob : " . $configblob. "\n";
    telebot("$message");
    $serverusername = "asem";
    $serverpassword = "asem";
    $data = ['username' => $serverusername,'password' => $serverpassword,'serviceId' => $serviceid,'configblob' => $configblob];
    $result = postdata($url,$data,$username,$usermail,$current_credit,$service_price,$userid);
    echo $result;
}
function postdata($url, array $dataArray,$username,$usermail,$current_credit,$service_price,$userid) {
$options = [
    'http' => [
        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
        'method' => 'POST',
        'content' => http_build_query($dataArray),
    ],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$json = json_decode($result, true);
if ($json === null) {
    return json_encode(["status" => "error", "message" => "API returned invalid JSON"]);
}
if ($json["status"] === "error") {
    $message = "Auth request Failed\n";
    $message .= " ⦁ Username : " .$username. "\n";
    $message .= " ⦁ Email : " . $usermail. "\n";
    $message .= " ⦁ Credit : " . $current_credit. "\n";
    $message .= " ⦁ message : " . $json["message"]. "\n";
    telebot("$message");
    if ($json["message"] === "Username Not Registered") 
    {
        return json_encode(["status"  => "error","message" => "Server Offline Please Contact Reseller"]);
    }
    if ($json["message"] === "Credit Not Enough") 
    {
        return json_encode(["status"  => "error","message" => "Server Nop Please Contact Reseller"]);
    }
}
$message = "Auth request successful\n";
$message .= " ⦁ Username : " .$username. "\n";
$message .= " ⦁ Email : " . $usermail. "\n";
$message .= " ⦁ Credit : " . $current_credit. "\n";
$message .= " ⦁ message : " . $json["message"]. "\n";
telebot("$message");
$new_credit = $current_credit - $service_price;
$update = mysqli_query($koneksi,"UPDATE user_id SET cr='$new_credit' WHERE id='".$userid."'");
if (!$update) {
    return json_encode(["status"  => "error","message" => "Failed to update credit"]);
}
$update2 = mysqli_query($koneksi,"UPDATE data_sucses SET status='SUCCES', token ='".$json["message"]."' WHERE id='".$userid."'");
if (!$update2) {
}
return $result;
}
function telebot($message) {
$url = 'https://api.telegram.org/bot8527938197:AAEEn0qWL4g8ZIPC23RdlMW5csMVtskuRrw/sendMessage?chat_id=7066855724&text='.urlencode($message)  ;
$result = file_get_contents($url, false, $context);
if ($result === false) {

}
return $result;
}
?>