From c297eaad81309a72ee09df7752d29c1974d5f80a Mon Sep 17 00:00:00 2001 From: tracer Date: Mon, 31 Jan 2022 14:33:27 +0100 Subject: [PATCH] initial commit Signed-off-by: tracer --- src/Repository/DomainRepository.php | 240 ++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 src/Repository/DomainRepository.php diff --git a/src/Repository/DomainRepository.php b/src/Repository/DomainRepository.php new file mode 100644 index 0000000..d4a5477 --- /dev/null +++ b/src/Repository/DomainRepository.php @@ -0,0 +1,240 @@ +databaseConnection->getConnection()->prepare(query: $sql); + $statement->execute(); + while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { + $domain = new Domain(name: $result['name'], id: $result['id'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); + $domains[] = $domain; + } + return $domains; + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + + + /** + * @param int $id + * + * @return \App\Entity\Domain + */ + public function findByID(int $id): Domain + { + $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(); + $result = $statement->fetch(mode: PDO::FETCH_ASSOC); + + return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); + } catch (PDOException $e) { + exit($e->getMessage()); + } + } + + + + /** + * @param String $name + * + * @return \App\Entity\Domain + */ + public function findByName(string $name): Domain + { + $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(); + $result = $statement->fetch(mode: PDO::FETCH_ASSOC); + + return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); + } 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(); + + 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']; + } + exec(command: '/usr/sbin/rndc reload'); + + return $statement->rowCount(); + } catch (PDOException $e) { + print($e->getMessage()); + return false; + } + } + + + /** + * @param $id + * + * @return int + */ + public function delete($id): int + { + $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(); + + 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()); + } + } +} \ No newline at end of file