2022-01-26 19:36:56 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-31 20:49:44 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-26 19:36:56 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
|
2022-01-31 20:49:44 +01:00
|
|
|
use App\Repository\ApikeyRepository;
|
|
|
|
use App\Repository\DomainRepository;
|
|
|
|
use DI\Container;
|
|
|
|
use DI\ContainerBuilder;
|
2022-01-20 11:06:58 +01:00
|
|
|
use UnhandledMatchError;
|
2022-01-31 20:49:44 +01:00
|
|
|
use function DI\autowire;
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class RequestController
|
|
|
|
{
|
2022-01-31 20:49:44 +01:00
|
|
|
//private DatabaseConnection $databaseConnection;
|
|
|
|
private DomainRepository $domainRepository;
|
|
|
|
private ApikeyRepository $apikeyRepository;
|
|
|
|
private Container $container;
|
|
|
|
private string $header;
|
2022-01-20 11:06:58 +01:00
|
|
|
private array $result;
|
2022-01-31 20:49:44 +01:00
|
|
|
private string $status;
|
|
|
|
private string $message;
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-20 11:06:58 +01:00
|
|
|
/**
|
2022-01-25 20:50:26 +01:00
|
|
|
* @param array $config
|
|
|
|
* @param String $requestMethod
|
|
|
|
* @param array $uri
|
2022-01-31 20:49:44 +01:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2022-01-20 11:06:58 +01:00
|
|
|
*/
|
2022-01-31 20:49:44 +01:00
|
|
|
public function __construct(private array $config, private string $requestMethod, private array $uri)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-26 19:36:56 +01:00
|
|
|
$this->requestMethod = strtoupper(string: $requestMethod);
|
2022-01-31 20:49:44 +01:00
|
|
|
|
|
|
|
$containerBuilder = new ContainerBuilder();
|
|
|
|
$containerBuilder->addDefinitions([
|
|
|
|
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config),
|
|
|
|
]);
|
|
|
|
$this->container = $containerBuilder->build();
|
|
|
|
|
|
|
|
$this->domainRepository = $this->container->get(name: DomainRepository::class);
|
|
|
|
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:06:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-18 19:14:24 +01:00
|
|
|
public function processRequest()
|
|
|
|
{
|
2022-01-24 18:57:47 +01:00
|
|
|
if (empty($this->uri[2]) || !(($this->uri[2] == 'domains') || $this->uri[2] == 'ping')) {
|
2022-01-25 20:50:26 +01:00
|
|
|
$this->header = '404 Not Found';
|
2022-01-18 19:14:24 +01:00
|
|
|
$this->status = "404 Not Found";
|
|
|
|
$this->message = "Endpoint not found.";
|
|
|
|
} else {
|
|
|
|
if ($this->checkPassword()) {
|
2022-01-24 18:57:47 +01:00
|
|
|
if ($this->uri[2] == "ping") {
|
2022-01-25 20:50:26 +01:00
|
|
|
$this->header = '200 OK';
|
2022-01-24 18:57:47 +01:00
|
|
|
$this->status = 'pong';
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
match ($this->requestMethod) {
|
|
|
|
'GET' => $this->handleDomainGetRequest(),
|
|
|
|
'POST' => $this->handleDomainPostRequest(),
|
|
|
|
'PUT' => $this->handleDomainPutRequest(),
|
|
|
|
'DELETE' => $this->handleDomainDeleteRequest()
|
|
|
|
};
|
2022-01-31 20:49:44 +01:00
|
|
|
} catch (UnhandledMatchError) {
|
2022-01-25 20:50:26 +01:00
|
|
|
$this->header = '400 Bad Request';
|
|
|
|
$this->status = '400 Bad Request';
|
2022-01-18 19:14:24 +01:00
|
|
|
$this->message = "unknown request method: $this->requestMethod";
|
2022-01-24 18:57:47 +01:00
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-31 20:49:44 +01:00
|
|
|
|
|
|
|
if (!empty($this->header)) {
|
2022-01-25 20:50:26 +01:00
|
|
|
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
|
|
|
|
}
|
2022-01-20 11:06:58 +01:00
|
|
|
if (!empty($this->result)) {
|
2022-01-26 19:36:56 +01:00
|
|
|
echo json_encode(value: $this->result);
|
2022-01-18 19:14:24 +01:00
|
|
|
} else {
|
2022-01-26 19:50:58 +01:00
|
|
|
if (!empty($this->status) && $this->status == 'pong') {
|
2022-01-26 19:36:56 +01:00
|
|
|
echo json_encode(value: [
|
2022-01-24 18:57:47 +01:00
|
|
|
'response' => $this->status
|
|
|
|
]);
|
|
|
|
} else {
|
2022-01-26 19:36:56 +01:00
|
|
|
echo json_encode(value: [
|
2022-01-31 20:49:44 +01:00
|
|
|
'status' => $this->status ?? "Error: No status",
|
2022-01-24 18:57:47 +01:00
|
|
|
'message' => $this->message ?? "Error: No message."
|
|
|
|
]);
|
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkPassword(): bool
|
|
|
|
{
|
2022-01-26 19:36:56 +01:00
|
|
|
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
|
|
|
|
$apiKey = $headers['X-API-KEY'] ?? '';
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
if (empty($apiKey)) {
|
2022-01-25 20:50:26 +01:00
|
|
|
$this->header = "401 Unauthorized";
|
2022-01-18 19:14:24 +01:00
|
|
|
$this->status = "401 Unauthorized";
|
|
|
|
$this->message = "API key is missing.";
|
|
|
|
return false;
|
|
|
|
} else {
|
2022-01-26 19:36:56 +01:00
|
|
|
[$prefix,] = explode(separator: '.', string: $apiKey);
|
2022-01-31 20:49:44 +01:00
|
|
|
if ($apiResult = $this->apikeyRepository->findByPrefix(prefix: $prefix)) {
|
|
|
|
$storedHash = $apiResult->getApiToken();
|
2022-01-27 15:16:20 +01:00
|
|
|
if (!password_verify(password: $apiKey, hash: $storedHash)) {
|
|
|
|
$this->header = "401 Unauthorized";
|
|
|
|
$this->status = "401 Unauthorized";
|
|
|
|
$this->message = "API key mismatch.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-25 20:50:26 +01:00
|
|
|
$this->header = "401 Unauthorized";
|
2022-01-18 19:14:24 +01:00
|
|
|
$this->status = "401 Unauthorized";
|
2022-01-31 20:49:44 +01:00
|
|
|
$this->message = "Invalid API key.";
|
2022-01-18 19:14:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:06:58 +01:00
|
|
|
/**
|
2022-01-22 17:36:15 +01:00
|
|
|
* @return void
|
2022-01-20 11:06:58 +01:00
|
|
|
*/
|
2022-01-22 17:32:36 +01:00
|
|
|
public function handleDomainGetRequest(): void
|
2022-01-20 11:06:58 +01:00
|
|
|
{
|
|
|
|
if (empty($this->uri[3])) {
|
2022-01-31 20:49:44 +01:00
|
|
|
$domains = $this->domainRepository->findAll();
|
|
|
|
$resultDomain = [];
|
|
|
|
foreach ($domains as $singleDomain) {
|
|
|
|
$domain = [
|
|
|
|
'id' => $singleDomain->getId(),
|
|
|
|
'name' => $singleDomain->getName(),
|
|
|
|
'panel_id' => $singleDomain->getPanelId(),
|
|
|
|
'a' => $singleDomain->getA(),
|
|
|
|
'aaaa' => $singleDomain->getAaaa()
|
|
|
|
];
|
|
|
|
$resultDomain[] = $domain;
|
|
|
|
}
|
|
|
|
$this->result = $resultDomain;
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-01-31 20:49:44 +01:00
|
|
|
if ($result = $this->domainRepository->findByName(name: $this->uri[3])) {
|
|
|
|
$domain = [
|
|
|
|
'id' => $result->getId(),
|
|
|
|
'name' => $result->getName(),
|
|
|
|
'panel_id' => $result->getPanelId(),
|
|
|
|
'a' => $result->getA(),
|
|
|
|
'aaaa' => $result->getAaaa()
|
|
|
|
];
|
|
|
|
$this->result = $domain;
|
2022-01-22 17:32:36 +01:00
|
|
|
} else {
|
2022-01-25 20:50:26 +01:00
|
|
|
$this->header = "404 Not Found ";
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "404 Not Found ";
|
|
|
|
$this->message = "The specified domain was not found.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:36:15 +01:00
|
|
|
|
2022-01-20 11:06:58 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleDomainPostRequest(): void
|
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$name = $_POST['name'] ?? '';
|
2022-01-27 15:16:20 +01:00
|
|
|
$panelID = intval(value: $_POST['panel_id'] ?? 0);
|
2022-01-22 17:32:36 +01:00
|
|
|
$a = $_POST['a'] ?? '';
|
|
|
|
$aaaa = $_POST['aaaa'] ?? '';
|
2022-01-20 11:06:58 +01:00
|
|
|
if (empty($name)) {
|
2022-01-27 15:16:20 +01:00
|
|
|
$this->header = "400 Bad Request";
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "400 Bad Request";
|
|
|
|
$this->message = "A name is required";
|
|
|
|
} else {
|
2022-01-27 15:16:20 +01:00
|
|
|
if (empty($a) && empty($aaaa) && empty($panelID)) {
|
|
|
|
$this->header = "400 Bad Request";
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "400 Bad Request";
|
2022-01-27 15:16:20 +01:00
|
|
|
$this->message = "At least one IP address or panel ID is required.";
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-01-31 20:49:44 +01:00
|
|
|
if ($this->domainRepository->findByName(name: $name)) {
|
2022-01-27 15:16:20 +01:00
|
|
|
$this->header = "400 Bad request";
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "400 Bad request";
|
|
|
|
$this->message = "Domain: $name already exists.";
|
|
|
|
} else {
|
2022-01-31 20:49:44 +01:00
|
|
|
$result = $this->domainRepository->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "201 Created";
|
|
|
|
$this->message = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleDomainPutRequest(): void
|
|
|
|
{
|
|
|
|
$putData = fopen(filename: 'php://input', mode: 'r');
|
2022-01-26 19:36:56 +01:00
|
|
|
$data = fread(stream: $putData, length: 512);
|
|
|
|
$params = explode(separator: '&', string: $data);
|
2022-01-20 11:06:58 +01:00
|
|
|
|
|
|
|
foreach ($params as $param) {
|
2022-01-26 19:36:56 +01:00
|
|
|
[$key, $value] = explode(separator: '=', string: $param);
|
2022-01-20 11:06:58 +01:00
|
|
|
$put[$key] = $value;
|
|
|
|
}
|
|
|
|
$id = $put['id'] ?? 0;
|
|
|
|
$name = $put['name'] ?? "";
|
2022-01-25 20:50:26 +01:00
|
|
|
$panelID = $put['panel_id'] ?? "";
|
2022-01-20 11:06:58 +01:00
|
|
|
$a = $put['a'] ?? "";
|
|
|
|
$aaaa = $put['aaaa'] ?? "";
|
|
|
|
|
|
|
|
if ($id == 0) {
|
|
|
|
$this->status = "400 Bad Request";
|
|
|
|
$this->message = "An ID is required";
|
|
|
|
} else {
|
2022-01-31 20:49:44 +01:00
|
|
|
if (!$this->domainRepository->findByID(id: $id)) {
|
2022-01-20 11:06:58 +01:00
|
|
|
$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 {
|
2022-01-31 20:49:44 +01:00
|
|
|
$this->domainRepository->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa);
|
2022-01-26 19:36:56 +01:00
|
|
|
$this->header = "201 Updated";
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "201 Updated";
|
2022-01-26 19:36:56 +01:00
|
|
|
$this->message = "201 Updated";
|
2022-01-20 11:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleDomainDeleteRequest(): void
|
|
|
|
{
|
|
|
|
$deleteData = fopen(filename: 'php://input', mode: 'r');
|
2022-01-26 19:36:56 +01:00
|
|
|
$data = fread(stream: $deleteData, length: 512);
|
2022-01-20 11:06:58 +01:00
|
|
|
$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) {
|
2022-01-26 19:36:56 +01:00
|
|
|
$this->header = "400 Bad Request";
|
|
|
|
$this->status = "400 Bad Request";
|
2022-01-22 17:32:36 +01:00
|
|
|
$this->message = "You need to supply an ID.";
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-01-31 20:49:44 +01:00
|
|
|
if (!$this->domainRepository->findByID(id: $id)) {
|
2022-01-26 19:36:56 +01:00
|
|
|
$this->header = "400 Bad Request";
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "400 Bad Request";
|
|
|
|
$this->message = "There is no domain with ID $id.";
|
|
|
|
} else {
|
2022-01-31 20:49:44 +01:00
|
|
|
$this->domainRepository->delete(id: $id);
|
2022-01-26 19:36:56 +01:00
|
|
|
$this->header = "204 No content.";
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "204 No content.";
|
|
|
|
$this->message = "The domain $id has been deleted.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|