3namespace PlanetTeamSpeak\TeamSpeak3Framework\Helper;
6use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3;
22 public static function bytes(
int $bytes,
int $precision = 10): string
25 $negative = (str_starts_with($bytes,
'-')) ?
true :
false;
28 $bytes = floatval(abs($bytes));
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)],
43 krsort($unit_conversions);
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;
53 return ($negative) ?
'-' . $bytes .
" B" : $bytes .
" B";
67 public static function seconds(
int $seconds,
bool $is_ms =
false,
string $format =
"%aD %H:%I:%S"): string
70 $seconds = $seconds / 1000;
73 $current_datetime =
new DateTime(
"@0");
74 $seconds_datetime =
new DateTime(
"@$seconds");
76 return $current_datetime->diff($seconds_datetime)->format($format);
85 public static function codec(
int $codec): string
88 return "Speex Narrowband";
91 return "Speex Wideband";
94 return "Speex Ultra-Wideband";
124 return "ServerQuery";
139 return "Server Group";
148 return "Channel Group";
151 return "Channel Client";
169 return "Global / Information";
172 return "Global / Virtual Server Management";
175 return "Global / Administration";
178 return "Global / Settings";
181 return "Virtual Server";
184 return "Virtual Server / Information";
187 return "Virtual Server / Administration";
190 return "Virtual Server / Settings";
196 return "Channel / Information";
199 return "Channel / Create";
202 return "Channel / Modify";
205 return "Channel / Delete";
208 return "Channel / Access";
214 return "Group / Information";
217 return "Group / Create";
220 return "Group / Modify";
223 return "Group / Delete";
229 return "Client / Information";
232 return "Client / Admin";
235 return "Client / Basics";
238 return "Client / Modify";
241 return "File Transfer";
256 public static function logLevel(mixed $level): string
258 if (is_numeric($level)) {
277 if (strtoupper($level) ==
"CRITICAL") {
280 if (strtoupper($level) ==
"ERROR") {
283 if (strtoupper($level) ==
"DEBUG") {
286 if (strtoupper($level) ==
"WARNING") {
289 if (strtoupper($level) ==
"INFO") {
303 public static function logEntry(
string $entry): array
305 $parts = explode(
"|", $entry, 5);
308 if (count($parts) != 5) {
309 $array[
"timestamp"] = 0;
311 $array[
"channel"] =
"ParamParser";
312 $array[
"server_id"] =
"";
314 $array[
"msg_plain"] = $entry;
315 $array[
"malformed"] =
true;
317 $array[
"timestamp"] = strtotime(trim($parts[0]) .
" UTC");
319 $array[
"channel"] = trim($parts[2]);
320 $array[
"server_id"] = trim($parts[3]);
322 $array[
"msg_plain"] = $entry;
323 $array[
"malformed"] =
false;
335 public static function iconId(
int $unsigned): int
339 if (PHP_INT_SIZE > 4) {
340 if ($signed & 0x80000000) {
341 return $signed - 0x100000000;
354 public static function password(
string $plain): string
356 return base64_encode(sha1($plain,
true));
372 $buildno = $version->
section(
"[", 1)->filterDigits()->toInt();
374 return ($buildno <= 15001) ? $version : $version->section(
"[")->append(
"(" . date($format, $buildno) .
")");
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";
408 4 =>
"image/x-windows-bmp",
413 return $type[count($matches) - 1];
Helper class for data conversion.
static permissionCategory(int $pcat)
static seconds(int $seconds, bool $is_ms=false, string $format="%aD %H:%I:%S")
static imageMimeType(string $binary)
static bytes(int $bytes, int $precision=10)
static version(string $version, string $format="Y-m-d h:i:s")
static logEntry(string $entry)
static iconId(int $unsigned)
static logLevel(mixed $level)
static groupType(int $type)
static password(string $plain)
static versionShort(string $version)
static permissionType(int $type)
Helper class for string handling.
section(string $separator, int $first=0, int $last=0)
static factory(string $string)
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 LOGLEVEL_DEVEL
5: development output
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