Location: PHPKode > scripts > Socketeer > socketeer/socketeer.php
<?php
require_once "ogamecontrol.php";

error_reporting( E_ALL ^ E_NOTICE );

/**
 * Socket utility library, suitable for handling HTTP sockets
 *
 */
class Socketeer extends OgameControl {
	var $host = "";
	var $sessionid = "";
	var $loginname = "";
	var $password = "";
	var $sock = null;
	var $z = null;
	var $content = array();
	/**
	 * Socketeer constructor
	 * host and login parameters are handled and initialized
	 * 
	 * @param string host is the uni#.ogame.??
	 * @param string login, use this as users loginname
	 * @param string pass, the password
	 *
	 * More on this
	 */
	public function __construct($host, $login, $pass)
	{
		$this->loginname = $login;
		$this->password = $pass;
		$this->host = $host;
		$url = parse_url($host); $url = explode(".", $url['path']);
		$this->universe['CONTENT'] = preg_replace("/uni/", "", $url[0]);
		$this->fields_initialize();
	}

	/***************************************************************************
	 * Socket library functions features,
	 * a html-fitting functionality for debug output, (readDebug & printChar)
	 * some common error handling on failed connections (if/else in readInput)
	 * and cookie handling. Save this object in your local session
	 * to keep-alive on a remote host
	 *
	 * Adapt readInputStream to your needs
	 **************************************************************************/
	function socketConnect()
	{
		$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die(socket_strerror(socket_last_error()));
		$this->z = socket_connect($this->sock, gethostbyname($this->host), '80') or die(socket_strerror(socket_last_error()));
		if($this->z === false)
		echo 'Error msg: '. socket_strerror(socket_last_error());
	}

	/**
	 * Print a line with formats sorted out for HTML viewing
	 * replaces less/greater than brackets as well as indents
	 * 
	 * @param unknown_type $line
	 */
	function readDebug($line)
	{
		$txt = preg_replace("/</", "&lt;", $line);
		$txt = preg_replace("/>/", "&gt;", $txt);
		$txt = preg_replace("/\t/", "&nbsp;&nbsp;&nbsp;&nbsp;", $txt);
		$txt .= "<br>";
		echo $txt;
	}
	/**
	 * Print a char in W3C format
	 * replaces less/greater than brackets 
	 * as well as indents n newlines
	 *
	 * @param character $c
	 */
	function printChar($c)
	{
		$c = preg_replace("/</", "&lt;", $c);
		$c = preg_replace("/>/", "&gt;", $c);
		$c = preg_replace("/\n/", "<br>;", $c);
		$c = preg_replace("/\t/", "&nbsp;&nbsp;&nbsp;&nbsp;", $c);
		echo $c;
	}

	/**
	 * Send request, no fuss nor parsing done
	 *
	 */
	function talkToTheHand()
	{
		$this->socketConnect();
		socket_write($this->sock, $this->header, strlen($this->header));
		sleep(1);
		socket_close($this->sock);
		$this->sock = null;
	}
	/**
	 * Grabs contents from request, starting with $begin line until $end pattern
	 * TODO implementation on $disconnect / keep-alive is not complete, otherwise an existing 
	 * 		channel is possible to be reused in request
	 *
	 * @param regular_expr [ $begin = null ]
	 * @param regular_expr [ $end = null ]
	 * @param bool [ $disconnect = true ]
	 */
	function request($begin = null, $end = null, $disconnect = true)
	{
		if($this->sock == null) {
			$this->socketConnect();
		}
		//$this->outputHeader($this->header);
		socket_write($this->sock, $this->header, strlen($this->header));
		$this->readInputStream($begin, $end, $disconnect);
		socket_close($this->sock);
		$this->sock = null;
	}
	
	/**
	 * Parses inputstream for common errorhandling and logs response data
	 * The response from server is found in Socketeer::contents, logged
	 * starting from when $begin pattern is met until $end pattern is found 
	 * TODO implementation on $disconnect / keep-alive is not complete, otherwise an existing 
	 *		channel is possible to be reused in request
	 *
	 *  @param unknown_type $begin
	 * @param unknown_type $end
	 * @param unknown_type $disconnect
	 */
	function readInputStream($begin, $end, $disconnect)
	{
		($begin == null) ? $record = true : $record = false;
		$counter = 0;
		$relogin = 0;
		while(array_pop($this->content));
		$line = "";
		while (true) {
			$c = socket_read($this->sock, 1);
			// or die(socket_strerror(socket_last_error()));
			if($c == "") {
				return;
			}
	  if ($c == "\n") {
	  	if($begin != null) {
	  		if( preg_match("/$begin/", $line) ) {
	  			//echo "NOW recording...<br>";
	  			//$this->readDebug($line);
	  			$record = true;
	  		}
	  	}
	  	if($record) {
	  		/* Catch Set-Cookie if any */
			if (preg_match("/Set-Cookie/", $line)) {
				$this->setCookie($line);
			}
	  		/* Push to stack and begin newline */
	  		array_push($this->content, $line);
	  	}
	  	//$this->readDebug($line); 		// DEBUG
	  	$line = "";
	  } else {
	  	/* Append read character to line */
	  	$line .= $c;
		if($c == null || $c == "" || $c == "\n") {
			continue;
		}
	  	/* Check for ending delimiter */
	  	if(preg_match("/<\/html/i", $line) ) {
	  		array_push($this->content, $line);
	  		break;
	  	}
		elseif( $record )
		{
			if( $end != null && preg_match("/$end/", $line) )
	  		{
		  		array_push($this->content, $line);
		  		// $this->readDebug($line); 	// DEBUG
		  		break;
		  	}
		}
	  	/* Error handle */
	  	if( preg_match("/Connection closed/i", $line) ) {
	  		print("\nModtagervært lukkede forbindelsen\n");
	  	} elseif ( preg_match("/DB problem/i", $line)) {
	  		print("\nDB problem / session fejl - lukkede forbindelsen\n");
	  		$relogin=true;
	  		break;
	  	} elseif( preg_match("/30[0-9] OK/", $line) ) {
	  		print("\nRedirigeres uden referencer. Fejl i header!\n");
	  		break;
	  	} elseif( preg_match("/404 Not/", $line) ) {
	  		print("\nSiden ikke fundet.. Fejl i header!\n");
	  		break;
	  	} elseif( preg_match("/Session er ugyldig/", $line) ) {
	  		$relogin = true;
	  		print("\nNy ogame-session er nødvendig!!!\n");
	  		break;
	  	} elseif( preg_match("/Der opstod en fejl/", $line) ) {
	  		$relogin=true;
	  		print("\nHmm fejl!!!\n");
	  		break;
	  	}
	  }
		} // WHILE
		if($relogin)
		{
			$_SESSION['relogin'] += 1;
			//	echo "WARNING : relogin required...\n";
		}
	}
	
	/**
	 * When a GET/POST set of parameters have been pushed into an array
	 * this function formats in sence of param1=val1&param2=val2 ...
	 * 
	 * @param array $content strings of format 'parameter=value'
	 * @return unknown
	 */
	function formURL($content)
	{
		$max = count($content);
		$index = 0;
		$retval = "";
		foreach($content as $field)
		{
			$retval .= $field;
			$index++;
			if($index != $max)
			$retval .= "&";
		}
		return $retval;
	}
	function outputHeader($header)
	{
		echo '<div style="width:70%; display: block;background:#EAD4D6; margin-left:30px; border: 1px solid;">';
		$fields = array('Host:', 'Connection:', 'Accept:', 'Accept-Charset:', 'Cookie:', 'Referer:', 'Keep-Alive:', 'Content-Length:', 'Content-Type', 'User-Agent:');
		$out = preg_replace("/GET/", "<strong>GET</strong>", $header);
		$out = preg_replace("/POST/", "<strong>POST</strong>", $out);
		foreach($fields as $pattern)
		$out = preg_replace("/$pattern/i", "<font color='blue'>".$pattern."</font>", $out);
		echo preg_replace("/\n/", "<br>", $out);
		echo '</div>';
		//$file = fopen("telnet.txt", "w");
		//fwrite($file, $header);
		//fclose($file);
	}
	function outputContents($out)
	{
		foreach($out as $line) {
			$txt = preg_replace("/</", "&lt;", $line);
			$txt = preg_replace("/>/", "&gt;", $txt);
			$txt = preg_replace("/\t/", "&nbsp;&nbsp;&nbsp;&nbsp;", $txt);
			$txt .= "<br>";
			echo $txt;
		}
	}
	function outputSelectedFleet()
	{
		echo "<br>\n";
		echo "<table align=center border=1><tr>";
		echo "<th colspan=\"".(sizeof($this->fleets['REALNAME'])+1)."\">The Selected Fleet</th>";
		//	echo "</tr><tr>";
		//	echo "<td>Type</td>";
		foreach($this->fleets as $val) echo "<td style=\"font-weight:bold;\">".$val['REALNAME']."</td>\n";
		echo "</tr><tr>";
		echo "<td>Consumption</td>";
		foreach($this->fleets as $val) echo "<td>".$val['CONSUMPT']."</td>\n";
		echo "</tr><tr>";
		echo "<td>Available</td>";
		foreach($this->fleets as $val) echo "<td>".$val['MAXVALUE']."</td>\n";
		echo "</tr></table>";
	}
	function outputCoords()
	{
		echo "<br>\n";
		echo "<table align=center border=1><tr>";
		echo "<th colspan=\"".(sizeof($this->myplanets['NAME'])+1)."\">myPlanets INFO</th>";
		echo "</tr><tr>";
		echo "<td>Name</td>";
		foreach($this->myplanets['NAME'] as $val)
		echo "<td align=center>$val</td>\n";
		echo "</tr><tr>";
		echo "<td>Coordinate</td>";
		foreach($this->myplanets['COORD'] as $val) {
			echo "<td align=center>$val</td>\n";
		}
		echo "</tr><tr>";
		echo "<td>CP</td>";
		$timers = array();
		foreach($this->myplanets['CP'] as $val)
		{
			echo "<td align=center>$val</td>\n";
			$thistime = $this->getAttackTimeByCP($val);
			if($thistime != null)
			{
				array_push($timers, $thistime);
			} else {
				array_push($timers, "~");
			}
		}

		echo "</tr><tr>";
		echo "<td>E.T.A</td>";

		foreach($timers as $val)
		echo "<td align=center style=\"font-weight:bold\">$val</td>\n";
		echo "</tr></table>";
	}

} // CLASS
?>
Return current item: Socketeer