added missing named parameters, added strict_types

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-26 19:36:09 +01:00
parent 30180e6d71
commit 518c4ae90d
1 changed files with 41 additions and 35 deletions

View File

@ -1,7 +1,9 @@
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
use PDO; use PDO;
use PDOException; use PDOException;
@ -29,13 +31,14 @@ class DomainController
*/ */
public function findAll(): bool|array public function findAll(): bool|array
{ {
$statement = " $sql = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name"; ORDER BY name";
try { try {
$statement = $this->databaseConnection->getConnection()->query($statement); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC); return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
@ -56,10 +59,10 @@ class DomainController
WHERE name = :name"; WHERE name = :name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC); return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -79,10 +82,10 @@ class DomainController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param:':id', var: $id); $statement->bindParam(param:':id', var: $id);
$statement->execute(); $statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC); return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -95,9 +98,9 @@ class DomainController
* @param String $a * @param String $a
* @param String $aaaa * @param String $aaaa
* *
* @return int * @return string|false
*/ */
public function insert(String $name, int $panelID, String $a, String $aaaa): int public function insert(String $name, int $panelID, String $a, String $aaaa): bool|string
{ {
// TODO create zone file and include // TODO create zone file and include
$sql = " $sql = "
@ -105,7 +108,7 @@ class DomainController
VALUES (:name, :panel_id, :a, :aaaa)"; VALUES (:name, :panel_id, :a, :aaaa)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_d', var: $panelID); $statement->bindParam(param: ':panel_d', var: $panelID);
$statement->bindParam(param: ':a', var: $a); $statement->bindParam(param: ':a', var: $a);
@ -113,7 +116,7 @@ class DomainController
$statement->execute(); $statement->execute();
if ($panel = $this->panelController->findByID($panelID)) { if ($panel = $this->panelController->findByID(id: $panelID)) {
$a = $panel['a']; $a = $panel['a'];
$aaaa = $panel['aaaa']; $aaaa = $panel['aaaa'];
} }
@ -121,9 +124,9 @@ class DomainController
$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(filename: $this->localZoneFile, mode: 'a')) {
fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL); fputs(stream: $localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
fclose($localZones); fclose(stream: $localZones);
} else { } else {
echo "Error writing to $this->localZoneFile, check permissions"; echo "Error writing to $this->localZoneFile, check permissions";
exit(1); exit(1);
@ -147,7 +150,7 @@ class DomainController
*/ */
public function update(Int $id, String $name, int $panelID, 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); $current = $this->findByID(id: $id);
/* doesn't work /* doesn't work
$statement = " $statement = "
@ -182,7 +185,7 @@ class DomainController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $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: 'panel_id', var: $panelID);
@ -191,7 +194,7 @@ class DomainController
$statement->execute(); $statement->execute();
// recreate zonefile // recreate zonefile
if ($panel = $this->panelController->findByID($panelID)) { if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
$a = $panel['a']; $a = $panel['a'];
$aaaa = $panel['aaaa']; $aaaa = $panel['aaaa'];
} }
@ -214,12 +217,12 @@ class DomainController
public function delete($id): int public function delete($id): int
{ {
// TODO delete zone file and include // TODO delete zone file and include
$statement = " $sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($statement); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return $statement->rowCount();
@ -235,11 +238,11 @@ class DomainController
*/ */
public function getLongestEntry(String $field): int public function getLongestEntry(String $field): int
{ {
$statement = " $sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($statement); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];
@ -257,7 +260,7 @@ class DomainController
$uid = posix_geteuid(); $uid = posix_geteuid();
print("UID:\t$uid" . PHP_EOL); print("UID:\t$uid" . PHP_EOL);
$pwuid = posix_getpwuid($uid); $pwuid = posix_getpwuid(user_id: $uid);
$name = $pwuid['name']; $name = $pwuid['name'];
print("Name:\t$name" . PHP_EOL); print("Name:\t$name" . PHP_EOL);
$bindGroup = posix_getgrnam(name: 'bind'); $bindGroup = posix_getgrnam(name: 'bind');
@ -275,8 +278,8 @@ class DomainController
} }
echo "Checking $this->namedConfLocalFile" . PHP_EOL; echo "Checking $this->namedConfLocalFile" . PHP_EOL;
if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) { if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
if (!str_contains($namedConfLocal, $this->localZoneFile)) { if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
echo "\t$this->localZoneFile needs to be included in $this->namedConfLocalFile." . PHP_EOL; echo "\t$this->localZoneFile needs to be included in $this->namedConfLocalFile." . PHP_EOL;
} else { } else {
echo "\t$this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL; echo "\t$this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
@ -300,10 +303,12 @@ class DomainController
*/ */
function checkDomains(): array|bool function checkDomains(): array|bool
{ {
return true;
/*
$domains = $this->findAll(); $domains = $this->findAll();
if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) { if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
if (!str_contains($namedConfLocal, $this->localZoneFile)) { if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
return "$this->localZoneFile needs to be included in $this->namedConfLocalFile."; return "$this->localZoneFile needs to be included in $this->namedConfLocalFile.";
} }
} else { } else {
@ -333,6 +338,7 @@ class DomainController
} else { } else {
return $errors; return $errors;
} }
*/
} }
@ -346,18 +352,18 @@ class DomainController
public function createZoneFile(String $name, String $a, String $aaaa): void public function createZoneFile(String $name, String $a, String $aaaa): void
{ {
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) { if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
fputs($zonefile, data: "zone \"$name\" IN {" . PHP_EOL); fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
fputs($zonefile, data: "\ttype slave;" . PHP_EOL); fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
fputs($zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL); fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
fputs($zonefile, data: "\tmasters {" . PHP_EOL); fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
if (!empty($a)) { if (!empty($a)) {
fputs($zonefile, data: "\t\t$a;" . PHP_EOL); fputs(stream: $zonefile, data: "\t\t$a;" . PHP_EOL);
} }
if (!empty($aaaa)) { if (!empty($aaaa)) {
fputs($zonefile, data: "\t\t$aaaa;" . PHP_EOL); fputs(stream: $zonefile, data: "\t\t$aaaa;" . PHP_EOL);
} }
fputs($zonefile, data: "\t};" . PHP_EOL); fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
fputs($zonefile, data: "};" . PHP_EOL); fputs(stream: $zonefile, data: "};" . PHP_EOL);
} }
// TODO check if ist exist in the include, else create // TODO check if ist exist in the include, else create