Compare commits

..

No commits in common. "6cf4c835935c8c6f06ad43c22d23b0d90af01d72" and "28763504f0b53778c0789d578c647d7ea60edd1d" have entirely different histories.

2 changed files with 14 additions and 145 deletions

View File

@ -26,7 +26,6 @@ class BindAPI
private ApiUsers $apiUsers;
private DomainController $domainController;
private PanelController $panelController;
private NameserverController $nameserverController;
public function __construct(private int $argc, private array $argv)
{
@ -34,10 +33,8 @@ class BindAPI
$this->panelController = new PanelController($this->dbConnection);
$this->apiUsers = new ApiUsers($this->dbConnection);
$this->domainController = new DomainController($this->dbConnection);
$this->nameserverController = new NameserverController($this->dbConnection);
}
function handleCheck(String $subcommand)
{
try {
@ -74,8 +71,7 @@ class BindAPI
'check' => $this->handleChecks($subcommand),
'panels' => $this->handlePanels($subcommand),
'apikeys' => $this->handleApiKeys($subcommand),
'domains' => $this->handleDomains($subcommand),
'nameservers' => $this->handleNameservers($subcommand)
'domains' => $this->handleDomains($subcommand)
};
} catch(UnhandledMatchError) {
echo 'Unknown command: ' . $command . PHP_EOL;
@ -101,41 +97,20 @@ class BindAPI
echo "\033[33mapikeys\t\033[39mAPI keys for other nameservers" . PHP_EOL;
echo "\033[32m\tapikeys:list" . PHP_EOL;
echo "\033[32m\tapikeys:create" . PHP_EOL;
echo "\033[32m\tapikeys:delete <ID>" . PHP_EOL;
echo "\033[32m\tnameservers:delete <ID>" . PHP_EOL;
echo "\033[33mnameservers\t\033[39mother nameservers" . PHP_EOL;
echo "\033[32m\tnameservers:list" . PHP_EOL;
echo "\033[32m\tnameservers:create <name> {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
echo "\033[32m\tnameservers:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
echo "\033[32m\tnameservers:create" . PHP_EOL;
echo "\033[32m\tnameservers:delete <ID>" . PHP_EOL;
echo "\033[33mdomains\t\033[39mdomains this server is responsible for" . PHP_EOL;
echo "\033[32m\tdomains:list" . PHP_EOL;
echo "\033[32m\tdomains:create <name> {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
echo "\033[32m\tdomains:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
echo "\033[32m\tdomains:delete <ID>" . PHP_EOL;
echo "\033[32m\tdomains:delete" . PHP_EOL;
echo "\033[32m\tdomains:check" . PHP_EOL;
echo PHP_EOL . "\033[39me.g. ./bin/console apikey:list" . PHP_EOL;
}
/**
* @param string $subcommand
*
* @return void
*/
public function handleNameservers(string $subcommand): void
{
try {
match ($subcommand) {
'create' => $this->handleNameserversCreate(),
'list' => $this->handleNameserversList(),
'update' => $this->handleNameserversUpdate(),
'delete' => $this->handleNameserversDelete(),
};
} catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL;
}
}
/**
* @param string $subcommand
*
@ -194,47 +169,6 @@ class BindAPI
}
}
/**
* @return void
*/
function handleNameserversCreate(): void
{
$name = $this->argv[2] ?? '';
if (empty($name)) {
echo 'You need to supply the nameserver name.' . PHP_EOL;
exit(1);
}
$filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME);
if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) {
$name = $filteredName;
} else {
echo "$name is no valid nameserver name." . PHP_EOL;
exit(1);
}
$arguments = $this->parseArguments();
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
if (empty($a) && empty($aaaa)) {
echo 'At least one IP address is required.' . PHP_EOL;
exit(0);
}
$apikey = $argiments['apikey'] ?? '';
if ($this->nameserverController->findByName($name)) {
echo "Nameserver: $name already exists." . PHP_EOL;
exit(1);
} else {
$result = $this->nameserverController->insert($name, $a, $aaaa, $apikey);
echo "Nameserver $name has been created with id $result" . PHP_EOL;
exit(0);
}
}
/**
* @return array
*/
@ -408,29 +342,6 @@ class BindAPI
}
}
/**
* @return void
*/
function handleNameserversList(): void
{
echo 'All available nameserver:' . PHP_EOL;
$domains = $this->nameserverController->findAll();
if ($domains) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
foreach ($domains as $domain) {
$table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $domain['apikey']]);
}
$table->setPadding(value: 2);
$table->display();
} else {
echo 'No nameservers found.' . PHP_EOL;
exit(1);
}
exit(0);
}
/**
* @return void
*/
@ -515,55 +426,7 @@ class BindAPI
}
}
function handleNameserversUpdate()
{
$arguments = $this->parseArguments();
$id = $this->argv[2] ?? 0;
$name = $arguments['name'] ?? '';
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
$apikey = $arguments['apikey'] ?? '';
if ($id == 0) {
echo 'An ID is required' . PHP_EOL;
exit(1);
}
if (!$this->nameserverController->findByID($id)) {
echo "Nameserver with ID : $id doesn't exist." . PHP_EOL;
exit(1);
}
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;
}
}
function handleNameserversDelete()
{
if (empty($this->argv[2])) {
echo "You need to supply an ID." . PHP_EOL;
exit(1);
}
$id = intval($this->argv[2]) ?? 0;
if ($id == 0) {
echo "Nameserver with ID $id not found." . PHP_EOL;
exit(1);
}
if (!$this->nameserverController->findByID($id)) {
echo "There is no nameserver with ID $id." . PHP_EOL;
exit(1);
}
$this->nameserverController->delete($id);
echo "The nameserver with ID $id has been deleted." .PHP_EOL;
}
function handleDomainsDelete()
function handleDomainsDelete()
{
if (empty($this->argv[2])) {
echo "You need to supply an ID." . PHP_EOL;

View File

@ -12,15 +12,21 @@ class DatabaseConnection
{
private PDO $dbConnection;
public function __construct(private array $config)
public function __construct()
{
extract($this->config);
// get from config later
$dbHost = "localhost";
$dbPort = 3306;
$dbDatabase = "tfunix_db1";
$dbUser = "tfunix_db1";
$dbPassword = "aWeirder1";
try {
$this->dbConnection = new PDO(
dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
username: $dbUser,
password: $dbPassword
);
} catch (PDOException $exception) {
exit($exception->getMessage());