From ee7ec8a74f4e1c526aaf0cfd8e1d9c4e9e2d82a9 Mon Sep 17 00:00:00 2001 From: tracer Date: Tue, 25 Jan 2022 20:50:26 +0100 Subject: [PATCH] added http response headers Signed-off-by: tracer --- src/Controller/RequestController.php | 32 +++++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Controller/RequestController.php b/src/Controller/RequestController.php index 4f03270..61685dd 100644 --- a/src/Controller/RequestController.php +++ b/src/Controller/RequestController.php @@ -9,20 +9,25 @@ use UnhandledMatchError; */ class RequestController { + private DatabaseConnection $databaseConnection; private DomainController $domainController; + private PanelController $panelController; + private String $header; private array $result; private String $status; private String $message; /** - * @param DatabaseConnection $databaseConnection - * @param String $requestMethod - * @param array $uri + * @param array $config + * @param String $requestMethod + * @param array $uri */ - public function __construct(private DatabaseConnection $databaseConnection, private String $requestMethod, private array $uri) + public function __construct(private array $config, private String $requestMethod, private array $uri) { $this->requestMethod = strtoupper($requestMethod); - $this->domainController = new DomainController($this->databaseConnection); + $this->databaseConnection = new DatabaseConnection($this->config); + $this->panelController = new PanelController($this->databaseConnection); + $this->domainController = new DomainController($this->databaseConnection, $this->panelController); } @@ -32,11 +37,13 @@ class RequestController public function processRequest() { if (empty($this->uri[2]) || !(($this->uri[2] == 'domains') || $this->uri[2] == 'ping')) { + $this->header = '404 Not Found'; $this->status = "404 Not Found"; $this->message = "Endpoint not found."; } else { if ($this->checkPassword()) { if ($this->uri[2] == "ping") { + $this->header = '200 OK'; $this->status = 'pong'; } else { try { @@ -47,13 +54,17 @@ class RequestController 'DELETE' => $this->handleDomainDeleteRequest() }; } catch(UnhandledMatchError) { - $this->status = "400 Bad Request"; + $this->header = '400 Bad Request'; + $this->status = '400 Bad Request'; $this->message = "unknown request method: $this->requestMethod"; } } } } + if(!empty($this->header)) { + header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header); + } if (!empty($this->result)) { echo json_encode($this->result); } else { @@ -80,6 +91,7 @@ class RequestController $apiKey = $headers['X-API-KEY'] ?? ""; if (empty($apiKey)) { + $this->header = "401 Unauthorized"; $this->status = "401 Unauthorized"; $this->message = "API key is missing."; return false; @@ -90,6 +102,7 @@ class RequestController $storedHash = $apiResult['api_token']; if (!password_verify($apiKey, $storedHash)) { + $this->header = "401 Unauthorized"; $this->status = "401 Unauthorized"; $this->message = "API key mismatch."; return false; @@ -109,6 +122,7 @@ class RequestController if ($result = $this->domainController->findByID(intval($this->uri[3]))) { $this->result = $result; } else { + $this->header = "404 Not Found "; $this->status = "404 Not Found "; $this->message = "The specified domain was not found."; } @@ -122,6 +136,7 @@ class RequestController public function handleDomainPostRequest(): void { $name = $_POST['name'] ?? ''; + $panelID = $_POST['panel_id'] ?? ''; $a = $_POST['a'] ?? ''; $aaaa = $_POST['aaaa'] ?? ''; if (empty($name)) { @@ -136,7 +151,7 @@ class RequestController $this->status = "400 Bad request"; $this->message = "Domain: $name already exists."; } else { - $result = $this->domainController->insert($name, $a, $aaaa); + $result = $this->domainController->insert($name, $panelID, $a, $aaaa); $this->status = "201 Created"; $this->message = $result; } @@ -160,6 +175,7 @@ class RequestController } $id = $put['id'] ?? 0; $name = $put['name'] ?? ""; + $panelID = $put['panel_id'] ?? ""; $a = $put['a'] ?? ""; $aaaa = $put['aaaa'] ?? ""; @@ -180,7 +196,7 @@ class RequestController $this->status = "400 Bad Request"; $this->message = "At least one IP address is required."; } else { - $dcResult = $this->domainController->update($id, $name, $a, $aaaa); + $dcResult = $this->domainController->update($id, $panelID, $name, $a, $aaaa); $this->status = "201 Updated"; $this->message = $dcResult; }