Compare commits

..

6 Commits

Author SHA1 Message Date
tracer 2e779ed606 fixed messing with invalid api key
Signed-off-by: tracer <tracer@24unix.net>
2022-01-27 15:16:20 +01:00
tracer d1182c6243 added order
Signed-off-by: tracer <tracer@24unix.net>
2022-01-27 15:15:42 +01:00
tracer d8b2082778 improved handling/creation of zone files
Signed-off-by: tracer <tracer@24unix.net>
2022-01-27 15:15:19 +01:00
tracer 62790a01f3 added POST support
Signed-off-by: tracer <tracer@24unix.net>
2022-01-27 15:14:37 +01:00
tracer 283f5328df added ns fix
Signed-off-by: tracer <tracer@24unix.net>
2022-01-27 15:14:14 +01:00
tracer 491c26e80c fixed sql error
Signed-off-by: tracer <tracer@24unix.net>
2022-01-27 15:13:51 +01:00
6 changed files with 92 additions and 26 deletions

View File

@ -26,7 +26,8 @@ class ApiKeys
FROM " . DatabaseConnection::TABLE_USER;
try {
$statement = $this->databaseConnection->getConnection()->query(statement: $sql);
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) {
exit($e->getMessage());

View File

@ -144,13 +144,17 @@ class BindAPI
/**
* @param String $domain
* @param $panel
*
* @return bool|void
*/
function checkNS(String $domain)
function checkNS(String $domain, $panel)
{
$nameServers = $this->nameserverController->findAll();
foreach($nameServers as $nameServer) {
if ($nameServer['name'] == 'ns1.24unix.net') {
continue;
}
echo COLOR_DEFAULT . ' ' . $nameServer['name'];
if (!empty($nameServer['aaaa'])) {
$result = $this->checkController->sendCommand(serverName: $nameServer['name'],
@ -166,9 +170,39 @@ class BindAPI
type: 'nameserver' . $domain);
}
if ($result['header'] == 200) {
return true;
echo COLOR_GREEN . ' OK';
} else {
return false;
echo COLOR_RED . ' missing' . COLOR_DEFAULT;
$arguments = $this->parseArguments();
if (!empty($arguments['fix']) && $arguments['fix'] == 'yes') {
echo 'trying to fix …';
$body = [
'name' => $domain,
'panel_id' => $panel['id']
];
if (!empty($nameServer['aaaa'])) {
$result = $this->checkController->sendCommand(
serverName: $nameServer['name'],
versionIP: 6,
apiKey: $nameServer['apikey'],
command: 'domains',
type: 'nameserver',
post: true,
body: $body);
} else {
$result =$this->checkController->sendCommand(
serverName: $nameServer['name'],
versionIP: 4,
apiKey: $nameServer['apikey'],
command: 'domains',
type: 'nameserver',
post: true,
body: $body);
}
die();
} else {
echo 'error';
}
}
}
echo PHP_EOL;
@ -208,11 +242,7 @@ class BindAPI
foreach ($domains as $domain) {
if ($domain->id_parent_domain == 0 && !str_contains(haystack: $domain->domain, needle: $panel['name'])) {
echo PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName);
if ($this->checkNS(domain: $domain->domain)) {
echo COLOR_GREEN . ' OK';
} else {
echo COLOR_RED . 'Missing';
}
$this->checkNS(domain: $domain->domain, panel: $panel);
}
}
} else {
@ -794,8 +824,6 @@ class BindAPI
$id = intval(value: $this->arguments[1] ?? 0);
$name = $arguments['name'] ?? '';
print_r(value: $arguments); //findme
print("$id: id" . PHP_EOL);
$panelID = intval(value: $arguments['panel_id'] ?? 0);
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';

View File

@ -19,7 +19,7 @@ class CheckController
*
* @return array
*/
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command, String $type): array
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command, String $type, bool $post = false, array $body = []): array
{
$curl = curl_init();
if ($type == "panel") {
@ -38,6 +38,10 @@ class CheckController
curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
if ($post) {
curl_setopt($curl, option: CURLOPT_POST, value: true);
curl_setopt($curl, option: CURLOPT_POSTFIELDS, value: $body);
}
if ($resultJSON = curl_exec(handle: $curl)) {
$httpResponse = curl_getinfo(handle: $curl)['http_code'];
@ -58,6 +62,7 @@ class CheckController
$result = 'Missing or wrong API Key';
break;
default:
echo __FILE__, __LINE__, $resultJSON;
$result = 'Unhandled error: ' . $httpResponse;
}
} else {

View File

@ -102,7 +102,7 @@ class DomainController
*/
public function insert(String $name, int $panelID, String $a, String $aaaa): bool|string
{
// TODO create zone file and include
print("here");
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)";
@ -110,13 +110,13 @@ class DomainController
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_d', var: $panelID);
$statement->bindParam(param: ':panel_id', var: $panelID);
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->execute();
print(PHP_EOL . "there");
if ($panel = $this->panelController->findByID(id: $panelID)) {
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
@ -209,6 +209,19 @@ class DomainController
}
function createIncludeFile()
{
$domains = $this->findAll();
print("$this->localZoneFile");
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
foreach ($domains as $domain) {
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain['name'] . '";' . PHP_EOL);
}
fclose(stream: $oFile);
}
/**
* @param $id
*
@ -216,7 +229,15 @@ class DomainController
*/
public function delete($id): int
{
// TODO delete zone file and include
if ($domain = $this->findByID(id: $id)) {
$zoneFile = $this->localZonesDir . $domain['name'];
print($zoneFile . PHP_EOL);
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
print("file exists");
unlink(filename: $zoneFile);
}
}
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
@ -225,6 +246,8 @@ class DomainController
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->execute();
$this->createIncludeFile();
return $statement->rowCount();
} catch (PDOException $e) {
exit($e->getMessage());

View File

@ -23,7 +23,8 @@ class NameserverController
{
$sql = "
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS;
FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);

View File

@ -99,13 +99,18 @@ class RequestController
} else {
[$prefix,] = explode(separator: '.', string: $apiKey);
$apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
$apiResult = $apiUsers->findByPrefix(prefix: $prefix);
$storedHash = $apiResult['api_token'];
if (!password_verify(password: $apiKey, hash: $storedHash)) {
if ($apiResult = $apiUsers->findByPrefix(prefix: $prefix)) {
$storedHash = $apiResult['api_token'];
if (!password_verify(password: $apiKey, hash: $storedHash)) {
$this->header = "401 Unauthorized";
$this->status = "401 Unauthorized";
$this->message = "API key mismatch.";
return false;
}
} else {
$this->header = "401 Unauthorized";
$this->status = "401 Unauthorized";
$this->message = "API key mismatch.";
$this->message = "API key not found.";
return false;
}
}
@ -137,18 +142,21 @@ class RequestController
public function handleDomainPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panelID = $_POST['panel_id'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0);
$a = $_POST['a'] ?? '';
$aaaa = $_POST['aaaa'] ?? '';
if (empty($name)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else {
if (empty($a) && empty($aaaa)) {
if (empty($a) && empty($aaaa) && empty($panelID)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "At least one IP address is required.";
$this->message = "At least one IP address or panel ID is required.";
} else {
if ($this->domainController->findByName(name: $name)) {
$this->header = "400 Bad request";
$this->status = "400 Bad request";
$this->message = "Domain: $name already exists.";
} else {