-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.php
More file actions
419 lines (360 loc) · 15.4 KB
/
config.php
File metadata and controls
419 lines (360 loc) · 15.4 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
// config.php - Configuration and utility functions
class MinecraftServerManager {
private const PISTON_META_MANIFEST = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";
private static $serverProcess = null;
private static $logFile = null;
public static function fetchJson($url) {
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (MCserverPy-PHP Setup)',
'timeout' => 60
]
]);
$data = file_get_contents($url, false, $context);
if ($data === false) {
throw new Exception("Failed to fetch data from $url");
}
return json_decode($data, true);
}
public static function getAvailableVersions() {
try {
$manifest = self::fetchJson(self::PISTON_META_MANIFEST);
$versions = $manifest['versions'] ?? [];
$latestRelease = $manifest['latest']['release'] ?? '';
$latestSnapshot = $manifest['latest']['snapshot'] ?? '';
// Filter to releases and recent snapshots
$releases = array_slice(array_filter($versions, fn($v) => $v['type'] === 'release'), 0, 20);
$snapshots = array_slice(array_filter($versions, fn($v) => $v['type'] === 'snapshot'), 0, 10);
return [
'latest_release' => $latestRelease,
'latest_snapshot' => $latestSnapshot,
'releases' => $releases,
'snapshots' => $snapshots
];
} catch (Exception $e) {
return ['error' => $e->getMessage()];
}
}
public static function getVersionInfo($version = 'latest') {
$manifest = self::fetchJson(self::PISTON_META_MANIFEST);
if (!$version || $version === 'latest') {
$versionId = $manifest['latest']['release'] ?? null;
if (!$versionId) {
throw new Exception("Could not determine latest release");
}
} else {
$versionId = $version;
}
$versions = $manifest['versions'] ?? [];
$selected = null;
foreach ($versions as $v) {
if ($v['id'] === $versionId) {
$selected = $v;
break;
}
}
if (!$selected) {
if (in_array($version, ['latest-snapshot', 'snapshot'])) {
$snapId = $manifest['latest']['snapshot'] ?? null;
if (!$snapId) {
throw new Exception("Could not determine latest snapshot");
}
foreach ($versions as $v) {
if ($v['id'] === $snapId) {
$selected = $v;
$versionId = $snapId;
break;
}
}
}
if (!$selected) {
throw new Exception("Version '$versionId' not found");
}
}
$versionMeta = self::fetchJson($selected['url']);
$serverDownload = $versionMeta['downloads']['server'] ?? null;
if (!$serverDownload) {
throw new Exception("No server download found for version $versionId");
}
return [$versionId, $serverDownload];
}
public static function checkJavaVersion() {
$output = shell_exec('java -version 2>&1');
if (!$output) {
return [false, 'java not found in PATH'];
}
// Extract version number
preg_match('/version "([^"]+)"/', $output, $matches);
$versionStr = $matches[1] ?? '';
$isOk = false;
if ($versionStr) {
$major = (int)explode('.', $versionStr)[0];
$isOk = $major >= 17;
} else {
// Fallback check
$isOk = strpos($output, '17') !== false ||
strpos($output, '18') !== false ||
strpos($output, '19') !== false ||
strpos($output, '20') !== false ||
strpos($output, '21') !== false;
}
return [$isOk, $output];
}
public static function ensureDir($path) {
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
}
public static function downloadFile($url, $destPath, $progressCallback = null) {
$tmpPath = $destPath . '.part';
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (MCserverPy-PHP Setup)',
'timeout' => 300
]
]);
$source = fopen($url, 'rb', false, $context);
$dest = fopen($tmpPath, 'wb');
if (!$source || !$dest) {
throw new Exception("Failed to open streams for download");
}
$downloaded = 0;
while (!feof($source)) {
$chunk = fread($source, 64 * 1024);
if ($chunk === false) break;
fwrite($dest, $chunk);
$downloaded += strlen($chunk);
if ($progressCallback) {
$progressCallback($downloaded);
}
}
fclose($source);
fclose($dest);
// Move to final location
if (file_exists($destPath)) {
unlink($destPath);
}
rename($tmpPath, $destPath);
}
public static function sha1File($path) {
return sha1_file($path);
}
public static function writeEula($serverDir, $acceptEula) {
$eulaPath = $serverDir . DIRECTORY_SEPARATOR . 'eula.txt';
$content = "# By changing the setting below to TRUE you are indicating your agreement to the EULA\n";
$content .= "# https://aka.ms/MinecraftEULA\n";
$content .= "eula=" . ($acceptEula ? 'true' : 'false') . "\n";
file_put_contents($eulaPath, $content);
return $eulaPath;
}
public static function writeStartScripts($serverDir, $minMem, $maxMem, $nogui = true, $jvmFlags = '') {
$jvmFlagsStr = $jvmFlags ? ' ' . $jvmFlags : '';
$cmd = "java -Xms{$minMem} -Xmx{$maxMem}{$jvmFlagsStr} -jar server.jar" . ($nogui ? ' nogui' : '');
// Windows batch file
$batPath = $serverDir . DIRECTORY_SEPARATOR . 'start.bat';
$batContent = "@echo off\n";
$batContent .= "REM Generated by MCserverPy-PHP setup script\n";
$batContent .= "$cmd\n";
$batContent .= "pause\n";
file_put_contents($batPath, $batContent);
// Unix shell script
$shPath = $serverDir . DIRECTORY_SEPARATOR . 'start.sh';
$shContent = "#!/usr/bin/env bash\n";
$shContent .= "# Generated by MCserverPy-PHP setup script\n";
$shContent .= "$cmd\n";
file_put_contents($shPath, $shContent);
chmod($shPath, 0755);
return [$batPath, $shPath];
}
public static function startServer($serverDir, $minMem, $maxMem, $nogui = true, $jvmFlags = '', $foreground = false) {
$jarPath = $serverDir . DIRECTORY_SEPARATOR . 'server.jar';
if (!file_exists($jarPath)) {
throw new Exception('server.jar not found. Run setup first.');
}
$pidFile = $serverDir . DIRECTORY_SEPARATOR . 'server.pid';
$logFile = $serverDir . DIRECTORY_SEPARATOR . 'server_php.log';
// Check if already running
if (file_exists($pidFile)) {
$pid = (int)file_get_contents($pidFile);
if (self::isProcessRunning($pid)) {
throw new Exception('Server is already running (PID: ' . $pid . ')');
}
}
$jvmFlagsStr = $jvmFlags ? ' ' . $jvmFlags : '';
$noguiStr = $nogui ? ' nogui' : '';
// For Windows, handle foreground mode differently
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
if ($foreground) {
// Start in foreground with visible console window
$cmd = sprintf('start "Minecraft Server" cmd /k "cd /d %s && java -Xms%s -Xmx%s%s -jar server.jar%s"',
escapeshellarg($serverDir),
escapeshellarg($minMem),
escapeshellarg($maxMem),
$jvmFlagsStr,
$noguiStr
);
shell_exec($cmd);
// For foreground mode, we can't easily track PID, so just mark as running
file_put_contents($pidFile, 'foreground');
return ['status' => 'started', 'pid' => 'foreground', 'mode' => 'foreground'];
} else {
// Background mode - use PowerShell for better process control
$cmd = sprintf('powershell -Command "Start-Process -FilePath java -ArgumentList \'-Xms%s\', \'-Xmx%s\'%s, \'-jar\', \'server.jar\'%s -WindowStyle Hidden -PassThru | ForEach-Object { $_.Id } | Out-File -FilePath \'%s\' -Encoding ascii"',
$minMem,
$maxMem,
$jvmFlagsStr ? ', \'' . str_replace('\'', '\'\'', $jvmFlagsStr) . '\'' : '',
$noguiStr ? ', \'nogui\'' : '',
str_replace('\\', '\\\\', $pidFile)
);
}
} else {
// Unix systems
if ($foreground) {
// Start in foreground (blocking)
$cmd = sprintf('java -Xms%s -Xmx%s%s -jar server.jar%s',
escapeshellarg($minMem),
escapeshellarg($maxMem),
$jvmFlagsStr,
$noguiStr
);
// Note: This will block the PHP process
chdir($serverDir);
passthru($cmd);
return ['status' => 'started', 'pid' => 'foreground', 'mode' => 'foreground'];
} else {
// Background mode
$cmd = sprintf('java -Xms%s -Xmx%s%s -jar server.jar%s > %s 2>&1 & echo $! > %s',
escapeshellarg($minMem),
escapeshellarg($maxMem),
$jvmFlagsStr,
$noguiStr,
escapeshellarg($logFile),
escapeshellarg($pidFile)
);
}
}
chdir($serverDir);
$output = shell_exec($cmd);
// Give it a moment to start
sleep(1);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// On Windows, check if we successfully got a PID
sleep(2); // Give PowerShell time to write PID
if (file_exists($pidFile)) {
$pidContent = trim(file_get_contents($pidFile));
if ($pidContent && $pidContent !== 'foreground') {
$pid = (int)$pidContent;
if (self::isProcessRunning($pid)) {
return ['status' => 'started', 'pid' => $pid, 'mode' => 'background'];
}
}
}
// Fallback: check by process name
$processes = shell_exec('tasklist /FI "IMAGENAME eq java.exe" /FO CSV');
if (strpos($processes, 'java.exe') !== false) {
file_put_contents($pidFile, 'running'); // Just a marker
return ['status' => 'started', 'pid' => 'unknown', 'mode' => 'background'];
} else {
throw new Exception('Failed to start server');
}
} else {
if (file_exists($pidFile)) {
$pid = (int)file_get_contents($pidFile);
return ['status' => 'started', 'pid' => $pid, 'mode' => 'background'];
} else {
throw new Exception('Failed to start server');
}
}
}
public static function stopServer($serverDir) {
$pidFile = $serverDir . DIRECTORY_SEPARATOR . 'server.pid';
if (!file_exists($pidFile)) {
throw new Exception('Server is not running');
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// On Windows, kill all java processes (crude but works for demo)
shell_exec('taskkill /F /IM java.exe');
} else {
$pid = (int)file_get_contents($pidFile);
if (!self::isProcessRunning($pid)) {
unlink($pidFile);
throw new Exception('Server is not running');
}
shell_exec("kill $pid");
}
unlink($pidFile);
return ['status' => 'stopped'];
}
public static function getServerStatus($serverDir) {
$pidFile = $serverDir . DIRECTORY_SEPARATOR . 'server.pid';
if (!file_exists($pidFile)) {
return ['status' => 'not_started'];
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$processes = shell_exec('tasklist /FI "IMAGENAME eq java.exe" /FO CSV');
if (strpos($processes, 'java.exe') !== false) {
return ['status' => 'running', 'pid' => 'unknown'];
} else {
unlink($pidFile);
return ['status' => 'stopped'];
}
} else {
$pid = (int)file_get_contents($pidFile);
if (self::isProcessRunning($pid)) {
return ['status' => 'running', 'pid' => $pid];
} else {
unlink($pidFile);
return ['status' => 'stopped'];
}
}
}
public static function getServerLogs($serverDir, $lines = 50, $source = 'auto') {
$latestLog = $serverDir . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . 'latest.log';
$phpLog = $serverDir . DIRECTORY_SEPARATOR . 'server_php.log';
// Decide candidates based on requested source
$candidates = [];
switch (strtolower($source)) {
case 'latest':
$candidates = [$latestLog];
break;
case 'php':
$candidates = [$phpLog];
break;
case 'auto':
default:
$candidates = [$latestLog, $phpLog];
break;
}
$logFile = null;
foreach ($candidates as $cand) {
if (file_exists($cand)) { $logFile = $cand; break; }
}
if (!$logFile || !file_exists($logFile)) {
return ['logs' => []];
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// Windows - use PowerShell to get last N lines (escape single quotes)
$escaped = str_replace("'", "''", $logFile);
$cmd = "powershell -Command \"Get-Content '$escaped' -Tail $lines\"";
} else {
// Unix - use tail
$cmd = "tail -n $lines " . escapeshellarg($logFile);
}
$output = shell_exec($cmd);
$logLines = $output ? explode("\n", trim($output)) : [];
return ['logs' => $logLines, 'source' => (strpos($logFile, 'latest.log') !== false ? 'latest' : 'php')];
}
private static function isProcessRunning($pid) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$output = shell_exec("tasklist /FI \"PID eq $pid\" /FO CSV");
return strpos($output, (string)$pid) !== false;
} else {
$output = shell_exec("ps -p $pid");
return strpos($output, (string)$pid) !== false;
}
}
}
?>