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\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::byUuid($uuid); |
71
|
|
|
|
72
|
|
|
if (! $server) { |
73
|
|
|
return response()->json([], 404); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (! $server->installed) { |
|
|
|
|
77
|
|
|
return response()->json(['status' => 20]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($server->suspended) { |
|
|
|
|
81
|
|
|
return response()->json(['status' => 30]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$res = $server->guzzleClient()->request('GET', '/server'); |
86
|
|
|
if ($res->getStatusCode() === 200) { |
87
|
|
|
return response()->json(json_decode($res->getBody())); |
88
|
|
|
} |
89
|
|
|
} catch (RequestException $e) { |
90
|
|
|
// |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return response()->json([]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a listing of files in a given directory for a server. |
98
|
|
|
* |
99
|
|
|
* @param \Illuminate\Http\Request $request |
100
|
|
|
* @param string $uuid` |
|
|
|
|
101
|
|
|
* @return \Illuminate\Contracts\View\View |
102
|
|
|
*/ |
103
|
|
|
public function postDirectoryList(Request $request, $uuid) |
104
|
|
|
{ |
105
|
|
|
$server = Models\Server::byUuid($uuid); |
106
|
|
|
$this->authorize('list-files', $server); |
107
|
|
|
|
108
|
|
|
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); |
109
|
|
|
$prevDir = [ |
110
|
|
|
'header' => ($this->directory !== '/') ? $this->directory : '', |
111
|
|
|
]; |
112
|
|
|
if ($this->directory !== '/') { |
113
|
|
|
$prevDir['first'] = true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Determine if we should show back links in the file browser. |
117
|
|
|
// This code is strange, and could probably be rewritten much better. |
118
|
|
|
$goBack = explode('/', trim($this->directory, '/')); |
119
|
|
|
if (! empty(array_filter($goBack)) && count($goBack) >= 2) { |
120
|
|
|
$prevDir['show'] = true; |
121
|
|
|
array_pop($goBack); |
122
|
|
|
$prevDir['link'] = '/' . implode('/', $goBack); |
123
|
|
|
$prevDir['link_show'] = implode('/', $goBack) . '/'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$controller = new Repositories\Daemon\FileRepository($uuid); |
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
$directoryContents = $controller->returnDirectoryListing($this->directory); |
130
|
|
|
} catch (DisplayException $ex) { |
131
|
|
|
return response($ex->getMessage(), 500); |
132
|
|
|
} catch (\Exception $ex) { |
133
|
|
|
Log::error($ex); |
134
|
|
|
|
135
|
|
|
return response('An error occured while attempting to load the requested directory, please try again.', 500); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return view('server.files.list', [ |
139
|
|
|
'server' => $server, |
140
|
|
|
'files' => $directoryContents->files, |
141
|
|
|
'folders' => $directoryContents->folders, |
142
|
|
|
'editableMime' => Repositories\HelperRepository::editableFiles(), |
143
|
|
|
'directory' => $prevDir, |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Handles a POST request to save a file. |
149
|
|
|
* |
150
|
|
|
* @param Request $request |
151
|
|
|
* @param string $uuid |
152
|
|
|
* @return \Illuminate\Http\Response |
153
|
|
|
*/ |
154
|
|
|
public function postSaveFile(Request $request, $uuid) |
155
|
|
|
{ |
156
|
|
|
$server = Models\Server::byUuid($uuid); |
157
|
|
|
$this->authorize('save-files', $server); |
158
|
|
|
|
159
|
|
|
$controller = new Repositories\Daemon\FileRepository($uuid); |
160
|
|
|
|
161
|
|
|
try { |
162
|
|
|
$controller->saveFileContents($request->input('file'), $request->input('contents')); |
|
|
|
|
163
|
|
|
|
164
|
|
|
return response(null, 204); |
165
|
|
|
} catch (DisplayException $ex) { |
166
|
|
|
return response($ex->getMessage(), 500); |
167
|
|
|
} catch (\Exception $ex) { |
168
|
|
|
Log::error($ex); |
169
|
|
|
|
170
|
|
|
return response('An error occured while attempting to save this file, please try again.', 500); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* [postSetPrimary description]. |
176
|
|
|
* @param Request $request |
177
|
|
|
* @param string $uuid |
178
|
|
|
* @return \Illuminate\Http\Response |
179
|
|
|
*/ |
180
|
|
|
public function postSetPrimary(Request $request, $uuid) |
181
|
|
|
{ |
182
|
|
|
$server = Models\Server::byUuid($uuid)->load('allocations'); |
183
|
|
|
$this->authorize('set-connection', $server); |
184
|
|
|
|
185
|
|
|
if ((int) $request->input('allocation') === $server->allocation_id) { |
|
|
|
|
186
|
|
|
return response()->json([ |
|
|
|
|
187
|
|
|
'error' => 'You are already using this as your default connection.', |
188
|
|
|
], 409); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
try { |
192
|
|
|
$allocation = $server->allocations->where('id', $request->input('allocation'))->where('server_id', $server->id)->first(); |
|
|
|
|
193
|
|
|
if (! $allocation) { |
194
|
|
|
return response()->json([ |
|
|
|
|
195
|
|
|
'error' => 'No allocation matching your request was found in the system.', |
196
|
|
|
], 422); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$repo = new Repositories\ServerRepository; |
200
|
|
|
$repo->changeBuild($server->id, [ |
201
|
|
|
'default' => $allocation->ip . ':' . $allocation->port, |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
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.'); |
205
|
|
|
} catch (DisplayValidationException $ex) { |
206
|
|
|
return response()->json([ |
|
|
|
|
207
|
|
|
'error' => json_decode($ex->getMessage(), true), |
208
|
|
|
], 422); |
209
|
|
|
} catch (DisplayException $ex) { |
210
|
|
|
return response()->json([ |
|
|
|
|
211
|
|
|
'error' => $ex->getMessage(), |
212
|
|
|
], 503); |
213
|
|
|
} catch (\Exception $ex) { |
214
|
|
|
Log::error($ex); |
215
|
|
|
|
216
|
|
|
return response()->json([ |
|
|
|
|
217
|
|
|
'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.', |
218
|
|
|
], 503); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function postResetDatabasePassword(Request $request, $uuid) |
223
|
|
|
{ |
224
|
|
|
$server = Models\Server::byUuid($uuid); |
225
|
|
|
$this->authorize('reset-db-password', $server); |
226
|
|
|
|
227
|
|
|
$database = Models\Database::where('server_id', $server->id)->findOrFail($request->input('database')); |
|
|
|
|
228
|
|
|
$repo = new Repositories\DatabaseRepository; |
229
|
|
|
|
230
|
|
|
try { |
231
|
|
|
$password = str_random(20); |
232
|
|
|
$repo->password($database->id, $password); |
233
|
|
|
|
234
|
|
|
return response($password); |
235
|
|
|
} catch (DisplayException $ex) { |
236
|
|
|
return response()->json(['error' => $ex->getMessage()], 503); |
237
|
|
|
} catch (\Exception $ex) { |
238
|
|
|
Log::error($ex); |
239
|
|
|
|
240
|
|
|
return response()->json([ |
241
|
|
|
'error' => 'An unhandled error occured while attempting to modify this database\'s password.', |
242
|
|
|
], 503); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.