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,249 +6,257 @@ use App\Controller\DatabaseConnection;
use App\Entity\Nameserver; use App\Entity\Nameserver;
use PDO; use PDO;
use PDOException; use PDOException;
use SodiumException;
/** /**
* *
*/ */
class NameserverRepository 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";
try { try {
$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;
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @return \App\Entity\Nameserver * @return Nameserver|null
*/ */
public function findFirst(): Nameserver public function findFirst(): ?Nameserver
{ {
$nameservers = []; $sql = "
$sql = " SELECT id, name, a, aaaa, apikey, apikey_prefix
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS . " FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
ORDER BY name"; ORDER BY name";
try { try {
$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());
} }
} }
/** /**
* @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";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$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;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @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";
try { 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->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());
} }
} }
/** /**
* @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 = " $name = $nameserver->getName();
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey) $a = $nameserver->getA();
VALUES (:name, :a, :aaaa, :apikey)"; $aaaa = $nameserver->getAaaa();
$apikey = $nameserver->getApikey();
try { $apikeyPrefix = $nameserver->getApikeyPrefix();
$name = $nameserver->getName();
$a = $nameserver->getA();
$aaaa = $nameserver->getAaaa();
$apikey = $nameserver->getApikey();
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->bindParam(param: ':apikey', var: $apikey);
$statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/** $sql = "
* @param Int $id INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey, apikey_prefix)
* @param String $name VALUES (:name, :a, :aaaa, :apikey, :apikey_prefix)";
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return false|int
*/
public function update(int $id, string $name, string $a, string $aaaa, string $apikey): bool|int
{
$current = $this->findByID(id: $id);
/* doesn't work try {
$statement = " $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
INSERT INTO domains(id, name, a, aaaa) $statement->bindParam(param: ':name', var: $name);
VALUES(:id, :name, :a, :aaaa) $statement->bindParam(param: ':a', var: $a);
ON DUPLICATE KEY UPDATE $statement->bindParam(param: ':aaaa', var: $aaaa);
name=COALESCE(VALUES(name), :name), $statement->bindParam(param: ':apikey', var: $apikey);
a=COALESCE(:a, a), $statement->bindParam(param: ':apikey_prefix', var: $apikeyPrefix);
aaaa=COALESCE(:aaaa, aaaa)"; $statement->execute();
*/
if (empty($name)) { return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
$name = $current->getName(); } catch (PDOException $e) {
} exit($e->getMessage() . PHP_EOL);
if (empty($a)) { }
$a = $current->getA(); }
}
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
}
if (empty($apikey)) {
$apikey = $current->getApikey();
}
$sql = "
/**
* @param Nameserver $nameserver
* @return false|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);
if (empty($name)) {
$name = $current->getName();
}
if (empty($a)) {
$a = $current->getA();
}
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
}
if (empty($passphrase)) {
$apikey = $current->getApikey();
$apikeyPrefix = $current->getApikeyPrefix();
}
$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 {
$statement = $this->databaseConnection->getConnection()->prepare(query: $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: '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->execute(); $statement->bindParam(param: 'apikey_prefix', var: $apikeyPrefix);
$statement->execute();
return $statement->rowCount(); try {
} catch (PDOException $e) { sodium_memzero(string: $apikey);
echo $e->getMessage(); } catch(SodiumException $e) {
return false; die($e->getMessage() . PHP_EOL);
} }
} return intval(value: $statement->rowCount());
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}
/** /**
* @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 . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $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->rowCount(); return $statement->rowCount();
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $field * @param String $field
* *
* @return int * @return int
*/ */
public function getLongestEntry(string $field): int public function getLongestEntry(string $field): int
{ {
$sql = " $sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_NAMESERVERS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_NAMESERVERS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
} }