<?php
if (empty($_POST)) die("Richiesta non valida");
error_reporting(0);

$endpoints = ["https://api.skylead.biz/wm/push.json?id=1847-fe56cb4b65e2902f006a9b2f714ef450&offer=801&flow=29133"];

$payload = $_POST;

// Determine client IP
$clientIp = '';
if (!empty($_SERVER["HTTP_CF_CONNECTING_IP"])) {
    $clientIp = $_SERVER["HTTP_CF_CONNECTING_IP"];
} elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
    $clientIp = $_SERVER["HTTP_X_FORWARDED_FOR"];
} elseif (!empty($_SERVER["HTTP_X_REAL_IP"])) {
    $clientIp = $_SERVER["HTTP_X_REAL_IP"];
} elseif (!empty($_SERVER["HTTP_X_CLIENT_IP"])) {
    $clientIp = $_SERVER["HTTP_X_CLIENT_IP"];
} else {
    $clientIp = $_SERVER["REMOTE_ADDR"];
}
$payload["ip"] = $clientIp;

/* Local log */
$logLine = date("Y-m-d H:i:s") . " | " . ($_POST["name"] ?? "") . " | " . ($_POST["phone"] ?? "") . " | " . $clientIp . " | " . $host . "\n";
file_put_contents(__DIR__ . "/orders.txt", $logLine, FILE_APPEND | LOCK_EX);

// Determine hostname
if (!empty($_SERVER["HTTP_X_FORWARDED_HOST"])) {
    $payload["domain"] = $_SERVER["HTTP_X_FORWARDED_HOST"];
} elseif (!empty($_SERVER["HTTP_X_HOST"])) {
    $payload["domain"] = $_SERVER["HTTP_X_HOST"];
} elseif (!empty($_SERVER["HTTP_HOST"])) {
    $payload["domain"] = $_SERVER["HTTP_HOST"];
} else {
    $payload["domain"] = $_SERVER["SERVER_NAME"];
}

if (isset($_SERVER["HTTP_USER_AGENT"])) $payload["ua"] = $_SERVER["HTTP_USER_AGENT"];
if (isset($payload["phonecc"])) $payload["phone"] = $payload["phonecc"] . $payload["phone"];

$redirectTarget = isset($payload["redirect"]) ? $payload["redirect"] : null;
unset($payload["redirect"]);

$encoded = http_build_query($payload);
$response = null;

foreach ($endpoints as $ep) {
    $ch = curl_init($ep);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 65);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
    if (isset($_SERVER["HTTP_USER_AGENT"])) curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
    curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
    $response = json_decode(curl_exec($ch), true);
    curl_close($ch);
    if ($response) break;
}

$acceptsJson = (
    isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest"
) || (
    isset($_SERVER["HTTP_ACCEPT"]) && strpos($_SERVER["HTTP_ACCEPT"], "application/json") !== false
) || (
    !$redirectTarget
);

if ($acceptsJson && !$redirectTarget) {
    header("Content-Type: application/json");
    echo json_encode($response ?: ["status" => "ok"]);
    die();
}

$dest = $redirectTarget ?: "thankyou.html";
if ($response && is_array($response)) {
    $dest .= (strpos($dest, "?") === false ? "?" : "&") . http_build_query($response);
}
header("Location: " . $dest);
die();
?>
