added creation of zone files
Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
parent
b3e8ff6ffb
commit
83709d06e0
@ -10,13 +10,20 @@ use PDOException;
|
||||
*/
|
||||
class DomainController
|
||||
{
|
||||
private PDO $dbConnection;
|
||||
private String $localZoneFile;
|
||||
private String $localZonesDir;
|
||||
private String $namedConfLocalFile;
|
||||
private string $zoneCachePath;
|
||||
|
||||
public function __construct(PDO $dbConnection)
|
||||
public function __construct(private PDO $dbConnection)
|
||||
{
|
||||
$this->dbConnection = $dbConnection;
|
||||
$this->localZoneFile = '/etc/bind/local.zones';
|
||||
$this->localZonesDir = '/etc/bind/zones/';
|
||||
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
|
||||
$this->zoneCachePath = '/var/cache/bind/';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|false
|
||||
*/
|
||||
@ -28,7 +35,7 @@ class DomainController
|
||||
|
||||
try {
|
||||
$statement = $this->dbConnection->query($statement);
|
||||
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
@ -42,41 +49,40 @@ class DomainController
|
||||
*/
|
||||
public function findByName(String $name): bool|array
|
||||
{
|
||||
$statement = "
|
||||
$sql = "
|
||||
SELECT id, name, a, aaaa
|
||||
FROM domains
|
||||
WHERE name = :name";
|
||||
|
||||
try {
|
||||
$statement = $this->dbConnection->prepare($statement);
|
||||
$statement->bindParam(':name', $name);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->execute();
|
||||
|
||||
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (\PDOException $e) {
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Int $id
|
||||
* @param int $id
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
public function findByID(Int $id): bool|array
|
||||
public function findByID(int $id): bool|array
|
||||
{
|
||||
$statement = "
|
||||
$sql = "
|
||||
SELECT id, name, a, aaaa
|
||||
FROM domains
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->dbConnection->prepare($statement);
|
||||
$statement->bindParam(':id', $id);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->bindParam(param:':id', var: $id);
|
||||
$statement->execute();
|
||||
|
||||
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (\PDOException $e) {
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
@ -92,24 +98,34 @@ class DomainController
|
||||
public function insert(String $name, String $a, String $aaaa): int
|
||||
{
|
||||
// TODO create zone file and include
|
||||
$statement = "
|
||||
$sql = "
|
||||
INSERT INTO domains (name, a, aaaa)
|
||||
VALUES (:name, :a, :aaaa)";
|
||||
|
||||
try {
|
||||
$statement = $this->dbConnection->prepare($statement);
|
||||
$statement->bindParam(':name', $name);
|
||||
$statement->bindParam(':a', $a);
|
||||
$statement->bindParam(':aaaa', $aaaa);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->bindParam(param: ':a', var: $a);
|
||||
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
||||
$statement->execute();
|
||||
return $statement->rowCount();
|
||||
} catch (\PDOException $e) {
|
||||
|
||||
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
|
||||
$zoneFilename = $this->localZonesDir . $name;
|
||||
echo $zoneFilename . PHP_EOL;
|
||||
|
||||
if ($localZones = fopen($this->localZoneFile, 'a')) {
|
||||
fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
|
||||
fclose($localZones);
|
||||
} else {
|
||||
echo "Error writing to $this->localZoneFile, check permissions";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return $this->dbConnection->lastInsertId();
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
|
||||
// TODO
|
||||
// create zone file
|
||||
// add zone file to include file
|
||||
}
|
||||
|
||||
|
||||
@ -123,28 +139,52 @@ class DomainController
|
||||
*/
|
||||
public function update(Int $id, String $name, String $a, String $aaaa)
|
||||
{
|
||||
// TODO UPDATE Zone file
|
||||
$current = $this->findByID($id);
|
||||
|
||||
/* doesn't work
|
||||
$statement = "
|
||||
UPDATE domains SET
|
||||
name = :name,
|
||||
a = :a,
|
||||
aaaa = :aaaa
|
||||
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($a)) {
|
||||
$a = $current['a'];
|
||||
}
|
||||
if (empty($aaaa)) {
|
||||
$aaaa = $current['aaaa'];
|
||||
}
|
||||
|
||||
$sql = "
|
||||
UPDATE domains SET
|
||||
name = :name,
|
||||
a = :a,
|
||||
aaaa = :aaaa
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->dbConnection->prepare($statement);
|
||||
$statement->bindParam('id', $id);
|
||||
$statement->bindParam('name', $name);
|
||||
$statement->bindParam('a', $a);
|
||||
$statement->bindParam('aaaa', $aaaa);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->bindParam(param: 'name', var: $name);
|
||||
$statement->bindParam(param: 'a', var: $a);
|
||||
$statement->bindParam(param: 'aaaa', var: $aaaa);
|
||||
$statement->execute();
|
||||
|
||||
// recreate zonefile
|
||||
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
|
||||
exec('/usr/sbin/rndc reload');
|
||||
|
||||
return $statement->rowCount();
|
||||
} catch (\PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
} catch (PDOException $e) {
|
||||
print($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO
|
||||
// recreate zone file
|
||||
}
|
||||
|
||||
|
||||
@ -162,11 +202,77 @@ class DomainController
|
||||
|
||||
try {
|
||||
$statement = $this->dbConnection->prepare($statement);
|
||||
$statement->bindParam('id', $id);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->rowCount();
|
||||
} catch (\PDOException $e) {
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|bool
|
||||
*/
|
||||
function checkDomains(): array|bool
|
||||
{
|
||||
$domains = $this->findAll();
|
||||
|
||||
if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) {
|
||||
if (!str_contains($namedConfLocal, $this->localZoneFile)) {
|
||||
return "$this->localZoneFile needs to be included in $this->namedConfLocalFile.";
|
||||
}
|
||||
} else {
|
||||
return "No access to '$this->namedConfLocalFile'. Please check permissions";
|
||||
}
|
||||
|
||||
if (!fileperms($this->localZoneFile)) {
|
||||
return "No access to $this->localZoneFile. Please check permissions.";
|
||||
}
|
||||
|
||||
$localZones = file_get_contents($this->localZoneFile);
|
||||
|
||||
foreach($domains as $domain) {
|
||||
if(!str_contains($localZones, $domain['name'])) {
|
||||
$errors[] = $domain['name'] . " is missing in '$this->localZoneFile'";
|
||||
}
|
||||
|
||||
$zoneFile = $this->localZonesDir . $domain['name'];
|
||||
|
||||
if (!file_exists($zoneFile)) {
|
||||
$errors[] = "Missing zone file for $zoneFile. Update zone to create it";
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
return true;
|
||||
} else {
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $name
|
||||
* @param mixed $a
|
||||
* @param mixed $aaaa
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createZoneFile(String $name, String $a, String $aaaa): void
|
||||
{
|
||||
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
|
||||
fputs($zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
|
||||
fputs($zonefile, data: "\ttype slave;" . PHP_EOL);
|
||||
fputs($zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
|
||||
fputs($zonefile, data: "\tmasters {" . PHP_EOL);
|
||||
if (!empty($a)) {
|
||||
fputs($zonefile, data: "\t\t$a;" . PHP_EOL);
|
||||
}
|
||||
if (!empty($aaaa)) {
|
||||
fputs($zonefile, data: "\t\t$aaaa;" . PHP_EOL);
|
||||
}
|
||||
fputs($zonefile, data: "\t};" . PHP_EOL);
|
||||
fputs($zonefile, data: "};" . PHP_EOL);
|
||||
}
|
||||
}
|
||||
}
|
@ -39,7 +39,7 @@ class RequestController
|
||||
if ($this->checkPassword()) {
|
||||
try {
|
||||
match ($this->requestMethod) {
|
||||
'GET' => $this->result = $this->handleDomainGetRequest(),
|
||||
'GET' => $this->handleDomainGetRequest(),
|
||||
'POST' => $this->handleDomainPostRequest(),
|
||||
'PUT' => $this->handleDomainPutRequest(),
|
||||
'DELETE' => $this->handleDomainDeleteRequest()
|
||||
@ -92,17 +92,19 @@ class RequestController
|
||||
/**
|
||||
* @return array|bool
|
||||
*/
|
||||
public function handleDomainGetRequest(): array|bool
|
||||
public function handleDomainGetRequest(): void
|
||||
{
|
||||
$result = '';
|
||||
if (empty($this->uri[3])) {
|
||||
$this->result = $this->domainController->findAll();
|
||||
} else {
|
||||
if (!$this->result = $this->domainController->findByName($this->uri[3])) {
|
||||
if ($result = $this->domainController->findByID(intval($this->uri[3]))) {
|
||||
$this->result = $result;
|
||||
} else {
|
||||
$this->status = "404 Not Found ";
|
||||
$this->message = "The specified domain was not found.";
|
||||
}
|
||||
}
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,9 +112,10 @@ class RequestController
|
||||
*/
|
||||
public function handleDomainPostRequest(): void
|
||||
{
|
||||
$name = $_POST['name'] ?? "";
|
||||
$a = $_POST['a'] ?? "";
|
||||
$aaaa = $_POST['aaaa'] ?? "";
|
||||
$name = $_POST['name'] ?? '';
|
||||
$a = $_POST['a'] ?? '';
|
||||
$aaaa = $_POST['aaaa'] ?? '';
|
||||
$apikey = $_POST['apikey'] ?? '';
|
||||
if (empty($name)) {
|
||||
$this->status = "400 Bad Request";
|
||||
$this->message = "A name is required";
|
||||
@ -125,7 +128,7 @@ class RequestController
|
||||
$this->status = "400 Bad request";
|
||||
$this->message = "Domain: $name already exists.";
|
||||
} else {
|
||||
$result = $this->domainController->insert($name, $a, $aaaa);
|
||||
$result = $this->domainController->insert($name, $a, $aaaa, $apikey);
|
||||
$this->status = "201 Created";
|
||||
$this->message = $result;
|
||||
}
|
||||
@ -151,6 +154,7 @@ class RequestController
|
||||
$name = $put['name'] ?? "";
|
||||
$a = $put['a'] ?? "";
|
||||
$aaaa = $put['aaaa'] ?? "";
|
||||
$apikey = $put['apikey'] ?? "";
|
||||
|
||||
if ($id == 0) {
|
||||
$this->status = "400 Bad Request";
|
||||
@ -169,7 +173,7 @@ class RequestController
|
||||
$this->status = "400 Bad Request";
|
||||
$this->message = "At least one IP address is required.";
|
||||
} else {
|
||||
$dcResult = $this->domainController->update($id, $name, $a, $aaaa);
|
||||
$dcResult = $this->domainController->update($id, $name, $a, $aaaa, $apikey);
|
||||
$this->status = "201 Updated";
|
||||
$this->message = $dcResult;
|
||||
}
|
||||
@ -196,7 +200,7 @@ class RequestController
|
||||
|
||||
if ($id == 0) {
|
||||
$this->status = "404 Bad Request";
|
||||
$this->message = "Domain with ID $id not found.";
|
||||
$this->message = "You need to supply an ID.";
|
||||
} else {
|
||||
if (!$this->domainController->findByID($id)) {
|
||||
$this->status = "400 Bad Request";
|
||||
|
Loading…
Reference in New Issue
Block a user