#!/usr/bin/php
<?php

#
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
#  Maintenance / Developer utilities
#
#  (c) 2012-2025 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
#
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
#

error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);

require_once __DIR__ . '/config.inc.php';
require_once __DIR__ . '/rirs.inc.php';

function rir_get_md5_sum($url) {
	// As of 16 June 2025, the RIRs are outputting the following contents for "delegated-...-extended-latest.md5"
	// MD5 (delegated-afrinic-extended-latest) = a8049729882f34dc158da987508b5806
	// MD5 (delegated-apnic-extended-latest) = b008ab8e4c22e2537ce04b409c7f7cea
	// 6d23ed5278c9d21741b1595f4369f0a1  delegated-arin-extended-20250615
	// MD5 (delegated-lacnic-extended-latest) = 1c3553246b034ec19a743cf88d71e109
	// MD5 (delegated-ripencc-extended-latest) = 45417d14f60629c09aad1aeef6de8890

	$options = [
	    "http" => [
	        "method" => "GET",
	        "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) " .
	                    "AppleWebKit/537.36 (KHTML, like Gecko) " .
	                    "Chrome/114.0.0.0 Safari/537.36\r\n"
	    ]
	];
	$context = stream_context_create($options);
	$md5_cont = file_get_contents($url.'.md5', false, $context);

	if (substr($md5_cont, 0, 5) != 'MD5 (') {
		return substr($md5_cont, 0, 32);
	} else {
		return trim(explode(' = ', $md5_cont)[1] ?? '');
	}
}

function rir_download_report($url, $outfile, $do_md5_check = true) {
	if (($do_md5_check) && (file_exists($outfile))) {
		$md5_ist  = md5_file($outfile);
		$md5_soll = rir_get_md5_sum($url);
		if ($md5_soll == $md5_ist) {
			@unlink("$outfile.fail");
			@touch("$outfile.success");
			return true;
		}
	}

	$options = [
	    "http" => [
	        "method" => "GET",
	        "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) " .
	                    "AppleWebKit/537.36 (KHTML, like Gecko) " .
	                    "Chrome/114.0.0.0 Safari/537.36\r\n"
	    ]
	];
	$context = stream_context_create($options);
	$cont = file_get_contents($url, false, $context);

	if (!$cont) {
		echo "$url: Download fail\n";
		@unlink("$outfile.success");
		//@touch("$outfile.fail");
		file_put_contents("$outfile.fail", "$url: Download fail\n");
		return false;
	}

	if ($do_md5_check) {
		$md5_ist = md5($cont);
		if (!isset($md5_soll)) $md5_soll = rir_get_md5_sum($url);
		if ($md5_soll != $md5_ist) {
			echo "$url : MD5 fail (calculated $md5_ist, should be $md5_soll)\n";
			@unlink("$outfile.success");
			//@touch("$outfile.fail");
			file_put_contents("$outfile.fail", "$url: Download fail\n");
			return false;
		}
	}

	$h = fopen($outfile, 'w');
	if (!$h) return false;
	if (!fwrite($h, $cont)) {
		echo "Write $url to $outfile failed\n";
		@unlink("$outfile.success");
		//@touch("$outfile.fail");
		file_put_contents("$outfile.fail", "Write $url to $outfile failed\n");
		return false;
	}
	fclose($h);

	@unlink("$outfile.fail");
	@touch("$outfile.success");
	return true;
}

$rirs[] = 'iana';

@mkdir(RIRSTATS_CACHE_DIR.'/', 0777, true);

foreach ($rirs as $rir) {
	if (!isset($rirstat_urls[$rir])) continue;

	$url = $rirstat_urls[$rir];

	$failcounter = 0;
	while (!rir_download_report($url, RIRSTATS_CACHE_DIR."/$rir", $rir != 'iana')) {
		$failcounter++;
		echo "Retry downloading $rir stats ($failcounter)...\n";
		if ($failcounter > 100) break;
		sleep(60);
	}
}
