diff --git a/composer.json b/composer.json index 3e9a1e9..69615ab 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "24unix/bindapi", "description": "manage Bind9 DNS server via REST API", "version": "2023.0.1", - "build_number": "331", + "build_number": "332", "authors": [ { "name": "Micha Espey", diff --git a/phinx.php b/phinx.php index e05ad65..f2bc5e8 100644 --- a/phinx.php +++ b/phinx.php @@ -4,7 +4,7 @@ require 'vendor/autoload.php'; - $configController = new ConfigController(); + $configController = new ConfigController(quiet: true); $dbHost = $configController->getConfig(configKey: 'dbHost'); $dbPort = $configController->getConfig(configKey: 'dbPort'); diff --git a/src/Controller/ApiController.php b/src/Controller/ApiController.php index f863575..b072c79 100644 --- a/src/Controller/ApiController.php +++ b/src/Controller/ApiController.php @@ -31,7 +31,7 @@ class ApiController 'nameserver' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command) }; } catch (UnhandledMatchError) { - echo 'Unhandled match: ' . $serverType; + exit('Unhandled match: ' . $serverType .' in ' . __FILE__ . ' on line ' . __LINE__ . PHP_EOL); } curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1); diff --git a/src/Controller/BindAPI.php b/src/Controller/BindAPI.php index c94d77d..bae2725 100755 --- a/src/Controller/BindAPI.php +++ b/src/Controller/BindAPI.php @@ -28,14 +28,14 @@ class BindAPI /** * @throws Exception */ - public function __construct() + public function __construct(bool $quiet) { // init the logger $dateFormat = "Y:m:d H:i:s"; $output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra% $formatter = new LineFormatter(format: $output, dateFormat: $dateFormat); - $debug = (new ConfigController)->getConfig(configKey: 'debug'); + $debug = (new ConfigController(quiet: $quiet))->getConfig(configKey: 'debug'); if ($debug) { $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Debug); } else { @@ -50,12 +50,14 @@ class BindAPI $containerBuilder = new ContainerBuilder(); $containerBuilder->addDefinitions([ - ConfigController::class => autowire(), + ConfigController::class => autowire() + ->constructorParameter(parameter: 'quiet', value: $quiet), CLIController::class => autowire() ->constructorParameter(parameter: 'logger', value: $this->logger), DomainController::class => autowire() - ->constructorParameter(parameter: 'logger', value: $this->logger), - DomainRepository::class => autowire() + ->constructorParameter(parameter: 'logger', value: $this->logger) + ->constructorParameter(parameter: 'quiet', value: $quiet), + DomainRepository::class => autowire() ->constructorParameter(parameter: 'logger', value: $this->logger), DynDnsRepository::class => autowire() ->constructorParameter(parameter: 'logger', value: $this->logger), diff --git a/src/Controller/CLIController.php b/src/Controller/CLIController.php index 1a72e4c..f61479a 100644 --- a/src/Controller/CLIController.php +++ b/src/Controller/CLIController.php @@ -296,7 +296,7 @@ function runCheckSetup(): void { - if (!$this->domainController->checkPermissions(quiet: true)) { + if (!$this->domainController->checkPermissions()) { echo COLOR_RED . 'You need to setup the bindAPI permission first.' . COLOR_DEFAULT . PHP_EOL; echo 'Run ' . COLOR_YELLOW . './bin/console check:setup' . COLOR_DEFAULT . ' as root or with sudo.' . PHP_EOL; } @@ -514,11 +514,14 @@ $encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey'); $decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey); - if (empty($panel->getA())) { + $f = $panel->getA(); + echo COLOR_DEFAULT . ' IPv4: ' . COLOR_YELLOW . $f . COLOR_DEFAULT; + + if (!empty($panel->getA())) { $panelRequest = $this->apiController->sendCommand( requestType: 'GET', serverName : $panel->getName(), - versionIP : 6, + versionIP : 4, apiKey : $decryptedKey, command : '/server', serverType : 'panel'); @@ -526,7 +529,7 @@ $panelRequest = $this->apiController->sendCommand( requestType: 'GET', serverName : $panel->getName(), - versionIP : 4, + versionIP : 6, apiKey : $decryptedKey, command : '/server', serverType : 'panel'); @@ -727,7 +730,9 @@ case 404: echo COLOR_RED . ' ' . $result['header'] . COLOR_DEFAULT; if (!empty($this->arguments['fix']) && $this->arguments['fix'] == 'yes') { - echo ' trying to fix …'; + if (!$this->quiet) { + echo ' trying to fix …'; + } $body = [ 'name' => $domainName, 'panel' => $panel->getName(), diff --git a/src/Controller/Commands/CommandGroupContainer.php b/src/Controller/Commands/CommandGroupContainer.php index 39e9ad7..919c389 100644 --- a/src/Controller/Commands/CommandGroupContainer.php +++ b/src/Controller/Commands/CommandGroupContainer.php @@ -75,7 +75,7 @@ class CommandGroupContainer // check for command group and print available commands foreach ($this->commandGroups as $group) { if ($group->getName() === $command) { - echo 'Available subcommands for : ' . COLOR_YELLOW . $group->getName() . COLOR_DEFAULT . ':' . PHP_EOL; + echo 'Available subcommands for: ' . COLOR_YELLOW . $group->getName() . COLOR_DEFAULT . ':' . PHP_EOL; $group->printCommands(strlen(string: $group->getName())); exit(0); } diff --git a/src/Controller/ConfigController.php b/src/Controller/ConfigController.php index 437b2c3..bd65ce7 100644 --- a/src/Controller/ConfigController.php +++ b/src/Controller/ConfigController.php @@ -9,7 +9,7 @@ class ConfigController { private array $config; - public function __construct( bool $test = false) + public function __construct(bool $quiet, bool $test = false) { if ($test) { @@ -46,17 +46,13 @@ class ConfigController $this->config = json_decode(json: $configJSON, associative: true); + $this->config['quiet'] = (bool)$quiet; $this->config['test'] = (bool)$test; } public function getConfig(string $configKey): string { - if ($configKey === 'quiet') { - echo 'FIXME: handle quiet …'; - return ''; - } else { - return $this->config[$configKey]; - } + return $this->config[$configKey]; } } diff --git a/src/Controller/DatabaseConnection.php b/src/Controller/DatabaseConnection.php index 4c7b66f..931c8ae 100644 --- a/src/Controller/DatabaseConnection.php +++ b/src/Controller/DatabaseConnection.php @@ -52,8 +52,8 @@ class DatabaseConnection $result = $statement->fetch(); if (empty($result)) { // ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`; - echo 'Error: Cannot find tables.' . PHP_EOL; - echo 'run the migration …' . PHP_EOL; + echo COLOR_RED . 'Error: ' . COLOR_DEFAULT . 'Cannot find tables.' . PHP_EOL; + echo 'Run the migration: ' . COLOR_YELLOW . './bin/console migrations:make' . COLOR_DEFAULT . PHP_EOL; } } catch (PDOException $exception) { echo $exception->getMessage() . PHP_EOL; diff --git a/src/Controller/DomainController.php b/src/Controller/DomainController.php index 482dac6..a0a1f95 100644 --- a/src/Controller/DomainController.php +++ b/src/Controller/DomainController.php @@ -29,7 +29,9 @@ class DomainController private readonly DomainRepository $domainRepository, private readonly PanelRepository $panelRepository, private readonly ConfigController $configController, - private readonly Logger $logger) + private readonly Logger $logger, + private readonly bool $quiet + ) { $this->localZoneFile = '/etc/bind/local.zones'; $this->localZonesDir = '/etc/bind/zones/'; @@ -78,8 +80,9 @@ class DomainController $domains = $this->domainRepository->findAll(); foreach ($domains as $domain) { - // FIXME check for quiet - echo 'Create zone: ' . $domain->getName() . PHP_EOL; + if (!$this->quiet) { + echo 'Create zone: ' . $domain->getName() . PHP_EOL; + } $this->createSlaveZoneFile(domain: $domain); } @@ -137,18 +140,12 @@ class DomainController } - function checkPermissions(bool $quiet = false, $impersonatedUserId = null): bool + function checkPermissions($impersonatedUserId = null): bool { $this->logger->debug(message: "checkPermissions()"); $setupIsValid = true; - if (!$quiet) { - $quiet = $this->configController->getConfig(configKey: 'quiet'); - } - -// echo 'quiet: ' . ($quiet ? 'true' : 'false') . PHP_EOL; - - if (!$quiet) { + if (!$this->quiet) { echo 'Checking permissions...' . PHP_EOL; } if ($impersonatedUserId) { @@ -156,12 +153,12 @@ class DomainController } else { $uid = posix_geteuid(); } - if (!$quiet) { + if (!$this->quiet) { echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL; } $pwuid = posix_getpwuid(user_id: $uid); $name = $pwuid['name']; - if (!$quiet) { + if (!$this->quiet) { echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL; } @@ -170,60 +167,60 @@ class DomainController } $members = $bindGroup['members'] ?? []; if (in_array(needle: $name, haystack: $members)) { - if (!$quiet) { + if (!$this->quiet) { echo "\t✅ $name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL; } } else { $setupIsValid = false; - if (!$quiet) { + if (!$this->quiet) { echo COLOR_RED . "\t❌$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL; } } - if (!$quiet) { + if (!$this->quiet) { echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL; } $localZoneFilePermissions = @fileperms(filename: $this->localZoneFile); if ($localZoneFilePermissions & 0x0010) { - if (!$quiet) { + if (!$this->quiet) { echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL; } } else { $setupIsValid = false; - if (!$quiet) { + if (!$this->quiet) { echo COLOR_RED . "\t❌Group needs write permission!" . COLOR_DEFAULT . PHP_EOL; } } - if (!$quiet) { + if (!$this->quiet) { echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; } if (file_exists(filename: $this->namedConfLocalFile) && $namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) { if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) { $setupIsValid = false; - if (!$quiet) { + if (!$this->quiet) { echo "\t❌ $this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; } } else { - if (!$quiet) { + if (!$this->quiet) { echo "\t✅ $this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; } } } else { $setupIsValid = false; - if (!$quiet) { + if (!$this->quiet) { echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL; } } - if (!$quiet) { + if (!$this->quiet) { echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL; } $localZoneDirPermissions = @fileperms(filename: $this->localZonesDir); if ($localZoneDirPermissions & 0x0010) { - if (!$quiet) { + if (!$this->quiet) { echo "\t✅ Group has write access." . PHP_EOL; } } else { $setupIsValid = false; - if (!$quiet) { + if (!$this->quiet) { echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL; } } diff --git a/src/Util/Console.php b/src/Util/Console.php index 30f858f..558623c 100644 --- a/src/Util/Console.php +++ b/src/Util/Console.php @@ -58,7 +58,7 @@ if (array_key_exists(key: 'q', array: $options) || array_key_exists(key: 'quiet' try { - $app = new BindAPI(); + $app = new BindAPI(quiet: $quiet); } catch (Exception $e) { echo 'Could not initialize the application: ' . $e->getMessage() . PHP_EOL; exit(1);