Compare commits
No commits in common. "662737318b4124c1a4613c6ab15f32fd25c5ee6c" and "383dc29ba5ba2b0b8139c68979af9813a3c26104" have entirely different histories.
662737318b
...
383dc29ba5
|
@ -1,8 +1,8 @@
|
|||
#!/usr/bin/keyhelp-php81
|
||||
<?php declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
exit;
|
||||
|
@ -94,7 +94,7 @@ function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n']
|
|||
echo '): ';
|
||||
|
||||
$handle = fopen(filename: "php://stdin", mode: 'r');
|
||||
$line = trim(string: fgetc(stream: $handle));
|
||||
$line = trim(fgetc(stream: $handle));
|
||||
fclose(stream: $handle);
|
||||
|
||||
if ($line == '') {
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
namespace App\Controller;
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
|
||||
// 61eec33c8829a.91a1d27d70ba26710092381a073d9e546597dff6033de272
|
||||
require dirname(path: __DIR__) . '/vendor/autoload.php';
|
||||
|
||||
// read config
|
||||
$configFile = dirname(path: __DIR__) . "/config.json";
|
||||
$configJSON = file_get_contents(filename: $configFile);
|
||||
$config = json_decode(json: $configJSON, associative: true);
|
||||
$configJSON = file_get_contents($configFile);
|
||||
$config = json_decode($configJSON, associative: true);
|
||||
|
||||
|
||||
// TODO only valid clients?
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
namespace App\Controller;
|
||||
|
||||
use Exception;
|
||||
use PDO;
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
|
||||
|
||||
define(constant_name: 'COLOR_RED', value: "\033[31m");
|
||||
define(constant_name: 'COLOR_GREEN', value: "\033[32m");
|
||||
define(constant_name: 'COLOR_YELLOW', value: "\033[33m");
|
||||
define(constant_name: 'COLOR_BLUE', value: "\033[34m");
|
||||
define(constant_name: 'COLOR_DEFAULT', value: "\033[39m");
|
||||
define('COLOR_RED', "\033[31m");
|
||||
define('COLOR_GREEN', "\033[32m");
|
||||
define('COLOR_YELLOW', "\033[33m");
|
||||
define('COLOR_BLUE', "\033[34m");
|
||||
define('COLOR_DEFAULT', "\033[39m");
|
||||
|
||||
// TODO add to all Controllers
|
||||
error_reporting(error_level: E_ALL);
|
||||
|
@ -37,10 +35,10 @@ class BindAPI
|
|||
public function __construct(private array $config, private int $argumentsCount, private array $arguments)
|
||||
{
|
||||
$this->databaseConnection = new DatabaseConnection(config: $this->config);
|
||||
$this->panelController = new PanelController(databaseConnection: $this->databaseConnection);
|
||||
$this->apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
|
||||
$this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController);
|
||||
$this->nameserverController = new NameserverController(databaseConnection: $this->databaseConnection);
|
||||
$this->panelController = new PanelController($this->databaseConnection);
|
||||
$this->apiUsers = new ApiKeys($this->databaseConnection);
|
||||
$this->domainController = new DomainController($this->databaseConnection, $this->panelController);
|
||||
$this->nameserverController = new NameserverController($this->databaseConnection);
|
||||
$this->checkController = new CheckController();
|
||||
}
|
||||
|
||||
|
@ -63,57 +61,50 @@ class BindAPI
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array $server
|
||||
* @param String $type
|
||||
* @param bool|array $panel
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkPing(array $server, String $type): Bool
|
||||
public function checkNSPing(bool|array $nameserver): Bool
|
||||
{
|
||||
$maxName = $this->nameserverController->getLongestEntry(field: 'name');
|
||||
$maxA = $this->nameserverController->getLongestEntry(field: 'a');
|
||||
$maxAAAA = $this->nameserverController->getLongestEntry(field: 'aaaa');
|
||||
|
||||
$error = false;
|
||||
|
||||
if ($type == 'nameserver') {
|
||||
$maxName = $this->nameserverController->getLongestEntry(field: 'name');
|
||||
$maxA = $this->nameserverController->getLongestEntry(field: 'a');
|
||||
$maxAAAA = $this->nameserverController->getLongestEntry(field: 'aaaa');
|
||||
} else {
|
||||
$maxName = $this->panelController->getLongestEntry(field: 'name');
|
||||
$maxA = $this->panelController->getLongestEntry(field: 'a');
|
||||
$maxAAAA = $this->panelController->getLongestEntry(field: 'aaaa');
|
||||
}
|
||||
|
||||
if ($this->config['verbose']) {
|
||||
print(COLOR_YELLOW . str_pad(string: $server['name'], length: $maxName));
|
||||
print(COLOR_YELLOW . str_pad(string: $nameserver['name'], length: $maxName));
|
||||
}
|
||||
|
||||
$a = $server['a'] ?? '';
|
||||
$a = $nameserver['a'] ?? '';
|
||||
if (!empty($a)) {
|
||||
if ($this->config['verbose']) {
|
||||
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
|
||||
}
|
||||
if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 4, apiKey: $server['apikey'], command: 'ping', type: $type)) {
|
||||
if ($result = $this->checkController->sendCommand(serverName: $nameserver['name'], versionIP: 4, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
|
||||
if ($this->config['verbose']) {
|
||||
if ($result['data'] == 'pong') {
|
||||
echo COLOR_GREEN . $result['data'];
|
||||
if ($result == 'pong') {
|
||||
echo COLOR_GREEN . $result;
|
||||
} else {
|
||||
echo COLOR_BLUE . 'skip';
|
||||
echo COLOR_BLUE . $result; // TODO 'skip';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
$aaaa = $server['aaaa'] ?? '';
|
||||
$aaaa = $nameserver['aaaa'] ?? '';
|
||||
if (!empty($aaaa)) {
|
||||
if ($this->config['verbose']) {
|
||||
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
|
||||
}
|
||||
if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 6, apiKey: $server['apikey'], command: 'ping', type: $type)) {
|
||||
if ($result = $this->checkController->sendCommand(serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
|
||||
if ($this->config['verbose']) {
|
||||
if ($result['data'] == 'pong') {
|
||||
echo COLOR_GREEN . $result['data'];
|
||||
if ($result == 'pong') {
|
||||
echo COLOR_GREEN . $result;
|
||||
} else {
|
||||
echo COLOR_BLUE . $result['data']; // TODO 'skip';
|
||||
echo COLOR_BLUE . $result; // TODO 'skip';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -127,6 +118,62 @@ class BindAPI
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool|array $panel
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkPanelPing(bool|array $panel): Bool
|
||||
{
|
||||
$maxName = $this->panelController->getLongestEntry(field: 'name');
|
||||
$maxA = $this->panelController->getLongestEntry(field: 'a');
|
||||
$maxAAAA = $this->panelController->getLongestEntry(field: 'aaaa');
|
||||
|
||||
$error = false;
|
||||
|
||||
if ($this->config['verbose']) {
|
||||
print(COLOR_YELLOW . str_pad(string: $panel['name'], length: $maxName));
|
||||
}
|
||||
|
||||
$a = $panel['a'] ?? '';
|
||||
if (!empty($a)) {
|
||||
if ($this->config['verbose']) {
|
||||
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
|
||||
}
|
||||
if ($result = $this->checkController->sendPanelCommand(serverName: $panel['name'], versionIP: 4, apiKey: $panel['apikey'], command: 'ping')) {
|
||||
if ($this->config['verbose']) {
|
||||
if ($result == 'pong') {
|
||||
echo COLOR_GREEN . $result;
|
||||
} else {
|
||||
echo COLOR_BLUE . 'skip';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
$aaaa = $panel['aaaa'] ?? '';
|
||||
if (!empty($aaaa)) {
|
||||
if ($this->config['verbose']) {
|
||||
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
|
||||
}
|
||||
if ($result = $this->checkController->sendPanelCommand(serverName: $panel['name'], versionIP: 6, apiKey: $panel['apikey'], command: 'ping')) {
|
||||
if ($this->config['verbose']) {
|
||||
if ($result == 'pong') {
|
||||
echo COLOR_GREEN . $result;
|
||||
} else {
|
||||
echo COLOR_BLUE . 'skip';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
if ($this->config['verbose']) {
|
||||
echo PHP_EOL;
|
||||
}
|
||||
return $error;
|
||||
}
|
||||
|
||||
|
||||
function handleChecks(String $subcommand)
|
||||
|
@ -150,8 +197,10 @@ class BindAPI
|
|||
function checkNS(String $domain)
|
||||
{
|
||||
$nameServers = $this->nameserverController->findAll();
|
||||
$i = 1;
|
||||
foreach($nameServers as $nameServer) {
|
||||
echo COLOR_DEFAULT . ' ' . $nameServer['name'];
|
||||
print("nameserver $i");
|
||||
$i++;
|
||||
if (!empty($nameServer['aaaa'])) {
|
||||
$result = $this->checkController->sendCommand(serverName: $nameServer['name'],
|
||||
versionIP: 6,
|
||||
|
@ -166,9 +215,9 @@ class BindAPI
|
|||
type: 'nameserver' . $domain);
|
||||
}
|
||||
if ($result['header'] == 200) {
|
||||
return true;
|
||||
print("OK");
|
||||
} else {
|
||||
return false;
|
||||
print("missing");
|
||||
}
|
||||
}
|
||||
echo PHP_EOL;
|
||||
|
@ -178,10 +227,13 @@ class BindAPI
|
|||
function handleCheckPanels()
|
||||
{
|
||||
$id = $this->getId();
|
||||
print("id: $id " . PHP_EOL);
|
||||
$maxDomain = $this->panelController->getLongestEntry(field:'name');
|
||||
|
||||
print("$maxDomain");
|
||||
die();
|
||||
|
||||
if ($id != 0) {
|
||||
if ($panel = $this->panelController->findByID(id: $id)) {
|
||||
if ($panel = $this->panelController->findByID($id)) {
|
||||
if (!empty($panel['aaaa'])) {
|
||||
$result = $this->checkController->sendCommand(serverName: $panel['name'],
|
||||
versionIP: 6,
|
||||
|
@ -196,19 +248,11 @@ class BindAPI
|
|||
type: 'panel');
|
||||
}
|
||||
|
||||
$domains = json_decode(json: $result['data']);
|
||||
$maxDomainName = 0;
|
||||
// TODO this is ugly code
|
||||
foreach ($domains as $domain) {
|
||||
if (($domain->id_parent_domain == 0 && !str_contains(haystack: $domain->domain, needle: $panel['name'])) && (strlen(string: $domain->domain) > $maxDomainName)) {
|
||||
$maxDomainName = strlen(string: $domain->domain);
|
||||
}
|
||||
}
|
||||
|
||||
$domains = json_decode($result['data']);
|
||||
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 PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad($domain->domain, $maxDomain);
|
||||
if ($this->checkNS($domain->domain)) {
|
||||
echo COLOR_GREEN . ' OK';
|
||||
} else {
|
||||
echo COLOR_RED . 'Missing';
|
||||
|
@ -219,8 +263,6 @@ class BindAPI
|
|||
echo "Unknown Panel ID: $id" . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
echo "check all …" . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -247,11 +289,11 @@ class BindAPI
|
|||
|
||||
try {
|
||||
match ($command) {
|
||||
'check' => $this->handleChecks(subcommand: $subcommand),
|
||||
'panels' => $this->handlePanels(subcommand: $subcommand),
|
||||
'apikeys' => $this->handleApiKeys(subcommand: $subcommand),
|
||||
'domains' => $this->handleDomains(subcommand: $subcommand),
|
||||
'nameservers' => $this->handleNameservers(subcommand: $subcommand)
|
||||
'check' => $this->handleChecks($subcommand),
|
||||
'panels' => $this->handlePanels($subcommand),
|
||||
'apikeys' => $this->handleApiKeys($subcommand),
|
||||
'domains' => $this->handleDomains($subcommand),
|
||||
'nameservers' => $this->handleNameservers($subcommand)
|
||||
};
|
||||
} catch(UnhandledMatchError) {
|
||||
echo 'Unknown command: ' . $command . PHP_EOL;
|
||||
|
@ -319,7 +361,8 @@ class BindAPI
|
|||
'list' => $this->handleNameserversList(),
|
||||
'update' => $this->handleNameserversUpdate(),
|
||||
'delete' => $this->handleNameserversDelete(),
|
||||
'apiping' => $this->handleAPIPing(type: 'nameserver')
|
||||
'apiping' => $this->handleNameserversAPIPing(),
|
||||
|
||||
};
|
||||
} catch (UnhandledMatchError) {
|
||||
echo 'Unknown action: ' . $subcommand . PHP_EOL;
|
||||
|
@ -336,22 +379,18 @@ class BindAPI
|
|||
{
|
||||
try {
|
||||
match ($subcommand) {
|
||||
'apiping' => $this->handlePanelsAPIPing(),
|
||||
'create' => $this->handlePanelsCreate(),
|
||||
'list' => $this->handlePanelsList(),
|
||||
'update' => $this->handlePanelsUpdate(),
|
||||
'delete' => $this->handlePanelsDelete(),
|
||||
'apiping' => $this->handleAPIPing(type: 'panel')
|
||||
};
|
||||
} catch (UnhandledMatchError) {
|
||||
echo 'Unknown action: ' . $subcommand . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* TODO remove
|
||||
*/
|
||||
/*
|
||||
|
||||
function handleNameserversAPIPing()
|
||||
{
|
||||
// TODO get apikey from stdin
|
||||
|
@ -372,8 +411,8 @@ class BindAPI
|
|||
}
|
||||
|
||||
if ($id != 0) {
|
||||
if ($nameserver = $this->nameserverController->findByID(id: $id)) {
|
||||
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
|
||||
if ($nameserver = $this->nameserverController->findByID($id)) {
|
||||
if (!$this->checkNSPing($nameserver)) {
|
||||
$error = true;
|
||||
}
|
||||
} else {
|
||||
|
@ -385,7 +424,7 @@ class BindAPI
|
|||
} else {
|
||||
$nameservers = $this->nameserverController->findAll();
|
||||
foreach ($nameservers as $nameserver) {
|
||||
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
|
||||
if (!$this->checkNSPing(nameserver: $nameserver)) {
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
|
@ -397,40 +436,37 @@ class BindAPI
|
|||
exit(0);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
function handleAPIPing(String $type)
|
||||
|
||||
function handlePanelsAPIPing()
|
||||
{
|
||||
$error = false;
|
||||
|
||||
$id = $this->getId();
|
||||
|
||||
if ($id != 0) {
|
||||
if ($type == 'panel') {
|
||||
$server = $this->panelController->findByID(id: $id);
|
||||
} else {
|
||||
$server = $this->nameserverController->findByID(id: $id);
|
||||
if (!empty($this->arguments[1])) {
|
||||
$id = intval(value: $this->arguments[1] ?? 0);
|
||||
if ($id != $this->arguments[1]) {
|
||||
echo 'ID has to be a number.' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ($server) {
|
||||
if (!$this->checkPing(server: $server, type: $type)) {
|
||||
} else {
|
||||
$id = 0;
|
||||
}
|
||||
if ($id != 0) {
|
||||
if ($panel = $this->panelController->findByID($id)) {
|
||||
if (!$this->checkPanelPing($panel)) {
|
||||
$error = true;
|
||||
}
|
||||
} else {
|
||||
if ($this->config['verbose']) {
|
||||
echo "Unknown $type ID: $id" . PHP_EOL;
|
||||
echo 'Unknown panel ID: $id' . PHP_EOL;
|
||||
}
|
||||
$error = true;
|
||||
}
|
||||
} else {
|
||||
if ($type == 'panel') {
|
||||
$servers = $this->panelController->findAll();
|
||||
} else {
|
||||
$servers = $this->nameserverController->findAll();
|
||||
}
|
||||
foreach ($servers as $server) {
|
||||
if (!$this->checkPing(server: $server, type: $type)) {
|
||||
$panels = $this->panelController->findAll();
|
||||
foreach ($panels as $panel) {
|
||||
if (!$this->checkPanelPing(panel: $panel)) {
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
|
@ -473,11 +509,11 @@ class BindAPI
|
|||
}
|
||||
$apikey = $arguments['apikey'] ?? '';
|
||||
|
||||
if ($this->panelController->findByName(name: $name)) {
|
||||
if ($this->panelController->findByName($name)) {
|
||||
echo "Panel: $name already exists." . PHP_EOL;
|
||||
exit(1);
|
||||
} else {
|
||||
$result = $this->panelController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey);
|
||||
$result = $this->panelController->insert($name, $a, $aaaa, $apikey);
|
||||
echo "Panel $name has been created with id $result" . PHP_EOL;
|
||||
exit(0);
|
||||
}
|
||||
|
@ -513,11 +549,11 @@ class BindAPI
|
|||
}
|
||||
$apikey = $argiments['apikey'] ?? '';
|
||||
|
||||
if ($this->nameserverController->findByName(name: $name)) {
|
||||
if ($this->nameserverController->findByName($name)) {
|
||||
echo "Nameserver: $name already exists." . PHP_EOL;
|
||||
exit(1);
|
||||
} else {
|
||||
$result = $this->nameserverController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey);
|
||||
$result = $this->nameserverController->insert($name, $a, $aaaa, $apikey);
|
||||
echo "Nameserver $name has been created with id $result" . PHP_EOL;
|
||||
exit(0);
|
||||
}
|
||||
|
@ -533,7 +569,7 @@ class BindAPI
|
|||
foreach ($this->arguments as $argument) {
|
||||
if (str_contains(haystack: $argument, needle: '=')) {
|
||||
[$key, $value] = explode(separator: '=', string: $argument);
|
||||
$arguments[strtolower(string: $key)] = $value;
|
||||
$arguments[strtolower($key)] = $value;
|
||||
}
|
||||
}
|
||||
return $arguments;
|
||||
|
@ -548,10 +584,10 @@ class BindAPI
|
|||
$panels = $this->panelController->findAll();
|
||||
if ($panels) {
|
||||
$table = new ConsoleTable();
|
||||
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']);
|
||||
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
|
||||
foreach ($panels as $panel) {
|
||||
$token = strtok(string: $panel['apikey'], token: '.');
|
||||
$table->addRow(data: [$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]);
|
||||
$table->addRow([$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]);
|
||||
}
|
||||
$table->setPadding(value: 2);
|
||||
$table->display();
|
||||
|
@ -576,11 +612,11 @@ class BindAPI
|
|||
echo 'An ID is required' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if (!$this->panelController->findByID(id: $id)) {
|
||||
if (!$this->panelController->findByID($id)) {
|
||||
echo "Panel with ID : $id doesn't exist." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if ($this->panelController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $apikey) !== false) {
|
||||
if ($this->panelController->update($id, $name, $a, $aaaa, $apikey) !== false) {
|
||||
echo 'Panel has been updated' . PHP_EOL;
|
||||
} else {
|
||||
echo 'Error while updating domain server.' . PHP_EOL;
|
||||
|
@ -594,20 +630,19 @@ class BindAPI
|
|||
exit(1);
|
||||
}
|
||||
|
||||
$id = intval(value: $this->arguments[1]) ?? 0;
|
||||
$id = intval($this->arguments[1]) ?? 0;
|
||||
if ($id == 0) {
|
||||
echo "Panel with ID $id not found." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if (!$this->panelController->findByID(id: $id)) {
|
||||
if (!$this->panelController->findByID($id)) {
|
||||
echo "There is no panel with ID $id." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
$this->panelController->delete(id: $id);
|
||||
$this->panelController->delete($id);
|
||||
echo "The panel with ID $id has been deleted." .PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $subcommand
|
||||
*
|
||||
|
@ -649,9 +684,9 @@ class BindAPI
|
|||
$keys = $this->apiUsers->findAll();
|
||||
if ($keys) {
|
||||
$table = new ConsoleTable();
|
||||
$table->setHeaders(content: ['ID', 'Name', 'API key prefix']);
|
||||
$table->setHeaders(['ID', 'Name', 'API key prefix']);
|
||||
foreach ($keys as $key) {
|
||||
$table->addRow(data: [$key['id'], $key['name'], $key['api_token_prefix']]);
|
||||
$table->addRow([$key['id'], $key['name'], $key['api_token_prefix']]);
|
||||
}
|
||||
$table->setPadding(value: 2);
|
||||
$table->display();
|
||||
|
@ -671,8 +706,8 @@ class BindAPI
|
|||
echo 'You need to add the ID of the API key.' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if ($this->apiUsers->findByID(id: $id)) {
|
||||
$this->apiUsers->delete(id: $id);
|
||||
if ($this->apiUsers->findByID($id)) {
|
||||
$this->apiUsers->delete($id);
|
||||
echo 'API key ' . $id . ' has been deleted.' . PHP_EOL;
|
||||
exit(0);
|
||||
} else {
|
||||
|
@ -681,7 +716,6 @@ class BindAPI
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $subcommand
|
||||
*
|
||||
|
@ -711,10 +745,10 @@ class BindAPI
|
|||
$domains = $this->nameserverController->findAll();
|
||||
if ($domains) {
|
||||
$table = new ConsoleTable();
|
||||
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']);
|
||||
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
|
||||
foreach ($domains as $domain) {
|
||||
$token = strtok(string: $domain['apikey'], token: '.');
|
||||
$table->addRow(data: [$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]);
|
||||
$table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]);
|
||||
}
|
||||
$table->setPadding(value: 2);
|
||||
$table->display();
|
||||
|
@ -735,9 +769,9 @@ class BindAPI
|
|||
$domains = $this->domainController->findAll();
|
||||
if ($domains) {
|
||||
$table = new ConsoleTable();
|
||||
$table->setHeaders(content: ['ID', 'Name', 'Panel', 'A', 'AAAA']);
|
||||
$table->setHeaders(['ID', 'Name', 'Panel', 'A', 'AAAA']);
|
||||
foreach ($domains as $domain) {
|
||||
$table->addRow(data: [$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]);
|
||||
$table->addRow([$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]);
|
||||
}
|
||||
$table->setPadding(value: 2);
|
||||
$table->display();
|
||||
|
@ -778,11 +812,11 @@ class BindAPI
|
|||
exit(0);
|
||||
}
|
||||
|
||||
if ($this->domainController->findByName(name: $name)) {
|
||||
if ($this->domainController->findByName($name)) {
|
||||
echo "Domain: $name already exists." . PHP_EOL;
|
||||
exit(1);
|
||||
} else {
|
||||
$result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
|
||||
$result = $this->domainController->insert($name, $panelID, $a, $aaaa);
|
||||
echo "Domain $name has been created with id $result" . PHP_EOL;
|
||||
exit(0);
|
||||
}
|
||||
|
@ -792,10 +826,9 @@ class BindAPI
|
|||
{
|
||||
$arguments = $this->parseArguments();
|
||||
|
||||
$id = intval(value: $this->arguments[1] ?? 0);
|
||||
$id = $this->arguments[1] ?? 0;
|
||||
$name = $arguments['name'] ?? '';
|
||||
print_r(value: $arguments); //findme
|
||||
print("$id: id" . PHP_EOL);
|
||||
print_r($arguments); //findme
|
||||
$panelID = intval(value: $arguments['panel_id'] ?? 0);
|
||||
$a = $arguments['a'] ?? '';
|
||||
$aaaa = $arguments['aaaa'] ?? '';
|
||||
|
@ -804,11 +837,11 @@ class BindAPI
|
|||
echo 'An ID is required' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if (!$this->domainController->findByID(id: $id)) {
|
||||
if (!$this->domainController->findByID($id)) {
|
||||
echo "Domain with ID : $id doesn't exist." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if ($this->domainController->update(id: $id, name: $name, panelID: $panelID, a: $a, aaaa: $aaaa) !== false) {
|
||||
if ($this->domainController->update($id, $name, $panelID, $a, $aaaa) !== false) {
|
||||
echo 'Domain server has been updated' . PHP_EOL;
|
||||
} else {
|
||||
echo 'Error while updating domain server.' . PHP_EOL;
|
||||
|
@ -816,6 +849,7 @@ class BindAPI
|
|||
}
|
||||
|
||||
|
||||
|
||||
function handleNameserversUpdate()
|
||||
{
|
||||
$arguments = $this->parseArguments();
|
||||
|
@ -830,11 +864,11 @@ class BindAPI
|
|||
echo 'An ID is required' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if (!$this->nameserverController->findByID(id: $id)) {
|
||||
if (!$this->nameserverController->findByID($id)) {
|
||||
echo "Nameserver with ID : $id doesn't exist." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if ($this->nameserverController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $apikey) !== false) {
|
||||
if ($this->nameserverController->update($id, $name, $a, $aaaa, $apikey) !== false) {
|
||||
echo 'Nameserver server has been updated' . PHP_EOL;
|
||||
} else {
|
||||
echo 'Error while updating nameserver.' . PHP_EOL;
|
||||
|
@ -848,16 +882,16 @@ class BindAPI
|
|||
exit(1);
|
||||
}
|
||||
|
||||
$id = intval(value: $this->arguments[1]) ?? 0;
|
||||
$id = intval($this->arguments[1]) ?? 0;
|
||||
if ($id == 0) {
|
||||
echo "Nameserver with ID $id not found." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if (!$this->nameserverController->findByID(id: $id)) {
|
||||
if (!$this->nameserverController->findByID($id)) {
|
||||
echo "There is no nameserver with ID $id." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
$this->nameserverController->delete(id: $id);
|
||||
$this->nameserverController->delete($id);
|
||||
echo "The nameserver with ID $id has been deleted." .PHP_EOL;
|
||||
}
|
||||
|
||||
|
@ -869,16 +903,16 @@ function handleDomainsDelete()
|
|||
exit(1);
|
||||
}
|
||||
|
||||
$id = intval(value: $this->arguments[1]) ?? 0;
|
||||
$id = intval($this->arguments[1]) ?? 0;
|
||||
if ($id == 0) {
|
||||
echo "Domain with ID $id not found." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
if (!$this->domainController->findByID(id: $id)) {
|
||||
if (!$this->domainController->findByID($id)) {
|
||||
echo "There is no domain with ID $id." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
$this->domainController->delete(id: $id);
|
||||
$this->domainController->delete($id);
|
||||
echo "The domain with ID $id has been deleted." .PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
<?php
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
namespace App\Controller;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -23,27 +22,27 @@ class CheckController
|
|||
{
|
||||
$curl = curl_init();
|
||||
if ($type == "panel") {
|
||||
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
|
||||
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
|
||||
} else {
|
||||
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command);
|
||||
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command);
|
||||
}
|
||||
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);
|
||||
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
|
||||
curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1);
|
||||
curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
|
||||
|
||||
if ($versionIP == 4) {
|
||||
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
|
||||
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
|
||||
} else {
|
||||
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
|
||||
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
|
||||
}
|
||||
|
||||
curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
|
||||
curl_setopt($curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
|
||||
|
||||
if ($resultJSON = curl_exec(handle: $curl)) {
|
||||
$httpResponse = curl_getinfo(handle: $curl)['http_code'];
|
||||
if ($resultJSON = curl_exec($curl)) {
|
||||
$httpResponse = curl_getinfo($curl)['http_code'];
|
||||
|
||||
switch($httpResponse) {
|
||||
case 200:
|
||||
$apiResult = json_decode(json: $resultJSON);
|
||||
$apiResult = json_decode($resultJSON);
|
||||
if ($command == "ping" ) {
|
||||
if ($apiResult->response == "pong") {
|
||||
$result = $apiResult->response;
|
||||
|
@ -61,9 +60,9 @@ class CheckController
|
|||
$result = 'Unhandled error: ' . $httpResponse;
|
||||
}
|
||||
} else {
|
||||
$result = curl_error(handle: $curl);
|
||||
$result = curl_error($curl);
|
||||
}
|
||||
curl_close(handle: $curl);
|
||||
curl_close($curl);
|
||||
return [
|
||||
'data' => $result,
|
||||
'header' => $httpResponse ?? ''
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
|
@ -22,7 +20,7 @@ class DatabaseConnection
|
|||
|
||||
public function __construct(private array $config)
|
||||
{
|
||||
extract(array: $this->config);
|
||||
extract($this->config);
|
||||
|
||||
try {
|
||||
$this->dbConnection = new PDO(
|
||||
|
@ -31,13 +29,13 @@ class DatabaseConnection
|
|||
password: $dbPassword
|
||||
);
|
||||
$sql = "SHOW TABLES";
|
||||
$statement = $this->dbConnection->prepare(query: $sql);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->execute();
|
||||
$result = $statement->fetch();
|
||||
if (empty($result)) {
|
||||
// ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`;
|
||||
echo 'Error: Cannot find tables.' . PHP_EOL;
|
||||
if (confirm(message: 'Should I try to create them?')) {
|
||||
if (confirm('Should I try to create them?')) {
|
||||
$sql = "
|
||||
CREATE TABLE `apikeys` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
|
@ -46,7 +44,7 @@ class DatabaseConnection
|
|||
`api_token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
||||
$statement = $this->dbConnection->prepare(query: $sql);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->execute();
|
||||
|
||||
$sql = "
|
||||
|
@ -58,7 +56,7 @@ class DatabaseConnection
|
|||
`aaaa` varbinary(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
||||
$statement = $this->dbConnection->prepare(query: $sql);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->execute();
|
||||
|
||||
$sql = "
|
||||
|
@ -70,7 +68,7 @@ class DatabaseConnection
|
|||
`apikey` varbinary(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
||||
$statement = $this->dbConnection->prepare(query: $sql);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->execute();
|
||||
|
||||
$sql = "
|
||||
|
@ -82,7 +80,7 @@ class DatabaseConnection
|
|||
`apikey` varbinary(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
||||
$statement = $this->dbConnection->prepare(query: $sql);
|
||||
$statement = $this->dbConnection->prepare($sql);
|
||||
$statement->execute();
|
||||
|
||||
echo 'Tables have been created.' . PHP_EOL;
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
|
@ -31,14 +29,13 @@ class DomainController
|
|||
*/
|
||||
public function findAll(): bool|array
|
||||
{
|
||||
$sql = "
|
||||
$statement = "
|
||||
SELECT id, name, panel_id, a, aaaa
|
||||
FROM " . DatabaseConnection::TABLE_DOMAINS . "
|
||||
ORDER BY name";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->execute();
|
||||
$statement = $this->databaseConnection->getConnection()->query($statement);
|
||||
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
|
@ -59,10 +56,10 @@ class DomainController
|
|||
WHERE name = :name";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
|
@ -82,10 +79,10 @@ class DomainController
|
|||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param:':id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
|
@ -98,9 +95,9 @@ class DomainController
|
|||
* @param String $a
|
||||
* @param String $aaaa
|
||||
*
|
||||
* @return string|false
|
||||
* @return int
|
||||
*/
|
||||
public function insert(String $name, int $panelID, String $a, String $aaaa): bool|string
|
||||
public function insert(String $name, int $panelID, String $a, String $aaaa): int
|
||||
{
|
||||
// TODO create zone file and include
|
||||
$sql = "
|
||||
|
@ -108,7 +105,7 @@ class DomainController
|
|||
VALUES (:name, :panel_id, :a, :aaaa)";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->bindParam(param: ':panel_d', var: $panelID);
|
||||
$statement->bindParam(param: ':a', var: $a);
|
||||
|
@ -116,7 +113,7 @@ class DomainController
|
|||
$statement->execute();
|
||||
|
||||
|
||||
if ($panel = $this->panelController->findByID(id: $panelID)) {
|
||||
if ($panel = $this->panelController->findByID($panelID)) {
|
||||
$a = $panel['a'];
|
||||
$aaaa = $panel['aaaa'];
|
||||
}
|
||||
|
@ -124,9 +121,9 @@ class DomainController
|
|||
$zoneFilename = $this->localZonesDir . $name;
|
||||
echo $zoneFilename . PHP_EOL;
|
||||
|
||||
if ($localZones = fopen(filename: $this->localZoneFile, mode: 'a')) {
|
||||
fputs(stream: $localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
|
||||
fclose(stream: $localZones);
|
||||
if ($localZones = fopen($this->localZoneFile, mode: 'a')) {
|
||||
fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
|
||||
fclose($localZones);
|
||||
} else {
|
||||
echo "Error writing to $this->localZoneFile, check permissions";
|
||||
exit(1);
|
||||
|
@ -150,7 +147,7 @@ class DomainController
|
|||
*/
|
||||
public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int
|
||||
{
|
||||
$current = $this->findByID(id: $id);
|
||||
$current = $this->findByID($id);
|
||||
|
||||
/* doesn't work
|
||||
$statement = "
|
||||
|
@ -185,7 +182,7 @@ class DomainController
|
|||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->bindParam(param: 'name', var: $name);
|
||||
$statement->bindParam(param: 'panel_id', var: $panelID);
|
||||
|
@ -194,7 +191,7 @@ class DomainController
|
|||
$statement->execute();
|
||||
|
||||
// recreate zonefile
|
||||
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
|
||||
if ($panel = $this->panelController->findByID($panelID)) {
|
||||
$a = $panel['a'];
|
||||
$aaaa = $panel['aaaa'];
|
||||
}
|
||||
|
@ -217,12 +214,12 @@ class DomainController
|
|||
public function delete($id): int
|
||||
{
|
||||
// TODO delete zone file and include
|
||||
$sql = "
|
||||
$statement = "
|
||||
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($statement);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->rowCount();
|
||||
|
@ -238,11 +235,11 @@ class DomainController
|
|||
*/
|
||||
public function getLongestEntry(String $field): int
|
||||
{
|
||||
$sql = "
|
||||
$statement = "
|
||||
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($statement);
|
||||
$statement->execute();
|
||||
$result = $statement->fetch();
|
||||
return $result['length'];
|
||||
|
@ -260,7 +257,7 @@ class DomainController
|
|||
$uid = posix_geteuid();
|
||||
print("UID:\t$uid" . PHP_EOL);
|
||||
|
||||
$pwuid = posix_getpwuid(user_id: $uid);
|
||||
$pwuid = posix_getpwuid($uid);
|
||||
$name = $pwuid['name'];
|
||||
print("Name:\t$name" . PHP_EOL);
|
||||
$bindGroup = posix_getgrnam(name: 'bind');
|
||||
|
@ -278,8 +275,8 @@ class DomainController
|
|||
}
|
||||
|
||||
echo "Checking $this->namedConfLocalFile" . PHP_EOL;
|
||||
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
||||
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
|
||||
if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) {
|
||||
if (!str_contains($namedConfLocal, $this->localZoneFile)) {
|
||||
echo "\t❌ $this->localZoneFile needs to be included in $this->namedConfLocalFile." . PHP_EOL;
|
||||
} else {
|
||||
echo "\t✅ $this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
|
||||
|
@ -303,12 +300,10 @@ class DomainController
|
|||
*/
|
||||
function checkDomains(): array|bool
|
||||
{
|
||||
return true;
|
||||
/*
|
||||
$domains = $this->findAll();
|
||||
|
||||
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
||||
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
|
||||
if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) {
|
||||
if (!str_contains($namedConfLocal, $this->localZoneFile)) {
|
||||
return "$this->localZoneFile needs to be included in $this->namedConfLocalFile.";
|
||||
}
|
||||
} else {
|
||||
|
@ -338,7 +333,6 @@ class DomainController
|
|||
} else {
|
||||
return $errors;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
@ -352,18 +346,18 @@ class DomainController
|
|||
public function createZoneFile(String $name, String $a, String $aaaa): void
|
||||
{
|
||||
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
|
||||
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
|
||||
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
|
||||
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
|
||||
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
|
||||
fputs($zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
|
||||
fputs($zonefile, data: "\ttype slave;" . PHP_EOL);
|
||||
fputs($zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
|
||||
fputs($zonefile, data: "\tmasters {" . PHP_EOL);
|
||||
if (!empty($a)) {
|
||||
fputs(stream: $zonefile, data: "\t\t$a;" . PHP_EOL);
|
||||
fputs($zonefile, data: "\t\t$a;" . PHP_EOL);
|
||||
}
|
||||
if (!empty($aaaa)) {
|
||||
fputs(stream: $zonefile, data: "\t\t$aaaa;" . PHP_EOL);
|
||||
fputs($zonefile, data: "\t\t$aaaa;" . PHP_EOL);
|
||||
}
|
||||
fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
|
||||
fputs(stream: $zonefile, data: "};" . PHP_EOL);
|
||||
fputs($zonefile, data: "\t};" . PHP_EOL);
|
||||
fputs($zonefile, data: "};" . PHP_EOL);
|
||||
}
|
||||
|
||||
// TODO check if ist exist in the include, else create
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
<?php
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
namespace App\Controller;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
@ -21,13 +20,12 @@ class NameserverController
|
|||
*/
|
||||
public function findAll(): bool|array
|
||||
{
|
||||
$sql = "
|
||||
$statement = "
|
||||
SELECT id, name, a, aaaa, apikey
|
||||
FROM " . DatabaseConnection::TABLE_NAMESERVERS;
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->execute();
|
||||
$statement = $this->databaseConnection->getConnection()->query($statement);
|
||||
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
|
@ -48,10 +46,10 @@ class NameserverController
|
|||
WHERE name = :name";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
|
@ -71,10 +69,10 @@ class NameserverController
|
|||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param:':id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
|
@ -87,16 +85,16 @@ class NameserverController
|
|||
* @param String $aaaa
|
||||
* @param String $apikey
|
||||
*
|
||||
* @return string|false
|
||||
* @return int
|
||||
*/
|
||||
public function insert(String $name, String $a, String $aaaa, String $apikey): bool|string
|
||||
public function insert(String $name, String $a, String $aaaa, String $apikey): int
|
||||
{
|
||||
$sql = "
|
||||
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey)
|
||||
VALUES (:name, :a, :aaaa, :apikey)";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->bindParam(param: ':a', var: $a);
|
||||
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
||||
|
@ -121,7 +119,7 @@ class NameserverController
|
|||
*/
|
||||
public function update(Int $id, String $name, String $a, String $aaaa, String $apikey): bool|int
|
||||
{
|
||||
$current = $this->findByID(id: $id);
|
||||
$current = $this->findByID($id);
|
||||
|
||||
if (empty($name)) {
|
||||
$name = $current['name'] ?? '';
|
||||
|
@ -145,7 +143,7 @@ class NameserverController
|
|||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->bindParam(param: 'name', var: $name);
|
||||
$statement->bindParam(param: 'a', var: $a);
|
||||
|
@ -167,12 +165,12 @@ class NameserverController
|
|||
*/
|
||||
public function delete($id): int
|
||||
{
|
||||
$sql = "
|
||||
$statement = "
|
||||
DELETE FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($statement);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->rowCount();
|
||||
|
@ -188,11 +186,11 @@ class NameserverController
|
|||
*/
|
||||
public function getLongestEntry(String $field): int
|
||||
{
|
||||
$sql = "
|
||||
$statement = "
|
||||
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_NAMESERVERS;
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($statement);
|
||||
$statement->execute();
|
||||
$result = $statement->fetch();
|
||||
return $result['length'];
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
|
@ -28,8 +26,7 @@ class PanelController
|
|||
FROM " . DatabaseConnection::TABLE_PANELS;
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement);
|
||||
$statement->execute();
|
||||
$statement = $this->databaseConnection->getConnection()->query($statement);
|
||||
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
|
@ -50,10 +47,10 @@ class PanelController
|
|||
WHERE name = :name";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
|
@ -73,7 +70,7 @@ class PanelController
|
|||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param:':id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
|
@ -89,16 +86,16 @@ class PanelController
|
|||
* @param String $aaaa
|
||||
* @param String $apikey
|
||||
*
|
||||
* @return string|false
|
||||
* @return int
|
||||
*/
|
||||
public function insert(String $name, String $a, String $aaaa, String $apikey): bool|string
|
||||
public function insert(String $name, String $a, String $aaaa, String $apikey): int
|
||||
{
|
||||
$sql = "
|
||||
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey)
|
||||
VALUES (:name, :a, :aaaa, :apikey)";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
$statement->bindParam(param: ':a', var: $a);
|
||||
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
||||
|
@ -123,7 +120,7 @@ class PanelController
|
|||
*/
|
||||
public function update(Int $id, String $name, String $a, String $aaaa, String $apikey): bool|int
|
||||
{
|
||||
$current = $this->findByID(id: $id);
|
||||
$current = $this->findByID($id);
|
||||
|
||||
if (empty($name)) {
|
||||
$name = $current['name'];
|
||||
|
@ -147,7 +144,7 @@ class PanelController
|
|||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->bindParam(param: 'name', var: $name);
|
||||
$statement->bindParam(param: 'a', var: $a);
|
||||
|
@ -175,7 +172,7 @@ class PanelController
|
|||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($statement);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->rowCount();
|
||||
|
@ -196,7 +193,7 @@ class PanelController
|
|||
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement);
|
||||
$statement = $this->databaseConnection->getConnection()->prepare($statement);
|
||||
$statement->execute();
|
||||
$result = $statement->fetch();
|
||||
return $result['length'];
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
<?php
|
||||
|
||||
error_reporting(error_level: E_ALL);
|
||||
namespace App\Controller;
|
||||
|
||||
use UnhandledMatchError;
|
||||
|
||||
|
@ -25,10 +24,10 @@ class RequestController
|
|||
*/
|
||||
public function __construct(private array $config, private String $requestMethod, private array $uri)
|
||||
{
|
||||
$this->requestMethod = strtoupper(string: $requestMethod);
|
||||
$this->databaseConnection = new DatabaseConnection(config: $this->config);
|
||||
$this->panelController = new PanelController(databaseConnection: $this->databaseConnection);
|
||||
$this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController);
|
||||
$this->requestMethod = strtoupper($requestMethod);
|
||||
$this->databaseConnection = new DatabaseConnection($this->config);
|
||||
$this->panelController = new PanelController($this->databaseConnection);
|
||||
$this->domainController = new DomainController($this->databaseConnection, $this->panelController);
|
||||
}
|
||||
|
||||
|
||||
|
@ -67,14 +66,14 @@ class RequestController
|
|||
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
|
||||
}
|
||||
if (!empty($this->result)) {
|
||||
echo json_encode(value: $this->result);
|
||||
echo json_encode($this->result);
|
||||
} else {
|
||||
if ($this->status == 'pong') {
|
||||
echo json_encode(value: [
|
||||
echo json_encode([
|
||||
'response' => $this->status
|
||||
]);
|
||||
} else {
|
||||
echo json_encode(value: [
|
||||
echo json_encode([
|
||||
'status' => $this->status ?? "Error: No status",
|
||||
'message' => $this->message ?? "Error: No message."
|
||||
]);
|
||||
|
@ -88,8 +87,8 @@ class RequestController
|
|||
*/
|
||||
public function checkPassword(): bool
|
||||
{
|
||||
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
|
||||
$apiKey = $headers['X-API-KEY'] ?? '';
|
||||
$headers = array_change_key_case(getallheaders(), CASE_UPPER);
|
||||
$apiKey = $headers['X-API-KEY'] ?? "";
|
||||
|
||||
if (empty($apiKey)) {
|
||||
$this->header = "401 Unauthorized";
|
||||
|
@ -97,12 +96,12 @@ class RequestController
|
|||
$this->message = "API key is missing.";
|
||||
return false;
|
||||
} else {
|
||||
[$prefix,] = explode(separator: '.', string: $apiKey);
|
||||
$apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
|
||||
$apiResult = $apiUsers->findByPrefix(prefix: $prefix);
|
||||
[$prefix,] = explode('.', $apiKey);
|
||||
$apiUsers = new ApiKeys($this->databaseConnection);
|
||||
$apiResult = $apiUsers->findByPrefix($prefix);
|
||||
$storedHash = $apiResult['api_token'];
|
||||
|
||||
if (!password_verify(password: $apiKey, hash: $storedHash)) {
|
||||
if (!password_verify($apiKey, $storedHash)) {
|
||||
$this->header = "401 Unauthorized";
|
||||
$this->status = "401 Unauthorized";
|
||||
$this->message = "API key mismatch.";
|
||||
|
@ -120,7 +119,7 @@ class RequestController
|
|||
if (empty($this->uri[3])) {
|
||||
$this->result = $this->domainController->findAll();
|
||||
} else {
|
||||
if ($result = $this->domainController->findByName(name: $this->uri[3])) {
|
||||
if ($result = $this->domainController->findByID(intval($this->uri[3]))) {
|
||||
$this->result = $result;
|
||||
} else {
|
||||
$this->header = "404 Not Found ";
|
||||
|
@ -148,11 +147,11 @@ class RequestController
|
|||
$this->status = "400 Bad Request";
|
||||
$this->message = "At least one IP address is required.";
|
||||
} else {
|
||||
if ($this->domainController->findByName(name: $name)) {
|
||||
if ($this->domainController->findByName($name)) {
|
||||
$this->status = "400 Bad request";
|
||||
$this->message = "Domain: $name already exists.";
|
||||
} else {
|
||||
$result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
|
||||
$result = $this->domainController->insert($name, $panelID, $a, $aaaa);
|
||||
$this->status = "201 Created";
|
||||
$this->message = $result;
|
||||
}
|
||||
|
@ -167,11 +166,11 @@ class RequestController
|
|||
public function handleDomainPutRequest(): void
|
||||
{
|
||||
$putData = fopen(filename: 'php://input', mode: 'r');
|
||||
$data = fread(stream: $putData, length: 512);
|
||||
$params = explode(separator: '&', string: $data);
|
||||
$data = fread($putData, 512);
|
||||
$params = explode('&', $data);
|
||||
|
||||
foreach ($params as $param) {
|
||||
[$key, $value] = explode(separator: '=', string: $param);
|
||||
[$key, $value] = explode('=', $param);
|
||||
$put[$key] = $value;
|
||||
}
|
||||
$id = $put['id'] ?? 0;
|
||||
|
@ -184,7 +183,7 @@ class RequestController
|
|||
$this->status = "400 Bad Request";
|
||||
$this->message = "An ID is required";
|
||||
} else {
|
||||
if (!$this->domainController->findByID(id: $id)) {
|
||||
if (!$this->domainController->findByID($id)) {
|
||||
$this->status = "404 Not Found";
|
||||
$this->message = "Domain with ID : $id doesn't exist.";
|
||||
} else {
|
||||
|
@ -197,10 +196,9 @@ class RequestController
|
|||
$this->status = "400 Bad Request";
|
||||
$this->message = "At least one IP address is required.";
|
||||
} else {
|
||||
$dcResult = $this->domainController->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa);
|
||||
$this->header = "201 Updated";
|
||||
$dcResult = $this->domainController->update($id, $panelID, $name, $a, $aaaa);
|
||||
$this->status = "201 Updated";
|
||||
$this->message = "201 Updated";
|
||||
$this->message = $dcResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -213,7 +211,7 @@ class RequestController
|
|||
public function handleDomainDeleteRequest(): void
|
||||
{
|
||||
$deleteData = fopen(filename: 'php://input', mode: 'r');
|
||||
$data = fread(stream: $deleteData, length: 512);
|
||||
$data = fread($deleteData, length: 512);
|
||||
$params = explode(separator: '&', string: $data);
|
||||
|
||||
foreach ($params as $param) {
|
||||
|
@ -224,17 +222,14 @@ class RequestController
|
|||
$id = $delete['id'] ?? 0;
|
||||
|
||||
if ($id == 0) {
|
||||
$this->header = "400 Bad Request";
|
||||
$this->status = "400 Bad Request";
|
||||
$this->status = "404 Bad Request";
|
||||
$this->message = "You need to supply an ID.";
|
||||
} else {
|
||||
if (!$this->domainController->findByID(id: $id)) {
|
||||
$this->header = "400 Bad Request";
|
||||
if (!$this->domainController->findByID($id)) {
|
||||
$this->status = "400 Bad Request";
|
||||
$this->message = "There is no domain with ID $id.";
|
||||
} else {
|
||||
$this->domainController->delete(id: $id);
|
||||
$this->header = "204 No content.";
|
||||
$this->domainController->delete($id);
|
||||
$this->status = "204 No content.";
|
||||
$this->message = "The domain $id has been deleted.";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue