misc_pterodactyl-panel/app/Services/Databases/DatabaseManagementService.php

135 lines
3.9 KiB
PHP
Raw Normal View History

2017-07-15 16:52:34 +00:00
<?php
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;
use Pterodactyl\Helpers\Utilities;
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;
class DatabaseManagementService
2017-07-15 16:52:34 +00:00
{
/**
* @var \Illuminate\Database\DatabaseManager
2017-07-15 16:52:34 +00:00
*/
private $database;
2017-07-15 16:52:34 +00:00
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
private $dynamic;
2017-07-15 16:52:34 +00:00
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
private $encrypter;
2017-07-15 16:52:34 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
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(
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
* @return \Pterodactyl\Models\Database
2017-07-15 16:52:34 +00:00
*
* @throws \Exception
*/
public function create($server, array $data)
2017-07-15 16:52:34 +00:00
{
$data['server_id'] = $server;
$data['database'] = sprintf('s%d_%s', $server, $data['database']);
$data['username'] = sprintf('u%d_%s', $server, str_random(10));
$data['password'] = $this->encrypter->encrypt(
Utilities::randomStringWithSpecialCharacters(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']);
$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,
$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,
$database->remote
2017-07-15 16:52:34 +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) {
$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
*
* @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);
$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);
}
}