2017-07-15 16:52:34 +00:00
|
|
|
<?php
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
namespace Pterodactyl\Services\Databases;
|
2017-07-15 16:52:34 +00:00
|
|
|
|
2019-09-06 04:32:57 +00:00
|
|
|
use Exception;
|
2017-10-24 01:12:15 +00:00
|
|
|
use Pterodactyl\Models\Database;
|
2017-07-22 18:55:30 +00:00
|
|
|
use Illuminate\Database\DatabaseManager;
|
2017-07-15 16:52:34 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
|
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
|
|
|
|
2017-07-25 02:34:10 +00:00
|
|
|
class DatabaseManagementService
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-07-22 18:55:30 +00:00
|
|
|
* @var \Illuminate\Database\DatabaseManager
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-03-02 03:27:37 +00:00
|
|
|
private $database;
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
|
|
|
|
*/
|
2018-03-02 03:27:37 +00:00
|
|
|
private $dynamic;
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
2018-03-02 03:27:37 +00:00
|
|
|
private $encrypter;
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
2018-03-02 03:27:37 +00:00
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $useRandomHost = false;
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CreationService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\DatabaseManager $database
|
|
|
|
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
|
2017-07-15 16:52:34 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-07-22 18:55:30 +00:00
|
|
|
DatabaseManager $database,
|
2017-07-15 16:52:34 +00:00
|
|
|
DynamicDatabaseConnection $dynamic,
|
|
|
|
DatabaseRepositoryInterface $repository,
|
|
|
|
Encrypter $encrypter
|
|
|
|
) {
|
|
|
|
$this->database = $database;
|
|
|
|
$this->dynamic = $dynamic;
|
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new database that is linked to a specific host.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param int $server
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param array $data
|
2018-03-02 03:27:37 +00:00
|
|
|
* @return \Pterodactyl\Models\Database
|
2017-07-15 16:52:34 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2017-07-22 18:55:30 +00:00
|
|
|
public function create($server, array $data)
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2017-07-22 18:55:30 +00:00
|
|
|
$data['server_id'] = $server;
|
2018-02-18 19:46:42 +00:00
|
|
|
$data['database'] = sprintf('s%d_%s', $server, $data['database']);
|
2017-07-22 18:55:30 +00:00
|
|
|
$data['username'] = sprintf('u%d_%s', $server, str_random(10));
|
2019-03-23 21:04:57 +00:00
|
|
|
$data['password'] = $this->encrypter->encrypt(str_random(24));
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
$this->database->beginTransaction();
|
|
|
|
try {
|
|
|
|
$database = $this->repository->createIfNotExists($data);
|
|
|
|
$this->dynamic->set('dynamic', $data['database_host_id']);
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->repository->createDatabase($database->database);
|
2017-07-15 16:52:34 +00:00
|
|
|
$this->repository->createUser(
|
2017-08-22 03:10:48 +00:00
|
|
|
$database->username,
|
|
|
|
$database->remote,
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->encrypter->decrypt($database->password)
|
2017-07-15 16:52:34 +00:00
|
|
|
);
|
|
|
|
$this->repository->assignUserToDatabase(
|
2017-08-22 03:10:48 +00:00
|
|
|
$database->database,
|
|
|
|
$database->username,
|
2017-10-19 03:32:19 +00:00
|
|
|
$database->remote
|
2017-07-15 16:52:34 +00:00
|
|
|
);
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->repository->flush();
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
$this->database->commit();
|
2019-09-06 04:32:57 +00:00
|
|
|
} catch (Exception $ex) {
|
2017-07-15 16:52:34 +00:00
|
|
|
try {
|
2017-10-24 01:12:15 +00:00
|
|
|
if (isset($database) && $database instanceof Database) {
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->repository->dropDatabase($database->database);
|
|
|
|
$this->repository->dropUser($database->username, $database->remote);
|
|
|
|
$this->repository->flush();
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
2019-09-06 04:32:57 +00:00
|
|
|
} catch (Exception $exTwo) {
|
2017-07-15 16:52:34 +00:00
|
|
|
// ignore an exception
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->database->rollBack();
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $database;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a database from the given host server.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param int $id
|
2017-07-15 16:52:34 +00:00
|
|
|
* @return bool|null
|
2017-10-19 03:32:19 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$database = $this->repository->find($id);
|
|
|
|
$this->dynamic->set('dynamic', $database->database_host_id);
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->repository->dropDatabase($database->database);
|
|
|
|
$this->repository->dropUser($database->username, $database->remote);
|
|
|
|
$this->repository->flush();
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
return $this->repository->delete($id);
|
|
|
|
}
|
|
|
|
}
|