<?php
require_once __DIR__ . "/../Febe.php";
$febe = new Febe;
$febe->setCurrentSubFolderLanguageCookie();
$lpType = "";
$server = "";

//check if nginx is passing our header
if(!empty($_SERVER['HTTP_PX_BLOCK_MISSING_PARAMETER'])) {
    //block if the get parameter, passed in the header, is missing
    if(empty($_GET[$_SERVER['HTTP_PX_BLOCK_MISSING_PARAMETER']])) {
        http_response_code(403);
        die();
    }
}

$cfl = !empty($_GET["intgrtn_cfl"]) ? $_GET["intgrtn_cfl"] : false;
if(!empty($_GET["intgrtn_server"])) {
	$server = "//" . $_GET["intgrtn_server"];
}
$disableMetaDesc = false;
$availableLpTypes = array("1stepnopwd", "singlepg", "2steps");
if (!empty($_GET["intgrtn_lpType"])) {
	if (in_array($_GET["intgrtn_lpType"], $availableLpTypes)) {
		$lpType = $_GET["intgrtn_lpType"];
	}
}
$offerName = !empty($_GET["intgrtn_offerName"]) ? $_GET["intgrtn_offerName"] : "Offer Url";
$disableMetaDesc = !empty($_GET["intgrtn_disableMetaDesc"]) ? $_GET["intgrtn_disableMetaDesc"] : false;
if ($disableMetaDesc != true) {
	$offerDesc = !empty($_GET["intgrtn_offerName"]) ? $_GET["intgrtn_offerName"] : "Trading Software";
}
function getTrackingUrl(): string
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://server.4markets.net/api/v2/offers/tracking-domain");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Referer: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
    ]);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode != 200) {
        return "";
    }
    if (empty($response)) {
        return "";
    }

    $responseTrackingDomainJson = json_decode($response);

    if (empty($responseTrackingDomainJson)) {
        return "";
    }

    return $responseTrackingDomainJson->details->endpoint;
}

$trackingUrl = getTrackingUrl();
?>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<head>
    <?php
	if ($disableMetaDesc != true) {
    ?>
    <meta name="description" content="<?= $offerDesc ?>">
    <?php }?>
    <meta charset="UTF-8">
    <title><?= $offerName; ?></title>
    <base href="<?= $febe->getHref(); ?>">
    <link rel="shortcut icon" type="image/png" href="favicon.png"/>
    <link rel="apple-touch-icon" href="favicon.png"/>
    <meta name="robots" content="noindex, nofollow">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/video-js.css">
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="css/intgrtn.css?v=<?php echo filemtime(__DIR__ . '/../css/intgrtn.css'); ?>">

    <style class="js-intgrtn-i18n-styles">
        [data-i18n] {
            opacity: 0;
        }
    </style>
