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);
|
<?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,102 +26,17 @@ class DomainController
|
||||||
$this->zoneCachePath = '/var/cache/bind/';
|
$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 String $name
|
||||||
|
* @param mixed $a
|
||||||
|
* @param mixed $aaaa
|
||||||
*
|
*
|
||||||
* @return array|false
|
* @return void
|
||||||
*/
|
public function createZone(string $name, mixed $a, mixed $aaaa): void
|
||||||
public function findByName(String $name): bool|array
|
|
||||||
{
|
{
|
||||||
$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);
|
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
|
||||||
|
/*
|
||||||
$zoneFilename = $this->localZonesDir . $name;
|
$zoneFilename = $this->localZonesDir . $name;
|
||||||
echo $zoneFilename . PHP_EOL;
|
echo $zoneFilename . PHP_EOL;
|
||||||
|
|
||||||
|
@ -131,149 +47,79 @@ class DomainController
|
||||||
echo "Error writing to $this->localZoneFile, check permissions";
|
echo "Error writing to $this->localZoneFile, check permissions";
|
||||||
exit(1);
|
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()
|
function createIncludeFile()
|
||||||
{
|
{
|
||||||
$domains = $this->findAll();
|
$domains = $this->domainRepository->findAll();
|
||||||
|
|
||||||
print("$this->localZoneFile");
|
|
||||||
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
|
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
|
||||||
foreach ($domains as $domain) {
|
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);
|
fclose(stream: $oFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
function delete(int $id)
|
||||||
* @param $id
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function delete($id): int
|
|
||||||
{
|
{
|
||||||
if ($domain = $this->findByID(id: $id)) {
|
|
||||||
|
if ($domain = $this->domainRepository->findByID(id: $id)) {
|
||||||
|
$this->domainRepository->delete(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);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$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();
|
$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 {
|
if ($domain = $this->domainRepository->findByID(id: $id)) {
|
||||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
$zoneFile = $this->localZonesDir . $domain['name'];
|
||||||
$statement->execute();
|
print($zoneFile . PHP_EOL);
|
||||||
$result = $statement->fetch();
|
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
|
||||||
return $result['length'];
|
print("file exists");
|
||||||
} catch (PDOException $e) {
|
unlink(filename: $zoneFile);
|
||||||
exit($e->getMessage());
|
$this->createIncludeFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->deleteOnNameservers(id: $id);
|
||||||
|
$this->domainRepository->delete(id: $id);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue