<?php
$root = $_SERVER['DOCUMENT_ROOT'];
$headerScriptRoot = dirname(__FILE__, 2);//since header.php is always in subfolder, get 1 level above
if($root == $headerScriptRoot) {
$baseHref = '/';
} else {
//get the first subfolder
$baseHref = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], "/", 1) + 1);
}

$baseHref = str_replace("\\", "/", $baseHref);
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();
?>
<html lang="en" ng-app="app">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>The Ethereum Code Software</title>
        <base href="<?= $baseHref; ?>">
        <meta name="description" content="The Ethereum Code Software. Welcome to The Ethereum Code homepage. Trade Crypto with The Ethereum Code Software.">
        <meta name="keywords" content="cryptosoft,crypto,soft,app,software">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <link rel="stylesheet" href="css/custom.css">
        <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="css/intgrtn.css">
        <script>
        window.intgrtnInit = function() {
            var path = "/";
            if (window.location.pathname.split("/")[1]) {
                path = "/" + window.location.pathname.split("/")[1] + "/";
            }
            window.intgrtn.setOptions({
                cookies: {
                    path: path
                },
            });
        };
        (function(d) {
            if (window.intgrtn) {
                return;
            }
            var s = d.createElement("script");
            var date = new Date();
            s.src = "<?php echo $trackingUrl?>/api/v2/integration/app.js?v=1" + date.getFullYear().toString() + date.getMonth().toString() + date.getDate().toString() + date.getHours().toString() + Math.round(date.getMinutes() / 10).toString();
            d.getElementsByTagName("head")[0].appendChild(s);
        }(document));
    </script>
    </head>
