From 136569b428682deecb7bae9971e98371efbdb635 Mon Sep 17 00:00:00 2001 From: tracer Date: Thu, 20 Jan 2022 11:06:58 +0100 Subject: [PATCH] moved check domain to DomainController --- bindAPI/src/Controller/RequestController.php | 316 +++++++++---------- 1 file changed, 142 insertions(+), 174 deletions(-) diff --git a/bindAPI/src/Controller/RequestController.php b/bindAPI/src/Controller/RequestController.php index 8f93c54..293de60 100644 --- a/bindAPI/src/Controller/RequestController.php +++ b/bindAPI/src/Controller/RequestController.php @@ -3,217 +3,65 @@ namespace App\Controller; use PDO; +use UnhandledMatchError; /** * */ class RequestController { - private PDO $dbConnection; private DomainController $domainController; - private String $requestMethod; - private array $uri; + private array $result; private String $status; private String $message; - public function __construct(PDO $dbConnection, String $requestMethod, array $uri) + /** + * @param \PDO $dbConnection + * @param String $requestMethod + * @param array $uri + */ + public function __construct(private PDO $dbConnection, private String $requestMethod, private array $uri) { - $this->dbConnection = $dbConnection; $this->requestMethod = strtoupper($requestMethod); - $this->uri = $uri; - $this->domainController = new DomainController($dbConnection); - } + + /** + * @return void + */ public function processRequest() { - $result = Array(); - if (empty($this->uri[2]) || $this->uri[2] != 'domains') { $this->status = "404 Not Found"; $this->message = "Endpoint not found."; } else { if ($this->checkPassword()) { - switch ($this->requestMethod) { - case 'GET': - if (empty($this->uri[3])) { - $result = $this->domainController->findAll(); - } else { - if (strtolower($this->uri[3]) == "check") { - $result = $this->checkDomains(); - } else { - $result = $this->domainController->findByName($this->uri[3]); - } - } - break; - case 'POST': - $name = $_POST['name'] ?? ""; - $a = $_POST['a'] ?? ""; - $aaaa = $_POST['aaaa'] ?? ""; - if (empty($name)) { - $this->status = "400 Bad Request"; - $this->message = "A name is required"; - break; - } - if (empty($a) && empty($aaaa)) { - $this->status = "400 Bad Request"; - $this->message = "At least one IP address is required."; - break; - } - - if($this->domainController->findByName($name)) { - $this->status = "400 Bad request"; - $this->message = "Domain: $name already exists."; - } else { - $dcResult = $this->domainController->insert($name, $a, $aaaa); - $this->status = "201 Created"; - $this->message = $dcResult; - } - break; - case 'PUT': - $putData = fopen('php://input', 'r'); - $data = fread($putData, 512); - - $params = explode( '&', $data); - - foreach ($params as $param) { - [$key, $value] = explode('=', $param); - $put[$key] = $value; - } - $id = $put['id'] ?? 0; - $name = $put['name'] ?? ""; - $a = $put['a'] ?? ""; - $aaaa = $put['aaaa'] ?? ""; - - if ($id == 0) { - $this->status = "400 Bad Request"; - $this->message = "An ID is required"; - break; - } - if(!$this->domainController->findByID($id)) { - $this->status = "400 Bad request"; - $this->message = "Domain with ID : $id doesn't exist."; - break; - } - if (empty($name)) { - $this->status = "400 Bad Request"; - $this->message = "A name is required"; - break; - } - if (empty($a) && empty($aaaa)) { - $this->status = "400 Bad Request"; - $this->message = "At least one IP address is required."; - break; - } - - $dcResult = $this->domainController->update($id, $name, $a, $aaaa); - $this->status = "201 Updated"; - $this->message = $dcResult; - - break; - case "DELETE": - $deleteData = fopen('php://input', 'r'); - $data = fread($deleteData, 512); - - $params = explode( '&', $data); - - foreach ($params as $param) { - [$key, $value] = explode('=', $param); - $delete[$key] = $value; - } - - $id = $delete['id'] ?? 0; - - if ($id == 0) { - $this->status = "400 Bad Request"; - $this->message = "A valid ID is required."; - break; - } - - if(!$this->domainController->findByID($id)) { - $this->status = "400 Bad Request"; - $this->message = "There is no domain with ID $id."; - break; - } - $this->domainController->delete($id); - $this->status = "204 No content."; - $this->message = "The domain $id has been deleted."; - break; - default: + try { + match ($this->requestMethod) { + 'GET' => $this->result = $this->handleDomainGetRequest(), + 'POST' => $this->handleDomainPostRequest(), + 'PUT' => $this->handleDomainPutRequest(), + 'DELETE' => $this->handleDomainDeleteRequest() + }; + } catch(UnhandledMatchError) { $this->status = "400 Bad Request"; $this->message = "unknown request method: $this->requestMethod"; } } - } - if (!empty($result)) { - echo json_encode($result); + if (!empty($this->result)) { + echo json_encode($this->result); } else { echo json_encode([ 'status' => $this->status ?? "Error: No status", 'message' => $this->message ?? "Error: No message." ]); } - } - /** - * @return array - */ - function checkDomains(): array - { - $errors = Array(); - $domains = $this->domainController->findAll(); - - // check for included main file in /etc/bind/named.conf.local - // it needs to include "/etc/bind/local.zones"; - - $localZoneFile = '/etc/bind/local.zones'; - $localZonesDir = '/etc/bind/zones/'; - $namedConfLocalFile = '/etc/bind/named.conf.local'; - - if ($namedConfLocal = file_get_contents($namedConfLocalFile)) { - if (!str_contains($namedConfLocal, $localZoneFile)) { - $errors[] = "$localZoneFile needs to be included in $namedConfLocalFile."; - } - } else { - $errors[] = "No access to '$namedConfLocalFile'. Please check permissions"; - return $errors; - } - - if (!fileperms($localZoneFile)) { - $errors[] = "No access to $localZoneFile. Please check permissions."; - return $errors; - } - - $localZones = file_get_contents($localZoneFile); - - - foreach($domains as $domain) { - if(!str_contains($localZones, $domain['name'])) { - $errors[] = $domain['name'] . " is missing in '$localZoneFile'"; - } - - $zoneFile = $localZonesDir . $domain['name']; - - if (!file_exists($zoneFile)) { - $errors[] = "Missing zone file for $zoneFile. Update zone to create it"; - } - } - - if (empty($errors)) { - return [ - 'status' => "200 domains healthy.", - 'message' => "All domains ar setup." - ]; - } else { - return $errors; - } - } - /** * @return bool */ @@ -241,4 +89,124 @@ class RequestController return true; } + /** + * @return array|bool + */ + public function handleDomainGetRequest(): array|bool + { + if (empty($this->uri[3])) { + $this->result = $this->domainController->findAll(); + } else { + if (!$this->result = $this->domainController->findByName($this->uri[3])) { + $this->status = "404 Not Found "; + $this->message = "The specified domain was not found."; + } + } + return $this->result; + } + + /** + * @return void + */ + public function handleDomainPostRequest(): void + { + $name = $_POST['name'] ?? ""; + $a = $_POST['a'] ?? ""; + $aaaa = $_POST['aaaa'] ?? ""; + 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 { + if ($this->domainController->findByName($name)) { + $this->status = "400 Bad request"; + $this->message = "Domain: $name already exists."; + } else { + $result = $this->domainController->insert($name, $a, $aaaa); + $this->status = "201 Created"; + $this->message = $result; + } + } + } + } + + + /** + * @return void + */ + public function handleDomainPutRequest(): void + { + $putData = fopen(filename: 'php://input', mode: 'r'); + $data = fread($putData, 512); + $params = explode('&', $data); + + foreach ($params as $param) { + [$key, $value] = explode('=', $param); + $put[$key] = $value; + } + $id = $put['id'] ?? 0; + $name = $put['name'] ?? ""; + $a = $put['a'] ?? ""; + $aaaa = $put['aaaa'] ?? ""; + + if ($id == 0) { + $this->status = "400 Bad Request"; + $this->message = "An ID is required"; + } else { + if (!$this->domainController->findByID($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 { + $dcResult = $this->domainController->update($id, $name, $a, $aaaa); + $this->status = "201 Updated"; + $this->message = $dcResult; + } + } + } + } + } + + /** + * @return void + */ + public function handleDomainDeleteRequest(): void + { + $deleteData = fopen(filename: 'php://input', mode: 'r'); + $data = fread($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->status = "404 Bad Request"; + $this->message = "Domain with ID $id not found."; + } else { + if (!$this->domainController->findByID($id)) { + $this->status = "400 Bad Request"; + $this->message = "There is no domain with ID $id."; + } else { + $this->domainController->delete($id); + $this->status = "204 No content."; + $this->message = "The domain $id has been deleted."; + } + } + } + }