2022-01-26 19:01:52 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-19 21:07:22 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
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");
|
2022-01-22 16:37:30 +01:00
|
|
|
|
2022-01-19 21:07:22 +01:00
|
|
|
// TODO add to all Controllers
|
2022-01-23 15:24:12 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
2022-01-19 21:07:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
use LucidFrame\Console\ConsoleTable;
|
|
|
|
use UnhandledMatchError;
|
|
|
|
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class BindAPI
|
|
|
|
{
|
2022-01-22 18:46:28 +01:00
|
|
|
private DatabaseConnection $databaseConnection;
|
2022-01-23 15:22:20 +01:00
|
|
|
private ApiKeys $apiUsers;
|
2022-01-19 21:07:22 +01:00
|
|
|
private DomainController $domainController;
|
2022-01-20 19:55:04 +01:00
|
|
|
private PanelController $panelController;
|
2022-01-22 13:16:26 +01:00
|
|
|
private NameserverController $nameserverController;
|
2022-01-22 16:37:30 +01:00
|
|
|
private CheckController $checkController;
|
2022-01-19 21:07:22 +01:00
|
|
|
|
2022-01-23 15:22:20 +01:00
|
|
|
|
|
|
|
public function __construct(private array $config, private int $argumentsCount, private array $arguments)
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
2022-01-22 18:46:28 +01:00
|
|
|
$this->databaseConnection = new DatabaseConnection(config: $this->config);
|
2022-01-26 19:01:52 +01:00
|
|
|
$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);
|
2022-01-22 16:37:30 +01:00
|
|
|
$this->checkController = new CheckController();
|
|
|
|
}
|
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
/**
|
|
|
|
* @return int|void
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$id = 0;
|
|
|
|
}
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-26 19:01:52 +01:00
|
|
|
* @param array $server
|
|
|
|
* @param String $type
|
2022-01-25 20:31:31 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-01-26 19:01:52 +01:00
|
|
|
public function checkPing(array $server, String $type): Bool
|
2022-01-25 20:31:31 +01:00
|
|
|
{
|
|
|
|
$error = false;
|
2022-01-26 19:01:52 +01:00
|
|
|
|
|
|
|
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');
|
2022-01-25 20:31:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config['verbose']) {
|
2022-01-26 19:01:52 +01:00
|
|
|
print(COLOR_YELLOW . str_pad(string: $server['name'], length: $maxName));
|
2022-01-23 15:22:20 +01:00
|
|
|
}
|
2022-01-22 18:52:17 +01:00
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
$a = $server['a'] ?? '';
|
2022-01-22 16:37:30 +01:00
|
|
|
if (!empty($a)) {
|
2022-01-23 15:22:20 +01:00
|
|
|
if ($this->config['verbose']) {
|
|
|
|
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 4, apiKey: $server['apikey'], command: 'ping', type: $type)) {
|
2022-01-23 15:22:20 +01:00
|
|
|
if ($this->config['verbose']) {
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($result['data'] == 'pong') {
|
|
|
|
echo COLOR_GREEN . $result['data'];
|
2022-01-23 15:22:20 +01:00
|
|
|
} else {
|
2022-01-25 20:31:31 +01:00
|
|
|
echo COLOR_BLUE . 'skip';
|
2022-01-23 15:22:20 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 16:37:30 +01:00
|
|
|
} else {
|
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
$aaaa = $server['aaaa'] ?? '';
|
2022-01-22 16:37:30 +01:00
|
|
|
if (!empty($aaaa)) {
|
2022-01-23 15:22:20 +01:00
|
|
|
if ($this->config['verbose']) {
|
2022-01-25 20:31:31 +01:00
|
|
|
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
|
2022-01-23 15:22:20 +01:00
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 6, apiKey: $server['apikey'], command: 'ping', type: $type)) {
|
2022-01-23 15:22:20 +01:00
|
|
|
if ($this->config['verbose']) {
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($result['data'] == 'pong') {
|
|
|
|
echo COLOR_GREEN . $result['data'];
|
2022-01-23 15:22:20 +01:00
|
|
|
} else {
|
2022-01-26 19:01:52 +01:00
|
|
|
echo COLOR_BLUE . $result['data']; // TODO 'skip';
|
2022-01-23 15:22:20 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 16:37:30 +01:00
|
|
|
} else {
|
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 15:22:20 +01:00
|
|
|
if ($this->config['verbose']) {
|
|
|
|
echo PHP_EOL;
|
|
|
|
}
|
2022-01-22 18:52:17 +01:00
|
|
|
return $error;
|
2022-01-19 21:07:22 +01:00
|
|
|
}
|
|
|
|
|
2022-01-22 13:16:26 +01:00
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
|
|
|
|
|
2022-01-23 15:22:20 +01:00
|
|
|
function handleChecks(String $subcommand)
|
2022-01-20 19:55:04 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
match ($subcommand) {
|
|
|
|
'permissions' => $this->handleCheckPermissions(),
|
2022-01-25 20:31:31 +01:00
|
|
|
'panels' => $this->handleCheckPanels(),
|
2022-01-20 19:55:04 +01:00
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
|
|
|
echo 'Unknown action: ' . $subcommand . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $domain
|
2022-01-27 15:14:14 +01:00
|
|
|
* @param $panel
|
2022-01-25 20:31:31 +01:00
|
|
|
*
|
2022-01-26 19:35:14 +01:00
|
|
|
* @return bool|void
|
2022-01-25 20:31:31 +01:00
|
|
|
*/
|
2022-01-27 15:14:14 +01:00
|
|
|
function checkNS(String $domain, $panel)
|
2022-01-25 20:31:31 +01:00
|
|
|
{
|
|
|
|
$nameServers = $this->nameserverController->findAll();
|
|
|
|
foreach($nameServers as $nameServer) {
|
2022-01-27 15:14:14 +01:00
|
|
|
if ($nameServer['name'] == 'ns1.24unix.net') {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
echo COLOR_DEFAULT . ' ' . $nameServer['name'];
|
2022-01-25 20:31:31 +01:00
|
|
|
if (!empty($nameServer['aaaa'])) {
|
|
|
|
$result = $this->checkController->sendCommand(serverName: $nameServer['name'],
|
|
|
|
versionIP: 6,
|
|
|
|
apiKey: $nameServer['apikey'],
|
|
|
|
command: 'domains/' . $domain,
|
|
|
|
type: 'nameserver');
|
|
|
|
} else {
|
|
|
|
$result = $this->checkController->sendCommand(serverName: $nameServer['name'],
|
|
|
|
versionIP: 4,
|
|
|
|
apiKey: $nameServer['apikey'],
|
|
|
|
command: 'domains',
|
|
|
|
type: 'nameserver' . $domain);
|
|
|
|
}
|
|
|
|
if ($result['header'] == 200) {
|
2022-01-27 15:14:14 +01:00
|
|
|
echo COLOR_GREEN . ' OK';
|
2022-01-25 20:31:31 +01:00
|
|
|
} else {
|
2022-01-27 15:14:14 +01:00
|
|
|
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';
|
|
|
|
}
|
2022-01-25 20:31:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
echo PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function handleCheckPanels()
|
|
|
|
{
|
|
|
|
$id = $this->getId();
|
2022-01-26 19:01:52 +01:00
|
|
|
print("id: $id " . PHP_EOL);
|
2022-01-25 20:31:31 +01:00
|
|
|
|
|
|
|
if ($id != 0) {
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($panel = $this->panelController->findByID(id: $id)) {
|
2022-01-25 20:31:31 +01:00
|
|
|
if (!empty($panel['aaaa'])) {
|
|
|
|
$result = $this->checkController->sendCommand(serverName: $panel['name'],
|
|
|
|
versionIP: 6,
|
|
|
|
apiKey: $panel['apikey'],
|
|
|
|
command: 'domains',
|
|
|
|
type: 'panel');
|
|
|
|
} else {
|
|
|
|
$result = $this->checkController->sendCommand(serverName: $panel['name'],
|
|
|
|
versionIP: 4,
|
|
|
|
apiKey: $panel['apikey'],
|
|
|
|
command: 'domains',
|
|
|
|
type: 'panel');
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
foreach ($domains as $domain) {
|
|
|
|
if ($domain->id_parent_domain == 0 && !str_contains(haystack: $domain->domain, needle: $panel['name'])) {
|
2022-01-26 19:01:52 +01:00
|
|
|
echo PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName);
|
2022-01-27 15:14:14 +01:00
|
|
|
$this->checkNS(domain: $domain->domain, panel: $panel);
|
2022-01-25 20:31:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo "Unknown Panel ID: $id" . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
} else {
|
|
|
|
echo "check all …" . PHP_EOL;
|
2022-01-25 20:31:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
function handleCheckPermissions()
|
|
|
|
{
|
2022-01-25 20:31:31 +01:00
|
|
|
$this->domainController->checkPermissions();
|
2022-01-20 19:55:04 +01:00
|
|
|
}
|
2022-01-19 21:07:22 +01:00
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
|
2022-01-19 21:07:22 +01:00
|
|
|
function runCommand()
|
|
|
|
{
|
2022-01-23 15:22:20 +01:00
|
|
|
if ($this->argumentsCount < 1) {
|
2022-01-19 21:07:22 +01:00
|
|
|
$this->showUsage();
|
2022-01-20 19:55:04 +01:00
|
|
|
exit(0);
|
2022-01-19 21:07:22 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 15:22:20 +01:00
|
|
|
if (str_contains(haystack: $this->arguments[0], needle: ':')) {
|
|
|
|
[$command, $subcommand] = explode(separator: ':', string: $this->arguments[0]);
|
2022-01-20 19:55:04 +01:00
|
|
|
} else {
|
2022-01-23 15:22:20 +01:00
|
|
|
$command = $this->arguments[0];
|
2022-01-20 19:55:04 +01:00
|
|
|
$subcommand = '';
|
|
|
|
}
|
2022-01-19 21:07:22 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
match ($command) {
|
2022-01-26 19:01:52 +01:00
|
|
|
'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)
|
2022-01-19 21:07:22 +01:00
|
|
|
};
|
|
|
|
} catch(UnhandledMatchError) {
|
|
|
|
echo 'Unknown command: ' . $command . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function showUsage(): void
|
|
|
|
{
|
2022-01-25 20:31:31 +01:00
|
|
|
echo COLOR_YELLOW . 'Usage:' . PHP_EOL;
|
|
|
|
echo COLOR_DEFAULT . "\t./bin/console {options} {arguments}" . PHP_EOL . PHP_EOL;
|
|
|
|
|
|
|
|
echo COLOR_YELLOW . 'Options:' . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t-v, --version\t\t" . COLOR_DEFAULT . "Display the version of the API" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t-V, --verbose\t\t" . COLOR_DEFAULT . "All :list command are auto-verbose" . PHP_EOL . PHP_EOL;
|
|
|
|
|
2022-01-23 15:22:20 +01:00
|
|
|
echo COLOR_YELLOW . "check" . COLOR_DEFAULT . "\t health checks the system can perform" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t check:permissions" . PHP_EOL;
|
2022-01-25 20:31:31 +01:00
|
|
|
echo COLOR_GREEN . "\t check:panels {ID} {fix}" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t check:domains {ID} {fix}" . PHP_EOL;
|
2022-01-23 15:22:20 +01:00
|
|
|
|
|
|
|
echo COLOR_YELLOW . "panels" . COLOR_DEFAULT . "\t all Keyhelp systems configured" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t panels:list" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t panels:create <name> {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t panels:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t panels:delete" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t panels:apiping {<ID>}" . PHP_EOL;
|
|
|
|
|
|
|
|
echo COLOR_YELLOW . "nameservers" . COLOR_DEFAULT . " available nameservers" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t nameservers:list" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t nameservers:create <name> {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
2022-01-25 20:31:31 +01:00
|
|
|
echo COLOR_GREEN . "\t nameservers:update <ID> {name=<name>} {panel_id=<ID>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
2022-01-23 15:22:20 +01:00
|
|
|
echo COLOR_GREEN . "\t nameservers:delete <ID>" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t nameservers:apiping {<ID>}" . PHP_EOL;
|
|
|
|
|
|
|
|
echo COLOR_YELLOW . "domains" . COLOR_DEFAULT . " domains this server is responsible for" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t domains:list" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t domains:create <name> {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t domains:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t domains:delete <ID>" . PHP_EOL;
|
|
|
|
|
|
|
|
echo COLOR_YELLOW . "apikeys" . COLOR_DEFAULT . "\t API keys for other nameservers" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t apikeys:list" . PHP_EOL;
|
2022-01-25 20:31:31 +01:00
|
|
|
echo COLOR_GREEN . "\t apikeys:create {name=<name>}" . PHP_EOL;
|
|
|
|
echo COLOR_GREEN . "\t apikeys:update <ID> name=<name>" . PHP_EOL;
|
2022-01-23 15:22:20 +01:00
|
|
|
echo COLOR_GREEN . "\t apikeys:delete <ID>" . PHP_EOL;
|
|
|
|
|
|
|
|
echo PHP_EOL . "\033[39me.g. ./bin/console apikeys:list" . PHP_EOL;
|
2022-01-20 19:55:04 +01:00
|
|
|
}
|
|
|
|
|
2022-01-22 13:16:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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(),
|
2022-01-26 19:01:52 +01:00
|
|
|
'apiping' => $this->handleAPIPing(type: 'nameserver')
|
2022-01-22 13:16:26 +01:00
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
|
|
|
echo 'Unknown action: ' . $subcommand . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
/**
|
|
|
|
* @param string $subcommand
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handlePanels(string $subcommand): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
match ($subcommand) {
|
|
|
|
'create' => $this->handlePanelsCreate(),
|
|
|
|
'list' => $this->handlePanelsList(),
|
|
|
|
'update' => $this->handlePanelsUpdate(),
|
|
|
|
'delete' => $this->handlePanelsDelete(),
|
2022-01-26 19:01:52 +01:00
|
|
|
'apiping' => $this->handleAPIPing(type: 'panel')
|
2022-01-20 19:55:04 +01:00
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
|
|
|
echo 'Unknown action: ' . $subcommand . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* TODO remove
|
|
|
|
*/
|
|
|
|
/*
|
2022-01-25 20:31:31 +01:00
|
|
|
function handleNameserversAPIPing()
|
2022-01-22 16:37:30 +01:00
|
|
|
{
|
2022-01-25 20:31:31 +01:00
|
|
|
// TODO get apikey from stdin
|
|
|
|
//$test = fgets(stream: STDIN);
|
|
|
|
//echo "received: $test" . PHP_EOL;
|
|
|
|
|
2022-01-22 16:37:30 +01:00
|
|
|
$error = false;
|
2022-01-22 18:52:17 +01:00
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$id = 0;
|
|
|
|
}
|
2022-01-22 16:37:30 +01:00
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
if ($id != 0) {
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($nameserver = $this->nameserverController->findByID(id: $id)) {
|
|
|
|
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
|
2022-01-25 20:31:31 +01:00
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($this->config['verbose']) {
|
|
|
|
echo 'Unknown panel ID: $id' . PHP_EOL;
|
|
|
|
}
|
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nameservers = $this->nameserverController->findAll();
|
|
|
|
foreach ($nameservers as $nameserver) {
|
2022-01-26 19:01:52 +01:00
|
|
|
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
|
2022-01-25 20:31:31 +01:00
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo PHP_EOL;
|
|
|
|
if ($error) {
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
*/
|
2022-01-25 20:31:31 +01:00
|
|
|
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
function handleAPIPing(String $type)
|
2022-01-25 20:31:31 +01:00
|
|
|
{
|
|
|
|
$error = false;
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
$id = $this->getId();
|
|
|
|
|
2022-01-22 16:37:30 +01:00
|
|
|
if ($id != 0) {
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($type == 'panel') {
|
|
|
|
$server = $this->panelController->findByID(id: $id);
|
|
|
|
} else {
|
|
|
|
$server = $this->nameserverController->findByID(id: $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($server) {
|
|
|
|
if (!$this->checkPing(server: $server, type: $type)) {
|
2022-01-23 15:22:20 +01:00
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($this->config['verbose']) {
|
2022-01-26 19:01:52 +01:00
|
|
|
echo "Unknown $type ID: $id" . PHP_EOL;
|
2022-01-23 15:22:20 +01:00
|
|
|
}
|
2022-01-22 18:52:17 +01:00
|
|
|
$error = true;
|
|
|
|
}
|
2022-01-22 16:37:30 +01:00
|
|
|
} else {
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($type == 'panel') {
|
|
|
|
$servers = $this->panelController->findAll();
|
|
|
|
} else {
|
|
|
|
$servers = $this->nameserverController->findAll();
|
|
|
|
}
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
if (!$this->checkPing(server: $server, type: $type)) {
|
2022-01-22 18:52:17 +01:00
|
|
|
$error = true;
|
|
|
|
}
|
2022-01-22 16:37:30 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 18:52:17 +01:00
|
|
|
echo PHP_EOL;
|
|
|
|
if ($error) {
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
exit(0);
|
|
|
|
}
|
2022-01-22 16:37:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handlePanelsCreate(): void
|
|
|
|
{
|
2022-01-23 18:59:16 +01:00
|
|
|
$name = $this->arguments[1] ?? '';
|
2022-01-20 19:55:04 +01:00
|
|
|
if (empty($name)) {
|
|
|
|
echo 'You need to supply the panel 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 DNS domain 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);
|
|
|
|
}
|
2022-01-23 15:22:20 +01:00
|
|
|
$apikey = $arguments['apikey'] ?? '';
|
2022-01-20 19:55:04 +01:00
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($this->panelController->findByName(name: $name)) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "Panel: $name already exists." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2022-01-26 19:01:52 +01:00
|
|
|
$result = $this->panelController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey);
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "Panel $name has been created with id $result" . PHP_EOL;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 13:16:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handleNameserversCreate(): void
|
|
|
|
{
|
2022-01-23 15:22:20 +01:00
|
|
|
$name = $this->arguments[1] ?? '';
|
2022-01-22 13:16:26 +01:00
|
|
|
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'] ?? '';
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($this->nameserverController->findByName(name: $name)) {
|
2022-01-22 13:16:26 +01:00
|
|
|
echo "Nameserver: $name already exists." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2022-01-26 19:01:52 +01:00
|
|
|
$result = $this->nameserverController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey);
|
2022-01-22 13:16:26 +01:00
|
|
|
echo "Nameserver $name has been created with id $result" . PHP_EOL;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function parseArguments(): array
|
|
|
|
{
|
|
|
|
$arguments = [];
|
2022-01-23 15:22:20 +01:00
|
|
|
foreach ($this->arguments as $argument) {
|
2022-01-20 19:55:04 +01:00
|
|
|
if (str_contains(haystack: $argument, needle: '=')) {
|
|
|
|
[$key, $value] = explode(separator: '=', string: $argument);
|
2022-01-26 19:01:52 +01:00
|
|
|
$arguments[strtolower(string: $key)] = $value;
|
2022-01-20 19:55:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handlePanelsList(): void
|
|
|
|
{
|
|
|
|
echo 'All available panels:' . PHP_EOL;
|
|
|
|
$panels = $this->panelController->findAll();
|
|
|
|
if ($panels) {
|
|
|
|
$table = new ConsoleTable();
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']);
|
2022-01-20 19:55:04 +01:00
|
|
|
foreach ($panels as $panel) {
|
|
|
|
$token = strtok(string: $panel['apikey'], token: '.');
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->addRow(data: [$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]);
|
2022-01-20 19:55:04 +01:00
|
|
|
}
|
|
|
|
$table->setPadding(value: 2);
|
|
|
|
$table->display();
|
|
|
|
} else {
|
|
|
|
echo 'No panels found.' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-19 21:07:22 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
function handlePanelsUpdate()
|
|
|
|
{
|
|
|
|
$arguments = $this->parseArguments();
|
|
|
|
|
2022-01-23 15:22:20 +01:00
|
|
|
$id = $this->arguments[1] ?? 0;
|
2022-01-20 19:55:04 +01:00
|
|
|
$name = $arguments['name'] ?? '';
|
|
|
|
$a = $arguments['a'] ?? '';
|
|
|
|
$aaaa = $arguments['aaaa'] ?? '';
|
|
|
|
$apikey = $arguments['apikey'] ?? '';
|
|
|
|
|
|
|
|
if ($id == 0) {
|
|
|
|
echo 'An ID is required' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if (!$this->panelController->findByID(id: $id)) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "Panel with ID : $id doesn't exist." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($this->panelController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $apikey) !== false) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo 'Panel has been updated' . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo 'Error while updating domain server.' . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePanelsDelete()
|
|
|
|
{
|
2022-01-23 15:22:20 +01:00
|
|
|
if (empty($this->arguments[1])) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "You need to supply an ID." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
$id = intval(value: $this->arguments[1]) ?? 0;
|
2022-01-20 19:55:04 +01:00
|
|
|
if ($id == 0) {
|
|
|
|
echo "Panel with ID $id not found." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if (!$this->panelController->findByID(id: $id)) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "There is no panel with ID $id." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
$this->panelController->delete(id: $id);
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "The panel with ID $id has been deleted." .PHP_EOL;
|
|
|
|
}
|
2022-01-19 21:07:22 +01:00
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
|
2022-01-19 21:07:22 +01:00
|
|
|
/**
|
2022-01-20 19:55:04 +01:00
|
|
|
* @param string $subcommand
|
2022-01-19 21:07:22 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-20 19:55:04 +01:00
|
|
|
public function handleApiKeys(string $subcommand): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
match ($subcommand) {
|
|
|
|
'create' => $this->handleApikeysCreate(),
|
|
|
|
'list' => $this->handleApikeysList(),
|
|
|
|
'delete' => $this->handleApikeysDelete(),
|
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
|
|
|
echo 'Unknown action: ' . $subcommand . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handleApikeysCreate(): void
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
2022-01-25 20:31:31 +01:00
|
|
|
$arguments = $this->parseArguments();
|
|
|
|
$name = $arguments['name'] ?? '';
|
|
|
|
|
|
|
|
$result = $this->apiUsers->create(name: $name);
|
2022-01-19 21:07:22 +01:00
|
|
|
echo 'API key ' . $result['row'] . ' has been generated. Store it in a save place, it cannot be recovered.' . PHP_EOL;
|
|
|
|
echo "\033[32m\t" . $result['tokenPrefix'] . '.' . $result['key'] . PHP_EOL;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-20 19:55:04 +01:00
|
|
|
function handleApikeysList(): void
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
|
|
|
echo 'All valid API keys:' . PHP_EOL;
|
2022-01-20 19:55:04 +01:00
|
|
|
$keys = $this->apiUsers->findAll();
|
2022-01-19 21:07:22 +01:00
|
|
|
if ($keys) {
|
|
|
|
$table = new ConsoleTable();
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->setHeaders(content: ['ID', 'Name', 'API key prefix']);
|
2022-01-19 21:07:22 +01:00
|
|
|
foreach ($keys as $key) {
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->addRow(data: [$key['id'], $key['name'], $key['api_token_prefix']]);
|
2022-01-19 21:07:22 +01:00
|
|
|
}
|
2022-01-20 19:55:04 +01:00
|
|
|
$table->setPadding(value: 2);
|
2022-01-19 21:07:22 +01:00
|
|
|
$table->display();
|
|
|
|
} else {
|
|
|
|
echo 'No keys found.' . PHP_EOL;
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-20 19:55:04 +01:00
|
|
|
function handleApikeysDelete(): void
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
2022-01-25 20:31:31 +01:00
|
|
|
$id = intval(value: $this->arguments[1] ?? 0);
|
2022-01-19 21:07:22 +01:00
|
|
|
if ($id == 0) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo 'You need to add the ID of the API key.' . PHP_EOL;
|
2022-01-19 21:07:22 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($this->apiUsers->findByID(id: $id)) {
|
|
|
|
$this->apiUsers->delete(id: $id);
|
2022-01-20 19:55:04 +01:00
|
|
|
echo 'API key ' . $id . ' has been deleted.' . PHP_EOL;
|
2022-01-19 21:07:22 +01:00
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
echo 'Unknown ID: ' . $id . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
|
2022-01-19 21:07:22 +01:00
|
|
|
/**
|
2022-01-20 19:55:04 +01:00
|
|
|
* @param string $subcommand
|
2022-01-19 21:07:22 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-20 19:55:04 +01:00
|
|
|
public function handleDomains(string $subcommand): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
match ($subcommand) {
|
|
|
|
'list' => $this->handleDomainsList(),
|
|
|
|
'create' => $this->handleDomainsCreate(),
|
|
|
|
'update' => $this->handleDomainsUpdate(),
|
|
|
|
'delete' => $this->handleDomainsDelete(),
|
|
|
|
};
|
|
|
|
} catch (UnhandledMatchError) {
|
|
|
|
echo "Unknown Command: $subcommand" . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 13:16:26 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handleNameserversList(): void
|
|
|
|
{
|
2022-01-23 15:22:20 +01:00
|
|
|
echo 'All available nameservers:' . PHP_EOL;
|
2022-01-22 13:16:26 +01:00
|
|
|
$domains = $this->nameserverController->findAll();
|
|
|
|
if ($domains) {
|
|
|
|
$table = new ConsoleTable();
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']);
|
2022-01-22 13:16:26 +01:00
|
|
|
foreach ($domains as $domain) {
|
2022-01-25 20:31:31 +01:00
|
|
|
$token = strtok(string: $domain['apikey'], token: '.');
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->addRow(data: [$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]);
|
2022-01-22 13:16:26 +01:00
|
|
|
}
|
|
|
|
$table->setPadding(value: 2);
|
|
|
|
$table->display();
|
|
|
|
} else {
|
|
|
|
echo 'No nameservers found.' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handleDomainsList(): void
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
|
|
|
echo 'All available domains:' . PHP_EOL;
|
2022-01-20 19:55:04 +01:00
|
|
|
$domains = $this->domainController->findAll();
|
2022-01-19 21:07:22 +01:00
|
|
|
if ($domains) {
|
|
|
|
$table = new ConsoleTable();
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->setHeaders(content: ['ID', 'Name', 'Panel', 'A', 'AAAA']);
|
2022-01-19 21:07:22 +01:00
|
|
|
foreach ($domains as $domain) {
|
2022-01-26 19:01:52 +01:00
|
|
|
$table->addRow(data: [$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]);
|
2022-01-19 21:07:22 +01:00
|
|
|
}
|
2022-01-20 19:55:04 +01:00
|
|
|
$table->setPadding(value: 2);
|
2022-01-19 21:07:22 +01:00
|
|
|
$table->display();
|
|
|
|
} else {
|
|
|
|
echo 'No domains found.' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-20 19:55:04 +01:00
|
|
|
function handleDomainsCreate(): void
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
2022-01-23 15:22:20 +01:00
|
|
|
$name = $this->arguments[1] ?? "";
|
2022-01-19 21:07:22 +01:00
|
|
|
if (empty($name)) {
|
|
|
|
echo 'You need to supply the domain name.' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
$filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME);
|
|
|
|
if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) {
|
2022-01-19 21:07:22 +01:00
|
|
|
$name = $filteredName;
|
|
|
|
} else {
|
|
|
|
echo "$name is no valid DNS domain name." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
$arguments = $this->parseArguments();
|
|
|
|
|
2022-01-19 21:07:22 +01:00
|
|
|
$a = $arguments['a'] ?? "";
|
|
|
|
$aaaa = $arguments['aaaa'] ?? "";
|
2022-01-25 20:31:31 +01:00
|
|
|
$panelID = $arguments['panel_id'] ?? 0;
|
|
|
|
|
|
|
|
if (empty($a) && empty($aaaa) && empty($panelID)) {
|
|
|
|
echo 'At least one IP address or panel ID is required.' . PHP_EOL;
|
2022-01-19 21:07:22 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($this->domainController->findByName(name: $name)) {
|
2022-01-19 21:07:22 +01:00
|
|
|
echo "Domain: $name already exists." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2022-01-26 19:01:52 +01:00
|
|
|
$result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "Domain $name has been created with id $result" . PHP_EOL;
|
2022-01-19 21:07:22 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 19:55:04 +01:00
|
|
|
function handleDomainsUpdate()
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
2022-01-20 19:55:04 +01:00
|
|
|
$arguments = $this->parseArguments();
|
|
|
|
|
2022-01-26 19:35:14 +01:00
|
|
|
$id = intval(value: $this->arguments[1] ?? 0);
|
2022-01-20 19:55:04 +01:00
|
|
|
$name = $arguments['name'] ?? '';
|
2022-01-25 20:31:31 +01:00
|
|
|
$panelID = intval(value: $arguments['panel_id'] ?? 0);
|
2022-01-20 19:55:04 +01:00
|
|
|
$a = $arguments['a'] ?? '';
|
|
|
|
$aaaa = $arguments['aaaa'] ?? '';
|
|
|
|
|
|
|
|
if ($id == 0) {
|
|
|
|
echo 'An ID is required' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if (!$this->domainController->findByID(id: $id)) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "Domain with ID : $id doesn't exist." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($this->domainController->update(id: $id, name: $name, panelID: $panelID, a: $a, aaaa: $aaaa) !== false) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo 'Domain server has been updated' . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo 'Error while updating domain server.' . PHP_EOL;
|
2022-01-19 21:07:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 13:16:26 +01:00
|
|
|
|
|
|
|
function handleNameserversUpdate()
|
|
|
|
{
|
|
|
|
$arguments = $this->parseArguments();
|
2022-01-23 15:22:20 +01:00
|
|
|
|
|
|
|
$id = $this->arguments[1] ?? 0;
|
2022-01-22 13:16:26 +01:00
|
|
|
$name = $arguments['name'] ?? '';
|
|
|
|
$a = $arguments['a'] ?? '';
|
|
|
|
$aaaa = $arguments['aaaa'] ?? '';
|
|
|
|
$apikey = $arguments['apikey'] ?? '';
|
|
|
|
|
|
|
|
if ($id == 0) {
|
|
|
|
echo 'An ID is required' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if (!$this->nameserverController->findByID(id: $id)) {
|
2022-01-22 13:16:26 +01:00
|
|
|
echo "Nameserver with ID : $id doesn't exist." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if ($this->nameserverController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $apikey) !== false) {
|
2022-01-22 13:16:26 +01:00
|
|
|
echo 'Nameserver server has been updated' . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo 'Error while updating nameserver.' . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleNameserversDelete()
|
|
|
|
{
|
2022-01-23 15:22:20 +01:00
|
|
|
if (empty($this->arguments[1])) {
|
2022-01-22 13:16:26 +01:00
|
|
|
echo "You need to supply an ID." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
$id = intval(value: $this->arguments[1]) ?? 0;
|
2022-01-22 13:16:26 +01:00
|
|
|
if ($id == 0) {
|
|
|
|
echo "Nameserver with ID $id not found." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if (!$this->nameserverController->findByID(id: $id)) {
|
2022-01-22 13:16:26 +01:00
|
|
|
echo "There is no nameserver with ID $id." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
$this->nameserverController->delete(id: $id);
|
2022-01-22 13:16:26 +01:00
|
|
|
echo "The nameserver with ID $id has been deleted." .PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function handleDomainsDelete()
|
2022-01-19 21:07:22 +01:00
|
|
|
{
|
2022-01-23 15:22:20 +01:00
|
|
|
if (empty($this->arguments[1])) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "You need to supply an ID." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
$id = intval(value: $this->arguments[1]) ?? 0;
|
2022-01-20 19:55:04 +01:00
|
|
|
if ($id == 0) {
|
|
|
|
echo "Domain with ID $id not found." . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
if (!$this->domainController->findByID(id: $id)) {
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "There is no domain with ID $id." . PHP_EOL;
|
2022-01-19 21:07:22 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
$this->domainController->delete(id: $id);
|
2022-01-20 19:55:04 +01:00
|
|
|
echo "The domain with ID $id has been deleted." .PHP_EOL;
|
2022-01-19 21:07:22 +01:00
|
|
|
}
|
|
|
|
}
|