Advertisement
holblin

PHP Quake3 Rcon

Jun 25th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. <?php
  2.  
  3. function urt_query($ip, $port, $query, $answer = true) {
  4.     $fp = fsockopen("udp://$ip",$port, $errno, $errstr, 2);
  5.     stream_set_blocking( $fp , 0);
  6.     socket_set_timeout($fp,2);
  7.     stream_set_timeout($fp,2);
  8.  
  9.     if (!$fp)   {
  10.         echo "$errstr ($errno)<br/>\n";
  11.     } else {
  12.         $query = chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff) . $query;
  13.         $tmp = fwrite($fp,$query);
  14.     }
  15.     if ( !$answer)
  16.         return;
  17.        
  18.     $data = '';
  19.     $tmp1 = microtime(true);
  20.     do {
  21.         $d = fread ($fp, 2048);
  22.         $data .= $d;
  23.        
  24.         if (strlen($d) == 0)
  25.             usleep( 50 );
  26.     } while ( strlen($d) == 0  &&  (microtime(true) - $tmp1 < 3 ));
  27.    
  28.     if (microtime(true) - $tmp1 > 3)
  29.         return false;
  30.    
  31.    
  32.     $nb_zero = 0;
  33.     do {
  34.         $d = fread ($fp, 2048);
  35.         $data .= $d;
  36.        
  37.         if (strlen($d) == 0) {
  38.             $nb_zero++;
  39.             usleep(50);
  40.         }else
  41.             $nb_zero = 0;
  42.     } while ( $nb_zero < 5 );
  43.    
  44.     fclose ($fp);
  45.     return $data;
  46. }
  47.  
  48. function status ($ip, $port ) {
  49.     $data = urt_query($ip, $port, 'getstatus' , true );
  50.     return explode_backslash($data);
  51. }
  52.  
  53. function rcon ($ip, $port, $rcon_pass, $command, $answer = true) {
  54.     $data = urt_query($ip, $port, 'rcon "' . $rcon_pass . '" ' . $command , $answer );
  55.     if ( substr($data,0 , -1) == chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff) . "print\nBad rconpassword." ){ session_destroy(); echo 'Bad rconpassword'; die(); }
  56.    
  57.     if (! $answer)
  58.         return $data;
  59.    
  60.     $data = preg_replace ("/....print\n/", "", $data);
  61.     return $data;
  62. }
  63.  
  64. function rcon_status ($ip, $port, $rcon_pass) {
  65.     $data = rcon ($ip, $port, $rcon_pass, "status");
  66.     $data = explode ("\n", $data);
  67.  
  68.     # ok, let's deal with the following :
  69.     #
  70.     # map: q3wcp9
  71.     # num score ping name            lastmsg address               qport rate
  72.     # --- ----- ---- --------------- ------- --------------------- ----- -----
  73.     #   1    19   33 l33t^n1ck       33 62.212.106.216:27960   5294 25000
  74.  
  75.     $map = array_shift($data); // 1st line : map q3wcp9
  76.     $map = preg_replace('/^map: /','',$map);
  77.     array_shift($data); // 2nd line : col headers
  78.     array_shift($data); // 3rd line : -- ------ ----
  79.     array_pop($data);
  80.     array_pop($data); // two empty lines at the end, go figure.
  81.  
  82.     $result = array();
  83.     foreach ($data as $line) {
  84.         $player = $line;
  85.         // preg_match_all("/^\s*(\d+)\s*(\d+)\s*(\d+)(.*?)(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $player, $out); // weeeeeeeeee \o/
  86.         preg_match_all("/^\s*(\d+)\s*(\-?\d+)\s*(\d+)\s*(.*?)\s*(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $player, $out);
  87.         $num = $out[1][0];
  88.         $score = $out[2][0];
  89.         $ping = $out[3][0];
  90.         $name = trim($out[4][0]);
  91.         $lastmsg = $out[5][0];
  92.         $address = $out[6][0];
  93.         if ($address != 'bot')
  94.             $address = preg_replace ("/:.*$/", "", $address);
  95.         $qport = $out[7][0];
  96.         $rate = $out[8][0];
  97.        
  98.         $result[$num] = array(
  99.                         'num'=> $num,
  100.                         'score'=> $score,
  101.                         'ping'=> $ping,
  102.                         'name'=> $name,
  103.                         'lastmsg'=> $lastmsg,
  104.                         'address'=> $address,
  105.                         'qport'=> $qport,
  106.                         'rate'=> $rate,);
  107.     }
  108.     return array( 'map' => $map , 'data' => $result);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement