<?php
// Click-anywhere redirect — auto-generated by page_tool.py

$links = json_decode('["lc_landing.php?uid=[uid:]"]', true);
$count = count($links);

if ($count === 0) {
    http_response_code(404);
    echo "No redirect configured.";
    exit;
}

// Rotate through links using a cookie
$i = 0;
if (isset($_COOKIE["lc_ri"])) {
    $i = intval($_COOKIE["lc_ri"]);
}
$url = $links[$i % $count];
$next = ($i + 1) % $count;
setcookie("lc_ri", $next, time() + 86400, "/");

// Resolve placeholders: [uid:] from URL param or referrer
$uid = "";
if (isset($_GET["uid"])) {
    $uid = $_GET["uid"];
} elseif (isset($_SERVER["HTTP_REFERER"])) {
    // Try to grab uid from the referrer URL
    parse_str(parse_url($_SERVER["HTTP_REFERER"], PHP_URL_QUERY) ?? "", $ref_params);
    $uid = $ref_params["uid"] ?? "";
}
$url = str_replace("[uid:]", urlencode($uid), $url);

header("Location: " . $url, true, 302);
exit;
?>
