forked from dennisabrams/teamspeak-banner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts3-banner.php
More file actions
361 lines (326 loc) · 11.2 KB
/
Copy pathts3-banner.php
File metadata and controls
361 lines (326 loc) · 11.2 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
<?php
declare(strict_types=1);
use PlanetTeamSpeak\TeamSpeak3Framework\Exception\AdapterException;
use PlanetTeamSpeak\TeamSpeak3Framework\Exception\HelperException;
use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3;
use PlanetTeamSpeak\TeamSpeak3Framework\Exception\ServerQueryException;
final class TeamSpeakBanner
{
private readonly array $config;
private readonly ImageDrawer $drawer;
private TeamSpeak3 $ts3;
private GdImage $banner;
public function __construct()
{
$this->config = require_once('config.php');
require_once('drawing.php');
require_once($this->config['ts3']['library_path']);
}
/**
* Generates an output by connecting to the server, initializing an image, rendering a banner,
* and outputting the final image. Handles any exceptions that occur during the process.
*
* @return void
*/
public function generate(): void
{
try {
$this->connectToServer();
$this->initializeImage();
$this->renderBanner();
$this->outputImage();
} catch (Exception $e) {
$this->handleError($e);
}
}
/**
* Establishes a connection to the TeamSpeak 3 server using the configuration provided.
* Constructs a server query URI based on connection details and initializes the TeamSpeak3 instance.
*
* @return void
* @throws ServerQueryException
* @throws AdapterException
* @throws HelperException
*/
private function connectToServer(): void
{
$connection = $this->config['ts3']['connection'];
$queryUri = sprintf(
'serverquery://%s:%s@%s:%d/?server_port=%d',
rawurlencode($connection['username']),
rawurlencode($connection['password']),
$connection['ip'],
$connection['query_port'],
$connection['server_port']
);
$this->ts3 = TeamSpeak3::factory($queryUri);
}
/**
* Initializes the image for display, including loading, validating, and processing the background image.
*
* This method reads the background image from the configured path,
* validates the image data, applies optional blur effects if enabled,
* and prepares an ImageDrawer instance for rendering.
*
* @return void
*/
private function initializeImage(): void
{
$backgroundPath = $this->config['display']['background']['path'];
$imageData = file_get_contents($backgroundPath);
if ($imageData === false) {
throw new RuntimeException("Could not read background image: $backgroundPath");
}
$this->banner = imagecreatefromstring($imageData);
if ($this->banner === false) {
throw new RuntimeException("Invalid background image data");
}
// Apply blur if enabled
if ($this->config['display']['background']['blur']['enabled']) {
$this->applyBlur();
}
[$width, $height] = getimagesize($backgroundPath);
$this->drawer = new ImageDrawer($this->banner, $width, $height);
}
/**
* Applies a blur effect to the background image based on the configured intensity.
*
* This method uses the Gaussian blur filter multiple times, as specified by
* the blur intensity in the configuration, to achieve the desired blurring effect.
*
* @return void
*/
private function applyBlur(): void
{
$intensity = $this->config['display']['background']['blur']['intensity'];
for ($i = 0; $i < $intensity; $i++) {
imagefilter($this->banner, IMG_FILTER_GAUSSIAN_BLUR);
}
}
/**
* Renders the banner by drawing server and client details along with their statistics.
*
* This method retrieves server and client information, creates necessary colors,
* and draws the following components on the banner:
* - Server header with relevant details
* - Server and client statistics
* - Additional client information if the client is connected
*
* @return void
*/
private function renderBanner(): void
{
$serverInfo = $this->getServerInfo();
$clientInfo = $this->getClientInfo();
// Create colors
$colors = $this->createColors();
// Draw server header
$this->drawServerHeader($serverInfo, $colors);
// Draw statistics
$this->drawStatistics($serverInfo, $clientInfo, $colors);
// Draw client information
if ($clientInfo['connected']) {
$this->drawClientInfo($clientInfo, $colors);
}
}
/**
* Retrieves detailed information about the server, including its name, uptime, version,
* active channels, client statistics, maximum client capacity, and system load.
*
* The method gathers data from the TeamSpeak 3 server instance, processes and formats
* it where necessary, and returns the information as an associative array.
*
* @return array An associative array containing server details:
* - 'name': string (sanitized server name),
* - 'uptime': string (formatted server uptime),
* - 'version': string (server version),
* - 'channels': int (number of active channels),
* - 'clients': int (number of connected clients excluding bots),
* - 'maxClients': int (maximum number of allowed clients),
* - 'load': array (system load averages).
*/
private function getServerInfo(): array
{
return [
'name' => $this->sanitizeText($this->ts3->virtualserver_name),
'uptime' => $this->formatUptime($this->ts3->virtualserver_uptime),
'version' => strtok($this->ts3->virtualserver_version, ' '),
'channels' => $this->ts3->virtualserver_channelsonline,
'clients' => $this->ts3->virtualserver_clientsonline -
$this->ts3->virtualserver_queryclientsonline -
$this->config['ts3']['connection']['bots_count'],
'maxClients' => $this->ts3->virtualserver_maxclients,
'load' => sys_getloadavg()
];
}
/**
* Retrieves information about the current client and server state, including connection status,
* the most recently joined user, and the number of administrators connected.
*
* This method collects data about the client's connection to the server, such as their nickname,
* connection time, bandwidth usage, and server groups. It also identifies the last user to join
* the server and counts the number of connected administrators.
*
* @return array An associative array containing client information and server state details:
* - 'connected': (bool) Whether the client is connected.
* - 'nickname': (string) The sanitized nickname of the client, if connected.
* - 'connected_time': (string) The formatted duration since the connection started, if connected.
* - 'total_connections': (int) The total number of times the client has connected, if connected.
* - 'upload': (int) The bandwidth upload in the last minute, if connected.
* - 'download': (int) The bandwidth download in the last minute, if connected.
* - 'country': (string) The lowercase ISO country code of the client, if connected.
* - 'groups': (array) A list of server group IDs the client belongs to, if connected.
* - 'lastJoined': (array) An associative array with details of the last joined user:
* - 'time': (int) The connection time in milliseconds of the last joined user.
* - 'user': (string) The sanitized nickname of the last joined user.
* - 'adminCount': (int) The total number of connected administrators.
*/
private function getClientInfo(): array
{
$clientIp = $this->getClientIp();
$clientInfo = ['connected' => false];
$lastJoined = ['time' => PHP_INT_MAX, 'user' => ''];
$adminCount = 0;
foreach ($this->ts3->clientList() as $client) {
if ($client->client_type) {
continue;
}
$connectedTime = $client->connection_connected_time;
if ($connectedTime < $lastJoined['time']) {
$lastJoined = [
'time' => $connectedTime,
'user' => $this->sanitizeText($client->client_nickname)
];
}
if ($client->getProperty('connection_client_ip') === $clientIp) {
$clientInfo = [
'connected' => true,
'nickname' => $this->sanitizeText($client->client_nickname),
'connected_time' => $this->formatConnectedTime($connectedTime),
'total_connections' => $client->client_totalconnections,
'upload' => $client->connection_bandwidth_sent_last_minute_total,
'download' => $client->connection_bandwidth_received_last_minute_total,
'country' => strtolower($client->client_country),
'groups' => array_map('intval', explode(',', $client->client_servergroups))
];
}
if (in_array($this->config['admin']['group_id'],
explode(',', $client->client_servergroups))) {
$adminCount++;
}
}
$clientInfo['lastJoined'] = $lastJoined;
$clientInfo['adminCount'] = $adminCount;
return $clientInfo;
}
/**
* @return array
*/
private function createColors(): array
{
return [
'primary' => imagecolorallocatealpha(
$this->banner,
...$this->config['display']['text']['colors']['primary']
),
'secondary' => imagecolorallocatealpha(
$this->banner,
...$this->config['display']['text']['colors']['secondary']
),
'box' => imagecolorallocatealpha(
$this->banner,
...$this->config['display']['box']['color']
)
];
}
/**
* @param array $serverInfo
* @param array $colors
* @return void
*/
private function drawServerHeader(array $serverInfo, array $colors): void
{
$logo = $this->config['server']['logo']['path'];
if (empty($logo)) {
$this->drawer->drawText(
$this->config['display']['text']['sizes']['server'],
$colors['primary'],
$this->config['display']['text']['font'],
$serverInfo['name'],
2,
2.2
);
} else {
$this->drawer->drawImage($logo, $this->config['server']['logo']['size']);
}
}
/**
* @return void
*/
private function outputImage(): void
{
header('Content-Type: image/png');
imagepng($this->banner);
imagedestroy($this->banner);
}
/**
* @param Exception $e
* @return void
*/
private function handleError(Exception $e): void
{
header('Content-Type: text/html; charset=utf-8');
die(sprintf('<pre><b>Error Code: %d</b> %s</pre>',
$e->getCode(),
htmlspecialchars($e->getMessage())
));
}
/**
* @param string $text
* @return string
*/
private function sanitizeText(string $text): string
{
return $this->config['display']['text']['ascii_only']
? preg_replace('/[^[:ascii:]]/', '', $text)
: $text;
}
/**
* @param string $uptime
* @return string
*/
private function formatUptime(string $uptime): string
{
return str_replace(
['D', ':'],
['d', 'h '],
substr($uptime, 0, -3)
) . 'min';
}
/**
* @param int $msTime
* @return array
*/
private function formatConnectedTime(int $msTime): array
{
$minutes = (round($msTime / 60000) + 1);
return [
'hours' => floor($minutes / 60),
'minutes' => $minutes % 60
];
}
/**
* Retrieves the IP address of the client making the request.
*
* @return string The client's IP address, determined from server variables.
*/
private function getClientIp(): string
{
return $_SERVER['HTTP_CLIENT_IP']
?? $_SERVER['HTTP_X_FORWARDED_FOR']
?? $_SERVER['REMOTE_ADDR'];
}
}
// Execute
$banner = new TeamSpeakBanner();
$banner->generate();