-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.php
More file actions
363 lines (173 loc) · 7.5 KB
/
create.php
File metadata and controls
363 lines (173 loc) · 7.5 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
session_start();
include("global.php");
include("config.php");
include("plans.php");
if( checklogin() == false ) {
notloggedin("You must be logged in.");
}
$user = $_SESSION['discord_user'];
$getPanelIDofUser = $conn->query("SELECT * FROM users WHERE discord_id='" . mysqli_real_escape_string($conn, $user->id) . "'")->fetch_assoc()['pterodactyl_userid'];
if( isset($_POST['submit']) ) {
if( empty($_POST['server_name']) || empty($_POST['server_ram']) || empty($_POST['location']) || empty($_POST['servertype']) || empty($_POST['cpu']) || !isset($_POST['server_name']) || !isset($_POST['server_ram']) || !isset($_POST['location']) || !isset($_POST['servertype']) || !isset($_POST['cpu']) ) {
ShowError("All the fields are required.");
}
if(is_numeric($_POST['server_ram']) && $_POST['server_ram'] > 0 && $_POST['server_ram'] == round($_POST['server_ram'], 0)){
//pass
} else {
ShowError("RAM must be a number.");
}
if(is_numeric($_POST['cpu']) && $_POST['cpu'] > 0 && $_POST['cpu'] == round($_POST['cpu'], 0)){
//pass
} else {
ShowError("CPU must be a number.");
}
if( $_POST['server_ram'] < 512 ) {
ShowError("The minimum memory is 512 MB.");
}
//Check if user reached max servers or max CPU cores
$currentUserServersAmount = $conn->query("SELECT * FROM servers WHERE owner_id='" . mysqli_real_escape_string($conn, $user->id) . "'")->num_rows;
if( $currentUserServersAmount >= ($maxservers + $user_extra_servers) ) {
ShowError("Sorry, you have reached your maximum servers amount. (" . $maxservers . " servers is your maximum amount)");
}
if( $_POST['cpu'] > ($max_cores + $user_extra_cores) ) {
ShowError("Sorry, you have reached your maximum CPU cores. (" . ($max_cores + $user_extra_cores) . " is your maximum CPU cores)");
}
//Check if user exceeded his max RAM balance
$currentUsedBalance = 0 + $_POST['server_ram'];
$userServers = $conn->query("SELECT * FROM servers WHERE owner_id='" . mysqli_real_escape_string($conn, $user->id) . "'");
if($userServers->num_rows > 0) {
while($row = $userServers->fetch_assoc()) {
$ch = curl_init("https://" . $ptero_domain . "/api/application/servers/" . $row['pterodactyl_serverid']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " . $ptero_key,
"Content-Type: application/json",
"Accept: Application/vnd.pterodactyl.v1+json"
));
$result = curl_exec($ch);
curl_close($ch);
$currentUsedBalance = $currentUsedBalance + json_decode($result, true)['attributes']['limits']['memory'];
}
}
if( $currentUsedBalance > ($rambalance + $user_extra_ram) ) {
ShowError("Sorry, you have reached your max RAM balance. (" . ($rambalance + $user_extra_ram) . " MB is your balance)");
}
// Location checker
if( $GET_USER_LEVEL == 0 || $GET_USER_LEVEL == 1 ) {
// this user is free
$allowed_locations = array(10, 13, 14);
} else {
// this user is donator
$allowed_locations = array(9, 12);
}
if( !in_array($_POST['location'], $allowed_locations) ) {
ShowError("You don't have permissions to create a server in this location.");
}
// get some info for the specificied egg .. those are needed to create server
$ch = curl_init("https://" . $ptero_domain . "/api/application/nests/1/eggs/" . intval($_POST['servertype']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " . $ptero_key,
"Content-Type: application/json",
"Accept: Application/vnd.pterodactyl.v1+json"
));
$result = curl_exec($ch);
curl_close($ch);
$result_jdecoded = json_decode($result, true);
$docker_image = $result_jdecoded['attributes']['docker_image'];
$startup_info = $result_jdecoded['attributes']['startup'];
$ch = curl_init("https://" . $ptero_domain . "/api/application/servers");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " . $ptero_key,
"Content-Type: application/json",
"Accept: Application/vnd.pterodactyl.v1+json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
"name" => $_POST['server_name'],
"user" => intval($getPanelIDofUser),
"nest" => 1,
"egg" => intval($_POST['servertype']),
"docker_image" => $docker_image,
"startup" => $startup_info,
"limits" => array(
"memory" => $_POST['server_ram'],
"swap" => 0,
"disk" => 10000,
"io" => 500,
"cpu" => intval($_POST['cpu']) * 100
),
"feature_limits" => array(
"databases" => 0,
"allocations" => 0
),
"environment" => array(
"DL_VERSION" => "latest",
"SERVER_JARFILE" => "server.jar",
"BUILD_NUMBER" => "latest",
"BUNGEE_VERSION" => "latest",
),
"deploy" => array(
"locations" => [intval($_POST['location'])],
"dedicated_ip" => false,
"port_range" => []
),
"start_on_completion" => false
)));
$result = curl_exec($ch);
curl_close($ch);
$serverinfo = json_decode($result, true);
if( $serverinfo['object'] !== "server" ) {
ShowError("An error has occured while creating your server. If this error still exists for long time, please contact support.");
}
//add server to database
$conn->query("INSERT INTO servers (pterodactyl_serverid, owner_id) VALUES (" . $serverinfo['attributes']['id'] . ", '" . mysqli_real_escape_string($conn, $user->id) . "')");
//redirect to homepage with success message
ShowSuccess("Created server!");
}
// Get user's RAM balance
$currentUsedBalance = 0;
$userServers = $conn->query("SELECT * FROM servers WHERE owner_id='" . mysqli_real_escape_string($conn, $user->id) . "'");
if($userServers->num_rows > 0) {
while($row = $userServers->fetch_assoc()) {
$ch = curl_init("https://" . $ptero_domain . "/api/application/servers/" . $row['pterodactyl_serverid']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " . $ptero_key,
"Content-Type: application/json",
"Accept: Application/vnd.pterodactyl.v1+json"
));
$result = curl_exec($ch);
curl_close($ch);
$currentUsedBalance = $currentUsedBalance + json_decode($result, true)['attributes']['limits']['memory'];
}
}
?>
<strong>Your used RAM balance is:</strong> <?php echo $currentUsedBalance; ?> MB / <?php echo ($rambalance + $user_extra_ram); ?> MB
<form action="create" method="post">
Server Name: <input type="text" name="server_name" class="form-control" required><br />
Server RAM (in MB): <input type="number" name="server_ram" class="form-control" min="1000" value="1000" required><br />
CPU Cores: <input <input type="number" name="cpu" class="form-control" min="1" value="1" required><br />
Location:
<input type="radio" name="location" value="10"> Free - Germany
<input type="radio" name="location" value="13"> Free - US
<input type="radio" name="location" value="14"> Free - UK
<input type="radio" name="location" value="9"> Donator - Germany
<input type="radio" name="location" value="12"> Donator - US
<br /><br />
Server Type:
<div id="ServerType">
<div class="card" style="width: 25rem;">
<div class="card-body">
<h5 class="card-title">Hosting</h5>
<input type="radio" name="servertype" value="1"> BungeeCord
<input type="radio" name="servertype" value="3"> Spigot
<input type="radio" name="servertype" value="17"> NodeJS
</div>
</div>
</div>
<br />
<input type="submit" name="submit" value="Create!" class="btn btn-success">
</form>