refacted to use bindAPi in CLIController

This commit is contained in:
tracer 2022-07-20 19:09:40 +02:00
parent 838c571d75
commit 5444035a09
14 changed files with 1568 additions and 1423 deletions

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" autoUpload="On explicit save action" serverName="shadow.24unix.net" autoUploadExternalChanges="true">
<component name="PublishConfigData" autoUpload="On explicit save action" promptOnRemoteOverwrite="CHECK_TIMESTAMP" serverName="KeyHelpLocalDev" preserveFilePermissions="true" autoUploadExternalChanges="true" notifyRemoteChanges="true">
<serverData>
<paths name="shadow.24unix.net">
<paths name="KeyHelpLocalDev">
<serverdata>
<mappings>
<mapping deploy="/bindAPI" local="$PROJECT_DIR$" web="/" />
<mapping deploy="/" local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>

View File

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpCodeSniffer">
<phpcs_settings>
<PhpCSConfiguration tool_path="$PROJECT_DIR$/vendor/bin/phpcs" />
</phpcs_settings>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.1">
<option name="suggestChangeDefaultLanguageLevel" value="false" />
</component>

View File

@ -1,10 +1,12 @@
#!/usr/bin/keyhelp-php81
<?php declare(strict_types=1);
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);
@ -18,9 +20,9 @@ $version = '0.0.1';
require dirname(path: __DIR__) . '/vendor/autoload.php';
$configFile = dirname(path: __DIR__) ."/config.json.local";
$configFile = dirname(path: __DIR__) . "/config.json.local";
if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__) ."/config.json";
$configFile = dirname(path: __DIR__) . "/config.json";
}
if (!file_exists(filename: $configFile)) {
@ -41,8 +43,8 @@ if (!$config = json_decode(json: $configJSON, associative: true)) {
echo $configJSON;
}
$shortOpts = 'V::'; // version
$shortOpts .= "v::"; // verbose
$shortOpts = 'v::'; // version
$shortOpts .= "V::"; // verbose
$shortOpts .= "h::"; // help
$longOpts = [
@ -53,17 +55,17 @@ $longOpts = [
$options = getopt(short_options: $shortOpts, long_options: $longOpts, rest_index: $restIndex);
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'version', array: $options) ) {
if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'version', array: $options)) {
print("bindAPI version: $version" . PHP_EOL);
exit(0);
}
if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options) ) {
if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options)) {
print("Help …" . PHP_EOL);
exit(0);
}
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;
} else {
$config['verbose'] = false;
@ -72,15 +74,14 @@ if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'verbos
$arguments = array_slice(array: $argv, offset: $restIndex);
try {
$app = new BindAPI(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
$app->runCommand();
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
$app = new CLIController(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
} catch (DependencyException|NotFoundException|Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
} catch (Exception $e) {
echo $e->getMessage();
}
$app->runCommand();
/**
* @param String $message
@ -89,7 +90,7 @@ try {
*
* @return bool
*/
function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n'], string $default ='n'): bool
function confirm(string $message = 'Are you sure? ', array $options = ['y', 'n'], string $default = 'n'): bool
{
// first $options means true, any other false
echo $message, ' (';

View File

@ -11,16 +11,17 @@
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"ext-curl": "*",
"ext-json": "*",
"ext-pdo": "*",
"arubacao/tld-checker": "^1.2",
"monolog/monolog": "^2.3",
"php-di/php-di": "^6.3",
"phplucidframe/console-table": "^1.2",
"zircote/swagger-php": "^4.2"
},
"php": ">=8.1",
"ext-curl": "*",
"ext-json": "*",
"ext-pdo": "*",
"arubacao/tld-checker": "^1.2",
"monolog/monolog": "^2.3",
"php-di/php-di": "^6.3",
"phplucidframe/console-table": "^1.2",
"squizlabs/php_codesniffer": "^3.7",
"zircote/swagger-php": "^4.2"
},
"config": {
"optimize-autoloader": true,
"preferred-install": {
@ -39,6 +40,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
"escapestudios/symfony2-coding-standard": "3.x-dev",
"phpunit/phpunit": "^9.5"
}
}

120
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f7096254eb316738d722dfff777682ef",
"content-hash": "ebdf6dd62a66775d7344acac9e59a5ce",
"packages": [
{
"name": "arubacao/tld-checker",
@ -745,6 +745,62 @@
},
"time": "2021-07-14T16:46:02+00:00"
},
{
"name": "squizlabs/php_codesniffer",
"version": "3.7.1",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
"reference": "1359e176e9307e906dc3d890bcc9603ff6d90619"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/1359e176e9307e906dc3d890bcc9603ff6d90619",
"reference": "1359e176e9307e906dc3d890bcc9603ff6d90619",
"shasum": ""
},
"require": {
"ext-simplexml": "*",
"ext-tokenizer": "*",
"ext-xmlwriter": "*",
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"bin": [
"bin/phpcs",
"bin/phpcbf"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Greg Sherwood",
"role": "lead"
}
],
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "https://github.com/squizlabs/PHP_CodeSniffer",
"keywords": [
"phpcs",
"standards"
],
"support": {
"issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues",
"source": "https://github.com/squizlabs/PHP_CodeSniffer",
"wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki"
},
"time": "2022-06-18T07:21:10+00:00"
},
{
"name": "symfony/finder",
"version": "v6.0.3",
@ -1109,6 +1165,64 @@
],
"time": "2022-03-03T08:28:38+00:00"
},
{
"name": "escapestudios/symfony2-coding-standard",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/djoos/Symfony-coding-standard.git",
"reference": "5cc7ad11da242182d9776b98b950d5565c32acd0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/djoos/Symfony-coding-standard/zipball/5cc7ad11da242182d9776b98b950d5565c32acd0",
"reference": "5cc7ad11da242182d9776b98b950d5565c32acd0",
"shasum": ""
},
"require": {
"squizlabs/php_codesniffer": "^3.3.1"
},
"conflict": {
"squizlabs/php_codesniffer": "<3 || >=4"
},
"require-dev": {
"phpunit/phpunit": "^5.0 || ^6.0 || ^7.0"
},
"default-branch": true,
"type": "phpcodesniffer-standard",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "David Joos",
"email": "iam@davidjoos.com"
},
{
"name": "Community contributors",
"homepage": "https://github.com/djoos/Symfony-coding-standard/graphs/contributors"
}
],
"description": "CodeSniffer ruleset for the Symfony 2+ coding standard",
"homepage": "https://github.com/djoos/Symfony-coding-standard",
"keywords": [
"Coding Standard",
"Symfony2",
"phpcs",
"symfony"
],
"support": {
"issues": "https://github.com/djoos/Symfony-coding-standard/issues",
"source": "https://github.com/djoos/Symfony-coding-standard"
},
"time": "2021-03-24T15:17:16+00:00"
},
{
"name": "myclabs/deep-copy",
"version": "1.11.0",
@ -3058,7 +3172,9 @@
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"stability-flags": {
"escapestudios/symfony2-coding-standard": 20
},
"prefer-stable": true,
"prefer-lowest": false,
"platform": {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -24,7 +24,7 @@ class DatabaseConnection
public function __construct(private array $config)
{
extract(array: $this->config);
// TODO create config => encryption key
try {
$this->dbConnection = new PDO(
dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",

View File

@ -17,9 +17,9 @@ error_reporting(error_level: E_ALL);
*/
class DomainController
{
private string $localZoneFile;
private string $localZonesDir;
private string $namedConfLocalFile;
public string $localZoneFile;
public string $localZonesDir;
public string $namedConfLocalFile;
private string $zoneCachePath;
@ -132,55 +132,90 @@ class DomainController
/**
* @return void
* @return bool
*/
function checkPermissions(): void
function checkPermissions(): bool
{
$setupIsValid = true;
if ($this->config['debug']) {
$this->log->debug(message: "checkPermissions()");
}
echo 'Checking permission:' . PHP_EOL . PHP_EOL;
if ($this->config['verbose']) {
echo 'Checking permissions...' . PHP_EOL;
}
$uid = posix_geteuid();
echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL;
if ($this->config['verbose']) {
echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL;
}
$pwuid = posix_getpwuid(user_id: $uid);
$name = $pwuid['name'];
echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL;
if ($this->config['verbose']) {
echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL;
}
$bindGroup = posix_getgrnam(name: 'bind');
$members = $bindGroup['members'];
if (in_array(needle: $name, haystack: $members)) {
echo "\t$name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL;
} else {
echo "\t$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL;
}
echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL;
$localZoneFilePermissions = @fileperms(filename: $this->localZoneFile);
if ($localZoneFilePermissions & 0x0010) {
echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL;
} else {
echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL;
}
echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
echo "\t$this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
} else {
echo "\t$this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
if ($this->config['verbose']) {
echo "\t$name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL;
}
} else {
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
$setupIsValid = false;
if ($this->config['verbose']) {
echo COLOR_RED . "\t$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL;
}
}
if ($this->config['verbose']) {
echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL;
}
$localZoneFilePermissions = @fileperms(filename: $this->localZoneFile);
if ($localZoneFilePermissions & 0x0010) {
if ($this->config['verbose']) {
echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL;
}
} else {
$setupIsValid = false;
if ($this->config['verbose']) {
echo COLOR_RED . "\t❌Group needs write permission!" . COLOR_DEFAULT . PHP_EOL;
}
}
if ($this->config['verbose']) {
echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
}
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
$setupIsValid = false;
if ($this->config['verbose']) {
echo "\t$this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
}
} else {
if ($this->config['verbose']) {
echo "\t$this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
}
}
} else {
$setupIsValid = false;
if ($this->config['verbose']) {
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
}
}
if ($this->config['verbose']) {
echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL;
}
echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL;
$localZoneDirPermissions = @fileperms(filename: $this->localZonesDir);
if ($localZoneDirPermissions & 0x0010) {
echo "\t✅ Group has write access." . PHP_EOL;
if ($this->config['verbose']) {
echo "\t✅ Group has write access." . PHP_EOL;
}
} else {
echo "\t❌Group needs write permission!" . PHP_EOL;
$setupIsValid = false;
if ($this->config['verbose']) {
echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL;
}
}
return $setupIsValid;
}
@ -189,6 +224,10 @@ class DomainController
*/
function checkDomains(): void
{
if (!file_exists($this->localZoneFile)) {
echo COLOR_DEFAULT . 'Local Zone file ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT . ' does not exist.' . PHP_EOL;
exit(1);
}
$localZones = file_get_contents(filename: $this->localZoneFile);
$maxNameLength = $this->domainRepository->getLongestEntry(field: 'name');
$domains = $this->domainRepository->findAll();

View File

@ -1,18 +0,0 @@
<?php declare(strict_types=1);
namespace App\Controller;
error_reporting(error_level: E_ALL);
/**
*
*/
class NameserverController
{
public function __construct(private DatabaseConnection $databaseConnection)
{}
}

View File

@ -1,19 +0,0 @@
<?php declare(strict_types=1);
namespace App\Controller;
error_reporting(error_level: E_ALL);
/**
*
*/
class PanelController
{
public function __construct(private DatabaseConnection $databaseConnection)
{}
}

View File

@ -4,7 +4,6 @@ namespace App\Repository;
error_reporting(error_level: E_ALL);
use App\Controller\DatabaseConnection;
use App\Controller\PanelController;
use App\Entity\Apikey;
use Exception;
use PDO;
@ -15,7 +14,7 @@ use PDOException;
*/
class ApikeyRepository
{
public function __construct(private DatabaseConnection $databaseConnection, PanelController $panelController)
public function __construct(private readonly DatabaseConnection $databaseConnection)
{}

View File

@ -67,9 +67,9 @@ class NameserverRepository
/**
* @param int $id
*
* @return \App\Entity\Nameserver
* @return null|\App\Entity\Nameserver
*/
public function findByID(int $id): Nameserver
public function findByID(int $id): ?Nameserver
{
$sql = "
SELECT id, name, a, aaaa, apikey
@ -80,8 +80,11 @@ class NameserverRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Nameserver(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
} else {
return null;
}
} catch (PDOException $e) {
exit($e->getMessage());
}

View File

@ -44,9 +44,9 @@ class PanelRepository
/**
* @param int $id
*
* @return \App\Entity\Panel
* @return null|\App\Entity\Panel
*/
public function findByID(int $id): Panel
public function findByID(int $id): ?Panel
{
$sql = "
SELECT id, name, a, aaaa, apikey
@ -57,8 +57,11 @@ class PanelRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
} else {
return null;
}
} catch (PDOException $e) {
exit($e->getMessage());
}