#!/usr/bin/env php
<?php
	$root = substr( __DIR__, 0, strrpos( __DIR__, '/' ) );
	require_once( $root . '/dronebl.config.php' );
	require_once( $frameworks_path . '/DroneBL/Database.framework' );
	require_once( $frameworks_path . '/DroneBL/Validation.framework' );
	require_once( $frameworks_path . '/DroneBL/Cron.framework' );
	require_once( $frameworks_path . '/DroneBL/Logging.framework');

	chkprog( 'Country/ASN/Bogon Database Updater' );
	$maxbatch = 1000;

	function ip_next($ip)
	{
		global $cache;
		if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
			return long2ip(ip2long($ip)+1);
		else if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
		{
			$unp = unpack( 'H*hex', inet_pton( $ip ));
			$reg = array_reverse( str_split( $unp['hex'] ) );
			$add = 1;
			for ($pos = 0; $pos < count($reg); $pos++)
			{
				if ($add)
				{
					if ( $reg[$pos] == 'f')
						$reg[$pos] = 0;
					else
					{
						$reg[$pos] = dechex( hexdec( $reg[$pos] ) + $add );
						$add = 0;
					}
				}
			}
			return inet_ntop(pack('H*', implode('', array_reverse($reg))));
		}
		else
			return false;
	}
	
	function dlprogress($ch, $download_size, $downloaded, $upload_size)
	{
		printf("\r%s Downloaded %s bytes", date('d-m-Y H:i:s'), $download_size);
	}
	
	function ipcmp($a, $b)
	{
		$a = ip2long($a);
		$b = ip2long($b);
		return ($a == $b ? 0 : ($a > $b ? 1 : -1));
	}
	
	function simplify(&$list)
	{
		global $cur;
		$records = count($list);
		conlog($records.' records to reduce. Starting uksort.');
		uksort($list, 'ipcmp');
		conlog('Done sorting.');
		$cur = 1;
		$found = 0;
		$removelist = [];
		foreach(array_keys($list) AS $ip_start)
		{
			progress($records, $found, 'list');
			if (isset($removelist[$ip_start])) continue;
			$r = $list[$ip_start];
			while ($r)
			{
				$next = ip_next($r['ip_stop']);
				$t = (isset($list[$next]) ? $list[$next] : false);
				if ($t)
				{
					if (isset($t['asn']) && $t['asn'] != $r['asn']) break;
					if (isset($t['country']) && $t['country'] != $r['country']) break;
					unset($list[$t['ip_start']]);
					$removelist[$t['ip_start']] = 1;
					$list[$ip_start]['ip_stop'] = $t['ip_stop'];
					$r['ip_stop'] = $t['ip_stop'];
					$found++;
					continue;
				}
				break;
			}
		}
		$remain = count($list);
		echo PHP_EOL;conlog($remain.'/'.$records.' records remaining.');
		return $remain;
	}
	
	function startTable($name)
	{
		get_db_rows("DROP TABLE ".$name."_new");
		get_db_rows("CREATE TABLE ".$name."_new (LIKE ".$name." INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES)");
	}
	
	function stopTable($name)
	{
		get_db_rows("BEGIN");
		get_db_rows("DROP TABLE ".$name);
		get_db_rows("ALTER TABLE ".$name."_new RENAME TO ".$name);
		get_db_rows("COMMIT");
	}
	
	$file = 'https://iptoasn.com/data/ip2asn-combined.tsv.gz';
	conlog('Fetching database from '.$file);
	if (false)
	{
		$cur = 1;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $file);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36');
		curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'dlprogress');
		curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
		$data = curl_exec ($ch);
		file_put_contents('ip2asn-combined.tsv.gz', $data);
		curl_close($ch);
		echo PHP_EOL;
	}
	else
		$data = file_get_contents('ip2asn-combined.tsv.gz');
	
	$filedata = gzdecode($data);
	unset($data);
	$lines = explode(PHP_EOL, $filedata);
	if (empty($lines))
		endprog();
	unset($filedata);
	
	$linecount = count($lines);
	$cur = 1;
	$added = 0;
	$ip2info = [];
	$ip2info_count = 0;

	foreach ($lines as $line)
	{
		progress($linecount, $added, 'data (I:'.$ip2info_count.')');
		
		if (empty($line)) continue;
		
		list($ip_start, $ip_stop, $asn, $country, $desc) = explode("\t", $line, 5);
		if ($country == 'Unknown') $country = '00';
		
		if (intval($asn) > 0)
		{
			$ip2info[$ip_start] = [
				'ip_start'	=> db_escape($ip_start),
				'ip_stop'	=> db_escape($ip_stop),
				'country'	=> db_escape($country),
				'asn'		=> intval($asn)
			];
			$ip2info_count++;
		}
		$added++;
	}
	unset($lines);
	echo PHP_EOL;
	
	$ip2info_count = simplify($ip2info);
	startTable('ip2info');
	$header = "INSERT INTO ip2info_new (ip_start, ip_stop, asn, country) VALUES ";
	$cur = 1;
	$batches = 0;
	$tmp = [];
	foreach ($ip2info AS $item)
	{
		progress($ip2info_count, $batches, 'ip2info');
		$tmp[] = " ('".$item['ip_start']."','".$item['ip_stop']."','".$item['asn']."','".$item['country']."')";
		if (count($tmp) >= $maxbatch)
		{
			get_db_rows($header.implode(',',$tmp));
			$tmp = [];
			$batches++;
		}
	}
	echo PHP_EOL;
	if (!empty($tmp))
		get_db_rows($header.implode(',',$tmp));
	stopTable('ip2info');

	endprog();
?>
