score = $score; $this->ping = $ping; $name=trim(trim($rawname), "\""); $this->rawname=$name; $this->name=removecolorcodes($name); $this->cname=translatecolorcodes($name); } } // // CoDQuery class // class CoDQuery { // Variables for normal use var $ip; //IP adress var $port="28960"; //UDP port var $name="TIMEOUT"; //Server name color codes removed var $cname="TIMEOUT"; //Server name color coded html var $map=""; //Current map var $gametype; //Gametype var $numplayers=0; //Number of players var $maxplayers=0; //Maximum players var $privateplayers=0; //Private players var $password=0; //Password needed to join? var $pure=0; //Pure server? var $modified=0; //Mod:ed server? var $friendlyfire=0; //Friendly fire? var $killcam=0; //Killcam? var $maxping=0; //Max ping var $minping=0; //Min ping var $maxrate=0; //Max rate var $version=0; //Version var $punkbuster=0; //Punkbuster? var $players = array(); //Array with players // Variables for advanced users var $rawgetinfo; //Raw information from the getinfo command var $rawgetstatus; //Raw information from the getstatus command var $info = array(); //Array with values from $rawgetinfo var $status = array(); //Array with values from $rawgetstatus var $rawplayers = array(); //Array with raw player information // Constructor function CoDQuery($ip,$port) { // Init some variables $this->ip = gethostbyname($ip); //Resolv IP adress if($port) //If $port is set to a value... $this->port = $port; //...use it. // Open socket $fp = fsockopen("udp://" . $this->ip, $this->port, $errno, $errstr, 1); if (!$fp) { // OK? $this->name=$this->cname="ERROR!!"; //If error, set servername to ERROR!! } else { // Set a low timeout to avoid longs waits if a player disconnects while we are still getting info stream_set_timeout($fp, 2); // Try one of the following instead of the line above if you use PHP prior to 4.3.0 // socket_set_timeout($fp, 2); // set_socket_timeout($fp, 2); fputs($fp, "\xff\xff\xff\xffgetinfo\n"); // Send getinfo command usleep(100000); // Wait 0.1 seconds between the commands. fputs($fp, "\xff\xff\xff\xffgetstatus\n"); // Send getstatus command // Get response $dummy=fgets($fp); //Skip first line // Get line with serverinfo $this->rawgetinfo = preg_replace("/\xff\xff\xff\xffstatusResponse/", "", fgets($fp)); //Save the raw server info // Split it into an array $arr=explode("\\",$this->rawgetinfo); // Skip first element which will be empty and then loop trough the rest for ($i=1;$iinfo[$arr[$i++]] = $arr[$i]; //Tidy up the data into a neat array } // Get line with serverstatus $this->rawgetstatus=fgets($fp); // Split it into an array $arr=explode("\\",$this->rawgetstatus); // Skip first element which will be empty and then loop trough the rest for ($i=1;$istatus[$arr[$i++]]=$arr[$i]; //Tidy up the data into a neat array } } // Assign the variables that is normally used if(isset($this->info[hostname])) $this->name = removecolorcodes($this->info[hostname]); if(isset($this->info[hostname])) $this->cname = translatecolorcodes($this->info[hostname]); if(isset($this->info[mapname])) $this->map = $this->info[mapname]; if(isset($this->info[gametype])) $this->gametype = $this->info[gametype]; if(isset($this->info[clients])) $this->numplayers = $this->info[clients]; if(isset($this->info[sv_maxclients])) $this->maxplayers = $this->info[sv_maxclients]; if(isset($this->status[sv_privateClients])) $this->privateplayers=$this->status[sv_privateClients]; if(isset($this->status[pswrd])) $this->password = $this->status[pswrd]; if(isset($this->info[pure])) $this->pure = $this->info[pure]; if(isset($this->info[mod])) $this->modified = $this->info[mod]; if(isset($this->info[ff])) $this->friendlyfire = $this->info[ff]; if(isset($this->status[scr_killcam])) $this->killcam = $this->status[scr_killcam]; if(isset($this->info[maxPing])) $this->maxping = $this->info[maxPing]; if(isset($this->status[sv_minPing])) $this->minping = $this->status[sv_minPing]; if(isset($this->status[sv_maxRate])) $this->maxrate = $this->status[sv_maxRate]; if(isset($this->status[shortversion])) $this->version = $this->status[shortversion]; if(isset($this->info[pb])) $this->punkbuster = $this->info[pb]; // Loop through the players for ($i=0;$i<$this->numplayers;$i++) { // Exit if we get a timeout // This can happen if a player disconnects before we have recieved all the player data if (!($this->rawplayers[$i]=fgets($fp))) { $this->numplayers = $this->info[clients] = $i; // Change to numplayers to actual value break; } // Split the raw data into an array $player=explode(" ",$this->rawplayers[$i],3); // Use the an array of instances of the CoDPlayer class (score, ping, name) $this->players[$i] = new CoDPlayer($player[0], $player[1], $player[2]); } // Close the socket fclose($fp); } } // Function to remove the color codes function removecolorcodes($string) { return preg_replace("/\^[0-9]/", "", htmlspecialchars($string)); } // Function to translate the color codes into html tags function translatecolorcodes($string) { $pattern[0]="/\^0/"; $pattern[1]="/\^1/"; $pattern[2]="/\^2/"; $pattern[3]="/\^3/"; $pattern[4]="/\^4/"; $pattern[5]="/\^5/"; $pattern[6]="/\^6/"; $pattern[7]="/\^7/"; $pattern[8]="/\^8/"; $pattern[9]="/\^9/"; $replacement[0]=""; $replacement[1]=""; $replacement[2]=""; $replacement[3]=""; $replacement[4]=""; $replacement[5]=""; $replacement[6]=""; $replacement[7]=""; $replacement[8]=""; $replacement[9]=""; $string = preg_replace($pattern, $replacement, htmlspecialchars($string)); return "" . $string . ""; } ?>