Add more type declarations, support for hiding channels in viewer

This commit is contained in:
Wruczek 2020-10-06 04:29:59 +02:00
parent 8bcca7d0c1
commit 16b6e829fe
4 changed files with 49 additions and 52 deletions

View File

@ -36,7 +36,8 @@ INSERT INTO `DBPREFIXconfig` (`identifier`, `type`, `value`, `user_editable`) VA
('adminstatus_offlinehiddenbydefault', 'BOOL', 'false', 1), ('adminstatus_offlinehiddenbydefault', 'BOOL', 'false', 1),
('imprint_enabled', 'BOOL', 'false', 1), ('imprint_enabled', 'BOOL', 'false', 1),
('imprint_url', 'STRING', 'imprint.php', 1), ('imprint_url', 'STRING', 'imprint.php', 1),
('assigner_required_sgids', 'JSON', '[]', 1); ('assigner_required_sgids', 'JSON', '[]', 1),
('viewer_hidden_channel_ids', 'JSON', '[]', 1);
DROP TABLE IF EXISTS `DBPREFIXfaq`; DROP TABLE IF EXISTS `DBPREFIXfaq`;
CREATE TABLE `DBPREFIXfaq` ( CREATE TABLE `DBPREFIXfaq` (

View File

@ -34,11 +34,11 @@ class TeamSpeakChannel {
$this->info = $this->channelList[$cid]; $this->info = $this->channelList[$cid];
} }
private function getChannelList() { private function getChannelList(): ?array {
return $this->channelList; return $this->channelList;
} }
private function getClientList() { private function getClientList(): ?array {
if ($this->clientList === null) { if ($this->clientList === null) {
$this->clientList = CacheManager::i()->getClientList(); $this->clientList = CacheManager::i()->getClientList();
} }
@ -46,19 +46,19 @@ class TeamSpeakChannel {
return $this->clientList; return $this->clientList;
} }
public function getInfo() { public function getInfo(): ?array {
return $this->info; return $this->info;
} }
public function getId() { public function getId(): int {
return (int) $this->info["cid"]; return (int) $this->info["cid"];
} }
public function getName() { public function getName(): string {
return (string) $this->info["channel_name"]; return (string) $this->info["channel_name"];
} }
public function getDisplayName() { public function getDisplayName(): string {
if ($this->isSpacer()) { if ($this->isSpacer()) {
// If its a spacer, remove everything before the // If its a spacer, remove everything before the
// first "]", and then the "]" itself. // first "]", and then the "]" itself.
@ -68,15 +68,15 @@ class TeamSpeakChannel {
return $this->getName(); return $this->getName();
} }
public function isPermanent() { public function isPermanent(): bool {
return (bool) $this->info["channel_flag_permanent"]; return (bool) $this->info["channel_flag_permanent"];
} }
public function getParentId() { public function getParentId(): int {
return (int) $this->info["pid"]; return (int) $this->info["pid"];
} }
public function isOccupied($checkChildrens = false, $includeQuery = false) { public function isOccupied(bool $checkChildrens = false, bool $includeQuery = false): bool {
if ($checkChildrens) { if ($checkChildrens) {
// Loop through all the children channels, and check if they are occupied // Loop through all the children channels, and check if they are occupied
foreach ($this->getChildChannels(true) as $channel) { foreach ($this->getChildChannels(true) as $channel) {
@ -101,28 +101,28 @@ class TeamSpeakChannel {
return false; return false;
} }
public function hasPassword() { public function hasPassword(): bool {
return $this->info["channel_flag_password"] === 1; return $this->info["channel_flag_password"] === 1;
} }
public function getTotalClients() { public function getTotalClients(): int {
return (int) $this->info["total_clients"]; return (int) $this->info["total_clients"];
} }
public function isFullyOccupied() { public function isFullyOccupied(): bool {
return $this->info["channel_maxclients"] !== -1 && return $this->info["channel_maxclients"] !== -1 &&
$this->info["channel_maxclients"] <= $this->info["total_clients"]; $this->info["channel_maxclients"] <= $this->info["total_clients"];
} }
public function isDefaultChannel() { public function isDefaultChannel(): bool {
return $this->info["channel_flag_default"] === 1; return $this->info["channel_flag_default"] === 1;
} }
public function isTopChannel() { public function isTopChannel(): bool {
return $this->getParentId() === 0; return $this->getParentId() === 0;
} }
public function getParentChannels($max = -1) { public function getParentChannels(int $max = -1): array {
$pid = (int) $this->info["pid"]; $pid = (int) $this->info["pid"];
$parents = []; $parents = [];
@ -135,21 +135,21 @@ class TeamSpeakChannel {
return $parents; return $parents;
} }
public function getClosestParentChannel() { public function getClosestParentChannel(): ?TeamSpeakChannel {
$parentChannels = $this->getParentChannels(1); $parentChannels = $this->getParentChannels(1);
return isset($parentChannels[0]) ? $parentChannels[0] : null; return $parentChannels[0] ?? null;
} }
public function getChildChannels($resursive = false) { public function getChildChannels(bool $resursive = false): array {
$childList = []; $childList = [];
foreach ($this->getChannelList() as $child) { foreach ($this->getChannelList() as $channel) {
if ($child["pid"] === $this->getId()) { if ($channel["pid"] === $this->getId()) {
$childTSC = new TeamSpeakChannel($child); $childChannel = new TeamSpeakChannel($channel);
$childList[$childTSC->getId()] = $childTSC; $childList[$childChannel->getId()] = $childChannel;
if ($resursive) { if ($resursive) {
$childList += $childTSC->getChildChannels(true); $childList += $childChannel->getChildChannels(true);
} }
} }
} }
@ -157,17 +157,16 @@ class TeamSpeakChannel {
return $childList; return $childList;
} }
public function getClosestChildChannel() { public function getClosestChildChannel(): ?TeamSpeakChannel {
$childChannels = $this->getChildChannels(1); $childChannels = $this->getChildChannels(1);
return isset($childChannels[0]) ? $childChannels[0] : null; return $childChannels[0] ?? null;
} }
public function getChannelMembers($includeQuery = false) { public function getChannelMembers(bool $includeQuery = false): array {
$clientList = []; $clientList = [];
foreach ($this->getClientList() as $client) { foreach ($this->getClientList() as $client) {
if ($client["cid"] === $this->getId() && ($includeQuery || !$client["client_type"])) { if ($client["cid"] === $this->getId() && ($includeQuery || !$client["client_type"])) {
// $childTSC = new TeamSpeakClient($child["clid"]);
$clientList[$client["clid"]] = $client; $clientList[$client["clid"]] = $client;
} }
} }
@ -175,7 +174,7 @@ class TeamSpeakChannel {
return $clientList; return $clientList;
} }
public function isSpacer() { public function isSpacer(): bool {
return preg_match("/\[[^\]]*spacer[^\]]*\]/", $this->getName()) && $this->isPermanent() && !$this->getParentId(); return preg_match("/\[[^\]]*spacer[^\]]*\]/", $this->getName()) && $this->isPermanent() && !$this->getParentId();
} }
@ -225,7 +224,6 @@ class TeamSpeakChannel {
} }
} }
public function __toString() { public function __toString() {
return $this->getName(); return $this->getName();
} }

View File

@ -2,7 +2,6 @@
namespace Wruczek\TSWebsite; namespace Wruczek\TSWebsite;
use function __get;
use TeamSpeak3; use TeamSpeak3;
use Wruczek\TSWebsite\Utils\Utils; use Wruczek\TSWebsite\Utils\Utils;
@ -19,10 +18,11 @@ class ViewerRenderer {
private $renderQueryClients = false; private $renderQueryClients = false;
private $hiddenChannels = []; private $hiddenChannelIds;
public function __construct($imgPath) { public function __construct($imgPath, array $hiddenChannelIds = []) {
$this->imgPath = $imgPath; $this->imgPath = $imgPath;
$this->hiddenChannelIds = $hiddenChannelIds;
$cm = CacheManager::i(); $cm = CacheManager::i();
$this->serverInfo = $cm->getServerInfo(); $this->serverInfo = $cm->getServerInfo();
@ -38,11 +38,11 @@ class ViewerRenderer {
* or when we dont have required permissions to check for a specific item. * or when we dont have required permissions to check for a specific item.
* @return bool true on success, false otherwise * @return bool true on success, false otherwise
*/ */
public function checkRequiredData() { public function checkRequiredData(): bool {
return isset($this->channelList, $this->clientList, $this->serverGroupList, $this->channelGroupList); return isset($this->channelList, $this->clientList, $this->serverGroupList, $this->channelGroupList);
} }
private function add($html, ...$args) { private function add(string $html, string ...$args): void {
foreach ($args as $i => $iValue) { foreach ($args as $i => $iValue) {
// Prevent argument placeholder injection // Prevent argument placeholder injection
$iValue = str_replace(["{", "}"], ["&#123;", "&#125;"], $iValue); $iValue = str_replace(["{", "}"], ["&#123;", "&#125;"], $iValue);
@ -53,7 +53,7 @@ class ViewerRenderer {
$this->resultHtml .= $html; $this->resultHtml .= $html;
} }
public function renderViewer() { public function renderViewer(): string {
if (!$this->checkRequiredData()) { if (!$this->checkRequiredData()) {
throw new \Exception("Failed to load required data from the cache. " . throw new \Exception("Failed to load required data from the cache. " .
"Is the server online? Do we have enough permissions?"); "Is the server online? Do we have enough permissions?");
@ -84,7 +84,7 @@ EOD;
foreach ($this->channelList as $channel) { foreach ($this->channelList as $channel) {
// Start rendering the top channels, they are gonna // Start rendering the top channels, they are gonna
// render all the childrens recursively // render all the children recursively
if ($channel["pid"] === 0) { if ($channel["pid"] === 0) {
$this->renderChannel(new TeamSpeakChannel($channel)); $this->renderChannel(new TeamSpeakChannel($channel));
} }
@ -93,7 +93,7 @@ EOD;
return $this->resultHtml; return $this->resultHtml;
} }
public function getIcon($name, $tooltip = null, $alt = "Icon") { public function getIcon($name, string $tooltip = null, $alt = "Icon"): string {
if (is_string($name)) { if (is_string($name)) {
$path = "{$this->imgPath}/$name"; $path = "{$this->imgPath}/$name";
} else { } else {
@ -104,22 +104,19 @@ EOD;
return '<img class="icon" src="' . $path . '" alt="' . Utils::escape($alt) . '"' . $ttip . '>'; return '<img class="icon" src="' . $path . '" alt="' . Utils::escape($alt) . '"' . $ttip . '>';
} }
/** public function renderChannel(TeamSpeakChannel $channel): void {
* @param $channel TeamSpeakChannel
*/
public function renderChannel($channel) {
$hasParent = $channel->getParentId(); $hasParent = $channel->getParentId();
$isHidden = in_array($channel->getId(), $this->hiddenChannels); $isHidden = in_array($channel->getId(), $this->hiddenChannelIds, true);
$channelDisplayName = $channel->getDisplayName(); $channelDisplayName = $channel->getDisplayName();
$channelClasses = $hasParent ? "has-parent" : "no-parent"; $channelClasses = $hasParent ? "has-parent" : "no-parent";
$channelIcon = ""; $channelIcon = "";
$suffixIcons = ""; $suffixIcons = "";
// If this channel is occupied // If this channel is occupied
if ($channel->isOccupied(false, $this->renderQueryClients) && !$isHidden) { if (!$isHidden && $channel->isOccupied(false, $this->renderQueryClients)) {
$channelClasses .= " is-occupied"; $channelClasses .= " is-occupied";
} else if ($channel->isOccupied(true, $this->renderQueryClients) && !$isHidden) { } else if (!$isHidden && $channel->isOccupied(true, $this->renderQueryClients)) {
$channelClasses .= " occupied-childs"; $channelClasses .= " occupied-childs";
} else { } else {
$channelClasses .= " not-occupied"; $channelClasses .= " not-occupied";
@ -180,7 +177,7 @@ EOD;
$this->add('</div>' . PHP_EOL . PHP_EOL); $this->add('</div>' . PHP_EOL . PHP_EOL);
} }
public function renderClient($client) { public function renderClient(array $client): void {
$isQuery = (bool) $client["client_type"]; $isQuery = (bool) $client["client_type"];
$clientSGIDs = explode(",", $client["client_servergroups"]); $clientSGIDs = explode(",", $client["client_servergroups"]);
@ -234,7 +231,7 @@ EOD;
); );
} }
private function getChannelIcon(TeamSpeakChannel $channel, $isHidden) { private function getChannelIcon(TeamSpeakChannel $channel, bool $isHidden): string {
$subscribed = $isHidden ? "" : "_subscribed"; $subscribed = $isHidden ? "" : "_subscribed";
$unsub = $isHidden ? __get("VIEWER_CHANNEL_UNSUB1") : ""; $unsub = $isHidden ? __get("VIEWER_CHANNEL_UNSUB1") : "";
@ -253,7 +250,7 @@ EOD;
return $this->getIcon("channel_green{$subscribed}.svg", $isHidden ? __get("VIEWER_CHANNEL_UNSUB2") : null); return $this->getIcon("channel_green{$subscribed}.svg", $isHidden ? __get("VIEWER_CHANNEL_UNSUB2") : null);
} }
private function getChannelSuffixIcons(TeamSpeakChannel $channel) { private function getChannelSuffixIcons(TeamSpeakChannel $channel): string {
$info = $channel->getInfo(); $info = $channel->getInfo();
$html = ""; $html = "";
@ -281,7 +278,7 @@ EOD;
return $html; return $html;
} }
public function getClientIcon($client) { public function getClientIcon(array $client): string {
if($client["client_type"]) { if($client["client_type"]) {
return $this->getIcon("server_query.svg"); return $this->getIcon("server_query.svg");
} }
@ -313,7 +310,7 @@ EOD;
return $this->getIcon("player_off.svg"); return $this->getIcon("player_off.svg");
} }
public function getClientSuffixIcons($client, $groups, $cntp) { public function getClientSuffixIcons(array $client, array $groups, int $neededTalkPower): string {
$html = ""; $html = "";
if($client["client_is_priority_speaker"]) { if($client["client_is_priority_speaker"]) {
@ -326,7 +323,7 @@ EOD;
if($client["client_is_talker"]) { if($client["client_is_talker"]) {
$html .= $this->getIcon("talk_power_grant.svg", __get("VIEWER_CLIENT_TALK_POWER_GRANTED")); $html .= $this->getIcon("talk_power_grant.svg", __get("VIEWER_CLIENT_TALK_POWER_GRANTED"));
} else if($cntp && $cntp > $client["client_talk_power"]) { } else if($neededTalkPower && $neededTalkPower > $client["client_talk_power"]) {
$html .= $this->getIcon("input_muted.svg", __get("VIEWER_CLIENT_TALK_POWER_INSUFFICIENT")); $html .= $this->getIcon("input_muted.svg", __get("VIEWER_CLIENT_TALK_POWER_INSUFFICIENT"));
} }

View File

@ -1,12 +1,13 @@
<?php <?php
use Wruczek\TSWebsite\Config;
use Wruczek\TSWebsite\Utils\TemplateUtils; use Wruczek\TSWebsite\Utils\TemplateUtils;
use Wruczek\TSWebsite\ViewerRenderer; use Wruczek\TSWebsite\ViewerRenderer;
require_once __DIR__ . "/private/php/load.php"; require_once __DIR__ . "/private/php/load.php";
$html = null; $html = null;
$viewerRenderer = new ViewerRenderer("img/ts-icons"); $viewerRenderer = new ViewerRenderer("img/ts-icons", Config::get("viewer_hidden_channel_ids"));
if ($viewerRenderer->checkRequiredData()) { if ($viewerRenderer->checkRequiredData()) {
$html = $viewerRenderer->renderViewer(); $html = $viewerRenderer->renderViewer();