<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/functions.php';

$page_title   = 'DNS Lookup – Query DNS Records of a Domain';
$page_desc    = 'Free DNS Lookup: query A, AAAA, MX, NS, TXT, CNAME records of any domain – fast and free on ipcheck.tools.';
$page_current = 'dns-lookup';

$query   = trim(strip_tags($_GET['domain'] ?? ''));
$type    = strtoupper(trim($_GET['type'] ?? 'ALL'));
$results = [];
$error   = '';
$counts  = [];

$valid_types = ['ALL','A','AAAA','MX','NS','TXT','CNAME','SOA','PTR','SRV'];
if (!in_array($type, $valid_types)) $type = 'ALL';

if ($query) {
    $host = preg_replace('/^https?:\/\//i', '', $query);
    $host = explode('/', $host)[0];
    $host = strtolower(trim($host));

    $check_types = $type === 'ALL' ? ['A','AAAA','MX','NS','TXT','CNAME'] : [$type];

    foreach ($check_types as $t) {
        $dns = @dns_get_record($host, constant('DNS_' . $t));
        if ($dns) {
            foreach ($dns as $rec) {
                $entry = ['type' => $t, 'ttl' => $rec['ttl'] ?? 0];
                switch ($t) {
                    case 'A':     $entry['ip']     = $rec['ip'] ?? ''; break;
                    case 'AAAA':  $entry['ipv6']   = $rec['ipv6'] ?? ''; break;
                    case 'MX':    $entry['target']  = $rec['target'] ?? ''; $entry['pri'] = $rec['pri'] ?? 0; break;
                    case 'NS':    $entry['target']  = $rec['target'] ?? ''; break;
                    case 'TXT':   $entry['txt']     = implode('', $rec['entries'] ?? [$rec['txt'] ?? '']); break;
                    case 'CNAME': $entry['target']  = $rec['target'] ?? ''; break;
                }
                $results[$t][] = $entry;
            }
            $counts[$t] = count($dns);
        }
    }

    if (empty($results)) {
        $error = "No DNS records found for <strong>" . h($host) . "</strong>.";
    }
}

$total = array_sum($counts);
$has_mx = !empty($results['MX']);
$has_aaaa = !empty($results['AAAA']);

require_once __DIR__ . '/header.php';
?>

<div class="hero">
  <div class="hero-label">Tool</div>
  <div class="hero-title">DNS Lookup</div>
  <div class="hero-sub">Query and analyze DNS records of a domain</div>
  <form method="GET" action="/en/dns-lookup.php">
    <div class="input-group">
      <input class="input-text" type="text" name="domain"
             value="<?= h($query) ?>"
             placeholder="e.g. example.com"
             required />
      <select name="type" class="input-select">
        <?php foreach ($valid_types as $t): ?>
        <option value="<?= $t ?>" <?= $type === $t ? 'selected' : '' ?>><?= $t ?></option>
        <?php endforeach; ?>
      </select>
      <button type="submit" class="btn btn-primary">
        <i class="ti ti-search"></i> Query
      </button>
    </div>
  </form>
</div>

<div class="wrap">

  <div class="ad-slot"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8287576653347400" data-ad-slot="2715725452" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>

  <?php if ($error): ?>
    <div class="alert alert-danger"><i class="ti ti-alert-circle"></i><span><?= $error ?></span></div>

  <?php elseif ($results): ?>

    <div class="stat-grid">
      <div class="stat">
        <div class="stat-val" style="color:var(--green);">OK</div>
        <div class="stat-key">Status</div>
      </div>
      <div class="stat">
        <div class="stat-val"><?= $total ?></div>
        <div class="stat-key">Records total</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="color:<?= $has_mx ? 'var(--green)' : 'var(--text4)' ?>;">
          <?= $has_mx ? 'Yes' : 'No' ?>
        </div>
        <div class="stat-key">Mail (MX)</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="color:<?= $has_aaaa ? 'var(--purple-dim)' : 'var(--text4)' ?>;">
          <?= $has_aaaa ? 'Yes' : 'No' ?>
        </div>
        <div class="stat-key">IPv6 (AAAA)</div>
      </div>
    </div>

    <?php foreach ($results as $rtype => $records): ?>
    <div class="card">
      <div class="card-title">
        <i class="ti ti-dns"></i> <?= $rtype ?>-Records (<?= count($records) ?>)
      </div>
      <table class="result-table">
        <thead>
          <tr>
            <th style="width:60px;">Type</th>
            <th>Value</th>
            <th style="text-align:right; width:80px;">TTL</th>
            <th style="text-align:right; width:100px;">Action</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($records as $rec): ?>
          <tr>
            <td><span class="badge badge-v4"><?= $rtype ?></span></td>
            <td class="mono" style="font-size:12px; word-break:break-all;">
              <?php if ($rtype === 'A'): ?>
                <?= h($rec['ip']) ?>
              <?php elseif ($rtype === 'AAAA'): ?>
                <span style="color:var(--purple-dim);"><?= h($rec['ipv6']) ?></span>
              <?php elseif ($rtype === 'MX'): ?>
                Priority <?= $rec['pri'] ?> → <?= h(trim($rec['target'], '.')) ?>
              <?php elseif ($rtype === 'TXT'): ?>
                <span style="color:var(--text3); font-size:11px;"><?= h($rec['txt']) ?></span>
              <?php else: ?>
                <?= h(trim($rec['target'] ?? '', '.')) ?>
              <?php endif; ?>
            </td>
            <td class="mono" style="text-align:right; color:var(--text4);"><?= $rec['ttl'] ?>s</td>
            <td style="text-align:right;">
              <?php if ($rtype === 'A'): ?>
                <a href="/en/ip-lookup.php?ip=<?= urlencode($rec['ip']) ?>" class="btn btn-ghost" style="font-size:11px; padding:3px 8px; margin-left:8px;">
                  <i class="ti ti-world-search"></i> IP Lookup
                </a>
              <?php elseif ($rtype === 'MX'): ?>
                <a href="/en/blacklist.php?ip=<?= urlencode(trim($rec['target'], '.')) ?>" class="btn btn-ghost" style="font-size:11px; padding:3px 8px; margin-left:8px;">
                  <i class="ti ti-shield-search"></i> Blacklist
                </a>
              <?php endif; ?>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
    <?php endforeach; ?>

    <div class="card">
      <div class="card-title"><i class="ti ti-arrow-right"></i> Continue with</div>
      <div style="display:flex; gap:8px; flex-wrap:wrap;">
        <a href="/en/ssl-check.php?host=<?= urlencode($query) ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px;">
          <i class="ti ti-lock"></i> SSL Check
        </a>
        <a href="/en/whois.php?domain=<?= urlencode($query) ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px; background:#1e1a3a; border-color:#3d3080; color:var(--purple-dim);">
          <i class="ti ti-id"></i> WHOIS
        </a>
        <a href="/en/headers.php?url=<?= urlencode('https://'.$query) ?>" class="btn btn-ghost" style="font-size:12px; padding:6px 14px;">
          <i class="ti ti-eye"></i> HTTP Headers
        </a>
      </div>
    </div>

  <?php else: ?>
    <div class="alert alert-info">
      <i class="ti ti-info-circle"></i>
      <span>Enter a domain name to query its DNS records. You can filter by record type using the dropdown.</span>
    </div>
  <?php endif; ?>

  <div class="ad-slot"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8287576653347400" data-ad-slot="2715725452" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>

  <div class="tools-row">
    <a href="/en/"                class="tool-btn"><i class="ti ti-home"></i><span>My IP</span></a>
    <a href="/en/dns-lookup.php"  class="tool-btn current"><i class="ti ti-search"></i><span>DNS Lookup</span></a>
    <a href="/en/ssl-check.php"   class="tool-btn"><i class="ti ti-lock"></i><span>SSL Check</span></a>
    <a href="/en/whois.php"       class="tool-btn"><i class="ti ti-id"></i><span>WHOIS</span></a>
    <a href="/en/ip-lookup.php"   class="tool-btn"><i class="ti ti-world-search"></i><span>IP Lookup</span></a>
  </div>

  <div class="seo-box">
    <h2>What is a DNS Lookup?</h2>
    <p>A DNS Lookup queries the DNS (Domain Name System) records of a domain. DNS is like the phone book of the internet – it translates domain names like "example.com" into IP addresses. Different record types serve different purposes: <strong>A records</strong> point to IPv4 addresses, <strong>AAAA records</strong> to IPv6, <strong>MX records</strong> define mail servers, <strong>NS records</strong> name the authoritative nameservers, and <strong>TXT records</strong> contain text data like SPF or verification codes.</p>
  </div>

<?php require_once __DIR__ . '/footer.php'; ?>
