Compare commits

..

24 Commits

Author SHA1 Message Date
tracer 21b4a412a8 made changes for panel object instead of array
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:41:08 +01:00
tracer 91c12e1855 added strict
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:40:29 +01:00
tracer 3f0c044c49 added annotations
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:40:01 +01:00
tracer e0e6c27c00 added annotations
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:39:46 +01:00
tracer 93142c9744 added strict, OA schema
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:39:23 +01:00
tracer 0104259187 added strict
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:39:01 +01:00
tracer 63cf544fc4 added strict
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:38:52 +01:00
tracer 6625f64a7d added strict
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:38:36 +01:00
tracer 2dc031dd66 added strict
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:38:27 +01:00
tracer 2a56aef004 ...
Signed-off-by: tracer <tracer@24unix.net>
2022-02-01 20:38:11 +01:00
tracer 8ea59843d2 ...
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 21:09:04 +01:00
tracer 4b3a2f0a20 initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 21:07:19 +01:00
tracer c7aff14f15 initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 21:06:44 +01:00
tracer 06cc5b0ec5 initial commit
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 21:05:14 +01:00
tracer dd83331bad made a fix needed for a bug in PhpStorm
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 21:02:32 +01:00
tracer aec1a3e125 moved PDO stuff to repository
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 21:01:00 +01:00
tracer 9f8d572244 finished use od DI
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 21:00:24 +01:00
tracer 545e09636c changed some variables
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 20:59:36 +01:00
tracer d83126ac79 renamed TABLE_USER to TABLE_APIKEYS
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 20:58:12 +01:00
tracer 37cf94bbe6 removed injection of nameservercontroller
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 20:56:28 +01:00
tracer 4b19963279 moved PDO stuff to repository
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 20:54:18 +01:00
tracer 7976c2387e moved body to repository
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 20:53:50 +01:00
tracer 276764b5f9 moved body to repository
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 20:53:27 +01:00
tracer 74f3f40adf fixed select statement
Signed-off-by: tracer <tracer@24unix.net>
2022-01-31 20:50:32 +01:00
18 changed files with 1622 additions and 1338 deletions

View File

@ -0,0 +1,2 @@
TODO:
~~When deleting domain, remove them from nameservers.~~

View File

@ -1,14 +1,15 @@
#!/usr/bin/keyhelp-php81
#!/usr/local/bin/php
<?php declare(strict_types=1);
namespace App\Controller;
//#!/usr/bin/keyhelp-php81
error_reporting(error_level: E_ALL);
// & ~E_DEPRECATED is needed because of a bug in PhpStorm
error_reporting(error_level: E_ALL & ~E_DEPRECATED);
if (php_sapi_name() !== 'cli') {
exit;
}
// version, store that somewhere else
$version = '0.0.1';

View File

@ -16,126 +16,5 @@ class ApiKeys
{}
/**
* @return array|false
*/
public function findAll(): bool|array
{
$sql = "
SELECT id, name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_USER;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
*
* @return array|false
*/
public function findByID(Int $id): bool|array
{
$sql = "
SELECT name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_USER . "
WHERE id = :id;
";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $prefix
*
* @return bool|array
*/
public function findByPrefix(String $prefix): bool|array
{
$sql = "
SELECT name, api_token
FROM " . DatabaseConnection::TABLE_USER . "
WHERE api_token_prefix = :prefix";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':prefix', var: $prefix);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} 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_USER . " (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 $id
*
* @return int
*/
public function delete($id): int
{
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_USER . "
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());
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -11,19 +11,21 @@ class CheckController
{
/**
* @param String $requestType
* @param String $serverName
* @param int $versionIP
* @param String $apiKey
* @param String $command
* @param String $type
* @param String $serverType
* @param array $body
*
* @return array
*/
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command, String $type, bool $post = false, array $body = []): array
function sendCommand(String $requestType, String $serverName, int $versionIP, String $apiKey, String $command, String $serverType, array $body = []): array
{
$error = false;
$curl = curl_init();
if ($type == "panel") {
if ($serverType == "panel") {
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
} else {
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command);
@ -40,10 +42,13 @@ class CheckController
curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
if ($post) {
curl_setopt($curl, option: CURLOPT_POST, value: true);
curl_setopt($curl, option: CURLOPT_POSTFIELDS, value: $body);
if ($requestType == "POST") {
curl_setopt(handle: $curl, option: CURLOPT_POST, value: true);
curl_setopt(handle: $curl, option: CURLOPT_POSTFIELDS, value: $body);
}
curl_setopt(handle: $curl, option: CURLOPT_CUSTOMREQUEST, value: $requestType);
if ($resultJSON = curl_exec(handle: $curl)) {
$httpResponse = curl_getinfo(handle: $curl)['http_code'];
@ -65,6 +70,7 @@ class CheckController
break;
case 404:
$result = '404 Not Found';
break;
default:
$result = 'Unhandled error: ' . $httpResponse;
}

View File

@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php
namespace App\Controller;
error_reporting(error_level: E_ALL);
@ -18,7 +18,7 @@ class DatabaseConnection
const TABLE_DOMAINS = self::TABLE_PREFIX . "domains";
const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers";
const TABLE_PANELS = self::TABLE_PREFIX . "panels";
const TABLE_USER = self::TABLE_PREFIX . "apikeys";
const TABLE_APIKEYS = self::TABLE_PREFIX . "apikeys";
public function __construct(private array $config)
{
@ -105,13 +105,16 @@ class DatabaseConnection
}
function generatePassword($length = 8) {
/**
* @param int $length
*
* @return string
*/
function generatePassword(int $length = 8): string
{
$chars = '23456789bcdfhkmnprstvzBCDFHJKLMNPRSTVZ';
$shuffled = str_shuffle($chars);
$result = mb_substr($shuffled, 0, $length);
return $result;
$shuffled = str_shuffle(string: $chars);
return mb_substr(string: $shuffled, start: 0, length: $length);
}
/**

View File

@ -1,23 +1,25 @@
<?php declare(strict_types=1);
namespace App\Controller;
use App\Repository\DomainRepository;
use App\Repository\NameserverRepository;
error_reporting(error_level: E_ALL);
use PDO;
use PDOException;
// TODO check include "/etc/bind/local.zones";
/**
*
*/
class DomainController
{
private String $localZoneFile;
private String $localZonesDir;
private String $namedConfLocalFile;
private string $localZoneFile;
private string $localZonesDir;
private string $namedConfLocalFile;
private string $zoneCachePath;
public function __construct(private DatabaseConnection $databaseConnection, private PanelController $panelController)
public function __construct(private NameserverRepository $nameserverRepository, private CheckController $checkController, private DomainRepository $domainRepository)
{
$this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/';
@ -25,102 +27,17 @@ class DomainController
$this->zoneCachePath = '/var/cache/bind/';
}
/**
* @return array|false
*/
public function findAll(): bool|array
{
$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();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/*
/**
* @param String $name
* @param mixed $a
* @param mixed $aaaa
*
* @return array|false
*/
public function findByName(String $name): bool|array
* @return void
public function createZone(string $name, mixed $a, mixed $aaaa): void
{
$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();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param int $id
*
* @return array|false
*/
public function findByID(int $id): bool|array
{
$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();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} 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();
print(PHP_EOL . "there");
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
/*
$zoneFilename = $this->localZonesDir . $name;
echo $zoneFilename . PHP_EOL;
@ -131,149 +48,79 @@ class DomainController
echo "Error writing to $this->localZoneFile, check permissions";
exit(1);
}
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();
// recreate zonefile
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
exec(command: '/usr/sbin/rndc reload');
return $statement->rowCount();
} catch (PDOException $e) {
print($e->getMessage());
return false;
}
}
function createIncludeFile()
{
$domains = $this->findAll();
$domains = $this->domainRepository->findAll();
print("$this->localZoneFile");
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
foreach ($domains as $domain) {
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain['name'] . '";' . PHP_EOL);
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain->getName() . '";' . PHP_EOL);
}
fclose(stream: $oFile);
}
/**
* @param $id
*
* @return int
*/
public function delete($id): int
function delete(int $id)
{
if ($domain = $this->findByID(id: $id)) {
if ($domain = $this->domainRepository->findByID(id: $id)) {
$this->domainRepository->delete(id: $id);
$zoneFile = $this->localZonesDir . $domain['name'];
print($zoneFile . PHP_EOL);
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
print("file exists");
unlink(filename: $zoneFile);
}
}
$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();
$this->createIncludeFile();
return $statement->rowCount();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
$this->deleteOnNameservers(id: $id);
}
function deleteOnNameservers(int $id)
{
$nameservers = $this->nameserverRepository->findAll();
foreach ($nameservers as $nameserver) {
echo($nameserver['name']);
$body = [
'id' => $id
];
if (!empty($nameserver['aaaa'])) {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], command: 'delete', serverType: 'nameserver', body: $body);
} else {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver['name'], versionIP: 4, apiKey: $nameserver['apikey'], command: 'delete', serverType: 'nameserver', body: $body);
}
}
}
/**
* @param String $field
* @param int $id
*
* @return int
* @return void
*/
public function getLongestEntry(String $field): int
function deleteZone(int $id)
{
$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());
if ($domain = $this->domainRepository->findByID(id: $id)) {
$zoneFile = $this->localZonesDir . $domain['name'];
print($zoneFile . PHP_EOL);
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
print("file exists");
unlink(filename: $zoneFile);
$this->createIncludeFile();
}
}
$this->deleteOnNameservers(id: $id);
$this->domainRepository->delete(id: $id);
}
/**
* @return void
*/
@ -292,10 +139,10 @@ class DomainController
echo "\t✅ is in group 'bind" . PHP_EOL;
}
echo 'Checking file: ' .$this->localZoneFile . PHP_EOL;
echo 'Checking file: ' . $this->localZoneFile . PHP_EOL;
$localZoneFilePermissions = fileperms(filename: $this->localZoneFile);
if ($localZoneFilePermissions & 0x0010) {
echo "\t✅ Group has write access." . PHP_EOL;
echo "\t✅ Group has write access . " . PHP_EOL;
} else {
echo "\t❌Group needs write permission!" . PHP_EOL;
}
@ -303,18 +150,18 @@ class DomainController
echo "Checking $this->namedConfLocalFile" . PHP_EOL;
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
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 {
echo "\t$this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
}
} else {
echo "\t❌ No access to '$this->namedConfLocalFile'. Please check permissions" . PHP_EOL;
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
}
echo 'Checking directory: ' . $this->localZonesDir . PHP_EOL;
$localZoneDirPermissions = fileperms(filename: $this->localZonesDir);
if ($localZoneDirPermissions & 0x0010) {
echo "\t✅ Group has write access." . PHP_EOL;
echo "\t✅ Group has write access . " . PHP_EOL;
} else {
echo "\t❌Group needs write permission!" . PHP_EOL;
}
@ -332,14 +179,14 @@ class DomainController
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
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 {
return "No access to '$this->namedConfLocalFile'. Please check permissions";
return "No access to '$this->namedConfLocalFile' . Please check permissions";
}
if (!fileperms($this->localZoneFile)) {
return "No access to $this->localZoneFile. Please check permissions.";
return "No access to $this->localZoneFile . Please check permissions . ";
}
$localZones = file_get_contents($this->localZoneFile);
@ -352,7 +199,7 @@ class DomainController
$zoneFile = $this->localZonesDir . $domain['name'];
if (!file_exists($zoneFile)) {
$errors[] = "Missing zone file for $zoneFile. Update zone to create it";
$errors[] = "Missing zone file for $zoneFile . Update zone to create it";
}
}
@ -372,7 +219,7 @@ class DomainController
*
* @return void
*/
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')) {
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
@ -388,7 +235,6 @@ class DomainController
fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
fputs(stream: $zonefile, data: "};" . PHP_EOL);
}
// TODO check if ist exist in the include, else create
$this->createIncludeFile();
}
}

View File

@ -3,8 +3,6 @@ namespace App\Controller;
error_reporting(error_level: E_ALL);
use PDO;
use PDOException;
/**
*
@ -16,189 +14,5 @@ class NameserverController
{}
/**
* @return array|false
*/
public function findAll(): bool|array
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
*
* @return array|false
*/
public function findByName(String $name): bool|array
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
WHERE name = :name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
*
* @return array|false
*/
public function findByID(Int $id): bool|array
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param:':id', var: $id);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return string|false
*/
public function insert(String $name, String $a, String $aaaa, String $apikey): bool|string
{
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
try {
$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());
}
}
/**
* @param Int $id
* @param String $name
* @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);
if (empty($name)) {
$name = $current['name'] ?? '';
}
if (empty($a)) {
$a = $current['a'] ?? '';
}
if (empty($aaaa)) {
$aaaa = $current['aaaa'] ?? '';
}
if (empty($apikey)) {
$apikey = $current['apikey'] ?? '';
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_NAMESERVERS . " SET
name = :name,
a = :a,
aaaa = :aaaa,
apikey = :apikey
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: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey);
$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_NAMESERVERS . "
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_NAMESERVERS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}

View File

@ -4,9 +4,6 @@ namespace App\Controller;
error_reporting(error_level: E_ALL);
use PDO;
use PDOException;
/**
*
@ -18,190 +15,5 @@ class PanelController
{}
/**
* @return array|false
*/
public function findAll(): bool|array
{
$statement = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_PANELS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
*
* @return array|false
*/
public function findByName(String $name): bool|array
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
*
* @return array|false
*/
public function findByID(Int $id): bool|array
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM ". DatabaseConnection::TABLE_PANELS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param:':id', var: $id);
$statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return string|false
*/
public function insert(String $name, String $a, String $aaaa, String $apikey): bool|string
{
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
try {
$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());
}
}
/**
* @param Int $id
* @param String $name
* @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);
if (empty($name)) {
$name = $current['name'];
}
if (empty($a)) {
$a = $current['a'] ?? '';
}
if (empty($aaaa)) {
$aaaa = $current['aaaa'] ?? '';
}
if (empty($apikey)) {
$apikey = $current['apikey'] ?? '';
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name,
a = :a,
aaaa = :aaaa,
apikey = :apikey
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: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey);
$statement->execute();
return $statement->rowCount();
} catch (PDOException $e) {
print($e->getMessage());
return false;
}
}
/**
* @param $id
*
* @return int
*/
public function delete($id): int
{
$statement = "
DELETE FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement);
$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
{
$statement = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}

View File

@ -8,12 +8,17 @@ use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository;
use DI\Container;
use DI\ContainerBuilder;
use OpenApi\Annotations\ServerVariable;
use OpenApi\Annotations\Tag;
use OpenApi\Generator;
use UnhandledMatchError;
use function DI\autowire;
use OpenApi\Attributes as OAT;
/**
*
*/
#[OAT\Info(version: '0.0.1', title: 'bindAPI' )]
class RequestController
{
//private DatabaseConnection $databaseConnection;
@ -49,16 +54,120 @@ class RequestController
/**
* @OA\Server(
* url = "https://ns2.24unix.net/api"
* )
* @OA\Tag(name = "Server")
* @OA\Get(
* path = "/ping",
* summary = "Returning pong.",
* description = "Can be used to check API or server availability.",
* tags={"Server"},
* @OA\Response(response = "200", description = "OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* security={
* {"Authorization":{"read"}}
* }
* )
*
* @OA\SecurityScheme (name="bindAPISecurity",
* type="apiKey",
* description="description",
* name="X-API-Key",
* in="header",
* securityScheme="Authorization"
*
* )
* @SwaggerDefinition(
* securityDefinition = @SecurityDefinition(
* apiKeyAuthDefinitions = {
* @ApiKeyAuthDefinition(
* key = "X-API-Key", in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, name = "X-API-KEY"
* )
* }
* )
* )
* @OA\Tag(name = "Domains")
* @OA\Get(
* path="/domains",
* summary="Listing all domains.",
* description="desc",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Post(
* path="/domains",
* summary="Create a domain.",
* description="Creates a new domain.",
* tags={"Domains"},
* @OA\Response(response="201", description="Created"),
* @OA\Response(response = "400", description = "Invalid request body."),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Get(
* path="/domains/{name}",
* summary="Returns a single domain.",
* description="Returns information of a single domain specified by its domain name.",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Put(
* path="/domains/{name}",
* summary="Updates a domain.",
* description="Updates a domain. Only supplied fields will be updated, existing won't be affected.",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Delete (
* path="/domains/{name}",
* summary="Deletes a domain.",
* description="Deletes a domain.",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
*
* @return void
*/
public function processRequest()
{
if (empty($this->uri[2]) || !(($this->uri[2] == 'domains') || $this->uri[2] == 'ping')) {
$command = $this->uri[2];
if (empty($command) || !(($command == 'domains') || ($command == 'ping') || ($command == 'apidoc'))) {
$this->header = '404 Not Found';
$this->status = "404 Not Found";
$this->message = "Endpoint not found.";
} else {
if ($command == 'apidoc') {
$openapi = Generator::scan(sources: [__DIR__ . 'RequestController.php']);
$this->status = 'openapi';
$this->result[] = $openapi->toJson();
} else {
if ($this->checkPassword()) {
if ($this->uri[2] == "ping") {
$this->header = '200 OK';
$this->status = 'pong';
@ -83,7 +192,12 @@ class RequestController
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
}
if (!empty($this->result)) {
if (!empty($this->status) && $this->status == 'openapi') {
header(header: 'Content-Type: application/json');
echo $this->result[0];
} else {
echo json_encode(value: $this->result);
}
} else {
if (!empty($this->status) && $this->status == 'pong') {
echo json_encode(value: [
@ -97,6 +211,7 @@ class RequestController
}
}
}
}
/**
@ -173,7 +288,8 @@ class RequestController
/**
* @return void
*/
public function handleDomainPostRequest(): void
public
function handleDomainPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0);
@ -206,7 +322,8 @@ class RequestController
/**
* @return void
*/
public function handleDomainPutRequest(): void
public
function handleDomainPutRequest(): void
{
$putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 512);
@ -249,10 +366,12 @@ class RequestController
}
}
/**
* @return void
*/
public function handleDomainDeleteRequest(): void
public
function handleDomainDeleteRequest(): void
{
$deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512);

View File

@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace App\Entity;

107
src/Entity/Domain.php Normal file
View File

@ -0,0 +1,107 @@
<?php declare(strict_types=1);
namespace App\Entity;
use OpenApi\Attributes as OAT;
/**
*
*/
class Domain
{
private int $id;
private int $panelID;
private String $name;
private String $a;
private String $aaaa;
public function __construct(String $name, int $id = 0, int $panelID = 0, String $a = '', String $aaaa = '')
{
$this->id = $id;
$this->panelID = $panelID;
$this->name = $name;
$this->a = $a;
$this->aaaa = $aaaa;
}
/**
* @return String
*/
public function getA(): string
{
return $this->a;
}
/**
* @return String
*/
public function getAaaa(): string
{
return $this->aaaa;
}
/**
* @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;
}
/**
* @return int
*/
public function getPanelID(): int
{
return $this->panelID;
}
/**
* @param int $panelID
*/
public function setPanelID(int $panelID): void
{
$this->panelID = $panelID;
}
/**
* @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

@ -1,10 +1,14 @@
<?php
<?php declare(strict_types=1);
namespace App\Entity;
use OpenApi\Attributes as OAT;
/**
*
*/
#[OAT\Schema(schema: 'nameserver')]
class Nameserver
{
private int $id;
@ -26,30 +30,58 @@ class Nameserver
/**
* @return String
*/
#[OAT\Property(type: 'string')]
public function getA(): string
{
return $this->a;
}
/**
* @param String $a
*/
public function setA(string $a): void
{
$this->a = $a;
}
/**
* @return String
*/
#[OAT\Property(type: 'string')]
public function getAaaa(): string
{
return $this->aaaa;
}
/**
* @param String $aaaa
*/
public function setAaaa(string $aaaa): void
{
$this->aaaa = $aaaa;
}
/**
* @return String
*/
#[OAT\Property(type: 'string')]
public function getApikey(): string
{
return $this->apikey;
}
/**
* @param String $apikey
*/
public function setApikey(string $apikey): void
{
$this->apikey = $apikey;
}
/**
* @return int
*/
#[OAT\Property(type: 'int')]
public function getId(): int
{
return $this->id;
@ -66,21 +98,12 @@ class Nameserver
/**
* @return String
*/
#[OAT\Property(type: 'string')]
public function getName(): string
{
return $this->name;
}
/**
* @param String $apikey
*/
public function setApikey(string $apikey): void
{
$this->apikey = $apikey;
}
/**
* @param String $name
*/
@ -89,20 +112,4 @@ class Nameserver
$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

@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace App\Entity;

View File

@ -81,7 +81,7 @@ class ApikeyRepository
public function findByPrefix(String $prefix): Apikey|bool
{
$sql = "
SELECT name, api_token
SELECT id, name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE api_token_prefix = :prefix";
@ -90,7 +90,7 @@ class ApikeyRepository
$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']);
return new Apikey(name: $result['name'], apiTokenPrefix: $result['api_token_prefix'], apiToken: $result['api_token'], id: $result['id']);
} else {
return false;
}

View File

@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace App\Repository;

View File

@ -0,0 +1,227 @@
<?php declare(strict_types=1);
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Entity\Nameserver;
use PDO;
use PDOException;
/**
*
*/
class NameserverRepository
{
public function __construct(private DatabaseConnection $databaseConnection)
{}
/**
* @return array
*/
public function findAll(): array
{
$nameservers = [];
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
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']);
$nameservers[] = $nameserver;
}
return $nameservers;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param int $id
*
* @return \App\Entity\Nameserver
*/
public function findByID(int $id): Nameserver
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM . " . DatabaseConnection::TABLE_NAMESERVERS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
*
* @return \App\Entity\Nameserver|bool
*/
public function findByName(string $name): Nameserver|bool
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
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 Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa']);
} else {
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return string|false
*/
public function insert(string $name, string $a, string $aaaa, String $apikey): bool|string
{
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
try {
$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());
}
}
/**
* @param Int $id
* @param String $name
* @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
$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->getName();
}
if (empty($a)) {
$a = $current->getA();
}
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
}
if (empty($apikey)) {
$apikey = $current->getApikey();
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_NAMESERVERS . " SET
name = :name,
a = :a,
aaaa = :aaaa,
apikey = :apikey
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: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey);
$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_NAMESERVERS . "
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_NAMESERVERS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}

View File

@ -0,0 +1,217 @@
<?php declare(strict_types=1);
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Entity\Panel;
use PDO;
use PDOException;
/**
*
*/
class PanelRepository
{
public function __construct(private DatabaseConnection $databaseConnection)
{}
/**
* @return array
*/
public function findAll(): array
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
$panels[] = $panel;
}
return $panels;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param int $id
*
* @return \App\Entity\Panel
*/
public function findByID(int $id): Panel
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
*
* @return \App\Entity\Panel|bool
*/
public function findByName(string $name): Panel|bool
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_PANELS . "
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 Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa']);
} else {
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return string|false
*/
public function insert(string $name, string $a, string $aaaa, String $apikey): bool|string
{
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
try {
$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());
}
}
/**
* @param Int $id
* @param String $name
* @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);
if (empty($name)) {
$name = $current->getName();
}
if (empty($a)) {
$a = $current->getA();
}
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
}
if (empty($apikey)) {
$apikey = $current->getApikey();
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name,
a = :a,
aaaa = :aaaa,
apikey = :apikey
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: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey);
$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_PANELS . "
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_PANELS;
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}