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

View File

@ -15,7 +15,7 @@ class DomainController
private String $namedConfLocalFile;
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->localZonesDir = '/etc/bind/zones/';
@ -30,9 +30,8 @@ class DomainController
public function findAll(): bool|array
{
$statement = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name";
SELECT id, name, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS;
try {
$statement = $this->databaseConnection->getConnection()->query($statement);
@ -51,7 +50,7 @@ class DomainController
public function findByName(String $name): bool|array
{
$sql = "
SELECT id, name, panel_id, a, aaaa
SELECT id, name, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name";
@ -74,7 +73,7 @@ class DomainController
public function findByID(int $id): bool|array
{
$sql = "
SELECT id, name, panel_id, a, aaaa
SELECT id, name, a, aaaa
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
@ -91,37 +90,30 @@ class DomainController
/**
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
*
* @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
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)";
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, a, aaaa)
VALUES (:name, :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, mode: 'a')) {
if ($localZones = fopen($this->localZoneFile, 'a')) {
fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
fclose($localZones);
} else {
@ -133,19 +125,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, 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);
@ -162,10 +154,6 @@ 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'];
}
@ -176,7 +164,6 @@ class DomainController
$sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name,
panel_id = :panel_id,
a = :a,
aaaa = :aaaa
WHERE id = :id";
@ -185,16 +172,11 @@ 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');
@ -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
@ -359,7 +275,5 @@ class DomainController
fputs($zonefile, data: "\t};" . 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());
}
}
/**
* @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
*/