Subversion Repositories oidplus

Rev

Rev 261 | Rev 557 | 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
 
511 daniel-mar 4
/*
5
 * OIDplus 2.0
6
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
 
97 daniel-mar 21
require_once __DIR__ . '/../includes/oidplus.inc.php';
61 daniel-mar 22
 
23
OIDplus::init(false);
261 daniel-mar 24
if (!OIDplus::baseConfig()->exists('OIDINFO_API_URL')) {
61 daniel-mar 25
	die("OIDINFO_API_URL not available (API is currently not public)\n");
26
}
27
 
28
define('VERBOSE', false);
29
define('DEFAULT_EMAIL', 'oidra@viathinksoft.de');
30
define('ALL_OID_LIST', '/home/oidplus/all_oids.txt'); // contains all OIDs (non-public file)
31
define('DESIRED_ROOT', '1.3.6.1.4.1.37476');
32
 
33
# ---
34
 
35
exec("cat ".escapeshellarg(ALL_OID_LIST)." | sort | grep '^".DESIRED_ROOT."'", $out, $ec);
36
 
37
foreach ($out as $oid) {
38
	$oid = trim($oid);
39
	if ($oid == '') continue;
40
 
41
	check_oid($oid);
42
}
43
 
44
function check_oid($oid) {
45
 
261 daniel-mar 46
	$res = OIDplus::db()->query("select * from ###objects where id = ?", array("oid:$oid"));
236 daniel-mar 47
	$ok = $res->num_rows() > 0;
61 daniel-mar 48
 
49
	if (!$ok) {
50
		echo "Not existing: $oid : Adding!\n";
51
		add_oid($oid);
52
	}
53
}
54
 
55
echo "OK\n";
56
 
57
# ---
58
 
59
function add_oid($oid, $root=DESIRED_ROOT) {
60
	if (VERBOSE) echo "Adding $oid ...\n";
61
	$data = ft_get_oid_data($oid);
62
 
63
	if (!isset($data['oid'])) {
64
		echo "Warning: Cannot gain information about OID $oid\n";
65
 
66
		$parent = ($oid == $root) ? '' : oid_up($oid);
67
		$title = '(tbd)';
68
		$description = '(tbd)';
69
		$ra_email = '';
70
		if ((DEFAULT_EMAIL != '') && ($ra_email == '')) $ra_email = DEFAULT_EMAIL;
71
 
261 daniel-mar 72
		sql_execute("insert into ###objects (id, parent, title, description, ra_email) values (?, ?, ?, ?, ?)", array("oid:$oid", "oid:$parent", $title, $description, $ra_email));
61 daniel-mar 73
 
74
		return;
75
	}
76
 
77
	if (!isset($data['oid']['identifier'])) $data['oid']['identifier'] = array();
78
	foreach ($data['oid']['identifier'] as $identifier) {
261 daniel-mar 79
		sql_execute("insert into ###asn1id (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 80
	}
81
 
82
	if (!isset($data['oid']['unicode-label'])) $data['oid']['unicode-label'] = array();
83
	foreach ($data['oid']['unicode-label'] as $identifier) {
261 daniel-mar 84
		sql_execute("insert into ###iri (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 85
	}
86
 
87
	$parent = ($oid == $root) ? '' : oid_up($oid);
88
 
89
	$title = strip_tags(@$data['oid']['description']);
90
	$title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');
91
 
92
	$description = '<strong>' . trim(@$data['oid']['description']) . '</strong><br><br>' . nl2br(trim(@$data['oid']['information']));
93
	$description = str_replace(array("\r", "\n"), '', $description);
94
	$description = str_ireplace('<br />', '<br>', $description);
95
	$description = str_ireplace('<br/>', '<br>', $description);
96
	$description = '<p>' . str_ireplace('<br><br>', '</p><p>', $description) . '</p>';
97
 
98
	$ra_email = @$data['oid']['current_registrant']['email'];
99
	if ($ra_email == '') $ra_email = @$data['oid']['first_registrant']['email'];
100
 
261 daniel-mar 101
	$resx = OIDplus::db()->query("select ra_email from ###objects where id = ?", array("oid:$parent"));
236 daniel-mar 102
	if ($rowx = $resx->fetch_array()) $ra_email = $rowx['ra_email'];
61 daniel-mar 103
 
104
	$created = @$data['oid']['first_registrant']['creation-date'];
105
	$updated = @$data['oid']['last-modification'];
106
 
261 daniel-mar 107
	sql_execute("insert into ###objects (id, parent, title, description, ra_email, created, updated) values (?, ?, ?, ?, ?, ?, ?)",
150 daniel-mar 108
		array("oid:$oid", "oid:$parent", $title, $description, $ra_email, $created, $updated));
61 daniel-mar 109
}
110
 
111
function ft_get_oid_data($oid) {
261 daniel-mar 112
	$url = OIDplus::baseConfig()->getValue('OIDINFO_API_URL') . '&oid='.urlencode($oid);
61 daniel-mar 113
	$cont_json = @file_get_contents($url);
114
	if (!$cont_json) {
115
		sleep(5);
116
                $cont_json = @file_get_contents($url);
117
                if (!$cont_json) return false;
118
	}
119
	$data = json_decode($cont_json,true);
120
 
121
	return $data;
122
}
123
 
150 daniel-mar 124
function sql_execute($sql, $prep=null) {
61 daniel-mar 125
#	echo "$sql\n";
237 daniel-mar 126
	try {
127
		OIDplus::db()->query($sql, $prep);
128
	} catch (Exception $e) {
129
		echo "Warning: " . $e->getMessage() . "\n";
130
	}
61 daniel-mar 131
}