Subversion Repositories vgwhois

Rev

Rev 4 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
#!/usr/bin/php
2
<?php
3
 
4
#
5
#  generic Whois - Automatic Pattern Generator RIR-Statistics-Synchronisation-Tool
6
#
7
#  (c) 2012-2015 Daniel Marschall, ViaThinkSoft [www.viathinksoft.de]
8
#
9
#  Distribution, usage etc. pp. regulated by the current version of GPL.
10
#
11
#
12
#  Version 2015-04-14
13
#
14
 
15
error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);
16
 
17
require_once __DIR__ . '/config.inc.php';
18
require_once __DIR__ . '/rirs.inc.php';
19
 
20
define('RIRSTATS_CACHE_DIR', __DIR__ . '/../.cache/pattern-generator/rirstats');
21
 
22
function rir_get_md5_sum($url) {
23
	// MD5 (/var/opt/ftp/pub/apnic/stats/apnic/delegated-apnic-extended-latest) = 82c291bb5d4363a3db254853c1602777
24
	// MD5 (delegated-lacnic-extended-latest) = 2815f0b5837d5658acf1659dff98bb52
25
	// MD5 (delegated-ripencc-extended-latest) = d4caddde59952c44b584cacf720ef836
26
	// MD5 (delegated-arin-latest) = 3137d635d5e647481af972ddc87e5570
27
	// MD5 (delegated-afrinic-latest) = 9c1cd55e8894402062282e3ebdcf53c8             <-- WRONG FILE NAME SHOWN FOR delegated-afrinic-extended-latest.md5!
28
 
29
	$md5_cont = file_get_contents($url.'.md5');
30
 
31
	if (substr($md5_cont, 0, 5) != 'MD5 (') {
32
		// Some older *.md5 files of AfriNIC have following formats:
33
		// 73d5e32afd43eac0beb4635b6a9056c4  delegated-afrinic-latest
34
		// since 2012-06-25 AfriNIC uses the "normal" format
35
		// MD5 (delegated-afrinic-latest) = 9c1cd55e8894402062282e3ebdcf53c8
36
		// however, the filename is wrong (delegated-afrinic-latest instead of delegated-afrinic-extended-latest)
37
		return substr($md5_cont, 0, 32);
38
	} else {
39
		// Die anderen RIRs
40
		$tmp = explode(' = ', $md5_cont);
41
		return trim($tmp[1]);
42
	}
43
}
44
 
45
function rir_download_report($url, $outfile, $do_md5_check = true) {
46
	if (($do_md5_check) && (file_exists($outfile))) {
47
		$md5_ist  = md5_file($outfile);
48
		$md5_soll = rir_get_md5_sum($url);
49
		if ($md5_soll == $md5_ist) {
50
			touch("$outfile.success");
51
			return true;
52
		}
53
	}
54
 
55
	$cont = file_get_contents($url);
56
 
57
	if (!$cont) {
58
		touch("$outfile.fail");
59
		return false;
60
	}
61
 
62
	if ($do_md5_check) {
63
		$md5_ist = md5($cont);
64
		if (!isset($md5_soll)) $md5_soll = rir_get_md5_sum($url);
65
		if ($md5_soll != $md5_ist) {
66
			touch("$outfile.fail");
67
			return false;
68
		}
69
	}
70
 
71
	$h = fopen($outfile, 'w');
72
	if (!$h) return false;
73
	if (!fwrite($h, $cont)) {
74
		touch("$outfile.fail");
75
		return false;
76
	}
77
	fclose($h);
78
 
79
	touch("$outfile.success");
80
	return true;
81
}
82
 
83
$rirs[] = 'iana';
84
 
85
@mkdir(RIRSTATS_CACHE_DIR.'/', 0777, true);
86
 
87
foreach ($rirs as $rir) {
88
	$url = $rirstat_urls[$rir];
89
 
90
	$failcounter = 0;
91
	while (!rir_download_report($url, RIRSTATS_CACHE_DIR."/$rir", $rir != 'iana')) {
92
		$failcounter++;
93
		echo "Retry downloading $rir stats ($failcounter)...\n";
94
		if ($failcounter > 100) break;
95
		sleep(60);
96
	}
97
}