<?php

if (isset($_GET["username"])) {
    $username = $_GET["username"];
}
if (isset($_GET["password"])) {
    $password = $_GET["password"];
}
if (isset($_GET["action"])) {
    $action = $_GET["action"];
}
if (isset($_GET["action"])) {
    $db = new SQLite3("./ott.db");
    $res = $db->query("SELECT * FROM dns");
    $arr = [];
    while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
        $arr[] = $row["url"];
        foreach ($arr as $value) {
            $api_call = call_api((string) $value . "/player_api.php?username=" . $username . "&password=" . $password);
            if ($api_call->user_info->auth == 1) {
                $dns = $value;
                $xc_api = substr(json_encode($api_call), 1);
            }
        }
    }
    if ($action == "getLinks") {
        $dns_arr = str_replace(["http:\\/\\/", "https:\\/\\/"], "", json_encode($arr));
        $dns_arr = json_encode(json_decode($dns_arr), true);
        $string = "{\"result\":{\"links_encrypted\":" . $dns_arr . "},\"status\":\"true\"," . $xc_api;
        echo do_work($string);
    } else {
        if ($action == "getMovie") {
            $protocol = parse_url($dns, PHP_URL_SCHEME);
            $json = call_api((string) $dns . "/panel_api.php?username=" . $username . "&password=" . $password);
            if ($protocol == "https") {
                $port = $json->server_info->https_port;
            } else {
                $port = $json->server_info->port;
            }
            $link = $protocol . "://" . $json->server_info->url . ":" . $port;
            $vod_arr = [];
            $json_vod = call_api((string) $dns . "/player_api.php?username=" . $username . "&password=" . $password . "&action=get_vod_streams");
            for ($i = 0; $i < sizeof($json_vod); $i++) {
                $vod_val = $json_vod[$i];
                $vod_arr[] = ["num" => $vod_val->num, "name" => $vod_val->name, "type" => $vod_val->stream_type, "url" => $link . "/" . $vod_val->stream_type . "/" . $json->user_info->username . "/" . $json->user_info->password . "/" . $vod_val->stream_id . "." . $vod_val->container_extension, "img" => $vod_val->stream_icon, "rating" => $vod_val->rating, "date" => $vod_val->added, "container" => $vod_val->container_extension, "id" => $vod_val->stream_id];
            }
            echo do_work(json_encode($vod_arr, true));
        } else {
            echo do_work("{\"user_info\":{\"auth\":0}}");
        }
    }
} else {
    echo "{\"user_info\":{\"auth\":0}}";
}
function call_api($api_link)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_link);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $result = json_decode(curl_exec($ch));
    if (!empty($result)) {
        return $result;
    }
    return NULL;
}
function do_work($string)
{
    $encrypt_method = "AES-128-CBC";
    $iv = "765c21fba9843ed0";
    $key = "c23f1789ab40de56";
    return bin2hex(openssl_encrypt(base64_encode($string), $encrypt_method, $key, OPENSSL_RAW_DATA, $iv));
}

?>