<?php
/**
 * Lucas IT Services - SSL Expiration Monitor
 * Version 1.0.0 - Form on Grey / Results in Boxed
 */
$output = "";
$targets_input = "";
$timeout_val = "4.0";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $targets_input = $_POST['targets_input'] ?? '';
    $timeout_val = $_POST['timeout'] ?? '4.0';

    // Verify fallback numeric float limits for the Python process
    if (!is_numeric($timeout_val) || floatval($timeout_val) <= 0) {
        $timeout_val = "4.0";
    }

    // Direct path execution routing to the updated positional python engine
    $check_script = "../cgi-bin/check-cert-exp.py";

    if (!empty(trim($targets_input))) {
        // Build arguments cleanly out of raw multiline/comma values
        // Python handles space-separated positional args automatically via nargs="*"
        $raw_args = preg_split('/[\s,]+/', trim($targets_input));
        $escaped_args = array_map('escapeshellarg', $raw_args);
        
        if (!empty($escaped_args)) {
            $cmd = $check_script . " --timeout " . escapeshellarg($timeout_val) . " " . implode(" ", $escaped_args);
            // Ingest raw JSON payload directly from the python script pipeline
            $output = shell_exec($cmd . " 2>&1");
        }
    }
}
?><!DOCTYPE html>
<html data-theme="dark">
    <head>
        <title>Lucas IT Services - SSL Expiration Monitor</title>
        <link rel="icon" type="image/svg+xml" href="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 50 50'><path fill='%230078d4' fill-rule='evenodd' d='M 25 0 A 24.999868 25.000025 0 0 0 0 25 A 24.999868 25.000025 0 0 0 25 50 A 24.999868 25.000025 0 0 0 50 25 A 24.999868 25.000025 0 0 0 25 0 z M 25 6.9960938 A 18.000229 18.000229 0 0 1 43 24.996094 A 18.000229 18.000229 0 0 1 25 42.996094 A 18.000229 18.000229 0 0 1 7 24.996094 A 18.000229 18.000229 0 0 1 25 6.9960938 z M 21.496094 12 A 0.5 0.5 0 0 0 20.996094 12.5 L 20.996094 25.701172 A 0.5 0.5 0 0 1 20.496094 26.201172 L 11.998047 26.201172 A 0.18766001 0.18766001 0 0 0 11.875 26.529297 L 24.623047 37.669922 A 0.57222682 0.57222682 0 0 0 25.376953 37.669922 L 38.123047 26.529297 A 0.18768046 0.18768046 0 0 0 38 26.201172 L 29.5 26.201172 A 0.5 0.5 0 0 1 29 25.701172 L 29 12.5 A 0.5 0.5 0 0 0 28.5 12 L 21.496094 12 z'/></svg>">
        <link href="../css/style.css" rel="stylesheet" type="text/css">
        <meta charset="UTF-8" />
        <meta name="google" content="notranslate" />  
        <meta http-equiv="Content-Language" content="en_US" />
        <style>
            body {
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            .header-bar {
                display: flex;
                justify-content: space-between;
                align-items: flex-start;
                width: 100%;
                max-width: 1050px;
                box-sizing: border-box;
            }
            .page-header {
                display: flex;
                align-items: center;
                gap: 20px;
                margin-bottom: 20px;
            }
            .page-header svg {
                width: 64px;
                height: 64px;
                flex-shrink: 0;
            }
            .page-title {
                margin: 0;
                font-size: 2.25rem;
                font-weight: 700;
            }
            .content-wrapper {
                width: 100%;
                max-width: 1050px;
                box-sizing: border-box;
            }
            .intro-text {
                color: var(--text-muted);
                margin-top: 0;
                margin-bottom: 30px;
            }
            .form-section label {
                display: block;
                font-weight: 600;
                margin-bottom: 8px;
                color: var(--text-main);
            }
            .textarea-field {
                width: 100%;
                height: 120px;
                font-family: monospace;
                padding: 10px;
                border: 1px solid var(--border-color);
                border-radius: 4px;
                background-color: var(--bg-panel);
                color: var(--text-main);
                box-sizing: border-box;
                resize: vertical;
            }
            .inline-fields {
                display: flex;
                flex-wrap: wrap;
                align-items: center;
                gap: 15px;
                margin: 20px 0;
            }
            .inline-group {
                display: flex;
                align-items: center;
                gap: 8px;
            }
            .action-row {
                display: flex;
                gap: 10px;
                margin-bottom: 30px;
            }
            .section-title {
                margin: 35px 0 10px 0;
                font-size: 1.25rem;
                font-weight: 600;
                color: var(--text-main);
            }
            [data-theme="dark"] .section-title {
                color: #fff;
            }
            .value-panel {
                background-color: var(--bg-panel);
                border: 1px solid var(--border-color);
                border-radius: 8px;
                padding: 20px;
                box-shadow: 0 4px 6px rgba(0,0,0,0.2);
                box-sizing: border-box;
            }
            .value-panel pre {
                margin: 0;
                font-family: monospace;
                font-size: 14px;
                line-height: 1.5;
                overflow-x: auto;
                white-space: pre-wrap;
            }
            .footer-links {
                margin-top: 40px;
                font-size: 14px;
            }
            .footer-links a {
                color: var(--accent);
                text-decoration: none;
            }
            .footer-links a:hover {
                text-decoration: underline;
            }

            @media (max-width: 768px) {
                .header-bar {
                    flex-direction: column;
                    gap: 15px;
                }
                .inline-fields {
                    flex-direction: column;
                    align-items: stretch;
                }
                .inline-group {
                    flex-direction: column;
                    align-items: flex-start;
                }
                .inline-group .input-field {
                    width: 100% !important;
                }
            }
        </style>
    </head>
    <body>
        <div class="header-bar">
            <div class="page-header">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="100%" height="100%">
                    <path fill="var(--accent)" fill-rule="evenodd" d="M 25 0 A 24.999868 25.000025 0 0 0 0 25 A 24.999868 25.000025 0 0 0 25 50 A 24.999868 25.000025 0 0 0 50 25 A 24.999868 25.000025 0 0 0 25 0 z M 25 6.9960938 A 18.000229 18.000229 0 0 1 43 24.996094 A 18.000229 18.000229 0 0 1 25 42.996094 A 18.000229 18.000229 0 0 1 7 24.996094 A 18.000229 18.000229 0 0 1 25 6.9960938 z M 21.496094 12 A 0.5 0.5 0 0 0 20.996094 12.5 L 20.996094 25.701172 A 0.5 0.5 0 1 1 20.496094 26.201172 L 11.998047 26.201172 A 0.18766001 0.18766001 0 0 0 11.875 26.529297 L 24.623047 37.669922 A 0.57222682 0.57222682 0 0 0 25.376953 37.669922 L 38.123047 26.529297 A 0.18768046 0.18768046 0 0 0 38 26.201172 L 29.5 26.201172 A 0.5 0.5 0 1 1 29 25.701172 L 29 12.5 A 0.5 0.5 0 0 0 28.5 12 L 21.496094 12 z" />
                </svg>
                <h1 class="page-title">SSL Expiration Monitor</h1>
            </div>
            <div style="margin-top: 15px;">
                <button class="button" id="theme-toggle">Toggle Theme</button>
            </div>
        </div>

        <div class="content-wrapper">
            <p class="intro-text">Analyze remote SSL certificate lifecycle data across multiple nodes synchronously.</p>

            <form method="POST" action=".">
                <div class="form-section">
                    <label>Target Host Configurations:</label>
                    <textarea name="targets_input" class="textarea-field" placeholder="google.com, yahoo.com:8443, example.com"><?php echo htmlspecialchars($targets_input); ?></textarea>
                </div>

                <div class="inline-fields">
                    <span style="font-weight: 600; color: var(--text-main);">Connection Adjustments:</span>
                    <div class="inline-group">
                        <span style="font-size: 14px; color: var(--text-muted);">Timeout (s):</span>
                        <input type="text" class="input-field" name="timeout" value="<?php echo htmlspecialchars($timeout_val); ?>" placeholder="4.0" style="width: 80px;"> 
                    </div>
                </div>

                <div class="action-row">
                    <input type="submit" name="validate" value="Scan Targets" class="button">
                    <input type="button" name="reset" value="Reset" class="button" onclick="window.location.href='.';">
                </div>
            </form>

            <?php if ($output): ?>
                <h3 class="section-title">Execution Response</h3>
                
                <?php 
                // Attempt to parse out structured data for clean table injection
                $json_data = json_decode($output, true);
                if (json_last_error() === JSON_ERROR_NONE && is_array($json_data)): 
                ?>
                    <div class="results-panel">
                        <table class="otp-table">
                            <thead>
                                <tr>
                                    <th>Target Endpoint</th>
                                    <th>Status</th>
                                    <th>Expiration Date</th>
                                    <th>Days Remaining</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($json_data as $target => $info): ?>
                                    <?php 
                                    $row_class = "";
                                    $status_badge = "";
                                    $exp_date = "—";
                                    $days_left = "—";

                                    if ($info['status'] === 'success') {
                                        if ($info['expired']) {
                                            $row_class = "row-listed";
                                            $status_badge = '<span class="badge-status bg-listed">Expired</span>';
                                        } else {
                                            $row_class = "row-clean";
                                            $status_badge = '<span class="badge-status bg-clean">Active</span>';
                                        }
                                        $exp_date = htmlspecialchars($info['expiration_date']);
                                        $days_left = htmlspecialchars($info['days_remaining']);
                                    } else {
                                        $row_class = "row-listed";
                                        $status_badge = '<span class="badge-status bg-listed">Error</span>';
                                        $exp_date = '<span style="color: var(--danger-text)">' . htmlspecialchars($info['message']) . '</span>';
                                    }
                                    ?>
                                    <tr class="<?php echo $row_class; ?>">
                                        <td class="code-cell"><?php echo htmlspecialchars($target); ?></td>
                                        <td><?php echo $status_badge; ?></td>
                                        <td class="code-cell"><?php echo $exp_date; ?></td>
                                        <td><strong><?php echo $days_left; ?></strong></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <!-- Fallback to raw container block if shell output throws a direct error -->
                    <div class="redboxed"><?php echo htmlspecialchars($output); ?></div>
                <?php endif; ?>
            <?php endif; ?>            
            
            <p class="footer-links">
                <a href="index.phps">View Source</a> | <a href="../">Back to Tools</a>
            </p>
        </div>

        <script>
            (function () {
                function setCookie(name, value, days = 365) {
                    const d = new Date();
                    d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
                    document.cookie = name + "=" + value + ";path=/;expires=" + d.toUTCString() + ";SameSite=Strict";
                }

                function getCookie(name) {
                    const nameEQ = name + "=";
                    const ca = document.cookie.split(';');
                    for(let i = 0; i < ca.length; i++) {
                        let c = ca[i].trim();
                        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
                    }
                    return null;
                }

                let savedTheme = getCookie("theme");
                if (!savedTheme) {
                    savedTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
                }

                document.documentElement.setAttribute("data-theme", savedTheme);

                document.addEventListener("DOMContentLoaded", function() {
                    const toggleBtn = document.getElementById("theme-toggle");
                    if (toggleBtn) {
                        toggleBtn.addEventListener("click", function() {
                            const currentTheme = document.documentElement.getAttribute("data-theme");
                            const newTheme = currentTheme === "dark" ? "light" : "dark";

                            document.documentElement.setAttribute("data-theme", newTheme);
                            setCookie("theme", newTheme);
                        });
                    }
                });
            })();
        </script>
    </body>
</html>