Location: PHPKode > scripts > Team Speak Admin Class > team-speak-admin-class/tsac.class.php
<?php

	/**
	* Page-level DocBlock
	* @package Classes
	*/
	/**
	* Class-level DocBlock
	* @package tsac
	* @author Nils Hulsch
	* @copyright (c) Nils Hulsch
	* @version 1.1
	*/
	
	class tsac {
		
		/**
		* @var mixed 
		*/
		var $socket;
		
		/**
		* @var string $error
		*/
		var $error;
		
		/**
		* Connect and verify TS-Server
		* @param string $ip IP of the TS-Server
		* @param integer $port Port of TS-Server
		* @param string $user Superadmin user
		* @param string $password Password of Superadmin
		* @return bool TRUE on established and verified connection, FALSE on failures.
		*/
		function connect($ip,$port,$user,$password) {
			// socket creation
			$this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
			if ($this->socket < 0) {
				$this->error="socket_create() failed: reason: ".socket_strerror($this->socket);
				return false;
			}
			
			// connect
			$result = @socket_connect($this->socket, $ip, $port);
			if ($result < 0) {
				$this->error="@socket_connect() failed: reason: ($result) ".socket_strerror($result);
				return false;
			}
			
			// Verifying server
			$out=@socket_read($this->socket, 2048);
			if($out!="[TS]\r\n") {
				$this->error="This is not a TS Server!";
				$this->disconnect();
				return false;
			}
			
			// login
			$in="slogin $user $password\r\n";
			@socket_write($this->socket, $in, strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="Login failed!";
				$this->disconnect();
				return false;
			}
			return true;
		} //connect
		
		/**
		* Close the socket
		* @return bool TRUE on success, FALSE on failure
		*/
		function disconnect() {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			
			$in="quit\r\n";
			@socket_write($this->socket,$in,strlen($in));
			@socket_close($this->socket);
			return true;
		} //disconnect
		
		
		/**
		* Get serverlist from TS-Server
		*
		* parsed list:<br>
		* array[i]['id'] returns the serverid<br>
		* array[i]['port'] returns the port of the server<br>
		* array[i]['name'] returns the servername<br>
		* array[i]['online'] returns 1 if the server is started and 0 if the server is stopped
		* @param integer $raw raw (1) or parsed (0) list (default=0)
		* @return mixed raw serverlist on success (raw=1), parsed serverlist as array (raw=0), FALSE on failure
		*/
		function getserverlist($raw=0) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			
			$in="dbserverlist\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,10000);
			if($raw==1) return $out;
			
			if($raw==0) {
				$i=0;
				while(preg_match("!(\d+)\s+(\d+)\s+\"(.+)\"\s+(\d+)!i",$out,$match)) {
					$array[$i]['id']=$match[1];
					$array[$i]['port']=$match[2];
					$array[$i]['name']=$match[3];
					$array[$i]['online']=$match[4];
					$out=str_replace($match[0],"",$out);
					$i++;
				}
				return $array;
			}
		} //getserverlist
		
		/**
		* Get playerlist from a specified port
		*
		* parsed list:<br>
		* array[i]['player_id'] returns the playerid<br>
		* array[i]['channel_id'] returns the channelid of the chanenl where the player is in<br>
		* array[i]['packets_sent'] returns the amount of packets the client sent to the server<br>
		* array[i]['bytes_sent'] returns the amount of bytes the client sent to the server<br>
		* array[i]['packets_received'] returns the amount of packets the client received from the server<br>
		* array[i]['bytes_received'] returns the amount of bytes the client received from the server<br>
		* array[i]['packets_lost'] returns the amount of losts packets<br>
		* array[i]['ping'] returns the ping of the client to the server in milliseconds<br>
		* array[i]['logintime'] returns the time in seconds since the user is connected to the server<br>
		* array[i]['idletime'] returns the time in seconds the user is idling<br>
		* array[i]['cprivs'] returns the current channel privileges. (CA=1,O=2,V=4,AO=8,AV=16)<br>
		* array[i]['pprivs'] returns the current player privileges. (SA=1,Allow Registration=2,Registred=4,Internal Use=8, Stickey=16)<br>
		* array[i]['pflags'] returns the current player flags. (Channel Commander=1,Voice Request=2,Doesnt accept whisper=4,Away=8,Microphone muted=16,Sound muted=32,Recording=64)<br>
		* array[i]['ip'] returns the IP of the user<br>
		* array[i]['nick'] returns the nick of the user<br>
		* array[i]['loginname'] returns the loginname of the user
		* @param integer $port Port of the Server
		* @param integer $raw raw (1) or parsed (0) list (default=0)
		* @return mixed raw playerlist on success (raw=1), parsed serverlist as array (raw=0), FALSE on failure
		*/
		function getplayerlist($port,$raw=0) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			
			$in="sel $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="No such server, or server is offline";
				return false;
			}
			
			$in="pl\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,100000);
			if($raw==1) return $out;
			
			if($raw==0) {
				$i=0;
				while(preg_match("!(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+\"(.+)\"\s+\"(.+)\"\s+\"(.*)\"!i",$out,$match)) {
					$array[$i]['player_id']=$match[1];
					$array[$i]['channel_id']=$match[2];
					$array[$i]['packets_sent']=$match[3];
					$array[$i]['bytes_sent']=$match[4];
					$array[$i]['packets_received']=$match[5];
					$array[$i]['bytes_received']=$match[6];
					$array[$i]['packets_lost']=$match[7];
					$array[$i]['ping']=$match[8];
					$array[$i]['logintime']=$match[9];
					$array[$i]['idletime']=$match[10];
					$array[$i]['cprivs']=$match[11];
					$array[$i]['pprivs']=$match[12];
					$array[$i]['pflags']=$match[13];
					$array[$i]['ip']=$match[14];
					$array[$i]['nick']=$match[15];
					$array[$i]['loginname']=$match[16];
					$out=str_replace($match[0],"",$out);
					$i++;
				}
				return $array;
			}
		} //getplayerlist
		
		/**
		* Get serverinformation from a specified port
		*
		* parsed list:<br>
		* array['server_id'] returns the serverid<br>
		* array['server_name'] returns the servername<br>
		* array['server_platform'] returns the serverplatform<br>
		* array['server_welcomemessage'] returns the welcomemessage<br>
		* array['server_webpost_linkurl'] returns the webpost linkurl<br>
		* array['server_webpost_posturl'] returns the webpost posturl<br>
		* array['server_password'] returns 1 if a password is set, and 0 is none is set<br>
		* array['server_clan_server'] returns 1 if the servermode is clanserver or 0 if its set to publicserver<br>
		* array['server_udpport'] returns the port of the server<br>
		* array['server_maxusers'] returns the amount of maximum users<br>
		* array['server_allow_codec_celp51'] returns -1 if celp51 codec is allowed and 0 if not<br>
		* array['server_allow_codec_celp63'] returns -1 if celp63 codec is allowed and 0 if not<br>
		* array['server_allow_codec_gsm148'] returns -1 if gsm148 codec is allowed and 0 if not<br>
		* array['server_allow_codec_gsm164'] returns -1 if gsm164 codec is allowed and 0 if not<br>
		* array['server_allow_codec_windowscelp52'] returns -1 if windowscelp52 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex2150'] returns -1 if speex2150 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex3950'] returns -1 if speex3950 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex5950'] returns -1 if speex5950 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex8000'] returns -1 if speex8000 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex11000'] returns -1 if speex11000 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex15000'] returns -1 if speex15000 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex18200'] returns -1 if speex18200 codec is allowed and 0 if not<br>
		* array['server_allow_codec_speex24600'] returns -1 if speex24600 codec is allowed and 0 if not<br>
		* array['server_packetssend'] returns the amount of packets the server sent<br>
		* array['server_bytessend'] returns the amount of bytes the server sent<br>
		* array['server_packetsreceived'] returns the amount of packets the server received<br>
		* array['server_bytesreceived'] returns the amount of packets the server received<br>
		* array['server_uptime'] returns the uptime in seconds<br>
		* array['server_currentusers'] returns the amount of current users on the server<br>
		* array['server_currentchannels'] returns the amount of current channels on the server<br>
		* array['server_bwinlastsec'] returns the amount of bytes received in the last second<br>
		* array['server_bwoutlastsec'] returns the amount of bytes sent in the last second<br>
		* array['server_bwinlastmin'] returns the amount of bytes received in the last minute<br>
		* array['server_bwoutlastmin'] returns the amount of bytes sent in the last minute<br>
		* @param integer $port Port of the server
		* @param integer $raw raw (1) or parsed (0) list (default=0)
		* @return mixed raw serverinfo on success (raw=1), parsed serverlist as array (raw=0), FALSE on failure
		*/
		function getserverinfo($port,$raw=0) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			
			$in="sel $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="No such server, or server is offline";
				return false;
			}
			
			$in="si\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,100000);
			if($raw==1) return $out;
			
			if($raw==0) {
				while(preg_match("!^(.+)=(.*)!i",$out,$match)) {
					$array[$match[1]]=trim($match[2]);
					$out=str_replace($match[0]."\n","",$out);
				}
				return $array;
			}
		} //getserverinfo
		
		
		/**
		* Get Serverid from Port
		* @param integer $port Port of the server
		* @return mixed ID of the server, FALSE on failure
		*/
		function port2serverid($port) {
			$serverlist=$this->getserverlist(1);
			if (!$serverlist) return false;
			
			if(!preg_match("!(\d+)\s+$port\s+\".*\"!i",$serverlist,$match)) {
				$this->error="No server found";
				return false;
			}
			return $match[1];
		} //port2serverid
		
		/**
		* Starts the specified server
		* @param integer $port Port of the server
		* @return bool TRUE on success, FALSE on failure
		*/
		function startserver($port) {
			$id=$this->port2serverid($port);
			if(!$id) return false;
			
			$in="serverstart ".$id."\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out=="OK\r\n") return true;
			
			$this->error="Server not started. Maybe not found or already started";
			return false;
		} //startserver
		
		/**
		* Stops the specified server
		* @param integer $port Port of the server
		* @return bool TRUE on success, FALSE on failure
		*/
		function stopserver($port) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			
			$in="sel $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			@socket_read($this->socket,2048);
			$in="serverstop\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if ($out=="OK\r\n") return true;

			$this->error="Server not stopped. Maybe already stopped or not found";
			@socket_read($this->socket,2048);
			return false;
		} //stopserver
		
		/**
		* Restarts the specified server
		* @param integer $port Port of the server
		* @return bool TRUE on success, FALSE on failure
		*/
		function restartserver($port) {
			$this->stopserver($port);
			if(!$this->startserver($port)) return false;
			return true;
		} //restartserver
		
		/** 
		* Sets a servers password
		* @param integer $port Port of the server
		* @param string $password New password of the server
		* @return bool TRUE on success, FALSE on failure
		*/
		function setpassword($port,$password) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			$in="sel $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			@socket_read($this->socket,2048);
			$in="serverset server_password $password\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="Password wasn't set";
				return false;
			}
			return true;
		} //setpassword
		
		/**
		* Sets the new maximum userlimit on the specified server
		* @param integer $port Port of the server
		* @param integer $maxusers New maximum userlimit
		* @return bool TRUE on success, FALSE on failure
		*/
		function setmaxusers($port,$maxusers) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			$in="sel $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			@socket_read($this->socket,2048);
			$in="serverset server_maxusers $maxusers\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="Max users wasn't set";
				return false;
			}
			return true;
		} //setmaxusers
		
		/**
		* Sets the servers name
		* @param integer $port Port of the server
		* @param string $servername New name of the server (Up to 29 characters)
		* @return bool TRUE on sucess, FALSE on failure
		*/
		function setservername($port,$servername) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			$in="sel $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			@socket_read($this->socket,2048);
			$in="serverset server_name $servername\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="Servername wasn't set";
				return false;
			}
			return true;
		} //setservername
		
		/**
		* Adds a new user to the server
		* @param integer $port Port of the server
		* @param string $name Loginname of the user
		* @param string $password Password of the user
		* @param integer $admin 1 = yes, 0 = no (default)
		* @return bool TRUE on success, FALSE on failure
		*/
		function adduser($port,$name,$password,$admin=0) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			$in="sel $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			@socket_read($this->socket,2048);
			$in="dbuseradd $name $password $password $admin\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="User wasn't created";
				return false;
			}
			return true;
		} //adduser
		
		/**
		* Adds a new server
		* @param integer $port Port of the new server
		* @param integer $maxusers The maximum users allowed on this server
		* @param string $adminname The Username of the admin User.
		* @param string $adminpw The password of the 'admin' User
		* @param string $password The password of the server. (optional)
		* @param string $servername The name of the server (optional). Can be up to 29 characters
		* @return bool TRUE on success, FALSE on failure
		*/
		function addserver($port,$maxusers,$adminname,$adminpw,$password="",$servername="TeamSpeak Server") {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			
			$in="serveradd $port\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out!="OK\r\n") {
				$this->error="Server couldn't be added. Maybe port already used";
				return false;
			}
			
			if($password!="") {
				if(!$this->setpassword($port,$password)) return false;
			}
			
			if(!$this->setmaxusers($port,$maxusers)) return false;
			
			if(!$this->setservername($port,$servername)) return false;
			
			if(!$this->adduser($port,$adminname,$adminpw,1)) return false;
			
			return true;
		} //addserver
		
		/**
		* Deletes the specified server
		* @param integer $port Port of the server
		* @return bool TRUE on success, FALSE on failure
		*/
		function deleteserver($port) {
			$this->stopserver($port);
			
			$id=$this->port2serverid($port);
			if(!$id) return false;
			
			$in="serverdel ".$id."\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,2048);
			if($out=="OK\r\n") return true;
			
			$this->error=$out;
			return false;
		} // deleteserver
		
		/**
		* Get global informations from the server
		*
		* parsed list:<br>
		* array['total_server_uptime'] returns the serveruptime<br>
		* array['total_server_version'] returns the serverversion<br>
		* array['total_server_platform'] returns the serverplatform<br>
		* array['total_servers'] returns the amount of virtual servers<br>
		* array['total_users_online'] return the current amount of online users<br>
		* array['total_users_maximal'] returns the maximum amount of users on all servers<br>
		* array['total_channels'] returns the current amount of channels on all servers<br>
		* array['total_bytesreceived'] returns the amount of bytes the server received<br>
		* array['total_bytessend'] returns the amount of bytes the server sent<br>
		* array['total_packetssend'] returns the amount of packets the server sent<br>
		* array['total_packetsreceived'] returns the amount of packets the server received<br>
		* array['total_bwoutlastmin'] returns the amount of bytes sent in the last minute<br>
		* array['total_bwoutlastsec'] returns the amount of bytes sent in the last second<br>
		* array['total_bwinlastmin'] returns the amount of bytes received in the last minute<br>
		* array['total_bwinlastsec'] returns the amount of bytes received in the last second<br>
		* array['isp_ispname'] returns the name of the ISP set in the config<br>
		* array['isp_linkurl'] returns the url of the ISP<br>
		* array['isp_adminemail'] returns the admin contact of the ISP<br>
		* array['isp_countrynumber'] returns the countrynumber of the ISP<br>
		* @param integer $raw raw (1) or parsed (0) list (default=0)
		* @return mixed raw globalinfo on success (raw=1), parsed serverlist as array (raw=0), FALSE on failure
		*/
		function getglobalinfo($raw=0) {
			if ($this->socket < 0) {
				$this->error="Not connected";
				return false;
			}
			
			$in="gi\r\n";
			@socket_write($this->socket,$in,strlen($in));
			$out=@socket_read($this->socket,100000);
			if($raw==1) return $out;
			
			if($raw==0) {
				while(preg_match("!^(.+)=(.*)!i",$out,$match)) {
					$array[$match[1]]=trim($match[2]);
					$out=str_replace($match[0]."\n","",$out);
				}
				return $array;
			}
		} //getglobalinfo
		
	} // tsac
	
?>
Return current item: Team Speak Admin Class