1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
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
|
|
|
|
25
|
|
|
namespace Pterodactyl\Repositories; |
26
|
|
|
|
27
|
|
|
use DB; |
28
|
|
|
use Crypt; |
29
|
|
|
use Config; |
30
|
|
|
use Validator; |
31
|
|
|
use Pterodactyl\Models; |
32
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
33
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
34
|
|
|
|
35
|
|
|
class DatabaseRepository |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Adds a new database to a specified database host server. |
39
|
|
|
* |
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
|
|
|
* |
43
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException |
44
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException |
45
|
|
|
* @throws \Exception |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function create($server, $data) |
49
|
|
|
{ |
50
|
|
|
$server = Models\Server::findOrFail($server); |
51
|
|
|
|
52
|
|
|
$validator = Validator::make($data, [ |
53
|
|
|
'host' => 'required|exists:database_servers,id', |
54
|
|
|
'database' => 'required|regex:/^\w{1,100}$/', |
55
|
|
|
'connection' => 'required|regex:/^[0-9%.]{1,15}$/', |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
if ($validator->fails()) { |
59
|
|
|
throw new DisplayValidationException($validator->errors()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$host = Models\DatabaseServer::findOrFail($data['host']); |
63
|
|
|
DB::beginTransaction(); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$database = Models\Database::firstOrNew([ |
67
|
|
|
'server_id' => $server->id, |
68
|
|
|
'db_server' => $data['host'], |
69
|
|
|
'database' => sprintf('s%d_%s', $server->id, $data['database']), |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
if ($database->exists) { |
73
|
|
|
throw new DisplayException('A database with those details already exists in the system.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$database->username = sprintf('s%d_%s', $server->id, str_random(10)); |
77
|
|
|
$database->remote = $data['connection']; |
78
|
|
|
$database->password = Crypt::encrypt(str_random(20)); |
79
|
|
|
|
80
|
|
|
$database->save(); |
81
|
|
|
} catch (\Exception $ex) { |
82
|
|
|
DB::rollBack(); |
83
|
|
|
throw $ex; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
Config::set('database.connections.dynamic', [ |
87
|
|
|
'driver' => 'mysql', |
88
|
|
|
'host' => $host->host, |
89
|
|
|
'port' => $host->port, |
90
|
|
|
'database' => 'mysql', |
91
|
|
|
'username' => $host->username, |
92
|
|
|
'password' => Crypt::decrypt($host->password), |
93
|
|
|
'charset' => 'utf8', |
94
|
|
|
'collation' => 'utf8_unicode_ci', |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
DB::connection('dynamic')->statement(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database->database)); |
99
|
|
|
DB::connection('dynamic')->statement(sprintf( |
100
|
|
|
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', |
101
|
|
|
$database->username, $database->remote, Crypt::decrypt($database->password) |
102
|
|
|
)); |
103
|
|
|
DB::connection('dynamic')->statement(sprintf( |
104
|
|
|
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`', |
105
|
|
|
$database->database, $database->username, $database->remote |
106
|
|
|
)); |
107
|
|
|
|
108
|
|
|
DB::connection('dynamic')->statement('FLUSH PRIVILEGES'); |
109
|
|
|
|
110
|
|
|
// Save Everything |
111
|
|
|
DB::commit(); |
112
|
|
|
} catch (\Exception $ex) { |
113
|
|
|
try { |
114
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database)); |
115
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote)); |
116
|
|
|
DB::connection('dynamic')->statement('FLUSH PRIVILEGES'); |
117
|
|
|
} catch (\Exception $ex) { |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
DB::rollBack(); |
121
|
|
|
throw $ex; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Updates the password for a given database. |
127
|
|
|
* @param int $id The ID of the database to modify. |
128
|
|
|
* @param string $password The new password to use for the database. |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function password($id, $password) |
132
|
|
|
{ |
133
|
|
|
$database = Models\Database::with('host')->findOrFail($id); |
|
|
|
|
134
|
|
|
|
135
|
|
|
DB::beginTransaction(); |
136
|
|
|
try { |
137
|
|
|
$database->password = Crypt::encrypt($password); |
138
|
|
|
$database->save(); |
139
|
|
|
|
140
|
|
|
Config::set('database.connections.dynamic', [ |
141
|
|
|
'driver' => 'mysql', |
142
|
|
|
'host' => $database->host->host, |
143
|
|
|
'port' => $database->host->port, |
144
|
|
|
'database' => 'mysql', |
145
|
|
|
'username' => $database->host->username, |
146
|
|
|
'password' => Crypt::decrypt($database->host->password), |
147
|
|
|
'charset' => 'utf8', |
148
|
|
|
'collation' => 'utf8_unicode_ci', |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
DB::connection('dynamic')->statement(sprintf( |
152
|
|
|
'SET PASSWORD FOR `%s`@`%s` = PASSWORD(\'%s\')', |
153
|
|
|
$database->username, $database->remote, $password |
154
|
|
|
)); |
155
|
|
|
|
156
|
|
|
DB::commit(); |
157
|
|
|
} catch (\Exception $ex) { |
158
|
|
|
DB::rollBack(); |
159
|
|
|
throw $ex; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Drops a database from the associated MySQL Server. |
165
|
|
|
* @param int $id The ID of the database to drop. |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function drop($id) |
169
|
|
|
{ |
170
|
|
|
$database = Models\Database::with('host')->findOrFail($id); |
|
|
|
|
171
|
|
|
|
172
|
|
|
DB::beginTransaction(); |
173
|
|
|
try { |
174
|
|
|
Config::set('database.connections.dynamic', [ |
175
|
|
|
'driver' => 'mysql', |
176
|
|
|
'host' => $database->host->host, |
177
|
|
|
'port' => $database->host->port, |
178
|
|
|
'database' => 'mysql', |
179
|
|
|
'username' => $database->host->username, |
180
|
|
|
'password' => Crypt::decrypt($database->host->password), |
181
|
|
|
'charset' => 'utf8', |
182
|
|
|
'collation' => 'utf8_unicode_ci', |
183
|
|
|
]); |
184
|
|
|
|
185
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database)); |
186
|
|
|
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote)); |
187
|
|
|
DB::connection('dynamic')->statement('FLUSH PRIVILEGES'); |
188
|
|
|
|
189
|
|
|
$database->delete(); |
190
|
|
|
|
191
|
|
|
DB::commit(); |
192
|
|
|
} catch (\Exception $ex) { |
193
|
|
|
DB::rollback(); |
194
|
|
|
throw $ex; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Deletes a database server from the system if it is empty. |
200
|
|
|
* |
201
|
|
|
* @param int $server The ID of the Database Server. |
202
|
|
|
* @return |
203
|
|
|
*/ |
204
|
|
|
public function delete($server) |
205
|
|
|
{ |
206
|
|
|
$host = Models\DatabaseServer::withCount('databases')->findOrFail($server); |
207
|
|
|
|
208
|
|
|
if ($host->databases_count > 0) { |
209
|
|
|
throw new DisplayException('You cannot delete a database server that has active databases attached to it.'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $host->delete(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Adds a new Database Server to the system. |
217
|
|
|
* @param array $data |
218
|
|
|
*/ |
219
|
|
|
public function add(array $data) |
220
|
|
|
{ |
221
|
|
|
if (isset($data['host'])) { |
222
|
|
|
$data['host'] = gethostbyname($data['host']); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$validator = Validator::make($data, [ |
226
|
|
|
'name' => 'required|string|max:255', |
227
|
|
|
'host' => 'required|ip|unique:database_servers,host', |
228
|
|
|
'port' => 'required|numeric|between:1,65535', |
229
|
|
|
'username' => 'required|string|max:32', |
230
|
|
|
'password' => 'required|string', |
231
|
|
|
'linked_node' => 'sometimes', |
232
|
|
|
]); |
233
|
|
|
|
234
|
|
|
if ($validator->fails()) { |
235
|
|
|
throw new DisplayValidationException($validator->errors()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
DB::beginTransaction(); |
239
|
|
|
try { |
240
|
|
|
Config::set('database.connections.dynamic', [ |
241
|
|
|
'driver' => 'mysql', |
242
|
|
|
'host' => $data['host'], |
243
|
|
|
'port' => $data['port'], |
244
|
|
|
'database' => 'mysql', |
245
|
|
|
'username' => $data['username'], |
246
|
|
|
'password' => $data['password'], |
247
|
|
|
'charset' => 'utf8', |
248
|
|
|
'collation' => 'utf8_unicode_ci', |
249
|
|
|
]); |
250
|
|
|
|
251
|
|
|
// Allows us to check that we can connect to things. |
252
|
|
|
DB::connection('dynamic')->select('SELECT 1 FROM dual'); |
253
|
|
|
|
254
|
|
|
Models\DatabaseServer::create([ |
255
|
|
|
'name' => $data['name'], |
256
|
|
|
'host' => $data['host'], |
257
|
|
|
'port' => $data['port'], |
258
|
|
|
'username' => $data['username'], |
259
|
|
|
'password' => Crypt::encrypt($data['password']), |
260
|
|
|
'max_databases' => null, |
261
|
|
|
'linked_node' => (! empty($data['linked_node']) && $data['linked_node'] > 0) ? $data['linked_node'] : null, |
262
|
|
|
]); |
263
|
|
|
|
264
|
|
|
DB::commit(); |
265
|
|
|
} catch (\Exception $ex) { |
266
|
|
|
DB::rollBack(); |
267
|
|
|
throw $ex; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.