Compare commits

..

10 Commits

Author SHA1 Message Date
tracer cbb428d417 more text …
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 15:59:38 +01:00
tracer 4d22d81de9 added debug option
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 15:59:16 +01:00
tracer 5b0ba2bd09 added response time
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 15:32:59 +01:00
tracer 18112b7937 chenged from content to panel
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 15:32:23 +01:00
tracer 8c7755e096 changed from content to panel
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 15:31:49 +01:00
tracer 1ad945fbf2 added 201 header
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 14:06:35 +01:00
tracer 1369cab7fb changed from content to panel
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 14:06:07 +01:00
tracer 440aea8d61 changed from content to panel
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 14:05:41 +01:00
tracer fe1270f3d0 added new create statements
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 13:59:33 +01:00
tracer b3ce1d9dbf added curl timing
Signed-off-by: tracer <tracer@24unix.net>
2022-02-22 13:57:19 +01:00
9 changed files with 356 additions and 341 deletions

View File

@ -335,7 +335,7 @@ ns1.24unix.net 176.9.165.128 pong 2a01:4f8:161:12cd::128 pong
ns2.24unix.net 37.120.185.117 pong 2a03:4000:f:5e2:a80c:2dff:fed1:e109 pong
```
Now we can add all out panels:
Now we can add all our panels:
```
$ ./bin/console panels:create executor.24unix.net a=176.9.165.128 aaaa=2a01:4f8:161:12cd::128 apikey=Lo7jsXYQ.[truncated]
@ -373,7 +373,7 @@ shadow.24unix.net 37.120.185.117 pong 2a03:4000:f:5e2:a80c:2dff:fed1:e109 p
tector.24unix.net 176.9.165.137 pong 2a01:4f8:161:12cd::137 pong
```
OK, Nameservers ✅
OK, Nameservers ✅
Panels ✅
What's next? Check the domains on each panel:
@ -446,6 +446,7 @@ tfunix has to be a group member of the „bind“ group.
As root perform:
`usermod -G bind tfunix`
(Mind that it is a capital G, else you'll change the primary group and the checks will fail.
Don't ask how I noticed …)
@ -478,15 +479,46 @@ Checking permission:
UID: 5001
Name: tfunix
✅ is in group 'bind
Checking file: /etc/bind/local.zones
✅ Group has write access .
Checking /etc/bind/named.conf.local
✅ /etc/bind/local.zones is included in /etc/bind/named.conf.local
Checking directory: /etc/bind/zones/
✅ Group has write access .
```
Now, there is one manual step left, for now.
We have to go to each panel and add an include file.
BUT: This is a temporary workaround, a later version of Keyhelp will enable us to automate this step.
So, execute ./bin/console check:showincludes:
```
./bin/console check:showincludes
You need to add these lines to /etc/bind/local.bindapi.options and make sure
that include "/etc/bind/local.bindapi.options"; exists in /etc/bind/named.conf.options.
allow-transfer {
176.9.165.128;
2a01:4f8:161:12cd::128;
37.120.185.117;
2a03:4000:f:5e2:a80c:2dff:fed1:e109;
212.227.160.159;
2001:8d8:1801:701::1;
};
also-notify {
176.9.165.128;
2a01:4f8:161:12cd::128;
37.120.185.117;
2a03:4000:f:5e2:a80c:2dff:fed1:e109;
212.227.160.159;
2001:8d8:1801:701::1;
};
After the modification feel free to run named-checkconf to ensure there were no errors.
```
TODO

View File

@ -3,5 +3,6 @@
"dbPort": 3306,
"dbDatabase": "sampledb",
"dbUser": "sampleuser",
"dbPassword": "secret"
"dbPassword": "secret",
"debug": false
}

View File

@ -41,7 +41,7 @@ class ApiController
}
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 3000);
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 19999);
curl_setopt(handle: $curl, option: CURLOPT_HTTP_VERSION, value: CURL_HTTP_VERSION_2TLS);
if ($versionIP == 4) {
@ -91,11 +91,16 @@ class ApiController
$error = true;
$result = curl_error(handle: $curl);
}
$info = curl_getinfo(handle: $curl);
$responseTime = $info['total_time'];
curl_close(handle: $curl);
return [
'error' => $error,
'data' => $result,
'header' => $httpResponse ?? ''
'responseTime' => $responseTime,
'error' => $error,
'data' => $result,
'header' => $httpResponse ?? ''
];
}

View File

@ -145,6 +145,8 @@ class BindAPI
echo COLOR_GREEN . "\t check:permissions" . PHP_EOL;
echo COLOR_GREEN . "\t check:panels {ID} {fix=yes}" . PHP_EOL;
echo COLOR_GREEN . "\t check:domains {ID} {fix=yes}" . PHP_EOL;
echo COLOR_GREEN . "\t check:showinclude" . COLOR_DEFAULT . " Temporary needed until KeyHelp 22.1" . PHP_EOL;
echo COLOR_YELLOW . "panels" . COLOR_DEFAULT . "\t all Keyhelp systems configured" . PHP_EOL;
echo COLOR_GREEN . "\t panels:list" . PHP_EOL;
@ -185,6 +187,7 @@ class BindAPI
match ($subcommand) {
'permissions' => $this->handleCheckPermissions(),
'panels' => $this->handleCheckPanels(),
'showincludes' => $this->showIncludes(),
};
} catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL;
@ -245,16 +248,59 @@ class BindAPI
$this->log->debug(message: "checkSinglePanel()");
}
echo COLOR_DEFAULT . 'Keyhelp-Panel: ' . COLOR_YELLOW . $panel->getName() . PHP_EOL;
if (!empty($panel->getAaaa())) {
echo COLOR_DEFAULT . 'Keyhelp-Panel: ' . COLOR_YELLOW . $panel->getName();
if ($this->config['verbose']) {
if (empty($panel->getA())) {
try {
$panelRequest = $this->apiController->sendCommand(
requestType: 'GET',
serverName : $panel->getName(),
versionIP : 6,
apiKey : $panel->getApikey(),
command : '/server',
serverType : 'panel');
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
exit(1);
}
} else {
try {
$panelRequest = $this->apiController->sendCommand(
requestType: 'GET',
serverName : $panel->getName(),
versionIP : 4,
apiKey : $panel->getApikey(),
command : '/server',
serverType : 'panel');
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
exit();
}
}
$panelData = json_decode(json: $panelRequest['data']);
if (!empty($panelData)) {
$panelVersion = $panelData->meta->panel_version;
$responseTime = sprintf("%0.3f", $panelRequest['responseTime']);
} else {
$panelVersion = 'n/a';
$responseTime = 'n/a';
}
echo COLOR_DEFAULT . ' KeyHelp version: ' . $panelVersion . " ($responseTime seconds)" . PHP_EOL;
} else {
echo PHP_EOL;
}
if (empty($panel->getA())) {
try {
$result = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 6,
apiKey: $panel->getApikey(),
command: 'domains',
serverType: 'panel'
serverName : $panel->getName(),
versionIP : 6,
apiKey : $panel->getApikey(),
command : 'domains',
serverType : 'panel'
);
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
@ -263,16 +309,17 @@ class BindAPI
try {
$result = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 4,
apiKey: $panel->getApikey(),
command: 'domains',
serverType: 'panel');
serverName : $panel->getName(),
versionIP : 4,
apiKey : $panel->getApikey(),
command : 'domains',
serverType : 'panel');
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
}
}
if (!empty($result['error'])) {
echo $result['data'] . PHP_EOL;
exit(1);
@ -295,7 +342,7 @@ class BindAPI
$domainCount = 0;
foreach ($domains as $domain) {
if ($this->isValidSecondLevelDomain(domainName: $domain->domain, panel: $panel->getName(), parent: $domain->id_parent_domain)) {
echo COLOR_DEFAULT . "Domain: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName);
echo COLOR_DEFAULT . " Domain: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName);
try {
$this->checkNS(domainName: $domain->domain, panel: $panel);
} catch (DependencyException|NotFoundException $e) {
@ -306,9 +353,9 @@ class BindAPI
}
}
if ($domainCount == 0) {
echo 'No second level domains found.' . PHP_EOL;
echo 'No second level domains found.' . COLOR_DEFAULT . PHP_EOL;
}
echo PHP_EOL;
}
function isValidSecondLevelDomain(string $domainName, string $panel, int $parent): bool
@ -371,19 +418,19 @@ class BindAPI
if (!empty($nameServer->getName())) {
$result = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $nameServer->getName(),
versionIP: 6,
apiKey: $nameServer->getApikey(),
command: 'domains/name/' . $domainName,
serverType: 'nameserver');
serverName : $nameServer->getName(),
versionIP : 6,
apiKey : $nameServer->getApikey(),
command : 'domains/name/' . $domainName,
serverType : 'nameserver');
} else {
$result = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $nameServer->getName(),
versionIP: 4,
apiKey: $nameServer->getApikey(),
command: 'domains/name/',
serverType: 'nameserver' . $domainName);
serverName : $nameServer->getName(),
versionIP : 4,
apiKey : $nameServer->getApikey(),
command : 'domains/name/',
serverType : 'nameserver' . $domainName);
}
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
@ -403,26 +450,26 @@ class BindAPI
'name' => $domainName,
'panel' => $panel->getName(),
'content' => $this->domainController->createPanelContent(panel: $panel->getName())
];
];
try {
if (!empty($nameServer->getAaaa())) {
$create = $this->apiController->sendCommand(
requestType: 'POST',
serverName: $nameServer->getName(),
versionIP: 6,
apiKey: $nameServer->getApikey(),
command: 'domains',
serverType: 'nameserver',
body: $body);
serverName : $nameServer->getName(),
versionIP : 6,
apiKey : $nameServer->getApikey(),
command : 'domains',
serverType : 'nameserver',
body : $body);
} else {
$create = $this->apiController->sendCommand(
requestType: 'POST',
serverName: $nameServer->getName(),
versionIP: 4,
apiKey: $nameServer->getAPikey(),
command: 'domains',
serverType: 'nameserver',
body: $body);
serverName : $nameServer->getName(),
versionIP : 4,
apiKey : $nameServer->getAPikey(),
command : 'domains',
serverType : 'nameserver',
body : $body);
}
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
@ -748,11 +795,11 @@ class BindAPI
try {
if ($result = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $server->getName(),
versionIP: 4,
apiKey: $server->getApikey(),
command: 'ping',
serverType: $type)) {
serverName : $server->getName(),
versionIP : 4,
apiKey : $server->getApikey(),
command : 'ping',
serverType : $type)) {
if ($this->config['verbose']) {
if ($result['data'] == 'pong') {
echo COLOR_GREEN . $result['data'];
@ -775,11 +822,11 @@ class BindAPI
try {
if ($result = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $server->getName(),
versionIP: 6,
apiKey: $server->getApikey(),
command: 'ping',
serverType: $type)) {
serverName : $server->getName(),
versionIP : 6,
apiKey : $server->getApikey(),
command : 'ping',
serverType : $type)) {
if ($this->config['verbose']) {
if ($result['data'] == 'pong') {
echo COLOR_GREEN . $result['data'];
@ -969,28 +1016,14 @@ class BindAPI
}
if (!empty($domains)) {
$table = new ConsoleTable();
$table->setHeaders(content: ['ID', 'Name', 'Content']);
$table->setHeaders(content: ['ID', 'Name', 'Panel']);
/** @var Domain $domain */
foreach ($domains as $domain) {
$row = [];
try {
$row[] = $domain->getId();
$row[] = $domain->getName();
$content = json_decode(json: $domain->getContent());
$column = '';
if (!empty($content->panel)) {
$column = 'Panel: ' . $content->panel;
}
if (!empty($content->custom)) {
$column .= 'Custom: ';
if (!empty($content->custom->a)) {
$column .= 'A: ' . $content->custom->a;
}
if (!empty($content->custom->aaaa)) {
$column .= ' AAAA: ' . $content->custom->a;
}
}
$row[] = $column;
$row[] = $domain->getPanel();
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
}
@ -1023,16 +1056,11 @@ class BindAPI
}
$arguments = $this->parseArguments();
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
$panel = $arguments['panel'] ?? '';
print("panel: $panel");
if (empty($a) && empty($aaaa) && empty($panel)) {
echo 'At least one IP address or panel is required.' . PHP_EOL;
exit(0);
if (empty($panel)) {
echo 'You need to supply the panel name.' . PHP_EOL;
exit(1);
}
try {
@ -1040,24 +1068,13 @@ class BindAPI
echo "Domain: $name already exists." . PHP_EOL;
exit(1);
} else {
if (!empty($panel)) {
if ($this->panelRepository->findByName(name: $panel)) {
$content = $this->domainController->createPanelContent(panel: $panel);
} else {
echo 'Unknown panel: ' . $panel;
exit(1);
}
if ($this->panelRepository->findByName(name: $panel)) {
$content = $this->domainController->createPanelContent(panel: $panel);
} else {
$content = [];
if (!empty($a)) {
$content['a'] = $a;
}
if (!empty($aaaa)) {
$content['aaaa'] = $aaaa;
}
$content = $this->domainController->createIPContent(ip: $content);
echo 'Unknown panel: ' . $panel;
exit(1);
}
$domain = new Domain(name: $name, content: $content);
$domain = new Domain(name: $name, panel: $panel);
$result = $this->domainRepository->insert(domain: $domain);
echo "Domain $name has been created with id $result" . PHP_EOL;
$this->domainController->createSlaveZoneFile(domain: $domain);
@ -1078,9 +1095,7 @@ class BindAPI
$id = intval(value: $this->arguments[1] ?? 0);
$name = $arguments['name'] ?? '';
$panel = $arguments['panel'] ?? '';
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
$panelName = $arguments['panel'] ?? '';
if ($id == 0) {
echo 'An ID is required' . PHP_EOL;
@ -1093,35 +1108,17 @@ class BindAPI
$content = [];
if (!empty($panel)) {
$panel = $this->panelRepository->findByName(name: $panel);
$content = $this->domainController->createPanelContent(panel: $panel->getName());
} else {
$addresses = [];
if (!empty($a)) {
$addresses[] = [
'a' => $a
];
}
if (!empty($aaaa)) {
$addresses[] = [
'aaaa' => $aaaa
];
}
if (!empty($addresses)) {
$content = $this->domainController->createIPContent(ip: $content);
}
if (!empty($panelName)) {
$panel = $this->panelRepository->findByName(name: $panelName);
}
if (empty($name) && empty($content)) {
if (empty($name) && empty($panel)) {
echo 'No name or panel given, just recreate the config file' . PHP_EOL;
$this->domainController->createSlaveZoneFile(domain: $domain);
exit(1);
}
if (empty($content)) {
$newDomain = new Domain(name: $name, id: $domain->getId(), content: '');
} else {
$newDomain = new Domain(name: $name, id: $domain->getId(), content: $content);
}
$newDomain = new Domain(name: $name, panel: $panelName, id: $domain->getId());
if ($this->domainRepository->update(domain: $newDomain) !== false) {
echo 'Domain server has been updated' . PHP_EOL;
$this->domainController->createSlaveZoneFile(domain: $domain);
@ -1319,4 +1316,36 @@ class BindAPI
$this->nameserverRepository->delete(id: $id);
echo "The nameserver with ID $id has been deleted." . PHP_EOL;
}
/**
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
*/
private function showIncludes()
{
$nameservers = $this->nameserverRepository->findAll();
echo COLOR_DEFAULT . 'You need to add these lines to ' . COLOR_YELLOW . '/etc/bind/local.bindapi.options' . COLOR_DEFAULT . ' and make sure' . PHP_EOL;
echo 'that ' . COLOR_YELLOW . 'include "/etc/bind/local.bindapi.options";' . COLOR_DEFAULT . ' exists in ' . COLOR_YELLOW . '/etc/bind/named.conf.options' . COLOR_DEFAULT . '.' . PHP_EOL;
$ip = [];
foreach ($nameservers as $nameserver) {
if (!empty($nameserver->getA())) {
$ip[] = $nameserver->getA();
}
if (!empty($nameserver->getAaaa())) {
$ip[] = $nameserver->getAaaa();
}
}
echo PHP_EOL . 'allow-transfer {' . PHP_EOL;
foreach ($ip as $currentIp)
echo "\t$currentIp;" . PHP_EOL;
echo '};';
echo PHP_EOL . 'also-notify {' . PHP_EOL;
foreach ($ip as $currentIp)
echo "\t$currentIp;" . PHP_EOL;
echo '};' . PHP_EOL;
echo PHP_EOL . 'After the modification feel free to run ' . COLOR_YELLOW . 'named-checkconf' . COLOR_DEFAULT . ' to ensure there were no errors.' . PHP_EOL;
}
}

View File

@ -53,9 +53,7 @@ class DatabaseConnection
CREATE TABLE `domains` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`panel_id` int(11) DEFAULT 0,
`a` varbinary(255) DEFAULT NULL,
`aaaa` varbinary(255) DEFAULT NULL,
`panel` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql);
@ -81,7 +79,7 @@ class DatabaseConnection
`aaaa` varbinary(255) DEFAULT NULL,
`apikey` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql);
$statement->execute();

View File

@ -117,7 +117,7 @@ class DomainController
$bindGroup = posix_getgrnam(name: 'bind');
$members = $bindGroup['members'];
if (in_array(needle: $name, haystack: $members)) {
echo "\tis in group 'bind" . PHP_EOL;
echo "\t$name is in group 'bind" . PHP_EOL;
} else {
echo "\t$name needs to be in group $bindGroup!" . PHP_EOL;
@ -211,15 +211,13 @@ class DomainController
// check if we're a master zone
$keyhelpConf = file_get_contents(filename: $this->keyhelpNamedCond);
if (str_contains($keyhelpConf, $domain->getName())) {
if (str_contains(haystack: $keyhelpConf, needle: $domain->getName())) {
echo 'We a zone master for ' . $domain->getName() . PHP_EOL;
exit(1);
}
if ($zonefile = fopen(filename: $this->localZonesDir . $domain->getName(), mode: 'w')) {
// TODO fixme soon
$content = json_decode(json: $domain->getContent());
$panelName = $content->panel;
$panelName = $domain->getPanel();
$panel = $this->panelRepository->findByName(name: $panelName);
$a = $panel->getA();
$aaaa = $panel->getAaaa();
@ -237,42 +235,7 @@ class DomainController
fputs(stream: $zonefile, data: "};" . PHP_EOL);
}
$this->createIncludeFile();
// TODO add on nameservers
}
/**
* @param String $panel
*
* @return String
*/
function createPanelContent(String $panel): String
{
return json_encode(value: [
'panel' => $panel
]
);
}
/**
* @param array $ip
*
* @return String
*/
function createIPContent(array $ip): String
{
$result = [];
if (!empty($ip['a'])) {
$result['a'] = $ip['a'];
}
if (!empty($ip['aaaa'])) {
$result['aaaa'] = $ip['aaaa'];
}
return json_encode(value: [
'custom' => $result
]
);
}
}

View File

@ -22,17 +22,17 @@ use OpenApi\Attributes as OAT;
*/
#[OAT\Info(version: '0.0.1', title: 'bindAPI')]
#[OAT\Server(
url: "{schema}://{hostname}/api",
url : "{schema}://{hostname}/api",
description: "The bindAPI URL.",
variables: [
variables : [
new OAT\ServerVariable(
serverVariable: "schema",
default: "https",
enum: ["https", "http"]
default : "https",
enum : ["https", "http"]
),
new OAT\ServerVariable(
serverVariable: "hostname",
default: "ns2.24unix.net",
default : "ns2.24unix.net",
)
]
)]
@ -41,10 +41,10 @@ use OpenApi\Attributes as OAT;
)]
#[OAT\SecurityScheme(
securityScheme: "Authorization",
type: "apiKey",
description: "description",
name: "X-API-Key",
in: "header"
type : "apiKey",
description : "description",
name : "X-API-Key",
in : "header"
)]
class RequestController
{
@ -101,29 +101,29 @@ class RequestController
* @return void
*/
#[OAT\Get(
path: '/domains',
path : '/domains',
operationId: 'getAllDomains',
description: 'Returns a list of all domains on this server.',
summary: 'Listing all domains.',
summary : 'Listing all domains.',
// security: [
// 'Authorization' => [
//
// "read:api"
// ]
// ],
servers: [],
tags: ['Domains'],
responses: [
servers : [],
tags : ['Domains'],
responses : [
new OAT\Response(
response: 200,
response : 200,
description: 'OK'
),
new OAT\Response(
response: 401,
response : 401,
description: 'API key is missing or invalid.'
),
new OAT\Response(
response: 404,
response : 404,
description: 'Domain not found.'
)]
)]
@ -186,27 +186,27 @@ class RequestController
*/
#[OAT\Get(
path: '/domains/{name}',
path : '/domains/{name}',
operationId: 'getSingleDomain',
description: 'Returns information of a single domain specified by its domain name.',
summary: 'Returns a single domain.',
security: [
summary : 'Returns a single domain.',
security : [
],
tags: ['Domains'],
parameters: [
tags : ['Domains'],
parameters : [
new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')),
],
responses: [
responses : [
new OAT\Response(
response: 200,
response : 200,
description: 'OK'
),
new OAT\Response(
response: 401,
response : 401,
description: 'API key is missing or invalid.'
),
new OAT\Response(
response: 404,
response : 404,
description: 'Domain not found.'
)]
@ -314,9 +314,9 @@ class RequestController
if ($this->uri[3] == 'name') {
if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
$domain = [
'id' => $result->getId(),
'name' => $result->getName(),
'content' => json_decode(json: $result->getContent())
'id' => $result->getId(),
'name' => $result->getName(),
'panel' => $result->getPanel()
];
$this->result = $domain;
} else {
@ -332,9 +332,9 @@ class RequestController
if ($id > 0) {
if ($result = $this->domainRepository->findById(id: $id)) {
$domain = [
'id' => $result->getId(),
'name' => $result->getName(),
'content' => json_decode(json: $result->getContent())
'id' => $result->getId(),
'name' => $result->getName(),
'panel' => $result->getPanel()
];
$this->result = $domain;
} else {
@ -342,7 +342,6 @@ class RequestController
$this->status = "404 Not Found ";
$this->message = "The specified domain was not found.";
}
} else {
$this->header = "400 Bad request";
$this->status = "400 Not Found";
@ -352,128 +351,117 @@ class RequestController
}
}
}
/**
* @return void
*/
public
function handleDomainPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panel = $_POST['panel'] ?? '';
$content = $_POST['content'] ?? '';
if (empty($name)) {
/**
* @return void
*/
public
function handleDomainPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panel = $_POST['panel'] ?? '';
if (empty($name)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else {
if (empty($panel)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "A name is required";
$this->message = "A panel ID is required.";
} else {
if (empty($a) && empty($aaaa) && empty($panel)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "At least one IP address or panel ID is required.";
if ($this->domainRepository->findByName(name: $name)) {
$this->header = "400 Bad request";
$this->status = "400 Bad request";
$this->message = "Domain: $name already exists.";
} else {
if ($this->domainRepository->findByName(name: $name)) {
$this->header = "400 Bad request";
$this->status = "400 Bad request";
$this->message = "Domain: $name already exists.";
$domain = new Domain(name: $name, panel: $panel);
if ($result = $this->domainRepository->insert(domain: $domain)) {
$this->header = "201 Created";
$this->status = "201 Created";
} else {
$domain = new Domain(name: $name, content: $content);
if ($result = $this->domainRepository->insert(domain: $domain)) {
$this->status = "201 Created";
$this->message = $result;
} else {
$this->header = "500 Server error";
$this->status = "500 Server error";
$this->message = $result;
}
$this->header = "500 Server error";
$this->status = "500 Server error";
}
$this->message = $result;
}
}
}
/**
* @return void
*/
public
function handleDomainPutRequest(): void
{
$putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$put[$key] = $value;
}
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$content = $put['content'] ?? "";
if ($id == 0) {
$this->status = "400 Bad Request";
$this->message = "An ID is required";
} else {
if (!$this->domainRepository->findByID(id: $id)) {
$this->status = "404 Not Found";
$this->message = "Domain with ID : $id doesn't exist.";
} else {
// TODO not required, as we rely on the ID
if (empty($name)) {
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else {
if (empty($a) && empty($aaaa)) {
$this->status = "400 Bad Request";
$this->message = "At least one IP address is required.";
} else {
$domain = new Domain(name: $name, id: $id, content: $content);
$this->domainRepository->update(domain: $domain);
$this->header = "201 Updated";
$this->status = "201 Updated";
$this->message = "201 Updated";
}
}
}
}
}
/**
* @return void
*/
public
function handleDomainDeleteRequest(): void
{
$deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$delete[$key] = $value;
}
$id = $delete['id'] ?? 0;
if ($id == 0) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID.";
} else {
if (!$domain = $this->domainRepository->findByID(id: $id)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id.";
} else {
$this->domainRepository->delete(domain: $domain);
$this->header = "204 No content.";
$this->status = "204 No content.";
$this->message = "The domain $id has been deleted.";
}
}
}
}
/**
* @return void
*/
public function handleDomainPutRequest(): void
{
$putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$put[$key] = $value;
}
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$panel = $put['panel'] ?? "";
if ($id == 0) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "An ID is required";
} else {
if (!$this->domainRepository->findByID(id: $id)) {
$this->status = "404 Not Found";
$this->message = "Domain with ID : $id doesn't exist.";
} else {
// TODO not required, as we rely on the ID
$domain = new Domain(name: $name, panel: $panel, id: $id);
$this->domainRepository->update(domain: $domain);
$this->header = "201 Updated";
$this->status = "201 Updated";
$this->message = "201 Updated";
}
}
}
/**
* @return void
*/
public
function handleDomainDeleteRequest(): void
{
$deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$delete[$key] = $value;
}
$id = $delete['id'] ?? 0;
if ($id == 0) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID.";
} else {
if (!$domain = $this->domainRepository->findByID(id: $id)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id.";
} else {
$this->domainRepository->delete(domain: $domain);
$this->header = "204 No content.";
$this->status = "204 No content.";
$this->message = "The domain $id has been deleted.";
}
}
}
}

View File

@ -14,9 +14,25 @@ class Domain
/**
*/
public function __construct(private string $name, private int $id = 0, private string $content = '')
public function __construct(private string $name, private String $panel, private int $id = 0)
{}
/**
* @return String
*/
public function getPanel(): string
{
return $this->panel;
}
/**
* @param String $panel
*/
public function setPanel(string $panel): void
{
$this->panel = $panel;
}
/**
* @return String
@ -51,21 +67,4 @@ class Domain
$this->id = $id;
}
/**
* @return string
*/
public function getContent(): string
{
return $this->content;
}
/**
* @param string $content
*/
public function setContent(string $content): void
{
$this->content = $content;
}
}

View File

@ -33,7 +33,7 @@ class DomainRepository
$domains = [];
$sql = "
SELECT id, name, content
SELECT id, name, panel
FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name";
@ -43,7 +43,7 @@ class DomainRepository
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
//print_r($result);
//die();
$domain = new Domain(name: $result['name'], id: $result['id'], content: $result['content']);
$domain = new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']);
$domains[] = $domain;
}
return $domains;
@ -75,7 +75,7 @@ class DomainRepository
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], id: $result['id'], content: $result['content']);
return new Domain(name: $result['name'], panel: $result['content'], id: $result['id']);
} else {
return false;
}
@ -105,7 +105,7 @@ class DomainRepository
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], id: $result['id'], content: $result['content']);
return new Domain(name: $result['name'], panel: $result['content'], id: $result['id']);
} else {
return false;
}
@ -133,10 +133,10 @@ class DomainRepository
try {
$name = $domain->getName();
$content = $domain->getContent();
$panel = $domain->getPanel();
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':content', var: $content);
$statement->bindParam(param: ':panel', var: $panel);
$statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId();
@ -166,10 +166,10 @@ class DomainRepository
} else {
$name = $domain->getName();
}
if (empty($domain->getContent())) {
$content = $current->getContent();
if (empty($domain->getPanel())) {
$content = $current->getPanel();
} else {
$content = $domain->getContent();
$content = $domain->getPanel();
}
$sql = "