changed from content to panel

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-02-22 15:31:49 +01:00
parent 1ad945fbf2
commit 8c7755e096
1 changed files with 136 additions and 148 deletions

View File

@ -22,17 +22,17 @@ use OpenApi\Attributes as OAT;
*/ */
#[OAT\Info(version: '0.0.1', title: 'bindAPI')] #[OAT\Info(version: '0.0.1', title: 'bindAPI')]
#[OAT\Server( #[OAT\Server(
url: "{schema}://{hostname}/api", url : "{schema}://{hostname}/api",
description: "The bindAPI URL.", description: "The bindAPI URL.",
variables: [ variables : [
new OAT\ServerVariable( new OAT\ServerVariable(
serverVariable: "schema", serverVariable: "schema",
default: "https", default : "https",
enum: ["https", "http"] enum : ["https", "http"]
), ),
new OAT\ServerVariable( new OAT\ServerVariable(
serverVariable: "hostname", serverVariable: "hostname",
default: "ns2.24unix.net", default : "ns2.24unix.net",
) )
] ]
)] )]
@ -41,10 +41,10 @@ use OpenApi\Attributes as OAT;
)] )]
#[OAT\SecurityScheme( #[OAT\SecurityScheme(
securityScheme: "Authorization", securityScheme: "Authorization",
type: "apiKey", type : "apiKey",
description: "description", description : "description",
name: "X-API-Key", name : "X-API-Key",
in: "header" in : "header"
)] )]
class RequestController class RequestController
{ {
@ -101,29 +101,29 @@ class RequestController
* @return void * @return void
*/ */
#[OAT\Get( #[OAT\Get(
path: '/domains', path : '/domains',
operationId: 'getAllDomains', operationId: 'getAllDomains',
description: 'Returns a list of all domains on this server.', description: 'Returns a list of all domains on this server.',
summary: 'Listing all domains.', summary : 'Listing all domains.',
// security: [ // security: [
// 'Authorization' => [ // 'Authorization' => [
// //
// "read:api" // "read:api"
// ] // ]
// ], // ],
servers: [], servers : [],
tags: ['Domains'], tags : ['Domains'],
responses: [ responses : [
new OAT\Response( new OAT\Response(
response: 200, response : 200,
description: 'OK' description: 'OK'
), ),
new OAT\Response( new OAT\Response(
response: 401, response : 401,
description: 'API key is missing or invalid.' description: 'API key is missing or invalid.'
), ),
new OAT\Response( new OAT\Response(
response: 404, response : 404,
description: 'Domain not found.' description: 'Domain not found.'
)] )]
)] )]
@ -186,27 +186,27 @@ class RequestController
*/ */
#[OAT\Get( #[OAT\Get(
path: '/domains/{name}', path : '/domains/{name}',
operationId: 'getSingleDomain', operationId: 'getSingleDomain',
description: 'Returns information of a single domain specified by its domain name.', description: 'Returns information of a single domain specified by its domain name.',
summary: 'Returns a single domain.', summary : 'Returns a single domain.',
security: [ security : [
], ],
tags: ['Domains'], tags : ['Domains'],
parameters: [ parameters : [
new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')), new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')),
], ],
responses: [ responses : [
new OAT\Response( new OAT\Response(
response: 200, response : 200,
description: 'OK' description: 'OK'
), ),
new OAT\Response( new OAT\Response(
response: 401, response : 401,
description: 'API key is missing or invalid.' description: 'API key is missing or invalid.'
), ),
new OAT\Response( new OAT\Response(
response: 404, response : 404,
description: 'Domain not found.' description: 'Domain not found.'
)] )]
@ -314,9 +314,9 @@ class RequestController
if ($this->uri[3] == 'name') { if ($this->uri[3] == 'name') {
if ($result = $this->domainRepository->findByName(name: $this->uri[4])) { if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
$domain = [ $domain = [
'id' => $result->getId(), 'id' => $result->getId(),
'name' => $result->getName(), 'name' => $result->getName(),
'content' => json_decode(json: $result->getContent()) 'panel' => $result->getPanel()
]; ];
$this->result = $domain; $this->result = $domain;
} else { } else {
@ -332,9 +332,9 @@ class RequestController
if ($id > 0) { if ($id > 0) {
if ($result = $this->domainRepository->findById(id: $id)) { if ($result = $this->domainRepository->findById(id: $id)) {
$domain = [ $domain = [
'id' => $result->getId(), 'id' => $result->getId(),
'name' => $result->getName(), 'name' => $result->getName(),
'content' => json_decode(json: $result->getContent()) 'panel' => $result->getPanel()
]; ];
$this->result = $domain; $this->result = $domain;
} else { } else {
@ -342,7 +342,6 @@ class RequestController
$this->status = "404 Not Found "; $this->status = "404 Not Found ";
$this->message = "The specified domain was not found."; $this->message = "The specified domain was not found.";
} }
} else { } else {
$this->header = "400 Bad request"; $this->header = "400 Bad request";
$this->status = "400 Not Found"; $this->status = "400 Not Found";
@ -352,128 +351,117 @@ class RequestController
} }
} }
} }
/** /**
* @return void * @return void
*/ */
public public
function handleDomainPostRequest(): void function handleDomainPostRequest(): void
{ {
$name = $_POST['name'] ?? ''; $name = $_POST['name'] ?? '';
$panel = $_POST['panel'] ?? ''; $panel = $_POST['panel'] ?? '';
$content = $_POST['content'] ?? ''; if (empty($name)) {
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->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "A name is required"; $this->message = "A panel ID is required.";
} else { } else {
if (empty($a) && empty($aaaa) && empty($panel)) { if ($this->domainRepository->findByName(name: $name)) {
$this->header = "400 Bad Request"; $this->header = "400 Bad request";
$this->status = "400 Bad Request"; $this->status = "400 Bad request";
$this->message = "At least one IP address or panel ID is required."; $this->message = "Domain: $name already exists.";
} else { } else {
if ($this->domainRepository->findByName(name: $name)) { $domain = new Domain(name: $name, panel: $panel);
$this->header = "400 Bad request"; if ($result = $this->domainRepository->insert(domain: $domain)) {
$this->status = "400 Bad request"; $this->header = "201 Created";
$this->message = "Domain: $name already exists."; $this->status = "201 Created";
} else { } else {
$domain = new Domain(name: $name, content: $content); $this->header = "500 Server error";
if ($result = $this->domainRepository->insert(domain: $domain)) { $this->status = "500 Server error";
$this->header = "201 Created";
$this->status = "201 Created";
} else {
$this->header = "500 Server error";
$this->status = "500 Server error";
}
$this->message = $result;
} }
$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.";
}
}
}
}