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); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
@ -6,7 +6,6 @@ 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);
@ -15,33 +14,16 @@ 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
@ -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)) { if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$config['verbose'] = true; $verbose = true;
} else { } else {
$config['verbose'] = false; $verbose = false;
} }
$arguments = array_slice(array: $argv, offset: $restIndex); $arguments = array_slice(array: $argv, offset: $restIndex);
try { 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) { } 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,6 +2,7 @@
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;
@ -13,11 +14,12 @@ use PDOException;
*/ */
class DomainRepository 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->logger->debug(message: "DomainRepository::__construct()");
$this->log->debug(message: "DomainRepository::__construct()");
}
} }
@ -27,9 +29,7 @@ class DomainRepository
*/ */
public function findAll(): array public function findAll(): array
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findAll()");
$this->log->debug(message: "findAll()");
}
$domains = []; $domains = [];
$sql = " $sql = "
@ -58,9 +58,7 @@ class DomainRepository
*/ */
public function findByID(int $id): bool|Domain public function findByID(int $id): bool|Domain
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findById($id)");
$this->log->debug(message: "findById($id)");
}
$sql = " $sql = "
SELECT id, name, panel SELECT id, name, panel
@ -90,9 +88,8 @@ class DomainRepository
*/ */
public function findByName(string $name): Domain|bool public function findByName(string $name): Domain|bool
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findByName($name)");
$this->log->debug(message: "findByName($name)");
}
$sql = " $sql = "
SELECT id, name, panel SELECT id, name, panel
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -113,17 +110,14 @@ class DomainRepository
} }
/** /**
* @param String $name * @param string $host
* *
* @return \App\Entity\Domain|bool * @return \App\Entity\Domain|bool
*/ */
public function findByHost(string $host): Domain|bool public function findByHost(string $host): Domain|bool
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findByHost($host)");
$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: '.');
@ -134,7 +128,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 {
@ -143,7 +137,6 @@ class DomainRepository
} }
/** /**
* @param \App\Entity\Domain $domain * @param \App\Entity\Domain $domain
* *
@ -151,10 +144,8 @@ class DomainRepository
*/ */
public function insert(Domain $domain): bool|string public function insert(Domain $domain): bool|string
{ {
if ($this->config['debug']) { $domainName = $domain->getName();
$domainName = $domain->getName(); $this->logger->info(message: "insert($domainName)");
$this->log->debug(message: "insert($domainName)");
}
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel) INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel)
@ -182,10 +173,8 @@ class DomainRepository
*/ */
public function update(Domain $domain): bool|int public function update(Domain $domain): bool|int
{ {
if ($this->config['debug']) { $domainName = $domain->getName();
$domainName = $domain->getName(); $this->logger->debug(message: "update($domainName)");
$this->log->debug(message: "update($domainName)");
}
$id = $domain->getId(); $id = $domain->getId();
$current = $this->findByID(id: $id); $current = $this->findByID(id: $id);
@ -229,10 +218,8 @@ class DomainRepository
*/ */
public function delete(Domain $domain): int public function delete(Domain $domain): int
{ {
if ($this->config['debug']) { $domainName = $domain->getName();
$domainName = $domain->getName(); $this->logger->debug(message: "delete($domainName)");
$this->log->debug(message: "delete($domainName)");
}
$sql = " $sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -256,7 +243,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;
@ -264,10 +251,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,6 +3,7 @@
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;
@ -12,9 +13,31 @@ use PDOException;
*/ */
class PanelRepository 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 * @return array
@ -23,7 +46,7 @@ class PanelRepository
{ {
$panels = []; $panels = [];
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name"; ORDER BY name";
@ -31,7 +54,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']); $panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
$panels[] = $panel; $panels[] = $panel;
} }
return $panels; return $panels;
@ -49,7 +72,7 @@ class PanelRepository
public function findByID(int $id): ?Panel public function findByID(int $id): ?Panel
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey SELECT id, name, a, aaaa, apikey, self
FROM . " . DatabaseConnection::TABLE_PANELS . " FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id"; WHERE id = :id";
@ -58,7 +81,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']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else { } else {
return null; return null;
} }
@ -76,7 +99,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 SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name"; WHERE name = :name";
@ -85,7 +108,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']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else { } else {
return false; return false;
} }
@ -103,11 +126,11 @@ class PanelRepository
* *
* @return string|false * @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 = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey) INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self)
VALUES (:name, :a, :aaaa, :apikey)"; VALUES (:name, :a, :aaaa, :apikey, :self)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
@ -115,6 +138,7 @@ 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();
@ -133,7 +157,7 @@ class PanelRepository
* *
* @return false|int * @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); $current = $this->findByID(id: $id);
@ -150,12 +174,22 @@ 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 {
@ -165,6 +199,7 @@ 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();