65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
final class DirectAdminContext
|
|
{
|
|
private function __construct(
|
|
private string $username,
|
|
private string $sourceHost,
|
|
private string $serverName,
|
|
private string $method
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $server
|
|
*/
|
|
public static function fromServer(array $server, Settings $settings): self
|
|
{
|
|
$username = trim((string)($server['USERNAME'] ?? ''));
|
|
if (!preg_match('/^[A-Za-z0-9][A-Za-z0-9._-]{0,31}$/', $username)) {
|
|
throw new InvalidArgumentException('Invalid DirectAdmin username');
|
|
}
|
|
$host = self::trustedSourceHost($server);
|
|
if ($host === '') {
|
|
throw new InvalidArgumentException('Missing source host');
|
|
}
|
|
$serverName = $settings->sourceServerName();
|
|
if ($serverName === '') {
|
|
$serverName = $settings->sanitizeServerName(explode('.', $host)[0] ?? '');
|
|
}
|
|
if ($serverName === '') {
|
|
throw new InvalidArgumentException('Invalid source server name');
|
|
}
|
|
return new self($username, $host, $serverName, strtoupper((string)($server['REQUEST_METHOD'] ?? 'GET')));
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $server
|
|
*/
|
|
private static function trustedSourceHost(array $server): string
|
|
{
|
|
return Settings::normalizeHost((string)($server['SERVER_NAME'] ?? ''));
|
|
}
|
|
|
|
public function username(): string
|
|
{
|
|
return $this->username;
|
|
}
|
|
|
|
public function sourceHost(): string
|
|
{
|
|
return $this->sourceHost;
|
|
}
|
|
|
|
public function serverName(): string
|
|
{
|
|
return $this->serverName;
|
|
}
|
|
|
|
public function method(): string
|
|
{
|
|
return $this->method;
|
|
}
|
|
}
|