Compare commits

..

No commits in common. "46fdadf8e5314d5b49ee22229d19ebca3a5d5076" and "ac4cb3473ee778384728529de2b483c110b0dc68" have entirely different histories.

4 changed files with 477 additions and 713 deletions

40
bin/console Normal file → Executable file
View File

@ -1,4 +1,4 @@
#!/usr/bin/env php
#!/usr/bin/keyhelp-php81
<?php declare(strict_types=1);
namespace App\Controller;
@ -6,6 +6,7 @@ namespace App\Controller;
// & ~E_DEPRECATED is needed because of a bug in PhpStorm
use DI\DependencyException;
use DI\NotFoundException;
use App\Controller\CLIController;
use Exception;
error_reporting(error_level: E_ALL & ~E_DEPRECATED);
@ -14,16 +15,33 @@ if (php_sapi_name() !== 'cli') {
exit;
}
// version, store that somewhere else
$version = '0.0.1';
if (!is_file(filename: dirname(path: __DIR__).'/vendor/autoload.php')) {
die('Required runtime components are missing. Try running "composer install".' . PHP_EOL);
}
require dirname(path: __DIR__) . '/vendor/autoload.php';
$configFile = dirname(path: __DIR__) . "/config.json.local";
if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__) . "/config.json";
}
if (!file_exists(filename: $configFile)) {
echo 'Missing config file' . PHP_EOL;
if (confirm(message: 'Should I create a new config based on config.json.sample?')) {
copy(from: 'config.json.sample', to: 'config.json');
echo 'Config file has been generated. Adjust it to your needs, then proceed to database setup.' . PHP_EOL;
} else {
echo 'You first have to setup the bindAPI. Bye.' . PHP_EOL;
exit(0);
}
exit(1);
}
$configJSON = file_get_contents(filename: $configFile);
if (!$config = json_decode(json: $configJSON, associative: true)) {
echo 'Error parsing the config file.' . PHP_EOL;
echo $configJSON;
}
$shortOpts = 'v::'; // version
$shortOpts .= "V::"; // verbose
@ -48,22 +66,22 @@ if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help',
}
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$verbose = true;
$config['verbose'] = true;
} else {
$verbose = false;
$config['verbose'] = false;
}
$arguments = array_slice(array: $argv, offset: $restIndex);
try {
$app = new BindAPI(verbose: $verbose );
$app->runCommand(argumentsCount: count(value: $arguments), arguments: $arguments);
$app = new CLIController(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
} catch (DependencyException|NotFoundException|Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
}
$app->runCommand();
/**
* @param String $message

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,6 @@
namespace App\Repository;
use App\Controller\ConfigController;
use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use Monolog\Logger;
@ -14,12 +13,11 @@ use PDOException;
*/
class DomainRepository
{
public function __construct(
private readonly DatabaseConnection $databaseConnection,
private readonly ConfigController $configController,
private readonly Logger $logger)
public function __construct(private DatabaseConnection $databaseConnection, private array $config, private Logger $log)
{
$this->logger->debug(message: "DomainRepository::__construct()");
if ($this->config['debug']) {
$this->log->debug(message: "DomainRepository::__construct()");
}
}
@ -29,7 +27,9 @@ class DomainRepository
*/
public function findAll(): array
{
$this->logger->debug(message: "findAll()");
if ($this->config['debug']) {
$this->log->debug(message: "findAll()");
}
$domains = [];
$sql = "
@ -58,7 +58,9 @@ class DomainRepository
*/
public function findByID(int $id): bool|Domain
{
$this->logger->debug(message: "findById($id)");
if ($this->config['debug']) {
$this->log->debug(message: "findById($id)");
}
$sql = "
SELECT id, name, panel
@ -88,8 +90,9 @@ class DomainRepository
*/
public function findByName(string $name): Domain|bool
{
$this->logger->debug(message: "findByName($name)");
if ($this->config['debug']) {
$this->log->debug(message: "findByName($name)");
}
$sql = "
SELECT id, name, panel
FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -110,14 +113,17 @@ class DomainRepository
}
/**
* @param string $host
* @param String $name
*
* @return \App\Entity\Domain|bool
*/
public function findByHost(string $host): Domain|bool
{
$this->logger->debug(message: "findByHost($host)");
if ($this->config['debug']) {
$this->log->debug(message: "findByHost($host)");
}
$host = strtolower(string: trim(string: $host));
$count = substr_count(haystack: $host, needle: '.');
@ -128,7 +134,7 @@ class DomainRepository
} elseif ($count > 2) {
$host = $this->findByHost(host: explode(separator: '.', string: $host, limit: 2)[1]);
}
if ($domain = $this->findByName(name: $host)) {
return $domain;
} else {
@ -137,6 +143,7 @@ class DomainRepository
}
/**
* @param \App\Entity\Domain $domain
*
@ -144,8 +151,10 @@ class DomainRepository
*/
public function insert(Domain $domain): bool|string
{
$domainName = $domain->getName();
$this->logger->info(message: "insert($domainName)");
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "insert($domainName)");
}
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel)
@ -173,8 +182,10 @@ class DomainRepository
*/
public function update(Domain $domain): bool|int
{
$domainName = $domain->getName();
$this->logger->debug(message: "update($domainName)");
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "update($domainName)");
}
$id = $domain->getId();
$current = $this->findByID(id: $id);
@ -218,8 +229,10 @@ class DomainRepository
*/
public function delete(Domain $domain): int
{
$domainName = $domain->getName();
$this->logger->debug(message: "delete($domainName)");
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "delete($domainName)");
}
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -243,7 +256,7 @@ class DomainRepository
*
* @return int
*/
public function getLongestEntry(string $field): int
public function getLongestEntry(String $field): int
{
$sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
@ -251,10 +264,10 @@ class DomainRepository
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
$result = $statement->fetch();
$result = $statement->fetch();
return $result['length'];
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
}

View File

@ -3,7 +3,6 @@
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use App\Entity\Panel;
use PDO;
use PDOException;
@ -13,31 +12,9 @@ use PDOException;
*/
class PanelRepository
{
public function __construct(private readonly DatabaseConnection $databaseConnection)
public function __construct(private DatabaseConnection $databaseConnection)
{}
public function findSelf(): array
{
$sql = "
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE self = 1";
$panels = [];
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
$panels[] = $panel;
}
return $panels;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @return array
@ -46,7 +23,7 @@ class PanelRepository
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey, self
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name";
@ -54,7 +31,7 @@ class PanelRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
$panels[] = $panel;
}
return $panels;
@ -72,7 +49,7 @@ class PanelRepository
public function findByID(int $id): ?Panel
{
$sql = "
SELECT id, name, a, aaaa, apikey, self
SELECT id, name, a, aaaa, apikey
FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id";
@ -81,7 +58,7 @@ class PanelRepository
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
} else {
return null;
}
@ -99,7 +76,7 @@ class PanelRepository
public function findByName(string $name): Panel|bool
{
$sql = "
SELECT id, name, a, aaaa, apikey, self
SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name";
@ -108,7 +85,7 @@ class PanelRepository
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
} else {
return false;
}
@ -126,11 +103,11 @@ class PanelRepository
*
* @return string|false
*/
public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string
public function insert(string $name, string $a, string $aaaa, String $apikey): bool|string
{
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self)
VALUES (:name, :a, :aaaa, :apikey, :self)";
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
@ -138,7 +115,6 @@ class PanelRepository
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->bindParam(param: ':apikey', var: $apikey);
$statement->bindParam(param: ':self', var: $self);
$statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId();
@ -157,7 +133,7 @@ class PanelRepository
*
* @return false|int
*/
public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int
public function update(int $id, string $name, string $a, string $aaaa, String $apikey): bool|int
{
$current = $this->findByID(id: $id);
@ -174,22 +150,12 @@ class PanelRepository
$apikey = $current->getApikey();
}
if (empty($self)) {
echo "self is empty";
$self = $current->getSelf();
} else {
if ($self == -1) {
$self = 0;
}
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name,
a = :a,
aaaa = :aaaa,
apikey = :apikey,
self = :self
apikey = :apikey
WHERE id = :id";
try {
@ -199,7 +165,6 @@ class PanelRepository
$statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey);
$statement->bindParam(param: 'self', var: $self);
$statement->execute();
return $statement->rowCount();