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\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\Pack; |
36
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
37
|
|
|
|
38
|
|
|
class PackController extends Controller |
39
|
|
|
{ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
// |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function formatServices() |
46
|
|
|
{ |
47
|
|
|
$options = Models\ServiceOptions::select( |
48
|
|
|
'services.name AS p_service', |
49
|
|
|
'service_options.id', |
50
|
|
|
'service_options.name' |
51
|
|
|
)->join('services', 'services.id', '=', 'service_options.parent_service')->get(); |
52
|
|
|
|
53
|
|
|
$array = []; |
54
|
|
View Code Duplication |
foreach ($options as &$option) { |
|
|
|
|
55
|
|
|
if (! array_key_exists($option->p_service, $array)) { |
56
|
|
|
$array[$option->p_service] = []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$array[$option->p_service] = array_merge($array[$option->p_service], [[ |
60
|
|
|
'id' => $option->id, |
61
|
|
|
'name' => $option->name, |
62
|
|
|
]]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $array; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function listAll(Request $request) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
return view('admin.services.packs.index', [ |
71
|
|
|
'services' => Models\Service::all(), |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function listByOption(Request $request, $id) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$option = Models\ServiceOptions::findOrFail($id); |
78
|
|
|
|
79
|
|
|
return view('admin.services.packs.byoption', [ |
80
|
|
|
'packs' => Models\ServicePack::where('option', $option->id)->get(), |
81
|
|
|
'service' => Models\Service::findOrFail($option->parent_service), |
82
|
|
|
'option' => $option, |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function listByService(Request $request, $id) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return view('admin.services.packs.byservice', [ |
89
|
|
|
'service' => Models\Service::findOrFail($id), |
90
|
|
|
'options' => Models\ServiceOptions::select( |
91
|
|
|
'service_options.id', |
92
|
|
|
'service_options.name', |
93
|
|
|
DB::raw('(SELECT COUNT(id) FROM service_packs WHERE service_packs.option = service_options.id) AS p_count') |
94
|
|
|
)->where('parent_service', $id)->get(), |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function new(Request $request, $opt = null) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
return view('admin.services.packs.new', [ |
101
|
|
|
'services' => $this->formatServices(), |
102
|
|
|
'packFor' => $opt, |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function create(Request $request) |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
$repo = new Pack; |
110
|
|
|
$id = $repo->create($request->except([ |
111
|
|
|
'_token', |
112
|
|
|
])); |
113
|
|
|
Alert::success('Successfully created new service!')->flash(); |
114
|
|
|
|
115
|
|
|
return redirect()->route('admin.services.packs.edit', $id)->withInput(); |
116
|
|
|
} catch (DisplayValidationException $ex) { |
117
|
|
|
return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput(); |
|
|
|
|
118
|
|
|
} catch (DisplayException $ex) { |
119
|
|
|
Alert::danger($ex->getMessage())->flash(); |
120
|
|
|
} catch (\Exception $ex) { |
121
|
|
|
Log::error($ex); |
122
|
|
|
Alert::danger('An error occured while attempting to add a new service pack.')->flash(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return redirect()->route('admin.services.packs.new', $request->input('option'))->withInput(); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function edit(Request $request, $id) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$pack = Models\ServicePack::findOrFail($id); |
131
|
|
|
$option = Models\ServiceOptions::select('id', 'parent_service', 'name')->where('id', $pack->option)->first(); |
132
|
|
|
|
133
|
|
|
return view('admin.services.packs.edit', [ |
134
|
|
|
'pack' => $pack, |
135
|
|
|
'services' => $this->formatServices(), |
136
|
|
|
'files' => Storage::files('packs/' . $pack->uuid), |
|
|
|
|
137
|
|
|
'service' => Models\Service::findOrFail($option->parent_service), |
138
|
|
|
'option' => $option, |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function update(Request $request, $id) |
143
|
|
|
{ |
144
|
|
|
if (! is_null($request->input('action_delete'))) { |
145
|
|
|
try { |
146
|
|
|
$repo = new Pack; |
147
|
|
|
$repo->delete($id); |
148
|
|
|
Alert::success('The requested service pack has been deleted from the system.')->flash(); |
149
|
|
|
|
150
|
|
|
return redirect()->route('admin.services.packs'); |
151
|
|
|
} catch (DisplayException $ex) { |
152
|
|
|
Alert::danger($ex->getMessage())->flash(); |
153
|
|
|
} catch (\Exception $ex) { |
154
|
|
|
Log::error($ex); |
155
|
|
|
Alert::danger('An error occured while attempting to delete this pack.')->flash(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return redirect()->route('admin.services.packs.edit', $id); |
159
|
|
|
} else { |
160
|
|
|
try { |
161
|
|
|
$repo = new Pack; |
162
|
|
|
$repo->update($id, $request->except([ |
163
|
|
|
'_token', |
164
|
|
|
])); |
165
|
|
|
Alert::success('Service pack has been successfully updated.')->flash(); |
166
|
|
|
} catch (DisplayValidationException $ex) { |
167
|
|
|
return redirect()->route('admin.services.packs.edit', $id)->withErrors(json_decode($ex->getMessage()))->withInput(); |
168
|
|
|
} catch (\Exception $ex) { |
169
|
|
|
Log::error($ex); |
170
|
|
|
Alert::danger('An error occured while attempting to add edit this pack.')->flash(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return redirect()->route('admin.services.packs.edit', $id); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function export(Request $request, $id, $files = false) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
$pack = Models\ServicePack::findOrFail($id); |
180
|
|
|
$json = [ |
181
|
|
|
'name' => $pack->name, |
182
|
|
|
'version' => $pack->version, |
183
|
|
|
'description' => $pack->dscription, |
184
|
|
|
'selectable' => (bool) $pack->selectable, |
185
|
|
|
'visible' => (bool) $pack->visible, |
186
|
|
|
'build' => [ |
187
|
|
|
'memory' => $pack->build_memory, |
188
|
|
|
'swap' => $pack->build_swap, |
189
|
|
|
'cpu' => $pack->build_cpu, |
190
|
|
|
'io' => $pack->build_io, |
191
|
|
|
'container' => $pack->build_container, |
192
|
|
|
'script' => $pack->build_script, |
193
|
|
|
], |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'pterodactyl_'); |
197
|
|
|
if ((bool) $files) { |
198
|
|
|
$zip = new \ZipArchive; |
199
|
|
|
if (! $zip->open($filename, \ZipArchive::CREATE)) { |
200
|
|
|
abort(503, 'Unable to open file for writing.'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$files = Storage::files('packs/' . $pack->uuid); |
|
|
|
|
204
|
|
|
foreach ($files as $file) { |
205
|
|
|
$zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file))); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT)); |
209
|
|
|
$zip->close(); |
210
|
|
|
|
211
|
|
|
return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true); |
212
|
|
|
} else { |
213
|
|
|
$fp = fopen($filename, 'a+'); |
214
|
|
|
fwrite($fp, json_encode($json, JSON_PRETTY_PRINT)); |
215
|
|
|
fclose($fp); |
216
|
|
|
|
217
|
|
|
return response()->download($filename, 'pack-' . $pack->name . '.json', [ |
218
|
|
|
'Content-Type' => 'application/json', |
219
|
|
|
])->deleteFileAfterSend(true); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function uploadForm(Request $request, $for = null) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
return view('admin.services.packs.upload', [ |
226
|
|
|
'services' => $this->formatServices(), |
227
|
|
|
'for' => $for, |
228
|
|
|
]); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
View Code Duplication |
public function postUpload(Request $request) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
try { |
234
|
|
|
$repo = new Pack; |
235
|
|
|
$id = $repo->createWithTemplate($request->except([ |
236
|
|
|
'_token', |
237
|
|
|
])); |
238
|
|
|
Alert::success('Successfully created new service!')->flash(); |
239
|
|
|
|
240
|
|
|
return redirect()->route('admin.services.packs.edit', $id)->withInput(); |
241
|
|
|
} catch (DisplayValidationException $ex) { |
242
|
|
|
return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput(); |
243
|
|
|
} catch (DisplayException $ex) { |
244
|
|
|
Alert::danger($ex->getMessage())->flash(); |
245
|
|
|
} catch (\Exception $ex) { |
246
|
|
|
Log::error($ex); |
247
|
|
|
Alert::danger('An error occured while attempting to add a new service pack.')->flash(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return redirect()->back(); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.