ts3phpframework
Loading...
Searching...
No Matches
Json.php
Go to the documentation of this file.
1<?php
2
3namespace PlanetTeamSpeak\TeamSpeak3Framework\Viewer;
4
5use PlanetTeamSpeak\TeamSpeak3Framework\Helper\Convert;
6use PlanetTeamSpeak\TeamSpeak3Framework\Node\Channel;
7use PlanetTeamSpeak\TeamSpeak3Framework\Node\ChannelGroup;
8use PlanetTeamSpeak\TeamSpeak3Framework\Node\Client;
9use PlanetTeamSpeak\TeamSpeak3Framework\Node\Node;
10use PlanetTeamSpeak\TeamSpeak3Framework\Node\Server;
11use PlanetTeamSpeak\TeamSpeak3Framework\Node\ServerGroup;
12use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3;
13use stdClass;
14
21class Json implements ViewerInterface
22{
28 protected array $data;
29
35 protected ?Node $currObj = null;
36
43 protected ?array $currSib = null;
44
51 protected int $lastLvl = 0;
52
59 public function __construct(array &$data = [])
60 {
61 $this->data = &$data;
62 return $this;
63 }
64
72 public function fetchObject(Node $node, array $siblings = []): string
73 {
74 $this->currObj = $node;
75 $this->currSib = $siblings;
76
77 $obj = new stdClass();
78
79 $obj->ident = $this->getId();
80 $obj->parent = $this->getParent();
81 $obj->children = $node->count();
82 $obj->level = $this->getLevel();
83 $obj->first = $obj->level != $this->lastLvl;
84 $obj->last = (bool)array_pop($siblings);
85 $obj->siblings = array_map("boolval", $siblings);
86 $obj->class = $this->getType();
87 $obj->name = $this->getName();
88 $obj->image = $this->getImage();
89 $obj->props = $this->getProps();
90
91 $this->data[] = $obj;
92 $this->lastLvl = $obj->level;
93
94 return "";
95 }
96
102 protected function getId(): bool|string
103 {
104 if ($this->currObj instanceof Server) {
105 return "ts3_s" . $this->currObj->virtualserver_id;
106 } elseif ($this->currObj instanceof Channel) {
107 return "ts3_c" . $this->currObj->cid;
108 } elseif ($this->currObj instanceof Client) {
109 return "ts3_u" . $this->currObj->clid;
110 }
111
112 return false;
113 }
114
120 protected function getParent(): string
121 {
122 if ($this->currObj instanceof Channel) {
123 return $this->currObj->pid ? "ts3_c" . $this->currObj->pid : "ts3_s" . $this->currObj->getParent()->getId();
124 } elseif ($this->currObj instanceof Client) {
125 return $this->currObj->cid ? "ts3_c" . $this->currObj->cid : "ts3_s" . $this->currObj->getParent()->getId();
126 }
127
128 return "ts3";
129 }
130
136 protected function getLevel(): int
137 {
138 if ($this->currObj instanceof Channel) {
139 return $this->currObj->getLevel() + 2;
140 } elseif ($this->currObj instanceof Client) {
141 return $this->currObj->channelGetById($this->currObj->cid)->getLevel() + 3;
142 }
143
144 return 1;
145 }
146
152 protected function getType(): string
153 {
154 if ($this->currObj instanceof Server) {
155 return "server";
156 } elseif ($this->currObj instanceof Channel) {
157 return "channel";
158 } elseif ($this->currObj instanceof Client) {
159 return "client";
160 } elseif ($this->currObj instanceof ServerGroup || $this->currObj instanceof ChannelGroup) {
161 return "group";
162 }
163
164 return "host";
165 }
166
174 protected function getClass(): string
175 {
176 $extras = "";
177
178 if ($this->currObj instanceof Channel && $this->currObj->isSpacer()) {
179 switch ($this->currObj->spacerGetType()) {
180 case (string)TeamSpeak3::SPACER_SOLIDLINE:
181 $extras .= " solidline";
182 break;
183
184 case (string)TeamSpeak3::SPACER_DASHLINE:
185 $extras .= " dashline";
186 break;
187
189 $extras .= " dashdotline";
190 break;
191
193 $extras .= " dashdotdotline";
194 break;
195
196 case (string)TeamSpeak3::SPACER_DOTLINE:
197 $extras .= " dotline";
198 break;
199 }
200
201 switch ($this->currObj->spacerGetAlign()) {
203 $extras .= " repeat";
204 break;
205
207 $extras .= " center";
208 break;
209
211 $extras .= " right";
212 break;
213
215 $extras .= " left";
216 break;
217 }
218 }
219
220 return $this->currObj->getClass(null) . $extras;
221 }
222
228 protected function getSpacerType(): string
229 {
230 $type = "";
231
232 if (!$this->currObj instanceof Channel || !$this->currObj->isSpacer()) {
233 return "none";
234 }
235
236 $type .= match ($this->currObj->spacerGetType()) {
237 (string)TeamSpeak3::SPACER_SOLIDLINE => "solidline",
238 (string)TeamSpeak3::SPACER_DASHLINE => "dashline",
239 (string)TeamSpeak3::SPACER_DASHDOTLINE => "dashdotline",
240 (string)TeamSpeak3::SPACER_DASHDOTDOTLINE => "dashdotdotline",
241 (string)TeamSpeak3::SPACER_DOTLINE => "dotline",
242 default => "custom",
243 };
244
245 if ($type == "custom") {
246 $type .= match ($this->currObj->spacerGetAlign()) {
250 default => "left",
251 };
252 }
253
254 return $type;
255 }
256
263 protected function getName(): string
264 {
265 if ($this->currObj instanceof Channel && $this->currObj->isSpacer()) {
266 return $this->currObj["channel_name"]->section("]", 1, 99)->toString();
267 } elseif ($this->currObj instanceof Client) {
268 $before = [];
269 $behind = [];
270
271 foreach ($this->currObj->memberOf() as $group) {
272 if ($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEFORE) {
273 $before[] = "[" . $group["name"] . "]";
274 } elseif ($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEHIND) {
275 $behind[] = "[" . $group["name"] . "]";
276 }
277 }
278
279 return trim(implode("", $before) . " " . $this->currObj . " " . implode("", $behind));
280 }
281
282 return $this->currObj->toString();
283 }
284
290 protected function getProps(): stdClass
291 {
292 $props = new stdClass();
293
294 if (is_a($this->currObj, Node::class)) {
295 $this->id = 0;
296 $this->icon = 0;
297 $props->version = $this->currObj->version("version")->toString();
298 $props->platform = $this->currObj->version("platform")->toString();
299 $props->users = $this->currObj->virtualservers_total_clients_online;
300 $props->slots = $this->currObj->virtualservers_total_maxclients;
301 $props->flags = 0;
302 } elseif (is_a($this->currObj, Server::class)) {
303 $props->id = $this->currObj->getId();
304 $props->icon = ($this->currObj->virtualserver_icon_id < 0) ? pow(2, 32) - ($this->currObj->virtualserver_icon_id * -1) : $this->currObj->virtualserver_icon_id;
305 $props->welcmsg = strlen($this->currObj->virtualserver_welcomemessage) ? trim($this->currObj->virtualserver_welcomemessage) : null;
306 $props->hostmsg = strlen($this->currObj->virtualserver_hostmessage) ? trim($this->currObj->virtualserver_hostmessage) : null;
307 $props->version = Convert::versionShort($this->currObj->virtualserver_version)->toString();
308 $props->platform = $this->currObj->virtualserver_platform->toString();
309 $props->country = null;
310 $props->users = $this->currObj->clientCount();
311 $props->slots = $this->currObj->virtualserver_maxclients;
312 $props->flags = 0;
313
314 $props->flags += $this->currObj->virtualserver_status === "online" ? 1 : 0;
315 $props->flags += $this->currObj->virtualserver_flag_password ? 2 : 0;
316 $props->flags += $this->currObj->virtualserver_autostart ? 4 : 0;
317 $props->flags += $this->currObj->virtualserver_weblist_enabled ? 8 : 0;
318 $props->flags += $this->currObj->virtualserver_ask_for_privilegekey ? 16 : 0;
319 } elseif (is_a($this->currObj, Channel::class)) {
320 $props->id = $this->currObj->getId();
321 $props->icon = 0;
322 if (!$this->currObj->isSpacer()) {
323 $props->icon = $this->currObj->channel_icon_id < 0 ? (2 ** 32) - ($this->currObj->channel_icon_id * -1) : $this->currObj->channel_icon_id;
324 }
325
326 $props->path = trim($this->currObj->getPathway());
327 $props->topic = strlen($this->currObj->channel_topic) ? trim($this->currObj->channel_topic) : null;
328 $props->codec = $this->currObj->channel_codec;
329 $props->users = $this->currObj->total_clients == -1 ? 0 : $this->currObj->total_clients;
330 $props->slots = $this->currObj->channel_maxclients == -1 ? $this->currObj->getParent()->virtualserver_maxclients : $this->currObj->channel_maxclients;
331 $props->famusers = $this->currObj->total_clients_family == -1 ? 0 : $this->currObj->total_clients_family;
332 $props->famslots = $this->currObj->channel_maxfamilyclients == -1 ? $this->currObj->getParent()->virtualserver_maxclients : $this->currObj->channel_maxfamilyclients;
333 $props->spacer = $this->getSpacerType();
334 $props->flags = 0;
335
336 $props->flags += $this->currObj->channel_flag_default ? 1 : 0;
337 $props->flags += $this->currObj->channel_flag_password ? 2 : 0;
338 $props->flags += $this->currObj->channel_flag_permanent ? 4 : 0;
339 $props->flags += $this->currObj->channel_flag_semi_permanent ? 8 : 0;
340 $props->flags += ($props->codec == 3 || $props->codec == 5) ? 16 : 0;
341 $props->flags += $this->currObj->channel_needed_talk_power != 0 ? 32 : 0;
342 $props->flags += $this->currObj->total_clients != -1 ? 64 : 0;
343 $props->flags += $this->currObj->isSpacer() ? 128 : 0;
344 } elseif (is_a($this->currObj, Client::class)) {
345 $props->id = $this->currObj->getId();
346 $props->icon = $this->currObj->client_icon_id < 0 ? pow(2, 32) - ($this->currObj->client_icon_id * -1) : $this->currObj->client_icon_id;
347 $props->version = Convert::versionShort($this->currObj->client_version)->toString();
348 $props->platform = $this->currObj->client_platform->toString();
349 $props->country = strlen($this->currObj->client_country) ? trim($this->currObj->client_country) : null;
350 $props->awaymesg = strlen($this->currObj->client_away_message) ? trim($this->currObj->client_away_message) : null;
351 $props->memberof = [];
352 $props->badges = $this->currObj->getBadges();
353 $props->flags = 0;
354
355 foreach ($this->currObj->memberOf() as $num => $group) {
356 $props->memberof[$num] = new stdClass();
357
358 $props->memberof[$num]->name = trim($group->name);
359 $props->memberof[$num]->icon = $group->iconid < 0 ? pow(2, 32) - ($group->iconid * -1) : $group->iconid;
360 $props->memberof[$num]->order = $group->sortid;
361 $props->memberof[$num]->flags = 0;
362
363 $props->memberof[$num]->flags += $group->namemode;
364 $props->memberof[$num]->flags += $group->type == 2 ? 4 : 0;
365 $props->memberof[$num]->flags += $group->type == 0 ? 8 : 0;
366 $props->memberof[$num]->flags += $group->savedb ? 16 : 0;
367 $props->memberof[$num]->flags += $group instanceof ServerGroup ? 32 : 0;
368 }
369
370 $props->flags += $this->currObj->client_away ? 1 : 0;
371 $props->flags += $this->currObj->client_is_recording ? 2 : 0;
372 $props->flags += $this->currObj->client_is_channel_commander ? 4 : 0;
373 $props->flags += $this->currObj->client_is_priority_speaker ? 8 : 0;
374 $props->flags += $this->currObj->client_is_talker ? 16 : 0;
375 $props->flags += $this->currObj->channelGetById($this->currObj->cid)->channel_needed_talk_power > $this->currObj->client_talk_power && !$this->currObj->client_is_talker ? 32 : 0;
376 $props->flags += $this->currObj->client_input_muted || !$this->currObj->client_input_hardware ? 64 : 0;
377 $props->flags += $this->currObj->client_output_muted || !$this->currObj->client_output_hardware ? 128 : 0;
378 } elseif (is_a($this->currObj, ServerGroup::class) || is_a($this->currObj, ChannelGroup::class)) {
379 $props->id = $this->currObj->getId();
380 $props->icon = $this->currObj->iconid < 0 ? pow(2, 32) - ($this->currObj->iconid * -1) : $this->currObj->iconid;
381 $props->order = $this->currObj->sortid;
382 $props->n_map = $this->currObj->n_member_addp;
383 $props->n_mrp = $this->currObj->n_member_removep;
384 $props->flags = 0;
385
386 $props->flags += $this->currObj->namemode;
387 $props->flags += $this->currObj->type == 2 ? 4 : 0;
388 $props->flags += $this->currObj->type == 0 ? 8 : 0;
389 $props->flags += $this->currObj->savedb ? 16 : 0;
390 $props->flags += $this->currObj instanceof ServerGroup ? 32 : 0;
391 }
392
393 return $props;
394 }
395
401 protected function getImage(): string
402 {
403 return str_replace("_", "-", $this->currObj->getIcon());
404 }
405
411 public function toString(): string
412 {
413 return $this->__toString();
414 }
415
421 public function __toString()
422 {
423 return json_encode($this->data);
424 }
425}
Class describing a TeamSpeak 3 channel group and all it's parameters.
Class describing a TeamSpeak 3 channel and all it's parameters.
Definition Channel.php:18
Class describing a TeamSpeak 3 client and all it's parameters.
Definition Client.php:18
Class describing a TeamSpeak 3 virtual server and all it's parameters.
Definition Server.php:21
const GROUP_NAMEMODE_BEHIND
2: display name after client nickname
const GROUP_NAMEMODE_BEFORE
1: display name before client nickname
const SPACER_ALIGN_REPEAT
3: repeat until the whole line is filled
const SPACER_DASHDOTDOTLINE
4: dash dot dot line
fetchObject(Node $node, array $siblings=[])
Definition Json.php:72