added SodiumException

This commit is contained in:
tracer 2022-10-08 10:57:56 +02:00
parent 28a9e4ac08
commit 6cca02b1cf
1 changed files with 231 additions and 223 deletions

View File

@ -6,6 +6,7 @@ use App\Controller\DatabaseConnection;
use App\Entity\Nameserver; use App\Entity\Nameserver;
use PDO; use PDO;
use PDOException; use PDOException;
use SodiumException;
/** /**
* *
@ -14,17 +15,18 @@ class NameserverRepository
{ {
public function __construct(private readonly DatabaseConnection $databaseConnection) public function __construct(private readonly DatabaseConnection $databaseConnection)
{ {
// no body
} }
/** /**
* @return array * @return array|null
*/ */
public function findAll(): array public function findAll(): ?array
{ {
$nameservers = []; $nameservers = [];
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey SELECT id, name, a, aaaa, apikey, apikey_prefix
FROM " . DatabaseConnection::TABLE_NAMESERVERS . " FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
ORDER BY name"; ORDER BY name";
@ -32,7 +34,7 @@ class NameserverRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$nameserver = new Nameserver(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']); $nameserver = new Nameserver(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix']);
$nameservers[] = $nameserver; $nameservers[] = $nameserver;
} }
return $nameservers; return $nameservers;
@ -43,13 +45,12 @@ class NameserverRepository
/** /**
* @return \App\Entity\Nameserver * @return Nameserver|null
*/ */
public function findFirst(): Nameserver public function findFirst(): ?Nameserver
{ {
$nameservers = [];
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey SELECT id, name, a, aaaa, apikey, apikey_prefix
FROM " . DatabaseConnection::TABLE_NAMESERVERS . " FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
ORDER BY name"; ORDER BY name";
@ -57,7 +58,7 @@ class NameserverRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC); $result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Nameserver(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']); return new Nameserver(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix']);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -67,12 +68,12 @@ class NameserverRepository
/** /**
* @param int $id * @param int $id
* *
* @return null|\App\Entity\Nameserver * @return null|Nameserver
*/ */
public function findByID(int $id): ?Nameserver public function findByID(int $id): ?Nameserver
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey SELECT id, name, a, aaaa, apikey, apikey_prefix
FROM . " . DatabaseConnection::TABLE_NAMESERVERS . " FROM . " . DatabaseConnection::TABLE_NAMESERVERS . "
WHERE id = :id"; WHERE id = :id";
@ -81,7 +82,7 @@ class NameserverRepository
$statement->bindParam(param: ':id', var: $id); $statement->bindParam(param: ':id', var: $id);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']); return new Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix']);
} else { } else {
return null; return null;
} }
@ -94,12 +95,12 @@ class NameserverRepository
/** /**
* @param String $name * @param String $name
* *
* @return \App\Entity\Nameserver|bool * @return Nameserver|null
*/ */
public function findByName(string $name): Nameserver|bool public function findByName(string $name): ?Nameserver
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey SELECT id, name, a, aaaa, apikey, apikey_prefix
FROM " . DatabaseConnection::TABLE_NAMESERVERS . " FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
WHERE name = :name"; WHERE name = :name";
@ -108,9 +109,9 @@ class NameserverRepository
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa']); return new Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix']);
} else { } else {
return false; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
@ -119,57 +120,55 @@ class NameserverRepository
/** /**
* @param \App\Entity\Nameserver $nameserver * @param Nameserver $nameserver
* *
* @return string|false * @return int|null
*/ */
public function insert(Nameserver $nameserver): bool|string public function insert(Nameserver $nameserver): ?int
{ {
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
try {
$name = $nameserver->getName(); $name = $nameserver->getName();
$a = $nameserver->getA(); $a = $nameserver->getA();
$aaaa = $nameserver->getAaaa(); $aaaa = $nameserver->getAaaa();
$apikey = $nameserver->getApikey(); $apikey = $nameserver->getApikey();
$apikeyPrefix = $nameserver->getApikeyPrefix();
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey, apikey_prefix)
VALUES (:name, :a, :aaaa, :apikey, :apikey_prefix)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$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->bindParam(param: ':apikey', var: $apikey); $statement->bindParam(param: ':apikey', var: $apikey);
$statement->bindParam(param: ':apikey_prefix', var: $apikeyPrefix);
$statement->execute(); $statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId(); return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage() . PHP_EOL);
} }
} }
/** /**
* @param Int $id * @param Nameserver $nameserver
* @param String $name
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return false|int * @return false|int
*/ */
public function update(int $id, string $name, string $a, string $aaaa, string $apikey): bool|int public function update(Nameserver $nameserver): bool|int
{ {
$id = $nameserver->getId();
$name = $nameserver->getName();
$a = $nameserver->getA();
$aaaa = $nameserver->getAaaa();
$apikey = $nameserver->getApikey();
$apikeyPrefix = $nameserver->getApikeyPrefix();
$passphrase = $nameserver->getPassphrase();
$current = $this->findByID(id: $id); $current = $this->findByID(id: $id);
/* doesn't work
$statement = "
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)) { if (empty($name)) {
$name = $current->getName(); $name = $current->getName();
@ -180,16 +179,20 @@ class NameserverRepository
if (empty($aaaa)) { if (empty($aaaa)) {
$aaaa = $current->getAaaa(); $aaaa = $current->getAaaa();
} }
if (empty($apikey)) {
if (empty($passphrase)) {
$apikey = $current->getApikey(); $apikey = $current->getApikey();
$apikeyPrefix = $current->getApikeyPrefix();
} }
$sql = " $sql = "
UPDATE " . DatabaseConnection::TABLE_NAMESERVERS . " SET UPDATE " . DatabaseConnection::TABLE_NAMESERVERS . " SET
name = :name, name = :name,
a = :a, a = :a,
aaaa = :aaaa, aaaa = :aaaa,
apikey = :apikey apikey = :apikey,
apikey_prefix = :apikey_prefix
WHERE id = :id"; WHERE id = :id";
try { try {
@ -199,9 +202,14 @@ class NameserverRepository
$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->bindParam(param: 'apikey', var: $apikey); $statement->bindParam(param: 'apikey', var: $apikey);
$statement->bindParam(param: 'apikey_prefix', var: $apikeyPrefix);
$statement->execute(); $statement->execute();
try {
return $statement->rowCount(); sodium_memzero(string: $apikey);
} catch(SodiumException $e) {
die($e->getMessage() . PHP_EOL);
}
return intval(value: $statement->rowCount());
} catch (PDOException $e) { } catch (PDOException $e) {
echo $e->getMessage(); echo $e->getMessage();
return false; return false;
@ -212,9 +220,9 @@ class NameserverRepository
/** /**
* @param $id * @param $id
* *
* @return int * @return int|null
*/ */
public function delete($id): int public function delete($id): ?int
{ {
$sql = " $sql = "
DELETE FROM " . DatabaseConnection::TABLE_NAMESERVERS . " DELETE FROM " . DatabaseConnection::TABLE_NAMESERVERS . "