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\Admin; |
26
|
|
|
|
27
|
|
|
use DB; |
28
|
|
|
use Log; |
29
|
|
|
use Alert; |
30
|
|
|
use Storage; |
31
|
|
|
use Pterodactyl\Models; |
32
|
|
|
use Illuminate\Http\Request; |
33
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
34
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
35
|
|
|
use Pterodactyl\Repositories\ServiceRepository; |
36
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
37
|
|
|
|
38
|
|
|
class ServiceController extends Controller |
39
|
|
|
{ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
// |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getIndex(Request $request) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return view('admin.services.index', [ |
48
|
|
|
'services' => Models\Service::withCount('servers')->get(), |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getNew(Request $request) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
return view('admin.services.new'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function postNew(Request $request) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$repo = new ServiceRepository\Service; |
61
|
|
|
$service = $repo->create($request->only([ |
62
|
|
|
'name', |
63
|
|
|
'description', |
64
|
|
|
'file', |
65
|
|
|
'executable', |
66
|
|
|
'startup', |
67
|
|
|
])); |
68
|
|
|
Alert::success('Successfully created new service!')->flash(); |
69
|
|
|
|
70
|
|
|
return redirect()->route('admin.services.service', $service->id); |
|
|
|
|
71
|
|
|
} catch (DisplayValidationException $ex) { |
72
|
|
|
return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
73
|
|
|
} catch (DisplayException $ex) { |
74
|
|
|
Alert::danger($ex->getMessage())->flash(); |
75
|
|
|
} catch (\Exception $ex) { |
76
|
|
|
Log::error($ex); |
77
|
|
|
Alert::danger('An error occured while attempting to add a new service.')->flash(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return redirect()->route('admin.services.new')->withInput(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getService(Request $request, $service) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return view('admin.services.view', [ |
86
|
|
|
'service' => Models\Service::with('options', 'options.servers')->findOrFail($service), |
|
|
|
|
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function postService(Request $request, $service) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$repo = new ServiceRepository\Service; |
94
|
|
|
$repo->update($service, $request->only([ |
95
|
|
|
'name', |
96
|
|
|
'description', |
97
|
|
|
'file', |
98
|
|
|
'executable', |
99
|
|
|
'startup', |
100
|
|
|
])); |
101
|
|
|
Alert::success('Successfully updated this service.')->flash(); |
102
|
|
|
} catch (DisplayValidationException $ex) { |
103
|
|
|
return redirect()->route('admin.services.service', $service)->withErrors(json_decode($ex->getMessage()))->withInput(); |
104
|
|
|
} catch (DisplayException $ex) { |
105
|
|
|
Alert::danger($ex->getMessage())->flash(); |
106
|
|
|
} catch (\Exception $ex) { |
107
|
|
|
Log::error($ex); |
108
|
|
|
Alert::danger('An error occurred while attempting to update this service.')->flash(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return redirect()->route('admin.services.service', $service)->withInput(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
public function deleteService(Request $request, $service) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$repo = new ServiceRepository\Service; |
118
|
|
|
$repo->delete($service); |
119
|
|
|
Alert::success('Successfully deleted that service.')->flash(); |
120
|
|
|
|
121
|
|
|
return redirect()->route('admin.services'); |
122
|
|
|
} catch (DisplayException $ex) { |
123
|
|
|
Alert::danger($ex->getMessage())->flash(); |
124
|
|
|
} catch (\Exception $ex) { |
125
|
|
|
Log::error($ex); |
126
|
|
|
Alert::danger('An error was encountered while attempting to delete that service.')->flash(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return redirect()->route('admin.services.service', $service); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getOption(Request $request, $service, $option) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$option = Models\ServiceOptions::with('service', 'variables')->findOrFail($option); |
|
|
|
|
135
|
|
|
$option->setRelation('servers', $option->servers()->with('user')->paginate(25)); |
136
|
|
|
|
137
|
|
|
return view('admin.services.options.view', [ |
138
|
|
|
'option' => $option, |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function postOption(Request $request, $service, $option) |
143
|
|
|
{ |
144
|
|
|
try { |
145
|
|
|
$repo = new ServiceRepository\Option; |
146
|
|
|
$repo->update($option, $request->only([ |
147
|
|
|
'name', |
148
|
|
|
'description', |
149
|
|
|
'tag', |
150
|
|
|
'executable', |
151
|
|
|
'docker_image', |
152
|
|
|
'startup', |
153
|
|
|
])); |
154
|
|
|
Alert::success('Option settings successfully updated.')->flash(); |
155
|
|
|
} catch (DisplayValidationException $ex) { |
156
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput(); |
157
|
|
|
} catch (\Exception $ex) { |
158
|
|
|
Log::error($ex); |
159
|
|
|
Alert::danger('An error occured while attempting to modify this option.')->flash(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withInput(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
View Code Duplication |
public function deleteOption(Request $request, $service, $option) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
try { |
168
|
|
|
$repo = new ServiceRepository\Option; |
169
|
|
|
$repo->delete($option); |
170
|
|
|
|
171
|
|
|
Alert::success('Successfully deleted that option.')->flash(); |
172
|
|
|
|
173
|
|
|
return redirect()->route('admin.services.service', $service); |
174
|
|
|
} catch (DisplayException $ex) { |
175
|
|
|
Alert::danger($ex->getMessage())->flash(); |
176
|
|
|
} catch (\Exception $ex) { |
177
|
|
|
Log::error($ex); |
178
|
|
|
Alert::danger('An error was encountered while attempting to delete this option.')->flash(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return redirect()->route('admin.services.option', [$service, $option]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function postOptionVariable(Request $request, $service, $option, $variable) |
185
|
|
|
{ |
186
|
|
|
try { |
187
|
|
|
$repo = new ServiceRepository\Variable; |
188
|
|
|
|
189
|
|
|
// Because of the way old() works on the display side we prefix all of the variables with thier ID |
190
|
|
|
// We need to remove that prefix here since the repo doesn't want it. |
191
|
|
|
$data = [ |
192
|
|
|
'user_viewable' => '0', |
193
|
|
|
'user_editable' => '0', |
194
|
|
|
'required' => '0', |
195
|
|
|
]; |
196
|
|
|
foreach ($request->except(['_token']) as $id => $val) { |
197
|
|
|
$data[str_replace($variable . '_', '', $id)] = $val; |
198
|
|
|
} |
199
|
|
|
$repo->update($variable, $data); |
200
|
|
|
Alert::success('Successfully updated variable.')->flash(); |
201
|
|
|
} catch (DisplayValidationException $ex) { |
202
|
|
|
$data = []; |
203
|
|
|
foreach (json_decode($ex->getMessage(), true) as $id => $val) { |
204
|
|
|
$data[$variable . '_' . $id] = $val; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withErrors((object) $data)->withInput(); |
208
|
|
|
} catch (DisplayException $ex) { |
209
|
|
|
Alert::danger($ex->getMessage())->flash(); |
210
|
|
|
} catch (\Exception $ex) { |
211
|
|
|
Log::error($ex); |
212
|
|
|
Alert::danger('An error occurred while attempting to update this service.')->flash(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withInput(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function getNewVariable(Request $request, $service, $option) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
return view('admin.services.options.variable', [ |
221
|
|
|
'option' => Models\ServiceOptions::with('service')->findOrFail($option), |
|
|
|
|
222
|
|
|
]); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function postNewVariable(Request $request, $service, $option) |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
|
|
$repo = new ServiceRepository\Variable; |
229
|
|
|
$repo->create($option, $request->only([ |
230
|
|
|
'name', |
231
|
|
|
'description', |
232
|
|
|
'env_variable', |
233
|
|
|
'default_value', |
234
|
|
|
'user_viewable', |
235
|
|
|
'user_editable', |
236
|
|
|
'required', |
237
|
|
|
'regex', |
238
|
|
|
])); |
239
|
|
|
Alert::success('Successfully added new variable to this option.')->flash(); |
240
|
|
|
|
241
|
|
|
return redirect()->route('admin.services.option', [$service, $option]); |
242
|
|
|
} catch (DisplayValidationException $ex) { |
243
|
|
|
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput(); |
244
|
|
|
} catch (DisplayException $ex) { |
245
|
|
|
Alert::danger($ex->getMessage())->flash(); |
246
|
|
|
} catch (\Exception $ex) { |
247
|
|
|
Log::error($ex); |
248
|
|
|
Alert::danger('An error occurred while attempting to add this variable.')->flash(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withInput(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function newOption(Request $request, $service) |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
return view('admin.services.options.new', [ |
257
|
|
|
'service' => Models\Service::findOrFail($service), |
258
|
|
|
]); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function postNewOption(Request $request, $service) |
262
|
|
|
{ |
263
|
|
|
try { |
264
|
|
|
$repo = new ServiceRepository\Option; |
265
|
|
|
$id = $repo->create($service, $request->except([ |
266
|
|
|
'_token', |
267
|
|
|
])); |
268
|
|
|
Alert::success('Successfully created new service option.')->flash(); |
269
|
|
|
|
270
|
|
|
return redirect()->route('admin.services.option', [$service, $id]); |
271
|
|
|
} catch (DisplayValidationException $ex) { |
272
|
|
|
return redirect()->route('admin.services.option.new', $service)->withErrors(json_decode($ex->getMessage()))->withInput(); |
273
|
|
|
} catch (\Exception $ex) { |
274
|
|
|
Log::error($ex); |
275
|
|
|
Alert::danger('An error occured while attempting to add this service option.')->flash(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return redirect()->route('admin.services.option.new', $service)->withInput(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function deleteVariable(Request $request, $service, $option, $variable) |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
try { |
284
|
|
|
$repo = new ServiceRepository\Variable; |
285
|
|
|
$repo->delete($variable); |
286
|
|
|
Alert::success('Deleted variable.')->flash(); |
287
|
|
|
} catch (DisplayException $ex) { |
288
|
|
|
Alert::danger($ex->getMessage())->flash(); |
289
|
|
|
} catch (\Exception $ex) { |
290
|
|
|
Log::error($ex); |
291
|
|
|
Alert::danger('An error occured while attempting to delete that variable.')->flash(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return redirect()->route('admin.services.option', [$service, $option]); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function getConfiguration(Request $request, $serviceId) |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
$service = Models\Service::findOrFail($serviceId); |
300
|
|
|
|
301
|
|
|
return view('admin.services.config', [ |
302
|
|
|
'service' => $service, |
303
|
|
|
'contents' => [ |
304
|
|
|
'json' => Storage::get('services/' . $service->file . '/main.json'), |
305
|
|
|
'index' => Storage::get('services/' . $service->file . '/index.js'), |
306
|
|
|
], |
307
|
|
|
]); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function postConfiguration(Request $request, $serviceId) |
311
|
|
|
{ |
312
|
|
|
try { |
313
|
|
|
$repo = new ServiceRepository\Service; |
314
|
|
|
$repo->updateFile($serviceId, $request->only([ |
315
|
|
|
'file', |
316
|
|
|
'contents', |
317
|
|
|
])); |
318
|
|
|
|
319
|
|
|
return response('', 204); |
320
|
|
|
} catch (DisplayException $ex) { |
321
|
|
|
return response()->json([ |
322
|
|
|
'error' => $ex->getMessage(), |
323
|
|
|
], 503); |
324
|
|
|
} catch (\Exception $ex) { |
325
|
|
|
Log::error($ex); |
326
|
|
|
|
327
|
|
|
return response()->json([ |
328
|
|
|
'error' => 'An error occured while attempting to save the file.', |
329
|
|
|
], 503); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.