From 4b19963279bdc86b6d6f166f2633b7fbdfafcd5a Mon Sep 17 00:00:00 2001 From: tracer Date: Mon, 31 Jan 2022 20:54:18 +0100 Subject: [PATCH] moved PDO stuff to repository Signed-off-by: tracer --- src/Controller/DomainController.php | 325 ++++++++-------------------- 1 file changed, 85 insertions(+), 240 deletions(-) diff --git a/src/Controller/DomainController.php b/src/Controller/DomainController.php index abe3156..fd464f9 100644 --- a/src/Controller/DomainController.php +++ b/src/Controller/DomainController.php @@ -1,23 +1,24 @@ localZoneFile = '/etc/bind/local.zones'; $this->localZonesDir = '/etc/bind/zones/'; @@ -25,46 +26,73 @@ class DomainController $this->zoneCachePath = '/var/cache/bind/'; } - + /* /** - * @return array|false - */ - public function findAll(): bool|array + * @param String $name + * @param mixed $a + * @param mixed $aaaa + * + * @return void + public function createZone(string $name, mixed $a, mixed $aaaa): void { - $sql = " - SELECT id, name, panel_id, a, aaaa - FROM " . DatabaseConnection::TABLE_DOMAINS . " - ORDER BY name"; + $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa); +/* + $zoneFilename = $this->localZonesDir . $name; + echo $zoneFilename . PHP_EOL; - try { - $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); - $statement->execute(); - return $statement->fetchAll(mode: PDO::FETCH_ASSOC); - } catch (PDOException $e) { - exit($e->getMessage()); + 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); } } + */ + + 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); + } - /** - * @param String $name - * - * @return array|false - */ - public function findByName(String $name): bool|array + function delete(int $id) { - $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()); + 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(); + } + } + + $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 * - * @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 { - $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)) { + 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(); } } - $sql = " - DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " - WHERE id = :id"; + $this->deleteOnNameservers(id: $id); + $this->domainRepository->delete(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'); $members = $bindGroup['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); 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(); } } \ No newline at end of file