bindAPI/bindAPI/bin/console

103 lines
2.9 KiB
Plaintext
Raw Normal View History

2022-01-18 19:14:24 +01:00
#!/usr/bin/keyhelp-php81
<?php
use App\Controller\ApiUsers;
use App\Controller\DatabaseConnection;
use App\Controller\DomainController;
// 61e6ce5dd8a1b.bc1c314ce364f6878084c254fe4c6345801c43a49bb8eb71
if ($argc < 2) {
echo 'Usage' . PHP_EOL;
echo "\033[33mapikeys" . 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[33mdomains" . 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} {A=<IPv4>} {AAAA=<IPv6>}" . 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;
exit(0);
}
require dirname(__DIR__) . '/vendor/autoload.php';
$dbConnection = (new DatabaseConnection())->getConnection();
$apiUsers = new ApiUsers($dbConnection);
$domainController = new DomainController($dbConnection);
[$command, $subcommand] = explode(':', $argv[1]);
switch($command) {
case 'apikeys':
switch($subcommand) {
case "create";
$result = $apiUsers->create();
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);
case "list":
2022-01-18 19:27:08 +01:00
echo 'All valid API keys:' . PHP_EOL;
2022-01-18 19:14:24 +01:00
$keys = $apiUsers->findAll();
if ($keys) {
2022-01-18 19:27:08 +01:00
$table = new \LucidFrame\Console\ConsoleTable();
$table->setHeaders(['ID', 'API key prefix']);
2022-01-18 19:14:24 +01:00
foreach ($keys as $key) {
2022-01-18 19:27:08 +01:00
$table->addRow([$key['id'], $key['api_token_prefix']]);
2022-01-18 19:14:24 +01:00
}
2022-01-18 19:27:08 +01:00
$table->setPadding(2);
$table->display();
2022-01-18 19:14:24 +01:00
} else {
echo 'No keys found.' . PHP_EOL;
}
exit(0);
case "delete":
$id = $argv[2] ?? 0;
if ($id == 0) {
echo 'You need to add the ID of the token.' .PHP_EOL;
exit(1);
}
if ($apiUsers->findByID($id)) {
$apiUsers->delete($id);
echo 'Token ' . $id . ' has been deleted.' . PHP_EOL;
exit(0);
} else {
echo 'Unknown ID: ' . $id . PHP_EOL;
exit(1);
}
default:
echo 'Unknown command: ' . $subcommand . PHP_EOL;
}
break;
case 'domains':
switch($subcommand) {
case 'list':
echo 'All available domains:' . PHP_EOL;
$domains = $domainController->findAll();
if ($domains) {
$table = new \LucidFrame\Console\ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA']);
foreach ($domains as $domain) {
$table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa']]);
}
$table->setPadding(2);
$table->display();
} else {
echo 'No domains found.' . PHP_EOL;
}
exit(0);
default:
echo("Unknown Command: $subcommand" . PHP_EOL);
exit(1);
}
default:
echo 'Unknown command: ' . $command . PHP_EOL;
}