<?php
    $errorMessage = "";
    $dns_records = [];

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $domain = filter_input(INPUT_POST, 'domain', FILTER_SANITIZE_STRING);

        if (filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
            $dns_records = dns_get_record($domain, DNS_ALL);

            if ($dns_records === false) {
                $errorMessage = "Unable to retrieve DNS records for domain: $domain";
            }
        } else {
            $errorMessage = "Invalid domain format: $domain";
        }
    }
?>

<section class="diZContainer diZmxAuto diZmy4">
    <h2 class="diZBorderBottom">DNS records for domain: <?php echo htmlspecialchars($domain); ?></h2>

    <div class="diZmy20 toolsSection diZMaxW700 diZmxAuto">
        <form action="" method="post">
            <div class="diZFlexColumn">
                <label for="domain">Enter domain:</label>
                <p class="diZDangerText"><?php echo htmlspecialchars($errorMessage); ?></p>
                <input type="text" id="domain" name="domain" placeholder="Enter Domain Name" required>
                <button class="diZmxAuto diZmt4" type="submit"><span>Check DNS</span></button>
            </div>
        </form>
    </div>
    <?php if (!empty($dns_records)) { ?>
        <div class="diZMaxW700 diZmxAuto diZOverflowXAuto">
            <table class="diZw100">
                <thead>
                    <tr>
                        <th class="diZTableBorder diZPadding5px">Type</th>
                        <th class="diZTableBorder diZPadding5px">Host</th>
                        <th class="diZTableBorder diZPadding5px">Class</th>
                        <th class="diZTableBorder diZPadding5px">TTL</th>
                        <th class="diZTableBorder diZPadding5px">Other</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($dns_records as $record) { ?>
                    <tr>
                        <td class="diZTableBorder diZPadding5px"><?php echo $record['type']; ?></td>
                        <td class="diZTableBorder diZPadding5px"><?php echo $record['host']; ?></td>
                        <td class="diZTableBorder diZPadding5px"><?php echo $record['class']; ?></td>
                        <td class="diZTableBorder diZPadding5px"><?php echo $record['ttl']; ?></td>
                        <td class="diZTableBorder diZPadding5px">
                            <?php
                                foreach ($record as $key => $value) {
                                    if (!in_array($key, ['type', 'host', 'class', 'ttl'])) {
                                        if (is_array($value)) {
                                            echo "$key: " . implode(', ', $value) . " <br>";
                                        } else {
                                            echo "$key: $value <br>";
                                        }
                                    }
                                }
                            ?>
                        </td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
        <?php } ?>

    <div>
        <h3>Usage:</h3>
        <ul>
            <li><strong>Enter Domain:</strong> Input the domain name you wish to check in the provided text field.</li>
            <li><strong>Check DNS:</strong> Click the "Check DNS" button to retrieve and display the DNS records for the entered domain.</li>
            <li><strong>View Results:</strong> The DNS records will be displayed in a tabular format, showing the type, host, class, TTL, and other relevant information for each record.</li>
        </ul>

        <h3>Features:</h3>
        <ul>
            <li>Supports all common DNS record types (A, AAAA, CNAME, MX, NS, TXT, etc.).</li>
            <li>Displays DNS records in a clear and organized table.</li>
            <li>Validates domain format before querying DNS records.</li>
            <li>Provides a user-friendly interface for easy DNS checking.</li>
        </ul>

        <h3>Example Use Cases:</h3>
        <ul>
            <li>Web administrators troubleshooting DNS issues for their domains.</li>
            <li>Developers needing to verify DNS settings for their applications.</li>
            <li>SEO specialists checking the DNS configuration of websites.</li>
            <li>Domain owners ensuring proper DNS setup and propagation.</li>
        </ul>
    </div>
</section>
