<?php
/* --- PHP LOGIC AT THE TOP --- */
$output = "";
$cmd_used = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 // Whitelist: Alpha, numeric, dots, dashes, @, plus, and spaces
 $pattern = '/[^a-zA-Z0-9\s\.@\-+]/';
 
 if (!empty($_POST['advanced_cmd'])) {
 $clean_input = preg_replace($pattern, '', $_POST['advanced_cmd']);
 $cmd_used = "lits-dig.sh " . $clean_input;
 } else {
 $domain = preg_replace($pattern, '', $_POST['domain']);
 $server = !empty($_POST['server']) ? "@" . preg_replace($pattern, '', $_POST['server']) : "@1.1.1.1";
 $type = preg_replace($pattern, '', $_POST['type'] ?? 'A');
 
 $flags = "";
 if (isset($_POST['trace'])) $flags .= " +trace";
 if (isset($_POST['short'])) $flags .= " +short";
 if (isset($_POST['multiline'])) $flags .= " +multiline";
 
 $cmd_used = "/usr/local/bin/lits-dig.sh $server $domain $type $flags";
 }

 // Capture stdout and stderr
 $output = shell_exec($cmd_used . " 2>&1");
}
?>
<html>
    <head>
        <title>Lucas IT Services - Web Dig Utility</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" />
        <style>
            /* Shared Button Class */
            .button {
                background-color: rgb(45, 147, 231);
                color: white;
                border: none;
                padding: 0 15px;
                height: 32px;
                border-radius: 3px;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 13.3px;
                cursor: pointer;
                text-decoration: none;
                display: inline-flex;
                align-items: center;
                justify-content: center;
                box-sizing: border-box;
                transition: background-color 0.2s;
            }
            .button:hover {
                background-color: #1a7ab9;
            }
            input[type="text"], select {
                padding: 5px;
                height: 32px;
                border: 1px solid #ccc;
                box-sizing: border-box;
            }
        </style>
    </head>
    <body>
        <img src="../images/Logo.png" width="96" height="96"/>
        <p><font size="+4">Web Dig Utility</font></p>

        <form method="POST">
            <div style="margin-bottom: 15px; display: flex; align-items: center;">
                <input type="text" name="domain" placeholder="Target (domain.com)" 
                    value="<?= htmlspecialchars($_POST['domain'] ?? '') ?>" style="width: 200px;">
                &nbsp;
                <input type="text" name="server" placeholder="DNS Server (1.1.1.1)" 
                    value="<?= htmlspecialchars($_POST['server'] ?? '') ?>" style="width: 160px;">
                &nbsp;
                <select name="type">
                    <?php 
                        $types = ['A', 'MX', 'TXT', 'CNAME', 'NS', 'SOA', 'ANY'];
                        foreach ($types as $t) {
                            $selected = ($_POST['type'] ?? 'A') == $t ? 'selected' : '';
                            echo "<option value=\"$t\" $selected>$t</option>";
                        }
                    ?>
                </select>
                &nbsp;
                <input type="submit" value="Run Dig" class="button">
                &nbsp;
                <a href="./" class="button">Reset</a>
            </div>

            <div style="margin-bottom: 15px;">
                <label><input type="checkbox" name="trace" <?= isset($_POST['trace']) ? 'checked' : '' ?>> +trace</label> &nbsp;
                <label><input type="checkbox" name="short" <?= isset($_POST['short']) ? 'checked' : '' ?>> +short</label> &nbsp;
                <label><input type="checkbox" name="multiline" <?= isset($_POST['multiline']) ? 'checked' : '' ?>> +multiline</label>
            </div>

            <p><strong>Advanced / Custom Command:</strong></p>
            <input type="text" name="advanced_cmd" style="width:100%; max-width: 600px;" 
                placeholder="example.com MX +trace" value="<?= htmlspecialchars($_POST['advanced_cmd'] ?? '') ?>">
            
            <br><br>
        </form>

        <?php if ($output): ?>
            <p><strong>Result:</strong></p>
            <div class="boxed">
                <pre style="margin: 0; white-space: pre-wrap;"><?= htmlspecialchars($output) ?></pre>
            </div>
            <p><small>Command used: <code><?= htmlspecialchars($cmd_used) ?></code></small></p>
        <?php endif; ?>

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