2022-01-26 19:01:52 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-31 21:00:24 +01:00
|
|
|
|
2022-01-19 21:07:22 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-26 19:01:52 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
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;
|
2022-02-06 17:59:43 +01:00
|
|
|
use Monolog\Formatter\LineFormatter;
|
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Logger;
|
2022-01-31 21:00:24 +01:00
|
|
|
use function DI\autowire;
|
2022-01-19 21:07:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class BindAPI
|
|
|
|
{
|
2022-07-20 19:09:40 +02:00
|
|
|
public Logger $log;
|
|
|
|
public ApiController $apiController;
|
|
|
|
public ApikeyRepository $apikeyRepository;
|
|
|
|
public DomainController $domainController;
|
|
|
|
public DomainRepository $domainRepository;
|
|
|
|
public NameserverRepository $nameserverRepository;
|
|
|
|
public PanelRepository $panelRepository;
|
2022-01-31 21:00:24 +01:00
|
|
|
private Container $container;
|
2022-01-20 19:55:04 +01:00
|
|
|
|
2022-01-25 20:31:31 +01:00
|
|
|
|
|
|
|
/**
|
2022-01-31 21:00:24 +01:00
|
|
|
* @throws \DI\DependencyException
|
|
|
|
* @throws \DI\NotFoundException
|
|
|
|
* @throws \Exception
|
2022-01-25 20:31:31 +01:00
|
|
|
*/
|
2022-07-20 19:09:40 +02:00
|
|
|
public function __construct(private readonly array $config)
|
2022-01-25 20:31:31 +01:00
|
|
|
{
|
2022-02-06 17:59:43 +01:00
|
|
|
$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);
|
2022-01-31 21:00:24 +01:00
|
|
|
$containerBuilder = new ContainerBuilder();
|
|
|
|
$containerBuilder->addDefinitions([
|
|
|
|
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config),
|
2022-02-06 17:59:43 +01:00
|
|
|
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),
|
2022-01-31 21:00:24 +01:00
|
|
|
]);
|
|
|
|
$this->container = $containerBuilder->build();
|
2022-01-25 20:31:31 +01:00
|
|
|
|
2022-02-06 17:59:43 +01:00
|
|
|
$this->apiController = $this->container->get(name: ApiController::class);
|
2022-01-31 21:00:24 +01:00
|
|
|
$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);
|
2022-02-06 17:59:43 +01:00
|
|
|
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
|
2022-01-31 21:00:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 17:59:43 +01:00
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
// TODO mode to domainController
|
2022-01-31 21:00:24 +01:00
|
|
|
function isValidSecondLevelDomain(string $domainName, string $panel, int $parent): bool
|
2022-01-25 20:31:31 +01:00
|
|
|
{
|
2022-02-06 17:59:43 +01:00
|
|
|
if ($this->config['debug']) {
|
|
|
|
$this->log->debug(message: "isValidSecondLevelDomain()");
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
// subdomain
|
|
|
|
if ($parent != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-25 20:31:31 +01:00
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
// system domain
|
|
|
|
if (str_contains(haystack: $domainName, needle: $panel)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-26 19:01:52 +01:00
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2022-04-06 16:26:08 +02:00
|
|
|
|
2022-01-31 21:00:24 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|
2022-01-31 21:00:24 +01:00
|
|
|
return $id;
|
2022-01-20 19:55:04 +01:00
|
|
|
}
|
2022-01-19 21:07:22 +01:00
|
|
|
}
|