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); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use App\Repository\DomainRepository;
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
// TODO check include "/etc/bind/local.zones";
use PDO;
use PDOException;
/** /**
* *
*/ */
class DomainController class DomainController
{ {
private String $localZoneFile; private string $localZoneFile;
private String $localZonesDir; private string $localZonesDir;
private String $namedConfLocalFile; private string $namedConfLocalFile;
private string $zoneCachePath; 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->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/'; $this->localZonesDir = '/etc/bind/zones/';
@ -25,46 +26,73 @@ class DomainController
$this->zoneCachePath = '/var/cache/bind/'; $this->zoneCachePath = '/var/cache/bind/';
} }
/*
/** /**
* @return array|false * @param String $name
*/ * @param mixed $a
public function findAll(): bool|array * @param mixed $aaaa
*
* @return void
public function createZone(string $name, mixed $a, mixed $aaaa): void
{ {
$sql = " $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
SELECT id, name, panel_id, a, aaaa /*
FROM " . DatabaseConnection::TABLE_DOMAINS . " $zoneFilename = $this->localZonesDir . $name;
ORDER BY name"; echo $zoneFilename . PHP_EOL;
try { if ($localZones = fopen(filename: $this->localZoneFile, mode: 'a')) {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); fputs(stream: $localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
$statement->execute(); fclose(stream: $localZones);
return $statement->fetchAll(mode: PDO::FETCH_ASSOC); } else {
} catch (PDOException $e) { echo "Error writing to $this->localZoneFile, check permissions";
exit($e->getMessage()); exit(1);
} }
} }
*/
function createIncludeFile()
{
$domains = $this->domainRepository->findAll();
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
foreach ($domains as $domain) {
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain->getName() . '";' . PHP_EOL);
}
fclose(stream: $oFile);
}
/** function delete(int $id)
* @param String $name
*
* @return array|false
*/
public function findByName(String $name): bool|array
{ {
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name";
try { if ($domain = $this->domainRepository->findByID(id: $id)) {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $this->domainRepository->delete(id: $id);
$statement->bindParam(param: ':name', var: $name); $zoneFile = $this->localZonesDir . $domain['name'];
$statement->execute(); print($zoneFile . PHP_EOL);
return $statement->fetch(mode: PDO::FETCH_ASSOC); if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
} catch (PDOException $e) { print("file exists");
exit($e->getMessage()); unlink(filename: $zoneFile);
$this->createIncludeFile();
}
}
$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);
}
} }
} }
@ -72,206 +100,24 @@ class DomainController
/** /**
* @param int $id * @param int $id
* *
* @return array|false * @return void
*/ */
public function findByID(int $id): bool|array function deleteZone(int $id)
{ {
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try { if ($domain = $this->domainRepository->findByID(id: $id)) {
$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;
if ($localZones = fopen(filename: $this->localZoneFile, mode: 'a')) {
fputs(stream: $localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
fclose(stream: $localZones);
} else {
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();
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);
}
fclose(stream: $oFile);
}
/**
* @param $id
*
* @return int
*/
public function delete($id): int
{
if ($domain = $this->findByID(id: $id)) {
$zoneFile = $this->localZonesDir . $domain['name']; $zoneFile = $this->localZonesDir . $domain['name'];
print($zoneFile . PHP_EOL); print($zoneFile . PHP_EOL);
if (file_exists(filename: $this->localZonesDir . $domain['name'])) { if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
print("file exists"); print("file exists");
unlink(filename: $zoneFile); unlink(filename: $zoneFile);
$this->createIncludeFile();
} }
} }
$sql = " $this->deleteOnNameservers(id: $id);
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " $this->domainRepository->delete(id: $id);
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());
}
}
/**
* @param String $field
*
* @return int
*/
public function getLongestEntry(String $field): int
{
$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());
}
} }
/** /**
@ -289,13 +135,13 @@ class DomainController
$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)) {
echo "\t✅ is in group 'bind" . PHP_EOL; echo "\t✅ is in group 'bind" . PHP_EOL;
} }
echo 'Checking file: ' .$this->localZoneFile . PHP_EOL; echo 'Checking file: ' . $this->localZoneFile . PHP_EOL;
$localZoneFilePermissions = fileperms(filename: $this->localZoneFile); $localZoneFilePermissions = fileperms(filename: $this->localZoneFile);
if ($localZoneFilePermissions & 0x0010) { if ($localZoneFilePermissions & 0x0010) {
echo "\t✅ Group has write access." . PHP_EOL; echo "\t✅ Group has write access . " . PHP_EOL;
} else { } else {
echo "\t❌Group needs write permission!" . PHP_EOL; echo "\t❌Group needs write permission!" . PHP_EOL;
} }
@ -303,18 +149,18 @@ class DomainController
echo "Checking $this->namedConfLocalFile" . PHP_EOL; echo "Checking $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)) {
echo "\t$this->localZoneFile needs to be included in $this->namedConfLocalFile." . PHP_EOL; echo "\t$this->localZoneFile needs to be included in $this->namedConfLocalFile . " . PHP_EOL;
} else { } else {
echo "\t$this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL; echo "\t$this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
} }
} else { } else {
echo "\t❌ No access to '$this->namedConfLocalFile'. Please check permissions" . PHP_EOL; echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
} }
echo 'Checking directory: ' . $this->localZonesDir . PHP_EOL; echo 'Checking directory: ' . $this->localZonesDir . PHP_EOL;
$localZoneDirPermissions = fileperms(filename: $this->localZonesDir); $localZoneDirPermissions = fileperms(filename: $this->localZonesDir);
if ($localZoneDirPermissions & 0x0010) { if ($localZoneDirPermissions & 0x0010) {
echo "\t✅ Group has write access." . PHP_EOL; echo "\t✅ Group has write access . " . PHP_EOL;
} else { } else {
echo "\t❌Group needs write permission!" . PHP_EOL; echo "\t❌Group needs write permission!" . PHP_EOL;
} }
@ -332,14 +178,14 @@ class DomainController
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)) {
return "$this->localZoneFile needs to be included in $this->namedConfLocalFile."; return "$this->localZoneFile needs to be included in $this->namedConfLocalFile . ";
} }
} else { } else {
return "No access to '$this->namedConfLocalFile'. Please check permissions"; return "No access to '$this->namedConfLocalFile' . Please check permissions";
} }
if (!fileperms($this->localZoneFile)) { if (!fileperms($this->localZoneFile)) {
return "No access to $this->localZoneFile. Please check permissions."; return "No access to $this->localZoneFile . Please check permissions . ";
} }
$localZones = file_get_contents($this->localZoneFile); $localZones = file_get_contents($this->localZoneFile);
@ -352,7 +198,7 @@ class DomainController
$zoneFile = $this->localZonesDir . $domain['name']; $zoneFile = $this->localZonesDir . $domain['name'];
if (!file_exists($zoneFile)) { if (!file_exists($zoneFile)) {
$errors[] = "Missing zone file for $zoneFile. Update zone to create it"; $errors[] = "Missing zone file for $zoneFile . Update zone to create it";
} }
} }
@ -372,7 +218,7 @@ class DomainController
* *
* @return void * @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')) { if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL); 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: "\t};" . PHP_EOL);
fputs(stream: $zonefile, data: "};" . PHP_EOL); fputs(stream: $zonefile, data: "};" . PHP_EOL);
} }
$this->createIncludeFile();
// TODO check if ist exist in the include, else create
} }
} }