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),
('imprint_enabled', 'BOOL', 'false', 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`;
CREATE TABLE `DBPREFIXfaq` (

View File

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

View File

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

View File

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