Compare commits

...

11 Commits

Author SHA1 Message Date
tracer 36e376337e fixed some getters
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 19:18:02 +01:00
tracer 8c2da0b9d8 fixed some getters
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 19:17:47 +01:00
tracer e9fbb8879a removed injection of panel
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 19:17:01 +01:00
tracer 087db2b2a9 handled case domain not found, also in findbyid
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 19:14:23 +01:00
tracer 1052e00cf5 handled case domain not found, also in findbyid
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 19:12:17 +01:00
tracer d1e67fce03 handled case domain not found
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 19:10:51 +01:00
tracer 574b960ed1 initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 19:09:20 +01:00
tracer 31944984f9 initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 14:48:03 +01:00
tracer ea8c49a49b initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 14:42:17 +01:00
tracer 88b3c5b357 initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 14:40:47 +01:00
tracer c297eaad81 initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 14:33:27 +01:00
5 changed files with 732 additions and 0 deletions

90
src/Entity/Apikey.php Normal file
View File

@ -0,0 +1,90 @@
<?php
namespace App\Entity;
/**
*
*/
class Apikey
{
private int $id;
private string $name;
private string $apiTokenPrefix;
private string $apiToken;
public function __construct(string $name, string $apiTokenPrefix, string $apiToken, int $id = 0)
{
$this->id = $id;
$this->name = $name;
$this->apiTokenPrefix = $apiTokenPrefix;
$this->apiToken = $apiToken;
}
/**
* @return String
*/
public function getApiToken(): string
{
return $this->apiToken;
}
/**
* @return string
*/
public function getApiTokenPrefix(): string
{
return $this->apiTokenPrefix;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return String
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $apiTokenPrefix
*/
public function setApiTokenPrefix(string $apiTokenPrefix): void
{
$this->apiTokenPrefix = $apiTokenPrefix;
}
/**
* @param String $apiToken
*/
public function setApiToken(string $apiToken): void
{
$this->apiToken = $apiToken;
}
/**
* @param String $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
}

108
src/Entity/Nameserver.php Normal file
View File

@ -0,0 +1,108 @@
<?php
namespace App\Entity;
/**
*
*/
class Nameserver
{
private int $id;
private String $name;
private String $a;
private String $aaaa;
private String $apikey;
public function __construct(String $name, int $id = 0, String $a = '', String $aaaa = '', String $apikey = '')
{
$this->id = $id;
$this->name = $name;
$this->a = $a;
$this->aaaa = $aaaa;
$this->apikey = $apikey;
}
/**
* @return String
*/
public function getA(): string
{
return $this->a;
}
/**
* @return String
*/
public function getAaaa(): string
{
return $this->aaaa;
}
/**
* @return String
*/
public function getApikey(): string
{
return $this->apikey;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return String
*/
public function getName(): string
{
return $this->name;
}
/**
* @param String $apikey
*/
public function setApikey(string $apikey): void
{
$this->apikey = $apikey;
}
/**
* @param String $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @param String $a
*/
public function setA(string $a): void
{
$this->a = $a;
}
/**
* @param String $aaaa
*/
public function setAaaa(string $aaaa): void
{
$this->aaaa = $aaaa;
}
}

108
src/Entity/Panel.php Normal file
View File

@ -0,0 +1,108 @@
<?php
namespace App\Entity;
/**
*
*/
class Panel
{
private int $id;
private String $name;
private String $a;
private String $aaaa;
private String $apikey;
public function __construct(String $name, int $id = 0, String $a = '', String $aaaa = '', String $apikey = '')
{
$this->id = $id;
$this->name = $name;
$this->a = $a;
$this->aaaa = $aaaa;
$this->apikey = $apikey;
}
/**
* @return String
*/
public function getA(): string
{
return $this->a;
}
/**
* @return String
*/
public function getAaaa(): string
{
return $this->aaaa;
}
/**
* @return String
*/
public function getApikey(): string
{
return $this->apikey;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return String
*/
public function getName(): string
{
return $this->name;
}
/**
* @param String $apikey
*/
public function setApikey(string $apikey): void
{
$this->apikey = $apikey;
}
/**
* @param String $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @param String $a
*/
public function setA(string $a): void
{
$this->a = $a;
}
/**
* @param String $aaaa
*/
public function setAaaa(string $aaaa): void
{
$this->aaaa = $aaaa;
}
}

View File

@ -0,0 +1,191 @@
<?php declare(strict_types=1);
namespace App\Repository;
error_reporting(error_level: E_ALL);
use App\Controller\DatabaseConnection;
use App\Controller\PanelController;
use App\Entity\Apikey;
use Exception;
use PDO;
use PDOException;
/**
*
*/
class ApikeyRepository
{
public function __construct(private DatabaseConnection $databaseConnection, PanelController $panelController)
{}
/**
* @return array|false
*/
public function findAll(): bool|array
{
$sql = "
SELECT id, name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_APIKEYS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$apikeys = [];
while ($result = $statement->fetch()) {
$apikey = new Apikey(name: $result['name'], apiTokenPrefix: $result['api_token_prefix'], apiToken: $result['api_token'], id: $result['id']);
$apikeys[] = $apikey;
}
return $apikeys;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
*
* @return \App\Entity\Apikey|bool
*/
public function findByID(Int $id): Apikey|bool
{
$sql = "
SELECT id, name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE id = :id;
";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Apikey(name: $result['name'], apiTokenPrefix: $result['api_token_prefix'], apiToken: $result['api_token'], id: $result['id']);
} else {
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $prefix
*
* @return \App\Entity\Apikey|bool
*/
public function findByPrefix(String $prefix): Apikey|bool
{
$sql = "
SELECT name, api_token
FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE api_token_prefix = :prefix";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':prefix', var: $prefix);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Apikey(name: $result['name'], apiTokenPrefix: $result['api_token_prefix'], apiToken: $result['api_token_result'], id: $result['id']);
} else {
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @return array|void
*/
public function create(String $name = '')
{
$tokenPrefix = uniqid();
$result['tokenPrefix'] = $tokenPrefix;
try {
$key = bin2hex(string: random_bytes(length: 24));
$result['key'] = $key;
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
}
$token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID);
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_APIKEYS . " (name, api_token_prefix, api_token)
VALUES (:name, :token_prefix, :token)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':token_prefix', var: $tokenPrefix);
$statement->bindParam(param: ':token', var: $token);
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
$result['row'] = $this->databaseConnection->getConnection()->lastInsertId();
return $result;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
* @param String $name
*
* @return false|int
*/
public function update(int $id, string $name): bool|int
{
$current = $this->findByID(id: $id);
if (empty($name)) {
$name = $current['name'];
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_APIKEYS . " SET
name = :name
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name);
$statement->execute();
return $statement->rowCount();
} catch (PDOException $e) {
print($e->getMessage());
return false;
}
}
/**
* @param $id
*
* @return int
*/
public function delete($id): int
{
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->execute();
return $statement->rowCount();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}

View File

@ -0,0 +1,235 @@
<?php
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use PDO;
use PDOException;
/**
*
*/
class DomainRepository
{
public function __construct(private DatabaseConnection $databaseConnection)
{}
/**
* @return array
*/
public function findAll(): array
{
$domains = [];
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$domain = new Domain(name: $result['name'], id: $result['id'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
$domains[] = $domain;
}
return $domains;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param int $id
*
* @return bool|\App\Entity\Domain
*/
public function findByID(int $id): bool|Domain
{
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
} else {
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
*
* @return \App\Entity\Domain|bool
*/
public function findByName(string $name): Domain|bool
{
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
} else {
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
*
* @return string|false
*/
public function insert(string $name, int $panelID, string $a, string $aaaa): bool|string
{
print("here");
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$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();
return $this->databaseConnection->getConnection()->lastInsertId();
} 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
{
$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)) {
$name = $current['name'];
}
if (empty($panelID)) {
$panelID = $current['panel_id'];
}
$panelID = intval(value: $panelID);
if (empty($a)) {
$a = $current['a'];
}
if (empty($aaaa)) {
$aaaa = $current['aaaa'];
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name,
panel_id = :panel_id,
a = :a,
aaaa = :aaaa
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $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();
return $statement->rowCount();
} catch (PDOException $e) {
print($e->getMessage());
return false;
}
}
/**
* @param $id
*
* @return int
*/
public function delete($id): int
{
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->execute();
return $statement->rowCount();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $field
*
* @return int
*/
public function getLongestEntry(String $field): int
{
$sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}