added SodiumException
This commit is contained in:
parent
28a9e4ac08
commit
6cca02b1cf
|
@ -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 = [];
|
{
|
||||||
$sql = "
|
$nameservers = [];
|
||||||
SELECT id, name, a, aaaa, apikey
|
$sql = "
|
||||||
|
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();
|
$sql = "
|
||||||
$apikey = $nameserver->getApikey();
|
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey, apikey_prefix)
|
||||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
VALUES (:name, :a, :aaaa, :apikey, :apikey_prefix)";
|
||||||
$statement->bindParam(param: ':name', var: $name);
|
|
||||||
$statement->bindParam(param: ':a', var: $a);
|
try {
|
||||||
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||||
$statement->bindParam(param: ':apikey', var: $apikey);
|
$statement->bindParam(param: ':name', var: $name);
|
||||||
$statement->execute();
|
$statement->bindParam(param: ':a', var: $a);
|
||||||
|
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
||||||
return $this->databaseConnection->getConnection()->lastInsertId();
|
$statement->bindParam(param: ':apikey', var: $apikey);
|
||||||
} catch (PDOException $e) {
|
$statement->bindParam(param: ':apikey_prefix', var: $apikeyPrefix);
|
||||||
exit($e->getMessage());
|
$statement->execute();
|
||||||
}
|
|
||||||
}
|
return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
exit($e->getMessage() . PHP_EOL);
|
||||||
/**
|
}
|
||||||
* @param Int $id
|
}
|
||||||
* @param String $name
|
|
||||||
* @param String $a
|
|
||||||
* @param String $aaaa
|
/**
|
||||||
* @param String $apikey
|
* @param Nameserver $nameserver
|
||||||
*
|
* @return false|int
|
||||||
* @return false|int
|
*/
|
||||||
*/
|
public function update(Nameserver $nameserver): bool|int
|
||||||
public function update(int $id, string $name, string $a, string $aaaa, string $apikey): bool|int
|
{
|
||||||
{
|
$id = $nameserver->getId();
|
||||||
$current = $this->findByID(id: $id);
|
$name = $nameserver->getName();
|
||||||
|
$a = $nameserver->getA();
|
||||||
/* doesn't work
|
$aaaa = $nameserver->getAaaa();
|
||||||
$statement = "
|
$apikey = $nameserver->getApikey();
|
||||||
INSERT INTO domains(id, name, a, aaaa)
|
$apikeyPrefix = $nameserver->getApikeyPrefix();
|
||||||
VALUES(:id, :name, :a, :aaaa)
|
$passphrase = $nameserver->getPassphrase();
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
name=COALESCE(VALUES(name), :name),
|
$current = $this->findByID(id: $id);
|
||||||
a=COALESCE(:a, a),
|
|
||||||
aaaa=COALESCE(:aaaa, aaaa)";
|
|
||||||
*/
|
if (empty($name)) {
|
||||||
|
$name = $current->getName();
|
||||||
if (empty($name)) {
|
}
|
||||||
$name = $current->getName();
|
if (empty($a)) {
|
||||||
}
|
$a = $current->getA();
|
||||||
if (empty($a)) {
|
}
|
||||||
$a = $current->getA();
|
if (empty($aaaa)) {
|
||||||
}
|
$aaaa = $current->getAaaa();
|
||||||
if (empty($aaaa)) {
|
}
|
||||||
$aaaa = $current->getAaaa();
|
|
||||||
}
|
if (empty($passphrase)) {
|
||||||
if (empty($apikey)) {
|
$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 {
|
||||||
$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
|
}
|
||||||
*
|
}
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function delete($id): int
|
/**
|
||||||
{
|
* @param $id
|
||||||
$sql = "
|
*
|
||||||
|
* @return int|null
|
||||||
|
*/
|
||||||
|
public function delete($id): ?int
|
||||||
|
{
|
||||||
|
$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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue