|
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\Repositories\ServiceRepository; |
|
26
|
|
|
|
|
27
|
|
|
use DB; |
|
28
|
|
|
use Uuid; |
|
29
|
|
|
use Storage; |
|
30
|
|
|
use Validator; |
|
31
|
|
|
use Pterodactyl\Models; |
|
32
|
|
|
use Pterodactyl\Services\UuidService; |
|
33
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
|
34
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
|
35
|
|
|
|
|
36
|
|
|
class Pack |
|
37
|
|
|
{ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
// |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function create(array $data) |
|
44
|
|
|
{ |
|
45
|
|
|
$validator = Validator::make($data, [ |
|
46
|
|
|
'name' => 'required|string', |
|
47
|
|
|
'version' => 'required|string', |
|
48
|
|
|
'description' => 'sometimes|nullable|string', |
|
49
|
|
|
'option' => 'required|exists:service_options,id', |
|
50
|
|
|
'selectable' => 'sometimes|boolean', |
|
51
|
|
|
'visible' => 'sometimes|boolean', |
|
52
|
|
|
'build_memory' => 'required|integer|min:0', |
|
53
|
|
|
'build_swap' => 'required|integer|min:0', |
|
54
|
|
|
'build_cpu' => 'required|integer|min:0', |
|
55
|
|
|
'build_io' => 'required|integer|min:10|max:1000', |
|
56
|
|
|
'build_container' => 'required|string', |
|
57
|
|
|
'build_script' => 'sometimes|nullable|string', |
|
58
|
|
|
]); |
|
59
|
|
|
|
|
60
|
|
|
if ($validator->fails()) { |
|
61
|
|
|
throw new DisplayValidationException($validator->errors()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (isset($data['file_upload'])) { |
|
65
|
|
|
if (! $data['file_upload']->isValid()) { |
|
66
|
|
|
throw new DisplayException('The file provided does not appear to be valid.'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (! in_array($data['file_upload']->getMimeType(), [ |
|
70
|
|
|
'application/zip', |
|
71
|
|
|
'application/gzip', |
|
72
|
|
|
])) { |
|
73
|
|
|
throw new DisplayException('The file provided does not meet the required filetypes of application/zip or application/gzip.'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
DB::beginTransaction(); |
|
78
|
|
|
try { |
|
79
|
|
|
$uuid = new UuidService; |
|
80
|
|
|
$pack = Models\ServicePack::create([ |
|
81
|
|
|
'option' => $data['option'], |
|
82
|
|
|
'uuid' => $uuid->generate('servers', 'uuid'), |
|
83
|
|
|
'build_memory' => $data['build_memory'], |
|
84
|
|
|
'build_swap' => $data['build_swap'], |
|
85
|
|
|
'build_cpu' => $data['build_swap'], |
|
86
|
|
|
'build_io' => $data['build_io'], |
|
87
|
|
|
'build_script' => (empty($data['build_script'])) ? null : $data['build_script'], |
|
88
|
|
|
'build_container' => $data['build_container'], |
|
89
|
|
|
'name' => $data['name'], |
|
90
|
|
|
'version' => $data['version'], |
|
91
|
|
|
'description' => (empty($data['description'])) ? null : $data['description'], |
|
92
|
|
|
'selectable' => isset($data['selectable']), |
|
93
|
|
|
'visible' => isset($data['visible']), |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
Storage::makeDirectory('packs/' . $pack->uuid); |
|
|
|
|
|
|
97
|
|
|
if (isset($data['file_upload'])) { |
|
98
|
|
|
$filename = ($data['file_upload']->getMimeType() === 'application/zip') ? 'archive.zip' : 'archive.tar.gz'; |
|
99
|
|
|
$data['file_upload']->storeAs('packs/' . $pack->uuid, $filename); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
DB::commit(); |
|
103
|
|
|
} catch (\Exception $ex) { |
|
104
|
|
|
DB::rollBack(); |
|
105
|
|
|
throw $ex; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $pack->id; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function createWithTemplate(array $data) |
|
112
|
|
|
{ |
|
113
|
|
|
if (! isset($data['file_upload'])) { |
|
114
|
|
|
throw new DisplayException('No template file was found submitted with this request.'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (! $data['file_upload']->isValid()) { |
|
118
|
|
|
throw new DisplayException('The file provided does not appear to be valid.'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (! in_array($data['file_upload']->getMimeType(), [ |
|
122
|
|
|
'application/zip', |
|
123
|
|
|
'text/plain', |
|
124
|
|
|
'application/json', |
|
125
|
|
|
])) { |
|
126
|
|
|
throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetypes of application/zip or application/json.'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if ($data['file_upload']->getMimeType() === 'application/zip') { |
|
130
|
|
|
$zip = new \ZipArchive; |
|
131
|
|
|
if (! $zip->open($data['file_upload']->path())) { |
|
132
|
|
|
throw new DisplayException('The uploaded archive was unable to be opened.'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$isZip = $zip->locateName('archive.zip'); |
|
136
|
|
|
$isTar = $zip->locateName('archive.tar.gz'); |
|
137
|
|
|
|
|
138
|
|
|
if ($zip->locateName('import.json') === false || ($isZip === false && $isTar === false)) { |
|
139
|
|
|
throw new DisplayException('This contents of the provided archive were in an invalid format.'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$json = json_decode($zip->getFromName('import.json')); |
|
143
|
|
|
$id = $this->create([ |
|
144
|
|
|
'name' => $json->name, |
|
145
|
|
|
'version' => $json->version, |
|
146
|
|
|
'description' => $json->description, |
|
147
|
|
|
'option' => $data['option'], |
|
148
|
|
|
'selectable' => $json->selectable, |
|
149
|
|
|
'visible' => $json->visible, |
|
150
|
|
|
'build_memory' => $json->build->memory, |
|
151
|
|
|
'build_swap' => $json->build->swap, |
|
152
|
|
|
'build_cpu' => $json->build->cpu, |
|
153
|
|
|
'build_io' => $json->build->io, |
|
154
|
|
|
'build_container' => $json->build->container, |
|
155
|
|
|
'build_script' => $json->build->script, |
|
156
|
|
|
]); |
|
157
|
|
|
|
|
158
|
|
|
$pack = Models\ServicePack::findOrFail($id); |
|
159
|
|
|
if (! $zip->extractTo(storage_path('app/packs/' . $pack->uuid), ($isZip === false) ? 'archive.tar.gz' : 'archive.zip')) { |
|
160
|
|
|
$pack->delete(); |
|
161
|
|
|
throw new DisplayException('Unable to extract the archive file to the correct location.'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$zip->close(); |
|
165
|
|
|
|
|
166
|
|
|
return $pack->id; |
|
167
|
|
|
} else { |
|
168
|
|
|
$json = json_decode(file_get_contents($data['file_upload']->path())); |
|
169
|
|
|
|
|
170
|
|
|
return $this->create([ |
|
171
|
|
|
'name' => $json->name, |
|
172
|
|
|
'version' => $json->version, |
|
173
|
|
|
'description' => $json->description, |
|
174
|
|
|
'option' => $data['option'], |
|
175
|
|
|
'selectable' => $json->selectable, |
|
176
|
|
|
'visible' => $json->visible, |
|
177
|
|
|
'build_memory' => $json->build->memory, |
|
178
|
|
|
'build_swap' => $json->build->swap, |
|
179
|
|
|
'build_cpu' => $json->build->cpu, |
|
180
|
|
|
'build_io' => $json->build->io, |
|
181
|
|
|
'build_container' => $json->build->container, |
|
182
|
|
|
'build_script' => $json->build->script, |
|
183
|
|
|
]); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function update($id, array $data) |
|
188
|
|
|
{ |
|
189
|
|
|
$validator = Validator::make($data, [ |
|
190
|
|
|
'name' => 'required|string', |
|
191
|
|
|
'version' => 'required|string', |
|
192
|
|
|
'description' => 'string', |
|
193
|
|
|
'option' => 'required|exists:service_options,id', |
|
194
|
|
|
'selectable' => 'sometimes|boolean', |
|
195
|
|
|
'visible' => 'sometimes|boolean', |
|
196
|
|
|
'build_memory' => 'required|integer|min:0', |
|
197
|
|
|
'build_swap' => 'required|integer|min:0', |
|
198
|
|
|
'build_cpu' => 'required|integer|min:0', |
|
199
|
|
|
'build_io' => 'required|integer|min:10|max:1000', |
|
200
|
|
|
'build_container' => 'required|string', |
|
201
|
|
|
'build_script' => 'sometimes|string', |
|
202
|
|
|
]); |
|
203
|
|
|
|
|
204
|
|
|
if ($validator->fails()) { |
|
205
|
|
|
throw new DisplayValidationException($validator->errors()); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
DB::transaction(function () use ($id, $data) { |
|
209
|
|
|
Models\ServicePack::findOrFail($id)->update([ |
|
210
|
|
|
'option' => $data['option'], |
|
211
|
|
|
'build_memory' => $data['build_memory'], |
|
212
|
|
|
'build_swap' => $data['build_swap'], |
|
213
|
|
|
'build_cpu' => $data['build_swap'], |
|
214
|
|
|
'build_io' => $data['build_io'], |
|
215
|
|
|
'build_script' => (empty($data['build_script'])) ? null : $data['build_script'], |
|
216
|
|
|
'build_container' => $data['build_container'], |
|
217
|
|
|
'name' => $data['name'], |
|
218
|
|
|
'version' => $data['version'], |
|
219
|
|
|
'description' => (empty($data['description'])) ? null : $data['description'], |
|
220
|
|
|
'selectable' => isset($data['selectable']), |
|
221
|
|
|
'visible' => isset($data['visible']), |
|
222
|
|
|
]); |
|
223
|
|
|
|
|
224
|
|
|
return true; |
|
225
|
|
|
}); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function delete($id) |
|
229
|
|
|
{ |
|
230
|
|
|
$pack = Models\ServicePack::findOrFail($id); |
|
231
|
|
|
// @TODO Check for linked servers; foreign key should block this. |
|
232
|
|
|
DB::transaction(function () use ($pack) { |
|
233
|
|
|
$pack->delete(); |
|
234
|
|
|
Storage::deleteDirectory('packs/' . $pack->uuid); |
|
|
|
|
|
|
235
|
|
|
}); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.