added logger

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-02-06 17:59:17 +01:00
parent 4fb5cdcffd
commit ca1d4577dc

View File

@ -2,8 +2,10 @@
namespace App\Controller;
use App\Entity\Domain;
use App\Repository\DomainRepository;
use App\Repository\NameserverRepository;
use Monolog\Logger;
error_reporting(error_level: E_ALL);
@ -19,8 +21,13 @@ class DomainController
private string $namedConfLocalFile;
private string $zoneCachePath;
public function __construct(private NameserverRepository $nameserverRepository, private CheckController $checkController, private DomainRepository $domainRepository)
public function __construct(private NameserverRepository $nameserverRepository, private ApiController $checkController, private DomainRepository $domainRepository, private array $config, private Logger $log)
{
if ($this->config['debug']) {
$this->log->debug(message: "__construct()");
}
$this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/';
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
@ -53,6 +60,10 @@ class DomainController
function createIncludeFile()
{
if ($this->config['debug']) {
$this->log->debug(message: "createIncludeFile()");
}
$domains = $this->domainRepository->findAll();
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
@ -63,69 +74,55 @@ class DomainController
}
function delete(int $id)
function deleteOnNameservers(Domain $domain)
{
if ($domain = $this->domainRepository->findByID(id: $id)) {
$this->domainRepository->delete(id: $id);
$zoneFile = $this->localZonesDir . $domain['name'];
print($zoneFile . PHP_EOL);
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
print("file exists");
unlink(filename: $zoneFile);
$this->createIncludeFile();
}
if ($this->config['debug']) {
$this->log->debug(message: "deleteOnNameserver()");
}
$this->deleteOnNameservers(id: $id);
}
function deleteOnNameservers(int $id)
{
$nameservers = $this->nameserverRepository->findAll();
foreach ($nameservers as $nameserver) {
echo($nameserver['name']);
$body = [
'id' => $id
'name' => $domain->getName()
];
if (!empty($nameserver['aaaa'])) {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], command: 'delete', serverType: 'nameserver', body: $body);
if (!empty($nameserver->getAaaa())) {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 6, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body);
} else {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver['name'], versionIP: 4, apiKey: $nameserver['apikey'], command: 'delete', serverType: 'nameserver', body: $body);
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 4, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body);
}
}
}
/**
* @param int $id
* @param \App\Entity\Domain $domain
*
* @return void
*/
function deleteZone(int $id)
function deleteZone(Domain $domain)
{
if ($domain = $this->domainRepository->findByID(id: $id)) {
$zoneFile = $this->localZonesDir . $domain['name'];
print($zoneFile . PHP_EOL);
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
print("file exists");
unlink(filename: $zoneFile);
$this->createIncludeFile();
}
if ($this->config['debug']) {
$this->log->debug(message: "deleteZone()");
}
$this->deleteOnNameservers(id: $id);
$this->domainRepository->delete(id: $id);
$zoneFile = $this->localZonesDir . $domain->getName();
if (file_exists(filename: "$zoneFile")) {
unlink(filename: $zoneFile);
}
$this->createIncludeFile();
$this->deleteOnNameservers(domain: $domain);
}
/**
* @return void
*/
function checkPermissions(): void
{
if ($this->config['debug']) {
$this->log->debug(message: "checkPermissions()");
}
echo 'Checking permission:' . PHP_EOL . PHP_EOL;
$uid = posix_geteuid();
print("UID:\t$uid" . PHP_EOL);
@ -173,6 +170,7 @@ class DomainController
*/
function checkDomains(): array|bool
{
return true;
/*
$domains = $this->findAll();
@ -213,28 +211,33 @@ class DomainController
/**
* @param mixed $name
* @param mixed $a
* @param mixed $aaaa
* @param \App\Entity\Domain $domain
*
* @return void
*/
public function createZoneFile(string $name, string $a, string $aaaa): void
public function createZoneFile(Domain $domain): void
{
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "createZoneFile($domainName)");
}
if ($zonefile = fopen(filename: $this->localZonesDir . $domain->getName(), mode: 'w')) {
fputs(stream: $zonefile, data: 'zone \"' . $domain->getA() . '"' . ' IN {' . PHP_EOL);
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $domain->getName() . '.db";' . PHP_EOL);
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
if (!empty($a)) {
fputs(stream: $zonefile, data: "\t\t$a;" . PHP_EOL);
fputs(stream: $zonefile, data: "\t\t" . $domain->getA() . ';' . PHP_EOL);
}
if (!empty($aaaa)) {
fputs(stream: $zonefile, data: "\t\t$aaaa;" . PHP_EOL);
fputs(stream: $zonefile, data: "\t\t" . $domain->getAaaa() . ';' . PHP_EOL);
}
fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
fputs(stream: $zonefile, data: "};" . PHP_EOL);
}
$this->createIncludeFile();
// TODO add on nameservers
}
}