<?php
/**
 * Lucas IT Services - Email Auth Validator
 */

$output = "";
$domain = "";
$selector = "";
$raw_header = "";

function secure_clean($data) {
    if (empty($data)) return "";
    $data = trim($data);
    if (preg_match('/[^a-zA-Z0-9\._\-]/', $data)) return "";
    return $data;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $raw_header = $_POST['header_input'] ?? '';
    $manual_domain = secure_clean($_POST['domain'] ?? '');
    $manual_selector = secure_clean($_POST['selector'] ?? '');

    $parse_script = "parse_headers.sh";
    $check_script = "check_email_auth.sh";

    if (!empty($manual_domain)) {
        $domain = $manual_domain;
        $selector = $manual_selector;
    } elseif (!empty($raw_header)) {
        $tmp_file = tempnam(sys_get_temp_dir(), 'msg_');
        file_put_contents($tmp_file, $raw_header);
        $parse_out = [];
        exec($parse_script . " " . escapeshellarg($tmp_file), $parse_out);
        if (file_exists($tmp_file)) unlink($tmp_file);

        foreach ($parse_out as $line) {
            if (strpos($line, 'DOMAIN=') === 0) $domain = secure_clean(substr($line, 7));
            if (strpos($line, 'SELECTOR=') === 0) $selector = secure_clean(substr($line, 9));
        }
    }

    if (!empty($domain)) {
        $cmd = $check_script . " " . escapeshellarg($domain) . " " . escapeshellarg($selector);
        $output = shell_exec($cmd . " 2>&1");
    }
}
?>
<html>
    <head>
        <title>Lucas IT Services - Email Auth Validator</title>
        <link rel="icon" href="../images/favicon.ico" sizes="16x16 24x24 32x32 48x48 64x64" type="image/vnd.microsoft.icon"/>
        <link href="../css/tools.css" 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">Email Auth Validator</font></p>
        
        <p>Verify SPF, DKIM, and DMARC records via mail headers or manual lookup.</p>

        <form method="POST" action=".">
            <p>
                <strong>Paste Email Headers:</strong><br>
                <textarea name="header_input" style="width: 100%; height: 120px; font-family: monospace;"><?php echo htmlspecialchars($raw_header); ?></textarea>
            </p>

            <p>
                <strong>Manual Overrides:</strong><br>
                Domain: <input type="text" name="domain" value="<?php echo htmlspecialchars($domain); ?>" placeholder="example.com"> 
                Selector: <input type="text" name="selector" value="<?php echo htmlspecialchars($selector); ?>" placeholder="default">
            </p>

            <p>
                <input type="submit" name="validate" value="Validate Authentication" class="button">
                <input type="button" name="reset" value="Reset" class="button" onclick="window.location.href='.';">
            </p>
        </form>

        <?php if ($output): ?>
            <p><font size="+2">Results for <?php echo htmlspecialchars($domain); ?></font></p>
            <div class="boxed">
                <pre style="background: #000; color: #eee; padding: 10px; border: 1px solid #333; overflow-x: auto; font-family: monospace; line-height: 1.5; margin: 0;"><?php 
                    $ansi_map = [
                        "/\x1b\[0;32m/" => '<span style="color: #0f0;">',
                        "/\x1b\[1;33m/" => '<span style="color: #ff0;">',
                        "/\x1b\[0;31m/" => '<span style="color: #f00;">',
                        "/\x1b\[0m/"    => '</span>'
                    ];
                    echo preg_replace(array_keys($ansi_map), array_values($ansi_map), htmlspecialchars($output)); 
                ?></pre>
            </div>
        <?php endif; ?>
        
        <p>
            <a href="index.phps">View Source</a> | 
            <a href="../">Back to Tools</a>
        </p>
    </body>
</html>