Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function urt_query($ip, $port, $query, $answer = true) {
- $fp = fsockopen("udp://$ip",$port, $errno, $errstr, 2);
- stream_set_blocking( $fp , 0);
- socket_set_timeout($fp,2);
- stream_set_timeout($fp,2);
- if (!$fp) {
- echo "$errstr ($errno)<br/>\n";
- } else {
- $query = chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff) . $query;
- $tmp = fwrite($fp,$query);
- }
- if ( !$answer)
- return;
- $data = '';
- $tmp1 = microtime(true);
- do {
- $d = fread ($fp, 2048);
- $data .= $d;
- if (strlen($d) == 0)
- usleep( 50 );
- } while ( strlen($d) == 0 && (microtime(true) - $tmp1 < 3 ));
- if (microtime(true) - $tmp1 > 3)
- return false;
- $nb_zero = 0;
- do {
- $d = fread ($fp, 2048);
- $data .= $d;
- if (strlen($d) == 0) {
- $nb_zero++;
- usleep(50);
- }else
- $nb_zero = 0;
- } while ( $nb_zero < 5 );
- fclose ($fp);
- return $data;
- }
- function status ($ip, $port ) {
- $data = urt_query($ip, $port, 'getstatus' , true );
- return explode_backslash($data);
- }
- function rcon ($ip, $port, $rcon_pass, $command, $answer = true) {
- $data = urt_query($ip, $port, 'rcon "' . $rcon_pass . '" ' . $command , $answer );
- if ( substr($data,0 , -1) == chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff) . "print\nBad rconpassword." ){ session_destroy(); echo 'Bad rconpassword'; die(); }
- if (! $answer)
- return $data;
- $data = preg_replace ("/....print\n/", "", $data);
- return $data;
- }
- function rcon_status ($ip, $port, $rcon_pass) {
- $data = rcon ($ip, $port, $rcon_pass, "status");
- $data = explode ("\n", $data);
- # ok, let's deal with the following :
- #
- # map: q3wcp9
- # num score ping name lastmsg address qport rate
- # --- ----- ---- --------------- ------- --------------------- ----- -----
- # 1 19 33 l33t^n1ck 33 62.212.106.216:27960 5294 25000
- $map = array_shift($data); // 1st line : map q3wcp9
- $map = preg_replace('/^map: /','',$map);
- array_shift($data); // 2nd line : col headers
- array_shift($data); // 3rd line : -- ------ ----
- array_pop($data);
- array_pop($data); // two empty lines at the end, go figure.
- $result = array();
- foreach ($data as $line) {
- $player = $line;
- // preg_match_all("/^\s*(\d+)\s*(\d+)\s*(\d+)(.*?)(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $player, $out); // weeeeeeeeee \o/
- preg_match_all("/^\s*(\d+)\s*(\-?\d+)\s*(\d+)\s*(.*?)\s*(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $player, $out);
- $num = $out[1][0];
- $score = $out[2][0];
- $ping = $out[3][0];
- $name = trim($out[4][0]);
- $lastmsg = $out[5][0];
- $address = $out[6][0];
- if ($address != 'bot')
- $address = preg_replace ("/:.*$/", "", $address);
- $qport = $out[7][0];
- $rate = $out[8][0];
- $result[$num] = array(
- 'num'=> $num,
- 'score'=> $score,
- 'ping'=> $ping,
- 'name'=> $name,
- 'lastmsg'=> $lastmsg,
- 'address'=> $address,
- 'qport'=> $qport,
- 'rate'=> $rate,);
- }
- return array( 'map' => $map , 'data' => $result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement