moved PDO stuff to repository

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-31 20:54:18 +01:00
parent 7976c2387e
commit 4b19963279
1 changed files with 85 additions and 240 deletions

View File

@ -1,23 +1,24 @@
<?php declare(strict_types=1);
namespace App\Controller;
use App\Repository\DomainRepository;
error_reporting(error_level: E_ALL);
use PDO;
use PDOException;
// TODO check include "/etc/bind/local.zones";
/**
*
*/
class DomainController
{
private String $localZoneFile;
private String $localZonesDir;
private String $namedConfLocalFile;
private string $localZoneFile;
private string $localZonesDir;
private string $namedConfLocalFile;
private string $zoneCachePath;
public function __construct(private DatabaseConnection $databaseConnection, private PanelController $panelController)
public function __construct(private NameserverController $nameserverController, private CheckController $checkController, private DomainRepository $domainRepository)
{
$this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/';
@ -25,102 +26,17 @@ class DomainController
$this->zoneCachePath = '/var/cache/bind/';
}
/**
* @return array|false
*/
public function findAll(): bool|array
{
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/*
/**
* @param String $name
* @param mixed $a
* @param mixed $aaaa
*
* @return array|false
*/
public function findByName(String $name): bool|array
* @return void
public function createZone(string $name, mixed $a, mixed $aaaa): void
{
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param int $id
*
* @return array|false
*/
public function findByID(int $id): bool|array
{
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param:':id', var: $id);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
*
* @return string|false
*/
public function insert(String $name, int $panelID, String $a, String $aaaa): bool|string
{
print("here");
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_id', var: $panelID);
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->execute();
print(PHP_EOL . "there");
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
/*
$zoneFilename = $this->localZonesDir . $name;
echo $zoneFilename . PHP_EOL;
@ -131,149 +47,79 @@ class DomainController
echo "Error writing to $this->localZoneFile, check permissions";
exit(1);
}
return $this->databaseConnection->getConnection()->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
*
* @return false|int
*/
public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int
{
$current = $this->findByID(id: $id);
/* doesn't work
$statement = "
INSERT INTO domains(id, name, a, aaaa)
VALUES(:id, :name, :a, :aaaa)
ON DUPLICATE KEY UPDATE
name=COALESCE(VALUES(name), :name),
a=COALESCE(:a, a),
aaaa=COALESCE(:aaaa, aaaa)";
*/
if (empty($name)) {
$name = $current['name'];
}
if (empty($panelID)) {
$panelID = $current['panel_id'];
}
$panelID = intval(value: $panelID);
if (empty($a)) {
$a = $current['a'];
}
if (empty($aaaa)) {
$aaaa = $current['aaaa'];
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name,
panel_id = :panel_id,
a = :a,
aaaa = :aaaa
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'panel_id', var: $panelID);
$statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->execute();
// recreate zonefile
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
exec(command: '/usr/sbin/rndc reload');
return $statement->rowCount();
} catch (PDOException $e) {
print($e->getMessage());
return false;
}
}
function createIncludeFile()
{
$domains = $this->findAll();
$domains = $this->domainRepository->findAll();
print("$this->localZoneFile");
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
foreach ($domains as $domain) {
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain['name'] . '";' . PHP_EOL);
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain->getName() . '";' . PHP_EOL);
}
fclose(stream: $oFile);
}
/**
* @param $id
*
* @return int
*/
public function delete($id): int
function delete(int $id)
{
if ($domain = $this->findByID(id: $id)) {
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);
}
}
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->execute();
$this->createIncludeFile();
return $statement->rowCount();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
$this->deleteOnNameservers(id: $id);
}
function deleteOnNameservers(int $id)
{
$nameservers = $this->nameserverController->findAll();
foreach ($nameservers as $nameserver) {
echo($nameserver['name']);
$body = [
'id' => $id
];
if (!empty($nameserver['aaaa'])) {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], 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);
}
}
}
/**
* @param String $field
* @param int $id
*
* @return int
* @return void
*/
public function getLongestEntry(String $field): int
function deleteZone(int $id)
{
$sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
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();
}
}
$this->deleteOnNameservers(id: $id);
$this->domainRepository->delete(id: $id);
}
/**
* @return void
*/
@ -372,7 +218,7 @@ class DomainController
*
* @return void
*/
public function createZoneFile(String $name, String $a, String $aaaa): void
public function createZoneFile(string $name, string $a, string $aaaa): void
{
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
@ -388,7 +234,6 @@ class DomainController
fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
fputs(stream: $zonefile, data: "};" . PHP_EOL);
}
// TODO check if ist exist in the include, else create
$this->createIncludeFile();
}
}