ts3phpframework
Loading...
Searching...
No Matches
Convert.php
Go to the documentation of this file.
1<?php
2
3namespace PlanetTeamSpeak\TeamSpeak3Framework\Helper;
4
5use DateTime;
6use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3;
7
15{
22 public static function bytes(int $bytes, int $precision = 10): string
23 {
24 // Identify if its a negative or positive number
25 $negative = (str_starts_with($bytes, '-')) ? true : false;
26
27 // force calculation with positive numbers only
28 $bytes = floatval(abs($bytes));
29
30 $unit_conversions = [
31 0 => ["UNIT" => "B", "VALUE" => pow(1024, 0)],
32 1 => ["UNIT" => "KiB", "VALUE" => pow(1024, 1)],
33 2 => ["UNIT" => "MiB", "VALUE" => pow(1024, 2)],
34 3 => ["UNIT" => "GiB", "VALUE" => pow(1024, 3)],
35 4 => ["UNIT" => "TiB", "VALUE" => pow(1024, 4)],
36 5 => ["UNIT" => "PiB", "VALUE" => pow(1024, 5)],
37 6 => ["UNIT" => "EiB", "VALUE" => pow(1024, 6)],
38 7 => ["UNIT" => "ZiB", "VALUE" => pow(1024, 7)],
39 8 => ["UNIT" => "YiB", "VALUE" => pow(1024, 8)],
40 ];
41
42 // Sort from the biggest defined unit to smallest to get the best human readable format.
43 krsort($unit_conversions);
44
45 foreach ($unit_conversions as $conversion) {
46 if ($bytes >= $conversion["VALUE"]) {
47 $result = $bytes / $conversion["VALUE"];
48 $result = strval(round($result, $precision)) . " " . $conversion["UNIT"];
49 return ($negative) ? '-' . $result : $result;
50 }
51 }
52
53 return ($negative) ? '-' . $bytes . " B" : $bytes . " B";
54 }
55
67 public static function seconds(int $seconds, bool $is_ms = false, string $format = "%aD %H:%I:%S"): string
68 {
69 if ($is_ms) {
70 $seconds = $seconds / 1000;
71 }
72
73 $current_datetime = new DateTime("@0");
74 $seconds_datetime = new DateTime("@$seconds");
75
76 return $current_datetime->diff($seconds_datetime)->format($format);
77 }
78
85 public static function codec(int $codec): string
86 {
88 return "Speex Narrowband";
89 }
91 return "Speex Wideband";
92 }
94 return "Speex Ultra-Wideband";
95 }
96 if ($codec == TeamSpeak3::CODEC_CELT_MONO) {
97 return "CELT Mono";
98 }
99 if ($codec == TeamSpeak3::CODEC_OPUS_VOICE) {
100 return "Opus Voice";
101 }
102 if ($codec == TeamSpeak3::CODEC_OPUS_MUSIC) {
103 return "Opus Music";
104 }
105
106 return "Unknown";
107 }
108
115 public static function groupType(int $type): string
116 {
118 return "Template";
119 }
121 return "Regular";
122 }
124 return "ServerQuery";
125 }
126
127 return "Unknown";
128 }
129
136 public static function permissionType(int $type): string
137 {
139 return "Server Group";
140 }
141 if ($type == TeamSpeak3::PERM_TYPE_CLIENT) {
142 return "Client";
143 }
144 if ($type == TeamSpeak3::PERM_TYPE_CHANNEL) {
145 return "Channel";
146 }
148 return "Channel Group";
149 }
151 return "Channel Client";
152 }
153
154 return "Unknown";
155 }
156
163 public static function permissionCategory(int $pcat): string
164 {
165 if ($pcat == TeamSpeak3::PERM_CAT_GLOBAL) {
166 return "Global";
167 }
169 return "Global / Information";
170 }
172 return "Global / Virtual Server Management";
173 }
175 return "Global / Administration";
176 }
178 return "Global / Settings";
179 }
180 if ($pcat == TeamSpeak3::PERM_CAT_SERVER) {
181 return "Virtual Server";
182 }
184 return "Virtual Server / Information";
185 }
187 return "Virtual Server / Administration";
188 }
190 return "Virtual Server / Settings";
191 }
192 if ($pcat == TeamSpeak3::PERM_CAT_CHANNEL) {
193 return "Channel";
194 }
196 return "Channel / Information";
197 }
199 return "Channel / Create";
200 }
202 return "Channel / Modify";
203 }
205 return "Channel / Delete";
206 }
208 return "Channel / Access";
209 }
210 if ($pcat == TeamSpeak3::PERM_CAT_GROUP) {
211 return "Group";
212 }
214 return "Group / Information";
215 }
217 return "Group / Create";
218 }
220 return "Group / Modify";
221 }
223 return "Group / Delete";
224 }
225 if ($pcat == TeamSpeak3::PERM_CAT_CLIENT) {
226 return "Client";
227 }
229 return "Client / Information";
230 }
232 return "Client / Admin";
233 }
235 return "Client / Basics";
236 }
238 return "Client / Modify";
239 }
241 return "File Transfer";
242 }
244 return "Grant";
245 }
246
247 return "Unknown";
248 }
249
256 public static function logLevel(mixed $level): string
257 {
258 if (is_numeric($level)) {
259 if ($level == TeamSpeak3::LOGLEVEL_CRITICAL) {
260 return "CRITICAL";
261 }
262 if ($level == TeamSpeak3::LOGLEVEL_ERROR) {
263 return "ERROR";
264 }
265 if ($level == TeamSpeak3::LOGLEVEL_DEBUG) {
266 return "DEBUG";
267 }
268 if ($level == TeamSpeak3::LOGLEVEL_WARNING) {
269 return "WARNING";
270 }
271 if ($level == TeamSpeak3::LOGLEVEL_INFO) {
272 return "INFO";
273 }
274
275 return "DEVELOP";
276 } else {
277 if (strtoupper($level) == "CRITICAL") {
279 }
280 if (strtoupper($level) == "ERROR") {
282 }
283 if (strtoupper($level) == "DEBUG") {
285 }
286 if (strtoupper($level) == "WARNING") {
288 }
289 if (strtoupper($level) == "INFO") {
291 }
292
294 }
295 }
296
303 public static function logEntry(string $entry): array
304 {
305 $parts = explode("|", $entry, 5);
306 $array = [];
307
308 if (count($parts) != 5) {
309 $array["timestamp"] = 0;
310 $array["level"] = TeamSpeak3::LOGLEVEL_ERROR;
311 $array["channel"] = "ParamParser";
312 $array["server_id"] = "";
313 $array["msg"] = StringHelper::factory("convert error (" . trim($entry) . ")");
314 $array["msg_plain"] = $entry;
315 $array["malformed"] = true;
316 } else {
317 $array["timestamp"] = strtotime(trim($parts[0]) . " UTC");
318 $array["level"] = self::logLevel(trim($parts[1]));
319 $array["channel"] = trim($parts[2]);
320 $array["server_id"] = trim($parts[3]);
321 $array["msg"] = StringHelper::factory(trim($parts[4]));
322 $array["msg_plain"] = $entry;
323 $array["malformed"] = false;
324 }
325
326 return $array;
327 }
328
335 public static function iconId(int $unsigned): int
336 {
337 $signed = $unsigned;
338
339 if (PHP_INT_SIZE > 4) { // 64-bit
340 if ($signed & 0x80000000) {
341 return $signed - 0x100000000;
342 }
343 }
344
345 return $signed;
346 }
347
354 public static function password(string $plain): string
355 {
356 return base64_encode(sha1($plain, true));
357 }
358
366 public static function version(string $version, string $format = "Y-m-d h:i:s"): string|StringHelper
367 {
368 if (!$version instanceof StringHelper) {
369 $version = new StringHelper($version);
370 }
371
372 $buildno = $version->section("[", 1)->filterDigits()->toInt();
373
374 return ($buildno <= 15001) ? $version : $version->section("[")->append("(" . date($format, $buildno) . ")");
375 }
376
383 public static function versionShort(string $version): StringHelper
384 {
385 if (!$version instanceof StringHelper) {
386 $version = new StringHelper($version);
387 }
388
389 return $version->section(" ");
390 }
391
398 public static function imageMimeType(string $binary): string
399 {
400 if (!preg_match('/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(\\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/', $binary, $matches)) {
401 return "image/svg+xml";
402 }
403
404 $type = [
405 1 => "image/jpeg",
406 2 => "image/gif",
407 3 => "image/png",
408 4 => "image/x-windows-bmp",
409 5 => "image/tiff",
410 6 => "image/x-ilbm",
411 ];
412
413 return $type[count($matches) - 1];
414 }
415}
Helper class for data conversion.
Definition Convert.php:15
static seconds(int $seconds, bool $is_ms=false, string $format="%aD %H:%I:%S")
Definition Convert.php:67
static bytes(int $bytes, int $precision=10)
Definition Convert.php:22
static version(string $version, string $format="Y-m-d h:i:s")
Definition Convert.php:366
section(string $separator, int $first=0, int $last=0)
const PERM_CAT_SERVER_INFORMATION
00100001: virtual server permissions -> virtual server information
const GROUP_DBTYPE_SERVERQUERY
2: global query group (used for ServerQuery clients)
const CODEC_SPEEX_WIDEBAND
1: speex wideband (mono, 16bit, 16kHz)
const PERM_CAT_CLIENT_ADM_ACTIONS
01010010: client permissions -> client administrative actions
const LOGLEVEL_DEBUG
3: output that might help find a problem
const PERM_CAT_NEEDED_MODIFY_POWER
11111111: needed permission modify power (grant) permissions
const PERM_CAT_GLOBAL
00010000: global permissions
const PERM_CAT_GLOBAL_SERVER_MGMT
00010010: global permissions -> virtual server management
const PERM_TYPE_CHANNELGROUP
3: channel group permission
const PERM_CAT_CLIENT
01010000: client permissions
const PERM_CAT_GLOBAL_INFORMATION
00010001: global permissions -> global information
const CODEC_CELT_MONO
3: celt mono (mono, 16bit, 48kHz)
const PERM_CAT_GROUP
01000000: group permissions
const GROUP_DBTYPE_TEMPLATE
0: template group (used for new virtual servers)
const PERM_CAT_CLIENT_INFORMATION
01010001: client permissions -> client information
const CODEC_OPUS_VOICE
3: opus voice (interactive)
const LOGLEVEL_INFO
4: informational output
const PERM_CAT_CHANNEL
00110000: channel permissions
const PERM_CAT_GROUP_MODIFY
01000011: group permissions -> edit groups
const PERM_TYPE_CHANNELCLIENT
4: channel-client specific permission
const PERM_CAT_GROUP_INFORMATION
01000001: group permissions -> group information
const PERM_CAT_CHANNEL_ACCESS
00110101: channel permissions -> access channels
const CODEC_OPUS_MUSIC
3: opus music (interactive)
const PERM_CAT_CLIENT_BASICS
01010011: client permissions -> client basic communication
const LOGLEVEL_ERROR
1: everything that is awful
const PERM_CAT_GLOBAL_ADM_ACTIONS
00010011: global permissions -> global administrative actions
const PERM_CAT_GLOBAL_SETTINGS
00010100: global permissions -> global settings
const PERM_CAT_GROUP_DELETE
01000100: group permissions -> delete groups
const CODEC_SPEEX_NARROWBAND
0: speex narrowband (mono, 16bit, 8kHz)
const PERM_CAT_GROUP_CREATE
01000010: group permissions -> create groups
const PERM_CAT_CHANNEL_CREATE
00110010: channel permissions -> create channels
const LOGLEVEL_CRITICAL
0: these messages stop the program
const PERM_CAT_CLIENT_MODIFY
01010100: client permissions -> edit clients
const PERM_CAT_CHANNEL_MODIFY
00110011: channel permissions -> edit channels
const PERM_CAT_SERVER_ADM_ACTIONS
00100010: virtual server permissions -> virtual server administrative actions
const PERM_CAT_SERVER
00100000: virtual server permissions
const PERM_TYPE_CHANNEL
2: channel specific permission
const PERM_CAT_SERVER_SETTINGS
00100011: virtual server permissions -> virtual server settings
const CODEC_SPEEX_ULTRAWIDEBAND
2: speex ultra-wideband (mono, 16bit, 32kHz)
const PERM_CAT_CHANNEL_INFORMATION
00110001: channel permissions -> channel information
const GROUP_DBTYPE_REGULAR
1: regular group (used for regular clients)
const PERM_TYPE_CLIENT
1: client specific permission
const LOGLEVEL_WARNING
2: everything that might be bad
const PERM_CAT_CHANNEL_DELETE
00110100: channel permissions -> delete channels
const PERM_CAT_FILETRANSFER
01100000: file transfer permissions
const PERM_TYPE_SERVERGROUP
0: server group permission