1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2016 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\Http\Controllers\Server; |
26
|
|
|
|
27
|
|
|
use DB; |
28
|
|
|
use Log; |
29
|
|
|
use Uuid; |
30
|
|
|
use Alert; |
31
|
|
|
use Javascript; |
32
|
|
|
use Pterodactyl\Models; |
33
|
|
|
use Illuminate\Http\Request; |
34
|
|
|
use InvalidArgumentException; |
35
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
36
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
37
|
|
|
use Pterodactyl\Repositories\ServerRepository; |
38
|
|
|
use Pterodactyl\Repositories\Daemon\FileRepository; |
39
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
40
|
|
|
|
41
|
|
|
class ServerController extends Controller |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* Controller Constructor. |
45
|
|
|
* |
46
|
|
|
* @return void |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
// |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Renders server index page for specified server. |
55
|
|
|
* |
56
|
|
|
* @param \Illuminate\Http\Request $request |
57
|
|
|
* @return \Illuminate\Contracts\View\View |
58
|
|
|
*/ |
59
|
|
|
public function getIndex(Request $request) |
60
|
|
|
{ |
61
|
|
|
$server = Models\Server::getByUUID($request->route()->server); |
62
|
|
|
|
63
|
|
|
Javascript::put([ |
64
|
|
|
'meta' => [ |
65
|
|
|
'saveFile' => route('server.files.save', $server->uuidShort), |
|
|
|
|
66
|
|
|
'csrfToken' => csrf_token(), |
67
|
|
|
], |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
return view('server.index', [ |
71
|
|
|
'server' => $server, |
72
|
|
|
'allocations' => Models\Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(), |
|
|
|
|
73
|
|
|
'node' => Models\Node::find($server->node), |
|
|
|
|
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Renders file overview page. |
79
|
|
|
* |
80
|
|
|
* @param Request $request |
81
|
|
|
* @return \Illuminate\Contracts\View\View |
82
|
|
|
*/ |
83
|
|
|
public function getFiles(Request $request, $uuid) |
84
|
|
|
{ |
85
|
|
|
$server = Models\Server::getByUUID($uuid); |
86
|
|
|
$this->authorize('list-files', $server); |
87
|
|
|
|
88
|
|
|
$node = Models\Node::find($server->node); |
|
|
|
|
89
|
|
|
|
90
|
|
|
Javascript::put([ |
91
|
|
|
'server' => collect($server->makeVisible('daemonSecret'))->only('uuid', 'uuidShort', 'daemonSecret'), |
92
|
|
|
'node' => collect($node)->only('fqdn', 'scheme', 'daemonListen'), |
93
|
|
|
'meta' => [ |
94
|
|
|
'directoryList' => route('server.files.directory-list', $server->uuidShort), |
|
|
|
|
95
|
|
|
'csrftoken' => csrf_token(), |
96
|
|
|
], |
97
|
|
|
'permissions' => [ |
98
|
|
|
'moveFiles' => $request->user()->can('move-files', $server), |
99
|
|
|
'copyFiles' => $request->user()->can('copy-files', $server), |
100
|
|
|
'compressFiles' => $request->user()->can('compress-files', $server), |
101
|
|
|
'decompressFiles' => $request->user()->can('decompress-files', $server), |
102
|
|
|
'createFiles' => $request->user()->can('create-files', $server), |
103
|
|
|
'downloadFiles' => $request->user()->can('download-files', $server), |
104
|
|
|
'deleteFiles' => $request->user()->can('delete-files', $server), |
105
|
|
|
], |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
return view('server.files.index', [ |
109
|
|
|
'server' => $server, |
110
|
|
|
'node' => $node, |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Renders add file page. |
116
|
|
|
* |
117
|
|
|
* @param Request $request |
118
|
|
|
* @return \Illuminate\Contracts\View\View |
119
|
|
|
*/ |
120
|
|
|
public function getAddFile(Request $request, $uuid) |
121
|
|
|
{ |
122
|
|
|
$server = Models\Server::getByUUID($uuid); |
123
|
|
|
$this->authorize('add-files', $server); |
124
|
|
|
|
125
|
|
|
return view('server.files.add', [ |
126
|
|
|
'server' => $server, |
127
|
|
|
'node' => Models\Node::find($server->node), |
|
|
|
|
128
|
|
|
'directory' => (in_array($request->get('dir'), [null, '/', ''])) ? '' : trim($request->get('dir'), '/') . '/', |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Renders edit file page for a given file. |
134
|
|
|
* |
135
|
|
|
* @param Request $request |
136
|
|
|
* @param string $uuid |
137
|
|
|
* @param string $file |
138
|
|
|
* @return \Illuminate\Contracts\View\View |
139
|
|
|
*/ |
140
|
|
|
public function getEditFile(Request $request, $uuid, $file) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$server = Models\Server::getByUUID($uuid); |
143
|
|
|
$this->authorize('edit-files', $server); |
144
|
|
|
|
145
|
|
|
$fileInfo = (object) pathinfo($file); |
146
|
|
|
$controller = new FileRepository($uuid); |
147
|
|
|
|
148
|
|
|
try { |
149
|
|
|
$fileContent = $controller->returnFileContents($file); |
150
|
|
|
} catch (DisplayException $ex) { |
151
|
|
|
Alert::danger($ex->getMessage())->flash(); |
152
|
|
|
|
153
|
|
|
return redirect()->route('server.files.index', $uuid); |
|
|
|
|
154
|
|
|
} catch (\Exception $ex) { |
155
|
|
|
Log::error($ex); |
156
|
|
|
Alert::danger('An error occured while attempting to load the requested file for editing, please try again.')->flash(); |
157
|
|
|
|
158
|
|
|
return redirect()->route('server.files.index', $uuid); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return view('server.files.edit', [ |
162
|
|
|
'server' => $server, |
163
|
|
|
'node' => Models\Node::find($server->node), |
|
|
|
|
164
|
|
|
'file' => $file, |
165
|
|
|
'stat' => $fileContent['stat'], |
166
|
|
|
'contents' => $fileContent['file']->content, |
167
|
|
|
'directory' => (in_array($fileInfo->dirname, ['.', './', '/'])) ? '/' : trim($fileInfo->dirname, '/') . '/', |
168
|
|
|
]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Handles downloading a file for the user. |
173
|
|
|
* |
174
|
|
|
* @param Request $request |
175
|
|
|
* @param string $uuid |
176
|
|
|
* @param string $file |
177
|
|
|
* @return \Illuminate\Contracts\View\View |
178
|
|
|
*/ |
179
|
|
|
public function getDownloadFile(Request $request, $uuid, $file) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$server = Models\Server::getByUUID($uuid); |
182
|
|
|
$node = Models\Node::find($server->node); |
|
|
|
|
183
|
|
|
|
184
|
|
|
$this->authorize('download-files', $server); |
185
|
|
|
|
186
|
|
|
$download = new Models\Download; |
187
|
|
|
|
188
|
|
|
$download->token = (string) Uuid::generate(4); |
|
|
|
|
189
|
|
|
$download->server = $server->uuid; |
|
|
|
|
190
|
|
|
$download->path = $file; |
|
|
|
|
191
|
|
|
|
192
|
|
|
$download->save(); |
193
|
|
|
|
194
|
|
|
return redirect($node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/file/download/' . $download->token); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Renders server settings page. |
199
|
|
|
* |
200
|
|
|
* @param \Illuminate\Http\Request $request |
201
|
|
|
* @return \Illuminate\Contracts\View\View |
202
|
|
|
*/ |
203
|
|
|
public function getSettings(Request $request, $uuid) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$server = Models\Server::getByUUID($uuid); |
206
|
|
|
$allocation = Models\Allocation::findOrFail($server->allocation); |
|
|
|
|
207
|
|
|
|
208
|
|
|
$variables = Models\ServiceVariables::select( |
209
|
|
|
'service_variables.*', |
210
|
|
|
DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue') |
211
|
|
|
)->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id') |
212
|
|
|
->where('service_variables.option_id', $server->option) |
|
|
|
|
213
|
|
|
->where('server_variables.server_id', $server->id) |
|
|
|
|
214
|
|
|
->get(); |
215
|
|
|
|
216
|
|
|
$service = Models\Service::select( |
217
|
|
|
DB::raw('IFNULL(service_options.executable, services.executable) as executable') |
218
|
|
|
)->leftJoin('service_options', 'service_options.parent_service', '=', 'services.id') |
219
|
|
|
->where('service_options.id', $server->option) |
220
|
|
|
->where('services.id', $server->service) |
|
|
|
|
221
|
|
|
->first(); |
222
|
|
|
|
223
|
|
|
$serverVariables = [ |
224
|
|
|
'{{SERVER_MEMORY}}' => $server->memory, |
|
|
|
|
225
|
|
|
'{{SERVER_IP}}' => $allocation->ip, |
226
|
|
|
'{{SERVER_PORT}}' => $allocation->port, |
227
|
|
|
]; |
228
|
|
|
|
229
|
|
|
$processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup); |
|
|
|
|
230
|
|
|
foreach ($variables as &$variable) { |
231
|
|
|
$replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '**'; |
232
|
|
|
$processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return view('server.settings', [ |
236
|
|
|
'server' => $server, |
237
|
|
|
'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port') |
238
|
|
|
->where('server_id', $server->id) |
239
|
|
|
->join('database_servers', 'database_servers.id', '=', 'databases.db_server') |
240
|
|
|
->get(), |
241
|
|
|
'node' => Models\Node::find($server->node), |
|
|
|
|
242
|
|
|
'variables' => $variables->where('user_viewable', 1), |
243
|
|
|
'service' => $service, |
244
|
|
|
'processedStartup' => $processed, |
245
|
|
|
]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
View Code Duplication |
public function postSettingsSFTP(Request $request, $uuid) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$server = Models\Server::getByUUID($uuid); |
251
|
|
|
$this->authorize('reset-sftp', $server); |
252
|
|
|
|
253
|
|
|
try { |
254
|
|
|
$repo = new ServerRepository; |
255
|
|
|
$repo->updateSFTPPassword($server->id, $request->input('sftp_pass')); |
|
|
|
|
256
|
|
|
Alert::success('Successfully updated this servers SFTP password.')->flash(); |
257
|
|
|
} catch (DisplayValidationException $ex) { |
258
|
|
|
return redirect()->route('server.settings', $uuid)->withErrors(json_decode($ex->getMessage())); |
259
|
|
|
} catch (DisplayException $ex) { |
260
|
|
|
Alert::danger($ex->getMessage())->flash(); |
261
|
|
|
} catch (\Exception $ex) { |
262
|
|
|
Log::error($ex); |
263
|
|
|
Alert::danger('An unknown error occured while attempting to update this server\'s SFTP settings.')->flash(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return redirect()->route('server.settings', $uuid); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function postSettingsStartup(Request $request, $uuid) |
270
|
|
|
{ |
271
|
|
|
$server = Models\Server::getByUUID($uuid); |
272
|
|
|
$this->authorize('edit-startup', $server); |
273
|
|
|
|
274
|
|
|
try { |
275
|
|
|
$repo = new ServerRepository; |
276
|
|
|
$repo->updateStartup($server->id, $request->except([ |
|
|
|
|
277
|
|
|
'_token', |
278
|
|
|
])); |
279
|
|
|
Alert::success('Server startup variables were successfully updated.')->flash(); |
280
|
|
|
} catch (DisplayException $ex) { |
281
|
|
|
Alert::danger($ex->getMessage())->flash(); |
282
|
|
|
} catch (\Exception $ex) { |
283
|
|
|
Log::error($ex); |
284
|
|
|
Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return redirect()->route('server.settings', [ |
288
|
|
|
'uuid' => $uuid, |
289
|
|
|
'tab' => 'tab_startup', |
290
|
|
|
]); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.