mirror of
https://github.com/myronblair/novacpx
synced 2026-06-30 17:50:41 -05:00
feat: full PHP Manager — version install/remove, per-version extension management
php.php: install-version, remove-version, version-extensions, install-extension, remove-extension, fpm-action endpoints. versions now returns fpm_active status and panel_php (current runtime version). admin.js phpManager(): grid of installed/not-installed versions with Install/ Remove/Restart FPM buttons; Extensions panel slides in below with filterable list, dropdown of common extensions + custom input, per-extension Remove buttons. Panel PHP info card shows which version NovaCPX runs on. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -26,10 +26,96 @@ match ($action) {
|
||||
'versions' => (function() {
|
||||
$versions = [];
|
||||
foreach (['7.4','8.1','8.2','8.3'] as $v) {
|
||||
$installed = file_exists("/usr/bin/php{$v}");
|
||||
$versions[] = ['version' => $v, 'installed' => $installed, 'is_default' => $v === PHP_DEFAULT];
|
||||
$installed = file_exists("/usr/bin/php{$v}");
|
||||
$fpmActive = $installed ? trim(shell_exec("systemctl is-active php{$v}-fpm 2>/dev/null") ?: '') : '';
|
||||
$versions[] = [
|
||||
'version' => $v,
|
||||
'installed' => $installed,
|
||||
'fpm_active' => $fpmActive === 'active',
|
||||
'is_default' => $v === PHP_DEFAULT,
|
||||
];
|
||||
}
|
||||
Response::success($versions);
|
||||
// Detect which version the panel itself runs on (highest installed)
|
||||
$panelVer = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
|
||||
Response::success(['versions' => $versions, 'panel_php' => $panelVer]);
|
||||
})(),
|
||||
|
||||
'install-version' => (function() use ($body) {
|
||||
Auth::getInstance()->require('admin');
|
||||
$ver = $body['version'] ?? '';
|
||||
if (!preg_match('/^[78]\.\d$/', $ver)) Response::error("Invalid version");
|
||||
$pkgs = "php{$ver} php{$ver}-fpm php{$ver}-cli php{$ver}-common php{$ver}-mysql php{$ver}-curl php{$ver}-gd php{$ver}-xml php{$ver}-mbstring php{$ver}-zip php{$ver}-intl php{$ver}-bcmath php{$ver}-soap php{$ver}-opcache";
|
||||
$out = shell_exec("apt-get install -y $pkgs 2>&1");
|
||||
shell_exec("systemctl enable php{$ver}-fpm && systemctl start php{$ver}-fpm 2>/dev/null");
|
||||
audit('php.install-version', $ver);
|
||||
Response::success(['output' => substr($out ?: '', -1000)], "PHP $ver installed");
|
||||
})(),
|
||||
|
||||
'remove-version' => (function() use ($body) {
|
||||
Auth::getInstance()->require('admin');
|
||||
$ver = $body['version'] ?? '';
|
||||
if (!preg_match('/^[78]\.\d$/', $ver)) Response::error("Invalid version");
|
||||
if ($ver === PHP_DEFAULT) Response::error("Cannot remove the panel's default PHP version");
|
||||
shell_exec("systemctl stop php{$ver}-fpm 2>/dev/null || true");
|
||||
$out = shell_exec("apt-get remove -y php{$ver}* 2>&1");
|
||||
audit('php.remove-version', $ver);
|
||||
Response::success(['output' => substr($out ?: '', -1000)], "PHP $ver removed");
|
||||
})(),
|
||||
|
||||
'version-extensions' => (function() use ($body) {
|
||||
Auth::getInstance()->require('admin');
|
||||
$ver = $body['version'] ?? $_GET['version'] ?? '';
|
||||
if (!preg_match('/^[78]\.\d$/', $ver)) Response::error("Invalid version");
|
||||
if (!file_exists("/usr/bin/php{$ver}")) Response::error("PHP $ver not installed");
|
||||
$out = shell_exec("php{$ver} -m 2>/dev/null") ?: '';
|
||||
$exts = array_values(array_filter(explode("\n", trim($out)), fn($l) => $l && !str_starts_with($l, '[')));
|
||||
|
||||
// Available apt packages for this version (common ones)
|
||||
$common = ['bcmath','bz2','curl','gd','gmp','igbinary','imagick','imap','intl','ldap','mbstring',
|
||||
'memcached','mongodb','msgpack','mysql','odbc','opcache','pdo','pdo-mysql','pdo-pgsql',
|
||||
'pgsql','redis','soap','sqlite3','tidy','tokenizer','uuid','xml','xmlrpc','xsl','zip'];
|
||||
$available = array_map(fn($e) => "php{$ver}-{$e}", $common);
|
||||
Response::success(['version' => $ver, 'installed' => $exts, 'available' => $available]);
|
||||
})(),
|
||||
|
||||
'install-extension' => (function() use ($body) {
|
||||
Auth::getInstance()->require('admin');
|
||||
$ver = $body['version'] ?? '';
|
||||
$ext = preg_replace('/[^a-z0-9\-]/', '', strtolower($body['extension'] ?? ''));
|
||||
if (!preg_match('/^[78]\.\d$/', $ver) || !$ext) Response::error("Invalid input");
|
||||
$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;
|
||||
}
|
||||
shell_exec("systemctl reload php{$ver}-fpm 2>/dev/null || true");
|
||||
audit('php.install-extension', "$ver/$ext");
|
||||
Response::success(['output' => substr($out ?: '', -1000)], "Extension $ext installed for PHP $ver");
|
||||
})(),
|
||||
|
||||
'remove-extension' => (function() use ($body) {
|
||||
Auth::getInstance()->require('admin');
|
||||
$ver = $body['version'] ?? '';
|
||||
$ext = preg_replace('/[^a-z0-9\-]/', '', strtolower($body['extension'] ?? ''));
|
||||
if (!preg_match('/^[78]\.\d$/', $ver) || !$ext) Response::error("Invalid input");
|
||||
$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");
|
||||
audit('php.remove-extension', "$ver/$ext");
|
||||
Response::success(['output' => substr($out ?: '', -1000)], "Extension $ext removed from PHP $ver");
|
||||
})(),
|
||||
|
||||
'fpm-action' => (function() use ($body) {
|
||||
Auth::getInstance()->require('admin');
|
||||
$ver = $body['version'] ?? '';
|
||||
$cmd = $body['command'] ?? 'restart';
|
||||
if (!preg_match('/^[78]\.\d$/', $ver)) Response::error("Invalid version");
|
||||
if (!in_array($cmd, ['start','stop','restart','reload'])) Response::error("Invalid command");
|
||||
shell_exec("systemctl $cmd php{$ver}-fpm 2>/dev/null");
|
||||
audit('php.fpm-action', "$cmd php{$ver}-fpm");
|
||||
Response::success(null, "php{$ver}-fpm {$cmd}ed");
|
||||
})(),
|
||||
|
||||
'switch-version' => (function() use ($body, $accountId) {
|
||||
|
||||
+140
-12
@@ -457,28 +457,156 @@
|
||||
|
||||
// ── PHP Manager ────────────────────────────────────────────────────────────
|
||||
async function phpManager() {
|
||||
const res = await Nova.api('php', 'versions');
|
||||
const data = res?.data || {};
|
||||
const vers = data.versions || [];
|
||||
const panelPhp = data.panel_php || '—';
|
||||
|
||||
return `
|
||||
<div class="card">
|
||||
<div class="card-header"><span class="card-title">PHP Version Manager</span></div>
|
||||
<div class="page-header">
|
||||
<h2 class="page-title">PHP Manager</h2>
|
||||
<button class="btn btn-ghost btn-sm" onclick="adminPage('php-manager')">↻ Refresh</button>
|
||||
</div>
|
||||
|
||||
<div class="card mb-2">
|
||||
<div class="card-header"><span class="card-title">Panel PHP</span></div>
|
||||
<div class="card-body">
|
||||
<p class="text-muted text-sm">NovaCPX itself runs on <strong>PHP ${panelPhp}</strong> (always the highest installed version, updated automatically when a new version is installed).</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mb-2">
|
||||
<div class="card-header"><span class="card-title">Installed Versions</span></div>
|
||||
<div class="card-body">
|
||||
<p class="text-muted mb-2">Manage installed PHP versions and global extensions.</p>
|
||||
<div class="grid-4">
|
||||
${['7.4','8.1','8.2','8.3'].map(v => `
|
||||
${vers.map(v => `
|
||||
<div class="stat-card">
|
||||
<div class="stat-label">PHP ${v}</div>
|
||||
<div class="stat-value" style="font-size:1rem">${Nova.badge('Active','green')}</div>
|
||||
<div class="mt-2 flex gap-1">
|
||||
<button class="btn btn-ghost btn-sm" onclick="phpAction('${v}','fpm-restart')">Restart FPM</button>
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:.5rem">
|
||||
<strong>PHP ${v.version}</strong>
|
||||
${v.installed ? Nova.badge(v.fpm_active ? 'active' : 'stopped', v.fpm_active ? 'green' : 'yellow') : Nova.badge('not installed','muted')}
|
||||
</div>
|
||||
${v.is_default ? `<div class="text-xs text-muted mb-1">Panel default</div>` : ''}
|
||||
<div style="display:flex;flex-wrap:wrap;gap:.3rem;margin-bottom:.5rem">
|
||||
${v.installed ? `
|
||||
<button class="btn btn-xs" onclick="phpExtModal('${v.version}')">Extensions</button>
|
||||
<button class="btn btn-xs" onclick="phpFpmAction('${v.version}','restart')">Restart FPM</button>
|
||||
${!v.is_default ? `<button class="btn btn-xs btn-danger" onclick="phpRemoveVersion('${v.version}')">Remove</button>` : ''}
|
||||
` : `
|
||||
<button class="btn btn-xs btn-primary" onclick="phpInstallVersion('${v.version}')">Install</button>
|
||||
`}
|
||||
</div>
|
||||
</div>`).join('')}
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
<h4 class="mb-1">Global PHP Extensions</h4>
|
||||
<p class="text-muted text-sm">Extensions installed across all PHP versions: mbstring, curl, gd, xml, zip, opcache, redis, imagick, pdo, pdo_mysql, pdo_pgsql</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="php-ext-panel" style="display:none"></div>`;
|
||||
}
|
||||
|
||||
window.phpInstallVersion = (ver) => {
|
||||
Nova.confirm(`Install PHP ${ver}? This will run apt-get and may take a minute.`, async () => {
|
||||
Nova.toast(`Installing PHP ${ver}…`, 'info', 15000);
|
||||
const r = await Nova.api('php', 'install-version', { method: 'POST', body: { version: ver } });
|
||||
if (r?.success) { Nova.toast(`PHP ${ver} installed`, 'success'); adminPage('php-manager'); }
|
||||
else Nova.toast(r?.message || 'Install failed', 'error');
|
||||
});
|
||||
};
|
||||
|
||||
window.phpRemoveVersion = (ver) => {
|
||||
Nova.confirm(`Remove PHP ${ver}? All FPM pools for this version will stop.`, async () => {
|
||||
const r = await Nova.api('php', 'remove-version', { method: 'POST', body: { version: ver } });
|
||||
if (r?.success) { Nova.toast(`PHP ${ver} removed`, 'success'); adminPage('php-manager'); }
|
||||
else Nova.toast(r?.message || 'Remove failed', 'error');
|
||||
}, true);
|
||||
};
|
||||
|
||||
window.phpFpmAction = async (ver, cmd) => {
|
||||
const r = await Nova.api('php', 'fpm-action', { method: 'POST', body: { version: ver, command: cmd } });
|
||||
if (r?.success) { Nova.toast(r.message, 'success'); adminPage('php-manager'); }
|
||||
else Nova.toast(r?.message || 'Action failed', 'error');
|
||||
};
|
||||
|
||||
window.phpExtModal = async (ver) => {
|
||||
const panel = document.getElementById('php-ext-panel');
|
||||
if (!panel) return;
|
||||
panel.style.display = '';
|
||||
panel.innerHTML = `<div class="card"><div class="card-body"><p class="text-muted">Loading extensions for PHP ${ver}…</p></div></div>`;
|
||||
panel.scrollIntoView({ behavior: 'smooth' });
|
||||
|
||||
const r = await Nova.api('php', 'version-extensions', { params: { version: ver } });
|
||||
if (!r?.success) { panel.innerHTML = `<div class="card"><div class="card-body"><p class="text-muted">${r?.message || 'Failed to load'}</p></div></div>`; return; }
|
||||
|
||||
const installed = r.data.installed || [];
|
||||
const available = r.data.available || [];
|
||||
const notInstalled = available.filter(pkg => {
|
||||
const ext = pkg.replace(/^php[\d.]+-/, '');
|
||||
return !installed.some(i => i.toLowerCase() === ext.toLowerCase() || i.toLowerCase().replace('_','-') === ext.toLowerCase());
|
||||
});
|
||||
|
||||
panel.innerHTML = `
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<span class="card-title">PHP ${ver} Extensions</span>
|
||||
<div style="display:flex;gap:.5rem;align-items:center">
|
||||
<input id="php-ext-search" class="form-control" style="width:160px" placeholder="Filter…" oninput="phpExtFilter(this.value)">
|
||||
<button class="btn btn-ghost btn-sm" onclick="document.getElementById('php-ext-panel').style.display='none'">✕ Close</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div style="margin-bottom:1rem">
|
||||
<strong>Add extension</strong>
|
||||
<div style="display:flex;gap:.5rem;margin-top:.5rem;flex-wrap:wrap">
|
||||
<select id="php-ext-add-sel" class="form-control" style="width:220px">
|
||||
<option value="">— choose from common list —</option>
|
||||
${notInstalled.map(p => `<option value="${p.replace(/^php[\d.]+-/,'')}">${p.replace(/^php[\d.]+-/,'')}</option>`).join('')}
|
||||
</select>
|
||||
<span class="text-muted" style="align-self:center">or</span>
|
||||
<input id="php-ext-add-custom" class="form-control" style="width:140px" placeholder="custom name">
|
||||
<button class="btn btn-primary btn-sm" onclick="phpExtInstall('${ver}')">Install</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="php-ext-list">
|
||||
<table class="table">
|
||||
<thead><tr><th>Extension</th><th style="text-align:right">Action</th></tr></thead>
|
||||
<tbody>
|
||||
${installed.map(e => `
|
||||
<tr class="php-ext-row" data-ext="${e.toLowerCase()}">
|
||||
<td><code>${e}</code></td>
|
||||
<td style="text-align:right">
|
||||
<button class="btn btn-xs btn-danger" onclick="phpExtRemove('${ver}','${e}')">Remove</button>
|
||||
</td>
|
||||
</tr>`).join('')}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
};
|
||||
|
||||
window.phpExtFilter = (q) => {
|
||||
document.querySelectorAll('.php-ext-row').forEach(row => {
|
||||
row.style.display = row.dataset.ext.includes(q.toLowerCase()) ? '' : 'none';
|
||||
});
|
||||
};
|
||||
|
||||
window.phpExtInstall = async (ver) => {
|
||||
const sel = document.getElementById('php-ext-add-sel')?.value;
|
||||
const custom = document.getElementById('php-ext-add-custom')?.value?.trim();
|
||||
const ext = custom || sel;
|
||||
if (!ext) { Nova.toast('Choose or type an extension name', 'error'); return; }
|
||||
Nova.toast(`Installing ${ext} for PHP ${ver}…`, 'info', 15000);
|
||||
const r = await Nova.api('php', 'install-extension', { method: 'POST', body: { version: ver, extension: ext } });
|
||||
if (r?.success) { Nova.toast(r.message, 'success'); phpExtModal(ver); }
|
||||
else Nova.toast(r?.message || 'Install failed', 'error');
|
||||
};
|
||||
|
||||
window.phpExtRemove = (ver, ext) => {
|
||||
Nova.confirm(`Remove extension ${ext} from PHP ${ver}?`, async () => {
|
||||
const r = await Nova.api('php', 'remove-extension', { method: 'POST', body: { version: ver, extension: ext } });
|
||||
if (r?.success) { Nova.toast(r.message, 'success'); phpExtModal(ver); }
|
||||
else Nova.toast(r?.message || 'Remove failed', 'error');
|
||||
}, true);
|
||||
};
|
||||
|
||||
// ── Notifications (#25) ───────────────────────────────────────────────────
|
||||
async function notifications() {
|
||||
|
||||
Reference in New Issue
Block a user