Teamspeak 3: Connection Class

phpDue to the recently released Teamspeak 3 BETA I have made a connection class in PHP to communicate with the server and fetch back information that I can display in a visual format. I have included a download ZIP for this that includes the PHP code, styles and icons used.

The full package includes a stylesheet and icons, see the archived file at the bottom of this post.

<?php
	/**
	 *	Teamspeak3 PHP Connection Class
	 *  @author: Steve McMillan
	 *  @created: 14:55:06 21/12/2009
	 *  @desc: Teamspeak3 Connection class, provide connectivity for remote servers.
	 *
	 */

	class Teamspeak3 {
		var $connection;
		var $serverIP;
		var $serverPort;
		var $virtualServer;

		function __construct($serverIP, $serverPort, $virtualServer = 1) {
			$this->serverIP = $serverIP;
			$this->serverPort = $serverPort;
			$this->virtualServer = $virtualServer;
		}

		function fetchRawCommand($serverCommand, $bytes = 4096) {
			if(isset($this->serverIP) && isset($this->serverPort) && isset($serverCommand)) {
				$serverCommand = "use ".$this->virtualServer."\n".implode("\n", $serverCommand)."\nquit\n";
				$this->connection = @fsockopen ($this->serverIP, $this->serverPort, &$errno, &$errstr, 1);
				if($this->connection) {
					$dataReturn = array();
					$error = fputs($this->connection, $serverCommand, strlen($serverCommand));
					while($rawData = fgets($this->connection, $bytes)){
						$dataReturn[] = $rawData;
					}
					return $dataReturn;
				} else {
					echo "Connection to ". $this->serverIP . ":" . $this->serverPort . " (" . $this->virtualServer . ") Failed.";
					die();
				}
			}
		}

		function formatPlayersArray($playerData) {
			if(isset($playerData)) {
				$playerArray = array();
				$players = explode('|', $playerData[2]);
				$playerID = 0;
				foreach($players as $player) {
					$playerID++;
					$data = explode(' ', $player);
					foreach($data as $val) {
						$test = explode('=', $val);
						$playerArray[$playerID][trim($test[0])] = str_replace("\s", " ", trim($test[1]));
					}
				}
				return $playerArray;
			}
		}

		function formatChannelData($channelData) {
			if(isset($channelData)) {
				$channelCnt = 0;
				$channelList = array();
				$channels = $channelData[6];
				$channelTmp = explode('|', $channels);
				foreach($channelTmp as $channel) {
					$channelCnt++;
					$data = explode(' ', $channel);
					foreach($data as $val) {
						$test = explode('=', $val);
						$channelList[$channelCnt][trim($test[0])] = str_replace("\s", " ", trim($test[1]));
					}
				}
				return $channelList;
			}
		}

		function sortChannels($data) {
			$channelList = array();
			foreach($data as $channel) {
				if($this->isChannelParent($data, $channel['cid'])) {
					$channelList[][$channel['channel_name']] = $this->findChildrenByParent($data, $channel['cid']);
				} else {
					if($channel['pid'] == '0') {
						$channelList[] = $channel;
					}
				}
			}
			return $channelList;
		}

		function findChildrenByParent($data, $id) {
			if($id != 0) {
				$channels = array();
				foreach($data as $key=>$val) {
					if($val['pid'] == $id) {
						$channels[] = $val;
					}
				}
				return $channels;
			}
		}

		function isChannelParent($data, $id) {
			foreach($data as $key=>$val) {
				if($val['pid'] == $id) {
					return true;
				}
			}
			return false;
		}

		function findChannelByParent($data, $id) {
			if(is_array($data) && is_numeric($id)) {
				foreach($data as $arrfind=>$vals) {
					if($vals['cid'] == $id) {
						return $arrfind;
					}
				}
			}
		}

		function outputDisplay($sorted, $players) {
			$tsOutput = null;
			foreach($sorted as $key=>$channel) {
				if(isset($channel['cid'])) {
					$tsOutput .= '<div class="channel"><img src="icons/server.png" />&nbsp;'.$channel['channel_name'] . "</div>\n";
					foreach($players as $player) {
						if($player['cid'] == $channel['cid'] && $player['client_type'] != 1) {
							$tsOutput .= '<div class="user"><img src="icons/user.png" />&nbsp;'.$player['client_nickname']. '</div>';
						}
					}
				} else {
					if(is_array($channel)) {
						foreach($channel as $key=>$subchannel) {
							$tsOutput .= '<div class="channel"><img src="icons/server.png" />&nbsp;' . $key . '</div>';
							if(is_array($subchannel)) {
								foreach($subchannel as $key=>$schan) {
									$tsOutput .= '<div class="subchannel"><img src="icons/subserver.png" />&nbsp;'.$schan['channel_name'].'</div>';
									foreach($players as $player) {
										if($player['cid'] == $schan['cid'] && $player['client_type'] != 1) {
											$tsOutput .= '<div class="subuser"><img src="icons/user.png" />&nbsp;'.$player['client_nickname'].'</div>';
										}
									}
								}
							}
						}
					}
				}
			}
			return $tsOutput;
		}
	}
?>

Example usage below;

<link href="ts3.css" rel="stylesheet" type="text/css" media="screen" />

<?php
	//Include the TS3 Connection Class
	require('ts3.class.php');

	//Init the class, with out IP, Port, Virtual Server
	$TS3 = new Teamspeak3('0.0.0.0', '10011', '1');

	//Send the commands we want data from.
	$rawData = $TS3->fetchRawCommand(array("clientlist", "serverinfo", "channellist"));

	//Output the display
	echo $TS3->outputDisplay($TS3->sortChannels($TS3->formatChannelData($rawData)), $TS3->formatPlayersArray($rawData));
?>

You can download the full package from here; Download Here

About Steve

Web Developer, IT enthusiast & PC Gamer.
This entry was posted in Code, PHP and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>