-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
141 lines (125 loc) · 3.79 KB
/
functions.php
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
<?php
require_once 'db.php';
/**
* Verfügbare Maps aus einer Datei abrufen.
*
* @param string $file
* @return array
*/
function getAvailableMaps($file) {
if (file_exists($file)) {
return file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
return [];
}
/**
* Konfigurationswert aus der Datenbank abrufen.
*
* @param string $key_name
* @return string|null
*/
function getConfigValue($key_name) {
$pdo = getDatabaseConnection();
$stmt = $pdo->prepare("SELECT value FROM config WHERE key_name = :key_name");
$stmt->execute(['key_name' => $key_name]);
$result = $stmt->fetch();
return $result ? $result['value'] : null;
}
/**
* Alle Server aus der Datenbank abrufen und mit IP und Port ergänzen.
*
* @return array
*/
function getAllServers() {
$pdo = getDatabaseConnection();
$servers = $pdo->query("SELECT * FROM servers")->fetchAll(PDO::FETCH_ASSOC);
foreach ($servers as &$server) {
$details = getPterodactylServerDetails($server['uuid']);
if ($details) {
$server['ip'] = $details['attributes']['resources']['allocation']['ip'] ?? 'Unbekannt';
$server['port'] = $details['attributes']['resources']['allocation']['port'] ?? 'Unbekannt';
} else {
$server['ip'] = 'Unbekannt';
$server['port'] = 'Unbekannt';
}
}
return $servers;
}
/**
* Details eines Servers von der Pterodactyl API abrufen.
*
* @param string $uuid
* @return array|null
*/
function getPterodactylServerDetails($uuid) {
$apiToken = getConfigValue('client_api_token');
$baseUrl = getConfigValue('client_api_base_url');
$url = "{$baseUrl}/servers/{$uuid}/resources";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer {$apiToken}",
"Accept: application/json"
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
return json_decode($response, true);
}
return null;
}
/**
* Alle Mods aus der Datenbank abrufen.
*
* @return array
*/
function getAllMods() {
$pdo = getDatabaseConnection();
$stmt = $pdo->query("SELECT * FROM mods");
return $stmt->fetchAll();
}
/**
* Nutzung des Servers protokollieren.
*
* @param int $server_id
* @param string $map_name
* @param string $mod_name
*/
function logServerUsage($server_id, $map_name, $mod_name) {
$pdo = getDatabaseConnection();
$stmt = $pdo->prepare("
INSERT INTO usage_logs (server_id, map_name, mod_name)
VALUES (:server_id, :map_name, :mod_name)
");
$stmt->execute([
'server_id' => $server_id,
'map_name' => $map_name,
'mod_name' => $mod_name,
]);
}
/**
* Serverstatus, Map und Mod basierend auf gespeicherten Dateien abrufen.
*
* @param int $server_id
* @param int $runtime_limit
* @return array
*/
function getServerStatusFromFiles($server_id, $runtime_limit) {
$start_time_file = "start_time_{$server_id}.txt";
$start_time = file_exists($start_time_file) ? (int)file_get_contents($start_time_file) : 0;
$elapsed_time = time() - $start_time;
$remaining_time = max(0, $runtime_limit - $elapsed_time);
$current_map_file = "current_map_{$server_id}.txt";
$current_mod_file = "current_mod_{$server_id}.txt";
$current_map = file_exists($current_map_file) ? trim(file_get_contents($current_map_file)) : 'Keine Map';
$current_mod = file_exists($current_mod_file) ? trim(file_get_contents($current_mod_file)) : 'Kein Mod';
$status = $remaining_time > 0 ? 'Gestartet' : 'Gestoppt';
return [
'map' => $current_map,
'mod' => $current_mod,
'status' => $status,
'remaining_time' => $remaining_time,
];
}
?>