Files
pterodactyl-panel/app/Services/Databases/DatabaseHostGateway.php
T

129 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Services\Databases;
use Illuminate\Support\Facades\DB;
/**
* Runs the administrative statements (databases, users, grants) against a
* database host over the connection DynamicDatabaseConnection configures.
*/
class DatabaseHostGateway
{
public const string DEFAULT_CONNECTION_NAME = 'dynamic';
/**
* Return the connection to execute statements against.
*/
public function getConnection(): string
{
return self::DEFAULT_CONNECTION_NAME;
}
/**
* Create a new database on the host.
*/
public function createDatabase(string $database): bool
{
return $this->run(sprintf('CREATE DATABASE IF NOT EXISTS %s', $this->identifier($database)));
}
/**
* Create a new database user on the host.
*/
public function createUser(string $username, string $remote, string $password, ?int $maxConnections): bool
{
$statement = sprintf(
'CREATE USER %s IDENTIFIED BY %s',
$this->account($username, $remote),
DB::connection($this->getConnection())->escape($password),
);
if (! empty($maxConnections)) {
$statement .= sprintf(' WITH MAX_USER_CONNECTIONS %d', $maxConnections);
}
return $this->run($statement);
}
/**
* Give a user access to a database.
*/
public function assignUserToDatabase(string $database, string $username, string $remote): bool
{
return $this->run(sprintf(
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, REFERENCES, INDEX, LOCK TABLES, CREATE ROUTINE, ALTER ROUTINE, EXECUTE, CREATE TEMPORARY TABLES, CREATE VIEW, SHOW VIEW, EVENT, TRIGGER ON %s.* TO %s',
$this->identifier($this->grantableDatabaseName($database)),
$this->account($username, $remote),
));
}
/**
* Flush the host's privilege tables.
*/
public function flush(): bool
{
return $this->run('FLUSH PRIVILEGES');
}
/**
* Drop a database on the host.
*/
public function dropDatabase(string $database): bool
{
return DB::connection($this->getConnection())->getSchemaBuilder()->dropDatabaseIfExists($database);
}
/**
* Drop a user on the host.
*/
public function dropUser(string $username, string $remote): bool
{
return $this->run(sprintf('DROP USER IF EXISTS %s', $this->account($username, $remote)));
}
/**
* Return the database name to use in a GRANT statement. "_" and "%" are pattern
* characters in a database-level grant unless MySQL has partial_revokes enabled.
*/
private function grantableDatabaseName(string $database): string
{
return $this->grantsUseWildcards() ? addcslashes($database, '\\_%') : $database;
}
/**
* Determine if the host reads wildcards in database-level grants. MariaDB, and MySQL
* before 8.0.16, have no partial_revokes and always do.
*/
private function grantsUseWildcards(): bool
{
$variables = DB::connection($this->getConnection())->select("SHOW GLOBAL VARIABLES LIKE 'partial\\_revokes'");
return data_get($variables, '0.Value') !== 'ON';
}
/**
* Quote an account name, such as `user`@`host`.
*/
private function account(string $username, string $remote): string
{
return $this->identifier($username).'@'.$this->identifier($remote);
}
/**
* Quote a MySQL identifier. The query grammar's wrap() can't be used here because it
* splits on dots, and remotes such as 10.0.0.% contain them.
*/
private function identifier(string $value): string
{
return '`'.str_replace('`', '``', $value).'`';
}
private function run(string $statement): bool
{
return DB::connection($this->getConnection())->statement($statement);
}
}