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); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
@ -6,6 +6,7 @@ namespace App\Controller;
// & ~E_DEPRECATED is needed because of a bug in PhpStorm // & ~E_DEPRECATED is needed because of a bug in PhpStorm
use DI\DependencyException; use DI\DependencyException;
use DI\NotFoundException; use DI\NotFoundException;
use App\Controller\CLIController;
use Exception; use Exception;
error_reporting(error_level: E_ALL & ~E_DEPRECATED); error_reporting(error_level: E_ALL & ~E_DEPRECATED);
@ -14,16 +15,33 @@ if (php_sapi_name() !== 'cli') {
exit; exit;
} }
// version, store that somewhere else // version, store that somewhere else
$version = '0.0.1'; $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'; 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::'; // version
$shortOpts .= "V::"; // verbose $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)) { if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$verbose = true; $config['verbose'] = true;
} else { } else {
$verbose = false; $config['verbose'] = false;
} }
$arguments = array_slice(array: $argv, offset: $restIndex); $arguments = array_slice(array: $argv, offset: $restIndex);
try { try {
$app = new BindAPI(verbose: $verbose ); $app = new CLIController(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
$app->runCommand(argumentsCount: count(value: $arguments), arguments: $arguments);
} catch (DependencyException|NotFoundException|Exception $e) { } catch (DependencyException|NotFoundException|Exception $e) {
echo $e->getMessage() . PHP_EOL; echo $e->getMessage() . PHP_EOL;
exit(1); exit(1);
} }
$app->runCommand();
/** /**
* @param String $message * @param String $message

File diff suppressed because it is too large Load Diff

View File

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

View File

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