diff --git a/panel/api/endpoints/firewall.php b/panel/api/endpoints/firewall.php index a6fb914..5249233 100644 --- a/panel/api/endpoints/firewall.php +++ b/panel/api/endpoints/firewall.php @@ -100,10 +100,17 @@ function ufw_status(): array { // Default policy preg_match('/Default:\s+(\w+) \(incoming\),\s+(\w+) \(outgoing\)/', $raw, $pol); + // Logging level + preg_match('/Logging:\s+(\S+)/i', $raw, $logm); + $logging = strtolower($logm[1] ?? 'off'); + if ($logging === 'on (low)') $logging = 'low'; + // Normalise: strip parentheses if present e.g. "on (low)" → "low" + if (preg_match('/\((\w+)\)/', $logging, $lm)) $logging = $lm[1]; return [ 'active' => $active, 'default_incoming' => $pol[1] ?? 'deny', 'default_outgoing' => $pol[2] ?? 'allow', + 'logging' => $logging, 'rules' => $rules, 'raw' => $raw, ]; diff --git a/panel/api/endpoints/php.php b/panel/api/endpoints/php.php index 21af62e..1fefeb4 100644 --- a/panel/api/endpoints/php.php +++ b/panel/api/endpoints/php.php @@ -83,16 +83,40 @@ match ($action) { $ver = $body['version'] ?? ''; $ext = preg_replace('/[^a-z0-9\-]/', '', strtolower($body['extension'] ?? '')); if (!preg_match('/^[78]\.\d$/', $ver) || !$ext) Response::error("Invalid input"); + + header('Content-Type: text/event-stream'); + header('Cache-Control: no-cache'); + header('X-Accel-Buffering: no'); + while (ob_get_level()) ob_end_clean(); + $sse = function(string $line) { echo 'data: ' . json_encode(['line' => $line]) . "\n\n"; flush(); }; + $run = function(string $cmd) use ($sse): int { + $proc = proc_open($cmd, [1 => ['pipe','w'], 2 => ['pipe','w']], $pipes); + if (!$proc) { $sse(" [failed to start]\n"); return 1; } + while (!feof($pipes[1])) { $l = fgets($pipes[1]); if ($l !== false && $l !== '') $sse($l); } + while (!feof($pipes[2])) { $l = fgets($pipes[2]); if ($l !== false && $l !== '') $sse(" " . $l); } + fclose($pipes[1]); fclose($pipes[2]); + return proc_close($proc); + }; + $pkg = "php{$ver}-{$ext}"; - $out = shell_exec("apt-get install -y $pkg 2>&1"); - if (str_contains($out ?: '', 'Unable to locate') || str_contains($out ?: '', 'E:')) { - // Try pecl fallback - $out2 = shell_exec("php{$ver} /usr/bin/pecl install {$ext} 2>&1"); - $out .= "\n[pecl] " . $out2; + $sse("▶ Installing {$pkg}…\n"); + $rc = $run("sudo apt-get install -y $pkg 2>&1"); + + if ($rc !== 0) { + // Try PECL fallback + $sse("\n apt package not found — trying PECL…\n"); + $rc2 = $run("sudo php{$ver} /usr/bin/pecl install {$ext} 2>&1"); + if ($rc2 !== 0) { + $sse(" ✗ Could not install {$ext} via apt or PECL\n"); + echo 'data: ' . json_encode(['done' => true, 'success' => false]) . "\n\n"; flush(); exit; + } } - shell_exec("systemctl reload php{$ver}-fpm 2>/dev/null || true"); + + $sse("\n▶ Reloading PHP {$ver} FPM…\n"); + $run("sudo systemctl reload php{$ver}-fpm 2>&1 || sudo systemctl restart php{$ver}-fpm 2>&1"); + $sse(" ✓ php{$ver}-{$ext} installed\n"); audit('php.install-extension', "$ver/$ext"); - Response::success(['output' => substr($out ?: '', -1000)], "Extension $ext installed for PHP $ver"); + echo 'data: ' . json_encode(['done' => true, 'success' => true]) . "\n\n"; flush(); exit; })(), 'remove-extension' => (function() use ($body) { @@ -100,11 +124,29 @@ match ($action) { $ver = $body['version'] ?? ''; $ext = preg_replace('/[^a-z0-9\-]/', '', strtolower($body['extension'] ?? '')); if (!preg_match('/^[78]\.\d$/', $ver) || !$ext) Response::error("Invalid input"); + + header('Content-Type: text/event-stream'); + header('Cache-Control: no-cache'); + header('X-Accel-Buffering: no'); + while (ob_get_level()) ob_end_clean(); + $sse = function(string $line) { echo 'data: ' . json_encode(['line' => $line]) . "\n\n"; flush(); }; + $run = function(string $cmd) use ($sse): int { + $proc = proc_open($cmd, [1 => ['pipe','w'], 2 => ['pipe','w']], $pipes); + if (!$proc) { $sse(" [failed to start]\n"); return 1; } + while (!feof($pipes[1])) { $l = fgets($pipes[1]); if ($l !== false && $l !== '') $sse($l); } + while (!feof($pipes[2])) { $l = fgets($pipes[2]); if ($l !== false && $l !== '') $sse(" " . $l); } + fclose($pipes[1]); fclose($pipes[2]); + return proc_close($proc); + }; + $pkg = "php{$ver}-{$ext}"; - $out = shell_exec("apt-get remove -y $pkg 2>&1"); - shell_exec("systemctl reload php{$ver}-fpm 2>/dev/null || true"); + $sse("▶ Removing {$pkg}…\n"); + $run("sudo apt-get remove -y $pkg 2>&1"); + $sse("\n▶ Reloading PHP {$ver} FPM…\n"); + $run("sudo systemctl reload php{$ver}-fpm 2>&1 || sudo systemctl restart php{$ver}-fpm 2>&1"); + $sse(" ✓ php{$ver}-{$ext} removed\n"); audit('php.remove-extension', "$ver/$ext"); - Response::success(['output' => substr($out ?: '', -1000)], "Extension $ext removed from PHP $ver"); + echo 'data: ' . json_encode(['done' => true, 'success' => true]) . "\n\n"; flush(); exit; })(), 'fpm-action' => (function() use ($body) { diff --git a/panel/api/endpoints/ssl.php b/panel/api/endpoints/ssl.php index 5a115bc..97ee24f 100644 --- a/panel/api/endpoints/ssl.php +++ b/panel/api/endpoints/ssl.php @@ -21,14 +21,55 @@ match ($action) { Response::success($rows); })(), - 'issue' => (function() use ($body, $accountId) { + 'issue' => (function() use ($body, $accountId, $db) { $domain = trim($body['domain'] ?? ''); $email = trim($body['email'] ?? ''); if (!$domain) Response::error("domain required"); if (!$accountId) Response::error("account_id required"); - $result = SSLManager::issueLetsEncrypt($accountId, $domain, $email); - audit('ssl.issue', $domain); - Response::success($result, "SSL certificate issued for $domain"); + + header('Content-Type: text/event-stream'); + header('Cache-Control: no-cache'); + header('X-Accel-Buffering: no'); + while (ob_get_level()) ob_end_clean(); + $sse = function(string $line) { echo 'data: ' . json_encode(['line' => $line]) . "\n\n"; flush(); }; + + $sse("▶ Requesting Let's Encrypt certificate for {$domain}…\n"); + $sse(" (This may take 30–60 seconds)\n\n"); + + try { + $webRoot = $db->fetchOne("SELECT document_root FROM domains WHERE domain = ? AND account_id = ?", [$domain, $accountId]); + if (!$webRoot) { $sse(" ✗ Domain not found for this account\n"); echo 'data: '.json_encode(['done'=>true,'success'=>false])."\n\n"; flush(); exit; } + + $docRoot = $webRoot['document_root']; + $mail = $email ?: "ssl@{$domain}"; + $cmd = "certbot certonly --webroot -w " . escapeshellarg($docRoot) + . " -d " . escapeshellarg($domain) . " -d " . escapeshellarg("www.{$domain}") + . " --email " . escapeshellarg($mail) + . " --agree-tos --non-interactive 2>&1"; + + $proc = proc_open($cmd, [1 => ['pipe','w'], 2 => ['pipe','w']], $pipes); + if ($proc) { + while (!feof($pipes[1])) { $l = fgets($pipes[1]); if ($l !== false && $l !== '') $sse($l); } + fclose($pipes[1]); fclose($pipes[2]); + proc_close($proc); + } + + $certPath = "/etc/letsencrypt/live/{$domain}/fullchain.pem"; + if (!file_exists($certPath)) { + $sse("\n ✗ Certificate not created — check DNS is pointed to this server\n"); + echo 'data: '.json_encode(['done'=>true,'success'=>false])."\n\n"; flush(); exit; + } + + $sse("\n▶ Installing certificate on vhost…\n"); + $result = SSLManager::issueLetsEncrypt($accountId, $domain, $email); + audit('ssl.issue', $domain); + $sse(" ✓ SSL certificate installed (expires {$result['expires']})\n"); + echo 'data: '.json_encode(['done'=>true,'success'=>true,'cert_id'=>$result['cert_id']])."\n\n"; + } catch (Exception $e) { + $sse(" ✗ Error: " . $e->getMessage() . "\n"); + echo 'data: '.json_encode(['done'=>true,'success'=>false])."\n\n"; + } + flush(); exit; })(), 'generate-csr' => (function() use ($body, $accountId) { diff --git a/panel/public/assets/js/admin.js b/panel/public/assets/js/admin.js index a493db8..a4ced3a 100644 --- a/panel/public/assets/js/admin.js +++ b/panel/public/assets/js/admin.js @@ -636,22 +636,65 @@ }); }; - window.phpExtInstall = async (ver) => { + const _phpExtStream = (ver, ext, action) => { + const termId = 'phpext-term-' + Date.now(); + const verb = action === 'install-extension' ? 'Installing' : 'Removing'; + Nova.modal(`${verb} ${ext} (PHP ${ver})`, ` +