Compare commits
3 Commits
1954361cf7
...
8aaf607ad4
Author | SHA1 | Date |
---|---|---|
tracer | 8aaf607ad4 | |
tracer | da1f7203b0 | |
tracer | fb6fc90bc1 |
|
@ -1,111 +1,20 @@
|
|||
#!/usr/bin/keyhelp-php81
|
||||
<?php
|
||||
|
||||
use App\Controller\ApiUsers;
|
||||
use App\Controller\DatabaseConnection;
|
||||
use App\Controller\DomainController;
|
||||
use CLIFramework\Component\Table\Table;
|
||||
|
||||
//echo $argc;
|
||||
//print_r($argv);
|
||||
|
||||
// 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);
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
exit;
|
||||
}
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
$dbConnection = (new DatabaseConnection())->getConnection();
|
||||
$apiUsers = new ApiUsers($dbConnection);
|
||||
$domainController = new DomainController($dbConnection);
|
||||
use App\Controller\BindAPI;
|
||||
|
||||
[$command, $subcommand] = explode(':', $argv[1]);
|
||||
$arguments = [
|
||||
'argc' => $argc,
|
||||
'argv' => $argv
|
||||
];
|
||||
|
||||
$app = new BindAPI(...$arguments);
|
||||
$app->runCommand();
|
||||
|
||||
//echo $command, $subcommand;
|
||||
|
||||
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":
|
||||
echo 'All valid API keys:' . PHP_EOL;
|
||||
$keys = $apiUsers->findAll();
|
||||
if ($keys) {
|
||||
$table = new \LucidFrame\Console\ConsoleTable();
|
||||
$table->setHeaders(['ID', 'API key prefix']);
|
||||
foreach ($keys as $key) {
|
||||
$table->addRow([$key['id'], $key['api_token_prefix']]);
|
||||
}
|
||||
$table->setPadding(2);
|
||||
$table->display();
|
||||
|
||||
} 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);
|
||||
|
||||
break;
|
||||
default:
|
||||
echo("Unknown Command: $subcommand" . PHP_EOL);
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
echo 'Unknown command: ' . $command . PHP_EOL;
|
||||
}
|
|
@ -0,0 +1,248 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
// TODO add to all Controllers
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// 61e6ce5dd8a1b.bc1c314ce364f6878084c254fe4c6345801c43a49bb8eb71
|
||||
|
||||
|
||||
use LucidFrame\Console\ConsoleTable;
|
||||
use PDO;
|
||||
use UnhandledMatchError;
|
||||
|
||||
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class BindAPI
|
||||
{
|
||||
|
||||
private PDO $dbConnection;
|
||||
private ApiUsers $apiUsers;
|
||||
private DomainController $domainController;
|
||||
|
||||
public function __construct(private int $argc, private array $argv)
|
||||
{
|
||||
|
||||
$this->dbConnection = (new DatabaseConnection())->getConnection();
|
||||
$this->apiUsers = new ApiUsers($this->dbConnection);
|
||||
$this->domainController = new DomainController($this->dbConnection);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function runCommand()
|
||||
{
|
||||
if ($this->argc < 2) {
|
||||
$this->showUsage();
|
||||
}
|
||||
|
||||
[$command, $subcommand] = explode(':', $this->argv[1]);
|
||||
|
||||
try {
|
||||
match ($command) {
|
||||
'apikeys' => $this->handleApiKeys($subcommand),
|
||||
'domains' => $this->handleDomains($subcommand)
|
||||
};
|
||||
} catch(UnhandledMatchError) {
|
||||
echo 'Unknown command: ' . $command . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
function showUsage(): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \App\Controller\ApiUsers $apiUsers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function handleApikeysCreate(ApiUsers $apiUsers): void
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ApiUsers $apiUsers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function handleApikeysList(ApiUsers $apiUsers): void
|
||||
{
|
||||
echo 'All valid API keys:' . PHP_EOL;
|
||||
$keys = $apiUsers->findAll();
|
||||
if ($keys) {
|
||||
$table = new ConsoleTable();
|
||||
$table->setHeaders(['ID', 'API key prefix']);
|
||||
foreach ($keys as $key) {
|
||||
$table->addRow([$key['id'], $key['api_token_prefix']]);
|
||||
}
|
||||
$table->setPadding(2);
|
||||
$table->display();
|
||||
|
||||
} else {
|
||||
echo 'No keys found.' . PHP_EOL;
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ApiUsers $apiUsers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function handleApikeysDelete(ApiUsers $apiUsers): void
|
||||
{
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \App\Controller\DomainController $domainController
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function handleDomainsList(DomainController $domainController): void
|
||||
{
|
||||
echo 'All available domains:' . PHP_EOL;
|
||||
$domains = $domainController->findAll();
|
||||
if ($domains) {
|
||||
$table = new 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(1);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \App\Controller\DomainController $domainController
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function handleDomainCreate(DomainController $domainController): void
|
||||
{
|
||||
$name = $this->argv[2] ?? "";
|
||||
if (empty($name)) {
|
||||
echo 'You need to supply the domain name.' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$filteredName = filter_var($name, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
|
||||
if (!empty($filteredName) && str_contains($filteredName, '.')) {
|
||||
$name = $filteredName;
|
||||
} else {
|
||||
echo "$name is no valid DNS domain name." . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
foreach ($this->argv as $argument) {
|
||||
if (str_contains($argument, '=')) {
|
||||
[$key, $value] = explode('=', $argument);
|
||||
$arguments[$key] = $value;
|
||||
}
|
||||
}
|
||||
$a = $arguments['a'] ?? "";
|
||||
$aaaa = $arguments['aaaa'] ?? "";
|
||||
if (empty($a) && empty($aaaa)) {
|
||||
echo 'At least one IP address is required.';
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if ($domainController->findByName($name)) {
|
||||
echo "Domain: $name already exists." . PHP_EOL;
|
||||
exit(1);
|
||||
} else {
|
||||
$dcResult = $domainController->insert($name, $a, $aaaa);
|
||||
echo "Domain $name has been created" . PHP_EOL;
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $subcommand
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handleApiKeys(string $subcommand): void
|
||||
{
|
||||
try {
|
||||
match ($subcommand) {
|
||||
'create' => $this->handleApikeysCreate($this->apiUsers),
|
||||
'list' => $this->handleApikeysList($this->apiUsers),
|
||||
'delete' => $this->handleApikeysDelete($this->apiUsers),
|
||||
};
|
||||
} catch (UnhandledMatchError $error) {
|
||||
echo 'Unknown action: ' . $subcommand . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $subcommand
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handleDomains(string $subcommand): void
|
||||
{
|
||||
try {
|
||||
match ($subcommand) {
|
||||
'list' => $this->handleDomainsList($this->domainController),
|
||||
'create' => $this->handleDomainCreate($this->domainController),
|
||||
|
||||
};
|
||||
} catch (UnhandledMatchError $error) {
|
||||
echo "Unknown Command: $subcommand" . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue