moved PDO stuff to repository
Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
parent
7976c2387e
commit
4b19963279
|
@ -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
|
||||
*/
|
||||
|
@ -292,10 +138,10 @@ class DomainController
|
|||
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);
|
||||
if ($localZoneFilePermissions & 0x0010) {
|
||||
echo "\t✅ Group has write access." . PHP_EOL;
|
||||
echo "\t✅ Group has write access . " . PHP_EOL;
|
||||
} else {
|
||||
echo "\t❌Group needs write permission!" . PHP_EOL;
|
||||
}
|
||||
|
@ -303,18 +149,18 @@ class DomainController
|
|||
echo "Checking $this->namedConfLocalFile" . PHP_EOL;
|
||||
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
||||
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 {
|
||||
echo "\t✅ $this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
|
||||
}
|
||||
} 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;
|
||||
$localZoneDirPermissions = fileperms(filename: $this->localZonesDir);
|
||||
if ($localZoneDirPermissions & 0x0010) {
|
||||
echo "\t✅ Group has write access." . PHP_EOL;
|
||||
echo "\t✅ Group has write access . " . PHP_EOL;
|
||||
} else {
|
||||
echo "\t❌Group needs write permission!" . PHP_EOL;
|
||||
}
|
||||
|
@ -332,14 +178,14 @@ class DomainController
|
|||
|
||||
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
||||
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 {
|
||||
return "No access to '$this->namedConfLocalFile'. Please check permissions";
|
||||
return "No access to '$this->namedConfLocalFile' . Please check permissions";
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -352,7 +198,7 @@ class DomainController
|
|||
$zoneFile = $this->localZonesDir . $domain['name'];
|
||||
|
||||
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
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue