<?php
/**
 * System-Information - ANGEPASST FÜR NEUES LAYOUT
 */

require_once 'config.php';
requireBackendAccess();

$pageTitle = 'System-Information';

// System-Statistiken laden
$stats = getSystemStats();

// PHP Extensions prüfen
$extensions = ['pdo', 'pdo_mysql', 'gd', 'fileinfo', 'mbstring', 'zip', 'json', 'curl'];
$extStatus = [];
foreach ($extensions as $ext) {
    $extStatus[$ext] = extension_loaded($ext);
}

// Verzeichnis-Größen
$uploadDir = __DIR__ . '/../upload/';
$docsDir = __DIR__ . '/../documents/';
$uploadSize = 0;
$docsSize = 0;
$uploadCount = 0;
$docsCount = 0;

if (is_dir($uploadDir)) {
    $files = glob($uploadDir . '*');
    if (is_array($files)) {
        foreach ($files as $file) {
            if (is_file($file)) {
                $uploadSize += filesize($file);
                $uploadCount++;
            }
        }
    }
}

if (is_dir($docsDir)) {
    $files = glob($docsDir . '*');
    if (is_array($files)) {
        foreach ($files as $file) {
            if (is_file($file)) {
                $docsSize += filesize($file);
                $docsCount++;
            }
        }
    }
}

// Layout laden
include 'layout/header_next_page.php';
?>

<!-- Main Content -->
<main class="backend-main">
    
    <!-- PHP Information -->
    <div class="activity-timeline" style="margin-bottom: 30px;">
        <h2>🐘 PHP-Information</h2>
        
        <div class="dashboard-grid" style="margin-top: 20px;">
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-accent);">
                            <?php echo $stats['php_version']; ?>
                        </div>
                        <div class="widget-label">PHP Version</div>
                    </div>
                    <div class="widget-icon">🐘</div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-warning);">
                            <?php echo ini_get('memory_limit'); ?>
                        </div>
                        <div class="widget-label">Memory Limit</div>
                    </div>
                    <div class="widget-icon"><i class="ti ti-device-floppy"></i></div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-accent);">
                            <?php echo ini_get('upload_max_filesize'); ?>
                        </div>
                        <div class="widget-label">Max Upload</div>
                    </div>
                    <div class="widget-icon">📤</div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-danger);">
                            <?php echo ini_get('max_execution_time'); ?>s
                        </div>
                        <div class="widget-label">Max Execution</div>
                    </div>
                    <div class="widget-icon">⏱️</div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- PHP Extensions -->
    <div class="activity-timeline" style="margin-bottom: 30px;">
        <h2>🔌 PHP Extensions</h2>
        
        <table class="backend-table">
            <thead>
                <tr>
                    <th>Extension</th>
                    <th style="width: 150px;">Status</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($extStatus as $ext => $loaded): ?>
                    <tr>
                        <td>
                            <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px; font-family: 'Courier New', monospace;">
                                <?php echo $ext; ?>
                            </code>
                        </td>
                        <td>
                            <?php if ($loaded): ?>
                                <span class="badge badge-success"><i class="ti ti-circle-check" style="color:var(--vs-success);"></i> Geladen</span>
                            <?php else: ?>
                                <span class="badge badge-danger"><i class="ti ti-circle-x" style="color:var(--vs-danger);"></i> Nicht gefunden</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    
    <!-- Datenbank-Information -->
    <div class="activity-timeline" style="margin-bottom: 30px;">
        <h2>🗄️ Datenbank-Information</h2>
        
        <div class="dashboard-grid" style="margin-top: 20px;">
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 24px; color: var(--vs-accent);">
                            <?php echo explode('-', $stats['mysql_version'])[0]; ?>
                        </div>
                        <div class="widget-label">🐬 MySQL Version</div>
                    </div>
                    <div class="widget-icon">🐬</div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 24px; color: var(--vs-success);">
                            <?php echo formatBytes($stats['db_size']); ?>
                        </div>
                        <div class="widget-label">💽 DB-Größe</div>
                    </div>
                    <div class="widget-icon">💽</div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 18px; color: var(--vs-text-muted);">
                            <?php echo DB_NAME; ?>
                        </div>
                        <div class="widget-label"><i class="ti ti-chart-bar"></i> DB-Name</div>
                    </div>
                    <div class="widget-icon"><i class="ti ti-chart-bar"></i></div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 18px; color: var(--vs-accent);">
                            <?php echo DB_HOST; ?>
                        </div>
                        <div class="widget-label"><i class="ti ti-world"></i> DB-Host</div>
                    </div>
                    <div class="widget-icon"><i class="ti ti-world"></i></div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Speicher-Nutzung -->
    <div class="activity-timeline" style="margin-bottom: 30px;">
        <h2><i class="ti ti-device-floppy"></i> Speicher-Nutzung</h2>
        
        <div class="dashboard-grid" style="margin-top: 20px;">
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-success);">
                            <?php echo formatBytes($uploadSize); ?>
                        </div>
                        <div class="widget-label"><i class="ti ti-photo"></i> Upload-Verzeichnis</div>
                    </div>
                    <div class="widget-icon"><i class="ti ti-photo"></i></div>
                </div>
                <div class="widget-footer">
                    <div style="font-size: 12px; color: var(--vs-text-muted);">
                        <?php echo number_format($uploadCount, 0, ',', '.'); ?> Dateien
                    </div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-accent);">
                            <?php echo formatBytes($docsSize); ?>
                        </div>
                        <div class="widget-label">📄 Dokumente</div>
                    </div>
                    <div class="widget-icon">📄</div>
                </div>
                <div class="widget-footer">
                    <div style="font-size: 12px; color: var(--vs-text-muted);">
                        <?php echo number_format($docsCount, 0, ',', '.'); ?> Dateien
                    </div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-warning);">
                            <?php echo formatBytes($uploadSize + $docsSize + $stats['db_size']); ?>
                        </div>
                        <div class="widget-label">💿 Gesamt</div>
                    </div>
                    <div class="widget-icon">💿</div>
                </div>
                <div class="widget-footer">
                    <div style="font-size: 12px; color: var(--vs-text-muted);">
                        Uploads + Dokumente + Datenbank
                    </div>
                </div>
            </div>
            
            <div class="widget-card">
                <div class="widget-header">
                    <div>
                        <div class="widget-value" style="font-size: 28px; color: var(--vs-text-muted);">
                            <?php echo number_format($uploadCount + $docsCount, 0, ',', '.'); ?>
                        </div>
                        <div class="widget-label"><i class="ti ti-folder"></i> Dateien</div>
                    </div>
                    <div class="widget-icon"><i class="ti ti-folder"></i></div>
                </div>
                <div class="widget-footer">
                    <div style="font-size: 12px; color: var(--vs-text-muted);">
                        Gesamt im System
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Server-Information -->
    <div class="activity-timeline">
        <h2><i class="ti ti-device-desktop"></i> Server-Information</h2>
        
        <table class="backend-table">
            <tbody>
                <tr>
                    <td style="width: 250px;"><strong>Server Software</strong></td>
                    <td>
                        <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px;">
                            <?php echo $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown'; ?>
                        </code>
                    </td>
                </tr>
                <tr>
                    <td><strong>Server Name</strong></td>
                    <td>
                        <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px;">
                            <?php echo $_SERVER['SERVER_NAME'] ?? 'Unknown'; ?>
                        </code>
                    </td>
                </tr>
                <tr>
                    <td><strong>Server Adresse</strong></td>
                    <td>
                        <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px;">
                            <?php echo $_SERVER['SERVER_ADDR'] ?? 'Unknown'; ?>
                        </code>
                    </td>
                </tr>
                <tr>
                    <td><strong>Server Port</strong></td>
                    <td>
                        <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px;">
                            <?php echo $_SERVER['SERVER_PORT'] ?? 'Unknown'; ?>
                        </code>
                    </td>
                </tr>
                <tr>
                    <td><strong>Server Zeit</strong></td>
                    <td>
                        <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px;">
                            <?php echo date('d.m.Y H:i:s'); ?>
                        </code>
                    </td>
                </tr>
                <tr>
                    <td><strong>Zeitzone</strong></td>
                    <td>
                        <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px;">
                            <?php echo date_default_timezone_get(); ?>
                        </code>
                    </td>
                </tr>
                <tr>
                    <td><strong>Document Root</strong></td>
                    <td>
                        <code style="background: var(--vs-surface-2); padding: 4px 8px; border-radius: 4px; font-size: 12px;">
                            <?php echo $_SERVER['DOCUMENT_ROOT'] ?? 'Unknown'; ?>
                        </code>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    
<!-- Security Headers Status -->
<div class="activity-timeline" style="margin-bottom: 30px;">
    <h2><i class="ti ti-lock"></i> Security Headers Status</h2>
    
    <table class="backend-table" style="margin-left: 0;">
        <tbody>
            <?php
            // Security Headers testen
            $securityHeaders = SecurityHeaders::getSetHeaders();
            
            $expectedHeaders = [
                'Content-Security-Policy' => 'XSS Protection',
                'X-Frame-Options' => 'Clickjacking Protection',
                'X-Content-Type-Options' => 'MIME-Sniffing Protection',
                'X-XSS-Protection' => 'Legacy XSS Filter',
                'Referrer-Policy' => 'Privacy Protection',
                'Permissions-Policy' => 'Feature Control'
            ];
            
            // HSTS nur bei HTTPS erwarten
            $isHTTPS = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || 
                       $_SERVER['SERVER_PORT'] == 443 ||
                       (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
            
            if ($isHTTPS) {
                $expectedHeaders['Strict-Transport-Security'] = 'HTTPS Enforcement';
            }
            
            $setHeaderNames = array_map(function($h) {
                return trim(explode(':', $h, 2)[0]);
            }, $securityHeaders);
            
            $allOK = true;
            foreach ($expectedHeaders as $name => $description) {
                $isSet = in_array($name, $setHeaderNames);
                if (!$isSet) $allOK = false;
                
                $icon = $isSet ? '<i class="ti ti-circle-check" style="color:var(--vs-success);"></i>' : '<i class="ti ti-circle-x" style="color:var(--vs-danger);"></i>';
                $badge = $isSet ? 
                    '<span class="badge badge-success"><i class="ti ti-circle-check" style="color:var(--vs-success);"></i> Aktiv</span>' : 
                    '<span class="badge badge-danger"><i class="ti ti-circle-x" style="color:var(--vs-danger);"></i> Fehlt</span>';
                
                echo '<tr>';
                echo '<td style="width: 250px;"><strong>' . htmlspecialchars($name) . '</strong><br><small style="color: #666;">' . htmlspecialchars($description) . '</small></td>';
                echo '<td>' . $badge . '</td>';
                echo '</tr>';
            }
            ?>
        </tbody>
    </table>
    
    <div style="margin-top: 15px; padding: 10px; background: <?php echo $allOK ? '#d4edda' : '#fff3cd'; ?>; border-radius: 4px;">
        <?php if ($allOK): ?>
            <strong style="color: var(--vs-success-text);">✓ Alle Security Headers aktiv!</strong>
        <?php else: ?>
            <strong style="color: var(--vs-warning-text);"><i class="ti ti-alert-triangle" style="color:var(--vs-warning);"></i> Einige Security Headers fehlen</strong>
            <br><small>Prüfe die SecurityHeaders.php Konfiguration</small>
        <?php endif; ?>
    </div>
    
    <div style="margin-top: 10px;">
    </div>
</div>

<!-- Security Certificate -->
<div class="activity-timeline" style="margin-bottom: 30px; padding: 25px; background: var(--vs-surface); border-radius: 12px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
    <h2>🏆 Security Certification</h2>
    
    <div style="background: var(--vs-accent); padding: 25px; border-radius: 8px; border: 2px solid var(--vs-success);">
        <div style="text-align: center; margin-bottom: 20px;">
            <div style="font-size: 48px; margin-bottom: 10px;"><i class="ti ti-shield"></i></div>
            <h3 style="color: var(--vs-success); margin: 0 0 5px 0;">SECURITY CERTIFIED</h3>
            <div style="font-size: 24px; color: var(--vs-warning);"><i class="ti ti-star"></i><i class="ti ti-star"></i><i class="ti ti-star"></i><i class="ti ti-star"></i><i class="ti ti-star"></i></div>
            <p style="color: #666; margin: 10px 0 0 0;">5/5 Stars - Gold Standard</p>
        </div>
        
        <table class="backend-table" style="margin-bottom: 15px;">
            <tbody>
                <tr>
                    <td style="width: 200px;"><strong>Projekt Version</strong></td>
                    <td><strong>4.0</strong></td>
                </tr>
                <tr>
                    <td><strong>Audit Datum</strong></td>
                    <td>6. Juni 2026</td>
                </tr>
                <tr>
                    <td><strong>Gültig bis</strong></td>
                    <td>6. Juni 2027</td>
                </tr>
                <tr>
                    <td><strong>Status</strong></td>
                    <td>
                        <span class="badge badge-success"><i class="ti ti-circle-check" style="color:var(--vs-success);"></i> PRODUCTION READY</span>
                    </td>
                </tr>
            </tbody>
        </table>
        
        <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 15px;">
            <div style="background: var(--vs-surface); padding: 12px; border-radius: 6px; text-align: center; border: 1px solid var(--vs-success);">
                <div style="font-size: 20px; font-weight: 600; color: var(--vs-success);">0</div>
                <div style="font-size: 11px; color: #666;">Kritische Fehler</div>
            </div>
            <div style="background: var(--vs-surface); padding: 12px; border-radius: 6px; text-align: center; border: 1px solid var(--vs-success);">
                <div style="font-size: 20px; font-weight: 600; color: var(--vs-success);">10/10</div>
                <div style="font-size: 11px; color: #666;">OWASP Compliance</div>
            </div>
            <div style="background: var(--vs-surface); padding: 12px; border-radius: 6px; text-align: center; border: 1px solid var(--vs-warning);">
                <div style="font-size: 20px; font-weight: 600; color: var(--vs-warning);">5/5</div>
                <div style="font-size: 11px; color: #666;">Audit Bereiche</div>
            </div>
            <div style="background: var(--vs-surface); padding: 12px; border-radius: 6px; text-align: center; border: 1px solid var(--vs-accent);">
                <div style="font-size: 20px; font-weight: 600; color: var(--vs-accent);"><i class="ti ti-star"></i><i class="ti ti-star"></i><i class="ti ti-star"></i><i class="ti ti-star"></i><i class="ti ti-star"></i></div>
                <div style="font-size: 11px; color: #666;">Gesamtbewertung</div>
            </div>
        </div>
        
        <div style="margin-top: 15px;">
            <a href="../../SECURITY_CERTIFICATE.md" 
               target="_blank"
               style="display: inline-block; width: 100%; text-align: center; padding: 12px; background: var(--vs-success); color: var(--vs-surface); text-decoration: none; border-radius: 6px; font-weight: 600;">
                📄 Vollständiges Zertifikat anzeigen
            </a>
        </div>
    </div>
</div>

</main>

<?php include 'layout/footer_next_page.php'; ?>
