2026-10-05 18:10:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Databases;
|
|
|
|
|
|
2026-10-08 17:22:14 -04:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
{
|
2026-10-08 19:17:30 -04:00
|
|
|
return self::DEFAULT_CONNECTION_NAME;
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new database on the host.
|
|
|
|
|
*/
|
|
|
|
|
public function createDatabase(string $database): bool
|
|
|
|
|
{
|
2026-12-01 04:58:46 +09:00
|
|
|
return $this->run(sprintf('CREATE DATABASE IF NOT EXISTS %s', $this->identifier($database)));
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new database user on the host.
|
|
|
|
|
*/
|
|
|
|
|
public function createUser(string $username, string $remote, string $password, ?int $maxConnections): bool
|
|
|
|
|
{
|
2026-12-01 04:58:46 +09:00
|
|
|
$statement = sprintf(
|
|
|
|
|
'CREATE USER %s IDENTIFIED BY %s',
|
|
|
|
|
$this->account($username, $remote),
|
2026-12-01 05:16:38 +09:00
|
|
|
DB::connection($this->getConnection())->escape($password),
|
2026-12-01 04:58:46 +09:00
|
|
|
);
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
if (! empty($maxConnections)) {
|
2026-12-01 04:58:46 +09:00
|
|
|
$statement .= sprintf(' WITH MAX_USER_CONNECTIONS %d', $maxConnections);
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 04:58:46 +09:00
|
|
|
return $this->run($statement);
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Give a user access to a database.
|
|
|
|
|
*/
|
|
|
|
|
public function assignUserToDatabase(string $database, string $username, string $remote): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->run(sprintf(
|
2026-12-01 04:58:46 +09:00
|
|
|
'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),
|
2026-10-05 18:10:18 -04:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
{
|
2026-12-01 05:16:38 +09:00
|
|
|
return DB::connection($this->getConnection())->getSchemaBuilder()->dropDatabaseIfExists($database);
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Drop a user on the host.
|
|
|
|
|
*/
|
|
|
|
|
public function dropUser(string $username, string $remote): bool
|
|
|
|
|
{
|
2026-12-01 04:58:46 +09:00
|
|
|
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
|
|
|
|
|
{
|
2026-12-01 05:16:38 +09:00
|
|
|
$variables = DB::connection($this->getConnection())->select("SHOW GLOBAL VARIABLES LIKE 'partial\\_revokes'");
|
2026-12-01 04:58:46 +09:00
|
|
|
|
|
|
|
|
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).'`';
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
private function run(string $statement): bool
|
|
|
|
|
{
|
2026-12-01 05:16:38 +09:00
|
|
|
return DB::connection($this->getConnection())->statement($statement);
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
}
|