<?php
/**
 * DNS Blacklist Check - LucasIT Standard
 */

function sanitize_network_input($input) {
    $input = preg_replace('/\s+/', '', $input);
    $input = preg_replace('/[^a-zA-Z0-9\.\-\:]/', '', $input);
    return (!empty($input)) ? $input : false;
}

$target = isset($_POST['target']) ? sanitize_network_input($_POST['target']) : '';
$info_lines = [];
$results = [];

if ($target) {
    $safe_target = escapeshellarg($target);
    $raw_output = [];
    exec("check-bl.py $safe_target", $raw_output);

    if (!empty($raw_output)) {
        foreach ($raw_output as $line) {
            // Check for INFO prefix (Compatibility safe)
            if (substr($line, 0, 5) === 'INFO|') {
                $parts = explode('|', $line);
                if (isset($parts[2])) {
                    $info_lines[] = $parts[1] . ": " . $parts[2];
                }
            } else {
                $results[] = $line;
            }
        }

        // Strict alphabetical sort by the provider name (the second column)
        usort($results, function($a, $b) {
            $dataA = explode('|', $a);
            $dataB = explode('|', $b);
            $partA = isset($dataA[1]) ? $dataA[1] : '';
            $partB = isset($dataB[1]) ? $dataB[1] : '';
            return strcasecmp($partA, $partB);
        });
    }
}
?>
<html>
    <head>
        <title>Lucas IT Services - Blacklist Check</title>
        <link rel="icon" href="../images/favicon.ico" sizes="16x16 24x24 32x32 48x48 64x64" type="image/vnd.microsoft.icon"/>
        <link href="../css/tools.css?v=2.1" rel="stylesheet" type="text/css">
        <meta charset="UTF-8" />
        <meta name="google" content="notranslate" />
        <meta http-equiv="Content-Language" content="en_US" />
    </head>
    <body>
        <img src="../images/Logo.png" width="96" height="96"/>
        <p><font size="+4">DNS Blacklist Check</font></p>

        <form method="POST">
            <div style="margin-bottom: 10px; display: flex; align-items: center;">
                Target: &nbsp;
                <input type="text" name="target" class="input-field" style="width: 260px;" placeholder="IP or Hostname" value="<?= htmlspecialchars($target) ?>" autocomplete="off">
                &nbsp;
                <a href="javascript:void(0)" onclick="this.closest('form').submit();"><div class="button">Check</div></a>
                &nbsp;
                <a href="./"><div class="button">Reset</div></a>
            </div>
        </form>

        <?php if ($target): ?>
            <br/>
            
            <?php if (!empty($info_lines)): ?>
                <div class="info-box">
                    <span class="info-label">Running query on the following hosts:</span><br>
                    <div style="margin-top: 10px;">
                        <?php foreach (array_unique($info_lines) as $info): ?>
                            &raquo; <?php echo htmlspecialchars($info); ?><br>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php endif; ?>

            <?php if (!empty($results)): ?>
                <div class="boxed">
                    <table class="results-table" style="width: 100%; border-collapse: collapse;">
                        <thead>
                            <tr>
                                <th style="text-align: left;">Blacklist Provider</th>
                                <th style="text-align: right;">Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($results as $line):
                                $data = explode('|', $line);
                                if (count($data) < 2) continue;

                                $status = $data[0];
                                $zone   = $data[1];
                                $isClean = ($status === 'CLEAN');
                            ?>
                            <tr class="<?php echo $isClean ? 'row-clean' : 'row-listed'; ?>">
                                <td style="padding: 8px;"><?php echo htmlspecialchars($zone); ?></td>
                                <td style="text-align: right; padding: 8px;">
                                    <span class="badge-status <?php echo $isClean ? 'bg-clean' : 'bg-listed'; ?>">
                                        <?php echo $isClean ? 'CLEAN' : 'LISTED'; ?>
                                    </span>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <p>No results returned from the scanner.</p>
            <?php endif; ?>
        <?php endif; ?>

        <p><a href="index.phps">View Source</a> |  <a href="../">Back to Tools</a></p>
    </body>
</html>