/** * http://developer.valvesoftware.com/wiki/Source_RCON_Protocol * * @param string $ip * @param string $port * @param string $rcon * @param string $string1 * @param string $string2 * @return array $data */ function Source_Rcon($ip, $port, $rcon, $string1, $string2 = NULL) { $reqid = mt_rand(0, 255); $reqtype = 3; # add the zero /* $rcon .= "\x00"; $string1 .= "\x00"; $string2 .= "\x00"; */ if (!($server_connection = fsockopen("udp://" . $ip, $port))) { return false; } socket_set_blocking($server_connection, 1); # Time out after 5 seconds socket_set_timeout($server_connection, 5); # First we need to auth # Auth packet creation $command = pack('VV', $reqid, $reqtype) . $rcon . $string2; $cmdlen = strlen($command); $command = pack('V', $cmdlen) . $command; # Request Auth fwrite($server_connection, $command, strlen($command)); # Get the thingy packet that currently has no use $data = Get_A_Packet($server_connection); # Get auth packet $data = Get_A_Packet($server_connection); $datastart = 0; $recsize = GetInt32($data, $datastart); $recid = GetInt32($data, $datastart); $rectype = GetInt32($data, $datastart); $recstring1 = GetString($data, $datastart); $recstring2 = GetString($data, $datastart); # If we don't get what we expect, we must have failed? if ($rectype != 2) { fclose($server_connection); return false; } # If the packet ID's dont match, auth failed if ($recid != $reqid) { fclose($server_connection); return false; } # Prepare new command $reqid = mt_rand(0, 255); $reqtype = 2; $command = pack('VV', $reqid, $reqtype) . $string1 . $string2; $cmdlen = strlen($command); $command = pack('V', $cmdlen) . $command; # Send new command fwrite($server_connection, $command, strlen($command)); $data = ''; # i will increment every loop # t will only increment when a packet was received. # should something break, it should.. in theory, kill itself # but hopefully something will have killed it earlier $i = 0; $t = 1; #while ($i <= $t) #{ #$i++; $packet = ''; #if ($packet = Get_A_Packet($server_connection)) $packet = Get_A_Packet($server_connection); #{ # $t++; #} $datastart = 0; $recsize = GetInt32($packet, $datastart); $recid = GetInt32($packet, $datastart); if ($recid == $reqid) { # I wonder how long it will be till this gets triggered # Maybe? Never? Fear the bugs in the protocol! # Header packet $rectype = GetInt32($packet, $datastart); $data .= GetString($packet, $datastart); $recstring2 = GetString($packet, $datastart); } else { $data .= $packet; } #} # Make sure socket is closed fclose($server_connection); return $data; }
/** * get 32 float * @param string $data * @param int $datastart */ function GetFloat32($data, &$datastart) { $decnumber = GetInt32($data, $datastart); $binnumber = base_convert($decnumber, 10, 2); while (strlen($binnumber) < 32) { $binnumber = '0' . $binnumber; } $exp = abs(base_convert(substr($binnumber, 1, 8), 2, 10)) - 127; if (substr($binnumber, 0, 1) == 1) { $exp = 0 - $exp; } $mantissa = 1; $mantadd = 0.5; for ($counter = 9; $counter < 32; $counter++) { if (substr($binnumber, $counter, 1) == 1) { $mantissa += $mantadd; } $mantadd = $mantadd / 2; } $temp = round(pow(2, $exp) * $mantissa); return $temp; }