Location: PHPKode > scripts > Socketeer > socketeer/fields.php
<?php

error_reporting( E_ALL ^ E_NOTICE );
/**
 * The class contains variable names, header presets and so forth, specific
 * for the applikation in the use-case, ogame (.dk)
 */
class Fields {

	/** run in ctor chain
	 */
	function fields_initialize()
	{
//		$this->scanForFarms(array(4, 28, 4));
		$this->scanForFarms(array(1, 259, 8));
	}
	var $cookies = array();
	var $header = string;
	var $servertime = string;

	var $fleets = array(
	array(
	REALNAME => "Lille Transporter",
	FORMNAME => "202",
	CONSUMPT => "",
	CAPACITY => "5000",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Stor Transporter",
	FORMNAME => "203",
	CONSUMPT => "",
	CAPACITY => "25000",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Lille Jæger",
	FORMNAME => "204",
	CONSUMPT => "",
	CAPACITY => "50",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Stor Jæger",
	FORMNAME => "205",
	CONSUMPT => "",
	CAPACITY => "100",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Krydser",
	FORMNAME => "206",
	CONSUMPT => "",
	CAPACITY => "800",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Slagskib",
	FORMNAME => "207",
	CONSUMPT => "",
	CAPACITY => "1500",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Koloniskib",
	FORMNAME => "208",
	CONSUMPT => "",
	CAPACITY => "7500",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Recycler",
	FORMNAME => "209",
	CONSUMPT => "",
	CAPACITY => "20000",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Spionagesonde",
	FORMNAME => "210",
	CONSUMPT => "",
	CAPACITY => "0",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Bomber",
	FORMNAME => "211",
	CONSUMPT => "",
	CAPACITY => "500",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Solarsatellit",
	FORMNAME => "212",
	CONSUMPT => "",
	CAPACITY => "",
	SPEED    => "",
	MAXVALUE => ""),

	array(
	REALNAME => "Destroyer",
	FORMNAME => "213",
	CONSUMPT => "",
	CAPACITY => "2000",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Dødsstjerne",
	FORMNAME => "214",
	CONSUMPT => "",
	CAPACITY => "",
	SPEED    => "",
	MAXVALUE => ""),
	array(
	REALNAME => "Interceptor",
	FORMNAME => "215",
	CONSUMPT => "",
	CAPACITY => "750",
	SPEED    => "",
	MAXVALUE => "")
	/*
	 array(
	 REALNAME => "Unknown",
	 FORMNAME => "216",
	 CONSUMPT => "",
	 CAPACITY => "",
	 SPEED    => "",
	 MAXVALUE => "")
		"Koloniskib";
		"Bomber";
		"Destroyer";
		"Dødsstjerne";
		"Interceptor";
		*/
	);

	/* Resource arrays */
	var $resource = array(
	metal => "",
	krystal => "",
	deut => "");
	var $shipment = array( 0, 0, 0);

	/* Coordinates */
	var $universe = array(
	FORMVAL => "thisuniverse" ,
	CONTENT => "" );
	var $galaxy = array(
	FORMVAL => "thisgalaxy" ,
	CONTENT => "" );
	var $system = array(
	FORMVAL => "thissystem" ,
	CONTENT => "" );
	var $planet = array(
	FORMVAL => "thisplanet" ,
	CONTENT => "" );
	var $planettype = array(
	FORMVAL => "thisplanettype" ,
	CONTENT => "" );
	/* The planets */
	var $cp = string;
	var $planetsloaded = false;
	var $myplanets = array(
	NAME => array(),
	COORD => array(),
	CP => array(),
	MAINP => array());
	/* ATTACK */
	var $attackStar = array(
	STAR => array(),
	CP => array());
	var $targetPlanet = array();
	var $farmPlanets = array(
	COORDS => array(),
	RETURNS => array(),
	SCANNED => array(),
	SS => array());
	var $nFarms;
	var $farmTrigger;
	/**
	 * Whether we should still send farms out
	 * checked in whileloop and evaluated with the timestamp Fields::farmDone
	 *
	 * @var bool
	 */
	var $farmStatus = false;

	function getPlanetCPByName($name)
	{

		for($i = 0; $i <  count($this->myplanets['NAME']); $i++)
		if ( preg_match("/$name/i", $this->myplanets['NAME'][$i]) )
		return $this->myplanets['CP'][$i];
		return null;
	}
	function getCoordsByCP($cp)
	{
		for($i = 0; $i < count($this->myplanets['CP']); $i++)
		if( $this->myplanets['CP'][$i] == $cp)
		return $this->myplanets['COORD'][$i];
		return null;
	}
	function getAttackTimeByCP($cp)
	{
		for($i = 0; $i < count($this->attackStar['CP']); $i++)
		if( $this->attackStar['CP'][$i] == $cp)
		return $this->attackStar['STAR'][$i];
		return null;
	}
	function setFarm($coord, $ss = 0)
	{
		array_push($this->farmPlanets['COORDS'], $coord);
		array_push($this->farmPlanets['SS'], $ss);
		array_push($this->farmPlanets['SCANNED'], false);
		array_push($this->farmPlanets['RETURNS'], (date("U")-1000000));
		$this->nFarms++;
	}

	function setFarmTarget()
	{
		$i = 0;
		while(true)
		{
			$key = array_rand($this->farmPlanets['RETURNS']);
			if($i++ > 50) {
				echo "No farm targets available<br>\n";
				break;
			}
			if( $this->farmPlanets['RETURNS'][$key] - $this->servertime < 0) {
				break;
			}
		}
		$this->farmCoords = $this->farmPlanets['COORDS'][$key];
		$this->farmTrigger = $key;
		return $this->farmCoords;
	}
	/**
	 *
	 * @param long epoch - the unix-time in seconds, parsed from the output when
	 * a farm-action is sent
	 */
	function setFarmReturns($epoch)
	{ 
		// farmTrigger represents the current target
		$this->farmPlanets['RETURNS'][$this->farmTrigger] = $epoch;
	}
	/**
	 * Each coordinate is classified in categories determined
	 * by its defence structures (how much fleet to send but transporters)
	 *
	 * @returns integer category
	 */
	function getFarmDefenceCategory()
	{
		return $this->farmPlanets['SS'][$this->farmTrigger];
	}
	/**
	 * @param bool cp - whether or not to represent planet as CP id
	 *
	 * @returns either a CP id or the coordinates from the home-planet
	 */
	function getMain($cp = false)
	{
		$count=0;
		foreach($this->myplanets['MAINP'] as $planet) {
			if($planet)
			break;
			$count++;
		}
		if($cp) {
			return $this->myplanets['CP'][$count];
		} else {
			return $this->myplanets['COORD'][$count];
		}
	}
	/**
	 * The main functionality of this class lies here. Switches 'which', and
	 * sets up the socketeer with the fields according to the action taken
	 *
	 * @param string which - may be login/fleetX/return
	 */
	function setHeader($which)
	{
		$post = false;
		switch($which)
		{
			case 'login':
				$post = true;
				$msg = "v=2&universe=$this->host&login=$this->loginname&pass=$this->password";
				$this->header =
		    "POST /game/reg/login2.php HTTP/1.1\n" .
		    "Referer: http://ogame.dk/home.php\n";
				break;
			case 'overview':
				$data = "?page=overview&session=".$this->sessionid;
				$ref = "?page=flotten1&session=".$this->sessionid."&mode=Flotte";
				$this->header =
		    "GET /game/index.php".$data." HTTP/1.1\n".
		    "Referer: http://$this->host/game/index.php$ref\n";
				break;
			case 'fleet1':
				$data = "?page=flotten1&session=".$this->sessionid."&mode=Flotte&cp=$this->cp";
				$this->header =
		      "GET /game/index.php$data HTTP/1.1\n";
				break;
			case 'fleet2':
				$data = "?page=flotten2&session=".$this->sessionid;
				$ref = "?page=flotten1&session=".$this->sessionid."&mode=Flotte";
				$post = true;
				$msg = $this->formURL($this->contentBuffer);
				$this->header =
		    "POST /game/index.php$data HTTP/1.1\n".
		    "Referer: http://$this->host/game/index.php$ref\n";
				break;
			case 'fleet3':
				$data = "?page=flotten3&session=".$this->sessionid;
				$ref = "?page=flotten2&session=".$this->sessionid;
				$post = true;
				$msg = $this->formURL($this->contentBuffer);
				$this->header =
		    "POST /game/index.php$data HTTP/1.1\n".
		    "Referer: http://$this->host/game/index.php$ref\n";
				break;
			case 'fleet4':
				$data = "?page=flottenversand&session=".$this->sessionid;
				$ref ="?page=flotten3&session=".$this->sessionid;
				$post = true;
				$msg = $this->formURL($this->contentBuffer);
				$this->header =
		    "POST /game/index.php$data HTTP/1.1\n".
		    "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.10) Gecko/20071231 Firefox/2.0.0.10\n" .
		    "Referer: http://$this->host/game/index.php$ref\n";
				break;
			case 'return':
				$data = "?page=flotten1&session=".$this->sessionid;
				$ref ="?page=flotten1&session=".$this->sessionid;
				$post = true;
				$msg = $this->formURL($this->contentBuffer);
				$this->header =
		    "POST /game/index.php$data HTTP/1.1\n";
		    "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.10) Gecko/20071231 Firefox/2.0.0.10\n" .
		    "Referer: http://$this->host/game/index.php$ref\n";
				break;
		}
		$this->header .=
	    "Host: $this->host\n" .
	    "Accept-Charset: ISO-8859-1\n" .
	    "Keep-Alive: 300\n" .
	    "Connection: keep-alive\n" .
		//"Connection: close\n".
	    "Cookie: ".$this->getCookies()."\n";
		if($post) {
			/* Send post */
			$this->header .=
		"Content-Type: application/x-www-form-urlencoded\n" .
		"Content-Length: " . strlen($msg) . "\n\n" .$msg. "\n";
		} else {
			/* Flush */
			$this->header .= "\n\n";
		}
	}
	/**
	 * resets the socketeer cookies variable
	 */ 
	function resetCookies()
	{
		for($i = 0; $i < count($this->cookies); $i++) {
			array_pop($this->cookies);
		}
		array_pop($this->cookies);
	}
	/**
	 * sets the socketeer cookies from a parsed header-line
	 */
	function setCookie ($line)
	{
		$i = 0;
		if( preg_match("/Set-Cook/", $line) ) {
			$content = substr($line, strlen("Set-Cookie: ") );
		} else {
			$content = $line;
		}

		$content = preg_replace("/;.*/", "", $content);
		$ident = preg_replace("/=.*/", "", $content);
		foreach($this->cookies as $theCookie)
		{
			if(preg_match("/$ident/i", $theCookie)) {
				$this->cookies[$i] = $content;
				return;
			}
			$i++;
		}
		array_push($this->cookies, $content);
				echo "is now ".$content."<br>";
	}
	/**
	 * WiP, requires a little shellscript, parsing sessionstore 
	 * file from user directory
	 */
	function setCookiesFromFF()
	{
		$this->resetCookies();
		system('./ffCookie');
		$fp = fopen("firefox.cookies", "r");
		if(!$fp) {
			print "Problem with FF cookies<br>\n"; return;
		}
		$i = 0; $sessid_set = false;
		while(!feof($fp))
		{
			$buf = fgets($fp);
			if( preg_match("/domain/", $buf) )
			{
				$buf = fgets($fp);
				$sub = preg_replace("/[.*\"]/", "", $buf);
				$sub = preg_replace("/\".*/", "", $sub);
				$sub = preg_replace("/value[0-9]*:/", "", $sub);
				$sub = preg_replace("/;.*/", "", $sub);
				echo "Cookie parse: <br>$sub<br>";
				array_push($this->cookies, preg_replace("/\n/", "", $sub));
			//} else if( preg_match("/url/", $buf) && !$sessid_set) {
			//	$sessid_set = $this->setSession($buf);
			//	echo "Session id set: <br>$this->sessionid<br>";
			}
			$i++;
		}
		fclose($fp);
		echo "done";
		foreach($this->cookies as $cookie)
			echo $cookie."<br>";
	}
	/**
	 * gets a specific cookie
	 * @returns string cookies - the specific cookie, asked for
	 */
	function getCookie($type)
	{
		foreach($this->cookies as $theCookie)
		if(preg_match("/$type/i", $theCookie))
		return $theCookie;
	}
	/**
	 * gets a formatted cookie string
	 * @returns string cookies - all stored cookies
	 */
	function getCookies()
	{
		$ret = "";
		$z = sizeof($this->cookies);
		for($i=0; $i < $z; $i++)
		( $i == ($z-1) ) ? $ret.=$this->cookies[$i] : $ret.=$this->cookies[$i].";";

		return $ret;
	}
	/**
	 * sets the ogame sessionid from a req-parsed line
	 * @param string line - the current line in the contents-parser
	 */
	function setSession($line)
	{
//		if(! (preg_match("session" , $line) && preg_match("uni", $line)) )
//			return false;
		$this->sessionid = substr($line, ((strpos($line, "session="))+strlen("session=")), 12);
		$_SESSION['id'] = $this->sessionid;
		return true;
	}
	/**
	 * setter for a 'static' set of coordinates
	 * @param galaxy
	 * @param system
	 * @param planet
	 * @param string type - either (P)lanet, (R)uin(M)ark or (M)oon
	 */
	function setCoords($galaxy, $system, $planet, $type)
	{
		$this->galaxy['CONTENT'] = $galaxy;
		$this->system['CONTENT'] = $system;
		$this->planet['CONTENT'] = $planet;
		/* P == Planet, RM == Ruinmark, M == Moon */
		switch($type)
		{
			case 'P' :
				$this->planettype['CONTENT'] = 1;
				break;
			case 'RM':
				$this->planettype['CONTENT'] = 2;
				break;
			case 'M' :
				$this->planettype['CONTENT'] = 3;
				break;
		}
	}
	/**
	 * Parses the few lines from response-payload, 
	 * identifying the 'star' property. This is a unix (1970) timestamp
	 *
	 * @param array $array, the '$obj->content' arr, stripped to a couple lines
	 */
	function setTimer($array)
	{
		// Identify CP and attackStar
		$search = "star='";
		$pattern = "nærmer sig";

		foreach($array as $line)
		{
			$this->readDebug($line);
			if(strstr( $line, $pattern) != false )
			{
				$name = preg_replace("/.*nærmer sig/", "",  $line);
				$name = preg_replace("/<.*/", "", $name);
				$name = preg_replace("/Planeten /", "", $name);
				$name = preg_replace("/\ /", "", $name);
				array_push($this->attackStar['CP'], $this->getPlanetCPByName($name));

			} else if( strstr($line, $search) != false ) {

				$begin = strpos($line, $search) + strlen($search);
				$end = strpos($line, "'", ($begin+1) );
				// $len = 10 but will increase.. eventually :)
				$length = $end-$begin;
				$epoch = (substr($line, $begin, $length));
				array_push($this->attackStar['STAR'], ($epoch)); // - $this->servertime));
			}
		}
	}
}

?>
Return current item: Socketeer