Page 1 of 1

Help need using fsockopen please

Posted: Wed Apr 14, 2010 10:48 am
by Billydozer
Hey Guys

I hope someone can help me with this, i found a script to allow me to connect to a fps game server (assaultcube) and display the information on a webpage, I'm pretty much a newbie to PHP coding, I understand the concept and a little code, but i'm stumped with this. I have XAMP installed and if I run this on my pc it works ok but when I upload it to my hosting provider (http://www.host2x.com) I get this error 'Error:: Cannot get server status' . I suspect the problem lies somewhere in the tostr function but I just can't get my head around it. I would very much appreciate it if someone can help me understand what is going on here.

Code: Select all

<head>
<title>AssaultCube server info by PxL</title>
<style>
body {
	background-color: #696969;
	color: #000000;
}
</style>
</head>

<body>
<?php
if(!isset($_POST['ac_server'])) {
?>
<center>
<form action="" method="post">
<table width="600" border=1>
<tr>
	<td>Server IP:</td>
	<td><input type="text" name="ac_server" /></td>
</tr>
<tr>
	<td>Server Port:</td>
	<td><input type="text" name="port" /></td>
</tr>
<tr>
	<td colspan="2" align="center"><input type="submit" value="check" /></td>
</tr>
</table>
</form>
</center>
<?php
	exit;
}

$ip = $_REQUEST['ac_server'];
$port = (int)$_REQUEST['port'];

if (!preg_match('![0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}!s', $ip)) { // Not the perfect check, but just for filtering
	die("Error:: Incorrect IP address");
}

$si = ac_serverinfo($ip, $port);
if ($si == -1) {
	die("Error:: Cannot get server status");
}

$si['description'] = textcolors($si['description']);

?>
<center>
<table width="600" border=1>
<tr>
	<td align="right"><b>Server:</b></td>
	<td><?=$ip.":".$port?></td>
</tr>
<tr>
	<td align="right"><b>Mode:</b></td>
	<td><?=$si['mode']?></td>
</tr>
<tr>
	<td align="right"><b>Players:</b></td>
	<td><?=$si['numplayers']."/".$si['maxplayers']?></td>
</tr>
<tr>
	<td align="right"><b>Map:</b></td>
	<td><?=$si['map']?></td>
</tr>
<tr>
	<td align="right"><b>Description:</b></td>
	<td><?=$si['description']?></td>
</tr>
<tr>
	<td align="right"><b>Status:</b></td>
	<td><?=$si['pongresponse']?></td>
</tr>
<tr>
	<td align="right"><b>Playerlist:</b></td>
	<td>
<?php
	$players = explode(',', $si['players']);

	foreach($players as $player) {
		print htmlspecialchars($player)."<br />";
	}
?>
	</td>
</tr>
</table>
</center>

</body>

<?php
// from AC docs/colouredtext.txt
function textcolors($text) {
//	$xterm_colors = array('\033[1;32m','\033[1;34m','\033[1;33m','\033[1;31m','\033[1;30m','\033[1;37m','\033[1;36m','\033[1;35m'); // used that for testing
	$html_colors = array(
		"<span style='color: #00ee00'>",
		"<span style='color: #0000ee'>",
		"<span style='color: #f7de12'>",
		"<span style='color: #ee0000'>",
		"<span style='color: #767676'>",
		"<span style='color: #eeeeee'>",
		"<span style='color: #824f03'>",
		"<span style='color: #9a0000'>"
	);

	$cflg = false;
	$out = '';
	$first = true;
	$founded = false;

	for($i = 0; $i < strlen($text); $i++) {
		$c = ord($text[$i]);

		if ($cflg) {
			$out.=($first?'':"</span>").$html_colors[(int)$text[$i]];
			$cflg = false;
			$first = false;
		} else if($c == 12) {
			$cflg = true;
			$founded = true;
		}
		else {
			$out.=$text[$i];
		}
	}

	return $out.($founded?'</span>':'');
}

function ac_serverinfo($server, $port) {
// by PxL pxl@insecurebg.org
// do whatever you like with that code as long as this comment stayes
// 02/25/2009

	//Local functions
	function tostr($hs) {
		$rsp = '';
		for($i = 0; $i < strlen($hs); $i+=2) {
			$rsp.= chr(hexdec($hs[$i].$hs[$i+1]));
		}

		return $rsp;
	}

	// From AC protocol.cpp
	function getint($p) {
		$c = ord($p);

			if($c == -128) return ($c | char($c) << 8);
			else if($c== -127) { $n = $c; $n |= $c<<8; $n |= $c<<16; $n |= ($c<<24); return $n; }

		    return $c;
	}

	define("DEFAULT_TIMEOUT", '3');
	define("PING_REQUEST", '0a01');

	// protocol.h:enum { PONGFLAG_PASSWORD = 0, PONGFLAG_BANNED, PONGFLAG_BLACKLIST, PONGFLAG_MASTERMODE = 6, PONGFLAG_NUM };
	define('PONGFLAG_PASSWORD', 0);
	define('PONGFLAG_BANNED', 1);
	define('PONGFLAG_BLACKLIST', 2);
	define('PONGFLAG_MASTERMODE', 6);
	define('PONGFLAG_NUM', 7);

	// From AC prtocol.cpp
	$mmnames = array("open", "private");

	$modefullnames = array(
		"demo playback",
		"team deathmatch", "coopedit", "deathmatch", "survivor", 
		"team survivor", "ctf", "pistol frenzy", "bot team deathmatch", "bot deathmatch", "last swiss standing", 
		"one shot, one kill", "team one shot, one kill", "bot one shot, one kill", "hunt the flag", "team keep the flag", "keep the flag" 
	); 


	$server_h = fsockopen("udp://" . $server, $port, $errno, $errstr);
	if (!$server_h) {
		printf("Error creating socket[%d]:: %s\n", $errno, $errstr);
		return -1;
	}
	socket_set_timeout($server_h, DEFAULT_TIMEOUT);

	fwrite($server_h, tostr(PING_REQUEST));
	$data = fread($server_h, 1024);

	if (strlen($data) == 0) return -1;

	$si['mode'] = $modefullnames[getint($data[5])+1];
	$si['numplayers'] = getint($data[6]);
	$si['minremain'] = getint($data[7]);

	$si['map'] = '';
	for($i = 8;;$i++) {
		if (ord($data[$i]) == 0) break;

		$si['map'].=$data[$i];
	}

	$si['description'] = '';
	for($j = $i+1;;$j++) {
		if (ord($data[$j]) == 0) break;

		$si['description'].=$data[$j];
	}

	$si['maxplayers'] = getint($data[$j+1]);
	$pongflags = $si['pongflags'] = getint($data[$j + 2]);
	$j = $j+4;
	$si['players'] = preg_replace('!\00!s', ',', substr($data, $j, strlen($data)-$j-2));

	$si['pongresponse'] = 'open';

	// from AC serverbrowser.cpp
	if($pongflags > 0) {
		$mm = $pongflags >> PONGFLAG_MASTERMODE;

		if($pongflags & (1 << PONGFLAG_BANNED))
			$si['pongresponse'] = "you are banned from this server";
		if($pongflags & (1 << PONGFLAG_BLACKLIST))
			$si['pongresponse'] = "you are blacklisted on this server";
		else if($pongflags & (1 << PONGFLAG_PASSWORD))
			$si['pongresponse'] = "this server is password-protected";
		else if($mm)  $si['pongresponse'] = $mmnames[$mm];
	}

	fclose($server_h);

	return $si;
}

?>
The result is :-

Code: Select all

Error:: Cannot get server status 

Re: Help need using fsockopen please

Posted: Wed Apr 14, 2010 12:46 pm
by Technocrat
If it works locally and not on your host then its most likely something with your host. They probably have the ability to fopen a remote location locked out.

Re: Help need using fsockopen please

Posted: Wed Apr 14, 2010 1:04 pm
by Billydozer
Thanx for your suggestion, I had a look around and found this little script that uses fsockopen and it works ok, so I'm thinking it must be the config on the server.

Code: Select all

<?PHP
$ts_ip = "94.76.217.112"; // Change to your server's IP external or domain name
$ts_port = "7900"; // Make sure this port is open on the router or firewall

$output = @fsockopen("udp://".$ts_ip, $ts_port, $errno, $errstr, 2);
socket_set_timeout($output, 000002);

if (!$output) {
echo "<FONT COLOR=#000000><B>$ts_ip:$ts_port<FONT COLOR=#00DD00>OFFLINE<br /></B></FONT>";
} else {
echo "<FONT COLOR=#000000><B>$ts_ip:$ts_port<FONT COLOR=#00DD00> ONLINE<br /></B></FONT>";
}
@fclose($output);
?>


Re: Help need using fsockopen please

Posted: Wed Apr 14, 2010 1:17 pm
by Technocrat
If you do a phpinfo() and look at allow_url_fopen that should tell you if you can or not. Some hosts lock that out as in their eyes it's insecure for some reason.

Re: Help need using fsockopen please

Posted: Wed Apr 14, 2010 2:58 pm
by Billydozer
allow_url_fopen is on but there is another entry under disable_functions -'show_source, system, allow_url_fopen exec, popen' would that have anything to do with it.

Re: Help need using fsockopen please

Posted: Wed Apr 14, 2010 3:05 pm
by Technocrat
I am not a server guy, but that seems likely the reason.

Re: Help need using fsockopen please

Posted: Wed Apr 14, 2010 5:28 pm
by Billydozer
Technocrat wrote:I am not a server guy, but that seems likely the reason.
Thanks for your help, I'm waiting on the hosting to get back too me, but i'm not holding my breath as its a free site and probably wont allow me to do what I want too. :D