Compare commits

..

No commits in common. "9eee91fd775381a31afcb086e289af28e2fa81ef" and "3a14eeb3c50a5a401110c1c3786df188bb3a0eb8" have entirely different histories.

4 changed files with 14 additions and 121 deletions

View File

@ -33,7 +33,7 @@ class DatabaseConnection
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
if (empty($result)) { if (empty($result)) {
// ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`;
echo 'Error: Cannot find tables.' . PHP_EOL; echo 'Error: Cannot find tables.' . PHP_EOL;
if (confirm('Should I try to create them?')) { if (confirm('Should I try to create them?')) {
$sql = " $sql = "
@ -51,7 +51,6 @@ class DatabaseConnection
CREATE TABLE `domains` ( CREATE TABLE `domains` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`panel_id` int(11) DEFAULT 0,
`a` varbinary(255) DEFAULT NULL, `a` varbinary(255) DEFAULT NULL,
`aaaa` varbinary(255) DEFAULT NULL, `aaaa` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)

View File

@ -15,7 +15,7 @@ class DomainController
private String $namedConfLocalFile; private String $namedConfLocalFile;
private string $zoneCachePath; private string $zoneCachePath;
public function __construct(private DatabaseConnection $databaseConnection, private PanelController $panelController) public function __construct(private DatabaseConnection $databaseConnection)
{ {
$this->localZoneFile = '/etc/bind/local.zones'; $this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/'; $this->localZonesDir = '/etc/bind/zones/';
@ -30,9 +30,8 @@ class DomainController
public function findAll(): bool|array public function findAll(): bool|array
{ {
$statement = " $statement = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS;
ORDER BY name";
try { try {
$statement = $this->databaseConnection->getConnection()->query($statement); $statement = $this->databaseConnection->getConnection()->query($statement);
@ -51,7 +50,7 @@ class DomainController
public function findByName(String $name): bool|array public function findByName(String $name): bool|array
{ {
$sql = " $sql = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name"; WHERE name = :name";
@ -74,7 +73,7 @@ class DomainController
public function findByID(int $id): bool|array public function findByID(int $id): bool|array
{ {
$sql = " $sql = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, a, aaaa
FROM . " . DatabaseConnection::TABLE_DOMAINS . " FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id"; WHERE id = :id";
@ -91,37 +90,30 @@ class DomainController
/** /**
* @param String $name * @param String $name
* @param int $panelID
* @param String $a * @param String $a
* @param String $aaaa * @param String $aaaa
* *
* @return int * @return int
*/ */
public function insert(String $name, int $panelID, String $a, String $aaaa): int public function insert(String $name, String $a, String $aaaa): int
{ {
// TODO create zone file and include // TODO create zone file and include
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa) INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)"; VALUES (:name, :a, :aaaa)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_d', var: $panelID);
$statement->bindParam(param: ':a', var: $a); $statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa); $statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->execute(); $statement->execute();
if ($panel = $this->panelController->findByID($panelID)) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa); $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
$zoneFilename = $this->localZonesDir . $name; $zoneFilename = $this->localZonesDir . $name;
echo $zoneFilename . PHP_EOL; echo $zoneFilename . PHP_EOL;
if ($localZones = fopen($this->localZoneFile, mode: 'a')) { if ($localZones = fopen($this->localZoneFile, 'a')) {
fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL); fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
fclose($localZones); fclose($localZones);
} else { } else {
@ -133,19 +125,19 @@ class DomainController
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param Int $id * @param Int $id
* @param String $name * @param String $name
* @param int $panelID
* @param String $a * @param String $a
* @param String $aaaa * @param String $aaaa
* *
* @return false|int * @return false|int
*/ */
public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int public function update(Int $id, String $name, String $a, String $aaaa): bool|int
{ {
$current = $this->findByID($id); $current = $this->findByID($id);
@ -162,10 +154,6 @@ class DomainController
if (empty($name)) { if (empty($name)) {
$name = $current['name']; $name = $current['name'];
} }
if (empty($panelID)) {
$panelID = $current['panel_id'];
}
$panelID = intval(value: $panelID);
if (empty($a)) { if (empty($a)) {
$a = $current['a']; $a = $current['a'];
} }
@ -176,7 +164,6 @@ class DomainController
$sql = " $sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name, name = :name,
panel_id = :panel_id,
a = :a, a = :a,
aaaa = :aaaa aaaa = :aaaa
WHERE id = :id"; WHERE id = :id";
@ -185,16 +172,11 @@ class DomainController
$statement = $this->databaseConnection->getConnection()->prepare($sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name); $statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'panel_id', var: $panelID);
$statement->bindParam(param: 'a', var: $a); $statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa); $statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->execute(); $statement->execute();
// recreate zonefile // recreate zonefile
if ($panel = $this->panelController->findByID($panelID)) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa); $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
exec(command: '/usr/sbin/rndc reload'); exec(command: '/usr/sbin/rndc reload');
@ -228,72 +210,6 @@ 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 * @return array|bool
@ -359,7 +275,5 @@ class DomainController
fputs($zonefile, data: "\t};" . PHP_EOL); fputs($zonefile, data: "\t};" . PHP_EOL);
fputs($zonefile, data: "};" . PHP_EOL); fputs($zonefile, data: "};" . PHP_EOL);
} }
// TODO check if ist exist in the include, else create
} }
} }

View File

@ -178,24 +178,4 @@ class NameserverController
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/**
* @param String $field
*
* @return int
*/
public function getLongestEntry(String $field): int
{
$statement = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_NAMESERVERS;
try {
$statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
} }

View File

@ -183,7 +183,7 @@ class PanelController
/** /**
* @param String $field * @param $id
* *
* @return int * @return int
*/ */