1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
| <?php
class Rcon
{
var $challenge_number;
var $connected;
var $server_ip ;
var $server_password;
var $server_port;
var $socket;
//Constructor
function Rcon()
{
$this->challenge_number = 0;
$this->connected = true;
$this->server_password = "";
$this->server_password = 27015;
$this->server_password = "";
}
//Open socket to gameserver
function Connect($server_ip, $server_port, $server_password = "")
{
//store server data
$this->server_ip = gethostbyname($server_ip);
$this->server_port = $server_port;
$this->server_password = $server_password;
//open connection to gameserver
$fp = fsockopen($this->server_ip, $this->server_port, $errno, $errstr, 5);
var_dump($fp);
if($fp)
$this->connected = true;
else
{
$this->connected = false;
return false;
}
echo $fp;
//store socket
$this->socket = $fp;
//return success
return true;
} //function Connect($server_ip, $server_port, $server_password = "")
//Close socket to gameserver
function Disconnect()
{
//close socket
fclose($this->socket);
$connected = false;
} //function Disconnect()
//Is there an open connection
function IsConnected()
{
return $this->connected;
} //function IsConnected()
//Get detailed player info via rcon
function ServerInfo()
{
//If there is no open connection return false
if(!$this->connected)
return $this->connected;
//get server information
$status = $this->RconCommand("status");
echo $status;
//If there is no open connection return false
//If there is bad rcon password return "Bad rcon_password."
if(!$status || trim($status) == "Bad rcon_password.")
return $status;
//format global server info
$line = explode("\n", $status);
$map = substr($line[3], strpos($line[3], ":") + 1);
$players = trim(substr($line[4], strpos($line[4], ":") + 1));
$active = explode(" ", $players);
$result["ip"] = trim(substr($line[2], strpos($line[2], ":") + 1));
$result["name"] = trim(substr($line[0], strpos($line[0], ":") + 1));
$result["map"] = trim(substr($map, 0, strpos($map, "at:")));
$result["mod"] = "Counterstrike " . trim(substr($line[1], strpos($line[1], ":") + 1));
$result["game"] = "Halflife";
$result["activeplayers"] = $active[0];
$result["maxplayers"] = substr($active[2], 1);
//format player info
for($i = 1; $i <= $result["activeplayers"]; $i++)
{
//get possible player line
$tmp = $line[$i + 6];
//break if no more players are left
if(substr_count($tmp, "#") <= 0)
break;
//name
$begin = strpos($tmp, "\"") + 1;
$end = strrpos($tmp, "\"");
$result[$i]["name"] = substr($tmp, $begin, $end - $begin);
$tmp = trim(substr($tmp, $end + 1));
//ID
$end = strpos($tmp, " ");
$result[$i]["id"] = substr($tmp, 0, $end);
$tmp = trim(substr($tmp, $end));
//WonID
$end = strpos($tmp, " ");
$result[$i]["wonid"] = substr($tmp, 0, $end);
$tmp = trim(substr($tmp, $end));
//Frag
$end = strpos($tmp, " ");
$result[$i]["frag"] = substr($tmp, 0, $end);
$tmp = trim(substr($tmp, $end));
//Time
$end = strpos($tmp, " ");
$result[$i]["time"] = substr($tmp, 0, $end);
$tmp = trim(substr($tmp, $end));
//Ping
$end = strpos($tmp, " ");
$result[$i]["ping"] = substr($tmp, 0, $end);
$tmp = trim(substr($tmp, $end));
//Loss
$tmp = trim(substr($tmp, $end));
//Adress
$result[$i]["adress"] = $tmp;
} //for($i = 1; $i < $result["activeplayers"]; $i++)
//return formatted result
return $result;
} //function ServerInfo()
//Get server info via info protocoll
function Info()
{
//If there is no open connection return false
if(!$this->connected)
return $this->connected;
//send info command
$command = "\xff\xff\xff\xffinfo\x00";
$buffer = $this->Communicate($command);
//If no connection is open
if(trim($buffer) == "")
{
$this->connected = false;
return false;
}
//build info array
$buffer = explode("\x00", $buffer);
$result["ip"] = substr($buffer[0], 5);
$result["name"] = $buffer[1];
$result["map"] = $buffer[2];
$result["mod"] = $buffer[3];
$result["game"] = $buffer[4];
$result["activeplayers"] = (strlen($buffer[5]) > 1)?ord($buffer[5][0]):"0";
$result["maxplayers"] = (strlen($buffer[5]) > 1)?ord($buffer[5][1]):"0";
//return formatted result
return $result;
} //function Info()
//Get players via info protocoll
function Players()
{
//If there is no open connection return false
if(!$this->connected)
return $this->connected;
//send players command
$command = "\xff\xff\xff\xffplayers\x00";
$buffer = $this->Communicate($command);
//If no connection is open
if(trim($buffer) == "")
{
$this->connected = false;
return false;
}
//get number of online players
$buffer = substr($buffer, 1);
//build players array
for($i = 1; strlen($buffer) > 0; $i++)
{
//playername
$tmp = strpos($buffer, "\x00");
$result[$i]["name"] = substr($buffer, 1, $tmp);
//frag count
$result[$i]["frag"] = ord($buffer[$tmp + 1]) +
(ord($buffer[$tmp + 2]) << 8) +
(ord($buffer[$tmp + 3]) << 16) +
(ord($buffer[$tmp + 4]) << 24);
//online time
$tmptime = @unpack('ftime', substr($buffer, $tmp + 5, 4));
$result[$i]["time"] = date('i:s', round($tmptime['time'], 0) + 82800);
$buffer = substr($buffer, $tmp + 9);
} //for($i = 1; $i <= $count; $i++)
//return formatted result
return $result;
} //function Players()
//Get server rules via info protocoll
function ServerRules()
{
//If there is no open connection return false
if(!$this->connected)
return $this->connected;
//build info command
$command = "\xff\xff\xff\xffrules\x00";
$buffer = $this->Communicate($command);
//If no connection is open
if(trim($buffer) == "")
{
$this->connected = false;
return false;
}
//seperate rules
$buffer = substr($buffer, 2);
$buffer = explode("\x00", $buffer);
$buffer_count = floor(sizeof($buffer) / 2);
//build rules array
for($i = 0; $i < $buffer_count; $i++)
{
$result[$buffer[2 * $i]] = $buffer[2 * $i + 1];
}
//sort rules
ksort($result);
//return formatted result
return $result;
} //function ServerRules()
//Execute rcon command on open socket $fp
function RconCommand($command, $pagenumber = 0, $single = true)
{
//If there is no open connection return false
if(!$this->connected)
return $this->connected;
//get challenge number
if($this->challenge_number == "")
{
//send request of challenge number
$challenge = "\xff\xff\xff\xffchallenge rcon\n";
$buffer = $this->Communicate($challenge);
//If no connection is open
if(trim($buffer) == "")
{
$this->connected = false;
return false;
}
//get challenge number
$buffer = explode(" ", $buffer);
$this->challenge_number = trim($buffer[2]);
}
//build command
$command = "\xff\xff\xff\xffrcon $this->challenge_number \"$this->server_password\" $command\n";
//get specified page
$result = "";
$buffer = "";
while($pagenumber >= 0)
{
//send rcon command
$buffer .= $this->Communicate($command);
//get only one package
if($single == true)
$result = $buffer;
//get more then one package and put them together
else
$result .= $buffer;
//clear command for higher iterations
$command = "";
$pagenumber--;
} //while($pagenumber >= 0)
//return unformatted result
return trim($result);
} //function RconCommand ($command)
//Communication between PHPrcon and the Gameserver
function Communicate($command)
{
//If there is no open connection return false
if(!$this->connected)
return $this->connected;
//write command on socket
if($command != "")
fputs($this->socket, $command, strlen($command));
//get results from server
$buffer = @fread ($this->socket, 1);
$status = @socket_get_status($this->socket);
$buffer .= @fread($this->socket, $status["unread_bytes"]);
//If there is another package waiting
if(substr($buffer, 0, 4) == "\xfe\xff\xff\xff")
{
//get results from server
$buffer2 = @fread ($this->socket, 1);
$status = @socket_get_status($this->socket);
$buffer2 .= @fread($this->socket, $status["unread_bytes"]);
//If the second one came first
if(strlen($buffer) > strlen($buffer2))
$buffer = substr($buffer, 14) . substr($buffer2, 9);
else
$buffer = substr($buffer2, 14) . substr($buffer, 9);
}
//In case there is only one package
else
$buffer = substr($buffer, 5);
//return unformatted result
return $buffer;
} //function Communicate($buffer)
}
?> |
Partager