|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +namespace Pterodactyl\Repositories; |
| 25 | + |
| 26 | +use Crypt; |
| 27 | +use DB; |
| 28 | +use Validator; |
| 29 | + |
| 30 | +use Pterodactyl\Models; |
| 31 | +use Pterodactyl\Exceptions\DisplayException; |
| 32 | +use Pterodactyl\Exceptions\DisplayValidationException; |
| 33 | + |
| 34 | +use Illuminate\Database\Capsule\Manager as Capsule; |
| 35 | + |
| 36 | +class DatabaseRepository { |
| 37 | + |
| 38 | + /** |
| 39 | + * Adds a new database to a given database server. |
| 40 | + * @param int $server Id of the server to add a database for. |
| 41 | + * @param array $options Array of options for creating that database. |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function create($server, $options) |
| 45 | + { |
| 46 | + $server = Models\Server::findOrFail($server); |
| 47 | + $validator = Validator::make($options, [ |
| 48 | + 'db_server' => 'required|exists:database_servers,id', |
| 49 | + 'database' => 'required|regex:/^\w{1,100}$/', |
| 50 | + 'remote' => 'required|regex:/^[0-9%.]{1,15}$/', |
| 51 | + ]); |
| 52 | + |
| 53 | + if ($validator->fails()) { |
| 54 | + throw new DisplayValidationException($validator->errors()); |
| 55 | + } |
| 56 | + |
| 57 | + DB::beginTransaction(); |
| 58 | + |
| 59 | + $db = new Models\Database; |
| 60 | + $db->fill([ |
| 61 | + 'server' => $server->id, |
| 62 | + 'db_server' => $options['db_server'], |
| 63 | + 'database' => $server->uuidShort . '_' . $options['database'], |
| 64 | + 'username' => $server->uuidShort . '_' . str_random(7), |
| 65 | + 'remote' => $options['remote'], |
| 66 | + 'password' => Crypt::encrypt(str_random(20)) |
| 67 | + ]); |
| 68 | + $db->save(); |
| 69 | + |
| 70 | + // Contact Remote |
| 71 | + $dbr = Models\DatabaseServer::findOrFail($options['db_server']); |
| 72 | + |
| 73 | + try { |
| 74 | + |
| 75 | + $capsule = new Capsule; |
| 76 | + $capsule->addConnection([ |
| 77 | + 'driver' => 'mysql', |
| 78 | + 'host' => $dbr->host, |
| 79 | + 'port' => $dbr->port, |
| 80 | + 'database' => 'mysql', |
| 81 | + 'username' => $dbr->username, |
| 82 | + 'password' => Crypt::decrypt($dbr->password), |
| 83 | + 'charset' => 'utf8', |
| 84 | + 'collation' => 'utf8_unicode_ci', |
| 85 | + 'prefix' => '' |
| 86 | + ]); |
| 87 | + |
| 88 | + $capsule->setAsGlobal(); |
| 89 | + |
| 90 | + Capsule::statement('CREATE DATABASE ' . $db->database); |
| 91 | + Capsule::statement('CREATE USER \'' . $db->username . '\'@\'' . $db->remote . '\' IDENTIFIED BY \'' . Crypt::decrypt($db->password) . '\''); |
| 92 | + Capsule::statement('GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON ' . $db->database . '.* TO \'' . $db->username . '\'@\'' . $db->remote . '\''); |
| 93 | + Capsule::statement('FLUSH PRIVILEGES'); |
| 94 | + |
| 95 | + DB::commit(); |
| 96 | + return true; |
| 97 | + } catch (\Exception $ex) { |
| 98 | + DB::rollback(); |
| 99 | + throw $ex; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Drops a database from the associated MySQL Server |
| 105 | + * @param int $database The ID of the database to drop. |
| 106 | + * @return boolean |
| 107 | + */ |
| 108 | + public function drop($database) |
| 109 | + { |
| 110 | + $db = Models\Database::findOrFail($database); |
| 111 | + $dbr = Models\DatabaseServer::findOrFail($db->db_server); |
| 112 | + |
| 113 | + try { |
| 114 | + |
| 115 | + DB::beginTransaction(); |
| 116 | + |
| 117 | + $capsule = new Capsule; |
| 118 | + $capsule->addConnection([ |
| 119 | + 'driver' => 'mysql', |
| 120 | + 'host' => $dbr->host, |
| 121 | + 'port' => $dbr->port, |
| 122 | + 'database' => 'mysql', |
| 123 | + 'username' => $dbr->username, |
| 124 | + 'password' => Crypt::decrypt($dbr->password), |
| 125 | + 'charset' => 'utf8', |
| 126 | + 'collation' => 'utf8_unicode_ci', |
| 127 | + 'prefix' => '' |
| 128 | + ]); |
| 129 | + |
| 130 | + $capsule->setAsGlobal(); |
| 131 | + |
| 132 | + Capsule::statement('DROP USER \'' . $db->username . '\'@\'' . $db->remote . '\''); |
| 133 | + Capsule::statement('DROP DATABASE ' . $db->database); |
| 134 | + |
| 135 | + $db->delete(); |
| 136 | + |
| 137 | + DB::commit(); |
| 138 | + return true; |
| 139 | + } catch (\Exception $ex) { |
| 140 | + DB::rollback(); |
| 141 | + throw $ex; |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments