2022-01-18 19:14:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class DomainController
|
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
private String $localZoneFile;
|
|
|
|
private String $localZonesDir;
|
|
|
|
private String $namedConfLocalFile;
|
|
|
|
private string $zoneCachePath;
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-22 18:25:18 +01:00
|
|
|
public function __construct(private DatabaseConnection $databaseConnection)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$this->localZoneFile = '/etc/bind/local.zones';
|
|
|
|
$this->localZonesDir = '/etc/bind/zones/';
|
|
|
|
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
|
|
|
|
$this->zoneCachePath = '/var/cache/bind/';
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
/**
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function findAll(): bool|array
|
|
|
|
{
|
|
|
|
$statement = "
|
|
|
|
SELECT id, name, a, aaaa
|
2022-01-22 18:25:18 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_DOMAINS;
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
try {
|
2022-01-22 18:25:18 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->query($statement);
|
2022-01-22 17:32:36 +01:00
|
|
|
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
2022-01-18 19:14:24 +01:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function findByName(String $name): bool|array
|
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$sql = "
|
2022-01-18 19:14:24 +01:00
|
|
|
SELECT id, name, a, aaaa
|
2022-01-22 18:25:18 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_DOMAINS . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE name = :name";
|
|
|
|
|
|
|
|
try {
|
2022-01-22 18:25:18 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 17:32:36 +01:00
|
|
|
return $statement->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
/**
|
2022-01-22 17:32:36 +01:00
|
|
|
* @param int $id
|
2022-01-18 19:14:24 +01:00
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2022-01-22 17:32:36 +01:00
|
|
|
public function findByID(int $id): bool|array
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$sql = "
|
2022-01-18 19:14:24 +01:00
|
|
|
SELECT id, name, a, aaaa
|
2022-01-22 18:25:18 +01:00
|
|
|
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
2022-01-22 18:25:18 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param:':id', var: $id);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 17:32:36 +01:00
|
|
|
return $statement->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
* @param String $a
|
|
|
|
* @param String $aaaa
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function insert(String $name, String $a, String $aaaa): int
|
|
|
|
{
|
|
|
|
// TODO create zone file and include
|
2022-01-22 17:32:36 +01:00
|
|
|
$sql = "
|
2022-01-22 18:25:18 +01:00
|
|
|
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, a, aaaa)
|
2022-01-18 19:14:24 +01:00
|
|
|
VALUES (:name, :a, :aaaa)";
|
|
|
|
|
|
|
|
try {
|
2022-01-22 18:25:18 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
|
|
|
$statement->bindParam(param: ':a', var: $a);
|
|
|
|
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 17:32:36 +01:00
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2022-01-22 18:25:18 +01:00
|
|
|
return $this->databaseConnection->getConnection()->lastInsertId();
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Int $id
|
|
|
|
* @param String $name
|
|
|
|
* @param String $a
|
|
|
|
* @param String $aaaa
|
|
|
|
*
|
2022-01-22 18:25:18 +01:00
|
|
|
* @return false|int
|
2022-01-18 19:14:24 +01:00
|
|
|
*/
|
2022-01-22 18:25:18 +01:00
|
|
|
public function update(Int $id, String $name, String $a, String $aaaa): bool|int
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$current = $this->findByID($id);
|
|
|
|
|
|
|
|
/* doesn't work
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement = "
|
2022-01-22 17:32:36 +01:00
|
|
|
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 = "
|
2022-01-22 18:25:18 +01:00
|
|
|
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
|
2022-01-22 17:32:36 +01:00
|
|
|
name = :name,
|
|
|
|
a = :a,
|
|
|
|
aaaa = :aaaa
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
2022-01-22 18:25:18 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->bindParam(param: 'name', var: $name);
|
|
|
|
$statement->bindParam(param: 'a', var: $a);
|
|
|
|
$statement->bindParam(param: 'aaaa', var: $aaaa);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 17:32:36 +01:00
|
|
|
|
|
|
|
// recreate zonefile
|
|
|
|
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
|
2022-01-22 18:25:18 +01:00
|
|
|
exec(command: '/usr/sbin/rndc reload');
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
return $statement->rowCount();
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
print($e->getMessage());
|
|
|
|
return false;
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function delete($id): int
|
|
|
|
{
|
|
|
|
// TODO delete zone file and include
|
|
|
|
$statement = "
|
2022-01-22 18:25:18 +01:00
|
|
|
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
2022-01-22 18:25:18 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($statement);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
|
|
|
return $statement->rowCount();
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 17:32:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 18:25:18 +01:00
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|