<?php

function get_substring($string, $start, $end){
	$string = ' ' . $string;
	$ini = strpos($string, $start);
	if ($ini == 0) return '';
	$ini += strlen($start);
	$len = strpos($string, $end, $ini) - $ini;
	return substr($string, $ini, $len);
}

function ip2geo ($host) {
	@$data = file_get_contents('http://ipinfo.io/'.$host);
	if ($data) {
		$data = json_decode($data);
		return $data->loc;
	} else {
		return "";
	
	}
}

$data = array("points" => array());
$traceroute = "";

function traceroute() {
	global $data, $traceroute;
	$traceroute = shell_exec("traceroute google.com");

	$first = true;
	foreach(preg_split("/((\r?\n)|(\r\n?))/", $traceroute) as $line){
		if ($first) {
			$first = false;
			continue;
		}
		$ipgeo = explode(",", ip2geo(get_substring($line, "(", ")")));

		$coordinates = array('x' => $ipgeo[0], 'y' => $ipgeo[1]);
		
		array_push($data['points'], $coordinates);
	}
}

function raw_output() {
	global $traceroute;
	echo json_encode($traceroute);
}

function json_output() {
	global $data;
	echo json_encode($data);
}





?>