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

$page_title   = 'SSL Check – Analyze SSL/TLS Certificate of a Website';
$page_desc    = 'Free SSL certificate checker: verify issuer, validity, algorithm, SAN domains and more – on ipcheck.tools.';
$page_current = 'ssl-check';

$query  = trim(strip_tags($_GET['host'] ?? ''));
$result = [];
$error  = '';

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

    $context = stream_context_create([
        'ssl' => [
            'capture_peer_cert' => true,
            'verify_peer'       => false,
            'verify_peer_name'  => false,
        ]
    ]);

    $client = @stream_socket_client(
        "ssl://{$host}:443", $errno, $errstr, 10,
        STREAM_CLIENT_CONNECT, $context
    );

    if (!$client) {
        $error = "Could not connect to <strong>" . h($host) . "</strong> on port 443. The server may not support SSL/TLS or is not reachable.";
    } else {
        $params = stream_context_get_params($client);
        $cert   = openssl_x509_parse($params['options']['ssl']['peer_certificate'] ?? '');
        fclose($client);

        if (!$cert) {
            $error = "SSL certificate could not be read.";
        } else {
            $valid_from  = date('d.m.Y', $cert['validFrom_time_t']);
            $valid_to    = date('d.m.Y', $cert['validTo_time_t']);
            $days_left   = (int)(($cert['validTo_time_t'] - time()) / 86400);
            $is_valid    = $days_left > 0;

            $san = [];
            if (isset($cert['extensions']['subjectAltName'])) {
                preg_match_all('/DNS:([^,\s]+)/', $cert['extensions']['subjectAltName'], $m);
                $san = $m[1];
            }

            $result = [
                'host'       => $host,
                'cn'         => $cert['subject']['CN'] ?? '–',
                'issuer'     => $cert['issuer']['O'] ?? ($cert['issuer']['CN'] ?? '–'),
                'issuer_cn'  => $cert['issuer']['CN'] ?? '–',
                'valid_from' => $valid_from,
                'valid_to'   => $valid_to,
                'days_left'  => $days_left,
                'is_valid'   => $is_valid,
                'san'        => $san,
                'serial'     => $cert['serialNumberHex'] ?? '–',
                'algo'       => $cert['signatureTypeSN'] ?? '–',
                'version'    => ($cert['version'] ?? 0) + 1,
            ];
        }
    }
}

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

<div class="hero">
  <div class="hero-label">Tool</div>
  <div class="hero-title">SSL Check</div>
  <div class="hero-sub">Analyze SSL/TLS certificate of a website</div>
  <form method="GET" action="/en/ssl-check.php">
    <div class="input-group">
      <input class="input-text" type="text" name="host"
             value="<?= h($query) ?>"
             placeholder="e.g. example.com or https://example.com"
             required />
      <button type="submit" class="btn btn-primary">
        <i class="ti ti-lock"></i> Check
      </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 ($result): ?>

    <div class="stat-grid">
      <div class="stat">
        <div class="stat-val" style="color:<?= $result['is_valid'] ? 'var(--green)' : 'var(--red)' ?>;">
          <?= $result['is_valid'] ? 'Valid' : 'Expired' ?>
        </div>
        <div class="stat-key">Status</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="color:<?= $result['days_left'] > 30 ? 'var(--green)' : ($result['days_left'] > 0 ? 'var(--orange)' : 'var(--red)') ?>;">
          <?= $result['days_left'] > 0 ? $result['days_left'] . ' days' : 'Expired' ?>
        </div>
        <div class="stat-key">Expires in</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="font-size:12px;"><?= h($result['issuer']) ?></div>
        <div class="stat-key">Issuer</div>
      </div>
      <div class="stat">
        <div class="stat-val"><?= count($result['san']) ?></div>
        <div class="stat-key">SAN Domains</div>
      </div>
    </div>

    <div class="grid2">
      <div class="card card-<?= $result['is_valid'] ? 'green' : 'blue' ?>">
        <div class="card-title"><i class="ti ti-lock"></i> Certificate Details</div>
        <div class="data-row"><span class="dk">Domain</span><span class="dv mono"><?= h($result['host']) ?></span></div>
        <div class="data-row"><span class="dk">Common Name (CN)</span><span class="dv mono" style="font-size:12px;"><?= h($result['cn']) ?></span></div>
        <div class="data-row"><span class="dk">Issuer</span><span class="dv"><?= h($result['issuer']) ?></span></div>
        <div class="data-row"><span class="dk">Issuer CN</span><span class="dv" style="font-size:12px;"><?= h($result['issuer_cn']) ?></span></div>
        <div class="data-row"><span class="dk">Valid from</span><span class="dv mono"><?= h($result['valid_from']) ?></span></div>
        <div class="data-row"><span class="dk">Valid until</span><span class="dv mono" style="color:<?= $result['days_left'] > 30 ? 'var(--green)' : 'var(--red)' ?>;"><?= h($result['valid_to']) ?></span></div>
        <div class="data-row"><span class="dk">Days remaining</span><span class="dv mono" style="color:<?= $result['days_left'] > 30 ? 'var(--green)' : ($result['days_left'] > 0 ? 'var(--orange)' : 'var(--red)') ?>;"><?= $result['days_left'] > 0 ? $result['days_left'] . ' days' : 'Expired' ?></span></div>
        <div class="data-row"><span class="dk">Serial number</span><span class="dv mono" style="font-size:11px;"><?= h($result['serial']) ?></span></div>
        <div class="data-row"><span class="dk">Algorithm</span><span class="dv mono"><?= h($result['algo']) ?></span></div>
        <div class="data-row"><span class="dk">Version</span><span class="dv mono">TLS v<?= h($result['version']) ?></span></div>
      </div>

      <?php if ($result['san']): ?>
      <div class="card">
        <div class="card-title"><i class="ti ti-list"></i> SAN Domains (<?= count($result['san']) ?>)</div>
        <?php foreach ($result['san'] as $san): ?>
        <div class="data-row">
          <span class="dk mono" style="font-size:12px;"><?= h($san) ?></span>
          <span class="dv">
            <a href="/en/ssl-check.php?host=<?= urlencode(ltrim($san, '*. ')) ?>" style="font-size:11px; color:var(--text4);">
              <i class="ti ti-external-link"></i>
            </a>
          </span>
        </div>
        <?php endforeach; ?>
      </div>
      <?php endif; ?>
    </div>

  <?php if ($result): ?>
    <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/headers.php?url=<?= urlencode('https://'.$result['host']) ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px;">
          <i class="ti ti-eye"></i> HTTP Headers
        </a>
        <a href="/en/dns-lookup.php?domain=<?= urlencode($result['host']) ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px; background:#1e1a3a; border-color:#3d3080; color:var(--purple-dim);">
          <i class="ti ti-search"></i> DNS Lookup
        </a>
        <a href="/en/whois.php?domain=<?= urlencode($result['host']) ?>" class="btn btn-ghost" style="font-size:12px; padding:6px 14px;">
          <i class="ti ti-id"></i> WHOIS
        </a>
        <a href="/en/ip-lookup.php?ip=<?= urlencode($result['host']) ?>" class="btn btn-ghost" style="font-size:12px; padding:6px 14px;">
          <i class="ti ti-world-search"></i> IP Lookup
        </a>
      </div>
    </div>
  <?php endif; ?>
  <?php else: ?>
    <div class="alert alert-info">
      <i class="ti ti-info-circle"></i>
      <span>Enter a URL or domain to check the SSL certificate. Works without https:// too.</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/ssl-check.php" class="tool-btn current"><i class="ti ti-lock"></i><span>SSL Check</span></a>
    <a href="/en/dns-lookup.php" class="tool-btn"><i class="ti ti-search"></i><span>DNS Lookup</span></a>
    <a href="/en/headers.php"   class="tool-btn"><i class="ti ti-eye"></i><span>HTTP Headers</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 an SSL certificate?</h2>
    <p>An SSL/TLS certificate is a digital document that confirms the identity of a website and enables encrypted data transmission. The "https://" in the address bar indicates that a secure connection exists. Important components: The <strong>CN (Common Name)</strong> specifies which domain the certificate was issued for. The <strong>CA (Certificate Authority)</strong> is the trusted entity that issued the certificate (e.g. Let's Encrypt, DigiCert). <strong>SAN entries</strong> (Subject Alternative Names) list all domains for which the certificate is valid.</p>
  </div>

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