Subversion Repositories oidplus

Rev

Rev 236 | Rev 261 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 daniel-mar 1
#!/usr/bin/php
2
<?php
3
 
97 daniel-mar 4
require_once __DIR__ . '/../includes/oidplus.inc.php';
61 daniel-mar 5
 
6
OIDplus::init(false);
7
if (!defined('OIDINFO_API_URL')) {
8
	die("OIDINFO_API_URL not available (API is currently not public)\n");
9
}
10
 
11
define('VERBOSE', false);
12
define('DEFAULT_EMAIL', 'oidra@viathinksoft.de');
13
define('ALL_OID_LIST', '/home/oidplus/all_oids.txt'); // contains all OIDs (non-public file)
14
define('DESIRED_ROOT', '1.3.6.1.4.1.37476');
15
 
16
# ---
17
 
18
exec("cat ".escapeshellarg(ALL_OID_LIST)." | sort | grep '^".DESIRED_ROOT."'", $out, $ec);
19
 
20
foreach ($out as $oid) {
21
	$oid = trim($oid);
22
	if ($oid == '') continue;
23
 
24
	check_oid($oid);
25
}
26
 
27
function check_oid($oid) {
28
 
150 daniel-mar 29
	$res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array("oid:$oid"));
236 daniel-mar 30
	$ok = $res->num_rows() > 0;
61 daniel-mar 31
 
32
	if (!$ok) {
33
		echo "Not existing: $oid : Adding!\n";
34
		add_oid($oid);
35
	}
36
}
37
 
38
echo "OK\n";
39
 
40
# ---
41
 
42
function add_oid($oid, $root=DESIRED_ROOT) {
43
	if (VERBOSE) echo "Adding $oid ...\n";
44
	$data = ft_get_oid_data($oid);
45
 
46
	if (!isset($data['oid'])) {
47
		echo "Warning: Cannot gain information about OID $oid\n";
48
 
49
		$parent = ($oid == $root) ? '' : oid_up($oid);
50
		$title = '(tbd)';
51
		$description = '(tbd)';
52
		$ra_email = '';
53
		if ((DEFAULT_EMAIL != '') && ($ra_email == '')) $ra_email = DEFAULT_EMAIL;
54
 
150 daniel-mar 55
		sql_execute("insert into ".OIDPLUS_TABLENAME_PREFIX."objects (id, parent, title, description, ra_email) values (?, ?, ?, ?, ?)", array("oid:$oid", "oid:$parent", $title, $description, $ra_email));
61 daniel-mar 56
 
57
		return;
58
	}
59
 
60
	if (!isset($data['oid']['identifier'])) $data['oid']['identifier'] = array();
61
	foreach ($data['oid']['identifier'] as $identifier) {
150 daniel-mar 62
		sql_execute("insert into ".OIDPLUS_TABLENAME_PREFIX."asn1id (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 63
	}
64
 
65
	if (!isset($data['oid']['unicode-label'])) $data['oid']['unicode-label'] = array();
66
	foreach ($data['oid']['unicode-label'] as $identifier) {
150 daniel-mar 67
		sql_execute("insert into ".OIDPLUS_TABLENAME_PREFIX."iri (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 68
	}
69
 
70
	$parent = ($oid == $root) ? '' : oid_up($oid);
71
 
72
	$title = strip_tags(@$data['oid']['description']);
73
	$title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');
74
 
75
	$description = '<strong>' . trim(@$data['oid']['description']) . '</strong><br><br>' . nl2br(trim(@$data['oid']['information']));
76
	$description = str_replace(array("\r", "\n"), '', $description);
77
	$description = str_ireplace('<br />', '<br>', $description);
78
	$description = str_ireplace('<br/>', '<br>', $description);
79
	$description = '<p>' . str_ireplace('<br><br>', '</p><p>', $description) . '</p>';
80
 
81
	$ra_email = @$data['oid']['current_registrant']['email'];
82
	if ($ra_email == '') $ra_email = @$data['oid']['first_registrant']['email'];
83
 
150 daniel-mar 84
	$resx = OIDplus::db()->query("select ra_email from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array("oid:$parent"));
236 daniel-mar 85
	if ($rowx = $resx->fetch_array()) $ra_email = $rowx['ra_email'];
61 daniel-mar 86
 
87
	$created = @$data['oid']['first_registrant']['creation-date'];
88
	$updated = @$data['oid']['last-modification'];
89
 
150 daniel-mar 90
	sql_execute("insert into ".OIDPLUS_TABLENAME_PREFIX."objects (id, parent, title, description, ra_email, created, updated) values (?, ?, ?, ?, ?, ?, ?)",
91
		array("oid:$oid", "oid:$parent", $title, $description, $ra_email, $created, $updated));
61 daniel-mar 92
}
93
 
94
function ft_get_oid_data($oid) {
95
	$url = OIDINFO_API_URL . '&oid='.urlencode($oid);
96
	$cont_json = @file_get_contents($url);
97
	if (!$cont_json) {
98
		sleep(5);
99
                $cont_json = @file_get_contents($url);
100
                if (!$cont_json) return false;
101
	}
102
	$data = json_decode($cont_json,true);
103
 
104
	return $data;
105
}
106
 
150 daniel-mar 107
function sql_execute($sql, $prep=null) {
61 daniel-mar 108
#	echo "$sql\n";
237 daniel-mar 109
	try {
110
		OIDplus::db()->query($sql, $prep);
111
	} catch (Exception $e) {
112
		echo "Warning: " . $e->getMessage() . "\n";
113
	}
61 daniel-mar 114
}