added logger

This commit is contained in:
tracer 2022-09-17 16:27:01 +02:00
parent d1325202ee
commit b25f0ab1eb
1 changed files with 25 additions and 42 deletions

View File

@ -28,15 +28,9 @@ class DomainController
private readonly ApiController $checkController, private readonly ApiController $checkController,
private readonly DomainRepository $domainRepository, private readonly DomainRepository $domainRepository,
private readonly PanelRepository $panelRepository, private readonly PanelRepository $panelRepository,
private readonly bool $verbose, private readonly ConfigController $configController,
private readonly bool $debug, private readonly Logger $logger)
private readonly Logger $log)
{ {
if ($this->$debug) {
$this->log->debug(message: "__construct()");
}
$this->localZoneFile = '/etc/bind/local.zones'; $this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/'; $this->localZonesDir = '/etc/bind/zones/';
$this->namedConfLocalFile = '/etc/bind/named.conf.local'; $this->namedConfLocalFile = '/etc/bind/named.conf.local';
@ -46,9 +40,7 @@ class DomainController
function createIncludeFile(): void function createIncludeFile(): void
{ {
if ($this->debug) { $this->logger->debug(message: "createIncludeFile()");
$this->log->debug(message: "createIncludeFile()");
}
$domains = $this->domainRepository->findAll(); $domains = $this->domainRepository->findAll();
@ -76,9 +68,7 @@ class DomainController
function updateSlaveZones(): void function updateSlaveZones(): void
{ {
if ($this->verbose) { $this->logger->debug(message: 'Delete all slave zones');
echo 'Delete all slave zones';
}
$zones = glob(pattern: $this->localZonesDir . '*'); $zones = glob(pattern: $this->localZonesDir . '*');
foreach ($zones as $zone) { foreach ($zones as $zone) {
@ -88,7 +78,7 @@ class DomainController
$domains = $this->domainRepository->findAll(); $domains = $this->domainRepository->findAll();
foreach ($domains as $domain) { foreach ($domains as $domain) {
if ($this->verbose) { if ($this->configController->getConfig(configKey: 'verbose')) {
echo 'Create zone: ' . $domain->getName() . PHP_EOL; echo 'Create zone: ' . $domain->getName() . PHP_EOL;
} }
$this->createSlaveZoneFile(domain: $domain); $this->createSlaveZoneFile(domain: $domain);
@ -100,9 +90,7 @@ class DomainController
function deleteOnNameservers(Domain $domain): void function deleteOnNameservers(Domain $domain): void
{ {
if ($this->debug) { $this->logger->debug(message: "deleteOnNameserver()");
$this->log->debug(message: "deleteOnNameserver()");
}
$nameservers = $this->nameserverRepository->findAll(); $nameservers = $this->nameserverRepository->findAll();
foreach ($nameservers as $nameserver) { foreach ($nameservers as $nameserver) {
@ -125,9 +113,7 @@ class DomainController
*/ */
function deleteZone(Domain $domain): void function deleteZone(Domain $domain): void
{ {
if ($this->debug) { $this->logger->debug(message: "deleteZone()");
$this->log->debug(message: "deleteZone()");
}
$zoneFile = $this->localZonesDir . $domain->getName(); $zoneFile = $this->localZonesDir . $domain->getName();
if (file_exists(filename: "$zoneFile")) { if (file_exists(filename: "$zoneFile")) {
@ -144,80 +130,79 @@ class DomainController
function checkPermissions(): bool function checkPermissions(): bool
{ {
$setupIsValid = true; $setupIsValid = true;
$verbose = $this->configController->getConfig(configKey: 'verbose');
if ($this->debug) { $this->logger->debug(message: "checkPermissions()");
$this->log->debug(message: "checkPermissions()");
}
if ($this->verbose) { if ($verbose) {
echo 'Checking permissions...' . PHP_EOL; echo 'Checking permissions...' . PHP_EOL;
} }
$uid = posix_geteuid(); $uid = posix_geteuid();
if ($this->verbose) { if ($verbose) {
echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL; echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL;
} }
$pwuid = posix_getpwuid(user_id: $uid); $pwuid = posix_getpwuid(user_id: $uid);
$name = $pwuid['name']; $name = $pwuid['name'];
if ($this->verbose) { if ($verbose) {
echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL; echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL;
} }
$bindGroup = posix_getgrnam(name: 'bind'); $bindGroup = posix_getgrnam(name: 'bind');
$members = $bindGroup['members']; $members = $bindGroup['members'];
if (in_array(needle: $name, haystack: $members)) { if (in_array(needle: $name, haystack: $members)) {
if ($this->verbose) { if ($verbose) {
echo "\t$name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL; echo "\t$name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL;
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if ($this->verbose) { if ($verbose) {
echo COLOR_RED . "\t$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL; echo COLOR_RED . "\t$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL;
} }
} }
if ($this->verbose) { if ($verbose) {
echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL; echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL;
} }
$localZoneFilePermissions = @fileperms(filename: $this->localZoneFile); $localZoneFilePermissions = @fileperms(filename: $this->localZoneFile);
if ($localZoneFilePermissions & 0x0010) { if ($localZoneFilePermissions & 0x0010) {
if ($this->verbose) { if ($verbose) {
echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL; echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL;
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if ($this->verbose) { if ($verbose) {
echo COLOR_RED . "\t❌Group needs write permission!" . COLOR_DEFAULT . PHP_EOL; echo COLOR_RED . "\t❌Group needs write permission!" . COLOR_DEFAULT . PHP_EOL;
} }
} }
if ($this->verbose) { if ($verbose) {
echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
} }
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) { if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) { if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
$setupIsValid = false; $setupIsValid = false;
if ($this->verbose) { if ($verbose) {
echo "\t$this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; echo "\t$this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
} }
} else { } else {
if ($this->verbose) { if ($verbose) {
echo "\t$this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; echo "\t$this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
} }
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if ($this->verbose) { if ($verbose) {
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL; echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
} }
} }
if ($this->verbose) { if ($verbose) {
echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL; echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL;
} }
$localZoneDirPermissions = @fileperms(filename: $this->localZonesDir); $localZoneDirPermissions = @fileperms(filename: $this->localZonesDir);
if ($localZoneDirPermissions & 0x0010) { if ($localZoneDirPermissions & 0x0010) {
if ($this->verbose) { if ($verbose) {
echo "\t✅ Group has write access." . PHP_EOL; echo "\t✅ Group has write access." . PHP_EOL;
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if ($this->verbose) { if ($verbose) {
echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL; echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL;
} }
} }
@ -273,9 +258,7 @@ class DomainController
public function createSlaveZoneFile(Domain $domain): void public function createSlaveZoneFile(Domain $domain): void
{ {
$domainName = $domain->getName(); $domainName = $domain->getName();
if ($this->debug) { $this->logger->info(message: "createZoneFile($domainName)");
$this->log->debug(message: "createZoneFile($domainName)");
}
// check if we're a master zone // check if we're a master zone
if ($this->isMasterZone(domain: $domain)) { if ($this->isMasterZone(domain: $domain)) {