<?php
// ================= CONFIG =================
$indexNowKey      = 'fc56483a7704421487dc4f8e6e4e86ab';           // ← Your generated IndexNow key
$yourHost         = 'https://discordbooster.com';           // ← Your verified site (with https://)
$keyFileLocation  = $yourHost . '/' . $indexNowKey . '.txt';  // Must be publicly accessible

// ================= FUNCTIONS =================
function submitUrlToIndexNow($urlToSubmit, $key, $host, $keyLocation) {
    // Recommended: Use the universal IndexNow endpoint (shares with Bing + others)
    $endpoint = 'https://api.indexnow.org/indexnow';

    // For single URL simple GET (easiest & works perfectly for most cases)
    $simpleGetUrl = $endpoint . '?url=' . urlencode($urlToSubmit) . '&key=' . urlencode($key);

    // For batch / more formal POST (optional - use if submitting many URLs at once)
    $payload = json_encode([
        'host'        => parse_url($host, PHP_URL_HOST),  // just the domain, e.g. yourdomain.com
        'key'         => $key,
        'keyLocation' => $keyLocation,
        'urlList'     => [$urlToSubmit]
    ]);

    // You can switch between GET and POST — here we show both (use one)

    // === Option 1: Simple GET (recommended for single URL) ===
    $ch = curl_init($simpleGetUrl);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 10,
        CURLOPT_FOLLOWLOCATION => true,
    ]);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error    = curl_error($ch);
    curl_close($ch);

    if ($httpCode >= 200 && $httpCode < 300) {
        return [
            'success'    => true,
            'http_code'  => $httpCode,
            'message'    => 'URL submitted successfully (IndexNow accepted)',
            'raw'        => $response ?: '(no body - normal for 200/202)'
        ];
    }

    // === Option 2: POST batch (uncomment if you prefer / need batch) ===
    /*
    $ch = curl_init($endpoint);
    curl_setopt_array($ch, [
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $payload,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => [
            'Content-Type: application/json; charset=utf-8'
        ],
        CURLOPT_TIMEOUT        => 15,
    ]);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error    = curl_error($ch);
    curl_close($ch);
    */

    return [
        'success'   => false,
        'http_code' => $httpCode,
        'message'   => $error ?: 'Failed - check key, key file location, or URL format',
        'raw'       => $response
    ];
}

// ================= FORM HANDLING =================
$result = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['url'])) {
    $urlToSubmit = trim($_POST['url']);
    
    // Basic validation
    if (filter_var($urlToSubmit, FILTER_VALIDATE_URL) === false) {
        $result = 'Invalid URL format.';
    } else if (strpos($urlToSubmit, $yourHost) !== 0) {
        $result = 'URL must belong to your domain (' . $yourHost . ')';
    } else {
        $apiResult = submitUrlToIndexNow($urlToSubmit, $indexNowKey, $yourHost, $keyFileLocation);
        
        if ($apiResult['success']) {
            $success = true;
            $result  = "Success! URL submitted to IndexNow.\n"
                     . "HTTP Code: " . $apiResult['http_code'] . "\n"
                     . $apiResult['message'];
        } else {
            $result = "Error submitting URL.\n"
                    . "HTTP Code: " . $apiResult['http_code'] . "\n"
                    . $apiResult['message'] . "\n\nRaw response:\n" . $apiResult['raw'];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Bing IndexNow URL Submission (2026)</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 40px auto; padding: 0 20px; }
        h2 { color: #0067c5; }
        input[type="url"] { width: 100%; max-width: 500px; padding: 10px; font-size: 16px; }
        button { padding: 10px 20px; font-size: 16px; background: #0067c5; color: white; border: none; cursor: pointer; }
        button:hover { background: #0054a0; }
        pre { background: #f8f9fa; padding: 15px; border: 1px solid #ddd; white-space: pre-wrap; word-wrap: break-word; }
        .success { color: green; font-weight: bold; }
        .error   { color: red;   font-weight: bold; }
    </style>
</head>
<body>

<h2>Bing IndexNow – Submit URL for Fast Indexing</h2>
<p>Uses the modern IndexNow protocol (recommended by Bing in 2026 – no Azure needed).</p>

<form method="post">
    <p>
        <input type="url" name="url" placeholder="https://yourdomain.com/new-page" required autofocus>
    </p>
    <p>
        <button type="submit">Submit URL to Bing (IndexNow)</button>
    </p>
</form>

<?php if ($result): ?>
    <h3>Result:</h3>
    <pre class="<?= $success ? 'success' : 'error' ?>">
<?= htmlspecialchars($result) ?>
    </pre>
<?php endif; ?>

<p><small>Note: Make sure your key file is live at <code><?= htmlspecialchars($keyFileLocation) ?></code></small></p>

</body>
</html>