added panel_id

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-25 20:34:13 +01:00
parent b5003caf6e
commit 35ae3c4505
1 changed files with 98 additions and 12 deletions

View File

@ -15,7 +15,7 @@ class DomainController
private String $namedConfLocalFile;
private string $zoneCachePath;
public function __construct(private DatabaseConnection $databaseConnection)
public function __construct(private DatabaseConnection $databaseConnection, private PanelController $panelController)
{
$this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/';
@ -30,8 +30,9 @@ class DomainController
public function findAll(): bool|array
{
$statement = "
SELECT id, name, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS;
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->query($statement);
@ -50,7 +51,7 @@ class DomainController
public function findByName(String $name): bool|array
{
$sql = "
SELECT id, name, a, aaaa
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name";
@ -73,7 +74,7 @@ class DomainController
public function findByID(int $id): bool|array
{
$sql = "
SELECT id, name, a, aaaa
SELECT id, name, panel_id, a, aaaa
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
@ -90,30 +91,37 @@ class DomainController
/**
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
*
* @return int
*/
public function insert(String $name, String $a, String $aaaa): int
public function insert(String $name, int $panelID, String $a, String $aaaa): int
{
// TODO create zone file and include
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, a, aaaa)
VALUES (:name, :a, :aaaa)";
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)";
try {
$statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_d', var: $panelID);
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->execute();
if ($panel = $this->panelController->findByID($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($this->localZoneFile, 'a')) {
if ($localZones = fopen($this->localZoneFile, mode: 'a')) {
fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
fclose($localZones);
} else {
@ -125,19 +133,19 @@ class DomainController
} 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, String $a, String $aaaa): bool|int
public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int
{
$current = $this->findByID($id);
@ -154,6 +162,10 @@ class DomainController
if (empty($name)) {
$name = $current['name'];
}
if (empty($panelID)) {
$panelID = $current['panel_id'];
}
$panelID = intval(value: $panelID);
if (empty($a)) {
$a = $current['a'];
}
@ -164,6 +176,7 @@ class DomainController
$sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name,
panel_id = :panel_id,
a = :a,
aaaa = :aaaa
WHERE id = :id";
@ -172,11 +185,16 @@ class DomainController
$statement = $this->databaseConnection->getConnection()->prepare($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($panelID)) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
exec(command: '/usr/sbin/rndc reload');
@ -210,6 +228,72 @@ class DomainController
}
}
/**
* @param String $field
*
* @return int
*/
public function getLongestEntry(String $field): int
{
$statement = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
try {
$statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @return void
*/
function checkPermissions(): void
{
echo 'Checking permission:' . PHP_EOL . PHP_EOL;
$uid = posix_geteuid();
print("UID:\t$uid" . PHP_EOL);
$pwuid = posix_getpwuid($uid);
$name = $pwuid['name'];
print("Name:\t$name" . PHP_EOL);
$bindGroup = posix_getgrnam(name: 'bind');
$members = $bindGroup['members'];
if (in_array(needle: $name, haystack: $members)) {
echo "\t✅ is in group 'bind" . 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;
} else {
echo "\t❌Group needs write permission!" . PHP_EOL;
}
echo "Checking $this->namedConfLocalFile" . PHP_EOL;
if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) {
if (!str_contains($namedConfLocal, $this->localZoneFile)) {
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 'Checking directory: ' . $this->localZonesDir . PHP_EOL;
$localZoneDirPermissions = fileperms(filename: $this->localZonesDir);
if ($localZoneDirPermissions & 0x0010) {
echo "\t✅ Group has write access." . PHP_EOL;
} else {
echo "\t❌Group needs write permission!" . PHP_EOL;
}
}
/**
* @return array|bool
@ -275,5 +359,7 @@ class DomainController
fputs($zonefile, data: "\t};" . PHP_EOL);
fputs($zonefile, data: "};" . PHP_EOL);
}
// TODO check if ist exist in the include, else create
}
}