27.10.2010, 03:09
PHP код:
<?
$result=true;
$ip='*.*.*.*';
$port='7777';
function monitor_samp($ip, $port, $request) {
$fp = @fsockopen("udp://$ip", $port, $errno, $errstr, 1);
if (!$fp) return FALSE;
stream_set_timeout($fp, 1, 0);
stream_set_blocking($fp, true);
$ip_array = explode(".", $ip);
$challenge = 'SAMP';
$challenge .= chr($ip_array[0]).chr($ip_array[1]).chr($ip_array[2]).chr($ip_array[3]);
$challenge .= chr($port & "\xFF");
$challenge .= chr($port >> 8 & "\xFF");
if ($request == "info") $challenge .= "i";
if ($request == "settings") $challenge .= "r";
if ($request == "players") $challenge .= "d";
fwrite($fp, $challenge);
$buffer = fread($fp, 4096);
fclose($fp);
if (!trim($buffer)) return FALSE;
if (!substr($buffer, 0, 4) == "SAMP") return FALSE;
$buffer = substr($buffer, 11);
if ($request == "info") {
$data['password'] = ord(substr($buffer, 0, 1));
$data['players'] = (ord(substr($buffer, 2, 1))==0) ? ord(substr($buffer, 1, 2)) : 256+ord(substr($buffer, 1, 2));
$data['maxplayers'] = (ord(substr($buffer, 4, 1))==0) ? ord(substr($buffer, 3, 2)) : 256+ord(substr($buffer, 3, 2));
$buffer = substr($buffer, 5);
$data_length = ord(substr($buffer, 0, 4));
$data['name'] = substr($buffer, 4, $data_length);
$buffer = substr($buffer, 4 + $data_length);
$data_length = ord(substr($buffer, 0, 4));
$data['gametext'] = substr($buffer, 4, $data_length);
$buffer = substr($buffer, 4 + $data_length);
$data_length = ord(substr($buffer, 0, 4));
$data['map'] = substr($buffer, 4, $data_length);
$data['ip'] = $ip;
$data['port'] = $port;
return $data;
}
if ($request == "settings") {
$setting_count = ord(substr($buffer, 0, 2));
$buffer = substr($buffer, 2);
if ($setting_count > 128) return FALSE;
for ($i=0; $i<$setting_count; $i++) {
if (!$buffer) return FALSE;
$data_length = ord(substr($buffer, 0, 1));
$key = substr($buffer, 1, $data_length);
$buffer = substr($buffer, 1 + $data_length);
$data_length = ord(substr($buffer, 0, 1));
$value = substr($buffer, 1, $data_length);
$buffer = substr($buffer, 1 + $data_length);
$setting[$key] = $value;
}
return $setting;
}
if ($request == "players") {
$player_count = ord(substr($buffer, 0, 2));
$buffer = substr($buffer, 2);
if ($player_count > 128) return FALSE;
for ($i=1; $i<=$player_count; $i++) {
if (!$buffer) return FALSE;
$player[$i]['pid'] = ord(substr($buffer, 0, 1));
$buffer = substr($buffer, 1);
$data_length = ord(substr($buffer, 0, 1));
$player[$i]['name'] = substr($buffer, 1, $data_length);
$buffer = substr($buffer, 1 + $data_length);
list(,$player[$i]['score']) = unpack("s", substr($buffer, 0, 4));
$buffer = substr($buffer, 4);
$player[$i]['ping'] = ord(substr($buffer, 0, 4));
$buffer = substr($buffer, 4);
}
return $player;
}
}
if(!$g=monitor_samp($ip, $port, "players")) $result=false;
if($result){
$f=monitor_samp($ip, $port, "info");
print "<center>".'Игроков:'.$f['players'].'/'.$f['maxplayers']."<br><table border=1 cellspacing=0 cellpadding=0 style='padding: 0px; margin: 0px; width: 100%;'><tr><td><b>Ник</b></th><th><b>Уровень</b></td></tr>";
for($i=1; $i<501; $i++){
if(!$g[$i]) continue;
$name=$g[$i]["name"];
$score=$g[$i]["score"];
print "<tr><td>".$name."</td><td>".$score." уровень </td></tr>";
}
print '</table></center>';
}
?>