<?php

/**
 * UltyScan Web Interface - Scan Status
 * Returns current scan status as JSON
 */

header('Content-Type: application/json');

// Check if any sniper process is running
$output = shell_exec('pgrep -f "sniper" 2>/dev/null');
$running = !empty(trim($output));

// Get list of running scans
$processes = [];
if ($running) {
    $psOutput = shell_exec('ps aux | grep "sniper" | grep -v grep 2>/dev/null');
    if (!empty($psOutput)) {
        $lines = explode("\n", trim($psOutput));
        foreach ($lines as $line) {
            if (!empty($line)) {
                $processes[] = $line;
            }
        }
    }
}

// Get recent log files
$logs = [];
$logDir = '/var/log/ultyscan';
if (is_dir($logDir)) {
    $files = glob($logDir . '/scan_*.log');
    usort($files, function ($a, $b) {
        return filemtime($b) - filemtime($a);
    });
    $logs = array_slice($files, 0, 5); // Last 5 logs
}

echo json_encode([
    'running' => $running,
    'processCount' => count($processes),
    'processes' => $processes,
    'recentLogs' => $logs,
    'timestamp' => date('Y-m-d H:i:s')
]);
