ServerManager project files
This commit is contained in:
189
src/Models/Server.php
Executable file
189
src/Models/Server.php
Executable file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ServerManager\Models;
|
||||
|
||||
use ServerManager\Core\Database;
|
||||
use ServerManager\Core\App;
|
||||
use ServerManager\Core\Encryption;
|
||||
|
||||
class Server
|
||||
{
|
||||
private Database $db;
|
||||
private Encryption $encryption;
|
||||
private array $encryptedFields = ['ssh_password', 'ssh_key'];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = Database::getInstance();
|
||||
$this->encryption = App::getEncryption();
|
||||
}
|
||||
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
$server = $this->db->fetch('SELECT * FROM servers WHERE id = ?', [$id]);
|
||||
if ($server) {
|
||||
$server = $this->encryption->decryptArray($server, $this->encryptedFields);
|
||||
}
|
||||
return $server;
|
||||
}
|
||||
|
||||
public function findAll(bool $activeOnly = false): array
|
||||
{
|
||||
$sql = 'SELECT * FROM servers';
|
||||
$params = [];
|
||||
|
||||
if ($activeOnly) {
|
||||
$sql .= ' WHERE is_active = 1';
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY name ASC';
|
||||
$servers = $this->db->fetchAll($sql, $params);
|
||||
|
||||
return array_map(function ($server) {
|
||||
return $this->encryption->decryptArray($server, $this->encryptedFields);
|
||||
}, $servers);
|
||||
}
|
||||
|
||||
public function getAll(int $page = 1, int $perPage = 20, array $filters = []): array
|
||||
{
|
||||
$where = [];
|
||||
$params = [];
|
||||
|
||||
if (!empty($filters['search'])) {
|
||||
$where[] = '(s.name LIKE ? OR s.ip_address LIKE ? OR s.description LIKE ?)';
|
||||
$search = '%' . $filters['search'] . '%';
|
||||
$params[] = $search;
|
||||
$params[] = $search;
|
||||
$params[] = $search;
|
||||
}
|
||||
|
||||
if (isset($filters['is_active']) && $filters['is_active'] !== '') {
|
||||
$where[] = 's.is_active = ?';
|
||||
$params[] = (int) $filters['is_active'];
|
||||
}
|
||||
|
||||
if (!empty($filters['group_name'])) {
|
||||
$where[] = 's.group_name = ?';
|
||||
$params[] = $filters['group_name'];
|
||||
}
|
||||
|
||||
if (!empty($filters['status'])) {
|
||||
$where[] = 's.current_status = ?';
|
||||
$params[] = $filters['status'];
|
||||
}
|
||||
|
||||
$whereClause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
$total = $this->db->fetch(
|
||||
"SELECT COUNT(*) as total FROM servers s {$whereClause}",
|
||||
$params
|
||||
);
|
||||
|
||||
$servers = $this->db->fetchAll(
|
||||
"SELECT s.* FROM servers s {$whereClause} ORDER BY s.name ASC LIMIT ? OFFSET ?",
|
||||
array_merge($params, [$perPage, $offset])
|
||||
);
|
||||
|
||||
$servers = array_map(function ($server) {
|
||||
return $this->encryption->decryptArray($server, $this->encryptedFields);
|
||||
}, $servers);
|
||||
|
||||
return [
|
||||
'data' => $servers,
|
||||
'total' => (int) ($total['total'] ?? 0),
|
||||
'page' => $page,
|
||||
'per_page' => $perPage,
|
||||
'total_pages' => ceil((int) ($total['total'] ?? 0) / $perPage),
|
||||
];
|
||||
}
|
||||
|
||||
public function create(array $data): int
|
||||
{
|
||||
if (!empty($data['ssh_password'])) {
|
||||
$data['ssh_password'] = $this->encryption->encrypt($data['ssh_password']);
|
||||
} else {
|
||||
$data['ssh_password'] = null;
|
||||
}
|
||||
|
||||
if (!empty($data['ssh_key'])) {
|
||||
$data['ssh_key'] = $this->encryption->encrypt($data['ssh_key']);
|
||||
} else {
|
||||
$data['ssh_key'] = null;
|
||||
}
|
||||
|
||||
$data['created_at'] = date('Y-m-d H:i:s');
|
||||
$data['updated_at'] = date('Y-m-d H:i:s');
|
||||
|
||||
if (empty($data['current_status'])) {
|
||||
$data['current_status'] = 'unknown';
|
||||
}
|
||||
|
||||
return $this->db->insert('servers', $data);
|
||||
}
|
||||
|
||||
public function update(int $id, array $data): bool
|
||||
{
|
||||
if (array_key_exists('ssh_password', $data)) {
|
||||
if (!empty($data['ssh_password'])) {
|
||||
$data['ssh_password'] = $this->encryption->encrypt($data['ssh_password']);
|
||||
} else {
|
||||
unset($data['ssh_password']);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('ssh_key', $data)) {
|
||||
if (!empty($data['ssh_key'])) {
|
||||
$data['ssh_key'] = $this->encryption->encrypt($data['ssh_key']);
|
||||
} else {
|
||||
unset($data['ssh_key']);
|
||||
}
|
||||
}
|
||||
|
||||
$data['updated_at'] = date('Y-m-d H:i:s');
|
||||
|
||||
return $this->db->update('servers', $data, 'id = ?', [$id]) > 0;
|
||||
}
|
||||
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
return $this->db->delete('servers', 'id = ?', [$id]) > 0;
|
||||
}
|
||||
|
||||
public function toggleActive(int $id): ?bool
|
||||
{
|
||||
$server = $this->db->fetch('SELECT is_active FROM servers WHERE id = ?', [$id]);
|
||||
if (!$server) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$newState = $server['is_active'] ? 0 : 1;
|
||||
$this->db->update('servers', ['is_active' => $newState, 'updated_at' => date('Y-m-d H:i:s')], 'id = ?', [$id]);
|
||||
|
||||
return (bool) $newState;
|
||||
}
|
||||
|
||||
public function getGroups(): array
|
||||
{
|
||||
$groups = $this->db->fetchAll(
|
||||
'SELECT DISTINCT group_name FROM servers WHERE group_name IS NOT NULL AND group_name != "" ORDER BY group_name'
|
||||
);
|
||||
return array_column($groups, 'group_name');
|
||||
}
|
||||
|
||||
public function countAll(): int
|
||||
{
|
||||
$result = $this->db->fetch("SELECT COUNT(*) as total FROM servers");
|
||||
return (int) ($result['total'] ?? 0);
|
||||
}
|
||||
|
||||
public function countOnline(): int
|
||||
{
|
||||
$result = $this->db->fetch(
|
||||
"SELECT COUNT(*) as total FROM servers WHERE current_status = 'online' AND is_active = 1"
|
||||
);
|
||||
return (int) ($result['total'] ?? 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user