%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /data/www_bck/varak.net_bck/ingress.varak.net/api/
Upload File :
Create Path :
Current File : //data/www_bck/varak.net_bck/ingress.varak.net/api/getActions.php

<?php
	function getmicrotime() 
	{
  		list($usec,$sec)=explode(" ",microtime()); 
  		return ((float)$usec+(float)$sec);
	}
	require("config.php");
	error_reporting(0);
	$spojeni = mysql_connect($srv, $usr, $pwd);
	mysql_select_db($dbname, $spojeni);
	header("Content-type: text/plain");
	$total_count = 0;

	$cities = array();
	// Brno
	$c = new stdClass();
	$c->name = "Brno";
	$c->minLat = 49128973;
	$c->minLng = 16015598;
	$c->maxLat = 49353120;
	$c->maxLng = 17128650;
	$cities[] = $c;
	// Kromeriz
	$c = new stdClass();
	$c->name = "Kromeriz";
	$c->minLat = 49241285;
	$c->minLng = 17308453;
	$c->maxLat = 49349083;
	$c->maxLng = 17473746;
	$cities[] = $c;
	// Ostrava
	$c = new stdClass();
	$c->name = "Ostrava";
	$c->minLat = 49698283;
	$c->minLng = 17907715;
	$c->maxLat = 49941057;
	$c->maxLng = 18566895;
	$cities[] = $c;

	ob_start();
	$tstart = getmicrotime();
	$players = array();
	$player_guids = array();
	$res = mysql_query("select id, name, guid from players");	
	while($r = mysql_fetch_array($res))
	{
		$players[$r[1]] = $r[0];
		$player_guids[$r[0]] = trim($r[2]);
	}

	foreach($cities as $city)
	{
		echo "Processing ".$city->name."\n========================================\n";
		$maxtime_guids = array();
		$cnt = 0;
		$end = false;
		$toInsert = array();
		$guids = array();
		$last = 0;
		$iter = 0;
		$msgs = array();
		$duplicates = 0;
		$maxtime = 0;
		$res = mysql_query("select max(`when`) from actions where city='".$city->name."'");
		if($res != false)
		{
			if($r = mysql_fetch_array($res))
			{
				$maxtime = $r[0];
				$res = mysql_query("select guid from messages where time >= ".$maxtime."000 AND city='".$city->name."'");
				while($r = mysql_fetch_object($res))
				{
					$maxtime_guids[] = trim($r->guid);
				}
			}
		}
		$maxtimems = -1;
		while(!$end)
		{
			$dashboard = false;
			$i = 0;
			while((!$dashboard || !isset($dashboard->result)) && $i < 3)
			{
				$i++;
				$data = "";
				//echo 'curl https://www.ingress.com/rpc/dashboard.getPaginatedPlextsV2 --compressed --insecure --header "DNT: 1" --user-agent "'.$ing_user_agent.'" --cookie "'.$ing_cookie.'" --cookie-jar "ingress_intel_chat.cookie.jar" --referer "https://www.ingress.com/intel" --header "X-CSRFToken: '.$ing_csrf.'" --header "X-Requested-With: XMLHttpRequest" --data-binary "{\"desiredNumItems\":200,\"minLatE6\":'.$city->minLat.',\"minLngE6\":'.$city->minLng.',\"maxLatE6\":'.$city->maxLat.',\"maxLngE6\":'.$city->maxLng.',\"minTimestampMs\":-1,\"maxTimestampMs\":'.$maxtimems.',\"method\":\"dashboard.getPaginatedPlextsV2\"}" 2> /dev/null';
				//die;
				$input = popen('curl https://www.ingress.com/rpc/dashboard.getPaginatedPlextsV2 --compressed --insecure --header "DNT: 1" --user-agent "'.$ing_user_agent.'" --cookie "'.$ing_cookie.'" --cookie-jar "ingress_intel_chat.cookie.jar" --referer "https://www.ingress.com/intel" --header "X-CSRFToken: '.$ing_csrf.'" --header "X-Requested-With: XMLHttpRequest" --data-binary "{\"desiredNumItems\":200,\"minLatE6\":'.$city->minLat.',\"minLngE6\":'.$city->minLng.',\"maxLatE6\":'.$city->maxLat.',\"maxLngE6\":'.$city->maxLng.',\"minTimestampMs\":-1,\"maxTimestampMs\":'.$maxtimems.',\"method\":\"dashboard.getPaginatedPlextsV2\"}" --interface eth1:2 2> /dev/null', "r");
				while(!feof($input))
				{
					$data .= fgets($input, 64*1024);
				}
				pclose($input);
				//print_r($data);
				$dashboard = json_decode($data);
			}
			$last = $dashboard->result[0][1];
			foreach($dashboard->result as $res)
			{
				$time = (int)substr($res[1], 0, 10);
				if($time < $maxtime)
				{
					$end = true;
					continue;
				}
				$mguid = trim($res[0]);
				if(in_array($mguid, $guids) || in_array($mguid, $maxtime_guids))
				{
					$duplicates++;
					continue;
				}
				$guids[] = $mguid;
				$msg = "insert into messages(time, guid, message, city) values(".$res[1].", '".$res[0]."', '".str_replace("'", "\\'", json_encode($res))."', '".$city->name."')";
				$msgs[] = $msg; 
				//print_r($msgs);
				$maxtimems = $res[1];
				if($res[2]->plext->plextType == "PLAYER_GENERATED") continue;
				if($res[2]->plext->team == "NEUTRAL") continue;
				if($res[2]->plext->plextType == "SYSTEM_NARROWCAST") continue;
				if($res[2]->plext->markup[4][1]->plain == 'has decayed') continue;
				if($res[2]->plext->markup[0][0] != "PLAYER" && $res[2]->plext->markup[4][1]->plain != 'has decayed')
				{
					print_r($res);
					continue;
				}
				$level = 0;
				$who = $res[2]->plext->markup[0][1]->plain;
				$side = $res[2]->plext->markup[0][1]->team;
				$guid = $res[2]->plext->markup[0][1]->guid;
				// Get player ID or insert into DB
				if(!isset($players[$who]))
				{
					mysql_query("insert into players(name, side, guid) values('$who', '$side', '$guid')");
					$players[$who] = mysql_insert_id();
					$player_guids[$players[$who]] = $guid;
					echo "Inserting player $who (#".$players[$who].", GUID: $guid)\n";
				}
				$who = $players[$who];
				if(!isset($player_guids[$who]) || $player_guids[$who] == "")
				{
					mysql_query("update players set guid='$guid' where id=$who");
					echo "Adding GUID $guid to player ".$res[2]->plext->markup[0][1]->plain."\n";
					$player_guids[$who] = $guid;
				}
				$action = $res[2]->plext->markup[1][1]->plain;
				$ap = 0;
				switch(trim($action))
				{
					case 'destroyed an':
					{
						$level = (int)substr($res[2]->plext->markup[2][1]->plain, 1);
						$action = 'resonator_destroy';
						$ap = 75;
						break;
					}
					case 'destroyed the Link':
					{
						$action = 'link_destroy';
						$ap = 187;
						break;
					}
					case 'destroyed a Control Field @':
					{
						$action = 'field_destroy';
						$ap = 750;
						break;
					}
					case 'deployed an':
					{
						$level = (int)substr($res[2]->plext->markup[2][1]->plain, 1);
						$action = 'resonator_create';
						$ap = 125;
						break;
					}
					case 'linked':
					{
						$action = 'link_create';
						$ap = 313;
						break;
					}
					case 'created a Control Field @':
					{
						$action = "field_create";
						$ap = 1250;
						break;
					}
					case 'captured':
					{
						$action = 'capture';
						$ap = 500;
						break;
					}
					default:
					{
						print_r($res);
						break;
					}
				}
				// Now get position of the portal
				$lat = 0;
				$lng = 0;
				foreach($res[2]->plext->markup as $item)
				{
					if($item[0] == 'PORTAL')
					{
						$lat = $item[1]->latE6;
						$lng = $item[1]->lngE6;
						break;
					}
				}
				$query = "insert into actions(who, `when`, action, level, ap, latitude, longitude, city) values($who, $time, '$action', $level, $ap, $lat, $lng, '".$city->name."')";
				//echo $query."\n";
				if($maxtimems != $last)
				{
					//echo "aaa\n";
					$last = $maxtimems;
					foreach ($toInsert as $act)
					{
						mysql_query($act);
						$cnt++;
					}
					foreach ($msgs as $m) 
					{
						mysql_query($m);
					}
					$toInsert = array();
					//$guids = array();
					$msgs = array();
					$toInsert[] = $query;
				}
				else
				{
					//echo "bb\n";
					$toInsert[] = $query;
				}
			}
			$iter++;
			echo "Request $iter...\n";
			if($iter > 20)
				$end = true;
		}
		echo "Inserted $cnt actions ($duplicates duplicates)\n";
		$total_count += $cnt;
		if($end == false)
			echo "END NOT REACHED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
		echo "\n\n";
	}
	mysql_close();
	$tend = getmicrotime();
	$tim = $tend - $tstart;
	printf("Saving %d actions took %01.4f seconds\n", $total_count, $tim);
?>
<?php
	$ret = ob_get_clean();

	// Gzip/Deflate/Bzip2 compression
	if(stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "bzip2"))
	{
		$ret = bzcompress($ret, 9);
		header("Content-Encoding: bzip2");
	}
    elseif(stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip"))
	{
		$ret = gzencode($ret, 9);
		header("Content-Encoding: gzip");
	}
	elseif (stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "deflate")) 
	{
		$ret = gzdeflate($ret, 9);
		header("Content-Encoding: deflate");
	}
	
	header("Content-length: ".strlen($ret));
	echo $ret;
?>

Zerion Mini Shell 1.0