2016-02-08 23:03:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
2016-02-08 23:03:02 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-08 23:03:02 +00:00
|
|
|
namespace Pterodactyl\Repositories;
|
|
|
|
|
|
|
|
use DB;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Crypt;
|
2017-03-05 21:37:38 +00:00
|
|
|
use Config;
|
2016-02-08 23:03:02 +00:00
|
|
|
use Validator;
|
2017-03-16 23:35:29 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2016-02-08 23:03:02 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
2016-02-08 23:03:02 +00:00
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
class DatabaseRepository
|
|
|
|
{
|
2016-02-08 23:03:02 +00:00
|
|
|
/**
|
2017-03-05 21:37:38 +00:00
|
|
|
* Adds a new database to a specified database host server.
|
|
|
|
*
|
2017-03-16 23:35:29 +00:00
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
* @return \Pterodactyl\Models\Database
|
2017-03-05 21:37:38 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
2016-02-08 23:03:02 +00:00
|
|
|
*/
|
2017-03-16 23:35:29 +00:00
|
|
|
public function create($id, array $data)
|
2016-02-08 23:03:02 +00:00
|
|
|
{
|
2017-03-16 23:35:29 +00:00
|
|
|
$server = Server::findOrFail($server);
|
2017-03-05 21:37:38 +00:00
|
|
|
|
|
|
|
$validator = Validator::make($data, [
|
|
|
|
'host' => 'required|exists:database_servers,id',
|
2016-02-08 23:03:02 +00:00
|
|
|
'database' => 'required|regex:/^\w{1,100}$/',
|
2017-03-05 21:37:38 +00:00
|
|
|
'connection' => 'required|regex:/^[0-9%.]{1,15}$/',
|
2016-02-08 23:03:02 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2017-03-16 23:35:29 +00:00
|
|
|
throw new DisplayValidationException(json_encode($validator->errors()));
|
2016-02-08 23:03:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
$host = DatabaseHost::findOrFail($data['host']);
|
2016-02-08 23:03:02 +00:00
|
|
|
DB::beginTransaction();
|
2017-03-05 21:37:38 +00:00
|
|
|
|
2016-02-08 23:03:02 +00:00
|
|
|
try {
|
2017-03-05 21:37:38 +00:00
|
|
|
$database = Models\Database::firstOrNew([
|
2016-08-16 04:07:10 +00:00
|
|
|
'server_id' => $server->id,
|
2017-03-16 23:35:29 +00:00
|
|
|
'database_host_id' => $data['host'],
|
2017-03-05 21:37:38 +00:00
|
|
|
'database' => sprintf('s%d_%s', $server->id, $data['database']),
|
2016-02-15 02:43:20 +00:00
|
|
|
]);
|
|
|
|
|
2017-03-05 21:37:38 +00:00
|
|
|
if ($database->exists) {
|
|
|
|
throw new DisplayException('A database with those details already exists in the system.');
|
|
|
|
}
|
2016-02-08 23:03:02 +00:00
|
|
|
|
2017-03-05 21:37:38 +00:00
|
|
|
$database->username = sprintf('s%d_%s', $server->id, str_random(10));
|
|
|
|
$database->remote = $data['connection'];
|
|
|
|
$database->password = Crypt::encrypt(str_random(20));
|
2016-02-08 23:03:02 +00:00
|
|
|
|
2017-03-05 21:37:38 +00:00
|
|
|
$database->save();
|
2016-11-26 22:34:14 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
DB::rollBack();
|
2017-03-05 21:37:38 +00:00
|
|
|
throw $ex;
|
2016-11-26 22:34:14 +00:00
|
|
|
}
|
2016-02-08 23:03:02 +00:00
|
|
|
|
2017-03-05 21:37:38 +00:00
|
|
|
Config::set('database.connections.dynamic', [
|
|
|
|
'driver' => 'mysql',
|
|
|
|
'host' => $host->host,
|
|
|
|
'port' => $host->port,
|
|
|
|
'database' => 'mysql',
|
|
|
|
'username' => $host->username,
|
|
|
|
'password' => Crypt::decrypt($host->password),
|
|
|
|
'charset' => 'utf8',
|
|
|
|
'collation' => 'utf8_unicode_ci',
|
|
|
|
]);
|
|
|
|
|
2016-11-26 22:34:14 +00:00
|
|
|
try {
|
2017-03-05 21:37:38 +00:00
|
|
|
DB::connection('dynamic')->statement(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database->database));
|
|
|
|
DB::connection('dynamic')->statement(sprintf(
|
|
|
|
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
|
|
|
|
$database->username, $database->remote, Crypt::decrypt($database->password)
|
|
|
|
));
|
|
|
|
DB::connection('dynamic')->statement(sprintf(
|
|
|
|
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`',
|
|
|
|
$database->database, $database->username, $database->remote
|
|
|
|
));
|
|
|
|
|
|
|
|
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
|
|
|
|
|
|
|
|
// Save Everything
|
2016-02-08 23:03:02 +00:00
|
|
|
DB::commit();
|
2017-03-16 23:35:29 +00:00
|
|
|
|
|
|
|
return $database;
|
2016-02-08 23:03:02 +00:00
|
|
|
} catch (\Exception $ex) {
|
2016-11-26 22:34:14 +00:00
|
|
|
try {
|
2017-03-05 21:37:38 +00:00
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database));
|
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
|
|
|
|
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
|
2017-03-06 01:28:29 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
}
|
2017-03-05 21:37:38 +00:00
|
|
|
|
|
|
|
DB::rollBack();
|
|
|
|
throw $ex;
|
2016-02-08 23:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 04:07:10 +00:00
|
|
|
/**
|
|
|
|
* Updates the password for a given database.
|
2017-03-16 23:35:29 +00:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param string $password
|
|
|
|
* @return void
|
2016-08-16 04:07:10 +00:00
|
|
|
*/
|
2017-03-05 21:37:38 +00:00
|
|
|
public function password($id, $password)
|
2016-08-16 04:07:10 +00:00
|
|
|
{
|
2017-02-09 22:43:54 +00:00
|
|
|
$database = Models\Database::with('host')->findOrFail($id);
|
2016-08-16 04:07:10 +00:00
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
DB::transaction(function () use ($database, $password) {
|
2017-02-09 22:43:54 +00:00
|
|
|
$database->password = Crypt::encrypt($password);
|
2016-08-16 04:07:10 +00:00
|
|
|
|
2017-03-05 21:37:38 +00:00
|
|
|
Config::set('database.connections.dynamic', [
|
2016-08-16 04:07:10 +00:00
|
|
|
'driver' => 'mysql',
|
2017-02-09 22:43:54 +00:00
|
|
|
'host' => $database->host->host,
|
|
|
|
'port' => $database->host->port,
|
2016-08-16 04:07:10 +00:00
|
|
|
'database' => 'mysql',
|
2017-02-09 22:43:54 +00:00
|
|
|
'username' => $database->host->username,
|
|
|
|
'password' => Crypt::decrypt($database->host->password),
|
2017-03-05 21:37:38 +00:00
|
|
|
'charset' => 'utf8',
|
2016-08-16 04:07:10 +00:00
|
|
|
'collation' => 'utf8_unicode_ci',
|
|
|
|
]);
|
|
|
|
|
2017-03-05 21:37:38 +00:00
|
|
|
DB::connection('dynamic')->statement(sprintf(
|
2016-11-26 22:34:14 +00:00
|
|
|
'SET PASSWORD FOR `%s`@`%s` = PASSWORD(\'%s\')',
|
2017-03-05 21:37:38 +00:00
|
|
|
$database->username, $database->remote, $password
|
2016-08-16 04:07:10 +00:00
|
|
|
));
|
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
$database->save();
|
|
|
|
});
|
2016-08-16 04:07:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 23:03:02 +00:00
|
|
|
/**
|
2017-03-16 23:35:29 +00:00
|
|
|
* Drops a database from the associated database host.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return void
|
2016-02-08 23:03:02 +00:00
|
|
|
*/
|
2017-02-09 22:43:54 +00:00
|
|
|
public function drop($id)
|
2016-02-08 23:03:02 +00:00
|
|
|
{
|
2017-03-16 23:35:29 +00:00
|
|
|
$database = Database::with('host')->findOrFail($id);
|
2016-02-08 23:03:02 +00:00
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
DB::transaction(function () use ($database) {
|
2017-03-05 21:37:38 +00:00
|
|
|
Config::set('database.connections.dynamic', [
|
2016-02-08 23:03:02 +00:00
|
|
|
'driver' => 'mysql',
|
2017-02-09 22:43:54 +00:00
|
|
|
'host' => $database->host->host,
|
|
|
|
'port' => $database->host->port,
|
2016-02-08 23:03:02 +00:00
|
|
|
'database' => 'mysql',
|
2017-02-09 22:43:54 +00:00
|
|
|
'username' => $database->host->username,
|
|
|
|
'password' => Crypt::decrypt($database->host->password),
|
2017-03-05 21:37:38 +00:00
|
|
|
'charset' => 'utf8',
|
2016-02-08 23:03:02 +00:00
|
|
|
'collation' => 'utf8_unicode_ci',
|
|
|
|
]);
|
|
|
|
|
2017-03-05 21:37:38 +00:00
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database));
|
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
|
|
|
|
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
|
2016-02-08 23:03:02 +00:00
|
|
|
|
2017-02-09 22:43:54 +00:00
|
|
|
$database->delete();
|
2017-03-16 23:35:29 +00:00
|
|
|
});
|
2016-02-08 23:03:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 02:43:20 +00:00
|
|
|
/**
|
2017-03-16 23:35:29 +00:00
|
|
|
* Deletes a database host from the system if it has no associated databases.
|
|
|
|
*
|
|
|
|
* @param int $server
|
|
|
|
* @return void
|
2017-02-09 22:43:54 +00:00
|
|
|
*
|
2017-03-16 23:35:29 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2016-02-15 02:43:20 +00:00
|
|
|
*/
|
2017-03-16 23:35:29 +00:00
|
|
|
public function delete($id)
|
2016-02-15 02:43:20 +00:00
|
|
|
{
|
2017-03-16 23:35:29 +00:00
|
|
|
$host = DatabaseHost::withCount('databases')->findOrFail($id);
|
2016-02-15 02:43:20 +00:00
|
|
|
|
2017-02-09 22:43:54 +00:00
|
|
|
if ($host->databases_count > 0) {
|
2017-03-16 23:35:29 +00:00
|
|
|
throw new DisplayException('You cannot delete a database host that has active databases attached to it.');
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
$host->delete();
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-16 23:35:29 +00:00
|
|
|
* Adds a new Database Host to the system.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return \Pterodactyl\Models\DatabaseHost
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
2016-02-15 02:43:20 +00:00
|
|
|
*/
|
|
|
|
public function add(array $data)
|
|
|
|
{
|
2016-11-27 01:27:36 +00:00
|
|
|
if (isset($data['host'])) {
|
|
|
|
$data['host'] = gethostbyname($data['host']);
|
|
|
|
}
|
|
|
|
|
2016-02-15 02:43:20 +00:00
|
|
|
$validator = Validator::make($data, [
|
|
|
|
'name' => 'required|string|max:255',
|
2017-03-16 23:35:29 +00:00
|
|
|
'host' => 'required|ip|unique:database_hosts,host',
|
2016-02-15 02:43:20 +00:00
|
|
|
'port' => 'required|numeric|between:1,65535',
|
|
|
|
'username' => 'required|string|max:32',
|
|
|
|
'password' => 'required|string',
|
2017-03-16 23:35:29 +00:00
|
|
|
'node_id' => 'sometimes|required|exists:nodes,id',
|
2016-02-15 02:43:20 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2017-03-16 23:35:29 +00:00
|
|
|
throw new DisplayValidationException(json_encode($validator->errors()));
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
return DB::transaction(function () use ($data) {
|
2017-03-05 21:37:38 +00:00
|
|
|
Config::set('database.connections.dynamic', [
|
2016-02-15 02:43:20 +00:00
|
|
|
'driver' => 'mysql',
|
|
|
|
'host' => $data['host'],
|
|
|
|
'port' => $data['port'],
|
|
|
|
'database' => 'mysql',
|
|
|
|
'username' => $data['username'],
|
|
|
|
'password' => $data['password'],
|
2017-03-05 21:37:38 +00:00
|
|
|
'charset' => 'utf8',
|
2016-02-15 02:43:20 +00:00
|
|
|
'collation' => 'utf8_unicode_ci',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Allows us to check that we can connect to things.
|
2017-03-05 21:37:38 +00:00
|
|
|
DB::connection('dynamic')->select('SELECT 1 FROM dual');
|
2016-02-15 02:43:20 +00:00
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
$host = new DatabaseHost;
|
|
|
|
$host->password = Crypt::encrypt($data['password']);
|
|
|
|
|
|
|
|
$host->fill([
|
2016-02-15 02:43:20 +00:00
|
|
|
'name' => $data['name'],
|
|
|
|
'host' => $data['host'],
|
|
|
|
'port' => $data['port'],
|
|
|
|
'username' => $data['username'],
|
2016-12-07 22:46:38 +00:00
|
|
|
'max_databases' => null,
|
2017-03-16 23:35:29 +00:00
|
|
|
'node_id' => (isset($data['node_id'])) ? $data['node_id'] : null,
|
|
|
|
])->save();
|
2016-02-15 02:43:20 +00:00
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
return $host;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a Database Host on the system.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
* @return \Pterodactyl\Models\DatabaseHost
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
|
|
|
*/
|
|
|
|
public function update($id, array $data)
|
|
|
|
{
|
|
|
|
$host = DatabaseHost::findOrFail($id);
|
|
|
|
|
|
|
|
if (isset($data['host'])) {
|
|
|
|
$data['host'] = gethostbyname($data['host']);
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
2017-03-16 23:35:29 +00:00
|
|
|
|
|
|
|
$validator = Validator::make($data, [
|
|
|
|
'name' => 'sometimes|required|string|max:255',
|
|
|
|
'host' => 'sometimes|required|ip|unique:database_hosts,host,' . $host->id,
|
|
|
|
'port' => 'sometimes|required|numeric|between:1,65535',
|
|
|
|
'username' => 'sometimes|required|string|max:32',
|
|
|
|
'password' => 'sometimes|required|string',
|
|
|
|
'node_id' => 'sometimes|required|exists:nodes,id',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new DisplayValidationException(json_encode($validator->errors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return DB::transaction(function () use ($data, $host) {
|
|
|
|
if (isset($data['password'])) {
|
|
|
|
$host->password = Crypt::encrypt($data['password']);
|
|
|
|
}
|
|
|
|
$host->fill($data)->save();
|
|
|
|
|
|
|
|
// Check that we can still connect with these details.
|
|
|
|
Config::set('database.connections.dynamic', [
|
|
|
|
'driver' => 'mysql',
|
|
|
|
'host' => $host->host,
|
|
|
|
'port' => $host->port,
|
|
|
|
'database' => 'mysql',
|
|
|
|
'username' => $host->username,
|
|
|
|
'password' => Crypt::decrypt($host->password),
|
|
|
|
'charset' => 'utf8',
|
|
|
|
'collation' => 'utf8_unicode_ci',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Allows us to check that we can connect to things.
|
|
|
|
DB::connection('dynamic')->select('SELECT 1 FROM dual');
|
|
|
|
|
|
|
|
return $host;
|
|
|
|
});
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
2016-02-08 23:03:02 +00:00
|
|
|
}
|