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 Log; |
28
|
|
|
use Pterodactyl\Models; |
29
|
|
|
use Illuminate\Http\Request; |
30
|
|
|
use Pterodactyl\Repositories; |
31
|
|
|
use GuzzleHttp\Exception\RequestException; |
32
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
33
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
34
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
35
|
|
|
|
36
|
|
|
class AjaxController extends Controller |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $files = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $folders = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $directory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Controller Constructor. |
55
|
|
|
*/ |
56
|
|
|
public function __construct() |
57
|
|
|
{ |
58
|
|
|
// |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns true or false depending on the power status of the requested server. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Http\Request $request |
65
|
|
|
* @param string $uuid |
66
|
|
|
* @return \Illuminate\Contracts\View\View |
67
|
|
|
*/ |
68
|
|
|
public function getStatus(Request $request, $uuid) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$server = Models\Server::getByUUID($uuid); |
71
|
|
|
|
72
|
|
|
if (! $server) { |
73
|
|
|
return response()->json([], 404); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$client = Models\Node::guzzleRequest($server->node); |
|
|
|
|
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
$res = $client->request('GET', '/server', [ |
80
|
|
|
'headers' => Models\Server::getGuzzleHeaders($uuid), |
81
|
|
|
]); |
82
|
|
|
if ($res->getStatusCode() === 200) { |
83
|
|
|
return response()->json(json_decode($res->getBody())); |
84
|
|
|
} |
85
|
|
|
} catch (RequestException $e) { |
86
|
|
|
// |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return response()->json([]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns a listing of files in a given directory for a server. |
94
|
|
|
* |
95
|
|
|
* @param \Illuminate\Http\Request $request |
96
|
|
|
* @param string $uuid` |
|
|
|
|
97
|
|
|
* @return \Illuminate\Contracts\View\View |
98
|
|
|
*/ |
99
|
|
|
public function postDirectoryList(Request $request, $uuid) |
100
|
|
|
{ |
101
|
|
|
$server = Models\Server::getByUUID($uuid); |
102
|
|
|
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); |
103
|
|
|
$this->authorize('list-files', $server); |
104
|
|
|
|
105
|
|
|
$prevDir = [ |
106
|
|
|
'header' => ($this->directory !== '/') ? $this->directory : '', |
107
|
|
|
]; |
108
|
|
|
if ($this->directory !== '/') { |
109
|
|
|
$prevDir['first'] = true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Determine if we should show back links in the file browser. |
113
|
|
|
// This code is strange, and could probably be rewritten much better. |
114
|
|
|
$goBack = explode('/', trim($this->directory, '/')); |
115
|
|
|
if (! empty(array_filter($goBack)) && count($goBack) >= 2) { |
116
|
|
|
$prevDir['show'] = true; |
117
|
|
|
array_pop($goBack); |
118
|
|
|
$prevDir['link'] = '/' . implode('/', $goBack); |
119
|
|
|
$prevDir['link_show'] = implode('/', $goBack) . '/'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$controller = new Repositories\Daemon\FileRepository($uuid); |
123
|
|
|
|
124
|
|
|
try { |
125
|
|
|
$directoryContents = $controller->returnDirectoryListing($this->directory); |
126
|
|
|
} catch (DisplayException $ex) { |
127
|
|
|
return response($ex->getMessage(), 500); |
128
|
|
|
} catch (\Exception $ex) { |
129
|
|
|
Log::error($ex); |
130
|
|
|
|
131
|
|
|
return response('An error occured while attempting to load the requested directory, please try again.', 500); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return view('server.files.list', [ |
135
|
|
|
'server' => $server, |
136
|
|
|
'files' => $directoryContents->files, |
137
|
|
|
'folders' => $directoryContents->folders, |
138
|
|
|
'editableMime' => Repositories\HelperRepository::editableFiles(), |
139
|
|
|
'directory' => $prevDir, |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Handles a POST request to save a file. |
145
|
|
|
* |
146
|
|
|
* @param Request $request |
147
|
|
|
* @param string $uuid |
148
|
|
|
* @return \Illuminate\Http\Response |
149
|
|
|
*/ |
150
|
|
|
public function postSaveFile(Request $request, $uuid) |
151
|
|
|
{ |
152
|
|
|
$server = Models\Server::getByUUID($uuid); |
153
|
|
|
$this->authorize('save-files', $server); |
154
|
|
|
|
155
|
|
|
$controller = new Repositories\Daemon\FileRepository($uuid); |
156
|
|
|
|
157
|
|
|
try { |
158
|
|
|
$controller->saveFileContents($request->input('file'), $request->input('contents')); |
|
|
|
|
159
|
|
|
|
160
|
|
|
return response(null, 204); |
161
|
|
|
} catch (DisplayException $ex) { |
162
|
|
|
return response($ex->getMessage(), 500); |
163
|
|
|
} catch (\Exception $ex) { |
164
|
|
|
Log::error($ex); |
165
|
|
|
|
166
|
|
|
return response('An error occured while attempting to save this file, please try again.', 500); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* [postSetPrimary description]. |
172
|
|
|
* @param Request $request |
173
|
|
|
* @param string $uuid |
174
|
|
|
* @return \Illuminate\Http\Response |
175
|
|
|
*/ |
176
|
|
|
public function postSetPrimary(Request $request, $uuid) |
177
|
|
|
{ |
178
|
|
|
$server = Models\Server::getByUUID($uuid); |
179
|
|
|
$this->authorize('set-connection', $server); |
180
|
|
|
|
181
|
|
|
if ((int) $request->input('allocation') === $server->allocation) { |
|
|
|
|
182
|
|
|
return response()->json([ |
|
|
|
|
183
|
|
|
'error' => 'You are already using this as your default connection.', |
184
|
|
|
], 409); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
try { |
188
|
|
|
$allocation = Models\Allocation::where('id', $request->input('allocation'))->where('assigned_to', $server->id)->first(); |
|
|
|
|
189
|
|
|
if (! $allocation) { |
190
|
|
|
return response()->json([ |
|
|
|
|
191
|
|
|
'error' => 'No allocation matching your request was found in the system.', |
192
|
|
|
], 422); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$repo = new Repositories\ServerRepository; |
196
|
|
|
$repo->changeBuild($server->id, [ |
197
|
|
|
'default' => $allocation->ip . ':' . $allocation->port, |
198
|
|
|
]); |
199
|
|
|
|
200
|
|
|
return response('The default connection for this server has been updated. Please be aware that you will need to restart your server for this change to go into effect.'); |
201
|
|
|
} catch (DisplayValidationException $ex) { |
202
|
|
|
return response()->json([ |
|
|
|
|
203
|
|
|
'error' => json_decode($ex->getMessage(), true), |
204
|
|
|
], 422); |
205
|
|
|
} catch (DisplayException $ex) { |
206
|
|
|
return response()->json([ |
|
|
|
|
207
|
|
|
'error' => $ex->getMessage(), |
208
|
|
|
], 503); |
209
|
|
|
} catch (\Exception $ex) { |
210
|
|
|
Log::error($ex); |
211
|
|
|
|
212
|
|
|
return response()->json([ |
|
|
|
|
213
|
|
|
'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.', |
214
|
|
|
], 503); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function postResetDatabasePassword(Request $request, $uuid) |
219
|
|
|
{ |
220
|
|
|
$server = Models\Server::getByUUID($uuid); |
221
|
|
|
$database = Models\Database::where('id', $request->input('database'))->where('server_id', $server->id)->firstOrFail(); |
|
|
|
|
222
|
|
|
|
223
|
|
|
$this->authorize('reset-db-password', $server); |
224
|
|
|
try { |
225
|
|
|
$repo = new Repositories\DatabaseRepository; |
226
|
|
|
$password = str_random(16); |
227
|
|
|
$repo->modifyPassword($request->input('database'), $password); |
|
|
|
|
228
|
|
|
|
229
|
|
|
return response($password); |
230
|
|
|
} catch (\Pterodactyl\Exceptions\DisplayException $ex) { |
231
|
|
|
return response()->json([ |
232
|
|
|
'error' => $ex->getMessage(), |
233
|
|
|
], 503); |
234
|
|
|
} catch (\Exception $ex) { |
235
|
|
|
Log::error($ex); |
236
|
|
|
|
237
|
|
|
return response()->json([ |
238
|
|
|
'error' => 'An unhandled error occured while attempting to modify this database\'s password.', |
239
|
|
|
], 503); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.