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-02-06 17:58:52 +01:00
|
|
|
use App\Entity\Domain;
|
2022-04-06 16:28:38 +02:00
|
|
|
use App\Entity\DynDNS;
|
2022-01-31 20:49:44 +01:00
|
|
|
use App\Repository\ApikeyRepository;
|
|
|
|
use App\Repository\DomainRepository;
|
2022-04-06 16:28:38 +02:00
|
|
|
use App\Repository\DynDNSRepository;
|
2022-03-01 16:42:34 +01:00
|
|
|
use App\Repository\PanelRepository;
|
2022-02-12 19:27:38 +01:00
|
|
|
use Monolog\Logger;
|
2022-03-10 20:05:42 +01:00
|
|
|
use OpenApi\Attributes as OAT;
|
2022-01-20 11:06:58 +01:00
|
|
|
use UnhandledMatchError;
|
2022-09-17 16:28:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
// TODO attributes for swaggerUI
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2022-02-06 17:58:52 +01:00
|
|
|
#[OAT\Info(version: '0.0.1', title: 'bindAPI')]
|
|
|
|
#[OAT\Server(
|
2022-02-22 15:31:49 +01:00
|
|
|
url : "{schema}://{hostname}/api",
|
2022-02-06 17:58:52 +01:00
|
|
|
description: "The bindAPI URL.",
|
2022-02-22 15:31:49 +01:00
|
|
|
variables : [
|
2022-02-06 17:58:52 +01:00
|
|
|
new OAT\ServerVariable(
|
|
|
|
serverVariable: "schema",
|
2022-02-22 15:31:49 +01:00
|
|
|
default : "https",
|
|
|
|
enum : ["https", "http"]
|
2022-02-06 17:58:52 +01:00
|
|
|
),
|
|
|
|
new OAT\ServerVariable(
|
|
|
|
serverVariable: "hostname",
|
2022-02-22 15:31:49 +01:00
|
|
|
default : "ns2.24unix.net",
|
2022-02-06 17:58:52 +01:00
|
|
|
)
|
|
|
|
]
|
|
|
|
)]
|
|
|
|
#[OAT\Tag(
|
|
|
|
name: "Server"
|
|
|
|
)]
|
|
|
|
#[OAT\SecurityScheme(
|
|
|
|
securityScheme: "Authorization",
|
2022-02-22 15:31:49 +01:00
|
|
|
type : "apiKey",
|
|
|
|
description : "description",
|
|
|
|
name : "X-API-Key",
|
|
|
|
in : "header"
|
2022-02-06 17:58:52 +01:00
|
|
|
)]
|
2022-01-18 19:14:24 +01:00
|
|
|
class RequestController
|
|
|
|
{
|
2022-01-31 20:49:44 +01:00
|
|
|
private string $status;
|
2022-09-17 16:28:04 +02:00
|
|
|
private string $response;
|
2022-01-31 20:49:44 +01:00
|
|
|
private string $message;
|
2022-09-17 16:28:04 +02:00
|
|
|
private array $result;
|
|
|
|
private string $requestMethod;
|
|
|
|
private array $uri;
|
2022-03-10 20:05:42 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-20 11:06:58 +01:00
|
|
|
/**
|
2022-09-22 18:53:29 +02:00
|
|
|
* @param ApiController $apiController
|
|
|
|
* @param ApikeyRepository $apikeyRepository
|
|
|
|
* @param DomainController $domainController
|
|
|
|
* @param DomainRepository $domainRepository
|
|
|
|
* @param DynDNSRepository $dynDNSRepository
|
|
|
|
* @param PanelRepository $panelRepository
|
|
|
|
* @param Logger $logger
|
2022-01-20 11:06:58 +01:00
|
|
|
*/
|
2022-09-17 16:28:04 +02:00
|
|
|
public function __construct(
|
|
|
|
private readonly ApiController $apiController,
|
|
|
|
private readonly ApikeyRepository $apikeyRepository,
|
|
|
|
private readonly DomainController $domainController,
|
|
|
|
private readonly DomainRepository $domainRepository,
|
|
|
|
private readonly DynDNSRepository $dynDNSRepository,
|
|
|
|
private readonly PanelRepository $panelRepository,
|
|
|
|
private readonly Logger $logger)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '';
|
|
|
|
$this->response = '';
|
|
|
|
$this->message = '';
|
|
|
|
$this->result = [];
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
2022-01-20 11:06:58 +01:00
|
|
|
|
|
|
|
/**
|
2022-02-06 17:58:52 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
#[OAT\Get(
|
2022-02-22 15:31:49 +01:00
|
|
|
path : '/domains',
|
2022-02-06 17:58:52 +01:00
|
|
|
operationId: 'getAllDomains',
|
|
|
|
description: 'Returns a list of all domains on this server.',
|
2022-02-22 15:31:49 +01:00
|
|
|
summary : 'Listing all domains.',
|
2022-02-06 17:58:52 +01:00
|
|
|
// security: [
|
|
|
|
// 'Authorization' => [
|
|
|
|
//
|
|
|
|
// "read:api"
|
|
|
|
// ]
|
|
|
|
// ],
|
2022-02-22 15:31:49 +01:00
|
|
|
servers : [],
|
|
|
|
tags : ['Domains'],
|
|
|
|
responses : [
|
2022-02-06 17:58:52 +01:00
|
|
|
new OAT\Response(
|
2022-02-22 15:31:49 +01:00
|
|
|
response : 200,
|
2022-02-06 17:58:52 +01:00
|
|
|
description: 'OK'
|
|
|
|
),
|
|
|
|
new OAT\Response(
|
2022-02-22 15:31:49 +01:00
|
|
|
response : 401,
|
2022-02-06 17:58:52 +01:00
|
|
|
description: 'API key is missing or invalid.'
|
|
|
|
),
|
|
|
|
new OAT\Response(
|
2022-02-22 15:31:49 +01:00
|
|
|
response : 404,
|
2022-02-06 17:58:52 +01:00
|
|
|
description: 'Domain not found.'
|
|
|
|
)]
|
|
|
|
)]
|
2022-03-01 16:42:34 +01:00
|
|
|
private function handleAllDomainsGetRequest(): void
|
2022-02-06 17:58:52 +01:00
|
|
|
{
|
|
|
|
$domains = $this->domainRepository->findAll();
|
|
|
|
$resultDomain = [];
|
|
|
|
foreach ($domains as $singleDomain) {
|
|
|
|
$domain = [
|
2022-02-22 16:04:40 +01:00
|
|
|
'id' => $singleDomain->getId(),
|
|
|
|
'name' => $singleDomain->getName(),
|
|
|
|
'panel' => $singleDomain->getPanel()
|
2022-02-06 17:58:52 +01:00
|
|
|
];
|
|
|
|
$resultDomain[] = $domain;
|
|
|
|
}
|
|
|
|
$this->result = $resultDomain;
|
|
|
|
}
|
|
|
|
|
2022-03-01 16:42:34 +01:00
|
|
|
/**
|
|
|
|
*/
|
2022-09-17 16:28:04 +02:00
|
|
|
private function handlePing(): void
|
2022-03-01 16:42:34 +01:00
|
|
|
{
|
|
|
|
if ($this->checkPassword()) {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '200 OK';
|
|
|
|
$this->response = 'pong';
|
2022-03-01 16:42:34 +01:00
|
|
|
} else {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '401 Unauthorized';
|
|
|
|
$this->message = 'API key is missing or invalid';
|
2022-03-01 16:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-10 20:05:42 +01:00
|
|
|
|
2022-03-01 16:42:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function handleDomains(): void
|
|
|
|
{
|
|
|
|
if ($this->checkPassword()) {
|
|
|
|
try {
|
|
|
|
match ($this->requestMethod) {
|
|
|
|
'GET' => $this->handleDomainsGetRequest(),
|
|
|
|
'POST' => $this->handleDomainsPostRequest(),
|
|
|
|
'PUT' => $this->handleDomainsPutRequest(),
|
|
|
|
'DELETE' => $this->handleDomainsDeleteRequest()
|
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
|
|
|
$this->status = '400 Bad Request';
|
|
|
|
$this->message = "unknown request method: $this->requestMethod";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 17:58:52 +01:00
|
|
|
|
|
|
|
/**
|
2022-02-01 20:39:46 +01:00
|
|
|
* @OA\Tag(name = "Server")
|
|
|
|
* @OA\Get(
|
|
|
|
* path = "/ping",
|
|
|
|
* summary = "Returning pong.",
|
|
|
|
* description = "Can be used to check API or server availability.",
|
|
|
|
* tags={"Server"},
|
|
|
|
* @OA\Response(response = "200", description = "OK"),
|
|
|
|
* @OA\Response(response = "401", description = "API key is missing or invalid."),
|
|
|
|
* security={
|
|
|
|
* {"Authorization":{"read"}}
|
|
|
|
* }
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @OA\Tag(name = "Domains")
|
|
|
|
* @OA\Put(
|
|
|
|
* path="/domains/{name}",
|
|
|
|
* summary="Updates a domain.",
|
|
|
|
* description="Updates a domain. Only supplied fields will be updated, existing won't be affected.",
|
|
|
|
* tags={"Domains"},
|
|
|
|
* @OA\Response(response="200", description="OK"),
|
|
|
|
* @OA\Response(response = "401", description = "API key is missing or invalid."),
|
|
|
|
* @OA\Response(response="404", description="Domain not found."),
|
|
|
|
* security={
|
|
|
|
* {"Authorization":{"read":"write"}}
|
|
|
|
* }
|
|
|
|
* )
|
|
|
|
* @OA\Delete (
|
|
|
|
* path="/domains/{name}",
|
|
|
|
* summary="Deletes a domain.",
|
|
|
|
* description="Deletes a domain.",
|
|
|
|
* tags={"Domains"},
|
|
|
|
* @OA\Response(response="200", description="OK"),
|
|
|
|
* @OA\Response(response = "401", description = "API key is missing or invalid."),
|
|
|
|
* @OA\Response(response="404", description="Domain not found."),
|
|
|
|
* security={
|
|
|
|
* {"Authorization":{"read":"write"}}
|
|
|
|
* }
|
|
|
|
* )
|
2022-09-17 16:28:04 +02:00
|
|
|
* @param string $requestMethod
|
|
|
|
* @param array $uri
|
|
|
|
*
|
2022-01-20 11:06:58 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2022-02-06 17:58:52 +01:00
|
|
|
|
2022-03-01 16:42:34 +01:00
|
|
|
#[
|
|
|
|
OAT\Get(
|
|
|
|
path : '/domains/{name}',
|
|
|
|
operationId: 'getSingleDomain',
|
|
|
|
description: 'Returns information of a single domain specified by its domain name.',
|
|
|
|
summary : 'Returns a single domain.',
|
|
|
|
security : [
|
|
|
|
],
|
|
|
|
tags : ['Domains'],
|
|
|
|
parameters : [
|
|
|
|
new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')),
|
|
|
|
],
|
|
|
|
responses : [
|
|
|
|
new OAT\Response(
|
|
|
|
response : 200,
|
|
|
|
description: 'OK'
|
|
|
|
),
|
|
|
|
new OAT\Response(
|
|
|
|
response : 401,
|
|
|
|
description: 'API key is missing or invalid.'
|
|
|
|
),
|
|
|
|
new OAT\Response(
|
|
|
|
response : 404,
|
|
|
|
description: 'Domain not found.'
|
|
|
|
)]
|
|
|
|
|
|
|
|
)]
|
2022-09-17 16:28:04 +02:00
|
|
|
public function handleRequest(string $requestMethod, array $uri): void
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->logger->debug(message: "Request: $requestMethod $uri[1]");
|
|
|
|
|
|
|
|
$this->requestMethod = strtoupper(string: $requestMethod);
|
|
|
|
$this->uri = $uri;
|
|
|
|
|
2022-02-01 20:39:46 +01:00
|
|
|
$command = $this->uri[2];
|
|
|
|
|
2022-03-01 16:42:34 +01:00
|
|
|
if (empty($command) || !(($command == 'domains') || ($command == 'ping') || ($command == 'apidoc') || ($command == 'dyndns'))) {
|
2022-01-18 19:14:24 +01:00
|
|
|
$this->status = "404 Not Found";
|
|
|
|
$this->message = "Endpoint not found.";
|
|
|
|
} else {
|
2022-03-01 16:42:34 +01:00
|
|
|
try {
|
|
|
|
match ($command) {
|
|
|
|
'dyndns' => $this->handleDynDNS(),
|
|
|
|
'ping' => $this->handlePing(),
|
|
|
|
'domains' => $this->handleDomains(),
|
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '400 Bad Request';
|
2022-03-01 16:42:34 +01:00
|
|
|
$this->message = 'Unknown path: ' . $command;
|
2022-01-24 18:57:47 +01:00
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
2022-03-01 16:42:34 +01:00
|
|
|
|
2022-09-17 16:28:04 +02:00
|
|
|
if (!empty($this->status)) {
|
|
|
|
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->status);
|
2022-03-01 16:42:34 +01:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:28:04 +02:00
|
|
|
if (!empty($this->response)) {
|
|
|
|
echo json_encode(value: [
|
|
|
|
'response' => $this->response
|
|
|
|
]);
|
|
|
|
} elseif (!empty($this->result)) {
|
|
|
|
echo json_encode(value: [
|
|
|
|
'result' => $this->result
|
|
|
|
]);
|
|
|
|
} elseif (!empty($this->message)) {
|
|
|
|
echo json_encode(value: [
|
|
|
|
'message' => $this->message
|
|
|
|
]);
|
2022-03-01 16:42:34 +01:00
|
|
|
} else {
|
|
|
|
echo json_encode(value: [
|
2022-09-17 16:28:04 +02:00
|
|
|
'message' => $this->message ?? 'Error: No message.'
|
2022-03-01 16:42:34 +01:00
|
|
|
]);
|
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-03-01 16:42:34 +01:00
|
|
|
private function checkPassword(): bool
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-26 19:36:56 +01:00
|
|
|
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
|
|
|
|
$apiKey = $headers['X-API-KEY'] ?? '';
|
2022-09-22 18:53:29 +02:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
if (empty($apiKey)) {
|
|
|
|
$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->status = "401 Unauthorized";
|
|
|
|
$this->message = "API key mismatch.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
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-03-01 16:42:34 +01:00
|
|
|
|
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-03-01 16:42:34 +01:00
|
|
|
private function handleDomainsGetRequest(): void
|
2022-01-20 11:06:58 +01:00
|
|
|
{
|
2022-03-01 16:42:34 +01:00
|
|
|
$name = $this->uri[3] ?? '';
|
|
|
|
|
|
|
|
if ($name == 'name') {
|
2022-02-12 19:27:38 +01:00
|
|
|
if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
|
2022-01-31 20:49:44 +01:00
|
|
|
$domain = [
|
2022-02-22 15:31:49 +01:00
|
|
|
'id' => $result->getId(),
|
|
|
|
'name' => $result->getName(),
|
|
|
|
'panel' => $result->getPanel()
|
2022-01-31 20:49:44 +01:00
|
|
|
];
|
|
|
|
$this->result = $domain;
|
2022-01-22 17:32:36 +01:00
|
|
|
} else {
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "404 Not Found ";
|
|
|
|
$this->message = "The specified domain was not found.";
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-01 16:42:34 +01:00
|
|
|
if (empty($name)) {
|
2022-02-12 19:27:38 +01:00
|
|
|
$this->handleAllDomainsGetRequest();
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-03-01 16:42:34 +01:00
|
|
|
$id = intval(value: $name);
|
2022-02-13 15:47:23 +01:00
|
|
|
if ($id > 0) {
|
|
|
|
if ($result = $this->domainRepository->findById(id: $id)) {
|
|
|
|
$domain = [
|
2022-02-22 15:31:49 +01:00
|
|
|
'id' => $result->getId(),
|
|
|
|
'name' => $result->getName(),
|
|
|
|
'panel' => $result->getPanel()
|
2022-02-13 15:47:23 +01:00
|
|
|
];
|
|
|
|
$this->result = $domain;
|
|
|
|
} else {
|
|
|
|
$this->status = "404 Not Found ";
|
|
|
|
$this->message = "The specified domain was not found.";
|
|
|
|
}
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = "400 Bad Request";
|
2022-02-13 15:47:23 +01:00
|
|
|
$this->message = "You need to supply an ID or user the /domain/name/<name> path.";
|
|
|
|
|
2022-01-20 11:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 15:31:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-03-01 16:42:34 +01:00
|
|
|
private function handleDomainsPostRequest(): void
|
2022-02-22 15:31:49 +01:00
|
|
|
{
|
|
|
|
$name = $_POST['name'] ?? '';
|
|
|
|
$panel = $_POST['panel'] ?? '';
|
|
|
|
if (empty($name)) {
|
|
|
|
$this->status = "400 Bad Request";
|
|
|
|
$this->message = "A name is required";
|
|
|
|
} else {
|
|
|
|
if (empty($panel)) {
|
2022-02-12 19:27:38 +01:00
|
|
|
$this->status = "400 Bad Request";
|
2022-02-22 15:31:49 +01:00
|
|
|
$this->message = "A panel ID is required.";
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-02-22 15:31:49 +01:00
|
|
|
if ($this->domainRepository->findByName(name: $name)) {
|
|
|
|
$this->status = "400 Bad request";
|
|
|
|
$this->message = "Domain: $name already exists.";
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-02-22 15:31:49 +01:00
|
|
|
$domain = new Domain(name: $name, panel: $panel);
|
|
|
|
if ($result = $this->domainRepository->insert(domain: $domain)) {
|
|
|
|
$this->status = "201 Created";
|
2022-02-22 16:39:14 +01:00
|
|
|
$this->domainController->createSlaveZoneFile(domain: $domain);
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-02-22 15:31:49 +01:00
|
|
|
$this->status = "500 Server error";
|
2022-01-20 11:06:58 +01:00
|
|
|
}
|
2022-02-22 15:31:49 +01:00
|
|
|
$this->message = $result;
|
2022-01-20 11:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 15:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-03-01 16:42:34 +01:00
|
|
|
private function handleDomainsPutRequest(): void
|
2022-02-22 15:31:49 +01:00
|
|
|
{
|
|
|
|
$putData = fopen(filename: 'php://input', mode: 'r');
|
2022-03-01 16:42:34 +01:00
|
|
|
$data = fread(stream: $putData, length: 8192);
|
2022-02-22 15:31:49 +01:00
|
|
|
$params = explode(separator: '&', string: $data);
|
2022-03-10 20:05:42 +01:00
|
|
|
|
2022-02-22 15:31:49 +01:00
|
|
|
foreach ($params as $param) {
|
|
|
|
[$key, $value] = explode(separator: '=', string: $param);
|
|
|
|
$put[$key] = $value;
|
|
|
|
}
|
|
|
|
$id = $put['id'] ?? 0;
|
|
|
|
$name = $put['name'] ?? '';
|
|
|
|
$panel = $put['panel'] ?? "";
|
2022-02-12 19:27:38 +01:00
|
|
|
|
2022-02-22 15:31:49 +01:00
|
|
|
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.";
|
2022-02-12 19:27:38 +01:00
|
|
|
} else {
|
2022-02-22 15:31:49 +01:00
|
|
|
$domain = new Domain(name: $name, panel: $panel, id: $id);
|
|
|
|
$this->domainRepository->update(domain: $domain);
|
|
|
|
$this->status = "201 Updated";
|
|
|
|
$this->message = "201 Updated";
|
2022-02-22 16:34:29 +01:00
|
|
|
$this->domainController->createSlaveZoneFile(domain: $domain);
|
2022-02-12 19:27:38 +01:00
|
|
|
}
|
2022-01-20 11:06:58 +01:00
|
|
|
}
|
2022-02-22 15:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-03-01 16:42:34 +01:00
|
|
|
private function handleDomainsDeleteRequest(): void
|
2022-02-22 15:31:49 +01:00
|
|
|
{
|
|
|
|
$deleteData = fopen(filename: 'php://input', mode: 'r');
|
|
|
|
$data = fread(stream: $deleteData, length: 512);
|
|
|
|
$params = explode(separator: '&', string: $data);
|
2022-01-20 11:06:58 +01:00
|
|
|
|
2022-02-22 15:31:49 +01:00
|
|
|
foreach ($params as $param) {
|
|
|
|
[$key, $value] = explode(separator: '=', string: $param);
|
|
|
|
$delete[$key] = $value;
|
|
|
|
}
|
2022-01-20 11:06:58 +01:00
|
|
|
|
2022-02-22 15:31:49 +01:00
|
|
|
$id = $delete['id'] ?? 0;
|
|
|
|
|
|
|
|
if ($id == 0) {
|
|
|
|
$this->status = "400 Bad Request";
|
|
|
|
$this->message = "You need to supply an ID.";
|
|
|
|
} else {
|
2022-02-06 17:58:52 +01:00
|
|
|
|
2022-02-22 15:31:49 +01:00
|
|
|
if (!$domain = $this->domainRepository->findByID(id: $id)) {
|
2022-01-20 11:06:58 +01:00
|
|
|
$this->status = "400 Bad Request";
|
2022-02-22 15:31:49 +01:00
|
|
|
$this->message = "There is no domain with ID $id.";
|
2022-01-20 11:06:58 +01:00
|
|
|
} else {
|
2022-02-22 15:31:49 +01:00
|
|
|
$this->domainRepository->delete(domain: $domain);
|
|
|
|
$this->status = "204 No content.";
|
|
|
|
$this->message = "The domain $id has been deleted.";
|
2022-01-20 11:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 15:31:49 +01:00
|
|
|
|
2022-03-01 16:42:34 +01:00
|
|
|
|
2022-09-17 16:28:04 +02:00
|
|
|
private function handleDynDNS(): void
|
2022-03-01 16:42:34 +01:00
|
|
|
{
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->logger->debug(message: 'handleDynDNS()');
|
2022-04-06 16:28:38 +02:00
|
|
|
|
2022-03-01 16:42:34 +01:00
|
|
|
if ($this->checkPassword()) {
|
|
|
|
$host = $this->uri[3] ?? '';
|
|
|
|
|
|
|
|
if (empty($host)) {
|
|
|
|
$this->status = '400 Bad Request';
|
|
|
|
} else {
|
|
|
|
$a = $_POST['a'] ?? '';
|
|
|
|
$aaaa = $_POST['aaaa'] ?? '';
|
|
|
|
|
|
|
|
if (empty($a) && empty($aaaa)) {
|
|
|
|
$address = $_SERVER['REMOTE_ADDR'];
|
|
|
|
|
|
|
|
if (filter_var(value: $address, filter: FILTER_VALIDATE_IP, options: FILTER_FLAG_IPV6)) {
|
|
|
|
$aaaa = $address;
|
|
|
|
} else {
|
|
|
|
$a = $address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->logger->debug(message: 'a: ' . $a);
|
|
|
|
$this->logger->debug(message: 'aaaa: ' . $aaaa);
|
2022-03-01 16:42:34 +01:00
|
|
|
|
|
|
|
|
2022-04-06 16:28:38 +02:00
|
|
|
$domainName = $this->getDomain(host: $host);
|
|
|
|
$hostName = str_replace(search: '.' . $domainName, replace: '', subject: $host);
|
|
|
|
if (!$domain = $this->domainRepository->findByName(name: $domainName)) {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '404 Not Found';
|
2022-04-06 16:28:38 +02:00
|
|
|
$this->message = 'Domain ' . $domainName . ' not found';
|
2022-03-01 16:42:34 +01:00
|
|
|
} else {
|
2022-04-06 16:28:38 +02:00
|
|
|
// check if address has changed
|
|
|
|
if ($dynDNS = $this->dynDNSRepository->findByName(name: $host)) {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->logger->debug(message: 'found host: ' . $host);
|
|
|
|
$this->logger->debug(message: "a: $a");
|
|
|
|
$this->logger->debug(message: "aaaa: $aaaa");
|
2022-04-06 16:28:38 +02:00
|
|
|
|
|
|
|
$ipChanged = false;
|
|
|
|
|
|
|
|
if (!empty($a)) {
|
|
|
|
if ($a != $dynDNS->getA()) {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->logger->debug(message: $a . ' != ' . $dynDNS->getA());
|
2022-04-06 16:28:38 +02:00
|
|
|
$dynDNS->setA(a: $a);
|
|
|
|
$ipChanged = true;
|
2022-03-01 16:42:34 +01:00
|
|
|
}
|
2022-04-06 16:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($aaaa)) {
|
|
|
|
if ($aaaa != $dynDNS->getAaaa()) {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->logger->debug(message: $aaaa . ' != ' . $dynDNS->getAaaa());
|
2022-04-06 16:28:38 +02:00
|
|
|
$dynDNS->setAaaa(aaaa: $aaaa);
|
|
|
|
$ipChanged = true;
|
2022-03-01 16:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-06 16:28:38 +02:00
|
|
|
|
2022-04-06 17:14:23 +02:00
|
|
|
if ($ipChanged) {
|
2022-04-06 16:28:38 +02:00
|
|
|
$this->dynDNSRepository->update(dynDNS: $dynDNS);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dynDNS = new DynDNS(name: $host, a: $a, aaaa: $aaaa);
|
|
|
|
$this->dynDNSRepository->insert(dynDNS: $dynDNS);
|
2022-04-06 17:14:23 +02:00
|
|
|
$ipChanged = true;
|
2022-03-01 16:42:34 +01:00
|
|
|
}
|
2022-04-06 16:28:38 +02:00
|
|
|
|
|
|
|
|
2022-04-06 17:14:23 +02:00
|
|
|
if ($ipChanged) {
|
|
|
|
$panel = $this->panelRepository->findByName(name: $domain->getPanel());
|
|
|
|
|
|
|
|
if (!empty($panel->getAaaa())) {
|
|
|
|
$domainData = $this->apiController->sendCommand(
|
|
|
|
requestType: 'GET',
|
|
|
|
serverName : $panel->getName(),
|
|
|
|
versionIP : 6,
|
|
|
|
apiKey : $panel->getApikey(),
|
|
|
|
command : 'domains/name/' . $domainName,
|
|
|
|
serverType : 'panel');
|
|
|
|
} else {
|
|
|
|
$domainData = $this->apiController->sendCommand(
|
|
|
|
requestType: 'GET',
|
|
|
|
serverName : $panel->getName(),
|
|
|
|
versionIP : 4,
|
|
|
|
apiKey : $panel->getApikey(),
|
|
|
|
command : 'domains/name/' . $domainName,
|
|
|
|
serverType : 'panel');
|
2022-04-06 16:28:38 +02:00
|
|
|
}
|
2022-04-06 17:14:23 +02:00
|
|
|
|
|
|
|
$domainDecodedData = json_decode(json: $domainData['data']);
|
|
|
|
$domainID = $domainDecodedData->id;
|
2022-04-06 16:28:38 +02:00
|
|
|
|
|
|
|
if (!empty($panel->getAaaa())) {
|
2022-04-06 17:14:23 +02:00
|
|
|
$dnsData = $this->apiController->sendCommand(
|
|
|
|
requestType: 'GET',
|
2022-04-06 16:28:38 +02:00
|
|
|
serverName : $panel->getName(),
|
|
|
|
versionIP : 6,
|
|
|
|
apiKey : $panel->getApikey(),
|
|
|
|
command : 'dns/' . $domainID,
|
2022-04-06 17:14:23 +02:00
|
|
|
serverType : 'panel');
|
2022-04-06 16:28:38 +02:00
|
|
|
} else {
|
2022-04-06 17:14:23 +02:00
|
|
|
$dnsData = $this->apiController->sendCommand(
|
|
|
|
requestType: 'GET',
|
2022-04-06 16:28:38 +02:00
|
|
|
serverName : $panel->getName(),
|
|
|
|
versionIP : 4,
|
|
|
|
apiKey : $panel->getApikey(),
|
|
|
|
command : 'dns/' . $domainID,
|
2022-04-06 17:14:23 +02:00
|
|
|
serverType : 'panel');
|
2022-04-06 16:28:38 +02:00
|
|
|
}
|
2022-04-06 17:14:23 +02:00
|
|
|
|
|
|
|
$dnsDataDecoded = json_decode(json: $dnsData['data']);
|
|
|
|
$soa = $dnsDataDecoded->records->soa;
|
|
|
|
$others = $dnsDataDecoded->records->other;
|
|
|
|
|
|
|
|
$hostFound = false;
|
|
|
|
|
|
|
|
$updateHost = function (object $host) use ($hostName, $a, $aaaa, &$hostFound) {
|
|
|
|
if ($host->host == $hostName) {
|
|
|
|
$hostFound = true;
|
|
|
|
if ($host->type == 'A') {
|
|
|
|
if (!empty($a)) {
|
|
|
|
$host->value = $a;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!empty($aaaa)) {
|
|
|
|
$host->value = $aaaa;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
array_map(callback: $updateHost, array: $others);
|
|
|
|
|
|
|
|
if ($hostFound) {
|
|
|
|
$newDnsData = json_encode(value: [
|
|
|
|
'records' => [
|
|
|
|
'soa' => $soa,
|
|
|
|
'other' => $others
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!empty($panel->getAaaa())) {
|
|
|
|
$result = $this->apiController->sendCommand(
|
|
|
|
requestType: 'PUT',
|
|
|
|
serverName : $panel->getName(),
|
|
|
|
versionIP : 6,
|
|
|
|
apiKey : $panel->getApikey(),
|
|
|
|
command : 'dns/' . $domainID,
|
|
|
|
serverType : 'panel',
|
|
|
|
body : json_decode(json: $newDnsData, associative: true)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$result = $this->apiController->sendCommand(
|
|
|
|
requestType: 'PUT',
|
|
|
|
serverName : $panel->getName(),
|
|
|
|
versionIP : 4,
|
|
|
|
apiKey : $panel->getApikey(),
|
|
|
|
command : 'dns/' . $domainID,
|
|
|
|
serverType : 'panel',
|
|
|
|
body : json_decode(json: $newDnsData, associative: true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($result['header'] == 200) {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '200 OK';
|
2022-04-06 17:14:23 +02:00
|
|
|
$this->message = 'DynDNS host successfully updated';
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '404 Not Found';
|
2022-04-06 17:14:23 +02:00
|
|
|
$this->message = 'Host ' . $hostName . ' not found';
|
2022-04-06 16:28:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-17 16:28:04 +02:00
|
|
|
$this->status = '204 No content';
|
2022-04-06 17:14:23 +02:00
|
|
|
$this->message = 'No content';
|
2022-03-10 20:05:42 +01:00
|
|
|
}
|
2022-03-01 16:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 18:53:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $host
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getDomain(string $host): string
|
|
|
|
{
|
|
|
|
$host = strtolower(string: trim(string: $host));
|
|
|
|
$count = substr_count(haystack: $host, needle: '.');
|
|
|
|
if ($count == 2) {
|
|
|
|
if (strlen(string: explode(separator: '.', string: $host)[1]) > 3) {
|
|
|
|
$host = explode(separator: '.', string: $host, limit: 2)[1];
|
|
|
|
}
|
|
|
|
} elseif ($count > 2) {
|
|
|
|
$host = $this->getDomain(host: explode(separator: '.', string: $host, limit: 2)[1]);
|
|
|
|
}
|
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-22 15:31:49 +01:00
|
|
|
}
|