57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
namespace SyslogDash;
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
class Database
|
|
{
|
|
private static ?PDO $instance = null;
|
|
|
|
public static function getInstance(): PDO
|
|
{
|
|
if (self::$instance === null) {
|
|
$config = self::getConfig();
|
|
try {
|
|
self::$instance = new PDO(
|
|
$config['dsn'],
|
|
$config['username'],
|
|
$config['password'],
|
|
[
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
]
|
|
);
|
|
} catch (PDOException $e) {
|
|
throw new \RuntimeException('Database verbinding mislukt: ' . $e->getMessage());
|
|
}
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private static function getConfig(): array
|
|
{
|
|
$cfg = require __DIR__ . '/../config.php';
|
|
return $cfg['db'];
|
|
}
|
|
|
|
public static function table(): string
|
|
{
|
|
$cfg = require __DIR__ . '/../config.php';
|
|
return $cfg['syslog']['table'] ?? 'SystemEvents';
|
|
}
|
|
|
|
public static function getHostToIp(): array
|
|
{
|
|
$cfg = require __DIR__ . '/../config.php';
|
|
return $cfg['syslog']['host_to_ip'] ?? [];
|
|
}
|
|
|
|
public static function resolveHostnames(): bool
|
|
{
|
|
$cfg = require __DIR__ . '/../config.php';
|
|
return $cfg['syslog']['resolve_hostnames'] ?? true;
|
|
}
|
|
}
|