Compare commits

...

5 Commits

Author SHA1 Message Date
tracer 46fdadf8e5 added check for debugging 2022-09-17 15:46:25 +02:00
tracer 051e7cbd0b Merge remote-tracking branch 'origin/master'
# Conflicts:
#	bin/console
#	src/Controller/CLIController.php
#	src/Repository/PanelRepository.php
2022-09-17 15:41:31 +02:00
tracer d8ad733d2c added find self 2022-09-17 15:40:44 +02:00
tracer e12a226b4f modified shebang behaviour 2022-09-16 15:03:19 +02:00
tracer 9716ad40d2 reworking dyndns 2022-09-16 14:59:23 +02:00
4 changed files with 723 additions and 487 deletions

40
bin/console Executable file → Normal file
View File

@ -1,4 +1,4 @@
#!/usr/bin/keyhelp-php81
#!/usr/bin/env php
<?php declare(strict_types=1);
namespace App\Controller;
@ -6,7 +6,6 @@ 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);
@ -15,33 +14,16 @@ 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
@ -66,22 +48,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)) {
$config['verbose'] = true;
$verbose = true;
} else {
$config['verbose'] = false;
$verbose = false;
}
$arguments = array_slice(array: $argv, offset: $restIndex);
try {
$app = new CLIController(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
$app = new BindAPI(verbose: $verbose );
$app->runCommand(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,6 +2,7 @@
namespace App\Repository;
use App\Controller\ConfigController;
use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use Monolog\Logger;
@ -13,11 +14,12 @@ use PDOException;
*/
class DomainRepository
{
public function __construct(private DatabaseConnection $databaseConnection, private array $config, private Logger $log)
public function __construct(
private readonly DatabaseConnection $databaseConnection,
private readonly ConfigController $configController,
private readonly Logger $logger)
{
if ($this->config['debug']) {
$this->log->debug(message: "DomainRepository::__construct()");
}
$this->logger->debug(message: "DomainRepository::__construct()");
}
@ -27,9 +29,7 @@ class DomainRepository
*/
public function findAll(): array
{
if ($this->config['debug']) {
$this->log->debug(message: "findAll()");
}
$this->logger->debug(message: "findAll()");
$domains = [];
$sql = "
@ -58,9 +58,7 @@ class DomainRepository
*/
public function findByID(int $id): bool|Domain
{
if ($this->config['debug']) {
$this->log->debug(message: "findById($id)");
}
$this->logger->debug(message: "findById($id)");
$sql = "
SELECT id, name, panel
@ -90,9 +88,8 @@ class DomainRepository
*/
public function findByName(string $name): Domain|bool
{
if ($this->config['debug']) {
$this->log->debug(message: "findByName($name)");
}
$this->logger->debug(message: "findByName($name)");
$sql = "
SELECT id, name, panel
FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -113,17 +110,14 @@ class DomainRepository
}
/**
* @param String $name
* @param string $host
*
* @return \App\Entity\Domain|bool
*/
public function findByHost(string $host): Domain|bool
{
if ($this->config['debug']) {
$this->log->debug(message: "findByHost($host)");
}
$this->logger->debug(message: "findByHost($host)");
$host = strtolower(string: trim(string: $host));
$count = substr_count(haystack: $host, needle: '.');
@ -143,7 +137,6 @@ class DomainRepository
}
/**
* @param \App\Entity\Domain $domain
*
@ -151,10 +144,8 @@ class DomainRepository
*/
public function insert(Domain $domain): bool|string
{
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "insert($domainName)");
}
$this->logger->info(message: "insert($domainName)");
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel)
@ -182,10 +173,8 @@ class DomainRepository
*/
public function update(Domain $domain): bool|int
{
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "update($domainName)");
}
$this->logger->debug(message: "update($domainName)");
$id = $domain->getId();
$current = $this->findByID(id: $id);
@ -229,10 +218,8 @@ class DomainRepository
*/
public function delete(Domain $domain): int
{
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "delete($domainName)");
}
$this->logger->debug(message: "delete($domainName)");
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -256,7 +243,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;

View File

@ -3,6 +3,7 @@
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use App\Entity\Panel;
use PDO;
use PDOException;
@ -12,9 +13,31 @@ use PDOException;
*/
class PanelRepository
{
public function __construct(private DatabaseConnection $databaseConnection)
public function __construct(private readonly 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
@ -23,7 +46,7 @@ class PanelRepository
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name";
@ -31,7 +54,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']);
$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;
@ -49,7 +72,7 @@ class PanelRepository
public function findByID(int $id): ?Panel
{
$sql = "
SELECT id, name, a, aaaa, apikey
SELECT id, name, a, aaaa, apikey, self
FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id";
@ -58,7 +81,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']);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else {
return null;
}
@ -76,7 +99,7 @@ class PanelRepository
public function findByName(string $name): Panel|bool
{
$sql = "
SELECT id, name, a, aaaa, apikey
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name";
@ -85,7 +108,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']);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else {
return false;
}
@ -103,11 +126,11 @@ class PanelRepository
*
* @return string|false
*/
public function insert(string $name, string $a, string $aaaa, String $apikey): bool|string
public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string
{
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self)
VALUES (:name, :a, :aaaa, :apikey, :self)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
@ -115,6 +138,7 @@ 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();
@ -133,7 +157,7 @@ class PanelRepository
*
* @return false|int
*/
public function update(int $id, string $name, string $a, string $aaaa, String $apikey): bool|int
public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int
{
$current = $this->findByID(id: $id);
@ -150,12 +174,22 @@ 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
apikey = :apikey,
self = :self
WHERE id = :id";
try {
@ -165,6 +199,7 @@ 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();