bindAPI/src/Controller/BindAPI.php

121 lines
3.3 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2022-01-19 21:07:22 +01:00
namespace App\Controller;
error_reporting(error_level: E_ALL);
use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository;
use App\Repository\NameserverRepository;
use App\Repository\PanelRepository;
use Arubacao\TldChecker\Validator;
use DI\Container;
use DI\ContainerBuilder;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use function DI\autowire;
2022-01-19 21:07:22 +01:00
/**
*
*/
class BindAPI
{
public Logger $log;
public ApiController $apiController;
public ApikeyRepository $apikeyRepository;
public DomainController $domainController;
public DomainRepository $domainRepository;
public NameserverRepository $nameserverRepository;
public PanelRepository $panelRepository;
private Container $container;
2022-01-20 19:55:04 +01:00
/**
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \Exception
*/
public function __construct(private readonly array $config)
{
$dateFormat = "Y:m:d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log');
$stream->setFormatter(formatter: $formatter);
$this->log = new Logger(name: 'bindAPI');
$this->log->pushHandler(handler: $stream);
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config),
DomainController::class => autowire()
->constructorParameter(parameter: 'config', value: $this->config)
->constructorParameter(parameter: 'log', value: $this->log),
DomainRepository::class => autowire()
->constructorParameter(parameter: 'config', value: $this->config)
->constructorParameter(parameter: 'log', value: $this->log),
]);
$this->container = $containerBuilder->build();
$this->apiController = $this->container->get(name: ApiController::class);
$this->domainController = $this->container->get(name: DomainController::class);
$this->domainRepository = $this->container->get(name: DomainRepository::class);
$this->nameserverRepository = $this->container->get(name: NameserverRepository::class);
$this->panelRepository = $this->container->get(name: PanelRepository::class);
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
}
// TODO mode to domainController
function isValidSecondLevelDomain(string $domainName, string $panel, int $parent): bool
{
if ($this->config['debug']) {
$this->log->debug(message: "isValidSecondLevelDomain()");
}
// subdomain
if ($parent != 0) {
return false;
}
// system domain
if (str_contains(haystack: $domainName, needle: $panel)) {
return false;
}
// valid second level domain
if (!Validator::endsWithTld(value: $domainName)) {
return false;
}
// no second level domain
if (substr_count(haystack: $domainName, needle: '.') > 1) {
return false;
}
return true;
}
/**
* @return int|void
*/
public function getId()
{
if (!empty($this->arguments[1])) {
$id = intval(value: $this->arguments[1] ?? 0);
if ($id != $this->arguments[1]) {
echo 'ID has to be a number.' . PHP_EOL;
exit(1);
}
} else {
$id = 0;
2022-01-20 19:55:04 +01:00
}
return $id;
2022-01-20 19:55:04 +01:00
}
2022-01-19 21:07:22 +01:00
}