Files
novacpx/panel/lib/Auth.php
T
myron 716d292e77 feat: dedicated ports per panel tier (8880/8881/8882)
Each panel now has its own dedicated port and is fully self-contained:
- Port 8880: User panel (end-user hosting dashboard)
- Port 8881: Reseller panel (account/package management)
- Port 8882: Admin panel (datacenter/server manager)

Changes:
- install.sh: PORT_USER/PORT_RESELLER/PORT_ADMIN constants; three separate
  nginx/Apache vhosts; UFW opens all three ports; Fail2Ban jail per port;
  credentials file shows all three URLs
- config.ini: stores port_user/port_reseller/port_admin
- Core.php: defines PORT_USER/RESELLER/ADMIN, detects CURRENT_PORTAL from
  SERVER_PORT so the API knows which tier is being accessed
- Auth.php: portalUrl() maps role → correct port for cross-portal redirects
- auth.php endpoint: returns portal_url on login so JS redirects to right port
- index.php login: uses portal_url from API response (no hardcoded paths)
- admin/index.php: inline login form (port 8882 is self-contained, no redirect)
- user/index.php: inline login form (port 8880 self-contained)
- reseller/index.php: new full reseller panel with inline login (port 8881);
  sidebar with accounts, packages, DNS, branding, bandwidth report sections

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-07 05:35:48 +00:00

116 lines
4.1 KiB
PHP

<?php
class Auth {
private static ?Auth $instance = null;
private ?array $user = null;
private function __construct() {}
public static function getInstance(): self {
if (!self::$instance) self::$instance = new self();
return self::$instance;
}
public function check(): bool {
// Bearer token (API)
$header = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
if (str_starts_with($header, 'Bearer ')) {
$token = substr($header, 7);
return $this->loginByToken($token);
}
// Session cookie
$sessionId = $_COOKIE['ncpx_session'] ?? '';
if ($sessionId) {
return $this->loginBySession($sessionId);
}
return false;
}
private function loginBySession(string $sessionId): bool {
$db = DB::getInstance();
$row = $db->fetchOne(
"SELECT s.*, u.id as uid, u.username, u.email, u.role, u.status, u.reseller_id, u.theme
FROM sessions s
JOIN users u ON u.id = s.user_id
WHERE s.id = ? AND s.expires_at > NOW() AND u.status = 'active'",
[hash('sha256', $sessionId)]
);
if (!$row) return false;
$this->user = $row;
return true;
}
private function loginByToken(string $token): bool {
$db = DB::getInstance();
$row = $db->fetchOne(
"SELECT t.permissions, u.id as uid, u.username, u.email, u.role, u.status
FROM api_tokens t
JOIN users u ON u.id = t.user_id
WHERE t.token = ? AND (t.expires_at IS NULL OR t.expires_at > NOW()) AND u.status = 'active'",
[hash('sha256', $token)]
);
if (!$row) return false;
$db->execute("UPDATE api_tokens SET last_used = NOW() WHERE token = ?", [hash('sha256', $token)]);
$this->user = $row;
return true;
}
public function attempt(string $username, string $password): ?string {
$db = DB::getInstance();
$user = $db->fetchOne(
"SELECT * FROM users WHERE (username = ? OR email = ?) AND status = 'active'",
[$username, $username]
);
if (!$user || !password_verify($password, $user['password'])) return null;
// Create session
$token = bin2hex(random_bytes(32));
$sessionId = hash('sha256', $token);
$db->execute(
"INSERT INTO sessions (id, user_id, ip_address, user_agent, expires_at)
VALUES (?, ?, ?, ?, DATE_ADD(NOW(), INTERVAL 8 HOUR))",
[$sessionId, $user['id'], $_SERVER['REMOTE_ADDR'] ?? '', $_SERVER['HTTP_USER_AGENT'] ?? '']
);
$db->execute("UPDATE users SET last_login = NOW() WHERE id = ?", [$user['id']]);
setcookie('ncpx_session', $token, [
'expires' => time() + 28800,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Strict',
]);
$this->user = $user;
return $token;
}
public function logout(): void {
$sessionId = hash('sha256', $_COOKIE['ncpx_session'] ?? '');
DB::getInstance()->execute("DELETE FROM sessions WHERE id = ?", [$sessionId]);
setcookie('ncpx_session', '', time() - 3600, '/', '', true, true);
}
public function user(): ?array { return $this->user; }
public function require(string ...$roles): void {
$user = $this->user();
if (!$user || !in_array($user['role'], $roles)) {
Response::error('Forbidden', 403);
}
}
/**
* Returns the correct panel URL for a given role
* Used by login redirect so each role lands on the right port
*/
public static function portalUrl(string $role, string $path = '/'): string {
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$hostname = preg_replace('/:\d+$/', '', $host);
$port = match($role) {
'admin' => PORT_ADMIN,
'reseller' => PORT_RESELLER,
default => PORT_USER,
};
return "https://{$hostname}:{$port}{$path}";
}
}