forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjaxController.php
More file actions
214 lines (168 loc) · 6.1 KB
/
AjaxController.php
File metadata and controls
214 lines (168 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Pterodactyl\Http\Controllers\Server;
use Log;
use Debugbar;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class AjaxController extends Controller
{
/**
* @var array
*/
protected $files = [];
/**
* @var array
*/
protected $folders = [];
/**
* @var string
*/
protected $directory;
/**
* Controller Constructor
*/
public function __construct()
{
//
}
/**
* Returns true or false depending on the power status of the requested server.
*
* @param \Illuminate\Http\Request $request
* @param string $uuid
* @return \Illuminate\Contracts\View\View
*/
public function getStatus(Request $request, $uuid)
{
$server = Server::getByUUID($uuid);
if (!$server) {
return response()->json([], 404);
}
$client = Node::guzzleRequest($server->node);
try {
$res = $client->request('GET', '/server', [
'headers' => Server::getGuzzleHeaders($uuid)
]);
if($res->getStatusCode() === 200) {
return response()->json(json_decode($res->getBody()));
} else {
return response()->json([]);
}
} catch (RequestException $e) {
Log::notice('An exception was raised while attempting to contact a daemon instance to get server status information.', [
'exception' => $e->getMessage(),
'path' => $request->path()
]);
}
return response()->json([]);
}
/**
* Returns a listing of files in a given directory for a server.
*
* @param \Illuminate\Http\Request $request
* @param string $uuid`
* @return \Illuminate\Contracts\View\View
*/
public function postDirectoryList(Request $request, $uuid)
{
$server = Server::getByUUID($uuid);
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
$this->authorize('list-files', $server);
$prevDir = [
'header' => ($this->directory !== '/') ? $this->directory : ''
];
if ($this->directory !== '/') {
$prevDir['first'] = true;
}
// Determine if we should show back links in the file browser.
// This code is strange, and could probably be rewritten much better.
$goBack = explode('/', rtrim($this->directory, '/'));
if (isset($goBack[2]) && !empty($goBack[2])) {
$prevDir['show'] = true;
$prevDir['link'] = '/' . trim(str_replace(end($goBack), '', $this->directory), '/');
$prevDir['link_show'] = trim($prevDir['link'], '/');
}
$controller = new Repositories\Daemon\FileRepository($uuid);
try {
$directoryContents = $controller->returnDirectoryListing($this->directory);
} catch (\Exception $e) {
Debugbar::addException($e);
$exception = 'An error occured while attempting to load the requested directory, please try again.';
if ($e instanceof DisplayException) {
$exception = $e->getMessage();
}
return response($exception, 500);
}
return view('server.files.list', [
'server' => $server,
'files' => $directoryContents->files,
'folders' => $directoryContents->folders,
'extensions' => Repositories\HelperRepository::editableFiles(),
'directory' => $prevDir
]);
}
/**
* Handles a POST request to save a file.
*
* @param Request $request
* @param string $uuid
* @return \Illuminate\Http\Response
*/
public function postSaveFile(Request $request, $uuid)
{
$server = Server::getByUUID($uuid);
$this->authorize('save-files', $server);
$controller = new Repositories\Daemon\FileRepository($uuid);
try {
$controller->saveFileContents($request->input('file'), $request->input('contents'));
return response(null, 204);
} catch (\Exception $e) {
Debugbar::addException($e);
$exception = 'An error occured while attempting to save that file, please try again.';
if ($e instanceof DisplayException) {
$exception = $e->getMessage();
}
return response($exception, 500);
}
}
/**
* [postSetConnection description]
* @param Request $request
* @param string $uuid
* @return \Illuminate\Http\Response
*/
public function postSetConnection(Request $request, $uuid)
{
$server = Server::getByUUID($uuid);
$this->authorize('set-connection', $server);
if ($request->input('connection') === $server->ip . ':' . $server->port) {
return response()->json([
'error' => 'You are already using this as your default connection.'
], 409);
}
try {
$repo = new Repositories\ServerRepository;
$repo->changeBuild($server->id, [
'default' => $request->input('connection'),
]);
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.');
} catch (\Exception $e) {
if ($e instanceof \Pterodactyl\Exceptions\DisplayException || $e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
return response()->json([
'error' => $e->getMessage(),
], 503);
} else {
Log::error($e);
return response()->json([
'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.'
], 503);
}
}
}
}